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