19

我通常使用 org-mode 来跟踪 TODO。我有如下文件:

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

在按预期归档整个子树Project 1时,我无法移动Task 1Misc.归档文件看起来像这样(在此示例中忽略 PROPERTIES):

* Misc
** DONE Task 1

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

4

3 回答 3

29

您可能希望: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(除非您一次性存档整个树)。在子树之后归档父树似乎会将其添加为目标下的附加子标题。它不支持多个级别,因此如果您需要这种复杂的设置,您必须提前设置存档文件的标题,以确保它以您想要的方式输出。

您还可以使用此属性将特定树存档到单独的文件中(用于导出/发布/共享目的)。

于 2012-04-13T16:46:31.467 回答
11
;; 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)
  )

这个 hack 就像用相同的父结构重新归档到你的存档文件,:PROPERTIES:这里没有存档。

也作为这里的要点:https ://gist.github.com/CodeFalling/87b116291aa87fde72cb

于 2016-02-18T08:03:16.857 回答
5

我认为不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并使用此将存档条目重新定位到其原始大纲路径。

于 2012-04-13T16:07:50.173 回答