unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Tassilo Horn <tassilo@member.fsf.org>
To: emacs-devel@gnu.org
Cc: "Lennart Borgman \(gmail\)" <lennart.borgman@gmail.com>
Subject: Re: A simple implementation of context-sensitive keys
Date: Wed, 10 Sep 2008 12:57:56 +0200	[thread overview]
Message-ID: <87hc8o6zez.fsf@thinkpad.tsdh.de> (raw)
In-Reply-To: <48C7A31A.3000500@gmail.com> (Lennart Borgman's message of "Wed,  10 Sep 2008 12:36:10 +0200")

"Lennart Borgman (gmail)" <lennart.borgman@gmail.com> 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. 




  reply	other threads:[~2008-09-10 10:57 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-09-10  7:17 A simple implementation of context-sensitive keys Tassilo Horn
2008-09-10 10:36 ` Lennart Borgman (gmail)
2008-09-10 10:57   ` Tassilo Horn [this message]
2008-09-10 16:43     ` Ted Zlatanov
2008-09-10 14:14 ` Sean O'Rourke
2008-09-10 14:48   ` Miles Bader
2008-09-10 14:53     ` Juanma Barranquero
2008-09-10 15:17       ` Sean O'Rourke
2008-09-10 15:32         ` Juanma Barranquero
2008-09-11  7:35     ` Tassilo Horn
2008-09-11  8:17       ` Miles Bader
2008-09-11  8:48         ` Tassilo Horn
2008-09-10 17:49 ` Stefan Monnier
2008-09-10 19:21   ` Tassilo Horn
2008-09-11  1:41     ` Stefan Monnier
2008-09-11  7:17       ` Tassilo Horn
2008-09-11 14:40         ` Ted Zlatanov
2008-09-11 15:53           ` Tassilo Horn
2008-09-11 13:41       ` Lennart Borgman (gmail)
2008-09-11 13:48         ` Lennart Borgman (gmail)
2008-09-11 14:22           ` Tassilo Horn
2008-09-11 20:38             ` Lennart Borgman (gmail)
2008-09-12  6:58               ` Tassilo Horn
2008-09-12  8:34                 ` Lennart Borgman (gmail)
2008-09-12  9:47                   ` Tassilo Horn
2008-09-12 11:00                     ` Lennart Borgman
2008-09-12 16:13                       ` Tassilo Horn
2008-09-12 23:46                         ` Lennart Borgman (gmail)
2008-09-13  7:28                           ` Tassilo Horn
2008-09-13  9:32                             ` Lennart Borgman (gmail)
2008-09-15  7:26                               ` Tassilo Horn
2008-09-15 22:39                                 ` Lennart Borgman (gmail)
2008-09-11 20:44         ` Stefan Monnier
2008-09-11 21:14           ` Lennart Borgman (gmail)
2008-09-12  1:33             ` Stefan Monnier
2008-09-12  8:29               ` Lennart Borgman (gmail)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87hc8o6zez.fsf@thinkpad.tsdh.de \
    --to=tassilo@member.fsf.org \
    --cc=emacs-devel@gnu.org \
    --cc=lennart.borgman@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).