1 ;;; org-colview.el --- Column View in Org-mode
3 ;; Copyright (C) 2004-2014 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-current-fmt nil
47 "Local variable, holds the currently active column format.")
48 (make-variable-buffer-local 'org-columns-current-fmt
)
49 (defvar org-columns-current-fmt-compiled nil
50 "Local variable, holds the currently active column format.
51 This is the compiled version of the format.")
52 (make-variable-buffer-local 'org-columns-current-fmt-compiled
)
53 (defvar org-columns-current-widths nil
54 "Loval variable, holds the currently widths of fields.")
55 (make-variable-buffer-local 'org-columns-current-widths
)
56 (defvar org-columns-current-maxwidths nil
57 "Loval variable, holds the currently active maximum column widths.")
58 (make-variable-buffer-local 'org-columns-current-maxwidths
)
59 (defvar org-columns-begin-marker
(make-marker)
60 "Points to the position where last a column creation command was called.")
61 (defvar org-columns-top-level-marker
(make-marker)
62 "Points to the position where current columns region starts.")
64 (defvar org-columns-map
(make-sparse-keymap)
65 "The keymap valid in column display.")
67 (defun org-columns-content ()
68 "Switch to contents view while in columns view."
73 (org-defkey org-columns-map
"c" 'org-columns-content
)
74 (org-defkey org-columns-map
"o" 'org-overview
)
75 (org-defkey org-columns-map
"e" 'org-columns-edit-value
)
76 (org-defkey org-columns-map
"\C-c\C-t" 'org-columns-todo
)
77 (org-defkey org-columns-map
"\C-c\C-c" 'org-columns-set-tags-or-toggle
)
78 (org-defkey org-columns-map
"\C-c\C-o" 'org-columns-open-link
)
79 (org-defkey org-columns-map
"v" 'org-columns-show-value
)
80 (org-defkey org-columns-map
"q" 'org-columns-quit
)
81 (org-defkey org-columns-map
"r" 'org-columns-redo
)
82 (org-defkey org-columns-map
"g" 'org-columns-redo
)
83 (org-defkey org-columns-map
[left] 'backward-char)
84 (org-defkey org-columns-map "\M-b" 'backward-char)
85 (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
86 (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
87 (org-defkey org-columns-map "\M-f"
88 (lambda () (interactive) (goto-char (1+ (point)))))
89 (org-defkey org-columns-map [right]
90 (lambda () (interactive) (goto-char (1+ (point)))))
91 (org-defkey org-columns-map [down]
92 (lambda () (interactive)
93 (let ((col (current-column)))
95 (while (and (org-invisible-p2) (not (eobp)))
96 (beginning-of-line 2))
98 (if (eq major-mode 'org-agenda-mode)
99 (org-agenda-do-context-action)))))
100 (org-defkey org-columns-map [up]
101 (lambda () (interactive)
102 (let ((col (current-column)))
103 (beginning-of-line 0)
104 (while (and (org-invisible-p2) (not (bobp)))
105 (beginning-of-line 0))
107 (if (eq major-mode 'org-agenda-mode)
108 (org-agenda-do-context-action)))))
109 (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
110 (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
111 (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
112 (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
113 (org-defkey org-columns-map "<" 'org-columns-narrow)
114 (org-defkey org-columns-map ">" 'org-columns-widen)
115 (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
116 (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
117 (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
118 (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
120 (org-defkey org-columns-map (number-to-string i)
121 `(lambda () (interactive)
122 (org-columns-next-allowed-value nil ,i))))
124 (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
126 ["Edit property" org-columns-edit-value t]
127 ["Next allowed value" org-columns-next-allowed-value t]
128 ["Previous allowed value" org-columns-previous-allowed-value t]
129 ["Show full value" org-columns-show-value t]
130 ["Edit allowed values" org-columns-edit-allowed t]
132 ["Edit column attributes" org-columns-edit-attributes t]
133 ["Increase column width" org-columns-widen t]
134 ["Decrease column width" org-columns-narrow t]
136 ["Move column right" org-columns-move-right t]
137 ["Move column left" org-columns-move-left t]
138 ["Add column" org-columns-new t]
139 ["Delete column" org-columns-delete t]
141 ["CONTENTS" org-columns-content t]
142 ["OVERVIEW" org-overview t]
143 ["Refresh columns display" org-columns-redo t]
145 ["Open link" org-columns-open-link t]
147 ["Quit" org-columns-quit t]))
149 (defun org-columns-new-overlay (beg end &optional string face)
150 "Create a new column overlay and add it to the list."
151 (let ((ov (make-overlay beg end)))
152 (overlay-put ov 'face (or face 'secondary-selection))
153 (remove-text-properties 0 (length string) '(face nil) string)
154 (org-overlay-display ov string face)
155 (push ov org-columns-overlays)
158 (defun org-columns-display-here (&optional props dateline)
159 "Overlay the current line with column display."
161 (let* ((fmt org-columns-current-fmt-compiled)
163 (level-face (save-excursion
164 (beginning-of-line 1)
165 (and (looking-at "\\(\\**\\)\\(\\* \\)")
166 (org-get-level-face 2))))
167 (ref-face (or level-face
168 (and (eq major-mode 'org-agenda-mode)
169 (get-text-property (point-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 (cphr (get-text-property (point-at-bol) 'org-complex-heading-regexp))
177 pom property ass width f fc string fm ov column val modval s2 title calc)
178 ;; Check if the entry is in another buffer.
180 (if (eq major-mode 'org-agenda-mode)
181 (setq pom (or (org-get-at-bol 'org-hd-marker)
182 (org-get-at-bol 'org-marker))
183 props (if pom (org-entry-properties pom) nil))
184 (setq props (org-entry-properties nil))))
186 (while (setq column (pop fmt))
187 (setq property (car column)
189 ass (assoc-string property props t)
191 (assoc-string property org-columns-current-maxwidths t))
194 f (format "%%-%d.%ds | " width width)
198 val (or (cdr ass) "")
199 modval (cond ((and org-columns-modify-value-for-display-function
201 org-columns-modify-value-for-display-function))
202 (funcall org-columns-modify-value-for-display-function
204 ((equal property "ITEM")
205 (org-columns-compact-links val))
206 (fc (org-columns-number-to-string
207 (org-columns-string-to-number val fm) fm fc))
208 ((and calc (functionp calc)
209 (not (string= val ""))
210 (not (get-text-property 0 'org-computed val)))
211 (org-columns-number-to-string
212 (funcall calc (org-columns-string-to-number
214 (setq s2 (org-columns-add-ellipses (or modval val) width))
215 (setq string (format f s2))
216 ;; Create the overlay
217 (org-with-silent-modifications
218 (setq ov (org-columns-new-overlay
219 beg (setq beg (1+ beg)) string (if dateline face1 face)))
220 (overlay-put ov 'keymap org-columns-map)
221 (overlay-put ov 'org-columns-key property)
222 (overlay-put ov 'org-columns-value (cdr ass))
223 (overlay-put ov 'org-columns-value-modified modval)
224 (overlay-put ov 'org-columns-pom pom)
225 (overlay-put ov 'org-columns-format f)
226 (overlay-put ov 'line-prefix "")
227 (overlay-put ov 'wrap-prefix ""))
228 (if (or (not (char-after beg))
229 (equal (char-after beg) ?\n))
230 (let ((inhibit-read-only t))
233 (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
234 ;; Make the rest of the line disappear.
236 (setq ov (org-columns-new-overlay beg (point-at-eol)))
237 (overlay-put ov 'invisible t)
238 (overlay-put ov 'keymap org-columns-map)
239 (overlay-put ov 'line-prefix "")
240 (overlay-put ov 'wrap-prefix "")
241 (push ov org-columns-overlays)
242 (setq ov (make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
243 (overlay-put ov 'keymap org-columns-map)
244 (push ov org-columns-overlays)
245 (let ((inhibit-read-only t))
246 (put-text-property (max (point-min) (1- (point-at-bol)))
247 (min (point-max) (1+ (point-at-eol)))
248 'read-only "Type `e' to edit property")))))
250 (defun org-columns-add-ellipses (string width)
251 "Truncate STRING with WIDTH characters, with ellipses."
253 ((<= (length string) width) string)
254 ((<= width (length org-columns-ellipses))
255 (substring org-columns-ellipses 0 width))
256 (t (concat (substring string 0 (- width (length org-columns-ellipses)))
257 org-columns-ellipses))))
259 (defvar org-columns-full-header-line-format nil
260 "The full header line format, will be shifted by horizontal scrolling." )
261 (defvar org-previous-header-line-format nil
262 "The header line format before column view was turned on.")
263 (defvar org-columns-inhibit-recalculation nil
264 "Inhibit recomputing of columns on column view startup.")
265 (defvar org-columns-flyspell-was-active nil
266 "Remember the state of `flyspell-mode' before column view.
267 Flyspell-mode can cause problems in columns view, so it is turned off
268 for the duration of the command.")
270 (defvar header-line-format)
271 (defvar org-columns-previous-hscroll 0)
273 (defun org-columns-display-here-title ()
274 "Overlay the newline before the current line with the table title."
276 (let ((fmt org-columns-current-fmt-compiled)
278 property width f column str widths)
279 (while (setq column (pop fmt))
280 (setq property (car column)
281 str (or (nth 1 column) property)
282 width (or (cdr (assoc-string property
283 org-columns-current-maxwidths
287 widths (push width widths)
288 f (format "%%-%d.%ds | " width width)
289 string (format f str)
290 title (concat title string)))
292 (org-add-props " " nil 'display '(space :align-to 0))
293 ;;(org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
294 (org-add-props title nil 'face 'org-column-title)))
295 (org-set-local 'org-previous-header-line-format header-line-format)
296 (org-set-local 'org-columns-current-widths (nreverse widths))
297 (setq org-columns-full-header-line-format title)
298 (setq org-columns-previous-hscroll -1)
299 ; (org-columns-hscoll-title)
300 (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
302 (defun org-columns-hscoll-title ()
303 "Set the `header-line-format' so that it scrolls along with the table."
304 (sit-for .0001) ; need to force a redisplay to update window-hscroll
305 (when (not (= (window-hscroll) org-columns-previous-hscroll))
306 (setq header-line-format
307 (concat (substring org-columns-full-header-line-format 0 1)
308 (substring org-columns-full-header-line-format
309 (1+ (window-hscroll))))
310 org-columns-previous-hscroll (window-hscroll))
311 (force-mode-line-update)))
313 (defvar org-colview-initial-truncate-line-value nil
314 "Remember the value of `truncate-lines' across colview.")
317 (defun org-columns-remove-overlays ()
318 "Remove all currently active column overlays."
320 (when (marker-buffer org-columns-begin-marker)
321 (with-current-buffer (marker-buffer org-columns-begin-marker)
322 (when (local-variable-p 'org-previous-header-line-format)
323 (setq header-line-format org-previous-header-line-format)
324 (kill-local-variable 'org-previous-header-line-format)
325 (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
326 (move-marker org-columns-begin-marker nil)
327 (move-marker org-columns-top-level-marker nil)
328 (org-with-silent-modifications
329 (mapc 'delete-overlay org-columns-overlays)
330 (setq org-columns-overlays nil)
331 (let ((inhibit-read-only t))
332 (remove-text-properties (point-min) (point-max) '(read-only t))))
333 (when org-columns-flyspell-was-active
335 (when (local-variable-p 'org-colview-initial-truncate-line-value)
336 (setq truncate-lines org-colview-initial-truncate-line-value)))))
338 (defun org-columns-compact-links (s)
339 "Replace [[link][desc]] with [desc] or [link]."
340 (while (string-match org-bracket-link-regexp s)
341 (setq s (replace-match
342 (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
346 (defun org-columns-show-value ()
347 "Show the full value of the property."
349 (let ((value (get-char-property (point) 'org-columns-value)))
350 (message "Value is: %s" (or value ""))))
352 (defvar org-agenda-columns-active) ;; defined in org-agenda.el
354 (defun org-columns-quit ()
355 "Remove the column overlays and in this way exit column editing."
357 (org-with-silent-modifications
358 (org-columns-remove-overlays)
359 (let ((inhibit-read-only t))
360 (remove-text-properties (point-min) (point-max) '(read-only t))))
361 (when (eq major-mode 'org-agenda-mode)
362 (setq org-agenda-columns-active nil)
364 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
366 (defun org-columns-check-computed ()
367 "Check if this column value is computed.
368 If yes, throw an error indicating that changing it does not make sense."
369 (let ((val (get-char-property (point) 'org-columns-value)))
370 (when (and (stringp val)
371 (get-char-property 0 'org-computed val))
372 (error "This value is computed from the entry's children"))))
374 (defun org-columns-todo (&optional arg)
375 "Change the TODO state during column view."
377 (org-columns-edit-value "TODO"))
379 (defun org-columns-set-tags-or-toggle (&optional arg)
380 "Toggle checkbox at point, or set tags for current headline."
382 (if (string-match "\\`\\[[ xX-]\\]\\'"
383 (get-char-property (point) 'org-columns-value))
384 (org-columns-next-allowed-value)
385 (org-columns-edit-value "TAGS")))
387 (defvar org-agenda-overriding-columns-format nil
388 "When set, overrides any other format definition for the agenda.
389 Don't set this, this is meant for dynamic scoping.")
391 (defun org-columns-edit-value (&optional key)
392 "Edit the value of the property at point in column view.
393 Where possible, use the standard interface for changing this line."
395 (org-columns-check-computed)
396 (let* ((col (current-column))
397 (key (or key (get-char-property (point) 'org-columns-key)))
398 (value (get-char-property (point) 'org-columns-value))
399 (bol (point-at-bol)) (eol (point-at-eol))
400 (pom (or (get-text-property bol 'org-hd-marker)
401 (point))) ; keep despite of compiler waring
403 (delq nil (mapcar (lambda (x)
404 (and (eq (overlay-buffer x) (current-buffer))
405 (>= (overlay-start x) bol)
406 (<= (overlay-start x) eol)
408 org-columns-overlays)))
409 (org-columns-time (time-to-number-of-days (current-time)))
412 ((equal key "CLOCKSUM")
413 (error "This special column cannot be edited"))
415 (setq eval '(org-with-point-at pom
416 (org-edit-headline))))
418 (setq eval '(org-with-point-at
420 (call-interactively 'org-todo))))
421 ((equal key "PRIORITY")
422 (setq eval '(org-with-point-at pom
423 (call-interactively 'org-priority))))
425 (setq eval '(org-with-point-at pom
426 (let ((org-fast-tag-selection-single-key
427 (if (eq org-fast-tag-selection-single-key 'expert)
428 t org-fast-tag-selection-single-key)))
429 (call-interactively 'org-set-tags)))))
430 ((equal key "DEADLINE")
431 (setq eval '(org-with-point-at pom
432 (call-interactively 'org-deadline))))
433 ((equal key "SCHEDULED")
434 (setq eval '(org-with-point-at pom
435 (call-interactively 'org-schedule))))
436 ((equal key "BEAMER_env")
437 (setq eval '(org-with-point-at pom
438 (call-interactively 'org-beamer-select-environment))))
440 (setq allowed (org-property-get-allowed-values pom key 'table))
442 (setq nval (org-icompleting-read
443 "Value: " allowed nil
444 (not (get-text-property 0 'org-unrestricted
446 (setq nval (read-string "Edit: " value)))
447 (setq nval (org-trim nval))
448 (when (not (equal nval value))
449 (setq eval '(org-entry-put pom key nval)))))
453 ((equal major-mode 'org-agenda-mode)
454 (org-columns-eval eval)
455 ;; The following let preserves the current format, and makes sure
456 ;; that in only a single file things need to be updated.
457 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
458 (buffer (marker-buffer pom))
459 (org-agenda-contributing-files
460 (list (with-current-buffer buffer
461 (buffer-file-name (buffer-base-buffer))))))
462 (org-agenda-columns)))
464 (let ((inhibit-read-only t))
465 (org-with-silent-modifications
466 (remove-text-properties
467 (max (point-min) (1- bol)) eol '(read-only t)))
470 (setq org-columns-overlays
471 (org-delete-all line-overlays org-columns-overlays))
472 (mapc 'delete-overlay line-overlays)
473 (org-columns-eval eval))
474 (org-columns-display-here)))
475 (org-move-to-column col)
476 (if (and (derived-mode-p 'org-mode)
477 (nth 3 (assoc-string key org-columns-current-fmt-compiled t)))
478 (org-columns-update key)))))))
480 (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
481 "Edit the current headline, the part without TODO keyword, TAGS."
482 (org-back-to-heading)
483 (when (looking-at org-todo-line-regexp)
485 (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
486 (txt (match-string 3))
489 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@#%]+:[ \t]*$") txt)
490 (setq post (match-string 0 txt)
491 txt (substring txt 0 (match-beginning 0))))
492 (setq txt2 (read-string "Edit: " txt))
493 (when (not (equal txt txt2))
495 (insert pre txt2 post)
496 (delete-region (point) (point-at-eol))
497 (org-set-tags nil t)))))
499 (defun org-columns-edit-allowed ()
500 "Edit the list of allowed values for the current property."
502 (let* ((pom (or (org-get-at-bol 'org-marker)
503 (org-get-at-bol 'org-hd-marker)
505 (key (get-char-property (point) 'org-columns-key))
506 (key1 (concat key "_ALL"))
507 (allowed (org-entry-get pom key1 t))
509 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
510 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
511 (setq nval (read-string "Allowed: " allowed))
513 (cond ((marker-position org-entry-property-inherited-from)
514 org-entry-property-inherited-from)
515 ((marker-position org-columns-top-level-marker)
516 org-columns-top-level-marker)
520 (defun org-columns-eval (form)
523 (beginning-of-line 1)
524 ;; `next-line' is needed here, because it skips invisible line.
525 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
526 (setq hidep (org-at-heading-p 1)))
528 (and hidep (hide-entry))))
530 (defun org-columns-previous-allowed-value ()
531 "Switch to the previous allowed value for this column."
533 (org-columns-next-allowed-value t))
535 (defun org-columns-next-allowed-value (&optional previous nth)
536 "Switch to the next allowed value for this column.
537 When PREVIOUS is set, go to the previous value. When NTH is
538 an integer, select that value."
540 (org-columns-check-computed)
541 (let* ((col (current-column))
542 (key (get-char-property (point) 'org-columns-key))
543 (value (get-char-property (point) 'org-columns-value))
544 (bol (point-at-bol)) (eol (point-at-eol))
545 (pom (or (get-text-property bol 'org-hd-marker)
546 (point))) ; keep despite of compiler waring
548 (delq nil (mapcar (lambda (x)
549 (and (eq (overlay-buffer x) (current-buffer))
550 (>= (overlay-start x) bol)
551 (<= (overlay-start x) eol)
553 org-columns-overlays)))
554 (allowed (or (org-property-get-allowed-values pom key)
556 (nth 4 (assoc-string key
557 org-columns-current-fmt-compiled
559 '(checkbox checkbox-n-of-m checkbox-percent))
561 (org-colview-construct-allowed-dates value)))
565 (if (= nth -1) (setq nth 9)))
566 (when (equal key "ITEM")
567 (error "Cannot edit item headline from here"))
568 (unless (or allowed (member key '("SCHEDULED" "DEADLINE" "CLOCKSUM")))
569 (error "Allowed values for this property have not been defined"))
570 (if (member key '("SCHEDULED" "DEADLINE" "CLOCKSUM"))
571 (setq nval (if previous 'earlier 'later))
572 (if previous (setq allowed (reverse allowed)))
575 (setq nval (nth nth allowed))
577 (error "There are only %d allowed values for property `%s'"
578 (length allowed) key)))
579 ((member value allowed)
580 (setq nval (or (car (cdr (member value allowed)))
582 (if (equal nval value)
583 (error "Only one allowed value for this property")))
584 (t (setq nval (car allowed)))))
586 ((equal major-mode 'org-agenda-mode)
587 (org-columns-eval '(org-entry-put pom key nval))
588 ;; The following let preserves the current format, and makes sure
589 ;; that in only a single file things need to be updated.
590 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
591 (buffer (marker-buffer pom))
592 (org-agenda-contributing-files
593 (list (with-current-buffer buffer
594 (buffer-file-name (buffer-base-buffer))))))
595 (org-agenda-columns)))
597 (let ((inhibit-read-only t))
598 (remove-text-properties (1- bol) eol '(read-only t))
601 (setq org-columns-overlays
602 (org-delete-all line-overlays org-columns-overlays))
603 (mapc 'delete-overlay line-overlays)
604 (org-columns-eval '(org-entry-put pom key nval)))
605 (org-columns-display-here)))
606 (org-move-to-column col)
607 (and (nth 3 (assoc-string key org-columns-current-fmt-compiled t))
608 (org-columns-update key))))))
610 (defun org-colview-construct-allowed-dates (s)
611 "Construct a list of three dates around the date in S.
612 This respects the format of the time stamp in S, active or non-active,
613 and also including time or not. S must be just a time stamp, no text
615 (when (and s (string-match (concat "^" org-ts-regexp3 "$") s))
616 (let* ((time (org-parse-time-string s 'nodefaults))
617 (active (equal (string-to-char s) ?<))
618 (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
619 time-before time-after)
620 (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
621 (setf (car time) (or (car time) 0))
622 (setf (nth 1 time) (or (nth 1 time) 0))
623 (setf (nth 2 time) (or (nth 2 time) 0))
624 (setq time-before (copy-sequence time))
625 (setq time-after (copy-sequence time))
626 (setf (nth 3 time-before) (1- (nth 3 time)))
627 (setf (nth 3 time-after) (1+ (nth 3 time)))
628 (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
629 (list time-before time time-after)))))
631 (defun org-verify-version (task)
634 (if (or (featurep 'xemacs)
635 (< emacs-major-version 22))
636 (error "Emacs 22 is required for the columns feature")))))
638 (defun org-columns-open-link (&optional arg)
640 (let ((value (get-char-property (point) 'org-columns-value)))
641 (org-open-link-from-string value arg)))
644 (defun org-columns-get-format-and-top-level ()
645 (let ((fmt (org-columns-get-format)))
646 (org-columns-goto-top-level)
649 (defun org-columns-get-format (&optional fmt-string)
651 (let (fmt-as-property fmt)
652 (when (condition-case nil (org-back-to-heading) (error nil))
653 (setq fmt-as-property (org-entry-get nil "COLUMNS" t)))
654 (setq fmt (or fmt-string fmt-as-property org-columns-default-format))
655 (org-set-local 'org-columns-current-fmt fmt)
656 (org-columns-compile-format fmt)
659 (defun org-columns-goto-top-level ()
660 (when (condition-case nil (org-back-to-heading) (error nil))
661 (org-entry-get nil "COLUMNS" t))
662 (if (marker-position org-entry-property-inherited-from)
663 (move-marker org-columns-top-level-marker org-entry-property-inherited-from)
664 (move-marker org-columns-top-level-marker (point))))
667 (defun org-columns (&optional columns-fmt-string)
668 "Turn on column view on an org-mode file.
669 When COLUMNS-FMT-STRING is non-nil, use it as the column format."
671 (org-verify-version 'columns)
672 (org-columns-remove-overlays)
673 (move-marker org-columns-begin-marker (point))
674 (let ((org-columns-time (time-to-number-of-days (current-time)))
675 beg end fmt cache maxwidths)
676 (org-columns-goto-top-level)
677 (setq fmt (org-columns-get-format columns-fmt-string))
679 (goto-char org-columns-top-level-marker)
681 (unless org-columns-inhibit-recalculation
682 (org-columns-compute-all))
683 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
685 ;; Get and cache the properties
687 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
690 (narrow-to-region beg end)
692 (when (assoc "CLOCKSUM_T" org-columns-current-fmt-compiled)
695 (narrow-to-region beg end)
696 (org-clock-sum-today))))
697 (while (re-search-forward org-outline-regexp-bol end t)
698 (if (and org-columns-skip-archived-trees
699 (looking-at (concat ".*:" org-archive-tag ":")))
700 (org-end-of-subtree t)
701 (push (cons (org-current-line) (org-entry-properties)) cache)))
703 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
704 (org-set-local 'org-columns-current-maxwidths maxwidths)
705 (org-columns-display-here-title)
706 (when (org-set-local 'org-columns-flyspell-was-active
707 (org-bound-and-true-p flyspell-mode))
709 (unless (local-variable-p 'org-colview-initial-truncate-line-value)
710 (org-set-local 'org-colview-initial-truncate-line-value
712 (setq truncate-lines t)
714 (org-goto-line (car x))
715 (org-columns-display-here (cdr x)))
718 (eval-when-compile (defvar org-columns-time))
720 (defvar org-columns-compile-map
726 ("X/" checkbox-n-of-m +)
727 ("X%" checkbox-percent +)
728 ("max" max_numbers max)
729 ("min" min_numbers min)
731 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
732 (":max" max_times max)
733 (":min" min_times min)
735 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
736 ("@min" min_age min (lambda (x) (- org-columns-time x)))
737 ("@max" max_age max (lambda (x) (- org-columns-time x)))
739 (lambda (&rest x) (/ (apply '+ x) (float (length x))))
740 (lambda (x) (- org-columns-time x)))
741 ("est+" estimate org-estimate-combine))
742 "Operator <-> format,function,calc map.
743 Used to compile/uncompile columns format and completing read in
744 interactive function `org-columns-new'.
746 operator string used in #+COLUMNS definition describing the
748 format symbol describing summary type selected interactively in
749 `org-columns-new' and internally in
750 `org-columns-number-to-string' and
751 `org-columns-string-to-number'
752 function called with a list of values as argument to calculate
754 calc function called on every element before summarizing. This is
755 optional and should only be specified if needed")
757 (defun org-columns-new (&optional prop title width op fmt fun &rest rest)
758 "Insert a new column, to the left of the current column."
760 (let ((editp (and prop
761 (assoc-string prop org-columns-current-fmt-compiled t)))
763 (setq prop (org-icompleting-read
764 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
766 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
767 (setq width (read-string "Column width: " (if width (number-to-string width))))
768 (if (string-match "\\S-" width)
769 (setq width (string-to-number width))
771 (setq fmt (org-icompleting-read
773 (mapcar (lambda (x) (list (symbol-name (cadr x))))
774 org-columns-compile-map)
776 (setq fmt (intern fmt)
777 fun (cdr (assoc fmt (mapcar 'cdr org-columns-compile-map))))
778 (if (eq fmt 'none) (setq fmt nil))
782 (setcdr editp (list title width nil fmt nil fun)))
783 (setq cell (nthcdr (1- (current-column))
784 org-columns-current-fmt-compiled))
785 (setcdr cell (cons (list prop title width nil fmt nil
786 (car fun) (cadr fun))
788 (org-columns-store-format)
791 (defun org-columns-delete ()
792 "Delete the column at point from columns view."
794 (let* ((n (current-column))
795 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
797 (format "Are you sure you want to remove column \"%s\"? " title))
798 (setq org-columns-current-fmt-compiled
799 (delq (nth n org-columns-current-fmt-compiled)
800 org-columns-current-fmt-compiled))
801 (org-columns-store-format)
803 (if (>= (current-column) (length org-columns-current-fmt-compiled))
804 (backward-char 1)))))
806 (defun org-columns-edit-attributes ()
807 "Edit the attributes of the current column."
809 (let* ((n (current-column))
810 (info (nth n org-columns-current-fmt-compiled)))
811 (apply 'org-columns-new info)))
813 (defun org-columns-widen (arg)
814 "Make the column wider by ARG characters."
816 (let* ((n (current-column))
817 (entry (nth n org-columns-current-fmt-compiled))
818 (width (or (nth 2 entry)
819 (cdr (assoc-string (car entry)
820 org-columns-current-maxwidths
822 (setq width (max 1 (+ width arg)))
823 (setcar (nthcdr 2 entry) width)
824 (org-columns-store-format)
827 (defun org-columns-narrow (arg)
828 "Make the column narrower by ARG characters."
830 (org-columns-widen (- arg)))
832 (defun org-columns-move-right ()
833 "Swap this column with the one to the right."
835 (let* ((n (current-column))
836 (cell (nthcdr n org-columns-current-fmt-compiled))
838 (when (>= n (1- (length org-columns-current-fmt-compiled)))
839 (error "Cannot shift this column further to the right"))
841 (setcar cell (car (cdr cell)))
842 (setcdr cell (cons e (cdr (cdr cell))))
843 (org-columns-store-format)
847 (defun org-columns-move-left ()
848 "Swap this column with the one to the left."
850 (let* ((n (current-column)))
852 (error "Cannot shift this column further to the left"))
854 (org-columns-move-right)
857 (defun org-columns-store-format ()
858 "Store the text version of the current columns format in appropriate place.
859 This is either in the COLUMNS property of the node starting the current column
860 display, or in the #+COLUMNS line of the current buffer."
862 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
863 (org-set-local 'org-columns-current-fmt fmt)
864 (if (marker-position org-columns-top-level-marker)
866 (goto-char org-columns-top-level-marker)
867 (if (and (org-at-heading-p)
868 (org-entry-get nil "COLUMNS"))
869 (org-entry-put nil "COLUMNS" fmt)
870 (goto-char (point-min))
871 ;; Overwrite all #+COLUMNS lines....
872 (while (re-search-forward "^[ \t]*#\\+COLUMNS:.*" nil t)
874 (replace-match (concat "#+COLUMNS: " fmt) t t))
876 (goto-char (point-min))
877 (or (org-at-heading-p t) (outline-next-heading))
878 (let ((inhibit-read-only t))
879 (insert-before-markers "#+COLUMNS: " fmt "\n")))
880 (org-set-local 'org-columns-default-format fmt))))))
882 (defun org-columns-get-autowidth-alist (s cache)
883 "Derive the maximum column widths from the format and the cache."
885 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
886 (push (cons (match-string 1 s) 1) rtn)
887 (setq start (match-end 0)))
891 (let ((prop (car x)))
894 (length (or (cdr (assoc-string prop (cdr y) t))
900 (defun org-columns-compute-all ()
901 "Compute all columns that have operators defined."
902 (org-with-silent-modifications
903 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
904 (let ((columns org-columns-current-fmt-compiled)
905 (org-columns-time (time-to-number-of-days (current-time)))
907 (while (setq col (pop columns))
910 (org-columns-compute (car col)))))))
912 (defun org-columns-update (property)
913 "Recompute PROPERTY, and update the columns display for it."
914 (org-columns-compute property)
918 (when (equal (overlay-get ov 'org-columns-key) property)
919 (setq pos (overlay-start ov))
921 (when (setq val (cdr (assoc-string
924 (point-at-bol) 'org-summaries)
926 (setq fmt (overlay-get ov 'org-columns-format))
927 (overlay-put ov 'org-columns-value val)
928 (overlay-put ov 'display (format fmt val)))))
929 org-columns-overlays))))
931 (defvar org-inlinetask-min-level
932 (if (featurep 'org-inlinetask) org-inlinetask-min-level 15))
935 (defun org-columns-compute (property)
936 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
938 (let* ((re org-outline-regexp-bol)
939 (lmax 30) ; Does anyone use deeper levels???
940 (lvals (make-vector lmax nil))
941 (lflag (make-vector lmax nil))
943 (ass (assoc-string property org-columns-current-fmt-compiled t))
947 (calc (or (nth 7 ass) 'identity))
948 (beg org-columns-top-level-marker)
949 (inminlevel org-inlinetask-min-level)
950 (last-level org-inlinetask-min-level)
951 val valflag flag end sumpos sum-alist sum str str1 useval)
953 ;; Find the region to compute
955 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
957 ;; Walk the tree from the back and do the computations
958 (while (re-search-backward re beg t)
959 (setq sumpos (match-beginning 0)
960 last-level (if (not (or (zerop level) (eq level inminlevel)))
962 level (org-outline-level)
963 val (org-entry-get nil property)
964 valflag (and val (string-match "\\S-" val)))
966 ((< level last-level)
967 ;; Put the sum of lower levels here as a property. If
968 ;; values are estimate, use an appropriate sum function.
970 (if (eq fun 'org-estimate-combine) #'org-estimate-combine
972 (if (and (/= last-level inminlevel)
973 (aref lvals last-level))
974 (apply fun (aref lvals last-level)) 0)
975 (if (aref lvals inminlevel)
976 (apply fun (aref lvals inminlevel)) 0))
977 flag (or (aref lflag last-level) ; any valid entries from children?
978 (aref lflag inminlevel)) ; or inline tasks?
979 str (org-columns-number-to-string sum format printf)
980 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
981 useval (if flag str1 (if valflag val ""))
982 sum-alist (get-text-property sumpos 'org-summaries))
983 (let ((old (assoc-string property sum-alist t)))
984 (if old (setcdr old useval)
985 (push (cons property useval) sum-alist)
986 (org-with-silent-modifications
987 (add-text-properties sumpos (1+ sumpos)
988 (list 'org-summaries sum-alist)))))
989 (when (and val (not (equal val (if flag str val))))
990 (org-entry-put nil property (if flag str val)))
991 ;; add current to current level accumulator
992 (when (or flag valflag)
995 (funcall calc (org-columns-string-to-number
996 (if flag str val) format)))
998 (aset lflag level t))
999 ;; clear accumulators for deeper levels
1000 (loop for l from (1+ level) to (1- lmax) do
1002 (aset lflag l nil)))
1003 ((>= level last-level)
1004 ;; add what we have here to the accumulator for this level
1006 (push (funcall calc (org-columns-string-to-number val format))
1008 (aset lflag level t)))
1009 (t (error "This should not happen")))))))
1011 (defun org-columns-redo ()
1012 "Construct the column display again."
1014 (message "Recomputing columns...")
1015 (let ((line (org-current-line))
1016 (col (current-column)))
1018 (if (marker-position org-columns-begin-marker)
1019 (goto-char org-columns-begin-marker))
1020 (org-columns-remove-overlays)
1021 (if (derived-mode-p 'org-mode)
1022 (call-interactively 'org-columns)
1024 (call-interactively 'org-agenda-columns)))
1025 (org-goto-line line)
1026 (move-to-column col))
1027 (message "Recomputing columns...done"))
1029 (defun org-columns-not-in-agenda ()
1030 (if (eq major-mode 'org-agenda-mode)
1031 (error "This command is only allowed in Org-mode buffers")))
1033 (defun org-string-to-number (s)
1034 "Convert string to number, and interpret hh:mm:ss."
1035 (if (not (string-match ":" s))
1036 (string-to-number s)
1037 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1039 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1043 (defun org-columns-number-to-string (n fmt &optional printf)
1044 "Convert a computed column number to a string value, according to FMT."
1046 ((memq fmt '(estimate)) (org-estimate-print n printf))
1047 ((not (numberp n)) "")
1048 ((memq fmt '(add_times max_times min_times mean_times))
1049 (org-hours-to-clocksum-string n))
1051 (cond ((= n (floor n)) "[X]")
1054 ((memq fmt '(checkbox-n-of-m checkbox-percent))
1055 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
1056 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
1057 (printf (format printf n))
1060 ((memq fmt '(min_age max_age mean_age))
1061 (org-format-time-period n))
1062 (t (number-to-string n))))
1064 (defun org-nofm-to-completion (n m &optional percent)
1066 (format "[%d/%d]" n m)
1067 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
1070 (defun org-columns-string-to-number (s fmt)
1071 "Convert a column value to a number that can be used for column computing."
1074 ((memq fmt '(min_age max_age mean_age))
1075 (cond ((string= s "") org-columns-time)
1077 "\\([0-9]+\\)d \\([0-9]+\\)h \\([0-9]+\\)m \\([0-9]+\\)s"
1079 (+ (* 60 (+ (* 60 (+ (* 24 (string-to-number (match-string 1 s)))
1080 (string-to-number (match-string 2 s))))
1081 (string-to-number (match-string 3 s))))
1082 (string-to-number (match-string 4 s))))
1083 (t (time-to-number-of-days (apply 'encode-time
1084 (org-parse-time-string s t))))))
1085 ((string-match ":" s)
1086 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1088 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1090 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
1091 (if (equal s "[X]") 1. 0.000001))
1092 ((memq fmt '(estimate)) (org-string-to-estimate s))
1093 ((string-match (concat "\\([0-9.]+\\) *\\("
1094 (regexp-opt (mapcar 'car org-effort-durations))
1096 (setq s (concat "0:" (org-duration-string-to-minutes s t)))
1097 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1099 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1101 (t (string-to-number s)))))
1103 (defun org-columns-uncompile-format (cfmt)
1104 "Turn the compiled columns format back into a string representation."
1105 (let ((rtn "") e s prop title op op-match width fmt printf fun calc ee map)
1106 (while (setq e (pop cfmt))
1115 (setq map (copy-sequence org-columns-compile-map))
1116 (while (setq ee (pop map))
1117 (if (equal fmt (nth 1 ee))
1118 (setq op (car ee) map nil)))
1119 (if (and op printf) (setq op (concat op ";" printf)))
1120 (if (equal title prop) (setq title nil))
1121 (setq s (concat "%" (if width (number-to-string width))
1123 (if title (concat "(" title ")"))
1124 (if op (concat "{" op "}"))))
1125 (setq rtn (concat rtn " " s)))
1128 (defun org-columns-compile-format (fmt)
1129 "Turn a column format string into an alist of specifications.
1130 The alist has one entry for each column in the format. The elements of
1132 property the property
1133 title the title field for the columns
1134 width the column width in characters, can be nil for automatic
1135 operator the operator if any
1136 format the output format for computed results, derived from operator
1137 printf a printf format for computed values
1138 fun the lisp function to compute summary values, derived from operator
1139 calc function to get values from base elements"
1140 (let ((start 0) width prop title op op-match f printf fun calc)
1141 (setq org-columns-current-fmt-compiled nil)
1142 (while (string-match
1143 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
1145 (setq start (match-end 0)
1146 width (match-string 1 fmt)
1147 prop (match-string 2 fmt)
1148 title (or (match-string 3 fmt) prop)
1149 op (match-string 4 fmt)
1154 (if width (setq width (string-to-number width)))
1155 (when (and op (string-match ";" op))
1156 (setq printf (substring op (match-end 0))
1157 op (substring op 0 (match-beginning 0))))
1158 (when (setq op-match (assoc op org-columns-compile-map))
1159 (setq f (cadr op-match)
1160 fun (caddr op-match)
1161 calc (cadddr op-match)))
1162 (push (list prop title width op f printf fun calc)
1163 org-columns-current-fmt-compiled))
1164 (setq org-columns-current-fmt-compiled
1165 (nreverse org-columns-current-fmt-compiled))))
1168 ;;; Dynamic block for Column view
1170 (defvar org-heading-regexp) ; defined in org.el
1171 (defvar org-heading-keyword-regexp-format) ; defined in org.el
1172 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
1173 "Get the column view of the current buffer or subtree.
1174 The first optional argument MAXLEVEL sets the level limit. A
1175 second optional argument SKIP-EMPTY-ROWS tells whether to skip
1176 empty rows, an empty row being one where all the column view
1177 specifiers except ITEM are empty. This function returns a list
1178 containing the title row and all other rows. Each row is a list
1181 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1182 (re-archive (concat ".*:" org-archive-tag ":"))
1183 (n (length title)) row tbl)
1184 (goto-char (point-min))
1185 (while (re-search-forward org-heading-regexp nil t)
1187 (when (and (or (null maxlevel)
1189 (if org-odd-levels-only
1190 (/ (1+ (length (match-string 1))) 2)
1191 (length (match-string 1)))))
1192 (get-char-property (match-beginning 0) 'org-columns-key))
1193 (when (or (org-in-commented-heading-p t)
1196 (looking-at re-archive)))
1197 (org-end-of-subtree t)
1200 (loop for i from 0 to (1- n) do
1203 (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
1204 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
1207 (setq row (nreverse row))
1208 (unless (and skip-empty-rows
1209 (eq 1 (length (delete "" (delete-dups (copy-sequence row))))))
1211 (append (list title 'hline) (nreverse tbl)))))
1214 (defun org-dblock-write:columnview (params)
1215 "Write the column view table.
1216 PARAMS is a property list of parameters:
1218 :width enforce same column widths with <N> specifiers.
1219 :id the :ID: property of the entry where the columns view
1220 should be built. When the symbol `local', call locally.
1221 When `global' call column view with the cursor at the beginning
1222 of the buffer (usually this means that the whole buffer switches
1223 to column view). When \"file:path/to/file.org\", invoke column
1224 view at the start of that file. Otherwise, the ID is located
1225 using `org-id-find'.
1226 :hlines When t, insert a hline before each item. When a number, insert
1227 a hline before each level <= that number.
1228 :vlines When t, make each column a colgroup to enforce vertical lines.
1229 :maxlevel When set to a number, don't capture headlines below this level.
1231 When t, skip rows where all specifiers other than ITEM are empty.
1232 :format When non-nil, specify the column view format to use."
1233 (let ((pos (point-marker))
1234 (hlines (plist-get params :hlines))
1235 (vlines (plist-get params :vlines))
1236 (maxlevel (plist-get params :maxlevel))
1237 (content-lines (org-split-string (plist-get params :content) "\n"))
1238 (skip-empty-rows (plist-get params :skip-empty-rows))
1239 (columns-fmt (plist-get params :format))
1240 (case-fold-search t)
1241 tbl id idpos nfields tmp recalc line
1242 id-as-string view-file view-pos)
1243 (when (setq id (plist-get params :id))
1244 (setq id-as-string (cond ((numberp id) (number-to-string id))
1245 ((symbolp id) (symbol-name id))
1248 (cond ((not id) nil)
1249 ((eq id 'global) (setq view-pos (point-min)))
1251 ((string-match "^file:\\(.*\\)" id-as-string)
1252 (setq view-file (match-string 1 id-as-string)
1254 (unless (file-exists-p view-file)
1255 (error "No such file: \"%s\"" id-as-string)))
1256 ((setq idpos (org-find-entry-with-id id))
1257 (setq view-pos idpos))
1258 ((setq idpos (org-id-find id))
1259 (setq view-file (car idpos))
1260 (setq view-pos (cdr idpos)))
1261 (t (error "Cannot find entry with :ID: %s" id))))
1262 (with-current-buffer (if view-file
1263 (get-file-buffer view-file)
1268 (goto-char (or view-pos (point)))
1269 (org-columns columns-fmt)
1270 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1271 (setq nfields (length (car tbl)))
1272 (org-columns-quit))))
1274 (move-marker pos nil)
1276 (when (plist-get params :hlines)
1279 (if (eq (car tbl) 'hline)
1280 (push (pop tbl) tmp)
1281 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1282 (if (and (not (eq (car tmp) 'hline))
1284 (and (numberp hlines)
1285 (<= (- (match-end 1) (match-beginning 1))
1288 (push (pop tbl) tmp)))
1289 (setq tbl (nreverse tmp)))
1291 (setq tbl (mapcar (lambda (x)
1292 (if (eq 'hline x) x (cons "" x)))
1294 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1296 (while (string-match "^#" (car content-lines))
1297 (insert (pop content-lines) "\n")))
1299 (insert (org-listtable-to-string tbl))
1300 (when (plist-get params :width)
1301 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1302 org-columns-current-widths "|")))
1303 (while (setq line (pop content-lines))
1304 (when (string-match "^#" line)
1306 (when (string-match "^[ \t]*#\\+tblfm" line)
1309 (progn (goto-char pos) (org-table-recalculate 'all))
1311 (org-table-align)))))
1313 (defun org-listtable-to-string (tbl)
1314 "Convert a listtable TBL to a string that contains the Org-mode table.
1315 The table still need to be aligned. The resulting string has no leading
1316 and tailing newline characters."
1321 (concat "|" (mapconcat 'identity x "|") "|"))
1322 ((eq x 'hline) "|-|")
1323 (t (error "Garbage in listtable: %s" x))))
1327 (defun org-insert-columns-dblock ()
1328 "Create a dynamic block capturing a column view table."
1330 (let ((defaults '(:name "columnview" :hlines 1))
1331 (id (org-icompleting-read
1332 "Capture columns (local, global, entry with :ID: property) [local]: "
1333 (append '(("global") ("local"))
1334 (mapcar 'list (org-property-values "ID"))))))
1335 (if (equal id "") (setq id 'local))
1336 (if (equal id "global") (setq id 'global))
1337 (setq defaults (append defaults (list :id id)))
1338 (org-create-dblock defaults)
1339 (org-update-dblock)))
1341 ;;; Column view in the agenda
1343 (defvar org-agenda-view-columns-initially nil
1344 "When set, switch to columns view immediately after creating the agenda.")
1346 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1347 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1348 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1351 (defun org-agenda-columns ()
1352 "Turn on or update column view in the agenda."
1354 (org-verify-version 'columns)
1355 (org-columns-remove-overlays)
1356 (move-marker org-columns-begin-marker (point))
1357 (let ((org-columns-time (time-to-number-of-days (current-time)))
1358 cache maxwidths m p a d fmt)
1360 ((and (boundp 'org-agenda-overriding-columns-format)
1361 org-agenda-overriding-columns-format)
1362 (setq fmt org-agenda-overriding-columns-format))
1363 ((setq m (org-get-at-bol 'org-hd-marker))
1364 (setq fmt (or (org-entry-get m "COLUMNS" t)
1365 (with-current-buffer (marker-buffer m)
1366 org-columns-default-format))))
1367 ((and (boundp 'org-columns-current-fmt)
1368 (local-variable-p 'org-columns-current-fmt)
1369 org-columns-current-fmt)
1370 (setq fmt org-columns-current-fmt))
1371 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
1372 (setq m (get-text-property m 'org-hd-marker))
1373 (setq fmt (or (org-entry-get m "COLUMNS" t)
1374 (with-current-buffer (marker-buffer m)
1375 org-columns-default-format)))))
1376 (setq fmt (or fmt org-columns-default-format))
1377 (org-set-local 'org-columns-current-fmt fmt)
1378 (org-columns-compile-format fmt)
1379 (when org-agenda-columns-compute-summary-properties
1380 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1382 ;; Get and cache the properties
1383 (goto-char (point-min))
1385 (when (setq m (or (org-get-at-bol 'org-hd-marker)
1386 (org-get-at-bol 'org-marker)))
1387 (setq p (org-entry-properties m))
1389 (when (or (not (setq a (assoc-string org-effort-property p t)))
1390 (not (string-match "\\S-" (or (cdr a) ""))))
1391 ;; OK, the property is not defined. Use appointment duration?
1392 (when (and org-agenda-columns-add-appointments-to-effort-sum
1393 (setq d (get-text-property (point) 'duration)))
1394 (setq d (org-minutes-to-clocksum-string d))
1395 (put-text-property 0 (length d) 'face 'org-warning d)
1396 (push (cons org-effort-property d) p)))
1397 (push (cons (org-current-line) p) cache))
1398 (beginning-of-line 2))
1400 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
1401 (org-set-local 'org-columns-current-maxwidths maxwidths)
1402 (org-columns-display-here-title)
1403 (when (org-set-local 'org-columns-flyspell-was-active
1404 (org-bound-and-true-p flyspell-mode))
1407 (org-goto-line (car x))
1408 (org-columns-display-here (cdr x)))
1410 (when org-agenda-columns-show-summaries
1411 (org-agenda-colview-summarize cache))))))
1413 (defun org-agenda-colview-summarize (cache)
1414 "Summarize the summarizable columns in column view in the agenda.
1415 This will add overlays to the date lines, to show the summary for each day."
1416 (let* ((fmt (mapcar (lambda (x)
1417 (if (string-match "CLOCKSUM.*" (car x))
1418 (list (match-string 0 (car x))
1419 (nth 1 x) (nth 2 x) ":" 'add_times
1422 org-columns-current-fmt-compiled))
1423 line c c1 stype calc sumfunc props lsum entries prop v title)
1425 (when (delq nil (mapcar 'cadr fmt))
1426 ;; OK, at least one summation column, it makes sense to try this
1427 (goto-char (point-max))
1429 (when (or (get-text-property (point) 'org-date-line)
1430 (eq (get-text-property (point) 'face)
1431 'org-agenda-structure))
1432 ;; OK, this is a date line that should be used
1433 (setq line (org-current-line))
1434 (setq entries nil c cache cache nil)
1435 (while (setq c1 (pop c))
1436 (if (> (car c1) line)
1439 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1440 ;; Compute the summaries for the properties we want,
1441 ;; set nil properties for the rest.
1442 (when (setq entries (mapcar 'cdr entries))
1450 calc (or (nth 7 f) 'identity))
1452 ((equal prop "ITEM")
1453 (cons prop (buffer-substring (point-at-bol)
1455 ((not stype) (cons prop ""))
1456 (t ;; do the summary
1459 (setq v (cdr (assoc-string prop x t)))
1463 (if (not (get-text-property 0 'org-computed v))
1466 (org-columns-string-to-number
1469 (setq lsum (remove nil lsum))
1471 (cond ((> (length lsum) 1)
1472 (org-columns-number-to-string
1473 (apply sumfunc lsum) stype))
1474 ((eq (length lsum) 1)
1475 (org-columns-number-to-string
1478 (put-text-property 0 (length lsum) 'face 'bold lsum)
1479 (unless (eq calc 'identity)
1480 (put-text-property 0 (length lsum) 'org-computed t lsum))
1483 (org-columns-display-here props 'dateline)
1484 (org-set-local 'org-agenda-columns-active t)))
1485 (if (bobp) (throw 'exit t))
1486 (beginning-of-line 0))))))
1488 (defun org-agenda-colview-compute (fmt)
1489 "Compute the relevant columns in the contributing source buffers."
1490 (let ((files org-agenda-contributing-files)
1491 (org-columns-begin-marker (make-marker))
1492 (org-columns-top-level-marker (make-marker))
1494 (while (setq f (pop files))
1495 (setq b (find-buffer-visiting f))
1496 (with-current-buffer (or (buffer-base-buffer b) b)
1500 (org-with-silent-modifications
1501 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
1502 (goto-char (point-min))
1503 (org-columns-get-format-and-top-level)
1504 (while (setq fm (pop fmt))
1505 (cond ((equal (car fm) "CLOCKSUM")
1507 ((equal (car fm) "CLOCKSUM_T")
1508 (org-clock-sum-today))
1510 (setq a (assoc-string (car fm)
1511 org-columns-current-fmt-compiled
1513 (equal (nth 4 a) (nth 4 fm)))
1514 (org-columns-compute (car fm)))))))))))
1516 (defun org-format-time-period (interval)
1517 "Convert time in fractional days to days/hours/minutes/seconds."
1518 (if (numberp interval)
1519 (let* ((days (floor interval))
1520 (frac-hours (* 24 (- interval days)))
1521 (hours (floor frac-hours))
1522 (minutes (floor (* 60 (- frac-hours hours))))
1523 (seconds (floor (* 60 (- (* 60 (- frac-hours hours)) minutes)))))
1524 (format "%dd %02dh %02dm %02ds" days hours minutes seconds))
1527 (defun org-estimate-mean-and-var (v)
1528 "Return the mean and variance of an estimate."
1529 (let* ((v (cond ((consp v) v)
1530 ((numberp v) (list v v))
1531 (t (error "Invalid estimate type"))))
1532 (low (float (car v)))
1533 (high (float (cadr v)))
1534 (mean (/ (+ low high) 2.0))
1535 (var (/ (+ (expt (- mean low) 2.0) (expt (- high mean) 2.0)) 2.0)))
1538 (defun org-estimate-combine (&rest el)
1539 "Combine a list of estimates, using mean and variance.
1540 The mean and variance of the result will be the sum of the means
1541 and variances (respectively) of the individual estimates."
1545 (let ((stats (org-estimate-mean-and-var e)))
1546 (setq mean (+ mean (car stats)))
1547 (setq var (+ var (cadr stats)))))
1549 (let ((stdev (sqrt var)))
1550 (list (- mean stdev) (+ mean stdev)))))
1552 (defun org-estimate-print (e &optional fmt)
1553 "Prepare a string representation of an estimate.
1554 This formats these numbers as two numbers with a \"-\" between them."
1555 (let ((fmt (or fmt "%.0f"))
1556 (e (cond ((consp e) e)
1557 ((numberp e) (list e e))
1558 (t (error "Invalid estimate type")))))
1559 (format "%s" (mapconcat (lambda (n) (format fmt n)) e "-"))))
1561 (defun org-string-to-estimate (s)
1562 "Convert a string to an estimate.
1563 The string should be two numbers joined with a \"-\"."
1564 (if (string-match "\\(.*\\)-\\(.*\\)" s)
1565 (list (string-to-number (match-string 1 s))
1566 (string-to-number(match-string 2 s)))
1567 (list (string-to-number s) (string-to-number s))))
1569 (provide 'org-colview)
1571 ;;; org-colview.el ends here