Release 7.3
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-colview-xemacs.el
blob29cb30cffe3bc6748cf0f9e058ceea5ecfb8fe79
1 ;;; org-colview-xemacs.el --- Column View in Org-mode, XEmacs-specific version
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 7.3
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;;; Commentary:
31 ;; This file contains the column view for Org.
33 ;;; Code:
35 (eval-when-compile (require 'cl))
36 (require 'org)
38 (declare-function org-agenda-redo "org-agenda" ())
41 ;;; Define additional faces for column view
43 (when (featurep 'xemacs)
45 (defface org-columns-level-1;; font-lock-function-name-face
46 (org-compatible-face
47 'outline-1
48 '((((class color) (min-colors 88) (background light)) (:foreground "Blue1" :background "grey90"))
49 (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue" :background "grey30"))
50 (((class color) (min-colors 16) (background light)) (:foreground "Blue" :background "grey90"))
51 (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue" :background "grey30"))
52 (((class color) (min-colors 8)) (:foreground "blue" :bold t))
53 (t (:bold t))))
54 "Face used for columns-level 1 headlines."
55 :group 'org-faces)
57 (defface org-columns-level-2;; font-lock-variable-name-face
58 (org-compatible-face
59 'outline-2
60 '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod" :background "grey90"))
61 (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod" :background "grey30"))
62 (((class color) (min-colors 8) (background light)) (:foreground "yellow" :background "grey90"))
63 (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
64 (t (:bold t))))
65 "Face used for columns-level 2 headlines."
66 :group 'org-faces)
68 (defface org-columns-level-3;; font-lock-keyword-face
69 (org-compatible-face
70 'outline-3
71 '((((class color) (min-colors 88) (background light)) (:foreground "Purple" :background "grey90"))
72 (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1" :background "grey30"))
73 (((class color) (min-colors 16) (background light)) (:foreground "Purple" :background "grey90"))
74 (((class color) (min-colors 16) (background dark)) (:foreground "Cyan" :background "grey30"))
75 (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
76 (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
77 (t (:bold t))))
78 "Face used for columns-level 3 headlines."
79 :group 'org-faces)
81 (defface org-columns-level-4;; font-lock-comment-face
82 (org-compatible-face
83 'outline-4
84 '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick" :background "grey90"))
85 (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1" :background "grey30"))
86 (((class color) (min-colors 16) (background light)) (:foreground "red"))
87 (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
88 (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
89 (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
90 (t (:bold t))))
91 "Face used for columns-level 4 headlines."
92 :group 'org-faces)
94 (defface org-columns-level-5;; font-lock-type-face
95 (org-compatible-face
96 'outline-5
97 '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :background "grey90"))
98 (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :background "grey30"))
99 (((class color) (min-colors 8)) (:foreground "green"))))
100 "Face used for columns-level 5 headlines."
101 :group 'org-faces)
103 (defface org-columns-level-6;; font-lock-constant-face
104 (org-compatible-face
105 'outline-6
106 '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue" :background "grey90"))
107 (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine" :background "grey30"))
108 (((class color) (min-colors 8)) (:foreground "magenta"))))
109 "Face used for columns-level 6 headlines."
110 :group 'org-faces)
112 (defface org-columns-level-7;; font-lock-builtin-face
113 (org-compatible-face
114 'outline-7
115 '((((class color) (min-colors 16) (background light)) (:foreground "Orchid" :background "grey90"))
116 (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue" :background "grey30"))
117 (((class color) (min-colors 8)) (:foreground "blue"))))
118 "Face used for columns-level 7 headlines."
119 :group 'org-faces)
121 (defface org-columns-level-8;; font-lock-string-face
122 (org-compatible-face
123 'outline-8
124 '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown" :background "grey90"))
125 (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon" :background "grey30"))
126 (((class color) (min-colors 8)) (:foreground "green"))))
127 "Face used for columns-level 8 headlines."
128 :group 'org-faces)
131 (defface org-columns-space;; font-lock-function-name-face
132 (org-compatible-face
133 'outline-1
134 '((((class color) (min-colors 88) (background light)) (:background "grey90"))
135 (((class color) (min-colors 88) (background dark)) (:background "grey30"))
136 (((class color) (min-colors 16) (background light)) (:background "grey90"))
137 (((class color) (min-colors 16) (background dark)) (:background "grey30"))
138 (((class color) (min-colors 8)) (:bold t :underline t))))
139 "Face used for columns space headlines."
140 :group 'org-faces)
142 (defface org-columns-space1;; font-lock-function-name-face
143 (org-compatible-face
144 'outline-1
145 '((((class color) (min-colors 88) (background light)) (:background "grey90"))
146 (((class color) (min-colors 88) (background dark)) (:background "grey30"))
147 (((class color) (min-colors 16) (background light)) (:background "grey90"))
148 (((class color) (min-colors 16) (background dark)) (:background "grey30"))
149 (((class color) (min-colors 8)) (:bold t :underline t))))
150 "Face used for columns space headlines."
151 :group 'org-faces)
154 (when (featurep 'xemacs)
155 (defconst org-columns-level-faces
156 '(org-columns-level-1
157 org-columns-level-2 org-columns-level-3
158 org-columns-level-4 org-columns-level-5 org-columns-level-6
159 org-columns-level-7 org-columns-level-8
162 (defun org-get-columns-level-face (n)
163 "Get the right face for match N in font-lock matching of headlines."
164 (setq org-l (- (match-end 2) (match-beginning 1) 1))
165 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
166 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-columns-level-faces))
167 (cond
168 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
169 ((eq n 2) org-f)
170 (t (if org-level-color-stars-only nil org-f))))
174 ;;; Column View
176 (defvar org-columns-overlays nil
177 "Holds the list of current column overlays.")
179 (defvar org-columns-current-fmt nil
180 "Local variable, holds the currently active column format.")
181 (make-variable-buffer-local 'org-columns-current-fmt)
182 (defvar org-columns-current-fmt-compiled nil
183 "Local variable, holds the currently active column format.
184 This is the compiled version of the format.")
185 (make-variable-buffer-local 'org-columns-current-fmt-compiled)
186 (defvar org-columns-current-widths nil
187 "Local variable, holds the currently widths of fields.")
188 (make-variable-buffer-local 'org-columns-current-widths)
189 (defvar org-columns-current-maxwidths nil
190 "Local variable, holds the currently active maximum column widths.")
191 (make-variable-buffer-local 'org-columns-current-maxwidths)
192 (defvar org-columns-begin-marker (make-marker)
193 "Points to the position where last a column creation command was called.")
194 (defvar org-columns-top-level-marker (make-marker)
195 "Points to the position where current columns region starts.")
197 (defvar org-columns-map (make-sparse-keymap)
198 "The keymap valid in column display.")
200 (defun org-columns-content ()
201 "Switch to contents view while in columns view."
202 (interactive)
203 (org-overview)
204 (org-content))
206 (org-defkey org-columns-map "c" 'org-columns-content)
207 (org-defkey org-columns-map "o" 'org-overview)
208 (org-defkey org-columns-map "e" 'org-columns-edit-value)
209 (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
210 (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
211 (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
212 (org-defkey org-columns-map "v" 'org-columns-show-value)
213 (org-defkey org-columns-map "q" 'org-columns-quit)
214 (org-defkey org-columns-map "r" 'org-columns-redo)
215 (org-defkey org-columns-map "g" 'org-columns-redo)
216 (org-defkey org-columns-map [left] 'org-columns-backward-char)
217 (org-defkey org-columns-map "\M-b" 'org-columns-backward-char)
218 (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
219 (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
220 (org-defkey org-columns-map "\M-f" 'org-columns-forward-char)
221 (org-defkey org-columns-map [right] 'org-columns-forward-char)
222 (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
223 (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
224 (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
225 (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
226 (org-defkey org-columns-map "<" 'org-columns-narrow)
227 (org-defkey org-columns-map ">" 'org-columns-widen)
228 (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
229 (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
230 (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
231 (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
232 (dotimes (i 10)
233 (org-defkey org-columns-map (number-to-string i)
234 `(lambda () (interactive)
235 (org-columns-next-allowed-value nil ,i))))
237 (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
238 '("Column"
239 ["Edit property" org-columns-edit-value t]
240 ["Next allowed value" org-columns-next-allowed-value t]
241 ["Previous allowed value" org-columns-previous-allowed-value t]
242 ["Show full value" org-columns-show-value t]
243 ["Edit allowed values" org-columns-edit-allowed t]
244 "--"
245 ["Edit column attributes" org-columns-edit-attributes t]
246 ["Increase column width" org-columns-widen t]
247 ["Decrease column width" org-columns-narrow t]
248 "--"
249 ["Move column right" org-columns-move-right t]
250 ["Move column left" org-columns-move-left t]
251 ["Add column" org-columns-new t]
252 ["Delete column" org-columns-delete t]
253 "--"
254 ["CONTENTS" org-columns-content t]
255 ["OVERVIEW" org-overview t]
256 ["Refresh columns display" org-columns-redo t]
257 "--"
258 ["Open link" org-columns-open-link t]
259 "--"
260 ["Quit" org-columns-quit t]))
262 (defun org-columns-current-column ()
263 (if (featurep 'xemacs)
264 (/ (current-column) 2)
265 (current-column)))
267 (defun org-columns-forward-char ()
268 (interactive)
269 (forward-char)
270 (if (featurep 'xemacs)
271 (while (not (or (eolp)
272 (member (extent-at
273 (point) (current-buffer)
274 'org-columns-key) org-columns-overlays)))
275 (forward-char))))
277 (defun org-columns-backward-char ()
278 (interactive)
279 (backward-char)
280 (if (featurep 'xemacs)
281 (while (not (or (bolp)
282 (member (extent-at (point) (current-buffer) 'org-columns-key) org-columns-overlays)))
283 (backward-char))))
285 (defun org-columns-new-overlay (beg end &optional string face)
286 "Create a new column overlay and add it to the list."
287 (let ((ov (make-overlay beg end)))
288 (if (featurep 'xemacs)
289 (progn
290 (overlay-put ov 'face (or face 'org-columns-space1))
291 (overlay-put ov 'start-open t)
292 (if string
293 (org-overlay-display ov string (or face 'org-columns-space1))))
294 (overlay-put ov 'face (or face 'secondary-selection))
295 (org-overlay-display ov string face))
296 (push ov org-columns-overlays)
297 ov))
299 (defun org-columns-display-here (&optional props)
300 "Overlay the current line with column display."
301 (interactive)
302 (let* ((fmt org-columns-current-fmt-compiled)
303 (beg (point-at-bol))
304 (level-face (save-excursion
305 (beginning-of-line 1)
306 (and (looking-at "\\(\\**\\)\\(\\* \\)")
307 (org-get-level-face 2))))
308 (item (save-match-data
309 (org-no-properties
310 (org-remove-tabs
311 (buffer-substring-no-properties
312 (point-at-bol) (point-at-eol))))))
313 (color (if (featurep 'xemacs)
314 (save-excursion
315 (beginning-of-line 1)
316 (and (looking-at "\\(\\**\\)\\(\\* \\)")
317 (org-get-columns-level-face 2)))
318 (list :foreground
319 (face-attribute
320 (or level-face
321 (and (eq major-mode 'org-agenda-mode)
322 (get-text-property (point-at-bol) 'face))
323 'default) :foreground))))
324 (face (if (featurep 'xemacs) color (list color 'org-column)))
325 (pl (or (get-text-property (point-at-bol) 'prefix-length) 0))
326 (cphr (get-text-property (point-at-bol) 'org-complex-heading-regexp))
327 pom property ass width f string ov column val modval s2 title calc)
328 ;; Check if the entry is in another buffer.
329 (unless props
330 (if (eq major-mode 'org-agenda-mode)
331 (setq pom (or (org-get-at-bol 'org-hd-marker)
332 (org-get-at-bol 'org-marker))
333 props (if pom (org-entry-properties pom) nil))
334 (setq props (org-entry-properties nil))))
335 ;; Walk the format
336 (while (setq column (pop fmt))
337 (setq property (car column)
338 title (nth 1 column)
339 ass (if (equal property "ITEM")
340 (cons "ITEM" item)
341 (assoc property props))
342 width (or (cdr (assoc property org-columns-current-maxwidths))
343 (nth 2 column)
344 (length property))
345 f (format (if (featurep 'xemacs) "%%-%d.%ds |" "%%-%d.%ds | ")
346 width width)
347 val (or (cdr ass) "")
348 calc (nth 7 column)
349 modval (cond ((and org-columns-modify-value-for-display-function
350 (functionp
351 org-columns-modify-value-for-display-function))
352 (funcall org-columns-modify-value-for-display-function
353 title val))
354 ((equal property "ITEM")
355 (if (org-mode-p)
356 (org-columns-cleanup-item
357 val org-columns-current-fmt-compiled)
358 (org-agenda-columns-cleanup-item
359 val pl cphr org-columns-current-fmt-compiled)))
360 ((and calc (functionp calc)
361 (not (string= val ""))
362 (not (get-text-property 0 'org-computed val)))
363 (org-columns-number-to-string
364 (funcall calc (org-columns-string-to-number
365 val (nth 4 column)))
366 (nth 4 column)))))
367 (setq s2 (org-columns-add-ellipses (or modval val) width))
368 (setq string (format f s2))
369 ;; Create the overlay
370 (org-unmodified
371 (setq ov (org-columns-new-overlay
372 beg (setq beg (1+ beg)) string face))
373 (overlay-put ov 'keymap org-columns-map)
374 (overlay-put ov 'org-columns-key property)
375 (overlay-put ov 'org-columns-value (cdr ass))
376 (overlay-put ov 'org-columns-value-modified modval)
377 (overlay-put ov 'org-columns-pom pom)
378 (overlay-put ov 'org-columns-format f)
379 (when (featurep 'xemacs)
380 (if (or (not (char-after beg))
381 (equal (char-after beg) ?\n))
382 (let ((inhibit-read-only t))
383 (save-excursion
384 (goto-char beg)
385 (org-unmodified (insert " "))
386 ;; FIXME: add props and remove later?
388 (goto-char beg)
389 (org-columns-new-overlay
390 beg (1+ beg) nil 'org-columns-space)
391 (setq beg (1+ beg))))
393 (if (or (not (char-after beg))
394 (equal (char-after beg) ?\n))
395 (let ((inhibit-read-only t))
396 (save-excursion
397 (goto-char beg)
398 ;; FIXME: add props and remove later?
399 (org-unmodified (insert " "))))))
400 ;; Make the rest of the line disappear.
401 (org-unmodified
402 (setq ov (org-columns-new-overlay beg (point-at-eol)))
403 (overlay-put ov 'invisible t)
404 (overlay-put ov 'keymap org-columns-map)
405 (overlay-put ov 'intangible t)
406 (push ov org-columns-overlays)
407 (setq ov (make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
408 (overlay-put ov 'keymap org-columns-map)
409 (push ov org-columns-overlays)
410 (let ((inhibit-read-only t))
411 (put-text-property (max (point-min) (1- (point-at-bol)))
412 (min (point-max) (1+ (point-at-eol)))
413 'read-only "Type `e' to edit property")))))
415 (defun org-columns-add-ellipses (string width)
416 "Truncate STRING with WIDTH characters, with ellipses."
417 (cond
418 ((<= (length string) width) string)
419 ((<= width (length org-columns-ellipses))
420 (substring org-columns-ellipses 0 width))
421 (t (concat (substring string 0 (- width (length org-columns-ellipses)))
422 org-columns-ellipses))))
424 (defvar org-columns-full-header-line-format nil
425 "The full header line format, will be shifted by horizontal scrolling." )
426 (defvar org-previous-header-line-format nil
427 "The header line format before column view was turned on.")
428 (defvar org-columns-inhibit-recalculation nil
429 "Inhibit recomputing of columns on column view startup.")
432 (defvar header-line-format)
433 (defvar org-columns-previous-hscroll 0)
435 (defun org-columns-display-here-title ()
436 "Overlay the newline before the current line with the table title."
437 (interactive)
438 (let ((fmt org-columns-current-fmt-compiled)
439 string (title "")
440 property width f column str widths)
441 (while (setq column (pop fmt))
442 (setq property (car column)
443 str (or (nth 1 column) property)
444 width (or (cdr (assoc property org-columns-current-maxwidths))
445 (nth 2 column)
446 (length str))
447 widths (push width widths)
448 f (format "%%-%d.%ds | " width width)
449 string (format f str)
450 title (concat title string)))
451 (if (featurep 'xemacs)
452 (let ((ext (make-extent nil nil)))
453 (set-extent-endpoints ext 0 (length title) title)
454 (set-extent-face ext (list 'bold 'underline 'org-columns-space1))
455 (org-set-local 'org-previous-header-line-format
456 (specifier-specs top-gutter))
457 (org-set-local 'org-columns-current-widths (nreverse widths))
458 (set-specifier top-gutter (make-gutter-specifier
459 (cons (current-buffer) title))))
460 (setq title (concat
461 (org-add-props " " nil 'display '(space :align-to 0))
462 (org-add-props title nil 'face '(:weight bold :underline t))))
463 (org-set-local 'org-previous-header-line-format header-line-format)
464 (org-set-local 'org-columns-current-widths (nreverse widths))
465 (setq org-columns-full-header-line-format title)
466 (setq org-columns-previous-hscroll -1)
467 (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local))))
469 (defun org-columns-hscoll-title ()
470 "Set the `header-line-format' so that it scrolls along with the table."
471 (sit-for .0001) ; need to force a redisplay to update window-hscroll
472 (when (not (= (window-hscroll) org-columns-previous-hscroll))
473 (setq header-line-format
474 (concat (substring org-columns-full-header-line-format 0 1)
475 (substring org-columns-full-header-line-format
476 (1+ (window-hscroll))))
477 org-columns-previous-hscroll (window-hscroll))
478 (force-mode-line-update)))
480 (defvar org-colview-initial-truncate-line-value nil
481 "Remember the value of `truncate-lines' across colview.")
483 (defun org-columns-remove-overlays ()
484 "Remove all currently active column overlays."
485 (interactive)
486 (when (marker-buffer org-columns-begin-marker)
487 (with-current-buffer (marker-buffer org-columns-begin-marker)
488 (when (local-variable-p 'org-previous-header-line-format (current-buffer))
489 (if (featurep 'xemacs)
490 (set-specifier top-gutter
491 (make-gutter-specifier
492 (cons (current-buffer)
493 (cdar org-previous-header-line-format))))
494 (setq header-line-format org-previous-header-line-format)
495 (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
496 (kill-local-variable 'org-previous-header-line-format))
497 (move-marker org-columns-begin-marker nil)
498 (move-marker org-columns-top-level-marker nil)
499 (org-unmodified
500 (mapc 'delete-overlay org-columns-overlays)
501 (setq org-columns-overlays nil)
502 (let ((inhibit-read-only t))
503 (remove-text-properties (point-min) (point-max) '(read-only t))))
504 (when (local-variable-p 'org-colview-initial-truncate-line-value
505 (current-buffer))
506 (setq truncate-lines org-colview-initial-truncate-line-value)))))
509 (defun org-columns-cleanup-item (item fmt)
510 "Remove from ITEM what is a column in the format FMT."
511 (if (not org-complex-heading-regexp)
512 item
513 (when (string-match org-complex-heading-regexp item)
514 (setq item
515 (concat
516 (org-add-props (match-string 1 item) nil
517 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
518 (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
519 (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
520 " " (save-match-data (org-columns-compact-links (match-string 4 item)))
521 (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))
522 (add-text-properties
523 0 (1+ (match-end 1))
524 (list 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
525 item)
526 item)))
528 (defun org-columns-compact-links (s)
529 "Replace [[link][desc]] with [desc] or [link]."
530 (while (string-match org-bracket-link-regexp s)
531 (setq s (replace-match
532 (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
533 t t s)))
536 (defvar org-agenda-columns-remove-prefix-from-item)
538 (defun org-agenda-columns-cleanup-item (item pl cphr fmt)
539 "Cleanup the time property for agenda column view.
540 See also the variable `org-agenda-columns-remove-prefix-from-item'."
541 (let* ((org-complex-heading-regexp cphr)
542 (prefix (substring item 0 pl))
543 (rest (substring item pl))
544 (fake (concat "* " rest))
545 (cleaned (org-trim (substring (org-columns-cleanup-item fake fmt) 1))))
546 (if org-agenda-columns-remove-prefix-from-item
547 cleaned
548 (concat prefix cleaned))))
550 (defun org-columns-show-value ()
551 "Show the full value of the property."
552 (interactive)
553 (let ((value (get-char-property (point) 'org-columns-value)))
554 (message "Value is: %s" (or value ""))))
556 (defvar org-agenda-columns-active) ;; defined in org-agenda.el
558 (defun org-columns-quit ()
559 "Remove the column overlays and in this way exit column editing."
560 (interactive)
561 (org-unmodified
562 (org-columns-remove-overlays)
563 (let ((inhibit-read-only t))
564 (remove-text-properties (point-min) (point-max) '(read-only t))))
565 (when (eq major-mode 'org-agenda-mode)
566 (setq org-agenda-columns-active nil)
567 (message
568 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
570 (defun org-columns-check-computed ()
571 "Check if this column value is computed.
572 If yes, throw an error indicating that changing it does not make sense."
573 (let ((val (get-char-property (point) 'org-columns-value)))
574 (when (and (stringp val)
575 (get-char-property 0 'org-computed val))
576 (error "This value is computed from the entry's children"))))
578 (defun org-columns-todo (&optional arg)
579 "Change the TODO state during column view."
580 (interactive "P")
581 (org-columns-edit-value "TODO"))
583 (defun org-columns-set-tags-or-toggle (&optional arg)
584 "Toggle checkbox at point, or set tags for current headline."
585 (interactive "P")
586 (if (string-match "\\`\\[[ xX-]\\]\\'"
587 (get-char-property (point) 'org-columns-value))
588 (org-columns-next-allowed-value)
589 (org-columns-edit-value "TAGS")))
591 (defun org-columns-edit-value (&optional key)
592 "Edit the value of the property at point in column view.
593 Where possible, use the standard interface for changing this line."
594 (interactive)
595 (org-columns-check-computed)
596 (let* ((col (current-column))
597 (key (or key (get-char-property (point) 'org-columns-key)))
598 (value (get-char-property (point) 'org-columns-value))
599 (bol (point-at-bol)) (eol (point-at-eol))
600 (pom (or (get-text-property bol 'org-hd-marker)
601 (point))) ; keep despite of compiler warning
602 (line-overlays
603 (delq nil (mapcar (lambda (x)
604 (and (eq (overlay-buffer x) (current-buffer))
605 (>= (overlay-start x) bol)
606 (<= (overlay-start x) eol)
608 org-columns-overlays)))
609 (org-columns-time (time-to-number-of-days (current-time)))
610 nval eval allowed)
611 (cond
612 ((equal key "CLOCKSUM")
613 (error "This special column cannot be edited"))
614 ((equal key "ITEM")
615 (setq eval '(org-with-point-at pom (org-edit-headline))))
616 ((equal key "TODO")
617 (setq eval '(org-with-point-at
619 (call-interactively 'org-todo))))
620 ((equal key "PRIORITY")
621 (setq eval '(org-with-point-at pom
622 (call-interactively 'org-priority))))
623 ((equal key "TAGS")
624 (setq eval '(org-with-point-at
626 (let ((org-fast-tag-selection-single-key
627 (if (eq org-fast-tag-selection-single-key 'expert)
628 t org-fast-tag-selection-single-key)))
629 (call-interactively 'org-set-tags)))))
630 ((equal key "DEADLINE")
631 (setq eval '(org-with-point-at
633 (call-interactively 'org-deadline))))
634 ((equal key "SCHEDULED")
635 (setq eval '(org-with-point-at
637 (call-interactively 'org-schedule))))
639 (setq allowed (org-property-get-allowed-values pom key 'table))
640 (if allowed
641 (setq nval (org-icompleting-read
642 "Value: " allowed nil
643 (not (get-text-property 0 'org-unrestricted
644 (caar allowed)))))
645 (setq nval (read-string "Edit: " value)))
646 (setq nval (org-trim nval))
647 (when (not (equal nval value))
648 (setq eval '(org-entry-put pom key nval)))))
649 (when eval
651 (cond
652 ((equal major-mode 'org-agenda-mode)
653 (org-columns-eval eval)
654 ;; The following let preserves the current format, and makes sure
655 ;; that in only a single file things need to be upated.
656 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
657 (buffer (marker-buffer pom))
658 (org-agenda-contributing-files
659 (list (with-current-buffer buffer
660 (buffer-file-name (buffer-base-buffer))))))
661 (org-agenda-columns)))
663 (let ((inhibit-read-only t))
664 (org-unmodified
665 (remove-text-properties
666 (max (point-min) (1- bol)) eol '(read-only t)))
667 (unwind-protect
668 (progn
669 (setq org-columns-overlays
670 (org-delete-all line-overlays org-columns-overlays))
671 (mapc 'delete-overlay line-overlays)
672 (org-columns-eval eval))
673 (org-columns-display-here)))
674 (org-move-to-column col)
675 (if (and (org-mode-p)
676 (nth 3 (assoc key org-columns-current-fmt-compiled)))
677 (org-columns-update key)))))))
679 (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
680 "Edit the current headline, the part without TODO keyword, TAGS."
681 (org-back-to-heading)
682 (when (looking-at org-todo-line-regexp)
683 (let ((pos (point))
684 (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
685 (txt (match-string 3))
686 (post "")
687 txt2)
688 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@#%]+:[ \t]*$") txt)
689 (setq post (match-string 0 txt)
690 txt (substring txt 0 (match-beginning 0))))
691 (setq txt2 (read-string "Edit: " txt))
692 (when (not (equal txt txt2))
693 (goto-char pos)
694 (insert pre txt2 post)
695 (delete-region (point) (point-at-eol))
696 (org-set-tags nil t)))))
698 (defun org-columns-edit-allowed ()
699 "Edit the list of allowed values for the current property."
700 (interactive)
701 (let* ((pom (or (org-get-at-bol 'org-marker)
702 (org-get-at-bol 'org-hd-marker)
703 (point)))
704 (key (get-char-property (point) 'org-columns-key))
705 (key1 (concat key "_ALL"))
706 (allowed (org-entry-get pom key1 t))
707 nval)
708 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
709 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
710 (setq nval (read-string "Allowed: " allowed))
711 (org-entry-put
712 (cond ((marker-position org-entry-property-inherited-from)
713 org-entry-property-inherited-from)
714 ((marker-position org-columns-top-level-marker)
715 org-columns-top-level-marker)
716 (t pom))
717 key1 nval)))
719 (defun org-columns-eval (form)
720 (let (hidep)
721 (save-excursion
722 (beginning-of-line 1)
723 ;; `next-line' is needed here, because it skips invisible line.
724 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
725 (setq hidep (org-on-heading-p 1)))
726 (eval form)
727 (and hidep (hide-entry))))
729 (defun org-columns-previous-allowed-value ()
730 "Switch to the previous allowed value for this column."
731 (interactive)
732 (org-columns-next-allowed-value t))
734 (defun org-columns-next-allowed-value (&optional previous nth)
735 "Switch to the next allowed value for this column.
736 When PREVIOUS is set, go to the previous value. When NTH is
737 an integer, select that value."
738 (interactive)
739 (org-columns-check-computed)
740 (let* ((col (current-column))
741 (key (get-char-property (point) 'org-columns-key))
742 (value (get-char-property (point) 'org-columns-value))
743 (bol (point-at-bol)) (eol (point-at-eol))
744 (pom (or (get-text-property bol 'org-hd-marker)
745 (point))) ; keep despite of compiler waring
746 (line-overlays
747 (delq nil (mapcar (lambda (x)
748 (and (eq (overlay-buffer x) (current-buffer))
749 (>= (overlay-start x) bol)
750 (<= (overlay-start x) eol)
752 org-columns-overlays)))
753 (allowed (or (org-property-get-allowed-values pom key)
754 (and (memq
755 (nth 4 (assoc key org-columns-current-fmt-compiled))
756 '(checkbox checkbox-n-of-m checkbox-percent))
757 '("[ ]" "[X]"))
758 (org-colview-construct-allowed-dates value)))
759 nval)
760 (when (integerp nth)
761 (setq nth (1- nth))
762 (if (= nth -1) (setq nth 9)))
763 (when (equal key "ITEM")
764 (error "Cannot edit item headline from here"))
765 (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
766 (error "Allowed values for this property have not been defined"))
767 (if (member key '("SCHEDULED" "DEADLINE"))
768 (setq nval (if previous 'earlier 'later))
769 (if previous (setq allowed (reverse allowed)))
770 (cond
771 (nth
772 (setq nval (nth nth allowed))
773 (if (not nval)
774 (error "There are only %d allowed values for property `%s'"
775 (length allowed) key)))
776 ((member value allowed)
777 (setq nval (or (car (cdr (member value allowed)))
778 (car allowed)))
779 (if (equal nval value)
780 (error "Only one allowed value for this property")))
781 (t (setq nval (car allowed)))))
782 (cond
783 ((equal major-mode 'org-agenda-mode)
784 (org-columns-eval '(org-entry-put pom key nval))
785 ;; The following let preserves the current format, and makes sure
786 ;; that in only a single file things need to be upated.
787 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
788 (buffer (marker-buffer pom))
789 (org-agenda-contributing-files
790 (list (with-current-buffer buffer
791 (buffer-file-name (buffer-base-buffer))))))
792 (org-agenda-columns)))
794 (let ((inhibit-read-only t))
795 (remove-text-properties (1- bol) eol '(read-only t))
796 (unwind-protect
797 (progn
798 (setq org-columns-overlays
799 (org-delete-all line-overlays org-columns-overlays))
800 (mapc 'delete-overlay line-overlays)
801 (org-columns-eval '(org-entry-put pom key nval)))
802 (org-columns-display-here)))
803 (org-move-to-column col)
804 (and (nth 3 (assoc key org-columns-current-fmt-compiled))
805 (org-columns-update key))))))
807 (defun org-colview-construct-allowed-dates (s)
808 "Construct a list of three dates around the date in S.
809 This respects the format of the time stamp in S, active or non-active,
810 and also including time or not. S must be just a time stamp, no text
811 around it."
812 (when (and s (string-match (concat "^" org-ts-regexp3 "$") s))
813 (let* ((time (org-parse-time-string s 'nodefaults))
814 (active (equal (string-to-char s) ?<))
815 (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
816 time-before time-after)
817 (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
818 (setf (car time) (or (car time) 0))
819 (setf (nth 1 time) (or (nth 1 time) 0))
820 (setf (nth 2 time) (or (nth 2 time) 0))
821 (setq time-before (copy-sequence time))
822 (setq time-after (copy-sequence time))
823 (setf (nth 3 time-before) (1- (nth 3 time)))
824 (setf (nth 3 time-after) (1+ (nth 3 time)))
825 (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
826 (list time-before time time-after)))))
828 (defun org-verify-version (task)
829 (cond
830 ((eq task 'columns)
831 (if (or (and (featurep 'xemacs) (not (featurep 'org-colview-xemacs)))
832 (and (not (featurep 'xemacs)) (< emacs-major-version 22)))
833 (error "This version of Emacs cannot run Column View")))))
835 (defun org-columns-open-link (&optional arg)
836 (interactive "P")
837 (let ((value (get-char-property (point) 'org-columns-value)))
838 (org-open-link-from-string value arg)))
840 (defun org-columns-get-format-and-top-level ()
841 (let (fmt)
842 (when (condition-case nil (org-back-to-heading) (error nil))
843 (setq fmt (org-entry-get nil "COLUMNS" t)))
844 (setq fmt (or fmt org-columns-default-format))
845 (org-set-local 'org-columns-current-fmt fmt)
846 (org-columns-compile-format fmt)
847 (if (marker-position org-entry-property-inherited-from)
848 (move-marker org-columns-top-level-marker
849 org-entry-property-inherited-from)
850 (move-marker org-columns-top-level-marker (point)))
851 fmt))
853 (defun org-columns ()
854 "Turn on column view on an org-mode file."
855 (interactive)
856 (org-verify-version 'columns)
857 (when (featurep 'xemacs)
858 (set-face-foreground 'org-columns-space
859 (face-background 'org-columns-space)))
860 (org-columns-remove-overlays)
861 (move-marker org-columns-begin-marker (point))
862 (let ((org-columns-time (time-to-number-of-days (current-time)))
863 beg end fmt cache maxwidths)
864 (setq fmt (org-columns-get-format-and-top-level))
865 (save-excursion
866 (goto-char org-columns-top-level-marker)
867 (setq beg (point))
868 (unless org-columns-inhibit-recalculation
869 (org-columns-compute-all))
870 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
871 (point-max)))
872 ;; Get and cache the properties
873 (goto-char beg)
874 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
875 (save-excursion
876 (save-restriction
877 (narrow-to-region beg end)
878 (org-clock-sum))))
879 (while (re-search-forward (concat "^" outline-regexp) end t)
880 (if (and org-columns-skip-archived-trees
881 (looking-at (concat ".*:" org-archive-tag ":")))
882 (org-end-of-subtree t)
883 (push (cons (org-current-line) (org-entry-properties)) cache)))
884 (when cache
885 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
886 (org-set-local 'org-columns-current-maxwidths maxwidths)
887 (org-columns-display-here-title)
888 (unless (local-variable-p 'org-colview-initial-truncate-line-value
889 (current-buffer))
890 (org-set-local 'org-colview-initial-truncate-line-value
891 truncate-lines))
892 (setq truncate-lines t)
893 (mapc (lambda (x)
894 (org-goto-line (car x))
895 (org-columns-display-here (cdr x)))
896 cache)))))
898 (eval-when-compile (defvar org-columns-time))
900 (defvar org-columns-compile-map
901 '(("none" none +)
902 (":" add_times +)
903 ("+" add_numbers +)
904 ("$" currency +)
905 ("X" checkbox +)
906 ("X/" checkbox-n-of-m +)
907 ("X%" checkbox-percent +)
908 ("max" max_numbers max)
909 ("min" min_numbers min)
910 ("mean" mean_numbers
911 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
912 (":max" max_times max)
913 (":min" min_times min)
914 (":mean" mean_times
915 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
916 ("@min" min_age min (lambda (x) (- org-columns-time x)))
917 ("@max" max_age max (lambda (x) (- org-columns-time x)))
918 ("@mean" mean_age
919 (lambda (&rest x) (/ (apply '+ x) (float (length x))))
920 (lambda (x) (- org-columns-time x)))
921 ("est+" estimate org-estimate-combine))
922 "Operator <-> format,function,calc map.
923 Used to compile/uncompile columns format and completing read in
924 interactive function `org-columns-new'.
926 operator string used in #+COLUMNS definition describing the
927 summary type
928 format symbol describing summary type selected interactively in
929 `org-columns-new' and internally in
930 `org-columns-number-to-string' and
931 `org-columns-string-to-number'
932 function called with a list of values as argument to calculate
933 the summary value
934 calc function called on every element before summarizing. This is
935 optional and should only be specified if needed")
938 (defun org-columns-new (&optional prop title width op fmt fun &rest rest)
939 "Insert a new column, to the left of the current column."
940 (interactive)
941 (let ((n (org-columns-current-column))
942 (editp (and prop (assoc prop org-columns-current-fmt-compiled)))
943 cell)
944 (setq prop (org-icompleting-read
945 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
946 nil nil prop))
947 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
948 (setq width (read-string "Column width: " (if width (number-to-string width))))
949 (if (string-match "\\S-" width)
950 (setq width (string-to-number width))
951 (setq width nil))
952 (setq fmt (org-icompleting-read "Summary [none]: "
953 (mapcar (lambda (x) (list (symbol-name (cadr x)))) org-columns-compile-map)
954 nil t))
955 (setq fmt (intern fmt)
956 fun (cdr (assoc fmt (mapcar 'cdr org-columns-compile-map))))
957 (if (eq fmt 'none) (setq fmt nil))
958 (if editp
959 (progn
960 (setcar editp prop)
961 (setcdr editp (list title width nil fmt nil fun)))
962 (setq cell (nthcdr (1- n) org-columns-current-fmt-compiled))
963 (setcdr cell (cons (list prop title width nil fmt nil
964 (car fun) (cadr fun))
965 (cdr cell))))
966 (org-columns-store-format)
967 (org-columns-redo)))
969 (defun org-columns-delete ()
970 "Delete the column at point from columns view."
971 (interactive)
972 (let* ((n (org-columns-current-column))
973 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
974 (when (y-or-n-p
975 (format "Are you sure you want to remove column \"%s\"? " title))
976 (setq org-columns-current-fmt-compiled
977 (delq (nth n org-columns-current-fmt-compiled)
978 org-columns-current-fmt-compiled))
979 (org-columns-store-format)
980 (org-columns-redo)
981 (if (>= (org-columns-current-column)
982 (length org-columns-current-fmt-compiled))
983 (org-columns-backward-char)))))
985 (defun org-columns-edit-attributes ()
986 "Edit the attributes of the current column."
987 (interactive)
988 (let* ((n (org-columns-current-column))
989 (info (nth n org-columns-current-fmt-compiled)))
990 (apply 'org-columns-new info)))
992 (defun org-columns-widen (arg)
993 "Make the column wider by ARG characters."
994 (interactive "p")
995 (let* ((n (org-columns-current-column))
996 (entry (nth n org-columns-current-fmt-compiled))
997 (width (or (nth 2 entry)
998 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
999 (setq width (max 1 (+ width arg)))
1000 (setcar (nthcdr 2 entry) width)
1001 (org-columns-store-format)
1002 (org-columns-redo)))
1004 (defun org-columns-narrow (arg)
1005 "Make the column narrower by ARG characters."
1006 (interactive "p")
1007 (org-columns-widen (- arg)))
1009 (defun org-columns-move-right ()
1010 "Swap this column with the one to the right."
1011 (interactive)
1012 (let* ((n (org-columns-current-column))
1013 (cell (nthcdr n org-columns-current-fmt-compiled))
1015 (when (>= n (1- (length org-columns-current-fmt-compiled)))
1016 (error "Cannot shift this column further to the right"))
1017 (setq e (car cell))
1018 (setcar cell (car (cdr cell)))
1019 (setcdr cell (cons e (cdr (cdr cell))))
1020 (org-columns-store-format)
1021 (org-columns-redo)
1022 (org-columns-forward-char)))
1024 (defun org-columns-move-left ()
1025 "Swap this column with the one to the left."
1026 (interactive)
1027 (let* ((n (org-columns-current-column)))
1028 (when (= n 0)
1029 (error "Cannot shift this column further to the left"))
1030 (org-columns-backward-char)
1031 (org-columns-move-right)
1032 (org-columns-backward-char)))
1034 (defun org-columns-store-format ()
1035 "Store the text version of the current columns format in appropriate place.
1036 This is either in the COLUMNS property of the node starting the current column
1037 display, or in the #+COLUMNS line of the current buffer."
1038 (let (fmt (cnt 0))
1039 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
1040 (org-set-local 'org-columns-current-fmt fmt)
1041 (if (marker-position org-columns-top-level-marker)
1042 (save-excursion
1043 (goto-char org-columns-top-level-marker)
1044 (if (and (org-at-heading-p)
1045 (org-entry-get nil "COLUMNS"))
1046 (org-entry-put nil "COLUMNS" fmt)
1047 (goto-char (point-min))
1048 ;; Overwrite all #+COLUMNS lines....
1049 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
1050 (setq cnt (1+ cnt))
1051 (replace-match (concat "#+COLUMNS: " fmt) t t))
1052 (unless (> cnt 0)
1053 (goto-char (point-min))
1054 (or (org-on-heading-p t) (outline-next-heading))
1055 (let ((inhibit-read-only t))
1056 (insert-before-markers "#+COLUMNS: " fmt "\n")))
1057 (org-set-local 'org-columns-default-format fmt))))))
1059 (defvar org-agenda-overriding-columns-format nil
1060 "When set, overrides any other format definition for the agenda.
1061 Don't set this, this is meant for dynamic scoping.")
1063 (defun org-columns-get-autowidth-alist (s cache)
1064 "Derive the maximum column widths from the format and the cache."
1065 (let ((start 0) rtn)
1066 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
1067 (push (cons (match-string 1 s) 1) rtn)
1068 (setq start (match-end 0)))
1069 (mapc (lambda (x)
1070 (setcdr x (apply 'max
1071 (mapcar
1072 (lambda (y)
1073 (length (or (cdr (assoc (car x) (cdr y))) " ")))
1074 cache))))
1075 rtn)
1076 rtn))
1078 (defun org-columns-compute-all ()
1079 "Compute all columns that have operators defined."
1080 (org-unmodified
1081 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
1082 (let ((columns org-columns-current-fmt-compiled)
1083 (org-columns-time (time-to-number-of-days (current-time)))
1084 col)
1085 (while (setq col (pop columns))
1086 (when (nth 3 col)
1087 (save-excursion
1088 (org-columns-compute (car col)))))))
1090 (defun org-columns-update (property)
1091 "Recompute PROPERTY, and update the columns display for it."
1092 (org-columns-compute property)
1093 (let (fmt val pos face)
1094 (save-excursion
1095 (mapc (lambda (ov)
1096 (when (equal (overlay-get ov 'org-columns-key) property)
1097 (setq pos (overlay-start ov))
1098 (goto-char pos)
1099 (when (setq val (cdr (assoc property
1100 (get-text-property
1101 (point-at-bol) 'org-summaries))))
1102 (setq fmt (overlay-get ov 'org-columns-format))
1103 (overlay-put ov 'org-columns-value val)
1104 (if (featurep 'xemacs)
1105 (progn
1106 (setq face (glyph-face (extent-end-glyph ov)))
1107 (org-overlay-display ov (format fmt val) face))
1108 (org-overlay-display ov (format fmt val))))))
1109 org-columns-overlays))))
1111 (defun org-columns-compute (property)
1112 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
1113 (interactive)
1114 (let* ((re (concat "^" outline-regexp))
1115 (lmax 30) ; Does anyone use deeper levels???
1116 (lvals (make-vector lmax nil))
1117 (lflag (make-vector lmax nil))
1118 (level 0)
1119 (ass (assoc property org-columns-current-fmt-compiled))
1120 (format (nth 4 ass))
1121 (printf (nth 5 ass))
1122 (fun (nth 6 ass))
1123 (calc (or (nth 7 ass) 'identity))
1124 (beg org-columns-top-level-marker)
1125 last-level val valflag flag end sumpos sum-alist sum str str1 useval)
1126 (save-excursion
1127 ;; Find the region to compute
1128 (goto-char beg)
1129 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
1130 (goto-char end)
1131 ;; Walk the tree from the back and do the computations
1132 (while (re-search-backward re beg t)
1133 (setq sumpos (match-beginning 0)
1134 last-level level
1135 level (org-outline-level)
1136 val (org-entry-get nil property)
1137 valflag (and val (string-match "\\S-" val)))
1138 (cond
1139 ((< level last-level)
1140 ;; put the sum of lower levels here as a property
1141 (setq sum (when (aref lvals last-level)
1142 (apply fun (aref lvals last-level)))
1143 flag (aref lflag last-level) ; any valid entries from children?
1144 str (org-columns-number-to-string sum format printf)
1145 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
1146 useval (if flag str1 (if valflag val ""))
1147 sum-alist (get-text-property sumpos 'org-summaries))
1148 (if (assoc property sum-alist)
1149 (setcdr (assoc property sum-alist) useval)
1150 (push (cons property useval) sum-alist)
1151 (org-unmodified
1152 (add-text-properties sumpos (1+ sumpos)
1153 (list 'org-summaries sum-alist))))
1154 (when (and val (not (equal val (if flag str val))))
1155 (org-entry-put nil property (if flag str val)))
1156 ;; add current to current level accumulator
1157 (when (or flag valflag)
1158 (push (if flag
1160 (funcall calc (org-columns-string-to-number
1161 (if flag str val) format)))
1162 (aref lvals level))
1163 (aset lflag level t))
1164 ;; clear accumulators for deeper levels
1165 (loop for l from (1+ level) to (1- lmax) do
1166 (aset lvals l nil)
1167 (aset lflag l nil)))
1168 ((>= level last-level)
1169 ;; add what we have here to the accumulator for this level
1170 (when valflag
1171 (push (funcall calc (org-columns-string-to-number val format))
1172 (aref lvals level))
1173 (aset lflag level t)))
1174 (t (error "This should not happen")))))))
1176 (defun org-columns-redo ()
1177 "Construct the column display again."
1178 (interactive)
1179 (message "Recomputing columns...")
1180 (save-excursion
1181 (if (marker-position org-columns-begin-marker)
1182 (goto-char org-columns-begin-marker))
1183 (org-columns-remove-overlays)
1184 (if (org-mode-p)
1185 (call-interactively 'org-columns)
1186 (org-agenda-redo)
1187 (call-interactively 'org-agenda-columns)))
1188 (when (featurep 'xemacs)
1189 (while (not (or (eolp)
1190 (member (extent-at (point)) org-columns-overlays)))
1191 (forward-char)))
1192 (message "Recomputing columns...done"))
1194 (defun org-columns-not-in-agenda ()
1195 (if (eq major-mode 'org-agenda-mode)
1196 (error "This command is only allowed in Org-mode buffers")))
1198 (defun org-string-to-number (s)
1199 "Convert string to number, and interpret hh:mm:ss."
1200 (if (not (string-match ":" s))
1201 (string-to-number s)
1202 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1203 (while l
1204 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1205 sum)))
1207 (defun org-columns-number-to-string (n fmt &optional printf)
1208 "Convert a computed column number to a string value, according to FMT."
1209 (cond
1210 ((memq fmt '(estimate)) (org-estimate-print n printf))
1211 ((not (numberp n)) "")
1212 ((memq fmt '(add_times max_times min_times mean_times))
1213 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
1214 (format org-time-clocksum-format h m)))
1215 ((eq fmt 'checkbox)
1216 (cond ((= n (floor n)) "[X]")
1217 ((> n 1.) "[-]")
1218 (t "[ ]")))
1219 ((memq fmt '(checkbox-n-of-m checkbox-percent))
1220 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
1221 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
1222 (printf (format printf n))
1223 ((eq fmt 'currency)
1224 (format "%.2f" n))
1225 ((memq fmt '(min_age max_age mean_age))
1226 (org-format-time-period n))
1227 (t (number-to-string n))))
1229 (defun org-nofm-to-completion (n m &optional percent)
1230 (if (not percent)
1231 (format "[%d/%d]" n m)
1232 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
1234 (defun org-columns-string-to-number (s fmt)
1235 "Convert a column value to a number that can be used for column computing."
1236 (if s
1237 (cond
1238 ((memq fmt '(min_age max_age mean_age))
1239 (cond ((string= s "") org-columns-time)
1240 ((string-match
1241 "\\([0-9]+\\)d \\([0-9]+\\)h \\([0-9]+\\)m \\([0-9]+\\)s"
1243 (+ (* 60 (+ (* 60 (+ (* 24 (string-to-number (match-string 1 s)))
1244 (string-to-number (match-string 2 s))))
1245 (string-to-number (match-string 3 s))))
1246 (string-to-number (match-string 4 s))))
1247 (t (time-to-number-of-days (apply 'encode-time
1248 (org-parse-time-string s t))))))
1249 ((string-match ":" s)
1250 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1251 (while l
1252 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1253 sum))
1254 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
1255 (if (equal s "[X]") 1. 0.000001))
1256 ((memq fmt '(estimate)) (org-string-to-estimate s))
1257 (t (string-to-number s)))))
1259 (defun org-columns-uncompile-format (cfmt)
1260 "Turn the compiled columns format back into a string representation."
1261 (let ((rtn "") e s prop title op op-match width fmt printf fun calc)
1262 (while (setq e (pop cfmt))
1263 (setq prop (car e)
1264 title (nth 1 e)
1265 width (nth 2 e)
1266 op (nth 3 e)
1267 fmt (nth 4 e)
1268 printf (nth 5 e)
1269 fun (nth 6 e)
1270 calc (nth 7 e))
1271 (when (setq op-match (rassoc (list fmt fun calc) org-columns-compile-map))
1272 (setq op (car op-match)))
1273 (if (and op printf) (setq op (concat op ";" printf)))
1274 (if (equal title prop) (setq title nil))
1275 (setq s (concat "%" (if width (number-to-string width))
1276 prop
1277 (if title (concat "(" title ")"))
1278 (if op (concat "{" op "}"))))
1279 (setq rtn (concat rtn " " s)))
1280 (org-trim rtn)))
1282 (defun org-columns-compile-format (fmt)
1283 "Turn a column format string into an alist of specifications.
1284 The alist has one entry for each column in the format. The elements of
1285 that list are:
1286 property the property
1287 title the title field for the columns
1288 width the column width in characters, can be nil for automatic
1289 operator the operator if any
1290 format the output format for computed results, derived from operator
1291 printf a printf format for computed values
1292 fun the lisp function to compute summary values, derived from operator
1293 calc function to get values from base elements"
1294 (let ((start 0) width prop title op op-match f printf fun calc)
1295 (setq org-columns-current-fmt-compiled nil)
1296 (while (string-match
1297 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
1298 fmt start)
1299 (setq start (match-end 0)
1300 width (match-string 1 fmt)
1301 prop (match-string 2 fmt)
1302 title (or (match-string 3 fmt) prop)
1303 op (match-string 4 fmt)
1304 f nil
1305 printf nil
1306 fun '+
1307 calc nil)
1308 (if width (setq width (string-to-number width)))
1309 (when (and op (string-match ";" op))
1310 (setq printf (substring op (match-end 0))
1311 op (substring op 0 (match-beginning 0))))
1312 (when (setq op-match (assoc op org-columns-compile-map))
1313 (setq f (cadr op-match)
1314 fun (caddr op-match)
1315 calc (cadddr op-match)))
1316 (push (list prop title width op f printf fun calc)
1317 org-columns-current-fmt-compiled))
1318 (setq org-columns-current-fmt-compiled
1319 (nreverse org-columns-current-fmt-compiled))))
1322 ;;; Dynamic block for Column view
1324 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
1325 "Get the column view of the current buffer or subtree.
1326 The first optional argument MAXLEVEL sets the level limit. A
1327 second optional argument SKIP-EMPTY-ROWS tells whether to skip
1328 empty rows, an empty row being one where all the column view
1329 specifiers except ITEM are empty. This function returns a list
1330 containing the title row and all other rows. Each row is a list
1331 of fields."
1332 (if (featurep 'xemacs)
1333 (save-excursion
1334 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1335 (re-comment (concat "\\*+[ \t]+" org-comment-string "\\>"))
1336 (re-archive (concat ".*:" org-archive-tag ":"))
1337 (n (length title)) row tbl)
1338 (goto-char (point-min))
1340 (while (re-search-forward "^\\(\\*+\\) " nil t)
1341 (catch 'next
1342 (when (and (or (null maxlevel)
1343 (>= maxlevel
1344 (if org-odd-levels-only
1345 (/ (1+ (length (match-string 1))) 2)
1346 (length (match-string 1)))))
1347 (get-char-property (match-beginning 0) 'org-columns-key))
1348 (goto-char (match-beginning 0))
1349 (when (save-excursion
1350 (goto-char (point-at-bol))
1351 (or (looking-at re-comment)
1352 (looking-at re-archive)))
1353 (org-end-of-subtree t)
1354 (throw 'next t))
1355 (setq row nil)
1356 (loop for i from 0 to (1- n) do
1357 (push
1358 (org-quote-vert
1359 (or (get-char-property (point)
1360 'org-columns-value-modified)
1361 (get-char-property (point) 'org-columns-value)
1362 ""))
1363 row)
1364 (org-columns-forward-char))
1365 (setq row (nreverse row))
1366 (unless (and skip-empty-rows
1367 (eq 1 (length (delete "" (delete-dups (copy-sequence row))))))
1368 (push row tbl)))))
1369 (append (list title 'hline) (nreverse tbl))))
1370 (save-excursion
1371 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1372 (n (length title)) row tbl)
1373 (goto-char (point-min))
1374 (while (and (re-search-forward "^\\(\\*+\\) " nil t)
1375 (or (null maxlevel)
1376 (>= maxlevel
1377 (if org-odd-levels-only
1378 (/ (1+ (length (match-string 1))) 2)
1379 (length (match-string 1))))))
1380 (when (get-char-property (match-beginning 0) 'org-columns-key)
1381 (setq row nil)
1382 (loop for i from 0 to (1- n) do
1383 (push (or (get-char-property (+ (match-beginning 0) i)
1384 'org-columns-value-modified)
1385 (get-char-property (+ (match-beginning 0) i)
1386 'org-columns-value)
1388 row))
1389 (setq row (nreverse row))
1390 (unless (and skip-empty-rows
1391 (eq 1 (length (delete "" (delete-dups row)))))
1392 (push row tbl))))
1393 (append (list title 'hline) (nreverse tbl))))))
1395 (defun org-dblock-write:columnview (params)
1396 "Write the column view table.
1397 PARAMS is a property list of parameters:
1399 :width enforce same column widths with <N> specifiers.
1400 :id the :ID: property of the entry where the columns view
1401 should be built. When the symbol `local', call locally.
1402 When `global' call column view with the cursor at the beginning
1403 of the buffer (usually this means that the whole buffer switches
1404 to column view). When \"file:path/to/file.org\", invoke column
1405 view at the start of that file. Otherwise, the ID is located
1406 using `org-id-find'.
1407 :hlines When t, insert a hline before each item. When a number, insert
1408 a hline before each level <= that number.
1409 :vlines When t, make each column a colgroup to enforce vertical lines.
1410 :maxlevel When set to a number, don't capture headlines below this level.
1411 :skip-empty-rows
1412 When t, skip rows where all specifiers other than ITEM are empty."
1413 (let ((pos (move-marker (make-marker) (point)))
1414 (hlines (plist-get params :hlines))
1415 (vlines (plist-get params :vlines))
1416 (maxlevel (plist-get params :maxlevel))
1417 (content-lines (org-split-string (plist-get params :content) "\n"))
1418 (skip-empty-rows (plist-get params :skip-empty-rows))
1419 tbl id idpos nfields tmp recalc line
1420 id-as-string view-file view-pos)
1421 (when (setq id (plist-get params :id))
1422 (setq id-as-string (cond ((numberp id) (number-to-string id))
1423 ((symbolp id) (symbol-name id))
1424 ((stringp id) id)
1425 (t "")))
1426 (cond ((not id) nil)
1427 ((eq id 'global) (setq view-pos (point-min)))
1428 ((eq id 'local))
1429 ((string-match "^file:\\(.*\\)" id-as-string)
1430 (setq view-file (match-string 1 id-as-string)
1431 view-pos 1)
1432 (unless (file-exists-p view-file)
1433 (error "No such file: \"%s\"" id-as-string)))
1434 ((setq idpos (org-find-entry-with-id id))
1435 (setq view-pos idpos))
1436 ((setq idpos (org-id-find id))
1437 (setq view-file (car idpos))
1438 (setq view-pos (cdr idpos)))
1439 (t (error "Cannot find entry with :ID: %s" id))))
1440 (with-current-buffer (if view-file
1441 (get-file-buffer view-file)
1442 (current-buffer))
1443 (save-excursion
1444 (save-restriction
1445 (widen)
1446 (goto-char (or view-pos (point)))
1447 (org-columns)
1448 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1449 (setq nfields (length (car tbl)))
1450 (org-columns-quit))))
1451 (goto-char pos)
1452 (move-marker pos nil)
1453 (when tbl
1454 (when (plist-get params :hlines)
1455 (setq tmp nil)
1456 (while tbl
1457 (if (eq (car tbl) 'hline)
1458 (push (pop tbl) tmp)
1459 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1460 (if (and (not (eq (car tmp) 'hline))
1461 (or (eq hlines t)
1462 (and (numberp hlines)
1463 (<= (- (match-end 1) (match-beginning 1))
1464 hlines))))
1465 (push 'hline tmp)))
1466 (push (pop tbl) tmp)))
1467 (setq tbl (nreverse tmp)))
1468 (when vlines
1469 (setq tbl (mapcar (lambda (x)
1470 (if (eq 'hline x) x (cons "" x)))
1471 tbl))
1472 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1473 (setq pos (point))
1474 (when content-lines
1475 (while (string-match "^#" (car content-lines))
1476 (insert (pop content-lines) "\n")))
1477 (insert (org-listtable-to-string tbl))
1478 (when (plist-get params :width)
1479 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1480 org-columns-current-widths "|")))
1481 (while (setq line (pop content-lines))
1482 (when (string-match "^#" line)
1483 (insert "\n" line)
1484 (when (string-match "^[ \t]*#\\+TBLFM" line)
1485 (setq recalc t))))
1486 (if recalc
1487 (progn (goto-char pos) (org-table-recalculate 'all))
1488 (goto-char pos)
1489 (org-table-align)))))
1491 (defun org-listtable-to-string (tbl)
1492 "Convert a listtable TBL to a string that contains the Org-mode table.
1493 The table still need to be aligned. The resulting string has no leading
1494 and tailing newline characters."
1495 (mapconcat
1496 (lambda (x)
1497 (cond
1498 ((listp x)
1499 (concat "|" (mapconcat 'identity x "|") "|"))
1500 ((eq x 'hline) "|-|")
1501 (t (error "Garbage in listtable: %s" x))))
1502 tbl "\n"))
1504 (defun org-insert-columns-dblock ()
1505 "Create a dynamic block capturing a column view table."
1506 (interactive)
1507 (when (featurep 'xemacs) (org-columns-quit))
1508 (let ((defaults '(:name "columnview" :hlines 1))
1509 (id (org-icompleting-read
1510 "Capture columns (local, global, entry with :ID: property) [local]: "
1511 (append '(("global") ("local"))
1512 (mapcar 'list (org-property-values "ID"))))))
1513 (if (equal id "") (setq id 'local))
1514 (if (equal id "global") (setq id 'global))
1515 (setq defaults (append defaults (list :id id)))
1516 (org-create-dblock defaults)
1517 (org-update-dblock)))
1519 ;;; Column view in the agenda
1521 (defvar org-agenda-view-columns-initially nil
1522 "When set, switch to columns view immediately after creating the agenda.")
1524 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1525 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1526 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1528 (defun org-agenda-columns ()
1529 "Turn on or update column view in the agenda."
1530 (interactive)
1531 (org-verify-version 'columns)
1532 (org-columns-remove-overlays)
1533 (move-marker org-columns-begin-marker (point))
1534 (let ((org-columns-time (time-to-number-of-days (current-time)))
1535 cache maxwidths m p a d fmt)
1536 (cond
1537 ((and (boundp 'org-agenda-overriding-columns-format)
1538 org-agenda-overriding-columns-format)
1539 (setq fmt org-agenda-overriding-columns-format)
1540 (org-set-local 'org-agenda-overriding-columns-format fmt))
1541 ((setq m (org-get-at-bol 'org-hd-marker))
1542 (setq fmt (or (org-entry-get m "COLUMNS" t)
1543 (with-current-buffer (marker-buffer m)
1544 org-columns-default-format))))
1545 ((and (boundp 'org-columns-current-fmt)
1546 (local-variable-p 'org-columns-current-fmt (current-buffer))
1547 org-columns-current-fmt)
1548 (setq fmt org-columns-current-fmt))
1549 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
1550 (setq m (get-text-property m 'org-hd-marker))
1551 (setq fmt (or (org-entry-get m "COLUMNS" t)
1552 (with-current-buffer (marker-buffer m)
1553 org-columns-default-format)))))
1554 (setq fmt (or fmt org-columns-default-format))
1555 (org-set-local 'org-columns-current-fmt fmt)
1556 (org-columns-compile-format fmt)
1557 (when org-agenda-columns-compute-summary-properties
1558 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1559 (save-excursion
1560 ;; Get and cache the properties
1561 (goto-char (point-min))
1562 (while (not (eobp))
1563 (when (setq m (or (org-get-at-bol 'org-hd-marker)
1564 (org-get-at-bol 'org-marker)))
1565 (setq p (org-entry-properties m))
1567 (when (or (not (setq a (assoc org-effort-property p)))
1568 (not (string-match "\\S-" (or (cdr a) ""))))
1569 ;; OK, the property is not defined. Use appointment duration?
1570 (when (and org-agenda-columns-add-appointments-to-effort-sum
1571 (setq d (get-text-property (point) 'duration)))
1572 (setq d (org-minutes-to-hh:mm-string d))
1573 (put-text-property 0 (length d) 'face 'org-warning d)
1574 (push (cons org-effort-property d) p)))
1575 (push (cons (org-current-line) p) cache))
1576 (beginning-of-line 2))
1577 (when cache
1578 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
1579 (org-set-local 'org-columns-current-maxwidths maxwidths)
1580 (org-columns-display-here-title)
1581 (mapc (lambda (x)
1582 (org-goto-line (car x))
1583 (org-columns-display-here (cdr x)))
1584 cache)
1585 (when org-agenda-columns-show-summaries
1586 (org-agenda-colview-summarize cache))))))
1588 (defun org-agenda-colview-summarize (cache)
1589 "Summarize the summarizable columns in column view in the agenda.
1590 This will add overlays to the date lines, to show the summary for each day."
1591 (let* ((fmt (mapcar (lambda (x)
1592 (if (equal (car x) "CLOCKSUM")
1593 (list "CLOCKSUM" (nth 2 x) nil 'add_times nil '+ 'identity)
1594 (cdr x)))
1595 org-columns-current-fmt-compiled))
1596 line c c1 stype calc sumfunc props lsum entries prop v)
1597 (catch 'exit
1598 (when (delq nil (mapcar 'cadr fmt))
1599 ;; OK, at least one summation column, it makes sense to try this
1600 (goto-char (point-max))
1601 (while t
1602 (when (or (get-text-property (point) 'org-date-line)
1603 (eq (get-text-property (point) 'face)
1604 'org-agenda-structure))
1605 ;; OK, this is a date line that should be used
1606 (setq line (org-current-line))
1607 (setq entries nil c cache cache nil)
1608 (while (setq c1 (pop c))
1609 (if (> (car c1) line)
1610 (push c1 entries)
1611 (push c1 cache)))
1612 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1613 ;; Compute the summaries for the properties we want,
1614 ;; set nil properties for the rest.
1615 (when (setq entries (mapcar 'cdr entries))
1616 (setq props
1617 (mapcar
1618 (lambda (f)
1619 (setq prop (car f)
1620 stype (nth 3 f)
1621 sumfunc (nth 5 f)
1622 calc (or (nth 6 f) 'identity))
1623 (cond
1624 ((equal prop "ITEM")
1625 (cons prop (buffer-substring (point-at-bol)
1626 (point-at-eol))))
1627 ((not stype) (cons prop ""))
1628 (t ;; do the summary
1629 (setq lsum nil)
1630 (dolist (x entries)
1631 (setq v (cdr (assoc prop x)))
1632 (if v
1633 (push
1634 (funcall
1635 (if (not (get-text-property 0 'org-computed v))
1636 calc
1637 'identity)
1638 (org-columns-string-to-number
1639 v stype))
1640 lsum)))
1641 (setq lsum (remove nil lsum))
1642 (setq lsum
1643 (cond ((> (length lsum) 1)
1644 (org-columns-number-to-string
1645 (apply sumfunc lsum) stype))
1646 ((eq (length lsum) 1)
1647 (org-columns-number-to-string
1648 (car lsum) stype))
1649 (t "")))
1650 (put-text-property 0 (length lsum) 'face 'bold lsum)
1651 (unless (eq calc 'identity)
1652 (put-text-property 0 (length lsum) 'org-computed t lsum))
1653 (cons prop lsum))))
1654 fmt))
1655 (org-columns-display-here props)
1656 (org-set-local 'org-agenda-columns-active t)))
1657 (if (bobp) (throw 'exit t))
1658 (beginning-of-line 0))))))
1660 (defun org-agenda-colview-compute (fmt)
1661 "Compute the relevant columns in the contributing source buffers."
1662 (let ((files org-agenda-contributing-files)
1663 (org-columns-begin-marker (make-marker))
1664 (org-columns-top-level-marker (make-marker))
1665 f fm a b)
1666 (while (setq f (pop files))
1667 (setq b (find-buffer-visiting f))
1668 (with-current-buffer (or (buffer-base-buffer b) b)
1669 (save-excursion
1670 (save-restriction
1671 (widen)
1672 (org-unmodified
1673 (remove-text-properties (point-min) (point-max)
1674 '(org-summaries t)))
1675 (goto-char (point-min))
1676 (org-columns-get-format-and-top-level)
1677 (while (setq fm (pop fmt))
1678 (if (equal (car fm) "CLOCKSUM")
1679 (org-clock-sum)
1680 (when (and (nth 4 fm)
1681 (setq a (assoc (car fm)
1682 org-columns-current-fmt-compiled))
1683 (equal (nth 4 a) (nth 4 fm)))
1684 (org-columns-compute (car fm)))))))))))
1686 (defun org-format-time-period (interval)
1687 "Convert time in fractional days to days/hours/minutes/seconds."
1688 (if (numberp interval)
1689 (let* ((days (floor interval))
1690 (frac-hours (* 24 (- interval days)))
1691 (hours (floor frac-hours))
1692 (minutes (floor (* 60 (- frac-hours hours))))
1693 (seconds (floor (* 60 (- (* 60 (- frac-hours hours)) minutes)))))
1694 (format "%dd %02dh %02dm %02ds" days hours minutes seconds))
1695 ""))
1697 (defun org-estimate-mean-and-var (v)
1698 "Return the mean and variance of an estimate."
1699 (let* ((low (float (car v)))
1700 (high (float (cadr v)))
1701 (mean (/ (+ low high) 2.0))
1702 (var (/ (+ (expt (- mean low) 2.0) (expt (- high mean) 2.0)) 2.0)))
1703 (list mean var)))
1705 (defun org-estimate-combine (&rest el)
1706 "Combine a list of estimates, using mean and variance.
1707 The mean and variance of the result will be the sum of the means
1708 and variances (respectively) of the individual estimates."
1709 (let ((mean 0)
1710 (var 0))
1711 (mapc (lambda (e)
1712 (let ((stats (org-estimate-mean-and-var e)))
1713 (setq mean (+ mean (car stats)))
1714 (setq var (+ var (cadr stats)))))
1716 (let ((stdev (sqrt var)))
1717 (list (- mean stdev) (+ mean stdev)))))
1719 (defun org-estimate-print (e &optional fmt)
1720 "Prepare a string representation of an estimate.
1721 This formats these numbers as two numbers with a \"-\" between them."
1722 (if (null fmt) (set 'fmt "%.0f"))
1723 (format "%s" (mapconcat (lambda (n) (format fmt n)) e "-")))
1725 (defun org-string-to-estimate (s)
1726 "Convert a string to an estimate.
1727 The string should be two numbers joined with a \"-\"."
1728 (if (string-match "\\(.*\\)-\\(.*\\)" s)
1729 (list (string-to-number (match-string 1 s))
1730 (string-to-number(match-string 2 s)))
1731 (list (string-to-number s) (string-to-number s))))
1733 (provide 'org-colview)
1734 (provide 'org-colview-xemacs)
1736 ;;; org-colview-xemacs.el ends here