Release 6.15d.
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-colview-xemacs.el
blob060bc9440741c7f30ad1479700ac61f6505604a1
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.15d
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 column view for Org.
32 ;;; Code:
34 (eval-when-compile (require 'cl))
35 (require 'org)
37 (declare-function org-agenda-redo "org-agenda" ())
40 ;;; Define 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 headlines."
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 title)
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 title (nth 1 column)
338 ass (if (equal property "ITEM")
339 (cons "ITEM" item)
340 (assoc property props))
341 width (or (cdr (assoc property org-columns-current-maxwidths))
342 (nth 2 column)
343 (length property))
344 f (format (if (featurep 'xemacs) "%%-%d.%ds |" "%%-%d.%ds | ")
345 width width)
346 val (or (cdr ass) "")
347 modval (or (and org-columns-modify-value-for-display-function
348 (functionp
349 org-columns-modify-value-for-display-function)
350 (funcall
351 org-columns-modify-value-for-display-function
352 title val))
353 (if (equal property "ITEM")
354 (if (org-mode-p)
355 (org-columns-cleanup-item
356 val org-columns-current-fmt-compiled)
357 (org-agenda-columns-cleanup-item
358 val pl cphr org-columns-current-fmt-compiled)))))
359 (setq s2 (org-columns-add-ellipses (or modval val) width))
360 (setq string (format f s2))
361 ;; Create the overlay
362 (org-unmodified
363 (setq ov (org-columns-new-overlay
364 beg (setq beg (1+ beg)) string face))
365 (org-overlay-put ov 'keymap org-columns-map)
366 (org-overlay-put ov 'org-columns-key property)
367 (org-overlay-put ov 'org-columns-value (cdr ass))
368 (org-overlay-put ov 'org-columns-value-modified modval)
369 (org-overlay-put ov 'org-columns-pom pom)
370 (org-overlay-put ov 'org-columns-format f)
371 (when (featurep 'xemacs)
372 (if (or (not (char-after beg))
373 (equal (char-after beg) ?\n))
374 (let ((inhibit-read-only t))
375 (save-excursion
376 (goto-char beg)
377 (org-unmodified (insert " "))
378 ;; FIXME: add props and remove later?
380 (goto-char beg)
381 (org-columns-new-overlay
382 beg (1+ beg) nil 'org-columns-space)
383 (setq beg (1+ beg))))
385 (if (or (not (char-after beg))
386 (equal (char-after beg) ?\n))
387 (let ((inhibit-read-only t))
388 (save-excursion
389 (goto-char beg)
390 ;; FIXME: add props and remove later?
391 (org-unmodified (insert " "))))))
392 ;; Make the rest of the line disappear.
393 (org-unmodified
394 (setq ov (org-columns-new-overlay beg (point-at-eol)))
395 (org-overlay-put ov 'invisible t)
396 (org-overlay-put ov 'keymap org-columns-map)
397 (org-overlay-put ov 'intangible t)
398 (push ov org-columns-overlays)
399 (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
400 (org-overlay-put ov 'keymap org-columns-map)
401 (push ov org-columns-overlays)
402 (let ((inhibit-read-only t))
403 (put-text-property (max (point-min) (1- (point-at-bol)))
404 (min (point-max) (1+ (point-at-eol)))
405 'read-only "Type `e' to edit property")))))
407 (defun org-columns-add-ellipses (string width)
408 "Truncate STRING with WIDTH characters, with ellipses."
409 (cond
410 ((<= (length string) width) string)
411 ((<= width (length org-columns-ellipses))
412 (substring org-columns-ellipses 0 width))
413 (t (concat (substring string 0 (- width (length org-columns-ellipses)))
414 org-columns-ellipses))))
416 (defvar org-columns-full-header-line-format nil
417 "The full header line format, will be shifted by horizontal scrolling." )
418 (defvar org-previous-header-line-format nil
419 "The header line format before column view was turned on.")
420 (defvar org-columns-inhibit-recalculation nil
421 "Inhibit recomputing of columns on column view startup.")
424 (defvar header-line-format)
425 (defvar org-columns-previous-hscroll 0)
426 (defun org-columns-display-here-title ()
427 "Overlay the newline before the current line with the table title."
428 (interactive)
429 (let ((fmt org-columns-current-fmt-compiled)
430 string (title "")
431 property width f column str widths)
432 (while (setq column (pop fmt))
433 (setq property (car column)
434 str (or (nth 1 column) property)
435 width (or (cdr (assoc property org-columns-current-maxwidths))
436 (nth 2 column)
437 (length str))
438 widths (push width widths)
439 f (format "%%-%d.%ds | " width width)
440 string (format f str)
441 title (concat title string)))
442 (if (featurep 'xemacs)
443 (let ((ext (make-extent nil nil)))
444 (set-extent-endpoints ext 0 (length title) title)
445 (set-extent-face ext (list 'bold 'underline 'org-columns-space1))
446 (org-set-local 'org-previous-header-line-format
447 (specifier-specs top-gutter))
448 (org-set-local 'org-columns-current-widths (nreverse widths))
449 (set-specifier top-gutter (make-gutter-specifier
450 (cons (current-buffer) title))))
451 (setq title (concat
452 (org-add-props " " nil 'display '(space :align-to 0))
453 (org-add-props title nil 'face '(:weight bold :underline t))))
454 (org-set-local 'org-previous-header-line-format header-line-format)
455 (org-set-local 'org-columns-current-widths (nreverse widths))
456 (setq org-columns-full-header-line-format title)
457 (setq org-columns-previous-hscroll -1)
458 (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local))))
460 (defun org-columns-hscoll-title ()
461 "Set the header-line-format so that it scrolls along with the table."
462 (sit-for .0001) ; need to force a redisplay to update window-hscroll
463 (when (not (= (window-hscroll) org-columns-previous-hscroll))
464 (setq header-line-format
465 (concat (substring org-columns-full-header-line-format 0 1)
466 (substring org-columns-full-header-line-format
467 (1+ (window-hscroll))))
468 org-columns-previous-hscroll (window-hscroll))
469 (force-mode-line-update)))
471 (defun org-columns-remove-overlays ()
472 "Remove all currently active column overlays."
473 (interactive)
474 (when (marker-buffer org-columns-begin-marker)
475 (with-current-buffer (marker-buffer org-columns-begin-marker)
476 (when (local-variable-p 'org-previous-header-line-format (current-buffer))
477 (if (featurep 'xemacs)
478 (set-specifier top-gutter
479 (make-gutter-specifier
480 (cons (current-buffer)
481 (cdar org-previous-header-line-format))))
482 (setq header-line-format org-previous-header-line-format)
483 (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
484 (kill-local-variable 'org-previous-header-line-format))
485 (move-marker org-columns-begin-marker nil)
486 (move-marker org-columns-top-level-marker nil)
487 (org-unmodified
488 (mapc 'org-delete-overlay org-columns-overlays)
489 (setq org-columns-overlays nil)
490 (let ((inhibit-read-only t))
491 (remove-text-properties (point-min) (point-max) '(read-only t)))))))
493 (defun org-columns-cleanup-item (item fmt)
494 "Remove from ITEM what is a column in the format FMT."
495 (if (not org-complex-heading-regexp)
496 item
497 (when (string-match org-complex-heading-regexp item)
498 (setq item
499 (concat
500 (org-add-props (match-string 1 item) nil
501 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
502 (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
503 (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
504 " " (save-match-data (org-columns-compact-links (match-string 4 item)))
505 (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))
506 (add-text-properties
507 0 (1+ (match-end 1))
508 (list 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
509 item)
510 item)))
512 (defun org-columns-compact-links (s)
513 "Replace [[link][desc]] with [desc] or [link]."
514 (while (string-match org-bracket-link-regexp s)
515 (setq s (replace-match
516 (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
517 t t s)))
520 (defvar org-agenda-columns-remove-prefix-from-item)
521 (defun org-agenda-columns-cleanup-item (item pl cphr fmt)
522 "Cleanup the time property for agenda column view.
523 See also the variable `org-agenda-columns-remove-prefix-from-item'."
524 (let* ((org-complex-heading-regexp cphr)
525 (prefix (substring item 0 pl))
526 (rest (substring item pl))
527 (fake (concat "* " rest))
528 (cleaned (org-trim (substring (org-columns-cleanup-item fake fmt) 1))))
529 (if org-agenda-columns-remove-prefix-from-item
530 cleaned
531 (concat prefix cleaned))))
533 (defun org-columns-show-value ()
534 "Show the full value of the property."
535 (interactive)
536 (let ((value (get-char-property (point) 'org-columns-value)))
537 (message "Value is: %s" (or value ""))))
539 (defvar org-agenda-columns-active) ;; defined in org-agenda.el
540 (defun org-columns-quit ()
541 "Remove the column overlays and in this way exit column editing."
542 (interactive)
543 (org-unmodified
544 (org-columns-remove-overlays)
545 (let ((inhibit-read-only t))
546 (remove-text-properties (point-min) (point-max) '(read-only t))))
547 (when (eq major-mode 'org-agenda-mode)
548 (setq org-agenda-columns-active nil)
549 (message
550 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
552 (defun org-columns-check-computed ()
553 "Check if this column value is computed.
554 If yes, throw an error indicating that changing it does not make sense."
555 (let ((val (get-char-property (point) 'org-columns-value)))
556 (when (and (stringp val)
557 (get-char-property 0 'org-computed val))
558 (error "This value is computed from the entry's children"))))
560 (defun org-columns-todo (&optional arg)
561 "Change the TODO state during column view."
562 (interactive "P")
563 (org-columns-edit-value "TODO"))
565 (defun org-columns-set-tags-or-toggle (&optional arg)
566 "Toggle checkbox at point, or set tags for current headline."
567 (interactive "P")
568 (if (string-match "\\`\\[[ xX-]\\]\\'"
569 (get-char-property (point) 'org-columns-value))
570 (org-columns-next-allowed-value)
571 (org-columns-edit-value "TAGS")))
573 (defun org-columns-edit-value (&optional key)
574 "Edit the value of the property at point in column view.
575 Where possible, use the standard interface for changing this line."
576 (interactive)
577 (org-columns-check-computed)
578 (let* ((external-key key)
579 (col (current-column))
580 (key (or key (get-char-property (point) 'org-columns-key)))
581 (value (get-char-property (point) 'org-columns-value))
582 (bol (point-at-bol)) (eol (point-at-eol))
583 (pom (or (get-text-property bol 'org-hd-marker)
584 (point))) ; keep despite of compiler warning
585 (line-overlays
586 (delq nil (mapcar (lambda (x)
587 (and (eq (org-overlay-buffer x) (current-buffer))
588 (>= (org-overlay-start x) bol)
589 (<= (org-overlay-start x) eol)
591 org-columns-overlays)))
592 nval eval allowed)
593 (cond
594 ((equal key "CLOCKSUM")
595 (error "This special column cannot be edited"))
596 ((equal key "ITEM")
597 (setq eval '(org-with-point-at pom (org-edit-headline))))
598 ((equal key "TODO")
599 (setq eval '(org-with-point-at
601 (let ((current-prefix-arg
602 (if external-key current-prefix-arg '(4))))
603 (call-interactively 'org-todo)))))
604 ((equal key "PRIORITY")
605 (setq eval '(org-with-point-at pom
606 (call-interactively 'org-priority))))
607 ((equal key "TAGS")
608 (setq eval '(org-with-point-at
610 (let ((org-fast-tag-selection-single-key
611 (if (eq org-fast-tag-selection-single-key 'expert)
612 t org-fast-tag-selection-single-key)))
613 (call-interactively 'org-set-tags)))))
614 ((equal key "DEADLINE")
615 (setq eval '(org-with-point-at
617 (call-interactively 'org-deadline))))
618 ((equal key "SCHEDULED")
619 (setq eval '(org-with-point-at
621 (call-interactively 'org-schedule))))
623 (setq allowed (org-property-get-allowed-values pom key 'table))
624 (if allowed
625 (setq nval (org-ido-completing-read "Value: " allowed nil t))
626 (setq nval (read-string "Edit: " value)))
627 (setq nval (org-trim nval))
628 (when (not (equal nval value))
629 (setq eval '(org-entry-put pom key nval)))))
630 (when eval
632 (cond
633 ((equal major-mode 'org-agenda-mode)
634 (org-columns-eval eval)
635 ;; The following let preserves the current format, and makes sure
636 ;; that in only a single file things need to be upated.
637 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
638 (buffer (marker-buffer pom))
639 (org-agenda-contributing-files
640 (list (with-current-buffer buffer
641 (buffer-file-name (buffer-base-buffer))))))
642 (org-agenda-columns)))
644 (let ((inhibit-read-only t))
645 (org-unmodified
646 (remove-text-properties
647 (max (point-min) (1- bol)) eol '(read-only t)))
648 (unwind-protect
649 (progn
650 (setq org-columns-overlays
651 (org-delete-all line-overlays org-columns-overlays))
652 (mapc 'org-delete-overlay line-overlays)
653 (org-columns-eval eval))
654 (org-columns-display-here)))
655 (org-move-to-column col)
656 (if (and (org-mode-p)
657 (nth 3 (assoc key org-columns-current-fmt-compiled)))
658 (org-columns-update key)))))))
660 (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
661 "Edit the current headline, the part without TODO keyword, TAGS."
662 (org-back-to-heading)
663 (when (looking-at org-todo-line-regexp)
664 (let ((pos (point))
665 (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
666 (txt (match-string 3))
667 (post "")
668 txt2)
669 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
670 (setq post (match-string 0 txt)
671 txt (substring txt 0 (match-beginning 0))))
672 (setq txt2 (read-string "Edit: " txt))
673 (when (not (equal txt txt2))
674 (goto-char pos)
675 (insert pre txt2 post)
676 (delete-region (point) (point-at-eol))
677 (org-set-tags nil t)))))
679 (defun org-columns-edit-allowed ()
680 "Edit the list of allowed values for the current property."
681 (interactive)
682 (let* ((pom (or (get-text-property (point-at-bol) 'org-marker)
683 (get-text-property (point-at-bol) 'org-hd-marker)
684 (point)))
685 (key (get-char-property (point) 'org-columns-key))
686 (key1 (concat key "_ALL"))
687 (allowed (org-entry-get pom key1 t))
688 nval)
689 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
690 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
691 (setq nval (read-string "Allowed: " allowed))
692 (org-entry-put
693 (cond ((marker-position org-entry-property-inherited-from)
694 org-entry-property-inherited-from)
695 ((marker-position org-columns-top-level-marker)
696 org-columns-top-level-marker)
697 (t pom))
698 key1 nval)))
700 (defun org-columns-eval (form)
701 (let (hidep)
702 (save-excursion
703 (beginning-of-line 1)
704 ;; `next-line' is needed here, because it skips invisible line.
705 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
706 (setq hidep (org-on-heading-p 1)))
707 (eval form)
708 (and hidep (hide-entry))))
710 (defun org-columns-previous-allowed-value ()
711 "Switch to the previous allowed value for this column."
712 (interactive)
713 (org-columns-next-allowed-value t))
715 (defun org-columns-next-allowed-value (&optional previous nth)
716 "Switch to the next allowed value for this column.
717 When PREVIOUS is set, go to the previous value. When NTH is
718 an integer, select that value."
719 (interactive)
720 (org-columns-check-computed)
721 (let* ((col (current-column))
722 (key (get-char-property (point) 'org-columns-key))
723 (value (get-char-property (point) 'org-columns-value))
724 (bol (point-at-bol)) (eol (point-at-eol))
725 (pom (or (get-text-property bol 'org-hd-marker)
726 (point))) ; keep despite of compiler waring
727 (line-overlays
728 (delq nil (mapcar (lambda (x)
729 (and (eq (org-overlay-buffer x) (current-buffer))
730 (>= (org-overlay-start x) bol)
731 (<= (org-overlay-start x) eol)
733 org-columns-overlays)))
734 (allowed (or (org-property-get-allowed-values pom key)
735 (and (memq
736 (nth 4 (assoc key org-columns-current-fmt-compiled))
737 '(checkbox checkbox-n-of-m checkbox-percent))
738 '("[ ]" "[X]"))
739 (org-colview-construct-allowed-dates value)))
740 nval)
741 (when (integerp nth)
742 (setq nth (1- nth))
743 (if (= nth -1) (setq nth 9)))
744 (when (equal key "ITEM")
745 (error "Cannot edit item headline from here"))
746 (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
747 (error "Allowed values for this property have not been defined"))
748 (if (member key '("SCHEDULED" "DEADLINE"))
749 (setq nval (if previous 'earlier 'later))
750 (if previous (setq allowed (reverse allowed)))
751 (cond
752 (nth
753 (setq nval (nth nth allowed))
754 (if (not nval)
755 (error "There are only %d allowed values for property `%s'"
756 (length allowed) key)))
757 ((member value allowed)
758 (setq nval (or (car (cdr (member value allowed)))
759 (car allowed)))
760 (if (equal nval value)
761 (error "Only one allowed value for this property")))
762 (t (setq nval (car allowed)))))
763 (cond
764 ((equal major-mode 'org-agenda-mode)
765 (org-columns-eval '(org-entry-put pom key nval))
766 ;; The following let preserves the current format, and makes sure
767 ;; that in only a single file things need to be upated.
768 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
769 (buffer (marker-buffer pom))
770 (org-agenda-contributing-files
771 (list (with-current-buffer buffer
772 (buffer-file-name (buffer-base-buffer))))))
773 (org-agenda-columns)))
775 (let ((inhibit-read-only t))
776 (remove-text-properties (1- bol) eol '(read-only t))
777 (unwind-protect
778 (progn
779 (setq org-columns-overlays
780 (org-delete-all line-overlays org-columns-overlays))
781 (mapc 'org-delete-overlay line-overlays)
782 (org-columns-eval '(org-entry-put pom key nval)))
783 (org-columns-display-here)))
784 (org-move-to-column col)
785 (and (nth 3 (assoc key org-columns-current-fmt-compiled))
786 (org-columns-update key))))))
788 (defun org-colview-construct-allowed-dates (s)
789 "Construct a list of three dates around the date in S.
790 This respects the format of the time stamp in S, active or non-active,
791 and also including time or not. S must be just a time stamp, no text
792 around it."
793 (when (string-match (concat "^" org-ts-regexp3 "$") s)
794 (let* ((time (org-parse-time-string s 'nodefaults))
795 (active (equal (string-to-char s) ?<))
796 (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
797 time-before time-after)
798 (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
799 (setf (car time) (or (car time) 0))
800 (setf (nth 1 time) (or (nth 1 time) 0))
801 (setf (nth 2 time) (or (nth 2 time) 0))
802 (setq time-before (copy-sequence time))
803 (setq time-after (copy-sequence time))
804 (setf (nth 3 time-before) (1- (nth 3 time)))
805 (setf (nth 3 time-after) (1+ (nth 3 time)))
806 (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
807 (list time-before time time-after)))))
809 (defun org-verify-version (task)
810 (cond
811 ((eq task 'columns)
812 (if (or (and (featurep 'xemacs) (not (featurep 'org-colview-xemacs)))
813 (and (not (featurep 'xemacs)) (< emacs-major-version 22)))
814 (error "This version of Emacs cannot run Column View")))))
816 (defun org-columns-open-link (&optional arg)
817 (interactive "P")
818 (let ((value (get-char-property (point) 'org-columns-value)))
819 (org-open-link-from-string value arg)))
821 (defun org-columns-get-format-and-top-level ()
822 (let (fmt)
823 (when (condition-case nil (org-back-to-heading) (error nil))
824 (setq fmt (org-entry-get nil "COLUMNS" t)))
825 (setq fmt (or fmt org-columns-default-format))
826 (org-set-local 'org-columns-current-fmt fmt)
827 (org-columns-compile-format fmt)
828 (if (marker-position org-entry-property-inherited-from)
829 (move-marker org-columns-top-level-marker
830 org-entry-property-inherited-from)
831 (move-marker org-columns-top-level-marker (point)))
832 fmt))
834 (defun org-columns ()
835 "Turn on column view on an org-mode file."
836 (interactive)
837 (org-verify-version 'columns)
838 (when (featurep 'xemacs)
839 (set-face-foreground 'org-columns-space
840 (face-background 'org-columns-space)))
841 (org-columns-remove-overlays)
842 (move-marker org-columns-begin-marker (point))
843 (let (beg end fmt cache maxwidths)
844 (setq fmt (org-columns-get-format-and-top-level))
845 (save-excursion
846 (goto-char org-columns-top-level-marker)
847 (setq beg (point))
848 (unless org-columns-inhibit-recalculation
849 (org-columns-compute-all))
850 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
851 (point-max)))
852 ;; Get and cache the properties
853 (goto-char beg)
854 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
855 (save-excursion
856 (save-restriction
857 (narrow-to-region beg end)
858 (org-clock-sum))))
859 (while (re-search-forward (concat "^" outline-regexp) end t)
860 (push (cons (org-current-line) (org-entry-properties)) cache))
861 (when cache
862 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
863 (org-set-local 'org-columns-current-maxwidths maxwidths)
864 (org-columns-display-here-title)
865 (mapc (lambda (x)
866 (goto-line (car x))
867 (org-columns-display-here (cdr x)))
868 cache)))))
870 (defun org-columns-new (&optional prop title width op fmt &rest rest)
871 "Insert a new column, to the left of the current column."
872 (interactive)
873 (let ((n (org-columns-current-column))
874 (editp (and prop (assoc prop org-columns-current-fmt-compiled)))
875 cell)
876 (setq prop (org-ido-completing-read
877 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
878 nil nil prop))
879 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
880 (setq width (read-string "Column width: " (if width (number-to-string width))))
881 (if (string-match "\\S-" width)
882 (setq width (string-to-number width))
883 (setq width nil))
884 (setq fmt (org-ido-completing-read "Summary [none]: "
885 '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
886 nil t))
887 (if (string-match "\\S-" fmt)
888 (setq fmt (intern fmt))
889 (setq fmt nil))
890 (if (eq fmt 'none) (setq fmt nil))
891 (if editp
892 (progn
893 (setcar editp prop)
894 (setcdr editp (list title width nil fmt)))
895 (setq cell (nthcdr (1- n) org-columns-current-fmt-compiled))
896 (setcdr cell (cons (list prop title width nil fmt)
897 (cdr cell))))
898 (org-columns-store-format)
899 (org-columns-redo)))
901 (defun org-columns-delete ()
902 "Delete the column at point from columns view."
903 (interactive)
904 (let* ((n (org-columns-current-column))
905 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
906 (when (y-or-n-p
907 (format "Are you sure you want to remove column \"%s\"? " title))
908 (setq org-columns-current-fmt-compiled
909 (delq (nth n org-columns-current-fmt-compiled)
910 org-columns-current-fmt-compiled))
911 (org-columns-store-format)
912 (org-columns-redo)
913 (if (>= (org-columns-current-column)
914 (length org-columns-current-fmt-compiled))
915 (org-columns-backward-char)))))
917 (defun org-columns-edit-attributes ()
918 "Edit the attributes of the current column."
919 (interactive)
920 (let* ((n (org-columns-current-column))
921 (info (nth n org-columns-current-fmt-compiled)))
922 (apply 'org-columns-new info)))
924 (defun org-columns-widen (arg)
925 "Make the column wider by ARG characters."
926 (interactive "p")
927 (let* ((n (org-columns-current-column))
928 (entry (nth n org-columns-current-fmt-compiled))
929 (width (or (nth 2 entry)
930 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
931 (setq width (max 1 (+ width arg)))
932 (setcar (nthcdr 2 entry) width)
933 (org-columns-store-format)
934 (org-columns-redo)))
936 (defun org-columns-narrow (arg)
937 "Make the column narrower by ARG characters."
938 (interactive "p")
939 (org-columns-widen (- arg)))
941 (defun org-columns-move-right ()
942 "Swap this column with the one to the right."
943 (interactive)
944 (let* ((n (org-columns-current-column))
945 (cell (nthcdr n org-columns-current-fmt-compiled))
947 (when (>= n (1- (length org-columns-current-fmt-compiled)))
948 (error "Cannot shift this column further to the right"))
949 (setq e (car cell))
950 (setcar cell (car (cdr cell)))
951 (setcdr cell (cons e (cdr (cdr cell))))
952 (org-columns-store-format)
953 (org-columns-redo)
954 (org-columns-forward-char)))
956 (defun org-columns-move-left ()
957 "Swap this column with the one to the left."
958 (interactive)
959 (let* ((n (org-columns-current-column)))
960 (when (= n 0)
961 (error "Cannot shift this column further to the left"))
962 (org-columns-backward-char)
963 (org-columns-move-right)
964 (org-columns-backward-char)))
966 (defun org-columns-store-format ()
967 "Store the text version of the current columns format in appropriate place.
968 This is either in the COLUMNS property of the node starting the current column
969 display, or in the #+COLUMNS line of the current buffer."
970 (let (fmt (cnt 0))
971 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
972 (org-set-local 'org-columns-current-fmt fmt)
973 (if (marker-position org-columns-top-level-marker)
974 (save-excursion
975 (goto-char org-columns-top-level-marker)
976 (if (and (org-at-heading-p)
977 (org-entry-get nil "COLUMNS"))
978 (org-entry-put nil "COLUMNS" fmt)
979 (goto-char (point-min))
980 ;; Overwrite all #+COLUMNS lines....
981 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
982 (setq cnt (1+ cnt))
983 (replace-match (concat "#+COLUMNS: " fmt) t t))
984 (unless (> cnt 0)
985 (goto-char (point-min))
986 (or (org-on-heading-p t) (outline-next-heading))
987 (let ((inhibit-read-only t))
988 (insert-before-markers "#+COLUMNS: " fmt "\n")))
989 (org-set-local 'org-columns-default-format fmt))))))
991 (defvar org-agenda-overriding-columns-format nil
992 "When set, overrides any other format definition for the agenda.
993 Don't set this, this is meant for dynamic scoping.")
995 (defun org-columns-get-autowidth-alist (s cache)
996 "Derive the maximum column widths from the format and the cache."
997 (let ((start 0) rtn)
998 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
999 (push (cons (match-string 1 s) 1) rtn)
1000 (setq start (match-end 0)))
1001 (mapc (lambda (x)
1002 (setcdr x (apply 'max
1003 (mapcar
1004 (lambda (y)
1005 (length (or (cdr (assoc (car x) (cdr y))) " ")))
1006 cache))))
1007 rtn)
1008 rtn))
1010 (defun org-columns-compute-all ()
1011 "Compute all columns that have operators defined."
1012 (org-unmodified
1013 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
1014 (let ((columns org-columns-current-fmt-compiled) col)
1015 (while (setq col (pop columns))
1016 (when (nth 3 col)
1017 (save-excursion
1018 (org-columns-compute (car col)))))))
1020 (defun org-columns-update (property)
1021 "Recompute PROPERTY, and update the columns display for it."
1022 (org-columns-compute property)
1023 (let (fmt val pos face)
1024 (save-excursion
1025 (mapc (lambda (ov)
1026 (when (equal (org-overlay-get ov 'org-columns-key) property)
1027 (setq pos (org-overlay-start ov))
1028 (goto-char pos)
1029 (when (setq val (cdr (assoc property
1030 (get-text-property
1031 (point-at-bol) 'org-summaries))))
1032 (setq fmt (org-overlay-get ov 'org-columns-format))
1033 (org-overlay-put ov 'org-columns-value val)
1034 (if (featurep 'xemacs)
1035 (progn
1036 (setq face (glyph-face (extent-end-glyph ov)))
1037 (org-overlay-display ov (format fmt val) face))
1038 (org-overlay-display ov (format fmt val))))))
1039 org-columns-overlays))))
1041 (defun org-columns-compute (property)
1042 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
1043 (interactive)
1044 (let* ((re (concat "^" outline-regexp))
1045 (lmax 30) ; Does anyone use deeper levels???
1046 (lsum (make-vector lmax 0))
1047 (lflag (make-vector lmax nil))
1048 (level 0)
1049 (ass (assoc property org-columns-current-fmt-compiled))
1050 (format (nth 4 ass))
1051 (printf (nth 5 ass))
1052 (beg org-columns-top-level-marker)
1053 last-level val valflag flag end sumpos sum-alist sum str str1 useval)
1054 (save-excursion
1055 ;; Find the region to compute
1056 (goto-char beg)
1057 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
1058 (goto-char end)
1059 ;; Walk the tree from the back and do the computations
1060 (while (re-search-backward re beg t)
1061 (setq sumpos (match-beginning 0)
1062 last-level level
1063 level (org-outline-level)
1064 val (org-entry-get nil property)
1065 valflag (and val (string-match "\\S-" val)))
1066 (cond
1067 ((< level last-level)
1068 ;; put the sum of lower levels here as a property
1069 (setq sum (aref lsum last-level) ; current sum
1070 flag (aref lflag last-level) ; any valid entries from children?
1071 str (org-columns-number-to-string sum format printf)
1072 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
1073 useval (if flag str1 (if valflag val ""))
1074 sum-alist (get-text-property sumpos 'org-summaries))
1075 (if (assoc property sum-alist)
1076 (setcdr (assoc property sum-alist) useval)
1077 (push (cons property useval) sum-alist)
1078 (org-unmodified
1079 (add-text-properties sumpos (1+ sumpos)
1080 (list 'org-summaries sum-alist))))
1081 (when (and val (not (equal val (if flag str val))))
1082 (org-entry-put nil property (if flag str val)))
1083 ;; add current to current level accumulator
1084 (when (or flag valflag)
1085 (aset lsum level (+ (aref lsum level)
1086 (if flag sum (org-column-string-to-number
1087 (if flag str val) format))))
1088 (aset lflag level t))
1089 ;; clear accumulators for deeper levels
1090 (loop for l from (1+ level) to (1- lmax) do
1091 (aset lsum l 0)
1092 (aset lflag l nil)))
1093 ((>= level last-level)
1094 ;; add what we have here to the accumulator for this level
1095 (aset lsum level (+ (aref lsum level)
1096 (org-column-string-to-number (or val "0") format)))
1097 (and valflag (aset lflag level t)))
1098 (t (error "This should not happen")))))))
1100 (defun org-columns-redo ()
1101 "Construct the column display again."
1102 (interactive)
1103 (message "Recomputing columns...")
1104 (save-excursion
1105 (if (marker-position org-columns-begin-marker)
1106 (goto-char org-columns-begin-marker))
1107 (org-columns-remove-overlays)
1108 (if (org-mode-p)
1109 (call-interactively 'org-columns)
1110 (org-agenda-redo)
1111 (call-interactively 'org-agenda-columns)))
1112 (when (featurep 'xemacs)
1113 (while (not (or (eolp)
1114 (member (extent-at (point)) org-columns-overlays)))
1115 (forward-char)))
1116 (message "Recomputing columns...done"))
1118 (defun org-columns-not-in-agenda ()
1119 (if (eq major-mode 'org-agenda-mode)
1120 (error "This command is only allowed in Org-mode buffers")))
1123 (defun org-string-to-number (s)
1124 "Convert string to number, and interpret hh:mm:ss."
1125 (if (not (string-match ":" s))
1126 (string-to-number s)
1127 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1128 (while l
1129 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1130 sum)))
1132 (defun org-columns-number-to-string (n fmt &optional printf)
1133 "Convert a computed column number to a string value, according to FMT."
1134 (cond
1135 ((eq fmt 'add_times)
1136 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
1137 (format org-time-clocksum-format h m)))
1138 ((eq fmt 'checkbox)
1139 (cond ((= n (floor n)) "[X]")
1140 ((> n 1.) "[-]")
1141 (t "[ ]")))
1142 ((memq fmt '(checkbox-n-of-m checkbox-percent))
1143 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
1144 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
1145 (printf (format printf n))
1146 ((eq fmt 'currency)
1147 (format "%.2f" n))
1148 (t (number-to-string n))))
1150 (defun org-nofm-to-completion (n m &optional percent)
1151 (if (not percent)
1152 (format "[%d/%d]" n m)
1153 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
1155 (defun org-column-string-to-number (s fmt)
1156 "Convert a column value to a number that can be used for column computing."
1157 (cond
1158 ((string-match ":" s)
1159 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1160 (while l
1161 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1162 sum))
1163 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
1164 (if (equal s "[X]") 1. 0.000001))
1165 (t (string-to-number s))))
1167 (defun org-columns-uncompile-format (cfmt)
1168 "Turn the compiled columns format back into a string representation."
1169 (let ((rtn "") e s prop title op width fmt printf)
1170 (while (setq e (pop cfmt))
1171 (setq prop (car e)
1172 title (nth 1 e)
1173 width (nth 2 e)
1174 op (nth 3 e)
1175 fmt (nth 4 e)
1176 printf (nth 5 e))
1177 (cond
1178 ((eq fmt 'add_times) (setq op ":"))
1179 ((eq fmt 'checkbox) (setq op "X"))
1180 ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
1181 ((eq fmt 'checkbox-percent) (setq op "X%"))
1182 ((eq fmt 'add_numbers) (setq op "+"))
1183 ((eq fmt 'currency) (setq op "$")))
1184 (if (and op printf) (setq op (concat op ";" printf)))
1185 (if (equal title prop) (setq title nil))
1186 (setq s (concat "%" (if width (number-to-string width))
1187 prop
1188 (if title (concat "(" title ")"))
1189 (if op (concat "{" op "}"))))
1190 (setq rtn (concat rtn " " s)))
1191 (org-trim rtn)))
1193 (defun org-columns-compile-format (fmt)
1194 "Turn a column format string into an alist of specifications.
1195 The alist has one entry for each column in the format. The elements of
1196 that list are:
1197 property the property
1198 title the title field for the columns
1199 width the column width in characters, can be nil for automatic
1200 operator the operator if any
1201 format the output format for computed results, derived from operator
1202 printf a printf format for computed values"
1203 (let ((start 0) width prop title op f printf)
1204 (setq org-columns-current-fmt-compiled nil)
1205 (while (string-match
1206 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
1207 fmt start)
1208 (setq start (match-end 0)
1209 width (match-string 1 fmt)
1210 prop (match-string 2 fmt)
1211 title (or (match-string 3 fmt) prop)
1212 op (match-string 4 fmt)
1213 f nil
1214 printf nil)
1215 (if width (setq width (string-to-number width)))
1216 (when (and op (string-match ";" op))
1217 (setq printf (substring op (match-end 0))
1218 op (substring op 0 (match-beginning 0))))
1219 (cond
1220 ((equal op "+") (setq f 'add_numbers))
1221 ((equal op "$") (setq f 'currency))
1222 ((equal op ":") (setq f 'add_times))
1223 ((equal op "X") (setq f 'checkbox))
1224 ((equal op "X/") (setq f 'checkbox-n-of-m))
1225 ((equal op "X%") (setq f 'checkbox-percent))
1227 (push (list prop title width op f printf) org-columns-current-fmt-compiled))
1228 (setq org-columns-current-fmt-compiled
1229 (nreverse org-columns-current-fmt-compiled))))
1232 ;;; Dynamic block for Column view
1234 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
1235 "Get the column view of the current buffer or subtree.
1236 The first optional argument MAXLEVEL sets the level limit. A
1237 second optional argument SKIP-EMPTY-ROWS tells whether to skip
1238 empty rows, an empty row being one where all the column view
1239 specifiers except ITEM are empty. This function returns a list
1240 containing the title row and all other rows. Each row is a list
1241 of fields."
1242 (if (featurep 'xemacs)
1243 (save-excursion
1244 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1245 (n (length title)) row tbl)
1246 (goto-char (point-min))
1248 (while (re-search-forward "^\\(\\*+\\) " nil t)
1249 (when (and (or (null maxlevel)
1250 (>= maxlevel
1251 (if org-odd-levels-only
1252 (/ (1+ (length (match-string 1))) 2)
1253 (length (match-string 1)))))
1254 (get-char-property (match-beginning 0) 'org-columns-key))
1255 (goto-char (match-beginning 0))
1256 (setq row nil)
1257 (loop for i from 0 to (1- n) do
1258 (push (or (get-char-property (point)
1259 'org-columns-value-modified)
1260 (get-char-property (point) 'org-columns-value)
1262 row)
1263 (org-columns-forward-char))
1264 (setq row (nreverse row))
1265 (unless (and skip-empty-rows
1266 (eq 1 (length (delete "" (delete-dups row)))))
1267 (push row tbl))))
1268 (append (list title 'hline) (nreverse tbl))))
1269 (save-excursion
1270 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1271 (n (length title)) row tbl)
1272 (goto-char (point-min))
1273 (while (and (re-search-forward "^\\(\\*+\\) " nil t)
1274 (or (null maxlevel)
1275 (>= maxlevel
1276 (if org-odd-levels-only
1277 (/ (1+ (length (match-string 1))) 2)
1278 (length (match-string 1))))))
1279 (when (get-char-property (match-beginning 0) 'org-columns-key)
1280 (setq row nil)
1281 (loop for i from 0 to (1- n) do
1282 (push (or (get-char-property (+ (match-beginning 0) i)
1283 'org-columns-value-modified)
1284 (get-char-property (+ (match-beginning 0) i)
1285 'org-columns-value)
1287 row))
1288 (setq row (nreverse row))
1289 (unless (and skip-empty-rows
1290 (eq 1 (length (delete "" (delete-dups row)))))
1291 (push row tbl))))
1292 (append (list title 'hline) (nreverse tbl))))))
1294 (defun org-dblock-write:columnview (params)
1295 "Write the column view table.
1296 PARAMS is a property list of parameters:
1298 :width enforce same column widths with <N> specifiers.
1299 :id the :ID: property of the entry where the columns view
1300 should be built, as a string. When `local', call locally.
1301 When `global' call column view with the cursor at the beginning
1302 of the buffer (usually this means that the whole buffer switches
1303 to column view).
1304 :hlines When t, insert a hline before each item. When a number, insert
1305 a hline before each level <= that number.
1306 :vlines When t, make each column a colgroup to enforce vertical lines.
1307 :maxlevel When set to a number, don't capture headlines below this level.
1308 :skip-empty-rows
1309 When t, skip rows where all specifiers other than ITEM are empty."
1310 (let ((pos (move-marker (make-marker) (point)))
1311 (hlines (plist-get params :hlines))
1312 (vlines (plist-get params :vlines))
1313 (maxlevel (plist-get params :maxlevel))
1314 (skip-empty-rows (plist-get params :skip-empty-rows))
1315 tbl id idpos nfields tmp)
1316 (save-excursion
1317 (save-restriction
1318 (when (setq id (plist-get params :id))
1319 (cond ((not id) nil)
1320 ((eq id 'global) (goto-char (point-min)))
1321 ((eq id 'local) nil)
1322 ((setq idpos (org-find-entry-with-id id))
1323 (goto-char idpos))
1324 (t (error "Cannot find entry with :ID: %s" id))))
1325 (org-columns)
1326 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1327 (setq nfields (length (car tbl)))
1328 (org-columns-quit)))
1329 (goto-char pos)
1330 (move-marker pos nil)
1331 (when tbl
1332 (when (plist-get params :hlines)
1333 (setq tmp nil)
1334 (while tbl
1335 (if (eq (car tbl) 'hline)
1336 (push (pop tbl) tmp)
1337 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1338 (if (and (not (eq (car tmp) 'hline))
1339 (or (eq hlines t)
1340 (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
1341 (push 'hline tmp)))
1342 (push (pop tbl) tmp)))
1343 (setq tbl (nreverse tmp)))
1344 (when vlines
1345 (setq tbl (mapcar (lambda (x)
1346 (if (eq 'hline x) x (cons "" x)))
1347 tbl))
1348 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1349 (setq pos (point))
1350 (insert (org-listtable-to-string tbl))
1351 (when (plist-get params :width)
1352 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1353 org-columns-current-widths "|")))
1354 (goto-char pos)
1355 (org-table-align))))
1357 (defun org-listtable-to-string (tbl)
1358 "Convert a listtable TBL to a string that contains the Org-mode table.
1359 The table still need to be aligned. The resulting string has no leading
1360 and tailing newline characters."
1361 (mapconcat
1362 (lambda (x)
1363 (cond
1364 ((listp x)
1365 (concat "|" (mapconcat 'identity x "|") "|"))
1366 ((eq x 'hline) "|-|")
1367 (t (error "Garbage in listtable: %s" x))))
1368 tbl "\n"))
1370 (defun org-insert-columns-dblock ()
1371 "Create a dynamic block capturing a column view table."
1372 (interactive)
1373 (when (featurep 'xemacs) (org-columns-quit))
1374 (let ((defaults '(:name "columnview" :hlines 1))
1375 (id (org-ido-completing-read
1376 "Capture columns (local, global, entry with :ID: property) [local]: "
1377 (append '(("global") ("local"))
1378 (mapcar 'list (org-property-values "ID"))))))
1379 (if (equal id "") (setq id 'local))
1380 (if (equal id "global") (setq id 'global))
1381 (setq defaults (append defaults (list :id id)))
1382 (org-create-dblock defaults)
1383 (org-update-dblock)))
1385 ;;; Column view in the agenda
1387 (defvar org-agenda-view-columns-initially nil
1388 "When set, switch to columns view immediately after creating the agenda.")
1390 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1391 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1392 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1394 (defun org-agenda-columns ()
1395 "Turn on or update column view in the agenda."
1396 (interactive)
1397 (org-verify-version 'columns)
1398 (org-columns-remove-overlays)
1399 (move-marker org-columns-begin-marker (point))
1400 (let (fmt cache maxwidths m p a d)
1401 (cond
1402 ((and (boundp 'org-agenda-overriding-columns-format)
1403 org-agenda-overriding-columns-format)
1404 (setq fmt org-agenda-overriding-columns-format)
1405 (org-set-local 'org-agenda-overriding-columns-format fmt))
1406 ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
1407 (setq fmt (or (org-entry-get m "COLUMNS" t)
1408 (with-current-buffer (marker-buffer m)
1409 org-columns-default-format))))
1410 ((and (boundp 'org-columns-current-fmt)
1411 (local-variable-p 'org-columns-current-fmt (current-buffer))
1412 org-columns-current-fmt)
1413 (setq fmt org-columns-current-fmt))
1414 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
1415 (setq m (get-text-property m 'org-hd-marker))
1416 (setq fmt (or (org-entry-get m "COLUMNS" t)
1417 (with-current-buffer (marker-buffer m)
1418 org-columns-default-format)))))
1419 (setq fmt (or fmt org-columns-default-format))
1420 (org-set-local 'org-columns-current-fmt fmt)
1421 (org-columns-compile-format fmt)
1422 (when org-agenda-columns-compute-summary-properties
1423 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1424 (save-excursion
1425 ;; Get and cache the properties
1426 (goto-char (point-min))
1427 (while (not (eobp))
1428 (when (setq m (or (get-text-property (point) 'org-hd-marker)
1429 (get-text-property (point) 'org-marker)))
1430 (setq p (org-entry-properties m))
1432 (when (or (not (setq a (assoc org-effort-property p)))
1433 (not (string-match "\\S-" (or (cdr a) ""))))
1434 ;; OK, the property is not defined. Use appointment duration?
1435 (when (and org-agenda-columns-add-appointments-to-effort-sum
1436 (setq d (get-text-property (point) 'duration)))
1437 (setq d (org-minutes-to-hh:mm-string d))
1438 (put-text-property 0 (length d) 'face 'org-warning d)
1439 (push (cons org-effort-property d) p)))
1440 (push (cons (org-current-line) p) cache))
1441 (beginning-of-line 2))
1442 (when cache
1443 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
1444 (org-set-local 'org-columns-current-maxwidths maxwidths)
1445 (org-columns-display-here-title)
1446 (mapc (lambda (x)
1447 (goto-line (car x))
1448 (org-columns-display-here (cdr x)))
1449 cache)
1450 (when org-agenda-columns-show-summaries
1451 (org-agenda-colview-summarize cache))))))
1453 (defun org-agenda-colview-summarize (cache)
1454 "Summarize the summarizable columns in column view in the agenda.
1455 This will add overlays to the date lines, to show the summary for each day."
1456 (let* ((fmt (mapcar (lambda (x)
1457 (list (car x) (if (equal (car x) "CLOCKSUM")
1458 'add_times (nth 4 x))))
1459 org-columns-current-fmt-compiled))
1460 line c c1 stype props lsum entries prop v)
1461 (catch 'exit
1462 (when (delq nil (mapcar 'cadr fmt))
1463 ;; OK, at least one summation column, it makes sense to try this
1464 (goto-char (point-max))
1465 (while t
1466 (when (or (get-text-property (point) 'org-date-line)
1467 (eq (get-text-property (point) 'face)
1468 'org-agenda-structure))
1469 ;; OK, this is a date line that should be used
1470 (setq line (org-current-line))
1471 (setq entries nil c cache cache nil)
1472 (while (setq c1 (pop c))
1473 (if (> (car c1) line)
1474 (push c1 entries)
1475 (push c1 cache)))
1476 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1477 ;; Compute the summaries for the properties we want,
1478 ;; set nil properties for the rest.
1479 (when (setq entries (mapcar 'cdr entries))
1480 (setq props
1481 (mapcar
1482 (lambda (f)
1483 (setq prop (car f) stype (nth 1 f))
1484 (cond
1485 ((equal prop "ITEM")
1486 (cons prop (buffer-substring (point-at-bol)
1487 (point-at-eol))))
1488 ((not stype) (cons prop ""))
1490 ;; do the summary
1491 (setq lsum 0)
1492 (mapc (lambda (x)
1493 (setq v (cdr (assoc prop x)))
1494 (if v (setq lsum (+ lsum
1495 (org-column-string-to-number
1496 v stype)))))
1497 entries)
1498 (setq lsum (org-columns-number-to-string lsum stype))
1499 (put-text-property
1500 0 (length lsum) 'face 'bold lsum)
1501 (cons prop lsum))))
1502 fmt))
1503 (org-columns-display-here props)
1504 (org-set-local 'org-agenda-columns-active t)))
1505 (if (bobp) (throw 'exit t))
1506 (beginning-of-line 0))))))
1508 (defun org-agenda-colview-compute (fmt)
1509 "Compute the relevant columns in the contributing source buffers."
1510 (let ((files org-agenda-contributing-files)
1511 (org-columns-begin-marker (make-marker))
1512 (org-columns-top-level-marker (make-marker))
1513 f fm a b)
1514 (while (setq f (pop files))
1515 (setq b (find-buffer-visiting f))
1516 (with-current-buffer (or (buffer-base-buffer b) b)
1517 (save-excursion
1518 (save-restriction
1519 (widen)
1520 (org-unmodified
1521 (remove-text-properties (point-min) (point-max)
1522 '(org-summaries t)))
1523 (goto-char (point-min))
1524 (org-columns-get-format-and-top-level)
1525 (while (setq fm (pop fmt))
1526 (if (equal (car fm) "CLOCKSUM")
1527 (org-clock-sum)
1528 (when (and (nth 4 fm)
1529 (setq a (assoc (car fm)
1530 org-columns-current-fmt-compiled))
1531 (equal (nth 4 a) (nth 4 fm)))
1532 (org-columns-compute (car fm)))))))))))
1534 (provide 'org-colview)
1535 (provide 'org-colview-xemacs)
1537 ;;; org-colview-xemacs.el ends here