Search code examples
emacsevil-modekeymaps

How can I add a keymap to the emulation-mode-map-alists? Emacs


I have made a keymap and added it to a minor mode:

(defvar my-keymap (make-sparse-keymap))
(progn
    (define-key my-keymap (kbd "C-c s") '(lambda() (interactive) (message "Hello World")))
)

(define-minor-mode my-keybindings-mode
    nil
    :global t
    :lighter " keys"
    :keymap my-keymap)

(add-to-list emulation-mode-map-alists '(my-keybindings-mode . my-keymap))

However, whenever I try to add it to the emulation-mode-map-alists by writing:

(add-to-list emulation-mode-map-alists '(my-keybindings-mode . my-keymap))

I end up getting this error:

eval-region: Wrong type argument: symbolp, (evil-mode-map-alist)


Solution

  • However, whenever I try to add it to the emulation-mode-map-alists by writing:

    (add-to-list emulation-mode-map-alists '(my-keybindings-mode . my-keymap))
    

    I end up getting this error:

    Wrong type argument: symbolp, (evil-mode-map-alist)
    

    That's because the first argument to add-to-list should be a symbol (quoted), like this:

    (add-to-list 'emulation-mode-map-alists ...)
    

    Without that quote you're instead passing the evaluated value of the emulation-mode-map-alists variable.

    Note that C-hf add-to-list tells you that it's a function, and in particular note that when any function is called, all of its arguments are evaluated. This in turns tells you that in order to pass a symbol as an argument, you will need to quote it.

    (Macros and special forms are trickier -- their arguments aren't evaluated automatically, but they might be choosing to evaluate some of them explicitly, so you always need to pay attention to the documentation to be sure of what you should be passing them. Functions are nice and consistent in this respect, however.)