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