org-colview.el 57 KB

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