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