1

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 ()
  (interactive)
  (cl-flet ((foo () (insert "a")))
    (define-minor-mode this-buffer-local-mode
      :init-value nil
      :key-map '((kbd "M-_") . foo))
    (this-buffer-local-mode)))

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?

hatmatrix
  • 1,046
  • 9
  • 18
  • 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

1 Answers1

2

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:
nispio
  • 8,175
  • 2
  • 35
  • 73