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