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