1 ;;; org-colview.el --- Column View in Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2017 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; This file contains the column view for Org.
34 (declare-function org-agenda-redo
"org-agenda" (&optional all
))
35 (declare-function org-agenda-do-context-action
"org-agenda" ())
36 (declare-function org-clock-sum-today
"org-clock" (&optional headline-filter
))
37 (declare-function org-element-extract-element
"org-element" (element))
38 (declare-function org-element-interpret-data
"org-element" (data))
39 (declare-function org-element-map
"org-element" (data types fun
&optional info first-match no-recursion with-affiliated
))
40 (declare-function org-element-parse-secondary-string
"org-element" (string restriction
&optional parent
))
41 (declare-function org-element-property
"org-element" (property element
))
42 (declare-function org-element-restriction
"org-element" (element))
43 (declare-function org-element-type
"org-element" (element))
45 (defvar org-agenda-columns-add-appointments-to-effort-sum
)
46 (defvar org-agenda-columns-compute-summary-properties
)
47 (defvar org-agenda-columns-show-summaries
)
48 (defvar org-agenda-view-columns-initially
)
49 (defvar org-inlinetask-min-level
)
54 (defcustom org-columns-modify-value-for-display-function nil
55 "Function that modifies values for display in column view.
56 For example, it can be used to cut out a certain part from a time stamp.
57 The function must take 2 arguments:
59 column-title The title of the column (*not* the property name)
60 value The value that should be modified.
62 The function should return the value that should be displayed,
63 or nil if the normal value should be used."
64 :group
'org-properties
65 :type
'(choice (const nil
) (function)))
67 (defcustom org-columns-summary-types nil
68 "Alist between operators and summarize functions.
70 Each association follows the pattern (LABEL . SUMMARIZE) where
72 LABEL is a string used in #+COLUMNS definition describing the
73 summary type. It can contain any character but \"}\". It is
76 SUMMARIZE is a function called with two arguments. The first
77 argument is a non-empty list of values, as non-empty strings.
78 The second one is a format string or nil. It has to return
79 a string summarizing the list of values.
81 Note that the return value can become one value for an higher
82 order summary, so the function is expected to handle its own
85 Types defined in this variable take precedence over those defined
86 in `org-columns-summary-types-default', which see."
87 :group
'org-properties
89 :package-version
'(Org .
"9.0")
90 :type
'(alist :key-type
(string :tag
" Label")
91 :value-type
(function :tag
"Summarize")))
97 (defvar org-columns-overlays nil
98 "Holds the list of current column overlays.")
100 (defvar org-columns--time
0.0
101 "Number of seconds since the epoch, as a floating point number.")
103 (defvar-local org-columns-current-fmt nil
104 "Local variable, holds the currently active column format.")
106 (defvar-local org-columns-current-fmt-compiled nil
107 "Local variable, holds the currently active column format.
108 This is the compiled version of the format.")
110 (defvar-local org-columns-current-maxwidths nil
111 "Currently active maximum column widths, as a vector.")
113 (defvar org-columns-begin-marker
(make-marker)
114 "Points to the position where last a column creation command was called.")
116 (defvar org-columns-top-level-marker
(make-marker)
117 "Points to the position where current columns region starts.")
119 (defvar org-columns-map
(make-sparse-keymap)
120 "The keymap valid in column display.")
122 (defconst org-columns-summary-types-default
123 '(("+" . org-columns--summary-sum
)
124 ("$" . org-columns--summary-currencies
)
125 ("X" . org-columns--summary-checkbox
)
126 ("X/" . org-columns--summary-checkbox-count
)
127 ("X%" . org-columns--summary-checkbox-percent
)
128 ("max" . org-columns--summary-max
)
129 ("mean" . org-columns--summary-mean
)
130 ("min" . org-columns--summary-min
)
131 (":" . org-columns--summary-sum-times
)
132 (":max" . org-columns--summary-max-time
)
133 (":mean" . org-columns--summary-mean-time
)
134 (":min" . org-columns--summary-min-time
)
135 ("@max" . org-columns--summary-max-age
)
136 ("@mean" . org-columns--summary-mean-age
)
137 ("@min" . org-columns--summary-min-age
)
138 ("est+" . org-columns--summary-estimate
))
139 "Map operators to summarize functions.
140 See `org-columns-summary-types' for details.")
142 (defun org-columns-content ()
143 "Switch to contents view while in columns view."
148 (org-defkey org-columns-map
"c" 'org-columns-content
)
149 (org-defkey org-columns-map
"o" 'org-overview
)
150 (org-defkey org-columns-map
"e" 'org-columns-edit-value
)
151 (org-defkey org-columns-map
"\C-c\C-t" 'org-columns-todo
)
152 (org-defkey org-columns-map
"\C-c\C-c" 'org-columns-set-tags-or-toggle
)
153 (org-defkey org-columns-map
"\C-c\C-o" 'org-columns-open-link
)
154 (org-defkey org-columns-map
"v" 'org-columns-show-value
)
155 (org-defkey org-columns-map
"q" 'org-columns-quit
)
156 (org-defkey org-columns-map
"r" 'org-columns-redo
)
157 (org-defkey org-columns-map
"g" 'org-columns-redo
)
158 (org-defkey org-columns-map
[left] 'backward-char)
159 (org-defkey org-columns-map "\M-b" 'backward-char)
160 (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
161 (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
162 (org-defkey org-columns-map "\M-f"
163 (lambda () (interactive) (goto-char (1+ (point)))))
164 (org-defkey org-columns-map [right]
165 (lambda () (interactive) (goto-char (1+ (point)))))
166 (org-defkey org-columns-map [down]
167 (lambda () (interactive)
168 (let ((col (current-column)))
169 (beginning-of-line 2)
170 (while (and (org-invisible-p2) (not (eobp)))
171 (beginning-of-line 2))
173 (if (eq major-mode 'org-agenda-mode)
174 (org-agenda-do-context-action)))))
175 (org-defkey org-columns-map [up]
176 (lambda () (interactive)
177 (let ((col (current-column)))
178 (beginning-of-line 0)
179 (while (and (org-invisible-p2) (not (bobp)))
180 (beginning-of-line 0))
182 (if (eq major-mode 'org-agenda-mode)
183 (org-agenda-do-context-action)))))
184 (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
185 (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
186 (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
187 (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
188 (org-defkey org-columns-map "<" 'org-columns-narrow)
189 (org-defkey org-columns-map ">" 'org-columns-widen)
190 (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
191 (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
192 (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
193 (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
195 (org-defkey org-columns-map (number-to-string i)
196 `(lambda () (interactive)
197 (org-columns-next-allowed-value nil ,i))))
199 (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
201 ["Edit property" org-columns-edit-value t]
202 ["Next allowed value" org-columns-next-allowed-value t]
203 ["Previous allowed value" org-columns-previous-allowed-value t]
204 ["Show full value" org-columns-show-value t]
205 ["Edit allowed values" org-columns-edit-allowed t]
207 ["Edit column attributes" org-columns-edit-attributes t]
208 ["Increase column width" org-columns-widen t]
209 ["Decrease column width" org-columns-narrow t]
211 ["Move column right" org-columns-move-right t]
212 ["Move column left" org-columns-move-left t]
213 ["Add column" org-columns-new t]
214 ["Delete column" org-columns-delete t]
216 ["CONTENTS" org-columns-content t]
217 ["OVERVIEW" org-overview t]
218 ["Refresh columns display" org-columns-redo t]
220 ["Open link" org-columns-open-link t]
222 ["Quit" org-columns-quit t]))
224 (defun org-columns--displayed-value (spec value)
225 "Return displayed value for specification SPEC in current entry.
227 SPEC is a column format specification as stored in
228 `org-columns-current-fmt-compiled'. VALUE is the real value to
229 display, as a string."
231 ((and (functionp org-columns-modify-value-for-display-function)
232 (funcall org-columns-modify-value-for-display-function
235 ((equal (car spec) "ITEM")
236 (concat (make-string (1- (org-current-level))
237 (if org-hide-leading-stars ?\s ?*))
239 (org-columns-compact-links value)))
242 (defun org-columns--collect-values (&optional 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)))
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-duration-from-minutes
265 (get-text-property (point) 'duration))
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
280 (`(,_ ,_ ,(and width (pred wholenump)) . ,_) width)
282 ;; No width is specified in the columns format.
283 ;; Compute it by checking all possible values for
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)
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)
309 (let ((v (org-columns-add-ellipses value width)))
312 (propertize v 'face (org-get-priority-face original)))
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)))
320 ("TODO" (propertize v 'face (org-get-todo-face original)))
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'."
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))
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)
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.
354 (last (1- (length columns))))
355 (dolist (column columns)
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 |"
363 (ov (org-columns--new-overlay
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 "")
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))
390 (line-end-position 0)
391 (line-beginning-position 2)
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."
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."
425 (dolist (column org-columns-current-fmt-compiled)
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)))))))
432 (setq-local org-previous-header-line-format header-line-format)
433 (setq org-columns-full-header-line-format
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.")
455 (defun org-columns-remove-overlays ()
456 "Remove all currently active column overlays."
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
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) "]")
484 (defun org-columns-show-value ()
485 "Show the full value of the property."
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."
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)
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)))
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."
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."
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."
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)))
544 (error "This special column cannot be edited"))
546 (lambda () (org-with-point-at pom (org-edit-headline))))
549 (org-with-point-at pom (call-interactively #'org-todo))))
552 (org-with-point-at pom
553 (call-interactively #'org-priority))))
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)))))
564 (org-with-point-at pom (call-interactively #'org-deadline))))
567 (org-with-point-at pom (call-interactively #'org-schedule))))
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))
576 (if (null allowed) (read-string "Edit: " value)
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))))))))
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."
610 (let* ((pom (or (org-get-at-bol 'org-marker)
611 (org-get-at-bol 'org-hd-marker)
613 (key (get-char-property (point) 'org-columns-key))
614 (key1 (concat key "_ALL"))
615 (allowed (org-entry-get pom key1 t))
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))
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)
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))
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."
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."
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)
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))
667 ((member key '("SCHEDULED" "DEADLINE" "CLOCKSUM"))
668 (if previous 'earlier 'later))
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)))
677 (action (lambda () (org-entry-put pom key new))))
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)
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
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)
724 (let ((value (get-char-property (point) 'org-columns-value)))
725 (org-open-link-from-string value arg)))
728 (defun org-columns-get-format-and-top-level ()
729 (let ((fmt (org-columns-get-format)))
730 (org-columns-goto-top-level)
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'."
742 (org-entry-get nil "COLUMNS" t)
743 (org-with-wide-buffer
744 (goto-char (point-min))
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)))))
752 org-columns-default-format)))
753 (setq org-columns-current-fmt format)
754 (org-columns-compile-format 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."
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))))))
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."
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))
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)
794 (when (assoc "CLOCKSUM_T" org-columns-current-fmt-compiled)
795 (org-clock-sum-today))
797 ;; Collect contents of columns ahead of time so as to
798 ;; compute their maximum width.
800 (lambda () (cons (point) (org-columns--collect-values)))
801 nil nil (and org-columns-skip-archived-trees 'archive))))
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))
808 (unless (local-variable-p 'org-colview-initial-truncate-line-value)
809 (setq-local org-colview-initial-truncate-line-value
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
826 (let ((new (or attributes
830 (mapcar #'list (org-buffer-property-keys t nil t))
831 nil nil (nth 0 spec))))
833 (read-string (format "Column title [%s]: " prop)
835 ;; Use `read-string' instead of `read-number'
836 ;; to allow empty width.
837 (let ((w (read-string
840 (number-to-string (nth 2 spec))))))
841 (and (org-string-nw-p w) (string-to-number w)))
846 (cons '("") ;Allow empty operator.
847 (mapcar (lambda (x) (list (car x)))
849 org-columns-summary-types
850 org-columns-summary-types-default))))
853 (read-string "Format: " (nth 4 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)
861 (defun org-columns-delete ()
862 "Delete the column at point from columns view."
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? "
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))
877 (defun org-columns-edit-attributes ()
878 "Edit the attributes of the current column."
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."
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."
896 (org-columns-widen (- arg)))
898 (defun org-columns-move-right ()
899 "Swap this column with the one to the right."
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"))
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))
922 (defun org-columns-move-left ()
923 "Swap this column with the one to the left."
925 (let* ((n (current-column)))
927 (error "Cannot shift this column further to the left"))
929 (org-columns-move-right)
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
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.
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)
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))
975 (get-text-property (line-beginning-position)
977 (org-entry-get (point) key))))
979 (let ((displayed (org-columns--displayed-value spec value))
980 (format (overlay-get ov 'org-columns-format))
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)
987 (org-columns--overlay-text
988 displayed format width property value)))))))))))
990 (defun org-columns-redo ()
991 "Construct the column display again."
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)
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."
1013 (`(,prop ,title ,width ,op ,printf)
1015 (and width (number-to-string width))
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)))))))
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
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)
1037 (while (string-match
1038 "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\
1039 \\(?:{\\([^}]+\\)}\\)?\\s-*"
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)
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 (defun org-columns--age-to-minutes (s)
1060 "Turn age string S into a number of minutes.
1061 An age is either computed from a given time-stamp, or indicated
1062 as a canonical duration, i.e., using units defined in
1063 `org-duration-canonical-units'."
1065 ((string-match-p org-ts-regexp s)
1066 (/ (- org-columns--time
1067 (float-time (apply #'encode-time (org-parse-time-string s))))
1069 ((org-duration-p s) (org-duration-to-minutes s t)) ;skip user units
1070 (t (user-error "Invalid age: %S" s))))
1072 (defun org-columns--format-age (minutes)
1073 "Format MINUTES float as an age string."
1074 (org-duration-from-minutes minutes
1075 '(("d" . nil) ("h" . nil) ("min" . nil))
1076 t)) ;ignore user's custom units
1078 (defun org-columns--summary-apply-times (fun times)
1079 "Apply FUN to time values TIMES.
1080 Return the result as a duration."
1081 (org-duration-from-minutes
1083 (mapcar (lambda (time)
1084 ;; Unlike to `org-duration-to-minutes' standard
1085 ;; behavior, we want to consider plain numbers as
1086 ;; hours. As a consequence, we treat them
1088 (if (string-match-p "\\`[0-9]+\\(?:\\.[0-9]*\\)?\\'" time)
1089 (* 60 (string-to-number time))
1090 (org-duration-to-minutes time)))
1092 (org-duration-h:mm-only-p times)))
1094 (defun org-columns--compute-spec (spec &optional update)
1095 "Update tree according to SPEC.
1096 SPEC is a column format specification. When optional argument
1097 UPDATE is non-nil, summarized values can replace existing ones in
1098 properties drawers."
1099 (let* ((lmax (if (bound-and-true-p org-inlinetask-min-level)
1100 org-inlinetask-min-level
1101 29)) ;Hard-code deepest level.
1102 (lvals (make-vector (1+ lmax) nil))
1106 (property (car spec))
1107 (printf (nth 4 spec))
1108 (summarize (org-columns--summarize (nth 3 spec))))
1109 (org-with-wide-buffer
1110 ;; Find the region to compute.
1111 (goto-char org-columns-top-level-marker)
1112 (goto-char (condition-case nil (org-end-of-subtree t) (error (point-max))))
1113 ;; Walk the tree from the back and do the computations.
1114 (while (re-search-backward
1115 org-outline-regexp-bol org-columns-top-level-marker t)
1116 (unless (or (= level 0) (eq level inminlevel))
1117 (setq last-level level))
1118 (setq level (org-reduced-level (org-outline-level)))
1119 (let* ((pos (match-beginning 0))
1120 (value (org-entry-get nil property))
1121 (value-set (org-string-nw-p value)))
1123 ((< level last-level)
1124 ;; Collect values from lower levels and inline tasks here
1125 ;; and summarize them using SUMMARIZE. Store them in text
1126 ;; property `org-summaries', in alist whose key is SPEC.
1129 (let ((values (append (and (/= last-level inminlevel)
1130 (aref lvals last-level))
1131 (aref lvals inminlevel))))
1132 (and values (funcall summarize values printf))))))
1133 ;; Leaf values are not summaries: do not mark them.
1135 (let* ((summaries-alist (get-text-property pos 'org-summaries))
1136 (old (assoc spec summaries-alist)))
1137 (if old (setcdr old summary)
1138 (push (cons spec summary) summaries-alist)
1139 (org-with-silent-modifications
1140 (add-text-properties
1141 pos (1+ pos) (list 'org-summaries summaries-alist)))))
1142 ;; When PROPERTY exists in current node, even if empty,
1143 ;; but its value doesn't match the one computed, use
1144 ;; the latter instead.
1145 (when (and update value (not (equal value summary)))
1146 (org-entry-put (point) property summary)))
1147 ;; Add current to current level accumulator.
1148 (when (or summary value-set)
1149 (push (or summary value) (aref lvals level)))
1150 ;; Clear accumulators for deeper levels.
1151 (cl-loop for l from (1+ level) to lmax do (aset lvals l nil))))
1152 (value-set (push value (aref lvals level)))
1156 (defun org-columns-compute (property)
1157 "Summarize the values of PROPERTY hierarchically.
1158 Also update existing values for PROPERTY according to the first
1159 column specification."
1162 (upcase-prop (upcase property)))
1163 (dolist (spec org-columns-current-fmt-compiled)
1165 (`(,(pred (equal upcase-prop)) . ,_)
1166 (org-columns--compute-spec spec main-flag)
1167 ;; Only the first summary can update the property value.
1168 (when main-flag (setq main-flag nil)))))))
1170 (defun org-columns-compute-all ()
1171 "Compute all columns that have operators defined."
1172 (org-with-silent-modifications
1173 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
1174 (let ((org-columns--time (float-time (current-time)))
1176 (dolist (spec org-columns-current-fmt-compiled)
1177 (let ((property (car spec)))
1178 ;; Property value is updated only the first time a given
1179 ;; property is encountered.
1180 (org-columns--compute-spec spec (not (member property seen)))
1181 (push property seen)))))
1183 (defun org-columns--summary-sum (values printf)
1184 "Compute the sum of VALUES.
1185 When PRINTF is non-nil, use it to format the result."
1186 (format (or printf "%s") (apply #'+ (mapcar #'string-to-number values))))
1188 (defun org-columns--summary-currencies (values _)
1189 "Compute the sum of VALUES, with two decimals."
1190 (format "%.2f" (apply #'+ (mapcar #'string-to-number values))))
1192 (defun org-columns--summary-checkbox (check-boxes _)
1193 "Summarize CHECK-BOXES with a check-box."
1194 (let ((done (cl-count "[X]" check-boxes :test #'equal))
1195 (all (length check-boxes)))
1196 (cond ((= done all) "[X]")
1200 (defun org-columns--summary-checkbox-count (check-boxes _)
1201 "Summarize CHECK-BOXES with a check-box cookie."
1203 (cl-count "[X]" check-boxes :test #'equal)
1204 (length check-boxes)))
1206 (defun org-columns--summary-checkbox-percent (check-boxes _)
1207 "Summarize CHECK-BOXES with a check-box percent."
1209 (round (* 100.0 (cl-count "[X]" check-boxes :test #'equal))
1210 (float (length check-boxes)))))
1212 (defun org-columns--summary-min (values printf)
1213 "Compute the minimum of VALUES.
1214 When PRINTF is non-nil, use it to format the result."
1215 (format (or printf "%s")
1216 (apply #'min (mapcar #'string-to-number values))))
1218 (defun org-columns--summary-max (values printf)
1219 "Compute the maximum of VALUES.
1220 When PRINTF is non-nil, use it to format the result."
1221 (format (or printf "%s")
1222 (apply #'max (mapcar #'string-to-number values))))
1224 (defun org-columns--summary-mean (values printf)
1225 "Compute the mean of VALUES.
1226 When PRINTF is non-nil, use it to format the result."
1227 (format (or printf "%s")
1228 (/ (apply #'+ (mapcar #'string-to-number values))
1229 (float (length values)))))
1231 (defun org-columns--summary-sum-times (times _)
1233 (org-columns--summary-apply-times #'+ times))
1235 (defun org-columns--summary-min-time (times _)
1236 "Compute the minimum time among TIMES."
1237 (org-columns--summary-apply-times #'min times))
1239 (defun org-columns--summary-max-time (times _)
1240 "Compute the maximum time among TIMES."
1241 (org-columns--summary-apply-times #'max times))
1243 (defun org-columns--summary-mean-time (times _)
1244 "Compute the mean time among TIMES."
1245 (org-columns--summary-apply-times
1246 (lambda (&rest values) (/ (apply #'+ values) (float (length values))))
1249 (defun org-columns--summary-min-age (ages _)
1250 "Compute the minimum time among AGES."
1251 (org-columns--format-age
1252 (apply #'min (mapcar #'org-columns--age-to-minutes ages))))
1254 (defun org-columns--summary-max-age (ages _)
1255 "Compute the maximum time among AGES."
1256 (org-columns--format-age
1257 (apply #'max (mapcar #'org-columns--age-to-minutes ages))))
1259 (defun org-columns--summary-mean-age (ages _)
1260 "Compute the minimum time among AGES."
1261 (org-columns--format-age
1262 (/ (apply #'+ (mapcar #'org-columns--age-to-minutes ages))
1263 (float (length ages)))))
1265 (defun org-columns--summary-estimate (estimates printf)
1266 "Combine a list of estimates, using mean and variance.
1267 The mean and variance of the result will be the sum of the means
1268 and variances (respectively) of the individual estimates."
1271 (dolist (e estimates)
1272 (pcase (mapcar #'string-to-number (split-string e "-"))
1274 (let ((m (/ (+ low high) 2.0)))
1276 (cl-incf var (- (/ (+ (* low low) (* high high)) 2.0) (* m m)))))
1277 (`(,value) (cl-incf mean value))))
1278 (let ((sd (sqrt var)))
1280 (format (or printf "%.0f") (- mean sd))
1281 (format (or printf "%.0f") (+ mean sd))))))
1285 ;;; Dynamic block for Column view
1287 (defun org-columns--capture-view (maxlevel skip-empty format local)
1288 "Get the column view of the current buffer.
1290 MAXLEVEL sets the level limit. SKIP-EMPTY tells whether to skip
1291 empty rows, an empty row being one where all the column view
1292 specifiers but ITEM are empty. FORMAT is a format string for
1293 columns, or nil. When LOCAL is non-nil, only capture headings in
1296 This function returns a list containing the title row and all
1297 other rows. Each row is a list of fields, as strings, or
1299 (org-columns (not local) format)
1300 (goto-char org-columns-top-level-marker)
1301 (let ((columns (length org-columns-current-fmt-compiled))
1302 (has-item (assoc "ITEM" org-columns-current-fmt-compiled))
1306 (when (get-char-property (point) 'org-columns-key)
1308 (dotimes (i columns)
1309 (let* ((col (+ (line-beginning-position) i))
1310 (p (get-char-property col 'org-columns-key)))
1311 (push (org-quote-vert
1312 (get-char-property col
1313 (if (string= p "ITEM")
1315 'org-columns-value-modified)))
1317 (unless (and skip-empty
1318 (let ((r (delete-dups (remove "" row))))
1319 (or (null r) (and has-item (= (length r) 1)))))
1320 (push (cons (org-reduced-level (org-current-level)) (nreverse row))
1322 (and maxlevel (format "LEVEL<=%d" maxlevel))
1326 ;; Add column titles and a horizontal rule in front of the table.
1327 (cons (mapcar #'cadr org-columns-current-fmt-compiled)
1328 (cons 'hline (nreverse table)))))
1330 (defun org-columns--clean-item (item)
1331 "Remove sensitive contents from string ITEM.
1332 This includes objects that may not be duplicated within
1333 a document, e.g., a target, or those forbidden in tables, e.g.,
1334 an inline src-block."
1335 (let ((data (org-element-parse-secondary-string
1336 item (org-element-restriction 'headline))))
1337 (org-element-map data
1338 '(footnote-reference inline-babel-call inline-src-block target
1339 radio-target statistics-cookie)
1340 #'org-element-extract-element)
1341 (org-no-properties (org-element-interpret-data data))))
1344 (defun org-dblock-write:columnview (params)
1345 "Write the column view table.
1346 PARAMS is a property list of parameters:
1348 :id the :ID: property of the entry where the columns view
1349 should be built. When the symbol `local', call locally.
1350 When `global' call column view with the cursor at the beginning
1351 of the buffer (usually this means that the whole buffer switches
1352 to column view). When \"file:path/to/file.org\", invoke column
1353 view at the start of that file. Otherwise, the ID is located
1354 using `org-id-find'.
1355 :hlines When t, insert a hline before each item. When a number, insert
1356 a hline before each level <= that number.
1357 :indent When non-nil, indent each ITEM field according to its level.
1358 :vlines When t, make each column a colgroup to enforce vertical lines.
1359 :maxlevel When set to a number, don't capture headlines below this level.
1361 When t, skip rows where all specifiers other than ITEM are empty.
1362 :width apply widths specified in columns format using <N> specifiers.
1363 :format When non-nil, specify the column view format to use."
1365 (let ((id (plist-get params :id))
1369 ((or `local `nil) (setq view-pos (point)))
1370 ((and (let id-string (format "%s" id))
1371 (guard (string-match "^file:\\(.*\\)" id-string)))
1372 (setq view-file (match-string-no-properties 1 id-string))
1373 (unless (file-exists-p view-file)
1374 (user-error "No such file: %S" id-string)))
1375 ((and (let idpos (org-find-entry-with-id id)) (guard idpos))
1376 (setq view-pos idpos))
1377 ((let `(,filename . ,position) (org-id-find id))
1378 (setq view-file filename)
1379 (setq view-pos position))
1380 (_ (user-error "Cannot find entry with :ID: %s" id)))
1381 (with-current-buffer (if view-file (get-file-buffer view-file)
1383 (org-with-wide-buffer
1384 (when view-pos (goto-char view-pos))
1385 (org-columns--capture-view (plist-get params :maxlevel)
1386 (plist-get params :skip-empty-rows)
1387 (plist-get params :format)
1390 ;; Prune level information from the table. Also normalize
1391 ;; headings: remove stars, add indentation entities, if
1392 ;; required, and possibly precede some of them with a horizontal
1395 (let ((p (assoc "ITEM" org-columns-current-fmt-compiled)))
1396 (and p (cl-position p
1397 org-columns-current-fmt-compiled
1399 (hlines (plist-get params :hlines))
1400 (indent (plist-get params :indent))
1402 ;; Copy header and first rule.
1403 (push (pop table) new-table)
1404 (push (pop table) new-table)
1405 (dolist (row table (setq table (nreverse new-table)))
1406 (let ((level (car row)))
1407 (when (and (not (eq (car new-table) 'hline))
1409 (and (numberp hlines) (<= level hlines))))
1410 (push 'hline new-table))
1412 (let ((item (org-columns--clean-item (nth item-index (cdr row)))))
1413 (setf (nth item-index (cdr row))
1414 (if (and indent (> level 1))
1415 (concat "\\_" (make-string (* 2 (1- level)) ?\s) item)
1417 (push (cdr row) new-table))))
1418 (when (plist-get params :width)
1422 (mapcar (lambda (spec)
1423 (let ((w (nth 2 spec)))
1424 (if w (format "<%d>" (max 3 w)) "")))
1425 org-columns-current-fmt-compiled)))))
1426 (when (plist-get params :vlines)
1428 (let ((size (length org-columns-current-fmt-compiled)))
1429 (append (mapcar (lambda (x) (if (eq 'hline x) x (cons "" x)))
1431 (list (cons "/" (make-list size "<>")))))))
1432 (let ((content-lines (org-split-string (plist-get params :content) "\n"))
1434 ;; Insert affiliated keywords before the table.
1436 (while (string-match-p "\\`[ \t]*#\\+" (car content-lines))
1437 (insert (pop content-lines) "\n")))
1439 ;; Insert table at point.
1441 (mapconcat (lambda (row)
1442 (if (eq row 'hline) "|-|"
1443 (format "|%s|" (mapconcat #'identity row "|"))))
1446 ;; Insert TBLFM lines following table.
1447 (let ((case-fold-search t))
1448 (dolist (line content-lines)
1449 (when (string-match-p "\\`[ \t]*#\\+TBLFM:" line)
1451 (unless recalc (setq recalc t))))))
1452 (when recalc (org-table-recalculate 'all t))
1453 (org-table-align)))))
1456 (defun org-columns-insert-dblock ()
1457 "Create a dynamic block capturing a column view table."
1459 (let ((id (completing-read
1460 "Capture columns (local, global, entry with :ID: property) [local]: "
1461 (append '(("global") ("local"))
1462 (mapcar #'list (org-property-values "ID"))))))
1464 (list :name "columnview"
1466 :id (cond ((string= id "global") 'global)
1467 ((member id '("" "local")) 'local)
1469 (org-update-dblock))
1473 ;;; Column view in the agenda
1476 (defun org-agenda-columns ()
1477 "Turn on or update column view in the agenda."
1479 (org-columns-remove-overlays)
1480 (move-marker org-columns-begin-marker (point))
1481 (let* ((org-columns--time (float-time (current-time)))
1484 ((bound-and-true-p org-agenda-overriding-columns-format))
1485 ((let ((m (org-get-at-bol 'org-hd-marker)))
1487 (or (org-entry-get m "COLUMNS" t)
1488 (with-current-buffer (marker-buffer m)
1489 org-columns-default-format)))))
1490 ((and (local-variable-p 'org-columns-current-fmt)
1491 org-columns-current-fmt))
1492 ((let ((m (next-single-property-change (point-min) 'org-hd-marker)))
1494 (let ((m (get-text-property m 'org-hd-marker)))
1495 (or (org-entry-get m "COLUMNS" t)
1496 (with-current-buffer (marker-buffer m)
1497 org-columns-default-format))))))
1498 (t org-columns-default-format)))
1499 (compiled-fmt (org-columns-compile-format fmt)))
1500 (setq org-columns-current-fmt fmt)
1501 (when org-agenda-columns-compute-summary-properties
1502 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1504 ;; Collect properties for each headline in current view.
1505 (goto-char (point-min))
1508 (let ((m (or (org-get-at-bol 'org-hd-marker)
1509 (org-get-at-bol 'org-marker))))
1511 (push (cons (line-beginning-position)
1512 ;; `org-columns-current-fmt-compiled' is
1513 ;; initialized but only set locally to the
1514 ;; agenda buffer. Since current buffer is
1515 ;; changing, we need to force the original
1516 ;; compiled-fmt there.
1517 (org-with-point-at m
1518 (org-columns--collect-values compiled-fmt)))
1522 (org-columns--set-widths cache)
1523 (org-columns--display-here-title)
1524 (when (setq-local org-columns-flyspell-was-active
1525 (bound-and-true-p flyspell-mode))
1527 (dolist (entry cache)
1528 (goto-char (car entry))
1529 (org-columns--display-here (cdr entry)))
1530 (when org-agenda-columns-show-summaries
1531 (org-agenda-colview-summarize cache)))))))
1533 (defun org-agenda-colview-summarize (cache)
1534 "Summarize the summarizable columns in column view in the agenda.
1535 This will add overlays to the date lines, to show the summary for each day."
1539 (`(,property ,title ,width . ,_)
1540 (if (member property '("CLOCKSUM" "CLOCKSUM_T"))
1541 (list property title width ":" nil)
1543 org-columns-current-fmt-compiled)))
1544 ;; Ensure there's at least one summation column.
1545 (when (cl-some (lambda (spec) (nth 3 spec)) fmt)
1546 (goto-char (point-max))
1549 (when (or (get-text-property (point) 'org-date-line)
1550 (eq (get-text-property (point) 'face)
1551 'org-agenda-structure))
1552 ;; OK, this is a date line that should be used.
1556 (if (> (car c) (point))
1560 ;; ENTRIES contains entries below the current one.
1561 ;; CACHE is the rest. Compute the summaries for the
1562 ;; properties we want, set nil properties for the rest.
1563 (when (setq entries (mapcar #'cdr entries))
1564 (org-columns--display-here
1569 ;; Replace ITEM with current date. Preserve
1570 ;; properties for fontification.
1571 (let ((date (buffer-substring
1572 (line-beginning-position)
1573 (line-end-position))))
1574 (list spec date date)))
1575 (`(,_ ,_ ,_ nil ,_) (list spec "" ""))
1576 (`(,_ ,_ ,_ ,operator ,printf)
1577 (let* ((summarize (org-columns--summarize operator))
1579 ;; Use real values for summary, not
1580 ;; those prepared for display.
1583 (lambda (e) (org-string-nw-p
1584 (nth 1 (assoc spec e))))
1587 (funcall summarize values printf)
1589 (unless (equal final "")
1590 (put-text-property 0 (length final)
1592 (list spec final final)))))
1595 (setq-local org-agenda-columns-active t))))
1596 (if (bobp) (throw :complete t) (forward-line -1)))))))
1598 (defun org-agenda-colview-compute (fmt)
1599 "Compute the relevant columns in the contributing source buffers."
1600 (let ((files org-agenda-contributing-files)
1601 (org-columns-begin-marker (make-marker))
1602 (org-columns-top-level-marker (make-marker)))
1604 (let ((b (find-buffer-visiting f)))
1605 (with-current-buffer (or (buffer-base-buffer b) b)
1606 (org-with-wide-buffer
1607 (org-with-silent-modifications
1608 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
1609 (goto-char (point-min))
1610 (org-columns-get-format-and-top-level)
1612 (let ((prop (car spec)))
1614 ((equal prop "CLOCKSUM") (org-clock-sum))
1615 ((equal prop "CLOCKSUM_T") (org-clock-sum-today))
1617 (let ((a (assoc prop org-columns-current-fmt-compiled)))
1618 (equal (nth 3 a) (nth 3 spec))))
1619 (org-columns-compute prop)))))))))))
1622 (provide 'org-colview)
1624 ;;; org-colview.el ends here