org-choose.el 14 KB

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