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