Update copyright years.
[org-mode.git] / lisp / org-colview.el
blob9dd0340a23382e5c60056cb728f325b6b166c6b8
1 ;;; org-colview.el --- Column View in Org-mode
3 ;; Copyright (C) 2004-2014 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the column view for Org.
29 ;;; Code:
31 (eval-when-compile (require 'cl))
32 (require 'org)
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"))
41 ;;; Column View
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."
69 (interactive)
70 (org-overview)
71 (org-content))
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)))
94 (beginning-of-line 2)
95 (while (and (org-invisible-p2) (not (eobp)))
96 (beginning-of-line 2))
97 (move-to-column col)
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))
106 (move-to-column col)
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)
119 (dotimes (i 10)
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"
125 '("Column"
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]
131 "--"
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]
135 "--"
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]
140 "--"
141 ["CONTENTS" org-columns-content t]
142 ["OVERVIEW" org-overview t]
143 ["Refresh columns display" org-columns-redo t]
144 "--"
145 ["Open link" org-columns-open-link t]
146 "--"
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)
156 ov))
158 (defun org-columns-display-here (&optional props dateline)
159 "Overlay the current line with column display."
160 (interactive)
161 (let* ((fmt org-columns-current-fmt-compiled)
162 (beg (point-at-bol))
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))
170 'default))
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.
179 (unless props
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))))
185 ;; Walk the format
186 (while (setq column (pop fmt))
187 (setq property (car column)
188 title (nth 1 column)
189 ass (if (equal property "ITEM")
190 (cons "ITEM"
191 ;; When in a buffer, get the whole line,
192 ;; we'll clean it later…
193 (if (derived-mode-p 'org-mode)
194 (save-match-data
195 (org-remove-tabs
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))
204 (nth 2 column)
205 (length property))
206 f (format "%%-%d.%ds | " width width)
207 calc (nth 7 column)
208 val (or (cdr ass) "")
209 modval (cond ((and org-columns-modify-value-for-display-function
210 (functionp
211 org-columns-modify-value-for-display-function))
212 (funcall org-columns-modify-value-for-display-function
213 title val))
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
223 val (nth 4 column)))
224 (nth 4 column)))))
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))
242 (save-excursion
243 (goto-char beg)
244 (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
245 ;; Make the rest of the line disappear.
246 (org-unmodified
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."
264 (cond
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."
287 (interactive)
288 (let ((fmt org-columns-current-fmt-compiled)
289 string (title "")
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))
295 (nth 2 column)
296 (length str))
297 widths (push width widths)
298 f (format "%%-%d.%ds | " width width)
299 string (format f str)
300 title (concat title string)))
301 (setq title (concat
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 ;;;###autoload
327 (defun org-columns-remove-overlays ()
328 "Remove all currently active column overlays."
329 (interactive)
330 (when (marker-buffer org-columns-begin-marker)
331 (with-current-buffer (marker-buffer org-columns-begin-marker)
332 (when (local-variable-p 'org-previous-header-line-format)
333 (setq header-line-format org-previous-header-line-format)
334 (kill-local-variable 'org-previous-header-line-format)
335 (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
336 (move-marker org-columns-begin-marker nil)
337 (move-marker org-columns-top-level-marker nil)
338 (org-with-silent-modifications
339 (mapc 'delete-overlay org-columns-overlays)
340 (setq org-columns-overlays nil)
341 (let ((inhibit-read-only t))
342 (remove-text-properties (point-min) (point-max) '(read-only t))))
343 (when org-columns-flyspell-was-active
344 (flyspell-mode 1))
345 (when (local-variable-p 'org-colview-initial-truncate-line-value)
346 (setq truncate-lines org-colview-initial-truncate-line-value)))))
348 (defun org-columns-cleanup-item (item fmt cphr)
349 "Remove from ITEM what is a column in the format FMT.
350 CPHR is the complex heading regexp to use for parsing ITEM."
351 (let (fixitem)
352 (if (not cphr)
353 item
354 (unless (string-match "^\*+ " item)
355 (setq item (concat "* " item) fixitem t))
356 (if (string-match cphr item)
357 (setq item
358 (concat
359 (org-add-props (match-string 1 item) nil
360 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
361 (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
362 (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
363 " " (save-match-data (org-columns-compact-links (or (match-string 4 item) "")))
364 (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))
365 (add-text-properties
366 0 (1+ (match-end 1))
367 (list 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
368 item))
369 (if fixitem (replace-regexp-in-string "^\*+ " "" item) item))))
371 (defun org-columns-compact-links (s)
372 "Replace [[link][desc]] with [desc] or [link]."
373 (while (string-match org-bracket-link-regexp s)
374 (setq s (replace-match
375 (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
376 t t s)))
379 (defun org-columns-show-value ()
380 "Show the full value of the property."
381 (interactive)
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."
389 (interactive)
390 (org-with-silent-modifications
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)
396 (message
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."
409 (interactive "P")
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."
414 (interactive "P")
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 (defvar org-agenda-overriding-columns-format nil
421 "When set, overrides any other format definition for the agenda.
422 Don't set this, this is meant for dynamic scoping.")
424 (defun org-columns-edit-value (&optional key)
425 "Edit the value of the property at point in column view.
426 Where possible, use the standard interface for changing this line."
427 (interactive)
428 (org-columns-check-computed)
429 (let* ((col (current-column))
430 (key (or key (get-char-property (point) 'org-columns-key)))
431 (value (get-char-property (point) 'org-columns-value))
432 (bol (point-at-bol)) (eol (point-at-eol))
433 (pom (or (get-text-property bol 'org-hd-marker)
434 (point))) ; keep despite of compiler waring
435 (line-overlays
436 (delq nil (mapcar (lambda (x)
437 (and (eq (overlay-buffer x) (current-buffer))
438 (>= (overlay-start x) bol)
439 (<= (overlay-start x) eol)
441 org-columns-overlays)))
442 (org-columns-time (time-to-number-of-days (current-time)))
443 nval eval allowed)
444 (cond
445 ((equal key "CLOCKSUM")
446 (error "This special column cannot be edited"))
447 ((equal key "ITEM")
448 (setq eval '(org-with-point-at pom
449 (org-edit-headline))))
450 ((equal key "TODO")
451 (setq eval '(org-with-point-at
453 (call-interactively 'org-todo))))
454 ((equal key "PRIORITY")
455 (setq eval '(org-with-point-at pom
456 (call-interactively 'org-priority))))
457 ((equal key "TAGS")
458 (setq eval '(org-with-point-at pom
459 (let ((org-fast-tag-selection-single-key
460 (if (eq org-fast-tag-selection-single-key 'expert)
461 t org-fast-tag-selection-single-key)))
462 (call-interactively 'org-set-tags)))))
463 ((equal key "DEADLINE")
464 (setq eval '(org-with-point-at pom
465 (call-interactively 'org-deadline))))
466 ((equal key "SCHEDULED")
467 (setq eval '(org-with-point-at pom
468 (call-interactively 'org-schedule))))
469 ((equal key "BEAMER_env")
470 (setq eval '(org-with-point-at pom
471 (call-interactively 'org-beamer-select-environment))))
473 (setq allowed (org-property-get-allowed-values pom key 'table))
474 (if allowed
475 (setq nval (org-icompleting-read
476 "Value: " allowed nil
477 (not (get-text-property 0 'org-unrestricted
478 (caar allowed)))))
479 (setq nval (read-string "Edit: " value)))
480 (setq nval (org-trim nval))
481 (when (not (equal nval value))
482 (setq eval '(org-entry-put pom key nval)))))
483 (when eval
485 (cond
486 ((equal major-mode 'org-agenda-mode)
487 (org-columns-eval eval)
488 ;; The following let preserves the current format, and makes sure
489 ;; that in only a single file things need to be updated.
490 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
491 (buffer (marker-buffer pom))
492 (org-agenda-contributing-files
493 (list (with-current-buffer buffer
494 (buffer-file-name (buffer-base-buffer))))))
495 (org-agenda-columns)))
497 (let ((inhibit-read-only t))
498 (org-with-silent-modifications
499 (remove-text-properties
500 (max (point-min) (1- bol)) eol '(read-only t)))
501 (unwind-protect
502 (progn
503 (setq org-columns-overlays
504 (org-delete-all line-overlays org-columns-overlays))
505 (mapc 'delete-overlay line-overlays)
506 (org-columns-eval eval))
507 (org-columns-display-here)))
508 (org-move-to-column col)
509 (if (and (derived-mode-p 'org-mode)
510 (nth 3 (assoc key org-columns-current-fmt-compiled)))
511 (org-columns-update key)))))))
513 (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
514 "Edit the current headline, the part without TODO keyword, TAGS."
515 (org-back-to-heading)
516 (when (looking-at org-todo-line-regexp)
517 (let ((pos (point))
518 (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
519 (txt (match-string 3))
520 (post "")
521 txt2)
522 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@#%]+:[ \t]*$") txt)
523 (setq post (match-string 0 txt)
524 txt (substring txt 0 (match-beginning 0))))
525 (setq txt2 (read-string "Edit: " txt))
526 (when (not (equal txt txt2))
527 (goto-char pos)
528 (insert pre txt2 post)
529 (delete-region (point) (point-at-eol))
530 (org-set-tags nil t)))))
532 (defun org-columns-edit-allowed ()
533 "Edit the list of allowed values for the current property."
534 (interactive)
535 (let* ((pom (or (org-get-at-bol 'org-marker)
536 (org-get-at-bol 'org-hd-marker)
537 (point)))
538 (key (get-char-property (point) 'org-columns-key))
539 (key1 (concat key "_ALL"))
540 (allowed (org-entry-get pom key1 t))
541 nval)
542 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
543 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
544 (setq nval (read-string "Allowed: " allowed))
545 (org-entry-put
546 (cond ((marker-position org-entry-property-inherited-from)
547 org-entry-property-inherited-from)
548 ((marker-position org-columns-top-level-marker)
549 org-columns-top-level-marker)
550 (t pom))
551 key1 nval)))
553 (defun org-columns-eval (form)
554 (let (hidep)
555 (save-excursion
556 (beginning-of-line 1)
557 ;; `next-line' is needed here, because it skips invisible line.
558 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
559 (setq hidep (org-at-heading-p 1)))
560 (eval form)
561 (and hidep (hide-entry))))
563 (defun org-columns-previous-allowed-value ()
564 "Switch to the previous allowed value for this column."
565 (interactive)
566 (org-columns-next-allowed-value t))
568 (defun org-columns-next-allowed-value (&optional previous nth)
569 "Switch to the next allowed value for this column.
570 When PREVIOUS is set, go to the previous value. When NTH is
571 an integer, select that value."
572 (interactive)
573 (org-columns-check-computed)
574 (let* ((col (current-column))
575 (key (get-char-property (point) 'org-columns-key))
576 (value (get-char-property (point) 'org-columns-value))
577 (bol (point-at-bol)) (eol (point-at-eol))
578 (pom (or (get-text-property bol 'org-hd-marker)
579 (point))) ; keep despite of compiler waring
580 (line-overlays
581 (delq nil (mapcar (lambda (x)
582 (and (eq (overlay-buffer x) (current-buffer))
583 (>= (overlay-start x) bol)
584 (<= (overlay-start x) eol)
586 org-columns-overlays)))
587 (allowed (or (org-property-get-allowed-values pom key)
588 (and (memq
589 (nth 4 (assoc key org-columns-current-fmt-compiled))
590 '(checkbox checkbox-n-of-m checkbox-percent))
591 '("[ ]" "[X]"))
592 (org-colview-construct-allowed-dates value)))
593 nval)
594 (when (integerp nth)
595 (setq nth (1- nth))
596 (if (= nth -1) (setq nth 9)))
597 (when (equal key "ITEM")
598 (error "Cannot edit item headline from here"))
599 (unless (or allowed (member key '("SCHEDULED" "DEADLINE" "CLOCKSUM")))
600 (error "Allowed values for this property have not been defined"))
601 (if (member key '("SCHEDULED" "DEADLINE" "CLOCKSUM"))
602 (setq nval (if previous 'earlier 'later))
603 (if previous (setq allowed (reverse allowed)))
604 (cond
605 (nth
606 (setq nval (nth nth allowed))
607 (if (not nval)
608 (error "There are only %d allowed values for property `%s'"
609 (length allowed) key)))
610 ((member value allowed)
611 (setq nval (or (car (cdr (member value allowed)))
612 (car allowed)))
613 (if (equal nval value)
614 (error "Only one allowed value for this property")))
615 (t (setq nval (car allowed)))))
616 (cond
617 ((equal major-mode 'org-agenda-mode)
618 (org-columns-eval '(org-entry-put pom key nval))
619 ;; The following let preserves the current format, and makes sure
620 ;; that in only a single file things need to be updated.
621 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
622 (buffer (marker-buffer pom))
623 (org-agenda-contributing-files
624 (list (with-current-buffer buffer
625 (buffer-file-name (buffer-base-buffer))))))
626 (org-agenda-columns)))
628 (let ((inhibit-read-only t))
629 (remove-text-properties (1- bol) eol '(read-only t))
630 (unwind-protect
631 (progn
632 (setq org-columns-overlays
633 (org-delete-all line-overlays org-columns-overlays))
634 (mapc 'delete-overlay line-overlays)
635 (org-columns-eval '(org-entry-put pom key nval)))
636 (org-columns-display-here)))
637 (org-move-to-column col)
638 (and (nth 3 (assoc key org-columns-current-fmt-compiled))
639 (org-columns-update key))))))
641 (defun org-colview-construct-allowed-dates (s)
642 "Construct a list of three dates around the date in S.
643 This respects the format of the time stamp in S, active or non-active,
644 and also including time or not. S must be just a time stamp, no text
645 around it."
646 (when (and s (string-match (concat "^" org-ts-regexp3 "$") s))
647 (let* ((time (org-parse-time-string s 'nodefaults))
648 (active (equal (string-to-char s) ?<))
649 (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
650 time-before time-after)
651 (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
652 (setf (car time) (or (car time) 0))
653 (setf (nth 1 time) (or (nth 1 time) 0))
654 (setf (nth 2 time) (or (nth 2 time) 0))
655 (setq time-before (copy-sequence time))
656 (setq time-after (copy-sequence time))
657 (setf (nth 3 time-before) (1- (nth 3 time)))
658 (setf (nth 3 time-after) (1+ (nth 3 time)))
659 (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
660 (list time-before time time-after)))))
662 (defun org-verify-version (task)
663 (cond
664 ((eq task 'columns)
665 (if (or (featurep 'xemacs)
666 (< emacs-major-version 22))
667 (error "Emacs 22 is required for the columns feature")))))
669 (defun org-columns-open-link (&optional arg)
670 (interactive "P")
671 (let ((value (get-char-property (point) 'org-columns-value)))
672 (org-open-link-from-string value arg)))
674 ;;;###autoload
675 (defun org-columns-get-format-and-top-level ()
676 (let ((fmt (org-columns-get-format)))
677 (org-columns-goto-top-level)
678 fmt))
680 (defun org-columns-get-format (&optional fmt-string)
681 (interactive)
682 (let (fmt-as-property fmt)
683 (when (condition-case nil (org-back-to-heading) (error nil))
684 (setq fmt-as-property (org-entry-get nil "COLUMNS" t)))
685 (setq fmt (or fmt-string fmt-as-property org-columns-default-format))
686 (org-set-local 'org-columns-current-fmt fmt)
687 (org-columns-compile-format fmt)
688 fmt))
690 (defun org-columns-goto-top-level ()
691 (when (condition-case nil (org-back-to-heading) (error nil))
692 (org-entry-get nil "COLUMNS" t))
693 (if (marker-position org-entry-property-inherited-from)
694 (move-marker org-columns-top-level-marker org-entry-property-inherited-from)
695 (move-marker org-columns-top-level-marker (point))))
697 ;;;###autoload
698 (defun org-columns (&optional columns-fmt-string)
699 "Turn on column view on an org-mode file.
700 When COLUMNS-FMT-STRING is non-nil, use it as the column format."
701 (interactive)
702 (org-verify-version 'columns)
703 (org-columns-remove-overlays)
704 (move-marker org-columns-begin-marker (point))
705 (let ((org-columns-time (time-to-number-of-days (current-time)))
706 beg end fmt cache maxwidths)
707 (org-columns-goto-top-level)
708 (setq fmt (org-columns-get-format columns-fmt-string))
709 (save-excursion
710 (goto-char org-columns-top-level-marker)
711 (setq beg (point))
712 (unless org-columns-inhibit-recalculation
713 (org-columns-compute-all))
714 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
715 (point-max)))
716 ;; Get and cache the properties
717 (goto-char beg)
718 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
719 (save-excursion
720 (save-restriction
721 (narrow-to-region beg end)
722 (org-clock-sum))))
723 (when (assoc "CLOCKSUM_T" org-columns-current-fmt-compiled)
724 (save-excursion
725 (save-restriction
726 (narrow-to-region beg end)
727 (org-clock-sum-today))))
728 (while (re-search-forward org-outline-regexp-bol end t)
729 (if (and org-columns-skip-archived-trees
730 (looking-at (concat ".*:" org-archive-tag ":")))
731 (org-end-of-subtree t)
732 (push (cons (org-current-line) (org-entry-properties)) cache)))
733 (when cache
734 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
735 (org-set-local 'org-columns-current-maxwidths maxwidths)
736 (org-columns-display-here-title)
737 (when (org-set-local 'org-columns-flyspell-was-active
738 (org-bound-and-true-p flyspell-mode))
739 (flyspell-mode 0))
740 (unless (local-variable-p 'org-colview-initial-truncate-line-value)
741 (org-set-local 'org-colview-initial-truncate-line-value
742 truncate-lines))
743 (setq truncate-lines t)
744 (mapc (lambda (x)
745 (org-goto-line (car x))
746 (org-columns-display-here (cdr x)))
747 cache)))))
749 (eval-when-compile (defvar org-columns-time))
751 (defvar org-columns-compile-map
752 '(("none" none +)
753 (":" add_times +)
754 ("+" add_numbers +)
755 ("$" currency +)
756 ("X" checkbox +)
757 ("X/" checkbox-n-of-m +)
758 ("X%" checkbox-percent +)
759 ("max" max_numbers max)
760 ("min" min_numbers min)
761 ("mean" mean_numbers
762 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
763 (":max" max_times max)
764 (":min" min_times min)
765 (":mean" mean_times
766 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
767 ("@min" min_age min (lambda (x) (- org-columns-time x)))
768 ("@max" max_age max (lambda (x) (- org-columns-time x)))
769 ("@mean" mean_age
770 (lambda (&rest x) (/ (apply '+ x) (float (length x))))
771 (lambda (x) (- org-columns-time x)))
772 ("est+" estimate org-estimate-combine))
773 "Operator <-> format,function,calc map.
774 Used to compile/uncompile columns format and completing read in
775 interactive function `org-columns-new'.
777 operator string used in #+COLUMNS definition describing the
778 summary type
779 format symbol describing summary type selected interactively in
780 `org-columns-new' and internally in
781 `org-columns-number-to-string' and
782 `org-columns-string-to-number'
783 function called with a list of values as argument to calculate
784 the summary value
785 calc function called on every element before summarizing. This is
786 optional and should only be specified if needed")
788 (defun org-columns-new (&optional prop title width op fmt fun &rest rest)
789 "Insert a new column, to the left of the current column."
790 (interactive)
791 (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
792 cell)
793 (setq prop (org-icompleting-read
794 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
795 nil nil prop))
796 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
797 (setq width (read-string "Column width: " (if width (number-to-string width))))
798 (if (string-match "\\S-" width)
799 (setq width (string-to-number width))
800 (setq width nil))
801 (setq fmt (org-icompleting-read
802 "Summary [none]: "
803 (mapcar (lambda (x) (list (symbol-name (cadr x))))
804 org-columns-compile-map)
805 nil t))
806 (setq fmt (intern fmt)
807 fun (cdr (assoc fmt (mapcar 'cdr org-columns-compile-map))))
808 (if (eq fmt 'none) (setq fmt nil))
809 (if editp
810 (progn
811 (setcar editp prop)
812 (setcdr editp (list title width nil fmt nil fun)))
813 (setq cell (nthcdr (1- (current-column))
814 org-columns-current-fmt-compiled))
815 (setcdr cell (cons (list prop title width nil fmt nil
816 (car fun) (cadr fun))
817 (cdr cell))))
818 (org-columns-store-format)
819 (org-columns-redo)))
821 (defun org-columns-delete ()
822 "Delete the column at point from columns view."
823 (interactive)
824 (let* ((n (current-column))
825 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
826 (when (y-or-n-p
827 (format "Are you sure you want to remove column \"%s\"? " title))
828 (setq org-columns-current-fmt-compiled
829 (delq (nth n org-columns-current-fmt-compiled)
830 org-columns-current-fmt-compiled))
831 (org-columns-store-format)
832 (org-columns-redo)
833 (if (>= (current-column) (length org-columns-current-fmt-compiled))
834 (backward-char 1)))))
836 (defun org-columns-edit-attributes ()
837 "Edit the attributes of the current column."
838 (interactive)
839 (let* ((n (current-column))
840 (info (nth n org-columns-current-fmt-compiled)))
841 (apply 'org-columns-new info)))
843 (defun org-columns-widen (arg)
844 "Make the column wider by ARG characters."
845 (interactive "p")
846 (let* ((n (current-column))
847 (entry (nth n org-columns-current-fmt-compiled))
848 (width (or (nth 2 entry)
849 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
850 (setq width (max 1 (+ width arg)))
851 (setcar (nthcdr 2 entry) width)
852 (org-columns-store-format)
853 (org-columns-redo)))
855 (defun org-columns-narrow (arg)
856 "Make the column narrower by ARG characters."
857 (interactive "p")
858 (org-columns-widen (- arg)))
860 (defun org-columns-move-right ()
861 "Swap this column with the one to the right."
862 (interactive)
863 (let* ((n (current-column))
864 (cell (nthcdr n org-columns-current-fmt-compiled))
866 (when (>= n (1- (length org-columns-current-fmt-compiled)))
867 (error "Cannot shift this column further to the right"))
868 (setq e (car cell))
869 (setcar cell (car (cdr cell)))
870 (setcdr cell (cons e (cdr (cdr cell))))
871 (org-columns-store-format)
872 (org-columns-redo)
873 (forward-char 1)))
875 (defun org-columns-move-left ()
876 "Swap this column with the one to the left."
877 (interactive)
878 (let* ((n (current-column)))
879 (when (= n 0)
880 (error "Cannot shift this column further to the left"))
881 (backward-char 1)
882 (org-columns-move-right)
883 (backward-char 1)))
885 (defun org-columns-store-format ()
886 "Store the text version of the current columns format in appropriate place.
887 This is either in the COLUMNS property of the node starting the current column
888 display, or in the #+COLUMNS line of the current buffer."
889 (let (fmt (cnt 0))
890 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
891 (org-set-local 'org-columns-current-fmt fmt)
892 (if (marker-position org-columns-top-level-marker)
893 (save-excursion
894 (goto-char org-columns-top-level-marker)
895 (if (and (org-at-heading-p)
896 (org-entry-get nil "COLUMNS"))
897 (org-entry-put nil "COLUMNS" fmt)
898 (goto-char (point-min))
899 ;; Overwrite all #+COLUMNS lines....
900 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
901 (setq cnt (1+ cnt))
902 (replace-match (concat "#+COLUMNS: " fmt) t t))
903 (unless (> cnt 0)
904 (goto-char (point-min))
905 (or (org-at-heading-p t) (outline-next-heading))
906 (let ((inhibit-read-only t))
907 (insert-before-markers "#+COLUMNS: " fmt "\n")))
908 (org-set-local 'org-columns-default-format fmt))))))
910 (defun org-columns-get-autowidth-alist (s cache)
911 "Derive the maximum column widths from the format and the cache."
912 (let ((start 0) rtn)
913 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
914 (push (cons (match-string 1 s) 1) rtn)
915 (setq start (match-end 0)))
916 (mapc (lambda (x)
917 (setcdr x (apply 'max
918 (mapcar
919 (lambda (y)
920 (length (or (cdr (assoc (car x) (cdr y))) " ")))
921 cache))))
922 rtn)
923 rtn))
925 (defun org-columns-compute-all ()
926 "Compute all columns that have operators defined."
927 (org-with-silent-modifications
928 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
929 (let ((columns org-columns-current-fmt-compiled)
930 (org-columns-time (time-to-number-of-days (current-time)))
931 col)
932 (while (setq col (pop columns))
933 (when (nth 3 col)
934 (save-excursion
935 (org-columns-compute (car col)))))))
937 (defun org-columns-update (property)
938 "Recompute PROPERTY, and update the columns display for it."
939 (org-columns-compute property)
940 (let (fmt val pos)
941 (save-excursion
942 (mapc (lambda (ov)
943 (when (equal (overlay-get ov 'org-columns-key) property)
944 (setq pos (overlay-start ov))
945 (goto-char pos)
946 (when (setq val (cdr (assoc property
947 (get-text-property
948 (point-at-bol) 'org-summaries))))
949 (setq fmt (overlay-get ov 'org-columns-format))
950 (overlay-put ov 'org-columns-value val)
951 (overlay-put ov 'display (format fmt val)))))
952 org-columns-overlays))))
954 (defvar org-inlinetask-min-level
955 (if (featurep 'org-inlinetask) org-inlinetask-min-level 15))
957 ;;;###autoload
958 (defun org-columns-compute (property)
959 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
960 (interactive)
961 (let* ((re org-outline-regexp-bol)
962 (lmax 30) ; Does anyone use deeper levels???
963 (lvals (make-vector lmax nil))
964 (lflag (make-vector lmax nil))
965 (level 0)
966 (ass (assoc property org-columns-current-fmt-compiled))
967 (format (nth 4 ass))
968 (printf (nth 5 ass))
969 (fun (nth 6 ass))
970 (calc (or (nth 7 ass) 'identity))
971 (beg org-columns-top-level-marker)
972 (inminlevel org-inlinetask-min-level)
973 (last-level org-inlinetask-min-level)
974 val valflag flag end sumpos sum-alist sum str str1 useval)
975 (save-excursion
976 ;; Find the region to compute
977 (goto-char beg)
978 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
979 (goto-char end)
980 ;; Walk the tree from the back and do the computations
981 (while (re-search-backward re beg t)
982 (setq sumpos (match-beginning 0)
983 last-level (if (not (or (zerop level) (eq level inminlevel)))
984 level last-level)
985 level (org-outline-level)
986 val (org-entry-get nil property)
987 valflag (and val (string-match "\\S-" val)))
988 (cond
989 ((< level last-level)
990 ;; put the sum of lower levels here as a property
991 (setq sum (+ (if (and (/= last-level inminlevel)
992 (aref lvals last-level))
993 (apply fun (aref lvals last-level)) 0)
994 (if (aref lvals inminlevel)
995 (apply fun (aref lvals inminlevel)) 0))
996 flag (or (aref lflag last-level) ; any valid entries from children?
997 (aref lflag inminlevel)) ; or inline tasks?
998 str (org-columns-number-to-string sum format printf)
999 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
1000 useval (if flag str1 (if valflag val ""))
1001 sum-alist (get-text-property sumpos 'org-summaries))
1002 (if (assoc property sum-alist)
1003 (setcdr (assoc property sum-alist) useval)
1004 (push (cons property useval) sum-alist)
1005 (org-with-silent-modifications
1006 (add-text-properties sumpos (1+ sumpos)
1007 (list 'org-summaries sum-alist))))
1008 (when (and val (not (equal val (if flag str val))))
1009 (org-entry-put nil property (if flag str val)))
1010 ;; add current to current level accumulator
1011 (when (or flag valflag)
1012 (push (if flag
1014 (funcall calc (org-columns-string-to-number
1015 (if flag str val) format)))
1016 (aref lvals level))
1017 (aset lflag level t))
1018 ;; clear accumulators for deeper levels
1019 (loop for l from (1+ level) to (1- lmax) do
1020 (aset lvals l nil)
1021 (aset lflag l nil)))
1022 ((>= level last-level)
1023 ;; add what we have here to the accumulator for this level
1024 (when valflag
1025 (push (funcall calc (org-columns-string-to-number val format))
1026 (aref lvals level))
1027 (aset lflag level t)))
1028 (t (error "This should not happen")))))))
1030 (defun org-columns-redo ()
1031 "Construct the column display again."
1032 (interactive)
1033 (message "Recomputing columns...")
1034 (let ((line (org-current-line))
1035 (col (current-column)))
1036 (save-excursion
1037 (if (marker-position org-columns-begin-marker)
1038 (goto-char org-columns-begin-marker))
1039 (org-columns-remove-overlays)
1040 (if (derived-mode-p 'org-mode)
1041 (call-interactively 'org-columns)
1042 (org-agenda-redo)
1043 (call-interactively 'org-agenda-columns)))
1044 (org-goto-line line)
1045 (move-to-column col))
1046 (message "Recomputing columns...done"))
1048 (defun org-columns-not-in-agenda ()
1049 (if (eq major-mode 'org-agenda-mode)
1050 (error "This command is only allowed in Org-mode buffers")))
1052 (defun org-string-to-number (s)
1053 "Convert string to number, and interpret hh:mm:ss."
1054 (if (not (string-match ":" s))
1055 (string-to-number s)
1056 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1057 (while l
1058 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1059 sum)))
1061 ;;;###autoload
1062 (defun org-columns-number-to-string (n fmt &optional printf)
1063 "Convert a computed column number to a string value, according to FMT."
1064 (cond
1065 ((memq fmt '(estimate)) (org-estimate-print n printf))
1066 ((not (numberp n)) "")
1067 ((memq fmt '(add_times max_times min_times mean_times))
1068 (org-hours-to-clocksum-string n))
1069 ((eq fmt 'checkbox)
1070 (cond ((= n (floor n)) "[X]")
1071 ((> n 1.) "[-]")
1072 (t "[ ]")))
1073 ((memq fmt '(checkbox-n-of-m checkbox-percent))
1074 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
1075 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
1076 (printf (format printf n))
1077 ((eq fmt 'currency)
1078 (format "%.2f" n))
1079 ((memq fmt '(min_age max_age mean_age))
1080 (org-format-time-period n))
1081 (t (number-to-string n))))
1083 (defun org-nofm-to-completion (n m &optional percent)
1084 (if (not percent)
1085 (format "[%d/%d]" n m)
1086 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
1089 (defun org-columns-string-to-number (s fmt)
1090 "Convert a column value to a number that can be used for column computing."
1091 (if s
1092 (cond
1093 ((memq fmt '(min_age max_age mean_age))
1094 (cond ((string= s "") org-columns-time)
1095 ((string-match
1096 "\\([0-9]+\\)d \\([0-9]+\\)h \\([0-9]+\\)m \\([0-9]+\\)s"
1098 (+ (* 60 (+ (* 60 (+ (* 24 (string-to-number (match-string 1 s)))
1099 (string-to-number (match-string 2 s))))
1100 (string-to-number (match-string 3 s))))
1101 (string-to-number (match-string 4 s))))
1102 (t (time-to-number-of-days (apply 'encode-time
1103 (org-parse-time-string s t))))))
1104 ((string-match ":" s)
1105 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1106 (while l
1107 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1108 sum))
1109 ((string-match (concat "\\([0-9.]+\\) *\\("
1110 (regexp-opt (mapcar 'car org-effort-durations))
1111 "\\)") s)
1112 (setq s (concat "0:" (org-duration-string-to-minutes s t)))
1113 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1114 (while l
1115 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1116 sum))
1117 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
1118 (if (equal s "[X]") 1. 0.000001))
1119 ((memq fmt '(estimate)) (org-string-to-estimate s))
1120 (t (string-to-number s)))))
1122 (defun org-columns-uncompile-format (cfmt)
1123 "Turn the compiled columns format back into a string representation."
1124 (let ((rtn "") e s prop title op op-match width fmt printf fun calc)
1125 (while (setq e (pop cfmt))
1126 (setq prop (car e)
1127 title (nth 1 e)
1128 width (nth 2 e)
1129 op (nth 3 e)
1130 fmt (nth 4 e)
1131 printf (nth 5 e)
1132 fun (nth 6 e)
1133 calc (nth 7 e))
1134 (when (setq op-match (rassoc (list fmt fun calc) org-columns-compile-map))
1135 (setq op (car op-match)))
1136 (if (and op printf) (setq op (concat op ";" printf)))
1137 (if (equal title prop) (setq title nil))
1138 (setq s (concat "%" (if width (number-to-string width))
1139 prop
1140 (if title (concat "(" title ")"))
1141 (if op (concat "{" op "}"))))
1142 (setq rtn (concat rtn " " s)))
1143 (org-trim rtn)))
1145 (defun org-columns-compile-format (fmt)
1146 "Turn a column format string into an alist of specifications.
1147 The alist has one entry for each column in the format. The elements of
1148 that list are:
1149 property the property
1150 title the title field for the columns
1151 width the column width in characters, can be nil for automatic
1152 operator the operator if any
1153 format the output format for computed results, derived from operator
1154 printf a printf format for computed values
1155 fun the lisp function to compute summary values, derived from operator
1156 calc function to get values from base elements"
1157 (let ((start 0) width prop title op op-match f printf fun calc)
1158 (setq org-columns-current-fmt-compiled nil)
1159 (while (string-match
1160 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
1161 fmt start)
1162 (setq start (match-end 0)
1163 width (match-string 1 fmt)
1164 prop (match-string 2 fmt)
1165 title (or (match-string 3 fmt) prop)
1166 op (match-string 4 fmt)
1167 f nil
1168 printf nil
1169 fun '+
1170 calc nil)
1171 (if width (setq width (string-to-number width)))
1172 (when (and op (string-match ";" op))
1173 (setq printf (substring op (match-end 0))
1174 op (substring op 0 (match-beginning 0))))
1175 (when (setq op-match (assoc op org-columns-compile-map))
1176 (setq f (cadr op-match)
1177 fun (caddr op-match)
1178 calc (cadddr op-match)))
1179 (push (list prop title width op f printf fun calc)
1180 org-columns-current-fmt-compiled))
1181 (setq org-columns-current-fmt-compiled
1182 (nreverse org-columns-current-fmt-compiled))))
1185 ;;; Dynamic block for Column view
1187 (defvar org-heading-regexp) ; defined in org.el
1188 (defvar org-heading-keyword-regexp-format) ; defined in org.el
1189 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
1190 "Get the column view of the current buffer or subtree.
1191 The first optional argument MAXLEVEL sets the level limit. A
1192 second optional argument SKIP-EMPTY-ROWS tells whether to skip
1193 empty rows, an empty row being one where all the column view
1194 specifiers except ITEM are empty. This function returns a list
1195 containing the title row and all other rows. Each row is a list
1196 of fields."
1197 (save-excursion
1198 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1199 (re-comment (format org-heading-keyword-regexp-format
1200 org-comment-string))
1201 (re-archive (concat ".*:" org-archive-tag ":"))
1202 (n (length title)) row tbl)
1203 (goto-char (point-min))
1204 (while (re-search-forward org-heading-regexp nil t)
1205 (catch 'next
1206 (when (and (or (null maxlevel)
1207 (>= maxlevel
1208 (if org-odd-levels-only
1209 (/ (1+ (length (match-string 1))) 2)
1210 (length (match-string 1)))))
1211 (get-char-property (match-beginning 0) 'org-columns-key))
1212 (when (save-excursion
1213 (goto-char (point-at-bol))
1214 (or (looking-at re-comment)
1215 (looking-at re-archive)))
1216 (org-end-of-subtree t)
1217 (throw 'next t))
1218 (setq row nil)
1219 (loop for i from 0 to (1- n) do
1220 (push
1221 (org-quote-vert
1222 (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
1223 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
1224 ""))
1225 row))
1226 (setq row (nreverse row))
1227 (unless (and skip-empty-rows
1228 (eq 1 (length (delete "" (delete-dups (copy-sequence row))))))
1229 (push row tbl)))))
1230 (append (list title 'hline) (nreverse tbl)))))
1232 ;;;###autoload
1233 (defun org-dblock-write:columnview (params)
1234 "Write the column view table.
1235 PARAMS is a property list of parameters:
1237 :width enforce same column widths with <N> specifiers.
1238 :id the :ID: property of the entry where the columns view
1239 should be built. When the symbol `local', call locally.
1240 When `global' call column view with the cursor at the beginning
1241 of the buffer (usually this means that the whole buffer switches
1242 to column view). When \"file:path/to/file.org\", invoke column
1243 view at the start of that file. Otherwise, the ID is located
1244 using `org-id-find'.
1245 :hlines When t, insert a hline before each item. When a number, insert
1246 a hline before each level <= that number.
1247 :vlines When t, make each column a colgroup to enforce vertical lines.
1248 :maxlevel When set to a number, don't capture headlines below this level.
1249 :skip-empty-rows
1250 When t, skip rows where all specifiers other than ITEM are empty.
1251 :format When non-nil, specify the column view format to use."
1252 (let ((pos (point-marker))
1253 (hlines (plist-get params :hlines))
1254 (vlines (plist-get params :vlines))
1255 (maxlevel (plist-get params :maxlevel))
1256 (content-lines (org-split-string (plist-get params :content) "\n"))
1257 (skip-empty-rows (plist-get params :skip-empty-rows))
1258 (columns-fmt (plist-get params :format))
1259 (case-fold-search t)
1260 tbl id idpos nfields tmp recalc line
1261 id-as-string view-file view-pos)
1262 (when (setq id (plist-get params :id))
1263 (setq id-as-string (cond ((numberp id) (number-to-string id))
1264 ((symbolp id) (symbol-name id))
1265 ((stringp id) id)
1266 (t "")))
1267 (cond ((not id) nil)
1268 ((eq id 'global) (setq view-pos (point-min)))
1269 ((eq id 'local))
1270 ((string-match "^file:\\(.*\\)" id-as-string)
1271 (setq view-file (match-string 1 id-as-string)
1272 view-pos 1)
1273 (unless (file-exists-p view-file)
1274 (error "No such file: \"%s\"" id-as-string)))
1275 ((setq idpos (org-find-entry-with-id id))
1276 (setq view-pos idpos))
1277 ((setq idpos (org-id-find id))
1278 (setq view-file (car idpos))
1279 (setq view-pos (cdr idpos)))
1280 (t (error "Cannot find entry with :ID: %s" id))))
1281 (with-current-buffer (if view-file
1282 (get-file-buffer view-file)
1283 (current-buffer))
1284 (save-excursion
1285 (save-restriction
1286 (widen)
1287 (goto-char (or view-pos (point)))
1288 (org-columns columns-fmt)
1289 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1290 (setq nfields (length (car tbl)))
1291 (org-columns-quit))))
1292 (goto-char pos)
1293 (move-marker pos nil)
1294 (when tbl
1295 (when (plist-get params :hlines)
1296 (setq tmp nil)
1297 (while tbl
1298 (if (eq (car tbl) 'hline)
1299 (push (pop tbl) tmp)
1300 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1301 (if (and (not (eq (car tmp) 'hline))
1302 (or (eq hlines t)
1303 (and (numberp hlines)
1304 (<= (- (match-end 1) (match-beginning 1))
1305 hlines))))
1306 (push 'hline tmp)))
1307 (push (pop tbl) tmp)))
1308 (setq tbl (nreverse tmp)))
1309 (when vlines
1310 (setq tbl (mapcar (lambda (x)
1311 (if (eq 'hline x) x (cons "" x)))
1312 tbl))
1313 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1314 (when content-lines
1315 (while (string-match "^#" (car content-lines))
1316 (insert (pop content-lines) "\n")))
1317 (setq pos (point))
1318 (insert (org-listtable-to-string tbl))
1319 (when (plist-get params :width)
1320 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1321 org-columns-current-widths "|")))
1322 (while (setq line (pop content-lines))
1323 (when (string-match "^#" line)
1324 (insert "\n" line)
1325 (when (string-match "^[ \t]*#\\+tblfm" line)
1326 (setq recalc t))))
1327 (if recalc
1328 (progn (goto-char pos) (org-table-recalculate 'all))
1329 (goto-char pos)
1330 (org-table-align)))))
1332 (defun org-listtable-to-string (tbl)
1333 "Convert a listtable TBL to a string that contains the Org-mode table.
1334 The table still need to be aligned. The resulting string has no leading
1335 and tailing newline characters."
1336 (mapconcat
1337 (lambda (x)
1338 (cond
1339 ((listp x)
1340 (concat "|" (mapconcat 'identity x "|") "|"))
1341 ((eq x 'hline) "|-|")
1342 (t (error "Garbage in listtable: %s" x))))
1343 tbl "\n"))
1345 ;;;###autoload
1346 (defun org-insert-columns-dblock ()
1347 "Create a dynamic block capturing a column view table."
1348 (interactive)
1349 (let ((defaults '(:name "columnview" :hlines 1))
1350 (id (org-icompleting-read
1351 "Capture columns (local, global, entry with :ID: property) [local]: "
1352 (append '(("global") ("local"))
1353 (mapcar 'list (org-property-values "ID"))))))
1354 (if (equal id "") (setq id 'local))
1355 (if (equal id "global") (setq id 'global))
1356 (setq defaults (append defaults (list :id id)))
1357 (org-create-dblock defaults)
1358 (org-update-dblock)))
1360 ;;; Column view in the agenda
1362 (defvar org-agenda-view-columns-initially nil
1363 "When set, switch to columns view immediately after creating the agenda.")
1365 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1366 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1367 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1369 ;;;###autoload
1370 (defun org-agenda-columns ()
1371 "Turn on or update column view in the agenda."
1372 (interactive)
1373 (org-verify-version 'columns)
1374 (org-columns-remove-overlays)
1375 (move-marker org-columns-begin-marker (point))
1376 (let ((org-columns-time (time-to-number-of-days (current-time)))
1377 cache maxwidths m p a d fmt)
1378 (cond
1379 ((and (boundp 'org-agenda-overriding-columns-format)
1380 org-agenda-overriding-columns-format)
1381 (setq fmt org-agenda-overriding-columns-format))
1382 ((setq m (org-get-at-bol 'org-hd-marker))
1383 (setq fmt (or (org-entry-get m "COLUMNS" t)
1384 (with-current-buffer (marker-buffer m)
1385 org-columns-default-format))))
1386 ((and (boundp 'org-columns-current-fmt)
1387 (local-variable-p 'org-columns-current-fmt)
1388 org-columns-current-fmt)
1389 (setq fmt org-columns-current-fmt))
1390 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
1391 (setq m (get-text-property m 'org-hd-marker))
1392 (setq fmt (or (org-entry-get m "COLUMNS" t)
1393 (with-current-buffer (marker-buffer m)
1394 org-columns-default-format)))))
1395 (setq fmt (or fmt org-columns-default-format))
1396 (org-set-local 'org-columns-current-fmt fmt)
1397 (org-columns-compile-format fmt)
1398 (when org-agenda-columns-compute-summary-properties
1399 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1400 (save-excursion
1401 ;; Get and cache the properties
1402 (goto-char (point-min))
1403 (while (not (eobp))
1404 (when (setq m (or (org-get-at-bol 'org-hd-marker)
1405 (org-get-at-bol 'org-marker)))
1406 (setq p (org-entry-properties m))
1408 (when (or (not (setq a (assoc org-effort-property p)))
1409 (not (string-match "\\S-" (or (cdr a) ""))))
1410 ;; OK, the property is not defined. Use appointment duration?
1411 (when (and org-agenda-columns-add-appointments-to-effort-sum
1412 (setq d (get-text-property (point) 'duration)))
1413 (setq d (org-minutes-to-clocksum-string d))
1414 (put-text-property 0 (length d) 'face 'org-warning d)
1415 (push (cons org-effort-property d) p)))
1416 (push (cons (org-current-line) p) cache))
1417 (beginning-of-line 2))
1418 (when cache
1419 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
1420 (org-set-local 'org-columns-current-maxwidths maxwidths)
1421 (org-columns-display-here-title)
1422 (when (org-set-local 'org-columns-flyspell-was-active
1423 (org-bound-and-true-p flyspell-mode))
1424 (flyspell-mode 0))
1425 (mapc (lambda (x)
1426 (org-goto-line (car x))
1427 (org-columns-display-here (cdr x)))
1428 cache)
1429 (when org-agenda-columns-show-summaries
1430 (org-agenda-colview-summarize cache))))))
1432 (defun org-agenda-colview-summarize (cache)
1433 "Summarize the summarizable columns in column view in the agenda.
1434 This will add overlays to the date lines, to show the summary for each day."
1435 (let* ((fmt (mapcar (lambda (x)
1436 (if (string-match "CLOCKSUM.*" (car x))
1437 (list (match-string 0 (car x))
1438 (nth 1 x) (nth 2 x) ":" 'add_times
1439 nil '+ nil)
1441 org-columns-current-fmt-compiled))
1442 line c c1 stype calc sumfunc props lsum entries prop v title)
1443 (catch 'exit
1444 (when (delq nil (mapcar 'cadr fmt))
1445 ;; OK, at least one summation column, it makes sense to try this
1446 (goto-char (point-max))
1447 (while t
1448 (when (or (get-text-property (point) 'org-date-line)
1449 (eq (get-text-property (point) 'face)
1450 'org-agenda-structure))
1451 ;; OK, this is a date line that should be used
1452 (setq line (org-current-line))
1453 (setq entries nil c cache cache nil)
1454 (while (setq c1 (pop c))
1455 (if (> (car c1) line)
1456 (push c1 entries)
1457 (push c1 cache)))
1458 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1459 ;; Compute the summaries for the properties we want,
1460 ;; set nil properties for the rest.
1461 (when (setq entries (mapcar 'cdr entries))
1462 (setq props
1463 (mapcar
1464 (lambda (f)
1465 (setq prop (car f)
1466 title (nth 1 f)
1467 stype (nth 4 f)
1468 sumfunc (nth 6 f)
1469 calc (or (nth 7 f) 'identity))
1470 (cond
1471 ((equal prop "ITEM")
1472 (cons prop (buffer-substring (point-at-bol)
1473 (point-at-eol))))
1474 ((not stype) (cons prop ""))
1475 (t ;; do the summary
1476 (setq lsum nil)
1477 (dolist (x entries)
1478 (setq v (cdr (assoc prop x)))
1479 (if v
1480 (push
1481 (funcall
1482 (if (not (get-text-property 0 'org-computed v))
1483 calc
1484 'identity)
1485 (org-columns-string-to-number
1486 v stype))
1487 lsum)))
1488 (setq lsum (remove nil lsum))
1489 (setq lsum
1490 (cond ((> (length lsum) 1)
1491 (org-columns-number-to-string
1492 (apply sumfunc lsum) stype))
1493 ((eq (length lsum) 1)
1494 (org-columns-number-to-string
1495 (car lsum) stype))
1496 (t "")))
1497 (put-text-property 0 (length lsum) 'face 'bold lsum)
1498 (unless (eq calc 'identity)
1499 (put-text-property 0 (length lsum) 'org-computed t lsum))
1500 (cons prop lsum))))
1501 fmt))
1502 (org-columns-display-here props 'dateline)
1503 (org-set-local 'org-agenda-columns-active t)))
1504 (if (bobp) (throw 'exit t))
1505 (beginning-of-line 0))))))
1507 (defun org-agenda-colview-compute (fmt)
1508 "Compute the relevant columns in the contributing source buffers."
1509 (let ((files org-agenda-contributing-files)
1510 (org-columns-begin-marker (make-marker))
1511 (org-columns-top-level-marker (make-marker))
1512 f fm a b)
1513 (while (setq f (pop files))
1514 (setq b (find-buffer-visiting f))
1515 (with-current-buffer (or (buffer-base-buffer b) b)
1516 (save-excursion
1517 (save-restriction
1518 (widen)
1519 (org-with-silent-modifications
1520 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
1521 (goto-char (point-min))
1522 (org-columns-get-format-and-top-level)
1523 (while (setq fm (pop fmt))
1524 (cond ((equal (car fm) "CLOCKSUM")
1525 (org-clock-sum))
1526 ((equal (car fm) "CLOCKSUM_T")
1527 (org-clock-sum-today))
1528 ((and (nth 4 fm)
1529 (setq a (assoc (car fm)
1530 org-columns-current-fmt-compiled))
1531 (equal (nth 4 a) (nth 4 fm)))
1532 (org-columns-compute (car fm)))))))))))
1534 (defun org-format-time-period (interval)
1535 "Convert time in fractional days to days/hours/minutes/seconds."
1536 (if (numberp interval)
1537 (let* ((days (floor interval))
1538 (frac-hours (* 24 (- interval days)))
1539 (hours (floor frac-hours))
1540 (minutes (floor (* 60 (- frac-hours hours))))
1541 (seconds (floor (* 60 (- (* 60 (- frac-hours hours)) minutes)))))
1542 (format "%dd %02dh %02dm %02ds" days hours minutes seconds))
1543 ""))
1545 (defun org-estimate-mean-and-var (v)
1546 "Return the mean and variance of an estimate."
1547 (let* ((low (float (car v)))
1548 (high (float (cadr v)))
1549 (mean (/ (+ low high) 2.0))
1550 (var (/ (+ (expt (- mean low) 2.0) (expt (- high mean) 2.0)) 2.0)))
1551 (list mean var)))
1553 (defun org-estimate-combine (&rest el)
1554 "Combine a list of estimates, using mean and variance.
1555 The mean and variance of the result will be the sum of the means
1556 and variances (respectively) of the individual estimates."
1557 (let ((mean 0)
1558 (var 0))
1559 (mapc (lambda (e)
1560 (let ((stats (org-estimate-mean-and-var e)))
1561 (setq mean (+ mean (car stats)))
1562 (setq var (+ var (cadr stats)))))
1564 (let ((stdev (sqrt var)))
1565 (list (- mean stdev) (+ mean stdev)))))
1567 (defun org-estimate-print (e &optional fmt)
1568 "Prepare a string representation of an estimate.
1569 This formats these numbers as two numbers with a \"-\" between them."
1570 (if (null fmt) (set 'fmt "%.0f"))
1571 (format "%s" (mapconcat (lambda (n) (format fmt n)) e "-")))
1573 (defun org-string-to-estimate (s)
1574 "Convert a string to an estimate.
1575 The string should be two numbers joined with a \"-\"."
1576 (if (string-match "\\(.*\\)-\\(.*\\)" s)
1577 (list (string-to-number (match-string 1 s))
1578 (string-to-number(match-string 2 s)))
1579 (list (string-to-number s) (string-to-number s))))
1581 (provide 'org-colview)
1583 ;;; org-colview.el ends here