From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tassilo Horn Newsgroups: gmane.emacs.devel Subject: Re: suggestion for tab keybinding in hideshow minor mode. Date: Thu, 02 Dec 2010 18:28:56 +0100 Message-ID: <87ipzcqe1z.fsf@fastmail.fm> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: dough.gmane.org 1291311015 3389 80.91.229.12 (2 Dec 2010 17:30:15 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 2 Dec 2010 17:30:15 +0000 (UTC) Cc: emacs-devel@gnu.org To: alin soare Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Dec 02 18:30:10 2010 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1POCyt-00058u-Ac for ged-emacs-devel@m.gmane.org; Thu, 02 Dec 2010 18:30:08 +0100 Original-Received: from localhost ([127.0.0.1]:56413 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1POCys-00050R-Lj for ged-emacs-devel@m.gmane.org; Thu, 02 Dec 2010 12:30:06 -0500 Original-Received: from [140.186.70.92] (port=44868 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1POCyF-0004ai-4O for emacs-devel@gnu.org; Thu, 02 Dec 2010 12:30:01 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1POCxp-0002lA-3L for emacs-devel@gnu.org; Thu, 02 Dec 2010 12:29:27 -0500 Original-Received: from deliver.uni-koblenz.de ([141.26.64.15]:18457) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1POCxo-0002k5-R6 for emacs-devel@gnu.org; Thu, 02 Dec 2010 12:29:01 -0500 Original-Received: from localhost (localhost [127.0.0.1]) by deliver.uni-koblenz.de (Postfix) with ESMTP id 551AA7801631; Thu, 2 Dec 2010 18:28:58 +0100 (CET) Original-Received: from deliver.uni-koblenz.de ([127.0.0.1]) by localhost (deliver.uni-koblenz.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 14438-08; Thu, 2 Dec 2010 18:28:57 +0100 (CET) X-CHKRCPT: Envelopesender noch thorn@fastmail.fm Original-Received: from thinkpad (tsdh.uni-koblenz.de [141.26.67.142]) by deliver.uni-koblenz.de (Postfix) with ESMTP id 4731078015DD; Thu, 2 Dec 2010 18:28:57 +0100 (CET) User-Agent: Gnus/5.110011 (No Gnus v0.11) Emacs/24.0.50 (gnu/linux) X-Virus-Scanned: amavisd-new at uni-koblenz.de X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:133325 Archived-At: alin soare writes: Hi Alin, > I suggest to reproduce the behavior of org-mode for the Tab key , when > the hs minor mode is active. > > 1. first tab on a narowed line shows the block > 2. is the next key is also tab, to hide again > 3. otherwise, to preserve the previous binding of the major mode. I have a macro in my ~/.emacs which allows exactly the definition of such context sensitive shortcuts. 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 outline-minor-mode (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 (defun ,(gensym "context-key-") () ,(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--- To get the exact same behavior as your definition, I've stolen your predicates, and now use these "context keys": --8<---------------cut here---------------start------------->8--- (define-context-key hs-minor-mode-map (kbd "TAB") (let ((obj (car (overlays-in (save-excursion (move-beginning-of-line nil) (point)) (save-excursion (move-end-of-line nil) (point)))))) (and (null obj) (eq last-command this-command))) hs-hide-block) (define-context-key hs-minor-mode-map (kbd "TAB") (let ((obj (car (overlays-in (save-excursion (move-beginning-of-line nil ) (point)) (save-excursion (move-end-of-line nil) (point)))))) (and (overlayp obj) (eq 'hs (overlay-get obj 'invisible)))) hs-show-block) --8<---------------cut here---------------end--------------->8--- Bye, Tassilo