all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Stop modes from hijacking several global keys
@ 2014-11-05  7:02 Tassilo Horn
  2014-11-05  7:12 ` Tassilo Horn
  0 siblings, 1 reply; 4+ messages in thread
From: Tassilo Horn @ 2014-11-05  7:02 UTC (permalink / raw)
  To: help-gnu-emacs

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



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Stop modes from hijacking several global keys
  2014-11-05  7:02 Stop modes from hijacking several global keys Tassilo Horn
@ 2014-11-05  7:12 ` Tassilo Horn
  2014-11-05  9:41   ` Nicolas Richard
  0 siblings, 1 reply; 4+ messages in thread
From: Tassilo Horn @ 2014-11-05  7:12 UTC (permalink / raw)
  To: help-gnu-emacs

Tassilo Horn <tsdh@gnu.org> writes:

> 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.

Oh, that was easier than I expected:

--8<---------------cut here---------------start------------->8---
(defun th/select-nth-window-ensure-keys ()
  "Ensures that M-<number> is bound to `th/select-nth-window'.
Binds whatever else is bound to M-<number> to <escape> M-<number>."
  (dotimes (i 9)
    (let* ((key (kbd (format "M-%s" (1+ i))))
	   (cmd (key-binding key)))
      (unless (equal cmd #'th/select-nth-window)
	(local-set-key key 'th/select-nth-window)
	(local-set-key (kbd (format "<escape> M-%s" (1+ i))) cmd)))))

(add-hook 'after-change-major-mode-hook #'th/select-nth-window-ensure-keys)
--8<---------------cut here---------------end--------------->8---

> But is there a similar hook that runs after a minor mode has been
> activated?

This one is still open although I think I haven't encountered a
minor-mode that tried to redefine M-<number> yet, so it's less important
to me.

Bye,
Tassilo



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Stop modes from hijacking several global keys
  2014-11-05  7:12 ` Tassilo Horn
@ 2014-11-05  9:41   ` Nicolas Richard
  2014-11-05 11:38     ` Tassilo Horn
  0 siblings, 1 reply; 4+ messages in thread
From: Nicolas Richard @ 2014-11-05  9:41 UTC (permalink / raw)
  To: help-gnu-emacs

Tassilo Horn <tsdh@gnu.org> writes:

> Tassilo Horn <tsdh@gnu.org> writes:
>
>> 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.

Since minor modes have precedence of major modes, you could define your
own minor mode with those keybindings.

If you use John Wiegley's bind-key (or use-package), you already have
such a minor mode : override-global-mode. bind-key* will add bindings to
it.

-- 
Nicolas Richard



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Stop modes from hijacking several global keys
  2014-11-05  9:41   ` Nicolas Richard
@ 2014-11-05 11:38     ` Tassilo Horn
  0 siblings, 0 replies; 4+ messages in thread
From: Tassilo Horn @ 2014-11-05 11:38 UTC (permalink / raw)
  To: Nicolas Richard; +Cc: help-gnu-emacs

Nicolas Richard <theonewiththeevillook@yahoo.fr> writes:

Hi Nicolas,

>>> 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.
>
> Since minor modes have precedence of major modes, you could define
> your own minor mode with those keybindings.

I don't think that would make a difference.  Note that I want the
commands that a (major-) mode would bind to `M-<number>' to be still
available via `<escape> M-<number>', so I'd need to do what
`th/select-nth-window-ensure-keys' from my previous mail does whenever
my mode is activated.  (And for it to be a proper minor-mode, it should
undo that when it gets deactivated.)

Looking at `define-globalized-minor-mode' that basically does the same
what I did: put a function in `after-change-major-mode-hook' that
enables the minor-mode.  I've put a function there that reclaims my
keys.

And then the problem of a minor-mode that gets activated after my own
minor-mode and then again steals my keys isn't solved, too.  For that,
I'd somehow need to ensure that my own minor-mode (or currently my
gimme-back-my-keys function) is and always stays the last function run
by `after-change-major-mode-hook'.

Anyway, this is now a more or less theoretical discussion because until
now it have always been major modes stealing my keys.  And by
convention, a minor-mode shouldn't bind M-<number> anyway.

Bye,
Tassilo



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-11-05 11:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-11-05  7:02 Stop modes from hijacking several global keys Tassilo Horn
2014-11-05  7:12 ` Tassilo Horn
2014-11-05  9:41   ` Nicolas Richard
2014-11-05 11:38     ` Tassilo Horn

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.