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