Release 6.05b.
[org-mode.git] / lisp / org-colview-xemacs.el
blobfc8e2ac6aabf5511dbd55e94f0891c0c2e89d54f
1 ;;; org-colview-xemacs.el --- Column View in Org-mode, XEmacs-specific version
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;; Version: 6.05b
9 ;;
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;; Commentary:
30 ;; This file contains the face definitons for Org.
32 ;;; Code:
34 (eval-when-compile (require 'cl))
35 (require 'org)
37 (declare-function org-agenda-redo "org-agenda" ())
40 ;;; Definie additional faces for column view
42 (when (featurep 'xemacs)
44 (defface org-columns-level-1;; font-lock-function-name-face
45 (org-compatible-face
46 'outline-1
47 '((((class color) (min-colors 88) (background light)) (:foreground "Blue1" :background "grey90"))
48 (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue" :background "grey30"))
49 (((class color) (min-colors 16) (background light)) (:foreground "Blue" :background "grey90"))
50 (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue" :background "grey30"))
51 (((class color) (min-colors 8)) (:foreground "blue" :bold t))
52 (t (:bold t))))
53 "Face used for columns-level 1 headlines."
54 :group 'org-faces)
56 (defface org-columns-level-2;; font-lock-variable-name-face
57 (org-compatible-face
58 'outline-2
59 '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod" :background "grey90"))
60 (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod" :background "grey30"))
61 (((class color) (min-colors 8) (background light)) (:foreground "yellow" :background "grey90"))
62 (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
63 (t (:bold t))))
64 "Face used for columns-level 2 headlines."
65 :group 'org-faces)
67 (defface org-columns-level-3;; font-lock-keyword-face
68 (org-compatible-face
69 'outline-3
70 '((((class color) (min-colors 88) (background light)) (:foreground "Purple" :background "grey90"))
71 (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1" :background "grey30"))
72 (((class color) (min-colors 16) (background light)) (:foreground "Purple" :background "grey90"))
73 (((class color) (min-colors 16) (background dark)) (:foreground "Cyan" :background "grey30"))
74 (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
75 (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
76 (t (:bold t))))
77 "Face used for columns-level 3 headlines."
78 :group 'org-faces)
80 (defface org-columns-level-4;; font-lock-comment-face
81 (org-compatible-face
82 'outline-4
83 '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick" :background "grey90"))
84 (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1" :background "grey30"))
85 (((class color) (min-colors 16) (background light)) (:foreground "red"))
86 (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
87 (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
88 (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
89 (t (:bold t))))
90 "Face used for columns-level 4 headlines."
91 :group 'org-faces)
93 (defface org-columns-level-5;; font-lock-type-face
94 (org-compatible-face
95 'outline-5
96 '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :background "grey90"))
97 (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :background "grey30"))
98 (((class color) (min-colors 8)) (:foreground "green"))))
99 "Face used for columns-level 5 headlines."
100 :group 'org-faces)
102 (defface org-columns-level-6;; font-lock-constant-face
103 (org-compatible-face
104 'outline-6
105 '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue" :background "grey90"))
106 (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine" :background "grey30"))
107 (((class color) (min-colors 8)) (:foreground "magenta"))))
108 "Face used for columns-level 6 headlines."
109 :group 'org-faces)
111 (defface org-columns-level-7;; font-lock-builtin-face
112 (org-compatible-face
113 'outline-7
114 '((((class color) (min-colors 16) (background light)) (:foreground "Orchid" :background "grey90"))
115 (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue" :background "grey30"))
116 (((class color) (min-colors 8)) (:foreground "blue"))))
117 "Face used for columns-level 7 headlines."
118 :group 'org-faces)
120 (defface org-columns-level-8;; font-lock-string-face
121 (org-compatible-face
122 'outline-8
123 '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown" :background "grey90"))
124 (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon" :background "grey30"))
125 (((class color) (min-colors 8)) (:foreground "green"))))
126 "Face used for columns-level 8 headlines."
127 :group 'org-faces)
130 (defface org-columns-space;; font-lock-function-name-face
131 (org-compatible-face
132 'outline-1
133 '((((class color) (min-colors 88) (background light)) (:background "grey90"))
134 (((class color) (min-colors 88) (background dark)) (:background "grey30"))
135 (((class color) (min-colors 16) (background light)) (:background "grey90"))
136 (((class color) (min-colors 16) (background dark)) (:background "grey30"))
137 (((class color) (min-colors 8)) (:bold t :underline t))))
138 "Face used for columns space headlines."
139 :group 'org-faces)
141 (defface org-columns-space1;; font-lock-function-name-face
142 (org-compatible-face
143 'outline-1
144 '((((class color) (min-colors 88) (background light)) (:background "grey90"))
145 (((class color) (min-colors 88) (background dark)) (:background "grey30"))
146 (((class color) (min-colors 16) (background light)) (:background "grey90"))
147 (((class color) (min-colors 16) (background dark)) (:background "grey30"))
148 (((class color) (min-colors 8)) (:bold t :underline t))))
149 "Face used for columns space headlines."
150 :group 'org-faces)
153 (when (featurep 'xemacs)
154 (defconst org-columns-level-faces
155 '(org-columns-level-1
156 org-columns-level-2 org-columns-level-3
157 org-columns-level-4 org-columns-level-5 org-columns-level-6
158 org-columns-level-7 org-columns-level-8
161 (defun org-get-columns-level-face (n)
162 "Get the right face for match N in font-lock matching of healdines."
163 (setq org-l (- (match-end 2) (match-beginning 1) 1))
164 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
165 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-columns-level-faces))
166 (cond
167 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
168 ((eq n 2) org-f)
169 (t (if org-level-color-stars-only nil org-f))))
173 ;;; Column View
175 (defvar org-columns-overlays nil
176 "Holds the list of current column overlays.")
178 (defvar org-columns-current-fmt nil
179 "Local variable, holds the currently active column format.")
180 (make-variable-buffer-local 'org-columns-current-fmt)
181 (defvar org-columns-current-fmt-compiled nil
182 "Local variable, holds the currently active column format.
183 This is the compiled version of the format.")
184 (make-variable-buffer-local 'org-columns-current-fmt-compiled)
185 (defvar org-columns-current-widths nil
186 "Loval variable, holds the currently widths of fields.")
187 (make-variable-buffer-local 'org-columns-current-widths)
188 (defvar org-columns-current-maxwidths nil
189 "Loval variable, holds the currently active maximum column widths.")
190 (make-variable-buffer-local 'org-columns-current-maxwidths)
191 (defvar org-columns-begin-marker (make-marker)
192 "Points to the position where last a column creation command was called.")
193 (defvar org-columns-top-level-marker (make-marker)
194 "Points to the position where current columns region starts.")
196 (defvar org-columns-map (make-sparse-keymap)
197 "The keymap valid in column display.")
199 (defun org-columns-content ()
200 "Switch to contents view while in columns view."
201 (interactive)
202 (org-overview)
203 (org-content))
205 (org-defkey org-columns-map "c" 'org-columns-content)
206 (org-defkey org-columns-map "o" 'org-overview)
207 (org-defkey org-columns-map "e" 'org-columns-edit-value)
208 (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
209 (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
210 (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
211 (org-defkey org-columns-map "v" 'org-columns-show-value)
212 (org-defkey org-columns-map "q" 'org-columns-quit)
213 (org-defkey org-columns-map "r" 'org-columns-redo)
214 (org-defkey org-columns-map "g" 'org-columns-redo)
215 (org-defkey org-columns-map [left] 'org-columns-backward-char)
216 (org-defkey org-columns-map "\M-b" 'org-columns-backward-char)
217 (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
218 (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
219 (org-defkey org-columns-map "\M-f" 'org-columns-forward-char)
220 (org-defkey org-columns-map [right] 'org-columns-forward-char)
221 (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
222 (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
223 (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
224 (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
225 (org-defkey org-columns-map "<" 'org-columns-narrow)
226 (org-defkey org-columns-map ">" 'org-columns-widen)
227 (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
228 (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
229 (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
230 (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
231 (dotimes (i 10)
232 (org-defkey org-columns-map (number-to-string i)
233 `(lambda () (interactive)
234 (org-columns-next-allowed-value nil ,i))))
236 (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
237 '("Column"
238 ["Edit property" org-columns-edit-value t]
239 ["Next allowed value" org-columns-next-allowed-value t]
240 ["Previous allowed value" org-columns-previous-allowed-value t]
241 ["Show full value" org-columns-show-value t]
242 ["Edit allowed values" org-columns-edit-allowed t]
243 "--"
244 ["Edit column attributes" org-columns-edit-attributes t]
245 ["Increase column width" org-columns-widen t]
246 ["Decrease column width" org-columns-narrow t]
247 "--"
248 ["Move column right" org-columns-move-right t]
249 ["Move column left" org-columns-move-left t]
250 ["Add column" org-columns-new t]
251 ["Delete column" org-columns-delete t]
252 "--"
253 ["CONTENTS" org-columns-content t]
254 ["OVERVIEW" org-overview t]
255 ["Refresh columns display" org-columns-redo t]
256 "--"
257 ["Open link" org-columns-open-link t]
258 "--"
259 ["Quit" org-columns-quit t]))
261 (defun org-columns-current-column ()
262 (if (featurep 'xemacs)
263 (/ (current-column) 2)
264 (current-column)))
266 (defun org-columns-forward-char ()
267 (interactive)
268 (forward-char)
269 (if (featurep 'xemacs)
270 (while (not (or (eolp)
271 (member (extent-at
272 (point) (current-buffer)
273 'org-columns-key) org-columns-overlays)))
274 (forward-char))))
276 (defun org-columns-backward-char ()
277 (interactive)
278 (backward-char)
279 (if (featurep 'xemacs)
280 (while (not (or (bolp)
281 (member (extent-at (point) (current-buffer) 'org-columns-key) org-columns-overlays)))
282 (backward-char))))
284 (defun org-columns-new-overlay (beg end &optional string face)
285 "Create a new column overlay and add it to the list."
286 (let ((ov (org-make-overlay beg end)))
287 (if (featurep 'xemacs)
288 (progn
289 (org-overlay-put ov 'face (or face 'org-columns-space1))
290 (org-overlay-put ov 'start-open t)
291 (if string
292 (org-overlay-display ov string (or face 'org-columns-space1))))
293 (org-overlay-put ov 'face (or face 'secondary-selection))
294 (org-overlay-display ov string face))
295 (push ov org-columns-overlays)
296 ov))
298 (defun org-columns-display-here (&optional props)
299 "Overlay the current line with column display."
300 (interactive)
301 (let* ((fmt org-columns-current-fmt-compiled)
302 (beg (point-at-bol))
303 (level-face (save-excursion
304 (beginning-of-line 1)
305 (and (looking-at "\\(\\**\\)\\(\\* \\)")
306 (org-get-level-face 2))))
307 (item (save-match-data
308 (org-no-properties
309 (org-remove-tabs
310 (buffer-substring-no-properties
311 (point-at-bol) (point-at-eol))))))
312 (color (if (featurep 'xemacs)
313 (save-excursion
314 (beginning-of-line 1)
315 (and (looking-at "\\(\\**\\)\\(\\* \\)")
316 (org-get-columns-level-face 2)))
317 (list :foreground
318 (face-attribute
319 (or level-face
320 (and (eq major-mode 'org-agenda-mode)
321 (get-text-property (point-at-bol) 'face))
322 'default) :foreground))))
323 (face (if (featurep 'xemacs) color (list color 'org-column)))
324 (pl (or (get-text-property (point-at-bol) 'prefix-length) 0))
325 (cphr (get-text-property (point-at-bol) 'org-complex-heading-regexp))
326 pom property ass width f string ov column val modval s1 s2)
327 ;; Check if the entry is in another buffer.
328 (unless props
329 (if (eq major-mode 'org-agenda-mode)
330 (setq pom (or (get-text-property (point) 'org-hd-marker)
331 (get-text-property (point) 'org-marker))
332 props (if pom (org-entry-properties pom) nil))
333 (setq props (org-entry-properties nil))))
334 ;; Walk the format
335 (while (setq column (pop fmt))
336 (setq property (car column)
337 ass (if (equal property "ITEM")
338 (cons "ITEM" item)
339 (assoc property props))
340 width (or (cdr (assoc property org-columns-current-maxwidths))
341 (nth 2 column)
342 (length property))
343 f (format (if (featurep 'xemacs) "%%-%d.%ds |" "%%-%d.%ds | ")
344 width width)
345 val (or (cdr ass) "")
346 modval (if (equal property "ITEM")
347 (if (org-mode-p)
348 (org-columns-cleanup-item
349 val org-columns-current-fmt-compiled)
350 (org-agenda-columns-cleanup-item
351 val pl cphr org-columns-current-fmt-compiled))))
352 (setq s2 (org-columns-add-ellipses (or modval val) width))
353 (setq string (format f s2))
354 ;; Create the overlay
355 (org-unmodified
356 (setq ov (org-columns-new-overlay
357 beg (setq beg (1+ beg)) string face))
358 (org-overlay-put ov 'keymap org-columns-map)
359 (org-overlay-put ov 'org-columns-key property)
360 (org-overlay-put ov 'org-columns-value (cdr ass))
361 (org-overlay-put ov 'org-columns-value-modified modval)
362 (org-overlay-put ov 'org-columns-pom pom)
363 (org-overlay-put ov 'org-columns-format f)
364 (when (featurep 'xemacs)
365 (if (or (not (char-after beg))
366 (equal (char-after beg) ?\n))
367 (let ((inhibit-read-only t))
368 (save-excursion
369 (goto-char beg)
370 (org-unmodified (insert " "))
371 ;; FIXME: add props and remove later?
373 (goto-char beg)
374 (org-columns-new-overlay
375 beg (1+ beg) nil 'org-columns-space)
376 (setq beg (1+ beg))))
378 (if (or (not (char-after beg))
379 (equal (char-after beg) ?\n))
380 (let ((inhibit-read-only t))
381 (save-excursion
382 (goto-char beg)
383 ;; FIXME: add props and remove later?
384 (org-unmodified (insert " "))))))
385 ;; Make the rest of the line disappear.
386 (org-unmodified
387 (setq ov (org-columns-new-overlay beg (point-at-eol)))
388 (org-overlay-put ov 'invisible t)
389 (org-overlay-put ov 'keymap org-columns-map)
390 (org-overlay-put ov 'intangible t)
391 (push ov org-columns-overlays)
392 (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
393 (org-overlay-put ov 'keymap org-columns-map)
394 (push ov org-columns-overlays)
395 (let ((inhibit-read-only t))
396 (put-text-property (max (point-min) (1- (point-at-bol)))
397 (min (point-max) (1+ (point-at-eol)))
398 'read-only "Type `e' to edit property")))))
400 (defun org-columns-add-ellipses (string width)
401 "Truncate STRING with WIDTH characters, with ellipses."
402 (cond
403 ((<= (length string) width) string)
404 ((<= width (length org-columns-ellipses))
405 (substring org-columns-ellipses 0 width))
406 (t (concat (substring string 0 (- width (length org-columns-ellipses)))
407 org-columns-ellipses))))
409 (defvar org-columns-full-header-line-format nil
410 "Fthe full header line format, will be shifted by horizontal scrolling." )
411 (defvar org-previous-header-line-format nil
412 "The header line format before column view was turned on.")
413 (defvar org-columns-inhibit-recalculation nil
414 "Inhibit recomputing of columns on column view startup.")
417 (defvar header-line-format)
418 (defvar org-columns-previous-hscroll 0)
419 (defun org-columns-display-here-title ()
420 "Overlay the newline before the current line with the table title."
421 (interactive)
422 (let ((fmt org-columns-current-fmt-compiled)
423 string (title "")
424 property width f column str widths)
425 (while (setq column (pop fmt))
426 (setq property (car column)
427 str (or (nth 1 column) property)
428 width (or (cdr (assoc property org-columns-current-maxwidths))
429 (nth 2 column)
430 (length str))
431 widths (push width widths)
432 f (format "%%-%d.%ds | " width width)
433 string (format f str)
434 title (concat title string)))
435 (if (featurep 'xemacs)
436 (let ((ext (make-extent nil nil)))
437 (set-extent-endpoints ext 0 (length title) title)
438 (set-extent-face ext (list 'bold 'underline 'org-columns-space1))
439 (org-set-local 'org-previous-header-line-format
440 (specifier-specs top-gutter))
441 (org-set-local 'org-columns-current-widths (nreverse widths))
442 (set-specifier top-gutter (make-gutter-specifier
443 (cons (current-buffer) title))))
444 (setq title (concat
445 (org-add-props " " nil 'display '(space :align-to 0))
446 (org-add-props title nil 'face '(:weight bold :underline t))))
447 (org-set-local 'org-previous-header-line-format header-line-format)
448 (org-set-local 'org-columns-current-widths (nreverse widths))
449 (setq org-columns-full-header-line-format title)
450 (setq org-columns-previous-hscroll -1)
451 (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local))))
453 (defun org-columns-hscoll-title ()
454 "Set the header-line-format so that it scrolls along with the table."
455 (sit-for .0001) ; need to force a redisplay to update window-hscroll
456 (when (not (= (window-hscroll) org-columns-previous-hscroll))
457 (setq header-line-format
458 (concat (substring org-columns-full-header-line-format 0 1)
459 (substring org-columns-full-header-line-format
460 (1+ (window-hscroll))))
461 org-columns-previous-hscroll (window-hscroll))
462 (force-mode-line-update)))
464 (defun org-columns-remove-overlays ()
465 "Remove all currently active column overlays."
466 (interactive)
467 (when (marker-buffer org-columns-begin-marker)
468 (with-current-buffer (marker-buffer org-columns-begin-marker)
469 (when (local-variable-p 'org-previous-header-line-format (current-buffer))
470 (if (featurep 'xemacs)
471 (set-specifier top-gutter
472 (make-gutter-specifier
473 (cons (current-buffer)
474 (cdar org-previous-header-line-format))))
475 (setq header-line-format org-previous-header-line-format)
476 (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
477 (kill-local-variable 'org-previous-header-line-format))
478 (move-marker org-columns-begin-marker nil)
479 (move-marker org-columns-top-level-marker nil)
480 (org-unmodified
481 (mapc 'org-delete-overlay org-columns-overlays)
482 (setq org-columns-overlays nil)
483 (let ((inhibit-read-only t))
484 (remove-text-properties (point-min) (point-max) '(read-only t)))))))
486 (defun org-columns-cleanup-item (item fmt)
487 "Remove from ITEM what is a column in the format FMT."
488 (if (not org-complex-heading-regexp)
489 item
490 (when (string-match org-complex-heading-regexp item)
491 (setq item
492 (concat
493 (org-add-props (match-string 1 item) nil
494 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
495 (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
496 (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
497 " " (save-match-data (org-columns-compact-links (match-string 4 item)))
498 (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))
499 (add-text-properties
500 0 (1+ (match-end 1))
501 (list 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
502 item)
503 item)))
505 (defun org-columns-compact-links (s)
506 "Replace [[link][desc]] with [desc] or [link]."
507 (while (string-match org-bracket-link-regexp s)
508 (setq s (replace-match
509 (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
510 t t s)))
513 (defvar org-agenda-columns-remove-prefix-from-item)
514 (defun org-agenda-columns-cleanup-item (item pl cphr fmt)
515 "Cleanup the tiem property for agenda column view.
516 See also the variable `org-agenda-columns-remove-prefix-from-item'."
517 (let* ((org-complex-heading-regexp cphr)
518 (prefix (substring item 0 pl))
519 (rest (substring item pl))
520 (fake (concat "* " rest))
521 (cleaned (org-trim (substring (org-columns-cleanup-item fake fmt) 1))))
522 (if org-agenda-columns-remove-prefix-from-item
523 cleaned
524 (concat prefix cleaned))))
526 (defun org-columns-show-value ()
527 "Show the full value of the property."
528 (interactive)
529 (let ((value (get-char-property (point) 'org-columns-value)))
530 (message "Value is: %s" (or value ""))))
532 (defvar org-agenda-columns-active) ;; defined in org-agenda.el
533 (defun org-columns-quit ()
534 "Remove the column overlays and in this way exit column editing."
535 (interactive)
536 (org-unmodified
537 (org-columns-remove-overlays)
538 (let ((inhibit-read-only t))
539 (remove-text-properties (point-min) (point-max) '(read-only t))))
540 (when (eq major-mode 'org-agenda-mode)
541 (setq org-agenda-columns-active nil)
542 (message
543 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
545 (defun org-columns-check-computed ()
546 "Check if this column value is computed.
547 If yes, throw an error indicating that changing it does not make sense."
548 (let ((val (get-char-property (point) 'org-columns-value)))
549 (when (and (stringp val)
550 (get-char-property 0 'org-computed val))
551 (error "This value is computed from the entry's children"))))
553 (defun org-columns-todo (&optional arg)
554 "Change the TODO state during column view."
555 (interactive "P")
556 (org-columns-edit-value "TODO"))
558 (defun org-columns-set-tags-or-toggle (&optional arg)
559 "Toggle checkbox at point, or set tags for current headline."
560 (interactive "P")
561 (if (string-match "\\`\\[[ xX-]\\]\\'"
562 (get-char-property (point) 'org-columns-value))
563 (org-columns-next-allowed-value)
564 (org-columns-edit-value "TAGS")))
566 (defun org-columns-edit-value (&optional key)
567 "Edit the value of the property at point in column view.
568 Where possible, use the standard interface for changing this line."
569 (interactive)
570 (org-columns-check-computed)
571 (let* ((external-key key)
572 (col (current-column))
573 (key (or key (get-char-property (point) 'org-columns-key)))
574 (value (get-char-property (point) 'org-columns-value))
575 (bol (point-at-bol)) (eol (point-at-eol))
576 (pom (or (get-text-property bol 'org-hd-marker)
577 (point))) ; keep despite of compiler warning
578 (line-overlays
579 (delq nil (mapcar (lambda (x)
580 (and (eq (org-overlay-buffer x) (current-buffer))
581 (>= (org-overlay-start x) bol)
582 (<= (org-overlay-start x) eol)
584 org-columns-overlays)))
585 nval eval allowed)
586 (cond
587 ((equal key "CLOCKSUM")
588 (error "This special column cannot be edited"))
589 ((equal key "ITEM")
590 (setq eval '(org-with-point-at pom (org-edit-headline))))
591 ((equal key "TODO")
592 (setq eval '(org-with-point-at
594 (let ((current-prefix-arg
595 (if external-key current-prefix-arg '(4))))
596 (call-interactively 'org-todo)))))
597 ((equal key "PRIORITY")
598 (setq eval '(org-with-point-at pom
599 (call-interactively 'org-priority))))
600 ((equal key "TAGS")
601 (setq eval '(org-with-point-at
603 (let ((org-fast-tag-selection-single-key
604 (if (eq org-fast-tag-selection-single-key 'expert)
605 t org-fast-tag-selection-single-key)))
606 (call-interactively 'org-set-tags)))))
607 ((equal key "DEADLINE")
608 (setq eval '(org-with-point-at
610 (call-interactively 'org-deadline))))
611 ((equal key "SCHEDULED")
612 (setq eval '(org-with-point-at
614 (call-interactively 'org-schedule))))
616 (setq allowed (org-property-get-allowed-values pom key 'table))
617 (if allowed
618 (setq nval (completing-read "Value: " allowed nil t))
619 (setq nval (read-string "Edit: " value)))
620 (setq nval (org-trim nval))
621 (when (not (equal nval value))
622 (setq eval '(org-entry-put pom key nval)))))
623 (when eval
625 (cond
626 ((equal major-mode 'org-agenda-mode)
627 (org-columns-eval eval)
628 ;; The following let preserves the current format, and makes sure
629 ;; that in only a single file things need to be upated.
630 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
631 (buffer (marker-buffer pom))
632 (org-agenda-contributing-files
633 (list (with-current-buffer buffer
634 (buffer-file-name (buffer-base-buffer))))))
635 (org-agenda-columns)))
637 (let ((inhibit-read-only t))
638 (org-unmodified
639 (remove-text-properties
640 (max (point-min) (1- bol)) eol '(read-only t)))
641 (unwind-protect
642 (progn
643 (setq org-columns-overlays
644 (org-delete-all line-overlays org-columns-overlays))
645 (mapc 'org-delete-overlay line-overlays)
646 (org-columns-eval eval))
647 (org-columns-display-here)))
648 (org-move-to-column col)
649 (if (and (org-mode-p)
650 (nth 3 (assoc key org-columns-current-fmt-compiled)))
651 (org-columns-update key)))))))
653 (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
654 "Edit the current headline, the part without TODO keyword, TAGS."
655 (org-back-to-heading)
656 (when (looking-at org-todo-line-regexp)
657 (let ((pos (point))
658 (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
659 (txt (match-string 3))
660 (post "")
661 txt2)
662 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
663 (setq post (match-string 0 txt)
664 txt (substring txt 0 (match-beginning 0))))
665 (setq txt2 (read-string "Edit: " txt))
666 (when (not (equal txt txt2))
667 (goto-char pos)
668 (insert pre txt2 post)
669 (delete-region (point) (point-at-eol))
670 (org-set-tags nil t)))))
672 (defun org-columns-edit-allowed ()
673 "Edit the list of allowed values for the current property."
674 (interactive)
675 (let* ((pom (or (get-text-property (point-at-bol) 'org-marker)
676 (get-text-property (point-at-bol) 'org-hd-marker)
677 (point)))
678 (key (get-char-property (point) 'org-columns-key))
679 (key1 (concat key "_ALL"))
680 (allowed (org-entry-get pom key1 t))
681 nval)
682 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
683 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
684 (setq nval (read-string "Allowed: " allowed))
685 (org-entry-put
686 (cond ((marker-position org-entry-property-inherited-from)
687 org-entry-property-inherited-from)
688 ((marker-position org-columns-top-level-marker)
689 org-columns-top-level-marker)
690 (t pom))
691 key1 nval)))
693 (defun org-columns-eval (form)
694 (let (hidep)
695 (save-excursion
696 (beginning-of-line 1)
697 ;; `next-line' is needed here, because it skips invisible line.
698 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
699 (setq hidep (org-on-heading-p 1)))
700 (eval form)
701 (and hidep (hide-entry))))
703 (defun org-columns-previous-allowed-value ()
704 "Switch to the previous allowed value for this column."
705 (interactive)
706 (org-columns-next-allowed-value t))
708 (defun org-columns-next-allowed-value (&optional previous nth)
709 "Switch to the next allowed value for this column.
710 When PREVIOUS is set, go to the previous value. When NTH is
711 an integer, select that value."
712 (interactive)
713 (org-columns-check-computed)
714 (let* ((col (current-column))
715 (key (get-char-property (point) 'org-columns-key))
716 (value (get-char-property (point) 'org-columns-value))
717 (bol (point-at-bol)) (eol (point-at-eol))
718 (pom (or (get-text-property bol 'org-hd-marker)
719 (point))) ; keep despite of compiler waring
720 (line-overlays
721 (delq nil (mapcar (lambda (x)
722 (and (eq (org-overlay-buffer x) (current-buffer))
723 (>= (org-overlay-start x) bol)
724 (<= (org-overlay-start x) eol)
726 org-columns-overlays)))
727 (allowed (or (org-property-get-allowed-values pom key)
728 (and (memq
729 (nth 4 (assoc key org-columns-current-fmt-compiled))
730 '(checkbox checkbox-n-of-m checkbox-percent))
731 '("[ ]" "[X]"))))
732 nval)
733 (when (integerp nth)
734 (setq nth (1- nth))
735 (if (= nth -1) (setq nth 9)))
736 (when (equal key "ITEM")
737 (error "Cannot edit item headline from here"))
738 (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
739 (error "Allowed values for this property have not been defined"))
740 (if (member key '("SCHEDULED" "DEADLINE"))
741 (setq nval (if previous 'earlier 'later))
742 (if previous (setq allowed (reverse allowed)))
743 (cond
744 (nth
745 (setq nval (nth nth allowed))
746 (if (not nval)
747 (error "There are only %d allowed values for property `%s'"
748 (length allowed) key)))
749 ((member value allowed)
750 (setq nval (or (car (cdr (member value allowed)))
751 (car allowed)))
752 (if (equal nval value)
753 (error "Only one allowed value for this property")))
754 (t (setq nval (car allowed)))))
755 (cond
756 ((equal major-mode 'org-agenda-mode)
757 (org-columns-eval '(org-entry-put pom key nval))
758 ;; The following let preserves the current format, and makes sure
759 ;; that in only a single file things need to be upated.
760 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
761 (buffer (marker-buffer pom))
762 (org-agenda-contributing-files
763 (list (with-current-buffer buffer
764 (buffer-file-name (buffer-base-buffer))))))
765 (org-agenda-columns)))
767 (let ((inhibit-read-only t))
768 (remove-text-properties (1- bol) eol '(read-only t))
769 (unwind-protect
770 (progn
771 (setq org-columns-overlays
772 (org-delete-all line-overlays org-columns-overlays))
773 (mapc 'org-delete-overlay line-overlays)
774 (org-columns-eval '(org-entry-put pom key nval)))
775 (org-columns-display-here)))
776 (org-move-to-column col)
777 (and (nth 3 (assoc key org-columns-current-fmt-compiled))
778 (org-columns-update key))))))
780 (defun org-verify-version (task)
781 (cond
782 ((eq task 'columns)
783 (if (or (and (featurep 'xemacs) (not (featurep 'org-colview-xemacs)))
784 (and (not (featurep 'xemacs)) (< emacs-major-version 22)))
785 (error "This version of Emacs cannot run Column View")))))
787 (defun org-columns-open-link (&optional arg)
788 (interactive "P")
789 (let ((value (get-char-property (point) 'org-columns-value)))
790 (org-open-link-from-string value arg)))
792 (defun org-columns-get-format-and-top-level ()
793 (let (fmt)
794 (when (condition-case nil (org-back-to-heading) (error nil))
795 (move-marker org-entry-property-inherited-from nil)
796 (setq fmt (org-entry-get nil "COLUMNS" t)))
797 (setq fmt (or fmt org-columns-default-format))
798 (org-set-local 'org-columns-current-fmt fmt)
799 (org-columns-compile-format fmt)
800 (if (marker-position org-entry-property-inherited-from)
801 (move-marker org-columns-top-level-marker
802 org-entry-property-inherited-from)
803 (move-marker org-columns-top-level-marker (point)))
804 fmt))
806 (defun org-columns ()
807 "Turn on column view on an org-mode file."
808 (interactive)
809 (org-verify-version 'columns)
810 (when (featurep 'xemacs)
811 (set-face-foreground 'org-columns-space
812 (face-background 'org-columns-space)))
813 (org-columns-remove-overlays)
814 (move-marker org-columns-begin-marker (point))
815 (let (beg end fmt cache maxwidths)
816 (setq fmt (org-columns-get-format-and-top-level))
817 (save-excursion
818 (goto-char org-columns-top-level-marker)
819 (setq beg (point))
820 (unless org-columns-inhibit-recalculation
821 (org-columns-compute-all))
822 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
823 (point-max)))
824 ;; Get and cache the properties
825 (goto-char beg)
826 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
827 (save-excursion
828 (save-restriction
829 (narrow-to-region beg end)
830 (org-clock-sum))))
831 (while (re-search-forward (concat "^" outline-regexp) end t)
832 (push (cons (org-current-line) (org-entry-properties)) cache))
833 (when cache
834 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
835 (org-set-local 'org-columns-current-maxwidths maxwidths)
836 (org-columns-display-here-title)
837 (mapc (lambda (x)
838 (goto-line (car x))
839 (org-columns-display-here (cdr x)))
840 cache)))))
842 (defun org-columns-new (&optional prop title width op fmt &rest rest)
843 "Insert a new column, to the left of the current column."
844 (interactive)
845 (let ((n (org-columns-current-column))
846 (editp (and prop (assoc prop org-columns-current-fmt-compiled)))
847 cell)
848 (setq prop (completing-read
849 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
850 nil nil prop))
851 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
852 (setq width (read-string "Column width: " (if width (number-to-string width))))
853 (if (string-match "\\S-" width)
854 (setq width (string-to-number width))
855 (setq width nil))
856 (setq fmt (completing-read "Summary [none]: "
857 '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
858 nil t))
859 (if (string-match "\\S-" fmt)
860 (setq fmt (intern fmt))
861 (setq fmt nil))
862 (if (eq fmt 'none) (setq fmt nil))
863 (if editp
864 (progn
865 (setcar editp prop)
866 (setcdr editp (list title width nil fmt)))
867 (setq cell (nthcdr (1- n) org-columns-current-fmt-compiled))
868 (setcdr cell (cons (list prop title width nil fmt)
869 (cdr cell))))
870 (org-columns-store-format)
871 (org-columns-redo)))
873 (defun org-columns-delete ()
874 "Delete the column at point from columns view."
875 (interactive)
876 (let* ((n (org-columns-current-column))
877 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
878 (when (y-or-n-p
879 (format "Are you sure you want to remove column \"%s\"? " title))
880 (setq org-columns-current-fmt-compiled
881 (delq (nth n org-columns-current-fmt-compiled)
882 org-columns-current-fmt-compiled))
883 (org-columns-store-format)
884 (org-columns-redo)
885 (if (>= (org-columns-current-column)
886 (length org-columns-current-fmt-compiled))
887 (org-columns-backward-char)))))
889 (defun org-columns-edit-attributes ()
890 "Edit the attributes of the current column."
891 (interactive)
892 (let* ((n (org-columns-current-column))
893 (info (nth n org-columns-current-fmt-compiled)))
894 (apply 'org-columns-new info)))
896 (defun org-columns-widen (arg)
897 "Make the column wider by ARG characters."
898 (interactive "p")
899 (let* ((n (org-columns-current-column))
900 (entry (nth n org-columns-current-fmt-compiled))
901 (width (or (nth 2 entry)
902 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
903 (setq width (max 1 (+ width arg)))
904 (setcar (nthcdr 2 entry) width)
905 (org-columns-store-format)
906 (org-columns-redo)))
908 (defun org-columns-narrow (arg)
909 "Make the column nrrower by ARG characters."
910 (interactive "p")
911 (org-columns-widen (- arg)))
913 (defun org-columns-move-right ()
914 "Swap this column with the one to the right."
915 (interactive)
916 (let* ((n (org-columns-current-column))
917 (cell (nthcdr n org-columns-current-fmt-compiled))
919 (when (>= n (1- (length org-columns-current-fmt-compiled)))
920 (error "Cannot shift this column further to the right"))
921 (setq e (car cell))
922 (setcar cell (car (cdr cell)))
923 (setcdr cell (cons e (cdr (cdr cell))))
924 (org-columns-store-format)
925 (org-columns-redo)
926 (org-columns-forward-char)))
928 (defun org-columns-move-left ()
929 "Swap this column with the one to the left."
930 (interactive)
931 (let* ((n (org-columns-current-column)))
932 (when (= n 0)
933 (error "Cannot shift this column further to the left"))
934 (org-columns-backward-char)
935 (org-columns-move-right)
936 (org-columns-backward-char)))
938 (defun org-columns-store-format ()
939 "Store the text version of the current columns format in appropriate place.
940 This is either in the COLUMNS property of the node starting the current column
941 display, or in the #+COLUMNS line of the current buffer."
942 (let (fmt (cnt 0))
943 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
944 (org-set-local 'org-columns-current-fmt fmt)
945 (if (marker-position org-columns-top-level-marker)
946 (save-excursion
947 (goto-char org-columns-top-level-marker)
948 (if (and (org-at-heading-p)
949 (org-entry-get nil "COLUMNS"))
950 (org-entry-put nil "COLUMNS" fmt)
951 (goto-char (point-min))
952 ;; Overwrite all #+COLUMNS lines....
953 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
954 (setq cnt (1+ cnt))
955 (replace-match (concat "#+COLUMNS: " fmt) t t))
956 (unless (> cnt 0)
957 (goto-char (point-min))
958 (or (org-on-heading-p t) (outline-next-heading))
959 (let ((inhibit-read-only t))
960 (insert-before-markers "#+COLUMNS: " fmt "\n")))
961 (org-set-local 'org-columns-default-format fmt))))))
963 (defvar org-agenda-overriding-columns-format nil
964 "When set, overrides any other format definition for the agenda.
965 Don't set this, this is meant for dynamic scoping.")
967 (defun org-columns-get-autowidth-alist (s cache)
968 "Derive the maximum column widths from the format and the cache."
969 (let ((start 0) rtn)
970 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
971 (push (cons (match-string 1 s) 1) rtn)
972 (setq start (match-end 0)))
973 (mapc (lambda (x)
974 (setcdr x (apply 'max
975 (mapcar
976 (lambda (y)
977 (length (or (cdr (assoc (car x) (cdr y))) " ")))
978 cache))))
979 rtn)
980 rtn))
982 (defun org-columns-compute-all ()
983 "Compute all columns that have operators defined."
984 (org-unmodified
985 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
986 (let ((columns org-columns-current-fmt-compiled) col)
987 (while (setq col (pop columns))
988 (when (nth 3 col)
989 (save-excursion
990 (org-columns-compute (car col)))))))
992 (defun org-columns-update (property)
993 "Recompute PROPERTY, and update the columns display for it."
994 (org-columns-compute property)
995 (let (fmt val pos face)
996 (save-excursion
997 (mapc (lambda (ov)
998 (when (equal (org-overlay-get ov 'org-columns-key) property)
999 (setq pos (org-overlay-start ov))
1000 (goto-char pos)
1001 (when (setq val (cdr (assoc property
1002 (get-text-property
1003 (point-at-bol) 'org-summaries))))
1004 (setq fmt (org-overlay-get ov 'org-columns-format))
1005 (org-overlay-put ov 'org-columns-value val)
1006 (if (featurep 'xemacs)
1007 (progn
1008 (setq face (glyph-face (extent-end-glyph ov)))
1009 (org-overlay-display ov (format fmt val) face))
1010 (org-overlay-display ov (format fmt val))))))
1011 org-columns-overlays))))
1013 (defun org-columns-compute (property)
1014 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
1015 (interactive)
1016 (let* ((re (concat "^" outline-regexp))
1017 (lmax 30) ; Does anyone use deeper levels???
1018 (lsum (make-vector lmax 0))
1019 (lflag (make-vector lmax nil))
1020 (level 0)
1021 (ass (assoc property org-columns-current-fmt-compiled))
1022 (format (nth 4 ass))
1023 (printf (nth 5 ass))
1024 (beg org-columns-top-level-marker)
1025 last-level val valflag flag end sumpos sum-alist sum str str1 useval)
1026 (save-excursion
1027 ;; Find the region to compute
1028 (goto-char beg)
1029 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
1030 (goto-char end)
1031 ;; Walk the tree from the back and do the computations
1032 (while (re-search-backward re beg t)
1033 (setq sumpos (match-beginning 0)
1034 last-level level
1035 level (org-outline-level)
1036 val (org-entry-get nil property)
1037 valflag (and val (string-match "\\S-" val)))
1038 (cond
1039 ((< level last-level)
1040 ;; put the sum of lower levels here as a property
1041 (setq sum (aref lsum last-level) ; current sum
1042 flag (aref lflag last-level) ; any valid entries from children?
1043 str (org-columns-number-to-string sum format printf)
1044 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
1045 useval (if flag str1 (if valflag val ""))
1046 sum-alist (get-text-property sumpos 'org-summaries))
1047 (if (assoc property sum-alist)
1048 (setcdr (assoc property sum-alist) useval)
1049 (push (cons property useval) sum-alist)
1050 (org-unmodified
1051 (add-text-properties sumpos (1+ sumpos)
1052 (list 'org-summaries sum-alist))))
1053 (when (and val (not (equal val (if flag str val))))
1054 (org-entry-put nil property (if flag str val)))
1055 ;; add current to current level accumulator
1056 (when (or flag valflag)
1057 (aset lsum level (+ (aref lsum level)
1058 (if flag sum (org-column-string-to-number
1059 (if flag str val) format))))
1060 (aset lflag level t))
1061 ;; clear accumulators for deeper levels
1062 (loop for l from (1+ level) to (1- lmax) do
1063 (aset lsum l 0)
1064 (aset lflag l nil)))
1065 ((>= level last-level)
1066 ;; add what we have here to the accumulator for this level
1067 (aset lsum level (+ (aref lsum level)
1068 (org-column-string-to-number (or val "0") format)))
1069 (and valflag (aset lflag level t)))
1070 (t (error "This should not happen")))))))
1072 (defun org-columns-redo ()
1073 "Construct the column display again."
1074 (interactive)
1075 (message "Recomputing columns...")
1076 (save-excursion
1077 (if (marker-position org-columns-begin-marker)
1078 (goto-char org-columns-begin-marker))
1079 (org-columns-remove-overlays)
1080 (if (org-mode-p)
1081 (call-interactively 'org-columns)
1082 (org-agenda-redo)
1083 (call-interactively 'org-agenda-columns)))
1084 (when (featurep 'xemacs)
1085 (while (not (or (eolp)
1086 (member (extent-at (point)) org-columns-overlays)))
1087 (forward-char)))
1088 (message "Recomputing columns...done"))
1090 (defun org-columns-not-in-agenda ()
1091 (if (eq major-mode 'org-agenda-mode)
1092 (error "This command is only allowed in Org-mode buffers")))
1095 (defun org-string-to-number (s)
1096 "Convert string to number, and interpret hh:mm:ss."
1097 (if (not (string-match ":" s))
1098 (string-to-number s)
1099 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1100 (while l
1101 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1102 sum)))
1104 (defun org-columns-number-to-string (n fmt &optional printf)
1105 "Convert a computed column number to a string value, according to FMT."
1106 (cond
1107 ((eq fmt 'add_times)
1108 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
1109 (format org-time-clocksum-format h m)))
1110 ((eq fmt 'checkbox)
1111 (cond ((= n (floor n)) "[X]")
1112 ((> n 1.) "[-]")
1113 (t "[ ]")))
1114 ((memq fmt '(checkbox-n-of-m checkbox-percent))
1115 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
1116 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
1117 (printf (format printf n))
1118 ((eq fmt 'currency)
1119 (format "%.2f" n))
1120 (t (number-to-string n))))
1122 (defun org-nofm-to-completion (n m &optional percent)
1123 (if (not percent)
1124 (format "[%d/%d]" n m)
1125 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
1127 (defun org-column-string-to-number (s fmt)
1128 "Convert a column value to a number that can be used for column computing."
1129 (cond
1130 ((string-match ":" s)
1131 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1132 (while l
1133 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1134 sum))
1135 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
1136 (if (equal s "[X]") 1. 0.000001))
1137 (t (string-to-number s))))
1139 (defun org-columns-uncompile-format (cfmt)
1140 "Turn the compiled columns format back into a string representation."
1141 (let ((rtn "") e s prop title op width fmt printf)
1142 (while (setq e (pop cfmt))
1143 (setq prop (car e)
1144 title (nth 1 e)
1145 width (nth 2 e)
1146 op (nth 3 e)
1147 fmt (nth 4 e)
1148 printf (nth 5 e))
1149 (cond
1150 ((eq fmt 'add_times) (setq op ":"))
1151 ((eq fmt 'checkbox) (setq op "X"))
1152 ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
1153 ((eq fmt 'checkbox-percent) (setq op "X%"))
1154 ((eq fmt 'add_numbers) (setq op "+"))
1155 ((eq fmt 'currency) (setq op "$")))
1156 (if (and op printf) (setq op (concat op ";" printf)))
1157 (if (equal title prop) (setq title nil))
1158 (setq s (concat "%" (if width (number-to-string width))
1159 prop
1160 (if title (concat "(" title ")"))
1161 (if op (concat "{" op "}"))))
1162 (setq rtn (concat rtn " " s)))
1163 (org-trim rtn)))
1165 (defun org-columns-compile-format (fmt)
1166 "Turn a column format string into an alist of specifications.
1167 The alist has one entry for each column in the format. The elements of
1168 that list are:
1169 property the property
1170 title the title field for the columns
1171 width the column width in characters, can be nil for automatic
1172 operator the operator if any
1173 format the output format for computed results, derived from operator
1174 printf a printf format for computed values"
1175 (let ((start 0) width prop title op f printf)
1176 (setq org-columns-current-fmt-compiled nil)
1177 (while (string-match
1178 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
1179 fmt start)
1180 (setq start (match-end 0)
1181 width (match-string 1 fmt)
1182 prop (match-string 2 fmt)
1183 title (or (match-string 3 fmt) prop)
1184 op (match-string 4 fmt)
1185 f nil
1186 printf nil)
1187 (if width (setq width (string-to-number width)))
1188 (when (and op (string-match ";" op))
1189 (setq printf (substring op (match-end 0))
1190 op (substring op 0 (match-beginning 0))))
1191 (cond
1192 ((equal op "+") (setq f 'add_numbers))
1193 ((equal op "$") (setq f 'currency))
1194 ((equal op ":") (setq f 'add_times))
1195 ((equal op "X") (setq f 'checkbox))
1196 ((equal op "X/") (setq f 'checkbox-n-of-m))
1197 ((equal op "X%") (setq f 'checkbox-percent))
1199 (push (list prop title width op f printf) org-columns-current-fmt-compiled))
1200 (setq org-columns-current-fmt-compiled
1201 (nreverse org-columns-current-fmt-compiled))))
1204 ;;; Dynamic block for Column view
1206 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
1207 "Get the column view of the current buffer or subtree.
1208 The first optional argument MAXLEVEL sets the level limit. A
1209 second optional argument SKIP-EMPTY-ROWS tells whether to skip
1210 empty rows, an empty row being one where all the column view
1211 specifiers except ITEM are empty. This function returns a list
1212 containing the title row and all other rows. Each row is a list
1213 of fields."
1214 (if (featurep 'xemacs)
1215 (save-excursion
1216 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1217 (n (length title)) row tbl)
1218 (goto-char (point-min))
1219 (while (and (re-search-forward "^\\(\\*+\\) " nil t)
1220 (or (null maxlevel)
1221 (>= maxlevel
1222 (if org-odd-levels-only
1223 (/ (1+ (length (match-string 1))) 2)
1224 (length (match-string 1))))))
1225 (when (get-char-property (match-beginning 0) 'org-columns-key)
1226 (goto-char (match-beginning 0))
1227 (setq row nil)
1228 (loop for i from 0 to (1- n) do
1229 (push (or (get-char-property (point)
1230 'org-columns-value-modified)
1231 (get-char-property (point) 'org-columns-value)
1233 row)
1234 (org-columns-forward-char))
1235 (setq row (nreverse row))
1236 (unless (and skip-empty-rows
1237 (eq 1 (length (delete "" (delete-dups row)))))
1238 (push row tbl))))
1239 (append (list title 'hline) (nreverse tbl))))
1240 (save-excursion
1241 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1242 (n (length title)) row tbl)
1243 (goto-char (point-min))
1244 (while (and (re-search-forward "^\\(\\*+\\) " nil t)
1245 (or (null maxlevel)
1246 (>= maxlevel
1247 (if org-odd-levels-only
1248 (/ (1+ (length (match-string 1))) 2)
1249 (length (match-string 1))))))
1250 (when (get-char-property (match-beginning 0) 'org-columns-key)
1251 (setq row nil)
1252 (loop for i from 0 to (1- n) do
1253 (push (or (get-char-property (+ (match-beginning 0) i)
1254 'org-columns-value-modified)
1255 (get-char-property (+ (match-beginning 0) i)
1256 'org-columns-value)
1258 row))
1259 (setq row (nreverse row))
1260 (unless (and skip-empty-rows
1261 (eq 1 (length (delete "" (delete-dups row)))))
1262 (push row tbl))))
1263 (append (list title 'hline) (nreverse tbl))))))
1265 (defun org-dblock-write:columnview (params)
1266 "Write the column view table.
1267 PARAMS is a property list of parameters:
1269 :width enforce same column widths with <N> specifiers.
1270 :id the :ID: property of the entry where the columns view
1271 should be built, as a string. When `local', call locally.
1272 When `global' call column view with the cursor at the beginning
1273 of the buffer (usually this means that the whole buffer switches
1274 to column view).
1275 :hlines When t, insert a hline before each item. When a number, insert
1276 a hline before each level <= that number.
1277 :vlines When t, make each column a colgroup to enforce vertical lines.
1278 :maxlevel When set to a number, don't capture headlines below this level.
1279 :skip-empty-rows
1280 When t, skip rows where all specifiers other than ITEM are empty."
1281 (let ((pos (move-marker (make-marker) (point)))
1282 (hlines (plist-get params :hlines))
1283 (vlines (plist-get params :vlines))
1284 (maxlevel (plist-get params :maxlevel))
1285 (skip-empty-rows (plist-get params :skip-empty-rows))
1286 tbl id idpos nfields tmp)
1287 (save-excursion
1288 (save-restriction
1289 (when (setq id (plist-get params :id))
1290 (cond ((not id) nil)
1291 ((eq id 'global) (goto-char (point-min)))
1292 ((eq id 'local) nil)
1293 ((setq idpos (org-find-entry-with-id id))
1294 (goto-char idpos))
1295 (t (error "Cannot find entry with :ID: %s" id))))
1296 (org-columns)
1297 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1298 (setq nfields (length (car tbl)))
1299 (org-columns-quit)))
1300 (goto-char pos)
1301 (move-marker pos nil)
1302 (when tbl
1303 (when (plist-get params :hlines)
1304 (setq tmp nil)
1305 (while tbl
1306 (if (eq (car tbl) 'hline)
1307 (push (pop tbl) tmp)
1308 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1309 (if (and (not (eq (car tmp) 'hline))
1310 (or (eq hlines t)
1311 (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
1312 (push 'hline tmp)))
1313 (push (pop tbl) tmp)))
1314 (setq tbl (nreverse tmp)))
1315 (when vlines
1316 (setq tbl (mapcar (lambda (x)
1317 (if (eq 'hline x) x (cons "" x)))
1318 tbl))
1319 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1320 (setq pos (point))
1321 (insert (org-listtable-to-string tbl))
1322 (when (plist-get params :width)
1323 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1324 org-columns-current-widths "|")))
1325 (goto-char pos)
1326 (org-table-align))))
1328 (defun org-listtable-to-string (tbl)
1329 "Convert a listtable TBL to a string that contains the Org-mode table.
1330 The table still need to be alligned. The resulting string has no leading
1331 and tailing newline characters."
1332 (mapconcat
1333 (lambda (x)
1334 (cond
1335 ((listp x)
1336 (concat "|" (mapconcat 'identity x "|") "|"))
1337 ((eq x 'hline) "|-|")
1338 (t (error "Garbage in listtable: %s" x))))
1339 tbl "\n"))
1341 (defun org-insert-columns-dblock ()
1342 "Create a dynamic block capturing a column view table."
1343 (interactive)
1344 (when (featurep 'xemacs) (org-columns-quit))
1345 (let ((defaults '(:name "columnview" :hlines 1))
1346 (id (completing-read
1347 "Capture columns (local, global, entry with :ID: property) [local]: "
1348 (append '(("global") ("local"))
1349 (mapcar 'list (org-property-values "ID"))))))
1350 (if (equal id "") (setq id 'local))
1351 (if (equal id "global") (setq id 'global))
1352 (setq defaults (append defaults (list :id id)))
1353 (org-create-dblock defaults)
1354 (org-update-dblock)))
1356 ;;; Column view in the agenda
1358 (defvar org-agenda-view-columns-initially nil
1359 "When set, switch to columns view immediately after creating the agenda.")
1361 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1362 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1363 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1365 (defun org-agenda-columns ()
1366 "Turn on or update column view in the agenda."
1367 (interactive)
1368 (org-verify-version 'columns)
1369 (org-columns-remove-overlays)
1370 (move-marker org-columns-begin-marker (point))
1371 (let (fmt cache maxwidths m p a d)
1372 (cond
1373 ((and (boundp 'org-agenda-overriding-columns-format)
1374 org-agenda-overriding-columns-format)
1375 (setq fmt org-agenda-overriding-columns-format)
1376 (org-set-local 'org-agenda-overriding-columns-format fmt))
1377 ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
1378 (setq fmt (or (org-entry-get m "COLUMNS" t)
1379 (with-current-buffer (marker-buffer m)
1380 org-columns-default-format))))
1381 ((and (boundp 'org-columns-current-fmt)
1382 (local-variable-p 'org-columns-current-fmt (current-buffer))
1383 org-columns-current-fmt)
1384 (setq fmt org-columns-current-fmt))
1385 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
1386 (setq m (get-text-property m 'org-hd-marker))
1387 (setq fmt (or (org-entry-get m "COLUMNS" t)
1388 (with-current-buffer (marker-buffer m)
1389 org-columns-default-format)))))
1390 (setq fmt (or fmt org-columns-default-format))
1391 (org-set-local 'org-columns-current-fmt fmt)
1392 (org-columns-compile-format fmt)
1393 (when org-agenda-columns-compute-summary-properties
1394 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1395 (save-excursion
1396 ;; Get and cache the properties
1397 (goto-char (point-min))
1398 (while (not (eobp))
1399 (when (setq m (or (get-text-property (point) 'org-hd-marker)
1400 (get-text-property (point) 'org-marker)))
1401 (setq p (org-entry-properties m))
1403 (when (or (not (setq a (assoc org-effort-property p)))
1404 (not (string-match "\\S-" (or (cdr a) ""))))
1405 ;; OK, the property is not defined. Use appointment duration?
1406 (when (and org-agenda-columns-add-appointments-to-effort-sum
1407 (setq d (get-text-property (point) 'duration)))
1408 (setq d (org-minutes-to-hh:mm-string d))
1409 (put-text-property 0 (length d) 'face 'org-warning d)
1410 (push (cons org-effort-property d) p)))
1411 (push (cons (org-current-line) p) cache))
1412 (beginning-of-line 2))
1413 (when cache
1414 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
1415 (org-set-local 'org-columns-current-maxwidths maxwidths)
1416 (org-columns-display-here-title)
1417 (mapc (lambda (x)
1418 (goto-line (car x))
1419 (org-columns-display-here (cdr x)))
1420 cache)
1421 (when org-agenda-columns-show-summaries
1422 (org-agenda-colview-summarize cache))))))
1424 (defun org-agenda-colview-summarize (cache)
1425 "Summarize the summarizable columns in column view in the agenda.
1426 This will add overlays to the date lines, to show the summary for each day."
1427 (let* ((fmt (mapcar (lambda (x)
1428 (list (car x) (if (equal (car x) "CLOCKSUM")
1429 'add_times (nth 4 x))))
1430 org-columns-current-fmt-compiled))
1431 line c c1 stype props lsum entries prop v)
1432 (catch 'exit
1433 (when (delq nil (mapcar 'cadr fmt))
1434 ;; OK, at least one summation column, it makes sense to try this
1435 (goto-char (point-max))
1436 (while t
1437 (when (or (get-text-property (point) 'org-date-line)
1438 (eq (get-text-property (point) 'face)
1439 'org-agenda-structure))
1440 ;; OK, this is a date line that should be used
1441 (setq line (org-current-line))
1442 (setq entries nil c cache cache nil)
1443 (while (setq c1 (pop c))
1444 (if (> (car c1) line)
1445 (push c1 entries)
1446 (push c1 cache)))
1447 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1448 ;; Compute the summaries for the properties we want,
1449 ;; set nil properties for the rest.
1450 (when (setq entries (mapcar 'cdr entries))
1451 (setq props
1452 (mapcar
1453 (lambda (f)
1454 (setq prop (car f) stype (nth 1 f))
1455 (cond
1456 ((equal prop "ITEM")
1457 (cons prop (buffer-substring (point-at-bol)
1458 (point-at-eol))))
1459 ((not stype) (cons prop ""))
1461 ;; do the summary
1462 (setq lsum 0)
1463 (mapc (lambda (x)
1464 (setq v (cdr (assoc prop x)))
1465 (if v (setq lsum (+ lsum
1466 (org-column-string-to-number
1467 v stype)))))
1468 entries)
1469 (setq lsum (org-columns-number-to-string lsum stype))
1470 (put-text-property
1471 0 (length lsum) 'face 'bold lsum)
1472 (cons prop lsum))))
1473 fmt))
1474 (org-columns-display-here props)
1475 (org-set-local 'org-agenda-columns-active t)))
1476 (if (bobp) (throw 'exit t))
1477 (beginning-of-line 0))))))
1479 (defun org-agenda-colview-compute (fmt)
1480 "Compute the relevant columns in the contributing source buffers."
1481 (let ((files org-agenda-contributing-files)
1482 (org-columns-begin-marker (make-marker))
1483 (org-columns-top-level-marker (make-marker))
1484 f fm a b)
1485 (while (setq f (pop files))
1486 (setq b (find-buffer-visiting f))
1487 (with-current-buffer (or (buffer-base-buffer b) b)
1488 (save-excursion
1489 (save-restriction
1490 (widen)
1491 (org-unmodified
1492 (remove-text-properties (point-min) (point-max)
1493 '(org-summaries t)))
1494 (goto-char (point-min))
1495 (org-columns-get-format-and-top-level)
1496 (while (setq fm (pop fmt))
1497 (if (equal (car fm) "CLOCKSUM")
1498 (org-clock-sum)
1499 (when (and (nth 4 fm)
1500 (setq a (assoc (car fm)
1501 org-columns-current-fmt-compiled))
1502 (equal (nth 4 a) (nth 4 fm)))
1503 (org-columns-compute (car fm)))))))))))
1505 (provide 'org-colview)
1506 (provide 'org-colview-xemacs)
1508 ;;; org-colview-xemacs.el ends here