I am trying to execute the code here, which I copy below
(defun buffer-local-set-key (key func)
(interactive "KSet key on this buffer: \naCommand: ")
(let ((name (format "%s-magic" (buffer-name))))
(eval
`(define-minor-mode ,(intern name)
"Automagically built minor mode to define buffer-local keys."))
(let* ((mapname (format "%s-map" name))
(map (intern mapname)))
(unless (boundp (intern mapname))
(set map (make-sparse-keymap)))
(eval
`(define-key ,map ,key func)))
(funcall (intern name) t)))
However, when executing it like this
(buffer-local-set-key "C-x k" #'previous-buffer)
I am getting an error eval: Symbol’s value as variable is void: func
. I suspect it is at the line (eval `(define-key ,map ,key func)))
I understand what the code is doing, but I am not sure why eval
is being used here instead of the function being called directly, i.e. (define-key map key func)
. However, when I tried doing that instead I got the error
Wrong type argument: keymapp, buf-magic-map
Can someone explain to me why eval
might be needed here? And how to fix the error above?
I have lexical-binding
set to true in case that was relevant.