1 ;;; org-colview.el --- Column View in Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;; This file contains the column view for Org.
33 (eval-when-compile (require 'cl
))
36 (declare-function org-agenda-redo
"org-agenda" ())
37 (declare-function org-agenda-do-context-action
"org-agenda" ())
39 (when (featurep 'xemacs
)
40 (error "Do not load this file into XEmacs, use 'org-colview-xemacs.el'."))
44 (defvar org-columns-overlays nil
45 "Holds the list of current column overlays.")
47 (defvar org-columns-current-fmt nil
48 "Local variable, holds the currently active column format.")
49 (make-variable-buffer-local 'org-columns-current-fmt
)
50 (defvar org-columns-current-fmt-compiled nil
51 "Local variable, holds the currently active column format.
52 This is the compiled version of the format.")
53 (make-variable-buffer-local 'org-columns-current-fmt-compiled
)
54 (defvar org-columns-current-widths nil
55 "Loval variable, holds the currently widths of fields.")
56 (make-variable-buffer-local 'org-columns-current-widths
)
57 (defvar org-columns-current-maxwidths nil
58 "Loval variable, holds the currently active maximum column widths.")
59 (make-variable-buffer-local 'org-columns-current-maxwidths
)
60 (defvar org-columns-begin-marker
(make-marker)
61 "Points to the position where last a column creation command was called.")
62 (defvar org-columns-top-level-marker
(make-marker)
63 "Points to the position where current columns region starts.")
65 (defvar org-columns-map
(make-sparse-keymap)
66 "The keymap valid in column display.")
68 (defun org-columns-content ()
69 "Switch to contents view while in columns view."
74 (org-defkey org-columns-map
"c" 'org-columns-content
)
75 (org-defkey org-columns-map
"o" 'org-overview
)
76 (org-defkey org-columns-map
"e" 'org-columns-edit-value
)
77 (org-defkey org-columns-map
"\C-c\C-t" 'org-columns-todo
)
78 (org-defkey org-columns-map
"\C-c\C-c" 'org-columns-set-tags-or-toggle
)
79 (org-defkey org-columns-map
"\C-c\C-o" 'org-columns-open-link
)
80 (org-defkey org-columns-map
"v" 'org-columns-show-value
)
81 (org-defkey org-columns-map
"q" 'org-columns-quit
)
82 (org-defkey org-columns-map
"r" 'org-columns-redo
)
83 (org-defkey org-columns-map
"g" 'org-columns-redo
)
84 (org-defkey org-columns-map
[left] 'backward-char)
85 (org-defkey org-columns-map "\M-b" 'backward-char)
86 (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
87 (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
88 (org-defkey org-columns-map "\M-f"
89 (lambda () (interactive) (goto-char (1+ (point)))))
90 (org-defkey org-columns-map [right]
91 (lambda () (interactive) (goto-char (1+ (point)))))
92 (org-defkey org-columns-map [down]
93 (lambda () (interactive)
94 (let ((col (current-column)))
96 (while (and (org-invisible-p2) (not (eobp)))
97 (beginning-of-line 2))
99 (if (eq major-mode 'org-agenda-mode)
100 (org-agenda-do-context-action)))))
101 (org-defkey org-columns-map [up]
102 (lambda () (interactive)
103 (let ((col (current-column)))
104 (beginning-of-line 0)
105 (while (and (org-invisible-p2) (not (bobp)))
106 (beginning-of-line 0))
108 (if (eq major-mode 'org-agenda-mode)
109 (org-agenda-do-context-action)))))
110 (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
111 (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
112 (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
113 (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
114 (org-defkey org-columns-map "<" 'org-columns-narrow)
115 (org-defkey org-columns-map ">" 'org-columns-widen)
116 (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
117 (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
118 (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
119 (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
121 (org-defkey org-columns-map (number-to-string i)
122 `(lambda () (interactive)
123 (org-columns-next-allowed-value nil ,i))))
125 (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
127 ["Edit property" org-columns-edit-value t]
128 ["Next allowed value" org-columns-next-allowed-value t]
129 ["Previous allowed value" org-columns-previous-allowed-value t]
130 ["Show full value" org-columns-show-value t]
131 ["Edit allowed values" org-columns-edit-allowed t]
133 ["Edit column attributes" org-columns-edit-attributes t]
134 ["Increase column width" org-columns-widen t]
135 ["Decrease column width" org-columns-narrow t]
137 ["Move column right" org-columns-move-right t]
138 ["Move column left" org-columns-move-left t]
139 ["Add column" org-columns-new t]
140 ["Delete column" org-columns-delete t]
142 ["CONTENTS" org-columns-content t]
143 ["OVERVIEW" org-overview t]
144 ["Refresh columns display" org-columns-redo t]
146 ["Open link" org-columns-open-link t]
148 ["Quit" org-columns-quit t]))
150 (defun org-columns-new-overlay (beg end &optional string face)
151 "Create a new column overlay and add it to the list."
152 (let ((ov (make-overlay beg end)))
153 (overlay-put ov 'face (or face 'secondary-selection))
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 (face (list color 'org-column ref-face))
173 (face1 (list color 'org-agenda-column-dateline ref-face))
174 (pl (or (get-text-property (point-at-bol) 'prefix-length) 0))
175 (cphr (get-text-property (point-at-bol) 'org-complex-heading-regexp))
176 pom property ass width f string ov column val modval s2 title calc)
177 ;; Check if the entry is in another buffer.
179 (if (eq major-mode 'org-agenda-mode)
180 (setq pom (or (org-get-at-bol 'org-hd-marker)
181 (org-get-at-bol 'org-marker))
182 props (if pom (org-entry-properties pom) nil))
183 (setq props (org-entry-properties nil))))
185 (while (setq column (pop fmt))
186 (setq property (car column)
188 ass (if (equal property "ITEM")
193 (buffer-substring-no-properties
194 (point-at-bol) (point-at-eol))))))
195 (assoc property props))
196 width (or (cdr (assoc property org-columns-current-maxwidths))
199 f (format "%%-%d.%ds | " width width)
201 val (or (cdr ass) "")
202 modval (cond ((and org-columns-modify-value-for-display-function
204 org-columns-modify-value-for-display-function))
205 (funcall org-columns-modify-value-for-display-function
207 ((equal property "ITEM")
209 (org-columns-cleanup-item
210 val org-columns-current-fmt-compiled)
211 (org-agenda-columns-cleanup-item
212 val pl cphr org-columns-current-fmt-compiled)))
213 ((and calc (functionp calc)
214 (not (string= val ""))
215 (not (get-text-property 0 'org-computed val)))
216 (org-columns-number-to-string
217 (funcall calc (org-columns-string-to-number
220 (setq s2 (org-columns-add-ellipses (or modval val) width))
221 (setq string (format f s2))
222 ;; Create the overlay
224 (setq ov (org-columns-new-overlay
225 beg (setq beg (1+ beg)) string (if dateline face1 face)))
226 (overlay-put ov 'keymap org-columns-map)
227 (overlay-put ov 'org-columns-key property)
228 (overlay-put ov 'org-columns-value (cdr ass))
229 (overlay-put ov 'org-columns-value-modified modval)
230 (overlay-put ov 'org-columns-pom pom)
231 (overlay-put ov 'org-columns-format f))
232 (if (or (not (char-after beg))
233 (equal (char-after beg) ?\n))
234 (let ((inhibit-read-only t))
237 (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
238 ;; Make the rest of the line disappear.
240 (setq ov (org-columns-new-overlay beg (point-at-eol)))
241 (overlay-put ov 'invisible t)
242 (overlay-put ov 'keymap org-columns-map)
243 (overlay-put ov 'intangible t)
244 (push ov org-columns-overlays)
245 (setq ov (make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
246 (overlay-put ov 'keymap org-columns-map)
247 (push ov org-columns-overlays)
248 (let ((inhibit-read-only t))
249 (put-text-property (max (point-min) (1- (point-at-bol)))
250 (min (point-max) (1+ (point-at-eol)))
251 'read-only "Type `e' to edit property")))))
253 (defun org-columns-add-ellipses (string width)
254 "Truncate STRING with WIDTH characters, with ellipses."
256 ((<= (length string) width) string)
257 ((<= width (length org-columns-ellipses))
258 (substring org-columns-ellipses 0 width))
259 (t (concat (substring string 0 (- width (length org-columns-ellipses)))
260 org-columns-ellipses))))
262 (defvar org-columns-full-header-line-format nil
263 "The full header line format, will be shifted by horizontal scrolling." )
264 (defvar org-previous-header-line-format nil
265 "The header line format before column view was turned on.")
266 (defvar org-columns-inhibit-recalculation nil
267 "Inhibit recomputing of columns on column view startup.")
268 (defvar org-columns-flyspell-was-active nil
269 "Remember the state of `flyspell-mode' before column view.
270 Flyspell-mode can cause problems in columns view, so it is turned off
271 for the duration of the command.")
273 (defvar header-line-format)
274 (defvar org-columns-previous-hscroll 0)
276 (defun org-columns-display-here-title ()
277 "Overlay the newline before the current line with the table title."
279 (let ((fmt org-columns-current-fmt-compiled)
281 property width f column str widths)
282 (while (setq column (pop fmt))
283 (setq property (car column)
284 str (or (nth 1 column) property)
285 width (or (cdr (assoc property org-columns-current-maxwidths))
288 widths (push width widths)
289 f (format "%%-%d.%ds | " width width)
290 string (format f str)
291 title (concat title string)))
293 (org-add-props " " nil 'display '(space :align-to 0))
294 ;;(org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
295 (org-add-props title nil 'face 'org-column-title)))
296 (org-set-local 'org-previous-header-line-format header-line-format)
297 (org-set-local 'org-columns-current-widths (nreverse widths))
298 (setq org-columns-full-header-line-format title)
299 (setq org-columns-previous-hscroll -1)
300 ; (org-columns-hscoll-title)
301 (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
303 (defun org-columns-hscoll-title ()
304 "Set the `header-line-format' so that it scrolls along with the table."
305 (sit-for .0001) ; need to force a redisplay to update window-hscroll
306 (when (not (= (window-hscroll) org-columns-previous-hscroll))
307 (setq header-line-format
308 (concat (substring org-columns-full-header-line-format 0 1)
309 (substring org-columns-full-header-line-format
310 (1+ (window-hscroll))))
311 org-columns-previous-hscroll (window-hscroll))
312 (force-mode-line-update)))
314 (defvar org-colview-initial-truncate-line-value nil
315 "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)
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-cleanup-item (item fmt)
339 "Remove from ITEM what is a column in the format FMT."
340 (if (not org-complex-heading-regexp)
342 (when (string-match org-complex-heading-regexp item)
345 (org-add-props (match-string 1 item) nil
346 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
347 (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
348 (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
349 " " (save-match-data (org-columns-compact-links (match-string 4 item)))
350 (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))
353 (list 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
357 (defun org-columns-compact-links (s)
358 "Replace [[link][desc]] with [desc] or [link]."
359 (while (string-match org-bracket-link-regexp s)
360 (setq s (replace-match
361 (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
365 (defvar org-agenda-columns-remove-prefix-from-item)
367 (defun org-agenda-columns-cleanup-item (item pl cphr fmt)
368 "Cleanup the time property for agenda column view.
369 See also the variable `org-agenda-columns-remove-prefix-from-item'."
370 (let* ((org-complex-heading-regexp cphr)
371 (prefix (substring item 0 pl))
372 (rest (substring item pl))
373 (fake (concat "* " rest))
374 (cleaned (org-trim (substring (org-columns-cleanup-item fake fmt) 1))))
375 (if org-agenda-columns-remove-prefix-from-item
377 (concat prefix cleaned))))
379 (defun org-columns-show-value ()
380 "Show the full value of the property."
382 (let ((value (get-char-property (point) 'org-columns-value)))
383 (message "Value is: %s" (or value ""))))
385 (defvar org-agenda-columns-active) ;; defined in org-agenda.el
387 (defun org-columns-quit ()
388 "Remove the column overlays and in this way exit column editing."
391 (org-columns-remove-overlays)
392 (let ((inhibit-read-only t))
393 (remove-text-properties (point-min) (point-max) '(read-only t))))
394 (when (eq major-mode 'org-agenda-mode)
395 (setq org-agenda-columns-active nil)
397 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
399 (defun org-columns-check-computed ()
400 "Check if this column value is computed.
401 If yes, throw an error indicating that changing it does not make sense."
402 (let ((val (get-char-property (point) 'org-columns-value)))
403 (when (and (stringp val)
404 (get-char-property 0 'org-computed val))
405 (error "This value is computed from the entry's children"))))
407 (defun org-columns-todo (&optional arg)
408 "Change the TODO state during column view."
410 (org-columns-edit-value "TODO"))
412 (defun org-columns-set-tags-or-toggle (&optional arg)
413 "Toggle checkbox at point, or set tags for current headline."
415 (if (string-match "\\`\\[[ xX-]\\]\\'"
416 (get-char-property (point) 'org-columns-value))
417 (org-columns-next-allowed-value)
418 (org-columns-edit-value "TAGS")))
420 (defun org-columns-edit-value (&optional key)
421 "Edit the value of the property at point in column view.
422 Where possible, use the standard interface for changing this line."
424 (org-columns-check-computed)
425 (let* ((col (current-column))
426 (key (or key (get-char-property (point) 'org-columns-key)))
427 (value (get-char-property (point) 'org-columns-value))
428 (bol (point-at-bol)) (eol (point-at-eol))
429 (pom (or (get-text-property bol 'org-hd-marker)
430 (point))) ; keep despite of compiler waring
432 (delq nil (mapcar (lambda (x)
433 (and (eq (overlay-buffer x) (current-buffer))
434 (>= (overlay-start x) bol)
435 (<= (overlay-start x) eol)
437 org-columns-overlays)))
438 (org-columns-time (time-to-number-of-days (current-time)))
441 ((equal key "CLOCKSUM")
442 (error "This special column cannot be edited"))
444 (setq eval '(org-with-point-at pom
445 (org-edit-headline))))
447 (setq eval '(org-with-point-at
449 (call-interactively 'org-todo))))
450 ((equal key "PRIORITY")
451 (setq eval '(org-with-point-at pom
452 (call-interactively 'org-priority))))
454 (setq eval '(org-with-point-at pom
455 (let ((org-fast-tag-selection-single-key
456 (if (eq org-fast-tag-selection-single-key 'expert)
457 t org-fast-tag-selection-single-key)))
458 (call-interactively 'org-set-tags)))))
459 ((equal key "DEADLINE")
460 (setq eval '(org-with-point-at pom
461 (call-interactively 'org-deadline))))
462 ((equal key "SCHEDULED")
463 (setq eval '(org-with-point-at pom
464 (call-interactively 'org-schedule))))
465 ((equal key "BEAMER_env")
466 (setq eval '(org-with-point-at pom
467 (call-interactively 'org-beamer-set-environment-tag))))
469 (setq allowed (org-property-get-allowed-values pom key 'table))
471 (setq nval (org-icompleting-read
472 "Value: " allowed nil
473 (not (get-text-property 0 'org-unrestricted
475 (setq nval (read-string "Edit: " value)))
476 (setq nval (org-trim nval))
477 (when (not (equal nval value))
478 (setq eval '(org-entry-put pom key nval)))))
482 ((equal major-mode 'org-agenda-mode)
483 (org-columns-eval eval)
484 ;; The following let preserves the current format, and makes sure
485 ;; that in only a single file things need to be upated.
486 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
487 (buffer (marker-buffer pom))
488 (org-agenda-contributing-files
489 (list (with-current-buffer buffer
490 (buffer-file-name (buffer-base-buffer))))))
491 (org-agenda-columns)))
493 (let ((inhibit-read-only t))
495 (remove-text-properties
496 (max (point-min) (1- bol)) eol '(read-only t)))
499 (setq org-columns-overlays
500 (org-delete-all line-overlays org-columns-overlays))
501 (mapc 'delete-overlay line-overlays)
502 (org-columns-eval eval))
503 (org-columns-display-here)))
504 (org-move-to-column col)
505 (if (and (org-mode-p)
506 (nth 3 (assoc key org-columns-current-fmt-compiled)))
507 (org-columns-update key)))))))
509 (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
510 "Edit the current headline, the part without TODO keyword, TAGS."
511 (org-back-to-heading)
512 (when (looking-at org-todo-line-regexp)
514 (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
515 (txt (match-string 3))
518 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
519 (setq post (match-string 0 txt)
520 txt (substring txt 0 (match-beginning 0))))
521 (setq txt2 (read-string "Edit: " txt))
522 (when (not (equal txt txt2))
524 (insert pre txt2 post)
525 (delete-region (point) (point-at-eol))
526 (org-set-tags nil t)))))
528 (defun org-columns-edit-allowed ()
529 "Edit the list of allowed values for the current property."
531 (let* ((pom (or (org-get-at-bol 'org-marker)
532 (org-get-at-bol 'org-hd-marker)
534 (key (get-char-property (point) 'org-columns-key))
535 (key1 (concat key "_ALL"))
536 (allowed (org-entry-get pom key1 t))
538 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
539 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
540 (setq nval (read-string "Allowed: " allowed))
542 (cond ((marker-position org-entry-property-inherited-from)
543 org-entry-property-inherited-from)
544 ((marker-position org-columns-top-level-marker)
545 org-columns-top-level-marker)
549 (defun org-columns-eval (form)
552 (beginning-of-line 1)
553 ;; `next-line' is needed here, because it skips invisible line.
554 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
555 (setq hidep (org-on-heading-p 1)))
557 (and hidep (hide-entry))))
559 (defun org-columns-previous-allowed-value ()
560 "Switch to the previous allowed value for this column."
562 (org-columns-next-allowed-value t))
564 (defun org-columns-next-allowed-value (&optional previous nth)
565 "Switch to the next allowed value for this column.
566 When PREVIOUS is set, go to the previous value. When NTH is
567 an integer, select that value."
569 (org-columns-check-computed)
570 (let* ((col (current-column))
571 (key (get-char-property (point) 'org-columns-key))
572 (value (get-char-property (point) 'org-columns-value))
573 (bol (point-at-bol)) (eol (point-at-eol))
574 (pom (or (get-text-property bol 'org-hd-marker)
575 (point))) ; keep despite of compiler waring
577 (delq nil (mapcar (lambda (x)
578 (and (eq (overlay-buffer x) (current-buffer))
579 (>= (overlay-start x) bol)
580 (<= (overlay-start x) eol)
582 org-columns-overlays)))
583 (allowed (or (org-property-get-allowed-values pom key)
585 (nth 4 (assoc key org-columns-current-fmt-compiled))
586 '(checkbox checkbox-n-of-m checkbox-percent))
588 (org-colview-construct-allowed-dates value)))
592 (if (= nth -1) (setq nth 9)))
593 (when (equal key "ITEM")
594 (error "Cannot edit item headline from here"))
595 (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
596 (error "Allowed values for this property have not been defined"))
597 (if (member key '("SCHEDULED" "DEADLINE"))
598 (setq nval (if previous 'earlier 'later))
599 (if previous (setq allowed (reverse allowed)))
602 (setq nval (nth nth allowed))
604 (error "There are only %d allowed values for property `%s'"
605 (length allowed) key)))
606 ((member value allowed)
607 (setq nval (or (car (cdr (member value allowed)))
609 (if (equal nval value)
610 (error "Only one allowed value for this property")))
611 (t (setq nval (car allowed)))))
613 ((equal major-mode 'org-agenda-mode)
614 (org-columns-eval '(org-entry-put pom key nval))
615 ;; The following let preserves the current format, and makes sure
616 ;; that in only a single file things need to be upated.
617 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
618 (buffer (marker-buffer pom))
619 (org-agenda-contributing-files
620 (list (with-current-buffer buffer
621 (buffer-file-name (buffer-base-buffer))))))
622 (org-agenda-columns)))
624 (let ((inhibit-read-only t))
625 (remove-text-properties (1- bol) eol '(read-only t))
628 (setq org-columns-overlays
629 (org-delete-all line-overlays org-columns-overlays))
630 (mapc 'delete-overlay line-overlays)
631 (org-columns-eval '(org-entry-put pom key nval)))
632 (org-columns-display-here)))
633 (org-move-to-column col)
634 (and (nth 3 (assoc key org-columns-current-fmt-compiled))
635 (org-columns-update key))))))
637 (defun org-colview-construct-allowed-dates (s)
638 "Construct a list of three dates around the date in S.
639 This respects the format of the time stamp in S, active or non-active,
640 and also including time or not. S must be just a time stamp, no text
642 (when (and s (string-match (concat "^" org-ts-regexp3 "$") s))
643 (let* ((time (org-parse-time-string s 'nodefaults))
644 (active (equal (string-to-char s) ?<))
645 (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
646 time-before time-after)
647 (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
648 (setf (car time) (or (car time) 0))
649 (setf (nth 1 time) (or (nth 1 time) 0))
650 (setf (nth 2 time) (or (nth 2 time) 0))
651 (setq time-before (copy-sequence time))
652 (setq time-after (copy-sequence time))
653 (setf (nth 3 time-before) (1- (nth 3 time)))
654 (setf (nth 3 time-after) (1+ (nth 3 time)))
655 (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
656 (list time-before time time-after)))))
658 (defun org-verify-version (task)
661 (if (or (featurep 'xemacs)
662 (< emacs-major-version 22))
663 (error "Emacs 22 is required for the columns feature")))))
665 (defun org-columns-open-link (&optional arg)
667 (let ((value (get-char-property (point) 'org-columns-value)))
668 (org-open-link-from-string value arg)))
670 (defun org-columns-get-format-and-top-level ()
672 (when (condition-case nil (org-back-to-heading) (error nil))
673 (setq fmt (org-entry-get nil "COLUMNS" t)))
674 (setq fmt (or fmt org-columns-default-format))
675 (org-set-local 'org-columns-current-fmt fmt)
676 (org-columns-compile-format fmt)
677 (if (marker-position org-entry-property-inherited-from)
678 (move-marker org-columns-top-level-marker
679 org-entry-property-inherited-from)
680 (move-marker org-columns-top-level-marker (point)))
683 (defun org-columns ()
684 "Turn on column view on an org-mode file."
686 (org-verify-version 'columns)
687 (org-columns-remove-overlays)
688 (move-marker org-columns-begin-marker (point))
689 (let ((org-columns-time (time-to-number-of-days (current-time)))
690 beg end fmt cache maxwidths)
691 (setq fmt (org-columns-get-format-and-top-level))
693 (goto-char org-columns-top-level-marker)
695 (unless org-columns-inhibit-recalculation
696 (org-columns-compute-all))
697 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
699 ;; Get and cache the properties
701 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
704 (narrow-to-region beg end)
706 (while (re-search-forward (concat "^" outline-regexp) end t)
707 (if (and org-columns-skip-archived-trees
708 (looking-at (concat ".*:" org-archive-tag ":")))
709 (org-end-of-subtree t)
710 (push (cons (org-current-line) (org-entry-properties)) cache)))
712 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
713 (org-set-local 'org-columns-current-maxwidths maxwidths)
714 (org-columns-display-here-title)
715 (when (org-set-local 'org-columns-flyspell-was-active
716 (org-bound-and-true-p flyspell-mode))
718 (unless (local-variable-p 'org-colview-initial-truncate-line-value)
719 (org-set-local 'org-colview-initial-truncate-line-value
721 (setq truncate-lines t)
723 (org-goto-line (car x))
724 (org-columns-display-here (cdr x)))
727 (eval-when-compile (defvar org-columns-time))
729 (defvar org-columns-compile-map
735 ("X/" checkbox-n-of-m +)
736 ("X%" checkbox-percent +)
737 ("max" max_numbers max)
738 ("min" min_numbers min)
740 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
741 (":max" max_times max)
742 (":min" min_times min)
744 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
745 ("@min" min_age min (lambda (x) (- org-columns-time x)))
746 ("@max" max_age max (lambda (x) (- org-columns-time x)))
748 (lambda (&rest x) (/ (apply '+ x) (float (length x))))
749 (lambda (x) (- org-columns-time x)))
750 ("est+" estimate org-estimate-combine))
751 "Operator <-> format,function,calc map.
752 Used to compile/uncompile columns format and completing read in
753 interactive function `org-columns-new'.
755 operator string used in #+COLUMNS definition describing the
757 format symbol describing summary type selected interactively in
758 `org-columns-new' and internally in
759 `org-columns-number-to-string' and
760 `org-columns-string-to-number'
761 function called with a list of values as argument to calculate
763 calc function called on every element before summarizing. This is
764 optional and should only be specified if needed")
766 (defun org-columns-new (&optional prop title width op fmt fun &rest rest)
767 "Insert a new column, to the left of the current column."
769 (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
771 (setq prop (org-icompleting-read
772 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
774 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
775 (setq width (read-string "Column width: " (if width (number-to-string width))))
776 (if (string-match "\\S-" width)
777 (setq width (string-to-number width))
779 (setq fmt (org-icompleting-read
781 (mapcar (lambda (x) (list (symbol-name (cadr x))))
782 org-columns-compile-map)
784 (setq fmt (intern fmt)
785 fun (cdr (assoc fmt (mapcar 'cdr org-columns-compile-map))))
786 (if (eq fmt 'none) (setq fmt nil))
790 (setcdr editp (list title width nil fmt nil fun)))
791 (setq cell (nthcdr (1- (current-column))
792 org-columns-current-fmt-compiled))
793 (setcdr cell (cons (list prop title width nil fmt nil
794 (car fun) (cadr fun))
796 (org-columns-store-format)
799 (defun org-columns-delete ()
800 "Delete the column at point from columns view."
802 (let* ((n (current-column))
803 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
805 (format "Are you sure you want to remove column \"%s\"? " title))
806 (setq org-columns-current-fmt-compiled
807 (delq (nth n org-columns-current-fmt-compiled)
808 org-columns-current-fmt-compiled))
809 (org-columns-store-format)
811 (if (>= (current-column) (length org-columns-current-fmt-compiled))
812 (backward-char 1)))))
814 (defun org-columns-edit-attributes ()
815 "Edit the attributes of the current column."
817 (let* ((n (current-column))
818 (info (nth n org-columns-current-fmt-compiled)))
819 (apply 'org-columns-new info)))
821 (defun org-columns-widen (arg)
822 "Make the column wider by ARG characters."
824 (let* ((n (current-column))
825 (entry (nth n org-columns-current-fmt-compiled))
826 (width (or (nth 2 entry)
827 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
828 (setq width (max 1 (+ width arg)))
829 (setcar (nthcdr 2 entry) width)
830 (org-columns-store-format)
833 (defun org-columns-narrow (arg)
834 "Make the column narrower by ARG characters."
836 (org-columns-widen (- arg)))
838 (defun org-columns-move-right ()
839 "Swap this column with the one to the right."
841 (let* ((n (current-column))
842 (cell (nthcdr n org-columns-current-fmt-compiled))
844 (when (>= n (1- (length org-columns-current-fmt-compiled)))
845 (error "Cannot shift this column further to the right"))
847 (setcar cell (car (cdr cell)))
848 (setcdr cell (cons e (cdr (cdr cell))))
849 (org-columns-store-format)
853 (defun org-columns-move-left ()
854 "Swap this column with the one to the left."
856 (let* ((n (current-column)))
858 (error "Cannot shift this column further to the left"))
860 (org-columns-move-right)
863 (defun org-columns-store-format ()
864 "Store the text version of the current columns format in appropriate place.
865 This is either in the COLUMNS property of the node starting the current column
866 display, or in the #+COLUMNS line of the current buffer."
868 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
869 (org-set-local 'org-columns-current-fmt fmt)
870 (if (marker-position org-columns-top-level-marker)
872 (goto-char org-columns-top-level-marker)
873 (if (and (org-at-heading-p)
874 (org-entry-get nil "COLUMNS"))
875 (org-entry-put nil "COLUMNS" fmt)
876 (goto-char (point-min))
877 ;; Overwrite all #+COLUMNS lines....
878 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
880 (replace-match (concat "#+COLUMNS: " fmt) t t))
882 (goto-char (point-min))
883 (or (org-on-heading-p t) (outline-next-heading))
884 (let ((inhibit-read-only t))
885 (insert-before-markers "#+COLUMNS: " fmt "\n")))
886 (org-set-local 'org-columns-default-format fmt))))))
888 (defvar org-agenda-overriding-columns-format nil
889 "When set, overrides any other format definition for the agenda.
890 Don't set this, this is meant for dynamic scoping.")
892 (defun org-columns-get-autowidth-alist (s cache)
893 "Derive the maximum column widths from the format and the cache."
895 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
896 (push (cons (match-string 1 s) 1) rtn)
897 (setq start (match-end 0)))
899 (setcdr x (apply 'max
902 (length (or (cdr (assoc (car x) (cdr y))) " ")))
907 (defun org-columns-compute-all ()
908 "Compute all columns that have operators defined."
910 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
911 (let ((columns org-columns-current-fmt-compiled)
912 (org-columns-time (time-to-number-of-days (current-time)))
914 (while (setq col (pop columns))
917 (org-columns-compute (car col)))))))
919 (defun org-columns-update (property)
920 "Recompute PROPERTY, and update the columns display for it."
921 (org-columns-compute property)
925 (when (equal (overlay-get ov 'org-columns-key) property)
926 (setq pos (overlay-start ov))
928 (when (setq val (cdr (assoc property
930 (point-at-bol) 'org-summaries))))
931 (setq fmt (overlay-get ov 'org-columns-format))
932 (overlay-put ov 'org-columns-value val)
933 (overlay-put ov 'display (format fmt val)))))
934 org-columns-overlays))))
936 (defun org-columns-compute (property)
937 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
939 (let* ((re (concat "^" outline-regexp))
940 (lmax 30) ; Does anyone use deeper levels???
941 (lvals (make-vector lmax nil))
942 (lflag (make-vector lmax nil))
944 (ass (assoc property org-columns-current-fmt-compiled))
948 (calc (or (nth 7 ass) 'identity))
949 (beg org-columns-top-level-marker)
950 last-level val valflag flag end sumpos sum-alist sum str str1 useval)
952 ;; Find the region to compute
954 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
956 ;; Walk the tree from the back and do the computations
957 (while (re-search-backward re beg t)
958 (setq sumpos (match-beginning 0)
960 level (org-outline-level)
961 val (org-entry-get nil property)
962 valflag (and val (string-match "\\S-" val)))
964 ((< level last-level)
965 ;; put the sum of lower levels here as a property
966 (setq sum (when (aref lvals last-level)
967 (apply fun (aref lvals last-level)))
968 flag (aref lflag last-level) ; any valid entries from children?
969 str (org-columns-number-to-string sum format printf)
970 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
971 useval (if flag str1 (if valflag val ""))
972 sum-alist (get-text-property sumpos 'org-summaries))
973 (if (assoc property sum-alist)
974 (setcdr (assoc property sum-alist) useval)
975 (push (cons property useval) sum-alist)
977 (add-text-properties sumpos (1+ sumpos)
978 (list 'org-summaries sum-alist))))
979 (when (and val (not (equal val (if flag str val))))
980 (org-entry-put nil property (if flag str val)))
981 ;; add current to current level accumulator
982 (when (or flag valflag)
985 (funcall calc (org-columns-string-to-number
986 (if flag str val) format)))
988 (aset lflag level t))
989 ;; clear accumulators for deeper levels
990 (loop for l from (1+ level) to (1- lmax) do
993 ((>= level last-level)
994 ;; add what we have here to the accumulator for this level
996 (push (funcall calc (org-columns-string-to-number val format))
998 (aset lflag level t)))
999 (t (error "This should not happen")))))))
1001 (defun org-columns-redo ()
1002 "Construct the column display again."
1004 (message "Recomputing columns...")
1005 (let ((line (org-current-line))
1006 (col (current-column)))
1008 (if (marker-position org-columns-begin-marker)
1009 (goto-char org-columns-begin-marker))
1010 (org-columns-remove-overlays)
1012 (call-interactively 'org-columns)
1014 (call-interactively 'org-agenda-columns)))
1015 (org-goto-line line)
1016 (move-to-column col))
1017 (message "Recomputing columns...done"))
1019 (defun org-columns-not-in-agenda ()
1020 (if (eq major-mode 'org-agenda-mode)
1021 (error "This command is only allowed in Org-mode buffers")))
1023 (defun org-string-to-number (s)
1024 "Convert string to number, and interpret hh:mm:ss."
1025 (if (not (string-match ":" s))
1026 (string-to-number s)
1027 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1029 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1032 (defun org-columns-number-to-string (n fmt &optional printf)
1033 "Convert a computed column number to a string value, according to FMT."
1035 ((memq fmt '(estimate)) (org-estimate-print n printf))
1036 ((not (numberp n)) "")
1037 ((memq fmt '(add_times max_times min_times mean_times))
1038 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
1039 (format org-time-clocksum-format h m)))
1041 (cond ((= n (floor n)) "[X]")
1044 ((memq fmt '(checkbox-n-of-m checkbox-percent))
1045 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
1046 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
1047 (printf (format printf n))
1050 ((memq fmt '(min_age max_age mean_age))
1051 (org-format-time-period n))
1052 (t (number-to-string n))))
1054 (defun org-nofm-to-completion (n m &optional percent)
1056 (format "[%d/%d]" n m)
1057 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
1060 (defun org-columns-string-to-number (s fmt)
1061 "Convert a column value to a number that can be used for column computing."
1064 ((memq fmt '(min_age max_age mean_age))
1065 (cond ((string= s "") org-columns-time)
1067 "\\([0-9]+\\)d \\([0-9]+\\)h \\([0-9]+\\)m \\([0-9]+\\)s"
1069 (+ (* 60 (+ (* 60 (+ (* 24 (string-to-number (match-string 1 s)))
1070 (string-to-number (match-string 2 s))))
1071 (string-to-number (match-string 3 s))))
1072 (string-to-number (match-string 4 s))))
1073 (t (time-to-number-of-days (apply 'encode-time
1074 (org-parse-time-string s t))))))
1075 ((string-match ":" s)
1076 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1078 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1080 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
1081 (if (equal s "[X]") 1. 0.000001))
1082 ((memq fmt '(estimate)) (org-string-to-estimate s))
1083 (t (string-to-number s)))))
1085 (defun org-columns-uncompile-format (cfmt)
1086 "Turn the compiled columns format back into a string representation."
1087 (let ((rtn "") e s prop title op op-match width fmt printf fun calc)
1088 (while (setq e (pop cfmt))
1097 (when (setq op-match (rassoc (list fmt fun calc) org-columns-compile-map))
1098 (setq op (car op-match)))
1099 (if (and op printf) (setq op (concat op ";" printf)))
1100 (if (equal title prop) (setq title nil))
1101 (setq s (concat "%" (if width (number-to-string width))
1103 (if title (concat "(" title ")"))
1104 (if op (concat "{" op "}"))))
1105 (setq rtn (concat rtn " " s)))
1108 (defun org-columns-compile-format (fmt)
1109 "Turn a column format string into an alist of specifications.
1110 The alist has one entry for each column in the format. The elements of
1112 property the property
1113 title the title field for the columns
1114 width the column width in characters, can be nil for automatic
1115 operator the operator if any
1116 format the output format for computed results, derived from operator
1117 printf a printf format for computed values
1118 fun the lisp function to compute summary values, derived from operator
1119 calc function to get values from base elements"
1120 (let ((start 0) width prop title op op-match f printf fun calc)
1121 (setq org-columns-current-fmt-compiled nil)
1122 (while (string-match
1123 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
1125 (setq start (match-end 0)
1126 width (match-string 1 fmt)
1127 prop (match-string 2 fmt)
1128 title (or (match-string 3 fmt) prop)
1129 op (match-string 4 fmt)
1134 (if width (setq width (string-to-number width)))
1135 (when (and op (string-match ";" op))
1136 (setq printf (substring op (match-end 0))
1137 op (substring op 0 (match-beginning 0))))
1138 (when (setq op-match (assoc op org-columns-compile-map))
1139 (setq f (cadr op-match)
1140 fun (caddr op-match)
1141 calc (cadddr op-match)))
1142 (push (list prop title width op f printf fun calc)
1143 org-columns-current-fmt-compiled))
1144 (setq org-columns-current-fmt-compiled
1145 (nreverse org-columns-current-fmt-compiled))))
1148 ;;; Dynamic block for Column view
1150 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
1151 "Get the column view of the current buffer or subtree.
1152 The first optional argument MAXLEVEL sets the level limit. A
1153 second optional argument SKIP-EMPTY-ROWS tells whether to skip
1154 empty rows, an empty row being one where all the column view
1155 specifiers except ITEM are empty. This function returns a list
1156 containing the title row and all other rows. Each row is a list
1159 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1160 (re-comment (concat "\\*+[ \t]+" org-comment-string "\\>"))
1161 (re-archive (concat ".*:" org-archive-tag ":"))
1162 (n (length title)) row tbl)
1163 (goto-char (point-min))
1164 (while (re-search-forward "^\\(\\*+\\) " nil t)
1166 (when (and (or (null maxlevel)
1168 (if org-odd-levels-only
1169 (/ (1+ (length (match-string 1))) 2)
1170 (length (match-string 1)))))
1171 (get-char-property (match-beginning 0) 'org-columns-key))
1172 (when (save-excursion
1173 (goto-char (point-at-bol))
1174 (or (looking-at re-comment)
1175 (looking-at re-archive)))
1176 (org-end-of-subtree t)
1179 (loop for i from 0 to (1- n) do
1182 (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
1183 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
1186 (setq row (nreverse row))
1187 (unless (and skip-empty-rows
1188 (eq 1 (length (delete "" (delete-dups (copy-sequence row))))))
1190 (append (list title 'hline) (nreverse tbl)))))
1192 (defun org-dblock-write:columnview (params)
1193 "Write the column view table.
1194 PARAMS is a property list of parameters:
1196 :width enforce same column widths with <N> specifiers.
1197 :id the :ID: property of the entry where the columns view
1198 should be built. When the symbol `local', call locally.
1199 When `global' call column view with the cursor at the beginning
1200 of the buffer (usually this means that the whole buffer switches
1201 to column view). When \"file:path/to/file.org\", invoke column
1202 view at the start of that file. Otherwise, the ID is located
1203 using `org-id-find'.
1204 :hlines When t, insert a hline before each item. When a number, insert
1205 a hline before each level <= that number.
1206 :vlines When t, make each column a colgroup to enforce vertical lines.
1207 :maxlevel When set to a number, don't capture headlines below this level.
1209 When t, skip rows where all specifiers other than ITEM are empty."
1210 (let ((pos (move-marker (make-marker) (point)))
1211 (hlines (plist-get params :hlines))
1212 (vlines (plist-get params :vlines))
1213 (maxlevel (plist-get params :maxlevel))
1214 (content-lines (org-split-string (plist-get params :content) "\n"))
1215 (skip-empty-rows (plist-get params :skip-empty-rows))
1216 tbl id idpos nfields tmp recalc line
1217 id-as-string view-file view-pos)
1218 (when (setq id (plist-get params :id))
1219 (setq id-as-string (cond ((numberp id) (number-to-string id))
1220 ((symbolp id) (symbol-name id))
1223 (cond ((not id) nil)
1224 ((eq id 'global) (setq view-pos (point-min)))
1226 ((string-match "^file:\\(.*\\)" id-as-string)
1227 (setq view-file (match-string 1 id-as-string)
1229 (unless (file-exists-p view-file)
1230 (error "No such file: \"%s\"" id-as-string)))
1231 ((setq idpos (org-find-entry-with-id id))
1232 (setq view-pos idpos))
1233 ((setq idpos (org-id-find id))
1234 (setq view-file (car idpos))
1235 (setq view-pos (cdr idpos)))
1236 (t (error "Cannot find entry with :ID: %s" id))))
1237 (with-current-buffer (if view-file
1238 (get-file-buffer view-file)
1243 (goto-char (or view-pos (point)))
1245 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1246 (setq nfields (length (car tbl)))
1247 (org-columns-quit))))
1249 (move-marker pos nil)
1251 (when (plist-get params :hlines)
1254 (if (eq (car tbl) 'hline)
1255 (push (pop tbl) tmp)
1256 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1257 (if (and (not (eq (car tmp) 'hline))
1259 (and (numberp hlines)
1260 (<= (- (match-end 1) (match-beginning 1))
1263 (push (pop tbl) tmp)))
1264 (setq tbl (nreverse tmp)))
1266 (setq tbl (mapcar (lambda (x)
1267 (if (eq 'hline x) x (cons "" x)))
1269 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1272 (while (string-match "^#" (car content-lines))
1273 (insert (pop content-lines) "\n")))
1274 (insert (org-listtable-to-string tbl))
1275 (when (plist-get params :width)
1276 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1277 org-columns-current-widths "|")))
1278 (while (setq line (pop content-lines))
1279 (when (string-match "^#" line)
1281 (when (string-match "^[ \t]*#\\+TBLFM" line)
1284 (progn (goto-char pos) (org-table-recalculate 'all))
1286 (org-table-align)))))
1288 (defun org-listtable-to-string (tbl)
1289 "Convert a listtable TBL to a string that contains the Org-mode table.
1290 The table still need to be aligned. The resulting string has no leading
1291 and tailing newline characters."
1296 (concat "|" (mapconcat 'identity x "|") "|"))
1297 ((eq x 'hline) "|-|")
1298 (t (error "Garbage in listtable: %s" x))))
1301 (defun org-insert-columns-dblock ()
1302 "Create a dynamic block capturing a column view table."
1304 (let ((defaults '(:name "columnview" :hlines 1))
1305 (id (org-icompleting-read
1306 "Capture columns (local, global, entry with :ID: property) [local]: "
1307 (append '(("global") ("local"))
1308 (mapcar 'list (org-property-values "ID"))))))
1309 (if (equal id "") (setq id 'local))
1310 (if (equal id "global") (setq id 'global))
1311 (setq defaults (append defaults (list :id id)))
1312 (org-create-dblock defaults)
1313 (org-update-dblock)))
1315 ;;; Column view in the agenda
1317 (defvar org-agenda-view-columns-initially nil
1318 "When set, switch to columns view immediately after creating the agenda.")
1320 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1321 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1322 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1324 (defun org-agenda-columns ()
1325 "Turn on or update column view in the agenda."
1327 (org-verify-version 'columns)
1328 (org-columns-remove-overlays)
1329 (move-marker org-columns-begin-marker (point))
1330 (let ((org-columns-time (time-to-number-of-days (current-time)))
1331 cache maxwidths m p a d fmt)
1333 ((and (boundp 'org-agenda-overriding-columns-format)
1334 org-agenda-overriding-columns-format)
1335 (setq fmt org-agenda-overriding-columns-format)
1336 (org-set-local 'org-agenda-overriding-columns-format fmt))
1337 ((setq m (org-get-at-bol 'org-hd-marker))
1338 (setq fmt (or (org-entry-get m "COLUMNS" t)
1339 (with-current-buffer (marker-buffer m)
1340 org-columns-default-format))))
1341 ((and (boundp 'org-columns-current-fmt)
1342 (local-variable-p 'org-columns-current-fmt)
1343 org-columns-current-fmt)
1344 (setq fmt org-columns-current-fmt))
1345 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
1346 (setq m (get-text-property m 'org-hd-marker))
1347 (setq fmt (or (org-entry-get m "COLUMNS" t)
1348 (with-current-buffer (marker-buffer m)
1349 org-columns-default-format)))))
1350 (setq fmt (or fmt org-columns-default-format))
1351 (org-set-local 'org-columns-current-fmt fmt)
1352 (org-columns-compile-format fmt)
1353 (when org-agenda-columns-compute-summary-properties
1354 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1356 ;; Get and cache the properties
1357 (goto-char (point-min))
1359 (when (setq m (or (org-get-at-bol 'org-hd-marker)
1360 (org-get-at-bol 'org-marker)))
1361 (setq p (org-entry-properties m))
1363 (when (or (not (setq a (assoc org-effort-property p)))
1364 (not (string-match "\\S-" (or (cdr a) ""))))
1365 ;; OK, the property is not defined. Use appointment duration?
1366 (when (and org-agenda-columns-add-appointments-to-effort-sum
1367 (setq d (get-text-property (point) 'duration)))
1368 (setq d (org-minutes-to-hh:mm-string d))
1369 (put-text-property 0 (length d) 'face 'org-warning d)
1370 (push (cons org-effort-property d) p)))
1371 (push (cons (org-current-line) p) cache))
1372 (beginning-of-line 2))
1374 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
1375 (org-set-local 'org-columns-current-maxwidths maxwidths)
1376 (org-columns-display-here-title)
1377 (when (org-set-local 'org-columns-flyspell-was-active
1378 (org-bound-and-true-p flyspell-mode))
1381 (org-goto-line (car x))
1382 (org-columns-display-here (cdr x)))
1384 (when org-agenda-columns-show-summaries
1385 (org-agenda-colview-summarize cache))))))
1387 (defun org-agenda-colview-summarize (cache)
1388 "Summarize the summarizable columns in column view in the agenda.
1389 This will add overlays to the date lines, to show the summary for each day."
1390 (let* ((fmt (mapcar (lambda (x)
1391 (if (equal (car x) "CLOCKSUM")
1392 (list "CLOCKSUM" (nth 1 x) (nth 2 x) ":" 'add_times
1395 org-columns-current-fmt-compiled))
1396 line c c1 stype calc sumfunc props lsum entries prop v title)
1398 (when (delq nil (mapcar 'cadr fmt))
1399 ;; OK, at least one summation column, it makes sense to try this
1400 (goto-char (point-max))
1402 (when (or (get-text-property (point) 'org-date-line)
1403 (eq (get-text-property (point) 'face)
1404 'org-agenda-structure))
1405 ;; OK, this is a date line that should be used
1406 (setq line (org-current-line))
1407 (setq entries nil c cache cache nil)
1408 (while (setq c1 (pop c))
1409 (if (> (car c1) line)
1412 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1413 ;; Compute the summaries for the properties we want,
1414 ;; set nil properties for the rest.
1415 (when (setq entries (mapcar 'cdr entries))
1423 calc (or (nth 7 f) 'identity))
1425 ((equal prop "ITEM")
1426 (cons prop (buffer-substring (point-at-bol)
1428 ((not stype) (cons prop ""))
1429 (t ;; do the summary
1432 (setq v (cdr (assoc prop x)))
1436 (if (not (get-text-property 0 'org-computed v))
1439 (org-columns-string-to-number
1442 (setq lsum (remove nil lsum))
1444 (cond ((> (length lsum) 1)
1445 (org-columns-number-to-string
1446 (apply sumfunc lsum) stype))
1447 ((eq (length lsum) 1)
1448 (org-columns-number-to-string
1451 (put-text-property 0 (length lsum) 'face 'bold lsum)
1452 (unless (eq calc 'identity)
1453 (put-text-property 0 (length lsum) 'org-computed t lsum))
1456 (org-columns-display-here props 'dateline)
1457 (org-set-local 'org-agenda-columns-active t)))
1458 (if (bobp) (throw 'exit t))
1459 (beginning-of-line 0))))))
1461 (defun org-agenda-colview-compute (fmt)
1462 "Compute the relevant columns in the contributing source buffers."
1463 (let ((files org-agenda-contributing-files)
1464 (org-columns-begin-marker (make-marker))
1465 (org-columns-top-level-marker (make-marker))
1467 (while (setq f (pop files))
1468 (setq b (find-buffer-visiting f))
1469 (with-current-buffer (or (buffer-base-buffer b) b)
1474 (remove-text-properties (point-min) (point-max)
1475 '(org-summaries t)))
1476 (goto-char (point-min))
1477 (org-columns-get-format-and-top-level)
1478 (while (setq fm (pop fmt))
1479 (if (equal (car fm) "CLOCKSUM")
1481 (when (and (nth 4 fm)
1482 (setq a (assoc (car fm)
1483 org-columns-current-fmt-compiled))
1484 (equal (nth 4 a) (nth 4 fm)))
1485 (org-columns-compute (car fm)))))))))))
1487 (defun org-format-time-period (interval)
1488 "Convert time in fractional days to days/hours/minutes/seconds."
1489 (if (numberp interval)
1490 (let* ((days (floor interval))
1491 (frac-hours (* 24 (- interval days)))
1492 (hours (floor frac-hours))
1493 (minutes (floor (* 60 (- frac-hours hours))))
1494 (seconds (floor (* 60 (- (* 60 (- frac-hours hours)) minutes)))))
1495 (format "%dd %02dh %02dm %02ds" days hours minutes seconds))
1498 (defun org-estimate-mean-and-var (v)
1499 "Return the mean and variance of an estimate."
1500 (let* ((low (float (car v)))
1501 (high (float (cadr v)))
1502 (mean (/ (+ low high) 2.0))
1503 (var (/ (+ (expt (- mean low) 2.0) (expt (- high mean) 2.0)) 2.0)))
1506 (defun org-estimate-combine (&rest el)
1507 "Combine a list of estimates, using mean and variance.
1508 The mean and variance of the result will be the sum of the means
1509 and variances (respectively) of the individual estimates."
1513 (let ((stats (org-estimate-mean-and-var e)))
1514 (setq mean (+ mean (car stats)))
1515 (setq var (+ var (cadr stats)))))
1517 (let ((stdev (sqrt var)))
1518 (list (- mean stdev) (+ mean stdev)))))
1520 (defun org-estimate-print (e &optional fmt)
1521 "Prepare a string representation of an estimate.
1522 This formats these numbers as two numbers with a \"-\" between them."
1523 (if (null fmt) (set 'fmt "%.0f"))
1524 (format "%s" (mapconcat (lambda (n) (format fmt n)) e "-")))
1526 (defun org-string-to-estimate (s)
1527 "Convert a string to an estimate.
1528 The string should be two numbers joined with a \"-\"."
1529 (if (string-match "\\(.*\\)-\\(.*\\)" s)
1530 (list (string-to-number (match-string 1 s))
1531 (string-to-number(match-string 2 s)))
1532 (list (string-to-number s) (string-to-number s))))
1534 (provide 'org-colview)
1536 ;; arch-tag: 61f5128d-747c-4983-9479-e3871fa3d73c
1538 ;;; org-colview.el ends here