I'm in the early stages of creating a major mode for Emacs for browsing and interacting with the Stack Exchange Network.
Involved in it are several major modes, all with one-key keybindings similar to dired
. I looked at the source for dired
, and extracted what I thought would work:
(defvar stack-network-mode-map
(let ((map (make-keymap)))
(define-key map "n" 'stack-network-next-site)
(define-key map "p" 'stack-network-previous-site)
(define-key map "," 'stack-network-move-site-up)
(define-key map "." 'stack-network-move-site-down)
(define-key map "j" 'stack-network-jump-to-bookmarks)
(define-key map "\C-m" 'stack-network-do-enter-site) ; ret
(define-key map "o" 'stack-network-do-enter-site)
(define-key map "u" 'stack-network-do-profile-summary)
(define-key map "\C-uu" 'stack-network-do-profile-summary-for-user)
(define-key map "i" 'stack-network-do-inbox)
(define-key map "b" 'stack-network-toggle-bookmark)
(define-key map "?" 'stack-network-list-functions) ; [1]
(define-key map "\C-i" 'stack-network-display-details) ; tab
map)
"Keymap for Stack Exchange: Network Browser major mode")
but unfortunately this seems to have absolutely no effect; the buffer is simply edited just as any other normal buffer would be. How can I achieve single-key keybindings if this isn't they way? (Which, by the way, I'm sure it is. There has to be something else going on here.)
You can redefine function keys and mouse events in the same way; just type the function key or click the mouse when it's time to specify the key to rebind. redefines C-x 4 $ to run the (fictitious) command spell-other-window .
Usually, the major mode is automatically set by Emacs, when you first visit a file or create a buffer (see Choosing File Modes). You can explicitly select a new major mode by using an M-x command.
The standard default value is fundamental-mode . If the default value is nil , then whenever Emacs creates a new buffer via a command such as C-x b ( switch-to-buffer ), the new buffer is put in the major mode of the previously current buffer.
You want to define stack-network-mode
using define-derived-mode
(and make it derive from special-mode
, for example).
Other comments about your code:
()
rather than nil
for empty argument lists.(interactive)
right after the docstring to make it an interactive command otherwise it won't work as a key-binding.If you don't want to use special-mode
, you can call supress-keymap
right after creating your make-keymap
.
Move the definition of stack-network-mode-map
before the definition of the mode. Otherwise define-derived-mode
implicitly defines this variable, and defvar
does not change the value of non-nil variables, so the map will be empty actually.
See Derived Modes:
The new mode has its own sparse keymap, named variant
-map
.define-derived-mode
makes the parent mode's keymap the parent of the new map, unless variant-map
is already set and already has a parent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With