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