Backport changes from Emacs revs 115081 and 115082
[org-mode.git] / lisp / org-colview.el
blob523b42186b48cca7e0dbb479d2a76d99e4fd7564
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
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 (defun org-columns-remove-overlays ()
327 "Remove all currently active column overlays."
328 (interactive)
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
343 (flyspell-mode 1))
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."
350 (let (fixitem)
351 (if (not cphr)
352 item
353 (unless (string-match "^\*+ " item)
354 (setq item (concat "* " item) fixitem t))
355 (if (string-match cphr item)
356 (setq item
357 (concat
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)))))
364 (add-text-properties
365 0 (1+ (match-end 1))
366 (list 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
367 item))
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) "]")
375 t t s)))
378 (defun org-columns-show-value ()
379 "Show the full value of the property."
380 (interactive)
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."
388 (interactive)
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)
395 (message
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."
408 (interactive "P")
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."
413 (interactive "P")
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 (defvar org-agenda-overriding-columns-format nil
420 "When set, overrides any other format definition for the agenda.
421 Don't set this, this is meant for dynamic scoping.")
423 (defun org-columns-edit-value (&optional key)
424 "Edit the value of the property at point in column view.
425 Where possible, use the standard interface for changing this line."
426 (interactive)
427 (org-columns-check-computed)
428 (let* ((col (current-column))
429 (key (or key (get-char-property (point) 'org-columns-key)))
430 (value (get-char-property (point) 'org-columns-value))
431 (bol (point-at-bol)) (eol (point-at-eol))
432 (pom (or (get-text-property bol 'org-hd-marker)
433 (point))) ; keep despite of compiler waring
434 (line-overlays
435 (delq nil (mapcar (lambda (x)
436 (and (eq (overlay-buffer x) (current-buffer))
437 (>= (overlay-start x) bol)
438 (<= (overlay-start x) eol)
440 org-columns-overlays)))
441 (org-columns-time (time-to-number-of-days (current-time)))
442 nval eval allowed)
443 (cond
444 ((equal key "CLOCKSUM")
445 (error "This special column cannot be edited"))
446 ((equal key "ITEM")
447 (setq eval '(org-with-point-at pom
448 (org-edit-headline))))
449 ((equal key "TODO")
450 (setq eval '(org-with-point-at
452 (call-interactively 'org-todo))))
453 ((equal key "PRIORITY")
454 (setq eval '(org-with-point-at pom
455 (call-interactively 'org-priority))))
456 ((equal key "TAGS")
457 (setq eval '(org-with-point-at pom
458 (let ((org-fast-tag-selection-single-key
459 (if (eq org-fast-tag-selection-single-key 'expert)
460 t org-fast-tag-selection-single-key)))
461 (call-interactively 'org-set-tags)))))
462 ((equal key "DEADLINE")
463 (setq eval '(org-with-point-at pom
464 (call-interactively 'org-deadline))))
465 ((equal key "SCHEDULED")
466 (setq eval '(org-with-point-at pom
467 (call-interactively 'org-schedule))))
468 ((equal key "BEAMER_env")
469 (setq eval '(org-with-point-at pom
470 (call-interactively 'org-beamer-select-environment))))
472 (setq allowed (org-property-get-allowed-values pom key 'table))
473 (if allowed
474 (setq nval (org-icompleting-read
475 "Value: " allowed nil
476 (not (get-text-property 0 'org-unrestricted
477 (caar allowed)))))
478 (setq nval (read-string "Edit: " value)))
479 (setq nval (org-trim nval))
480 (when (not (equal nval value))
481 (setq eval '(org-entry-put pom key nval)))))
482 (when eval
484 (cond
485 ((equal major-mode 'org-agenda-mode)
486 (org-columns-eval eval)
487 ;; The following let preserves the current format, and makes sure
488 ;; that in only a single file things need to be updated.
489 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
490 (buffer (marker-buffer pom))
491 (org-agenda-contributing-files
492 (list (with-current-buffer buffer
493 (buffer-file-name (buffer-base-buffer))))))
494 (org-agenda-columns)))
496 (let ((inhibit-read-only t))
497 (org-with-silent-modifications
498 (remove-text-properties
499 (max (point-min) (1- bol)) eol '(read-only t)))
500 (unwind-protect
501 (progn
502 (setq org-columns-overlays
503 (org-delete-all line-overlays org-columns-overlays))
504 (mapc 'delete-overlay line-overlays)
505 (org-columns-eval eval))
506 (org-columns-display-here)))
507 (org-move-to-column col)
508 (if (and (derived-mode-p 'org-mode)
509 (nth 3 (assoc key org-columns-current-fmt-compiled)))
510 (org-columns-update key)))))))
512 (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
513 "Edit the current headline, the part without TODO keyword, TAGS."
514 (org-back-to-heading)
515 (when (looking-at org-todo-line-regexp)
516 (let ((pos (point))
517 (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
518 (txt (match-string 3))
519 (post "")
520 txt2)
521 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@#%]+:[ \t]*$") txt)
522 (setq post (match-string 0 txt)
523 txt (substring txt 0 (match-beginning 0))))
524 (setq txt2 (read-string "Edit: " txt))
525 (when (not (equal txt txt2))
526 (goto-char pos)
527 (insert pre txt2 post)
528 (delete-region (point) (point-at-eol))
529 (org-set-tags nil t)))))
531 (defun org-columns-edit-allowed ()
532 "Edit the list of allowed values for the current property."
533 (interactive)
534 (let* ((pom (or (org-get-at-bol 'org-marker)
535 (org-get-at-bol 'org-hd-marker)
536 (point)))
537 (key (get-char-property (point) 'org-columns-key))
538 (key1 (concat key "_ALL"))
539 (allowed (org-entry-get pom key1 t))
540 nval)
541 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
542 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
543 (setq nval (read-string "Allowed: " allowed))
544 (org-entry-put
545 (cond ((marker-position org-entry-property-inherited-from)
546 org-entry-property-inherited-from)
547 ((marker-position org-columns-top-level-marker)
548 org-columns-top-level-marker)
549 (t pom))
550 key1 nval)))
552 (defun org-columns-eval (form)
553 (let (hidep)
554 (save-excursion
555 (beginning-of-line 1)
556 ;; `next-line' is needed here, because it skips invisible line.
557 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
558 (setq hidep (org-at-heading-p 1)))
559 (eval form)
560 (and hidep (hide-entry))))
562 (defun org-columns-previous-allowed-value ()
563 "Switch to the previous allowed value for this column."
564 (interactive)
565 (org-columns-next-allowed-value t))
567 (defun org-columns-next-allowed-value (&optional previous nth)
568 "Switch to the next allowed value for this column.
569 When PREVIOUS is set, go to the previous value. When NTH is
570 an integer, select that value."
571 (interactive)
572 (org-columns-check-computed)
573 (let* ((col (current-column))
574 (key (get-char-property (point) 'org-columns-key))
575 (value (get-char-property (point) 'org-columns-value))
576 (bol (point-at-bol)) (eol (point-at-eol))
577 (pom (or (get-text-property bol 'org-hd-marker)
578 (point))) ; keep despite of compiler waring
579 (line-overlays
580 (delq nil (mapcar (lambda (x)
581 (and (eq (overlay-buffer x) (current-buffer))
582 (>= (overlay-start x) bol)
583 (<= (overlay-start x) eol)
585 org-columns-overlays)))
586 (allowed (or (org-property-get-allowed-values pom key)
587 (and (memq
588 (nth 4 (assoc key org-columns-current-fmt-compiled))
589 '(checkbox checkbox-n-of-m checkbox-percent))
590 '("[ ]" "[X]"))
591 (org-colview-construct-allowed-dates value)))
592 nval)
593 (when (integerp nth)
594 (setq nth (1- nth))
595 (if (= nth -1) (setq nth 9)))
596 (when (equal key "ITEM")
597 (error "Cannot edit item headline from here"))
598 (unless (or allowed (member key '("SCHEDULED" "DEADLINE" "CLOCKSUM")))
599 (error "Allowed values for this property have not been defined"))
600 (if (member key '("SCHEDULED" "DEADLINE" "CLOCKSUM"))
601 (setq nval (if previous 'earlier 'later))
602 (if previous (setq allowed (reverse allowed)))
603 (cond
604 (nth
605 (setq nval (nth nth allowed))
606 (if (not nval)
607 (error "There are only %d allowed values for property `%s'"
608 (length allowed) key)))
609 ((member value allowed)
610 (setq nval (or (car (cdr (member value allowed)))
611 (car allowed)))
612 (if (equal nval value)
613 (error "Only one allowed value for this property")))
614 (t (setq nval (car allowed)))))
615 (cond
616 ((equal major-mode 'org-agenda-mode)
617 (org-columns-eval '(org-entry-put pom key nval))
618 ;; The following let preserves the current format, and makes sure
619 ;; that in only a single file things need to be updated.
620 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
621 (buffer (marker-buffer pom))
622 (org-agenda-contributing-files
623 (list (with-current-buffer buffer
624 (buffer-file-name (buffer-base-buffer))))))
625 (org-agenda-columns)))
627 (let ((inhibit-read-only t))
628 (remove-text-properties (1- bol) eol '(read-only t))
629 (unwind-protect
630 (progn
631 (setq org-columns-overlays
632 (org-delete-all line-overlays org-columns-overlays))
633 (mapc 'delete-overlay line-overlays)
634 (org-columns-eval '(org-entry-put pom key nval)))
635 (org-columns-display-here)))
636 (org-move-to-column col)
637 (and (nth 3 (assoc key org-columns-current-fmt-compiled))
638 (org-columns-update key))))))
640 (defun org-colview-construct-allowed-dates (s)
641 "Construct a list of three dates around the date in S.
642 This respects the format of the time stamp in S, active or non-active,
643 and also including time or not. S must be just a time stamp, no text
644 around it."
645 (when (and s (string-match (concat "^" org-ts-regexp3 "$") s))
646 (let* ((time (org-parse-time-string s 'nodefaults))
647 (active (equal (string-to-char s) ?<))
648 (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
649 time-before time-after)
650 (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
651 (setf (car time) (or (car time) 0))
652 (setf (nth 1 time) (or (nth 1 time) 0))
653 (setf (nth 2 time) (or (nth 2 time) 0))
654 (setq time-before (copy-sequence time))
655 (setq time-after (copy-sequence time))
656 (setf (nth 3 time-before) (1- (nth 3 time)))
657 (setf (nth 3 time-after) (1+ (nth 3 time)))
658 (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
659 (list time-before time time-after)))))
661 (defun org-verify-version (task)
662 (cond
663 ((eq task 'columns)
664 (if (or (featurep 'xemacs)
665 (< emacs-major-version 22))
666 (error "Emacs 22 is required for the columns feature")))))
668 (defun org-columns-open-link (&optional arg)
669 (interactive "P")
670 (let ((value (get-char-property (point) 'org-columns-value)))
671 (org-open-link-from-string value arg)))
673 (defun org-columns-get-format-and-top-level ()
674 (let ((fmt (org-columns-get-format)))
675 (org-columns-goto-top-level)
676 fmt))
678 (defun org-columns-get-format (&optional fmt-string)
679 (interactive)
680 (let (fmt-as-property fmt)
681 (when (condition-case nil (org-back-to-heading) (error nil))
682 (setq fmt-as-property (org-entry-get nil "COLUMNS" t)))
683 (setq fmt (or fmt-string fmt-as-property org-columns-default-format))
684 (org-set-local 'org-columns-current-fmt fmt)
685 (org-columns-compile-format fmt)
686 fmt))
688 (defun org-columns-goto-top-level ()
689 (when (condition-case nil (org-back-to-heading) (error nil))
690 (org-entry-get nil "COLUMNS" t))
691 (if (marker-position org-entry-property-inherited-from)
692 (move-marker org-columns-top-level-marker org-entry-property-inherited-from)
693 (move-marker org-columns-top-level-marker (point))))
695 ;;;###autoload
696 (defun org-columns (&optional columns-fmt-string)
697 "Turn on column view on an org-mode file.
698 When COLUMNS-FMT-STRING is non-nil, use it as the column format."
699 (interactive)
700 (org-verify-version 'columns)
701 (org-columns-remove-overlays)
702 (move-marker org-columns-begin-marker (point))
703 (let ((org-columns-time (time-to-number-of-days (current-time)))
704 beg end fmt cache maxwidths)
705 (org-columns-goto-top-level)
706 (setq fmt (org-columns-get-format columns-fmt-string))
707 (save-excursion
708 (goto-char org-columns-top-level-marker)
709 (setq beg (point))
710 (unless org-columns-inhibit-recalculation
711 (org-columns-compute-all))
712 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
713 (point-max)))
714 ;; Get and cache the properties
715 (goto-char beg)
716 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
717 (save-excursion
718 (save-restriction
719 (narrow-to-region beg end)
720 (org-clock-sum))))
721 (when (assoc "CLOCKSUM_T" org-columns-current-fmt-compiled)
722 (save-excursion
723 (save-restriction
724 (narrow-to-region beg end)
725 (org-clock-sum-today))))
726 (while (re-search-forward org-outline-regexp-bol end t)
727 (if (and org-columns-skip-archived-trees
728 (looking-at (concat ".*:" org-archive-tag ":")))
729 (org-end-of-subtree t)
730 (push (cons (org-current-line) (org-entry-properties)) cache)))
731 (when cache
732 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
733 (org-set-local 'org-columns-current-maxwidths maxwidths)
734 (org-columns-display-here-title)
735 (when (org-set-local 'org-columns-flyspell-was-active
736 (org-bound-and-true-p flyspell-mode))
737 (flyspell-mode 0))
738 (unless (local-variable-p 'org-colview-initial-truncate-line-value)
739 (org-set-local 'org-colview-initial-truncate-line-value
740 truncate-lines))
741 (setq truncate-lines t)
742 (mapc (lambda (x)
743 (org-goto-line (car x))
744 (org-columns-display-here (cdr x)))
745 cache)))))
747 (eval-when-compile (defvar org-columns-time))
749 (defvar org-columns-compile-map
750 '(("none" none +)
751 (":" add_times +)
752 ("+" add_numbers +)
753 ("$" currency +)
754 ("X" checkbox +)
755 ("X/" checkbox-n-of-m +)
756 ("X%" checkbox-percent +)
757 ("max" max_numbers max)
758 ("min" min_numbers min)
759 ("mean" mean_numbers
760 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
761 (":max" max_times max)
762 (":min" min_times min)
763 (":mean" mean_times
764 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
765 ("@min" min_age min (lambda (x) (- org-columns-time x)))
766 ("@max" max_age max (lambda (x) (- org-columns-time x)))
767 ("@mean" mean_age
768 (lambda (&rest x) (/ (apply '+ x) (float (length x))))
769 (lambda (x) (- org-columns-time x)))
770 ("est+" estimate org-estimate-combine))
771 "Operator <-> format,function,calc map.
772 Used to compile/uncompile columns format and completing read in
773 interactive function `org-columns-new'.
775 operator string used in #+COLUMNS definition describing the
776 summary type
777 format symbol describing summary type selected interactively in
778 `org-columns-new' and internally in
779 `org-columns-number-to-string' and
780 `org-columns-string-to-number'
781 function called with a list of values as argument to calculate
782 the summary value
783 calc function called on every element before summarizing. This is
784 optional and should only be specified if needed")
786 (defun org-columns-new (&optional prop title width op fmt fun &rest rest)
787 "Insert a new column, to the left of the current column."
788 (interactive)
789 (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
790 cell)
791 (setq prop (org-icompleting-read
792 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
793 nil nil prop))
794 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
795 (setq width (read-string "Column width: " (if width (number-to-string width))))
796 (if (string-match "\\S-" width)
797 (setq width (string-to-number width))
798 (setq width nil))
799 (setq fmt (org-icompleting-read
800 "Summary [none]: "
801 (mapcar (lambda (x) (list (symbol-name (cadr x))))
802 org-columns-compile-map)
803 nil t))
804 (setq fmt (intern fmt)
805 fun (cdr (assoc fmt (mapcar 'cdr org-columns-compile-map))))
806 (if (eq fmt 'none) (setq fmt nil))
807 (if editp
808 (progn
809 (setcar editp prop)
810 (setcdr editp (list title width nil fmt nil fun)))
811 (setq cell (nthcdr (1- (current-column))
812 org-columns-current-fmt-compiled))
813 (setcdr cell (cons (list prop title width nil fmt nil
814 (car fun) (cadr fun))
815 (cdr cell))))
816 (org-columns-store-format)
817 (org-columns-redo)))
819 (defun org-columns-delete ()
820 "Delete the column at point from columns view."
821 (interactive)
822 (let* ((n (current-column))
823 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
824 (when (y-or-n-p
825 (format "Are you sure you want to remove column \"%s\"? " title))
826 (setq org-columns-current-fmt-compiled
827 (delq (nth n org-columns-current-fmt-compiled)
828 org-columns-current-fmt-compiled))
829 (org-columns-store-format)
830 (org-columns-redo)
831 (if (>= (current-column) (length org-columns-current-fmt-compiled))
832 (backward-char 1)))))
834 (defun org-columns-edit-attributes ()
835 "Edit the attributes of the current column."
836 (interactive)
837 (let* ((n (current-column))
838 (info (nth n org-columns-current-fmt-compiled)))
839 (apply 'org-columns-new info)))
841 (defun org-columns-widen (arg)
842 "Make the column wider by ARG characters."
843 (interactive "p")
844 (let* ((n (current-column))
845 (entry (nth n org-columns-current-fmt-compiled))
846 (width (or (nth 2 entry)
847 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
848 (setq width (max 1 (+ width arg)))
849 (setcar (nthcdr 2 entry) width)
850 (org-columns-store-format)
851 (org-columns-redo)))
853 (defun org-columns-narrow (arg)
854 "Make the column narrower by ARG characters."
855 (interactive "p")
856 (org-columns-widen (- arg)))
858 (defun org-columns-move-right ()
859 "Swap this column with the one to the right."
860 (interactive)
861 (let* ((n (current-column))
862 (cell (nthcdr n org-columns-current-fmt-compiled))
864 (when (>= n (1- (length org-columns-current-fmt-compiled)))
865 (error "Cannot shift this column further to the right"))
866 (setq e (car cell))
867 (setcar cell (car (cdr cell)))
868 (setcdr cell (cons e (cdr (cdr cell))))
869 (org-columns-store-format)
870 (org-columns-redo)
871 (forward-char 1)))
873 (defun org-columns-move-left ()
874 "Swap this column with the one to the left."
875 (interactive)
876 (let* ((n (current-column)))
877 (when (= n 0)
878 (error "Cannot shift this column further to the left"))
879 (backward-char 1)
880 (org-columns-move-right)
881 (backward-char 1)))
883 (defun org-columns-store-format ()
884 "Store the text version of the current columns format in appropriate place.
885 This is either in the COLUMNS property of the node starting the current column
886 display, or in the #+COLUMNS line of the current buffer."
887 (let (fmt (cnt 0))
888 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
889 (org-set-local 'org-columns-current-fmt fmt)
890 (if (marker-position org-columns-top-level-marker)
891 (save-excursion
892 (goto-char org-columns-top-level-marker)
893 (if (and (org-at-heading-p)
894 (org-entry-get nil "COLUMNS"))
895 (org-entry-put nil "COLUMNS" fmt)
896 (goto-char (point-min))
897 ;; Overwrite all #+COLUMNS lines....
898 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
899 (setq cnt (1+ cnt))
900 (replace-match (concat "#+COLUMNS: " fmt) t t))
901 (unless (> cnt 0)
902 (goto-char (point-min))
903 (or (org-at-heading-p t) (outline-next-heading))
904 (let ((inhibit-read-only t))
905 (insert-before-markers "#+COLUMNS: " fmt "\n")))
906 (org-set-local 'org-columns-default-format fmt))))))
908 (defun org-columns-get-autowidth-alist (s cache)
909 "Derive the maximum column widths from the format and the cache."
910 (let ((start 0) rtn)
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)))
914 (mapc (lambda (x)
915 (setcdr x (apply 'max
916 (mapcar
917 (lambda (y)
918 (length (or (cdr (assoc (car x) (cdr y))) " ")))
919 cache))))
920 rtn)
921 rtn))
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)))
929 col)
930 (while (setq col (pop columns))
931 (when (nth 3 col)
932 (save-excursion
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)
938 (let (fmt val pos)
939 (save-excursion
940 (mapc (lambda (ov)
941 (when (equal (overlay-get ov 'org-columns-key) property)
942 (setq pos (overlay-start ov))
943 (goto-char pos)
944 (when (setq val (cdr (assoc property
945 (get-text-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."
956 (interactive)
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))
961 (level 0)
962 (ass (assoc property org-columns-current-fmt-compiled))
963 (format (nth 4 ass))
964 (printf (nth 5 ass))
965 (fun (nth 6 ass))
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)
971 (save-excursion
972 ;; Find the region to compute
973 (goto-char beg)
974 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
975 (goto-char end)
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)))
980 level last-level)
981 level (org-outline-level)
982 val (org-entry-get nil property)
983 valflag (and val (string-match "\\S-" val)))
984 (cond
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)
1008 (push (if flag
1010 (funcall calc (org-columns-string-to-number
1011 (if flag str val) format)))
1012 (aref lvals level))
1013 (aset lflag level t))
1014 ;; clear accumulators for deeper levels
1015 (loop for l from (1+ level) to (1- lmax) do
1016 (aset lvals l nil)
1017 (aset lflag l nil)))
1018 ((>= level last-level)
1019 ;; add what we have here to the accumulator for this level
1020 (when valflag
1021 (push (funcall calc (org-columns-string-to-number val format))
1022 (aref lvals level))
1023 (aset lflag level t)))
1024 (t (error "This should not happen")))))))
1026 (defun org-columns-redo ()
1027 "Construct the column display again."
1028 (interactive)
1029 (message "Recomputing columns...")
1030 (let ((line (org-current-line))
1031 (col (current-column)))
1032 (save-excursion
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)
1038 (org-agenda-redo)
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))
1053 (while l
1054 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1055 sum)))
1057 (defun org-columns-number-to-string (n fmt &optional printf)
1058 "Convert a computed column number to a string value, according to FMT."
1059 (cond
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))
1064 ((eq fmt 'checkbox)
1065 (cond ((= n (floor n)) "[X]")
1066 ((> n 1.) "[-]")
1067 (t "[ ]")))
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))
1072 ((eq fmt 'currency)
1073 (format "%.2f" 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)
1079 (if (not 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."
1086 (if s
1087 (cond
1088 ((memq fmt '(min_age max_age mean_age))
1089 (cond ((string= s "") org-columns-time)
1090 ((string-match
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))
1101 (while l
1102 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1103 sum))
1104 ((string-match (concat "\\([0-9.]+\\) *\\("
1105 (regexp-opt (mapcar 'car org-effort-durations))
1106 "\\)") s)
1107 (setq s (concat "0:" (org-duration-string-to-minutes s t)))
1108 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1109 (while l
1110 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1111 sum))
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))
1121 (setq prop (car e)
1122 title (nth 1 e)
1123 width (nth 2 e)
1124 op (nth 3 e)
1125 fmt (nth 4 e)
1126 printf (nth 5 e)
1127 fun (nth 6 e)
1128 calc (nth 7 e))
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))
1134 prop
1135 (if title (concat "(" title ")"))
1136 (if op (concat "{" op "}"))))
1137 (setq rtn (concat rtn " " s)))
1138 (org-trim rtn)))
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
1143 that list are:
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-*")
1156 fmt start)
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)
1162 f nil
1163 printf nil
1164 fun '+
1165 calc nil)
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
1191 of fields."
1192 (save-excursion
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)
1200 (catch 'next
1201 (when (and (or (null maxlevel)
1202 (>= 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)
1212 (throw 'next t))
1213 (setq row nil)
1214 (loop for i from 0 to (1- n) do
1215 (push
1216 (org-quote-vert
1217 (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
1218 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
1219 ""))
1220 row))
1221 (setq row (nreverse row))
1222 (unless (and skip-empty-rows
1223 (eq 1 (length (delete "" (delete-dups (copy-sequence row))))))
1224 (push row tbl)))))
1225 (append (list title 'hline) (nreverse tbl)))))
1227 ;;;###autoload
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.
1244 :skip-empty-rows
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))
1260 ((stringp id) id)
1261 (t "")))
1262 (cond ((not id) nil)
1263 ((eq id 'global) (setq view-pos (point-min)))
1264 ((eq id 'local))
1265 ((string-match "^file:\\(.*\\)" id-as-string)
1266 (setq view-file (match-string 1 id-as-string)
1267 view-pos 1)
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)
1278 (current-buffer))
1279 (save-excursion
1280 (save-restriction
1281 (widen)
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))))
1287 (goto-char pos)
1288 (move-marker pos nil)
1289 (when tbl
1290 (when (plist-get params :hlines)
1291 (setq tmp nil)
1292 (while tbl
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))
1297 (or (eq hlines t)
1298 (and (numberp hlines)
1299 (<= (- (match-end 1) (match-beginning 1))
1300 hlines))))
1301 (push 'hline tmp)))
1302 (push (pop tbl) tmp)))
1303 (setq tbl (nreverse tmp)))
1304 (when vlines
1305 (setq tbl (mapcar (lambda (x)
1306 (if (eq 'hline x) x (cons "" x)))
1307 tbl))
1308 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1309 (when content-lines
1310 (while (string-match "^#" (car content-lines))
1311 (insert (pop content-lines) "\n")))
1312 (setq pos (point))
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)
1319 (insert "\n" line)
1320 (when (string-match "^[ \t]*#\\+tblfm" line)
1321 (setq recalc t))))
1322 (if recalc
1323 (progn (goto-char pos) (org-table-recalculate 'all))
1324 (goto-char pos)
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."
1331 (mapconcat
1332 (lambda (x)
1333 (cond
1334 ((listp x)
1335 (concat "|" (mapconcat 'identity x "|") "|"))
1336 ((eq x 'hline) "|-|")
1337 (t (error "Garbage in listtable: %s" x))))
1338 tbl "\n"))
1340 ;;;###autoload
1341 (defun org-insert-columns-dblock ()
1342 "Create a dynamic block capturing a column view table."
1343 (interactive)
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
1364 ;;;###autoload
1365 (defun org-agenda-columns ()
1366 "Turn on or update column view in the agenda."
1367 (interactive)
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)
1373 (cond
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))
1395 (save-excursion
1396 ;; Get and cache the properties
1397 (goto-char (point-min))
1398 (while (not (eobp))
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))
1413 (when cache
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))
1419 (flyspell-mode 0))
1420 (mapc (lambda (x)
1421 (org-goto-line (car x))
1422 (org-columns-display-here (cdr x)))
1423 cache)
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
1434 nil '+ nil)
1436 org-columns-current-fmt-compiled))
1437 line c c1 stype calc sumfunc props lsum entries prop v title)
1438 (catch 'exit
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))
1442 (while t
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)
1451 (push c1 entries)
1452 (push c1 cache)))
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))
1457 (setq props
1458 (mapcar
1459 (lambda (f)
1460 (setq prop (car f)
1461 title (nth 1 f)
1462 stype (nth 4 f)
1463 sumfunc (nth 6 f)
1464 calc (or (nth 7 f) 'identity))
1465 (cond
1466 ((equal prop "ITEM")
1467 (cons prop (buffer-substring (point-at-bol)
1468 (point-at-eol))))
1469 ((not stype) (cons prop ""))
1470 (t ;; do the summary
1471 (setq lsum nil)
1472 (dolist (x entries)
1473 (setq v (cdr (assoc prop x)))
1474 (if v
1475 (push
1476 (funcall
1477 (if (not (get-text-property 0 'org-computed v))
1478 calc
1479 'identity)
1480 (org-columns-string-to-number
1481 v stype))
1482 lsum)))
1483 (setq lsum (remove nil lsum))
1484 (setq 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
1490 (car lsum) stype))
1491 (t "")))
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))
1495 (cons prop lsum))))
1496 fmt))
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))
1507 f fm a b)
1508 (while (setq f (pop files))
1509 (setq b (find-buffer-visiting f))
1510 (with-current-buffer (or (buffer-base-buffer b) b)
1511 (save-excursion
1512 (save-restriction
1513 (widen)
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")
1520 (org-clock-sum))
1521 ((equal (car fm) "CLOCKSUM_T")
1522 (org-clock-sum-today))
1523 ((and (nth 4 fm)
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))
1538 ""))
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)))
1546 (list mean var)))
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."
1552 (let ((mean 0)
1553 (var 0))
1554 (mapc (lambda (e)
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