org-colview.el 63 KB

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