org-colview.el 62 KB

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