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