Emacs org-mode:如何折叠除当前标题之外的所有内容?

问题描述 投票:0回答:5

我使用组织模式来处理多个文件中的任务和项目。

在每周议程中,可以使用

<TAB>
<RET>
跳转到每个 TODO 条目的位置。如果目标文件之前未打开,则会加载该文件,并将光标设置到正确的标题,并且整个文档展开,包括抽屉

我非常希望只看到一棵稀疏的树,其中包含所有内容,但折叠了正确的标题(子树可见性并不重要)。

可以通过使用

C-u <TAB
循环全局可见性来折叠整个树,但随后我必须再次找到标题。

我知道我可以通过缩小缓冲区来隐藏其余部分,如下所述: Emacs,如何在组织模式下仅显示当前任务并隐藏其他任务? 但随后我失去了上下文(父标题,轻松访问兄弟姐妹)并且抽屉仍然打开。

理想情况下,我希望有一个显示以下内容的命令:

  • 顶级标题
  • 当前的头条新闻,以及所有家长的高层
  • 当前头条的孩子们

编辑:

发布的函数user3173715略有修改的版本似乎可以解决问题:

(defun org-show-current-heading-tidily ()
  "Show next entry, keeping other entries closed."
  (if (save-excursion (end-of-line) (outline-invisible-p))
      (progn (org-show-entry) (show-children))
    (outline-back-to-heading)
    (unless (and (bolp) (org-on-heading-p))
      (org-up-heading-safe)
      (hide-subtree)
      (error "Boundary reached"))
    (org-overview)
    (org-reveal t)
    (org-show-entry)
    (show-children)))
emacs org-mode
5个回答
8
投票

这是基于实际问题编辑中的答案。

如果对任何人有帮助: 当我尝试将上面的内容绑定到热键时,我不断收到错误,命令错误的参数某事......事实证明必须添加(交互式)标志才能使其工作。 下面是与 M-=

相关的函数示例
(defun org-show-current-heading-tidily ()
  (interactive)  ;Inteactive
  "Show next entry, keeping other entries closed."
  (if (save-excursion (end-of-line) (outline-invisible-p))
      (progn (org-show-entry) (show-children))
    (outline-back-to-heading)
    (unless (and (bolp) (org-on-heading-p))
      (org-up-heading-safe)
      (hide-subtree)
      (error "Boundary reached"))
    (org-overview)
    (org-reveal t)
    (org-show-entry)
    (show-children)))

(global-set-key "\M-=" 'org-show-current-heading-tidily)

@Patrick.B 感谢您的编辑!


4
投票

检查您的组织启动选项 (

customize-group
>
org-startup
),如
org-startup-folded
org-agenda-inhibit-startup
(其他人已经提到过这些),并将选项设置为仅显示折叠视图。
#+STARTUP
之类的组织模式变量将在here进行讨论。

您可能会注意到,当您现在跳转到议程时,所有内容都被折叠起来,甚至活动项目的父项也可能不可见。然后,您可以使用 org-reveal

C-c C-r
,根据
手册
)使上下文(父母、孩子、下一个兄弟姐妹)可见。


4
投票
当前(2022)的 emacs 不再需要复杂的设置来实现问题中所述的目标:

    顶级标题
  • 当前的头条新闻,以及所有家长的高层
  • 当前头条的孩子们
在当前的 Emacs 和 org-mode 版本(Emacs 27.1,截至 2022 年 8 月)中,您可以按

Shift-Tab Tab 关闭所有标题,然后打开当前标题。至关重要的是,光标保留在当前折叠的标题上,因此 Tab 可以重新打开它。

默认安装中唯一显着的变化是 Evil,这可能会也可能不会影响光标保留在折叠标题上的事实。


3
投票
我不太确定这是否是您的需求(我只是认为它适合您的问题标题),但是我通过将这两个函数绑定在 org-mode 的 speed 命令中,非常愉快地使用它们。您可以在

org-mode hacks 中找到这两个函数。我稍微修改了它们以满足我的目的。

两个功能支持:

    展开除当前标题之外的所有其他标题
  1. 将当前标题移至屏幕顶部以扩大阅读区域。
为了完成(2),你需要

(setq recenter-positions '(top bottom))

,可能有一些更好的解决方案,但我没有深入研究。

(defun ded/org-show-next-heading-tidily () "Show next entry, keeping other entries closed." (if (save-excursion (end-of-line) (outline-invisible-p)) (progn (org-show-entry) (show-children)) (outline-next-heading) (unless (and (bolp) (org-on-heading-p)) (org-up-heading-safe) (hide-subtree) (error "Boundary reached")) (org-overview) (org-reveal t) (org-show-entry) (recenter-top-bottom) (show-children) (recenter-top-bottom))) (defun ded/org-show-previous-heading-tidily () "Show previous entry, keeping other entries closed." (let ((pos (point))) (outline-previous-heading) (unless (and (< (point) pos) (bolp) (org-on-heading-p)) (goto-char pos) (hide-subtree) (error "Boundary reached")) (org-overview) (org-reveal t) (org-show-entry) (recenter-top-bottom) (show-children) (recenter-top-bottom)))

并且您可以使用 org-mode 快捷键与

j

l
 绑定它们,然后当光标位于标题开头时,您可以使用 
j
l
 来控制标题的折叠。

(setq org-speed-commands-user '(("j" . ded/org-show-next-heading-tidily) ("l" . ded/org-show-previous-heading-tidily))))

它非常适合读取组织模式文件,干杯!


0
投票
理论上

(save-excursion (outline-hide-other) (org-reveal))

 应该足够了,但是 org-reveal 不显示上部打开的标题的文本正文。

(org-reveal) 中有奇怪的参数,需要传递 '(4),不知道为什么。

对于组织模式:

(defun my/org-fold-hide-other () "Hide other headers and reveal current and don't hide headers and text in opened ones." (interactive) (save-excursion (org-overview) ;; hide others (org-reveal '(4))) ;; reveal current place appropriate) (add-hook 'org-mode-hook (lambda () (local-set-key (kbd "C-c e") 'my/org-fold-hide-other))
对于轮廓模式:

(defun my/outline-hide-other () "Hide other headers and don't hide headers and text in opened ones. like (outline-hide-other)" (interactive) (save-excursion (outline-hide-sublevels 1) ;; hide all, set level to required! (outline-show-children) ;; show headers, not shure how and wehere, (outline-back-to-heading t) ;; to header in depths (outline-show-entry) ;; show local text (outline-up-heading 1 t) ;; go upper (while ( > (funcall outline-level) 1) ;; while not at first header (outline-show-entry) (outline-show-children) ;; show subheaders (outline-up-heading 1 t) ;; go upper ))) (add-hook 'outline-mode-hook (lambda () (local-set-key (kbd "C-c e") 'my/outline-hide-other))
    
© www.soinside.com 2019 - 2024. All rights reserved.