org-colview.el 63 KB

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