all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tassilo Horn <tassilo@member.fsf.org>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: emacs-devel@gnu.org
Subject: Re: A simple implementation of context-sensitive keys
Date: Thu, 11 Sep 2008 09:17:48 +0200	[thread overview]
Message-ID: <87r67rjgmb.fsf@thinkpad.tsdh.de> (raw)
In-Reply-To: <jwvsks7sbn6.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Wed, 10 Sep 2008 21:41:22 -0400")

Stefan Monnier <monnier@iro.umontreal.ca> writes:

Hi!

> I hope we can do better than that, i.e. come up with a way to do
> something like what you proposed, but still have where-is, C-h k,
> etc... take it into account.

But what would C-h k say in that case?

TAB is bound to FOO if BAR matches, or to FOO2 if BAR2 matches, else to
FOO3.

Currently with my approach it says:

,----
| TAB (translated from <tab>) runs the command (lambda nil "Execute
| command if eolp matches." (interactive) (if (cond ((user-variable-p
| (quote eolp)) eolp) ((functionp (quote eolp)) (funcall (quote eolp)))
| (t (eval eolp)))...
`----

In my new version (below) I allow arbitrary forms as predicate, so the
command really can get long and unreadably.  I think it would get much
better, if describe-function would pretty-print lambda forms.

--8<---------------cut here---------------start------------->8---
(defmacro define-context-key (keymap key predicate command &optional mode)
  "Bind KEY in KEYMAP to a command which calls COMMAND if PREDICATE is non-nil.

If PREDICATE doesn't match and KEY is normally bound in KEYMAP,
the corresponding default command will be executed.

If KEY isn't normally bound in KEYMAP, MODE (defaulting to
s/KEYMAP/-map//) will be disabled temporally (to prevent an
infinite recursion) and the function which is then bound to KEY
will be called.

Here're two examples:

  ;; TAB on an outline heading toggles visibility
  (define-context-key outline-minor-mode-map
    (kbd \"TAB\")
    ;; This evals to non-nil, if `point' is on a outline-heading
    (save-excursion
      (goto-char (line-beginning-position))
      (looking-at outline-regexp))
    outline-toggle-children)

  ;; TAB at end of line insert a TAB character
  (define-context-key outline-minor-mode-map
    (kbd \"TAB\")
    eolp
    self-insert-command)

The context key for KEYMAP and KEY which was given as last has
precedence, so in this example TAB at the end of a line of an
outline heading inserts a TAB and doesn't toggle the visibility."
  (let* ((mode (or mode
                   (intern (replace-regexp-in-string "-map" ""
                                                     (symbol-name keymap)))))
         (default-fun (lookup-key (symbol-value keymap) (eval key))))
    `(define-key ,keymap ,key
       (lambda ()
         ,(concat "Execute " (symbol-name command)
                  " if " (format "%s" predicate) " matches.")
         (interactive)
         (if (cond
              ((user-variable-p (quote ,predicate))
               ,predicate)
              ((functionp (quote ,predicate))
               (funcall (quote ,predicate)))
              (t
               (eval ,predicate)))
             (call-interactively (quote ,command))
           (if (quote ,default-fun)
               (call-interactively (quote ,default-fun))
             (let (,mode)
               (call-interactively (key-binding ,key)))))))))
--8<---------------cut here---------------end--------------->8---

-- 
Chuck Norris played Russian Roulette with a fully loaded gun and won. 




  reply	other threads:[~2008-09-11  7:17 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
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 [this message]
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

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

  git send-email \
    --in-reply-to=87r67rjgmb.fsf@thinkpad.tsdh.de \
    --to=tassilo@member.fsf.org \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.