org-colview.el 64 KB

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