Merge branch 'master' of orgmode.org:org-mode
[org-mode.git] / lisp / org-colview.el
blob8da2d1ea87a566a07cde529c94f5d3ac40b10d3f
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 (org-columns-get-format)))
670 (org-columns-goto-top-level)
671 fmt))
673 (defun org-columns-get-format (&optional fmt-string)
674 (interactive)
675 (let (fmt-as-property fmt)
676 (when (condition-case nil (org-back-to-heading) (error nil))
677 (setq fmt-as-property (org-entry-get nil "COLUMNS" t)))
678 (setq fmt (or fmt-string fmt-as-property org-columns-default-format))
679 (org-set-local 'org-columns-current-fmt fmt)
680 (org-columns-compile-format fmt)
681 fmt))
683 (defun org-columns-goto-top-level ()
684 (when (condition-case nil (org-back-to-heading) (error nil))
685 (org-entry-get nil "COLUMNS" t)
686 (if (marker-position org-entry-property-inherited-from)
687 (move-marker org-columns-top-level-marker org-entry-property-inherited-from)
688 (move-marker org-columns-top-level-marker (point)))))
690 (defun org-columns (&optional columns-fmt-string)
691 "Turn on column view on an org-mode file.
692 When COLUMNS-FMT-STRING is non-nil, use it as the column format."
693 (interactive)
694 (org-verify-version 'columns)
695 (org-columns-remove-overlays)
696 (move-marker org-columns-begin-marker (point))
697 (let ((org-columns-time (time-to-number-of-days (current-time)))
698 beg end fmt cache maxwidths)
699 (org-columns-goto-top-level)
700 (setq fmt (org-columns-get-format columns-fmt-string))
701 (save-excursion
702 (goto-char org-columns-top-level-marker)
703 (setq beg (point))
704 (unless org-columns-inhibit-recalculation
705 (org-columns-compute-all))
706 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
707 (point-max)))
708 ;; Get and cache the properties
709 (goto-char beg)
710 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
711 (save-excursion
712 (save-restriction
713 (narrow-to-region beg end)
714 (org-clock-sum))))
715 (when (assoc "CLOCKSUM_T" org-columns-current-fmt-compiled)
716 (save-excursion
717 (save-restriction
718 (narrow-to-region beg end)
719 (org-clock-sum-today))))
720 (while (re-search-forward org-outline-regexp-bol end t)
721 (if (and org-columns-skip-archived-trees
722 (looking-at (concat ".*:" org-archive-tag ":")))
723 (org-end-of-subtree t)
724 (push (cons (org-current-line) (org-entry-properties)) cache)))
725 (when cache
726 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
727 (org-set-local 'org-columns-current-maxwidths maxwidths)
728 (org-columns-display-here-title)
729 (when (org-set-local 'org-columns-flyspell-was-active
730 (org-bound-and-true-p flyspell-mode))
731 (flyspell-mode 0))
732 (unless (local-variable-p 'org-colview-initial-truncate-line-value)
733 (org-set-local 'org-colview-initial-truncate-line-value
734 truncate-lines))
735 (setq truncate-lines t)
736 (mapc (lambda (x)
737 (org-goto-line (car x))
738 (org-columns-display-here (cdr x)))
739 cache)))))
741 (eval-when-compile (defvar org-columns-time))
743 (defvar org-columns-compile-map
744 '(("none" none +)
745 (":" add_times +)
746 ("+" add_numbers +)
747 ("$" currency +)
748 ("X" checkbox +)
749 ("X/" checkbox-n-of-m +)
750 ("X%" checkbox-percent +)
751 ("max" max_numbers max)
752 ("min" min_numbers min)
753 ("mean" mean_numbers
754 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
755 (":max" max_times max)
756 (":min" min_times min)
757 (":mean" mean_times
758 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
759 ("@min" min_age min (lambda (x) (- org-columns-time x)))
760 ("@max" max_age max (lambda (x) (- org-columns-time x)))
761 ("@mean" mean_age
762 (lambda (&rest x) (/ (apply '+ x) (float (length x))))
763 (lambda (x) (- org-columns-time x)))
764 ("est+" estimate org-estimate-combine))
765 "Operator <-> format,function,calc map.
766 Used to compile/uncompile columns format and completing read in
767 interactive function `org-columns-new'.
769 operator string used in #+COLUMNS definition describing the
770 summary type
771 format symbol describing summary type selected interactively in
772 `org-columns-new' and internally in
773 `org-columns-number-to-string' and
774 `org-columns-string-to-number'
775 function called with a list of values as argument to calculate
776 the summary value
777 calc function called on every element before summarizing. This is
778 optional and should only be specified if needed")
780 (defun org-columns-new (&optional prop title width op fmt fun &rest rest)
781 "Insert a new column, to the left of the current column."
782 (interactive)
783 (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
784 cell)
785 (setq prop (org-icompleting-read
786 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
787 nil nil prop))
788 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
789 (setq width (read-string "Column width: " (if width (number-to-string width))))
790 (if (string-match "\\S-" width)
791 (setq width (string-to-number width))
792 (setq width nil))
793 (setq fmt (org-icompleting-read
794 "Summary [none]: "
795 (mapcar (lambda (x) (list (symbol-name (cadr x))))
796 org-columns-compile-map)
797 nil t))
798 (setq fmt (intern fmt)
799 fun (cdr (assoc fmt (mapcar 'cdr org-columns-compile-map))))
800 (if (eq fmt 'none) (setq fmt nil))
801 (if editp
802 (progn
803 (setcar editp prop)
804 (setcdr editp (list title width nil fmt nil fun)))
805 (setq cell (nthcdr (1- (current-column))
806 org-columns-current-fmt-compiled))
807 (setcdr cell (cons (list prop title width nil fmt nil
808 (car fun) (cadr fun))
809 (cdr cell))))
810 (org-columns-store-format)
811 (org-columns-redo)))
813 (defun org-columns-delete ()
814 "Delete the column at point from columns view."
815 (interactive)
816 (let* ((n (current-column))
817 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
818 (when (y-or-n-p
819 (format "Are you sure you want to remove column \"%s\"? " title))
820 (setq org-columns-current-fmt-compiled
821 (delq (nth n org-columns-current-fmt-compiled)
822 org-columns-current-fmt-compiled))
823 (org-columns-store-format)
824 (org-columns-redo)
825 (if (>= (current-column) (length org-columns-current-fmt-compiled))
826 (backward-char 1)))))
828 (defun org-columns-edit-attributes ()
829 "Edit the attributes of the current column."
830 (interactive)
831 (let* ((n (current-column))
832 (info (nth n org-columns-current-fmt-compiled)))
833 (apply 'org-columns-new info)))
835 (defun org-columns-widen (arg)
836 "Make the column wider by ARG characters."
837 (interactive "p")
838 (let* ((n (current-column))
839 (entry (nth n org-columns-current-fmt-compiled))
840 (width (or (nth 2 entry)
841 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
842 (setq width (max 1 (+ width arg)))
843 (setcar (nthcdr 2 entry) width)
844 (org-columns-store-format)
845 (org-columns-redo)))
847 (defun org-columns-narrow (arg)
848 "Make the column narrower by ARG characters."
849 (interactive "p")
850 (org-columns-widen (- arg)))
852 (defun org-columns-move-right ()
853 "Swap this column with the one to the right."
854 (interactive)
855 (let* ((n (current-column))
856 (cell (nthcdr n org-columns-current-fmt-compiled))
858 (when (>= n (1- (length org-columns-current-fmt-compiled)))
859 (error "Cannot shift this column further to the right"))
860 (setq e (car cell))
861 (setcar cell (car (cdr cell)))
862 (setcdr cell (cons e (cdr (cdr cell))))
863 (org-columns-store-format)
864 (org-columns-redo)
865 (forward-char 1)))
867 (defun org-columns-move-left ()
868 "Swap this column with the one to the left."
869 (interactive)
870 (let* ((n (current-column)))
871 (when (= n 0)
872 (error "Cannot shift this column further to the left"))
873 (backward-char 1)
874 (org-columns-move-right)
875 (backward-char 1)))
877 (defun org-columns-store-format ()
878 "Store the text version of the current columns format in appropriate place.
879 This is either in the COLUMNS property of the node starting the current column
880 display, or in the #+COLUMNS line of the current buffer."
881 (let (fmt (cnt 0))
882 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
883 (org-set-local 'org-columns-current-fmt fmt)
884 (if (marker-position org-columns-top-level-marker)
885 (save-excursion
886 (goto-char org-columns-top-level-marker)
887 (if (and (org-at-heading-p)
888 (org-entry-get nil "COLUMNS"))
889 (org-entry-put nil "COLUMNS" fmt)
890 (goto-char (point-min))
891 ;; Overwrite all #+COLUMNS lines....
892 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
893 (setq cnt (1+ cnt))
894 (replace-match (concat "#+COLUMNS: " fmt) t t))
895 (unless (> cnt 0)
896 (goto-char (point-min))
897 (or (org-at-heading-p t) (outline-next-heading))
898 (let ((inhibit-read-only t))
899 (insert-before-markers "#+COLUMNS: " fmt "\n")))
900 (org-set-local 'org-columns-default-format fmt))))))
902 (defvar org-agenda-overriding-columns-format nil
903 "When set, overrides any other format definition for the agenda.
904 Don't set this, this is meant for dynamic scoping.")
906 (defun org-columns-get-autowidth-alist (s cache)
907 "Derive the maximum column widths from the format and the cache."
908 (let ((start 0) rtn)
909 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
910 (push (cons (match-string 1 s) 1) rtn)
911 (setq start (match-end 0)))
912 (mapc (lambda (x)
913 (setcdr x (apply 'max
914 (mapcar
915 (lambda (y)
916 (length (or (cdr (assoc (car x) (cdr y))) " ")))
917 cache))))
918 rtn)
919 rtn))
921 (defun org-columns-compute-all ()
922 "Compute all columns that have operators defined."
923 (org-unmodified
924 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
925 (let ((columns org-columns-current-fmt-compiled)
926 (org-columns-time (time-to-number-of-days (current-time)))
927 col)
928 (while (setq col (pop columns))
929 (when (nth 3 col)
930 (save-excursion
931 (org-columns-compute (car col)))))))
933 (defun org-columns-update (property)
934 "Recompute PROPERTY, and update the columns display for it."
935 (org-columns-compute property)
936 (let (fmt val pos)
937 (save-excursion
938 (mapc (lambda (ov)
939 (when (equal (overlay-get ov 'org-columns-key) property)
940 (setq pos (overlay-start ov))
941 (goto-char pos)
942 (when (setq val (cdr (assoc property
943 (get-text-property
944 (point-at-bol) 'org-summaries))))
945 (setq fmt (overlay-get ov 'org-columns-format))
946 (overlay-put ov 'org-columns-value val)
947 (overlay-put ov 'display (format fmt val)))))
948 org-columns-overlays))))
950 (defvar org-inlinetask-min-level
951 (if (featurep 'org-inlinetask) org-inlinetask-min-level 15))
952 (defun org-columns-compute (property)
953 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
954 (interactive)
955 (let* ((re org-outline-regexp-bol)
956 (lmax 30) ; Does anyone use deeper levels???
957 (lvals (make-vector lmax nil))
958 (lflag (make-vector lmax nil))
959 (level 0)
960 (ass (assoc property org-columns-current-fmt-compiled))
961 (format (nth 4 ass))
962 (printf (nth 5 ass))
963 (fun (nth 6 ass))
964 (calc (or (nth 7 ass) 'identity))
965 (beg org-columns-top-level-marker)
966 (inminlevel org-inlinetask-min-level)
967 (last-level org-inlinetask-min-level)
968 val valflag flag end sumpos sum-alist sum str str1 useval)
969 (save-excursion
970 ;; Find the region to compute
971 (goto-char beg)
972 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
973 (goto-char end)
974 ;; Walk the tree from the back and do the computations
975 (while (re-search-backward re beg t)
976 (setq sumpos (match-beginning 0)
977 last-level (if (not (or (zerop level) (eq level inminlevel)))
978 level last-level)
979 level (org-outline-level)
980 val (org-entry-get nil property)
981 valflag (and val (string-match "\\S-" val)))
982 (cond
983 ((< level last-level)
984 ;; put the sum of lower levels here as a property
985 (setq sum (+ (if (and (/= last-level inminlevel)
986 (aref lvals last-level))
987 (apply fun (aref lvals last-level)) 0)
988 (if (aref lvals inminlevel)
989 (apply fun (aref lvals inminlevel)) 0))
990 flag (or (aref lflag last-level) ; any valid entries from children?
991 (aref lflag inminlevel)) ; or inline tasks?
992 str (org-columns-number-to-string sum format printf)
993 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
994 useval (if flag str1 (if valflag val ""))
995 sum-alist (get-text-property sumpos 'org-summaries))
996 (if (assoc property sum-alist)
997 (setcdr (assoc property sum-alist) useval)
998 (push (cons property useval) sum-alist)
999 (org-unmodified
1000 (add-text-properties sumpos (1+ sumpos)
1001 (list 'org-summaries sum-alist))))
1002 (when (and val (not (equal val (if flag str val))))
1003 (org-entry-put nil property (if flag str val)))
1004 ;; add current to current level accumulator
1005 (when (or flag valflag)
1006 (push (if flag
1008 (funcall calc (org-columns-string-to-number
1009 (if flag str val) format)))
1010 (aref lvals level))
1011 (aset lflag level t))
1012 ;; clear accumulators for deeper levels
1013 (loop for l from (1+ level) to (1- lmax) do
1014 (aset lvals l nil)
1015 (aset lflag l nil)))
1016 ((>= level last-level)
1017 ;; add what we have here to the accumulator for this level
1018 (when valflag
1019 (push (funcall calc (org-columns-string-to-number val format))
1020 (aref lvals level))
1021 (aset lflag level t)))
1022 (t (error "This should not happen")))))))
1024 (defun org-columns-redo ()
1025 "Construct the column display again."
1026 (interactive)
1027 (message "Recomputing columns...")
1028 (let ((line (org-current-line))
1029 (col (current-column)))
1030 (save-excursion
1031 (if (marker-position org-columns-begin-marker)
1032 (goto-char org-columns-begin-marker))
1033 (org-columns-remove-overlays)
1034 (if (derived-mode-p 'org-mode)
1035 (call-interactively 'org-columns)
1036 (org-agenda-redo)
1037 (call-interactively 'org-agenda-columns)))
1038 (org-goto-line line)
1039 (move-to-column col))
1040 (message "Recomputing columns...done"))
1042 (defun org-columns-not-in-agenda ()
1043 (if (eq major-mode 'org-agenda-mode)
1044 (error "This command is only allowed in Org-mode buffers")))
1046 (defun org-string-to-number (s)
1047 "Convert string to number, and interpret hh:mm:ss."
1048 (if (not (string-match ":" s))
1049 (string-to-number s)
1050 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1051 (while l
1052 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1053 sum)))
1055 (defun org-columns-number-to-string (n fmt &optional printf)
1056 "Convert a computed column number to a string value, according to FMT."
1057 (cond
1058 ((memq fmt '(estimate)) (org-estimate-print n printf))
1059 ((not (numberp n)) "")
1060 ((memq fmt '(add_times max_times min_times mean_times))
1061 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
1062 (format org-time-clocksum-format h m)))
1063 ((eq fmt 'checkbox)
1064 (cond ((= n (floor n)) "[X]")
1065 ((> n 1.) "[-]")
1066 (t "[ ]")))
1067 ((memq fmt '(checkbox-n-of-m checkbox-percent))
1068 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
1069 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
1070 (printf (format printf n))
1071 ((eq fmt 'currency)
1072 (format "%.2f" n))
1073 ((memq fmt '(min_age max_age mean_age))
1074 (org-format-time-period n))
1075 (t (number-to-string n))))
1077 (defun org-nofm-to-completion (n m &optional percent)
1078 (if (not percent)
1079 (format "[%d/%d]" n m)
1080 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
1083 (defun org-columns-string-to-number (s fmt)
1084 "Convert a column value to a number that can be used for column computing."
1085 (if s
1086 (cond
1087 ((memq fmt '(min_age max_age mean_age))
1088 (cond ((string= s "") org-columns-time)
1089 ((string-match
1090 "\\([0-9]+\\)d \\([0-9]+\\)h \\([0-9]+\\)m \\([0-9]+\\)s"
1092 (+ (* 60 (+ (* 60 (+ (* 24 (string-to-number (match-string 1 s)))
1093 (string-to-number (match-string 2 s))))
1094 (string-to-number (match-string 3 s))))
1095 (string-to-number (match-string 4 s))))
1096 (t (time-to-number-of-days (apply 'encode-time
1097 (org-parse-time-string s t))))))
1098 ((string-match ":" s)
1099 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1100 (while l
1101 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1102 sum))
1103 ((string-match (concat "\\([0-9.]+\\) *\\("
1104 (regexp-opt (mapcar 'car org-effort-durations))
1105 "\\)") s)
1106 (setq s (concat "0:" (org-duration-string-to-minutes s t)))
1107 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1108 (while l
1109 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1110 sum))
1111 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
1112 (if (equal s "[X]") 1. 0.000001))
1113 ((memq fmt '(estimate)) (org-string-to-estimate s))
1114 (t (string-to-number s)))))
1116 (defun org-columns-uncompile-format (cfmt)
1117 "Turn the compiled columns format back into a string representation."
1118 (let ((rtn "") e s prop title op op-match width fmt printf fun calc)
1119 (while (setq e (pop cfmt))
1120 (setq prop (car e)
1121 title (nth 1 e)
1122 width (nth 2 e)
1123 op (nth 3 e)
1124 fmt (nth 4 e)
1125 printf (nth 5 e)
1126 fun (nth 6 e)
1127 calc (nth 7 e))
1128 (when (setq op-match (rassoc (list fmt fun calc) org-columns-compile-map))
1129 (setq op (car op-match)))
1130 (if (and op printf) (setq op (concat op ";" printf)))
1131 (if (equal title prop) (setq title nil))
1132 (setq s (concat "%" (if width (number-to-string width))
1133 prop
1134 (if title (concat "(" title ")"))
1135 (if op (concat "{" op "}"))))
1136 (setq rtn (concat rtn " " s)))
1137 (org-trim rtn)))
1139 (defun org-columns-compile-format (fmt)
1140 "Turn a column format string into an alist of specifications.
1141 The alist has one entry for each column in the format. The elements of
1142 that list are:
1143 property the property
1144 title the title field for the columns
1145 width the column width in characters, can be nil for automatic
1146 operator the operator if any
1147 format the output format for computed results, derived from operator
1148 printf a printf format for computed values
1149 fun the lisp function to compute summary values, derived from operator
1150 calc function to get values from base elements"
1151 (let ((start 0) width prop title op op-match f printf fun calc)
1152 (setq org-columns-current-fmt-compiled nil)
1153 (while (string-match
1154 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
1155 fmt start)
1156 (setq start (match-end 0)
1157 width (match-string 1 fmt)
1158 prop (match-string 2 fmt)
1159 title (or (match-string 3 fmt) prop)
1160 op (match-string 4 fmt)
1161 f nil
1162 printf nil
1163 fun '+
1164 calc nil)
1165 (if width (setq width (string-to-number width)))
1166 (when (and op (string-match ";" op))
1167 (setq printf (substring op (match-end 0))
1168 op (substring op 0 (match-beginning 0))))
1169 (when (setq op-match (assoc op org-columns-compile-map))
1170 (setq f (cadr op-match)
1171 fun (caddr op-match)
1172 calc (cadddr op-match)))
1173 (push (list prop title width op f printf fun calc)
1174 org-columns-current-fmt-compiled))
1175 (setq org-columns-current-fmt-compiled
1176 (nreverse org-columns-current-fmt-compiled))))
1179 ;;; Dynamic block for Column view
1181 (defvar org-heading-regexp) ; defined in org.el
1182 (defvar org-heading-keyword-regexp-format) ; defined in org.el
1183 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
1184 "Get the column view of the current buffer or subtree.
1185 The first optional argument MAXLEVEL sets the level limit. A
1186 second optional argument SKIP-EMPTY-ROWS tells whether to skip
1187 empty rows, an empty row being one where all the column view
1188 specifiers except ITEM are empty. This function returns a list
1189 containing the title row and all other rows. Each row is a list
1190 of fields."
1191 (save-excursion
1192 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1193 (re-comment (format org-heading-keyword-regexp-format
1194 org-comment-string))
1195 (re-archive (concat ".*:" org-archive-tag ":"))
1196 (n (length title)) row tbl)
1197 (goto-char (point-min))
1198 (while (re-search-forward org-heading-regexp nil t)
1199 (catch 'next
1200 (when (and (or (null maxlevel)
1201 (>= maxlevel
1202 (if org-odd-levels-only
1203 (/ (1+ (length (match-string 1))) 2)
1204 (length (match-string 1)))))
1205 (get-char-property (match-beginning 0) 'org-columns-key))
1206 (when (save-excursion
1207 (goto-char (point-at-bol))
1208 (or (looking-at re-comment)
1209 (looking-at re-archive)))
1210 (org-end-of-subtree t)
1211 (throw 'next t))
1212 (setq row nil)
1213 (loop for i from 0 to (1- n) do
1214 (push
1215 (org-quote-vert
1216 (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
1217 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
1218 ""))
1219 row))
1220 (setq row (nreverse row))
1221 (unless (and skip-empty-rows
1222 (eq 1 (length (delete "" (delete-dups (copy-sequence row))))))
1223 (push row tbl)))))
1224 (append (list title 'hline) (nreverse tbl)))))
1226 (defun org-dblock-write:columnview (params)
1227 "Write the column view table.
1228 PARAMS is a property list of parameters:
1230 :width enforce same column widths with <N> specifiers.
1231 :id the :ID: property of the entry where the columns view
1232 should be built. When the symbol `local', call locally.
1233 When `global' call column view with the cursor at the beginning
1234 of the buffer (usually this means that the whole buffer switches
1235 to column view). When \"file:path/to/file.org\", invoke column
1236 view at the start of that file. Otherwise, the ID is located
1237 using `org-id-find'.
1238 :hlines When t, insert a hline before each item. When a number, insert
1239 a hline before each level <= that number.
1240 :vlines When t, make each column a colgroup to enforce vertical lines.
1241 :maxlevel When set to a number, don't capture headlines below this level.
1242 :skip-empty-rows
1243 When t, skip rows where all specifiers other than ITEM are empty.
1244 :format When non-nil, specify the column view format to use."
1245 (let ((pos (move-marker (make-marker) (point)))
1246 (hlines (plist-get params :hlines))
1247 (vlines (plist-get params :vlines))
1248 (maxlevel (plist-get params :maxlevel))
1249 (content-lines (org-split-string (plist-get params :content) "\n"))
1250 (skip-empty-rows (plist-get params :skip-empty-rows))
1251 (columns-fmt (plist-get params :format))
1252 (case-fold-search t)
1253 tbl id idpos nfields tmp recalc line
1254 id-as-string view-file view-pos)
1255 (when (setq id (plist-get params :id))
1256 (setq id-as-string (cond ((numberp id) (number-to-string id))
1257 ((symbolp id) (symbol-name id))
1258 ((stringp id) id)
1259 (t "")))
1260 (cond ((not id) nil)
1261 ((eq id 'global) (setq view-pos (point-min)))
1262 ((eq id 'local))
1263 ((string-match "^file:\\(.*\\)" id-as-string)
1264 (setq view-file (match-string 1 id-as-string)
1265 view-pos 1)
1266 (unless (file-exists-p view-file)
1267 (error "No such file: \"%s\"" id-as-string)))
1268 ((setq idpos (org-find-entry-with-id id))
1269 (setq view-pos idpos))
1270 ((setq idpos (org-id-find id))
1271 (setq view-file (car idpos))
1272 (setq view-pos (cdr idpos)))
1273 (t (error "Cannot find entry with :ID: %s" id))))
1274 (with-current-buffer (if view-file
1275 (get-file-buffer view-file)
1276 (current-buffer))
1277 (save-excursion
1278 (save-restriction
1279 (widen)
1280 (goto-char (or view-pos (point)))
1281 (org-columns columns-fmt)
1282 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1283 (setq nfields (length (car tbl)))
1284 (org-columns-quit))))
1285 (goto-char pos)
1286 (move-marker pos nil)
1287 (when tbl
1288 (when (plist-get params :hlines)
1289 (setq tmp nil)
1290 (while tbl
1291 (if (eq (car tbl) 'hline)
1292 (push (pop tbl) tmp)
1293 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1294 (if (and (not (eq (car tmp) 'hline))
1295 (or (eq hlines t)
1296 (and (numberp hlines)
1297 (<= (- (match-end 1) (match-beginning 1))
1298 hlines))))
1299 (push 'hline tmp)))
1300 (push (pop tbl) tmp)))
1301 (setq tbl (nreverse tmp)))
1302 (when vlines
1303 (setq tbl (mapcar (lambda (x)
1304 (if (eq 'hline x) x (cons "" x)))
1305 tbl))
1306 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1307 (setq pos (point))
1308 (when content-lines
1309 (while (string-match "^#" (car content-lines))
1310 (insert (pop content-lines) "\n")))
1311 (insert (org-listtable-to-string tbl))
1312 (when (plist-get params :width)
1313 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1314 org-columns-current-widths "|")))
1315 (while (setq line (pop content-lines))
1316 (when (string-match "^#" line)
1317 (insert "\n" line)
1318 (when (string-match "^[ \t]*#\\+tblfm" line)
1319 (setq recalc t))))
1320 (if recalc
1321 (progn (goto-char pos) (org-table-recalculate 'all))
1322 (goto-char pos)
1323 (org-table-align)))))
1325 (defun org-listtable-to-string (tbl)
1326 "Convert a listtable TBL to a string that contains the Org-mode table.
1327 The table still need to be aligned. The resulting string has no leading
1328 and tailing newline characters."
1329 (mapconcat
1330 (lambda (x)
1331 (cond
1332 ((listp x)
1333 (concat "|" (mapconcat 'identity x "|") "|"))
1334 ((eq x 'hline) "|-|")
1335 (t (error "Garbage in listtable: %s" x))))
1336 tbl "\n"))
1338 (defun org-insert-columns-dblock ()
1339 "Create a dynamic block capturing a column view table."
1340 (interactive)
1341 (let ((defaults '(:name "columnview" :hlines 1))
1342 (id (org-icompleting-read
1343 "Capture columns (local, global, entry with :ID: property) [local]: "
1344 (append '(("global") ("local"))
1345 (mapcar 'list (org-property-values "ID"))))))
1346 (if (equal id "") (setq id 'local))
1347 (if (equal id "global") (setq id 'global))
1348 (setq defaults (append defaults (list :id id)))
1349 (org-create-dblock defaults)
1350 (org-update-dblock)))
1352 ;;; Column view in the agenda
1354 (defvar org-agenda-view-columns-initially nil
1355 "When set, switch to columns view immediately after creating the agenda.")
1357 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1358 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1359 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1361 (defun org-agenda-columns ()
1362 "Turn on or update column view in the agenda."
1363 (interactive)
1364 (org-verify-version 'columns)
1365 (org-columns-remove-overlays)
1366 (move-marker org-columns-begin-marker (point))
1367 (let ((org-columns-time (time-to-number-of-days (current-time)))
1368 cache maxwidths m p a d fmt)
1369 (cond
1370 ((and (boundp 'org-agenda-overriding-columns-format)
1371 org-agenda-overriding-columns-format)
1372 (setq fmt org-agenda-overriding-columns-format)
1373 (org-set-local 'org-agenda-overriding-columns-format fmt))
1374 ((setq m (org-get-at-bol 'org-hd-marker))
1375 (setq fmt (or (org-entry-get m "COLUMNS" t)
1376 (with-current-buffer (marker-buffer m)
1377 org-columns-default-format))))
1378 ((and (boundp 'org-columns-current-fmt)
1379 (local-variable-p 'org-columns-current-fmt)
1380 org-columns-current-fmt)
1381 (setq fmt org-columns-current-fmt))
1382 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
1383 (setq m (get-text-property m 'org-hd-marker))
1384 (setq fmt (or (org-entry-get m "COLUMNS" t)
1385 (with-current-buffer (marker-buffer m)
1386 org-columns-default-format)))))
1387 (setq fmt (or fmt org-columns-default-format))
1388 (org-set-local 'org-columns-current-fmt fmt)
1389 (org-columns-compile-format fmt)
1390 (when org-agenda-columns-compute-summary-properties
1391 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1392 (save-excursion
1393 ;; Get and cache the properties
1394 (goto-char (point-min))
1395 (while (not (eobp))
1396 (when (setq m (or (org-get-at-bol 'org-hd-marker)
1397 (org-get-at-bol 'org-marker)))
1398 (setq p (org-entry-properties m))
1400 (when (or (not (setq a (assoc org-effort-property p)))
1401 (not (string-match "\\S-" (or (cdr a) ""))))
1402 ;; OK, the property is not defined. Use appointment duration?
1403 (when (and org-agenda-columns-add-appointments-to-effort-sum
1404 (setq d (get-text-property (point) 'duration)))
1405 (setq d (org-minutes-to-hh:mm-string d))
1406 (put-text-property 0 (length d) 'face 'org-warning d)
1407 (push (cons org-effort-property d) p)))
1408 (push (cons (org-current-line) p) cache))
1409 (beginning-of-line 2))
1410 (when cache
1411 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
1412 (org-set-local 'org-columns-current-maxwidths maxwidths)
1413 (org-columns-display-here-title)
1414 (when (org-set-local 'org-columns-flyspell-was-active
1415 (org-bound-and-true-p flyspell-mode))
1416 (flyspell-mode 0))
1417 (mapc (lambda (x)
1418 (org-goto-line (car x))
1419 (org-columns-display-here (cdr x)))
1420 cache)
1421 (when org-agenda-columns-show-summaries
1422 (org-agenda-colview-summarize cache))))))
1424 (defun org-agenda-colview-summarize (cache)
1425 "Summarize the summarizable columns in column view in the agenda.
1426 This will add overlays to the date lines, to show the summary for each day."
1427 (let* ((fmt (mapcar (lambda (x)
1428 (if (string-match "CLOCKSUM.*" (car x))
1429 (list (match-string 0) (nth 1 x) (nth 2 x) ":" 'add_times
1430 nil '+ nil)
1432 org-columns-current-fmt-compiled))
1433 line c c1 stype calc sumfunc props lsum entries prop v title)
1434 (catch 'exit
1435 (when (delq nil (mapcar 'cadr fmt))
1436 ;; OK, at least one summation column, it makes sense to try this
1437 (goto-char (point-max))
1438 (while t
1439 (when (or (get-text-property (point) 'org-date-line)
1440 (eq (get-text-property (point) 'face)
1441 'org-agenda-structure))
1442 ;; OK, this is a date line that should be used
1443 (setq line (org-current-line))
1444 (setq entries nil c cache cache nil)
1445 (while (setq c1 (pop c))
1446 (if (> (car c1) line)
1447 (push c1 entries)
1448 (push c1 cache)))
1449 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1450 ;; Compute the summaries for the properties we want,
1451 ;; set nil properties for the rest.
1452 (when (setq entries (mapcar 'cdr entries))
1453 (setq props
1454 (mapcar
1455 (lambda (f)
1456 (setq prop (car f)
1457 title (nth 1 f)
1458 stype (nth 4 f)
1459 sumfunc (nth 6 f)
1460 calc (or (nth 7 f) 'identity))
1461 (cond
1462 ((equal prop "ITEM")
1463 (cons prop (buffer-substring (point-at-bol)
1464 (point-at-eol))))
1465 ((not stype) (cons prop ""))
1466 (t ;; do the summary
1467 (setq lsum nil)
1468 (dolist (x entries)
1469 (setq v (cdr (assoc prop x)))
1470 (if v
1471 (push
1472 (funcall
1473 (if (not (get-text-property 0 'org-computed v))
1474 calc
1475 'identity)
1476 (org-columns-string-to-number
1477 v stype))
1478 lsum)))
1479 (setq lsum (remove nil lsum))
1480 (setq lsum
1481 (cond ((> (length lsum) 1)
1482 (org-columns-number-to-string
1483 (apply sumfunc lsum) stype))
1484 ((eq (length lsum) 1)
1485 (org-columns-number-to-string
1486 (car lsum) stype))
1487 (t "")))
1488 (put-text-property 0 (length lsum) 'face 'bold lsum)
1489 (unless (eq calc 'identity)
1490 (put-text-property 0 (length lsum) 'org-computed t lsum))
1491 (cons prop lsum))))
1492 fmt))
1493 (org-columns-display-here props 'dateline)
1494 (org-set-local 'org-agenda-columns-active t)))
1495 (if (bobp) (throw 'exit t))
1496 (beginning-of-line 0))))))
1498 (defun org-agenda-colview-compute (fmt)
1499 "Compute the relevant columns in the contributing source buffers."
1500 (let ((files org-agenda-contributing-files)
1501 (org-columns-begin-marker (make-marker))
1502 (org-columns-top-level-marker (make-marker))
1503 f fm a b)
1504 (while (setq f (pop files))
1505 (setq b (find-buffer-visiting f))
1506 (with-current-buffer (or (buffer-base-buffer b) b)
1507 (save-excursion
1508 (save-restriction
1509 (widen)
1510 (org-unmodified
1511 (remove-text-properties (point-min) (point-max)
1512 '(org-summaries t)))
1513 (goto-char (point-min))
1514 (org-columns-get-format-and-top-level)
1515 (while (setq fm (pop fmt))
1516 (cond ((equal (car fm) "CLOCKSUM")
1517 (org-clock-sum))
1518 ((equal (car fm) "CLOCKSUM_T")
1519 (org-clock-sum-today))
1520 ((and (nth 4 fm)
1521 (setq a (assoc (car fm)
1522 org-columns-current-fmt-compiled))
1523 (equal (nth 4 a) (nth 4 fm)))
1524 (org-columns-compute (car fm)))))))))))
1526 (defun org-format-time-period (interval)
1527 "Convert time in fractional days to days/hours/minutes/seconds."
1528 (if (numberp interval)
1529 (let* ((days (floor interval))
1530 (frac-hours (* 24 (- interval days)))
1531 (hours (floor frac-hours))
1532 (minutes (floor (* 60 (- frac-hours hours))))
1533 (seconds (floor (* 60 (- (* 60 (- frac-hours hours)) minutes)))))
1534 (format "%dd %02dh %02dm %02ds" days hours minutes seconds))
1535 ""))
1537 (defun org-estimate-mean-and-var (v)
1538 "Return the mean and variance of an estimate."
1539 (let* ((low (float (car v)))
1540 (high (float (cadr v)))
1541 (mean (/ (+ low high) 2.0))
1542 (var (/ (+ (expt (- mean low) 2.0) (expt (- high mean) 2.0)) 2.0)))
1543 (list mean var)))
1545 (defun org-estimate-combine (&rest el)
1546 "Combine a list of estimates, using mean and variance.
1547 The mean and variance of the result will be the sum of the means
1548 and variances (respectively) of the individual estimates."
1549 (let ((mean 0)
1550 (var 0))
1551 (mapc (lambda (e)
1552 (let ((stats (org-estimate-mean-and-var e)))
1553 (setq mean (+ mean (car stats)))
1554 (setq var (+ var (cadr stats)))))
1556 (let ((stdev (sqrt var)))
1557 (list (- mean stdev) (+ mean stdev)))))
1559 (defun org-estimate-print (e &optional fmt)
1560 "Prepare a string representation of an estimate.
1561 This formats these numbers as two numbers with a \"-\" between them."
1562 (if (null fmt) (set 'fmt "%.0f"))
1563 (format "%s" (mapconcat (lambda (n) (format fmt n)) e "-")))
1565 (defun org-string-to-estimate (s)
1566 "Convert a string to an estimate.
1567 The string should be two numbers joined with a \"-\"."
1568 (if (string-match "\\(.*\\)-\\(.*\\)" s)
1569 (list (string-to-number (match-string 1 s))
1570 (string-to-number(match-string 2 s)))
1571 (list (string-to-number s) (string-to-number s))))
1573 (provide 'org-colview)
1575 ;;; org-colview.el ends here