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