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