Fix typos
[org-mode.git] / lisp / org-colview.el
blob377343db04556d9e1821740af32a52deb3ca745a
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.26trans
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-ido-completing-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 (goto-line (car x))
698 (org-columns-display-here (cdr x)))
699 cache)))))
701 (defun org-columns-new (&optional prop title width op fmt &rest rest)
702 "Insert a new column, to the left of the current column."
703 (interactive)
704 (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
705 cell)
706 (setq prop (org-ido-completing-read
707 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
708 nil nil prop))
709 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
710 (setq width (read-string "Column width: " (if width (number-to-string width))))
711 (if (string-match "\\S-" width)
712 (setq width (string-to-number width))
713 (setq width nil))
714 (setq fmt (org-ido-completing-read "Summary [none]: "
715 '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
716 nil t))
717 (if (string-match "\\S-" fmt)
718 (setq fmt (intern fmt))
719 (setq fmt nil))
720 (if (eq fmt 'none) (setq fmt nil))
721 (if editp
722 (progn
723 (setcar editp prop)
724 (setcdr editp (list title width nil fmt)))
725 (setq cell (nthcdr (1- (current-column))
726 org-columns-current-fmt-compiled))
727 (setcdr cell (cons (list prop title width nil fmt)
728 (cdr cell))))
729 (org-columns-store-format)
730 (org-columns-redo)))
732 (defun org-columns-delete ()
733 "Delete the column at point from columns view."
734 (interactive)
735 (let* ((n (current-column))
736 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
737 (when (y-or-n-p
738 (format "Are you sure you want to remove column \"%s\"? " title))
739 (setq org-columns-current-fmt-compiled
740 (delq (nth n org-columns-current-fmt-compiled)
741 org-columns-current-fmt-compiled))
742 (org-columns-store-format)
743 (org-columns-redo)
744 (if (>= (current-column) (length org-columns-current-fmt-compiled))
745 (backward-char 1)))))
747 (defun org-columns-edit-attributes ()
748 "Edit the attributes of the current column."
749 (interactive)
750 (let* ((n (current-column))
751 (info (nth n org-columns-current-fmt-compiled)))
752 (apply 'org-columns-new info)))
754 (defun org-columns-widen (arg)
755 "Make the column wider by ARG characters."
756 (interactive "p")
757 (let* ((n (current-column))
758 (entry (nth n org-columns-current-fmt-compiled))
759 (width (or (nth 2 entry)
760 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
761 (setq width (max 1 (+ width arg)))
762 (setcar (nthcdr 2 entry) width)
763 (org-columns-store-format)
764 (org-columns-redo)))
766 (defun org-columns-narrow (arg)
767 "Make the column narrower by ARG characters."
768 (interactive "p")
769 (org-columns-widen (- arg)))
771 (defun org-columns-move-right ()
772 "Swap this column with the one to the right."
773 (interactive)
774 (let* ((n (current-column))
775 (cell (nthcdr n org-columns-current-fmt-compiled))
777 (when (>= n (1- (length org-columns-current-fmt-compiled)))
778 (error "Cannot shift this column further to the right"))
779 (setq e (car cell))
780 (setcar cell (car (cdr cell)))
781 (setcdr cell (cons e (cdr (cdr cell))))
782 (org-columns-store-format)
783 (org-columns-redo)
784 (forward-char 1)))
786 (defun org-columns-move-left ()
787 "Swap this column with the one to the left."
788 (interactive)
789 (let* ((n (current-column)))
790 (when (= n 0)
791 (error "Cannot shift this column further to the left"))
792 (backward-char 1)
793 (org-columns-move-right)
794 (backward-char 1)))
796 (defun org-columns-store-format ()
797 "Store the text version of the current columns format in appropriate place.
798 This is either in the COLUMNS property of the node starting the current column
799 display, or in the #+COLUMNS line of the current buffer."
800 (let (fmt (cnt 0))
801 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
802 (org-set-local 'org-columns-current-fmt fmt)
803 (if (marker-position org-columns-top-level-marker)
804 (save-excursion
805 (goto-char org-columns-top-level-marker)
806 (if (and (org-at-heading-p)
807 (org-entry-get nil "COLUMNS"))
808 (org-entry-put nil "COLUMNS" fmt)
809 (goto-char (point-min))
810 ;; Overwrite all #+COLUMNS lines....
811 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
812 (setq cnt (1+ cnt))
813 (replace-match (concat "#+COLUMNS: " fmt) t t))
814 (unless (> cnt 0)
815 (goto-char (point-min))
816 (or (org-on-heading-p t) (outline-next-heading))
817 (let ((inhibit-read-only t))
818 (insert-before-markers "#+COLUMNS: " fmt "\n")))
819 (org-set-local 'org-columns-default-format fmt))))))
821 (defvar org-agenda-overriding-columns-format nil
822 "When set, overrides any other format definition for the agenda.
823 Don't set this, this is meant for dynamic scoping.")
825 (defun org-columns-get-autowidth-alist (s cache)
826 "Derive the maximum column widths from the format and the cache."
827 (let ((start 0) rtn)
828 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
829 (push (cons (match-string 1 s) 1) rtn)
830 (setq start (match-end 0)))
831 (mapc (lambda (x)
832 (setcdr x (apply 'max
833 (mapcar
834 (lambda (y)
835 (length (or (cdr (assoc (car x) (cdr y))) " ")))
836 cache))))
837 rtn)
838 rtn))
840 (defun org-columns-compute-all ()
841 "Compute all columns that have operators defined."
842 (org-unmodified
843 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
844 (let ((columns org-columns-current-fmt-compiled) col)
845 (while (setq col (pop columns))
846 (when (nth 3 col)
847 (save-excursion
848 (org-columns-compute (car col)))))))
850 (defun org-columns-update (property)
851 "Recompute PROPERTY, and update the columns display for it."
852 (org-columns-compute property)
853 (let (fmt val pos)
854 (save-excursion
855 (mapc (lambda (ov)
856 (when (equal (org-overlay-get ov 'org-columns-key) property)
857 (setq pos (org-overlay-start ov))
858 (goto-char pos)
859 (when (setq val (cdr (assoc property
860 (get-text-property
861 (point-at-bol) 'org-summaries))))
862 (setq fmt (org-overlay-get ov 'org-columns-format))
863 (org-overlay-put ov 'org-columns-value val)
864 (org-overlay-put ov 'display (format fmt val)))))
865 org-columns-overlays))))
867 (defun org-columns-compute (property)
868 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
869 (interactive)
870 (let* ((re (concat "^" outline-regexp))
871 (lmax 30) ; Does anyone use deeper levels???
872 (lsum (make-vector lmax 0))
873 (lflag (make-vector lmax nil))
874 (level 0)
875 (ass (assoc property org-columns-current-fmt-compiled))
876 (format (nth 4 ass))
877 (printf (nth 5 ass))
878 (beg org-columns-top-level-marker)
879 last-level val valflag flag end sumpos sum-alist sum str str1 useval)
880 (save-excursion
881 ;; Find the region to compute
882 (goto-char beg)
883 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
884 (goto-char end)
885 ;; Walk the tree from the back and do the computations
886 (while (re-search-backward re beg t)
887 (setq sumpos (match-beginning 0)
888 last-level level
889 level (org-outline-level)
890 val (org-entry-get nil property)
891 valflag (and val (string-match "\\S-" val)))
892 (cond
893 ((< level last-level)
894 ;; put the sum of lower levels here as a property
895 (setq sum (aref lsum last-level) ; current sum
896 flag (aref lflag last-level) ; any valid entries from children?
897 str (org-columns-number-to-string sum format printf)
898 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
899 useval (if flag str1 (if valflag val ""))
900 sum-alist (get-text-property sumpos 'org-summaries))
901 (if (assoc property sum-alist)
902 (setcdr (assoc property sum-alist) useval)
903 (push (cons property useval) sum-alist)
904 (org-unmodified
905 (add-text-properties sumpos (1+ sumpos)
906 (list 'org-summaries sum-alist))))
907 (when (and val (not (equal val (if flag str val))))
908 (org-entry-put nil property (if flag str val)))
909 ;; add current to current level accumulator
910 (when (or flag valflag)
911 (aset lsum level (+ (aref lsum level)
912 (if flag sum (org-column-string-to-number
913 (if flag str val) format))))
914 (aset lflag level t))
915 ;; clear accumulators for deeper levels
916 (loop for l from (1+ level) to (1- lmax) do
917 (aset lsum l 0)
918 (aset lflag l nil)))
919 ((>= level last-level)
920 ;; add what we have here to the accumulator for this level
921 (aset lsum level (+ (aref lsum level)
922 (org-column-string-to-number (or val "0") format)))
923 (and valflag (aset lflag level t)))
924 (t (error "This should not happen")))))))
926 (defun org-columns-redo ()
927 "Construct the column display again."
928 (interactive)
929 (message "Recomputing columns...")
930 (let ((line (org-current-line))
931 (col (current-column)))
932 (save-excursion
933 (if (marker-position org-columns-begin-marker)
934 (goto-char org-columns-begin-marker))
935 (org-columns-remove-overlays)
936 (if (org-mode-p)
937 (call-interactively 'org-columns)
938 (org-agenda-redo)
939 (call-interactively 'org-agenda-columns)))
940 (goto-line line)
941 (move-to-column col))
942 (message "Recomputing columns...done"))
944 (defun org-columns-not-in-agenda ()
945 (if (eq major-mode 'org-agenda-mode)
946 (error "This command is only allowed in Org-mode buffers")))
949 (defun org-string-to-number (s)
950 "Convert string to number, and interpret hh:mm:ss."
951 (if (not (string-match ":" s))
952 (string-to-number s)
953 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
954 (while l
955 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
956 sum)))
958 (defun org-columns-number-to-string (n fmt &optional printf)
959 "Convert a computed column number to a string value, according to FMT."
960 (cond
961 ((eq fmt 'add_times)
962 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
963 (format org-time-clocksum-format h m)))
964 ((eq fmt 'checkbox)
965 (cond ((= n (floor n)) "[X]")
966 ((> n 1.) "[-]")
967 (t "[ ]")))
968 ((memq fmt '(checkbox-n-of-m checkbox-percent))
969 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
970 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
971 (printf (format printf n))
972 ((eq fmt 'currency)
973 (format "%.2f" n))
974 (t (number-to-string n))))
976 (defun org-nofm-to-completion (n m &optional percent)
977 (if (not percent)
978 (format "[%d/%d]" n m)
979 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
981 (defun org-column-string-to-number (s fmt)
982 "Convert a column value to a number that can be used for column computing."
983 (cond
984 ((string-match ":" s)
985 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
986 (while l
987 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
988 sum))
989 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
990 (if (equal s "[X]") 1. 0.000001))
991 (t (string-to-number s))))
993 (defun org-columns-uncompile-format (cfmt)
994 "Turn the compiled columns format back into a string representation."
995 (let ((rtn "") e s prop title op width fmt printf)
996 (while (setq e (pop cfmt))
997 (setq prop (car e)
998 title (nth 1 e)
999 width (nth 2 e)
1000 op (nth 3 e)
1001 fmt (nth 4 e)
1002 printf (nth 5 e))
1003 (cond
1004 ((eq fmt 'add_times) (setq op ":"))
1005 ((eq fmt 'checkbox) (setq op "X"))
1006 ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
1007 ((eq fmt 'checkbox-percent) (setq op "X%"))
1008 ((eq fmt 'add_numbers) (setq op "+"))
1009 ((eq fmt 'currency) (setq op "$")))
1010 (if (and op printf) (setq op (concat op ";" printf)))
1011 (if (equal title prop) (setq title nil))
1012 (setq s (concat "%" (if width (number-to-string width))
1013 prop
1014 (if title (concat "(" title ")"))
1015 (if op (concat "{" op "}"))))
1016 (setq rtn (concat rtn " " s)))
1017 (org-trim rtn)))
1019 (defun org-columns-compile-format (fmt)
1020 "Turn a column format string into an alist of specifications.
1021 The alist has one entry for each column in the format. The elements of
1022 that list are:
1023 property the property
1024 title the title field for the columns
1025 width the column width in characters, can be nil for automatic
1026 operator the operator if any
1027 format the output format for computed results, derived from operator
1028 printf a printf format for computed values"
1029 (let ((start 0) width prop title op f printf)
1030 (setq org-columns-current-fmt-compiled nil)
1031 (while (string-match
1032 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
1033 fmt start)
1034 (setq start (match-end 0)
1035 width (match-string 1 fmt)
1036 prop (match-string 2 fmt)
1037 title (or (match-string 3 fmt) prop)
1038 op (match-string 4 fmt)
1039 f nil
1040 printf nil)
1041 (if width (setq width (string-to-number width)))
1042 (when (and op (string-match ";" op))
1043 (setq printf (substring op (match-end 0))
1044 op (substring op 0 (match-beginning 0))))
1045 (cond
1046 ((equal op "+") (setq f 'add_numbers))
1047 ((equal op "$") (setq f 'currency))
1048 ((equal op ":") (setq f 'add_times))
1049 ((equal op "X") (setq f 'checkbox))
1050 ((equal op "X/") (setq f 'checkbox-n-of-m))
1051 ((equal op "X%") (setq f 'checkbox-percent))
1053 (push (list prop title width op f printf) org-columns-current-fmt-compiled))
1054 (setq org-columns-current-fmt-compiled
1055 (nreverse org-columns-current-fmt-compiled))))
1058 ;;; Dynamic block for Column view
1060 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
1061 "Get the column view of the current buffer or subtree.
1062 The first optional argument MAXLEVEL sets the level limit. A
1063 second optional argument SKIP-EMPTY-ROWS tells whether to skip
1064 empty rows, an empty row being one where all the column view
1065 specifiers except ITEM are empty. This function returns a list
1066 containing the title row and all other rows. Each row is a list
1067 of fields."
1068 (save-excursion
1069 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1070 (n (length title)) row tbl)
1071 (goto-char (point-min))
1072 (while (re-search-forward "^\\(\\*+\\) " nil t)
1073 (when (and (or (null maxlevel)
1074 (>= maxlevel
1075 (if org-odd-levels-only
1076 (/ (1+ (length (match-string 1))) 2)
1077 (length (match-string 1)))))
1078 (get-char-property (match-beginning 0) 'org-columns-key))
1079 (setq row nil)
1080 (loop for i from 0 to (1- n) do
1081 (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
1082 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
1084 row))
1085 (setq row (nreverse row))
1086 (unless (and skip-empty-rows
1087 (eq 1 (length (delete "" (delete-dups (copy-sequence row))))))
1088 (push row tbl))))
1089 (append (list title 'hline) (nreverse tbl)))))
1091 (defun org-dblock-write:columnview (params)
1092 "Write the column view table.
1093 PARAMS is a property list of parameters:
1095 :width enforce same column widths with <N> specifiers.
1096 :id the :ID: property of the entry where the columns view
1097 should be built. When the symbol `local', call locally.
1098 When `global' call column view with the cursor at the beginning
1099 of the buffer (usually this means that the whole buffer switches
1100 to column view). When \"file:path/to/file.org\", invoke column
1101 view at the start of that file. Otherwise, the ID is located
1102 using `org-id-find'.
1103 :hlines When t, insert a hline before each item. When a number, insert
1104 a hline before each level <= that number.
1105 :vlines When t, make each column a colgroup to enforce vertical lines.
1106 :maxlevel When set to a number, don't capture headlines below this level.
1107 :skip-empty-rows
1108 When t, skip rows where all specifiers other than ITEM are empty."
1109 (let ((pos (move-marker (make-marker) (point)))
1110 (hlines (plist-get params :hlines))
1111 (vlines (plist-get params :vlines))
1112 (maxlevel (plist-get params :maxlevel))
1113 (content-lines (org-split-string (plist-get params :content) "\n"))
1114 (skip-empty-rows (plist-get params :skip-empty-rows))
1115 tbl id idpos nfields tmp recalc line
1116 id-as-string view-file view-pos)
1117 (when (setq id (plist-get params :id))
1118 (setq id-as-string (cond ((numberp id) (number-to-string id))
1119 ((symbolp id) (symbol-name id))
1120 ((stringp id) id)
1121 (t "")))
1122 (cond ((not id) nil)
1123 ((eq id 'global) (setq view-pos (point-min)))
1124 ((eq id 'local))
1125 ((string-match "^file:\\(.*\\)" id-as-string)
1126 (setq view-file (match-string 1 id-as-string)
1127 view-pos 1)
1128 (unless (file-exists-p view-file)
1129 (error "No such file: \"%s\"" id-as-string)))
1130 ((setq idpos (org-find-entry-with-id id))
1131 (setq view-pos idpos))
1132 ((setq idpos (org-id-find id))
1133 (setq view-file (car idpos))
1134 (setq view-pos (cdr idpos)))
1135 (t (error "Cannot find entry with :ID: %s" id))))
1136 (with-current-buffer (if view-file
1137 (get-file-buffer view-file)
1138 (current-buffer))
1139 (save-excursion
1140 (save-restriction
1141 (widen)
1142 (goto-char (or view-pos (point)))
1143 (org-columns)
1144 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1145 (setq nfields (length (car tbl)))
1146 (org-columns-quit))))
1147 (goto-char pos)
1148 (move-marker pos nil)
1149 (when tbl
1150 (when (plist-get params :hlines)
1151 (setq tmp nil)
1152 (while tbl
1153 (if (eq (car tbl) 'hline)
1154 (push (pop tbl) tmp)
1155 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1156 (if (and (not (eq (car tmp) 'hline))
1157 (or (eq hlines t)
1158 (and (numberp hlines)
1159 (<= (- (match-end 1) (match-beginning 1))
1160 hlines))))
1161 (push 'hline tmp)))
1162 (push (pop tbl) tmp)))
1163 (setq tbl (nreverse tmp)))
1164 (when vlines
1165 (setq tbl (mapcar (lambda (x)
1166 (if (eq 'hline x) x (cons "" x)))
1167 tbl))
1168 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1169 (setq pos (point))
1170 (when content-lines
1171 (while (string-match "^#" (car content-lines))
1172 (insert (pop content-lines) "\n")))
1173 (insert (org-listtable-to-string tbl))
1174 (when (plist-get params :width)
1175 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1176 org-columns-current-widths "|")))
1177 (while (setq line (pop content-lines))
1178 (when (string-match "^#" line)
1179 (insert "\n" line)
1180 (when (string-match "^#\\+TBLFM" line)
1181 (setq recalc t))))
1182 (if recalc
1183 (progn (goto-char pos) (org-table-recalculate 'all))
1184 (goto-char pos)
1185 (org-table-align)))))
1187 (defun org-listtable-to-string (tbl)
1188 "Convert a listtable TBL to a string that contains the Org-mode table.
1189 The table still need to be aligned. The resulting string has no leading
1190 and tailing newline characters."
1191 (mapconcat
1192 (lambda (x)
1193 (cond
1194 ((listp x)
1195 (concat "|" (mapconcat 'identity x "|") "|"))
1196 ((eq x 'hline) "|-|")
1197 (t (error "Garbage in listtable: %s" x))))
1198 tbl "\n"))
1200 (defun org-insert-columns-dblock ()
1201 "Create a dynamic block capturing a column view table."
1202 (interactive)
1203 (let ((defaults '(:name "columnview" :hlines 1))
1204 (id (org-ido-completing-read
1205 "Capture columns (local, global, entry with :ID: property) [local]: "
1206 (append '(("global") ("local"))
1207 (mapcar 'list (org-property-values "ID"))))))
1208 (if (equal id "") (setq id 'local))
1209 (if (equal id "global") (setq id 'global))
1210 (setq defaults (append defaults (list :id id)))
1211 (org-create-dblock defaults)
1212 (org-update-dblock)))
1214 ;;; Column view in the agenda
1216 (defvar org-agenda-view-columns-initially nil
1217 "When set, switch to columns view immediately after creating the agenda.")
1219 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1220 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1221 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1223 (defun org-agenda-columns ()
1224 "Turn on or update column view in the agenda."
1225 (interactive)
1226 (org-verify-version 'columns)
1227 (org-columns-remove-overlays)
1228 (move-marker org-columns-begin-marker (point))
1229 (let (fmt cache maxwidths m p a d)
1230 (cond
1231 ((and (boundp 'org-agenda-overriding-columns-format)
1232 org-agenda-overriding-columns-format)
1233 (setq fmt org-agenda-overriding-columns-format)
1234 (org-set-local 'org-agenda-overriding-columns-format fmt))
1235 ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
1236 (setq fmt (or (org-entry-get m "COLUMNS" t)
1237 (with-current-buffer (marker-buffer m)
1238 org-columns-default-format))))
1239 ((and (boundp 'org-columns-current-fmt)
1240 (local-variable-p 'org-columns-current-fmt)
1241 org-columns-current-fmt)
1242 (setq fmt org-columns-current-fmt))
1243 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
1244 (setq m (get-text-property m 'org-hd-marker))
1245 (setq fmt (or (org-entry-get m "COLUMNS" t)
1246 (with-current-buffer (marker-buffer m)
1247 org-columns-default-format)))))
1248 (setq fmt (or fmt org-columns-default-format))
1249 (org-set-local 'org-columns-current-fmt fmt)
1250 (org-columns-compile-format fmt)
1251 (when org-agenda-columns-compute-summary-properties
1252 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1253 (save-excursion
1254 ;; Get and cache the properties
1255 (goto-char (point-min))
1256 (while (not (eobp))
1257 (when (setq m (or (get-text-property (point) 'org-hd-marker)
1258 (get-text-property (point) 'org-marker)))
1259 (setq p (org-entry-properties m))
1261 (when (or (not (setq a (assoc org-effort-property p)))
1262 (not (string-match "\\S-" (or (cdr a) ""))))
1263 ;; OK, the property is not defined. Use appointment duration?
1264 (when (and org-agenda-columns-add-appointments-to-effort-sum
1265 (setq d (get-text-property (point) 'duration)))
1266 (setq d (org-minutes-to-hh:mm-string d))
1267 (put-text-property 0 (length d) 'face 'org-warning d)
1268 (push (cons org-effort-property d) p)))
1269 (push (cons (org-current-line) p) cache))
1270 (beginning-of-line 2))
1271 (when cache
1272 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
1273 (org-set-local 'org-columns-current-maxwidths maxwidths)
1274 (org-columns-display-here-title)
1275 (when (org-set-local 'org-columns-flyspell-was-active
1276 (org-bound-and-true-p flyspell-mode))
1277 (flyspell-mode 0))
1278 (mapc (lambda (x)
1279 (goto-line (car x))
1280 (org-columns-display-here (cdr x)))
1281 cache)
1282 (when org-agenda-columns-show-summaries
1283 (org-agenda-colview-summarize cache))))))
1285 (defun org-agenda-colview-summarize (cache)
1286 "Summarize the summarizable columns in column view in the agenda.
1287 This will add overlays to the date lines, to show the summary for each day."
1288 (let* ((fmt (mapcar (lambda (x)
1289 (list (car x) (if (equal (car x) "CLOCKSUM")
1290 'add_times (nth 4 x))))
1291 org-columns-current-fmt-compiled))
1292 line c c1 stype props lsum entries prop v)
1293 (catch 'exit
1294 (when (delq nil (mapcar 'cadr fmt))
1295 ;; OK, at least one summation column, it makes sense to try this
1296 (goto-char (point-max))
1297 (while t
1298 (when (or (get-text-property (point) 'org-date-line)
1299 (eq (get-text-property (point) 'face)
1300 'org-agenda-structure))
1301 ;; OK, this is a date line that should be used
1302 (setq line (org-current-line))
1303 (setq entries nil c cache cache nil)
1304 (while (setq c1 (pop c))
1305 (if (> (car c1) line)
1306 (push c1 entries)
1307 (push c1 cache)))
1308 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1309 ;; Compute the summaries for the properties we want,
1310 ;; set nil properties for the rest.
1311 (when (setq entries (mapcar 'cdr entries))
1312 (setq props
1313 (mapcar
1314 (lambda (f)
1315 (setq prop (car f) stype (nth 1 f))
1316 (cond
1317 ((equal prop "ITEM")
1318 (cons prop (buffer-substring (point-at-bol)
1319 (point-at-eol))))
1320 ((not stype) (cons prop ""))
1322 ;; do the summary
1323 (setq lsum 0)
1324 (mapc (lambda (x)
1325 (setq v (cdr (assoc prop x)))
1326 (if v (setq lsum (+ lsum
1327 (org-column-string-to-number
1328 v stype)))))
1329 entries)
1330 (setq lsum (org-columns-number-to-string lsum stype))
1331 (put-text-property
1332 0 (length lsum) 'face 'bold lsum)
1333 (cons prop lsum))))
1334 fmt))
1335 (org-columns-display-here props 'dateline)
1336 (org-set-local 'org-agenda-columns-active t)))
1337 (if (bobp) (throw 'exit t))
1338 (beginning-of-line 0))))))
1340 (defun org-agenda-colview-compute (fmt)
1341 "Compute the relevant columns in the contributing source buffers."
1342 (let ((files org-agenda-contributing-files)
1343 (org-columns-begin-marker (make-marker))
1344 (org-columns-top-level-marker (make-marker))
1345 f fm a b)
1346 (while (setq f (pop files))
1347 (setq b (find-buffer-visiting f))
1348 (with-current-buffer (or (buffer-base-buffer b) b)
1349 (save-excursion
1350 (save-restriction
1351 (widen)
1352 (org-unmodified
1353 (remove-text-properties (point-min) (point-max)
1354 '(org-summaries t)))
1355 (goto-char (point-min))
1356 (org-columns-get-format-and-top-level)
1357 (while (setq fm (pop fmt))
1358 (if (equal (car fm) "CLOCKSUM")
1359 (org-clock-sum)
1360 (when (and (nth 4 fm)
1361 (setq a (assoc (car fm)
1362 org-columns-current-fmt-compiled))
1363 (equal (nth 4 a) (nth 4 fm)))
1364 (org-columns-compute (car fm)))))))))))
1366 (provide 'org-colview)
1368 ;; arch-tag: 61f5128d-747c-4983-9479-e3871fa3d73c
1370 ;;; org-colview.el ends here