2

I would like to have ; bound to other-window in special modes, unless the derived mode overrides the definition. It would be nice to also include modes not strictly deriving from special-mode (but defining 'mode-class), but that's not strictly necessary.

Ideas I've tried/considered:

  • Use global-set-key: I thought the global keymap would be of lower precedence than major-mode keymaps, so ; would work in modes that defined it, but instead I just overwrote self-insert-command
  • Add the keybinding in special-mode-hook: It doesn't seem like this hook is actually run, and this misses the 'mode-class special modes
  • Add a keybinding that either calls other-window or defers to the major mode. Not sure how I would do this?

Any ideas?

  • Just use define-key with special-mode-map. – Drew Oct 31 '16 at 14:14
4

I think you can use special-mode-map when possible. When it doesn't work, simply use the corresponding mode map such as Info-mode-map.


The following (needs bind-key.el) might works (I tested in info-mode and help-mode), it uses bind-key.el's override-global-mode, which overrides any minor-mode binding of ;.

(defun override-global-mode-semicolon ()
  (interactive)
  (if (eq (get major-mode 'mode-class) 'special)
      (call-interactively 'other-window)
    (let (buf (current-buffer))
      (override-global-mode -1)
      (unwind-protect
          (call-interactively (key-binding ";"))
        (and (buffer-live-p buf)
             (with-current-buffer buf
               (override-global-mode 1)))))))

(bind-key* ";" 'override-global-mode-semicolon)
  • Sorry should have added this to the ideas considered. This misses all of the special modes defined using 'mode-class as well as any that don't use special-mode-map as their parent keymap. – Felipe Oct 31 '16 at 12:43
  • @Felipe I see, I added another solution, try it if you want. – xuchunyang Oct 31 '16 at 13:56
  • 1
    Thanks, this is great! Since I know that the default global binding is self-insert-command, I don't need bind-key so am just doing (defun global-semi () (interactive) (if (eq (get major-mode 'mode-class) 'special) (call-interactively 'other-window) (call-interactively 'self-insert-command))) which I then global-set-key on ";". – Felipe Oct 31 '16 at 16:29
3

IIUC you're just looking for:

(define-key special-mode-map ";" #'other-window)

And for those other modes which don't (yet) derive from special-mode, you'll have to do something like

(add-hook '<foo>-mode-hook
          (lambda ()
            (local-set-key ";" #'other-window)))

tho I'd advise you to report those modes via M-x report-emacs-bug if you think they should derive from special-mode.

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.