org-colview.el 59 KB

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