help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Stop modes from hijacking several global keys


From: Tassilo Horn
Subject: Stop modes from hijacking several global keys
Date: Wed, 05 Nov 2014 08:02:27 +0100
User-agent: Gnus/5.130012 (Ma Gnus v0.12) Emacs/25.0.50 (gnu/linux)

Hi all,

I'm using this command for years for easily switching to windows with
M-<window-number>:

--8<---------------cut here---------------start------------->8---
(defun th/select-nth-window (n)
  "Selects the N-th window.
When called interactively, the N is provided as the last part of
the keybinding.  So you'd usually bind this function to
M-{1,..,9}, and then M-3 selects the third window."
  (interactive
   (list (let ((ev (event-basic-type last-command-event)))
           (string-to-number (char-to-string ev)))))
  (if (> n (length (window-list)))
      (user-error "There's no window %s." n)
    (select-window (window-at 0 0))
    (other-window (1- n))))

(dotimes (i 9)
  (global-set-key (kbd (format "M-%s" (1+ i))) 'th/select-nth-window))
--8<---------------cut here---------------end--------------->8---

But somewhat recently, many modes like magit and diff-mode (why does
that redefine M-<number> to `digit-argument' in its own mode map?) start
using M-<number> keys for other things.  Or maybe I just started using
modes that use these keys for other things...

Anyway, window-switching is such a frequent task, and doing M-<number>
for that is hard-wired in my muscle memory, so the question is: Is there
some automatic way to prevent modes and minor-modes from redefining it?

Ideally, that magic would stop the mode from hijacking M-<number> keys,
and whatever it wants to bind to M-<number> would automagically be bound
to `<escape> M-<number>' instead.

I think at least for major-modes I can use
`after-change-major-mode-hook' in combination with a function checking
(key-binding (kbd "M-<number>")) and doing `local-set-key' if needed.
But is there a similar hook that runs after a minor mode has been
activated?

Bye,
Tassilo



reply via email to

[Prev in Thread] Current Thread [Next in Thread]