1 ;;; org-choose.el --- decision management for org-mode
3 ;; Copyright (C) 2009-2014 Tom Breton (Tehom)
5 ;; This file is not part of GNU Emacs.
7 ;; Author: Tom Breton (Tehom)
8 ;; Keywords: outlines, convenience
10 ;; This file is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This file is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; This is code to support decision management. It lets you treat a
28 ;; group of sibling items in org-mode as alternatives in a decision.
30 ;; There are no user commands in this file. You use it by:
31 ;; * Loading it (manually or by M-x customize-apropos org-modules)
33 ;; * Setting up at least one set of TODO keywords with the
34 ;; interpretation "choose" by either:
36 ;; * Using the file directive #+CHOOSE_TODO:
38 ;; * For instance, "#+CHOOSE_TODO: NO(,-) MAYBE(,0) YES"
40 ;; * Or by M-x customize-apropos org-todo-keywords
42 ;; * Operating on single items with the TODO commands.
44 ;; * Use C-S-right to change the keyword set. Use this to change to
45 ;; the "choose" keyword set that you just defined.
47 ;; * Use S-right to advance the TODO mark to the next setting.
49 ;; For "choose", that means you like this alternative more than
50 ;; before. Other alternatives will be automatically demoted to
51 ;; keep your settings consistent.
53 ;; * Use S-left to demote TODO to the previous setting.
55 ;; For "choose", that means you don't like this alternative as much
56 ;; as before. Other alternatives will be automatically promoted,
57 ;; if this item was all that was keeping them down.
59 ;; * All the other TODO commands are available and behave essentially
72 (defstruct (org-choose-mark-data.
(:type list
))
73 "The format of an entry in org-choose-mark-data.
74 Indexes are 0-based or `nil'.
83 (defvar org-choose-mark-data
85 "Alist of information for choose marks.
87 Each entry is an `org-choose-mark-data.'" )
88 (make-variable-buffer-local 'org-choose-mark-data
)
90 ;;;_ . org-choose-filter-one
92 (defun org-choose-filter-one (i)
94 * a canonized version of the string
95 * optionally one symbol"
99 (string-match "(.*)" i
))
103 (end-text (match-beginning 0))
104 (vanilla-text (substring i
0 end-text
))
105 ;;Get the parenthesized part.
106 (match (match-string 0 i
))
107 ;;Remove the parentheses.
108 (args (substring match
1 -
1))
112 ((arglist-x (org-split-string args
",")))
113 ;;When string starts with "," `split-string' doesn't
114 ;;make a first arg, so in that case make one
117 (string-match "^," args
)
120 (decision-arg (second arglist
))
123 ((string= decision-arg
"0")
125 ((string= decision-arg
"+")
127 ((string= decision-arg
"-")
130 (vanilla-arg (first arglist
))
133 (concat vanilla-text
"("vanilla-arg
")")
136 (list vanilla-text vanilla-mark type
)
137 (list vanilla-text vanilla-mark
)))))
139 ;;;_ . org-choose-setup-vars
140 (defun org-choose-setup-vars (bot-lower-range top-upper-range
141 static-default num-items all-mark-texts
)
142 "Add to org-choose-mark-data according to arguments"
145 ;;If there's no bot-lower-range or no default, we don't
148 (if (and static-default bot-lower-range
)
150 ;;If there's no top-upper-range, use the last
153 (or top-upper-range
(1- num-items
)))
155 (1+ (- static-default bot-lower-range
)))
157 (- top-upper-range static-default
))
159 (min upper-range-length lower-range-length
)))
160 (make-org-choose-mark-data.
162 :bot-lower-range bot-lower-range
163 :top-upper-range top-upper-range
164 :range-length range-length
165 :static-default static-default
166 :all-keywords all-mark-texts
))
167 (make-org-choose-mark-data.
172 :static-default
(or static-default
0)
173 :all-keywords all-mark-texts
)))))
174 (dolist (text all-mark-texts
)
175 (pushnew (cons text tail
)
179 (equal (car a
) (car b
)))))))
181 ;;; org-choose-filter-tail
182 (defun org-choose-filter-tail (raw)
183 "Return a translation of RAW to vanilla and set appropriate
184 buffer-local variables.
186 RAW is a list of strings representing the input text of a choose
192 bot-lower-range top-upper-range range-length static-default
)
195 (vanilla-text vanilla-mark
&optional type
)
196 (org-choose-filter-one i
)
198 ((eq type
'bot-lower-range
)
199 (setq bot-lower-range index
))
200 ((eq type
'top-upper-range
)
201 (setq top-upper-range index
))
202 ((eq type
'default-mark
)
203 (setq static-default index
)))
205 (push vanilla-text all-mark-texts
)
206 (push vanilla-mark vanilla-list
)))
208 (org-choose-setup-vars bot-lower-range top-upper-range
209 static-default index
(reverse all-mark-texts
))
210 (nreverse vanilla-list
)))
212 ;;; org-choose-setup-filter
214 (defun org-choose-setup-filter (raw)
215 "A setup filter for choose interpretations."
216 (when (eq (car raw
) 'choose
)
219 (org-choose-filter-tail (cdr raw
)))))
221 ;;; org-choose-conform-after-promotion
222 (defun org-choose-conform-after-promotion (entry-pos keywords highest-ok-ix
)
223 "Conform the current item after another item was promoted"
225 ;;Skip the entry that triggered this by skipping any entry with
226 ;;the same starting position. plist uses the start of the
227 ;;header line as the position, but map no longer does, so we
228 ;;have to go back to the heading.
231 (org-back-to-heading)
236 (org-choose-get-entry-index keywords
)))
237 ;;If the index of the entry exceeds the highest allowable
238 ;;index, change it to that.
240 (> ix highest-ok-ix
))
242 (nth highest-ok-ix keywords
))))))
243 ;;;_ . org-choose-conform-after-demotion
244 (defun org-choose-conform-after-demotion (entry-pos keywords
247 "Conform the current item after another item was demoted."
249 ;;Skip the entry that triggered this.
252 (org-back-to-heading)
257 (org-choose-get-entry-index keywords
)))
258 ;;If the index of the entry was at or above the old allowable
259 ;;position, change it to the new mirror position if there is
264 (>= ix old-highest-ok-ix
))
266 (nth raise-to-ix keywords
))))))
268 ;;; org-choose-keep-sensible (the org-trigger-hook function)
269 (defun org-choose-keep-sensible (change-plist)
270 "Bring the other items back into a sensible state after an item's
271 setting was changed."
273 ( (from (plist-get change-plist
:from
))
274 (to (plist-get change-plist
:to
))
278 (plist-get change-plist
:position
)))
280 (assoc to org-todo-kwd-alist
)))
282 (eq (nth 1 kwd-data
) 'choose
)
286 (assoc to org-choose-mark-data
))
288 (org-choose-mark-data.-all-keywords data
))
290 (org-choose-get-index-in-keywords
294 (org-choose-get-index-in-keywords
298 (org-choose-highest-other-ok
303 ;;The entry doesn't participate in conformance,
304 ;;so give `nil' which does nothing.
305 ((not highest-ok-ix
) nil
)
306 ;;The entry was created or promoted
309 (> new-index old-index
))
311 #'org-choose-conform-after-promotion
314 (t ;;Otherwise the entry was demoted.
320 (org-choose-mark-data.-static-default
323 (org-choose-highest-other-ok
327 #'org-choose-conform-after-demotion
331 old-highest-ok-ix
))))))
333 ;;The funny-looking names are to make variable capture
334 ;;unlikely. (Poor-man's lexical bindings).
335 (destructuring-bind (func-d473 . args-46k
) funcdata
338 (org-choose-get-fn-map-group))
339 ;;We may call `org-todo', so let various hooks
340 ;;`nil' so we don't cause loops.
341 org-after-todo-state-change-hook
344 org-todo-get-default-hook
345 ;;Also let this alist `nil' so we don't log
346 ;;secondary transitions.
349 (funcall map-over-entries
351 (apply func-d473 args-46k
))))))))
353 (set-marker entry-pos nil
)))
355 ;;; Getting the default mark
356 ;;; org-choose-get-index-in-keywords
357 (defun org-choose-get-index-in-keywords (ix all-keywords
)
358 "Return the index of the current entry."
360 (position ix all-keywords
363 ;;; org-choose-get-entry-index
364 (defun org-choose-get-entry-index (all-keywords)
365 "Return index of current entry."
367 ((state (org-entry-get (point) "TODO")))
368 (org-choose-get-index-in-keywords state all-keywords
)))
370 ;;; org-choose-get-fn-map-group
372 (defun org-choose-get-fn-map-group ()
373 "Return a function to map over the group"
375 (require 'org-agenda
) ;; `org-map-entries' seems to need it.
377 (unless (org-up-heading-safe)
378 (error "Choosing is only supported between siblings in a tree, not on top level"))
380 ((level (org-reduced-level (org-outline-level))))
384 (format "LEVEL=%d" level
)
387 ;;; org-choose-get-highest-mark-index
389 (defun org-choose-get-highest-mark-index (keywords)
390 "Get the index of the highest current mark in the group.
391 If there is none, return 0"
393 ;;Func maps over applicable entries.
395 (org-choose-get-fn-map-group))
398 (funcall map-over-entries
400 (org-choose-get-entry-index keywords
))))))
403 (apply #'max indexes-list
)
406 ;;; org-choose-highest-ok
408 (defun org-choose-highest-other-ok (ix data
)
409 "Return the highest index that any choose mark can sensibly have,
410 given that another mark has index IX.
411 DATA must be a `org-choose-mark-data.'."
414 (org-choose-mark-data.-bot-lower-range data
))
416 (org-choose-mark-data.-top-upper-range data
))
418 (org-choose-mark-data.-range-length data
)))
419 (when (and ix bot-lower-range
)
422 (- top-upper-range ix
)))
424 (< range-length delta
)
425 (+ bot-lower-range delta
))))))
427 ;;; org-choose-get-default-mark-index
429 (defun org-choose-get-default-mark-index (data)
430 "Return the index of the default mark in a choose interpretation.
432 DATA must be a `org-choose-mark-data.'."
436 (org-choose-get-highest-mark-index
437 (org-choose-mark-data.-all-keywords data
))))
438 (org-choose-highest-other-ok
439 highest-mark-index data
))
440 (org-choose-mark-data.-static-default data
)))
442 ;;; org-choose-get-mark-N
443 (defun org-choose-get-mark-N (n data
)
444 "Get the text of the nth mark in a choose interpretation."
447 ((l (org-choose-mark-data.-all-keywords data
)))
450 ;;; org-choose-get-default-mark
452 (defun org-choose-get-default-mark (new-mark old-mark
)
453 "Get the default mark IFF in a choose interpretation.
454 NEW-MARK and OLD-MARK are the text of the new and old marks."
457 (assoc old-mark org-todo-kwd-alist
))
459 (assoc new-mark org-todo-kwd-alist
))
465 (eq (nth 1 old-kwd-data
) 'choose
)))
466 (eq (nth 1 new-kwd-data
) 'choose
))))
471 (assoc new-mark org-choose-mark-data
)))
474 (org-choose-get-mark-N
475 (org-choose-get-default-mark-index
478 (error "Somehow got an unrecognizable mark"))))))
480 ;;; Setting it all up
482 (eval-after-load "org"
484 (add-to-list 'org-todo-setup-filter-hook
485 #'org-choose-setup-filter
)
486 (add-to-list 'org-todo-get-default-hook
487 #'org-choose-get-default-mark
)
488 (add-to-list 'org-trigger-hook
489 #'org-choose-keep-sensible
)
490 (add-to-list 'org-todo-interpretation-widgets
491 '(:tag
"Choose (to record decisions)" choose
)
494 (provide 'org-choose
)
496 ;;; org-choose.el ends here