org-colview.el 60 KB

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