org-colview.el 63 KB

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