Subject: bug#41130: outline-mode: Add new commands like org-cycle and org=global-cycle

From: Stefan Kangas <[email-masked]>
Date: Mon, 18 May 2020 20:06:00 -0700

Tassilo Horn <[email-masked]> writes:

> I'm also very much in favor of this.  I think probably every user has
> his own incarnation of such a command.

I wish I was one of these users.  ;-)

> Well, I think those shouldn't be commands in outline-minor-mode-map as
> they should only be active when point is on an outline heading, no?  At
> least that's the case with org-mode where TAB acts quite differently
> depending on the context.

This is a good point, yes.

> --8<---------------cut here---------------start------------->8---
> (defmacro th/define-context-key (keymap key dispatch)
>   "Define KEY in KEYMAP to execute according to DISPATCH.
>
> DISPATCH is a form that is evaluated and should return the
> command to be executed.
>
> If DISPATCH returns nil, then the command normally bound to KEY
> will be executed.
>
> Example:
>
>   (th/define-context-key hs-minor-mode-map
>      (kbd \"<C-tab>\")
>      (cond
>       ((not (hs-already-hidden-p))
>        'hs-hide-block)
>       ((hs-already-hidden-p)
>        'hs-show-block)))
>
> This will make <C-tab> show a hidden block.  If the block is
> shown, then it'll be hidden."
>   (declare (indent 2))
>   `(define-key ,keymap ,key
>      `(menu-item "context-key" ignore
>                  :filter ,(lambda (&optional ignored)
>                             ,dispatch))))
>
> (th/define-context-key outline-minor-mode-map (kbd "<tab>")
>     (when (save-excursion
>             (move-beginning-of-line 1)
>             (looking-at-p outline-regexp))
>       'outline-toggle-children))
> --8<---------------cut here---------------end--------------->8---

Interesting.  I suppose the only question I have is this: how do we turn
this into something we could add to Emacs?

There is also the question of C-TAB conflicting with tab-bar-mode.

> PS: On a related note, I think there should be some standard facility
> for defining keys depending on the context.  If Stefan hadn't shown me
> the menu-item-with-:filter trick some years ago, I would probably not
> found out myself.

Agreed.  I had not seen that before, until now.

Best regards,
Stefan Kangas