[Top][All Lists]

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

Proposal: Remap interactive commands via keymaps

From: Kim F. Storm
Subject: Proposal: Remap interactive commands via keymaps
Date: 03 Jan 2002 23:56:41 +0100
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.1

Triggered by a message in gnu.emacs.bug by address@hidden, I
would like to propose a simple, yet advanced and powerful alternative
to using defadvice or rebinding lots of keys to have a minor mode
handle specific commands in a different manner than the standard
behaviour.  Here is an extract of the message:

> Is there something fundamental about forward-char and backward-char
> that prevents them from being advised?  I assume they are implemented
> in C, could that be the problem?  Is there anyway around it?
> I was trying to use the advice so that I could avoid having to save
> off all the old keybindings for cursor motion and restore them all when
> I exit my minor mode.  Plus, I hoped that by doing it that way my
> minor mode would pickup whatever keys the user was using for cursor
> motion, even if they had been changed.

I've been toying with the following idea for some time as an alternative to 
binding actual keys (or using advice) for modifying the behaviour of
interactive commands in minor modes:

Before executing an interactive command, say self-insert-command, the
normal keymap lookup is performed for that command to possibly find an
alternative command to be executed instead.

Suppose that in the x-mode minor mode, I want all keys bound to
self-insert-command to insert the typed character followed by an `x',
i.e. to execute the following command instead:

    (defun x-self-insert-command ()
        (insert (this-command-keys) "x"))

Rather than rebinding all the self-insert-command keys in x-mode's
minor mode keymap, we can achieve exactly the same functionality
with a single binding:

    (define-key x-mode-map [self-insert-command] 'x-self-insert-command)

Comparing this to the normal minor mode bindings or the advice mechanism
it has several advantages:

1) It makes it easy to modify the logical behaviour of a command
   in a minor mode based on the command, rather than having to
   bind a lot of keys.

2) It makes it easy to `advice' commands on a per-mode basis

3) It can `advice' built-in commands.

4) Since keymaps can be bound to specific sub-buffer sections
   using the 'keymap text or overlay property, it can be
   used for very fine granularity control.

5) It uses existing, well-known mechanisms for setup, activation,
   and deactivation.

In addition, I believe it would be quite simple to implement!

Kim F. Storm  http://www.cua.dk

reply via email to

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