1 ;;; org-colview.el --- Column View in Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2017 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.
226 SPEC is a column format specification as stored in
227 `org-columns-current-fmt-compiled'. VALUE is the real value to
228 display, as a string."
229 (or (and (functionp org-columns-modify-value-for-display-function)
230 (funcall org-columns-modify-value-for-display-function
231 (nth 1 spec) ;column name
235 (concat (make-string (1- (org-current-level))
236 (if org-hide-leading-stars ?\s ?*))
238 (org-columns-compact-links value)))
239 (`(,_ ,_ ,_ ,_ nil) value)
240 ;; If PRINTF is set, assume we are displaying a number and
241 ;; obey to the format string.
242 (`(,_ ,_ ,_ ,_ ,printf) (format printf (string-to-number value)))
243 (_ (error "Invalid column specification format: %S" spec)))))
245 (defun org-columns--collect-values (&optional compiled-fmt)
246 "Collect values for columns on the current line.
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
252 initialized is set in the current buffer. However, it is
253 possible to override it with optional argument COMPILED-FMT."
254 (let ((summaries (get-text-property (point) 'org-summaries)))
259 (let* ((v (or (cdr (assoc spec summaries))
260 (org-entry-get (point) p 'selective t)
261 (and compiled-fmt ;assume `org-agenda-columns'
262 ;; Effort property is not defined. Try
263 ;; to use appointment duration.
264 org-agenda-columns-add-appointments-to-effort-sum
265 (string= p (upcase org-effort-property))
266 (get-text-property (point) 'duration)
267 (propertize (org-minutes-to-clocksum-string
268 (get-text-property (point) 'duration))
271 (list spec v (org-columns--displayed-value spec v))))))
272 (or compiled-fmt 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-hscroll-title nil 'local)))
443 (defun org-columns-hscroll-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-hscroll-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 (bol (line-beginning-position))
540 (eol (line-end-position))
541 (pom (or (get-text-property bol 'org-hd-marker) (point)))
542 (key (or key (get-char-property (point) 'org-columns-key)))
543 (org-columns--time (float-time (current-time)))
547 (error "This special column cannot be edited"))
549 (lambda () (org-with-point-at pom (org-edit-headline))))
552 (org-with-point-at pom (call-interactively #'org-todo))))
555 (org-with-point-at pom
556 (call-interactively #'org-priority))))
559 (org-with-point-at pom
560 (let ((org-fast-tag-selection-single-key
561 (if (eq org-fast-tag-selection-single-key 'expert)
563 org-fast-tag-selection-single-key)))
564 (call-interactively #'org-set-tags)))))
567 (org-with-point-at pom (call-interactively #'org-deadline))))
570 (org-with-point-at pom (call-interactively #'org-schedule))))
573 (org-with-point-at pom
574 (call-interactively #'org-beamer-select-environment))))
576 (let* ((allowed (org-property-get-allowed-values pom key 'table))
577 (value (get-char-property (point) 'org-columns-value))
579 (if (null allowed) (read-string "Edit: " value)
581 "Value: " allowed nil
582 (not (get-text-property
583 0 'org-unrestricted (caar allowed))))))))
584 (and (not (equal nval value))
585 (lambda () (org-entry-put pom key nval))))))))
588 ((eq major-mode 'org-agenda-mode)
589 (org-columns--call action)
590 ;; The following let preserves the current format, and makes
591 ;; sure that in only a single file things need to be updated.
592 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
593 (buffer (marker-buffer pom))
594 (org-agenda-contributing-files
595 (list (with-current-buffer buffer
596 (buffer-file-name (buffer-base-buffer))))))
597 (org-agenda-columns)))
599 (let ((inhibit-read-only t))
600 (org-with-silent-modifications
601 (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t)))
602 (org-columns--call action))
603 ;; Some properties can modify headline (e.g., "TODO"), and
604 ;; possible shuffle overlays. Make sure they are still all at
605 ;; the right place on the current line.
606 (let ((org-columns-inhibit-recalculation)) (org-columns-redo))
607 (org-columns-update key)
608 (org-move-to-column col)))))
610 (defun org-columns-edit-allowed ()
611 "Edit the list of allowed values for the current property."
613 (let* ((pom (or (org-get-at-bol 'org-marker)
614 (org-get-at-bol 'org-hd-marker)
616 (key (get-char-property (point) 'org-columns-key))
617 (key1 (concat key "_ALL"))
618 (allowed (org-entry-get pom key1 t))
620 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
621 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
622 (setq nval (read-string "Allowed: " allowed))
624 (cond ((marker-position org-entry-property-inherited-from)
625 org-entry-property-inherited-from)
626 ((marker-position org-columns-top-level-marker)
627 org-columns-top-level-marker)
631 (defun org-columns--call (fun)
632 "Call function FUN while preserving heading visibility.
633 FUN is a function called with no argument."
634 (let ((hide-body (and (/= (line-end-position) (point-max))
636 (move-beginning-of-line 2)
637 (org-at-heading-p t)))))
638 (unwind-protect (funcall fun)
639 (when hide-body (outline-hide-entry)))))
641 (defun org-columns-previous-allowed-value ()
642 "Switch to the previous allowed value for this column."
644 (org-columns-next-allowed-value t))
646 (defun org-columns-next-allowed-value (&optional previous nth)
647 "Switch to the next allowed value for this column.
648 When PREVIOUS is set, go to the previous value. When NTH is
649 an integer, select that value."
651 (org-columns-check-computed)
652 (let* ((column (current-column))
653 (key (get-char-property (point) 'org-columns-key))
654 (value (get-char-property (point) 'org-columns-value))
655 (pom (or (get-text-property (line-beginning-position) 'org-hd-marker)
659 (or (org-property-get-allowed-values pom key)
660 (pcase (nth column org-columns-current-fmt-compiled)
661 (`(,_ ,_ ,_ ,(or "X" "X/" "X%") ,_) '("[ ]" "[X]")))
662 (org-colview-construct-allowed-dates value))))
663 (if previous (reverse all) all))))
664 (when (equal key "ITEM") (error "Cannot edit item headline from here"))
665 (unless (or allowed (member key '("SCHEDULED" "DEADLINE" "CLOCKSUM")))
666 (error "Allowed values for this property have not been defined"))
667 (let* ((l (length allowed))
670 ((member key '("SCHEDULED" "DEADLINE" "CLOCKSUM"))
671 (if previous 'earlier 'later))
673 (when (> (abs nth) l)
674 (user-error "Only %d allowed values for property `%s'" l key))
675 (nth (mod (1- nth) l) allowed))
676 ((member value allowed)
677 (when (= l 1) (error "Only one allowed value for this property"))
678 (or (nth 1 (member value allowed)) (car allowed)))
680 (action (lambda () (org-entry-put pom key new))))
682 ((eq major-mode 'org-agenda-mode)
683 (org-columns--call action)
684 ;; The following let preserves the current format, and makes
685 ;; sure that in only a single file things need to be updated.
686 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
687 (buffer (marker-buffer pom))
688 (org-agenda-contributing-files
689 (list (with-current-buffer buffer
690 (buffer-file-name (buffer-base-buffer))))))
691 (org-agenda-columns)))
693 (let ((inhibit-read-only t))
694 (remove-text-properties (line-end-position 0) (line-end-position)
696 (org-columns--call action))
697 ;; Some properties can modify headline (e.g., "TODO"), and
698 ;; possible shuffle overlays. Make sure they are still all at
699 ;; the right place on the current line.
700 (let ((org-columns-inhibit-recalculation)) (org-columns-redo))
701 (org-columns-update key)
702 (org-move-to-column column))))))
704 (defun org-colview-construct-allowed-dates (s)
705 "Construct a list of three dates around the date in S.
706 This respects the format of the time stamp in S, active or non-active,
707 and also including time or not. S must be just a time stamp, no text
709 (when (and s (string-match (concat "^" org-ts-regexp3 "$") s))
710 (let* ((time (org-parse-time-string s 'nodefaults))
711 (active (equal (string-to-char s) ?<))
712 (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
713 time-before time-after)
714 (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
715 (setf (car time) (or (car time) 0))
716 (setf (nth 1 time) (or (nth 1 time) 0))
717 (setf (nth 2 time) (or (nth 2 time) 0))
718 (setq time-before (copy-sequence time))
719 (setq time-after (copy-sequence time))
720 (setf (nth 3 time-before) (1- (nth 3 time)))
721 (setf (nth 3 time-after) (1+ (nth 3 time)))
722 (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
723 (list time-before time time-after)))))
725 (defun org-columns-open-link (&optional arg)
727 (let ((value (get-char-property (point) 'org-columns-value)))
728 (org-open-link-from-string value arg)))
731 (defun org-columns-get-format-and-top-level ()
732 (let ((fmt (org-columns-get-format)))
733 (org-columns-goto-top-level)
736 (defun org-columns-get-format (&optional fmt-string)
737 "Return columns format specifications.
738 When optional argument FMT-STRING is non-nil, use it as the
739 current specifications. This function also sets
740 `org-columns-current-fmt-compiled' and
741 `org-columns-current-fmt'."
745 (org-entry-get nil "COLUMNS" t)
746 (org-with-wide-buffer
747 (goto-char (point-min))
749 (let ((case-fold-search t))
750 (while (re-search-forward "^[ \t]*#\\+COLUMNS: .+$" nil t)
751 (let ((element (org-element-at-point)))
752 (when (eq (org-element-type element) 'keyword)
753 (throw :found (org-element-property :value element)))))
755 org-columns-default-format)))
756 (setq org-columns-current-fmt format)
757 (org-columns-compile-format format)
760 (defun org-columns-goto-top-level ()
761 "Move to the beginning of the column view area.
762 Also sets `org-columns-top-level-marker' to the new position."
765 org-columns-top-level-marker
766 (cond ((org-before-first-heading-p) (point-min))
767 ((org-entry-get nil "COLUMNS" t) org-entry-property-inherited-from)
768 (t (org-back-to-heading) (point))))))
771 (defun org-columns (&optional global columns-fmt-string)
772 "Turn on column view on an Org mode file.
774 Column view applies to the whole buffer if point is before the
775 first headline. Otherwise, it applies to the first ancestor
776 setting \"COLUMNS\" property. If there is none, it defaults to
777 the current headline. With a `\\[universal-argument]' prefix \
778 argument, turn on column
779 view for the whole buffer unconditionally.
781 When COLUMNS-FMT-STRING is non-nil, use it as the column format."
783 (org-columns-remove-overlays)
784 (when global (goto-char (point-min)))
785 (move-marker org-columns-begin-marker (point))
786 (org-columns-goto-top-level)
787 ;; Initialize `org-columns-current-fmt' and
788 ;; `org-columns-current-fmt-compiled'.
789 (let ((org-columns--time (float-time (current-time))))
790 (org-columns-get-format columns-fmt-string)
791 (unless org-columns-inhibit-recalculation (org-columns-compute-all))
794 (when (and (not global) (org-at-heading-p))
795 (narrow-to-region (point) (org-end-of-subtree t t)))
796 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
798 (when (assoc "CLOCKSUM_T" org-columns-current-fmt-compiled)
799 (org-clock-sum-today))
801 ;; Collect contents of columns ahead of time so as to
802 ;; compute their maximum width.
804 (lambda () (cons (point) (org-columns--collect-values)))
805 nil nil (and org-columns-skip-archived-trees 'archive))))
807 (org-columns--set-widths cache)
808 (org-columns--display-here-title)
809 (when (setq-local org-columns-flyspell-was-active
810 (bound-and-true-p flyspell-mode))
812 (unless (local-variable-p 'org-colview-initial-truncate-line-value)
813 (setq-local org-colview-initial-truncate-line-value
815 (setq truncate-lines t)
816 (dolist (entry cache)
817 (goto-char (car entry))
818 (org-columns--display-here (cdr entry)))))))))
820 (defun org-columns-new (&optional spec &rest attributes)
821 "Insert a new column, to the left of the current column.
822 Interactively fill attributes for new column. When column format
823 specification SPEC is provided, edit it instead.
825 When optional argument attributes can be a list of columns
826 specifications attributes to create the new column
827 non-interactively. See `org-columns-compile-format' for
830 (let ((new (or attributes
834 (mapcar #'list (org-buffer-property-keys t nil t))
835 nil nil (nth 0 spec))))
837 (read-string (format "Column title [%s]: " prop)
839 ;; Use `read-string' instead of `read-number'
840 ;; to allow empty width.
841 (let ((w (read-string
844 (number-to-string (nth 2 spec))))))
845 (and (org-string-nw-p w) (string-to-number w)))
850 (cons '("") ;Allow empty operator.
851 (mapcar (lambda (x) (list (car x)))
853 org-columns-summary-types
854 org-columns-summary-types-default))))
857 (read-string "Format: " (nth 4 spec))))))))
859 (progn (setcar spec (car new))
860 (setcdr spec (cdr new)))
861 (push new (nthcdr (current-column) org-columns-current-fmt-compiled)))
862 (org-columns-store-format)
865 (defun org-columns-delete ()
866 "Delete the column at point from columns view."
868 (let ((spec (nth (current-column) org-columns-current-fmt-compiled)))
869 (when (y-or-n-p (format "Are you sure you want to remove column %S? "
871 (setq org-columns-current-fmt-compiled
872 (delq spec org-columns-current-fmt-compiled))
873 (org-columns-store-format)
874 ;; This may leave a now wrong value in a node property. However
875 ;; updating it may prove counter-intuitive. See comments in
876 ;; `org-columns-move-right' for details.
877 (let ((org-columns-inhibit-recalculation t)) (org-columns-redo))
878 (when (>= (current-column) (length org-columns-current-fmt-compiled))
881 (defun org-columns-edit-attributes ()
882 "Edit the attributes of the current column."
884 (org-columns-new (nth (current-column) org-columns-current-fmt-compiled)))
886 (defun org-columns-widen (arg)
887 "Make the column wider by ARG characters."
889 (let* ((n (current-column))
890 (entry (nth n org-columns-current-fmt-compiled))
891 (width (aref org-columns-current-maxwidths n)))
892 (setq width (max 1 (+ width arg)))
893 (setcar (nthcdr 2 entry) width)
894 (org-columns-store-format)
895 (let ((org-columns-inhibit-recalculation t)) (org-columns-redo))))
897 (defun org-columns-narrow (arg)
898 "Make the column narrower by ARG characters."
900 (org-columns-widen (- arg)))
902 (defun org-columns-move-right ()
903 "Swap this column with the one to the right."
905 (let* ((n (current-column))
906 (cell (nthcdr n org-columns-current-fmt-compiled))
908 (when (>= n (1- (length org-columns-current-fmt-compiled)))
909 (error "Cannot shift this column further to the right"))
911 (setcar cell (car (cdr cell)))
912 (setcdr cell (cons e (cdr (cdr cell))))
913 (org-columns-store-format)
914 ;; Do not compute again properties, since we're just moving
915 ;; columns around. It can put a property value a bit off when
916 ;; switching between an non-computed and a computed value for the
917 ;; same property, e.g. from "%A %A{+}" to "%A{+} %A".
919 ;; In this case, the value needs to be updated since the first
920 ;; column related to a property determines how its value is
921 ;; computed. However, (correctly) updating the value could be
922 ;; surprising, so we leave it as-is nonetheless.
923 (let ((org-columns-inhibit-recalculation t)) (org-columns-redo))
926 (defun org-columns-move-left ()
927 "Swap this column with the one to the left."
929 (let* ((n (current-column)))
931 (error "Cannot shift this column further to the left"))
933 (org-columns-move-right)
936 (defun org-columns-store-format ()
937 "Store the text version of the current columns format.
938 The format is stored either in the COLUMNS property of the node
939 starting the current column display, or in a #+COLUMNS line of
941 (let ((fmt (org-columns-uncompile-format org-columns-current-fmt-compiled)))
942 (setq-local org-columns-current-fmt fmt)
943 (when (marker-position org-columns-top-level-marker)
944 (org-with-wide-buffer
945 (goto-char org-columns-top-level-marker)
946 (if (and (org-at-heading-p) (org-entry-get nil "COLUMNS"))
947 (org-entry-put nil "COLUMNS" fmt)
948 (goto-char (point-min))
949 (let ((case-fold-search t))
950 ;; Try to replace the first COLUMNS keyword available.
952 (while (re-search-forward "^[ \t]*#\\+COLUMNS:\\(.*\\)" nil t)
953 (let ((element (save-match-data (org-element-at-point))))
954 (when (and (eq (org-element-type element) 'keyword)
955 (equal (org-element-property :key element)
957 (replace-match (concat " " fmt) t t nil 1)
958 (throw :found nil))))
959 ;; No COLUMNS keyword in the buffer. Insert one at the
960 ;; beginning, right before the first heading, if any.
961 (goto-char (point-min))
962 (unless (org-at-heading-p t) (outline-next-heading))
963 (let ((inhibit-read-only t))
964 (insert-before-markers "#+COLUMNS: " fmt "\n"))))
965 (setq-local org-columns-default-format fmt))))))
967 (defun org-columns-update (property)
968 "Recompute PROPERTY, and update the columns display for it."
969 (org-columns-compute property)
970 (org-with-wide-buffer
971 (let ((p (upcase property)))
972 (dolist (ov org-columns-overlays)
973 (let ((key (overlay-get ov 'org-columns-key)))
974 (when (and key (equal key p) (overlay-start ov))
975 (goto-char (overlay-start ov))
976 (let* ((spec (nth (current-column) org-columns-current-fmt-compiled))
979 (get-text-property (line-beginning-position)
981 (org-entry-get (point) key))))
983 (let ((displayed (org-columns--displayed-value spec value))
984 (format (overlay-get ov 'org-columns-format))
986 (aref org-columns-current-maxwidths (current-column))))
987 (overlay-put ov 'org-columns-value value)
988 (overlay-put ov 'org-columns-value-modified displayed)
991 (org-columns--overlay-text
992 displayed format width property value)))))))))))
994 (defun org-columns-redo ()
995 "Construct the column display again."
997 (message "Recomputing columns...")
998 (org-with-wide-buffer
999 (when (marker-position org-columns-begin-marker)
1000 (goto-char org-columns-begin-marker))
1001 (org-columns-remove-overlays)
1002 (if (derived-mode-p 'org-mode)
1003 ;; Since we already know the columns format, provide it instead
1004 ;; of computing again.
1005 (call-interactively #'org-columns org-columns-current-fmt)
1007 (call-interactively #'org-agenda-columns)))
1008 (message "Recomputing columns...done"))
1010 (defun org-columns-uncompile-format (compiled)
1011 "Turn the compiled columns format back into a string representation.
1012 COMPILED is an alist, as returned by
1013 `org-columns-compile-format', which see."
1017 (`(,prop ,title ,width ,op ,printf)
1019 (and width (number-to-string width))
1021 (and title (not (equal prop title)) (format "(%s)" title))
1022 (cond ((not op) nil)
1023 (printf (format "{%s;%s}" op printf))
1024 (t (format "{%s}" op)))))))
1027 (defun org-columns-compile-format (fmt)
1028 "Turn a column format string FMT into an alist of specifications.
1030 The alist has one entry for each column in the format. The elements of
1032 property the property name, as an upper-case string
1033 title the title field for the columns, as a string
1034 width the column width in characters, can be nil for automatic width
1035 operator the summary operator, as a string, or nil
1036 printf a printf format for computed values, as a string, or nil
1038 This function updates `org-columns-current-fmt-compiled'."
1039 (setq org-columns-current-fmt-compiled nil)
1041 (while (string-match
1042 "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\
1043 \\(?:{\\([^}]+\\)}\\)?\\s-*"
1045 (setq start (match-end 0))
1046 (let* ((width (and (match-end 1) (string-to-number (match-string 1 fmt))))
1047 (prop (match-string-no-properties 2 fmt))
1048 (title (or (match-string-no-properties 3 fmt) prop))
1049 (operator (match-string-no-properties 4 fmt)))
1050 (push (if (not operator) (list (upcase prop) title width nil nil)
1052 (when (string-match ";" operator)
1053 (setq printf (substring operator (match-end 0)))
1054 (setq operator (substring operator 0 (match-beginning 0))))
1055 (list (upcase prop) title width operator printf)))
1056 org-columns-current-fmt-compiled)))
1057 (setq org-columns-current-fmt-compiled
1058 (nreverse org-columns-current-fmt-compiled))))
1061 ;;;; Column View Summary
1063 (defconst org-columns--duration-re
1064 (concat "[0-9.]+ *" (regexp-opt (mapcar #'car org-effort-durations)))
1065 "Regexp matching a duration.")
1067 (defun org-columns--time-to-seconds (s)
1068 "Turn time string S into a number of seconds.
1069 A time is expressed as HH:MM, HH:MM:SS, or with units defined in
1070 `org-effort-durations'. Plain numbers are considered as hours."
1072 ((string-match-p org-columns--duration-re s)
1073 (* 60 (org-duration-string-to-minutes s)))
1074 ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(?::\\([0-9]+\\)\\)?\\'" s)
1075 (+ (* 3600 (string-to-number (match-string 1 s)))
1076 (* 60 (string-to-number (match-string 2 s)))
1077 (if (match-end 3) (string-to-number (match-string 3 s)) 0)))
1078 (t (* 3600 (string-to-number s)))))
1080 (defun org-columns--age-to-seconds (s)
1081 "Turn age string S into a number of seconds.
1082 An age is either computed from a given time-stamp, or indicated
1083 as days/hours/minutes/seconds."
1085 ((string-match-p org-ts-regexp s)
1087 (- org-columns--time
1088 (float-time (apply #'encode-time (org-parse-time-string s))))))
1089 ;; Match own output for computations in upper levels.
1090 ((string-match "\\([0-9]+\\)d \\([0-9]+\\)h \\([0-9]+\\)m \\([0-9]+\\)s" s)
1091 (+ (* 86400 (string-to-number (match-string 1 s)))
1092 (* 3600 (string-to-number (match-string 2 s)))
1093 (* 60 (string-to-number (match-string 3 s)))
1094 (string-to-number (match-string 4 s))))
1095 (t (user-error "Invalid age: %S" s))))
1097 (defun org-columns--summary-apply-times (fun times)
1098 "Apply FUN to time values TIMES.
1099 If TIMES contains any time value expressed as a duration, return
1100 the result as a duration. If it contains any H:M:S, use that
1101 format instead. Otherwise, use H:M format."
1102 (let* ((hms-flag nil)
1110 ((string-match-p org-columns--duration-re time)
1111 (setq duration-flag t))
1113 ((string-match-p "\\`[0-9]+:[0-9]+:[0-9]+\\'" time)
1115 (org-columns--time-to-seconds time))
1117 (cond (duration-flag (org-minutes-to-clocksum-string (/ seconds 60.0)))
1118 (hms-flag (format-seconds "%h:%.2m:%.2s" seconds))
1119 (t (format-seconds "%h:%.2m" seconds)))))
1121 (defun org-columns--compute-spec (spec &optional update)
1122 "Update tree according to SPEC.
1123 SPEC is a column format specification. When optional argument
1124 UPDATE is non-nil, summarized values can replace existing ones in
1125 properties drawers."
1126 (let* ((lmax (if (bound-and-true-p org-inlinetask-min-level)
1127 org-inlinetask-min-level
1128 29)) ;Hard-code deepest level.
1129 (lvals (make-vector (1+ lmax) nil))
1133 (property (car spec))
1134 (printf (nth 4 spec))
1135 (summarize (org-columns--summarize (nth 3 spec))))
1136 (org-with-wide-buffer
1137 ;; Find the region to compute.
1138 (goto-char org-columns-top-level-marker)
1139 (goto-char (condition-case nil (org-end-of-subtree t) (error (point-max))))
1140 ;; Walk the tree from the back and do the computations.
1141 (while (re-search-backward
1142 org-outline-regexp-bol org-columns-top-level-marker t)
1143 (unless (or (= level 0) (eq level inminlevel))
1144 (setq last-level level))
1145 (setq level (org-reduced-level (org-outline-level)))
1146 (let* ((pos (match-beginning 0))
1147 (value (org-entry-get nil property))
1148 (value-set (org-string-nw-p value)))
1150 ((< level last-level)
1151 ;; Collect values from lower levels and inline tasks here
1152 ;; and summarize them using SUMMARIZE. Store them in text
1153 ;; property `org-summaries', in alist whose key is SPEC.
1156 (let ((values (append (and (/= last-level inminlevel)
1157 (aref lvals last-level))
1158 (aref lvals inminlevel))))
1159 (and values (funcall summarize values printf))))))
1160 ;; Leaf values are not summaries: do not mark them.
1162 (let* ((summaries-alist (get-text-property pos 'org-summaries))
1163 (old (assoc spec summaries-alist)))
1164 (if old (setcdr old summary)
1165 (push (cons spec summary) summaries-alist)
1166 (org-with-silent-modifications
1167 (add-text-properties
1168 pos (1+ pos) (list 'org-summaries summaries-alist)))))
1169 ;; When PROPERTY exists in current node, even if empty,
1170 ;; but its value doesn't match the one computed, use
1171 ;; the latter instead.
1173 ;; Ignore leading or trailing white spaces that might
1174 ;; have been introduced in summary, since those are not
1175 ;; significant in properties value.
1176 (let ((new-value (org-trim summary)))
1177 (when (and update value (not (equal value new-value)))
1178 (org-entry-put (point) property new-value))))
1179 ;; Add current to current level accumulator.
1180 (when (or summary value-set)
1181 (push (or summary value) (aref lvals level)))
1182 ;; Clear accumulators for deeper levels.
1183 (cl-loop for l from (1+ level) to lmax do (aset lvals l nil))))
1184 (value-set (push value (aref lvals level)))
1188 (defun org-columns-compute (property)
1189 "Summarize the values of PROPERTY hierarchically.
1190 Also update existing values for PROPERTY according to the first
1191 column specification."
1194 (upcase-prop (upcase property)))
1195 (dolist (spec org-columns-current-fmt-compiled)
1197 (`(,(pred (equal upcase-prop)) . ,_)
1198 (org-columns--compute-spec spec main-flag)
1199 ;; Only the first summary can update the property value.
1200 (when main-flag (setq main-flag nil)))))))
1202 (defun org-columns-compute-all ()
1203 "Compute all columns that have operators defined."
1204 (org-with-silent-modifications
1205 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
1206 (let ((org-columns--time (float-time (current-time)))
1208 (dolist (spec org-columns-current-fmt-compiled)
1209 (let ((property (car spec)))
1210 ;; Property value is updated only the first time a given
1211 ;; property is encountered.
1212 (org-columns--compute-spec spec (not (member property seen)))
1213 (push property seen)))))
1215 (defun org-columns--summary-sum (values printf)
1216 "Compute the sum of VALUES.
1217 When PRINTF is non-nil, use it to format the result."
1218 (format (or printf "%s") (apply #'+ (mapcar #'string-to-number values))))
1220 (defun org-columns--summary-currencies (values _)
1221 "Compute the sum of VALUES, with two decimals."
1222 (format "%.2f" (apply #'+ (mapcar #'string-to-number values))))
1224 (defun org-columns--summary-checkbox (check-boxes _)
1225 "Summarize CHECK-BOXES with a check-box."
1226 (let ((done (cl-count "[X]" check-boxes :test #'equal))
1227 (all (length check-boxes)))
1228 (cond ((= done all) "[X]")
1232 (defun org-columns--summary-checkbox-count (check-boxes _)
1233 "Summarize CHECK-BOXES with a check-box cookie."
1235 (cl-count-if (lambda (b) (or (equal b "[X]")
1236 (string-match-p "\\[\\([1-9]\\)/\\1\\]" b)))
1238 (length check-boxes)))
1240 (defun org-columns--summary-checkbox-percent (check-boxes _)
1241 "Summarize CHECK-BOXES with a check-box percent."
1243 (round (* 100.0 (cl-count-if (lambda (b) (member b '("[X]" "[100%]")))
1245 (length check-boxes))))
1247 (defun org-columns--summary-min (values printf)
1248 "Compute the minimum of VALUES.
1249 When PRINTF is non-nil, use it to format the result."
1250 (format (or printf "%s")
1251 (apply #'min (mapcar #'string-to-number values))))
1253 (defun org-columns--summary-max (values printf)
1254 "Compute the maximum of VALUES.
1255 When PRINTF is non-nil, use it to format the result."
1256 (format (or printf "%s")
1257 (apply #'max (mapcar #'string-to-number values))))
1259 (defun org-columns--summary-mean (values printf)
1260 "Compute the mean of VALUES.
1261 When PRINTF is non-nil, use it to format the result."
1262 (format (or printf "%s")
1263 (/ (apply #'+ (mapcar #'string-to-number values))
1264 (float (length values)))))
1266 (defun org-columns--summary-sum-times (times _)
1268 (org-columns--summary-apply-times #'+ times))
1270 (defun org-columns--summary-min-time (times _)
1271 "Compute the minimum time among TIMES."
1272 (org-columns--summary-apply-times #'min times))
1274 (defun org-columns--summary-max-time (times _)
1275 "Compute the maximum time among TIMES."
1276 (org-columns--summary-apply-times #'max times))
1278 (defun org-columns--summary-mean-time (times _)
1279 "Compute the mean time among TIMES."
1280 (org-columns--summary-apply-times
1281 (lambda (&rest values) (/ (apply #'+ values) (float (length values))))
1284 (defun org-columns--summary-min-age (ages _)
1285 "Compute the minimum time among AGES."
1288 (apply #'min (mapcar #'org-columns--age-to-seconds ages))))
1290 (defun org-columns--summary-max-age (ages _)
1291 "Compute the maximum time among AGES."
1294 (apply #'max (mapcar #'org-columns--age-to-seconds ages))))
1296 (defun org-columns--summary-mean-age (ages _)
1297 "Compute the minimum time among AGES."
1300 (/ (apply #'+ (mapcar #'org-columns--age-to-seconds ages))
1301 (float (length ages)))))
1303 (defun org-columns--summary-estimate (estimates _)
1304 "Combine a list of estimates, using mean and variance.
1305 The mean and variance of the result will be the sum of the means
1306 and variances (respectively) of the individual estimates."
1309 (dolist (e estimates)
1310 (pcase (mapcar #'string-to-number (split-string e "-"))
1312 (let ((m (/ (+ low high) 2.0)))
1314 (cl-incf var (- (/ (+ (* low low) (* high high)) 2.0) (* m m)))))
1315 (`(,value) (cl-incf mean value))))
1316 (let ((sd (sqrt var)))
1318 (format "%.0f" (- mean sd))
1319 (format "%.0f" (+ mean sd))))))
1323 ;;; Dynamic block for Column view
1325 (defun org-columns--capture-view (maxlevel skip-empty format local)
1326 "Get the column view of the current buffer.
1328 MAXLEVEL sets the level limit. SKIP-EMPTY tells whether to skip
1329 empty rows, an empty row being one where all the column view
1330 specifiers but ITEM are empty. FORMAT is a format string for
1331 columns, or nil. When LOCAL is non-nil, only capture headings in
1334 This function returns a list containing the title row and all
1335 other rows. Each row is a list of fields, as strings, or
1337 (org-columns (not local) format)
1338 (goto-char org-columns-top-level-marker)
1339 (let ((columns (length org-columns-current-fmt-compiled))
1340 (has-item (assoc "ITEM" org-columns-current-fmt-compiled))
1344 (when (get-char-property (point) 'org-columns-key)
1346 (dotimes (i columns)
1347 (let* ((col (+ (line-beginning-position) i))
1348 (p (get-char-property col 'org-columns-key)))
1349 (push (org-quote-vert
1350 (get-char-property col
1351 (if (string= p "ITEM")
1353 'org-columns-value-modified)))
1355 (unless (and skip-empty
1356 (let ((r (delete-dups (remove "" row))))
1357 (or (null r) (and has-item (= (length r) 1)))))
1358 (push (cons (org-reduced-level (org-current-level)) (nreverse row))
1360 (and maxlevel (format "LEVEL<=%d" maxlevel))
1364 ;; Add column titles and a horizontal rule in front of the table.
1365 (cons (mapcar #'cadr org-columns-current-fmt-compiled)
1366 (cons 'hline (nreverse table)))))
1368 (defun org-columns--clean-item (item)
1369 "Remove sensitive contents from string ITEM.
1370 This includes objects that may not be duplicated within
1371 a document, e.g., a target, or those forbidden in tables, e.g.,
1372 an inline src-block."
1373 (let ((data (org-element-parse-secondary-string
1374 item (org-element-restriction 'headline))))
1375 (org-element-map data
1376 '(footnote-reference inline-babel-call inline-src-block target
1377 radio-target statistics-cookie)
1378 #'org-element-extract-element)
1379 (org-no-properties (org-element-interpret-data data))))
1382 (defun org-dblock-write:columnview (params)
1383 "Write the column view table.
1384 PARAMS is a property list of parameters:
1386 :id the :ID: property of the entry where the columns view
1387 should be built. When the symbol `local', call locally.
1388 When `global' call column view with the cursor at the beginning
1389 of the buffer (usually this means that the whole buffer switches
1390 to column view). When \"file:path/to/file.org\", invoke column
1391 view at the start of that file. Otherwise, the ID is located
1392 using `org-id-find'.
1393 :hlines When t, insert a hline before each item. When a number, insert
1394 a hline before each level <= that number.
1395 :indent When non-nil, indent each ITEM field according to its level.
1396 :vlines When t, make each column a colgroup to enforce vertical lines.
1397 :maxlevel When set to a number, don't capture headlines below this level.
1399 When t, skip rows where all specifiers other than ITEM are empty.
1400 :width apply widths specified in columns format using <N> specifiers.
1401 :format When non-nil, specify the column view format to use."
1403 (let ((id (plist-get params :id))
1407 ((or `local `nil) (setq view-pos (point)))
1408 ((and (let id-string (format "%s" id))
1409 (guard (string-match "^file:\\(.*\\)" id-string)))
1410 (setq view-file (match-string-no-properties 1 id-string))
1411 (unless (file-exists-p view-file)
1412 (user-error "No such file: %S" id-string)))
1413 ((and (let idpos (org-find-entry-with-id id)) (guard idpos))
1414 (setq view-pos idpos))
1415 ((let `(,filename . ,position) (org-id-find id))
1416 (setq view-file filename)
1417 (setq view-pos position))
1418 (_ (user-error "Cannot find entry with :ID: %s" id)))
1419 (with-current-buffer (if view-file (get-file-buffer view-file)
1421 (org-with-wide-buffer
1422 (when view-pos (goto-char view-pos))
1423 (org-columns--capture-view (plist-get params :maxlevel)
1424 (plist-get params :skip-empty-rows)
1425 (plist-get params :format)
1428 ;; Prune level information from the table. Also normalize
1429 ;; headings: remove stars, add indentation entities, if
1430 ;; required, and possibly precede some of them with a horizontal
1433 (let ((p (assoc "ITEM" org-columns-current-fmt-compiled)))
1434 (and p (cl-position p
1435 org-columns-current-fmt-compiled
1437 (hlines (plist-get params :hlines))
1438 (indent (plist-get params :indent))
1440 ;; Copy header and first rule.
1441 (push (pop table) new-table)
1442 (push (pop table) new-table)
1443 (dolist (row table (setq table (nreverse new-table)))
1444 (let ((level (car row)))
1445 (when (and (not (eq (car new-table) 'hline))
1447 (and (numberp hlines) (<= level hlines))))
1448 (push 'hline new-table))
1450 (let ((item (org-columns--clean-item (nth item-index (cdr row)))))
1451 (setf (nth item-index (cdr row))
1452 (if (and indent (> level 1))
1453 (concat "\\_" (make-string (* 2 (1- level)) ?\s) item)
1455 (push (cdr row) new-table))))
1456 (when (plist-get params :width)
1460 (mapcar (lambda (spec)
1461 (let ((w (nth 2 spec)))
1462 (if w (format "<%d>" (max 3 w)) "")))
1463 org-columns-current-fmt-compiled)))))
1464 (when (plist-get params :vlines)
1466 (let ((size (length org-columns-current-fmt-compiled)))
1467 (append (mapcar (lambda (x) (if (eq 'hline x) x (cons "" x)))
1469 (list (cons "/" (make-list size "<>")))))))
1470 (let ((content-lines (org-split-string (plist-get params :content) "\n"))
1472 ;; Insert affiliated keywords before the table.
1474 (while (string-match-p "\\`[ \t]*#\\+" (car content-lines))
1475 (insert (pop content-lines) "\n")))
1477 ;; Insert table at point.
1479 (mapconcat (lambda (row)
1480 (if (eq row 'hline) "|-|"
1481 (format "|%s|" (mapconcat #'identity row "|"))))
1484 ;; Insert TBLFM lines following table.
1485 (let ((case-fold-search t))
1486 (dolist (line content-lines)
1487 (when (string-match-p "\\`[ \t]*#\\+TBLFM:" line)
1489 (unless recalc (setq recalc t))))))
1490 (when recalc (org-table-recalculate 'all t))
1491 (org-table-align)))))
1494 (defun org-columns-insert-dblock ()
1495 "Create a dynamic block capturing a column view table."
1497 (let ((id (completing-read
1498 "Capture columns (local, global, entry with :ID: property) [local]: "
1499 (append '(("global") ("local"))
1500 (mapcar #'list (org-property-values "ID"))))))
1502 (list :name "columnview"
1504 :id (cond ((string= id "global") 'global)
1505 ((member id '("" "local")) 'local)
1507 (org-update-dblock))
1511 ;;; Column view in the agenda
1514 (defun org-agenda-columns ()
1515 "Turn on or update column view in the agenda."
1517 (org-columns-remove-overlays)
1518 (move-marker org-columns-begin-marker (point))
1519 (let* ((org-columns--time (float-time (current-time)))
1522 ((bound-and-true-p org-agenda-overriding-columns-format))
1523 ((let ((m (org-get-at-bol 'org-hd-marker)))
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)))
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))
1542 ;; Collect properties for each headline in current view.
1543 (goto-char (point-min))
1546 (let ((m (org-get-at-bol 'org-hd-marker)))
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)))
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))
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."
1576 (`(,property ,title ,width . ,_)
1577 (if (member property '("CLOCKSUM" "CLOCKSUM_T"))
1578 (list property title width ":" nil)
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))
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.
1593 (if (> (car c) (point))
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
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))
1616 ;; Use real values for summary, not
1617 ;; those prepared for display.
1620 (lambda (e) (org-string-nw-p
1621 (nth 1 (assoc spec e))))
1624 (funcall summarize values printf)
1626 (unless (equal final "")
1627 (put-text-property 0 (length final)
1629 (list spec final final)))))
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 (let ((files org-agenda-contributing-files)
1638 (org-columns-begin-marker (make-marker))
1639 (org-columns-top-level-marker (make-marker)))
1641 (let ((b (find-buffer-visiting f)))
1642 (with-current-buffer (or (buffer-base-buffer b) b)
1643 (org-with-wide-buffer
1644 (org-with-silent-modifications
1645 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
1646 (goto-char (point-min))
1647 (org-columns-get-format-and-top-level)
1649 (let ((prop (car spec)))
1651 ((equal prop "CLOCKSUM") (org-clock-sum))
1652 ((equal prop "CLOCKSUM_T") (org-clock-sum-today))
1654 (let ((a (assoc prop org-columns-current-fmt-compiled)))
1655 (equal (nth 3 a) (nth 3 spec))))
1656 (org-columns-compute prop)))))))))))
1659 (provide 'org-colview)
1661 ;;; org-colview.el ends here