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