unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Tassilo Horn <tassilo@member.fsf.org>
To: help-gnu-emacs@gnu.org
Subject: Re: define a tab's behavior accounding to the context?
Date: Wed, 12 Nov 2008 12:57:18 +0100	[thread overview]
Message-ID: <87fxlxnom9.fsf@thinkpad.tsdh.de> (raw)
In-Reply-To: 86fxlxnqlk.fsf@fbsd.hasee.cpu

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.





  reply	other threads:[~2008-11-12 11:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-12 11:14 define a tab's behavior accounding to the context? anhnmncb
2008-11-12 11:57 ` Tassilo Horn [this message]
2008-11-12 12:32   ` anhnmncb

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=87fxlxnom9.fsf@thinkpad.tsdh.de \
    --to=tassilo@member.fsf.org \
    --cc=help-gnu-emacs@gnu.org \
    /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.
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).