org-colview.el 62 KB

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