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