1 ;;; org-colview.el --- Column View in Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2015 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.
31 (eval-when-compile (require 'cl
))
34 (declare-function org-agenda-redo
"org-agenda" ())
35 (declare-function org-agenda-do-context-action
"org-agenda" ())
36 (declare-function org-clock-sum-today
"org-clock" (&optional headline-filter
))
38 (when (featurep 'xemacs
)
39 (error "Do not load this file into XEmacs, use `org-colview-xemacs.el' from the contrib/ directory"))
43 (defvar org-columns-overlays nil
44 "Holds the list of current column overlays.")
46 (defvar org-columns-time
)
48 (defvar-local org-columns-current-fmt nil
49 "Local variable, holds the currently active column format.")
50 (defvar-local org-columns-current-fmt-compiled nil
51 "Local variable, holds the currently active column format.
52 This is the compiled version of the format.")
53 (defvar-local org-columns-current-widths nil
54 "Loval variable, holds the currently widths of fields.")
55 (defvar-local org-columns-current-maxwidths nil
56 "Loval variable, holds the currently active maximum column widths.")
57 (defvar org-columns-begin-marker
(make-marker)
58 "Points to the position where last a column creation command was called.")
59 (defvar org-columns-top-level-marker
(make-marker)
60 "Points to the position where current columns region starts.")
62 (defvar org-columns-map
(make-sparse-keymap)
63 "The keymap valid in column display.")
65 (defun org-columns-content ()
66 "Switch to contents view while in columns view."
71 (org-defkey org-columns-map
"c" 'org-columns-content
)
72 (org-defkey org-columns-map
"o" 'org-overview
)
73 (org-defkey org-columns-map
"e" 'org-columns-edit-value
)
74 (org-defkey org-columns-map
"\C-c\C-t" 'org-columns-todo
)
75 (org-defkey org-columns-map
"\C-c\C-c" 'org-columns-set-tags-or-toggle
)
76 (org-defkey org-columns-map
"\C-c\C-o" 'org-columns-open-link
)
77 (org-defkey org-columns-map
"v" 'org-columns-show-value
)
78 (org-defkey org-columns-map
"q" 'org-columns-quit
)
79 (org-defkey org-columns-map
"r" 'org-columns-redo
)
80 (org-defkey org-columns-map
"g" 'org-columns-redo
)
81 (org-defkey org-columns-map
[left] 'backward-char)
82 (org-defkey org-columns-map "\M-b" 'backward-char)
83 (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
84 (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
85 (org-defkey org-columns-map "\M-f"
86 (lambda () (interactive) (goto-char (1+ (point)))))
87 (org-defkey org-columns-map [right]
88 (lambda () (interactive) (goto-char (1+ (point)))))
89 (org-defkey org-columns-map [down]
90 (lambda () (interactive)
91 (let ((col (current-column)))
93 (while (and (org-invisible-p2) (not (eobp)))
94 (beginning-of-line 2))
96 (if (eq major-mode 'org-agenda-mode)
97 (org-agenda-do-context-action)))))
98 (org-defkey org-columns-map [up]
99 (lambda () (interactive)
100 (let ((col (current-column)))
101 (beginning-of-line 0)
102 (while (and (org-invisible-p2) (not (bobp)))
103 (beginning-of-line 0))
105 (if (eq major-mode 'org-agenda-mode)
106 (org-agenda-do-context-action)))))
107 (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
108 (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
109 (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
110 (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
111 (org-defkey org-columns-map "<" 'org-columns-narrow)
112 (org-defkey org-columns-map ">" 'org-columns-widen)
113 (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
114 (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
115 (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
116 (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
118 (org-defkey org-columns-map (number-to-string i)
119 `(lambda () (interactive)
120 (org-columns-next-allowed-value nil ,i))))
122 (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
124 ["Edit property" org-columns-edit-value t]
125 ["Next allowed value" org-columns-next-allowed-value t]
126 ["Previous allowed value" org-columns-previous-allowed-value t]
127 ["Show full value" org-columns-show-value t]
128 ["Edit allowed values" org-columns-edit-allowed t]
130 ["Edit column attributes" org-columns-edit-attributes t]
131 ["Increase column width" org-columns-widen t]
132 ["Decrease column width" org-columns-narrow t]
134 ["Move column right" org-columns-move-right t]
135 ["Move column left" org-columns-move-left t]
136 ["Add column" org-columns-new t]
137 ["Delete column" org-columns-delete t]
139 ["CONTENTS" org-columns-content t]
140 ["OVERVIEW" org-overview t]
141 ["Refresh columns display" org-columns-redo t]
143 ["Open link" org-columns-open-link t]
145 ["Quit" org-columns-quit t]))
147 (defun org-columns--value (property pos)
148 "Return value for PROPERTY at buffer position POS."
149 (or (cdr (assoc-string property (get-text-property pos 'org-summaries) t))
150 (org-entry-get pos property 'selective t)))
152 (defun org-columns-new-overlay (beg end &optional string face)
153 "Create a new column overlay and add it to the list."
154 (let ((ov (make-overlay beg end)))
155 (overlay-put ov 'face (or face 'secondary-selection))
156 (org-overlay-display ov string face)
157 (push ov org-columns-overlays)
160 (defun org-columns-display-here (&optional props dateline)
161 "Overlay the current line with column display."
165 (let* ((level-face (and (looking-at "\\(\\**\\)\\(\\* \\)")
166 (org-get-level-face 2)))
167 (ref-face (or level-face
168 (and (eq major-mode 'org-agenda-mode)
169 (org-get-at-bol 'face))
171 (color (list :foreground (face-attribute ref-face :foreground)))
172 (font (list :height (face-attribute 'default :height)
173 :family (face-attribute 'default :family)))
174 (face (list color font 'org-column ref-face))
175 (face1 (list color font 'org-agenda-column-dateline ref-face))
176 (pom (and (eq major-mode 'org-agenda-mode)
177 (or (org-get-at-bol 'org-hd-marker)
178 (org-get-at-bol 'org-marker))))
180 ((eq major-mode 'org-agenda-mode)
181 (and pom (org-entry-properties pom)))
182 (t (org-entry-properties)))))
183 ;; Each column is an overlay on top of a character. So there has
184 ;; to be at least as many characters available on the line as
185 ;; columns to display.
186 (let ((columns (length org-columns-current-fmt-compiled))
187 (chars (- (line-end-position) (line-beginning-position))))
188 (when (> columns chars)
191 (let ((inhibit-read-only t))
192 (insert (make-string (- columns chars) ?\s))))))
193 ;; Walk the format. Create and install the overlay for the
194 ;; current column on the next character.
195 (dolist (column org-columns-current-fmt-compiled)
196 (let* ((property (car column))
197 (title (nth 1 column))
198 (ass (assoc-string property props t))
201 (cdr (assoc-string property org-columns-current-maxwidths t))
204 (f (format "%%-%d.%ds | " width width))
207 (calc (nth 7 column))
208 (val (or (cdr ass) ""))
211 ((functionp org-columns-modify-value-for-display-function)
212 (funcall org-columns-modify-value-for-display-function
214 ((equal property "ITEM") (org-columns-compact-links val))
215 (fc (org-columns-number-to-string
216 (org-columns-string-to-number val fm) fm fc))
217 ((and calc (functionp calc)
218 (not (string= val ""))
219 (not (get-text-property 0 'org-computed val)))
220 (org-columns-number-to-string
221 (funcall calc (org-columns-string-to-number val fm)) fm))))
224 (let ((v (org-columns-add-ellipses
225 (or modval val) width)))
227 ((equal property "PRIORITY")
228 (propertize v 'face (org-get-priority-face val)))
229 ((equal property "TAGS")
230 (if (not org-tags-special-faces-re)
231 (propertize v 'face 'org-tag)
232 (replace-regexp-in-string
233 org-tags-special-faces-re
235 (propertize m 'face (org-get-tag-face m)))
237 ((equal property "TODO")
238 (propertize v 'face (org-get-todo-face val)))
240 (ov (org-columns-new-overlay
241 (point) (1+ (point)) string (if dateline face1 face))))
242 (overlay-put ov 'keymap org-columns-map)
243 (overlay-put ov 'org-columns-key property)
244 (overlay-put ov 'org-columns-value (cdr ass))
245 (overlay-put ov 'org-columns-value-modified modval)
246 (overlay-put ov 'org-columns-pom pom)
247 (overlay-put ov 'org-columns-format f)
248 (overlay-put ov 'line-prefix "")
249 (overlay-put ov 'wrap-prefix "")
251 ;; Make the rest of the line disappear.
252 (let ((ov (org-columns-new-overlay (point) (line-end-position))))
253 (overlay-put ov 'invisible t)
254 (overlay-put ov 'keymap org-columns-map)
255 (overlay-put ov 'line-prefix "")
256 (overlay-put ov 'wrap-prefix ""))
257 (let ((ov (make-overlay (1- (line-end-position))
258 (line-beginning-position 2))))
259 (overlay-put ov 'keymap org-columns-map)
260 (push ov org-columns-overlays))
261 (org-with-silent-modifications
262 (let ((inhibit-read-only t))
264 (line-end-position 0)
265 (line-beginning-position 2)
267 (substitute-command-keys
268 "Type \\<org-columns-map>\\[org-columns-edit-value] \
269 to edit property")))))))
271 (defun org-columns-add-ellipses (string width)
272 "Truncate STRING with WIDTH characters, with ellipses."
274 ((<= (length string) width) string)
275 ((<= width (length org-columns-ellipses))
276 (substring org-columns-ellipses 0 width))
277 (t (concat (substring string 0 (- width (length org-columns-ellipses)))
278 org-columns-ellipses))))
280 (defvar org-columns-full-header-line-format nil
281 "The full header line format, will be shifted by horizontal scrolling." )
282 (defvar org-previous-header-line-format nil
283 "The header line format before column view was turned on.")
284 (defvar org-columns-inhibit-recalculation nil
285 "Inhibit recomputing of columns on column view startup.")
286 (defvar org-columns-flyspell-was-active nil
287 "Remember the state of `flyspell-mode' before column view.
288 Flyspell-mode can cause problems in columns view, so it is turned off
289 for the duration of the command.")
291 (defvar header-line-format)
292 (defvar org-columns-previous-hscroll 0)
294 (defun org-columns-display-here-title ()
295 "Overlay the newline before the current line with the table title."
297 (let ((fmt org-columns-current-fmt-compiled)
299 property width f column str widths)
300 (while (setq column (pop fmt))
301 (setq property (car column)
302 str (or (nth 1 column) property)
303 width (or (cdr (assoc-string property
304 org-columns-current-maxwidths
308 widths (push width widths)
309 f (format "%%-%d.%ds | " width width)
310 string (format f str)
311 title (concat title string)))
313 (org-add-props " " nil 'display '(space :align-to 0))
314 ;;(org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
315 (org-add-props title nil 'face 'org-column-title)))
316 (setq-local org-previous-header-line-format header-line-format)
317 (setq-local org-columns-current-widths (nreverse widths))
318 (setq org-columns-full-header-line-format title)
319 (setq org-columns-previous-hscroll -1)
320 ; (org-columns-hscoll-title)
321 (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
323 (defun org-columns-hscoll-title ()
324 "Set the `header-line-format' so that it scrolls along with the table."
325 (sit-for .0001) ; need to force a redisplay to update window-hscroll
326 (when (not (= (window-hscroll) org-columns-previous-hscroll))
327 (setq header-line-format
328 (concat (substring org-columns-full-header-line-format 0 1)
329 (substring org-columns-full-header-line-format
330 (1+ (window-hscroll))))
331 org-columns-previous-hscroll (window-hscroll))
332 (force-mode-line-update)))
334 (defvar org-colview-initial-truncate-line-value nil
335 "Remember the value of `truncate-lines' across colview.")
338 (defun org-columns-remove-overlays ()
339 "Remove all currently active column overlays."
341 (when (marker-buffer org-columns-begin-marker)
342 (with-current-buffer (marker-buffer org-columns-begin-marker)
343 (when (local-variable-p 'org-previous-header-line-format)
344 (setq header-line-format org-previous-header-line-format)
345 (kill-local-variable 'org-previous-header-line-format)
346 (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
347 (move-marker org-columns-begin-marker nil)
348 (move-marker org-columns-top-level-marker nil)
349 (org-with-silent-modifications
350 (mapc 'delete-overlay org-columns-overlays)
351 (setq org-columns-overlays nil)
352 (let ((inhibit-read-only t))
353 (remove-text-properties (point-min) (point-max) '(read-only t))))
354 (when org-columns-flyspell-was-active
356 (when (local-variable-p 'org-colview-initial-truncate-line-value)
357 (setq truncate-lines org-colview-initial-truncate-line-value)))))
359 (defun org-columns-compact-links (s)
360 "Replace [[link][desc]] with [desc] or [link]."
361 (while (string-match org-bracket-link-regexp s)
362 (setq s (replace-match
363 (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
367 (defun org-columns-show-value ()
368 "Show the full value of the property."
370 (let ((value (get-char-property (point) 'org-columns-value)))
371 (message "Value is: %s" (or value ""))))
373 (defvar org-agenda-columns-active) ;; defined in org-agenda.el
375 (defun org-columns-quit ()
376 "Remove the column overlays and in this way exit column editing."
378 (org-with-silent-modifications
379 (org-columns-remove-overlays)
380 (let ((inhibit-read-only t))
381 (remove-text-properties (point-min) (point-max) '(read-only t))))
382 (when (eq major-mode 'org-agenda-mode)
383 (setq org-agenda-columns-active nil)
385 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
387 (defun org-columns-check-computed ()
388 "Check if this column value is computed.
389 If yes, throw an error indicating that changing it does not make sense."
390 (let ((val (get-char-property (point) 'org-columns-value)))
391 (when (and (stringp val)
392 (get-char-property 0 'org-computed val))
393 (error "This value is computed from the entry's children"))))
395 (defun org-columns-todo (&optional _arg)
396 "Change the TODO state during column view."
398 (org-columns-edit-value "TODO"))
400 (defun org-columns-set-tags-or-toggle (&optional _arg)
401 "Toggle checkbox at point, or set tags for current headline."
403 (if (string-match "\\`\\[[ xX-]\\]\\'"
404 (get-char-property (point) 'org-columns-value))
405 (org-columns-next-allowed-value)
406 (org-columns-edit-value "TAGS")))
408 (defvar org-agenda-overriding-columns-format nil
409 "When set, overrides any other format definition for the agenda.
410 Don't set this, this is meant for dynamic scoping.")
412 (defun org-columns-edit-value (&optional key)
413 "Edit the value of the property at point in column view.
414 Where possible, use the standard interface for changing this line."
416 (org-columns-check-computed)
417 (let* ((col (current-column))
418 (key (or key (get-char-property (point) 'org-columns-key)))
419 (value (get-char-property (point) 'org-columns-value))
420 (bol (point-at-bol)) (eol (point-at-eol))
421 (pom (or (get-text-property bol 'org-hd-marker)
422 (point))) ; keep despite of compiler waring
424 (delq nil (mapcar (lambda (x)
425 (and (eq (overlay-buffer x) (current-buffer))
426 (>= (overlay-start x) bol)
427 (<= (overlay-start x) eol)
429 org-columns-overlays)))
430 (org-columns-time (time-to-number-of-days (current-time)))
433 ((equal key "CLOCKSUM")
434 (error "This special column cannot be edited"))
436 (setq eval '(org-with-point-at pom
437 (org-edit-headline))))
439 (setq eval '(org-with-point-at
441 (call-interactively 'org-todo))))
442 ((equal key "PRIORITY")
443 (setq eval '(org-with-point-at pom
444 (call-interactively 'org-priority))))
446 (setq eval '(org-with-point-at pom
447 (let ((org-fast-tag-selection-single-key
448 (if (eq org-fast-tag-selection-single-key 'expert)
449 t org-fast-tag-selection-single-key)))
450 (call-interactively 'org-set-tags)))))
451 ((equal key "DEADLINE")
452 (setq eval '(org-with-point-at pom
453 (call-interactively 'org-deadline))))
454 ((equal key "SCHEDULED")
455 (setq eval '(org-with-point-at pom
456 (call-interactively 'org-schedule))))
457 ((equal key "BEAMER_env")
458 (setq eval '(org-with-point-at pom
459 (call-interactively 'org-beamer-select-environment))))
461 (setq allowed (org-property-get-allowed-values pom key 'table))
463 (setq nval (org-icompleting-read
464 "Value: " allowed nil
465 (not (get-text-property 0 'org-unrestricted
467 (setq nval (read-string "Edit: " value)))
468 (setq nval (org-trim nval))
469 (when (not (equal nval value))
470 (setq eval '(org-entry-put pom key nval)))))
474 ((equal major-mode 'org-agenda-mode)
475 (org-columns-eval eval)
476 ;; The following let preserves the current format, and makes sure
477 ;; that in only a single file things need to be updated.
478 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
479 (buffer (marker-buffer pom))
480 (org-agenda-contributing-files
481 (list (with-current-buffer buffer
482 (buffer-file-name (buffer-base-buffer))))))
483 (org-agenda-columns)))
485 (let ((inhibit-read-only t))
486 (org-with-silent-modifications
487 (remove-text-properties
488 (max (point-min) (1- bol)) eol '(read-only t)))
491 (setq org-columns-overlays
492 (org-delete-all line-overlays org-columns-overlays))
493 (mapc 'delete-overlay line-overlays)
494 (org-columns-eval eval))
495 (org-columns-display-here)))
496 (org-move-to-column col)
497 (if (and (derived-mode-p 'org-mode)
498 (nth 3 (assoc-string key org-columns-current-fmt-compiled t)))
499 (org-columns-update key)))))))
501 (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
502 "Edit the current headline, the part without TODO keyword, TAGS."
503 (org-back-to-heading)
504 (when (looking-at org-todo-line-regexp)
506 (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
507 (txt (match-string 3))
510 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@#%]+:[ \t]*$") txt)
511 (setq post (match-string 0 txt)
512 txt (substring txt 0 (match-beginning 0))))
513 (setq txt2 (read-string "Edit: " txt))
514 (when (not (equal txt txt2))
516 (insert pre txt2 post)
517 (delete-region (point) (point-at-eol))
518 (org-set-tags nil t)))))
520 (defun org-columns-edit-allowed ()
521 "Edit the list of allowed values for the current property."
523 (let* ((pom (or (org-get-at-bol 'org-marker)
524 (org-get-at-bol 'org-hd-marker)
526 (key (get-char-property (point) 'org-columns-key))
527 (key1 (concat key "_ALL"))
528 (allowed (org-entry-get pom key1 t))
530 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
531 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
532 (setq nval (read-string "Allowed: " allowed))
534 (cond ((marker-position org-entry-property-inherited-from)
535 org-entry-property-inherited-from)
536 ((marker-position org-columns-top-level-marker)
537 org-columns-top-level-marker)
541 (defun org-columns-eval (form)
544 (beginning-of-line 1)
545 ;; `next-line' is needed here, because it skips invisible line.
546 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
547 (setq hidep (org-at-heading-p 1)))
549 (and hidep (outline-hide-entry))))
551 (defun org-columns-previous-allowed-value ()
552 "Switch to the previous allowed value for this column."
554 (org-columns-next-allowed-value t))
556 (defun org-columns-next-allowed-value (&optional previous nth)
557 "Switch to the next allowed value for this column.
558 When PREVIOUS is set, go to the previous value. When NTH is
559 an integer, select that value."
561 (org-columns-check-computed)
562 (let* ((col (current-column))
563 (key (get-char-property (point) 'org-columns-key))
564 (value (get-char-property (point) 'org-columns-value))
565 (bol (point-at-bol)) (eol (point-at-eol))
566 (pom (or (get-text-property bol 'org-hd-marker)
567 (point))) ; keep despite of compiler waring
569 (delq nil (mapcar (lambda (x)
570 (and (eq (overlay-buffer x) (current-buffer))
571 (>= (overlay-start x) bol)
572 (<= (overlay-start x) eol)
574 org-columns-overlays)))
575 (allowed (or (org-property-get-allowed-values pom key)
577 (nth 4 (assoc-string key
578 org-columns-current-fmt-compiled
580 '(checkbox checkbox-n-of-m checkbox-percent))
582 (org-colview-construct-allowed-dates value)))
586 (if (= nth -1) (setq nth 9)))
587 (when (equal key "ITEM")
588 (error "Cannot edit item headline from here"))
589 (unless (or allowed (member key '("SCHEDULED" "DEADLINE" "CLOCKSUM")))
590 (error "Allowed values for this property have not been defined"))
591 (if (member key '("SCHEDULED" "DEADLINE" "CLOCKSUM"))
592 (setq nval (if previous 'earlier 'later))
593 (if previous (setq allowed (reverse allowed)))
596 (setq nval (nth nth allowed))
598 (error "There are only %d allowed values for property `%s'"
599 (length allowed) key)))
600 ((member value allowed)
601 (setq nval (or (car (cdr (member value allowed)))
603 (if (equal nval value)
604 (error "Only one allowed value for this property")))
605 (t (setq nval (car allowed)))))
607 ((equal major-mode 'org-agenda-mode)
608 (org-columns-eval '(org-entry-put pom key nval))
609 ;; The following let preserves the current format, and makes sure
610 ;; that in only a single file things need to be updated.
611 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
612 (buffer (marker-buffer pom))
613 (org-agenda-contributing-files
614 (list (with-current-buffer buffer
615 (buffer-file-name (buffer-base-buffer))))))
616 (org-agenda-columns)))
618 (let ((inhibit-read-only t))
619 (remove-text-properties (1- bol) eol '(read-only t))
622 (setq org-columns-overlays
623 (org-delete-all line-overlays org-columns-overlays))
624 (mapc 'delete-overlay line-overlays)
625 (org-columns-eval '(org-entry-put pom key nval)))
626 (org-columns-display-here)))
627 (org-move-to-column col)
628 (and (nth 3 (assoc-string key org-columns-current-fmt-compiled t))
629 (org-columns-update key))))))
631 (defun org-colview-construct-allowed-dates (s)
632 "Construct a list of three dates around the date in S.
633 This respects the format of the time stamp in S, active or non-active,
634 and also including time or not. S must be just a time stamp, no text
636 (when (and s (string-match (concat "^" org-ts-regexp3 "$") s))
637 (let* ((time (org-parse-time-string s 'nodefaults))
638 (active (equal (string-to-char s) ?<))
639 (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
640 time-before time-after)
641 (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
642 (setf (car time) (or (car time) 0))
643 (setf (nth 1 time) (or (nth 1 time) 0))
644 (setf (nth 2 time) (or (nth 2 time) 0))
645 (setq time-before (copy-sequence time))
646 (setq time-after (copy-sequence time))
647 (setf (nth 3 time-before) (1- (nth 3 time)))
648 (setf (nth 3 time-after) (1+ (nth 3 time)))
649 (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
650 (list time-before time time-after)))))
652 (defun org-verify-version (task)
655 (if (or (featurep 'xemacs)
656 (< emacs-major-version 22))
657 (error "Emacs 22 is required for the columns feature")))))
659 (defun org-columns-open-link (&optional arg)
661 (let ((value (get-char-property (point) 'org-columns-value)))
662 (org-open-link-from-string value arg)))
665 (defun org-columns-get-format-and-top-level ()
666 (let ((fmt (org-columns-get-format)))
667 (org-columns-goto-top-level)
670 (defun org-columns-get-format (&optional fmt-string)
672 (let (fmt-as-property fmt)
673 (when (condition-case nil (org-back-to-heading) (error nil))
674 (setq fmt-as-property (org-entry-get nil "COLUMNS" t)))
675 (setq fmt (or fmt-string fmt-as-property org-columns-default-format))
676 (setq-local org-columns-current-fmt fmt)
677 (org-columns-compile-format fmt)
680 (defun org-columns-goto-top-level ()
681 "Move to the beginning of the column view area.
682 Also sets `org-columns-top-level-marker' to the new position."
685 org-columns-top-level-marker
686 (cond ((org-before-first-heading-p) (point-min))
687 ((org-entry-get nil "COLUMNS" t) org-entry-property-inherited-from)
688 (t (org-back-to-heading) (point))))))
691 (defun org-columns (&optional columns-fmt-string)
692 "Turn on column view on an org-mode file.
693 When COLUMNS-FMT-STRING is non-nil, use it as the column format."
695 (org-verify-version 'columns)
696 (org-columns-remove-overlays)
697 (move-marker org-columns-begin-marker (point))
698 (org-columns-goto-top-level)
699 ;; Initialize `org-columns-current-fmt' and
700 ;; `org-columns-current-fmt-compiled'.
701 (let ((org-columns-time (time-to-number-of-days (current-time))))
702 (org-columns-get-format columns-fmt-string))
703 (unless org-columns-inhibit-recalculation (org-columns-compute-all))
708 (if (org-at-heading-p) (org-end-of-subtree t t) (point-max)))
709 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
711 (when (assoc "CLOCKSUM_T" org-columns-current-fmt-compiled)
712 (org-clock-sum-today))
713 (let* ((column-names (mapcar #'car org-columns-current-fmt-compiled))
719 (cons p (org-columns--value p (point))))
721 nil nil (and org-columns-skip-archived-trees 'archive))))
723 (setq-local org-columns-current-maxwidths
724 (org-columns-get-autowidth-alist
725 org-columns-current-fmt
727 (org-columns-display-here-title)
728 (when (setq-local org-columns-flyspell-was-active
729 (org-bound-and-true-p flyspell-mode))
731 (unless (local-variable-p 'org-colview-initial-truncate-line-value)
732 (setq-local org-colview-initial-truncate-line-value
734 (setq truncate-lines t)
737 (org-columns-display-here (cdr x))))))))
739 (defvar org-columns-compile-map
745 ("X/" checkbox-n-of-m +)
746 ("X%" checkbox-percent +)
747 ("max" max_numbers max)
748 ("min" min_numbers min)
750 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
751 (":max" max_times max)
752 (":min" min_times min)
754 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
755 ("@min" min_age min (lambda (x) (- org-columns-time x)))
756 ("@max" max_age max (lambda (x) (- org-columns-time x)))
758 (lambda (&rest x) (/ (apply '+ x) (float (length x))))
759 (lambda (x) (- org-columns-time x)))
760 ("est+" estimate org-estimate-combine))
761 "Operator <-> format,function,calc map.
762 Used to compile/uncompile columns format and completing read in
763 interactive function `org-columns-new'.
765 operator string used in #+COLUMNS definition describing the
767 format symbol describing summary type selected interactively in
768 `org-columns-new' and internally in
769 `org-columns-number-to-string' and
770 `org-columns-string-to-number'
771 function called with a list of values as argument to calculate
773 calc function called on every element before summarizing. This is
774 optional and should only be specified if needed")
776 (defun org-columns-new (&optional prop title width _op fmt fun &rest _rest)
777 "Insert a new column, to the left of the current column."
779 (let ((editp (and prop
780 (assoc-string prop org-columns-current-fmt-compiled t)))
782 (setq prop (org-icompleting-read
783 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
785 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
786 (setq width (read-string "Column width: " (if width (number-to-string width))))
787 (if (string-match "\\S-" width)
788 (setq width (string-to-number width))
790 (setq fmt (org-icompleting-read
792 (mapcar (lambda (x) (list (symbol-name (cadr x))))
793 org-columns-compile-map)
795 (setq fmt (intern fmt)
796 fun (cdr (assoc fmt (mapcar 'cdr org-columns-compile-map))))
797 (if (eq fmt 'none) (setq fmt nil))
801 (setcdr editp (list title width nil fmt nil fun)))
802 (setq cell (nthcdr (1- (current-column))
803 org-columns-current-fmt-compiled))
804 (setcdr cell (cons (list prop title width nil fmt nil
805 (car fun) (cadr fun))
807 (org-columns-store-format)
810 (defun org-columns-delete ()
811 "Delete the column at point from columns view."
813 (let* ((n (current-column))
814 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
816 (format "Are you sure you want to remove column \"%s\"? " title))
817 (setq org-columns-current-fmt-compiled
818 (delq (nth n org-columns-current-fmt-compiled)
819 org-columns-current-fmt-compiled))
820 (org-columns-store-format)
822 (if (>= (current-column) (length org-columns-current-fmt-compiled))
823 (backward-char 1)))))
825 (defun org-columns-edit-attributes ()
826 "Edit the attributes of the current column."
828 (let* ((n (current-column))
829 (info (nth n org-columns-current-fmt-compiled)))
830 (apply 'org-columns-new info)))
832 (defun org-columns-widen (arg)
833 "Make the column wider by ARG characters."
835 (let* ((n (current-column))
836 (entry (nth n org-columns-current-fmt-compiled))
837 (width (or (nth 2 entry)
838 (cdr (assoc-string (car entry)
839 org-columns-current-maxwidths
841 (setq width (max 1 (+ width arg)))
842 (setcar (nthcdr 2 entry) width)
843 (org-columns-store-format)
846 (defun org-columns-narrow (arg)
847 "Make the column narrower by ARG characters."
849 (org-columns-widen (- arg)))
851 (defun org-columns-move-right ()
852 "Swap this column with the one to the right."
854 (let* ((n (current-column))
855 (cell (nthcdr n org-columns-current-fmt-compiled))
857 (when (>= n (1- (length org-columns-current-fmt-compiled)))
858 (error "Cannot shift this column further to the right"))
860 (setcar cell (car (cdr cell)))
861 (setcdr cell (cons e (cdr (cdr cell))))
862 (org-columns-store-format)
866 (defun org-columns-move-left ()
867 "Swap this column with the one to the left."
869 (let* ((n (current-column)))
871 (error "Cannot shift this column further to the left"))
873 (org-columns-move-right)
876 (defun org-columns-store-format ()
877 "Store the text version of the current columns format in appropriate place.
878 This is either in the COLUMNS property of the node starting the current column
879 display, or in the #+COLUMNS line of the current buffer."
881 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
882 (setq-local org-columns-current-fmt fmt)
883 (if (marker-position org-columns-top-level-marker)
885 (goto-char org-columns-top-level-marker)
886 (if (and (org-at-heading-p)
887 (org-entry-get nil "COLUMNS"))
888 (org-entry-put nil "COLUMNS" fmt)
889 (goto-char (point-min))
890 ;; Overwrite all #+COLUMNS lines....
891 (while (re-search-forward "^[ \t]*#\\+COLUMNS:.*" nil t)
893 (replace-match (concat "#+COLUMNS: " fmt) t t))
895 (goto-char (point-min))
896 (or (org-at-heading-p t) (outline-next-heading))
897 (let ((inhibit-read-only t))
898 (insert-before-markers "#+COLUMNS: " fmt "\n")))
899 (setq-local org-columns-default-format fmt))))))
901 (defun org-columns-get-autowidth-alist (s cache)
902 "Derive the maximum column widths from the format and the cache."
904 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
905 (push (cons (match-string 1 s) 1) rtn)
906 (setq start (match-end 0)))
910 (let ((prop (car x)))
913 (length (or (cdr (assoc-string prop (cdr y) t))
919 (defun org-columns-compute-all ()
920 "Compute all columns that have operators defined."
921 (org-with-silent-modifications
922 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
923 (let ((columns org-columns-current-fmt-compiled)
924 (org-columns-time (time-to-number-of-days (current-time)))
926 (while (setq col (pop columns))
929 (org-columns-compute (car col)))))))
931 (defun org-columns-update (property)
932 "Recompute PROPERTY, and update the columns display for it."
933 (org-columns-compute property)
937 (when (equal (overlay-get ov 'org-columns-key) property)
938 (setq pos (overlay-start ov))
940 (when (setq val (cdr (assoc-string
943 (point-at-bol) 'org-summaries)
945 (setq fmt (overlay-get ov 'org-columns-format))
946 (overlay-put ov 'org-columns-value val)
947 (overlay-put ov 'display (format fmt val)))))
948 org-columns-overlays))))
950 (defvar org-inlinetask-min-level
951 (if (featurep 'org-inlinetask) org-inlinetask-min-level 15))
954 (defun org-columns-compute (property)
955 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
957 (let* ((re org-outline-regexp-bol)
958 (lmax 30) ; Does anyone use deeper levels???
959 (lvals (make-vector lmax nil))
960 (lflag (make-vector lmax nil))
962 (ass (assoc-string property org-columns-current-fmt-compiled t))
966 (calc (or (nth 7 ass) 'identity))
967 (beg org-columns-top-level-marker)
968 (inminlevel org-inlinetask-min-level)
969 (last-level org-inlinetask-min-level)
970 val valflag flag end sumpos sum-alist sum str str1 useval)
972 ;; Find the region to compute
974 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
976 ;; Walk the tree from the back and do the computations
977 (while (re-search-backward re beg t)
978 (setq sumpos (match-beginning 0)
979 last-level (if (not (or (zerop level) (eq level inminlevel)))
981 level (org-outline-level)
982 val (org-entry-get nil property)
983 valflag (and val (string-match "\\S-" val)))
985 ((< level last-level)
986 ;; Put the sum of lower levels here as a property. If
987 ;; values are estimate, use an appropriate sum function.
989 (if (eq fun 'org-estimate-combine) #'org-estimate-combine
991 (if (and (/= last-level inminlevel)
992 (aref lvals last-level))
993 (apply fun (aref lvals last-level)) 0)
994 (if (aref lvals inminlevel)
995 (apply fun (aref lvals inminlevel)) 0))
996 flag (or (aref lflag last-level) ; any valid entries from children?
997 (aref lflag inminlevel)) ; or inline tasks?
998 str (org-columns-number-to-string sum format printf)
999 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
1000 useval (if flag str1 (if valflag val ""))
1001 sum-alist (get-text-property sumpos 'org-summaries))
1002 (let ((old (assoc-string property sum-alist t)))
1003 (if old (setcdr old useval)
1004 (push (cons property useval) sum-alist)
1005 (org-with-silent-modifications
1006 (add-text-properties sumpos (1+ sumpos)
1007 (list 'org-summaries sum-alist)))))
1008 (when (and val (not (equal val (if flag str val))))
1009 (org-entry-put nil property (if flag str val)))
1010 ;; add current to current level accumulator
1011 (when (or flag valflag)
1014 (funcall calc (org-columns-string-to-number
1015 (if flag str val) format)))
1017 (aset lflag level t))
1018 ;; clear accumulators for deeper levels
1019 (loop for l from (1+ level) to (1- lmax) do
1021 (aset lflag l nil)))
1022 ((>= level last-level)
1023 ;; add what we have here to the accumulator for this level
1025 (push (funcall calc (org-columns-string-to-number val format))
1027 (aset lflag level t)))
1028 (t (error "This should not happen")))))))
1030 (defun org-columns-redo ()
1031 "Construct the column display again."
1033 (message "Recomputing columns...")
1034 (let ((line (org-current-line))
1035 (col (current-column)))
1037 (if (marker-position org-columns-begin-marker)
1038 (goto-char org-columns-begin-marker))
1039 (org-columns-remove-overlays)
1040 (if (derived-mode-p 'org-mode)
1041 (call-interactively 'org-columns)
1043 (call-interactively 'org-agenda-columns)))
1044 (org-goto-line line)
1045 (move-to-column col))
1046 (message "Recomputing columns...done"))
1048 (defun org-columns-not-in-agenda ()
1049 (if (eq major-mode 'org-agenda-mode)
1050 (error "This command is only allowed in Org-mode buffers")))
1052 (defun org-string-to-number (s)
1053 "Convert string to number, and interpret hh:mm:ss."
1054 (if (not (string-match ":" s))
1055 (string-to-number s)
1056 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1058 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1062 (defun org-columns-number-to-string (n fmt &optional printf)
1063 "Convert a computed column number to a string value, according to FMT."
1065 ((memq fmt '(estimate)) (org-estimate-print n printf))
1066 ((not (numberp n)) "")
1067 ((memq fmt '(add_times max_times min_times mean_times))
1068 (org-hours-to-clocksum-string n))
1070 (cond ((= n (floor n)) "[X]")
1073 ((memq fmt '(checkbox-n-of-m checkbox-percent))
1074 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
1075 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
1076 (printf (format printf n))
1079 ((memq fmt '(min_age max_age mean_age))
1080 (org-format-time-period n))
1081 (t (number-to-string n))))
1083 (defun org-nofm-to-completion (n m &optional percent)
1085 (format "[%d/%d]" n m)
1086 (format "[%d%%]" (round (* 100.0 n) m))))
1089 (defun org-columns-string-to-number (s fmt)
1090 "Convert a column value to a number that can be used for column computing."
1093 ((memq fmt '(min_age max_age mean_age))
1094 (cond ((string= s "") org-columns-time)
1096 "\\([0-9]+\\)d \\([0-9]+\\)h \\([0-9]+\\)m \\([0-9]+\\)s"
1098 (+ (* 60 (+ (* 60 (+ (* 24 (string-to-number (match-string 1 s)))
1099 (string-to-number (match-string 2 s))))
1100 (string-to-number (match-string 3 s))))
1101 (string-to-number (match-string 4 s))))
1102 (t (time-to-number-of-days (apply 'encode-time
1103 (org-parse-time-string s t))))))
1104 ((string-match ":" s)
1105 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1107 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1109 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
1110 (if (equal s "[X]") 1. 0.000001))
1111 ((memq fmt '(estimate)) (org-string-to-estimate s))
1112 ((string-match (concat "\\([0-9.]+\\) *\\("
1113 (regexp-opt (mapcar 'car org-effort-durations))
1115 (setq s (concat "0:" (org-duration-string-to-minutes s t)))
1116 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1118 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1120 (t (string-to-number s)))))
1122 (defun org-columns-uncompile-format (cfmt)
1123 "Turn the compiled columns format back into a string representation."
1124 (let ((rtn "") e s prop title op width fmt printf ee map)
1125 (while (setq e (pop cfmt))
1132 (setq map (copy-sequence org-columns-compile-map))
1133 (while (setq ee (pop map))
1134 (if (equal fmt (nth 1 ee))
1135 (setq op (car ee) map nil)))
1136 (if (and op printf) (setq op (concat op ";" printf)))
1137 (if (equal title prop) (setq title nil))
1138 (setq s (concat "%" (if width (number-to-string width))
1140 (if title (concat "(" title ")"))
1141 (if op (concat "{" op "}"))))
1142 (setq rtn (concat rtn " " s)))
1145 (defun org-columns-compile-format (fmt)
1146 "Turn a column format string FMT into an alist of specifications.
1148 The alist has one entry for each column in the format. The elements of
1150 property the property
1151 title the title field for the columns
1152 width the column width in characters, can be nil for automatic
1153 operator the operator if any
1154 format the output format for computed results, derived from operator
1155 printf a printf format for computed values
1156 fun the lisp function to compute summary values, derived from operator
1157 calc function to get values from base elements
1159 This function updates `org-columns-current-fmt-compiled'."
1160 (let ((start 0) width prop title op op-match f printf fun calc)
1161 (setq org-columns-current-fmt-compiled nil)
1162 (while (string-match
1163 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
1165 (setq start (match-end 0)
1166 width (match-string 1 fmt)
1167 prop (match-string 2 fmt)
1168 title (or (match-string 3 fmt) prop)
1169 op (match-string 4 fmt)
1174 (if width (setq width (string-to-number width)))
1175 (when (and op (string-match ";" op))
1176 (setq printf (substring op (match-end 0))
1177 op (substring op 0 (match-beginning 0))))
1178 (when (setq op-match (assoc op org-columns-compile-map))
1179 (setq f (cadr op-match)
1180 fun (caddr op-match)
1181 calc (cadddr op-match)))
1182 (push (list prop title width op f printf fun calc)
1183 org-columns-current-fmt-compiled))
1184 (setq org-columns-current-fmt-compiled
1185 (nreverse org-columns-current-fmt-compiled))))
1188 ;;; Dynamic block for Column view
1190 (defvar org-heading-regexp) ; defined in org.el
1191 (defvar org-heading-keyword-regexp-format) ; defined in org.el
1192 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
1193 "Get the column view of the current buffer or subtree.
1194 The first optional argument MAXLEVEL sets the level limit. A
1195 second optional argument SKIP-EMPTY-ROWS tells whether to skip
1196 empty rows, an empty row being one where all the column view
1197 specifiers except ITEM are empty. This function returns a list
1198 containing the title row and all other rows. Each row is a list
1201 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1202 (re-archive (concat ".*:" org-archive-tag ":"))
1203 (n (length title)) row tbl)
1204 (goto-char (point-min))
1205 (while (re-search-forward org-heading-regexp nil t)
1207 (when (and (or (null maxlevel)
1209 (if org-odd-levels-only
1210 (/ (1+ (length (match-string 1))) 2)
1211 (length (match-string 1)))))
1212 (get-char-property (match-beginning 0) 'org-columns-key))
1213 (when (or (org-in-commented-heading-p t)
1216 (looking-at re-archive)))
1217 (org-end-of-subtree t)
1220 (loop for i from 0 to (1- n) do
1223 (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
1224 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
1227 (setq row (nreverse row))
1228 (unless (and skip-empty-rows
1229 (eq 1 (length (delete "" (delete-dups (copy-sequence row))))))
1231 (append (list title 'hline) (nreverse tbl)))))
1234 (defun org-dblock-write:columnview (params)
1235 "Write the column view table.
1236 PARAMS is a property list of parameters:
1238 :width enforce same column widths with <N> specifiers.
1239 :id the :ID: property of the entry where the columns view
1240 should be built. When the symbol `local', call locally.
1241 When `global' call column view with the cursor at the beginning
1242 of the buffer (usually this means that the whole buffer switches
1243 to column view). When \"file:path/to/file.org\", invoke column
1244 view at the start of that file. Otherwise, the ID is located
1245 using `org-id-find'.
1246 :hlines When t, insert a hline before each item. When a number, insert
1247 a hline before each level <= that number.
1248 :vlines When t, make each column a colgroup to enforce vertical lines.
1249 :maxlevel When set to a number, don't capture headlines below this level.
1251 When t, skip rows where all specifiers other than ITEM are empty.
1252 :format When non-nil, specify the column view format to use."
1253 (let ((pos (point-marker))
1254 (hlines (plist-get params :hlines))
1255 (vlines (plist-get params :vlines))
1256 (maxlevel (plist-get params :maxlevel))
1257 (content-lines (org-split-string (plist-get params :content) "\n"))
1258 (skip-empty-rows (plist-get params :skip-empty-rows))
1259 (columns-fmt (plist-get params :format))
1260 (case-fold-search t)
1261 tbl id idpos nfields tmp recalc line
1262 id-as-string view-file view-pos)
1263 (when (setq id (plist-get params :id))
1264 (setq id-as-string (cond ((numberp id) (number-to-string id))
1265 ((symbolp id) (symbol-name id))
1268 (cond ((not id) nil)
1269 ((eq id 'global) (setq view-pos (point-min)))
1271 ((string-match "^file:\\(.*\\)" id-as-string)
1272 (setq view-file (match-string 1 id-as-string)
1274 (unless (file-exists-p view-file)
1275 (error "No such file: \"%s\"" id-as-string)))
1276 ((setq idpos (org-find-entry-with-id id))
1277 (setq view-pos idpos))
1278 ((setq idpos (org-id-find id))
1279 (setq view-file (car idpos))
1280 (setq view-pos (cdr idpos)))
1281 (t (error "Cannot find entry with :ID: %s" id))))
1282 (with-current-buffer (if view-file
1283 (get-file-buffer view-file)
1288 (goto-char (or view-pos (point)))
1289 (org-columns columns-fmt)
1290 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1291 (setq nfields (length (car tbl)))
1292 (org-columns-quit))))
1294 (move-marker pos nil)
1296 (when (plist-get params :hlines)
1299 (if (eq (car tbl) 'hline)
1300 (push (pop tbl) tmp)
1301 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1302 (if (and (not (eq (car tmp) 'hline))
1304 (and (numberp hlines)
1305 (<= (- (match-end 1) (match-beginning 1))
1308 (push (pop tbl) tmp)))
1309 (setq tbl (nreverse tmp)))
1311 (setq tbl (mapcar (lambda (x)
1312 (if (eq 'hline x) x (cons "" x)))
1314 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1316 (while (string-match "^#" (car content-lines))
1317 (insert (pop content-lines) "\n")))
1319 (insert (org-listtable-to-string tbl))
1320 (when (plist-get params :width)
1321 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1322 org-columns-current-widths "|")))
1323 (while (setq line (pop content-lines))
1324 (when (string-match "^#" line)
1326 (when (string-match "^[ \t]*#\\+tblfm" line)
1329 (progn (goto-char pos) (org-table-recalculate 'all))
1331 (org-table-align)))))
1333 (defun org-listtable-to-string (tbl)
1334 "Convert a listtable TBL to a string that contains the Org-mode table.
1335 The table still need to be aligned. The resulting string has no leading
1336 and tailing newline characters."
1341 (concat "|" (mapconcat 'identity x "|") "|"))
1342 ((eq x 'hline) "|-|")
1343 (t (error "Garbage in listtable: %s" x))))
1347 (defun org-insert-columns-dblock ()
1348 "Create a dynamic block capturing a column view table."
1350 (let ((defaults '(:name "columnview" :hlines 1))
1351 (id (org-icompleting-read
1352 "Capture columns (local, global, entry with :ID: property) [local]: "
1353 (append '(("global") ("local"))
1354 (mapcar 'list (org-property-values "ID"))))))
1355 (if (equal id "") (setq id 'local))
1356 (if (equal id "global") (setq id 'global))
1357 (setq defaults (append defaults (list :id id)))
1358 (org-create-dblock defaults)
1359 (org-update-dblock)))
1361 ;;; Column view in the agenda
1363 (defvar org-agenda-view-columns-initially nil
1364 "When set, switch to columns view immediately after creating the agenda.")
1366 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1367 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1368 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1371 (defun org-agenda-columns ()
1372 "Turn on or update column view in the agenda."
1374 (org-verify-version 'columns)
1375 (org-columns-remove-overlays)
1376 (move-marker org-columns-begin-marker (point))
1377 (let ((org-columns-time (time-to-number-of-days (current-time)))
1380 ((org-bound-and-true-p org-agenda-overriding-columns-format))
1381 ((let ((m (org-get-at-bol 'org-hd-marker)))
1383 (or (org-entry-get m "COLUMNS" t)
1384 (with-current-buffer (marker-buffer m)
1385 org-columns-default-format)))))
1386 ((and (local-variable-p 'org-columns-current-fmt)
1387 org-columns-current-fmt))
1388 ((let ((m (next-single-property-change (point-min) 'org-hd-marker)))
1390 (let ((m (get-text-property m 'org-hd-marker)))
1391 (or (org-entry-get m "COLUMNS" t)
1392 (with-current-buffer (marker-buffer m)
1393 org-columns-default-format))))))
1394 (t org-columns-default-format))))
1395 (setq-local org-columns-current-fmt fmt)
1396 (org-columns-compile-format fmt)
1397 (when org-agenda-columns-compute-summary-properties
1398 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1400 ;; Collect properties for each headline in current view.
1401 (goto-char (point-min))
1403 (let ((names (mapcar #'car org-columns-current-fmt-compiled)) m)
1405 (when (setq m (or (org-get-at-bol 'org-hd-marker)
1406 (org-get-at-bol 'org-marker)))
1409 (line-beginning-position)
1410 (org-with-point-at m
1413 (let ((value (org-columns--value name (point))))
1416 (if (and org-agenda-columns-add-appointments-to-effort-sum
1418 (eq (compare-strings name nil nil
1419 org-effort-property nil nil
1422 ;; Effort property is not defined. Try
1423 ;; to use appointment duration.
1424 (get-text-property (point) 'duration))
1426 (org-minutes-to-clocksum-string
1427 (get-text-property (point) 'duration))
1434 (setq-local org-columns-current-maxwidths
1435 (org-columns-get-autowidth-alist fmt cache))
1436 (org-columns-display-here-title)
1437 (when (setq-local org-columns-flyspell-was-active
1438 (org-bound-and-true-p flyspell-mode))
1442 (org-columns-display-here (cdr x)))
1443 (when org-agenda-columns-show-summaries
1444 (org-agenda-colview-summarize cache)))))))
1446 (defun org-agenda-colview-summarize (cache)
1447 "Summarize the summarizable columns in column view in the agenda.
1448 This will add overlays to the date lines, to show the summary for each day."
1449 (let* ((fmt (mapcar (lambda (x)
1450 (if (string-match "CLOCKSUM.*" (car x))
1451 (list (match-string 0 (car x))
1452 (nth 1 x) (nth 2 x) ":" 'add_times
1455 org-columns-current-fmt-compiled))
1456 line c c1 stype calc sumfunc props lsum entries prop v)
1458 (when (delq nil (mapcar 'cadr fmt))
1459 ;; OK, at least one summation column, it makes sense to try this
1460 (goto-char (point-max))
1462 (when (or (get-text-property (point) 'org-date-line)
1463 (eq (get-text-property (point) 'face)
1464 'org-agenda-structure))
1465 ;; OK, this is a date line that should be used
1466 (setq line (org-current-line))
1467 (setq entries nil c cache cache nil)
1468 (while (setq c1 (pop c))
1469 (if (> (car c1) line)
1472 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1473 ;; Compute the summaries for the properties we want,
1474 ;; set nil properties for the rest.
1475 (when (setq entries (mapcar 'cdr entries))
1482 calc (or (nth 7 f) 'identity))
1484 ((equal prop "ITEM")
1485 (cons prop (buffer-substring (point-at-bol)
1487 ((not stype) (cons prop ""))
1488 (t ;; do the summary
1491 (setq v (cdr (assoc-string prop x t)))
1495 (if (not (get-text-property 0 'org-computed v))
1498 (org-columns-string-to-number
1501 (setq lsum (remove nil lsum))
1503 (cond ((> (length lsum) 1)
1504 (org-columns-number-to-string
1505 (apply sumfunc lsum) stype))
1506 ((eq (length lsum) 1)
1507 (org-columns-number-to-string
1510 (put-text-property 0 (length lsum) 'face 'bold lsum)
1511 (unless (eq calc 'identity)
1512 (put-text-property 0 (length lsum) 'org-computed t lsum))
1515 (org-columns-display-here props 'dateline)
1516 (setq-local org-agenda-columns-active t)))
1517 (if (bobp) (throw 'exit t))
1518 (beginning-of-line 0))))))
1520 (defun org-agenda-colview-compute (fmt)
1521 "Compute the relevant columns in the contributing source buffers."
1522 (let ((files org-agenda-contributing-files)
1523 (org-columns-begin-marker (make-marker))
1524 (org-columns-top-level-marker (make-marker))
1526 (while (setq f (pop files))
1527 (setq b (find-buffer-visiting f))
1528 (with-current-buffer (or (buffer-base-buffer b) b)
1532 (org-with-silent-modifications
1533 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
1534 (goto-char (point-min))
1535 (org-columns-get-format-and-top-level)
1536 (while (setq fm (pop fmt))
1537 (cond ((equal (car fm) "CLOCKSUM")
1539 ((equal (car fm) "CLOCKSUM_T")
1540 (org-clock-sum-today))
1542 (setq a (assoc-string (car fm)
1543 org-columns-current-fmt-compiled
1545 (equal (nth 4 a) (nth 4 fm)))
1546 (org-columns-compute (car fm)))))))))))
1548 (defun org-format-time-period (interval)
1549 "Convert time in fractional days to days/hours/minutes/seconds."
1550 (if (numberp interval)
1551 (let* ((days (floor interval))
1552 (frac-hours (* 24 (- interval days)))
1553 (hours (floor frac-hours))
1554 (minutes (floor (* 60 (- frac-hours hours))))
1555 (seconds (floor (* 60 (- (* 60 (- frac-hours hours)) minutes)))))
1556 (format "%dd %02dh %02dm %02ds" days hours minutes seconds))
1559 (defun org-estimate-mean-and-var (v)
1560 "Return the mean and variance of an estimate."
1561 (let* ((v (cond ((consp v) v)
1562 ((numberp v) (list v v))
1563 (t (error "Invalid estimate type"))))
1564 (low (float (car v)))
1565 (high (float (cadr v)))
1566 (mean (/ (+ low high) 2.0))
1567 (var (/ (+ (expt (- mean low) 2.0) (expt (- high mean) 2.0)) 2.0)))
1570 (defun org-estimate-combine (&rest el)
1571 "Combine a list of estimates, using mean and variance.
1572 The mean and variance of the result will be the sum of the means
1573 and variances (respectively) of the individual estimates."
1577 (let ((stats (org-estimate-mean-and-var e)))
1578 (setq mean (+ mean (car stats)))
1579 (setq var (+ var (cadr stats)))))
1581 (let ((stdev (sqrt var)))
1582 (list (- mean stdev) (+ mean stdev)))))
1584 (defun org-estimate-print (e &optional fmt)
1585 "Prepare a string representation of an estimate.
1586 This formats these numbers as two numbers with a \"-\" between them."
1587 (let ((fmt (or fmt "%.0f"))
1588 (e (cond ((consp e) e)
1589 ((numberp e) (list e e))
1590 (t (error "Invalid estimate type")))))
1591 (format "%s" (mapconcat (lambda (n) (format fmt n)) e "-"))))
1593 (defun org-string-to-estimate (s)
1594 "Convert a string to an estimate.
1595 The string should be two numbers joined with a \"-\"."
1596 (if (string-match "\\(.*\\)-\\(.*\\)" s)
1597 (list (string-to-number (match-string 1 s))
1598 (string-to-number(match-string 2 s)))
1599 (list (string-to-number s) (string-to-number s))))
1601 (provide 'org-colview)
1603 ;;; org-colview.el ends here