在Emacs组织模式下归档时保持上下文

时间:2012-04-13 15:40:36

标签: emacs org-mode

我通常使用org-mode来跟踪TODO。我有像这样的文件:

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

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

* Misc
** DONE Task 1

换句话说,我想保留一个任务所属的所有部分。有快速的方法吗?

3 个答案:

答案 0 :(得分:19)

您可能希望在父标题上设置:ARCHIVE:属性。

它允许您进行标题特定的org-archive-location设置。 (见manual

例如,如果您的存档文件设置为%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中的子树执行相同的操作(除非您将整个树存档为一个射击)。在子树出现后存档父树将其添加为目标下的附加子标题。它不支持多个级别,因此您必须提前设置存档文件标题,以确保在需要复杂的类型设置时以您希望的方式输出。

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

答案 1 :(得分:5)

;; 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

答案 2 :(得分:4)

我认为org-mode不支持直接镜像归档文件中的当前上下文。

有一个相关变量org-archive-location可用于指定放置已归档项目的单个标题,但不支持树内的多个级别。在this page上,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上,使用此功能将已存档的条目重新定位到其原始大纲路径。