Release 6.21a
[org-mode/org-tableheadings.git] / lisp / org-colview.el
blobd5f6007c46a97ce5aacdc103daa85cc421970c03
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.21a
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* ((external-key key)
387 (col (current-column))
388 (key (or key (get-char-property (point) 'org-columns-key)))
389 (value (get-char-property (point) 'org-columns-value))
390 (bol (point-at-bol)) (eol (point-at-eol))
391 (pom (or (get-text-property bol 'org-hd-marker)
392 (point))) ; keep despite of compiler waring
393 (line-overlays
394 (delq nil (mapcar (lambda (x)
395 (and (eq (overlay-buffer x) (current-buffer))
396 (>= (overlay-start x) bol)
397 (<= (overlay-start x) eol)
399 org-columns-overlays)))
400 nval eval allowed)
401 (cond
402 ((equal key "CLOCKSUM")
403 (error "This special column cannot be edited"))
404 ((equal key "ITEM")
405 (setq eval '(org-with-point-at pom
406 (org-edit-headline))))
407 ((equal key "TODO")
408 (setq eval '(org-with-point-at pom
409 (let ((current-prefix-arg
410 (if external-key current-prefix-arg '(4))))
411 (call-interactively 'org-todo)))))
412 ((equal key "PRIORITY")
413 (setq eval '(org-with-point-at pom
414 (call-interactively 'org-priority))))
415 ((equal key "TAGS")
416 (setq eval '(org-with-point-at pom
417 (let ((org-fast-tag-selection-single-key
418 (if (eq org-fast-tag-selection-single-key 'expert)
419 t org-fast-tag-selection-single-key)))
420 (call-interactively 'org-set-tags)))))
421 ((equal key "DEADLINE")
422 (setq eval '(org-with-point-at pom
423 (call-interactively 'org-deadline))))
424 ((equal key "SCHEDULED")
425 (setq eval '(org-with-point-at pom
426 (call-interactively 'org-schedule))))
428 (setq allowed (org-property-get-allowed-values pom key 'table))
429 (if allowed
430 (setq nval (org-ido-completing-read "Value: " allowed nil t))
431 (setq nval (read-string "Edit: " value)))
432 (setq nval (org-trim nval))
433 (when (not (equal nval value))
434 (setq eval '(org-entry-put pom key nval)))))
435 (when eval
437 (cond
438 ((equal major-mode 'org-agenda-mode)
439 (org-columns-eval eval)
440 ;; The following let preserves the current format, and makes sure
441 ;; that in only a single file things need to be upated.
442 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
443 (buffer (marker-buffer pom))
444 (org-agenda-contributing-files
445 (list (with-current-buffer buffer
446 (buffer-file-name (buffer-base-buffer))))))
447 (org-agenda-columns)))
449 (let ((inhibit-read-only t))
450 (org-unmodified
451 (remove-text-properties
452 (max (point-min) (1- bol)) eol '(read-only t)))
453 (unwind-protect
454 (progn
455 (setq org-columns-overlays
456 (org-delete-all line-overlays org-columns-overlays))
457 (mapc 'org-delete-overlay line-overlays)
458 (org-columns-eval eval))
459 (org-columns-display-here)))
460 (org-move-to-column col)
461 (if (and (org-mode-p)
462 (nth 3 (assoc key org-columns-current-fmt-compiled)))
463 (org-columns-update key)))))))
465 (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
466 "Edit the current headline, the part without TODO keyword, TAGS."
467 (org-back-to-heading)
468 (when (looking-at org-todo-line-regexp)
469 (let ((pos (point))
470 (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
471 (txt (match-string 3))
472 (post "")
473 txt2)
474 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
475 (setq post (match-string 0 txt)
476 txt (substring txt 0 (match-beginning 0))))
477 (setq txt2 (read-string "Edit: " txt))
478 (when (not (equal txt txt2))
479 (goto-char pos)
480 (insert pre txt2 post)
481 (delete-region (point) (point-at-eol))
482 (org-set-tags nil t)))))
484 (defun org-columns-edit-allowed ()
485 "Edit the list of allowed values for the current property."
486 (interactive)
487 (let* ((pom (or (get-text-property (point-at-bol) 'org-marker)
488 (get-text-property (point-at-bol) 'org-hd-marker)
489 (point)))
490 (key (get-char-property (point) 'org-columns-key))
491 (key1 (concat key "_ALL"))
492 (allowed (org-entry-get pom key1 t))
493 nval)
494 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
495 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
496 (setq nval (read-string "Allowed: " allowed))
497 (org-entry-put
498 (cond ((marker-position org-entry-property-inherited-from)
499 org-entry-property-inherited-from)
500 ((marker-position org-columns-top-level-marker)
501 org-columns-top-level-marker)
502 (t pom))
503 key1 nval)))
505 (defun org-columns-eval (form)
506 (let (hidep)
507 (save-excursion
508 (beginning-of-line 1)
509 ;; `next-line' is needed here, because it skips invisible line.
510 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
511 (setq hidep (org-on-heading-p 1)))
512 (eval form)
513 (and hidep (hide-entry))))
515 (defun org-columns-previous-allowed-value ()
516 "Switch to the previous allowed value for this column."
517 (interactive)
518 (org-columns-next-allowed-value t))
520 (defun org-columns-next-allowed-value (&optional previous nth)
521 "Switch to the next allowed value for this column.
522 When PREVIOUS is set, go to the previous value. When NTH is
523 an integer, select that value."
524 (interactive)
525 (org-columns-check-computed)
526 (let* ((col (current-column))
527 (key (get-char-property (point) 'org-columns-key))
528 (value (get-char-property (point) 'org-columns-value))
529 (bol (point-at-bol)) (eol (point-at-eol))
530 (pom (or (get-text-property bol 'org-hd-marker)
531 (point))) ; keep despite of compiler waring
532 (line-overlays
533 (delq nil (mapcar (lambda (x)
534 (and (eq (overlay-buffer x) (current-buffer))
535 (>= (overlay-start x) bol)
536 (<= (overlay-start x) eol)
538 org-columns-overlays)))
539 (allowed (or (org-property-get-allowed-values pom key)
540 (and (memq
541 (nth 4 (assoc key org-columns-current-fmt-compiled))
542 '(checkbox checkbox-n-of-m checkbox-percent))
543 '("[ ]" "[X]"))
544 (org-colview-construct-allowed-dates value)))
545 nval)
546 (when (integerp nth)
547 (setq nth (1- nth))
548 (if (= nth -1) (setq nth 9)))
549 (when (equal key "ITEM")
550 (error "Cannot edit item headline from here"))
551 (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
552 (error "Allowed values for this property have not been defined"))
553 (if (member key '("SCHEDULED" "DEADLINE"))
554 (setq nval (if previous 'earlier 'later))
555 (if previous (setq allowed (reverse allowed)))
556 (cond
557 (nth
558 (setq nval (nth nth allowed))
559 (if (not nval)
560 (error "There are only %d allowed values for property `%s'"
561 (length allowed) key)))
562 ((member value allowed)
563 (setq nval (or (car (cdr (member value allowed)))
564 (car allowed)))
565 (if (equal nval value)
566 (error "Only one allowed value for this property")))
567 (t (setq nval (car allowed)))))
568 (cond
569 ((equal major-mode 'org-agenda-mode)
570 (org-columns-eval '(org-entry-put pom key nval))
571 ;; The following let preserves the current format, and makes sure
572 ;; that in only a single file things need to be upated.
573 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
574 (buffer (marker-buffer pom))
575 (org-agenda-contributing-files
576 (list (with-current-buffer buffer
577 (buffer-file-name (buffer-base-buffer))))))
578 (org-agenda-columns)))
580 (let ((inhibit-read-only t))
581 (remove-text-properties (1- bol) eol '(read-only t))
582 (unwind-protect
583 (progn
584 (setq org-columns-overlays
585 (org-delete-all line-overlays org-columns-overlays))
586 (mapc 'org-delete-overlay line-overlays)
587 (org-columns-eval '(org-entry-put pom key nval)))
588 (org-columns-display-here)))
589 (org-move-to-column col)
590 (and (nth 3 (assoc key org-columns-current-fmt-compiled))
591 (org-columns-update key))))))
593 (defun org-colview-construct-allowed-dates (s)
594 "Construct a list of three dates around the date in S.
595 This respects the format of the time stamp in S, active or non-active,
596 and also including time or not. S must be just a time stamp, no text
597 around it."
598 (when (and s (string-match (concat "^" org-ts-regexp3 "$") s))
599 (let* ((time (org-parse-time-string s 'nodefaults))
600 (active (equal (string-to-char s) ?<))
601 (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
602 time-before time-after)
603 (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
604 (setf (car time) (or (car time) 0))
605 (setf (nth 1 time) (or (nth 1 time) 0))
606 (setf (nth 2 time) (or (nth 2 time) 0))
607 (setq time-before (copy-sequence time))
608 (setq time-after (copy-sequence time))
609 (setf (nth 3 time-before) (1- (nth 3 time)))
610 (setf (nth 3 time-after) (1+ (nth 3 time)))
611 (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
612 (list time-before time time-after)))))
614 (defun org-verify-version (task)
615 (cond
616 ((eq task 'columns)
617 (if (or (featurep 'xemacs)
618 (< emacs-major-version 22))
619 (error "Emacs 22 is required for the columns feature")))))
621 (defun org-columns-open-link (&optional arg)
622 (interactive "P")
623 (let ((value (get-char-property (point) 'org-columns-value)))
624 (org-open-link-from-string value arg)))
626 (defun org-columns-get-format-and-top-level ()
627 (let (fmt)
628 (when (condition-case nil (org-back-to-heading) (error nil))
629 (setq fmt (org-entry-get nil "COLUMNS" t)))
630 (setq fmt (or fmt org-columns-default-format))
631 (org-set-local 'org-columns-current-fmt fmt)
632 (org-columns-compile-format fmt)
633 (if (marker-position org-entry-property-inherited-from)
634 (move-marker org-columns-top-level-marker
635 org-entry-property-inherited-from)
636 (move-marker org-columns-top-level-marker (point)))
637 fmt))
639 (defun org-columns ()
640 "Turn on column view on an org-mode file."
641 (interactive)
642 (org-verify-version 'columns)
643 (org-columns-remove-overlays)
644 (move-marker org-columns-begin-marker (point))
645 (let (beg end fmt cache maxwidths)
646 (setq fmt (org-columns-get-format-and-top-level))
647 (save-excursion
648 (goto-char org-columns-top-level-marker)
649 (setq beg (point))
650 (unless org-columns-inhibit-recalculation
651 (org-columns-compute-all))
652 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
653 (point-max)))
654 ;; Get and cache the properties
655 (goto-char beg)
656 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
657 (save-excursion
658 (save-restriction
659 (narrow-to-region beg end)
660 (org-clock-sum))))
661 (while (re-search-forward (concat "^" outline-regexp) end t)
662 (push (cons (org-current-line) (org-entry-properties)) cache))
663 (when cache
664 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
665 (org-set-local 'org-columns-current-maxwidths maxwidths)
666 (org-columns-display-here-title)
667 (when (org-set-local 'org-columns-flyspell-was-active
668 (org-bound-and-true-p flyspell-mode))
669 (flyspell-mode 0))
670 (mapc (lambda (x)
671 (goto-line (car x))
672 (org-columns-display-here (cdr x)))
673 cache)))))
675 (defun org-columns-new (&optional prop title width op fmt &rest rest)
676 "Insert a new column, to the left of the current column."
677 (interactive)
678 (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
679 cell)
680 (setq prop (org-ido-completing-read
681 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
682 nil nil prop))
683 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
684 (setq width (read-string "Column width: " (if width (number-to-string width))))
685 (if (string-match "\\S-" width)
686 (setq width (string-to-number width))
687 (setq width nil))
688 (setq fmt (org-ido-completing-read "Summary [none]: "
689 '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
690 nil t))
691 (if (string-match "\\S-" fmt)
692 (setq fmt (intern fmt))
693 (setq fmt nil))
694 (if (eq fmt 'none) (setq fmt nil))
695 (if editp
696 (progn
697 (setcar editp prop)
698 (setcdr editp (list title width nil fmt)))
699 (setq cell (nthcdr (1- (current-column))
700 org-columns-current-fmt-compiled))
701 (setcdr cell (cons (list prop title width nil fmt)
702 (cdr cell))))
703 (org-columns-store-format)
704 (org-columns-redo)))
706 (defun org-columns-delete ()
707 "Delete the column at point from columns view."
708 (interactive)
709 (let* ((n (current-column))
710 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
711 (when (y-or-n-p
712 (format "Are you sure you want to remove column \"%s\"? " title))
713 (setq org-columns-current-fmt-compiled
714 (delq (nth n org-columns-current-fmt-compiled)
715 org-columns-current-fmt-compiled))
716 (org-columns-store-format)
717 (org-columns-redo)
718 (if (>= (current-column) (length org-columns-current-fmt-compiled))
719 (backward-char 1)))))
721 (defun org-columns-edit-attributes ()
722 "Edit the attributes of the current column."
723 (interactive)
724 (let* ((n (current-column))
725 (info (nth n org-columns-current-fmt-compiled)))
726 (apply 'org-columns-new info)))
728 (defun org-columns-widen (arg)
729 "Make the column wider by ARG characters."
730 (interactive "p")
731 (let* ((n (current-column))
732 (entry (nth n org-columns-current-fmt-compiled))
733 (width (or (nth 2 entry)
734 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
735 (setq width (max 1 (+ width arg)))
736 (setcar (nthcdr 2 entry) width)
737 (org-columns-store-format)
738 (org-columns-redo)))
740 (defun org-columns-narrow (arg)
741 "Make the column narrower by ARG characters."
742 (interactive "p")
743 (org-columns-widen (- arg)))
745 (defun org-columns-move-right ()
746 "Swap this column with the one to the right."
747 (interactive)
748 (let* ((n (current-column))
749 (cell (nthcdr n org-columns-current-fmt-compiled))
751 (when (>= n (1- (length org-columns-current-fmt-compiled)))
752 (error "Cannot shift this column further to the right"))
753 (setq e (car cell))
754 (setcar cell (car (cdr cell)))
755 (setcdr cell (cons e (cdr (cdr cell))))
756 (org-columns-store-format)
757 (org-columns-redo)
758 (forward-char 1)))
760 (defun org-columns-move-left ()
761 "Swap this column with the one to the left."
762 (interactive)
763 (let* ((n (current-column)))
764 (when (= n 0)
765 (error "Cannot shift this column further to the left"))
766 (backward-char 1)
767 (org-columns-move-right)
768 (backward-char 1)))
770 (defun org-columns-store-format ()
771 "Store the text version of the current columns format in appropriate place.
772 This is either in the COLUMNS property of the node starting the current column
773 display, or in the #+COLUMNS line of the current buffer."
774 (let (fmt (cnt 0))
775 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
776 (org-set-local 'org-columns-current-fmt fmt)
777 (if (marker-position org-columns-top-level-marker)
778 (save-excursion
779 (goto-char org-columns-top-level-marker)
780 (if (and (org-at-heading-p)
781 (org-entry-get nil "COLUMNS"))
782 (org-entry-put nil "COLUMNS" fmt)
783 (goto-char (point-min))
784 ;; Overwrite all #+COLUMNS lines....
785 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
786 (setq cnt (1+ cnt))
787 (replace-match (concat "#+COLUMNS: " fmt) t t))
788 (unless (> cnt 0)
789 (goto-char (point-min))
790 (or (org-on-heading-p t) (outline-next-heading))
791 (let ((inhibit-read-only t))
792 (insert-before-markers "#+COLUMNS: " fmt "\n")))
793 (org-set-local 'org-columns-default-format fmt))))))
795 (defvar org-agenda-overriding-columns-format nil
796 "When set, overrides any other format definition for the agenda.
797 Don't set this, this is meant for dynamic scoping.")
799 (defun org-columns-get-autowidth-alist (s cache)
800 "Derive the maximum column widths from the format and the cache."
801 (let ((start 0) rtn)
802 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
803 (push (cons (match-string 1 s) 1) rtn)
804 (setq start (match-end 0)))
805 (mapc (lambda (x)
806 (setcdr x (apply 'max
807 (mapcar
808 (lambda (y)
809 (length (or (cdr (assoc (car x) (cdr y))) " ")))
810 cache))))
811 rtn)
812 rtn))
814 (defun org-columns-compute-all ()
815 "Compute all columns that have operators defined."
816 (org-unmodified
817 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
818 (let ((columns org-columns-current-fmt-compiled) col)
819 (while (setq col (pop columns))
820 (when (nth 3 col)
821 (save-excursion
822 (org-columns-compute (car col)))))))
824 (defun org-columns-update (property)
825 "Recompute PROPERTY, and update the columns display for it."
826 (org-columns-compute property)
827 (let (fmt val pos)
828 (save-excursion
829 (mapc (lambda (ov)
830 (when (equal (org-overlay-get ov 'org-columns-key) property)
831 (setq pos (org-overlay-start ov))
832 (goto-char pos)
833 (when (setq val (cdr (assoc property
834 (get-text-property
835 (point-at-bol) 'org-summaries))))
836 (setq fmt (org-overlay-get ov 'org-columns-format))
837 (org-overlay-put ov 'org-columns-value val)
838 (org-overlay-put ov 'display (format fmt val)))))
839 org-columns-overlays))))
841 (defun org-columns-compute (property)
842 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
843 (interactive)
844 (let* ((re (concat "^" outline-regexp))
845 (lmax 30) ; Does anyone use deeper levels???
846 (lsum (make-vector lmax 0))
847 (lflag (make-vector lmax nil))
848 (level 0)
849 (ass (assoc property org-columns-current-fmt-compiled))
850 (format (nth 4 ass))
851 (printf (nth 5 ass))
852 (beg org-columns-top-level-marker)
853 last-level val valflag flag end sumpos sum-alist sum str str1 useval)
854 (save-excursion
855 ;; Find the region to compute
856 (goto-char beg)
857 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
858 (goto-char end)
859 ;; Walk the tree from the back and do the computations
860 (while (re-search-backward re beg t)
861 (setq sumpos (match-beginning 0)
862 last-level level
863 level (org-outline-level)
864 val (org-entry-get nil property)
865 valflag (and val (string-match "\\S-" val)))
866 (cond
867 ((< level last-level)
868 ;; put the sum of lower levels here as a property
869 (setq sum (aref lsum last-level) ; current sum
870 flag (aref lflag last-level) ; any valid entries from children?
871 str (org-columns-number-to-string sum format printf)
872 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
873 useval (if flag str1 (if valflag val ""))
874 sum-alist (get-text-property sumpos 'org-summaries))
875 (if (assoc property sum-alist)
876 (setcdr (assoc property sum-alist) useval)
877 (push (cons property useval) sum-alist)
878 (org-unmodified
879 (add-text-properties sumpos (1+ sumpos)
880 (list 'org-summaries sum-alist))))
881 (when (and val (not (equal val (if flag str val))))
882 (org-entry-put nil property (if flag str val)))
883 ;; add current to current level accumulator
884 (when (or flag valflag)
885 (aset lsum level (+ (aref lsum level)
886 (if flag sum (org-column-string-to-number
887 (if flag str val) format))))
888 (aset lflag level t))
889 ;; clear accumulators for deeper levels
890 (loop for l from (1+ level) to (1- lmax) do
891 (aset lsum l 0)
892 (aset lflag l nil)))
893 ((>= level last-level)
894 ;; add what we have here to the accumulator for this level
895 (aset lsum level (+ (aref lsum level)
896 (org-column-string-to-number (or val "0") format)))
897 (and valflag (aset lflag level t)))
898 (t (error "This should not happen")))))))
900 (defun org-columns-redo ()
901 "Construct the column display again."
902 (interactive)
903 (message "Recomputing columns...")
904 (let ((line (org-current-line))
905 (col (current-column)))
906 (save-excursion
907 (if (marker-position org-columns-begin-marker)
908 (goto-char org-columns-begin-marker))
909 (org-columns-remove-overlays)
910 (if (org-mode-p)
911 (call-interactively 'org-columns)
912 (org-agenda-redo)
913 (call-interactively 'org-agenda-columns)))
914 (goto-line line)
915 (move-to-column col))
916 (message "Recomputing columns...done"))
918 (defun org-columns-not-in-agenda ()
919 (if (eq major-mode 'org-agenda-mode)
920 (error "This command is only allowed in Org-mode buffers")))
923 (defun org-string-to-number (s)
924 "Convert string to number, and interpret hh:mm:ss."
925 (if (not (string-match ":" s))
926 (string-to-number s)
927 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
928 (while l
929 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
930 sum)))
932 (defun org-columns-number-to-string (n fmt &optional printf)
933 "Convert a computed column number to a string value, according to FMT."
934 (cond
935 ((eq fmt 'add_times)
936 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
937 (format org-time-clocksum-format h m)))
938 ((eq fmt 'checkbox)
939 (cond ((= n (floor n)) "[X]")
940 ((> n 1.) "[-]")
941 (t "[ ]")))
942 ((memq fmt '(checkbox-n-of-m checkbox-percent))
943 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
944 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
945 (printf (format printf n))
946 ((eq fmt 'currency)
947 (format "%.2f" n))
948 (t (number-to-string n))))
950 (defun org-nofm-to-completion (n m &optional percent)
951 (if (not percent)
952 (format "[%d/%d]" n m)
953 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
955 (defun org-column-string-to-number (s fmt)
956 "Convert a column value to a number that can be used for column computing."
957 (cond
958 ((string-match ":" s)
959 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
960 (while l
961 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
962 sum))
963 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
964 (if (equal s "[X]") 1. 0.000001))
965 (t (string-to-number s))))
967 (defun org-columns-uncompile-format (cfmt)
968 "Turn the compiled columns format back into a string representation."
969 (let ((rtn "") e s prop title op width fmt printf)
970 (while (setq e (pop cfmt))
971 (setq prop (car e)
972 title (nth 1 e)
973 width (nth 2 e)
974 op (nth 3 e)
975 fmt (nth 4 e)
976 printf (nth 5 e))
977 (cond
978 ((eq fmt 'add_times) (setq op ":"))
979 ((eq fmt 'checkbox) (setq op "X"))
980 ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
981 ((eq fmt 'checkbox-percent) (setq op "X%"))
982 ((eq fmt 'add_numbers) (setq op "+"))
983 ((eq fmt 'currency) (setq op "$")))
984 (if (and op printf) (setq op (concat op ";" printf)))
985 (if (equal title prop) (setq title nil))
986 (setq s (concat "%" (if width (number-to-string width))
987 prop
988 (if title (concat "(" title ")"))
989 (if op (concat "{" op "}"))))
990 (setq rtn (concat rtn " " s)))
991 (org-trim rtn)))
993 (defun org-columns-compile-format (fmt)
994 "Turn a column format string into an alist of specifications.
995 The alist has one entry for each column in the format. The elements of
996 that list are:
997 property the property
998 title the title field for the columns
999 width the column width in characters, can be nil for automatic
1000 operator the operator if any
1001 format the output format for computed results, derived from operator
1002 printf a printf format for computed values"
1003 (let ((start 0) width prop title op f printf)
1004 (setq org-columns-current-fmt-compiled nil)
1005 (while (string-match
1006 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
1007 fmt start)
1008 (setq start (match-end 0)
1009 width (match-string 1 fmt)
1010 prop (match-string 2 fmt)
1011 title (or (match-string 3 fmt) prop)
1012 op (match-string 4 fmt)
1013 f nil
1014 printf nil)
1015 (if width (setq width (string-to-number width)))
1016 (when (and op (string-match ";" op))
1017 (setq printf (substring op (match-end 0))
1018 op (substring op 0 (match-beginning 0))))
1019 (cond
1020 ((equal op "+") (setq f 'add_numbers))
1021 ((equal op "$") (setq f 'currency))
1022 ((equal op ":") (setq f 'add_times))
1023 ((equal op "X") (setq f 'checkbox))
1024 ((equal op "X/") (setq f 'checkbox-n-of-m))
1025 ((equal op "X%") (setq f 'checkbox-percent))
1027 (push (list prop title width op f printf) org-columns-current-fmt-compiled))
1028 (setq org-columns-current-fmt-compiled
1029 (nreverse org-columns-current-fmt-compiled))))
1032 ;;; Dynamic block for Column view
1034 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
1035 "Get the column view of the current buffer or subtree.
1036 The first optional argument MAXLEVEL sets the level limit. A
1037 second optional argument SKIP-EMPTY-ROWS tells whether to skip
1038 empty rows, an empty row being one where all the column view
1039 specifiers except ITEM are empty. This function returns a list
1040 containing the title row and all other rows. Each row is a list
1041 of fields."
1042 (save-excursion
1043 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1044 (n (length title)) row tbl)
1045 (goto-char (point-min))
1046 (while (re-search-forward "^\\(\\*+\\) " nil t)
1047 (when (and (or (null maxlevel)
1048 (>= maxlevel
1049 (if org-odd-levels-only
1050 (/ (1+ (length (match-string 1))) 2)
1051 (length (match-string 1)))))
1052 (get-char-property (match-beginning 0) 'org-columns-key))
1053 (setq row nil)
1054 (loop for i from 0 to (1- n) do
1055 (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
1056 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
1058 row))
1059 (setq row (nreverse row))
1060 (unless (and skip-empty-rows
1061 (eq 1 (length (delete "" (delete-dups row)))))
1062 (push row tbl))))
1063 (append (list title 'hline) (nreverse tbl)))))
1065 (defun org-dblock-write:columnview (params)
1066 "Write the column view table.
1067 PARAMS is a property list of parameters:
1069 :width enforce same column widths with <N> specifiers.
1070 :id the :ID: property of the entry where the columns view
1071 should be built. When the symbol `local', call locally.
1072 When `global' call column view with the cursor at the beginning
1073 of the buffer (usually this means that the whole buffer switches
1074 to column view). When \"file:path/to/file.org\", invoke column
1075 view at the start of that file. Otherwise, the ID is located
1076 using `org-id-find'.
1077 :hlines When t, insert a hline before each item. When a number, insert
1078 a hline before each level <= that number.
1079 :vlines When t, make each column a colgroup to enforce vertical lines.
1080 :maxlevel When set to a number, don't capture headlines below this level.
1081 :skip-empty-rows
1082 When t, skip rows where all specifiers other than ITEM are empty."
1083 (let ((pos (move-marker (make-marker) (point)))
1084 (hlines (plist-get params :hlines))
1085 (vlines (plist-get params :vlines))
1086 (maxlevel (plist-get params :maxlevel))
1087 (content-lines (org-split-string (plist-get params :content) "\n"))
1088 (skip-empty-rows (plist-get params :skip-empty-rows))
1089 tbl id idpos nfields tmp recalc line
1090 id-as-string view-file view-pos)
1091 (when (setq id (plist-get params :id))
1092 (setq id-as-string (cond ((numberp id) (number-to-string id))
1093 ((symbolp id) (symbol-name id))
1094 ((stringp id) id)
1095 (t "")))
1096 (cond ((not id) nil)
1097 ((eq id 'global) (setq view-pos (point-min)))
1098 ((eq id 'local))
1099 ((string-match "^file:\\(.*\\)" id-as-string)
1100 (setq view-file (match-string 1 id-as-string)
1101 view-pos 1)
1102 (unless (file-exists-p view-file)
1103 (error "No such file: \"%s\"" id-as-string)))
1104 ((setq idpos (org-find-entry-with-id id))
1105 (setq view-pos idpos))
1106 ((setq idpos (org-id-find id))
1107 (setq view-file (car idpos))
1108 (setq view-pos (cdr idpos)))
1109 (t (error "Cannot find entry with :ID: %s" id))))
1110 (with-current-buffer (if view-file
1111 (get-file-buffer view-file)
1112 (current-buffer))
1113 (save-excursion
1114 (save-restriction
1115 (widen)
1116 (goto-char (or view-pos (point)))
1117 (org-columns)
1118 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1119 (setq nfields (length (car tbl)))
1120 (org-columns-quit))))
1121 (goto-char pos)
1122 (move-marker pos nil)
1123 (when tbl
1124 (when (plist-get params :hlines)
1125 (setq tmp nil)
1126 (while tbl
1127 (if (eq (car tbl) 'hline)
1128 (push (pop tbl) tmp)
1129 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1130 (if (and (not (eq (car tmp) 'hline))
1131 (or (eq hlines t)
1132 (and (numberp hlines)
1133 (<= (- (match-end 1) (match-beginning 1))
1134 hlines))))
1135 (push 'hline tmp)))
1136 (push (pop tbl) tmp)))
1137 (setq tbl (nreverse tmp)))
1138 (when vlines
1139 (setq tbl (mapcar (lambda (x)
1140 (if (eq 'hline x) x (cons "" x)))
1141 tbl))
1142 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1143 (setq pos (point))
1144 (when content-lines
1145 (while (string-match "^#" (car content-lines))
1146 (insert (pop content-lines) "\n")))
1147 (insert (org-listtable-to-string tbl))
1148 (when (plist-get params :width)
1149 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1150 org-columns-current-widths "|")))
1151 (while (setq line (pop content-lines))
1152 (when (string-match "^#" line)
1153 (insert "\n" line)
1154 (when (string-match "^#\\+TBLFM" line)
1155 (setq recalc t))))
1156 (if recalc
1157 (progn (goto-char pos) (org-table-recalculate 'all))
1158 (goto-char pos)
1159 (org-table-align)))))
1161 (defun org-listtable-to-string (tbl)
1162 "Convert a listtable TBL to a string that contains the Org-mode table.
1163 The table still need to be aligned. The resulting string has no leading
1164 and tailing newline characters."
1165 (mapconcat
1166 (lambda (x)
1167 (cond
1168 ((listp x)
1169 (concat "|" (mapconcat 'identity x "|") "|"))
1170 ((eq x 'hline) "|-|")
1171 (t (error "Garbage in listtable: %s" x))))
1172 tbl "\n"))
1174 (defun org-insert-columns-dblock ()
1175 "Create a dynamic block capturing a column view table."
1176 (interactive)
1177 (let ((defaults '(:name "columnview" :hlines 1))
1178 (id (org-ido-completing-read
1179 "Capture columns (local, global, entry with :ID: property) [local]: "
1180 (append '(("global") ("local"))
1181 (mapcar 'list (org-property-values "ID"))))))
1182 (if (equal id "") (setq id 'local))
1183 (if (equal id "global") (setq id 'global))
1184 (setq defaults (append defaults (list :id id)))
1185 (org-create-dblock defaults)
1186 (org-update-dblock)))
1188 ;;; Column view in the agenda
1190 (defvar org-agenda-view-columns-initially nil
1191 "When set, switch to columns view immediately after creating the agenda.")
1193 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1194 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1195 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1197 (defun org-agenda-columns ()
1198 "Turn on or update column view in the agenda."
1199 (interactive)
1200 (org-verify-version 'columns)
1201 (org-columns-remove-overlays)
1202 (move-marker org-columns-begin-marker (point))
1203 (let (fmt cache maxwidths m p a d)
1204 (cond
1205 ((and (boundp 'org-agenda-overriding-columns-format)
1206 org-agenda-overriding-columns-format)
1207 (setq fmt org-agenda-overriding-columns-format)
1208 (org-set-local 'org-agenda-overriding-columns-format fmt))
1209 ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
1210 (setq fmt (or (org-entry-get m "COLUMNS" t)
1211 (with-current-buffer (marker-buffer m)
1212 org-columns-default-format))))
1213 ((and (boundp 'org-columns-current-fmt)
1214 (local-variable-p 'org-columns-current-fmt)
1215 org-columns-current-fmt)
1216 (setq fmt org-columns-current-fmt))
1217 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
1218 (setq m (get-text-property m 'org-hd-marker))
1219 (setq fmt (or (org-entry-get m "COLUMNS" t)
1220 (with-current-buffer (marker-buffer m)
1221 org-columns-default-format)))))
1222 (setq fmt (or fmt org-columns-default-format))
1223 (org-set-local 'org-columns-current-fmt fmt)
1224 (org-columns-compile-format fmt)
1225 (when org-agenda-columns-compute-summary-properties
1226 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1227 (save-excursion
1228 ;; Get and cache the properties
1229 (goto-char (point-min))
1230 (while (not (eobp))
1231 (when (setq m (or (get-text-property (point) 'org-hd-marker)
1232 (get-text-property (point) 'org-marker)))
1233 (setq p (org-entry-properties m))
1235 (when (or (not (setq a (assoc org-effort-property p)))
1236 (not (string-match "\\S-" (or (cdr a) ""))))
1237 ;; OK, the property is not defined. Use appointment duration?
1238 (when (and org-agenda-columns-add-appointments-to-effort-sum
1239 (setq d (get-text-property (point) 'duration)))
1240 (setq d (org-minutes-to-hh:mm-string d))
1241 (put-text-property 0 (length d) 'face 'org-warning d)
1242 (push (cons org-effort-property d) p)))
1243 (push (cons (org-current-line) p) cache))
1244 (beginning-of-line 2))
1245 (when cache
1246 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
1247 (org-set-local 'org-columns-current-maxwidths maxwidths)
1248 (org-columns-display-here-title)
1249 (when (org-set-local 'org-columns-flyspell-was-active
1250 (org-bound-and-true-p flyspell-mode))
1251 (flyspell-mode 0))
1252 (mapc (lambda (x)
1253 (goto-line (car x))
1254 (org-columns-display-here (cdr x)))
1255 cache)
1256 (when org-agenda-columns-show-summaries
1257 (org-agenda-colview-summarize cache))))))
1259 (defun org-agenda-colview-summarize (cache)
1260 "Summarize the summarizable columns in column view in the agenda.
1261 This will add overlays to the date lines, to show the summary for each day."
1262 (let* ((fmt (mapcar (lambda (x)
1263 (list (car x) (if (equal (car x) "CLOCKSUM")
1264 'add_times (nth 4 x))))
1265 org-columns-current-fmt-compiled))
1266 line c c1 stype props lsum entries prop v)
1267 (catch 'exit
1268 (when (delq nil (mapcar 'cadr fmt))
1269 ;; OK, at least one summation column, it makes sense to try this
1270 (goto-char (point-max))
1271 (while t
1272 (when (or (get-text-property (point) 'org-date-line)
1273 (eq (get-text-property (point) 'face)
1274 'org-agenda-structure))
1275 ;; OK, this is a date line that should be used
1276 (setq line (org-current-line))
1277 (setq entries nil c cache cache nil)
1278 (while (setq c1 (pop c))
1279 (if (> (car c1) line)
1280 (push c1 entries)
1281 (push c1 cache)))
1282 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1283 ;; Compute the summaries for the properties we want,
1284 ;; set nil properties for the rest.
1285 (when (setq entries (mapcar 'cdr entries))
1286 (setq props
1287 (mapcar
1288 (lambda (f)
1289 (setq prop (car f) stype (nth 1 f))
1290 (cond
1291 ((equal prop "ITEM")
1292 (cons prop (buffer-substring (point-at-bol)
1293 (point-at-eol))))
1294 ((not stype) (cons prop ""))
1296 ;; do the summary
1297 (setq lsum 0)
1298 (mapc (lambda (x)
1299 (setq v (cdr (assoc prop x)))
1300 (if v (setq lsum (+ lsum
1301 (org-column-string-to-number
1302 v stype)))))
1303 entries)
1304 (setq lsum (org-columns-number-to-string lsum stype))
1305 (put-text-property
1306 0 (length lsum) 'face 'bold lsum)
1307 (cons prop lsum))))
1308 fmt))
1309 (org-columns-display-here props 'dateline)
1310 (org-set-local 'org-agenda-columns-active t)))
1311 (if (bobp) (throw 'exit t))
1312 (beginning-of-line 0))))))
1314 (defun org-agenda-colview-compute (fmt)
1315 "Compute the relevant columns in the contributing source buffers."
1316 (let ((files org-agenda-contributing-files)
1317 (org-columns-begin-marker (make-marker))
1318 (org-columns-top-level-marker (make-marker))
1319 f fm a b)
1320 (while (setq f (pop files))
1321 (setq b (find-buffer-visiting f))
1322 (with-current-buffer (or (buffer-base-buffer b) b)
1323 (save-excursion
1324 (save-restriction
1325 (widen)
1326 (org-unmodified
1327 (remove-text-properties (point-min) (point-max)
1328 '(org-summaries t)))
1329 (goto-char (point-min))
1330 (org-columns-get-format-and-top-level)
1331 (while (setq fm (pop fmt))
1332 (if (equal (car fm) "CLOCKSUM")
1333 (org-clock-sum)
1334 (when (and (nth 4 fm)
1335 (setq a (assoc (car fm)
1336 org-columns-current-fmt-compiled))
1337 (equal (nth 4 a) (nth 4 fm)))
1338 (org-columns-compute (car fm)))))))))))
1340 (provide 'org-colview)
1342 ;; arch-tag: 61f5128d-747c-4983-9479-e3871fa3d73c
1344 ;;; org-colview.el ends here