org-colview.el 59 KB

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