I noticed a pattern in some elisp modes I was putting together:
(let ((map (make-sparse-keymap)))
(define-key map KEY 'FN)
...
(setq FOO map))
so I wrote up the following macro
(defmacro def-sparse-map (name &rest key/fn-list)
`(let ((map (make-sparse-keymap)))
,@(loop for (key fn) on key/fn-list by #'cddr
collecting `(define-key map ,key ',fn))
(setq ,name map)))
which lets me write
(def-sparse-map FOO
KEY FN
...)
instead. All comments welcome, but some specific questions are
- Can this be done more cleanly (and more generally, is it acceptable practice to use the ported CL functions when defining Elisp modes)?
- Are there some issues I'm not seeing with that use of
let
/setq
? - Is it worth it writing up an elisp
with-gensyms
to keepmap
from being bound externally?
and most importantly
- Is there an Elisp primitive that does the same thing, or close to it?