org-choose.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. ;;;_ org-choose.el --- decision management for org-mode
  2. ;;;_. Headers
  3. ;;;_ , License
  4. ;; Copyright (C) 2009 Tom Breton (Tehom)
  5. ;; Author: Tom Breton (Tehom)
  6. ;; Keywords: outlines, convenience
  7. ;; This file is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 2, or (at your option)
  10. ;; any later version.
  11. ;; This file is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs; see the file COPYING. If not, write to
  17. ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. ;; Boston, MA 02111-1307, USA.
  19. ;;;_ , Commentary:
  20. ; This is code to support decision management. It lets you treat a
  21. ; group of sibling items in org-mode as alternatives in a decision.
  22. ; There are no user commands in this file. You use it by:
  23. ; * Loading it (manually or by M-x customize-apropos org-modules)
  24. ;; * Setting up at least one set of TODO keywords with the
  25. ;; interpretation "choose" by either:
  26. ;; * Using the file directive #+CHOOSE_TODO:
  27. ;; * For instance, "#+CHOOSE_TODO: NO(,-) MAYBE(,0) YES"
  28. ;; * Or by M-x customize-apropos org-todo-keywords
  29. ;; * Operating on single items with the TODO commands.
  30. ;; * Use C-S-right to change the keyword set. Use this to change to
  31. ;; the "choose" keyword set that you just defined.
  32. ;; * Use S-right to advance the TODO mark to the next setting.
  33. ;; For "choose", that means you like this alternative more than
  34. ;; before. Other alternatives will be automatically demoted to
  35. ;; keep your settings consistent.
  36. ;; * Use S-left to demote TODO to the previous setting.
  37. ;; For "choose", that means you don't like this alternative as much
  38. ;; as before. Other alternatives will be automatically promoted,
  39. ;; if this item was all that was keeping them down.
  40. ;; * All the other TODO commands are available and behave essentially
  41. ;; the normal way.
  42. ;;;_ , Requires
  43. (require 'org)
  44. (eval-when-compile
  45. (require 'cl))
  46. ;;;_. Body
  47. ;;;_ , The variables
  48. (defstruct (org-choose-mark-data. (:type list))
  49. "The format of an entry in org-choose-mark-data.
  50. Indexes are 0-based or `nil'.
  51. "
  52. keyword
  53. bot-lower-range
  54. top-upper-range
  55. range-length
  56. static-default
  57. all-keywords)
  58. (defvar org-choose-mark-data
  59. ()
  60. "Alist of information for choose marks.
  61. Each entry is an `org-choose-mark-data.'" )
  62. (make-variable-buffer-local 'org-choose-mark-data)
  63. ;;;_ , For setup
  64. ;;;_ . org-choose-filter-one
  65. (defun org-choose-filter-one (i)
  66. "Return a list of
  67. * a canonized version of the string
  68. * optionally one symbol"
  69. (if
  70. (not
  71. (string-match "(.*)" i))
  72. (list i i)
  73. (let*
  74. (
  75. (end-text (match-beginning 0))
  76. (vanilla-text (substring i 0 end-text))
  77. ;;Get the parenthesized part.
  78. (match (match-string 0 i))
  79. ;;Remove the parentheses.
  80. (args (substring match 1 -1))
  81. ;;Split it
  82. (arglist
  83. (let
  84. ((arglist-x (split-string args ",")))
  85. ;;When string starts with "," `split-string' doesn't
  86. ;;make a first arg, so in that case make one
  87. ;;manually.
  88. (if
  89. (string-match "^," args)
  90. (cons nil arglist-x)
  91. arglist-x)))
  92. (decision-arg (second arglist))
  93. (type
  94. (cond
  95. ((string= decision-arg "0")
  96. 'default-mark)
  97. ((string= decision-arg "+")
  98. 'top-upper-range)
  99. ((string= decision-arg "-")
  100. 'bot-lower-range)
  101. (t nil)))
  102. (vanilla-arg (first arglist))
  103. (vanilla-mark
  104. (if vanilla-arg
  105. (concat vanilla-text "("vanilla-arg")")
  106. vanilla-text)))
  107. (if type
  108. (list vanilla-text vanilla-mark type)
  109. (list vanilla-text vanilla-mark)))))
  110. ;;;_ . org-choose-setup-vars
  111. (defun org-choose-setup-vars (bot-lower-range top-upper-range
  112. static-default num-items all-mark-texts)
  113. "Add to org-choose-mark-data according to arguments"
  114. (let*
  115. (
  116. (tail
  117. ;;If there's no bot-lower-range or no default, we don't
  118. ;;have ranges.
  119. (cdr
  120. (if (and static-default bot-lower-range)
  121. (let*
  122. (
  123. ;;If there's no top-upper-range, use the last
  124. ;;item.
  125. (top-upper-range
  126. (or top-upper-range (1- num-items)))
  127. (lower-range-length
  128. (1+ (- static-default bot-lower-range)))
  129. (upper-range-length
  130. (- top-upper-range static-default))
  131. (range-length
  132. (min upper-range-length lower-range-length)))
  133. (make-org-choose-mark-data.
  134. :keyword nil
  135. :bot-lower-range bot-lower-range
  136. :top-upper-range top-upper-range
  137. :range-length range-length
  138. :static-default static-default
  139. :all-keywords all-mark-texts))
  140. (make-org-choose-mark-data.
  141. :keyword nil
  142. :bot-lower-range nil
  143. :top-upper-range nil
  144. :range-length nil
  145. :static-default (or static-default 0)
  146. :all-keywords all-mark-texts)))))
  147. (dolist (text all-mark-texts)
  148. (pushnew (cons text tail)
  149. org-choose-mark-data
  150. :test
  151. #'(lambda (a b)
  152. (equal (car a) (car b)))))))
  153. ;;;_ . org-choose-filter-tail
  154. (defun org-choose-filter-tail (raw)
  155. "Return a translation of RAW to vanilla and set appropriate
  156. buffer-local variables.
  157. RAW is a list of strings representing the input text of a choose
  158. interpretation."
  159. (let
  160. ((vanilla-list nil)
  161. (all-mark-texts nil)
  162. (index 0)
  163. bot-lower-range top-upper-range range-length static-default)
  164. (dolist (i raw)
  165. (destructuring-bind
  166. (vanilla-text vanilla-mark &optional type)
  167. (org-choose-filter-one i)
  168. (cond
  169. ((eq type 'bot-lower-range)
  170. (setq bot-lower-range index))
  171. ((eq type 'top-upper-range)
  172. (setq top-upper-range index))
  173. ((eq type 'default-mark)
  174. (setq static-default index)))
  175. (incf index)
  176. (push vanilla-text all-mark-texts)
  177. (push vanilla-mark vanilla-list)))
  178. (org-choose-setup-vars bot-lower-range top-upper-range
  179. static-default index (reverse all-mark-texts))
  180. (nreverse vanilla-list)))
  181. ;;;_ . org-choose-setup-filter
  182. (defun org-choose-setup-filter (raw)
  183. "A setup filter for choose interpretations."
  184. (when (eq (car raw) 'choose)
  185. (cons
  186. 'choose
  187. (org-choose-filter-tail (cdr raw)))))
  188. ;;;_ . org-choose-conform-after-promotion
  189. (defun org-choose-conform-after-promotion (entry-pos keywords highest-ok-ix)
  190. "Conform the current item after another item was promoted"
  191. (unless
  192. ;;Skip the entry that triggered this by skipping any entry with
  193. ;;the same starting position. Both map and plist use the start
  194. ;;of the header line as the position, so we can just compare
  195. ;;them with `='
  196. (= (point) entry-pos)
  197. (let
  198. ((ix
  199. (org-choose-get-entry-index keywords)))
  200. ;;If the index of the entry exceeds the highest allowable
  201. ;;index, change it to that.
  202. (when (and ix
  203. (> ix highest-ok-ix))
  204. (org-todo
  205. (nth highest-ok-ix keywords))))))
  206. ;;;_ . org-choose-conform-after-demotion
  207. (defun org-choose-conform-after-demotion (entry-pos keywords
  208. raise-to-ix
  209. old-highest-ok-ix)
  210. "Conform the current item after another item was demoted."
  211. (unless
  212. ;;Skip the entry that triggered this.
  213. (= (point) entry-pos)
  214. (let
  215. ((ix
  216. (org-choose-get-entry-index keywords)))
  217. ;;If the index of the entry was at or above the old allowable
  218. ;;position, change it to the new mirror position if there is
  219. ;;one.
  220. (when (and
  221. ix
  222. raise-to-ix
  223. (>= ix old-highest-ok-ix))
  224. (org-todo
  225. (nth raise-to-ix keywords))))))
  226. ;;;_ , org-choose-keep-sensible (the org-trigger-hook function)
  227. (defun org-choose-keep-sensible (change-plist)
  228. "Bring the other items back into a sensible state after an item's
  229. setting was changed."
  230. (let*
  231. ( (from (plist-get change-plist :from))
  232. (to (plist-get change-plist :to))
  233. (entry-pos
  234. (set-marker
  235. (make-marker)
  236. (plist-get change-plist :position)))
  237. (kwd-data
  238. (assoc to org-todo-kwd-alist)))
  239. (when
  240. (eq (nth 1 kwd-data) 'choose)
  241. (let*
  242. (
  243. (data
  244. (assoc to org-choose-mark-data))
  245. (keywords
  246. (org-choose-mark-data.-all-keywords data))
  247. (old-index
  248. (org-choose-get-index-in-keywords
  249. from
  250. keywords))
  251. (new-index
  252. (org-choose-get-index-in-keywords
  253. to
  254. keywords))
  255. (highest-ok-ix
  256. (org-choose-highest-other-ok
  257. new-index
  258. data))
  259. (funcdata
  260. (cond
  261. ;;The entry doesn't participate in conformance,
  262. ;;so give `nil' which does nothing.
  263. ((not highest-ok-ix) nil)
  264. ;;The entry was created or promoted
  265. ((or
  266. (not old-index)
  267. (> new-index old-index))
  268. (list
  269. #'org-choose-conform-after-promotion
  270. entry-pos keywords
  271. highest-ok-ix))
  272. (t ;;Otherwise the entry was demoted.
  273. (let
  274. (
  275. (raise-to-ix
  276. (min
  277. highest-ok-ix
  278. (org-choose-mark-data.-static-default
  279. data)))
  280. (old-highest-ok-ix
  281. (org-choose-highest-other-ok
  282. old-index
  283. data)))
  284. (list
  285. #'org-choose-conform-after-demotion
  286. entry-pos
  287. keywords
  288. raise-to-ix
  289. old-highest-ok-ix))))))
  290. (if funcdata
  291. ;;The funny-looking names are to make variable capture
  292. ;;unlikely. (Poor-man's lexical bindings).
  293. (destructuring-bind (func-d473 . args-46k) funcdata
  294. (let
  295. ((map-over-entries
  296. (org-choose-get-fn-map-group))
  297. ;;We may call `org-todo', so let various hooks
  298. ;;`nil' so we don't cause loops.
  299. org-after-todo-state-change-hook
  300. org-trigger-hook
  301. org-blocker-hook
  302. org-todo-get-default-hook
  303. ;;Also let this alist `nil' so we don't log
  304. ;;secondary transitions.
  305. org-todo-log-states)
  306. ;;Map over group
  307. (funcall map-over-entries
  308. #'(lambda ()
  309. (apply func-d473 args-46k))))))))
  310. ;;Remove the marker
  311. (set-marker entry-pos nil)))
  312. ;;;_ , Getting the default mark
  313. ;;;_ . org-choose-get-index-in-keywords
  314. (defun org-choose-get-index-in-keywords (ix all-keywords)
  315. "Return the index of the current entry."
  316. (if ix
  317. (position ix all-keywords
  318. :test #'equal)))
  319. ;;;_ . org-choose-get-entry-index
  320. (defun org-choose-get-entry-index (all-keywords)
  321. "Return index of current entry."
  322. (let*
  323. ((state (org-entry-get (point) "TODO")))
  324. (org-choose-get-index-in-keywords state all-keywords)))
  325. ;;;_ . org-choose-get-fn-map-group
  326. (defun org-choose-get-fn-map-group ()
  327. "Return a function to map over the group"
  328. #'(lambda (fn)
  329. (require 'org-agenda) ;; `org-map-entries' seems to need it.
  330. (save-excursion
  331. (unless (org-up-heading-safe)
  332. (error "Chosing is only supported between siblings in a tree, not on top level"))
  333. (save-restriction
  334. (org-map-entries fn nil 'tree)))))
  335. ;;;_ . org-choose-get-highest-mark-index
  336. (defun org-choose-get-highest-mark-index (keywords)
  337. "Get the index of the highest current mark in the group.
  338. If there is none, return 0"
  339. (let*
  340. (
  341. ;;Func maps over applicable entries.
  342. (map-over-entries
  343. (org-choose-get-fn-map-group))
  344. (indexes-list
  345. (remove nil
  346. (funcall map-over-entries
  347. #'(lambda ()
  348. (org-choose-get-entry-index keywords))))))
  349. (if
  350. indexes-list
  351. (apply #'max indexes-list)
  352. 0)))
  353. ;;;_ . org-choose-highest-ok
  354. (defun org-choose-highest-other-ok (ix data)
  355. "Return the highest index that any choose mark can sensibly have,
  356. given that another mark has index IX.
  357. DATA must be a `org-choose-mark-data.'."
  358. (let
  359. (
  360. (bot-lower-range
  361. (org-choose-mark-data.-bot-lower-range data))
  362. (top-upper-range
  363. (org-choose-mark-data.-top-upper-range data))
  364. (range-length
  365. (org-choose-mark-data.-range-length data)))
  366. (when (and ix bot-lower-range)
  367. (let*
  368. ((delta
  369. (- top-upper-range ix)))
  370. (unless
  371. (< range-length delta)
  372. (+ bot-lower-range delta))))))
  373. ;;;_ . org-choose-get-default-mark-index
  374. (defun org-choose-get-default-mark-index (data)
  375. "Return the index of the default mark in a choose interpretation.
  376. DATA must be a `org-choose-mark-data.'."
  377. (or
  378. (let
  379. ((highest-mark-index
  380. (org-choose-get-highest-mark-index
  381. (org-choose-mark-data.-all-keywords data))))
  382. (org-choose-highest-other-ok
  383. highest-mark-index data))
  384. (org-choose-mark-data.-static-default data)))
  385. ;;;_ . org-choose-get-mark-N
  386. (defun org-choose-get-mark-N (n data)
  387. "Get the text of the nth mark in a choose interpretation."
  388. (let*
  389. ((l (org-choose-mark-data.-all-keywords data)))
  390. (nth n l)))
  391. ;;;_ . org-choose-get-default-mark
  392. (defun org-choose-get-default-mark (new-mark old-mark)
  393. "Get the default mark IFF in a choose interpretation.
  394. NEW-MARK and OLD-MARK are the text of the new and old marks."
  395. (let*
  396. (
  397. (old-kwd-data
  398. (assoc old-mark org-todo-kwd-alist))
  399. (new-kwd-data
  400. (assoc new-mark org-todo-kwd-alist))
  401. (becomes-choose
  402. (and
  403. (or
  404. (not old-kwd-data)
  405. (not
  406. (eq (nth 1 old-kwd-data) 'choose)))
  407. (eq (nth 1 new-kwd-data) 'choose))))
  408. (when
  409. becomes-choose
  410. (let
  411. ((new-mark-data
  412. (assoc new-mark org-choose-mark-data)))
  413. (if
  414. new-mark
  415. (org-choose-get-mark-N
  416. (org-choose-get-default-mark-index
  417. new-mark-data)
  418. new-mark-data)
  419. (error "Somehow got an unrecognizable mark"))))))
  420. ;;;_ , Setting it all up
  421. (eval-after-load "org"
  422. '(progn
  423. (add-to-list 'org-todo-setup-filter-hook
  424. #'org-choose-setup-filter)
  425. (add-to-list 'org-todo-get-default-hook
  426. #'org-choose-get-default-mark)
  427. (add-to-list 'org-trigger-hook
  428. #'org-choose-keep-sensible)
  429. (add-to-list 'org-todo-interpretation-widgets
  430. '(:tag "Choose (to record decisions)" choose)
  431. 'append)
  432. ))
  433. ;;;_. Footers
  434. ;;;_ , Provides
  435. (provide 'org-choose)
  436. ;;;_ * Local emacs vars.
  437. ;;;_ + Local variables:
  438. ;;;_ + End:
  439. ;;;_ , End
  440. ;;; org-choose.el ends here