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