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