0

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)

Drew
  • 29,895
  • 7
  • 74
  • 104
Ali Awan
  • 180
  • 10
  • 1
    You can remove the quote before your lambda. – Drew May 10 '21 at 23:21
  • Do you have the same problem when you start Emacs with `emacs -Q` (no init file)? If not, bisect your init file to find the culprit. – Drew May 10 '21 at 23:22

1 Answers1

2

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

phils
  • 71,335
  • 11
  • 153
  • 198
  • 1
    This related Q&A might also be helpful? https://stackoverflow.com/questions/1780838/difference-between-symbol-and-variable-name-in-emacs-lisp – phils May 11 '21 at 05:50