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