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

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

Re: Keybindings and minor modes


From: Kevin Rodgers
Subject: Re: Keybindings and minor modes
Date: Thu, 29 Dec 2005 12:23:52 -0700
User-agent: Mozilla Thunderbird 0.9 (X11/20041105)

batkins57@gmail.com wrote:
> I'd like to bind a command to tab in a minor mode I'm writing, but I'd
> also like to preserve any command that might have been attached to that
> key before making my binding (such as indentation).  In other words,
> I'd like to add functionality to an existing keybinding without
> completely overwriting the binding.
>
> The solution I came up with was to simply store the currently bound
> command in a buffer-local variable whenever the minor mode is invoked.
> Then the keys are bound to functions that provide my additional
> functionality and then funcall the old bindings.  This works, but I
> wonder if there isn't a cleaner way to pass a keychord event on to the
> next handler.  The code I have now is pretty ugly, since a key bound to
> self-insert-command has to be handled separately so that
> self-insert-command will get an argument of 0.

I think you could avoid storing the command in a local variable by
accessing the local binding (defined by the major mode) or global
binding dynamically.  That would simplify things a little.

I'll admit, I don't understand the point of calling
(self-insert-command 0).

Perhaps a better approach altogether would be to add a pre-command-hook
that would check the key that was used to invoke it:

(defun ehinter-pre-command-hook ()
"Run `ehinter-hint' in Ehinter Minor Mode, when invoked via SPC, TAB, or RET."
  (when (and ehinter-mode
             (let ((key (this-single-command-keys)))
               (and (= (length key 1))
(member (aref key 0) '(? ?\t ?\r))))) ; SPC, TAB, or RET
    (ehinter-hint)))

(add-hook 'pre-command-hook 'ehinter-pre-command-hook)

Then the ehinter-mode function just needs to set or unset the
buffer-local ehinter-mode variable.

You could tweak that to have the ehinter-mode function add or remove
ehinter-pre-command-hook from the buffer-local pre-command-hook, to
avoid affecting other buffers.

--
Kevin Rodgers





reply via email to

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