org-colview.el 58 KB

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