emacs-devel
[Top][All Lists]
Advanced

[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: Thu, 11 Sep 2008 09:17:48 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux)

Stefan Monnier <address@hidden> 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 via email to

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