如何在Emacs org-mode中自动触发TODO状态的更改

5

使用Emacs org-mode时,我经常有一系列的待办事项,我希望它们按顺序逐个激活。例如,当列表中的项目A标记为“完成”时,我希望项目B自动标记为“待办”。如何实现这一点?这不应该是任何一种通用行为,它只会在我选择的特定项目或列表中发生。

2个回答

4
;; init-org-lawlist.el
;;
;; QUESTION @Brian Z -- stackoverflow.com:  "How to automatically trigger a change in TODO state in Emacs org-mode."
;;
;;    "I often have a list of TODO items that I want to have active one at a time, in sequence. For example when the
;;    Item A in a list is marked "DONE", then I want Item B to be automatically be marked "TODO".  How do I make this
;;    happen? This should not be any kind of general behavior, it should only happen with specific pairs or lists of
;;    items that I chose."
;;
;;
;; ANSWER @lawlist -- stackoverflow.com -- This script provides two (2) possible options:
;;
;;     1.  Navigate to a ** TODO heading (must have at least one todo subtree underneath) and enter:  M-x lawlist-done
;; OR
;;     2.  Within a ** TODO heading (must have at least one todo subtree underneath), Shift-Right or Shift-Left and cycle
;;         to the option ** DONE and then confirm yes or no.
;;
;; NOTE # A:  This configuration script assumes the `org-archive-location` will use the SAME lawlist.org file for everything.
;; In this example, the script uses:  (setq org-archive-location "/Users/HOME/.0.data/lawlist.org::* ARCHIVES")
;;
;; NOTE # B:  Whenever using setq . . ., there is a likelihood that other settings with the same variable will be trumped.
;; So, after testing this script, it would be best to mix and match portions you like with your own configuration for org-mode.
;; You may need to temporarily disable your other configurations that use the same setq variables so as not to conflict while
;; testing this script.
;;
;; NOTE # C:  Author to consider adding a check for the existence of at least one subsequent todo subtree as a condition precedent.
;;
;; NOTE # D:  Author to consider adding a check that the user is on a second level tier (i.e., a todo) as a condition precedent.
;;
;;
;;
;;
;; SAMPLE lawlist.org CONFIGURATION FILE
;;
;;
;;    * TASKS
;;    
;;    ** TODO [#A] First, figure out this new feature request. :lawlist:
;;       DEADLINE: <2013-07-09 Tue>
;;       :PROPERTIES:
;;       :lawlist-drawer:  ACTIVE
;;       :END:
;;    
;;    ** NEXT [#B] Second, If at first you don't succeed, then try again. :lawlist:
;;       :PROPERTIES:
;;       :lawlist-drawer:  PENDING
;;       :END:
;;    
;;    ** WAITING [#C] Third, try again to figure out this new feature request. :lawlist: :WAITING:
;;       :PROPERTIES:
;;       :lawlist-drawer:  DORMANT
;;       :END:
;;    
;;    ** HOLD [#D] Fourth, try, try again to figure out this new feature request. :lawlist: :WAITING:HOLD:
;;       :PROPERTIES:
;;       :lawlist-drawer:  DORMANT
;;       :END:
;;    
;;    
;;    * ARCHIVES
;;    
;;    ** DONE [#E] This task is a done deal. :lawlist:
;;       :PROPERTIES:
;;       :lawlist-drawer:  COMPLETED
;;       :END:


(require 'org)
(setq org-startup-folded 'showeverything) ;; overview | content | all | showeverything
(setq org-tags-column 0)
(setq org-startup-indented nil)
(setq org-indent-mode nil)
(setq org-support-shift-select t) ;; 'always to disable; or, t to use shift-select only when cursor is within a special context.
(setq org-cycle-separator-lines 1)
(setq org-insert-heading-respect-content t)
(setq org-blank-before-new-entry '((heading . t) (plain-list-item . nil)))
(setq org-enable-priority-commands t)
(setq org-highest-priority ?A)
(setq org-lowest-priority ?E)
(setq org-default-priority ?A)
(setq org-ellipsis " \u25bc" )


;; '!' (for a timestamp) or '@' (for a note with timestamp)
(setq org-todo-keywords
      (quote ((sequence  "TODO(t)" "NEXT(n)" "WAITING(w)" "HOLD(h)" "|" "DONE(d)" "CANCELED(c)") )))

(setq org-global-properties '(("lawlist-drawer_ALL". "ACTIVE PENDING DORMANT COMPLETED")))
(setq org-default-properties (cons "lawlist-drawer" org-default-properties))

(setq org-todo-state-tags-triggers
      (quote (("CANCELED" ("CANCELED" . t))
              ("WAITING" ("WAITING" . t))
              ("HOLD" ("WAITING" . t) ("HOLD" . t))
              (done ("WAITING") ("HOLD"))
              ("TODO" ("WAITING") ("CANCELED") ("HOLD"))
              ("NEXT" ("WAITING") ("CANCELED") ("HOLD"))
              ("DONE" ("WAITING") ("CANCELED") ("HOLD")))))

(setq org-tag-alist (quote ((:startgroup)
                            ("john-doe" . ?j)
                            ("jane-doe" . ?J)
                            (:endgroup)
                            ("lawlist" . ?0))))

(add-hook 'org-after-todo-state-change-hook 'lawlist-hook)
(defun lawlist-hook (&optional default-heading)
    (let ((lawlist-item default-heading)
            result)
        (unless lawlist-item
          (condition-case nil
              (progn 
                (org-back-to-heading t)
                (setq lawlist-item (elt (org-heading-components) 4)))
            )
         )
    (when (string-equal org-state "DONE")
        (if (string-equal org-state "DONE")
            (or (yes-or-no-p (format "%s -- process?" org-state))
                (error "You changed your mind.")))
        (org-forward-heading-same-level 1)
        (org-todo "TODO")
        (org-priority ?A)
        (org-deadline nil "<%<%Y-%m-%d %a>>")
        (org-set-property "lawlist-drawer" "ACTIVE")
        (org-backward-heading-same-level 1)
        (org-priority ?E)
        (org-deadline 'remove)
        (org-set-property "lawlist-drawer" "COMPLETED")
        ;; (setq org-archive-save-context-info nil) ;; Set to nil if user doesn't want archive info.
        ;; NOTE:  User must set the correct path to his / her lawlist.org file.
        (setq org-archive-location "/Users/HOME/.0.data/lawlist.org::* ARCHIVES")
        (org-archive-subtree)
        (goto-char (point-min))
        (re-search-forward "^\* ARCHIVES" nil t)
        (org-sort-entries t ?a)
        ;; (org-sort-entries t ?d) additional sorting criteria if deadline is not removed.
        (lawlist-org-cleanup)
        (goto-char (point-min))
        (re-search-forward lawlist-item nil t)
        (beginning-of-line)
        (message "Please take a moment to visually verify your completed tasks has been refiled correctly, then press any key.")
        (read-event) )))


   (defun delete-trailing-blank-lines-at-end-of-file ()
          "Deletes all blank lines at the end of the file, even the last one"
          (interactive)
          (save-excursion
            (save-restriction
              (widen)
              (goto-char (point-max))
              (delete-blank-lines)
              (let ((trailnewlines (abs (skip-chars-backward "\n\t"))))
                (if (> trailnewlines 0)
                    (progn
                      (delete-char trailnewlines)))))))

(defun lawlist-org-cleanup ()
(interactive)
(save-excursion 
(replace-regexp "\n+\\*\\* " "\n\n** " nil (point-min) (point-max))
(replace-regexp "\n+\\* " "\n\n\n* " nil (point-min) (point-max))
(replace-regexp "\n\t\s*" "\n   " nil (point-min) (point-max)) )
(delete-trailing-blank-lines-at-end-of-file) )


(defun lawlist-done (&optional default-heading)
(interactive)
    (remove-hook 'org-after-todo-state-change-hook 'lawlist-hook)
    (let ((lawlist-item default-heading)
            result)
        (unless lawlist-item
          (condition-case nil
              (progn 
                (org-back-to-heading t)
                (setq lawlist-item (elt (org-heading-components) 4)))
            )
         )
        (org-forward-heading-same-level 1)
        (org-todo "TODO")
        (org-priority ?A)
        (org-deadline nil "<%<%Y-%m-%d %a>>")
        (org-set-property "lawlist-drawer" "ACTIVE")
        (org-backward-heading-same-level 1)
        (org-todo "DONE")
        (org-priority ?E)
        (org-deadline 'remove)
        (org-set-property "lawlist-drawer" "COMPLETED")
        ;; (setq org-archive-save-context-info nil) ;; Set to nil if user doesn't want archive info.
        ;; NOTE:  User must set the correct path to his / her lawlist.org file.
        (setq org-archive-location "/Users/HOME/.0.data/lawlist.org::* ARCHIVES")
        (org-archive-subtree)
        (goto-char (point-min))
        (re-search-forward "^\* ARCHIVES" nil t)
        (org-sort-entries t ?a)
        ;; (org-sort-entries t ?d) additional sorting criteria if deadline is not removed.
        (lawlist-org-cleanup)
        (goto-char (point-min))
        (re-search-forward lawlist-item nil t)
        (beginning-of-line))
    (add-hook 'org-after-todo-state-change-hook 'lawlist-hook) )

(provide 'init-org-lawlist)

用于测试脚本的 lawlist.org 样本文件应该与以下内容 完全一致

* TASKS

** TODO [#A] First, figure out this new feature request. :lawlist:
   DEADLINE: <2013-07-09 Tue>
   :PROPERTIES:
   :lawlist-drawer:  ACTIVE
   :END:

** NEXT [#B] Second, If at first you don't succeed, then try again. :lawlist:
   :PROPERTIES:
   :lawlist-drawer:  PENDING
   :END:

** WAITING [#C] Third, try again to figure out this new feature request. :lawlist: :WAITING:
   :PROPERTIES:
   :lawlist-drawer:  DORMANT
   :END:

** HOLD [#D] Fourth, try, try again to figure out this new feature request. :lawlist: :WAITING:HOLD:
   :PROPERTIES:
   :lawlist-drawer:  DORMANT
   :END:


* ARCHIVES

** DONE [#E] This task is a done deal. :lawlist:
   :PROPERTIES:
   :lawlist-drawer:  COMPLETED
   :END:

哎呀,这比我预想的要复杂得多。我不太理解大部分代码将会做什么,但我已经加载了文件并将org-archive-location更改为指向我正在使用的实际org文件。当我尝试M-x lawlist-done时,在迷你缓冲区中出现错误:“符号的函数定义无效:org-forward-heading-same-level”。 - Brian Z
1
可能你使用的是早期版本的Emacs -- 我建议输入 M-X org- RET 并查看列表,看看是否能找到你所使用版本中使用的名称 -- 它应该非常相似。我认为早期版本的Emacs可能使用的旧名称类似于:org-forward-same-level - lawlist
一旦找到您在旧版本Emacs中使用的相应名称,只需将代码中的少数引用替换为您的旧名称即可。 - lawlist
这里是名称更改的参考--看起来是在2012年8月8日左右更改的: http://orgmode.org/w/?p=org-mode.git;a=commitdiff;h=1c595f3e5dc4001c718e71e18633bb7dd11fb652 - lawlist
1
你需要用org-backward-same-level替换org-backward-heading-same-level,方法与之前相同。我保留代码不变,因为它是基于当前版本的Emacs。通过对自己的代码进行这些修改,脚本应该可以正常运行。 - lawlist
如果您有任何其他问题,请告诉我。您的初始测试应该使用脚本内被注释掉的示例lawlist.org配置文件运行--当然,您需要删除 ;; 并使标题和副标题与左边缘对齐。一旦您把所有东西都搞定了,那么您可以按照自己的意愿进行更改。 - lawlist

1

您没有提供太多的上下文,这是您想要的吗?

(defun org-todo-plus ()
  (interactive)
  (org-todo)
  (org-forward-element)
  (org-todo))

它适用于这个列表:

* TODO spam
* eggs
* foo
* bar

在指向 "spam" 的点上,只需重复执行 M-x org-todo-plus,直到所有事项完成。

这只是将一行中的两个项目同时标记为TODO,对吗?那不是这样的。 - Brian Z
谢谢您的详细说明。我尝试了一下,但是在org-forward-element中出现了一个错误。听起来可能是因为我正在运行Emacs 24.2.1,所以我需要升级并再次尝试。 - Brian Z

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