org-colview.el 64 KB

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