I have all my keybindings in a separate minor mode - firstly, to be sure that keybindings that I consider mandatory wouldn't be overridden by some major mode; and secondly, to be able to disable all of them in one command in case I need too.
Up until now I have been extending prefix keys like so:
(define-key my-minor-mode-map (kbd "M-g f") 'my-goto-file-func)
But then I realized that there is such thing as prefix keymap, and for M-g
key it is goto-map
. So I'd rather extend this keymap instead of hardcoding the prefix key, so it would be like:
(define-key goto-map (kbd "f") 'my-goto-file-func)
But in this case my minor mode has nothing to do with it, so I lose all my advantages of using it.
One way would be to determine which key is bound to that particular prefix keymap and then use that key in the bindings. But it has a drawback - prefix key wouldn't be remapped for my minor mode if I remap it after loading my minor mode.
So are there any better ways to do that?
Extending keys the way you did is perfectly fine because it keeps complexity to a minimum. So while it is certainly possible to modify a keymap in your minor mode, it is not necessarily better.
In order to use a modified keymap in your minor mode without overriding the original one, simply set the original as your parent, e.g.
(defvar my-goto-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map goto-map)
map))
You can then use a modified version in your minor mode:
(define-key my-goto-map "f" 'find-file)
(define-key my-mode-map (kbd "M-g") my-goto-map)
But the original goto-map
is still alive and kicking, and modifying it will reflect in your my-goto-map
:
(define-key goto-map "i" 'info)
See "Inheritance and Keymaps" in the elisp manual.