org-colview.el 57 KB

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