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