DRY way of remapping a key in all relevant modes

Michael Markert markert.michael at googlemail.com
Sun Sep 23 02:38:30 CEST 2012


On Sun, Sep 23 2012 (01:47), Patrick Brinich-Langlois <pbrinichlanglois at gmail.com> wrote: 
>
> Thanks, Michael! That does help. It doesn't affect the bindings in Dired, though.
> Do I have to define those bindings specifically? For example,
>
> (evil-define-key 'normal dired-mode-map "i" 'evil-forward-char)
>
> Or is there a better way?

Well you can certainly pack it into the function (I made also some other
adjustments):


     (defun set-in-all-evil-states (key def &optional maps additional)
       (let ((maps (or maps (list evil-normal-state-map
                                  evil-visual-state-map
                                  evil-insert-state-map
                                  evil-emacs-state-map))))
         (dolist (map maps)
           (define-key map key def))
         (when additional
           (dolist (pair additional)
             (let ((state (first pair))
                   (map (second pair)))
               (evil-define-key state map key def))))))
               
although the interface gets quite messy as you have to encode the states
you want to bind:

    (set-in-all-evil-states "i" 'evil-forward-char nil (list (list 'normal
                                                                 dired-mode-map)
                                                           (list 'insert
                                                                 dired-mode-map)))
                                                             
You could admit symbols for the maps as well for a more concise one:

    (set-in-all-evil-states "i" 'evil-forward-char nil '((normal dired-mode-map)
                                                       (insert dired-mode-map)))

     (defun set-in-all-evil-states (key def &optional maps additional)
       (let ((maps (or maps (list evil-normal-state-map
                                  evil-visual-state-map
                                  evil-insert-state-map
                                  evil-emacs-state-map))))
         (dolist (map maps)
           (define-key map key def))
         (when additional
           (dolist (pair additional)
             (let ((state (first pair))
                   (map (second pair)))
               (evil-define-key state (if (symbolp map)
                                          (symbol-value map)
                                        map)
                 key def))))))
                 
Of course you can tinker any further with it.

Have fun,
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/20120923/e253e30c/attachment.pgp 


More information about the implementations-list mailing list