[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Buffer specific binding
From: |
Tassilo Horn |
Subject: |
Re: Buffer specific binding |
Date: |
Wed, 26 Aug 2015 09:44:45 +0200 |
User-agent: |
Gnus/5.130014 (Ma Gnus v0.14) Emacs/25.0.50 (gnu/linux) |
Ian Zimmerman <itz@buug.org> writes:
> What's the best way to bind a key to a command _only in a specific
> buffer_? AFAIK local-set-key will set it in the mode keymap so all
> buffers in the same mode will see the change or addition; I'd like to
> avoid that.
>
> I know one answer: make a specialized mode (inherited from the real
> one). Any other way?
I guess you could define a new keymap with the new bindings which has
the current local map as parent, and then use you new keymap as local
map.
(let ((map (make-sparse-keymap)))
(set-keymap-parent map (current-local-map))
(define-key map (kbd "C-c c")
(lambda ()
(interactive)
(message "Hey there!")))
(use-local-map map))
I guess you can easily wrap that into some function
`iz/define-buffer-local-keys' and then call that conveniently from a
file local variable section:
;; Local Variables:
;; eval: (iz/define-buffer-local-keys
;; (kbd "C-c c") #'iz/do-this
;; (kbd "C-c C") #'iz/do-that)
;; End:
HTH,
Tassilo