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