Improve `org-fix-tags-on-the-fly'
[org-mode/org-tableheadings.git] / lisp / org-colview.el
blobcf631482c16d37ec7ed3662ffe4e4c1354e00249
1 ;;; org-colview.el --- Column View in Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2018 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: https://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the column view for Org.
29 ;;; Code:
31 (require 'cl-lib)
32 (require 'org)
34 (declare-function org-agenda-redo "org-agenda" (&optional all))
35 (declare-function org-agenda-do-context-action "org-agenda" ())
36 (declare-function org-clock-sum-today "org-clock" (&optional headline-filter))
37 (declare-function org-element-extract-element "org-element" (element))
38 (declare-function org-element-interpret-data "org-element" (data))
39 (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion with-affiliated))
40 (declare-function org-element-parse-secondary-string "org-element" (string restriction &optional parent))
41 (declare-function org-element-property "org-element" (property element))
42 (declare-function org-element-restriction "org-element" (element))
43 (declare-function org-element-type "org-element" (element))
45 (defvar org-agenda-columns-add-appointments-to-effort-sum)
46 (defvar org-agenda-columns-compute-summary-properties)
47 (defvar org-agenda-columns-show-summaries)
48 (defvar org-agenda-view-columns-initially)
49 (defvar org-inlinetask-min-level)
52 ;;; Configuration
54 (defcustom org-columns-modify-value-for-display-function nil
55 "Function that modifies values for display in column view.
56 For example, it can be used to cut out a certain part from a time stamp.
57 The function must take 2 arguments:
59 column-title The title of the column (*not* the property name)
60 value The value that should be modified.
62 The function should return the value that should be displayed,
63 or nil if the normal value should be used."
64 :group 'org-properties
65 :type '(choice (const nil) (function)))
67 (defcustom org-columns-summary-types nil
68 "Alist between operators and summarize functions.
70 Each association follows the pattern (LABEL . SUMMARIZE),
71 or (LABEL SUMMARIZE COLLECT) where
73 LABEL is a string used in #+COLUMNS definition describing the
74 summary type. It can contain any character but \"}\". It is
75 case-sensitive.
77 SUMMARIZE is a function called with two arguments. The first
78 argument is a non-empty list of values, as non-empty strings.
79 The second one is a format string or nil. It has to return
80 a string summarizing the list of values.
82 COLLECT is a function called with one argument, a property
83 name. It is called in the context of a headline and must
84 return the collected property, or the empty string. You can
85 use this to only collect a property if a related conditional
86 properties is set, e.g., to return VACATION_DAYS only if
87 CONFIRMED is true.
89 Note that the return value can become one value for an higher
90 order summary, so the function is expected to handle its own
91 output.
93 Types defined in this variable take precedence over those defined
94 in `org-columns-summary-types-default', which see."
95 :group 'org-properties
96 :version "26.1"
97 :package-version '(Org . "9.0")
98 :type '(alist :key-type (string :tag " Label")
99 :value-type
100 (choice (function :tag "Summarize")
101 (list :tag "Collect and summarize"
102 (function :tag "Summarize")
103 (function :tag "Collect")))))
107 ;;; Column View
109 (defvar-local org-columns-overlays nil
110 "Holds the list of current column overlays.")
112 (defvar-local org-columns-current-fmt nil
113 "Local variable, holds the currently active column format.")
115 (defvar-local org-columns-current-fmt-compiled nil
116 "Local variable, holds the currently active column format.
117 This is the compiled version of the format.")
119 (defvar-local org-columns-current-maxwidths nil
120 "Currently active maximum column widths, as a vector.")
122 (defvar-local org-columns-begin-marker nil
123 "Points to the position where last a column creation command was called.")
125 (defvar-local org-columns-top-level-marker nil
126 "Points to the position where current columns region starts.")
128 (defvar org-columns--time 0.0
129 "Number of seconds since the epoch, as a floating point number.")
131 (defvar org-columns-map (make-sparse-keymap)
132 "The keymap valid in column display.")
134 (defconst org-columns-summary-types-default
135 '(("+" . org-columns--summary-sum)
136 ("$" . org-columns--summary-currencies)
137 ("X" . org-columns--summary-checkbox)
138 ("X/" . org-columns--summary-checkbox-count)
139 ("X%" . org-columns--summary-checkbox-percent)
140 ("max" . org-columns--summary-max)
141 ("mean" . org-columns--summary-mean)
142 ("min" . org-columns--summary-min)
143 (":" . org-columns--summary-sum-times)
144 (":max" . org-columns--summary-max-time)
145 (":mean" . org-columns--summary-mean-time)
146 (":min" . org-columns--summary-min-time)
147 ("@max" . org-columns--summary-max-age)
148 ("@mean" . org-columns--summary-mean-age)
149 ("@min" . org-columns--summary-min-age)
150 ("est+" . org-columns--summary-estimate))
151 "Map operators to summarize functions.
152 See `org-columns-summary-types' for details.")
154 (defun org-columns-content ()
155 "Switch to contents view while in columns view."
156 (interactive)
157 (org-overview)
158 (org-content))
160 (org-defkey org-columns-map "c" 'org-columns-content)
161 (org-defkey org-columns-map "o" 'org-overview)
162 (org-defkey org-columns-map "e" 'org-columns-edit-value)
163 (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
164 (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
165 (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
166 (org-defkey org-columns-map "v" 'org-columns-show-value)
167 (org-defkey org-columns-map "q" 'org-columns-quit)
168 (org-defkey org-columns-map "r" 'org-columns-redo)
169 (org-defkey org-columns-map "g" 'org-columns-redo)
170 (org-defkey org-columns-map [left] 'backward-char)
171 (org-defkey org-columns-map "\M-b" 'backward-char)
172 (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
173 (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
174 (org-defkey org-columns-map "\M-f"
175 (lambda () (interactive) (goto-char (1+ (point)))))
176 (org-defkey org-columns-map [right]
177 (lambda () (interactive) (goto-char (1+ (point)))))
178 (org-defkey org-columns-map [down]
179 (lambda () (interactive)
180 (let ((col (current-column)))
181 (beginning-of-line 2)
182 (while (and (org-invisible-p2) (not (eobp)))
183 (beginning-of-line 2))
184 (move-to-column col)
185 (if (eq major-mode 'org-agenda-mode)
186 (org-agenda-do-context-action)))))
187 (org-defkey org-columns-map [up]
188 (lambda () (interactive)
189 (let ((col (current-column)))
190 (beginning-of-line 0)
191 (while (and (org-invisible-p2) (not (bobp)))
192 (beginning-of-line 0))
193 (move-to-column col)
194 (if (eq major-mode 'org-agenda-mode)
195 (org-agenda-do-context-action)))))
196 (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
197 (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
198 (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
199 (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
200 (org-defkey org-columns-map "<" 'org-columns-narrow)
201 (org-defkey org-columns-map ">" 'org-columns-widen)
202 (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
203 (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
204 (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
205 (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
206 (dotimes (i 10)
207 (org-defkey org-columns-map (number-to-string i)
208 `(lambda () (interactive)
209 (org-columns-next-allowed-value nil ,i))))
211 (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
212 '("Column"
213 ["Edit property" org-columns-edit-value t]
214 ["Next allowed value" org-columns-next-allowed-value t]
215 ["Previous allowed value" org-columns-previous-allowed-value t]
216 ["Show full value" org-columns-show-value t]
217 ["Edit allowed values" org-columns-edit-allowed t]
218 "--"
219 ["Edit column attributes" org-columns-edit-attributes t]
220 ["Increase column width" org-columns-widen t]
221 ["Decrease column width" org-columns-narrow t]
222 "--"
223 ["Move column right" org-columns-move-right t]
224 ["Move column left" org-columns-move-left t]
225 ["Add column" org-columns-new t]
226 ["Delete column" org-columns-delete t]
227 "--"
228 ["CONTENTS" org-columns-content t]
229 ["OVERVIEW" org-overview t]
230 ["Refresh columns display" org-columns-redo t]
231 "--"
232 ["Open link" org-columns-open-link t]
233 "--"
234 ["Quit" org-columns-quit t]))
236 (defun org-columns--displayed-value (spec value)
237 "Return displayed value for specification SPEC in current entry.
238 SPEC is a column format specification as stored in
239 `org-columns-current-fmt-compiled'. VALUE is the real value to
240 display, as a string."
241 (or (and (functionp org-columns-modify-value-for-display-function)
242 (funcall org-columns-modify-value-for-display-function
243 (nth 1 spec) ;column name
244 value))
245 (pcase spec
246 (`("ITEM" . ,_)
247 (concat (make-string (1- (org-current-level))
248 (if org-hide-leading-stars ?\s ?*))
249 "* "
250 (org-columns-compact-links value)))
251 (`(,_ ,_ ,_ ,_ nil) value)
252 ;; If PRINTF is set, assume we are displaying a number and
253 ;; obey to the format string.
254 (`(,_ ,_ ,_ ,_ ,printf) (format printf (string-to-number value)))
255 (_ (error "Invalid column specification format: %S" spec)))))
257 (defun org-columns--collect-values (&optional compiled-fmt)
258 "Collect values for columns on the current line.
260 Return a list of triplets (SPEC VALUE DISPLAYED) suitable for
261 `org-columns--display-here'.
263 This function assumes `org-columns-current-fmt-compiled' is
264 initialized is set in the current buffer. However, it is
265 possible to override it with optional argument COMPILED-FMT."
266 (let ((summaries (get-text-property (point) 'org-summaries)))
267 (mapcar
268 (lambda (spec)
269 (pcase spec
270 (`(,p . ,_)
271 (let* ((v (or (cdr (assoc spec summaries))
272 (org-entry-get (point) p 'selective t)
273 (and compiled-fmt ;assume `org-agenda-columns'
274 ;; Effort property is not defined. Try
275 ;; to use appointment duration.
276 org-agenda-columns-add-appointments-to-effort-sum
277 (string= p (upcase org-effort-property))
278 (get-text-property (point) 'duration)
279 (propertize (org-duration-from-minutes
280 (get-text-property (point) 'duration))
281 'face 'org-warning))
282 "")))
283 (list spec v (org-columns--displayed-value spec v))))))
284 (or compiled-fmt org-columns-current-fmt-compiled))))
286 (defun org-columns--set-widths (cache)
287 "Compute the maximum column widths from the format and CACHE.
288 This function sets `org-columns-current-maxwidths' as a vector of
289 integers greater than 0."
290 (setq org-columns-current-maxwidths
291 (apply #'vector
292 (mapcar
293 (lambda (spec)
294 (pcase spec
295 (`(,_ ,_ ,(and width (pred wholenump)) . ,_) width)
296 (`(,_ ,name . ,_)
297 ;; No width is specified in the columns format.
298 ;; Compute it by checking all possible values for
299 ;; PROPERTY.
300 (let ((width (length name)))
301 (dolist (entry cache width)
302 (let ((value (nth 2 (assoc spec (cdr entry)))))
303 (setq width (max (length value) width))))))))
304 org-columns-current-fmt-compiled))))
306 (defun org-columns--new-overlay (beg end &optional string face)
307 "Create a new column overlay and add it to the list."
308 (let ((ov (make-overlay beg end)))
309 (overlay-put ov 'face (or face 'secondary-selection))
310 (org-overlay-display ov string face)
311 (push ov org-columns-overlays)
312 ov))
314 (defun org-columns--summarize (operator)
315 "Return summary function associated to string OPERATOR."
316 (pcase (or (assoc operator org-columns-summary-types)
317 (assoc operator org-columns-summary-types-default))
318 (`nil (error "Unknown %S operator" operator))
319 (`(,_ . ,(and (pred functionp) summarize)) summarize)
320 (`(,_ ,summarize ,_) summarize)
321 (_ (error "Invalid definition for operator %S" operator))))
323 (defun org-columns--collect (operator)
324 "Return collect function associated to string OPERATOR.
325 Return nil if no collect function is associated to OPERATOR."
326 (pcase (or (assoc operator org-columns-summary-types)
327 (assoc operator org-columns-summary-types-default))
328 (`nil (error "Unknown %S operator" operator))
329 (`(,_ . ,(pred functionp)) nil) ;default value
330 (`(,_ ,_ ,collect) collect)
331 (_ (error "Invalid definition for operator %S" operator))))
333 (defun org-columns--overlay-text (value fmt width property original)
334 "Return text "
335 (format fmt
336 (let ((v (org-columns-add-ellipses value width)))
337 (pcase property
338 ("PRIORITY"
339 (propertize v 'face (org-get-priority-face original)))
340 ("TAGS"
341 (if (not org-tags-special-faces-re)
342 (propertize v 'face 'org-tag)
343 (replace-regexp-in-string
344 org-tags-special-faces-re
345 (lambda (m) (propertize m 'face (org-get-tag-face m)))
346 v nil nil 1)))
347 ("TODO" (propertize v 'face (org-get-todo-face original)))
348 (_ v)))))
350 (defun org-columns--display-here (columns &optional dateline)
351 "Overlay the current line with column display.
352 COLUMNS is an alist (SPEC VALUE DISPLAYED). Optional argument
353 DATELINE is non-nil when the face used should be
354 `org-agenda-column-dateline'."
355 (save-excursion
356 (beginning-of-line)
357 (let* ((level-face (and (looking-at "\\(\\**\\)\\(\\* \\)")
358 (org-get-level-face 2)))
359 (ref-face (or level-face
360 (and (eq major-mode 'org-agenda-mode)
361 (org-get-at-bol 'face))
362 'default))
363 (color (list :foreground (face-attribute ref-face :foreground)))
364 (font (list :height (face-attribute 'default :height)
365 :family (face-attribute 'default :family)))
366 (face (list color font 'org-column ref-face))
367 (face1 (list color font 'org-agenda-column-dateline ref-face)))
368 ;; Each column is an overlay on top of a character. So there has
369 ;; to be at least as many characters available on the line as
370 ;; columns to display.
371 (let ((columns (length org-columns-current-fmt-compiled))
372 (chars (- (line-end-position) (line-beginning-position))))
373 (when (> columns chars)
374 (save-excursion
375 (end-of-line)
376 (let ((inhibit-read-only t))
377 (insert (make-string (- columns chars) ?\s))))))
378 ;; Display columns. Create and install the overlay for the
379 ;; current column on the next character.
380 (let ((i 0)
381 (last (1- (length columns))))
382 (dolist (column columns)
383 (pcase column
384 (`(,spec ,original ,value)
385 (let* ((property (car spec))
386 (width (aref org-columns-current-maxwidths i))
387 (fmt (format (if (= i last) "%%-%d.%ds |"
388 "%%-%d.%ds | ")
389 width width))
390 (ov (org-columns--new-overlay
391 (point) (1+ (point))
392 (org-columns--overlay-text
393 value fmt width property original)
394 (if dateline face1 face))))
395 (overlay-put ov 'keymap org-columns-map)
396 (overlay-put ov 'org-columns-key property)
397 (overlay-put ov 'org-columns-value original)
398 (overlay-put ov 'org-columns-value-modified value)
399 (overlay-put ov 'org-columns-format fmt)
400 (overlay-put ov 'line-prefix "")
401 (overlay-put ov 'wrap-prefix "")
402 (forward-char))))
403 (cl-incf i)))
404 ;; Make the rest of the line disappear.
405 (let ((ov (org-columns--new-overlay (point) (line-end-position))))
406 (overlay-put ov 'invisible t)
407 (overlay-put ov 'keymap org-columns-map)
408 (overlay-put ov 'line-prefix "")
409 (overlay-put ov 'wrap-prefix ""))
410 (let ((ov (make-overlay (1- (line-end-position))
411 (line-beginning-position 2))))
412 (overlay-put ov 'keymap org-columns-map)
413 (push ov org-columns-overlays))
414 (org-with-silent-modifications
415 (let ((inhibit-read-only t))
416 (put-text-property
417 (line-end-position 0)
418 (line-beginning-position 2)
419 'read-only
420 (substitute-command-keys
421 "Type \\<org-columns-map>`\\[org-columns-edit-value]' \
422 to edit property")))))))
424 (defun org-columns-add-ellipses (string width)
425 "Truncate STRING with WIDTH characters, with ellipses."
426 (cond
427 ((<= (length string) width) string)
428 ((<= width (length org-columns-ellipses))
429 (substring org-columns-ellipses 0 width))
430 (t (concat (substring string 0 (- width (length org-columns-ellipses)))
431 org-columns-ellipses))))
433 (defvar org-columns-full-header-line-format nil
434 "The full header line format, will be shifted by horizontal scrolling." )
435 (defvar org-previous-header-line-format nil
436 "The header line format before column view was turned on.")
437 (defvar org-columns-inhibit-recalculation nil
438 "Inhibit recomputing of columns on column view startup.")
439 (defvar org-columns-flyspell-was-active nil
440 "Remember the state of `flyspell-mode' before column view.
441 Flyspell-mode can cause problems in columns view, so it is turned off
442 for the duration of the command.")
444 (defvar header-line-format)
445 (defvar org-columns-previous-hscroll 0)
447 (defun org-columns--display-here-title ()
448 "Overlay the newline before the current line with the table title."
449 (interactive)
450 (let ((title "")
451 (linum-offset (org-line-number-display-width 'columns))
452 (i 0))
453 (dolist (column org-columns-current-fmt-compiled)
454 (pcase column
455 (`(,property ,name . ,_)
456 (let* ((width (aref org-columns-current-maxwidths i))
457 (fmt (format "%%-%d.%ds | " width width)))
458 (setq title (concat title (format fmt (or name property)))))))
459 (cl-incf i))
460 (setq-local org-previous-header-line-format header-line-format)
461 (setq org-columns-full-header-line-format
462 (concat
463 (org-add-props " " nil 'display `(space :align-to ,linum-offset))
464 (org-add-props (substring title 0 -1) nil 'face 'org-column-title)))
465 (setq org-columns-previous-hscroll -1)
466 (add-hook 'post-command-hook 'org-columns-hscroll-title nil 'local)))
468 (defun org-columns-hscroll-title ()
469 "Set the `header-line-format' so that it scrolls along with the table."
470 (sit-for .0001) ; need to force a redisplay to update window-hscroll
471 (when (not (= (window-hscroll) org-columns-previous-hscroll))
472 (setq header-line-format
473 (concat (substring org-columns-full-header-line-format 0 1)
474 (substring org-columns-full-header-line-format
475 (1+ (window-hscroll))))
476 org-columns-previous-hscroll (window-hscroll))
477 (force-mode-line-update)))
479 (defvar org-colview-initial-truncate-line-value nil
480 "Remember the value of `truncate-lines' across colview.")
482 ;;;###autoload
483 (defun org-columns-remove-overlays ()
484 "Remove all currently active column overlays."
485 (interactive)
486 (when org-columns-overlays
487 (when (local-variable-p 'org-previous-header-line-format)
488 (setq header-line-format org-previous-header-line-format)
489 (kill-local-variable 'org-previous-header-line-format)
490 (remove-hook 'post-command-hook 'org-columns-hscroll-title 'local))
491 (set-marker org-columns-begin-marker nil)
492 (when (markerp org-columns-top-level-marker)
493 (set-marker org-columns-top-level-marker nil))
494 (org-with-silent-modifications
495 (mapc #'delete-overlay org-columns-overlays)
496 (setq org-columns-overlays nil)
497 (let ((inhibit-read-only t))
498 (remove-text-properties (point-min) (point-max) '(read-only t))))
499 (when org-columns-flyspell-was-active
500 (flyspell-mode 1))
501 (when (local-variable-p 'org-colview-initial-truncate-line-value)
502 (setq truncate-lines org-colview-initial-truncate-line-value))))
504 (defun org-columns-compact-links (s)
505 "Replace [[link][desc]] with [desc] or [link]."
506 (while (string-match org-bracket-link-regexp s)
507 (setq s (replace-match
508 (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
509 t t s)))
512 (defun org-columns-show-value ()
513 "Show the full value of the property."
514 (interactive)
515 (let ((value (get-char-property (point) 'org-columns-value)))
516 (message "Value is: %s" (or value ""))))
518 (defvar org-agenda-columns-active) ;; defined in org-agenda.el
520 (defun org-columns-quit ()
521 "Remove the column overlays and in this way exit column editing."
522 (interactive)
523 (org-with-silent-modifications
524 (org-columns-remove-overlays)
525 (let ((inhibit-read-only t))
526 (remove-text-properties (point-min) (point-max) '(read-only t))))
527 (if (not (eq major-mode 'org-agenda-mode))
528 (setq org-columns-current-fmt nil)
529 (setq org-agenda-columns-active nil)
530 (message
531 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
533 (defun org-columns-check-computed ()
534 "Throw an error if current column value is computed."
535 (let ((spec (nth (current-column) org-columns-current-fmt-compiled)))
536 (and
537 (nth 3 spec)
538 (assoc spec (get-text-property (line-beginning-position) 'org-summaries))
539 (error "This value is computed from the entry's children"))))
541 (defun org-columns-todo (&optional _arg)
542 "Change the TODO state during column view."
543 (interactive "P")
544 (org-columns-edit-value "TODO"))
546 (defun org-columns-set-tags-or-toggle (&optional _arg)
547 "Toggle checkbox at point, or set tags for current headline."
548 (interactive "P")
549 (if (string-match "\\`\\[[ xX-]\\]\\'"
550 (get-char-property (point) 'org-columns-value))
551 (org-columns-next-allowed-value)
552 (org-columns-edit-value "TAGS")))
554 (defvar org-agenda-overriding-columns-format nil
555 "When set, overrides any other format definition for the agenda.
556 Don't set this, this is meant for dynamic scoping.")
558 (defun org-columns-edit-value (&optional key)
559 "Edit the value of the property at point in column view.
560 Where possible, use the standard interface for changing this line."
561 (interactive)
562 (org-columns-check-computed)
563 (let* ((col (current-column))
564 (bol (line-beginning-position))
565 (eol (line-end-position))
566 (pom (or (get-text-property bol 'org-hd-marker) (point)))
567 (key (or key (get-char-property (point) 'org-columns-key)))
568 (org-columns--time (float-time (current-time)))
569 (action
570 (pcase key
571 ("CLOCKSUM"
572 (error "This special column cannot be edited"))
573 ("ITEM"
574 (lambda () (org-with-point-at pom (org-edit-headline))))
575 ("TODO"
576 (lambda ()
577 (org-with-point-at pom (call-interactively #'org-todo))))
578 ("PRIORITY"
579 (lambda ()
580 (org-with-point-at pom
581 (call-interactively #'org-priority))))
582 ("TAGS"
583 (lambda ()
584 (org-with-point-at pom
585 (let ((org-fast-tag-selection-single-key
586 (if (eq org-fast-tag-selection-single-key 'expert)
588 org-fast-tag-selection-single-key)))
589 (call-interactively #'org-set-tags-command)))))
590 ("DEADLINE"
591 (lambda ()
592 (org-with-point-at pom (call-interactively #'org-deadline))))
593 ("SCHEDULED"
594 (lambda ()
595 (org-with-point-at pom (call-interactively #'org-schedule))))
596 ("BEAMER_ENV"
597 (lambda ()
598 (org-with-point-at pom
599 (call-interactively #'org-beamer-select-environment))))
601 (let* ((allowed (org-property-get-allowed-values pom key 'table))
602 (value (get-char-property (point) 'org-columns-value))
603 (nval (org-trim
604 (if (null allowed) (read-string "Edit: " value)
605 (completing-read
606 "Value: " allowed nil
607 (not (get-text-property
608 0 'org-unrestricted (caar allowed))))))))
609 (and (not (equal nval value))
610 (lambda () (org-entry-put pom key nval))))))))
611 (cond
612 ((null action))
613 ((eq major-mode 'org-agenda-mode)
614 (org-columns--call action)
615 ;; The following let preserves the current format, and makes
616 ;; sure that in only a single file things need to be updated.
617 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
618 (buffer (marker-buffer pom))
619 (org-agenda-contributing-files
620 (list (with-current-buffer buffer
621 (buffer-file-name (buffer-base-buffer))))))
622 (org-agenda-columns)))
624 (let ((inhibit-read-only t))
625 (org-with-silent-modifications
626 (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t)))
627 (org-columns--call action))
628 ;; Some properties can modify headline (e.g., "TODO"), and
629 ;; possible shuffle overlays. Make sure they are still all at
630 ;; the right place on the current line.
631 (let ((org-columns-inhibit-recalculation)) (org-columns-redo))
632 (org-columns-update key)
633 (org-move-to-column col)))))
635 (defun org-columns-edit-allowed ()
636 "Edit the list of allowed values for the current property."
637 (interactive)
638 (let* ((pom (or (org-get-at-bol 'org-marker)
639 (org-get-at-bol 'org-hd-marker)
640 (point)))
641 (key (concat (or (get-char-property (point) 'org-columns-key)
642 (user-error "No column to edit at point"))
643 "_ALL"))
644 (allowed (org-entry-get pom key t))
645 (new-value (read-string "Allowed: " allowed)))
646 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
647 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
648 (org-entry-put
649 (cond ((marker-position org-entry-property-inherited-from)
650 org-entry-property-inherited-from)
651 ((marker-position org-columns-top-level-marker)
652 org-columns-top-level-marker)
653 (t pom))
654 key new-value)))
656 (defun org-columns--call (fun)
657 "Call function FUN while preserving heading visibility.
658 FUN is a function called with no argument."
659 (let ((hide-body (and (/= (line-end-position) (point-max))
660 (save-excursion
661 (move-beginning-of-line 2)
662 (org-at-heading-p t)))))
663 (unwind-protect (funcall fun)
664 (when hide-body (outline-hide-entry)))))
666 (defun org-columns-previous-allowed-value ()
667 "Switch to the previous allowed value for this column."
668 (interactive)
669 (org-columns-next-allowed-value t))
671 (defun org-columns-next-allowed-value (&optional previous nth)
672 "Switch to the next allowed value for this column.
673 When PREVIOUS is set, go to the previous value. When NTH is
674 an integer, select that value."
675 (interactive)
676 (org-columns-check-computed)
677 (let* ((column (current-column))
678 (key (get-char-property (point) 'org-columns-key))
679 (value (get-char-property (point) 'org-columns-value))
680 (pom (or (get-text-property (line-beginning-position) 'org-hd-marker)
681 (point)))
682 (allowed
683 (let ((all
684 (or (org-property-get-allowed-values pom key)
685 (pcase (nth column org-columns-current-fmt-compiled)
686 (`(,_ ,_ ,_ ,(or "X" "X/" "X%") ,_) '("[ ]" "[X]")))
687 (org-colview-construct-allowed-dates value))))
688 (if previous (reverse all) all))))
689 (when (equal key "ITEM") (error "Cannot edit item headline from here"))
690 (unless (or allowed (member key '("SCHEDULED" "DEADLINE" "CLOCKSUM")))
691 (error "Allowed values for this property have not been defined"))
692 (let* ((l (length allowed))
693 (new
694 (cond
695 ((member key '("SCHEDULED" "DEADLINE" "CLOCKSUM"))
696 (if previous 'earlier 'later))
697 ((integerp nth)
698 (when (> (abs nth) l)
699 (user-error "Only %d allowed values for property `%s'" l key))
700 (nth (mod (1- nth) l) allowed))
701 ((member value allowed)
702 (when (= l 1) (error "Only one allowed value for this property"))
703 (or (nth 1 (member value allowed)) (car allowed)))
704 (t (car allowed))))
705 (action (lambda () (org-entry-put pom key new))))
706 (cond
707 ((eq major-mode 'org-agenda-mode)
708 (org-columns--call action)
709 ;; The following let preserves the current format, and makes
710 ;; sure that in only a single file things need to be updated.
711 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
712 (buffer (marker-buffer pom))
713 (org-agenda-contributing-files
714 (list (with-current-buffer buffer
715 (buffer-file-name (buffer-base-buffer))))))
716 (org-agenda-columns)))
718 (let ((inhibit-read-only t))
719 (remove-text-properties (line-end-position 0) (line-end-position)
720 '(read-only t))
721 (org-columns--call action))
722 ;; Some properties can modify headline (e.g., "TODO"), and
723 ;; possible shuffle overlays. Make sure they are still all at
724 ;; the right place on the current line.
725 (let ((org-columns-inhibit-recalculation)) (org-columns-redo))
726 (org-columns-update key)
727 (org-move-to-column column))))))
729 (defun org-colview-construct-allowed-dates (s)
730 "Construct a list of three dates around the date in S.
731 This respects the format of the time stamp in S, active or non-active,
732 and also including time or not. S must be just a time stamp, no text
733 around it."
734 (when (and s (string-match (concat "^" org-ts-regexp3 "$") s))
735 (let* ((time (org-parse-time-string s 'nodefaults))
736 (active (equal (string-to-char s) ?<))
737 (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
738 time-before time-after)
739 (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
740 (setf (car time) (or (car time) 0))
741 (setf (nth 1 time) (or (nth 1 time) 0))
742 (setf (nth 2 time) (or (nth 2 time) 0))
743 (setq time-before (copy-sequence time))
744 (setq time-after (copy-sequence time))
745 (setf (nth 3 time-before) (1- (nth 3 time)))
746 (setf (nth 3 time-after) (1+ (nth 3 time)))
747 (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
748 (list time-before time time-after)))))
750 (defun org-columns-open-link (&optional arg)
751 (interactive "P")
752 (let ((value (get-char-property (point) 'org-columns-value)))
753 (org-open-link-from-string value arg)))
755 ;;;###autoload
756 (defun org-columns-get-format-and-top-level ()
757 (let ((fmt (org-columns-get-format)))
758 (org-columns-goto-top-level)
759 fmt))
761 (defun org-columns-get-format (&optional fmt-string)
762 "Return columns format specifications.
763 When optional argument FMT-STRING is non-nil, use it as the
764 current specifications. This function also sets
765 `org-columns-current-fmt-compiled' and
766 `org-columns-current-fmt'."
767 (interactive)
768 (let ((format
769 (or fmt-string
770 (org-entry-get nil "COLUMNS" t)
771 (org-with-wide-buffer
772 (goto-char (point-min))
773 (catch :found
774 (let ((case-fold-search t))
775 (while (re-search-forward "^[ \t]*#\\+COLUMNS: .+$" nil t)
776 (let ((element (org-element-at-point)))
777 (when (eq (org-element-type element) 'keyword)
778 (throw :found (org-element-property :value element)))))
779 nil)))
780 org-columns-default-format)))
781 (setq org-columns-current-fmt format)
782 (org-columns-compile-format format)
783 format))
785 (defun org-columns-goto-top-level ()
786 "Move to the beginning of the column view area.
787 Also sets `org-columns-top-level-marker' to the new position."
788 (unless (markerp org-columns-top-level-marker)
789 (setq org-columns-top-level-marker (make-marker)))
790 (goto-char
791 (move-marker
792 org-columns-top-level-marker
793 (cond ((org-before-first-heading-p) (point-min))
794 ((org-entry-get nil "COLUMNS" t) org-entry-property-inherited-from)
795 (t (org-back-to-heading) (point))))))
797 ;;;###autoload
798 (defun org-columns (&optional global columns-fmt-string)
799 "Turn on column view on an Org mode file.
801 Column view applies to the whole buffer if point is before the
802 first headline. Otherwise, it applies to the first ancestor
803 setting \"COLUMNS\" property. If there is none, it defaults to
804 the current headline. With a `\\[universal-argument]' prefix \
805 argument, turn on column
806 view for the whole buffer unconditionally.
808 When COLUMNS-FMT-STRING is non-nil, use it as the column format."
809 (interactive "P")
810 (org-columns-remove-overlays)
811 (when global (goto-char (point-min)))
812 (if (markerp org-columns-begin-marker)
813 (move-marker org-columns-begin-marker (point))
814 (setq org-columns-begin-marker (point-marker)))
815 (org-columns-goto-top-level)
816 ;; Initialize `org-columns-current-fmt' and
817 ;; `org-columns-current-fmt-compiled'.
818 (let ((org-columns--time (float-time (current-time))))
819 (org-columns-get-format columns-fmt-string)
820 (unless org-columns-inhibit-recalculation (org-columns-compute-all))
821 (save-excursion
822 (save-restriction
823 (when (and (not global) (org-at-heading-p))
824 (narrow-to-region (point) (org-end-of-subtree t t)))
825 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
826 (org-clock-sum))
827 (when (assoc "CLOCKSUM_T" org-columns-current-fmt-compiled)
828 (org-clock-sum-today))
829 (let ((cache
830 ;; Collect contents of columns ahead of time so as to
831 ;; compute their maximum width.
832 (org-map-entries
833 (lambda () (cons (point) (org-columns--collect-values)))
834 nil nil (and org-columns-skip-archived-trees 'archive))))
835 (when cache
836 (org-columns--set-widths cache)
837 (org-columns--display-here-title)
838 (when (setq-local org-columns-flyspell-was-active
839 (bound-and-true-p flyspell-mode))
840 (flyspell-mode 0))
841 (unless (local-variable-p 'org-colview-initial-truncate-line-value)
842 (setq-local org-colview-initial-truncate-line-value
843 truncate-lines))
844 (setq truncate-lines t)
845 (dolist (entry cache)
846 (goto-char (car entry))
847 (org-columns--display-here (cdr entry)))))))))
849 (defun org-columns-new (&optional spec &rest attributes)
850 "Insert a new column, to the left of the current column.
851 Interactively fill attributes for new column. When column format
852 specification SPEC is provided, edit it instead.
854 When optional argument attributes can be a list of columns
855 specifications attributes to create the new column
856 non-interactively. See `org-columns-compile-format' for
857 details."
858 (interactive)
859 (let ((new (or attributes
860 (let ((prop
861 (completing-read
862 "Property: "
863 (mapcar #'list (org-buffer-property-keys t nil t))
864 nil nil (nth 0 spec))))
865 (list prop
866 (read-string (format "Column title [%s]: " prop)
867 (nth 1 spec))
868 ;; Use `read-string' instead of `read-number'
869 ;; to allow empty width.
870 (let ((w (read-string
871 "Column width: "
872 (and (nth 2 spec)
873 (number-to-string (nth 2 spec))))))
874 (and (org-string-nw-p w) (string-to-number w)))
875 (org-string-nw-p
876 (completing-read
877 "Summary: "
878 (delete-dups
879 (cons '("") ;Allow empty operator.
880 (mapcar (lambda (x) (list (car x)))
881 (append
882 org-columns-summary-types
883 org-columns-summary-types-default))))
884 nil t (nth 3 spec)))
885 (org-string-nw-p
886 (read-string "Format: " (nth 4 spec))))))))
887 (if spec
888 (progn (setcar spec (car new))
889 (setcdr spec (cdr new)))
890 (push new (nthcdr (current-column) org-columns-current-fmt-compiled)))
891 (org-columns-store-format)
892 (org-columns-redo)))
894 (defun org-columns-delete ()
895 "Delete the column at point from columns view."
896 (interactive)
897 (let ((spec (nth (current-column) org-columns-current-fmt-compiled)))
898 (when (y-or-n-p (format "Are you sure you want to remove column %S? "
899 (nth 1 spec)))
900 (setq org-columns-current-fmt-compiled
901 (delq spec org-columns-current-fmt-compiled))
902 (org-columns-store-format)
903 ;; This may leave a now wrong value in a node property. However
904 ;; updating it may prove counter-intuitive. See comments in
905 ;; `org-columns-move-right' for details.
906 (let ((org-columns-inhibit-recalculation t)) (org-columns-redo))
907 (when (>= (current-column) (length org-columns-current-fmt-compiled))
908 (backward-char)))))
910 (defun org-columns-edit-attributes ()
911 "Edit the attributes of the current column."
912 (interactive)
913 (org-columns-new (nth (current-column) org-columns-current-fmt-compiled)))
915 (defun org-columns-widen (arg)
916 "Make the column wider by ARG characters."
917 (interactive "p")
918 (let* ((n (current-column))
919 (entry (nth n org-columns-current-fmt-compiled))
920 (width (aref org-columns-current-maxwidths n)))
921 (setq width (max 1 (+ width arg)))
922 (setcar (nthcdr 2 entry) width)
923 (org-columns-store-format)
924 (let ((org-columns-inhibit-recalculation t)) (org-columns-redo))))
926 (defun org-columns-narrow (arg)
927 "Make the column narrower by ARG characters."
928 (interactive "p")
929 (org-columns-widen (- arg)))
931 (defun org-columns-move-right ()
932 "Swap this column with the one to the right."
933 (interactive)
934 (let* ((n (current-column))
935 (cell (nthcdr n org-columns-current-fmt-compiled))
937 (when (>= n (1- (length org-columns-current-fmt-compiled)))
938 (error "Cannot shift this column further to the right"))
939 (setq e (car cell))
940 (setcar cell (car (cdr cell)))
941 (setcdr cell (cons e (cdr (cdr cell))))
942 (org-columns-store-format)
943 ;; Do not compute again properties, since we're just moving
944 ;; columns around. It can put a property value a bit off when
945 ;; switching between an non-computed and a computed value for the
946 ;; same property, e.g. from "%A %A{+}" to "%A{+} %A".
948 ;; In this case, the value needs to be updated since the first
949 ;; column related to a property determines how its value is
950 ;; computed. However, (correctly) updating the value could be
951 ;; surprising, so we leave it as-is nonetheless.
952 (let ((org-columns-inhibit-recalculation t)) (org-columns-redo))
953 (forward-char 1)))
955 (defun org-columns-move-left ()
956 "Swap this column with the one to the left."
957 (interactive)
958 (let* ((n (current-column)))
959 (when (= n 0)
960 (error "Cannot shift this column further to the left"))
961 (backward-char 1)
962 (org-columns-move-right)
963 (backward-char 1)))
965 (defun org-columns-store-format ()
966 "Store the text version of the current columns format.
967 The format is stored either in the COLUMNS property of the node
968 starting the current column display, or in a #+COLUMNS line of
969 the current buffer."
970 (let ((fmt (org-columns-uncompile-format org-columns-current-fmt-compiled)))
971 (setq-local org-columns-current-fmt fmt)
972 (when org-columns-overlays
973 (org-with-point-at org-columns-top-level-marker
974 (if (and (org-at-heading-p) (org-entry-get nil "COLUMNS"))
975 (org-entry-put nil "COLUMNS" fmt)
976 (goto-char (point-min))
977 (let ((case-fold-search t))
978 ;; Try to replace the first COLUMNS keyword available.
979 (catch :found
980 (while (re-search-forward "^[ \t]*#\\+COLUMNS:\\(.*\\)" nil t)
981 (let ((element (save-match-data (org-element-at-point))))
982 (when (and (eq (org-element-type element) 'keyword)
983 (equal (org-element-property :key element)
984 "COLUMNS"))
985 (replace-match (concat " " fmt) t t nil 1)
986 (throw :found nil))))
987 ;; No COLUMNS keyword in the buffer. Insert one at the
988 ;; beginning, right before the first heading, if any.
989 (goto-char (point-min))
990 (unless (org-at-heading-p t) (outline-next-heading))
991 (let ((inhibit-read-only t))
992 (insert-before-markers "#+COLUMNS: " fmt "\n"))))
993 (setq-local org-columns-default-format fmt))))))
995 (defun org-columns-update (property)
996 "Recompute PROPERTY, and update the columns display for it."
997 (org-columns-compute property)
998 (org-with-wide-buffer
999 (let ((p (upcase property)))
1000 (dolist (ov org-columns-overlays)
1001 (let ((key (overlay-get ov 'org-columns-key)))
1002 (when (and key (equal key p) (overlay-start ov))
1003 (goto-char (overlay-start ov))
1004 (let* ((spec (nth (current-column) org-columns-current-fmt-compiled))
1005 (value
1006 (or (cdr (assoc spec
1007 (get-text-property (line-beginning-position)
1008 'org-summaries)))
1009 (org-entry-get (point) key))))
1010 (when value
1011 (let ((displayed (org-columns--displayed-value spec value))
1012 (format (overlay-get ov 'org-columns-format))
1013 (width
1014 (aref org-columns-current-maxwidths (current-column))))
1015 (overlay-put ov 'org-columns-value value)
1016 (overlay-put ov 'org-columns-value-modified displayed)
1017 (overlay-put ov
1018 'display
1019 (org-columns--overlay-text
1020 displayed format width property value)))))))))))
1022 (defun org-columns-redo ()
1023 "Construct the column display again."
1024 (interactive)
1025 (when org-columns-overlays
1026 (message "Recomputing columns...")
1027 (org-with-point-at org-columns-begin-marker
1028 (org-columns-remove-overlays)
1029 (if (derived-mode-p 'org-mode)
1030 ;; Since we already know the columns format, provide it
1031 ;; instead of computing again.
1032 (call-interactively #'org-columns org-columns-current-fmt)
1033 (org-agenda-redo)
1034 (call-interactively #'org-agenda-columns)))
1035 (message "Recomputing columns...done")))
1037 (defun org-columns-uncompile-format (compiled)
1038 "Turn the compiled columns format back into a string representation.
1039 COMPILED is an alist, as returned by
1040 `org-columns-compile-format', which see."
1041 (mapconcat
1042 (lambda (spec)
1043 (pcase spec
1044 (`(,prop ,title ,width ,op ,printf)
1045 (concat "%"
1046 (and width (number-to-string width))
1047 prop
1048 (and title (not (equal prop title)) (format "(%s)" title))
1049 (cond ((not op) nil)
1050 (printf (format "{%s;%s}" op printf))
1051 (t (format "{%s}" op)))))))
1052 compiled " "))
1054 (defun org-columns-compile-format (fmt)
1055 "Turn a column format string FMT into an alist of specifications.
1057 The alist has one entry for each column in the format. The elements of
1058 that list are:
1059 property the property name, as an upper-case string
1060 title the title field for the columns, as a string
1061 width the column width in characters, can be nil for automatic width
1062 operator the summary operator, as a string, or nil
1063 printf a printf format for computed values, as a string, or nil
1065 This function updates `org-columns-current-fmt-compiled'."
1066 (setq org-columns-current-fmt-compiled nil)
1067 (let ((start 0))
1068 (while (string-match
1069 "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\
1070 \\(?:{\\([^}]+\\)}\\)?\\s-*"
1071 fmt start)
1072 (setq start (match-end 0))
1073 (let* ((width (and (match-end 1) (string-to-number (match-string 1 fmt))))
1074 (prop (match-string-no-properties 2 fmt))
1075 (title (or (match-string-no-properties 3 fmt) prop))
1076 (operator (match-string-no-properties 4 fmt)))
1077 (push (if (not operator) (list (upcase prop) title width nil nil)
1078 (let (printf)
1079 (when (string-match ";" operator)
1080 (setq printf (substring operator (match-end 0)))
1081 (setq operator (substring operator 0 (match-beginning 0))))
1082 (list (upcase prop) title width operator printf)))
1083 org-columns-current-fmt-compiled)))
1084 (setq org-columns-current-fmt-compiled
1085 (nreverse org-columns-current-fmt-compiled))))
1088 ;;;; Column View Summary
1090 (defun org-columns--age-to-minutes (s)
1091 "Turn age string S into a number of minutes.
1092 An age is either computed from a given time-stamp, or indicated
1093 as a canonical duration, i.e., using units defined in
1094 `org-duration-canonical-units'."
1095 (cond
1096 ((string-match-p org-ts-regexp s)
1097 (/ (- org-columns--time
1098 (float-time (apply #'encode-time (org-parse-time-string s))))
1099 60))
1100 ((org-duration-p s) (org-duration-to-minutes s t)) ;skip user units
1101 (t (user-error "Invalid age: %S" s))))
1103 (defun org-columns--format-age (minutes)
1104 "Format MINUTES float as an age string."
1105 (org-duration-from-minutes minutes
1106 '(("d" . nil) ("h" . nil) ("min" . nil))
1107 t)) ;ignore user's custom units
1109 (defun org-columns--summary-apply-times (fun times)
1110 "Apply FUN to time values TIMES.
1111 Return the result as a duration."
1112 (org-duration-from-minutes
1113 (apply fun
1114 (mapcar (lambda (time)
1115 ;; Unlike to `org-duration-to-minutes' standard
1116 ;; behavior, we want to consider plain numbers as
1117 ;; hours. As a consequence, we treat them
1118 ;; differently.
1119 (if (string-match-p "\\`[0-9]+\\(?:\\.[0-9]*\\)?\\'" time)
1120 (* 60 (string-to-number time))
1121 (org-duration-to-minutes time)))
1122 times))
1123 (org-duration-h:mm-only-p times)))
1125 (defun org-columns--compute-spec (spec &optional update)
1126 "Update tree according to SPEC.
1127 SPEC is a column format specification. When optional argument
1128 UPDATE is non-nil, summarized values can replace existing ones in
1129 properties drawers."
1130 (let* ((lmax (if (bound-and-true-p org-inlinetask-min-level)
1131 org-inlinetask-min-level
1132 29)) ;Hard-code deepest level.
1133 (lvals (make-vector (1+ lmax) nil))
1134 (level 0)
1135 (inminlevel lmax)
1136 (last-level lmax)
1137 (property (car spec))
1138 (printf (nth 4 spec))
1139 (operator (nth 3 spec))
1140 (collect (and operator (org-columns--collect operator)))
1141 (summarize (and operator (org-columns--summarize operator))))
1142 (org-with-wide-buffer
1143 ;; Find the region to compute.
1144 (goto-char org-columns-top-level-marker)
1145 (goto-char (condition-case nil (org-end-of-subtree t) (error (point-max))))
1146 ;; Walk the tree from the back and do the computations.
1147 (while (re-search-backward
1148 org-outline-regexp-bol org-columns-top-level-marker t)
1149 (unless (or (= level 0) (eq level inminlevel))
1150 (setq last-level level))
1151 (setq level (org-reduced-level (org-outline-level)))
1152 (let* ((pos (match-beginning 0))
1153 (value (if collect (funcall collect property)
1154 (org-entry-get (point) property)))
1155 (value-set (org-string-nw-p value)))
1156 (cond
1157 ((< level last-level)
1158 ;; Collect values from lower levels and inline tasks here
1159 ;; and summarize them using SUMMARIZE. Store them in text
1160 ;; property `org-summaries', in alist whose key is SPEC.
1161 (let* ((summary
1162 (and summarize
1163 (let ((values (append (and (/= last-level inminlevel)
1164 (aref lvals last-level))
1165 (aref lvals inminlevel))))
1166 (and values (funcall summarize values printf))))))
1167 ;; Leaf values are not summaries: do not mark them.
1168 (when summary
1169 (let* ((summaries-alist (get-text-property pos 'org-summaries))
1170 (old (assoc spec summaries-alist)))
1171 (if old (setcdr old summary)
1172 (push (cons spec summary) summaries-alist)
1173 (org-with-silent-modifications
1174 (add-text-properties
1175 pos (1+ pos) (list 'org-summaries summaries-alist)))))
1176 ;; When PROPERTY exists in current node, even if empty,
1177 ;; but its value doesn't match the one computed, use
1178 ;; the latter instead.
1180 ;; Ignore leading or trailing white spaces that might
1181 ;; have been introduced in summary, since those are not
1182 ;; significant in properties value.
1183 (let ((new-value (org-trim summary)))
1184 (when (and update value (not (equal value new-value)))
1185 (org-entry-put (point) property new-value))))
1186 ;; Add current to current level accumulator.
1187 (when (or summary value-set)
1188 (push (or summary value) (aref lvals level)))
1189 ;; Clear accumulators for deeper levels.
1190 (cl-loop for l from (1+ level) to lmax do (aset lvals l nil))))
1191 (value-set (push value (aref lvals level)))
1192 (t nil)))))))
1194 ;;;###autoload
1195 (defun org-columns-compute (property)
1196 "Summarize the values of PROPERTY hierarchically.
1197 Also update existing values for PROPERTY according to the first
1198 column specification."
1199 (interactive)
1200 (let ((main-flag t)
1201 (upcase-prop (upcase property)))
1202 (dolist (spec org-columns-current-fmt-compiled)
1203 (pcase spec
1204 (`(,(pred (equal upcase-prop)) . ,_)
1205 (org-columns--compute-spec spec main-flag)
1206 ;; Only the first summary can update the property value.
1207 (when main-flag (setq main-flag nil)))))))
1209 (defun org-columns-compute-all ()
1210 "Compute all columns that have operators defined."
1211 (org-with-silent-modifications
1212 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
1213 (let ((org-columns--time (float-time (current-time)))
1214 seen)
1215 (dolist (spec org-columns-current-fmt-compiled)
1216 (let ((property (car spec)))
1217 ;; Property value is updated only the first time a given
1218 ;; property is encountered.
1219 (org-columns--compute-spec spec (not (member property seen)))
1220 (push property seen)))))
1222 (defun org-columns--summary-sum (values printf)
1223 "Compute the sum of VALUES.
1224 When PRINTF is non-nil, use it to format the result."
1225 (format (or printf "%s") (apply #'+ (mapcar #'string-to-number values))))
1227 (defun org-columns--summary-currencies (values _)
1228 "Compute the sum of VALUES, with two decimals."
1229 (format "%.2f" (apply #'+ (mapcar #'string-to-number values))))
1231 (defun org-columns--summary-checkbox (check-boxes _)
1232 "Summarize CHECK-BOXES with a check-box."
1233 (let ((done (cl-count "[X]" check-boxes :test #'equal))
1234 (all (length check-boxes)))
1235 (cond ((= done all) "[X]")
1236 ((> done 0) "[-]")
1237 (t "[ ]"))))
1239 (defun org-columns--summary-checkbox-count (check-boxes _)
1240 "Summarize CHECK-BOXES with a check-box cookie."
1241 (format "[%d/%d]"
1242 (cl-count-if (lambda (b) (or (equal b "[X]")
1243 (string-match-p "\\[\\([1-9]\\)/\\1\\]" b)))
1244 check-boxes)
1245 (length check-boxes)))
1247 (defun org-columns--summary-checkbox-percent (check-boxes _)
1248 "Summarize CHECK-BOXES with a check-box percent."
1249 (format "[%d%%]"
1250 (round (* 100.0 (cl-count-if (lambda (b) (member b '("[X]" "[100%]")))
1251 check-boxes))
1252 (length check-boxes))))
1254 (defun org-columns--summary-min (values printf)
1255 "Compute the minimum of VALUES.
1256 When PRINTF is non-nil, use it to format the result."
1257 (format (or printf "%s")
1258 (apply #'min (mapcar #'string-to-number values))))
1260 (defun org-columns--summary-max (values printf)
1261 "Compute the maximum of VALUES.
1262 When PRINTF is non-nil, use it to format the result."
1263 (format (or printf "%s")
1264 (apply #'max (mapcar #'string-to-number values))))
1266 (defun org-columns--summary-mean (values printf)
1267 "Compute the mean of VALUES.
1268 When PRINTF is non-nil, use it to format the result."
1269 (format (or printf "%s")
1270 (/ (apply #'+ (mapcar #'string-to-number values))
1271 (float (length values)))))
1273 (defun org-columns--summary-sum-times (times _)
1274 "Sum TIMES."
1275 (org-columns--summary-apply-times #'+ times))
1277 (defun org-columns--summary-min-time (times _)
1278 "Compute the minimum time among TIMES."
1279 (org-columns--summary-apply-times #'min times))
1281 (defun org-columns--summary-max-time (times _)
1282 "Compute the maximum time among TIMES."
1283 (org-columns--summary-apply-times #'max times))
1285 (defun org-columns--summary-mean-time (times _)
1286 "Compute the mean time among TIMES."
1287 (org-columns--summary-apply-times
1288 (lambda (&rest values) (/ (apply #'+ values) (float (length values))))
1289 times))
1291 (defun org-columns--summary-min-age (ages _)
1292 "Compute the minimum time among AGES."
1293 (org-columns--format-age
1294 (apply #'min (mapcar #'org-columns--age-to-minutes ages))))
1296 (defun org-columns--summary-max-age (ages _)
1297 "Compute the maximum time among AGES."
1298 (org-columns--format-age
1299 (apply #'max (mapcar #'org-columns--age-to-minutes ages))))
1301 (defun org-columns--summary-mean-age (ages _)
1302 "Compute the minimum time among AGES."
1303 (org-columns--format-age
1304 (/ (apply #'+ (mapcar #'org-columns--age-to-minutes ages))
1305 (float (length ages)))))
1307 (defun org-columns--summary-estimate (estimates _)
1308 "Combine a list of estimates, using mean and variance.
1309 The mean and variance of the result will be the sum of the means
1310 and variances (respectively) of the individual estimates."
1311 (let ((mean 0)
1312 (var 0))
1313 (dolist (e estimates)
1314 (pcase (mapcar #'string-to-number (split-string e "-"))
1315 (`(,low ,high)
1316 (let ((m (/ (+ low high) 2.0)))
1317 (cl-incf mean m)
1318 (cl-incf var (- (/ (+ (* low low) (* high high)) 2.0) (* m m)))))
1319 (`(,value) (cl-incf mean value))))
1320 (let ((sd (sqrt var)))
1321 (format "%s-%s"
1322 (format "%.0f" (- mean sd))
1323 (format "%.0f" (+ mean sd))))))
1327 ;;; Dynamic block for Column view
1329 (defun org-columns--capture-view (maxlevel match skip-empty format local)
1330 "Get the column view of the current buffer.
1332 MAXLEVEL sets the level limit. SKIP-EMPTY tells whether to skip
1333 empty rows, an empty row being one where all the column view
1334 specifiers but ITEM are empty. FORMAT is a format string for
1335 columns, or nil. When LOCAL is non-nil, only capture headings in
1336 current subtree.
1338 This function returns a list containing the title row and all
1339 other rows. Each row is a list of fields, as strings, or
1340 `hline'."
1341 (org-columns (not local) format)
1342 (goto-char org-columns-top-level-marker)
1343 (let ((columns (length org-columns-current-fmt-compiled))
1344 (has-item (assoc "ITEM" org-columns-current-fmt-compiled))
1345 table)
1346 (org-map-entries
1347 (lambda ()
1348 (when (get-char-property (point) 'org-columns-key)
1349 (let (row)
1350 (dotimes (i columns)
1351 (let* ((col (+ (line-beginning-position) i))
1352 (p (get-char-property col 'org-columns-key)))
1353 (push (org-quote-vert
1354 (get-char-property col
1355 (if (string= p "ITEM")
1356 'org-columns-value
1357 'org-columns-value-modified)))
1358 row)))
1359 (unless (and skip-empty
1360 (let ((r (delete-dups (remove "" row))))
1361 (or (null r) (and has-item (= (length r) 1)))))
1362 (push (cons (org-reduced-level (org-current-level)) (nreverse row))
1363 table)))))
1364 (or (and maxlevel (format "LEVEL<=%d" maxlevel))
1365 (and match match))
1366 (and local 'tree)
1367 'archive 'comment)
1368 (org-columns-quit)
1369 ;; Add column titles and a horizontal rule in front of the table.
1370 (cons (mapcar #'cadr org-columns-current-fmt-compiled)
1371 (cons 'hline (nreverse table)))))
1373 (defun org-columns--clean-item (item)
1374 "Remove sensitive contents from string ITEM.
1375 This includes objects that may not be duplicated within
1376 a document, e.g., a target, or those forbidden in tables, e.g.,
1377 an inline src-block."
1378 (let ((data (org-element-parse-secondary-string
1379 item (org-element-restriction 'headline))))
1380 (org-element-map data
1381 '(footnote-reference inline-babel-call inline-src-block target
1382 radio-target statistics-cookie)
1383 #'org-element-extract-element)
1384 (org-no-properties (org-element-interpret-data data))))
1386 ;;;###autoload
1387 (defun org-dblock-write:columnview (params)
1388 "Write the column view table.
1389 PARAMS is a property list of parameters:
1391 :id the :ID: property of the entry where the columns view
1392 should be built. When the symbol `local', call locally.
1393 When `global' call column view with the cursor at the beginning
1394 of the buffer (usually this means that the whole buffer switches
1395 to column view). When \"file:path/to/file.org\", invoke column
1396 view at the start of that file. Otherwise, the ID is located
1397 using `org-id-find'.
1398 :hlines When t, insert a hline before each item. When a number, insert
1399 a hline before each level <= that number.
1400 :indent When non-nil, indent each ITEM field according to its level.
1401 :vlines When t, make each column a colgroup to enforce vertical lines.
1402 :maxlevel When set to a number, don't capture headlines below this level.
1403 :match When set to a string, use this as a tags/property match filter.
1404 :skip-empty-rows
1405 When t, skip rows where all specifiers other than ITEM are empty.
1406 :format When non-nil, specify the column view format to use."
1407 (let ((table
1408 (let ((id (plist-get params :id))
1409 view-file view-pos)
1410 (pcase id
1411 (`global nil)
1412 ((or `local `nil) (setq view-pos (point)))
1413 ((and (let id-string (format "%s" id))
1414 (guard (string-match "^file:\\(.*\\)" id-string)))
1415 (setq view-file (match-string-no-properties 1 id-string))
1416 (unless (file-exists-p view-file)
1417 (user-error "No such file: %S" id-string)))
1418 ((and (let idpos (org-find-entry-with-id id)) (guard idpos))
1419 (setq view-pos idpos))
1420 ((let `(,filename . ,position) (org-id-find id))
1421 (setq view-file filename)
1422 (setq view-pos position))
1423 (_ (user-error "Cannot find entry with :ID: %s" id)))
1424 (with-current-buffer (if view-file (get-file-buffer view-file)
1425 (current-buffer))
1426 (org-with-wide-buffer
1427 (when view-pos (goto-char view-pos))
1428 (org-columns--capture-view (plist-get params :maxlevel)
1429 (plist-get params :match)
1430 (plist-get params :skip-empty-rows)
1431 (plist-get params :format)
1432 view-pos))))))
1433 (when table
1434 ;; Prune level information from the table. Also normalize
1435 ;; headings: remove stars, add indentation entities, if
1436 ;; required, and possibly precede some of them with a horizontal
1437 ;; rule.
1438 (let ((item-index
1439 (let ((p (assoc "ITEM" org-columns-current-fmt-compiled)))
1440 (and p (cl-position p
1441 org-columns-current-fmt-compiled
1442 :test #'equal))))
1443 (hlines (plist-get params :hlines))
1444 (indent (plist-get params :indent))
1445 new-table)
1446 ;; Copy header and first rule.
1447 (push (pop table) new-table)
1448 (push (pop table) new-table)
1449 (dolist (row table (setq table (nreverse new-table)))
1450 (let ((level (car row)))
1451 (when (and (not (eq (car new-table) 'hline))
1452 (or (eq hlines t)
1453 (and (numberp hlines) (<= level hlines))))
1454 (push 'hline new-table))
1455 (when item-index
1456 (let ((item (org-columns--clean-item (nth item-index (cdr row)))))
1457 (setf (nth item-index (cdr row))
1458 (if (and indent (> level 1))
1459 (concat "\\_" (make-string (* 2 (1- level)) ?\s) item)
1460 item))))
1461 (push (cdr row) new-table))))
1462 (when (plist-get params :vlines)
1463 (setq table
1464 (let ((size (length org-columns-current-fmt-compiled)))
1465 (append (mapcar (lambda (x) (if (eq 'hline x) x (cons "" x)))
1466 table)
1467 (list (cons "/" (make-list size "<>")))))))
1468 (let ((content-lines (org-split-string (plist-get params :content) "\n"))
1469 recalc)
1470 ;; Insert affiliated keywords before the table.
1471 (when content-lines
1472 (while (string-match-p "\\`[ \t]*#\\+" (car content-lines))
1473 (insert (pop content-lines) "\n")))
1474 (save-excursion
1475 ;; Insert table at point.
1476 (insert
1477 (mapconcat (lambda (row)
1478 (if (eq row 'hline) "|-|"
1479 (format "|%s|" (mapconcat #'identity row "|"))))
1480 table
1481 "\n"))
1482 ;; Insert TBLFM lines following table.
1483 (let ((case-fold-search t))
1484 (dolist (line content-lines)
1485 (when (string-match-p "\\`[ \t]*#\\+TBLFM:" line)
1486 (insert "\n" line)
1487 (unless recalc (setq recalc t))))))
1488 (when recalc (org-table-recalculate 'all t))
1489 (org-table-align)))))
1491 ;;;###autoload
1492 (defun org-columns-insert-dblock ()
1493 "Create a dynamic block capturing a column view table."
1494 (interactive)
1495 (let ((id (completing-read
1496 "Capture columns (local, global, entry with :ID: property) [local]: "
1497 (append '(("global") ("local"))
1498 (mapcar #'list (org-property-values "ID"))))))
1499 (org-create-dblock
1500 (list :name "columnview"
1501 :hlines 1
1502 :id (cond ((string= id "global") 'global)
1503 ((member id '("" "local")) 'local)
1504 (id)))))
1505 (org-update-dblock))
1509 ;;; Column view in the agenda
1511 ;;;###autoload
1512 (defun org-agenda-columns ()
1513 "Turn on or update column view in the agenda."
1514 (interactive)
1515 (org-columns-remove-overlays)
1516 (if (markerp org-columns-begin-marker)
1517 (move-marker org-columns-begin-marker (point))
1518 (setq org-columns-begin-marker (point-marker)))
1519 (let* ((org-columns--time (float-time (current-time)))
1520 (fmt
1521 (cond
1522 ((bound-and-true-p org-agenda-overriding-columns-format))
1523 ((let ((m (org-get-at-bol 'org-hd-marker)))
1524 (and m
1525 (or (org-entry-get m "COLUMNS" t)
1526 (with-current-buffer (marker-buffer m)
1527 org-columns-default-format)))))
1528 ((and (local-variable-p 'org-columns-current-fmt)
1529 org-columns-current-fmt))
1530 ((let ((m (next-single-property-change (point-min) 'org-hd-marker)))
1531 (and m
1532 (let ((m (get-text-property m 'org-hd-marker)))
1533 (or (org-entry-get m "COLUMNS" t)
1534 (with-current-buffer (marker-buffer m)
1535 org-columns-default-format))))))
1536 (t org-columns-default-format)))
1537 (compiled-fmt (org-columns-compile-format fmt)))
1538 (setq org-columns-current-fmt fmt)
1539 (when org-agenda-columns-compute-summary-properties
1540 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1541 (save-excursion
1542 ;; Collect properties for each headline in current view.
1543 (goto-char (point-min))
1544 (let (cache)
1545 (while (not (eobp))
1546 (let ((m (org-get-at-bol 'org-hd-marker)))
1547 (when m
1548 (push (cons (line-beginning-position)
1549 ;; `org-columns-current-fmt-compiled' is
1550 ;; initialized but only set locally to the
1551 ;; agenda buffer. Since current buffer is
1552 ;; changing, we need to force the original
1553 ;; compiled-fmt there.
1554 (org-with-point-at m
1555 (org-columns--collect-values compiled-fmt)))
1556 cache)))
1557 (forward-line))
1558 (when cache
1559 (org-columns--set-widths cache)
1560 (org-columns--display-here-title)
1561 (when (setq-local org-columns-flyspell-was-active
1562 (bound-and-true-p flyspell-mode))
1563 (flyspell-mode 0))
1564 (dolist (entry cache)
1565 (goto-char (car entry))
1566 (org-columns--display-here (cdr entry)))
1567 (when org-agenda-columns-show-summaries
1568 (org-agenda-colview-summarize cache)))))))
1570 (defun org-agenda-colview-summarize (cache)
1571 "Summarize the summarizable columns in column view in the agenda.
1572 This will add overlays to the date lines, to show the summary for each day."
1573 (let ((fmt (mapcar
1574 (lambda (spec)
1575 (pcase spec
1576 (`(,property ,title ,width . ,_)
1577 (if (member property '("CLOCKSUM" "CLOCKSUM_T"))
1578 (list property title width ":" nil)
1579 spec))))
1580 org-columns-current-fmt-compiled)))
1581 ;; Ensure there's at least one summation column.
1582 (when (cl-some (lambda (spec) (nth 3 spec)) fmt)
1583 (goto-char (point-max))
1584 (catch :complete
1585 (while t
1586 (when (or (get-text-property (point) 'org-date-line)
1587 (eq (get-text-property (point) 'face)
1588 'org-agenda-structure))
1589 ;; OK, this is a date line that should be used.
1590 (let (entries)
1591 (let (rest)
1592 (dolist (c cache)
1593 (if (> (car c) (point))
1594 (push c entries)
1595 (push c rest)))
1596 (setq cache rest))
1597 ;; ENTRIES contains entries below the current one.
1598 ;; CACHE is the rest. Compute the summaries for the
1599 ;; properties we want, set nil properties for the rest.
1600 (when (setq entries (mapcar #'cdr entries))
1601 (org-columns--display-here
1602 (mapcar
1603 (lambda (spec)
1604 (pcase spec
1605 (`("ITEM" . ,_)
1606 ;; Replace ITEM with current date. Preserve
1607 ;; properties for fontification.
1608 (let ((date (buffer-substring
1609 (line-beginning-position)
1610 (line-end-position))))
1611 (list spec date date)))
1612 (`(,_ ,_ ,_ nil ,_) (list spec "" ""))
1613 (`(,_ ,_ ,_ ,operator ,printf)
1614 (let* ((summarize (org-columns--summarize operator))
1615 (values
1616 ;; Use real values for summary, not
1617 ;; those prepared for display.
1618 (delq nil
1619 (mapcar
1620 (lambda (e) (org-string-nw-p
1621 (nth 1 (assoc spec e))))
1622 entries)))
1623 (final (if values
1624 (funcall summarize values printf)
1625 "")))
1626 (unless (equal final "")
1627 (put-text-property 0 (length final)
1628 'face 'bold final))
1629 (list spec final final)))))
1630 fmt)
1631 'dateline)
1632 (setq-local org-agenda-columns-active t))))
1633 (if (bobp) (throw :complete t) (forward-line -1)))))))
1635 (defun org-agenda-colview-compute (fmt)
1636 "Compute the relevant columns in the contributing source buffers."
1637 (dolist (file org-agenda-contributing-files)
1638 (let ((b (find-buffer-visiting file)))
1639 (with-current-buffer (or (buffer-base-buffer b) b)
1640 (org-with-wide-buffer
1641 (org-with-silent-modifications
1642 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
1643 (goto-char (point-min))
1644 (org-columns-get-format-and-top-level)
1645 (dolist (spec fmt)
1646 (let ((prop (car spec)))
1647 (cond
1648 ((equal prop "CLOCKSUM") (org-clock-sum))
1649 ((equal prop "CLOCKSUM_T") (org-clock-sum-today))
1650 ((and (nth 3 spec)
1651 (let ((a (assoc prop org-columns-current-fmt-compiled)))
1652 (equal (nth 3 a) (nth 3 spec))))
1653 (org-columns-compute prop))))))))))
1656 (provide 'org-colview)
1658 ;;; org-colview.el ends here