1

Is there a way to bind a key in a mode defined using define-generic-mode such as xmodmap-generic-mode? Generic does not appear to create keymaps.

ELISP> (require 'generic-x)
generic-x
ELISP> (let (result)
     (mapatoms
      (lambda (sym)
        (when (and (boundp sym)
               (string-match-p "generic.*keymap$" (symbol-name sym)))
          (push sym result))))
     result)
nil
1

Define a keymap variable as usual, conventionally called NAME-mode-map. To use this keymap, the major mode startup function needs to call use-local-map. If you're making your own call to define-generic-mode, pass a call to use-local-map in the function-list argument of define-generic-mode.

(defvar some-generic-mode-map
    (let ((map (make-sparse-keymap)))
      …
      map)
  "Keymap for some mode.")
(define-generic-mode some-generic-mode
  nil nil nil
  '("\\.some\\'")
  #'((lambda () (use-local-map some-generic-mode-map)))

If you don't control the call to define-generic-mode, add a call to use-local-map to the generic mode hook. The mode startup function runs NAME-mode-hook (even though it doesn't define that symbol).

(require 'generic-x)
(defvar xmodmap-generic-mode-map
    (let ((map (make-sparse-keymap)))
      …
      map)
  "Keymap for xmodmap generic mode.")
(defun use-xmodmap-generic-mode-map ()
  (use-local-map xmodmap-generic-mode-map))
(add-hook 'xmodmap-generic-mode-hook #'use-xmodmap-generic-mode-map)

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.