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