org-colview.el 64 KB

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