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