org-colview.el 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  1. ;;; org-colview.el --- Column View in Org-mode
  2. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;;
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;; This file contains the column view for Org.
  23. ;;; Code:
  24. (eval-when-compile (require 'cl))
  25. (require 'org)
  26. (declare-function org-agenda-redo "org-agenda" ())
  27. (declare-function org-agenda-do-context-action "org-agenda" ())
  28. (declare-function org-clock-sum-today "org-clock" (&optional headline-filter))
  29. (when (featurep 'xemacs)
  30. (error "Do not load this file into XEmacs, use 'org-colview-xemacs.el'."))
  31. ;;; Column View
  32. (defvar org-columns-overlays nil
  33. "Holds the list of current column overlays.")
  34. (defvar org-columns-current-fmt nil
  35. "Local variable, holds the currently active column format.")
  36. (make-variable-buffer-local 'org-columns-current-fmt)
  37. (defvar org-columns-current-fmt-compiled nil
  38. "Local variable, holds the currently active column format.
  39. This is the compiled version of the format.")
  40. (make-variable-buffer-local 'org-columns-current-fmt-compiled)
  41. (defvar org-columns-current-widths nil
  42. "Loval variable, holds the currently widths of fields.")
  43. (make-variable-buffer-local 'org-columns-current-widths)
  44. (defvar org-columns-current-maxwidths nil
  45. "Loval variable, holds the currently active maximum column widths.")
  46. (make-variable-buffer-local 'org-columns-current-maxwidths)
  47. (defvar org-columns-begin-marker (make-marker)
  48. "Points to the position where last a column creation command was called.")
  49. (defvar org-columns-top-level-marker (make-marker)
  50. "Points to the position where current columns region starts.")
  51. (defvar org-columns-map (make-sparse-keymap)
  52. "The keymap valid in column display.")
  53. (defun org-columns-content ()
  54. "Switch to contents view while in columns view."
  55. (interactive)
  56. (org-overview)
  57. (org-content))
  58. (org-defkey org-columns-map "c" 'org-columns-content)
  59. (org-defkey org-columns-map "o" 'org-overview)
  60. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  61. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  62. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  63. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  64. (org-defkey org-columns-map "v" 'org-columns-show-value)
  65. (org-defkey org-columns-map "q" 'org-columns-quit)
  66. (org-defkey org-columns-map "r" 'org-columns-redo)
  67. (org-defkey org-columns-map "g" 'org-columns-redo)
  68. (org-defkey org-columns-map [left] 'backward-char)
  69. (org-defkey org-columns-map "\M-b" 'backward-char)
  70. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  71. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  72. (org-defkey org-columns-map "\M-f"
  73. (lambda () (interactive) (goto-char (1+ (point)))))
  74. (org-defkey org-columns-map [right]
  75. (lambda () (interactive) (goto-char (1+ (point)))))
  76. (org-defkey org-columns-map [down]
  77. (lambda () (interactive)
  78. (let ((col (current-column)))
  79. (beginning-of-line 2)
  80. (while (and (org-invisible-p2) (not (eobp)))
  81. (beginning-of-line 2))
  82. (move-to-column col)
  83. (if (eq major-mode 'org-agenda-mode)
  84. (org-agenda-do-context-action)))))
  85. (org-defkey org-columns-map [up]
  86. (lambda () (interactive)
  87. (let ((col (current-column)))
  88. (beginning-of-line 0)
  89. (while (and (org-invisible-p2) (not (bobp)))
  90. (beginning-of-line 0))
  91. (move-to-column col)
  92. (if (eq major-mode 'org-agenda-mode)
  93. (org-agenda-do-context-action)))))
  94. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  95. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  96. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  97. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  98. (org-defkey org-columns-map "<" 'org-columns-narrow)
  99. (org-defkey org-columns-map ">" 'org-columns-widen)
  100. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  101. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  102. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  103. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  104. (dotimes (i 10)
  105. (org-defkey org-columns-map (number-to-string i)
  106. `(lambda () (interactive)
  107. (org-columns-next-allowed-value nil ,i))))
  108. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  109. '("Column"
  110. ["Edit property" org-columns-edit-value t]
  111. ["Next allowed value" org-columns-next-allowed-value t]
  112. ["Previous allowed value" org-columns-previous-allowed-value t]
  113. ["Show full value" org-columns-show-value t]
  114. ["Edit allowed values" org-columns-edit-allowed t]
  115. "--"
  116. ["Edit column attributes" org-columns-edit-attributes t]
  117. ["Increase column width" org-columns-widen t]
  118. ["Decrease column width" org-columns-narrow t]
  119. "--"
  120. ["Move column right" org-columns-move-right t]
  121. ["Move column left" org-columns-move-left t]
  122. ["Add column" org-columns-new t]
  123. ["Delete column" org-columns-delete t]
  124. "--"
  125. ["CONTENTS" org-columns-content t]
  126. ["OVERVIEW" org-overview t]
  127. ["Refresh columns display" org-columns-redo t]
  128. "--"
  129. ["Open link" org-columns-open-link t]
  130. "--"
  131. ["Quit" org-columns-quit t]))
  132. (defun org-columns-new-overlay (beg end &optional string face)
  133. "Create a new column overlay and add it to the list."
  134. (let ((ov (make-overlay beg end)))
  135. (overlay-put ov 'face (or face 'secondary-selection))
  136. (org-overlay-display ov string face)
  137. (push ov org-columns-overlays)
  138. ov))
  139. (defun org-columns-display-here (&optional props dateline)
  140. "Overlay the current line with column display."
  141. (interactive)
  142. (let* ((fmt org-columns-current-fmt-compiled)
  143. (beg (point-at-bol))
  144. (level-face (save-excursion
  145. (beginning-of-line 1)
  146. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  147. (org-get-level-face 2))))
  148. (ref-face (or level-face
  149. (and (eq major-mode 'org-agenda-mode)
  150. (get-text-property (point-at-bol) 'face))
  151. 'default))
  152. (color (list :foreground (face-attribute ref-face :foreground)))
  153. (face (list color 'org-column ref-face))
  154. (face1 (list color 'org-agenda-column-dateline ref-face))
  155. (cphr (get-text-property (point-at-bol) 'org-complex-heading-regexp))
  156. pom property ass width f string ov column val modval s2 title calc)
  157. ;; Check if the entry is in another buffer.
  158. (unless props
  159. (if (eq major-mode 'org-agenda-mode)
  160. (setq pom (or (org-get-at-bol 'org-hd-marker)
  161. (org-get-at-bol 'org-marker))
  162. props (if pom (org-entry-properties pom) nil))
  163. (setq props (org-entry-properties nil))))
  164. ;; Walk the format
  165. (while (setq column (pop fmt))
  166. (setq property (car column)
  167. title (nth 1 column)
  168. ass (if (equal property "ITEM")
  169. (cons "ITEM"
  170. ;; When in a buffer, get the whole line,
  171. ;; we'll clean it later…
  172. (if (derived-mode-p 'org-mode)
  173. (save-match-data
  174. (org-remove-tabs
  175. (buffer-substring-no-properties
  176. (point-at-bol) (point-at-eol))))
  177. ;; In agenda, just get the `txt' property
  178. (or (org-get-at-bol 'txt)
  179. (buffer-substring-no-properties
  180. (point) (progn (end-of-line) (point))))))
  181. (assoc property props))
  182. width (or (cdr (assoc property org-columns-current-maxwidths))
  183. (nth 2 column)
  184. (length property))
  185. f (format "%%-%d.%ds | " width width)
  186. calc (nth 7 column)
  187. val (or (cdr ass) "")
  188. modval (cond ((and org-columns-modify-value-for-display-function
  189. (functionp
  190. org-columns-modify-value-for-display-function))
  191. (funcall org-columns-modify-value-for-display-function
  192. title val))
  193. ((equal property "ITEM")
  194. (org-columns-cleanup-item
  195. val org-columns-current-fmt-compiled
  196. (or org-complex-heading-regexp cphr)))
  197. ((and calc (functionp calc)
  198. (not (string= val ""))
  199. (not (get-text-property 0 'org-computed val)))
  200. (org-columns-number-to-string
  201. (funcall calc (org-columns-string-to-number
  202. val (nth 4 column)))
  203. (nth 4 column)))))
  204. (setq s2 (org-columns-add-ellipses (or modval val) width))
  205. (setq string (format f s2))
  206. ;; Create the overlay
  207. (org-unmodified
  208. (setq ov (org-columns-new-overlay
  209. beg (setq beg (1+ beg)) string (if dateline face1 face)))
  210. (overlay-put ov 'keymap org-columns-map)
  211. (overlay-put ov 'org-columns-key property)
  212. (overlay-put ov 'org-columns-value (cdr ass))
  213. (overlay-put ov 'org-columns-value-modified modval)
  214. (overlay-put ov 'org-columns-pom pom)
  215. (overlay-put ov 'org-columns-format f)
  216. (overlay-put ov 'line-prefix "")
  217. (overlay-put ov 'wrap-prefix ""))
  218. (if (or (not (char-after beg))
  219. (equal (char-after beg) ?\n))
  220. (let ((inhibit-read-only t))
  221. (save-excursion
  222. (goto-char beg)
  223. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  224. ;; Make the rest of the line disappear.
  225. (org-unmodified
  226. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  227. (overlay-put ov 'invisible t)
  228. (overlay-put ov 'keymap org-columns-map)
  229. (overlay-put ov 'intangible t)
  230. (overlay-put ov 'line-prefix "")
  231. (overlay-put ov 'wrap-prefix "")
  232. (push ov org-columns-overlays)
  233. (setq ov (make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  234. (overlay-put ov 'keymap org-columns-map)
  235. (push ov org-columns-overlays)
  236. (let ((inhibit-read-only t))
  237. (put-text-property (max (point-min) (1- (point-at-bol)))
  238. (min (point-max) (1+ (point-at-eol)))
  239. 'read-only "Type `e' to edit property")))))
  240. (defun org-columns-add-ellipses (string width)
  241. "Truncate STRING with WIDTH characters, with ellipses."
  242. (cond
  243. ((<= (length string) width) string)
  244. ((<= width (length org-columns-ellipses))
  245. (substring org-columns-ellipses 0 width))
  246. (t (concat (substring string 0 (- width (length org-columns-ellipses)))
  247. org-columns-ellipses))))
  248. (defvar org-columns-full-header-line-format nil
  249. "The full header line format, will be shifted by horizontal scrolling." )
  250. (defvar org-previous-header-line-format nil
  251. "The header line format before column view was turned on.")
  252. (defvar org-columns-inhibit-recalculation nil
  253. "Inhibit recomputing of columns on column view startup.")
  254. (defvar org-columns-flyspell-was-active nil
  255. "Remember the state of `flyspell-mode' before column view.
  256. Flyspell-mode can cause problems in columns view, so it is turned off
  257. for the duration of the command.")
  258. (defvar header-line-format)
  259. (defvar org-columns-previous-hscroll 0)
  260. (defun org-columns-display-here-title ()
  261. "Overlay the newline before the current line with the table title."
  262. (interactive)
  263. (let ((fmt org-columns-current-fmt-compiled)
  264. string (title "")
  265. property width f column str widths)
  266. (while (setq column (pop fmt))
  267. (setq property (car column)
  268. str (or (nth 1 column) property)
  269. width (or (cdr (assoc property org-columns-current-maxwidths))
  270. (nth 2 column)
  271. (length str))
  272. widths (push width widths)
  273. f (format "%%-%d.%ds | " width width)
  274. string (format f str)
  275. title (concat title string)))
  276. (setq title (concat
  277. (org-add-props " " nil 'display '(space :align-to 0))
  278. ;;(org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
  279. (org-add-props title nil 'face 'org-column-title)))
  280. (org-set-local 'org-previous-header-line-format header-line-format)
  281. (org-set-local 'org-columns-current-widths (nreverse widths))
  282. (setq org-columns-full-header-line-format title)
  283. (setq org-columns-previous-hscroll -1)
  284. ; (org-columns-hscoll-title)
  285. (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
  286. (defun org-columns-hscoll-title ()
  287. "Set the `header-line-format' so that it scrolls along with the table."
  288. (sit-for .0001) ; need to force a redisplay to update window-hscroll
  289. (when (not (= (window-hscroll) org-columns-previous-hscroll))
  290. (setq header-line-format
  291. (concat (substring org-columns-full-header-line-format 0 1)
  292. (substring org-columns-full-header-line-format
  293. (1+ (window-hscroll))))
  294. org-columns-previous-hscroll (window-hscroll))
  295. (force-mode-line-update)))
  296. (defvar org-colview-initial-truncate-line-value nil
  297. "Remember the value of `truncate-lines' across colview.")
  298. (defun org-columns-remove-overlays ()
  299. "Remove all currently active column overlays."
  300. (interactive)
  301. (when (marker-buffer org-columns-begin-marker)
  302. (with-current-buffer (marker-buffer org-columns-begin-marker)
  303. (when (local-variable-p 'org-previous-header-line-format)
  304. (setq header-line-format org-previous-header-line-format)
  305. (kill-local-variable 'org-previous-header-line-format)
  306. (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
  307. (move-marker org-columns-begin-marker nil)
  308. (move-marker org-columns-top-level-marker nil)
  309. (org-unmodified
  310. (mapc 'delete-overlay org-columns-overlays)
  311. (setq org-columns-overlays nil)
  312. (let ((inhibit-read-only t))
  313. (remove-text-properties (point-min) (point-max) '(read-only t))))
  314. (when org-columns-flyspell-was-active
  315. (flyspell-mode 1))
  316. (when (local-variable-p 'org-colview-initial-truncate-line-value)
  317. (setq truncate-lines org-colview-initial-truncate-line-value)))))
  318. (defun org-columns-cleanup-item (item fmt cphr)
  319. "Remove from ITEM what is a column in the format FMT.
  320. CPHR is the complex heading regexp to use for parsing ITEM."
  321. (let (fixitem)
  322. (if (not cphr)
  323. item
  324. (unless (string-match "^\*+ " item)
  325. (setq item (concat "* " item) fixitem t))
  326. (if (string-match cphr item)
  327. (setq item
  328. (concat
  329. (org-add-props (match-string 1 item) nil
  330. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  331. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  332. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  333. " " (save-match-data (org-columns-compact-links (or (match-string 4 item) "")))
  334. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))
  335. (add-text-properties
  336. 0 (1+ (match-end 1))
  337. (list 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  338. item))
  339. (if fixitem (replace-regexp-in-string "^\*+ " "" item) item))))
  340. (defun org-columns-compact-links (s)
  341. "Replace [[link][desc]] with [desc] or [link]."
  342. (while (string-match org-bracket-link-regexp s)
  343. (setq s (replace-match
  344. (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
  345. t t s)))
  346. s)
  347. (defun org-columns-show-value ()
  348. "Show the full value of the property."
  349. (interactive)
  350. (let ((value (get-char-property (point) 'org-columns-value)))
  351. (message "Value is: %s" (or value ""))))
  352. (defvar org-agenda-columns-active) ;; defined in org-agenda.el
  353. (defun org-columns-quit ()
  354. "Remove the column overlays and in this way exit column editing."
  355. (interactive)
  356. (org-unmodified
  357. (org-columns-remove-overlays)
  358. (let ((inhibit-read-only t))
  359. (remove-text-properties (point-min) (point-max) '(read-only t))))
  360. (when (eq major-mode 'org-agenda-mode)
  361. (setq org-agenda-columns-active nil)
  362. (message
  363. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  364. (defun org-columns-check-computed ()
  365. "Check if this column value is computed.
  366. If yes, throw an error indicating that changing it does not make sense."
  367. (let ((val (get-char-property (point) 'org-columns-value)))
  368. (when (and (stringp val)
  369. (get-char-property 0 'org-computed val))
  370. (error "This value is computed from the entry's children"))))
  371. (defun org-columns-todo (&optional arg)
  372. "Change the TODO state during column view."
  373. (interactive "P")
  374. (org-columns-edit-value "TODO"))
  375. (defun org-columns-set-tags-or-toggle (&optional arg)
  376. "Toggle checkbox at point, or set tags for current headline."
  377. (interactive "P")
  378. (if (string-match "\\`\\[[ xX-]\\]\\'"
  379. (get-char-property (point) 'org-columns-value))
  380. (org-columns-next-allowed-value)
  381. (org-columns-edit-value "TAGS")))
  382. (defun org-columns-edit-value (&optional key)
  383. "Edit the value of the property at point in column view.
  384. Where possible, use the standard interface for changing this line."
  385. (interactive)
  386. (org-columns-check-computed)
  387. (let* ((col (current-column))
  388. (key (or key (get-char-property (point) 'org-columns-key)))
  389. (value (get-char-property (point) 'org-columns-value))
  390. (bol (point-at-bol)) (eol (point-at-eol))
  391. (pom (or (get-text-property bol 'org-hd-marker)
  392. (point))) ; keep despite of compiler waring
  393. (line-overlays
  394. (delq nil (mapcar (lambda (x)
  395. (and (eq (overlay-buffer x) (current-buffer))
  396. (>= (overlay-start x) bol)
  397. (<= (overlay-start x) eol)
  398. x))
  399. org-columns-overlays)))
  400. (org-columns-time (time-to-number-of-days (current-time)))
  401. nval eval allowed)
  402. (cond
  403. ((equal key "CLOCKSUM")
  404. (error "This special column cannot be edited"))
  405. ((equal key "ITEM")
  406. (setq eval '(org-with-point-at pom
  407. (org-edit-headline))))
  408. ((equal key "TODO")
  409. (setq eval '(org-with-point-at
  410. pom
  411. (call-interactively 'org-todo))))
  412. ((equal key "PRIORITY")
  413. (setq eval '(org-with-point-at pom
  414. (call-interactively 'org-priority))))
  415. ((equal key "TAGS")
  416. (setq eval '(org-with-point-at pom
  417. (let ((org-fast-tag-selection-single-key
  418. (if (eq org-fast-tag-selection-single-key 'expert)
  419. t org-fast-tag-selection-single-key)))
  420. (call-interactively 'org-set-tags)))))
  421. ((equal key "DEADLINE")
  422. (setq eval '(org-with-point-at pom
  423. (call-interactively 'org-deadline))))
  424. ((equal key "SCHEDULED")
  425. (setq eval '(org-with-point-at pom
  426. (call-interactively 'org-schedule))))
  427. ((equal key "BEAMER_env")
  428. (setq eval '(org-with-point-at pom
  429. (call-interactively 'org-beamer-select-environment))))
  430. (t
  431. (setq allowed (org-property-get-allowed-values pom key 'table))
  432. (if allowed
  433. (setq nval (org-icompleting-read
  434. "Value: " allowed nil
  435. (not (get-text-property 0 'org-unrestricted
  436. (caar allowed)))))
  437. (setq nval (read-string "Edit: " value)))
  438. (setq nval (org-trim nval))
  439. (when (not (equal nval value))
  440. (setq eval '(org-entry-put pom key nval)))))
  441. (when eval
  442. (cond
  443. ((equal major-mode 'org-agenda-mode)
  444. (org-columns-eval eval)
  445. ;; The following let preserves the current format, and makes sure
  446. ;; that in only a single file things need to be updated.
  447. (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
  448. (buffer (marker-buffer pom))
  449. (org-agenda-contributing-files
  450. (list (with-current-buffer buffer
  451. (buffer-file-name (buffer-base-buffer))))))
  452. (org-agenda-columns)))
  453. (t
  454. (let ((inhibit-read-only t))
  455. (org-unmodified
  456. (remove-text-properties
  457. (max (point-min) (1- bol)) eol '(read-only t)))
  458. (unwind-protect
  459. (progn
  460. (setq org-columns-overlays
  461. (org-delete-all line-overlays org-columns-overlays))
  462. (mapc 'delete-overlay line-overlays)
  463. (org-columns-eval eval))
  464. (org-columns-display-here)))
  465. (org-move-to-column col)
  466. (if (and (derived-mode-p 'org-mode)
  467. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  468. (org-columns-update key)))))))
  469. (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
  470. "Edit the current headline, the part without TODO keyword, TAGS."
  471. (org-back-to-heading)
  472. (when (looking-at org-todo-line-regexp)
  473. (let ((pos (point))
  474. (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  475. (txt (match-string 3))
  476. (post "")
  477. txt2)
  478. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@#%]+:[ \t]*$") txt)
  479. (setq post (match-string 0 txt)
  480. txt (substring txt 0 (match-beginning 0))))
  481. (setq txt2 (read-string "Edit: " txt))
  482. (when (not (equal txt txt2))
  483. (goto-char pos)
  484. (insert pre txt2 post)
  485. (delete-region (point) (point-at-eol))
  486. (org-set-tags nil t)))))
  487. (defun org-columns-edit-allowed ()
  488. "Edit the list of allowed values for the current property."
  489. (interactive)
  490. (let* ((pom (or (org-get-at-bol 'org-marker)
  491. (org-get-at-bol 'org-hd-marker)
  492. (point)))
  493. (key (get-char-property (point) 'org-columns-key))
  494. (key1 (concat key "_ALL"))
  495. (allowed (org-entry-get pom key1 t))
  496. nval)
  497. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  498. ;; FIXME: Write back to #+PROPERTY setting if that is needed.
  499. (setq nval (read-string "Allowed: " allowed))
  500. (org-entry-put
  501. (cond ((marker-position org-entry-property-inherited-from)
  502. org-entry-property-inherited-from)
  503. ((marker-position org-columns-top-level-marker)
  504. org-columns-top-level-marker)
  505. (t pom))
  506. key1 nval)))
  507. (defun org-columns-eval (form)
  508. (let (hidep)
  509. (save-excursion
  510. (beginning-of-line 1)
  511. ;; `next-line' is needed here, because it skips invisible line.
  512. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  513. (setq hidep (org-at-heading-p 1)))
  514. (eval form)
  515. (and hidep (hide-entry))))
  516. (defun org-columns-previous-allowed-value ()
  517. "Switch to the previous allowed value for this column."
  518. (interactive)
  519. (org-columns-next-allowed-value t))
  520. (defun org-columns-next-allowed-value (&optional previous nth)
  521. "Switch to the next allowed value for this column.
  522. When PREVIOUS is set, go to the previous value. When NTH is
  523. an integer, select that value."
  524. (interactive)
  525. (org-columns-check-computed)
  526. (let* ((col (current-column))
  527. (key (get-char-property (point) 'org-columns-key))
  528. (value (get-char-property (point) 'org-columns-value))
  529. (bol (point-at-bol)) (eol (point-at-eol))
  530. (pom (or (get-text-property bol 'org-hd-marker)
  531. (point))) ; keep despite of compiler waring
  532. (line-overlays
  533. (delq nil (mapcar (lambda (x)
  534. (and (eq (overlay-buffer x) (current-buffer))
  535. (>= (overlay-start x) bol)
  536. (<= (overlay-start x) eol)
  537. x))
  538. org-columns-overlays)))
  539. (allowed (or (org-property-get-allowed-values pom key)
  540. (and (memq
  541. (nth 4 (assoc key org-columns-current-fmt-compiled))
  542. '(checkbox checkbox-n-of-m checkbox-percent))
  543. '("[ ]" "[X]"))
  544. (org-colview-construct-allowed-dates value)))
  545. nval)
  546. (when (integerp nth)
  547. (setq nth (1- nth))
  548. (if (= nth -1) (setq nth 9)))
  549. (when (equal key "ITEM")
  550. (error "Cannot edit item headline from here"))
  551. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  552. (error "Allowed values for this property have not been defined"))
  553. (if (member key '("SCHEDULED" "DEADLINE"))
  554. (setq nval (if previous 'earlier 'later))
  555. (if previous (setq allowed (reverse allowed)))
  556. (cond
  557. (nth
  558. (setq nval (nth nth allowed))
  559. (if (not nval)
  560. (error "There are only %d allowed values for property `%s'"
  561. (length allowed) key)))
  562. ((member value allowed)
  563. (setq nval (or (car (cdr (member value allowed)))
  564. (car allowed)))
  565. (if (equal nval value)
  566. (error "Only one allowed value for this property")))
  567. (t (setq nval (car allowed)))))
  568. (cond
  569. ((equal major-mode 'org-agenda-mode)
  570. (org-columns-eval '(org-entry-put pom key nval))
  571. ;; The following let preserves the current format, and makes sure
  572. ;; that in only a single file things need to be updated.
  573. (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
  574. (buffer (marker-buffer pom))
  575. (org-agenda-contributing-files
  576. (list (with-current-buffer buffer
  577. (buffer-file-name (buffer-base-buffer))))))
  578. (org-agenda-columns)))
  579. (t
  580. (let ((inhibit-read-only t))
  581. (remove-text-properties (1- bol) eol '(read-only t))
  582. (unwind-protect
  583. (progn
  584. (setq org-columns-overlays
  585. (org-delete-all line-overlays org-columns-overlays))
  586. (mapc 'delete-overlay line-overlays)
  587. (org-columns-eval '(org-entry-put pom key nval)))
  588. (org-columns-display-here)))
  589. (org-move-to-column col)
  590. (and (nth 3 (assoc key org-columns-current-fmt-compiled))
  591. (org-columns-update key))))))
  592. (defun org-colview-construct-allowed-dates (s)
  593. "Construct a list of three dates around the date in S.
  594. This respects the format of the time stamp in S, active or non-active,
  595. and also including time or not. S must be just a time stamp, no text
  596. around it."
  597. (when (and s (string-match (concat "^" org-ts-regexp3 "$") s))
  598. (let* ((time (org-parse-time-string s 'nodefaults))
  599. (active (equal (string-to-char s) ?<))
  600. (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
  601. time-before time-after)
  602. (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  603. (setf (car time) (or (car time) 0))
  604. (setf (nth 1 time) (or (nth 1 time) 0))
  605. (setf (nth 2 time) (or (nth 2 time) 0))
  606. (setq time-before (copy-sequence time))
  607. (setq time-after (copy-sequence time))
  608. (setf (nth 3 time-before) (1- (nth 3 time)))
  609. (setf (nth 3 time-after) (1+ (nth 3 time)))
  610. (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
  611. (list time-before time time-after)))))
  612. (defun org-verify-version (task)
  613. (cond
  614. ((eq task 'columns)
  615. (if (or (featurep 'xemacs)
  616. (< emacs-major-version 22))
  617. (error "Emacs 22 is required for the columns feature")))))
  618. (defun org-columns-open-link (&optional arg)
  619. (interactive "P")
  620. (let ((value (get-char-property (point) 'org-columns-value)))
  621. (org-open-link-from-string value arg)))
  622. (defun org-columns-get-format-and-top-level ()
  623. (let ((fmt (org-columns-get-format)))
  624. (org-columns-goto-top-level)
  625. fmt))
  626. (defun org-columns-get-format (&optional fmt-string)
  627. (interactive)
  628. (let (fmt-as-property fmt)
  629. (when (condition-case nil (org-back-to-heading) (error nil))
  630. (setq fmt-as-property (org-entry-get nil "COLUMNS" t)))
  631. (setq fmt (or fmt-string fmt-as-property org-columns-default-format))
  632. (org-set-local 'org-columns-current-fmt fmt)
  633. (org-columns-compile-format fmt)
  634. fmt))
  635. (defun org-columns-goto-top-level ()
  636. (when (condition-case nil (org-back-to-heading) (error nil))
  637. (org-entry-get nil "COLUMNS" t)
  638. (if (marker-position org-entry-property-inherited-from)
  639. (move-marker org-columns-top-level-marker org-entry-property-inherited-from)
  640. (move-marker org-columns-top-level-marker (point)))))
  641. (defun org-columns (&optional columns-fmt-string)
  642. "Turn on column view on an org-mode file.
  643. When COLUMNS-FMT-STRING is non-nil, use it as the column format."
  644. (interactive)
  645. (org-verify-version 'columns)
  646. (org-columns-remove-overlays)
  647. (move-marker org-columns-begin-marker (point))
  648. (let ((org-columns-time (time-to-number-of-days (current-time)))
  649. beg end fmt cache maxwidths)
  650. (org-columns-goto-top-level)
  651. (setq fmt (org-columns-get-format columns-fmt-string))
  652. (save-excursion
  653. (goto-char org-columns-top-level-marker)
  654. (setq beg (point))
  655. (unless org-columns-inhibit-recalculation
  656. (org-columns-compute-all))
  657. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  658. (point-max)))
  659. ;; Get and cache the properties
  660. (goto-char beg)
  661. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  662. (save-excursion
  663. (save-restriction
  664. (narrow-to-region beg end)
  665. (org-clock-sum))))
  666. (when (assoc "CLOCKSUM_T" org-columns-current-fmt-compiled)
  667. (save-excursion
  668. (save-restriction
  669. (narrow-to-region beg end)
  670. (org-clock-sum-today))))
  671. (while (re-search-forward org-outline-regexp-bol end t)
  672. (if (and org-columns-skip-archived-trees
  673. (looking-at (concat ".*:" org-archive-tag ":")))
  674. (org-end-of-subtree t)
  675. (push (cons (org-current-line) (org-entry-properties)) cache)))
  676. (when cache
  677. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  678. (org-set-local 'org-columns-current-maxwidths maxwidths)
  679. (org-columns-display-here-title)
  680. (when (org-set-local 'org-columns-flyspell-was-active
  681. (org-bound-and-true-p flyspell-mode))
  682. (flyspell-mode 0))
  683. (unless (local-variable-p 'org-colview-initial-truncate-line-value)
  684. (org-set-local 'org-colview-initial-truncate-line-value
  685. truncate-lines))
  686. (setq truncate-lines t)
  687. (mapc (lambda (x)
  688. (org-goto-line (car x))
  689. (org-columns-display-here (cdr x)))
  690. cache)))))
  691. (eval-when-compile (defvar org-columns-time))
  692. (defvar org-columns-compile-map
  693. '(("none" none +)
  694. (":" add_times +)
  695. ("+" add_numbers +)
  696. ("$" currency +)
  697. ("X" checkbox +)
  698. ("X/" checkbox-n-of-m +)
  699. ("X%" checkbox-percent +)
  700. ("max" max_numbers max)
  701. ("min" min_numbers min)
  702. ("mean" mean_numbers
  703. (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
  704. (":max" max_times max)
  705. (":min" min_times min)
  706. (":mean" mean_times
  707. (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
  708. ("@min" min_age min (lambda (x) (- org-columns-time x)))
  709. ("@max" max_age max (lambda (x) (- org-columns-time x)))
  710. ("@mean" mean_age
  711. (lambda (&rest x) (/ (apply '+ x) (float (length x))))
  712. (lambda (x) (- org-columns-time x)))
  713. ("est+" estimate org-estimate-combine))
  714. "Operator <-> format,function,calc map.
  715. Used to compile/uncompile columns format and completing read in
  716. interactive function `org-columns-new'.
  717. operator string used in #+COLUMNS definition describing the
  718. summary type
  719. format symbol describing summary type selected interactively in
  720. `org-columns-new' and internally in
  721. `org-columns-number-to-string' and
  722. `org-columns-string-to-number'
  723. function called with a list of values as argument to calculate
  724. the summary value
  725. calc function called on every element before summarizing. This is
  726. optional and should only be specified if needed")
  727. (defun org-columns-new (&optional prop title width op fmt fun &rest rest)
  728. "Insert a new column, to the left of the current column."
  729. (interactive)
  730. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  731. cell)
  732. (setq prop (org-icompleting-read
  733. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  734. nil nil prop))
  735. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  736. (setq width (read-string "Column width: " (if width (number-to-string width))))
  737. (if (string-match "\\S-" width)
  738. (setq width (string-to-number width))
  739. (setq width nil))
  740. (setq fmt (org-icompleting-read
  741. "Summary [none]: "
  742. (mapcar (lambda (x) (list (symbol-name (cadr x))))
  743. org-columns-compile-map)
  744. nil t))
  745. (setq fmt (intern fmt)
  746. fun (cdr (assoc fmt (mapcar 'cdr org-columns-compile-map))))
  747. (if (eq fmt 'none) (setq fmt nil))
  748. (if editp
  749. (progn
  750. (setcar editp prop)
  751. (setcdr editp (list title width nil fmt nil fun)))
  752. (setq cell (nthcdr (1- (current-column))
  753. org-columns-current-fmt-compiled))
  754. (setcdr cell (cons (list prop title width nil fmt nil
  755. (car fun) (cadr fun))
  756. (cdr cell))))
  757. (org-columns-store-format)
  758. (org-columns-redo)))
  759. (defun org-columns-delete ()
  760. "Delete the column at point from columns view."
  761. (interactive)
  762. (let* ((n (current-column))
  763. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  764. (when (y-or-n-p
  765. (format "Are you sure you want to remove column \"%s\"? " title))
  766. (setq org-columns-current-fmt-compiled
  767. (delq (nth n org-columns-current-fmt-compiled)
  768. org-columns-current-fmt-compiled))
  769. (org-columns-store-format)
  770. (org-columns-redo)
  771. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  772. (backward-char 1)))))
  773. (defun org-columns-edit-attributes ()
  774. "Edit the attributes of the current column."
  775. (interactive)
  776. (let* ((n (current-column))
  777. (info (nth n org-columns-current-fmt-compiled)))
  778. (apply 'org-columns-new info)))
  779. (defun org-columns-widen (arg)
  780. "Make the column wider by ARG characters."
  781. (interactive "p")
  782. (let* ((n (current-column))
  783. (entry (nth n org-columns-current-fmt-compiled))
  784. (width (or (nth 2 entry)
  785. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  786. (setq width (max 1 (+ width arg)))
  787. (setcar (nthcdr 2 entry) width)
  788. (org-columns-store-format)
  789. (org-columns-redo)))
  790. (defun org-columns-narrow (arg)
  791. "Make the column narrower by ARG characters."
  792. (interactive "p")
  793. (org-columns-widen (- arg)))
  794. (defun org-columns-move-right ()
  795. "Swap this column with the one to the right."
  796. (interactive)
  797. (let* ((n (current-column))
  798. (cell (nthcdr n org-columns-current-fmt-compiled))
  799. e)
  800. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  801. (error "Cannot shift this column further to the right"))
  802. (setq e (car cell))
  803. (setcar cell (car (cdr cell)))
  804. (setcdr cell (cons e (cdr (cdr cell))))
  805. (org-columns-store-format)
  806. (org-columns-redo)
  807. (forward-char 1)))
  808. (defun org-columns-move-left ()
  809. "Swap this column with the one to the left."
  810. (interactive)
  811. (let* ((n (current-column)))
  812. (when (= n 0)
  813. (error "Cannot shift this column further to the left"))
  814. (backward-char 1)
  815. (org-columns-move-right)
  816. (backward-char 1)))
  817. (defun org-columns-store-format ()
  818. "Store the text version of the current columns format in appropriate place.
  819. This is either in the COLUMNS property of the node starting the current column
  820. display, or in the #+COLUMNS line of the current buffer."
  821. (let (fmt (cnt 0))
  822. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  823. (org-set-local 'org-columns-current-fmt fmt)
  824. (if (marker-position org-columns-top-level-marker)
  825. (save-excursion
  826. (goto-char org-columns-top-level-marker)
  827. (if (and (org-at-heading-p)
  828. (org-entry-get nil "COLUMNS"))
  829. (org-entry-put nil "COLUMNS" fmt)
  830. (goto-char (point-min))
  831. ;; Overwrite all #+COLUMNS lines....
  832. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  833. (setq cnt (1+ cnt))
  834. (replace-match (concat "#+COLUMNS: " fmt) t t))
  835. (unless (> cnt 0)
  836. (goto-char (point-min))
  837. (or (org-at-heading-p t) (outline-next-heading))
  838. (let ((inhibit-read-only t))
  839. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  840. (org-set-local 'org-columns-default-format fmt))))))
  841. (defvar org-agenda-overriding-columns-format nil
  842. "When set, overrides any other format definition for the agenda.
  843. Don't set this, this is meant for dynamic scoping.")
  844. (defun org-columns-get-autowidth-alist (s cache)
  845. "Derive the maximum column widths from the format and the cache."
  846. (let ((start 0) rtn)
  847. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  848. (push (cons (match-string 1 s) 1) rtn)
  849. (setq start (match-end 0)))
  850. (mapc (lambda (x)
  851. (setcdr x (apply 'max
  852. (mapcar
  853. (lambda (y)
  854. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  855. cache))))
  856. rtn)
  857. rtn))
  858. (defun org-columns-compute-all ()
  859. "Compute all columns that have operators defined."
  860. (org-unmodified
  861. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  862. (let ((columns org-columns-current-fmt-compiled)
  863. (org-columns-time (time-to-number-of-days (current-time)))
  864. col)
  865. (while (setq col (pop columns))
  866. (when (nth 3 col)
  867. (save-excursion
  868. (org-columns-compute (car col)))))))
  869. (defun org-columns-update (property)
  870. "Recompute PROPERTY, and update the columns display for it."
  871. (org-columns-compute property)
  872. (let (fmt val pos)
  873. (save-excursion
  874. (mapc (lambda (ov)
  875. (when (equal (overlay-get ov 'org-columns-key) property)
  876. (setq pos (overlay-start ov))
  877. (goto-char pos)
  878. (when (setq val (cdr (assoc property
  879. (get-text-property
  880. (point-at-bol) 'org-summaries))))
  881. (setq fmt (overlay-get ov 'org-columns-format))
  882. (overlay-put ov 'org-columns-value val)
  883. (overlay-put ov 'display (format fmt val)))))
  884. org-columns-overlays))))
  885. (defvar org-inlinetask-min-level
  886. (if (featurep 'org-inlinetask) org-inlinetask-min-level 15))
  887. (defun org-columns-compute (property)
  888. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  889. (interactive)
  890. (let* ((re org-outline-regexp-bol)
  891. (lmax 30) ; Does anyone use deeper levels???
  892. (lvals (make-vector lmax nil))
  893. (lflag (make-vector lmax nil))
  894. (level 0)
  895. (ass (assoc property org-columns-current-fmt-compiled))
  896. (format (nth 4 ass))
  897. (printf (nth 5 ass))
  898. (fun (nth 6 ass))
  899. (calc (or (nth 7 ass) 'identity))
  900. (beg org-columns-top-level-marker)
  901. (inminlevel org-inlinetask-min-level)
  902. (last-level org-inlinetask-min-level)
  903. val valflag flag end sumpos sum-alist sum str str1 useval)
  904. (save-excursion
  905. ;; Find the region to compute
  906. (goto-char beg)
  907. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  908. (goto-char end)
  909. ;; Walk the tree from the back and do the computations
  910. (while (re-search-backward re beg t)
  911. (setq sumpos (match-beginning 0)
  912. last-level (if (not (or (zerop level) (eq level inminlevel)))
  913. level last-level)
  914. level (org-outline-level)
  915. val (org-entry-get nil property)
  916. valflag (and val (string-match "\\S-" val)))
  917. (cond
  918. ((< level last-level)
  919. ;; put the sum of lower levels here as a property
  920. (setq sum (+ (if (and (/= last-level inminlevel)
  921. (aref lvals last-level))
  922. (apply fun (aref lvals last-level)) 0)
  923. (if (aref lvals inminlevel)
  924. (apply fun (aref lvals inminlevel)) 0))
  925. flag (or (aref lflag last-level) ; any valid entries from children?
  926. (aref lflag inminlevel)) ; or inline tasks?
  927. str (org-columns-number-to-string sum format printf)
  928. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  929. useval (if flag str1 (if valflag val ""))
  930. sum-alist (get-text-property sumpos 'org-summaries))
  931. (if (assoc property sum-alist)
  932. (setcdr (assoc property sum-alist) useval)
  933. (push (cons property useval) sum-alist)
  934. (org-unmodified
  935. (add-text-properties sumpos (1+ sumpos)
  936. (list 'org-summaries sum-alist))))
  937. (when (and val (not (equal val (if flag str val))))
  938. (org-entry-put nil property (if flag str val)))
  939. ;; add current to current level accumulator
  940. (when (or flag valflag)
  941. (push (if flag
  942. sum
  943. (funcall calc (org-columns-string-to-number
  944. (if flag str val) format)))
  945. (aref lvals level))
  946. (aset lflag level t))
  947. ;; clear accumulators for deeper levels
  948. (loop for l from (1+ level) to (1- lmax) do
  949. (aset lvals l nil)
  950. (aset lflag l nil)))
  951. ((>= level last-level)
  952. ;; add what we have here to the accumulator for this level
  953. (when valflag
  954. (push (funcall calc (org-columns-string-to-number val format))
  955. (aref lvals level))
  956. (aset lflag level t)))
  957. (t (error "This should not happen")))))))
  958. (defun org-columns-redo ()
  959. "Construct the column display again."
  960. (interactive)
  961. (message "Recomputing columns...")
  962. (let ((line (org-current-line))
  963. (col (current-column)))
  964. (save-excursion
  965. (if (marker-position org-columns-begin-marker)
  966. (goto-char org-columns-begin-marker))
  967. (org-columns-remove-overlays)
  968. (if (derived-mode-p 'org-mode)
  969. (call-interactively 'org-columns)
  970. (org-agenda-redo)
  971. (call-interactively 'org-agenda-columns)))
  972. (org-goto-line line)
  973. (move-to-column col))
  974. (message "Recomputing columns...done"))
  975. (defun org-columns-not-in-agenda ()
  976. (if (eq major-mode 'org-agenda-mode)
  977. (error "This command is only allowed in Org-mode buffers")))
  978. (defun org-string-to-number (s)
  979. "Convert string to number, and interpret hh:mm:ss."
  980. (if (not (string-match ":" s))
  981. (string-to-number s)
  982. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  983. (while l
  984. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  985. sum)))
  986. (defun org-columns-number-to-string (n fmt &optional printf)
  987. "Convert a computed column number to a string value, according to FMT."
  988. (cond
  989. ((memq fmt '(estimate)) (org-estimate-print n printf))
  990. ((not (numberp n)) "")
  991. ((memq fmt '(add_times max_times min_times mean_times))
  992. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  993. (format org-time-clocksum-format h m)))
  994. ((eq fmt 'checkbox)
  995. (cond ((= n (floor n)) "[X]")
  996. ((> n 1.) "[-]")
  997. (t "[ ]")))
  998. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  999. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  1000. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  1001. (printf (format printf n))
  1002. ((eq fmt 'currency)
  1003. (format "%.2f" n))
  1004. ((memq fmt '(min_age max_age mean_age))
  1005. (org-format-time-period n))
  1006. (t (number-to-string n))))
  1007. (defun org-nofm-to-completion (n m &optional percent)
  1008. (if (not percent)
  1009. (format "[%d/%d]" n m)
  1010. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  1011. (defun org-columns-string-to-number (s fmt)
  1012. "Convert a column value to a number that can be used for column computing."
  1013. (if s
  1014. (cond
  1015. ((memq fmt '(min_age max_age mean_age))
  1016. (cond ((string= s "") org-columns-time)
  1017. ((string-match
  1018. "\\([0-9]+\\)d \\([0-9]+\\)h \\([0-9]+\\)m \\([0-9]+\\)s"
  1019. s)
  1020. (+ (* 60 (+ (* 60 (+ (* 24 (string-to-number (match-string 1 s)))
  1021. (string-to-number (match-string 2 s))))
  1022. (string-to-number (match-string 3 s))))
  1023. (string-to-number (match-string 4 s))))
  1024. (t (time-to-number-of-days (apply 'encode-time
  1025. (org-parse-time-string s t))))))
  1026. ((string-match ":" s)
  1027. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  1028. (while l
  1029. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  1030. sum))
  1031. ((string-match (concat "\\([0-9.]+\\) *\\("
  1032. (regexp-opt (mapcar 'car org-effort-durations))
  1033. "\\)") s)
  1034. (setq s (concat "0:" (org-duration-string-to-minutes s t)))
  1035. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  1036. (while l
  1037. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  1038. sum))
  1039. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  1040. (if (equal s "[X]") 1. 0.000001))
  1041. ((memq fmt '(estimate)) (org-string-to-estimate s))
  1042. (t (string-to-number s)))))
  1043. (defun org-columns-uncompile-format (cfmt)
  1044. "Turn the compiled columns format back into a string representation."
  1045. (let ((rtn "") e s prop title op op-match width fmt printf fun calc)
  1046. (while (setq e (pop cfmt))
  1047. (setq prop (car e)
  1048. title (nth 1 e)
  1049. width (nth 2 e)
  1050. op (nth 3 e)
  1051. fmt (nth 4 e)
  1052. printf (nth 5 e)
  1053. fun (nth 6 e)
  1054. calc (nth 7 e))
  1055. (when (setq op-match (rassoc (list fmt fun calc) org-columns-compile-map))
  1056. (setq op (car op-match)))
  1057. (if (and op printf) (setq op (concat op ";" printf)))
  1058. (if (equal title prop) (setq title nil))
  1059. (setq s (concat "%" (if width (number-to-string width))
  1060. prop
  1061. (if title (concat "(" title ")"))
  1062. (if op (concat "{" op "}"))))
  1063. (setq rtn (concat rtn " " s)))
  1064. (org-trim rtn)))
  1065. (defun org-columns-compile-format (fmt)
  1066. "Turn a column format string into an alist of specifications.
  1067. The alist has one entry for each column in the format. The elements of
  1068. that list are:
  1069. property the property
  1070. title the title field for the columns
  1071. width the column width in characters, can be nil for automatic
  1072. operator the operator if any
  1073. format the output format for computed results, derived from operator
  1074. printf a printf format for computed values
  1075. fun the lisp function to compute summary values, derived from operator
  1076. calc function to get values from base elements"
  1077. (let ((start 0) width prop title op op-match f printf fun calc)
  1078. (setq org-columns-current-fmt-compiled nil)
  1079. (while (string-match
  1080. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  1081. fmt start)
  1082. (setq start (match-end 0)
  1083. width (match-string 1 fmt)
  1084. prop (match-string 2 fmt)
  1085. title (or (match-string 3 fmt) prop)
  1086. op (match-string 4 fmt)
  1087. f nil
  1088. printf nil
  1089. fun '+
  1090. calc nil)
  1091. (if width (setq width (string-to-number width)))
  1092. (when (and op (string-match ";" op))
  1093. (setq printf (substring op (match-end 0))
  1094. op (substring op 0 (match-beginning 0))))
  1095. (when (setq op-match (assoc op org-columns-compile-map))
  1096. (setq f (cadr op-match)
  1097. fun (caddr op-match)
  1098. calc (cadddr op-match)))
  1099. (push (list prop title width op f printf fun calc)
  1100. org-columns-current-fmt-compiled))
  1101. (setq org-columns-current-fmt-compiled
  1102. (nreverse org-columns-current-fmt-compiled))))
  1103. ;;; Dynamic block for Column view
  1104. (defvar org-heading-regexp) ; defined in org.el
  1105. (defvar org-heading-keyword-regexp-format) ; defined in org.el
  1106. (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
  1107. "Get the column view of the current buffer or subtree.
  1108. The first optional argument MAXLEVEL sets the level limit. A
  1109. second optional argument SKIP-EMPTY-ROWS tells whether to skip
  1110. empty rows, an empty row being one where all the column view
  1111. specifiers except ITEM are empty. This function returns a list
  1112. containing the title row and all other rows. Each row is a list
  1113. of fields."
  1114. (save-excursion
  1115. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  1116. (re-comment (format org-heading-keyword-regexp-format
  1117. org-comment-string))
  1118. (re-archive (concat ".*:" org-archive-tag ":"))
  1119. (n (length title)) row tbl)
  1120. (goto-char (point-min))
  1121. (while (re-search-forward org-heading-regexp nil t)
  1122. (catch 'next
  1123. (when (and (or (null maxlevel)
  1124. (>= maxlevel
  1125. (if org-odd-levels-only
  1126. (/ (1+ (length (match-string 1))) 2)
  1127. (length (match-string 1)))))
  1128. (get-char-property (match-beginning 0) 'org-columns-key))
  1129. (when (save-excursion
  1130. (goto-char (point-at-bol))
  1131. (or (looking-at re-comment)
  1132. (looking-at re-archive)))
  1133. (org-end-of-subtree t)
  1134. (throw 'next t))
  1135. (setq row nil)
  1136. (loop for i from 0 to (1- n) do
  1137. (push
  1138. (org-quote-vert
  1139. (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  1140. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  1141. ""))
  1142. row))
  1143. (setq row (nreverse row))
  1144. (unless (and skip-empty-rows
  1145. (eq 1 (length (delete "" (delete-dups (copy-sequence row))))))
  1146. (push row tbl)))))
  1147. (append (list title 'hline) (nreverse tbl)))))
  1148. (defun org-dblock-write:columnview (params)
  1149. "Write the column view table.
  1150. PARAMS is a property list of parameters:
  1151. :width enforce same column widths with <N> specifiers.
  1152. :id the :ID: property of the entry where the columns view
  1153. should be built. When the symbol `local', call locally.
  1154. When `global' call column view with the cursor at the beginning
  1155. of the buffer (usually this means that the whole buffer switches
  1156. to column view). When \"file:path/to/file.org\", invoke column
  1157. view at the start of that file. Otherwise, the ID is located
  1158. using `org-id-find'.
  1159. :hlines When t, insert a hline before each item. When a number, insert
  1160. a hline before each level <= that number.
  1161. :vlines When t, make each column a colgroup to enforce vertical lines.
  1162. :maxlevel When set to a number, don't capture headlines below this level.
  1163. :skip-empty-rows
  1164. When t, skip rows where all specifiers other than ITEM are empty.
  1165. :format When non-nil, specify the column view format to use."
  1166. (let ((pos (move-marker (make-marker) (point)))
  1167. (hlines (plist-get params :hlines))
  1168. (vlines (plist-get params :vlines))
  1169. (maxlevel (plist-get params :maxlevel))
  1170. (content-lines (org-split-string (plist-get params :content) "\n"))
  1171. (skip-empty-rows (plist-get params :skip-empty-rows))
  1172. (columns-fmt (plist-get params :format))
  1173. (case-fold-search t)
  1174. tbl id idpos nfields tmp recalc line
  1175. id-as-string view-file view-pos)
  1176. (when (setq id (plist-get params :id))
  1177. (setq id-as-string (cond ((numberp id) (number-to-string id))
  1178. ((symbolp id) (symbol-name id))
  1179. ((stringp id) id)
  1180. (t "")))
  1181. (cond ((not id) nil)
  1182. ((eq id 'global) (setq view-pos (point-min)))
  1183. ((eq id 'local))
  1184. ((string-match "^file:\\(.*\\)" id-as-string)
  1185. (setq view-file (match-string 1 id-as-string)
  1186. view-pos 1)
  1187. (unless (file-exists-p view-file)
  1188. (error "No such file: \"%s\"" id-as-string)))
  1189. ((setq idpos (org-find-entry-with-id id))
  1190. (setq view-pos idpos))
  1191. ((setq idpos (org-id-find id))
  1192. (setq view-file (car idpos))
  1193. (setq view-pos (cdr idpos)))
  1194. (t (error "Cannot find entry with :ID: %s" id))))
  1195. (with-current-buffer (if view-file
  1196. (get-file-buffer view-file)
  1197. (current-buffer))
  1198. (save-excursion
  1199. (save-restriction
  1200. (widen)
  1201. (goto-char (or view-pos (point)))
  1202. (org-columns columns-fmt)
  1203. (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
  1204. (setq nfields (length (car tbl)))
  1205. (org-columns-quit))))
  1206. (goto-char pos)
  1207. (move-marker pos nil)
  1208. (when tbl
  1209. (when (plist-get params :hlines)
  1210. (setq tmp nil)
  1211. (while tbl
  1212. (if (eq (car tbl) 'hline)
  1213. (push (pop tbl) tmp)
  1214. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  1215. (if (and (not (eq (car tmp) 'hline))
  1216. (or (eq hlines t)
  1217. (and (numberp hlines)
  1218. (<= (- (match-end 1) (match-beginning 1))
  1219. hlines))))
  1220. (push 'hline tmp)))
  1221. (push (pop tbl) tmp)))
  1222. (setq tbl (nreverse tmp)))
  1223. (when vlines
  1224. (setq tbl (mapcar (lambda (x)
  1225. (if (eq 'hline x) x (cons "" x)))
  1226. tbl))
  1227. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  1228. (setq pos (point))
  1229. (when content-lines
  1230. (while (string-match "^#" (car content-lines))
  1231. (insert (pop content-lines) "\n")))
  1232. (insert (org-listtable-to-string tbl))
  1233. (when (plist-get params :width)
  1234. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  1235. org-columns-current-widths "|")))
  1236. (while (setq line (pop content-lines))
  1237. (when (string-match "^#" line)
  1238. (insert "\n" line)
  1239. (when (string-match "^[ \t]*#\\+tblfm" line)
  1240. (setq recalc t))))
  1241. (if recalc
  1242. (progn (goto-char pos) (org-table-recalculate 'all))
  1243. (goto-char pos)
  1244. (org-table-align)))))
  1245. (defun org-listtable-to-string (tbl)
  1246. "Convert a listtable TBL to a string that contains the Org-mode table.
  1247. The table still need to be aligned. The resulting string has no leading
  1248. and tailing newline characters."
  1249. (mapconcat
  1250. (lambda (x)
  1251. (cond
  1252. ((listp x)
  1253. (concat "|" (mapconcat 'identity x "|") "|"))
  1254. ((eq x 'hline) "|-|")
  1255. (t (error "Garbage in listtable: %s" x))))
  1256. tbl "\n"))
  1257. (defun org-insert-columns-dblock ()
  1258. "Create a dynamic block capturing a column view table."
  1259. (interactive)
  1260. (let ((defaults '(:name "columnview" :hlines 1))
  1261. (id (org-icompleting-read
  1262. "Capture columns (local, global, entry with :ID: property) [local]: "
  1263. (append '(("global") ("local"))
  1264. (mapcar 'list (org-property-values "ID"))))))
  1265. (if (equal id "") (setq id 'local))
  1266. (if (equal id "global") (setq id 'global))
  1267. (setq defaults (append defaults (list :id id)))
  1268. (org-create-dblock defaults)
  1269. (org-update-dblock)))
  1270. ;;; Column view in the agenda
  1271. (defvar org-agenda-view-columns-initially nil
  1272. "When set, switch to columns view immediately after creating the agenda.")
  1273. (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
  1274. (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
  1275. (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
  1276. (defun org-agenda-columns ()
  1277. "Turn on or update column view in the agenda."
  1278. (interactive)
  1279. (org-verify-version 'columns)
  1280. (org-columns-remove-overlays)
  1281. (move-marker org-columns-begin-marker (point))
  1282. (let ((org-columns-time (time-to-number-of-days (current-time)))
  1283. cache maxwidths m p a d fmt)
  1284. (cond
  1285. ((and (boundp 'org-agenda-overriding-columns-format)
  1286. org-agenda-overriding-columns-format)
  1287. (setq fmt org-agenda-overriding-columns-format)
  1288. (org-set-local 'org-agenda-overriding-columns-format fmt))
  1289. ((setq m (org-get-at-bol 'org-hd-marker))
  1290. (setq fmt (or (org-entry-get m "COLUMNS" t)
  1291. (with-current-buffer (marker-buffer m)
  1292. org-columns-default-format))))
  1293. ((and (boundp 'org-columns-current-fmt)
  1294. (local-variable-p 'org-columns-current-fmt)
  1295. org-columns-current-fmt)
  1296. (setq fmt org-columns-current-fmt))
  1297. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  1298. (setq m (get-text-property m 'org-hd-marker))
  1299. (setq fmt (or (org-entry-get m "COLUMNS" t)
  1300. (with-current-buffer (marker-buffer m)
  1301. org-columns-default-format)))))
  1302. (setq fmt (or fmt org-columns-default-format))
  1303. (org-set-local 'org-columns-current-fmt fmt)
  1304. (org-columns-compile-format fmt)
  1305. (when org-agenda-columns-compute-summary-properties
  1306. (org-agenda-colview-compute org-columns-current-fmt-compiled))
  1307. (save-excursion
  1308. ;; Get and cache the properties
  1309. (goto-char (point-min))
  1310. (while (not (eobp))
  1311. (when (setq m (or (org-get-at-bol 'org-hd-marker)
  1312. (org-get-at-bol 'org-marker)))
  1313. (setq p (org-entry-properties m))
  1314. (when (or (not (setq a (assoc org-effort-property p)))
  1315. (not (string-match "\\S-" (or (cdr a) ""))))
  1316. ;; OK, the property is not defined. Use appointment duration?
  1317. (when (and org-agenda-columns-add-appointments-to-effort-sum
  1318. (setq d (get-text-property (point) 'duration)))
  1319. (setq d (org-minutes-to-hh:mm-string d))
  1320. (put-text-property 0 (length d) 'face 'org-warning d)
  1321. (push (cons org-effort-property d) p)))
  1322. (push (cons (org-current-line) p) cache))
  1323. (beginning-of-line 2))
  1324. (when cache
  1325. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  1326. (org-set-local 'org-columns-current-maxwidths maxwidths)
  1327. (org-columns-display-here-title)
  1328. (when (org-set-local 'org-columns-flyspell-was-active
  1329. (org-bound-and-true-p flyspell-mode))
  1330. (flyspell-mode 0))
  1331. (mapc (lambda (x)
  1332. (org-goto-line (car x))
  1333. (org-columns-display-here (cdr x)))
  1334. cache)
  1335. (when org-agenda-columns-show-summaries
  1336. (org-agenda-colview-summarize cache))))))
  1337. (defun org-agenda-colview-summarize (cache)
  1338. "Summarize the summarizable columns in column view in the agenda.
  1339. This will add overlays to the date lines, to show the summary for each day."
  1340. (let* ((fmt (mapcar (lambda (x)
  1341. (if (string-match "CLOCKSUM.*" (car x))
  1342. (list (match-string 0) (nth 1 x) (nth 2 x) ":" 'add_times
  1343. nil '+ nil)
  1344. x))
  1345. org-columns-current-fmt-compiled))
  1346. line c c1 stype calc sumfunc props lsum entries prop v title)
  1347. (catch 'exit
  1348. (when (delq nil (mapcar 'cadr fmt))
  1349. ;; OK, at least one summation column, it makes sense to try this
  1350. (goto-char (point-max))
  1351. (while t
  1352. (when (or (get-text-property (point) 'org-date-line)
  1353. (eq (get-text-property (point) 'face)
  1354. 'org-agenda-structure))
  1355. ;; OK, this is a date line that should be used
  1356. (setq line (org-current-line))
  1357. (setq entries nil c cache cache nil)
  1358. (while (setq c1 (pop c))
  1359. (if (> (car c1) line)
  1360. (push c1 entries)
  1361. (push c1 cache)))
  1362. ;; now ENTRIES are the ones we want to use, CACHE is the rest
  1363. ;; Compute the summaries for the properties we want,
  1364. ;; set nil properties for the rest.
  1365. (when (setq entries (mapcar 'cdr entries))
  1366. (setq props
  1367. (mapcar
  1368. (lambda (f)
  1369. (setq prop (car f)
  1370. title (nth 1 f)
  1371. stype (nth 4 f)
  1372. sumfunc (nth 6 f)
  1373. calc (or (nth 7 f) 'identity))
  1374. (cond
  1375. ((equal prop "ITEM")
  1376. (cons prop (buffer-substring (point-at-bol)
  1377. (point-at-eol))))
  1378. ((not stype) (cons prop ""))
  1379. (t ;; do the summary
  1380. (setq lsum nil)
  1381. (dolist (x entries)
  1382. (setq v (cdr (assoc prop x)))
  1383. (if v
  1384. (push
  1385. (funcall
  1386. (if (not (get-text-property 0 'org-computed v))
  1387. calc
  1388. 'identity)
  1389. (org-columns-string-to-number
  1390. v stype))
  1391. lsum)))
  1392. (setq lsum (remove nil lsum))
  1393. (setq lsum
  1394. (cond ((> (length lsum) 1)
  1395. (org-columns-number-to-string
  1396. (apply sumfunc lsum) stype))
  1397. ((eq (length lsum) 1)
  1398. (org-columns-number-to-string
  1399. (car lsum) stype))
  1400. (t "")))
  1401. (put-text-property 0 (length lsum) 'face 'bold lsum)
  1402. (unless (eq calc 'identity)
  1403. (put-text-property 0 (length lsum) 'org-computed t lsum))
  1404. (cons prop lsum))))
  1405. fmt))
  1406. (org-columns-display-here props 'dateline)
  1407. (org-set-local 'org-agenda-columns-active t)))
  1408. (if (bobp) (throw 'exit t))
  1409. (beginning-of-line 0))))))
  1410. (defun org-agenda-colview-compute (fmt)
  1411. "Compute the relevant columns in the contributing source buffers."
  1412. (let ((files org-agenda-contributing-files)
  1413. (org-columns-begin-marker (make-marker))
  1414. (org-columns-top-level-marker (make-marker))
  1415. f fm a b)
  1416. (while (setq f (pop files))
  1417. (setq b (find-buffer-visiting f))
  1418. (with-current-buffer (or (buffer-base-buffer b) b)
  1419. (save-excursion
  1420. (save-restriction
  1421. (widen)
  1422. (org-unmodified
  1423. (remove-text-properties (point-min) (point-max)
  1424. '(org-summaries t)))
  1425. (goto-char (point-min))
  1426. (org-columns-get-format-and-top-level)
  1427. (while (setq fm (pop fmt))
  1428. (cond ((equal (car fm) "CLOCKSUM")
  1429. (org-clock-sum))
  1430. ((equal (car fm) "CLOCKSUM_T")
  1431. (org-clock-sum-today))
  1432. ((and (nth 4 fm)
  1433. (setq a (assoc (car fm)
  1434. org-columns-current-fmt-compiled))
  1435. (equal (nth 4 a) (nth 4 fm)))
  1436. (org-columns-compute (car fm)))))))))))
  1437. (defun org-format-time-period (interval)
  1438. "Convert time in fractional days to days/hours/minutes/seconds."
  1439. (if (numberp interval)
  1440. (let* ((days (floor interval))
  1441. (frac-hours (* 24 (- interval days)))
  1442. (hours (floor frac-hours))
  1443. (minutes (floor (* 60 (- frac-hours hours))))
  1444. (seconds (floor (* 60 (- (* 60 (- frac-hours hours)) minutes)))))
  1445. (format "%dd %02dh %02dm %02ds" days hours minutes seconds))
  1446. ""))
  1447. (defun org-estimate-mean-and-var (v)
  1448. "Return the mean and variance of an estimate."
  1449. (let* ((low (float (car v)))
  1450. (high (float (cadr v)))
  1451. (mean (/ (+ low high) 2.0))
  1452. (var (/ (+ (expt (- mean low) 2.0) (expt (- high mean) 2.0)) 2.0)))
  1453. (list mean var)))
  1454. (defun org-estimate-combine (&rest el)
  1455. "Combine a list of estimates, using mean and variance.
  1456. The mean and variance of the result will be the sum of the means
  1457. and variances (respectively) of the individual estimates."
  1458. (let ((mean 0)
  1459. (var 0))
  1460. (mapc (lambda (e)
  1461. (let ((stats (org-estimate-mean-and-var e)))
  1462. (setq mean (+ mean (car stats)))
  1463. (setq var (+ var (cadr stats)))))
  1464. el)
  1465. (let ((stdev (sqrt var)))
  1466. (list (- mean stdev) (+ mean stdev)))))
  1467. (defun org-estimate-print (e &optional fmt)
  1468. "Prepare a string representation of an estimate.
  1469. This formats these numbers as two numbers with a \"-\" between them."
  1470. (if (null fmt) (set 'fmt "%.0f"))
  1471. (format "%s" (mapconcat (lambda (n) (format fmt n)) e "-")))
  1472. (defun org-string-to-estimate (s)
  1473. "Convert a string to an estimate.
  1474. The string should be two numbers joined with a \"-\"."
  1475. (if (string-match "\\(.*\\)-\\(.*\\)" s)
  1476. (list (string-to-number (match-string 1 s))
  1477. (string-to-number(match-string 2 s)))
  1478. (list (string-to-number s) (string-to-number s))))
  1479. (provide 'org-colview)
  1480. ;;; org-colview.el ends here