在Emacs org-mode中归档时保留上下文信息

21

通常我使用org-mode来跟踪待办事项。我的文件如下:

* Misc.
** TODO Task 1
** TODO Task 2
* Project 1
** TODO Task 1
** TODO Task 2

虽然归档整个子树(如Project 1)可以按预期工作,但我无法将Misc.中的Task 1移动到归档文件看起来像这样(在此示例中忽略PROPERTIES):

* Misc
** DONE Task 1

换句话说,我想保留任务所属的所有部分。 有没有快速的方法可以做到这一点?


我已经使用了这段代码一段时间了,它非常好用(已更新至Emacs 26),可以将完整路径+子树移动到归档标题。 - Mark
3个回答

33
你可能想在父标题上设置:ARCHIVE:属性。这允许你进行特定于标题的org-archive-location设置。(参见手册)例如,如果你的档案文件设置为%s_archive,则您希望您的原始文件看起来像这样。
* Misc.
  :PROPERTIES:
  :ARCHIVE:  %s_archive::* Misc
  :END:
** TODO Task 1
** TODO Task 2
* Project 1
  :PROPERTIES:
  :ARCHIVE:  %s_archive::* Project 1
  :END:
** TODO Task 1
** TODO Task 2
这会将 * Misc 下的任何子树发送到归档文件中的一个 * Misc 标题中,对于 Project 1 下的子树也是同样的操作(除非你一次性将整个树归档)。在归档子树之后再归档父级树似乎会将其作为目标下的附加子标题添加。它不支持多层级,因此如果需要复杂的设置,您必须提前设置好您的归档文件标题以确保按照所需的方式输出。
您还可以使用此属性将特定的子树归档到单独的文件中(以进行导出/发布/共享等目的)。

这是一个非常好的和干净的解决方案。按预期工作。我的设置足够简单,所以我想不会因为缺乏多级支持而遇到问题。谢谢! - Renke Grunwald

12
;; org-archive-subtree-hierarchical.el
;; modified from https://lists.gnu.org/archive/html/emacs-orgmode/2014-08/msg00109.html

;; In orgmode
;; * A
;; ** AA
;; *** AAA
;; ** AB
;; *** ABA
;; Archiving AA will remove the subtree from the original file and create
;; it like that in archive target:

;; * AA
;; ** AAA

;; And this give you
;; * A
;; ** AA
;; *** AAA


(require 'org-archive)

(defun org-archive-subtree-hierarchical--line-content-as-string ()
  "Returns the content of the current line as a string"
  (save-excursion
    (beginning-of-line)
    (buffer-substring-no-properties
     (line-beginning-position) (line-end-position))))

(defun org-archive-subtree-hierarchical--org-child-list ()
  "This function returns all children of a heading as a list. "
  (interactive)
  (save-excursion
    ;; this only works with org-version > 8.0, since in previous
    ;; org-mode versions the function (org-outline-level) returns
    ;; gargabe when the point is not on a heading.
    (if (= (org-outline-level) 0)
        (outline-next-visible-heading 1)
      (org-goto-first-child))
    (let ((child-list (list (org-archive-subtree-hierarchical--line-content-as-string))))
      (while (org-goto-sibling)
        (setq child-list (cons (org-archive-subtree-hierarchical--line-content-as-string) child-list)))
      child-list)))

(defun org-archive-subtree-hierarchical--org-struct-subtree ()
  "This function returns the tree structure in which a subtree
belongs as a list."
  (interactive)
  (let ((archive-tree nil))
    (save-excursion
      (while (org-up-heading-safe)
        (let ((heading
               (buffer-substring-no-properties
                (line-beginning-position) (line-end-position))))
          (if (eq archive-tree nil)
              (setq archive-tree (list heading))
            (setq archive-tree (cons heading archive-tree))))))
    archive-tree))

(defun org-archive-subtree-hierarchical ()
  "This function archives a subtree hierarchical"
  (interactive)
  (let ((org-tree (org-archive-subtree-hierarchical--org-struct-subtree))
        (this-buffer (current-buffer))
        (file (abbreviate-file-name
               (or (buffer-file-name (buffer-base-buffer))
                   (error "No file associated to buffer")))))
    (save-excursion
      (setq location (org-get-local-archive-location)
            afile (org-extract-archive-file location)
            heading (org-extract-archive-heading location)
            infile-p (equal file (abbreviate-file-name (or afile ""))))
      (unless afile
        (error "Invalid `org-archive-location'"))
      (if (> (length afile) 0)
          (setq newfile-p (not (file-exists-p afile))
                visiting (find-buffer-visiting afile)
                buffer (or visiting (find-file-noselect afile)))
        (setq buffer (current-buffer)))
      (unless buffer
        (error "Cannot access file \"%s\"" afile))
      (org-cut-subtree)
      (set-buffer buffer)
      (org-mode)
      (goto-char (point-min))
      (while (not (equal org-tree nil))
        (let ((child-list (org-archive-subtree-hierarchical--org-child-list)))
          (if (member (car org-tree) child-list)
              (progn
                (search-forward (car org-tree) nil t)
                (setq org-tree (cdr org-tree)))
            (progn
              (goto-char (point-max))
              (newline)
              (org-insert-struct org-tree)
              (setq org-tree nil)))))
      (newline)
      (org-yank)
      (when (not (eq this-buffer buffer))
        (save-buffer))
      (message "Subtree archived %s"
               (concat "in file: " (abbreviate-file-name afile))))))

(defun org-insert-struct (struct)
  "TODO"
  (interactive)
  (when struct
    (insert (car struct))
    (newline)
    (org-insert-struct (cdr struct))))

(defun org-archive-subtree ()
  (org-archive-subtree-hierarchical)
  )

这个技巧就像是将你的归档文件重新设置为具有相同父结构,这里没有任何归档:PROPERTIES:。此外,这里还有一个要点:https://gist.github.com/CodeFalling/87b116291aa87fde72cb

太棒了!非常感谢! - J..y B..y
喜欢它。与议程中的键很好地结合在一起: (define-key org-agenda-mode-map“z”'org-agenda-archive) - J..y B..y
这看起来是将子树重新放置到归档文件末尾,而不是正确的位于归档文件更高位置的子树。自从发布以来已经过去了几年,有必要进行调试。 - Brandon Arnold
2
这是更新版本,用于新的Org-mode(如果您遇到“Symbol's function definition is void: org-archive-subtree-hierarchical”错误)https://gist.github.com/kepi/2f4acc3cc93403c75fbba5684c5d852d - Kepi

6
我认为org-mode没有直接支持在归档文件中镜像当前上下文的功能。
有一个相关的变量,org-archive-location,可用于指定一个单独的标题来放置您的归档项目,但不支持树内的多个级别。在这个页面上,有两个关于org-archive-subtree的建议,可能已经足够好了。为防止该网站消失,我在此重复第一个建议:
(defadvice org-archive-subtree (around my-org-archive-subtree activate)
  (let ((org-archive-location
         (if (save-excursion (org-back-to-heading)
                             (> (org-outline-level) 1))
             (concat (car (split-string org-archive-location "::"))
                     "::* "
                     (car (org-get-outline-path)))
           org-archive-location)))
    ad-do-it))

第二个比较复杂的方法也会保留在顶级标题中发现的标记。
还有一个可能会有用的自定义变量是org-archive-save-context-info。如果此列表包含符号'olpath,则归档的条目将包含:ARCHIVE_OLPATH:属性,该属性设置为已归档条目的轮廓路径(例如:Projects/Misc)。也许您可以在org-archive-subtree上进行一些后期处理,并使用此属性将存档条目重新定位到其原始轮廓路径。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接