DRY way of remapping a key in all relevant modes

Michael Markert markert.michael at googlemail.com
Sat Sep 22 15:25:35 CEST 2012


On Sat, Sep 22 2012 (10:22), Patrick Brinich-Langlois <pbrinichlanglois at gmail.com> wrote: 

[14 lines snipped]

> I tried to write some Elisp to do this, but it doesn't work (Wrong type
> argument):
>
> (defun remap-all (char action states)
> (if states (progn (define-key (car states) char action)
> (remap-all char action (cdr states)))))
> (setq states '(evil-normal-state-map evil-visual-state-map))
> (remap-all "i" 'evil-forward-char states)

The problem is that you pass a list of symbols (e.g. (list
'evil-normal-state-map ...)) instead of a list of keymaps. Use (list
evil-normal-state-map ...) to build such a list.

Here's another approach, as recursion is not a fine choice for elisp:

    (defun set-in-all-evil-states (key def &optional maps)
      (unless maps
        (setq maps (list evil-normal-state-map
                         evil-visual-state-map
                         evil-insert-state-map
                         evil-emacs-state-map)))
      (while maps
        (define-key (pop maps) key def)))
        
    ;; usage

    (set-in-all-evil-states "i" 'evil-forward-char)

    ;; or

    (set-in-all-evil-states "i" 'evil-forward-char (list evil-normal-state-map evil-visual-state-map))
    
HTH,
Michael
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
Url : https://lists.ourproject.org/pipermail/implementations-list/attachments/20120922/de182c34/attachment.pgp 


More information about the implementations-list mailing list