org-colview.el 58 KB

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