org-colview.el 59 KB

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