1 ;;; org-colview.el --- Column View in Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2016 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
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 <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; This file contains the column view for 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
)
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) where
72 LABEL is a string used in #+COLUMNS definition describing the
73 summary type. It can contain any character but \"}\". It is
76 SUMMARIZE is a function called with two arguments. The first
77 argument is a non-empty list of values, as non-empty strings.
78 The second one is a format string or nil. It has to return
79 a string summarizing the list of values.
81 Note that the return value can become one value for an higher
82 order summary, so the function is expected to handle its own
85 Types defined in this variable take precedence over those defined
86 in `org-columns-summary-types-default', which see."
87 :group
'org-properties
89 :package-version
'(Org .
"9.0")
90 :type
'(alist :key-type
(string :tag
" Label")
91 :value-type
(function :tag
"Summarize")))
97 (defvar org-columns-overlays nil
98 "Holds the list of current column overlays.")
100 (defvar org-columns--time
0.0
101 "Number of seconds since the epoch, as a floating point number.")
103 (defvar-local org-columns-current-fmt nil
104 "Local variable, holds the currently active column format.")
106 (defvar-local org-columns-current-fmt-compiled nil
107 "Local variable, holds the currently active column format.
108 This is the compiled version of the format.")
110 (defvar-local org-columns-current-maxwidths nil
111 "Currently active maximum column widths, as a vector.")
113 (defvar org-columns-begin-marker
(make-marker)
114 "Points to the position where last a column creation command was called.")
116 (defvar org-columns-top-level-marker
(make-marker)
117 "Points to the position where current columns region starts.")
119 (defvar org-columns-map
(make-sparse-keymap)
120 "The keymap valid in column display.")
122 (defconst org-columns-summary-types-default
123 '(("+" . org-columns--summary-sum
)
124 ("$" . org-columns--summary-currencies
)
125 ("X" . org-columns--summary-checkbox
)
126 ("X/" . org-columns--summary-checkbox-count
)
127 ("X%" . org-columns--summary-checkbox-percent
)
128 ("max" . org-columns--summary-max
)
129 ("mean" . org-columns--summary-mean
)
130 ("min" . org-columns--summary-min
)
131 (":" . org-columns--summary-sum-times
)
132 (":max" . org-columns--summary-max-time
)
133 (":mean" . org-columns--summary-mean-time
)
134 (":min" . org-columns--summary-min-time
)
135 ("@max" . org-columns--summary-max-age
)
136 ("@mean" . org-columns--summary-mean-age
)
137 ("@min" . org-columns--summary-min-age
)
138 ("est+" . org-columns--summary-estimate
))
139 "Map operators to summarize functions.
140 See `org-columns-summary-types' for details.")
142 (defun org-columns-content ()
143 "Switch to contents view while in columns view."
148 (org-defkey org-columns-map
"c" 'org-columns-content
)
149 (org-defkey org-columns-map
"o" 'org-overview
)
150 (org-defkey org-columns-map
"e" 'org-columns-edit-value
)
151 (org-defkey org-columns-map
"\C-c\C-t" 'org-columns-todo
)
152 (org-defkey org-columns-map
"\C-c\C-c" 'org-columns-set-tags-or-toggle
)
153 (org-defkey org-columns-map
"\C-c\C-o" 'org-columns-open-link
)
154 (org-defkey org-columns-map
"v" 'org-columns-show-value
)
155 (org-defkey org-columns-map
"q" 'org-columns-quit
)
156 (org-defkey org-columns-map
"r" 'org-columns-redo
)
157 (org-defkey org-columns-map
"g" 'org-columns-redo
)
158 (org-defkey org-columns-map
[left] 'backward-char)
159 (org-defkey org-columns-map "\M-b" 'backward-char)
160 (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
161 (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
162 (org-defkey org-columns-map "\M-f"
163 (lambda () (interactive) (goto-char (1+ (point)))))
164 (org-defkey org-columns-map [right]
165 (lambda () (interactive) (goto-char (1+ (point)))))
166 (org-defkey org-columns-map [down]
167 (lambda () (interactive)
168 (let ((col (current-column)))
169 (beginning-of-line 2)
170 (while (and (org-invisible-p2) (not (eobp)))
171 (beginning-of-line 2))
173 (if (eq major-mode 'org-agenda-mode)
174 (org-agenda-do-context-action)))))
175 (org-defkey org-columns-map [up]
176 (lambda () (interactive)
177 (let ((col (current-column)))
178 (beginning-of-line 0)
179 (while (and (org-invisible-p2) (not (bobp)))
180 (beginning-of-line 0))
182 (if (eq major-mode 'org-agenda-mode)
183 (org-agenda-do-context-action)))))
184 (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
185 (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
186 (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
187 (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
188 (org-defkey org-columns-map "<" 'org-columns-narrow)
189 (org-defkey org-columns-map ">" 'org-columns-widen)
190 (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
191 (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
192 (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
193 (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
195 (org-defkey org-columns-map (number-to-string i)
196 `(lambda () (interactive)
197 (org-columns-next-allowed-value nil ,i))))
199 (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
201 ["Edit property" org-columns-edit-value t]
202 ["Next allowed value" org-columns-next-allowed-value t]
203 ["Previous allowed value" org-columns-previous-allowed-value t]
204 ["Show full value" org-columns-show-value t]
205 ["Edit allowed values" org-columns-edit-allowed t]
207 ["Edit column attributes" org-columns-edit-attributes t]
208 ["Increase column width" org-columns-widen t]
209 ["Decrease column width" org-columns-narrow t]
211 ["Move column right" org-columns-move-right t]
212 ["Move column left" org-columns-move-left t]
213 ["Add column" org-columns-new t]
214 ["Delete column" org-columns-delete t]
216 ["CONTENTS" org-columns-content t]
217 ["OVERVIEW" org-overview t]
218 ["Refresh columns display" org-columns-redo t]
220 ["Open link" org-columns-open-link t]
222 ["Quit" org-columns-quit t]))
224 (defun org-columns--displayed-value (spec value)
225 "Return displayed value for specification SPEC in current entry.
227 SPEC is a column format specification as stored in
228 `org-columns-current-fmt-compiled'. VALUE is the real value to
229 display, as a string."
231 ((and (functionp org-columns-modify-value-for-display-function)
232 (funcall org-columns-modify-value-for-display-function
235 ((equal (car spec) "ITEM")
236 (concat (make-string (1- (org-current-level))
237 (if org-hide-leading-stars ?\s ?*))
239 (org-columns-compact-links value)))
242 (defun org-columns--collect-values (&optional agenda)
243 "Collect values for columns on the current line.
245 When optional argument AGENDA is non-nil, assume the value is
246 meant for the agenda, i.e., caller is `org-agenda-columns'.
248 Return a list of triplets (SPEC VALUE DISPLAYED) suitable for
249 `org-columns--display-here'.
251 This function assumes `org-columns-current-fmt-compiled' is
253 (let ((summaries (get-text-property (point) 'org-summaries)))
258 (let* ((v (or (cdr (assoc spec summaries))
259 (org-entry-get (point) p 'selective t)
261 ;; Effort property is not defined. Try
262 ;; to use appointment duration.
263 org-agenda-columns-add-appointments-to-effort-sum
264 (string= p (upcase org-effort-property))
265 (get-text-property (point) 'duration)
267 (org-minutes-to-clocksum-string
268 (get-text-property (point) 'duration))
271 (list spec v (org-columns--displayed-value spec v))))))
272 org-columns-current-fmt-compiled)))
274 (defun org-columns--set-widths (cache)
275 "Compute the maximum column widths from the format and CACHE.
276 This function sets `org-columns-current-maxwidths' as a vector of
277 integers greater than 0."
278 (setq org-columns-current-maxwidths
283 (`(,_ ,_ ,(and width (pred wholenump)) . ,_) width)
285 ;; No width is specified in the columns format.
286 ;; Compute it by checking all possible values for
288 (let ((width (length name)))
289 (dolist (entry cache width)
290 (let ((value (nth 2 (assoc spec (cdr entry)))))
291 (setq width (max (length value) width))))))))
292 org-columns-current-fmt-compiled))))
294 (defun org-columns--new-overlay (beg end &optional string face)
295 "Create a new column overlay and add it to the list."
296 (let ((ov (make-overlay beg end)))
297 (overlay-put ov 'face (or face 'secondary-selection))
298 (org-overlay-display ov string face)
299 (push ov org-columns-overlays)
302 (defun org-columns--summarize (operator)
303 "Return summary function associated to string OPERATOR."
304 (if (not operator) nil
305 (cdr (or (assoc operator org-columns-summary-types)
306 (assoc operator org-columns-summary-types-default)
307 (error "Unknown %S operator" operator)))))
309 (defun org-columns--overlay-text (value fmt width property original)
312 (let ((v (org-columns-add-ellipses value width)))
315 (propertize v 'face (org-get-priority-face original)))
317 (if (not org-tags-special-faces-re)
318 (propertize v 'face 'org-tag)
319 (replace-regexp-in-string
320 org-tags-special-faces-re
321 (lambda (m) (propertize m 'face (org-get-tag-face m)))
323 ("TODO" (propertize v 'face (org-get-todo-face original)))
326 (defun org-columns--display-here (columns &optional dateline)
327 "Overlay the current line with column display.
328 COLUMNS is an alist (SPEC VALUE DISPLAYED). Optional argument
329 DATELINE is non-nil when the face used should be
330 `org-agenda-column-dateline'."
333 (let* ((level-face (and (looking-at "\\(\\**\\)\\(\\* \\)")
334 (org-get-level-face 2)))
335 (ref-face (or level-face
336 (and (eq major-mode 'org-agenda-mode)
337 (org-get-at-bol 'face))
339 (color (list :foreground (face-attribute ref-face :foreground)))
340 (font (list :height (face-attribute 'default :height)
341 :family (face-attribute 'default :family)))
342 (face (list color font 'org-column ref-face))
343 (face1 (list color font 'org-agenda-column-dateline ref-face)))
344 ;; Each column is an overlay on top of a character. So there has
345 ;; to be at least as many characters available on the line as
346 ;; columns to display.
347 (let ((columns (length org-columns-current-fmt-compiled))
348 (chars (- (line-end-position) (line-beginning-position))))
349 (when (> columns chars)
352 (let ((inhibit-read-only t))
353 (insert (make-string (- columns chars) ?\s))))))
354 ;; Display columns. Create and install the overlay for the
355 ;; current column on the next character.
357 (last (1- (length columns))))
358 (dolist (column columns)
360 (`(,spec ,original ,value)
361 (let* ((property (car spec))
362 (width (aref org-columns-current-maxwidths i))
363 (fmt (format (if (= i last) "%%-%d.%ds |"
366 (ov (org-columns--new-overlay
368 (org-columns--overlay-text
369 value fmt width property original)
370 (if dateline face1 face))))
371 (overlay-put ov 'keymap org-columns-map)
372 (overlay-put ov 'org-columns-key property)
373 (overlay-put ov 'org-columns-value original)
374 (overlay-put ov 'org-columns-value-modified value)
375 (overlay-put ov 'org-columns-format fmt)
376 (overlay-put ov 'line-prefix "")
377 (overlay-put ov 'wrap-prefix "")
380 ;; Make the rest of the line disappear.
381 (let ((ov (org-columns--new-overlay (point) (line-end-position))))
382 (overlay-put ov 'invisible t)
383 (overlay-put ov 'keymap org-columns-map)
384 (overlay-put ov 'line-prefix "")
385 (overlay-put ov 'wrap-prefix ""))
386 (let ((ov (make-overlay (1- (line-end-position))
387 (line-beginning-position 2))))
388 (overlay-put ov 'keymap org-columns-map)
389 (push ov org-columns-overlays))
390 (org-with-silent-modifications
391 (let ((inhibit-read-only t))
393 (line-end-position 0)
394 (line-beginning-position 2)
396 (substitute-command-keys
397 "Type \\<org-columns-map>\\[org-columns-edit-value] \
398 to edit property")))))))
400 (defun org-columns-add-ellipses (string width)
401 "Truncate STRING with WIDTH characters, with ellipses."
403 ((<= (length string) width) string)
404 ((<= width (length org-columns-ellipses))
405 (substring org-columns-ellipses 0 width))
406 (t (concat (substring string 0 (- width (length org-columns-ellipses)))
407 org-columns-ellipses))))
409 (defvar org-columns-full-header-line-format nil
410 "The full header line format, will be shifted by horizontal scrolling." )
411 (defvar org-previous-header-line-format nil
412 "The header line format before column view was turned on.")
413 (defvar org-columns-inhibit-recalculation nil
414 "Inhibit recomputing of columns on column view startup.")
415 (defvar org-columns-flyspell-was-active nil
416 "Remember the state of `flyspell-mode' before column view.
417 Flyspell-mode can cause problems in columns view, so it is turned off
418 for the duration of the command.")
420 (defvar header-line-format)
421 (defvar org-columns-previous-hscroll 0)
423 (defun org-columns--display-here-title ()
424 "Overlay the newline before the current line with the table title."
428 (dolist (column org-columns-current-fmt-compiled)
430 (`(,property ,name . ,_)
431 (let* ((width (aref org-columns-current-maxwidths i))
432 (fmt (format "%%-%d.%ds | " width width)))
433 (setq title (concat title (format fmt (or name property)))))))
435 (setq-local org-previous-header-line-format header-line-format)
436 (setq org-columns-full-header-line-format
438 (org-add-props " " nil 'display '(space :align-to 0))
439 (org-add-props (substring title 0 -1) nil 'face 'org-column-title)))
440 (setq org-columns-previous-hscroll -1)
441 (add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
443 (defun org-columns-hscoll-title ()
444 "Set the `header-line-format' so that it scrolls along with the table."
445 (sit-for .0001) ; need to force a redisplay to update window-hscroll
446 (when (not (= (window-hscroll) org-columns-previous-hscroll))
447 (setq header-line-format
448 (concat (substring org-columns-full-header-line-format 0 1)
449 (substring org-columns-full-header-line-format
450 (1+ (window-hscroll))))
451 org-columns-previous-hscroll (window-hscroll))
452 (force-mode-line-update)))
454 (defvar org-colview-initial-truncate-line-value nil
455 "Remember the value of `truncate-lines' across colview.")
458 (defun org-columns-remove-overlays ()
459 "Remove all currently active column overlays."
461 (when (marker-buffer org-columns-begin-marker)
462 (with-current-buffer (marker-buffer org-columns-begin-marker)
463 (when (local-variable-p 'org-previous-header-line-format)
464 (setq header-line-format org-previous-header-line-format)
465 (kill-local-variable 'org-previous-header-line-format)
466 (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
467 (move-marker org-columns-begin-marker nil)
468 (move-marker org-columns-top-level-marker nil)
469 (org-with-silent-modifications
470 (mapc 'delete-overlay org-columns-overlays)
471 (setq org-columns-overlays nil)
472 (let ((inhibit-read-only t))
473 (remove-text-properties (point-min) (point-max) '(read-only t))))
474 (when org-columns-flyspell-was-active
476 (when (local-variable-p 'org-colview-initial-truncate-line-value)
477 (setq truncate-lines org-colview-initial-truncate-line-value)))))
479 (defun org-columns-compact-links (s)
480 "Replace [[link][desc]] with [desc] or [link]."
481 (while (string-match org-bracket-link-regexp s)
482 (setq s (replace-match
483 (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
487 (defun org-columns-show-value ()
488 "Show the full value of the property."
490 (let ((value (get-char-property (point) 'org-columns-value)))
491 (message "Value is: %s" (or value ""))))
493 (defvar org-agenda-columns-active) ;; defined in org-agenda.el
495 (defun org-columns-quit ()
496 "Remove the column overlays and in this way exit column editing."
498 (org-with-silent-modifications
499 (org-columns-remove-overlays)
500 (let ((inhibit-read-only t))
501 (remove-text-properties (point-min) (point-max) '(read-only t))))
502 (if (not (eq major-mode 'org-agenda-mode))
503 (setq org-columns-current-fmt nil)
504 (setq org-agenda-columns-active nil)
506 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
508 (defun org-columns-check-computed ()
509 "Throw an error if current column value is computed."
510 (let ((spec (nth (current-column) org-columns-current-fmt-compiled)))
513 (assoc spec (get-text-property (line-beginning-position) 'org-summaries))
514 (error "This value is computed from the entry's children"))))
516 (defun org-columns-todo (&optional _arg)
517 "Change the TODO state during column view."
519 (org-columns-edit-value "TODO"))
521 (defun org-columns-set-tags-or-toggle (&optional _arg)
522 "Toggle checkbox at point, or set tags for current headline."
524 (if (string-match "\\`\\[[ xX-]\\]\\'"
525 (get-char-property (point) 'org-columns-value))
526 (org-columns-next-allowed-value)
527 (org-columns-edit-value "TAGS")))
529 (defvar org-agenda-overriding-columns-format nil
530 "When set, overrides any other format definition for the agenda.
531 Don't set this, this is meant for dynamic scoping.")
533 (defun org-columns-edit-value (&optional key)
534 "Edit the value of the property at point in column view.
535 Where possible, use the standard interface for changing this line."
537 (org-columns-check-computed)
538 (let* ((col (current-column))
539 (key (or key (get-char-property (point) 'org-columns-key)))
540 (value (get-char-property (point) 'org-columns-value))
541 (bol (point-at-bol)) (eol (point-at-eol))
542 (pom (or (get-text-property bol 'org-hd-marker)
543 (point))) ; keep despite of compiler waring
544 (org-columns--time (float-time (current-time)))
547 ((equal key "CLOCKSUM")
548 (error "This special column cannot be edited"))
550 (setq eval `(org-with-point-at ,pom
551 (org-edit-headline))))
553 (setq eval `(org-with-point-at ,pom
554 (call-interactively 'org-todo))))
555 ((equal key "PRIORITY")
556 (setq eval `(org-with-point-at ,pom
557 (call-interactively 'org-priority))))
559 (setq eval `(org-with-point-at ,pom
560 (let ((org-fast-tag-selection-single-key
561 (if (eq org-fast-tag-selection-single-key 'expert)
562 t org-fast-tag-selection-single-key)))
563 (call-interactively 'org-set-tags)))))
564 ((equal key "DEADLINE")
565 (setq eval `(org-with-point-at ,pom
566 (call-interactively 'org-deadline))))
567 ((equal key "SCHEDULED")
568 (setq eval `(org-with-point-at ,pom
569 (call-interactively 'org-schedule))))
570 ((equal key "BEAMER_env")
571 (setq eval `(org-with-point-at ,pom
572 (call-interactively 'org-beamer-select-environment))))
574 (setq allowed (org-property-get-allowed-values pom key 'table))
576 (setq nval (completing-read
577 "Value: " allowed nil
578 (not (get-text-property 0 'org-unrestricted
580 (setq nval (read-string "Edit: " value)))
581 (setq nval (org-trim nval))
582 (when (not (equal nval value))
583 (setq eval `(org-entry-put ,pom ,key ,nval)))))
586 ((equal major-mode 'org-agenda-mode)
587 (org-columns-eval eval)
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)))
597 (let ((inhibit-read-only t))
598 (org-with-silent-modifications
599 (remove-text-properties
600 (max (point-min) (1- bol)) eol '(read-only t)))
601 (org-columns-eval eval))
602 ;; Some properties can modify headline (e.g., "TODO"), and
603 ;; possible shuffle overlays. Make sure they are still all at
604 ;; the right place on the current line.
605 (let ((org-columns-inhibit-recalculation)) (org-columns-redo))
606 (org-columns-update key)
607 (org-move-to-column col))))))
609 (defun org-columns-edit-allowed ()
610 "Edit the list of allowed values for the current property."
612 (let* ((pom (or (org-get-at-bol 'org-marker)
613 (org-get-at-bol 'org-hd-marker)
615 (key (get-char-property (point) 'org-columns-key))
616 (key1 (concat key "_ALL"))
617 (allowed (org-entry-get pom key1 t))
619 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
620 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
621 (setq nval (read-string "Allowed: " allowed))
623 (cond ((marker-position org-entry-property-inherited-from)
624 org-entry-property-inherited-from)
625 ((marker-position org-columns-top-level-marker)
626 org-columns-top-level-marker)
630 (defun org-columns-eval (form)
633 (beginning-of-line 1)
634 ;; `next-line' is needed here, because it skips invisible line.
635 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
636 (setq hidep (org-at-heading-p 1)))
638 (and hidep (outline-hide-entry))))
640 (defun org-columns-previous-allowed-value ()
641 "Switch to the previous allowed value for this column."
643 (org-columns-next-allowed-value t))
645 (defun org-columns-next-allowed-value (&optional previous nth)
646 "Switch to the next allowed value for this column.
647 When PREVIOUS is set, go to the previous value. When NTH is
648 an integer, select that value."
650 (org-columns-check-computed)
651 (let* ((column (current-column))
652 (key (get-char-property (point) 'org-columns-key))
653 (value (get-char-property (point) 'org-columns-value))
654 (pom (or (get-text-property (line-beginning-position) 'org-hd-marker)
658 (or (org-property-get-allowed-values pom key)
659 (pcase (nth column org-columns-current-fmt-compiled)
660 (`(,_ ,_ ,_ ,(or "X" "X/" "X%") ,_) '("[ ]" "[X]")))
661 (org-colview-construct-allowed-dates value))))
662 (if previous (reverse all) all))))
663 (when (equal key "ITEM") (error "Cannot edit item headline from here"))
664 (unless (or allowed (member key '("SCHEDULED" "DEADLINE" "CLOCKSUM")))
665 (error "Allowed values for this property have not been defined"))
666 (let* ((l (length allowed))
669 ((member key '("SCHEDULED" "DEADLINE" "CLOCKSUM"))
670 (if previous 'earlier 'later))
672 (when (> (abs nth) l)
673 (user-error "Only %d allowed values for property `%s'" l key))
674 (nth (mod (1- nth) l) allowed))
675 ((member value allowed)
676 (when (= l 1) (error "Only one allowed value for this property"))
677 (or (nth 1 (member value allowed)) (car allowed)))
679 (sexp `(org-entry-put ,pom ,key ,new)))
681 ((equal major-mode 'org-agenda-mode)
682 (org-columns-eval sexp)
683 ;; The following let preserves the current format, and makes
684 ;; sure that in only a single file things need to be updated.
685 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
686 (buffer (marker-buffer pom))
687 (org-agenda-contributing-files
688 (list (with-current-buffer buffer
689 (buffer-file-name (buffer-base-buffer))))))
690 (org-agenda-columns)))
692 (let ((inhibit-read-only t))
693 (remove-text-properties (line-end-position 0) (line-end-position)
695 (org-columns-eval sexp))
696 ;; Some properties can modify headline (e.g., "TODO"), and
697 ;; possible shuffle overlays. Make sure they are still all at
698 ;; the right place on the current line.
699 (let ((org-columns-inhibit-recalculation)) (org-columns-redo))
700 (org-columns-update key)
701 (org-move-to-column column))))))
703 (defun org-colview-construct-allowed-dates (s)
704 "Construct a list of three dates around the date in S.
705 This respects the format of the time stamp in S, active or non-active,
706 and also including time or not. S must be just a time stamp, no text
708 (when (and s (string-match (concat "^" org-ts-regexp3 "$") s))
709 (let* ((time (org-parse-time-string s 'nodefaults))
710 (active (equal (string-to-char s) ?<))
711 (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
712 time-before time-after)
713 (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
714 (setf (car time) (or (car time) 0))
715 (setf (nth 1 time) (or (nth 1 time) 0))
716 (setf (nth 2 time) (or (nth 2 time) 0))
717 (setq time-before (copy-sequence time))
718 (setq time-after (copy-sequence time))
719 (setf (nth 3 time-before) (1- (nth 3 time)))
720 (setf (nth 3 time-after) (1+ (nth 3 time)))
721 (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
722 (list time-before time time-after)))))
724 (defun org-columns-open-link (&optional arg)
726 (let ((value (get-char-property (point) 'org-columns-value)))
727 (org-open-link-from-string value arg)))
730 (defun org-columns-get-format-and-top-level ()
731 (let ((fmt (org-columns-get-format)))
732 (org-columns-goto-top-level)
735 (defun org-columns-get-format (&optional fmt-string)
736 "Return columns format specifications.
737 When optional argument FMT-STRING is non-nil, use it as the
738 current specifications. This function also sets
739 `org-columns-current-fmt-compiled' and
740 `org-columns-current-fmt'."
744 (org-entry-get nil "COLUMNS" t)
745 (org-with-wide-buffer
746 (goto-char (point-min))
748 (let ((case-fold-search t))
749 (while (re-search-forward "^[ \t]*#\\+COLUMNS: .+$" nil t)
750 (let ((element (org-element-at-point)))
751 (when (eq (org-element-type element) 'keyword)
752 (throw :found (org-element-property :value element)))))
754 org-columns-default-format)))
755 (setq org-columns-current-fmt format)
756 (org-columns-compile-format format)
759 (defun org-columns-goto-top-level ()
760 "Move to the beginning of the column view area.
761 Also sets `org-columns-top-level-marker' to the new position."
764 org-columns-top-level-marker
765 (cond ((org-before-first-heading-p) (point-min))
766 ((org-entry-get nil "COLUMNS" t) org-entry-property-inherited-from)
767 (t (org-back-to-heading) (point))))))
770 (defun org-columns (&optional global columns-fmt-string)
771 "Turn on column view on an Org mode file.
773 Column view applies to the whole buffer if point is before the
774 first headline. Otherwise, it applies to the first ancestor
775 setting \"COLUMNS\" property. If there is none, it defaults to
776 the current headline. With a \\[universal-argument] prefix \
777 argument, turn on column
778 view for the whole buffer unconditionally.
780 When COLUMNS-FMT-STRING is non-nil, use it as the column format."
782 (org-columns-remove-overlays)
783 (move-marker org-columns-begin-marker (point))
784 (org-columns-goto-top-level)
785 ;; Initialize `org-columns-current-fmt' and
786 ;; `org-columns-current-fmt-compiled'.
787 (let ((org-columns--time (float-time (current-time))))
788 (org-columns-get-format columns-fmt-string)
789 (unless org-columns-inhibit-recalculation (org-columns-compute-all))
792 (when (and (not global) (org-at-heading-p))
793 (narrow-to-region (point) (org-end-of-subtree t t)))
794 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
796 (when (assoc "CLOCKSUM_T" org-columns-current-fmt-compiled)
797 (org-clock-sum-today))
799 ;; Collect contents of columns ahead of time so as to
800 ;; compute their maximum width.
802 (lambda () (cons (point) (org-columns--collect-values)))
803 nil nil (and org-columns-skip-archived-trees 'archive))))
805 (org-columns--set-widths cache)
806 (org-columns--display-here-title)
807 (when (setq-local org-columns-flyspell-was-active
808 (bound-and-true-p flyspell-mode))
810 (unless (local-variable-p 'org-colview-initial-truncate-line-value)
811 (setq-local org-colview-initial-truncate-line-value
813 (setq truncate-lines t)
814 (dolist (entry cache)
815 (goto-char (car entry))
816 (org-columns--display-here (cdr entry)))))))))
818 (defun org-columns-new (&optional spec &rest attributes)
819 "Insert a new column, to the left of the current column.
820 Interactively fill attributes for new column. When column format
821 specification SPEC is provided, edit it instead.
823 When optional argument attributes can be a list of columns
824 specifications attributes to create the new column
825 non-interactively. See `org-columns-compile-format' for
828 (let ((new (or attributes
832 (mapcar #'list (org-buffer-property-keys t nil t))
833 nil nil (nth 0 spec))))
835 (read-string (format "Column title [%s]: " prop)
837 ;; Use `read-string' instead of `read-number'
838 ;; to allow empty width.
839 (let ((w (read-string
842 (number-to-string (nth 2 spec))))))
843 (and (org-string-nw-p w) (string-to-number w)))
848 (cons '("") ;Allow empty operator.
849 (mapcar (lambda (x) (list (car x)))
851 org-columns-summary-types
852 org-columns-summary-types-default))))
855 (read-string "Format: " (nth 4 spec))))))))
857 (progn (setcar spec (car new))
858 (setcdr spec (cdr new)))
859 (push new (nthcdr (current-column) org-columns-current-fmt-compiled)))
860 (org-columns-store-format)
863 (defun org-columns-delete ()
864 "Delete the column at point from columns view."
866 (let ((spec (nth (current-column) org-columns-current-fmt-compiled)))
867 (when (y-or-n-p (format "Are you sure you want to remove column %S? "
869 (setq org-columns-current-fmt-compiled
870 (delq spec org-columns-current-fmt-compiled))
871 (org-columns-store-format)
872 ;; This may leave a now wrong value in a node property. However
873 ;; updating it may prove counter-intuitive. See comments in
874 ;; `org-columns-move-right' for details.
875 (let ((org-columns-inhibit-recalculation t)) (org-columns-redo))
876 (when (>= (current-column) (length org-columns-current-fmt-compiled))
879 (defun org-columns-edit-attributes ()
880 "Edit the attributes of the current column."
882 (org-columns-new (nth (current-column) org-columns-current-fmt-compiled)))
884 (defun org-columns-widen (arg)
885 "Make the column wider by ARG characters."
887 (let* ((n (current-column))
888 (entry (nth n org-columns-current-fmt-compiled))
889 (width (aref org-columns-current-maxwidths n)))
890 (setq width (max 1 (+ width arg)))
891 (setcar (nthcdr 2 entry) width)
892 (org-columns-store-format)
893 (let ((org-columns-inhibit-recalculation t)) (org-columns-redo))))
895 (defun org-columns-narrow (arg)
896 "Make the column narrower by ARG characters."
898 (org-columns-widen (- arg)))
900 (defun org-columns-move-right ()
901 "Swap this column with the one to the right."
903 (let* ((n (current-column))
904 (cell (nthcdr n org-columns-current-fmt-compiled))
906 (when (>= n (1- (length org-columns-current-fmt-compiled)))
907 (error "Cannot shift this column further to the right"))
909 (setcar cell (car (cdr cell)))
910 (setcdr cell (cons e (cdr (cdr cell))))
911 (org-columns-store-format)
912 ;; Do not compute again properties, since we're just moving
913 ;; columns around. It can put a property value a bit off when
914 ;; switching between an non-computed and a computed value for the
915 ;; same property, e.g. from "%A %A{+}" to "%A{+} %A".
917 ;; In this case, the value needs to be updated since the first
918 ;; column related to a property determines how its value is
919 ;; computed. However, (correctly) updating the value could be
920 ;; surprising, so we leave it as-is nonetheless.
921 (let ((org-columns-inhibit-recalculation t)) (org-columns-redo))
924 (defun org-columns-move-left ()
925 "Swap this column with the one to the left."
927 (let* ((n (current-column)))
929 (error "Cannot shift this column further to the left"))
931 (org-columns-move-right)
934 (defun org-columns-store-format ()
935 "Store the text version of the current columns format.
936 The format is stored either in the COLUMNS property of the node
937 starting the current column display, or in a #+COLUMNS line of
939 (let ((fmt (org-columns-uncompile-format org-columns-current-fmt-compiled)))
940 (setq-local org-columns-current-fmt fmt)
941 (when (marker-position org-columns-top-level-marker)
942 (org-with-wide-buffer
943 (goto-char org-columns-top-level-marker)
944 (if (and (org-at-heading-p) (org-entry-get nil "COLUMNS"))
945 (org-entry-put nil "COLUMNS" fmt)
946 (goto-char (point-min))
947 (let ((case-fold-search t))
948 ;; Try to replace the first COLUMNS keyword available.
950 (while (re-search-forward "^[ \t]*#\\+COLUMNS:\\(.*\\)" nil t)
951 (let ((element (save-match-data (org-element-at-point))))
952 (when (and (eq (org-element-type element) 'keyword)
953 (equal (org-element-property :key element)
955 (replace-match (concat " " fmt) t t nil 1)
956 (throw :found nil))))
957 ;; No COLUMNS keyword in the buffer. Insert one at the
958 ;; beginning, right before the first heading, if any.
959 (goto-char (point-min))
960 (unless (org-at-heading-p t) (outline-next-heading))
961 (let ((inhibit-read-only t))
962 (insert-before-markers "#+COLUMNS: " fmt "\n"))))
963 (setq-local org-columns-default-format fmt))))))
965 (defun org-columns-update (property)
966 "Recompute PROPERTY, and update the columns display for it."
967 (org-columns-compute property)
968 (org-with-wide-buffer
969 (let ((p (upcase property)))
970 (dolist (ov org-columns-overlays)
971 (let ((key (overlay-get ov 'org-columns-key)))
972 (when (and key (equal key p) (overlay-start ov))
973 (goto-char (overlay-start ov))
974 (let* ((spec (nth (current-column) org-columns-current-fmt-compiled))
977 (get-text-property (line-beginning-position)
979 (org-entry-get (point) key))))
981 (let ((displayed (org-columns--displayed-value spec value))
982 (format (overlay-get ov 'org-columns-format))
984 (aref org-columns-current-maxwidths (current-column))))
985 (overlay-put ov 'org-columns-value value)
986 (overlay-put ov 'org-columns-value-modified displayed)
989 (org-columns--overlay-text
990 displayed format width property value)))))))))))
992 (defun org-columns-redo ()
993 "Construct the column display again."
995 (message "Recomputing columns...")
996 (org-with-wide-buffer
997 (when (marker-position org-columns-begin-marker)
998 (goto-char org-columns-begin-marker))
999 (org-columns-remove-overlays)
1000 (if (derived-mode-p 'org-mode)
1001 ;; Since we already know the columns format, provide it instead
1002 ;; of computing again.
1003 (call-interactively #'org-columns org-columns-current-fmt)
1005 (call-interactively #'org-agenda-columns)))
1006 (message "Recomputing columns...done"))
1008 (defun org-columns-uncompile-format (compiled)
1009 "Turn the compiled columns format back into a string representation.
1010 COMPILED is an alist, as returned by
1011 `org-columns-compile-format', which see."
1015 (`(,prop ,title ,width ,op ,printf)
1017 (and width (number-to-string width))
1019 (and title (not (equal prop title)) (format "(%s)" title))
1020 (cond ((not op) nil)
1021 (printf (format "{%s;%s}" op printf))
1022 (t (format "{%s}" op)))))))
1025 (defun org-columns-compile-format (fmt)
1026 "Turn a column format string FMT into an alist of specifications.
1028 The alist has one entry for each column in the format. The elements of
1030 property the property name, as an upper-case string
1031 title the title field for the columns, as a string
1032 width the column width in characters, can be nil for automatic width
1033 operator the summary operator, as a string, or nil
1034 printf a printf format for computed values, as a string, or nil
1036 This function updates `org-columns-current-fmt-compiled'."
1037 (setq org-columns-current-fmt-compiled nil)
1039 (while (string-match
1040 "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\
1041 \\(?:{\\([^}]+\\)}\\)?\\s-*"
1043 (setq start (match-end 0))
1044 (let* ((width (and (match-end 1) (string-to-number (match-string 1 fmt))))
1045 (prop (match-string-no-properties 2 fmt))
1046 (title (or (match-string-no-properties 3 fmt) prop))
1047 (operator (match-string-no-properties 4 fmt)))
1048 (push (if (not operator) (list (upcase prop) title width nil nil)
1050 (when (string-match ";" operator)
1051 (setq printf (substring operator (match-end 0)))
1052 (setq operator (substring operator 0 (match-beginning 0))))
1053 (list (upcase prop) title width operator printf)))
1054 org-columns-current-fmt-compiled)))
1055 (setq org-columns-current-fmt-compiled
1056 (nreverse org-columns-current-fmt-compiled))))
1059 ;;;; Column View Summary
1061 (defconst org-columns--duration-re
1062 (concat "[0-9.]+ *" (regexp-opt (mapcar #'car org-effort-durations)))
1063 "Regexp matching a duration.")
1065 (defun org-columns--time-to-seconds (s)
1066 "Turn time string S into a number of seconds.
1067 A time is expressed as HH:MM, HH:MM:SS, or with units defined in
1068 `org-effort-durations'. Plain numbers are considered as hours."
1070 ((string-match "\\([0-9]+\\):\\([0-9]+\\)\\(?::\\([0-9]+\\)\\)?" s)
1071 (+ (* 3600 (string-to-number (match-string 1 s)))
1072 (* 60 (string-to-number (match-string 2 s)))
1073 (if (match-end 3) (string-to-number (match-string 3 s)) 0)))
1074 ((string-match-p org-columns--duration-re s)
1075 (* 60 (org-duration-string-to-minutes s)))
1076 (t (* 3600 (string-to-number s)))))
1078 (defun org-columns--age-to-seconds (s)
1079 "Turn age string S into a number of seconds.
1080 An age is either computed from a given time-stamp, or indicated
1081 as days/hours/minutes/seconds."
1083 ((string-match-p org-ts-regexp s)
1085 (- org-columns--time
1086 (float-time (apply #'encode-time (org-parse-time-string s))))))
1087 ;; Match own output for computations in upper levels.
1088 ((string-match "\\([0-9]+\\)d \\([0-9]+\\)h \\([0-9]+\\)m \\([0-9]+\\)s" s)
1089 (+ (* 86400 (string-to-number (match-string 1 s)))
1090 (* 3600 (string-to-number (match-string 2 s)))
1091 (* 60 (string-to-number (match-string 3 s)))
1092 (string-to-number (match-string 4 s))))
1093 (t (user-error "Invalid age: %S" s))))
1095 (defun org-columns--summary-apply-times (fun times)
1096 "Apply FUN to time values TIMES.
1097 If TIMES contains any time value expressed as a duration, return
1098 the result as a duration. If it contains any H:M:S, use that
1099 format instead. Otherwise, use H:M format."
1100 (let* ((hms-flag nil)
1108 ((string-match-p org-columns--duration-re time)
1109 (setq duration-flag t))
1111 ((string-match-p "\\`[0-9]+:[0-9]+:[0-9]+\\'" time)
1113 (org-columns--time-to-seconds time))
1115 (cond (duration-flag (org-minutes-to-clocksum-string (/ seconds 60.0)))
1116 (hms-flag (format-seconds "%h:%.2m:%.2s" seconds))
1117 (t (format-seconds "%h:%.2m" seconds)))))
1119 (defun org-columns--compute-spec (spec &optional update)
1120 "Update tree according to SPEC.
1121 SPEC is a column format specification. When optional argument
1122 UPDATE is non-nil, summarized values can replace existing ones in
1123 properties drawers."
1124 (let* ((lmax (if (bound-and-true-p org-inlinetask-min-level)
1125 org-inlinetask-min-level
1126 29)) ;Hard-code deepest level.
1127 (lvals (make-vector (1+ lmax) nil))
1131 (property (car spec))
1132 (printf (nth 4 spec))
1133 (summarize (org-columns--summarize (nth 3 spec))))
1134 (org-with-wide-buffer
1135 ;; Find the region to compute.
1136 (goto-char org-columns-top-level-marker)
1137 (goto-char (condition-case nil (org-end-of-subtree t) (error (point-max))))
1138 ;; Walk the tree from the back and do the computations.
1139 (while (re-search-backward
1140 org-outline-regexp-bol org-columns-top-level-marker t)
1141 (unless (or (= level 0) (eq level inminlevel))
1142 (setq last-level level))
1143 (setq level (org-reduced-level (org-outline-level)))
1144 (let* ((pos (match-beginning 0))
1145 (value (org-entry-get nil property))
1146 (value-set (org-string-nw-p value)))
1148 ((< level last-level)
1149 ;; Collect values from lower levels and inline tasks here
1150 ;; and summarize them using SUMMARIZE. Store them in text
1151 ;; property `org-summaries', in alist whose key is SPEC.
1154 (let ((values (append (and (/= last-level inminlevel)
1155 (aref lvals last-level))
1156 (aref lvals inminlevel))))
1157 (and values (funcall summarize values printf))))))
1158 ;; Leaf values are not summaries: do not mark them.
1160 (let* ((summaries-alist (get-text-property pos 'org-summaries))
1161 (old (assoc spec summaries-alist)))
1162 (if old (setcdr old summary)
1163 (push (cons spec summary) summaries-alist)
1164 (org-with-silent-modifications
1165 (add-text-properties
1166 pos (1+ pos) (list 'org-summaries summaries-alist)))))
1167 ;; When PROPERTY exists in current node, even if empty,
1168 ;; but its value doesn't match the one computed, use
1169 ;; the latter instead.
1170 (when (and update value (not (equal value summary)))
1171 (org-entry-put (point) property summary)))
1172 ;; Add current to current level accumulator.
1173 (when (or summary value-set)
1174 (push (or summary value) (aref lvals level)))
1175 ;; Clear accumulators for deeper levels.
1176 (cl-loop for l from (1+ level) to lmax do (aset lvals l nil))))
1177 (value-set (push value (aref lvals level)))
1181 (defun org-columns-compute (property)
1182 "Summarize the values of PROPERTY hierarchically.
1183 Also update existing values for PROPERTY according to the first
1184 column specification."
1187 (upcase-prop (upcase property)))
1188 (dolist (spec org-columns-current-fmt-compiled)
1190 (`(,(pred (equal upcase-prop)) . ,_)
1191 (org-columns--compute-spec spec main-flag)
1192 ;; Only the first summary can update the property value.
1193 (when main-flag (setq main-flag nil)))))))
1195 (defun org-columns-compute-all ()
1196 "Compute all columns that have operators defined."
1197 (org-with-silent-modifications
1198 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
1199 (let ((org-columns--time (float-time (current-time)))
1201 (dolist (spec org-columns-current-fmt-compiled)
1202 (let ((property (car spec)))
1203 ;; Property value is updated only the first time a given
1204 ;; property is encountered.
1205 (org-columns--compute-spec spec (not (member property seen)))
1206 (push property seen)))))
1208 (defun org-columns--summary-sum (values printf)
1209 "Compute the sum of VALUES.
1210 When PRINTF is non-nil, use it to format the result."
1211 (format (or printf "%s") (apply #'+ (mapcar #'string-to-number values))))
1213 (defun org-columns--summary-currencies (values _)
1214 "Compute the sum of VALUES, with two decimals."
1215 (format "%.2f" (apply #'+ (mapcar #'string-to-number values))))
1217 (defun org-columns--summary-checkbox (check-boxes _)
1218 "Summarize CHECK-BOXES with a check-box."
1219 (let ((done (cl-count "[X]" check-boxes :test #'equal))
1220 (all (length check-boxes)))
1221 (cond ((= done all) "[X]")
1225 (defun org-columns--summary-checkbox-count (check-boxes _)
1226 "Summarize CHECK-BOXES with a check-box cookie."
1228 (cl-count "[X]" check-boxes :test #'equal)
1229 (length check-boxes)))
1231 (defun org-columns--summary-checkbox-percent (check-boxes _)
1232 "Summarize CHECK-BOXES with a check-box percent."
1234 (round (* 100.0 (cl-count "[X]" check-boxes :test #'equal))
1235 (float (length check-boxes)))))
1237 (defun org-columns--summary-min (values printf)
1238 "Compute the minimum of VALUES.
1239 When PRINTF is non-nil, use it to format the result."
1240 (format (or printf "%s")
1241 (apply #'min (mapcar #'string-to-number values))))
1243 (defun org-columns--summary-max (values printf)
1244 "Compute the maximum of VALUES.
1245 When PRINTF is non-nil, use it to format the result."
1246 (format (or printf "%s")
1247 (apply #'max (mapcar #'string-to-number values))))
1249 (defun org-columns--summary-mean (values printf)
1250 "Compute the mean of VALUES.
1251 When PRINTF is non-nil, use it to format the result."
1252 (format (or printf "%s")
1253 (/ (apply #'+ (mapcar #'string-to-number values))
1254 (float (length values)))))
1256 (defun org-columns--summary-sum-times (times _)
1258 (org-columns--summary-apply-times #'+ times))
1260 (defun org-columns--summary-min-time (times _)
1261 "Compute the minimum time among TIMES."
1262 (org-columns--summary-apply-times #'min times))
1264 (defun org-columns--summary-max-time (times _)
1265 "Compute the maximum time among TIMES."
1266 (org-columns--summary-apply-times #'max times))
1268 (defun org-columns--summary-mean-time (times _)
1269 "Compute the mean time among TIMES."
1270 (org-columns--summary-apply-times
1271 (lambda (&rest values) (/ (apply #'+ values) (float (length values))))
1274 (defun org-columns--summary-min-age (ages _)
1275 "Compute the minimum time among AGES."
1278 (apply #'min (mapcar #'org-columns--age-to-seconds ages))))
1280 (defun org-columns--summary-max-age (ages _)
1281 "Compute the maximum time among AGES."
1284 (apply #'max (mapcar #'org-columns--age-to-seconds ages))))
1286 (defun org-columns--summary-mean-age (ages _)
1287 "Compute the minimum time among AGES."
1290 (/ (apply #'+ (mapcar #'org-columns--age-to-seconds ages))
1291 (float (length ages)))))
1293 (defun org-columns--summary-estimate (estimates printf)
1294 "Combine a list of estimates, using mean and variance.
1295 The mean and variance of the result will be the sum of the means
1296 and variances (respectively) of the individual estimates."
1299 (dolist (e estimates)
1300 (pcase (mapcar #'string-to-number (split-string e "-"))
1302 (let ((m (/ (+ low high) 2.0)))
1304 (cl-incf var (- (/ (+ (* low low) (* high high)) 2.0) (* m m)))))
1305 (`(,value) (cl-incf mean value))))
1306 (let ((sd (sqrt var)))
1308 (format (or printf "%.0f") (- mean sd))
1309 (format (or printf "%.0f") (+ mean sd))))))
1313 ;;; Dynamic block for Column view
1315 (defun org-columns--capture-view (maxlevel skip-empty format local)
1316 "Get the column view of the current buffer.
1318 MAXLEVEL sets the level limit. SKIP-EMPTY tells whether to skip
1319 empty rows, an empty row being one where all the column view
1320 specifiers but ITEM are empty. FORMAT is a format string for
1321 columns, or nil. When LOCAL is non-nil, only capture headings in
1324 This function returns a list containing the title row and all
1325 other rows. Each row is a list of fields, as strings, or
1327 (org-columns (not local) format)
1328 (goto-char org-columns-top-level-marker)
1329 (let ((columns (length org-columns-current-fmt-compiled))
1330 (has-item (assoc "ITEM" org-columns-current-fmt-compiled))
1334 (when (get-char-property (point) 'org-columns-key)
1336 (dotimes (i columns)
1337 (let* ((col (+ (line-beginning-position) i))
1338 (p (get-char-property col 'org-columns-key)))
1339 (push (org-quote-vert
1340 (get-char-property col
1341 (if (string= p "ITEM")
1343 'org-columns-value-modified)))
1345 (unless (and skip-empty
1346 (let ((r (delete-dups (remove "" row))))
1347 (or (null r) (and has-item (= (length r) 1)))))
1348 (push (cons (org-reduced-level (org-current-level)) (nreverse row))
1350 (and maxlevel (format "LEVEL<=%d" maxlevel))
1354 ;; Add column titles and a horizontal rule in front of the table.
1355 (cons (mapcar #'cadr org-columns-current-fmt-compiled)
1356 (cons 'hline (nreverse table)))))
1358 (defun org-columns--clean-item (item)
1359 "Remove sensitive contents from string ITEM.
1360 This includes objects that may not be duplicated within
1361 a document, e.g., a target, or those forbidden in tables, e.g.,
1362 an inline src-block."
1363 (let ((data (org-element-parse-secondary-string
1364 item (org-element-restriction 'headline))))
1365 (org-element-map data
1366 '(footnote-reference inline-babel-call inline-src-block target
1367 radio-target statistics-cookie)
1368 #'org-element-extract-element)
1369 (org-no-properties (org-element-interpret-data data))))
1372 (defun org-dblock-write:columnview (params)
1373 "Write the column view table.
1374 PARAMS is a property list of parameters:
1376 :id the :ID: property of the entry where the columns view
1377 should be built. When the symbol `local', call locally.
1378 When `global' call column view with the cursor at the beginning
1379 of the buffer (usually this means that the whole buffer switches
1380 to column view). When \"file:path/to/file.org\", invoke column
1381 view at the start of that file. Otherwise, the ID is located
1382 using `org-id-find'.
1383 :hlines When t, insert a hline before each item. When a number, insert
1384 a hline before each level <= that number.
1385 :indent When non-nil, indent each ITEM field according to its level.
1386 :vlines When t, make each column a colgroup to enforce vertical lines.
1387 :maxlevel When set to a number, don't capture headlines below this level.
1389 When t, skip rows where all specifiers other than ITEM are empty.
1390 :width apply widths specified in columns format using <N> specifiers.
1391 :format When non-nil, specify the column view format to use."
1393 (let ((id (plist-get params :id))
1397 ((or `local `nil) (setq view-pos (point)))
1398 ((and (let id-string (format "%s" id))
1399 (guard (string-match "^file:\\(.*\\)" id-string)))
1400 (setq view-file (match-string-no-properties 1 id-string))
1401 (unless (file-exists-p view-file)
1402 (user-error "No such file: %S" id-string)))
1403 ((and (let idpos (org-find-entry-with-id id)) (guard idpos))
1404 (setq view-pos idpos))
1405 ((let `(,filename . ,position) (org-id-find id))
1406 (setq view-file filename)
1407 (setq view-pos position))
1408 (_ (user-error "Cannot find entry with :ID: %s" id)))
1409 (with-current-buffer (if view-file (get-file-buffer view-file)
1411 (org-with-wide-buffer
1412 (when view-pos (goto-char view-pos))
1413 (org-columns--capture-view (plist-get params :maxlevel)
1414 (plist-get params :skip-empty-rows)
1415 (plist-get params :format)
1418 ;; Prune level information from the table. Also normalize
1419 ;; headings: remove stars, add indentation entities, if
1420 ;; required, and possibly precede some of them with a horizontal
1423 (let ((p (assoc "ITEM" org-columns-current-fmt-compiled)))
1424 (and p (cl-position p
1425 org-columns-current-fmt-compiled
1427 (hlines (plist-get params :hlines))
1428 (indent (plist-get params :indent))
1430 ;; Copy header and first rule.
1431 (push (pop table) new-table)
1432 (push (pop table) new-table)
1433 (dolist (row table (setq table (nreverse new-table)))
1434 (let ((level (car row)))
1435 (when (and (not (eq (car new-table) 'hline))
1437 (and (numberp hlines) (<= level hlines))))
1438 (push 'hline new-table))
1440 (let ((item (org-columns--clean-item (nth item-index (cdr row)))))
1441 (setf (nth item-index (cdr row))
1442 (if (and indent (> level 1))
1443 (concat "\\_" (make-string (* 2 (1- level)) ?\s) item)
1445 (push (cdr row) new-table))))
1446 (when (plist-get params :width)
1450 (mapcar (lambda (spec)
1451 (let ((w (nth 2 spec)))
1452 (if w (format "<%d>" (max 3 w)) "")))
1453 org-columns-current-fmt-compiled)))))
1454 (when (plist-get params :vlines)
1456 (let ((size (length org-columns-current-fmt-compiled)))
1457 (append (mapcar (lambda (x) (if (eq 'hline x) x (cons "" x)))
1459 (list (cons "/" (make-list size "<>")))))))
1460 (let ((content-lines (org-split-string (plist-get params :content) "\n"))
1462 ;; Insert affiliated keywords before the table.
1464 (while (string-match-p "\\`[ \t]*#\\+" (car content-lines))
1465 (insert (pop content-lines) "\n")))
1467 ;; Insert table at point.
1469 (mapconcat (lambda (row)
1470 (if (eq row 'hline) "|-|"
1471 (format "|%s|" (mapconcat #'identity row "|"))))
1474 ;; Insert TBLFM lines following table.
1475 (let ((case-fold-search t))
1476 (dolist (line content-lines)
1477 (when (string-match-p "\\`[ \t]*#\\+TBLFM:" line)
1479 (unless recalc (setq recalc t))))))
1480 (when recalc (org-table-recalculate 'all t))
1481 (org-table-align)))))
1484 (defun org-columns-insert-dblock ()
1485 "Create a dynamic block capturing a column view table."
1487 (let ((id (completing-read
1488 "Capture columns (local, global, entry with :ID: property) [local]: "
1489 (append '(("global") ("local"))
1490 (mapcar #'list (org-property-values "ID"))))))
1492 (list :name "columnview"
1494 :id (cond ((string= id "global") 'global)
1495 ((member id '("" "local")) 'local)
1497 (org-update-dblock))
1499 (define-obsolete-function-alias 'org-insert-columns-dblock
1500 'org-columns-insert-dblock "Org 9.0")
1504 ;;; Column view in the agenda
1507 (defun org-agenda-columns ()
1508 "Turn on or update column view in the agenda."
1510 (org-columns-remove-overlays)
1511 (move-marker org-columns-begin-marker (point))
1512 (let ((org-columns--time (float-time (current-time)))
1515 ((bound-and-true-p org-agenda-overriding-columns-format))
1516 ((let ((m (org-get-at-bol 'org-hd-marker)))
1518 (or (org-entry-get m "COLUMNS" t)
1519 (with-current-buffer (marker-buffer m)
1520 org-columns-default-format)))))
1521 ((and (local-variable-p 'org-columns-current-fmt)
1522 org-columns-current-fmt))
1523 ((let ((m (next-single-property-change (point-min) 'org-hd-marker)))
1525 (let ((m (get-text-property m 'org-hd-marker)))
1526 (or (org-entry-get m "COLUMNS" t)
1527 (with-current-buffer (marker-buffer m)
1528 org-columns-default-format))))))
1529 (t org-columns-default-format))))
1530 (setq-local org-columns-current-fmt fmt)
1531 (org-columns-compile-format fmt)
1532 (when org-agenda-columns-compute-summary-properties
1533 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1535 ;; Collect properties for each headline in current view.
1536 (goto-char (point-min))
1539 (let ((m (or (org-get-at-bol 'org-hd-marker)
1540 (org-get-at-bol 'org-marker))))
1542 (push (cons (line-beginning-position)
1543 (org-with-point-at m
1544 (org-columns--collect-values 'agenda)))
1548 (org-columns--set-widths cache)
1549 (org-columns--display-here-title)
1550 (when (setq-local org-columns-flyspell-was-active
1551 (bound-and-true-p flyspell-mode))
1553 (dolist (entry cache)
1554 (goto-char (car entry))
1555 (org-columns--display-here (cdr entry)))
1556 (when org-agenda-columns-show-summaries
1557 (org-agenda-colview-summarize cache)))))))
1559 (defun org-agenda-colview-summarize (cache)
1560 "Summarize the summarizable columns in column view in the agenda.
1561 This will add overlays to the date lines, to show the summary for each day."
1565 (`(,property ,title ,width . ,_)
1566 (if (member property '("CLOCKSUM" "CLOCKSUM_T"))
1567 (list property title width ":" nil)
1569 org-columns-current-fmt-compiled))
1571 ;; Ensure there's at least one summation column.
1572 (when (cl-some (lambda (spec) (nth 3 spec)) fmt)
1573 (goto-char (point-max))
1575 (when (or (get-text-property (point) 'org-date-line)
1576 (eq (get-text-property (point) 'face)
1577 'org-agenda-structure))
1578 ;; OK, this is a date line that should be used.
1580 (dolist (c cache (setq cache rest))
1581 (if (> (car c) (point))
1584 ;; Now ENTRIES contains entries below the current one.
1585 ;; CACHE is the rest. Compute the summaries for the
1586 ;; properties we want, set nil properties for the rest.
1587 (when (setq entries (mapcar 'cdr entries))
1588 (org-columns--display-here
1593 ;; Replace ITEM with current date. Preserve
1594 ;; properties for fontification.
1595 (let ((date (buffer-substring
1596 (line-beginning-position)
1597 (line-end-position))))
1598 (list spec date date)))
1599 (`(,_ ,_ ,_ nil ,_) (list spec "" ""))
1600 (`(,_ ,_ ,_ ,operator ,printf)
1601 (let* ((summarize (org-columns--summarize operator))
1603 ;; Use real values for summary, not those
1604 ;; prepared for display.
1608 (org-string-nw-p (nth 1 (assoc spec e))))
1610 (final (if values (funcall summarize values printf)
1612 (unless (equal final "")
1613 (put-text-property 0 (length final) 'face 'bold final))
1614 (list spec final final)))))
1617 (setq-local org-agenda-columns-active t)))
1618 (forward-line -1)))))
1620 (defun org-agenda-colview-compute (fmt)
1621 "Compute the relevant columns in the contributing source buffers."
1622 (let ((files org-agenda-contributing-files)
1623 (org-columns-begin-marker (make-marker))
1624 (org-columns-top-level-marker (make-marker)))
1626 (let ((b (find-buffer-visiting f)))
1627 (with-current-buffer (or (buffer-base-buffer b) b)
1628 (org-with-wide-buffer
1629 (org-with-silent-modifications
1630 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
1631 (goto-char (point-min))
1632 (org-columns-get-format-and-top-level)
1634 (let ((prop (car spec)))
1636 ((equal prop "CLOCKSUM") (org-clock-sum))
1637 ((equal prop "CLOCKSUM_T") (org-clock-sum-today))
1639 (let ((a (assoc prop org-columns-current-fmt-compiled)))
1640 (equal (nth 3 a) (nth 3 spec))))
1641 (org-columns-compute prop)))))))))))
1644 (provide 'org-colview)
1646 ;;; org-colview.el ends here