Version number 6.05pre01, with a detailed list of changes.
[org-mode.git] / lisp / org-colview-xemacs.el
blobf583d8b8c545a5a27bd632ecf97a5cfa44229656
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.05pre01
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 (cond
755 ((equal major-mode 'org-agenda-mode)
756 (org-columns-eval '(org-entry-put pom key nval))
757 ;; The following let preserves the current format, and makes sure
758 ;; that in only a single file things need to be upated.
759 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
760 (buffer (marker-buffer pom))
761 (org-agenda-contributing-files
762 (list (with-current-buffer buffer
763 (buffer-file-name (buffer-base-buffer))))))
764 (org-agenda-columns)))
766 (let ((inhibit-read-only t))
767 (remove-text-properties (1- bol) eol '(read-only t))
768 (unwind-protect
769 (progn
770 (setq org-columns-overlays
771 (org-delete-all line-overlays org-columns-overlays))
772 (mapc 'org-delete-overlay line-overlays)
773 (org-columns-eval '(org-entry-put pom key nval)))
774 (org-columns-display-here)))
775 (org-move-to-column col)
776 (and (nth 3 (assoc key org-columns-current-fmt-compiled))
777 (org-columns-update key))))))
779 (defun org-verify-version (task)
780 (cond
781 ((eq task 'columns)
782 (if (or (and (featurep 'xemacs) (not (featurep 'org-colview-xemacs)))
783 (and (not (featurep 'xemacs)) (< emacs-major-version 22)))
784 (error "This version of Emacs cannot run Column View")))))
786 (defun org-columns-open-link (&optional arg)
787 (interactive "P")
788 (let ((value (get-char-property (point) 'org-columns-value)))
789 (org-open-link-from-string value arg)))
791 (defun org-columns-get-format-and-top-level ()
792 (let (fmt)
793 (when (condition-case nil (org-back-to-heading) (error nil))
794 (move-marker org-entry-property-inherited-from nil)
795 (setq fmt (org-entry-get nil "COLUMNS" t)))
796 (setq fmt (or fmt org-columns-default-format))
797 (org-set-local 'org-columns-current-fmt fmt)
798 (org-columns-compile-format fmt)
799 (if (marker-position org-entry-property-inherited-from)
800 (move-marker org-columns-top-level-marker
801 org-entry-property-inherited-from)
802 (move-marker org-columns-top-level-marker (point)))
803 fmt))
805 (defun org-columns ()
806 "Turn on column view on an org-mode file."
807 (interactive)
808 (org-verify-version 'columns)
809 (when (featurep 'xemacs)
810 (set-face-foreground 'org-columns-space
811 (face-background 'org-columns-space)))
812 (org-columns-remove-overlays)
813 (move-marker org-columns-begin-marker (point))
814 (let (beg end fmt cache maxwidths)
815 (setq fmt (org-columns-get-format-and-top-level))
816 (save-excursion
817 (goto-char org-columns-top-level-marker)
818 (setq beg (point))
819 (unless org-columns-inhibit-recalculation
820 (org-columns-compute-all))
821 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
822 (point-max)))
823 ;; Get and cache the properties
824 (goto-char beg)
825 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
826 (save-excursion
827 (save-restriction
828 (narrow-to-region beg end)
829 (org-clock-sum))))
830 (while (re-search-forward (concat "^" outline-regexp) end t)
831 (push (cons (org-current-line) (org-entry-properties)) cache))
832 (when cache
833 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
834 (org-set-local 'org-columns-current-maxwidths maxwidths)
835 (org-columns-display-here-title)
836 (mapc (lambda (x)
837 (goto-line (car x))
838 (org-columns-display-here (cdr x)))
839 cache)))))
841 (defun org-columns-new (&optional prop title width op fmt &rest rest)
842 "Insert a new column, to the left of the current column."
843 (interactive)
844 (let ((n (org-columns-current-column))
845 (editp (and prop (assoc prop org-columns-current-fmt-compiled)))
846 cell)
847 (setq prop (completing-read
848 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
849 nil nil prop))
850 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
851 (setq width (read-string "Column width: " (if width (number-to-string width))))
852 (if (string-match "\\S-" width)
853 (setq width (string-to-number width))
854 (setq width nil))
855 (setq fmt (completing-read "Summary [none]: "
856 '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
857 nil t))
858 (if (string-match "\\S-" fmt)
859 (setq fmt (intern fmt))
860 (setq fmt nil))
861 (if (eq fmt 'none) (setq fmt nil))
862 (if editp
863 (progn
864 (setcar editp prop)
865 (setcdr editp (list title width nil fmt)))
866 (setq cell (nthcdr (1- n) org-columns-current-fmt-compiled))
867 (setcdr cell (cons (list prop title width nil fmt)
868 (cdr cell))))
869 (org-columns-store-format)
870 (org-columns-redo)))
872 (defun org-columns-delete ()
873 "Delete the column at point from columns view."
874 (interactive)
875 (let* ((n (org-columns-current-column))
876 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
877 (when (y-or-n-p
878 (format "Are you sure you want to remove column \"%s\"? " title))
879 (setq org-columns-current-fmt-compiled
880 (delq (nth n org-columns-current-fmt-compiled)
881 org-columns-current-fmt-compiled))
882 (org-columns-store-format)
883 (org-columns-redo)
884 (if (>= (org-columns-current-column)
885 (length org-columns-current-fmt-compiled))
886 (org-columns-backward-char)))))
888 (defun org-columns-edit-attributes ()
889 "Edit the attributes of the current column."
890 (interactive)
891 (let* ((n (org-columns-current-column))
892 (info (nth n org-columns-current-fmt-compiled)))
893 (apply 'org-columns-new info)))
895 (defun org-columns-widen (arg)
896 "Make the column wider by ARG characters."
897 (interactive "p")
898 (let* ((n (org-columns-current-column))
899 (entry (nth n org-columns-current-fmt-compiled))
900 (width (or (nth 2 entry)
901 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
902 (setq width (max 1 (+ width arg)))
903 (setcar (nthcdr 2 entry) width)
904 (org-columns-store-format)
905 (org-columns-redo)))
907 (defun org-columns-narrow (arg)
908 "Make the column nrrower by ARG characters."
909 (interactive "p")
910 (org-columns-widen (- arg)))
912 (defun org-columns-move-right ()
913 "Swap this column with the one to the right."
914 (interactive)
915 (let* ((n (org-columns-current-column))
916 (cell (nthcdr n org-columns-current-fmt-compiled))
918 (when (>= n (1- (length org-columns-current-fmt-compiled)))
919 (error "Cannot shift this column further to the right"))
920 (setq e (car cell))
921 (setcar cell (car (cdr cell)))
922 (setcdr cell (cons e (cdr (cdr cell))))
923 (org-columns-store-format)
924 (org-columns-redo)
925 (org-columns-forward-char)))
927 (defun org-columns-move-left ()
928 "Swap this column with the one to the left."
929 (interactive)
930 (let* ((n (org-columns-current-column)))
931 (when (= n 0)
932 (error "Cannot shift this column further to the left"))
933 (org-columns-backward-char)
934 (org-columns-move-right)
935 (org-columns-backward-char)))
937 (defun org-columns-store-format ()
938 "Store the text version of the current columns format in appropriate place.
939 This is either in the COLUMNS property of the node starting the current column
940 display, or in the #+COLUMNS line of the current buffer."
941 (let (fmt (cnt 0))
942 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
943 (org-set-local 'org-columns-current-fmt fmt)
944 (if (marker-position org-columns-top-level-marker)
945 (save-excursion
946 (goto-char org-columns-top-level-marker)
947 (if (and (org-at-heading-p)
948 (org-entry-get nil "COLUMNS"))
949 (org-entry-put nil "COLUMNS" fmt)
950 (goto-char (point-min))
951 ;; Overwrite all #+COLUMNS lines....
952 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
953 (setq cnt (1+ cnt))
954 (replace-match (concat "#+COLUMNS: " fmt) t t))
955 (unless (> cnt 0)
956 (goto-char (point-min))
957 (or (org-on-heading-p t) (outline-next-heading))
958 (let ((inhibit-read-only t))
959 (insert-before-markers "#+COLUMNS: " fmt "\n")))
960 (org-set-local 'org-columns-default-format fmt))))))
962 (defvar org-agenda-overriding-columns-format nil
963 "When set, overrides any other format definition for the agenda.
964 Don't set this, this is meant for dynamic scoping.")
966 (defun org-columns-get-autowidth-alist (s cache)
967 "Derive the maximum column widths from the format and the cache."
968 (let ((start 0) rtn)
969 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
970 (push (cons (match-string 1 s) 1) rtn)
971 (setq start (match-end 0)))
972 (mapc (lambda (x)
973 (setcdr x (apply 'max
974 (mapcar
975 (lambda (y)
976 (length (or (cdr (assoc (car x) (cdr y))) " ")))
977 cache))))
978 rtn)
979 rtn))
981 (defun org-columns-compute-all ()
982 "Compute all columns that have operators defined."
983 (org-unmodified
984 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
985 (let ((columns org-columns-current-fmt-compiled) col)
986 (while (setq col (pop columns))
987 (when (nth 3 col)
988 (save-excursion
989 (org-columns-compute (car col)))))))
991 (defun org-columns-update (property)
992 "Recompute PROPERTY, and update the columns display for it."
993 (org-columns-compute property)
994 (let (fmt val pos face)
995 (save-excursion
996 (mapc (lambda (ov)
997 (when (equal (org-overlay-get ov 'org-columns-key) property)
998 (setq pos (org-overlay-start ov))
999 (goto-char pos)
1000 (when (setq val (cdr (assoc property
1001 (get-text-property
1002 (point-at-bol) 'org-summaries))))
1003 (setq fmt (org-overlay-get ov 'org-columns-format))
1004 (org-overlay-put ov 'org-columns-value val)
1005 (if (featurep 'xemacs)
1006 (progn
1007 (setq face (glyph-face (extent-end-glyph ov)))
1008 (org-overlay-display ov (format fmt val) face))
1009 (org-overlay-display ov (format fmt val))))))
1010 org-columns-overlays))))
1012 (defun org-columns-compute (property)
1013 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
1014 (interactive)
1015 (let* ((re (concat "^" outline-regexp))
1016 (lmax 30) ; Does anyone use deeper levels???
1017 (lsum (make-vector lmax 0))
1018 (lflag (make-vector lmax nil))
1019 (level 0)
1020 (ass (assoc property org-columns-current-fmt-compiled))
1021 (format (nth 4 ass))
1022 (printf (nth 5 ass))
1023 (beg org-columns-top-level-marker)
1024 last-level val valflag flag end sumpos sum-alist sum str str1 useval)
1025 (save-excursion
1026 ;; Find the region to compute
1027 (goto-char beg)
1028 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
1029 (goto-char end)
1030 ;; Walk the tree from the back and do the computations
1031 (while (re-search-backward re beg t)
1032 (setq sumpos (match-beginning 0)
1033 last-level level
1034 level (org-outline-level)
1035 val (org-entry-get nil property)
1036 valflag (and val (string-match "\\S-" val)))
1037 (cond
1038 ((< level last-level)
1039 ;; put the sum of lower levels here as a property
1040 (setq sum (aref lsum last-level) ; current sum
1041 flag (aref lflag last-level) ; any valid entries from children?
1042 str (org-columns-number-to-string sum format printf)
1043 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
1044 useval (if flag str1 (if valflag val ""))
1045 sum-alist (get-text-property sumpos 'org-summaries))
1046 (if (assoc property sum-alist)
1047 (setcdr (assoc property sum-alist) useval)
1048 (push (cons property useval) sum-alist)
1049 (org-unmodified
1050 (add-text-properties sumpos (1+ sumpos)
1051 (list 'org-summaries sum-alist))))
1052 (when (and val (not (equal val (if flag str val))))
1053 (org-entry-put nil property (if flag str val)))
1054 ;; add current to current level accumulator
1055 (when (or flag valflag)
1056 (aset lsum level (+ (aref lsum level)
1057 (if flag sum (org-column-string-to-number
1058 (if flag str val) format))))
1059 (aset lflag level t))
1060 ;; clear accumulators for deeper levels
1061 (loop for l from (1+ level) to (1- lmax) do
1062 (aset lsum l 0)
1063 (aset lflag l nil)))
1064 ((>= level last-level)
1065 ;; add what we have here to the accumulator for this level
1066 (aset lsum level (+ (aref lsum level)
1067 (org-column-string-to-number (or val "0") format)))
1068 (and valflag (aset lflag level t)))
1069 (t (error "This should not happen")))))))
1071 (defun org-columns-redo ()
1072 "Construct the column display again."
1073 (interactive)
1074 (message "Recomputing columns...")
1075 (save-excursion
1076 (if (marker-position org-columns-begin-marker)
1077 (goto-char org-columns-begin-marker))
1078 (org-columns-remove-overlays)
1079 (if (org-mode-p)
1080 (call-interactively 'org-columns)
1081 (org-agenda-redo)
1082 (call-interactively 'org-agenda-columns)))
1083 (when (featurep 'xemacs)
1084 (while (not (or (eolp)
1085 (member (extent-at (point)) org-columns-overlays)))
1086 (forward-char)))
1087 (message "Recomputing columns...done"))
1089 (defun org-columns-not-in-agenda ()
1090 (if (eq major-mode 'org-agenda-mode)
1091 (error "This command is only allowed in Org-mode buffers")))
1094 (defun org-string-to-number (s)
1095 "Convert string to number, and interpret hh:mm:ss."
1096 (if (not (string-match ":" s))
1097 (string-to-number s)
1098 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1099 (while l
1100 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1101 sum)))
1103 (defun org-columns-number-to-string (n fmt &optional printf)
1104 "Convert a computed column number to a string value, according to FMT."
1105 (cond
1106 ((eq fmt 'add_times)
1107 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
1108 (format org-time-clocksum-format h m)))
1109 ((eq fmt 'checkbox)
1110 (cond ((= n (floor n)) "[X]")
1111 ((> n 1.) "[-]")
1112 (t "[ ]")))
1113 ((memq fmt '(checkbox-n-of-m checkbox-percent))
1114 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
1115 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
1116 (printf (format printf n))
1117 ((eq fmt 'currency)
1118 (format "%.2f" n))
1119 (t (number-to-string n))))
1121 (defun org-nofm-to-completion (n m &optional percent)
1122 (if (not percent)
1123 (format "[%d/%d]" n m)
1124 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
1126 (defun org-column-string-to-number (s fmt)
1127 "Convert a column value to a number that can be used for column computing."
1128 (cond
1129 ((string-match ":" s)
1130 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1131 (while l
1132 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1133 sum))
1134 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
1135 (if (equal s "[X]") 1. 0.000001))
1136 (t (string-to-number s))))
1138 (defun org-columns-uncompile-format (cfmt)
1139 "Turn the compiled columns format back into a string representation."
1140 (let ((rtn "") e s prop title op width fmt printf)
1141 (while (setq e (pop cfmt))
1142 (setq prop (car e)
1143 title (nth 1 e)
1144 width (nth 2 e)
1145 op (nth 3 e)
1146 fmt (nth 4 e)
1147 printf (nth 5 e))
1148 (cond
1149 ((eq fmt 'add_times) (setq op ":"))
1150 ((eq fmt 'checkbox) (setq op "X"))
1151 ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
1152 ((eq fmt 'checkbox-percent) (setq op "X%"))
1153 ((eq fmt 'add_numbers) (setq op "+"))
1154 ((eq fmt 'currency) (setq op "$")))
1155 (if (and op printf) (setq op (concat op ";" printf)))
1156 (if (equal title prop) (setq title nil))
1157 (setq s (concat "%" (if width (number-to-string width))
1158 prop
1159 (if title (concat "(" title ")"))
1160 (if op (concat "{" op "}"))))
1161 (setq rtn (concat rtn " " s)))
1162 (org-trim rtn)))
1164 (defun org-columns-compile-format (fmt)
1165 "Turn a column format string into an alist of specifications.
1166 The alist has one entry for each column in the format. The elements of
1167 that list are:
1168 property the property
1169 title the title field for the columns
1170 width the column width in characters, can be nil for automatic
1171 operator the operator if any
1172 format the output format for computed results, derived from operator
1173 printf a printf format for computed values"
1174 (let ((start 0) width prop title op f printf)
1175 (setq org-columns-current-fmt-compiled nil)
1176 (while (string-match
1177 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
1178 fmt start)
1179 (setq start (match-end 0)
1180 width (match-string 1 fmt)
1181 prop (match-string 2 fmt)
1182 title (or (match-string 3 fmt) prop)
1183 op (match-string 4 fmt)
1184 f nil
1185 printf nil)
1186 (if width (setq width (string-to-number width)))
1187 (when (and op (string-match ";" op))
1188 (setq printf (substring op (match-end 0))
1189 op (substring op 0 (match-beginning 0))))
1190 (cond
1191 ((equal op "+") (setq f 'add_numbers))
1192 ((equal op "$") (setq f 'currency))
1193 ((equal op ":") (setq f 'add_times))
1194 ((equal op "X") (setq f 'checkbox))
1195 ((equal op "X/") (setq f 'checkbox-n-of-m))
1196 ((equal op "X%") (setq f 'checkbox-percent))
1198 (push (list prop title width op f printf) org-columns-current-fmt-compiled))
1199 (setq org-columns-current-fmt-compiled
1200 (nreverse org-columns-current-fmt-compiled))))
1203 ;;; Dynamic block for Column view
1205 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
1206 "Get the column view of the current buffer or subtree.
1207 The first optional argument MAXLEVEL sets the level limit. A
1208 second optional argument SKIP-EMPTY-ROWS tells whether to skip
1209 empty rows, an empty row being one where all the column view
1210 specifiers except ITEM are empty. This function returns a list
1211 containing the title row and all other rows. Each row is a list
1212 of fields."
1213 (if (featurep 'xemacs)
1214 (save-excursion
1215 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1216 (n (length title)) row tbl)
1217 (goto-char (point-min))
1218 (while (and (re-search-forward "^\\(\\*+\\) " nil t)
1219 (or (null maxlevel)
1220 (>= maxlevel
1221 (if org-odd-levels-only
1222 (/ (1+ (length (match-string 1))) 2)
1223 (length (match-string 1))))))
1224 (when (get-char-property (match-beginning 0) 'org-columns-key)
1225 (goto-char (match-beginning 0))
1226 (setq row nil)
1227 (loop for i from 0 to (1- n) do
1228 (push (or (get-char-property (point)
1229 'org-columns-value-modified)
1230 (get-char-property (point) 'org-columns-value)
1232 row)
1233 (org-columns-forward-char))
1234 (setq row (nreverse row))
1235 (unless (and skip-empty-rows
1236 (eq 1 (length (delete "" (delete-dups row)))))
1237 (push row tbl))))
1238 (append (list title 'hline) (nreverse tbl))))
1239 (save-excursion
1240 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1241 (n (length title)) row tbl)
1242 (goto-char (point-min))
1243 (while (and (re-search-forward "^\\(\\*+\\) " nil t)
1244 (or (null maxlevel)
1245 (>= maxlevel
1246 (if org-odd-levels-only
1247 (/ (1+ (length (match-string 1))) 2)
1248 (length (match-string 1))))))
1249 (when (get-char-property (match-beginning 0) 'org-columns-key)
1250 (setq row nil)
1251 (loop for i from 0 to (1- n) do
1252 (push (or (get-char-property (+ (match-beginning 0) i)
1253 'org-columns-value-modified)
1254 (get-char-property (+ (match-beginning 0) i)
1255 'org-columns-value)
1257 row))
1258 (setq row (nreverse row))
1259 (unless (and skip-empty-rows
1260 (eq 1 (length (delete "" (delete-dups row)))))
1261 (push row tbl))))
1262 (append (list title 'hline) (nreverse tbl))))))
1264 (defun org-dblock-write:columnview (params)
1265 "Write the column view table.
1266 PARAMS is a property list of parameters:
1268 :width enforce same column widths with <N> specifiers.
1269 :id the :ID: property of the entry where the columns view
1270 should be built, as a string. When `local', call locally.
1271 When `global' call column view with the cursor at the beginning
1272 of the buffer (usually this means that the whole buffer switches
1273 to column view).
1274 :hlines When t, insert a hline before each item. When a number, insert
1275 a hline before each level <= that number.
1276 :vlines When t, make each column a colgroup to enforce vertical lines.
1277 :maxlevel When set to a number, don't capture headlines below this level.
1278 :skip-empty-rows
1279 When t, skip rows where all specifiers other than ITEM are empty."
1280 (let ((pos (move-marker (make-marker) (point)))
1281 (hlines (plist-get params :hlines))
1282 (vlines (plist-get params :vlines))
1283 (maxlevel (plist-get params :maxlevel))
1284 (skip-empty-rows (plist-get params :skip-empty-rows))
1285 tbl id idpos nfields tmp)
1286 (save-excursion
1287 (save-restriction
1288 (when (setq id (plist-get params :id))
1289 (cond ((not id) nil)
1290 ((eq id 'global) (goto-char (point-min)))
1291 ((eq id 'local) nil)
1292 ((setq idpos (org-find-entry-with-id id))
1293 (goto-char idpos))
1294 (t (error "Cannot find entry with :ID: %s" id))))
1295 (org-columns)
1296 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1297 (setq nfields (length (car tbl)))
1298 (org-columns-quit)))
1299 (goto-char pos)
1300 (move-marker pos nil)
1301 (when tbl
1302 (when (plist-get params :hlines)
1303 (setq tmp nil)
1304 (while tbl
1305 (if (eq (car tbl) 'hline)
1306 (push (pop tbl) tmp)
1307 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1308 (if (and (not (eq (car tmp) 'hline))
1309 (or (eq hlines t)
1310 (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
1311 (push 'hline tmp)))
1312 (push (pop tbl) tmp)))
1313 (setq tbl (nreverse tmp)))
1314 (when vlines
1315 (setq tbl (mapcar (lambda (x)
1316 (if (eq 'hline x) x (cons "" x)))
1317 tbl))
1318 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1319 (setq pos (point))
1320 (insert (org-listtable-to-string tbl))
1321 (when (plist-get params :width)
1322 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1323 org-columns-current-widths "|")))
1324 (goto-char pos)
1325 (org-table-align))))
1327 (defun org-listtable-to-string (tbl)
1328 "Convert a listtable TBL to a string that contains the Org-mode table.
1329 The table still need to be alligned. The resulting string has no leading
1330 and tailing newline characters."
1331 (mapconcat
1332 (lambda (x)
1333 (cond
1334 ((listp x)
1335 (concat "|" (mapconcat 'identity x "|") "|"))
1336 ((eq x 'hline) "|-|")
1337 (t (error "Garbage in listtable: %s" x))))
1338 tbl "\n"))
1340 (defun org-insert-columns-dblock ()
1341 "Create a dynamic block capturing a column view table."
1342 (interactive)
1343 (when (featurep 'xemacs) (org-columns-quit))
1344 (let ((defaults '(:name "columnview" :hlines 1))
1345 (id (completing-read
1346 "Capture columns (local, global, entry with :ID: property) [local]: "
1347 (append '(("global") ("local"))
1348 (mapcar 'list (org-property-values "ID"))))))
1349 (if (equal id "") (setq id 'local))
1350 (if (equal id "global") (setq id 'global))
1351 (setq defaults (append defaults (list :id id)))
1352 (org-create-dblock defaults)
1353 (org-update-dblock)))
1355 ;;; Column view in the agenda
1357 (defvar org-agenda-view-columns-initially nil
1358 "When set, switch to columns view immediately after creating the agenda.")
1360 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1361 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1362 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1364 (defun org-agenda-columns ()
1365 "Turn on or update column view in the agenda."
1366 (interactive)
1367 (org-verify-version 'columns)
1368 (org-columns-remove-overlays)
1369 (move-marker org-columns-begin-marker (point))
1370 (let (fmt cache maxwidths m p a d)
1371 (cond
1372 ((and (boundp 'org-agenda-overriding-columns-format)
1373 org-agenda-overriding-columns-format)
1374 (setq fmt org-agenda-overriding-columns-format)
1375 (org-set-local 'org-agenda-overriding-columns-format fmt))
1376 ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
1377 (setq fmt (or (org-entry-get m "COLUMNS" t)
1378 (with-current-buffer (marker-buffer m)
1379 org-columns-default-format))))
1380 ((and (boundp 'org-columns-current-fmt)
1381 (local-variable-p 'org-columns-current-fmt (current-buffer))
1382 org-columns-current-fmt)
1383 (setq fmt org-columns-current-fmt))
1384 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
1385 (setq m (get-text-property m 'org-hd-marker))
1386 (setq fmt (or (org-entry-get m "COLUMNS" t)
1387 (with-current-buffer (marker-buffer m)
1388 org-columns-default-format)))))
1389 (setq fmt (or fmt org-columns-default-format))
1390 (org-set-local 'org-columns-current-fmt fmt)
1391 (org-columns-compile-format fmt)
1392 (when org-agenda-columns-compute-summary-properties
1393 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1394 (save-excursion
1395 ;; Get and cache the properties
1396 (goto-char (point-min))
1397 (while (not (eobp))
1398 (when (setq m (or (get-text-property (point) 'org-hd-marker)
1399 (get-text-property (point) 'org-marker)))
1400 (setq p (org-entry-properties m))
1402 (when (or (not (setq a (assoc org-effort-property p)))
1403 (not (string-match "\\S-" (or (cdr a) ""))))
1404 ;; OK, the property is not defined. Use appointment duration?
1405 (when (and org-agenda-columns-add-appointments-to-effort-sum
1406 (setq d (get-text-property (point) 'duration)))
1407 (setq d (org-minutes-to-hh:mm-string d))
1408 (put-text-property 0 (length d) 'face 'org-warning d)
1409 (push (cons org-effort-property d) p)))
1410 (push (cons (org-current-line) p) cache))
1411 (beginning-of-line 2))
1412 (when cache
1413 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
1414 (org-set-local 'org-columns-current-maxwidths maxwidths)
1415 (org-columns-display-here-title)
1416 (mapc (lambda (x)
1417 (goto-line (car x))
1418 (org-columns-display-here (cdr x)))
1419 cache)
1420 (when org-agenda-columns-show-summaries
1421 (org-agenda-colview-summarize cache))))))
1423 (defun org-agenda-colview-summarize (cache)
1424 "Summarize the summarizable columns in column view in the agenda.
1425 This will add overlays to the date lines, to show the summary for each day."
1426 (let* ((fmt (mapcar (lambda (x)
1427 (list (car x) (if (equal (car x) "CLOCKSUM")
1428 'add_times (nth 4 x))))
1429 org-columns-current-fmt-compiled))
1430 line c c1 stype props lsum entries prop v)
1431 (catch 'exit
1432 (when (delq nil (mapcar 'cadr fmt))
1433 ;; OK, at least one summation column, it makes sense to try this
1434 (goto-char (point-max))
1435 (while t
1436 (when (or (get-text-property (point) 'org-date-line)
1437 (eq (get-text-property (point) 'face)
1438 'org-agenda-structure))
1439 ;; OK, this is a date line that should be used
1440 (setq line (org-current-line))
1441 (setq entries nil c cache cache nil)
1442 (while (setq c1 (pop c))
1443 (if (> (car c1) line)
1444 (push c1 entries)
1445 (push c1 cache)))
1446 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1447 ;; Compute the summaries for the properties we want,
1448 ;; set nil properties for the rest.
1449 (when (setq entries (mapcar 'cdr entries))
1450 (setq props
1451 (mapcar
1452 (lambda (f)
1453 (setq prop (car f) stype (nth 1 f))
1454 (cond
1455 ((equal prop "ITEM")
1456 (cons prop (buffer-substring (point-at-bol)
1457 (point-at-eol))))
1458 ((not stype) (cons prop ""))
1460 ;; do the summary
1461 (setq lsum 0)
1462 (mapc (lambda (x)
1463 (setq v (cdr (assoc prop x)))
1464 (if v (setq lsum (+ lsum
1465 (org-column-string-to-number
1466 v stype)))))
1467 entries)
1468 (setq lsum (org-columns-number-to-string lsum stype))
1469 (put-text-property
1470 0 (length lsum) 'face 'bold lsum)
1471 (cons prop lsum))))
1472 fmt))
1473 (org-columns-display-here props)
1474 (org-set-local 'org-agenda-columns-active t)))
1475 (if (bobp) (throw 'exit t))
1476 (beginning-of-line 0))))))
1478 (defun org-agenda-colview-compute (fmt)
1479 "Compute the relevant columns in the contributing source buffers."
1480 (let ((files org-agenda-contributing-files)
1481 (org-columns-begin-marker (make-marker))
1482 (org-columns-top-level-marker (make-marker))
1483 f fm a b)
1484 (while (setq f (pop files))
1485 (setq b (find-buffer-visiting f))
1486 (with-current-buffer (or (buffer-base-buffer b) b)
1487 (save-excursion
1488 (save-restriction
1489 (widen)
1490 (org-unmodified
1491 (remove-text-properties (point-min) (point-max)
1492 '(org-summaries t)))
1493 (goto-char (point-min))
1494 (org-columns-get-format-and-top-level)
1495 (while (setq fm (pop fmt))
1496 (if (equal (car fm) "CLOCKSUM")
1497 (org-clock-sum)
1498 (when (and (nth 4 fm)
1499 (setq a (assoc (car fm)
1500 org-columns-current-fmt-compiled))
1501 (equal (nth 4 a) (nth 4 fm)))
1502 (org-columns-compute (car fm)))))))))))
1504 (provide 'org-colview)
1505 (provide 'org-colview-xemacs)
1507 ;;; org-colview-xemacs.el ends here