Org-mode 导出过滤所有匹配模式的标题并推广子标题

Org-mode export filter all headlines matching pattern and promote subheaders

我有一个很大的 org 文件,其中包含本学期我在大学的一门课程中的所有笔记。每个部分包含两个 level-three headers,称为 "Screenings" 和 "Readings"。当调用 org-export-to-pdf 时,我想过滤掉所有这些标题并递归地提升这些部分的副标题(可能是对 org-do-promote 的递归调用)。

虽然 org 模式手册通常是此类内容的极好资源,但我发现 Advanced Configuration 部分对于年轻的 org-mode 用户来说有点简短详细(又名我)完全掌握。如果更有经验的 Emacs 用户能在正确的方向上指导我构建这个函数,我将不胜感激。

我使用 org-map-entries 上的文档(手册的 Using the mapping API) and the hook example in the Advanced Configuration 部分。

(defun promote-screenings-and-readings (&optional backend)
  (org-map-entries
   (lambda ()
     (let ((point (point))
           (heading
            (buffer-substring-no-properties
             (point)
             (save-excursion
               (forward-line)
               (point)))))
       (when
           (or
            (string= heading "*** Screenings\n")
            (string= heading "*** Readings\n"))
         (org-map-entries
          (lambda ()
            (unless (= (point) point)
              (org-promote)))
          nil
          'tree)
         (forward-line)
         (delete-region point (point)))))))


(add-hook 'org-export-before-parsing-hook 'promote-screenings-and-readings)

promote-screenings-and-readings 提升子树中的所有条目 除了 子树的根,然后删除标题。例如 运行 它在文件中

* Foo
** Bar
*** Screenings
**** A
***** D
**** B
**** C

生产

* Foo
** Bar
*** A
**** D
*** B
*** C

注意:promote-screenings-and-readings 只删除 header,而不是在它下面写的内容,而是在第一次放映或阅读之前。如果您需要,只需从 lambda 中 return (point) 而不是转发行,而是使用最小点(如果存在)。