【技巧分享+问题已解决】 在 org-mode 中只显示当前标题的内容

抄了一段代码,能显示当前标题内容的同时折叠其他标题内容。

相比 org-narrow-to-subtree,能看到其他标题,算是一个优点

不过有个小问题,每次执行 org-show-current-heading-tidily 之后光标都会跳到标题上而非保留在内容中,请问有什么办法执行的时候不移动光标吗?谢谢各位。

;; 显示当前 heading 内容并折叠其他
;; https://emacstil.com/til/2021/09/09/fold-heading/
(defun org-show-current-heading-tidily ()
  (interactive)
  "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)))

我一般用 S-<tab>,然后 C-c C-rorg-fold-reveal)。

学到了,谢谢。

是个不错的 workaround,不过每次都三次快捷键不如直接调用一个函数。

也可能是我想的太多了,毕竟不能保证每次执行函数时光标都在当前 heading 里。

直接加个 save-excursion 不行吗:

(defun org-show-current-heading-tidily ()
  (interactive)
  "Show next entry, keeping other entries closed."
  (if (save-excursion (end-of-line) (outline-invisible-p))
      (progn (org-show-entry) (show-children))
    (save-excursion 
      (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))
    ))
1 个赞

哇,你太棒了!

又学到了新知识,学习 special form 的时候光记着抄名字了没看文档,真到用的时候才发现自己知道的太少了。 :rofl: