Fix compatibility issues with org-choose.el
[org-mode.git] / contrib / lisp / org-choose.el
blob9e07f797ae758ea327ae47f01d2355e6b233053f
1 ;;;_ org-choose.el --- decision management for org-mode
3 ;;;_. Headers
4 ;;;_ , License
5 ;; Copyright (C) 2009 Tom Breton (Tehom)
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)
13 ;; any later version.
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.
25 ;;;_ , Commentary:
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
60 ;; the normal way.
63 ;;;_ , Requires
65 (require 'org)
66 ;(eval-when-compile
67 ; (require 'cl))
68 (require 'cl)
70 ;;;_. Body
71 ;;;_ , The variables
73 (defstruct (org-choose-mark-data. (:type list))
74 "The format of an entry in org-choose-mark-data.
75 Indexes are 0-based or `nil'.
77 keyword
78 bot-lower-range
79 top-upper-range
80 range-length
81 static-default
82 all-keywords)
84 (defvar org-choose-mark-data
86 "Alist of information for choose marks.
88 Each entry is an `org-choose-mark-data.'" )
89 (make-variable-buffer-local 'org-choose-mark-data)
90 ;;;_ , For setup
91 ;;;_ . org-choose-filter-one
93 (defun org-choose-filter-one (i)
94 "Return a list of
95 * a canonized version of the string
96 * optionally one symbol"
98 (if
99 (not
100 (string-match "(.*)" i))
101 (list i i)
102 (let*
104 (end-text (match-beginning 0))
105 (vanilla-text (substring i 0 end-text))
106 ;;Get the parenthesized part.
107 (match (match-string 0 i))
108 ;;Remove the parentheses.
109 (args (substring match 1 -1))
110 ;;Split it
111 (arglist
112 (let
113 ((arglist-x (org-split-string args ",")))
114 ;;When string starts with "," `split-string' doesn't
115 ;;make a first arg, so in that case make one
116 ;;manually.
117 (if
118 (string-match "^," args)
119 (cons nil arglist-x)
120 arglist-x)))
121 (decision-arg (second arglist))
122 (type
123 (cond
124 ((string= decision-arg "0")
125 'default-mark)
126 ((string= decision-arg "+")
127 'top-upper-range)
128 ((string= decision-arg "-")
129 'bot-lower-range)
130 (t nil)))
131 (vanilla-arg (first arglist))
132 (vanilla-mark
133 (if vanilla-arg
134 (concat vanilla-text "("vanilla-arg")")
135 vanilla-text)))
136 (if type
137 (list vanilla-text vanilla-mark type)
138 (list vanilla-text vanilla-mark)))))
140 ;;;_ . org-choose-setup-vars
141 (defun org-choose-setup-vars (bot-lower-range top-upper-range
142 static-default num-items all-mark-texts)
143 "Add to org-choose-mark-data according to arguments"
145 (let*
147 (tail
148 ;;If there's no bot-lower-range or no default, we don't
149 ;;have ranges.
150 (cdr
151 (if (and static-default bot-lower-range)
152 (let*
154 ;;If there's no top-upper-range, use the last
155 ;;item.
156 (top-upper-range
157 (or top-upper-range (1- num-items)))
158 (lower-range-length
159 (1+ (- static-default bot-lower-range)))
160 (upper-range-length
161 (- top-upper-range static-default))
162 (range-length
163 (min upper-range-length lower-range-length)))
166 (make-org-choose-mark-data.
167 :keyword nil
168 :bot-lower-range bot-lower-range
169 :top-upper-range top-upper-range
170 :range-length range-length
171 :static-default static-default
172 :all-keywords all-mark-texts))
174 (make-org-choose-mark-data.
175 :keyword nil
176 :bot-lower-range nil
177 :top-upper-range nil
178 :range-length nil
179 :static-default (or static-default 0)
180 :all-keywords all-mark-texts)))))
182 (dolist (text all-mark-texts)
183 (pushnew (cons text tail)
184 org-choose-mark-data
185 :test
186 #'(lambda (a b)
187 (equal (car a) (car b)))))))
192 ;;;_ . org-choose-filter-tail
193 (defun org-choose-filter-tail (raw)
194 "Return a translation of RAW to vanilla and set appropriate
195 buffer-local variables.
197 RAW is a list of strings representing the input text of a choose
198 interpretation."
199 (let
200 ((vanilla-list nil)
201 (all-mark-texts nil)
202 (index 0)
203 bot-lower-range top-upper-range range-length static-default)
204 (dolist (i raw)
205 (destructuring-bind
206 (vanilla-text vanilla-mark &optional type)
207 (org-choose-filter-one i)
208 (cond
209 ((eq type 'bot-lower-range)
210 (setq bot-lower-range index))
211 ((eq type 'top-upper-range)
212 (setq top-upper-range index))
213 ((eq type 'default-mark)
214 (setq static-default index)))
215 (incf index)
216 (push vanilla-text all-mark-texts)
217 (push vanilla-mark vanilla-list)))
219 (org-choose-setup-vars bot-lower-range top-upper-range
220 static-default index (reverse all-mark-texts))
221 (nreverse vanilla-list)))
223 ;;;_ . org-choose-setup-filter
225 (defun org-choose-setup-filter (raw)
226 "A setup filter for choose interpretations."
227 (when (eq (car raw) 'choose)
228 (cons
229 'choose
230 (org-choose-filter-tail (cdr raw)))))
232 ;;;_ . org-choose-conform-after-promotion
233 (defun org-choose-conform-after-promotion (entry-pos keywords highest-ok-ix)
234 "Conform the current item after another item was promoted"
236 (unless
237 ;;Skip the entry that triggered this by skipping any entry with
238 ;;the same starting position. Both map and plist use the start
239 ;;of the header line as the position, so we can just compare
240 ;;them with `='
241 (= (point) entry-pos)
242 (let
243 ((ix
244 (org-choose-get-entry-index keywords)))
245 ;;If the index of the entry exceeds the highest allowable
246 ;;index, change it to that.
247 (when (and ix
248 (> ix highest-ok-ix))
249 (org-todo
250 (nth highest-ok-ix keywords))))))
251 ;;;_ . org-choose-conform-after-demotion
252 (defun org-choose-conform-after-demotion (entry-pos keywords
253 raise-to-ix
254 old-highest-ok-ix)
255 "Conform the current item after another item was demoted."
257 (unless
258 ;;Skip the entry that triggered this.
259 (= (point) entry-pos)
260 (let
261 ((ix
262 (org-choose-get-entry-index keywords)))
263 ;;If the index of the entry was at or above the old allowable
264 ;;position, change it to the new mirror position if there is
265 ;;one.
266 (when (and
268 raise-to-ix
269 (>= ix old-highest-ok-ix))
270 (org-todo
271 (nth raise-to-ix keywords))))))
273 ;;;_ , org-choose-keep-sensible (the org-trigger-hook function)
274 (defun org-choose-keep-sensible (change-plist)
275 "Bring the other items back into a sensible state after an item's
276 setting was changed."
277 (let*
278 ( (from (plist-get change-plist :from))
279 (to (plist-get change-plist :to))
280 (entry-pos
281 (set-marker
282 (make-marker)
283 (plist-get change-plist :position)))
284 (kwd-data
285 (assoc to org-todo-kwd-alist)))
286 (when
287 (eq (nth 1 kwd-data) 'choose)
288 (let*
290 (data
291 (assoc to org-choose-mark-data))
292 (keywords
293 (org-choose-mark-data.-all-keywords data))
294 (old-index
295 (org-choose-get-index-in-keywords
296 from
297 keywords))
298 (new-index
299 (org-choose-get-index-in-keywords
301 keywords))
302 (highest-ok-ix
303 (org-choose-highest-other-ok
304 new-index
305 data))
306 (funcdata
307 (cond
308 ;;The entry doesn't participate in conformance,
309 ;;so give `nil' which does nothing.
310 ((not highest-ok-ix) nil)
311 ;;The entry was created or promoted
312 ((or
313 (not old-index)
314 (> new-index old-index))
315 (list
316 #'org-choose-conform-after-promotion
317 entry-pos keywords
318 highest-ok-ix))
319 (t ;;Otherwise the entry was demoted.
320 (let
322 (raise-to-ix
323 (min
324 highest-ok-ix
325 (org-choose-mark-data.-static-default
326 data)))
327 (old-highest-ok-ix
328 (org-choose-highest-other-ok
329 old-index
330 data)))
332 (list
333 #'org-choose-conform-after-demotion
334 entry-pos
335 keywords
336 raise-to-ix
337 old-highest-ok-ix))))))
339 (if funcdata
340 ;;The funny-looking names are to make variable capture
341 ;;unlikely. (Poor-man's lexical bindings).
342 (destructuring-bind (func-d473 . args-46k) funcdata
343 (let
344 ((map-over-entries
345 (org-choose-get-fn-map-group))
346 ;;We may call `org-todo', so let various hooks
347 ;;`nil' so we don't cause loops.
348 org-after-todo-state-change-hook
349 org-trigger-hook
350 org-blocker-hook
351 org-todo-get-default-hook
352 ;;Also let this alist `nil' so we don't log
353 ;;secondary transitions.
354 org-todo-log-states)
355 ;;Map over group
356 (funcall map-over-entries
357 #'(lambda ()
358 (apply func-d473 args-46k))))))))
360 ;;Remove the marker
361 (set-marker entry-pos nil)))
365 ;;;_ , Getting the default mark
366 ;;;_ . org-choose-get-index-in-keywords
367 (defun org-choose-get-index-in-keywords (ix all-keywords)
368 "Return the index of the current entry."
370 (if ix
371 (position ix all-keywords
372 :test #'equal)))
374 ;;;_ . org-choose-get-entry-index
375 (defun org-choose-get-entry-index (all-keywords)
376 "Return index of current entry."
378 (let*
379 ((state (org-entry-get (point) "TODO")))
380 (org-choose-get-index-in-keywords state all-keywords)))
382 ;;;_ . org-choose-get-fn-map-group
384 (defun org-choose-get-fn-map-group ()
385 "Return a function to map over the group"
387 #'(lambda (fn)
388 (require 'org-agenda) ;; `org-map-entries' seems to need it.
389 (save-excursion
390 (unless (org-up-heading-safe)
391 (error "Chosing is only supported between siblings in a tree, not on top level"))
392 (save-restriction
393 (org-map-entries fn nil 'tree)))))
395 ;;;_ . org-choose-get-highest-mark-index
397 (defun org-choose-get-highest-mark-index (keywords)
398 "Get the index of the highest current mark in the group.
399 If there is none, return 0"
401 (let*
403 ;;Func maps over applicable entries.
404 (map-over-entries
405 (org-choose-get-fn-map-group))
407 (indexes-list
408 (remove nil
409 (funcall map-over-entries
410 #'(lambda ()
411 (org-choose-get-entry-index keywords))))))
413 indexes-list
414 (apply #'max indexes-list)
415 0)))
418 ;;;_ . org-choose-highest-ok
420 (defun org-choose-highest-other-ok (ix data)
421 "Return the highest index that any choose mark can sensibly have,
422 given that another mark has index IX.
423 DATA must be a `org-choose-mark-data.'."
425 (let
427 (bot-lower-range
428 (org-choose-mark-data.-bot-lower-range data))
429 (top-upper-range
430 (org-choose-mark-data.-top-upper-range data))
431 (range-length
432 (org-choose-mark-data.-range-length data)))
433 (when (and ix bot-lower-range)
434 (let*
435 ((delta
436 (- top-upper-range ix)))
437 (unless
438 (< range-length delta)
439 (+ bot-lower-range delta))))))
441 ;;;_ . org-choose-get-default-mark-index
443 (defun org-choose-get-default-mark-index (data)
444 "Return the index of the default mark in a choose interpretation.
446 DATA must be a `org-choose-mark-data.'."
450 (let
451 ((highest-mark-index
452 (org-choose-get-highest-mark-index
453 (org-choose-mark-data.-all-keywords data))))
454 (org-choose-highest-other-ok
455 highest-mark-index data))
456 (org-choose-mark-data.-static-default data)))
460 ;;;_ . org-choose-get-mark-N
461 (defun org-choose-get-mark-N (n data)
462 "Get the text of the nth mark in a choose interpretation."
464 (let*
465 ((l (org-choose-mark-data.-all-keywords data)))
466 (nth n l)))
468 ;;;_ . org-choose-get-default-mark
470 (defun org-choose-get-default-mark (new-mark old-mark)
471 "Get the default mark IFF in a choose interpretation.
472 NEW-MARK and OLD-MARK are the text of the new and old marks."
474 (let*
476 (old-kwd-data
477 (assoc old-mark org-todo-kwd-alist))
478 (new-kwd-data
479 (assoc new-mark org-todo-kwd-alist))
480 (becomes-choose
481 (and
483 (not old-kwd-data)
484 (not
485 (eq (nth 1 old-kwd-data) 'choose)))
486 (eq (nth 1 new-kwd-data) 'choose))))
487 (when
488 becomes-choose
489 (let
490 ((new-mark-data
491 (assoc new-mark org-choose-mark-data)))
493 new-mark
494 (org-choose-get-mark-N
495 (org-choose-get-default-mark-index
496 new-mark-data)
497 new-mark-data)
498 (error "Somehow got an unrecognizable mark"))))))
500 ;;;_ , Setting it all up
502 (eval-after-load "org"
503 '(progn
504 (add-to-list 'org-todo-setup-filter-hook
505 #'org-choose-setup-filter)
506 (add-to-list 'org-todo-get-default-hook
507 #'org-choose-get-default-mark)
508 (add-to-list 'org-trigger-hook
509 #'org-choose-keep-sensible)
510 (add-to-list 'org-todo-interpretation-widgets
511 '(:tag "Choose (to record decisions)" choose)
512 'append)
516 ;;;_. Footers
517 ;;;_ , Provides
519 (provide 'org-choose)
521 ;;;_ * Local emacs vars.
522 ;;;_ + Local variables:
523 ;;;_ + End:
525 ;;;_ , End
526 ;;; org-choose.el ends here