org-colview.el 60 KB

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