emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: suggestion for tab keybinding in hideshow minor mode.


From: Tassilo Horn
Subject: Re: suggestion for tab keybinding in hideshow minor mode.
Date: Thu, 02 Dec 2010 18:28:56 +0100
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/24.0.50 (gnu/linux)

alin soare <address@hidden> 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



reply via email to

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