[Top][All Lists]

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

substitute-key-definition vs. define-key MAP [remap ...]

From: Teemu Likonen
Subject: substitute-key-definition vs. define-key MAP [remap ...]
Date: Thu, 15 Jul 2010 11:25:05 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2.50 (gnu/linux)

Some Emacs modes use substitute-key-definition function to redirect some
global key-commands to mode-specific commands. My view is that using
define-key and its [remap ...] functionality would be better.

The problem I'm seeing is that global keys defined in a global minor
mode don't work with substitute-key-definition. Here's an example, first
a working one: cc-mode has this (in cc-mode.el):

    (substitute-key-definition 'indent-new-comment-line
                               c-mode-base-map global-map)

In default global-map M-j runs indent-new-comment-line. Now, let's say
that I have this in my .emacs file:

    (global-set-key [f7] 'indent-new-comment-line)

This works nicely with cc-mode: f7 will run c-indent-new-comment-line
instead of indent-new-comment-line.

But if I don't redefine f7 key in the global map but use a global minor
mode instead to shadow some of the global map (like the command
indent-new-comment-line) then that binding won't be substituted in
cc-mode. In other words, when my global minor mode is activated, the key
for indent-new-comment-line will just run that and not
c-indent-new-comment-line in cc-mode. If you want to test that here's a
small minor mode for you:

    (defvar my-global-minor-mode-map
      (let ((map (make-sparse-keymap)))
        (define-key map [f7] 'indent-new-comment-line)

    (define-minor-mode my-global-minor-mode
      :global t :lighter " my-mode" :keymap my-global-minor-mode-map)

All this would work nicely if cc-mode used [remap ...]:

    (define-key c-mode-base-map
      [remap indent-new-comment-line]

So, am I right in saying that using substitute-key-definition on global
map directly is a bad practice? I guess the question is whether you/we
want to let user create custom global bindings through a global minor
mode. It would be nice and convenient because global minor modes can be
turned on and off. User may want to have different minor modes for
redefining the basic global keys for different editing tasks.

reply via email to

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