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