1 ;;; org-colview.el --- Column View in Org-mode
3 ;; Copyright (C) 2004-2011 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" ())
37 (when (featurep 'xemacs
)
38 (error "Do not load this file into XEmacs, use 'org-colview-xemacs.el'."))
42 (defvar org-columns-overlays nil
43 "Holds the list of current column overlays.")
45 (defvar org-columns-current-fmt nil
46 "Local variable, holds the currently active column format.")
47 (make-variable-buffer-local 'org-columns-current-fmt
)
48 (defvar org-columns-current-fmt-compiled nil
49 "Local variable, holds the currently active column format.
50 This is the compiled version of the format.")
51 (make-variable-buffer-local 'org-columns-current-fmt-compiled
)
52 (defvar org-columns-current-widths nil
53 "Loval variable, holds the currently widths of fields.")
54 (make-variable-buffer-local 'org-columns-current-widths
)
55 (defvar org-columns-current-maxwidths nil
56 "Loval variable, holds the currently active maximum column widths.")
57 (make-variable-buffer-local 'org-columns-current-maxwidths
)
58 (defvar org-columns-begin-marker
(make-marker)
59 "Points to the position where last a column creation command was called.")
60 (defvar org-columns-top-level-marker
(make-marker)
61 "Points to the position where current columns region starts.")
63 (defvar org-columns-map
(make-sparse-keymap)
64 "The keymap valid in column display.")
66 (defun org-columns-content ()
67 "Switch to contents view while in columns view."
72 (org-defkey org-columns-map
"c" 'org-columns-content
)
73 (org-defkey org-columns-map
"o" 'org-overview
)
74 (org-defkey org-columns-map
"e" 'org-columns-edit-value
)
75 (org-defkey org-columns-map
"\C-c\C-t" 'org-columns-todo
)
76 (org-defkey org-columns-map
"\C-c\C-c" 'org-columns-set-tags-or-toggle
)
77 (org-defkey org-columns-map
"\C-c\C-o" 'org-columns-open-link
)
78 (org-defkey org-columns-map
"v" 'org-columns-show-value
)
79 (org-defkey org-columns-map
"q" 'org-columns-quit
)
80 (org-defkey org-columns-map
"r" 'org-columns-redo
)
81 (org-defkey org-columns-map
"g" 'org-columns-redo
)
82 (org-defkey org-columns-map
[left] 'backward-char)
83 (org-defkey org-columns-map "\M-b" 'backward-char)
84 (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
85 (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
86 (org-defkey org-columns-map "\M-f"
87 (lambda () (interactive) (goto-char (1+ (point)))))
88 (org-defkey org-columns-map [right]
89 (lambda () (interactive) (goto-char (1+ (point)))))
90 (org-defkey org-columns-map [down]
91 (lambda () (interactive)
92 (let ((col (current-column)))
94 (while (and (org-invisible-p2) (not (eobp)))
95 (beginning-of-line 2))
97 (if (eq major-mode 'org-agenda-mode)
98 (org-agenda-do-context-action)))))
99 (org-defkey org-columns-map [up]
100 (lambda () (interactive)
101 (let ((col (current-column)))
102 (beginning-of-line 0)
103 (while (and (org-invisible-p2) (not (bobp)))
104 (beginning-of-line 0))
106 (if (eq major-mode 'org-agenda-mode)
107 (org-agenda-do-context-action)))))
108 (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
109 (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
110 (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
111 (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
112 (org-defkey org-columns-map "<" 'org-columns-narrow)
113 (org-defkey org-columns-map ">" 'org-columns-widen)
114 (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
115 (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
116 (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
117 (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
119 (org-defkey org-columns-map (number-to-string i)
120 `(lambda () (interactive)
121 (org-columns-next-allowed-value nil ,i))))
123 (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
125 ["Edit property" org-columns-edit-value t]
126 ["Next allowed value" org-columns-next-allowed-value t]
127 ["Previous allowed value" org-columns-previous-allowed-value t]
128 ["Show full value" org-columns-show-value t]
129 ["Edit allowed values" org-columns-edit-allowed t]
131 ["Edit column attributes" org-columns-edit-attributes t]
132 ["Increase column width" org-columns-widen t]
133 ["Decrease column width" org-columns-narrow t]
135 ["Move column right" org-columns-move-right t]
136 ["Move column left" org-columns-move-left t]
137 ["Add column" org-columns-new t]
138 ["Delete column" org-columns-delete t]
140 ["CONTENTS" org-columns-content t]
141 ["OVERVIEW" org-overview t]
142 ["Refresh columns display" org-columns-redo t]
144 ["Open link" org-columns-open-link t]
146 ["Quit" org-columns-quit t]))
148 (defun org-columns-new-overlay (beg end &optional string face)
149 "Create a new column overlay and add it to the list."
150 (let ((ov (make-overlay beg end)))
151 (overlay-put ov 'face (or face 'secondary-selection))
152 (org-overlay-display ov string face)
153 (push ov org-columns-overlays)
156 (defun org-columns-display-here (&optional props dateline)
157 "Overlay the current line with column display."
159 (let* ((fmt org-columns-current-fmt-compiled)
161 (level-face (save-excursion
162 (beginning-of-line 1)
163 (and (looking-at "\\(\\**\\)\\(\\* \\)")
164 (org-get-level-face 2))))
165 (ref-face (or level-face
166 (and (eq major-mode 'org-agenda-mode)
167 (get-text-property (point-at-bol) 'face))
169 (color (list :foreground (face-attribute ref-face :foreground)))
170 (face (list color 'org-column ref-face))
171 (face1 (list color 'org-agenda-column-dateline ref-face))
172 (cphr (get-text-property (point-at-bol) 'org-complex-heading-regexp))
173 pom property ass width f string ov column val modval s2 title calc)
174 ;; Check if the entry is in another buffer.
176 (if (eq major-mode 'org-agenda-mode)
177 (setq pom (or (org-get-at-bol 'org-hd-marker)
178 (org-get-at-bol 'org-marker))
179 props (if pom (org-entry-properties pom) nil))
180 (setq props (org-entry-properties nil))))
182 (while (setq column (pop fmt))
183 (setq property (car column)
185 ass (if (equal property "ITEM")
187 ;; When in a buffer, get the whole line,
188 ;; we'll clean it later…
189 (if (eq major-mode 'org-mode)
193 (buffer-substring-no-properties
194 (point-at-bol) (point-at-eol)))))
195 ;; In agenda, just get the `txt' property
197 (org-get-at-bol 'txt))))
198 (assoc property props))
199 width (or (cdr (assoc property org-columns-current-maxwidths))
202 f (format "%%-%d.%ds | " width width)
204 val (or (cdr ass) "")
205 modval (cond ((and org-columns-modify-value-for-display-function
207 org-columns-modify-value-for-display-function))
208 (funcall org-columns-modify-value-for-display-function
210 ((equal property "ITEM")
211 (org-columns-cleanup-item
212 val org-columns-current-fmt-compiled
213 (or org-complex-heading-regexp cphr)))
214 ((and calc (functionp calc)
215 (not (string= val ""))
216 (not (get-text-property 0 'org-computed val)))
217 (org-columns-number-to-string
218 (funcall calc (org-columns-string-to-number
221 (setq s2 (org-columns-add-ellipses (or modval val) width))
222 (setq string (format f s2))
223 ;; Create the overlay
225 (setq ov (org-columns-new-overlay
226 beg (setq beg (1+ beg)) string (if dateline face1 face)))
227 (overlay-put ov 'keymap org-columns-map)
228 (overlay-put ov 'org-columns-key property)
229 (overlay-put ov 'org-columns-value (cdr ass))
230 (overlay-put ov 'org-columns-value-modified modval)
231 (overlay-put ov 'org-columns-pom pom)
232 (overlay-put ov 'org-columns-format f)
233 (overlay-put ov 'line-prefix "")
234 (overlay-put ov 'wrap-prefix ""))
235 (if (or (not (char-after beg))
236 (equal (char-after beg) ?\n))
237 (let ((inhibit-read-only t))
240 (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
241 ;; Make the rest of the line disappear.
243 (setq ov (org-columns-new-overlay beg (point-at-eol)))
244 (overlay-put ov 'invisible t)
245 (overlay-put ov 'keymap org-columns-map)
246 (overlay-put ov 'intangible t)
247 (overlay-put ov 'line-prefix "")
248 (overlay-put ov 'wrap-prefix "")
249 (push ov org-columns-overlays)
250 (setq ov (make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
251 (overlay-put ov 'keymap org-columns-map)
252 (push ov org-columns-overlays)
253 (let ((inhibit-read-only t))
254 (put-text-property (max (point-min) (1- (point-at-bol)))
255 (min (point-max) (1+ (point-at-eol)))
256 'read-only "Type `e' to edit property")))))
258 (defun org-columns-add-ellipses (string width)
259 "Truncate STRING with WIDTH characters, with ellipses."
261 ((<= (length string) width) string)
262 ((<= width (length org-columns-ellipses))
263 (substring org-columns-ellipses 0 width))
264 (t (concat (substring string 0 (- width (length org-columns-ellipses)))
265 org-columns-ellipses))))
267 (defvar org-columns-full-header-line-format nil
268 "The full header line format, will be shifted by horizontal scrolling." )
269 (defvar org-previous-header-line-format nil
270 "The header line format before column view was turned on.")
271 (defvar org-columns-inhibit-recalculation nil
272 "Inhibit recomputing of columns on column view startup.")
273 (defvar org-columns-flyspell-was-active nil
274 "Remember the state of `flyspell-mode' before column view.
275 Flyspell-mode can cause problems in columns view, so it is turned off
276 for the duration of the command.")
278 (defvar header-line-format)
279 (defvar org-columns-previous-hscroll 0)
281 (defun org-columns-display-here-title ()
282 "Overlay the newline before the current line with the table title."
284 (let ((fmt org-columns-current-fmt-compiled)
286 property width f column str widths)
287 (while (setq column (pop fmt))
288 (setq property (car column)
289 str (or (nth 1 column) property)
290 width (or (cdr (assoc property org-columns-current-maxwidths))
293 widths (push width widths)
294 f (format "%%-%d.%ds | " width width)
295 string (format f str)
296 title (concat title string)))
298 (org-add-props " " nil 'display '(space :align-to 0))
299 ;;(org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
300 (org-add-props title nil 'face 'org-column-title)))
301 (org-set-local 'org-previous-header-line-format header-line-format)
302 (org-set-local 'org-columns-current-widths (nreverse widths))
303 (setq org-columns-full-header-line-format title)
304 (setq org-columns-previous-hscroll -1)
305 ; (org-columns-hscoll-title)
306 (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
308 (defun org-columns-hscoll-title ()
309 "Set the `header-line-format' so that it scrolls along with the table."
310 (sit-for .0001) ; need to force a redisplay to update window-hscroll
311 (when (not (= (window-hscroll) org-columns-previous-hscroll))
312 (setq header-line-format
313 (concat (substring org-columns-full-header-line-format 0 1)
314 (substring org-columns-full-header-line-format
315 (1+ (window-hscroll))))
316 org-columns-previous-hscroll (window-hscroll))
317 (force-mode-line-update)))
319 (defvar org-colview-initial-truncate-line-value nil
320 "Remember the value of `truncate-lines' across colview.")
322 (defun org-columns-remove-overlays ()
323 "Remove all currently active column overlays."
325 (when (marker-buffer org-columns-begin-marker)
326 (with-current-buffer (marker-buffer org-columns-begin-marker)
327 (when (local-variable-p 'org-previous-header-line-format)
328 (setq header-line-format org-previous-header-line-format)
329 (kill-local-variable 'org-previous-header-line-format)
330 (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
331 (move-marker org-columns-begin-marker nil)
332 (move-marker org-columns-top-level-marker nil)
334 (mapc 'delete-overlay org-columns-overlays)
335 (setq org-columns-overlays nil)
336 (let ((inhibit-read-only t))
337 (remove-text-properties (point-min) (point-max) '(read-only t))))
338 (when org-columns-flyspell-was-active
340 (when (local-variable-p 'org-colview-initial-truncate-line-value)
341 (setq truncate-lines org-colview-initial-truncate-line-value)))))
343 (defun org-columns-cleanup-item (item fmt cphr)
344 "Remove from ITEM what is a column in the format FMT.
345 CPHR is the complex heading regexp to use for parsing ITEM."
349 (unless (string-match "^\*+ " item)
350 (setq item (concat "* " item) fixitem t))
351 (if (string-match cphr item)
354 (org-add-props (match-string 1 item) nil
355 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
356 (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
357 (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
358 " " (save-match-data (org-columns-compact-links (match-string 4 item)))
359 (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))
362 (list 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
364 (if fixitem (replace-regexp-in-string "^\*+ " "" item) item))))
366 (defun org-columns-compact-links (s)
367 "Replace [[link][desc]] with [desc] or [link]."
368 (while (string-match org-bracket-link-regexp s)
369 (setq s (replace-match
370 (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
374 (defun org-columns-show-value ()
375 "Show the full value of the property."
377 (let ((value (get-char-property (point) 'org-columns-value)))
378 (message "Value is: %s" (or value ""))))
380 (defvar org-agenda-columns-active) ;; defined in org-agenda.el
382 (defun org-columns-quit ()
383 "Remove the column overlays and in this way exit column editing."
386 (org-columns-remove-overlays)
387 (let ((inhibit-read-only t))
388 (remove-text-properties (point-min) (point-max) '(read-only t))))
389 (when (eq major-mode 'org-agenda-mode)
390 (setq org-agenda-columns-active nil)
392 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
394 (defun org-columns-check-computed ()
395 "Check if this column value is computed.
396 If yes, throw an error indicating that changing it does not make sense."
397 (let ((val (get-char-property (point) 'org-columns-value)))
398 (when (and (stringp val)
399 (get-char-property 0 'org-computed val))
400 (error "This value is computed from the entry's children"))))
402 (defun org-columns-todo (&optional arg)
403 "Change the TODO state during column view."
405 (org-columns-edit-value "TODO"))
407 (defun org-columns-set-tags-or-toggle (&optional arg)
408 "Toggle checkbox at point, or set tags for current headline."
410 (if (string-match "\\`\\[[ xX-]\\]\\'"
411 (get-char-property (point) 'org-columns-value))
412 (org-columns-next-allowed-value)
413 (org-columns-edit-value "TAGS")))
415 (defun org-columns-edit-value (&optional key)
416 "Edit the value of the property at point in column view.
417 Where possible, use the standard interface for changing this line."
419 (org-columns-check-computed)
420 (let* ((col (current-column))
421 (key (or key (get-char-property (point) 'org-columns-key)))
422 (value (get-char-property (point) 'org-columns-value))
423 (bol (point-at-bol)) (eol (point-at-eol))
424 (pom (or (get-text-property bol 'org-hd-marker)
425 (point))) ; keep despite of compiler waring
427 (delq nil (mapcar (lambda (x)
428 (and (eq (overlay-buffer x) (current-buffer))
429 (>= (overlay-start x) bol)
430 (<= (overlay-start x) eol)
432 org-columns-overlays)))
433 (org-columns-time (time-to-number-of-days (current-time)))
436 ((equal key "CLOCKSUM")
437 (error "This special column cannot be edited"))
439 (setq eval '(org-with-point-at pom
440 (org-edit-headline))))
442 (setq eval '(org-with-point-at
444 (call-interactively 'org-todo))))
445 ((equal key "PRIORITY")
446 (setq eval '(org-with-point-at pom
447 (call-interactively 'org-priority))))
449 (setq eval '(org-with-point-at pom
450 (let ((org-fast-tag-selection-single-key
451 (if (eq org-fast-tag-selection-single-key 'expert)
452 t org-fast-tag-selection-single-key)))
453 (call-interactively 'org-set-tags)))))
454 ((equal key "DEADLINE")
455 (setq eval '(org-with-point-at pom
456 (call-interactively 'org-deadline))))
457 ((equal key "SCHEDULED")
458 (setq eval '(org-with-point-at pom
459 (call-interactively 'org-schedule))))
460 ((equal key "BEAMER_env")
461 (setq eval '(org-with-point-at pom
462 (call-interactively 'org-beamer-select-environment))))
464 (setq allowed (org-property-get-allowed-values pom key 'table))
466 (setq nval (org-icompleting-read
467 "Value: " allowed nil
468 (not (get-text-property 0 'org-unrestricted
470 (setq nval (read-string "Edit: " value)))
471 (setq nval (org-trim nval))
472 (when (not (equal nval value))
473 (setq eval '(org-entry-put pom key nval)))))
477 ((equal major-mode 'org-agenda-mode)
478 (org-columns-eval eval)
479 ;; The following let preserves the current format, and makes sure
480 ;; that in only a single file things need to be upated.
481 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
482 (buffer (marker-buffer pom))
483 (org-agenda-contributing-files
484 (list (with-current-buffer buffer
485 (buffer-file-name (buffer-base-buffer))))))
486 (org-agenda-columns)))
488 (let ((inhibit-read-only t))
490 (remove-text-properties
491 (max (point-min) (1- bol)) eol '(read-only t)))
494 (setq org-columns-overlays
495 (org-delete-all line-overlays org-columns-overlays))
496 (mapc 'delete-overlay line-overlays)
497 (org-columns-eval eval))
498 (org-columns-display-here)))
499 (org-move-to-column col)
500 (if (and (eq major-mode 'org-mode)
501 (nth 3 (assoc key org-columns-current-fmt-compiled)))
502 (org-columns-update key)))))))
504 (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
505 "Edit the current headline, the part without TODO keyword, TAGS."
506 (org-back-to-heading)
507 (when (looking-at org-todo-line-regexp)
509 (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
510 (txt (match-string 3))
513 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@#%]+:[ \t]*$") txt)
514 (setq post (match-string 0 txt)
515 txt (substring txt 0 (match-beginning 0))))
516 (setq txt2 (read-string "Edit: " txt))
517 (when (not (equal txt txt2))
519 (insert pre txt2 post)
520 (delete-region (point) (point-at-eol))
521 (org-set-tags nil t)))))
523 (defun org-columns-edit-allowed ()
524 "Edit the list of allowed values for the current property."
526 (let* ((pom (or (org-get-at-bol 'org-marker)
527 (org-get-at-bol 'org-hd-marker)
529 (key (get-char-property (point) 'org-columns-key))
530 (key1 (concat key "_ALL"))
531 (allowed (org-entry-get pom key1 t))
533 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
534 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
535 (setq nval (read-string "Allowed: " allowed))
537 (cond ((marker-position org-entry-property-inherited-from)
538 org-entry-property-inherited-from)
539 ((marker-position org-columns-top-level-marker)
540 org-columns-top-level-marker)
544 (defun org-columns-eval (form)
547 (beginning-of-line 1)
548 ;; `next-line' is needed here, because it skips invisible line.
549 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
550 (setq hidep (org-on-heading-p 1)))
552 (and hidep (hide-entry))))
554 (defun org-columns-previous-allowed-value ()
555 "Switch to the previous allowed value for this column."
557 (org-columns-next-allowed-value t))
559 (defun org-columns-next-allowed-value (&optional previous nth)
560 "Switch to the next allowed value for this column.
561 When PREVIOUS is set, go to the previous value. When NTH is
562 an integer, select that value."
564 (org-columns-check-computed)
565 (let* ((col (current-column))
566 (key (get-char-property (point) 'org-columns-key))
567 (value (get-char-property (point) 'org-columns-value))
568 (bol (point-at-bol)) (eol (point-at-eol))
569 (pom (or (get-text-property bol 'org-hd-marker)
570 (point))) ; keep despite of compiler waring
572 (delq nil (mapcar (lambda (x)
573 (and (eq (overlay-buffer x) (current-buffer))
574 (>= (overlay-start x) bol)
575 (<= (overlay-start x) eol)
577 org-columns-overlays)))
578 (allowed (or (org-property-get-allowed-values pom key)
580 (nth 4 (assoc key org-columns-current-fmt-compiled))
581 '(checkbox checkbox-n-of-m checkbox-percent))
583 (org-colview-construct-allowed-dates value)))
587 (if (= nth -1) (setq nth 9)))
588 (when (equal key "ITEM")
589 (error "Cannot edit item headline from here"))
590 (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
591 (error "Allowed values for this property have not been defined"))
592 (if (member key '("SCHEDULED" "DEADLINE"))
593 (setq nval (if previous 'earlier 'later))
594 (if previous (setq allowed (reverse allowed)))
597 (setq nval (nth nth allowed))
599 (error "There are only %d allowed values for property `%s'"
600 (length allowed) key)))
601 ((member value allowed)
602 (setq nval (or (car (cdr (member value allowed)))
604 (if (equal nval value)
605 (error "Only one allowed value for this property")))
606 (t (setq nval (car allowed)))))
608 ((equal major-mode 'org-agenda-mode)
609 (org-columns-eval '(org-entry-put pom key nval))
610 ;; The following let preserves the current format, and makes sure
611 ;; that in only a single file things need to be upated.
612 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
613 (buffer (marker-buffer pom))
614 (org-agenda-contributing-files
615 (list (with-current-buffer buffer
616 (buffer-file-name (buffer-base-buffer))))))
617 (org-agenda-columns)))
619 (let ((inhibit-read-only t))
620 (remove-text-properties (1- bol) eol '(read-only t))
623 (setq org-columns-overlays
624 (org-delete-all line-overlays org-columns-overlays))
625 (mapc 'delete-overlay line-overlays)
626 (org-columns-eval '(org-entry-put pom key nval)))
627 (org-columns-display-here)))
628 (org-move-to-column col)
629 (and (nth 3 (assoc key org-columns-current-fmt-compiled))
630 (org-columns-update key))))))
632 (defun org-colview-construct-allowed-dates (s)
633 "Construct a list of three dates around the date in S.
634 This respects the format of the time stamp in S, active or non-active,
635 and also including time or not. S must be just a time stamp, no text
637 (when (and s (string-match (concat "^" org-ts-regexp3 "$") s))
638 (let* ((time (org-parse-time-string s 'nodefaults))
639 (active (equal (string-to-char s) ?<))
640 (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
641 time-before time-after)
642 (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
643 (setf (car time) (or (car time) 0))
644 (setf (nth 1 time) (or (nth 1 time) 0))
645 (setf (nth 2 time) (or (nth 2 time) 0))
646 (setq time-before (copy-sequence time))
647 (setq time-after (copy-sequence time))
648 (setf (nth 3 time-before) (1- (nth 3 time)))
649 (setf (nth 3 time-after) (1+ (nth 3 time)))
650 (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
651 (list time-before time time-after)))))
653 (defun org-verify-version (task)
656 (if (or (featurep 'xemacs)
657 (< emacs-major-version 22))
658 (error "Emacs 22 is required for the columns feature")))))
660 (defun org-columns-open-link (&optional arg)
662 (let ((value (get-char-property (point) 'org-columns-value)))
663 (org-open-link-from-string value arg)))
665 (defun org-columns-get-format-and-top-level ()
667 (when (condition-case nil (org-back-to-heading) (error nil))
668 (setq fmt (org-entry-get nil "COLUMNS" t)))
669 (setq fmt (or fmt org-columns-default-format))
670 (org-set-local 'org-columns-current-fmt fmt)
671 (org-columns-compile-format fmt)
672 (if (marker-position org-entry-property-inherited-from)
673 (move-marker org-columns-top-level-marker
674 org-entry-property-inherited-from)
675 (move-marker org-columns-top-level-marker (point)))
678 (defun org-columns ()
679 "Turn on column view on an org-mode file."
681 (org-verify-version 'columns)
682 (org-columns-remove-overlays)
683 (move-marker org-columns-begin-marker (point))
684 (let ((org-columns-time (time-to-number-of-days (current-time)))
685 beg end fmt cache maxwidths)
686 (setq fmt (org-columns-get-format-and-top-level))
688 (goto-char org-columns-top-level-marker)
690 (unless org-columns-inhibit-recalculation
691 (org-columns-compute-all))
692 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
694 ;; Get and cache the properties
696 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
699 (narrow-to-region beg end)
701 (while (re-search-forward org-outline-regexp-bol end t)
702 (if (and org-columns-skip-archived-trees
703 (looking-at (concat ".*:" org-archive-tag ":")))
704 (org-end-of-subtree t)
705 (push (cons (org-current-line) (org-entry-properties)) cache)))
707 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
708 (org-set-local 'org-columns-current-maxwidths maxwidths)
709 (org-columns-display-here-title)
710 (when (org-set-local 'org-columns-flyspell-was-active
711 (org-bound-and-true-p flyspell-mode))
713 (unless (local-variable-p 'org-colview-initial-truncate-line-value)
714 (org-set-local 'org-colview-initial-truncate-line-value
716 (setq truncate-lines t)
718 (org-goto-line (car x))
719 (org-columns-display-here (cdr x)))
722 (eval-when-compile (defvar org-columns-time))
724 (defvar org-columns-compile-map
730 ("X/" checkbox-n-of-m +)
731 ("X%" checkbox-percent +)
732 ("max" max_numbers max)
733 ("min" min_numbers min)
735 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
736 (":max" max_times max)
737 (":min" min_times min)
739 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
740 ("@min" min_age min (lambda (x) (- org-columns-time x)))
741 ("@max" max_age max (lambda (x) (- org-columns-time x)))
743 (lambda (&rest x) (/ (apply '+ x) (float (length x))))
744 (lambda (x) (- org-columns-time x)))
745 ("est+" estimate org-estimate-combine))
746 "Operator <-> format,function,calc map.
747 Used to compile/uncompile columns format and completing read in
748 interactive function `org-columns-new'.
750 operator string used in #+COLUMNS definition describing the
752 format symbol describing summary type selected interactively in
753 `org-columns-new' and internally in
754 `org-columns-number-to-string' and
755 `org-columns-string-to-number'
756 function called with a list of values as argument to calculate
758 calc function called on every element before summarizing. This is
759 optional and should only be specified if needed")
761 (defun org-columns-new (&optional prop title width op fmt fun &rest rest)
762 "Insert a new column, to the left of the current column."
764 (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
766 (setq prop (org-icompleting-read
767 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
769 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
770 (setq width (read-string "Column width: " (if width (number-to-string width))))
771 (if (string-match "\\S-" width)
772 (setq width (string-to-number width))
774 (setq fmt (org-icompleting-read
776 (mapcar (lambda (x) (list (symbol-name (cadr x))))
777 org-columns-compile-map)
779 (setq fmt (intern fmt)
780 fun (cdr (assoc fmt (mapcar 'cdr org-columns-compile-map))))
781 (if (eq fmt 'none) (setq fmt nil))
785 (setcdr editp (list title width nil fmt nil fun)))
786 (setq cell (nthcdr (1- (current-column))
787 org-columns-current-fmt-compiled))
788 (setcdr cell (cons (list prop title width nil fmt nil
789 (car fun) (cadr fun))
791 (org-columns-store-format)
794 (defun org-columns-delete ()
795 "Delete the column at point from columns view."
797 (let* ((n (current-column))
798 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
800 (format "Are you sure you want to remove column \"%s\"? " title))
801 (setq org-columns-current-fmt-compiled
802 (delq (nth n org-columns-current-fmt-compiled)
803 org-columns-current-fmt-compiled))
804 (org-columns-store-format)
806 (if (>= (current-column) (length org-columns-current-fmt-compiled))
807 (backward-char 1)))))
809 (defun org-columns-edit-attributes ()
810 "Edit the attributes of the current column."
812 (let* ((n (current-column))
813 (info (nth n org-columns-current-fmt-compiled)))
814 (apply 'org-columns-new info)))
816 (defun org-columns-widen (arg)
817 "Make the column wider by ARG characters."
819 (let* ((n (current-column))
820 (entry (nth n org-columns-current-fmt-compiled))
821 (width (or (nth 2 entry)
822 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
823 (setq width (max 1 (+ width arg)))
824 (setcar (nthcdr 2 entry) width)
825 (org-columns-store-format)
828 (defun org-columns-narrow (arg)
829 "Make the column narrower by ARG characters."
831 (org-columns-widen (- arg)))
833 (defun org-columns-move-right ()
834 "Swap this column with the one to the right."
836 (let* ((n (current-column))
837 (cell (nthcdr n org-columns-current-fmt-compiled))
839 (when (>= n (1- (length org-columns-current-fmt-compiled)))
840 (error "Cannot shift this column further to the right"))
842 (setcar cell (car (cdr cell)))
843 (setcdr cell (cons e (cdr (cdr cell))))
844 (org-columns-store-format)
848 (defun org-columns-move-left ()
849 "Swap this column with the one to the left."
851 (let* ((n (current-column)))
853 (error "Cannot shift this column further to the left"))
855 (org-columns-move-right)
858 (defun org-columns-store-format ()
859 "Store the text version of the current columns format in appropriate place.
860 This is either in the COLUMNS property of the node starting the current column
861 display, or in the #+COLUMNS line of the current buffer."
863 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
864 (org-set-local 'org-columns-current-fmt fmt)
865 (if (marker-position org-columns-top-level-marker)
867 (goto-char org-columns-top-level-marker)
868 (if (and (org-at-heading-p)
869 (org-entry-get nil "COLUMNS"))
870 (org-entry-put nil "COLUMNS" fmt)
871 (goto-char (point-min))
872 ;; Overwrite all #+COLUMNS lines....
873 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
875 (replace-match (concat "#+COLUMNS: " fmt) t t))
877 (goto-char (point-min))
878 (or (org-on-heading-p t) (outline-next-heading))
879 (let ((inhibit-read-only t))
880 (insert-before-markers "#+COLUMNS: " fmt "\n")))
881 (org-set-local 'org-columns-default-format fmt))))))
883 (defvar org-agenda-overriding-columns-format nil
884 "When set, overrides any other format definition for the agenda.
885 Don't set this, this is meant for dynamic scoping.")
887 (defun org-columns-get-autowidth-alist (s cache)
888 "Derive the maximum column widths from the format and the cache."
890 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
891 (push (cons (match-string 1 s) 1) rtn)
892 (setq start (match-end 0)))
894 (setcdr x (apply 'max
897 (length (or (cdr (assoc (car x) (cdr y))) " ")))
902 (defun org-columns-compute-all ()
903 "Compute all columns that have operators defined."
905 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
906 (let ((columns org-columns-current-fmt-compiled)
907 (org-columns-time (time-to-number-of-days (current-time)))
909 (while (setq col (pop columns))
912 (org-columns-compute (car col)))))))
914 (defun org-columns-update (property)
915 "Recompute PROPERTY, and update the columns display for it."
916 (org-columns-compute property)
920 (when (equal (overlay-get ov 'org-columns-key) property)
921 (setq pos (overlay-start ov))
923 (when (setq val (cdr (assoc property
925 (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 (defun org-columns-compute (property)
932 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
934 (let* ((re org-outline-regexp-bol)
935 (lmax 30) ; Does anyone use deeper levels???
936 (lvals (make-vector lmax nil))
937 (lflag (make-vector lmax nil))
939 (ass (assoc property org-columns-current-fmt-compiled))
943 (calc (or (nth 7 ass) 'identity))
944 (beg org-columns-top-level-marker)
945 last-level val valflag flag end sumpos sum-alist sum str str1 useval)
947 ;; Find the region to compute
949 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
951 ;; Walk the tree from the back and do the computations
952 (while (re-search-backward re beg t)
953 (setq sumpos (match-beginning 0)
955 level (org-outline-level)
956 val (org-entry-get nil property)
957 valflag (and val (string-match "\\S-" val)))
959 ((< level last-level)
960 ;; put the sum of lower levels here as a property
961 (setq sum (when (aref lvals last-level)
962 (apply fun (aref lvals last-level)))
963 flag (aref lflag last-level) ; any valid entries from children?
964 str (org-columns-number-to-string sum format printf)
965 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
966 useval (if flag str1 (if valflag val ""))
967 sum-alist (get-text-property sumpos 'org-summaries))
968 (if (assoc property sum-alist)
969 (setcdr (assoc property sum-alist) useval)
970 (push (cons property useval) sum-alist)
972 (add-text-properties sumpos (1+ sumpos)
973 (list 'org-summaries sum-alist))))
974 (when (and val (not (equal val (if flag str val))))
975 (org-entry-put nil property (if flag str val)))
976 ;; add current to current level accumulator
977 (when (or flag valflag)
980 (funcall calc (org-columns-string-to-number
981 (if flag str val) format)))
983 (aset lflag level t))
984 ;; clear accumulators for deeper levels
985 (loop for l from (1+ level) to (1- lmax) do
988 ((>= level last-level)
989 ;; add what we have here to the accumulator for this level
991 (push (funcall calc (org-columns-string-to-number val format))
993 (aset lflag level t)))
994 (t (error "This should not happen")))))))
996 (defun org-columns-redo ()
997 "Construct the column display again."
999 (message "Recomputing columns...")
1000 (let ((line (org-current-line))
1001 (col (current-column)))
1003 (if (marker-position org-columns-begin-marker)
1004 (goto-char org-columns-begin-marker))
1005 (org-columns-remove-overlays)
1006 (if (eq major-mode 'org-mode)
1007 (call-interactively 'org-columns)
1009 (call-interactively 'org-agenda-columns)))
1010 (org-goto-line line)
1011 (move-to-column col))
1012 (message "Recomputing columns...done"))
1014 (defun org-columns-not-in-agenda ()
1015 (if (eq major-mode 'org-agenda-mode)
1016 (error "This command is only allowed in Org-mode buffers")))
1018 (defun org-string-to-number (s)
1019 "Convert string to number, and interpret hh:mm:ss."
1020 (if (not (string-match ":" s))
1021 (string-to-number s)
1022 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1024 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1027 (defun org-columns-number-to-string (n fmt &optional printf)
1028 "Convert a computed column number to a string value, according to FMT."
1030 ((memq fmt '(estimate)) (org-estimate-print n printf))
1031 ((not (numberp n)) "")
1032 ((memq fmt '(add_times max_times min_times mean_times))
1033 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
1034 (format org-time-clocksum-format h m)))
1036 (cond ((= n (floor n)) "[X]")
1039 ((memq fmt '(checkbox-n-of-m checkbox-percent))
1040 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
1041 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
1042 (printf (format printf n))
1045 ((memq fmt '(min_age max_age mean_age))
1046 (org-format-time-period n))
1047 (t (number-to-string n))))
1049 (defun org-nofm-to-completion (n m &optional percent)
1051 (format "[%d/%d]" n m)
1052 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
1055 (defun org-columns-string-to-number (s fmt)
1056 "Convert a column value to a number that can be used for column computing."
1059 ((memq fmt '(min_age max_age mean_age))
1060 (cond ((string= s "") org-columns-time)
1062 "\\([0-9]+\\)d \\([0-9]+\\)h \\([0-9]+\\)m \\([0-9]+\\)s"
1064 (+ (* 60 (+ (* 60 (+ (* 24 (string-to-number (match-string 1 s)))
1065 (string-to-number (match-string 2 s))))
1066 (string-to-number (match-string 3 s))))
1067 (string-to-number (match-string 4 s))))
1068 (t (time-to-number-of-days (apply 'encode-time
1069 (org-parse-time-string s t))))))
1070 ((string-match ":" s)
1071 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1073 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1075 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
1076 (if (equal s "[X]") 1. 0.000001))
1077 ((memq fmt '(estimate)) (org-string-to-estimate s))
1078 (t (string-to-number s)))))
1080 (defun org-columns-uncompile-format (cfmt)
1081 "Turn the compiled columns format back into a string representation."
1082 (let ((rtn "") e s prop title op op-match width fmt printf fun calc)
1083 (while (setq e (pop cfmt))
1092 (when (setq op-match (rassoc (list fmt fun calc) org-columns-compile-map))
1093 (setq op (car op-match)))
1094 (if (and op printf) (setq op (concat op ";" printf)))
1095 (if (equal title prop) (setq title nil))
1096 (setq s (concat "%" (if width (number-to-string width))
1098 (if title (concat "(" title ")"))
1099 (if op (concat "{" op "}"))))
1100 (setq rtn (concat rtn " " s)))
1103 (defun org-columns-compile-format (fmt)
1104 "Turn a column format string into an alist of specifications.
1105 The alist has one entry for each column in the format. The elements of
1107 property the property
1108 title the title field for the columns
1109 width the column width in characters, can be nil for automatic
1110 operator the operator if any
1111 format the output format for computed results, derived from operator
1112 printf a printf format for computed values
1113 fun the lisp function to compute summary values, derived from operator
1114 calc function to get values from base elements"
1115 (let ((start 0) width prop title op op-match f printf fun calc)
1116 (setq org-columns-current-fmt-compiled nil)
1117 (while (string-match
1118 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
1120 (setq start (match-end 0)
1121 width (match-string 1 fmt)
1122 prop (match-string 2 fmt)
1123 title (or (match-string 3 fmt) prop)
1124 op (match-string 4 fmt)
1129 (if width (setq width (string-to-number width)))
1130 (when (and op (string-match ";" op))
1131 (setq printf (substring op (match-end 0))
1132 op (substring op 0 (match-beginning 0))))
1133 (when (setq op-match (assoc op org-columns-compile-map))
1134 (setq f (cadr op-match)
1135 fun (caddr op-match)
1136 calc (cadddr op-match)))
1137 (push (list prop title width op f printf fun calc)
1138 org-columns-current-fmt-compiled))
1139 (setq org-columns-current-fmt-compiled
1140 (nreverse org-columns-current-fmt-compiled))))
1143 ;;; Dynamic block for Column view
1145 (defvar org-heading-regexp) ; defined in org.el
1146 (defvar org-heading-keyword-regexp-format) ; defined in org.el
1147 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
1148 "Get the column view of the current buffer or subtree.
1149 The first optional argument MAXLEVEL sets the level limit. A
1150 second optional argument SKIP-EMPTY-ROWS tells whether to skip
1151 empty rows, an empty row being one where all the column view
1152 specifiers except ITEM are empty. This function returns a list
1153 containing the title row and all other rows. Each row is a list
1156 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1157 (re-comment (format org-heading-keyword-regexp-format
1158 org-comment-string))
1159 (re-archive (concat ".*:" org-archive-tag ":"))
1160 (n (length title)) row tbl)
1161 (goto-char (point-min))
1162 (while (re-search-forward org-heading-regexp nil t)
1164 (when (and (or (null maxlevel)
1166 (if org-odd-levels-only
1167 (/ (1+ (length (match-string 1))) 2)
1168 (length (match-string 1)))))
1169 (get-char-property (match-beginning 0) 'org-columns-key))
1170 (when (save-excursion
1171 (goto-char (point-at-bol))
1172 (or (looking-at re-comment)
1173 (looking-at re-archive)))
1174 (org-end-of-subtree t)
1177 (loop for i from 0 to (1- n) do
1180 (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
1181 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
1184 (setq row (nreverse row))
1185 (unless (and skip-empty-rows
1186 (eq 1 (length (delete "" (delete-dups (copy-sequence row))))))
1188 (append (list title 'hline) (nreverse tbl)))))
1190 (defun org-dblock-write:columnview (params)
1191 "Write the column view table.
1192 PARAMS is a property list of parameters:
1194 :width enforce same column widths with <N> specifiers.
1195 :id the :ID: property of the entry where the columns view
1196 should be built. When the symbol `local', call locally.
1197 When `global' call column view with the cursor at the beginning
1198 of the buffer (usually this means that the whole buffer switches
1199 to column view). When \"file:path/to/file.org\", invoke column
1200 view at the start of that file. Otherwise, the ID is located
1201 using `org-id-find'.
1202 :hlines When t, insert a hline before each item. When a number, insert
1203 a hline before each level <= that number.
1204 :vlines When t, make each column a colgroup to enforce vertical lines.
1205 :maxlevel When set to a number, don't capture headlines below this level.
1207 When t, skip rows where all specifiers other than ITEM are empty."
1208 (let ((pos (move-marker (make-marker) (point)))
1209 (hlines (plist-get params :hlines))
1210 (vlines (plist-get params :vlines))
1211 (maxlevel (plist-get params :maxlevel))
1212 (content-lines (org-split-string (plist-get params :content) "\n"))
1213 (skip-empty-rows (plist-get params :skip-empty-rows))
1214 tbl id idpos nfields tmp recalc line
1215 id-as-string view-file view-pos)
1216 (when (setq id (plist-get params :id))
1217 (setq id-as-string (cond ((numberp id) (number-to-string id))
1218 ((symbolp id) (symbol-name id))
1221 (cond ((not id) nil)
1222 ((eq id 'global) (setq view-pos (point-min)))
1224 ((string-match "^file:\\(.*\\)" id-as-string)
1225 (setq view-file (match-string 1 id-as-string)
1227 (unless (file-exists-p view-file)
1228 (error "No such file: \"%s\"" id-as-string)))
1229 ((setq idpos (org-find-entry-with-id id))
1230 (setq view-pos idpos))
1231 ((setq idpos (org-id-find id))
1232 (setq view-file (car idpos))
1233 (setq view-pos (cdr idpos)))
1234 (t (error "Cannot find entry with :ID: %s" id))))
1235 (with-current-buffer (if view-file
1236 (get-file-buffer view-file)
1241 (goto-char (or view-pos (point)))
1243 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1244 (setq nfields (length (car tbl)))
1245 (org-columns-quit))))
1247 (move-marker pos nil)
1249 (when (plist-get params :hlines)
1252 (if (eq (car tbl) 'hline)
1253 (push (pop tbl) tmp)
1254 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1255 (if (and (not (eq (car tmp) 'hline))
1257 (and (numberp hlines)
1258 (<= (- (match-end 1) (match-beginning 1))
1261 (push (pop tbl) tmp)))
1262 (setq tbl (nreverse tmp)))
1264 (setq tbl (mapcar (lambda (x)
1265 (if (eq 'hline x) x (cons "" x)))
1267 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1270 (while (string-match "^#" (car content-lines))
1271 (insert (pop content-lines) "\n")))
1272 (insert (org-listtable-to-string tbl))
1273 (when (plist-get params :width)
1274 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1275 org-columns-current-widths "|")))
1276 (while (setq line (pop content-lines))
1277 (when (string-match "^#" line)
1279 (when (string-match "^[ \t]*#\\+TBLFM" line)
1282 (progn (goto-char pos) (org-table-recalculate 'all))
1284 (org-table-align)))))
1286 (defun org-listtable-to-string (tbl)
1287 "Convert a listtable TBL to a string that contains the Org-mode table.
1288 The table still need to be aligned. The resulting string has no leading
1289 and tailing newline characters."
1294 (concat "|" (mapconcat 'identity x "|") "|"))
1295 ((eq x 'hline) "|-|")
1296 (t (error "Garbage in listtable: %s" x))))
1299 (defun org-insert-columns-dblock ()
1300 "Create a dynamic block capturing a column view table."
1302 (let ((defaults '(:name "columnview" :hlines 1))
1303 (id (org-icompleting-read
1304 "Capture columns (local, global, entry with :ID: property) [local]: "
1305 (append '(("global") ("local"))
1306 (mapcar 'list (org-property-values "ID"))))))
1307 (if (equal id "") (setq id 'local))
1308 (if (equal id "global") (setq id 'global))
1309 (setq defaults (append defaults (list :id id)))
1310 (org-create-dblock defaults)
1311 (org-update-dblock)))
1313 ;;; Column view in the agenda
1315 (defvar org-agenda-view-columns-initially nil
1316 "When set, switch to columns view immediately after creating the agenda.")
1318 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1319 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1320 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1322 (defun org-agenda-columns ()
1323 "Turn on or update column view in the agenda."
1325 (org-verify-version 'columns)
1326 (org-columns-remove-overlays)
1327 (move-marker org-columns-begin-marker (point))
1328 (let ((org-columns-time (time-to-number-of-days (current-time)))
1329 cache maxwidths m p a d fmt)
1331 ((and (boundp 'org-agenda-overriding-columns-format)
1332 org-agenda-overriding-columns-format)
1333 (setq fmt org-agenda-overriding-columns-format)
1334 (org-set-local 'org-agenda-overriding-columns-format fmt))
1335 ((setq m (org-get-at-bol 'org-hd-marker))
1336 (setq fmt (or (org-entry-get m "COLUMNS" t)
1337 (with-current-buffer (marker-buffer m)
1338 org-columns-default-format))))
1339 ((and (boundp 'org-columns-current-fmt)
1340 (local-variable-p 'org-columns-current-fmt)
1341 org-columns-current-fmt)
1342 (setq fmt org-columns-current-fmt))
1343 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
1344 (setq m (get-text-property m 'org-hd-marker))
1345 (setq fmt (or (org-entry-get m "COLUMNS" t)
1346 (with-current-buffer (marker-buffer m)
1347 org-columns-default-format)))))
1348 (setq fmt (or fmt org-columns-default-format))
1349 (org-set-local 'org-columns-current-fmt fmt)
1350 (org-columns-compile-format fmt)
1351 (when org-agenda-columns-compute-summary-properties
1352 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1354 ;; Get and cache the properties
1355 (goto-char (point-min))
1357 (when (setq m (or (org-get-at-bol 'org-hd-marker)
1358 (org-get-at-bol 'org-marker)))
1359 (setq p (org-entry-properties m))
1361 (when (or (not (setq a (assoc org-effort-property p)))
1362 (not (string-match "\\S-" (or (cdr a) ""))))
1363 ;; OK, the property is not defined. Use appointment duration?
1364 (when (and org-agenda-columns-add-appointments-to-effort-sum
1365 (setq d (get-text-property (point) 'duration)))
1366 (setq d (org-minutes-to-hh:mm-string d))
1367 (put-text-property 0 (length d) 'face 'org-warning d)
1368 (push (cons org-effort-property d) p)))
1369 (push (cons (org-current-line) p) cache))
1370 (beginning-of-line 2))
1372 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
1373 (org-set-local 'org-columns-current-maxwidths maxwidths)
1374 (org-columns-display-here-title)
1375 (when (org-set-local 'org-columns-flyspell-was-active
1376 (org-bound-and-true-p flyspell-mode))
1379 (org-goto-line (car x))
1380 (org-columns-display-here (cdr x)))
1382 (when org-agenda-columns-show-summaries
1383 (org-agenda-colview-summarize cache))))))
1385 (defun org-agenda-colview-summarize (cache)
1386 "Summarize the summarizable columns in column view in the agenda.
1387 This will add overlays to the date lines, to show the summary for each day."
1388 (let* ((fmt (mapcar (lambda (x)
1389 (if (equal (car x) "CLOCKSUM")
1390 (list "CLOCKSUM" (nth 1 x) (nth 2 x) ":" 'add_times
1393 org-columns-current-fmt-compiled))
1394 line c c1 stype calc sumfunc props lsum entries prop v title)
1396 (when (delq nil (mapcar 'cadr fmt))
1397 ;; OK, at least one summation column, it makes sense to try this
1398 (goto-char (point-max))
1400 (when (or (get-text-property (point) 'org-date-line)
1401 (eq (get-text-property (point) 'face)
1402 'org-agenda-structure))
1403 ;; OK, this is a date line that should be used
1404 (setq line (org-current-line))
1405 (setq entries nil c cache cache nil)
1406 (while (setq c1 (pop c))
1407 (if (> (car c1) line)
1410 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1411 ;; Compute the summaries for the properties we want,
1412 ;; set nil properties for the rest.
1413 (when (setq entries (mapcar 'cdr entries))
1421 calc (or (nth 7 f) 'identity))
1423 ((equal prop "ITEM")
1424 (cons prop (buffer-substring (point-at-bol)
1426 ((not stype) (cons prop ""))
1427 (t ;; do the summary
1430 (setq v (cdr (assoc prop x)))
1434 (if (not (get-text-property 0 'org-computed v))
1437 (org-columns-string-to-number
1440 (setq lsum (remove nil lsum))
1442 (cond ((> (length lsum) 1)
1443 (org-columns-number-to-string
1444 (apply sumfunc lsum) stype))
1445 ((eq (length lsum) 1)
1446 (org-columns-number-to-string
1449 (put-text-property 0 (length lsum) 'face 'bold lsum)
1450 (unless (eq calc 'identity)
1451 (put-text-property 0 (length lsum) 'org-computed t lsum))
1454 (org-columns-display-here props 'dateline)
1455 (org-set-local 'org-agenda-columns-active t)))
1456 (if (bobp) (throw 'exit t))
1457 (beginning-of-line 0))))))
1459 (defun org-agenda-colview-compute (fmt)
1460 "Compute the relevant columns in the contributing source buffers."
1461 (let ((files org-agenda-contributing-files)
1462 (org-columns-begin-marker (make-marker))
1463 (org-columns-top-level-marker (make-marker))
1465 (while (setq f (pop files))
1466 (setq b (find-buffer-visiting f))
1467 (with-current-buffer (or (buffer-base-buffer b) b)
1472 (remove-text-properties (point-min) (point-max)
1473 '(org-summaries t)))
1474 (goto-char (point-min))
1475 (org-columns-get-format-and-top-level)
1476 (while (setq fm (pop fmt))
1477 (if (equal (car fm) "CLOCKSUM")
1479 (when (and (nth 4 fm)
1480 (setq a (assoc (car fm)
1481 org-columns-current-fmt-compiled))
1482 (equal (nth 4 a) (nth 4 fm)))
1483 (org-columns-compute (car fm)))))))))))
1485 (defun org-format-time-period (interval)
1486 "Convert time in fractional days to days/hours/minutes/seconds."
1487 (if (numberp interval)
1488 (let* ((days (floor interval))
1489 (frac-hours (* 24 (- interval days)))
1490 (hours (floor frac-hours))
1491 (minutes (floor (* 60 (- frac-hours hours))))
1492 (seconds (floor (* 60 (- (* 60 (- frac-hours hours)) minutes)))))
1493 (format "%dd %02dh %02dm %02ds" days hours minutes seconds))
1496 (defun org-estimate-mean-and-var (v)
1497 "Return the mean and variance of an estimate."
1498 (let* ((low (float (car v)))
1499 (high (float (cadr v)))
1500 (mean (/ (+ low high) 2.0))
1501 (var (/ (+ (expt (- mean low) 2.0) (expt (- high mean) 2.0)) 2.0)))
1504 (defun org-estimate-combine (&rest el)
1505 "Combine a list of estimates, using mean and variance.
1506 The mean and variance of the result will be the sum of the means
1507 and variances (respectively) of the individual estimates."
1511 (let ((stats (org-estimate-mean-and-var e)))
1512 (setq mean (+ mean (car stats)))
1513 (setq var (+ var (cadr stats)))))
1515 (let ((stdev (sqrt var)))
1516 (list (- mean stdev) (+ mean stdev)))))
1518 (defun org-estimate-print (e &optional fmt)
1519 "Prepare a string representation of an estimate.
1520 This formats these numbers as two numbers with a \"-\" between them."
1521 (if (null fmt) (set 'fmt "%.0f"))
1522 (format "%s" (mapconcat (lambda (n) (format fmt n)) e "-")))
1524 (defun org-string-to-estimate (s)
1525 "Convert a string to an estimate.
1526 The string should be two numbers joined with a \"-\"."
1527 (if (string-match "\\(.*\\)-\\(.*\\)" s)
1528 (list (string-to-number (match-string 1 s))
1529 (string-to-number(match-string 2 s)))
1530 (list (string-to-number s) (string-to-number s))))
1532 (provide 'org-colview)
1534 ;;; org-colview.el ends here