remap emacs key - emacs

I would like to remap a key in emacs, for example { and i would it to trigger usual event.
Example, i remapped alt + ( to get { and i would like, when i use a plugin like autopair to give me the associated }.
I tried this :
(global-set-key [(alt \()] ( lambda () (interactive) (insert "{") ))
It inserts { correctly but does not trigger autopair hooks.
Is there a way to achieve it ?

Try (not tested):
(define-key key-translation-map (kbd "M-(") (kbd "{"))

Related

Is there a way to make <menu> a prefix key in emacs?

I'm trying to get vimish-fold to work, and it does work. The problem is that I want to bind it to the menu key instead of the M key.
(progn
(define-prefix-command 'my-menu-key-map)
(define-key vimish-fold-mode (kdb "v f") 'vimish-fold)
(define-key vimish-fold-mode (kdb "v u") 'vimish-fold-refold)
)
(global-set-key (kdb "<menu>") my-menu-key-map)
I essentially want menu v f -> vimish-fold and menu v u -> vimish-fold-refold. The problem I'm having is it's giving me an error telling me I have the wrong type argument.
Error in private config: init.el, (wrong-type-argument keymapp nil)
You have some repeated typos:
(kdb should be (kbd
(define-key vimish-fold-mode should be (define-key my-menu-key-map

Elisp: call keymap from code?

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.

How to disable a mode specified key-binding in Emacs [duplicate]

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")
..
..

Moving the cursor inside brackets and intent when pressing RETURN

Let's see if I achieve to explain it. When I type this in emacs 24:
int foo() {|}
Note: | = Cursor
And press the Return key, I get the next output:
int foo() {
|}
So, my question is: how can I achieve the next behaviour?
int foo() {
|
}
Instead of global-set-key you should probably use something like (define-key 'c++-mode-map ..., but here's the basics.
(defun newline-and-push-brace ()
"`newline-and-indent', but bracket aware."
(interactive)
(insert "\n")
(when (looking-at "}")
(insert "\n")
(indent-according-to-mode)
(forward-line -1))
(indent-according-to-mode))
(global-set-key (kbd "RET") 'newline-and-push-brace)
You could define a function that checks if you're in that situation and does what you want if you are, and otherwise just calls whatever the newline command for your major mode is, e.g.:
(defun brackets-newline (point)
(interactive "d")
(setq next-char (char-before point))
(if (and next-char
(char-equal next-char 123))
;; if we are sitting in front of a close bracket, do what you want
(progn
(newline)
(newline)
(previous-line)
;;call whatever "TAB" is in this mode
(funcall (key-binding (kbd "TAB"))))
;; otherwise just insert a newline
(newline)))
Then bind this to (kbd "RET")
There may be a better way to do this using defadvice or some such, this seemed to work pretty well for me though.

How to I remap the Emacs command M-d into the macro M-b, M-d?

I would like the "delete to end of word" command to delete the word, regardless of cursor position.
(defun my-kill-word ()
(interactive)
(backward-word)
(kill-word 1))
(global-set-key (kbd "M-d") 'my-kill-word)
A better code could be:
(defun my-kill-word ()
(interactive)
(unless (looking-at "\\<")
(backward-word))
(kill-word 1))
(global-set-key (kbd "M-d") 'my-kill-word)
So we move backward only if we are not at the beginning of the word
yet.