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