org-colview.el 57 KB

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