org-colview.el 60 KB

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