Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically trigger a change in TODO state in Emacs org-mode

Tags:

emacs

org-mode

Using 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.

like image 857
Brian Z Avatar asked Jul 09 '13 22:07

Brian Z


1 Answers

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

The lawlist.org sample file used for testing the script should look exactly like this:

* 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:
like image 63
15 revs Avatar answered Nov 12 '22 00:11

15 revs