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