[implementations-list] vimpulse-modal.el --- modal key bindings

Vegard Øye vegard_oye at hotmail.com
Sat Dec 5 17:29:10 CET 2009


The :map, :imap and :vmap commands of Vim let one make two key
mappings starting with the same sequence of characters without one
overwriting the other. For example:

    :imap aa foo
    :imap aaa bar

When Vim has read "aa" in Insert mode, it will wait for another
character to decide whether to insert "foo" or "bar". If the user
types "a", "bar" is inserted; if another letter, "foo" plus that
letter.

Compare with the analogous use of Emacs' `global-set-key' function:

    (global-set-key "aa" 'foo)
    (global-set-key "aaa" 'bar)

Here, the first binding is simply overwritten by the more specific
second. The end result is that "aaa" is bound to `bar', while any
other sequence starting with "aa" is not bound to anything.

The attached code provides a set of Vim-like or "modal" functions for
making new keybindings "on top of" previous bindings. They are
`vimpulse-map', `vimpulse-imap' and `vimpulse-vmap', which mimic Vim's
commands, and `vimpulse-define-key', a generic function for specifying
the keymap. To load it, issue

    (require 'vimpulse-modal)

Then, for example, the key sequences "cae" and "dae" may be bound in
vi (command) mode using `vimpulse-map':

    (vimpulse-map "cae" 'vimpulse-change-a-sexp)
    (vimpulse-map "dae" 'vimpulse-delete-a-sexp)

This will execute `vimpulse-change-a-sexp' when typing "cae" in
vi mode, while retaining keys like "cw" for "change word" or "cas"
for "change a sentence".

Just for demonstration purposes, `vimpulse-change-a-sexp' and
`vimpulse-delete-a-sexp' are indeed real text object commands,
defined and bound as shown at the end of the file. They act on
Lisp S-expressions. I am not suggesting they be made part of
vimpulse.el, just making a point about extensibility.

Try it! And please comment. :)

Vegard Øye
 		 	   		  
_________________________________________________________________
Windows Live™ Mail. Flere kontoer på ett sted.
http://download.live.com/wlmail


More information about the implementations-list mailing list