1 ;;; org-colview.el --- Column View in Org-mode
3 ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; This file contains the column view for Org.
31 (eval-when-compile (require 'cl
))
34 (declare-function org-agenda-redo
"org-agenda" ())
35 (declare-function org-agenda-do-context-action
"org-agenda" ())
36 (declare-function org-clock-sum-today
"org-clock" (&optional headline-filter
))
38 (when (featurep 'xemacs
)
39 (error "Do not load this file into XEmacs, use `org-colview-xemacs.el' from the contrib/ directory"))
43 (defvar org-columns-overlays nil
44 "Holds the list of current column overlays.")
46 (defvar org-columns-current-fmt nil
47 "Local variable, holds the currently active column format.")
48 (make-variable-buffer-local 'org-columns-current-fmt
)
49 (defvar org-columns-current-fmt-compiled nil
50 "Local variable, holds the currently active column format.
51 This is the compiled version of the format.")
52 (make-variable-buffer-local 'org-columns-current-fmt-compiled
)
53 (defvar org-columns-current-widths nil
54 "Loval variable, holds the currently widths of fields.")
55 (make-variable-buffer-local 'org-columns-current-widths
)
56 (defvar org-columns-current-maxwidths nil
57 "Loval variable, holds the currently active maximum column widths.")
58 (make-variable-buffer-local 'org-columns-current-maxwidths
)
59 (defvar org-columns-begin-marker
(make-marker)
60 "Points to the position where last a column creation command was called.")
61 (defvar org-columns-top-level-marker
(make-marker)
62 "Points to the position where current columns region starts.")
64 (defvar org-columns-map
(make-sparse-keymap)
65 "The keymap valid in column display.")
67 (defun org-columns-content ()
68 "Switch to contents view while in columns view."
73 (org-defkey org-columns-map
"c" 'org-columns-content
)
74 (org-defkey org-columns-map
"o" 'org-overview
)
75 (org-defkey org-columns-map
"e" 'org-columns-edit-value
)
76 (org-defkey org-columns-map
"\C-c\C-t" 'org-columns-todo
)
77 (org-defkey org-columns-map
"\C-c\C-c" 'org-columns-set-tags-or-toggle
)
78 (org-defkey org-columns-map
"\C-c\C-o" 'org-columns-open-link
)
79 (org-defkey org-columns-map
"v" 'org-columns-show-value
)
80 (org-defkey org-columns-map
"q" 'org-columns-quit
)
81 (org-defkey org-columns-map
"r" 'org-columns-redo
)
82 (org-defkey org-columns-map
"g" 'org-columns-redo
)
83 (org-defkey org-columns-map
[left] 'backward-char)
84 (org-defkey org-columns-map "\M-b" 'backward-char)
85 (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
86 (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
87 (org-defkey org-columns-map "\M-f"
88 (lambda () (interactive) (goto-char (1+ (point)))))
89 (org-defkey org-columns-map [right]
90 (lambda () (interactive) (goto-char (1+ (point)))))
91 (org-defkey org-columns-map [down]
92 (lambda () (interactive)
93 (let ((col (current-column)))
95 (while (and (org-invisible-p2) (not (eobp)))
96 (beginning-of-line 2))
98 (if (eq major-mode 'org-agenda-mode)
99 (org-agenda-do-context-action)))))
100 (org-defkey org-columns-map [up]
101 (lambda () (interactive)
102 (let ((col (current-column)))
103 (beginning-of-line 0)
104 (while (and (org-invisible-p2) (not (bobp)))
105 (beginning-of-line 0))
107 (if (eq major-mode 'org-agenda-mode)
108 (org-agenda-do-context-action)))))
109 (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
110 (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
111 (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
112 (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
113 (org-defkey org-columns-map "<" 'org-columns-narrow)
114 (org-defkey org-columns-map ">" 'org-columns-widen)
115 (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
116 (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
117 (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
118 (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
120 (org-defkey org-columns-map (number-to-string i)
121 `(lambda () (interactive)
122 (org-columns-next-allowed-value nil ,i))))
124 (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
126 ["Edit property" org-columns-edit-value t]
127 ["Next allowed value" org-columns-next-allowed-value t]
128 ["Previous allowed value" org-columns-previous-allowed-value t]
129 ["Show full value" org-columns-show-value t]
130 ["Edit allowed values" org-columns-edit-allowed t]
132 ["Edit column attributes" org-columns-edit-attributes t]
133 ["Increase column width" org-columns-widen t]
134 ["Decrease column width" org-columns-narrow t]
136 ["Move column right" org-columns-move-right t]
137 ["Move column left" org-columns-move-left t]
138 ["Add column" org-columns-new t]
139 ["Delete column" org-columns-delete t]
141 ["CONTENTS" org-columns-content t]
142 ["OVERVIEW" org-overview t]
143 ["Refresh columns display" org-columns-redo t]
145 ["Open link" org-columns-open-link t]
147 ["Quit" org-columns-quit t]))
149 (defun org-columns-new-overlay (beg end &optional string face)
150 "Create a new column overlay and add it to the list."
151 (let ((ov (make-overlay beg end)))
152 (overlay-put ov 'face (or face 'secondary-selection))
153 (remove-text-properties 0 (length string) '(face nil) string)
154 (org-overlay-display ov string face)
155 (push ov org-columns-overlays)
158 (defun org-columns-display-here (&optional props dateline)
159 "Overlay the current line with column display."
161 (let* ((fmt org-columns-current-fmt-compiled)
163 (level-face (save-excursion
164 (beginning-of-line 1)
165 (and (looking-at "\\(\\**\\)\\(\\* \\)")
166 (org-get-level-face 2))))
167 (ref-face (or level-face
168 (and (eq major-mode 'org-agenda-mode)
169 (get-text-property (point-at-bol) 'face))
171 (color (list :foreground (face-attribute ref-face :foreground)))
172 (font (list :height (face-attribute 'default :height)
173 :family (face-attribute 'default :family)))
174 (face (list color font 'org-column ref-face))
175 (face1 (list color font 'org-agenda-column-dateline ref-face))
176 (cphr (get-text-property (point-at-bol) 'org-complex-heading-regexp))
177 pom property ass width f string ov column val modval s2 title calc)
178 ;; Check if the entry is in another buffer.
180 (if (eq major-mode 'org-agenda-mode)
181 (setq pom (or (org-get-at-bol 'org-hd-marker)
182 (org-get-at-bol 'org-marker))
183 props (if pom (org-entry-properties pom) nil))
184 (setq props (org-entry-properties nil))))
186 (while (setq column (pop fmt))
187 (setq property (car column)
189 ass (if (equal property "ITEM")
191 ;; When in a buffer, get the whole line,
192 ;; we'll clean it later…
193 (if (derived-mode-p 'org-mode)
196 (buffer-substring-no-properties
197 (point-at-bol) (point-at-eol))))
198 ;; In agenda, just get the `txt' property
199 (or (org-get-at-bol 'txt)
200 (buffer-substring-no-properties
201 (point) (progn (end-of-line) (point))))))
202 (assoc property props))
203 width (or (cdr (assoc property org-columns-current-maxwidths))
206 f (format "%%-%d.%ds | " width width)
208 val (or (cdr ass) "")
209 modval (cond ((and org-columns-modify-value-for-display-function
211 org-columns-modify-value-for-display-function))
212 (funcall org-columns-modify-value-for-display-function
214 ((equal property "ITEM")
215 (org-columns-cleanup-item
216 val org-columns-current-fmt-compiled
217 (or org-complex-heading-regexp cphr)))
218 ((and calc (functionp calc)
219 (not (string= val ""))
220 (not (get-text-property 0 'org-computed val)))
221 (org-columns-number-to-string
222 (funcall calc (org-columns-string-to-number
225 (setq s2 (org-columns-add-ellipses (or modval val) width))
226 (setq string (format f s2))
227 ;; Create the overlay
228 (org-with-silent-modifications
229 (setq ov (org-columns-new-overlay
230 beg (setq beg (1+ beg)) string (if dateline face1 face)))
231 (overlay-put ov 'keymap org-columns-map)
232 (overlay-put ov 'org-columns-key property)
233 (overlay-put ov 'org-columns-value (cdr ass))
234 (overlay-put ov 'org-columns-value-modified modval)
235 (overlay-put ov 'org-columns-pom pom)
236 (overlay-put ov 'org-columns-format f)
237 (overlay-put ov 'line-prefix "")
238 (overlay-put ov 'wrap-prefix ""))
239 (if (or (not (char-after beg))
240 (equal (char-after beg) ?\n))
241 (let ((inhibit-read-only t))
244 (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
245 ;; Make the rest of the line disappear.
247 (setq ov (org-columns-new-overlay beg (point-at-eol)))
248 (overlay-put ov 'invisible t)
249 (overlay-put ov 'keymap org-columns-map)
250 (overlay-put ov 'intangible t)
251 (overlay-put ov 'line-prefix "")
252 (overlay-put ov 'wrap-prefix "")
253 (push ov org-columns-overlays)
254 (setq ov (make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
255 (overlay-put ov 'keymap org-columns-map)
256 (push ov org-columns-overlays)
257 (let ((inhibit-read-only t))
258 (put-text-property (max (point-min) (1- (point-at-bol)))
259 (min (point-max) (1+ (point-at-eol)))
260 'read-only "Type `e' to edit property")))))
262 (defun org-columns-add-ellipses (string width)
263 "Truncate STRING with WIDTH characters, with ellipses."
265 ((<= (length string) width) string)
266 ((<= width (length org-columns-ellipses))
267 (substring org-columns-ellipses 0 width))
268 (t (concat (substring string 0 (- width (length org-columns-ellipses)))
269 org-columns-ellipses))))
271 (defvar org-columns-full-header-line-format nil
272 "The full header line format, will be shifted by horizontal scrolling." )
273 (defvar org-previous-header-line-format nil
274 "The header line format before column view was turned on.")
275 (defvar org-columns-inhibit-recalculation nil
276 "Inhibit recomputing of columns on column view startup.")
277 (defvar org-columns-flyspell-was-active nil
278 "Remember the state of `flyspell-mode' before column view.
279 Flyspell-mode can cause problems in columns view, so it is turned off
280 for the duration of the command.")
282 (defvar header-line-format)
283 (defvar org-columns-previous-hscroll 0)
285 (defun org-columns-display-here-title ()
286 "Overlay the newline before the current line with the table title."
288 (let ((fmt org-columns-current-fmt-compiled)
290 property width f column str widths)
291 (while (setq column (pop fmt))
292 (setq property (car column)
293 str (or (nth 1 column) property)
294 width (or (cdr (assoc property org-columns-current-maxwidths))
297 widths (push width widths)
298 f (format "%%-%d.%ds | " width width)
299 string (format f str)
300 title (concat title string)))
302 (org-add-props " " nil 'display '(space :align-to 0))
303 ;;(org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
304 (org-add-props title nil 'face 'org-column-title)))
305 (org-set-local 'org-previous-header-line-format header-line-format)
306 (org-set-local 'org-columns-current-widths (nreverse widths))
307 (setq org-columns-full-header-line-format title)
308 (setq org-columns-previous-hscroll -1)
309 ; (org-columns-hscoll-title)
310 (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
312 (defun org-columns-hscoll-title ()
313 "Set the `header-line-format' so that it scrolls along with the table."
314 (sit-for .0001) ; need to force a redisplay to update window-hscroll
315 (when (not (= (window-hscroll) org-columns-previous-hscroll))
316 (setq header-line-format
317 (concat (substring org-columns-full-header-line-format 0 1)
318 (substring org-columns-full-header-line-format
319 (1+ (window-hscroll))))
320 org-columns-previous-hscroll (window-hscroll))
321 (force-mode-line-update)))
323 (defvar org-colview-initial-truncate-line-value nil
324 "Remember the value of `truncate-lines' across colview.")
326 (defun org-columns-remove-overlays ()
327 "Remove all currently active column overlays."
329 (when (marker-buffer org-columns-begin-marker)
330 (with-current-buffer (marker-buffer org-columns-begin-marker)
331 (when (local-variable-p 'org-previous-header-line-format)
332 (setq header-line-format org-previous-header-line-format)
333 (kill-local-variable 'org-previous-header-line-format)
334 (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
335 (move-marker org-columns-begin-marker nil)
336 (move-marker org-columns-top-level-marker nil)
337 (org-with-silent-modifications
338 (mapc 'delete-overlay org-columns-overlays)
339 (setq org-columns-overlays nil)
340 (let ((inhibit-read-only t))
341 (remove-text-properties (point-min) (point-max) '(read-only t))))
342 (when org-columns-flyspell-was-active
344 (when (local-variable-p 'org-colview-initial-truncate-line-value)
345 (setq truncate-lines org-colview-initial-truncate-line-value)))))
347 (defun org-columns-cleanup-item (item fmt cphr)
348 "Remove from ITEM what is a column in the format FMT.
349 CPHR is the complex heading regexp to use for parsing ITEM."
353 (unless (string-match "^\*+ " item)
354 (setq item (concat "* " item) fixitem t))
355 (if (string-match cphr item)
358 (org-add-props (match-string 1 item) nil
359 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
360 (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
361 (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
362 " " (save-match-data (org-columns-compact-links (or (match-string 4 item) "")))
363 (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))
366 (list 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
368 (if fixitem (replace-regexp-in-string "^\*+ " "" item) item))))
370 (defun org-columns-compact-links (s)
371 "Replace [[link][desc]] with [desc] or [link]."
372 (while (string-match org-bracket-link-regexp s)
373 (setq s (replace-match
374 (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
378 (defun org-columns-show-value ()
379 "Show the full value of the property."
381 (let ((value (get-char-property (point) 'org-columns-value)))
382 (message "Value is: %s" (or value ""))))
384 (defvar org-agenda-columns-active) ;; defined in org-agenda.el
386 (defun org-columns-quit ()
387 "Remove the column overlays and in this way exit column editing."
389 (org-with-silent-modifications
390 (org-columns-remove-overlays)
391 (let ((inhibit-read-only t))
392 (remove-text-properties (point-min) (point-max) '(read-only t))))
393 (when (eq major-mode 'org-agenda-mode)
394 (setq org-agenda-columns-active nil)
396 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
398 (defun org-columns-check-computed ()
399 "Check if this column value is computed.
400 If yes, throw an error indicating that changing it does not make sense."
401 (let ((val (get-char-property (point) 'org-columns-value)))
402 (when (and (stringp val)
403 (get-char-property 0 'org-computed val))
404 (error "This value is computed from the entry's children"))))
406 (defun org-columns-todo (&optional arg)
407 "Change the TODO state during column view."
409 (org-columns-edit-value "TODO"))
411 (defun org-columns-set-tags-or-toggle (&optional arg)
412 "Toggle checkbox at point, or set tags for current headline."
414 (if (string-match "\\`\\[[ xX-]\\]\\'"
415 (get-char-property (point) 'org-columns-value))
416 (org-columns-next-allowed-value)
417 (org-columns-edit-value "TAGS")))
419 (defun org-columns-edit-value (&optional key)
420 "Edit the value of the property at point in column view.
421 Where possible, use the standard interface for changing this line."
423 (org-columns-check-computed)
424 (let* ((col (current-column))
425 (key (or key (get-char-property (point) 'org-columns-key)))
426 (value (get-char-property (point) 'org-columns-value))
427 (bol (point-at-bol)) (eol (point-at-eol))
428 (pom (or (get-text-property bol 'org-hd-marker)
429 (point))) ; keep despite of compiler waring
431 (delq nil (mapcar (lambda (x)
432 (and (eq (overlay-buffer x) (current-buffer))
433 (>= (overlay-start x) bol)
434 (<= (overlay-start x) eol)
436 org-columns-overlays)))
437 (org-columns-time (time-to-number-of-days (current-time)))
440 ((equal key "CLOCKSUM")
441 (error "This special column cannot be edited"))
443 (setq eval '(org-with-point-at pom
444 (org-edit-headline))))
446 (setq eval '(org-with-point-at
448 (call-interactively 'org-todo))))
449 ((equal key "PRIORITY")
450 (setq eval '(org-with-point-at pom
451 (call-interactively 'org-priority))))
453 (setq eval '(org-with-point-at pom
454 (let ((org-fast-tag-selection-single-key
455 (if (eq org-fast-tag-selection-single-key 'expert)
456 t org-fast-tag-selection-single-key)))
457 (call-interactively 'org-set-tags)))))
458 ((equal key "DEADLINE")
459 (setq eval '(org-with-point-at pom
460 (call-interactively 'org-deadline))))
461 ((equal key "SCHEDULED")
462 (setq eval '(org-with-point-at pom
463 (call-interactively 'org-schedule))))
464 ((equal key "BEAMER_env")
465 (setq eval '(org-with-point-at pom
466 (call-interactively 'org-beamer-select-environment))))
468 (setq allowed (org-property-get-allowed-values pom key 'table))
470 (setq nval (org-icompleting-read
471 "Value: " allowed nil
472 (not (get-text-property 0 'org-unrestricted
474 (setq nval (read-string "Edit: " value)))
475 (setq nval (org-trim nval))
476 (when (not (equal nval value))
477 (setq eval '(org-entry-put pom key nval)))))
481 ((equal major-mode 'org-agenda-mode)
482 (org-columns-eval eval)
483 ;; The following let preserves the current format, and makes sure
484 ;; that in only a single file things need to be updated.
485 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
486 (buffer (marker-buffer pom))
487 (org-agenda-contributing-files
488 (list (with-current-buffer buffer
489 (buffer-file-name (buffer-base-buffer))))))
490 (org-agenda-columns)))
492 (let ((inhibit-read-only t))
493 (org-with-silent-modifications
494 (remove-text-properties
495 (max (point-min) (1- bol)) eol '(read-only t)))
498 (setq org-columns-overlays
499 (org-delete-all line-overlays org-columns-overlays))
500 (mapc 'delete-overlay line-overlays)
501 (org-columns-eval eval))
502 (org-columns-display-here)))
503 (org-move-to-column col)
504 (if (and (derived-mode-p 'org-mode)
505 (nth 3 (assoc key org-columns-current-fmt-compiled)))
506 (org-columns-update key)))))))
508 (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
509 "Edit the current headline, the part without TODO keyword, TAGS."
510 (org-back-to-heading)
511 (when (looking-at org-todo-line-regexp)
513 (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
514 (txt (match-string 3))
517 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@#%]+:[ \t]*$") txt)
518 (setq post (match-string 0 txt)
519 txt (substring txt 0 (match-beginning 0))))
520 (setq txt2 (read-string "Edit: " txt))
521 (when (not (equal txt txt2))
523 (insert pre txt2 post)
524 (delete-region (point) (point-at-eol))
525 (org-set-tags nil t)))))
527 (defun org-columns-edit-allowed ()
528 "Edit the list of allowed values for the current property."
530 (let* ((pom (or (org-get-at-bol 'org-marker)
531 (org-get-at-bol 'org-hd-marker)
533 (key (get-char-property (point) 'org-columns-key))
534 (key1 (concat key "_ALL"))
535 (allowed (org-entry-get pom key1 t))
537 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
538 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
539 (setq nval (read-string "Allowed: " allowed))
541 (cond ((marker-position org-entry-property-inherited-from)
542 org-entry-property-inherited-from)
543 ((marker-position org-columns-top-level-marker)
544 org-columns-top-level-marker)
548 (defun org-columns-eval (form)
551 (beginning-of-line 1)
552 ;; `next-line' is needed here, because it skips invisible line.
553 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
554 (setq hidep (org-at-heading-p 1)))
556 (and hidep (hide-entry))))
558 (defun org-columns-previous-allowed-value ()
559 "Switch to the previous allowed value for this column."
561 (org-columns-next-allowed-value t))
563 (defun org-columns-next-allowed-value (&optional previous nth)
564 "Switch to the next allowed value for this column.
565 When PREVIOUS is set, go to the previous value. When NTH is
566 an integer, select that value."
568 (org-columns-check-computed)
569 (let* ((col (current-column))
570 (key (get-char-property (point) 'org-columns-key))
571 (value (get-char-property (point) 'org-columns-value))
572 (bol (point-at-bol)) (eol (point-at-eol))
573 (pom (or (get-text-property bol 'org-hd-marker)
574 (point))) ; keep despite of compiler waring
576 (delq nil (mapcar (lambda (x)
577 (and (eq (overlay-buffer x) (current-buffer))
578 (>= (overlay-start x) bol)
579 (<= (overlay-start x) eol)
581 org-columns-overlays)))
582 (allowed (or (org-property-get-allowed-values pom key)
584 (nth 4 (assoc key org-columns-current-fmt-compiled))
585 '(checkbox checkbox-n-of-m checkbox-percent))
587 (org-colview-construct-allowed-dates value)))
591 (if (= nth -1) (setq nth 9)))
592 (when (equal key "ITEM")
593 (error "Cannot edit item headline from here"))
594 (unless (or allowed (member key '("SCHEDULED" "DEADLINE" "CLOCKSUM")))
595 (error "Allowed values for this property have not been defined"))
596 (if (member key '("SCHEDULED" "DEADLINE" "CLOCKSUM"))
597 (setq nval (if previous 'earlier 'later))
598 (if previous (setq allowed (reverse allowed)))
601 (setq nval (nth nth allowed))
603 (error "There are only %d allowed values for property `%s'"
604 (length allowed) key)))
605 ((member value allowed)
606 (setq nval (or (car (cdr (member value allowed)))
608 (if (equal nval value)
609 (error "Only one allowed value for this property")))
610 (t (setq nval (car allowed)))))
612 ((equal major-mode 'org-agenda-mode)
613 (org-columns-eval '(org-entry-put pom key nval))
614 ;; The following let preserves the current format, and makes sure
615 ;; that in only a single file things need to be updated.
616 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
617 (buffer (marker-buffer pom))
618 (org-agenda-contributing-files
619 (list (with-current-buffer buffer
620 (buffer-file-name (buffer-base-buffer))))))
621 (org-agenda-columns)))
623 (let ((inhibit-read-only t))
624 (remove-text-properties (1- bol) eol '(read-only t))
627 (setq org-columns-overlays
628 (org-delete-all line-overlays org-columns-overlays))
629 (mapc 'delete-overlay line-overlays)
630 (org-columns-eval '(org-entry-put pom key nval)))
631 (org-columns-display-here)))
632 (org-move-to-column col)
633 (and (nth 3 (assoc key org-columns-current-fmt-compiled))
634 (org-columns-update key))))))
636 (defun org-colview-construct-allowed-dates (s)
637 "Construct a list of three dates around the date in S.
638 This respects the format of the time stamp in S, active or non-active,
639 and also including time or not. S must be just a time stamp, no text
641 (when (and s (string-match (concat "^" org-ts-regexp3 "$") s))
642 (let* ((time (org-parse-time-string s 'nodefaults))
643 (active (equal (string-to-char s) ?<))
644 (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
645 time-before time-after)
646 (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
647 (setf (car time) (or (car time) 0))
648 (setf (nth 1 time) (or (nth 1 time) 0))
649 (setf (nth 2 time) (or (nth 2 time) 0))
650 (setq time-before (copy-sequence time))
651 (setq time-after (copy-sequence time))
652 (setf (nth 3 time-before) (1- (nth 3 time)))
653 (setf (nth 3 time-after) (1+ (nth 3 time)))
654 (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
655 (list time-before time time-after)))))
657 (defun org-verify-version (task)
660 (if (or (featurep 'xemacs)
661 (< emacs-major-version 22))
662 (error "Emacs 22 is required for the columns feature")))))
664 (defun org-columns-open-link (&optional arg)
666 (let ((value (get-char-property (point) 'org-columns-value)))
667 (org-open-link-from-string value arg)))
669 (defun org-columns-get-format-and-top-level ()
670 (let ((fmt (org-columns-get-format)))
671 (org-columns-goto-top-level)
674 (defun org-columns-get-format (&optional fmt-string)
676 (let (fmt-as-property fmt)
677 (when (condition-case nil (org-back-to-heading) (error nil))
678 (setq fmt-as-property (org-entry-get nil "COLUMNS" t)))
679 (setq fmt (or fmt-string fmt-as-property org-columns-default-format))
680 (org-set-local 'org-columns-current-fmt fmt)
681 (org-columns-compile-format fmt)
684 (defun org-columns-goto-top-level ()
685 (when (condition-case nil (org-back-to-heading) (error nil))
686 (org-entry-get nil "COLUMNS" t))
687 (if (marker-position org-entry-property-inherited-from)
688 (move-marker org-columns-top-level-marker org-entry-property-inherited-from)
689 (move-marker org-columns-top-level-marker (point))))
692 (defun org-columns (&optional columns-fmt-string)
693 "Turn on column view on an org-mode file.
694 When COLUMNS-FMT-STRING is non-nil, use it as the column format."
696 (org-verify-version 'columns)
697 (org-columns-remove-overlays)
698 (move-marker org-columns-begin-marker (point))
699 (let ((org-columns-time (time-to-number-of-days (current-time)))
700 beg end fmt cache maxwidths)
701 (org-columns-goto-top-level)
702 (setq fmt (org-columns-get-format columns-fmt-string))
704 (goto-char org-columns-top-level-marker)
706 (unless org-columns-inhibit-recalculation
707 (org-columns-compute-all))
708 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
710 ;; Get and cache the properties
712 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
715 (narrow-to-region beg end)
717 (when (assoc "CLOCKSUM_T" org-columns-current-fmt-compiled)
720 (narrow-to-region beg end)
721 (org-clock-sum-today))))
722 (while (re-search-forward org-outline-regexp-bol end t)
723 (if (and org-columns-skip-archived-trees
724 (looking-at (concat ".*:" org-archive-tag ":")))
725 (org-end-of-subtree t)
726 (push (cons (org-current-line) (org-entry-properties)) cache)))
728 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
729 (org-set-local 'org-columns-current-maxwidths maxwidths)
730 (org-columns-display-here-title)
731 (when (org-set-local 'org-columns-flyspell-was-active
732 (org-bound-and-true-p flyspell-mode))
734 (unless (local-variable-p 'org-colview-initial-truncate-line-value)
735 (org-set-local 'org-colview-initial-truncate-line-value
737 (setq truncate-lines t)
739 (org-goto-line (car x))
740 (org-columns-display-here (cdr x)))
743 (eval-when-compile (defvar org-columns-time))
745 (defvar org-columns-compile-map
751 ("X/" checkbox-n-of-m +)
752 ("X%" checkbox-percent +)
753 ("max" max_numbers max)
754 ("min" min_numbers min)
756 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
757 (":max" max_times max)
758 (":min" min_times min)
760 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
761 ("@min" min_age min (lambda (x) (- org-columns-time x)))
762 ("@max" max_age max (lambda (x) (- org-columns-time x)))
764 (lambda (&rest x) (/ (apply '+ x) (float (length x))))
765 (lambda (x) (- org-columns-time x)))
766 ("est+" estimate org-estimate-combine))
767 "Operator <-> format,function,calc map.
768 Used to compile/uncompile columns format and completing read in
769 interactive function `org-columns-new'.
771 operator string used in #+COLUMNS definition describing the
773 format symbol describing summary type selected interactively in
774 `org-columns-new' and internally in
775 `org-columns-number-to-string' and
776 `org-columns-string-to-number'
777 function called with a list of values as argument to calculate
779 calc function called on every element before summarizing. This is
780 optional and should only be specified if needed")
782 (defun org-columns-new (&optional prop title width op fmt fun &rest rest)
783 "Insert a new column, to the left of the current column."
785 (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
787 (setq prop (org-icompleting-read
788 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
790 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
791 (setq width (read-string "Column width: " (if width (number-to-string width))))
792 (if (string-match "\\S-" width)
793 (setq width (string-to-number width))
795 (setq fmt (org-icompleting-read
797 (mapcar (lambda (x) (list (symbol-name (cadr x))))
798 org-columns-compile-map)
800 (setq fmt (intern fmt)
801 fun (cdr (assoc fmt (mapcar 'cdr org-columns-compile-map))))
802 (if (eq fmt 'none) (setq fmt nil))
806 (setcdr editp (list title width nil fmt nil fun)))
807 (setq cell (nthcdr (1- (current-column))
808 org-columns-current-fmt-compiled))
809 (setcdr cell (cons (list prop title width nil fmt nil
810 (car fun) (cadr fun))
812 (org-columns-store-format)
815 (defun org-columns-delete ()
816 "Delete the column at point from columns view."
818 (let* ((n (current-column))
819 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
821 (format "Are you sure you want to remove column \"%s\"? " title))
822 (setq org-columns-current-fmt-compiled
823 (delq (nth n org-columns-current-fmt-compiled)
824 org-columns-current-fmt-compiled))
825 (org-columns-store-format)
827 (if (>= (current-column) (length org-columns-current-fmt-compiled))
828 (backward-char 1)))))
830 (defun org-columns-edit-attributes ()
831 "Edit the attributes of the current column."
833 (let* ((n (current-column))
834 (info (nth n org-columns-current-fmt-compiled)))
835 (apply 'org-columns-new info)))
837 (defun org-columns-widen (arg)
838 "Make the column wider by ARG characters."
840 (let* ((n (current-column))
841 (entry (nth n org-columns-current-fmt-compiled))
842 (width (or (nth 2 entry)
843 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
844 (setq width (max 1 (+ width arg)))
845 (setcar (nthcdr 2 entry) width)
846 (org-columns-store-format)
849 (defun org-columns-narrow (arg)
850 "Make the column narrower by ARG characters."
852 (org-columns-widen (- arg)))
854 (defun org-columns-move-right ()
855 "Swap this column with the one to the right."
857 (let* ((n (current-column))
858 (cell (nthcdr n org-columns-current-fmt-compiled))
860 (when (>= n (1- (length org-columns-current-fmt-compiled)))
861 (error "Cannot shift this column further to the right"))
863 (setcar cell (car (cdr cell)))
864 (setcdr cell (cons e (cdr (cdr cell))))
865 (org-columns-store-format)
869 (defun org-columns-move-left ()
870 "Swap this column with the one to the left."
872 (let* ((n (current-column)))
874 (error "Cannot shift this column further to the left"))
876 (org-columns-move-right)
879 (defun org-columns-store-format ()
880 "Store the text version of the current columns format in appropriate place.
881 This is either in the COLUMNS property of the node starting the current column
882 display, or in the #+COLUMNS line of the current buffer."
884 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
885 (org-set-local 'org-columns-current-fmt fmt)
886 (if (marker-position org-columns-top-level-marker)
888 (goto-char org-columns-top-level-marker)
889 (if (and (org-at-heading-p)
890 (org-entry-get nil "COLUMNS"))
891 (org-entry-put nil "COLUMNS" fmt)
892 (goto-char (point-min))
893 ;; Overwrite all #+COLUMNS lines....
894 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
896 (replace-match (concat "#+COLUMNS: " fmt) t t))
898 (goto-char (point-min))
899 (or (org-at-heading-p t) (outline-next-heading))
900 (let ((inhibit-read-only t))
901 (insert-before-markers "#+COLUMNS: " fmt "\n")))
902 (org-set-local 'org-columns-default-format fmt))))))
904 (defvar org-agenda-overriding-columns-format nil
905 "When set, overrides any other format definition for the agenda.
906 Don't set this, this is meant for dynamic scoping.")
908 (defun org-columns-get-autowidth-alist (s cache)
909 "Derive the maximum column widths from the format and the cache."
911 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
912 (push (cons (match-string 1 s) 1) rtn)
913 (setq start (match-end 0)))
915 (setcdr x (apply 'max
918 (length (or (cdr (assoc (car x) (cdr y))) " ")))
923 (defun org-columns-compute-all ()
924 "Compute all columns that have operators defined."
925 (org-with-silent-modifications
926 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
927 (let ((columns org-columns-current-fmt-compiled)
928 (org-columns-time (time-to-number-of-days (current-time)))
930 (while (setq col (pop columns))
933 (org-columns-compute (car col)))))))
935 (defun org-columns-update (property)
936 "Recompute PROPERTY, and update the columns display for it."
937 (org-columns-compute property)
941 (when (equal (overlay-get ov 'org-columns-key) property)
942 (setq pos (overlay-start ov))
944 (when (setq val (cdr (assoc property
946 (point-at-bol) 'org-summaries))))
947 (setq fmt (overlay-get ov 'org-columns-format))
948 (overlay-put ov 'org-columns-value val)
949 (overlay-put ov 'display (format fmt val)))))
950 org-columns-overlays))))
952 (defvar org-inlinetask-min-level
953 (if (featurep 'org-inlinetask) org-inlinetask-min-level 15))
954 (defun org-columns-compute (property)
955 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
957 (let* ((re org-outline-regexp-bol)
958 (lmax 30) ; Does anyone use deeper levels???
959 (lvals (make-vector lmax nil))
960 (lflag (make-vector lmax nil))
962 (ass (assoc property org-columns-current-fmt-compiled))
966 (calc (or (nth 7 ass) 'identity))
967 (beg org-columns-top-level-marker)
968 (inminlevel org-inlinetask-min-level)
969 (last-level org-inlinetask-min-level)
970 val valflag flag end sumpos sum-alist sum str str1 useval)
972 ;; Find the region to compute
974 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
976 ;; Walk the tree from the back and do the computations
977 (while (re-search-backward re beg t)
978 (setq sumpos (match-beginning 0)
979 last-level (if (not (or (zerop level) (eq level inminlevel)))
981 level (org-outline-level)
982 val (org-entry-get nil property)
983 valflag (and val (string-match "\\S-" val)))
985 ((< level last-level)
986 ;; put the sum of lower levels here as a property
987 (setq sum (+ (if (and (/= last-level inminlevel)
988 (aref lvals last-level))
989 (apply fun (aref lvals last-level)) 0)
990 (if (aref lvals inminlevel)
991 (apply fun (aref lvals inminlevel)) 0))
992 flag (or (aref lflag last-level) ; any valid entries from children?
993 (aref lflag inminlevel)) ; or inline tasks?
994 str (org-columns-number-to-string sum format printf)
995 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
996 useval (if flag str1 (if valflag val ""))
997 sum-alist (get-text-property sumpos 'org-summaries))
998 (if (assoc property sum-alist)
999 (setcdr (assoc property sum-alist) useval)
1000 (push (cons property useval) sum-alist)
1001 (org-with-silent-modifications
1002 (add-text-properties sumpos (1+ sumpos)
1003 (list 'org-summaries sum-alist))))
1004 (when (and val (not (equal val (if flag str val))))
1005 (org-entry-put nil property (if flag str val)))
1006 ;; add current to current level accumulator
1007 (when (or flag valflag)
1010 (funcall calc (org-columns-string-to-number
1011 (if flag str val) format)))
1013 (aset lflag level t))
1014 ;; clear accumulators for deeper levels
1015 (loop for l from (1+ level) to (1- lmax) do
1017 (aset lflag l nil)))
1018 ((>= level last-level)
1019 ;; add what we have here to the accumulator for this level
1021 (push (funcall calc (org-columns-string-to-number val format))
1023 (aset lflag level t)))
1024 (t (error "This should not happen")))))))
1026 (defun org-columns-redo ()
1027 "Construct the column display again."
1029 (message "Recomputing columns...")
1030 (let ((line (org-current-line))
1031 (col (current-column)))
1033 (if (marker-position org-columns-begin-marker)
1034 (goto-char org-columns-begin-marker))
1035 (org-columns-remove-overlays)
1036 (if (derived-mode-p 'org-mode)
1037 (call-interactively 'org-columns)
1039 (call-interactively 'org-agenda-columns)))
1040 (org-goto-line line)
1041 (move-to-column col))
1042 (message "Recomputing columns...done"))
1044 (defun org-columns-not-in-agenda ()
1045 (if (eq major-mode 'org-agenda-mode)
1046 (error "This command is only allowed in Org-mode buffers")))
1048 (defun org-string-to-number (s)
1049 "Convert string to number, and interpret hh:mm:ss."
1050 (if (not (string-match ":" s))
1051 (string-to-number s)
1052 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1054 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1057 (defun org-columns-number-to-string (n fmt &optional printf)
1058 "Convert a computed column number to a string value, according to FMT."
1060 ((memq fmt '(estimate)) (org-estimate-print n printf))
1061 ((not (numberp n)) "")
1062 ((memq fmt '(add_times max_times min_times mean_times))
1063 (org-hours-to-clocksum-string n))
1065 (cond ((= n (floor n)) "[X]")
1068 ((memq fmt '(checkbox-n-of-m checkbox-percent))
1069 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
1070 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
1071 (printf (format printf n))
1074 ((memq fmt '(min_age max_age mean_age))
1075 (org-format-time-period n))
1076 (t (number-to-string n))))
1078 (defun org-nofm-to-completion (n m &optional percent)
1080 (format "[%d/%d]" n m)
1081 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
1084 (defun org-columns-string-to-number (s fmt)
1085 "Convert a column value to a number that can be used for column computing."
1088 ((memq fmt '(min_age max_age mean_age))
1089 (cond ((string= s "") org-columns-time)
1091 "\\([0-9]+\\)d \\([0-9]+\\)h \\([0-9]+\\)m \\([0-9]+\\)s"
1093 (+ (* 60 (+ (* 60 (+ (* 24 (string-to-number (match-string 1 s)))
1094 (string-to-number (match-string 2 s))))
1095 (string-to-number (match-string 3 s))))
1096 (string-to-number (match-string 4 s))))
1097 (t (time-to-number-of-days (apply 'encode-time
1098 (org-parse-time-string s t))))))
1099 ((string-match ":" s)
1100 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1102 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1104 ((string-match (concat "\\([0-9.]+\\) *\\("
1105 (regexp-opt (mapcar 'car org-effort-durations))
1107 (setq s (concat "0:" (org-duration-string-to-minutes s t)))
1108 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1110 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1112 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
1113 (if (equal s "[X]") 1. 0.000001))
1114 ((memq fmt '(estimate)) (org-string-to-estimate s))
1115 (t (string-to-number s)))))
1117 (defun org-columns-uncompile-format (cfmt)
1118 "Turn the compiled columns format back into a string representation."
1119 (let ((rtn "") e s prop title op op-match width fmt printf fun calc)
1120 (while (setq e (pop cfmt))
1129 (when (setq op-match (rassoc (list fmt fun calc) org-columns-compile-map))
1130 (setq op (car op-match)))
1131 (if (and op printf) (setq op (concat op ";" printf)))
1132 (if (equal title prop) (setq title nil))
1133 (setq s (concat "%" (if width (number-to-string width))
1135 (if title (concat "(" title ")"))
1136 (if op (concat "{" op "}"))))
1137 (setq rtn (concat rtn " " s)))
1140 (defun org-columns-compile-format (fmt)
1141 "Turn a column format string into an alist of specifications.
1142 The alist has one entry for each column in the format. The elements of
1144 property the property
1145 title the title field for the columns
1146 width the column width in characters, can be nil for automatic
1147 operator the operator if any
1148 format the output format for computed results, derived from operator
1149 printf a printf format for computed values
1150 fun the lisp function to compute summary values, derived from operator
1151 calc function to get values from base elements"
1152 (let ((start 0) width prop title op op-match f printf fun calc)
1153 (setq org-columns-current-fmt-compiled nil)
1154 (while (string-match
1155 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
1157 (setq start (match-end 0)
1158 width (match-string 1 fmt)
1159 prop (match-string 2 fmt)
1160 title (or (match-string 3 fmt) prop)
1161 op (match-string 4 fmt)
1166 (if width (setq width (string-to-number width)))
1167 (when (and op (string-match ";" op))
1168 (setq printf (substring op (match-end 0))
1169 op (substring op 0 (match-beginning 0))))
1170 (when (setq op-match (assoc op org-columns-compile-map))
1171 (setq f (cadr op-match)
1172 fun (caddr op-match)
1173 calc (cadddr op-match)))
1174 (push (list prop title width op f printf fun calc)
1175 org-columns-current-fmt-compiled))
1176 (setq org-columns-current-fmt-compiled
1177 (nreverse org-columns-current-fmt-compiled))))
1180 ;;; Dynamic block for Column view
1182 (defvar org-heading-regexp) ; defined in org.el
1183 (defvar org-heading-keyword-regexp-format) ; defined in org.el
1184 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
1185 "Get the column view of the current buffer or subtree.
1186 The first optional argument MAXLEVEL sets the level limit. A
1187 second optional argument SKIP-EMPTY-ROWS tells whether to skip
1188 empty rows, an empty row being one where all the column view
1189 specifiers except ITEM are empty. This function returns a list
1190 containing the title row and all other rows. Each row is a list
1193 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1194 (re-comment (format org-heading-keyword-regexp-format
1195 org-comment-string))
1196 (re-archive (concat ".*:" org-archive-tag ":"))
1197 (n (length title)) row tbl)
1198 (goto-char (point-min))
1199 (while (re-search-forward org-heading-regexp nil t)
1201 (when (and (or (null maxlevel)
1203 (if org-odd-levels-only
1204 (/ (1+ (length (match-string 1))) 2)
1205 (length (match-string 1)))))
1206 (get-char-property (match-beginning 0) 'org-columns-key))
1207 (when (save-excursion
1208 (goto-char (point-at-bol))
1209 (or (looking-at re-comment)
1210 (looking-at re-archive)))
1211 (org-end-of-subtree t)
1214 (loop for i from 0 to (1- n) do
1217 (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
1218 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
1221 (setq row (nreverse row))
1222 (unless (and skip-empty-rows
1223 (eq 1 (length (delete "" (delete-dups (copy-sequence row))))))
1225 (append (list title 'hline) (nreverse tbl)))))
1228 (defun org-dblock-write:columnview (params)
1229 "Write the column view table.
1230 PARAMS is a property list of parameters:
1232 :width enforce same column widths with <N> specifiers.
1233 :id the :ID: property of the entry where the columns view
1234 should be built. When the symbol `local', call locally.
1235 When `global' call column view with the cursor at the beginning
1236 of the buffer (usually this means that the whole buffer switches
1237 to column view). When \"file:path/to/file.org\", invoke column
1238 view at the start of that file. Otherwise, the ID is located
1239 using `org-id-find'.
1240 :hlines When t, insert a hline before each item. When a number, insert
1241 a hline before each level <= that number.
1242 :vlines When t, make each column a colgroup to enforce vertical lines.
1243 :maxlevel When set to a number, don't capture headlines below this level.
1245 When t, skip rows where all specifiers other than ITEM are empty.
1246 :format When non-nil, specify the column view format to use."
1247 (let ((pos (point-marker))
1248 (hlines (plist-get params :hlines))
1249 (vlines (plist-get params :vlines))
1250 (maxlevel (plist-get params :maxlevel))
1251 (content-lines (org-split-string (plist-get params :content) "\n"))
1252 (skip-empty-rows (plist-get params :skip-empty-rows))
1253 (columns-fmt (plist-get params :format))
1254 (case-fold-search t)
1255 tbl id idpos nfields tmp recalc line
1256 id-as-string view-file view-pos)
1257 (when (setq id (plist-get params :id))
1258 (setq id-as-string (cond ((numberp id) (number-to-string id))
1259 ((symbolp id) (symbol-name id))
1262 (cond ((not id) nil)
1263 ((eq id 'global) (setq view-pos (point-min)))
1265 ((string-match "^file:\\(.*\\)" id-as-string)
1266 (setq view-file (match-string 1 id-as-string)
1268 (unless (file-exists-p view-file)
1269 (error "No such file: \"%s\"" id-as-string)))
1270 ((setq idpos (org-find-entry-with-id id))
1271 (setq view-pos idpos))
1272 ((setq idpos (org-id-find id))
1273 (setq view-file (car idpos))
1274 (setq view-pos (cdr idpos)))
1275 (t (error "Cannot find entry with :ID: %s" id))))
1276 (with-current-buffer (if view-file
1277 (get-file-buffer view-file)
1282 (goto-char (or view-pos (point)))
1283 (org-columns columns-fmt)
1284 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1285 (setq nfields (length (car tbl)))
1286 (org-columns-quit))))
1288 (move-marker pos nil)
1290 (when (plist-get params :hlines)
1293 (if (eq (car tbl) 'hline)
1294 (push (pop tbl) tmp)
1295 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1296 (if (and (not (eq (car tmp) 'hline))
1298 (and (numberp hlines)
1299 (<= (- (match-end 1) (match-beginning 1))
1302 (push (pop tbl) tmp)))
1303 (setq tbl (nreverse tmp)))
1305 (setq tbl (mapcar (lambda (x)
1306 (if (eq 'hline x) x (cons "" x)))
1308 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1311 (while (string-match "^#" (car content-lines))
1312 (insert (pop content-lines) "\n")))
1313 (insert (org-listtable-to-string tbl))
1314 (when (plist-get params :width)
1315 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1316 org-columns-current-widths "|")))
1317 (while (setq line (pop content-lines))
1318 (when (string-match "^#" line)
1320 (when (string-match "^[ \t]*#\\+tblfm" line)
1323 (progn (goto-char pos) (org-table-recalculate 'all))
1325 (org-table-align)))))
1327 (defun org-listtable-to-string (tbl)
1328 "Convert a listtable TBL to a string that contains the Org-mode table.
1329 The table still need to be aligned. The resulting string has no leading
1330 and tailing newline characters."
1335 (concat "|" (mapconcat 'identity x "|") "|"))
1336 ((eq x 'hline) "|-|")
1337 (t (error "Garbage in listtable: %s" x))))
1341 (defun org-insert-columns-dblock ()
1342 "Create a dynamic block capturing a column view table."
1344 (let ((defaults '(:name "columnview" :hlines 1))
1345 (id (org-icompleting-read
1346 "Capture columns (local, global, entry with :ID: property) [local]: "
1347 (append '(("global") ("local"))
1348 (mapcar 'list (org-property-values "ID"))))))
1349 (if (equal id "") (setq id 'local))
1350 (if (equal id "global") (setq id 'global))
1351 (setq defaults (append defaults (list :id id)))
1352 (org-create-dblock defaults)
1353 (org-update-dblock)))
1355 ;;; Column view in the agenda
1357 (defvar org-agenda-view-columns-initially nil
1358 "When set, switch to columns view immediately after creating the agenda.")
1360 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1361 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1362 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1365 (defun org-agenda-columns ()
1366 "Turn on or update column view in the agenda."
1368 (org-verify-version 'columns)
1369 (org-columns-remove-overlays)
1370 (move-marker org-columns-begin-marker (point))
1371 (let ((org-columns-time (time-to-number-of-days (current-time)))
1372 cache maxwidths m p a d fmt)
1374 ((and (boundp 'org-agenda-overriding-columns-format)
1375 org-agenda-overriding-columns-format)
1376 (setq fmt org-agenda-overriding-columns-format))
1377 ((setq m (org-get-at-bol 'org-hd-marker))
1378 (setq fmt (or (org-entry-get m "COLUMNS" t)
1379 (with-current-buffer (marker-buffer m)
1380 org-columns-default-format))))
1381 ((and (boundp 'org-columns-current-fmt)
1382 (local-variable-p 'org-columns-current-fmt)
1383 org-columns-current-fmt)
1384 (setq fmt org-columns-current-fmt))
1385 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
1386 (setq m (get-text-property m 'org-hd-marker))
1387 (setq fmt (or (org-entry-get m "COLUMNS" t)
1388 (with-current-buffer (marker-buffer m)
1389 org-columns-default-format)))))
1390 (setq fmt (or fmt org-columns-default-format))
1391 (org-set-local 'org-columns-current-fmt fmt)
1392 (org-columns-compile-format fmt)
1393 (when org-agenda-columns-compute-summary-properties
1394 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1396 ;; Get and cache the properties
1397 (goto-char (point-min))
1399 (when (setq m (or (org-get-at-bol 'org-hd-marker)
1400 (org-get-at-bol 'org-marker)))
1401 (setq p (org-entry-properties m))
1403 (when (or (not (setq a (assoc org-effort-property p)))
1404 (not (string-match "\\S-" (or (cdr a) ""))))
1405 ;; OK, the property is not defined. Use appointment duration?
1406 (when (and org-agenda-columns-add-appointments-to-effort-sum
1407 (setq d (get-text-property (point) 'duration)))
1408 (setq d (org-minutes-to-clocksum-string d))
1409 (put-text-property 0 (length d) 'face 'org-warning d)
1410 (push (cons org-effort-property d) p)))
1411 (push (cons (org-current-line) p) cache))
1412 (beginning-of-line 2))
1414 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
1415 (org-set-local 'org-columns-current-maxwidths maxwidths)
1416 (org-columns-display-here-title)
1417 (when (org-set-local 'org-columns-flyspell-was-active
1418 (org-bound-and-true-p flyspell-mode))
1421 (org-goto-line (car x))
1422 (org-columns-display-here (cdr x)))
1424 (when org-agenda-columns-show-summaries
1425 (org-agenda-colview-summarize cache))))))
1427 (defun org-agenda-colview-summarize (cache)
1428 "Summarize the summarizable columns in column view in the agenda.
1429 This will add overlays to the date lines, to show the summary for each day."
1430 (let* ((fmt (mapcar (lambda (x)
1431 (if (string-match "CLOCKSUM.*" (car x))
1432 (list (match-string 0 (car x))
1433 (nth 1 x) (nth 2 x) ":" 'add_times
1436 org-columns-current-fmt-compiled))
1437 line c c1 stype calc sumfunc props lsum entries prop v title)
1439 (when (delq nil (mapcar 'cadr fmt))
1440 ;; OK, at least one summation column, it makes sense to try this
1441 (goto-char (point-max))
1443 (when (or (get-text-property (point) 'org-date-line)
1444 (eq (get-text-property (point) 'face)
1445 'org-agenda-structure))
1446 ;; OK, this is a date line that should be used
1447 (setq line (org-current-line))
1448 (setq entries nil c cache cache nil)
1449 (while (setq c1 (pop c))
1450 (if (> (car c1) line)
1453 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1454 ;; Compute the summaries for the properties we want,
1455 ;; set nil properties for the rest.
1456 (when (setq entries (mapcar 'cdr entries))
1464 calc (or (nth 7 f) 'identity))
1466 ((equal prop "ITEM")
1467 (cons prop (buffer-substring (point-at-bol)
1469 ((not stype) (cons prop ""))
1470 (t ;; do the summary
1473 (setq v (cdr (assoc prop x)))
1477 (if (not (get-text-property 0 'org-computed v))
1480 (org-columns-string-to-number
1483 (setq lsum (remove nil lsum))
1485 (cond ((> (length lsum) 1)
1486 (org-columns-number-to-string
1487 (apply sumfunc lsum) stype))
1488 ((eq (length lsum) 1)
1489 (org-columns-number-to-string
1492 (put-text-property 0 (length lsum) 'face 'bold lsum)
1493 (unless (eq calc 'identity)
1494 (put-text-property 0 (length lsum) 'org-computed t lsum))
1497 (org-columns-display-here props 'dateline)
1498 (org-set-local 'org-agenda-columns-active t)))
1499 (if (bobp) (throw 'exit t))
1500 (beginning-of-line 0))))))
1502 (defun org-agenda-colview-compute (fmt)
1503 "Compute the relevant columns in the contributing source buffers."
1504 (let ((files org-agenda-contributing-files)
1505 (org-columns-begin-marker (make-marker))
1506 (org-columns-top-level-marker (make-marker))
1508 (while (setq f (pop files))
1509 (setq b (find-buffer-visiting f))
1510 (with-current-buffer (or (buffer-base-buffer b) b)
1514 (org-with-silent-modifications
1515 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
1516 (goto-char (point-min))
1517 (org-columns-get-format-and-top-level)
1518 (while (setq fm (pop fmt))
1519 (cond ((equal (car fm) "CLOCKSUM")
1521 ((equal (car fm) "CLOCKSUM_T")
1522 (org-clock-sum-today))
1524 (setq a (assoc (car fm)
1525 org-columns-current-fmt-compiled))
1526 (equal (nth 4 a) (nth 4 fm)))
1527 (org-columns-compute (car fm)))))))))))
1529 (defun org-format-time-period (interval)
1530 "Convert time in fractional days to days/hours/minutes/seconds."
1531 (if (numberp interval)
1532 (let* ((days (floor interval))
1533 (frac-hours (* 24 (- interval days)))
1534 (hours (floor frac-hours))
1535 (minutes (floor (* 60 (- frac-hours hours))))
1536 (seconds (floor (* 60 (- (* 60 (- frac-hours hours)) minutes)))))
1537 (format "%dd %02dh %02dm %02ds" days hours minutes seconds))
1540 (defun org-estimate-mean-and-var (v)
1541 "Return the mean and variance of an estimate."
1542 (let* ((low (float (car v)))
1543 (high (float (cadr v)))
1544 (mean (/ (+ low high) 2.0))
1545 (var (/ (+ (expt (- mean low) 2.0) (expt (- high mean) 2.0)) 2.0)))
1548 (defun org-estimate-combine (&rest el)
1549 "Combine a list of estimates, using mean and variance.
1550 The mean and variance of the result will be the sum of the means
1551 and variances (respectively) of the individual estimates."
1555 (let ((stats (org-estimate-mean-and-var e)))
1556 (setq mean (+ mean (car stats)))
1557 (setq var (+ var (cadr stats)))))
1559 (let ((stdev (sqrt var)))
1560 (list (- mean stdev) (+ mean stdev)))))
1562 (defun org-estimate-print (e &optional fmt)
1563 "Prepare a string representation of an estimate.
1564 This formats these numbers as two numbers with a \"-\" between them."
1565 (if (null fmt) (set 'fmt "%.0f"))
1566 (format "%s" (mapconcat (lambda (n) (format fmt n)) e "-")))
1568 (defun org-string-to-estimate (s)
1569 "Convert a string to an estimate.
1570 The string should be two numbers joined with a \"-\"."
1571 (if (string-match "\\(.*\\)-\\(.*\\)" s)
1572 (list (string-to-number (match-string 1 s))
1573 (string-to-number(match-string 2 s)))
1574 (list (string-to-number s) (string-to-number s))))
1576 (provide 'org-colview)
1578 ;;; org-colview.el ends here