unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* suggestion for tab keybinding in hideshow minor mode.
@ 2010-12-02 13:43 alin soare
  2010-12-02 17:28 ` Tassilo Horn
  0 siblings, 1 reply; 5+ messages in thread
From: alin soare @ 2010-12-02 13:43 UTC (permalink / raw)
  To: emacs-devel

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.



(defun tab-hs-hide nil
  (interactive)
  (let ((obj (car (overlays-in
				   (save-excursion (move-beginning-of-line nil ) (point))
				   (save-excursion (move-end-of-line nil) (point)) ))))
	(cond ((and (null obj)
				(eq last-command this-command) )
		   (hs-hide-block) )
		  ((and (overlayp obj)
				(eq 'hs (overlay-get obj 'invisible)))
		   (hs-show-block) )
		  (t
		   (funcall (lookup-key (current-global-map) (kbd "^I") ) ) ) ) ) )

(define-key hs-minor-mode-map [tab] 'tab-hs-hide )







^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: suggestion for tab keybinding in hideshow minor mode.
  2010-12-02 13:43 suggestion for tab keybinding in hideshow minor mode alin soare
@ 2010-12-02 17:28 ` Tassilo Horn
  2010-12-02 23:42   ` Thien-Thi Nguyen
  2010-12-06 15:02   ` Alin Soare
  0 siblings, 2 replies; 5+ messages in thread
From: Tassilo Horn @ 2010-12-02 17:28 UTC (permalink / raw)
  To: alin soare; +Cc: emacs-devel

alin soare <as1789@gmail.com> 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



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: suggestion for tab keybinding in hideshow minor mode.
  2010-12-02 17:28 ` Tassilo Horn
@ 2010-12-02 23:42   ` Thien-Thi Nguyen
  2010-12-03  8:11     ` Tassilo Horn
  2010-12-06 15:02   ` Alin Soare
  1 sibling, 1 reply; 5+ messages in thread
From: Thien-Thi Nguyen @ 2010-12-02 23:42 UTC (permalink / raw)
  To: Tassilo Horn; +Cc: emacs-devel

() Tassilo Horn <tassilo@member.fsf.org>
() Thu, 02 Dec 2010 18:28:56 +0100

     (let ((obj (car (overlays-in
                      (save-excursion (move-beginning-of-line nil) (point))
                      (save-excursion (move-end-of-line nil) (point))))))
       ...)

See also ‘line-beginning-position’, ‘line-end-position’,
‘hs-already-hidden-p’, ‘hs-overlay-at’.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: suggestion for tab keybinding in hideshow minor mode.
  2010-12-02 23:42   ` Thien-Thi Nguyen
@ 2010-12-03  8:11     ` Tassilo Horn
  0 siblings, 0 replies; 5+ messages in thread
From: Tassilo Horn @ 2010-12-03  8:11 UTC (permalink / raw)
  To: Thien-Thi Nguyen; +Cc: emacs-devel

Thien-Thi Nguyen <ttn@gnuvola.org> writes:

> () Tassilo Horn <tassilo@member.fsf.org>
> () Thu, 02 Dec 2010 18:28:56 +0100
>
>      (let ((obj (car (overlays-in
>                       (save-excursion (move-beginning-of-line nil) (point))
>                       (save-excursion (move-end-of-line nil) (point))))))
>        ...)
>
> See also ‘line-beginning-position’, ‘line-end-position’,

Yeah, first I've replaced the above with exactly those, but the behavior
was different.  When I've hidden an inner block and then an outer block
(like, a function definition with an inner for loop), I was unable to
show it again.

I suspect that `line-beginning-position' and `line-end-position'
consider "real" lines, whereas the move-functions mean visible lines.
In a hidden block {...} the open brace is not on the same real line than
the closing one.

> ‘hs-already-hidden-p’, ‘hs-overlay-at’.

Yep, that makes it much simpler:

--8<---------------cut here---------------start------------->8---
(eval-after-load 'hideshow
  ;; Use TAB to toggle hiding/showing a block.
  '(progn
     (define-context-key hs-minor-mode-map
       (kbd "TAB")
       (and (not (hs-already-hidden-p))
	    (eq last-command this-command))
       hs-hide-block)

     (define-context-key hs-minor-mode-map
       (kbd "TAB")
       hs-already-hidden-p
       hs-show-block)))
--8<---------------cut here---------------end--------------->8---

Thanks for the hint!

Tassilo



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: suggestion for tab keybinding in hideshow minor mode.
  2010-12-02 17:28 ` Tassilo Horn
  2010-12-02 23:42   ` Thien-Thi Nguyen
@ 2010-12-06 15:02   ` Alin Soare
  1 sibling, 0 replies; 5+ messages in thread
From: Alin Soare @ 2010-12-06 15:02 UTC (permalink / raw)
  To: Tassilo Horn; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 878 bytes --]

I found a bug, here is the code again:

0. if the current line contains hidden text, it shows the block
1. otherwise, the first tab indents, and the second tab hides
2. afterwards, it switches hide/show



(defun tab-hs-hide ()
  (interactive)

    (let ((obj (car (overlays-in
                     (save-excursion (move-beginning-of-line nil ) (point) )
                     (save-excursion (move-end-of-line nil) (point) ) ) ) )
)
      (cond ((and (null obj)
                  (eq last-command this-command) )
             (hs-hide-block) )
            ((and (overlayp obj)
                  (eq 'hs (overlay-get obj 'invisible)))
             (progn
               (move-beginning-of-line nil)
               (hs-show-block) ) )
            (t
             (funcall (lookup-key (current-global-map) (kbd "^I") ) ) ) ) )
)

(define-key hs-minor-mode-map [tab] 'tab-hs-hide )

[-- Attachment #2: Type: text/html, Size: 1140 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-12-06 15:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-12-02 13:43 suggestion for tab keybinding in hideshow minor mode alin soare
2010-12-02 17:28 ` Tassilo Horn
2010-12-02 23:42   ` Thien-Thi Nguyen
2010-12-03  8:11     ` Tassilo Horn
2010-12-06 15:02   ` Alin Soare

Code repositories for project(s) associated with this public inbox

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

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).