[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: define a tab's behavior accounding to the context?
From: |
Tassilo Horn |
Subject: |
Re: define a tab's behavior accounding to the context? |
Date: |
Wed, 12 Nov 2008 12:57:18 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux) |
anhnmncb <anhnmncb@sina.com> writes:
Hi!
> How to let tab unfold the outline-tree when the point is at folded
> line, but just as normal tab when at unfolded line?
I added this functionality, or better a general approach to make context
sensitive keys, as a local hack. Here it is:
--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 in o-m-m
(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---
The doc string contains the usage you have in mind.
> orgmode's tab behaves like this, I don't know how it does?
The code above will not act exactly as in org, e.g. it won't "cycle".
Bye,
Tassilo
--
A morning without coffee is like something without something else.