org-colview.el 62 KB

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