org-choose.el 14 KB

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