org-colview.el 66 KB

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