org-colview.el 60 KB

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