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