I am trying to emulate buffer-specific keybindings using minor modes as described here.

For an arbitrary buffer, I am trying to call this function:

(defun buffer-set-keys ()
  (cl-flet ((foo () (insert "a")))
    (define-minor-mode this-buffer-local-mode
      :init-value nil
      :key-map '((kbd "M-_") . foo))

I'm pretty sure I'm doing something wrong, and in any case when I try M-_ I get that "M-_ is not defined". How can I get this right?

  • I'm pretty sure that quoting a form which contains a call to kbd is wrong.. Perhaps you want (cons (kbd "M-_") 'foo) ? – YoungFrog Mar 27 '16 at 15:43
  • What are you really trying to do (accomplish)? Instead of focusing immediately on your trial solution, what is the problem you are trying to solve? – Drew Mar 27 '16 at 19:04

I'm going to suggest that defining a minor mode within a function is not the right way to approach this problem. Something like this will do what you are after:

(defun foo () (interactive) (insert "a"))
(defun bar () (interactive) (insert "b"))

(defvar my-buffer-local-mode-map (make-sparse-keymap)
  "Special keymap used to apply specific buffer-local bindings")

(let ((map my-buffer-local-mode-map))
  (define-key map (kbd "M-_") 'foo)
  (define-key map (kbd "M-+") 'bar))

(define-minor-mode my-buffer-local-mode
  "An ad-hoc mode for providing a buffer-local keymap"
  nil nil my-buffer-local-mode-map)

And then use M-x my-buffer-local-mode to enable the mode in any buffer, or else add the following file-local eval to the file:

;; Local Variables:
;; eval: (my-buffer-local-mode 1)
;; End:
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.