I have globally assigned C-c/ to ace-jump-mode but reftex-mode (a minor mode for citations used with AucTeX) overrides this key with some function I never use.
I tried local-unset-key but it only unbinds keys from the current major mode's map.
How do I remove C-c/ from reftex-mode-map without making changes to reftex.el?
You can change an existing key map using define-key. By passing nil as the function to call, the key will become unbound. I guess that you should be able to do something like:
(define-key reftex-mode-map "\C-c/" nil)
Of course, you should do this in some kind of hook, for example:
(defun my-reftex-hook ()
(define-key reftex-mode-map "\C-c/" nil))
(add-hook 'reftex-mode-hook 'my-reftex-hook)
You can use following command:
(define-key reftex-mode-map "\C-c/" nil)
to unmap this function from C-c /... But reftex-mode should be loaded, so reftex-mode-map will available for modification
This is how I do it. It could be improved, though.
(defun get-key-combo (key)
"Just return the key combo entered by the user"
(interactive "kKey combo: ")
key)
(defun keymap-unset-key (key keymap)
"Remove binding of KEY in a keymap
KEY is a string or vector representing a sequence of keystrokes."
(interactive
(list (call-interactively #'get-key-combo)
(completing-read "Which map: " minor-mode-map-alist nil t)))
(let ((map (rest (assoc (intern keymap) minor-mode-map-alist))))
(when map
(define-key map key nil)
(message "%s unbound for %s" key keymap))))
;;
;; Then use it interativly
;; Or like this:
(keymap-unset-key '[C-M-left] "paredit-mode")
..
..
Related
If I want to make my own function which among other thing calls wg-save (workgroups.el - save workgroups) then I do something like this:
(defun foo ()
(interactive)
...
(call-interactively 'wg-save)
)
(global-set-key (kbd "my binding") 'foo)
What about the following scenario (I will use eyebrowse.el as an example):
eyebrowse uses C-c C-w 'number' to move to different window configurations e.g. C-c C-w 1 to move to 1 or C-c C-w 2 to move to 2.
How can I write a similar function like the 'foo' since now I need to pass to 'call-interactively' a 'number' as parameter?
EDIT: C-c C-w 1 calls eyebrowse-switch-to-window-config-1.
So I need to make a 'foo' function like the above that will 'call-interactively'
'eyebrowse-switch-to-window-config-1' when the key binding is 'C-c C-w 1', 'eyebrowse-switch-to-window-config-2' when the key binding is 'C-c C-w 2' etc.
Something like the following (if it makes sense):
(defun foo ()
(interactive)
...
(call-interactively 'eyebrowse-switch-to-window-config-"number")
)
(global-set-key (kbd "C-c C-w 'number'") 'foo)
From the documentation, read with C-h f call-interactively RET:
Optional third arg KEYS, if given, specifies the sequence of events to
supply, as a vector, if the command inquires which events were used to
invoke it. If KEYS is omitted or nil, the return value of
`this-command-keys-vector' is used.
So to pass arguments to call-interactively, give it a vector with the arguments, like so
(call-interactively 'my-fn t (vector arg1 arg2))
That way you don't need to call eyebrowse-switch-to-config-window-nwhere n is a number, you call the function they rely on, eyebrowse-switch-to-window-config, and give it a number as argument.
You can get a number for argument like this: (see help of "interactive" )
(defun foo (arg)
(interactive "nWindow? ")
(call-interactively 'my-fn t (vector arg))
)
But did you read the source of eyebrowse ? It will give ideas.
(defun eyebrowse-switch-to-window-config-0 ()
"Switch to window configuration 0."
(interactive)
(eyebrowse-switch-to-window-config 0))
You can do something like this I believe:
(defun foo (NUM)
"Enter the NUM of the screen to go to.
If the key pressed is not a number go to screen 0."
(interactive
(list (read-key-sequence "Which window: ")))
((eyebrowse-switch-to-window-config 3) (string-to-number NUM)))
The key is the read-key-sequence function
To save time and space, you can use a loop to define all the keybindings you'll use.
For example, for eyebrowse instead of defining each keybinding you could do this instead:
(load-library "eyebrowse")
(dotimes (i 10)
(global-set-key
(kbd (concat "C-c C-w " (number-to-string i)))
`(lambda ()
(interactive)
(eyebrowse-switch-to-window-config ,i))))
Sorry, I tried few times and I can't build simple defmacro - can anybody help me with that:
So, I want define macro which will expand to:
(define-key keymap "*" (lambda ()
(interactive)
(some-func "*")))
I tried something like:
(defmacro wrap-and-define-key (keymap key func)
`(define-key ,keymap ,key (lambda () (interactive) (,func ,key))))
And it works with:
(wrap-and-define-key keymap key func)
but doesn't work with
(wrap-and-define-key keymap key 'func)
How to change defmacro statement to make last form working? Thanks!
You don't need a macro for this.
(defun wrap-and-define-key (keymap key func)
(define-key keymap key (lambda () (interactive) (funcall func key))))
(Don't quote lambda forms. Avoid eval.)
Not sure why you want this, but anyway, this should do what you ask.
The reason your macro doesn't work for the second example is 'func is passed as the argument with the quote, since arguments to macros are not evaluated automatically. So the lambda in the macro expands to:
(lambda () (interactive) ('func key))
But you cannot quote a function name when it is invoked in this way. Only a lambda or an unquoted symbol can appear as the first item in a function invocation expression. If you change the macro to this:
(defmacro wrap-and-define-key (keymap key func)
`(define-key ,keymap ,key (lambda () (interactive) (funcall ,func ,key))))
It will work. When the lambda is invoked, 'func gets evaluated to the plain func symbol and passed to funcall. funcall then looks up the symbol in the function namespace (Elisp is a Lisp-2) and invokes it.
As suggested in another answer, you can accomplish this with a function. This function will work regardless of whether lexical mode is enabled:
(defun wrap-and-define-key (keymap key func)
(define-key keymap key `(lambda () (interactive) (funcall ',func ,key))))
Ok, find something working but not sure - that is elegant solution:
(defmacro wrap-and-define-key (keymap key func)
`(define-key ,keymap ,key '(lambda () (interactive) (eval (list ,func ,key)))))
I have buffers named *terminal<1>*, *terminal<2>*, *terminal<3>*, etc. Is there a way to bind a key combination that will take an argument for the number? That is, I want to bind C-c 1 to switch to *terminal<1>* and C-c 2 to switch to *terminal<2>*, and so on. If I can't do this directly, is it possible to do metaprogramming in Elisp that will define all the functions for me?
In this suggestion, the interactive switch-to-terminal will take either a prefix argument C-u 2 for example, or prompt the user.
The macro then makes setting up the key bindings a little easier.
Finally I bind C-c 1 to C-c 4 to switch to *terminal<1>* to *terminal<4>*.
(defun switch-to-terminal (buf-num)
(interactive "NNumber of buffer to visist: ")
(let* ((buf-name (format "*terminal<%d>*" buf-num))
(buf (get-buffer buf-name)))
(unless buf
(error "No buffer %s" buf-name))
(switch-to-buffer buf)))
(defmacro bind-switch-to-terminal (num)
`(global-set-key
,(kbd (format "C-c %d" num))
(lambda ()
(interactive)
(switch-to-terminal ,num))))
(bind-switch-to-terminal 1)
(bind-switch-to-terminal 2)
(bind-switch-to-terminal 3)
(bind-switch-to-terminal 4)
This change uses the same switch-to-terminal function, but replaces bind-switch-to-terminal with a function. The lexical-let* allows for the creation of a closure to create unique terminal switching functions, the dotimes loop then binds for C-c 1 to C-c 9.
(defun bind-switch-to-terminal (num)
(lexical-let* ((buf-num num)
(switch-func
(lambda ()
(interactive)
(switch-to-terminal buf-num))))
(global-set-key
(kbd (format "C-c %d" buf-num))
switch-func)))
(dotimes (num 9)
(bind-switch-to-terminal (1+ num)))
You can bind keys as usual:
(global-set-key (kbd "C-c 1") (lambda ()
(interactive)
(switch-to-buffer "*terminal<1>*")))
To create all the shortcuts from 1 to 9 we would use macros.
edit: This buggy version may put you on tracks. I give up :(
(defmacro gototerminal (count)
`(global-set-key (kbd ,(concat "C-c " (number-to-string count)))
;; with the comma I want to evaluate what is inside concat
(lambda () (interactive)
(switch-to-buffer (concat "*terminal<" ,count ">*"))))
)
(progn (setq count 1)
(while (< count 10)
(gototerminal count)
(setq count (1+ count))
))
ps: an elisp debugger is edebug. Set it with C-u C-M-x
I would write a function that calls interactive with an n argument which indicates that the function reads a number from the mini buffer:
(defun test (x)
(interactive "nNumber of buffer to visit: ")
(message (concat "received number: " (number-to-string x))))
Binding this to a key will let you enter a number in the mini buffer.
Another way is to use a numerical argument:
(defun test (x)
(interactive "P")
(message (concat "received number: " (number-to-string x))))
Say you bind this function to C-c c, you can then pass it the number 2 as an argument by pressing C-u 2 C-c c.
If you avoid the usage of an existing prefix key like C-c you can trigger a command with one keypress e.g. F9.
This command can have a single key as input.
Example:
(defun test (k)
(interactive "K")
(message "Pressed key: %d" (- (aref k 0) ?0)))
(local-set-key [f9] 'test)
Here's a minimal snippet to get things going:
(define-prefix-command 'foo)
(define-key foo "a" 'bar)
(define-key foo "b" 'baz)
(global-set-key (kbd "C-M-r") 'foo)
Now I can "call" the foo keymap when I press C-M-r.
But I wonder how I can do this from code, e.g. something like:
(funcall (lambda-from-keymap 'foo))
After this call, I expect the focus to be in the minibuffer, expecting either
a, or b or C-h to be entered.
Is something like this possible?
You can use read-key-sequence and lookup-key to implement this:
(defun call-keymap (map &optional prompt)
"Read a key sequence and call the command it's bound to in MAP."
;; Note: MAP must be a symbol so we can trick `describe-bindings' into giving
;; us a nice help text.
(let* ((overriding-local-map `(keymap (,map . ,map)))
(help-form `(describe-bindings ,(vector map)))
(key (read-key-sequence prompt))
(cmd (lookup-key map key t)))
(if (functionp cmd) (call-interactively cmd)
(user-error "%s is undefined" key))))
If you hit C-h read-key-sequence still waits for you to complete the sequence. I think you could simulate Emacs' normal behaviour by looping with read-key instead, it's a bit more involved though.
Use it like this:
(defun bar () (interactive) (message "you called bar"))
(defun baz () (interactive) (message "you called baz"))
(define-prefix-command 'foo)
(define-key foo "a" 'bar)
(define-key foo "b" 'baz)
(global-set-key (kbd "C-M-r") 'foo)
(defun call-foo ()
(interactive)
;; Note: pass the symbol form of the keymap so we can give nice help
(call-keymap 'foo "enter a foo command: "))
(global-set-key (kbd "C-c f") 'call-foo)
If the keymap is bound to a key sequence, you can invoke it by emulating the exact key sequence by setting unread-command-events:
(setq unread-command-events
(mapcar (lambda (e) `(t . ,e))
(listify-key-sequence (kbd "C-M-r"))
You need to have foo interactive. I did so using:
(global-set-key (kbd "C-M-r") (lambda () (interactive) ( foo)))
This should fix your problem.
all.
I decided to hack iswitchb this morning, and found a confusing thing.
Usually,when we command iswitchb,we got someting in minibuffer like:
iswitch {buffer1,buffer2 ...}
What in braces is the completions, as we typing its number
is shrinking.
And I didn't find how iswitchb achieved this when hacking on
its code (sorry for my dullness ).
This is original iswitchb-read-buffer with doc-string ripped
off:
(defun iswitchb-read-buffer (prompt &optional default require-match
start matches-set)
(let
(
buf-sel
iswitchb-final-text
(icomplete-mode nil) ;; prevent icomplete starting up
)
(iswitchb-define-mode-map)
(setq iswitchb-exit nil)
(setq iswitchb-default
(if (bufferp default)
(buffer-name default)
default))
(setq iswitchb-text (or start ""))
(unless matches-set
(setq iswitchb-rescan t)
(iswitchb-make-buflist iswitchb-default)
(iswitchb-set-matches))
(let
((minibuffer-local-completion-map iswitchb-mode-map)
;; Record the minibuffer depth that we expect to find once
;; the minibuffer is set up and iswitchb-entryfn-p is called.
(iswitchb-minibuf-depth (1+ (minibuffer-depth)))
(iswitchb-require-match require-match))
;; prompt the user for the buffer name
(setq iswitchb-final-text (completing-read
prompt ;the prompt
'(("dummy" . 1)) ;table
nil ;predicate
nil ;require-match [handled elsewhere]
start ;initial-contents
'iswitchb-history)))
(if (and (not (eq iswitchb-exit 'usefirst))
(get-buffer iswitchb-final-text))
;; This happens for example if the buffer was chosen with the mouse.
(setq iswitchb-matches (list iswitchb-final-text)
iswitchb-virtual-buffers nil))
;; If no buffer matched, but a virtual buffer was selected, visit
;; that file now and act as though that buffer had been selected.
(if (and iswitchb-virtual-buffers
(not (iswitchb-existing-buffer-p)))
(let ((virt (car iswitchb-virtual-buffers))
(new-buf))
;; Keep the name of the buffer returned by find-file-noselect, as
;; the buffer 'virt' could be a symlink to a file of a different name.
(setq new-buf (buffer-name (find-file-noselect (cdr virt))))
(setq iswitchb-matches (list new-buf)
iswitchb-virtual-buffers nil)))
;; Handling the require-match must be done in a better way.
(if (and require-match
(not (iswitchb-existing-buffer-p)))
(error "Must specify valid buffer"))
(if (or (eq iswitchb-exit 'takeprompt)
(null iswitchb-matches))
(setq buf-sel iswitchb-final-text)
;; else take head of list
(setq buf-sel (car iswitchb-matches)))
;; Or possibly choose the default buffer
(if (equal iswitchb-final-text "")
(setq buf-sel (car iswitchb-matches)))
buf-sel))
And this is the part of iswitchb-read buffer,which I thought
is responsible for functioning completion mechanism.
(defun iswitchb-read-buffer (prompt &optional default require-match
start matches-set)
(let
(
(iswitchb-minibuf-depth (1+ (minibuffer-depth)))
)
;; prompt the user for the buffer name
(completing-read
prompt ;the prompt
'(("dummy" . 1)) ;table
nil ;predicate
nil ;require-match [handled elsewhere]
start ;initial-contents
'iswitchb-history)))
Eval
(iswitchb-read-buffer "Test: ")
resulting
Test: {buffer1,buffer2,...}
So, I think I'm right.
So,what confused me is how can sexp:
(iswitchb-minibuf-depth (1+ (minibuffer-depth)))
has effect on what echos in minibuffer. Comment this
sexp,or replace iswitchb-minibuffer-depth with another
variable, the completions will disappear.
Any advice?
This variable is used in iswitchb-entryfn-p which is called from iswitchb-minibuffer-setup
(defun iswitchb-minibuffer-setup ()
"Set up minibuffer for `iswitchb-buffer'.
Copied from `icomplete-minibuffer-setup-hook'."
(when (iswitchb-entryfn-p)
(set (make-local-variable 'iswitchb-use-mycompletion) t)
(add-hook 'pre-command-hook 'iswitchb-pre-command nil t)
(add-hook 'post-command-hook 'iswitchb-post-command nil t)
(run-hooks 'iswitchb-minibuffer-setup-hook)))
When iswitchb-minibuf-depth is null then iswitchb-entryfn-p is null and the setup is not done.
The iswitchb-minibuffer-setup is a hook which is added to the iswitchb-mode.
BTW, while this may not directly answer your question, this part of iswitchb's behavior is also provided by icomplete-mode (for the normal completion code).