[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: A simple implementation of context-sensitive keys
From: |
Tassilo Horn |
Subject: |
Re: A simple implementation of context-sensitive keys |
Date: |
Wed, 10 Sep 2008 12:57:56 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux) |
"Lennart Borgman (gmail)" <address@hidden> writes:
Hi Lennart,
> I think there are a number of packages out there doing quite similar
> things: smart-tab, tabkey2, hippi-expand.
I know, and I think it's bad that each and every package tries to
reinvent the wheel, so I propose a more general approach.
Here's an improved version which gets rid of the cl dependency. IMO
this could be added to emacs.
--8<---------------cut here---------------start------------->8---
(defmacro define-context-key (mode key predicate function)
"Bind KEY in MODE's map to a command which calls FUNCTION if PREDICATE is
non-nil.
If PREDICATE doesn't match and KEY is normally bound in MODE, the
corresponding default command will be executed.
If KEY isn't normally bound in MODE, MODE will be disabled
temporally (to prevent an infinite recursion) and the function
which is then bound to KEY will be called."
(let* ((keymap (intern (concat (symbol-name mode) "-map")))
(default-fun (lookup-key (symbol-value keymap) (eval key))))
`(define-key ,keymap ,key
(lambda ()
(interactive)
(if (funcall (quote ,predicate))
(call-interactively (quote ,function))
(if (quote ,default-fun)
(call-interactively (quote ,default-fun))
(let (,mode)
(call-interactively (key-binding ,key)))))))))
--8<---------------cut here---------------end--------------->8---
Multiple calls operate cumulative, so something like this works
beautiful.
--8<---------------cut here---------------start------------->8---
(defun outline-context-p ()
(save-excursion
(goto-char (line-beginning-position))
(looking-at outline-regexp)))
(define-context-key outline-minor-mode
(kbd "TAB") outline-context-p outline-toggle-children)
(define-context-key outline-minor-mode
(kbd "TAB") eolp self-insert-command)
--8<---------------cut here---------------end--------------->8---
Bye,
Tassilo
--
One time, at band camp, Chuck Norris ate a percussionist.
- A simple implementation of context-sensitive keys, Tassilo Horn, 2008/09/10
- Re: A simple implementation of context-sensitive keys, Lennart Borgman (gmail), 2008/09/10
- Re: A simple implementation of context-sensitive keys,
Tassilo Horn <=
- Re: A simple implementation of context-sensitive keys, Sean O'Rourke, 2008/09/10
- Re: A simple implementation of context-sensitive keys, Tassilo Horn, 2008/09/11
- Re: A simple implementation of context-sensitive keys, Miles Bader, 2008/09/11
- Re: A simple implementation of context-sensitive keys, Tassilo Horn, 2008/09/11
Re: A simple implementation of context-sensitive keys, Stefan Monnier, 2008/09/10