Release 6.29c
[org-mode/org-tableheadings.git] / lisp / org-colview-xemacs.el
blob35793098943a94e221282bc45f1ec0306eabb648
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.29c
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 (defvar org-colview-initial-truncate-line-value nil
473 "Remember the value of `truncate-lines' across colview.")
475 (defun org-columns-remove-overlays ()
476 "Remove all currently active column overlays."
477 (interactive)
478 (when (marker-buffer org-columns-begin-marker)
479 (with-current-buffer (marker-buffer org-columns-begin-marker)
480 (when (local-variable-p 'org-previous-header-line-format (current-buffer))
481 (if (featurep 'xemacs)
482 (set-specifier top-gutter
483 (make-gutter-specifier
484 (cons (current-buffer)
485 (cdar org-previous-header-line-format))))
486 (setq header-line-format org-previous-header-line-format)
487 (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
488 (kill-local-variable 'org-previous-header-line-format))
489 (move-marker org-columns-begin-marker nil)
490 (move-marker org-columns-top-level-marker nil)
491 (org-unmodified
492 (mapc 'org-delete-overlay org-columns-overlays)
493 (setq org-columns-overlays nil)
494 (let ((inhibit-read-only t))
495 (remove-text-properties (point-min) (point-max) '(read-only t))))
496 (when (local-variable-p 'org-colview-initial-truncate-line-value
497 (current-buffer))
498 (setq truncate-lines org-colview-initial-truncate-line-value)))))
501 (defun org-columns-cleanup-item (item fmt)
502 "Remove from ITEM what is a column in the format FMT."
503 (if (not org-complex-heading-regexp)
504 item
505 (when (string-match org-complex-heading-regexp item)
506 (setq item
507 (concat
508 (org-add-props (match-string 1 item) nil
509 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
510 (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
511 (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
512 " " (save-match-data (org-columns-compact-links (match-string 4 item)))
513 (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))
514 (add-text-properties
515 0 (1+ (match-end 1))
516 (list 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
517 item)
518 item)))
520 (defun org-columns-compact-links (s)
521 "Replace [[link][desc]] with [desc] or [link]."
522 (while (string-match org-bracket-link-regexp s)
523 (setq s (replace-match
524 (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
525 t t s)))
528 (defvar org-agenda-columns-remove-prefix-from-item)
529 (defun org-agenda-columns-cleanup-item (item pl cphr fmt)
530 "Cleanup the time property for agenda column view.
531 See also the variable `org-agenda-columns-remove-prefix-from-item'."
532 (let* ((org-complex-heading-regexp cphr)
533 (prefix (substring item 0 pl))
534 (rest (substring item pl))
535 (fake (concat "* " rest))
536 (cleaned (org-trim (substring (org-columns-cleanup-item fake fmt) 1))))
537 (if org-agenda-columns-remove-prefix-from-item
538 cleaned
539 (concat prefix cleaned))))
541 (defun org-columns-show-value ()
542 "Show the full value of the property."
543 (interactive)
544 (let ((value (get-char-property (point) 'org-columns-value)))
545 (message "Value is: %s" (or value ""))))
547 (defvar org-agenda-columns-active) ;; defined in org-agenda.el
548 (defun org-columns-quit ()
549 "Remove the column overlays and in this way exit column editing."
550 (interactive)
551 (org-unmodified
552 (org-columns-remove-overlays)
553 (let ((inhibit-read-only t))
554 (remove-text-properties (point-min) (point-max) '(read-only t))))
555 (when (eq major-mode 'org-agenda-mode)
556 (setq org-agenda-columns-active nil)
557 (message
558 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
560 (defun org-columns-check-computed ()
561 "Check if this column value is computed.
562 If yes, throw an error indicating that changing it does not make sense."
563 (let ((val (get-char-property (point) 'org-columns-value)))
564 (when (and (stringp val)
565 (get-char-property 0 'org-computed val))
566 (error "This value is computed from the entry's children"))))
568 (defun org-columns-todo (&optional arg)
569 "Change the TODO state during column view."
570 (interactive "P")
571 (org-columns-edit-value "TODO"))
573 (defun org-columns-set-tags-or-toggle (&optional arg)
574 "Toggle checkbox at point, or set tags for current headline."
575 (interactive "P")
576 (if (string-match "\\`\\[[ xX-]\\]\\'"
577 (get-char-property (point) 'org-columns-value))
578 (org-columns-next-allowed-value)
579 (org-columns-edit-value "TAGS")))
581 (defun org-columns-edit-value (&optional key)
582 "Edit the value of the property at point in column view.
583 Where possible, use the standard interface for changing this line."
584 (interactive)
585 (org-columns-check-computed)
586 (let* ((col (current-column))
587 (key (or key (get-char-property (point) 'org-columns-key)))
588 (value (get-char-property (point) 'org-columns-value))
589 (bol (point-at-bol)) (eol (point-at-eol))
590 (pom (or (get-text-property bol 'org-hd-marker)
591 (point))) ; keep despite of compiler warning
592 (line-overlays
593 (delq nil (mapcar (lambda (x)
594 (and (eq (org-overlay-buffer x) (current-buffer))
595 (>= (org-overlay-start x) bol)
596 (<= (org-overlay-start x) eol)
598 org-columns-overlays)))
599 nval eval allowed)
600 (cond
601 ((equal key "CLOCKSUM")
602 (error "This special column cannot be edited"))
603 ((equal key "ITEM")
604 (setq eval '(org-with-point-at pom (org-edit-headline))))
605 ((equal key "TODO")
606 (setq eval '(org-with-point-at
608 (call-interactively 'org-todo))))
609 ((equal key "PRIORITY")
610 (setq eval '(org-with-point-at pom
611 (call-interactively 'org-priority))))
612 ((equal key "TAGS")
613 (setq eval '(org-with-point-at
615 (let ((org-fast-tag-selection-single-key
616 (if (eq org-fast-tag-selection-single-key 'expert)
617 t org-fast-tag-selection-single-key)))
618 (call-interactively 'org-set-tags)))))
619 ((equal key "DEADLINE")
620 (setq eval '(org-with-point-at
622 (call-interactively 'org-deadline))))
623 ((equal key "SCHEDULED")
624 (setq eval '(org-with-point-at
626 (call-interactively 'org-schedule))))
628 (setq allowed (org-property-get-allowed-values pom key 'table))
629 (if allowed
630 (setq nval (org-ido-completing-read "Value: " allowed nil t))
631 (setq nval (read-string "Edit: " value)))
632 (setq nval (org-trim nval))
633 (when (not (equal nval value))
634 (setq eval '(org-entry-put pom key nval)))))
635 (when eval
637 (cond
638 ((equal major-mode 'org-agenda-mode)
639 (org-columns-eval eval)
640 ;; The following let preserves the current format, and makes sure
641 ;; that in only a single file things need to be upated.
642 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
643 (buffer (marker-buffer pom))
644 (org-agenda-contributing-files
645 (list (with-current-buffer buffer
646 (buffer-file-name (buffer-base-buffer))))))
647 (org-agenda-columns)))
649 (let ((inhibit-read-only t))
650 (org-unmodified
651 (remove-text-properties
652 (max (point-min) (1- bol)) eol '(read-only t)))
653 (unwind-protect
654 (progn
655 (setq org-columns-overlays
656 (org-delete-all line-overlays org-columns-overlays))
657 (mapc 'org-delete-overlay line-overlays)
658 (org-columns-eval eval))
659 (org-columns-display-here)))
660 (org-move-to-column col)
661 (if (and (org-mode-p)
662 (nth 3 (assoc key org-columns-current-fmt-compiled)))
663 (org-columns-update key)))))))
665 (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
666 "Edit the current headline, the part without TODO keyword, TAGS."
667 (org-back-to-heading)
668 (when (looking-at org-todo-line-regexp)
669 (let ((pos (point))
670 (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
671 (txt (match-string 3))
672 (post "")
673 txt2)
674 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
675 (setq post (match-string 0 txt)
676 txt (substring txt 0 (match-beginning 0))))
677 (setq txt2 (read-string "Edit: " txt))
678 (when (not (equal txt txt2))
679 (goto-char pos)
680 (insert pre txt2 post)
681 (delete-region (point) (point-at-eol))
682 (org-set-tags nil t)))))
684 (defun org-columns-edit-allowed ()
685 "Edit the list of allowed values for the current property."
686 (interactive)
687 (let* ((pom (or (get-text-property (point-at-bol) 'org-marker)
688 (get-text-property (point-at-bol) 'org-hd-marker)
689 (point)))
690 (key (get-char-property (point) 'org-columns-key))
691 (key1 (concat key "_ALL"))
692 (allowed (org-entry-get pom key1 t))
693 nval)
694 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
695 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
696 (setq nval (read-string "Allowed: " allowed))
697 (org-entry-put
698 (cond ((marker-position org-entry-property-inherited-from)
699 org-entry-property-inherited-from)
700 ((marker-position org-columns-top-level-marker)
701 org-columns-top-level-marker)
702 (t pom))
703 key1 nval)))
705 (defun org-columns-eval (form)
706 (let (hidep)
707 (save-excursion
708 (beginning-of-line 1)
709 ;; `next-line' is needed here, because it skips invisible line.
710 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
711 (setq hidep (org-on-heading-p 1)))
712 (eval form)
713 (and hidep (hide-entry))))
715 (defun org-columns-previous-allowed-value ()
716 "Switch to the previous allowed value for this column."
717 (interactive)
718 (org-columns-next-allowed-value t))
720 (defun org-columns-next-allowed-value (&optional previous nth)
721 "Switch to the next allowed value for this column.
722 When PREVIOUS is set, go to the previous value. When NTH is
723 an integer, select that value."
724 (interactive)
725 (org-columns-check-computed)
726 (let* ((col (current-column))
727 (key (get-char-property (point) 'org-columns-key))
728 (value (get-char-property (point) 'org-columns-value))
729 (bol (point-at-bol)) (eol (point-at-eol))
730 (pom (or (get-text-property bol 'org-hd-marker)
731 (point))) ; keep despite of compiler waring
732 (line-overlays
733 (delq nil (mapcar (lambda (x)
734 (and (eq (org-overlay-buffer x) (current-buffer))
735 (>= (org-overlay-start x) bol)
736 (<= (org-overlay-start x) eol)
738 org-columns-overlays)))
739 (allowed (or (org-property-get-allowed-values pom key)
740 (and (memq
741 (nth 4 (assoc key org-columns-current-fmt-compiled))
742 '(checkbox checkbox-n-of-m checkbox-percent))
743 '("[ ]" "[X]"))
744 (org-colview-construct-allowed-dates value)))
745 nval)
746 (when (integerp nth)
747 (setq nth (1- nth))
748 (if (= nth -1) (setq nth 9)))
749 (when (equal key "ITEM")
750 (error "Cannot edit item headline from here"))
751 (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
752 (error "Allowed values for this property have not been defined"))
753 (if (member key '("SCHEDULED" "DEADLINE"))
754 (setq nval (if previous 'earlier 'later))
755 (if previous (setq allowed (reverse allowed)))
756 (cond
757 (nth
758 (setq nval (nth nth allowed))
759 (if (not nval)
760 (error "There are only %d allowed values for property `%s'"
761 (length allowed) key)))
762 ((member value allowed)
763 (setq nval (or (car (cdr (member value allowed)))
764 (car allowed)))
765 (if (equal nval value)
766 (error "Only one allowed value for this property")))
767 (t (setq nval (car allowed)))))
768 (cond
769 ((equal major-mode 'org-agenda-mode)
770 (org-columns-eval '(org-entry-put pom key nval))
771 ;; The following let preserves the current format, and makes sure
772 ;; that in only a single file things need to be upated.
773 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
774 (buffer (marker-buffer pom))
775 (org-agenda-contributing-files
776 (list (with-current-buffer buffer
777 (buffer-file-name (buffer-base-buffer))))))
778 (org-agenda-columns)))
780 (let ((inhibit-read-only t))
781 (remove-text-properties (1- bol) eol '(read-only t))
782 (unwind-protect
783 (progn
784 (setq org-columns-overlays
785 (org-delete-all line-overlays org-columns-overlays))
786 (mapc 'org-delete-overlay line-overlays)
787 (org-columns-eval '(org-entry-put pom key nval)))
788 (org-columns-display-here)))
789 (org-move-to-column col)
790 (and (nth 3 (assoc key org-columns-current-fmt-compiled))
791 (org-columns-update key))))))
793 (defun org-colview-construct-allowed-dates (s)
794 "Construct a list of three dates around the date in S.
795 This respects the format of the time stamp in S, active or non-active,
796 and also including time or not. S must be just a time stamp, no text
797 around it."
798 (when (and s (string-match (concat "^" org-ts-regexp3 "$") s))
799 (let* ((time (org-parse-time-string s 'nodefaults))
800 (active (equal (string-to-char s) ?<))
801 (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
802 time-before time-after)
803 (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
804 (setf (car time) (or (car time) 0))
805 (setf (nth 1 time) (or (nth 1 time) 0))
806 (setf (nth 2 time) (or (nth 2 time) 0))
807 (setq time-before (copy-sequence time))
808 (setq time-after (copy-sequence time))
809 (setf (nth 3 time-before) (1- (nth 3 time)))
810 (setf (nth 3 time-after) (1+ (nth 3 time)))
811 (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
812 (list time-before time time-after)))))
814 (defun org-verify-version (task)
815 (cond
816 ((eq task 'columns)
817 (if (or (and (featurep 'xemacs) (not (featurep 'org-colview-xemacs)))
818 (and (not (featurep 'xemacs)) (< emacs-major-version 22)))
819 (error "This version of Emacs cannot run Column View")))))
821 (defun org-columns-open-link (&optional arg)
822 (interactive "P")
823 (let ((value (get-char-property (point) 'org-columns-value)))
824 (org-open-link-from-string value arg)))
826 (defun org-columns-get-format-and-top-level ()
827 (let (fmt)
828 (when (condition-case nil (org-back-to-heading) (error nil))
829 (setq fmt (org-entry-get nil "COLUMNS" t)))
830 (setq fmt (or fmt org-columns-default-format))
831 (org-set-local 'org-columns-current-fmt fmt)
832 (org-columns-compile-format fmt)
833 (if (marker-position org-entry-property-inherited-from)
834 (move-marker org-columns-top-level-marker
835 org-entry-property-inherited-from)
836 (move-marker org-columns-top-level-marker (point)))
837 fmt))
839 (defun org-columns ()
840 "Turn on column view on an org-mode file."
841 (interactive)
842 (org-verify-version 'columns)
843 (when (featurep 'xemacs)
844 (set-face-foreground 'org-columns-space
845 (face-background 'org-columns-space)))
846 (org-columns-remove-overlays)
847 (move-marker org-columns-begin-marker (point))
848 (let (beg end fmt cache maxwidths)
849 (setq fmt (org-columns-get-format-and-top-level))
850 (save-excursion
851 (goto-char org-columns-top-level-marker)
852 (setq beg (point))
853 (unless org-columns-inhibit-recalculation
854 (org-columns-compute-all))
855 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
856 (point-max)))
857 ;; Get and cache the properties
858 (goto-char beg)
859 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
860 (save-excursion
861 (save-restriction
862 (narrow-to-region beg end)
863 (org-clock-sum))))
864 (while (re-search-forward (concat "^" outline-regexp) end t)
865 (if (and org-columns-skip-arrchived-trees
866 (looking-at (concat ".*:" org-archive-tag ":")))
867 (org-end-of-subtree t)
868 (push (cons (org-current-line) (org-entry-properties)) cache)))
869 (when cache
870 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
871 (org-set-local 'org-columns-current-maxwidths maxwidths)
872 (org-columns-display-here-title)
873 (unless (local-variable-p 'org-colview-initial-truncate-line-value
874 (current-buffer))
875 (org-set-local 'org-colview-initial-truncate-line-value
876 truncate-lines))
877 (setq truncate-lines t)
878 (mapc (lambda (x)
879 (goto-line (car x))
880 (org-columns-display-here (cdr x)))
881 cache)))))
883 (defvar org-columns-compile-map
884 '(("none" none +)
885 (":" add_times +)
886 ("+" add_numbers +)
887 ("$" currency +)
888 ("X" checkbox +)
889 ("X/" checkbox-n-of-m +)
890 ("X%" checkbox-percent +)
891 ("max" max_numbers max)
892 ("min" min_numbers min)
893 ("mean" mean_numbers (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
894 (":max" max_times max)
895 (":min" min_times min)
896 (":mean" mean_times (lambda (&rest x) (/ (apply '+ x) (float (length x))))))
897 "Operator <-> format,fuction map.
898 Used to compile/uncompile columns format and completing read in
899 interactive function org-columns-new.")
901 (defun org-columns-new (&optional prop title width op fmt fun &rest rest)
902 "Insert a new column, to the left of the current column."
903 (interactive)
904 (let ((n (org-columns-current-column))
905 (editp (and prop (assoc prop org-columns-current-fmt-compiled)))
906 cell)
907 (setq prop (org-ido-completing-read
908 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
909 nil nil prop))
910 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
911 (setq width (read-string "Column width: " (if width (number-to-string width))))
912 (if (string-match "\\S-" width)
913 (setq width (string-to-number width))
914 (setq width nil))
915 (setq fmt (org-ido-completing-read "Summary [none]: "
916 (mapcar (lambda (x) (list (symbol-name (cadr x)))) org-columns-compile-map)
917 nil t))
918 (setq fmt (intern fmt)
919 fun (cadr (assoc fmt (mapcar 'cdr org-columns-compile-map))))
920 (if (eq fmt 'none) (setq fmt nil))
921 (if editp
922 (progn
923 (setcar editp prop)
924 (setcdr editp (list title width nil fmt nil fun)))
925 (setq cell (nthcdr (1- n) org-columns-current-fmt-compiled))
926 (setcdr cell (cons (list prop title width nil fmt nil fun)
927 (cdr cell))))
928 (org-columns-store-format)
929 (org-columns-redo)))
931 (defun org-columns-delete ()
932 "Delete the column at point from columns view."
933 (interactive)
934 (let* ((n (org-columns-current-column))
935 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
936 (when (y-or-n-p
937 (format "Are you sure you want to remove column \"%s\"? " title))
938 (setq org-columns-current-fmt-compiled
939 (delq (nth n org-columns-current-fmt-compiled)
940 org-columns-current-fmt-compiled))
941 (org-columns-store-format)
942 (org-columns-redo)
943 (if (>= (org-columns-current-column)
944 (length org-columns-current-fmt-compiled))
945 (org-columns-backward-char)))))
947 (defun org-columns-edit-attributes ()
948 "Edit the attributes of the current column."
949 (interactive)
950 (let* ((n (org-columns-current-column))
951 (info (nth n org-columns-current-fmt-compiled)))
952 (apply 'org-columns-new info)))
954 (defun org-columns-widen (arg)
955 "Make the column wider by ARG characters."
956 (interactive "p")
957 (let* ((n (org-columns-current-column))
958 (entry (nth n org-columns-current-fmt-compiled))
959 (width (or (nth 2 entry)
960 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
961 (setq width (max 1 (+ width arg)))
962 (setcar (nthcdr 2 entry) width)
963 (org-columns-store-format)
964 (org-columns-redo)))
966 (defun org-columns-narrow (arg)
967 "Make the column narrower by ARG characters."
968 (interactive "p")
969 (org-columns-widen (- arg)))
971 (defun org-columns-move-right ()
972 "Swap this column with the one to the right."
973 (interactive)
974 (let* ((n (org-columns-current-column))
975 (cell (nthcdr n org-columns-current-fmt-compiled))
977 (when (>= n (1- (length org-columns-current-fmt-compiled)))
978 (error "Cannot shift this column further to the right"))
979 (setq e (car cell))
980 (setcar cell (car (cdr cell)))
981 (setcdr cell (cons e (cdr (cdr cell))))
982 (org-columns-store-format)
983 (org-columns-redo)
984 (org-columns-forward-char)))
986 (defun org-columns-move-left ()
987 "Swap this column with the one to the left."
988 (interactive)
989 (let* ((n (org-columns-current-column)))
990 (when (= n 0)
991 (error "Cannot shift this column further to the left"))
992 (org-columns-backward-char)
993 (org-columns-move-right)
994 (org-columns-backward-char)))
996 (defun org-columns-store-format ()
997 "Store the text version of the current columns format in appropriate place.
998 This is either in the COLUMNS property of the node starting the current column
999 display, or in the #+COLUMNS line of the current buffer."
1000 (let (fmt (cnt 0))
1001 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
1002 (org-set-local 'org-columns-current-fmt fmt)
1003 (if (marker-position org-columns-top-level-marker)
1004 (save-excursion
1005 (goto-char org-columns-top-level-marker)
1006 (if (and (org-at-heading-p)
1007 (org-entry-get nil "COLUMNS"))
1008 (org-entry-put nil "COLUMNS" fmt)
1009 (goto-char (point-min))
1010 ;; Overwrite all #+COLUMNS lines....
1011 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
1012 (setq cnt (1+ cnt))
1013 (replace-match (concat "#+COLUMNS: " fmt) t t))
1014 (unless (> cnt 0)
1015 (goto-char (point-min))
1016 (or (org-on-heading-p t) (outline-next-heading))
1017 (let ((inhibit-read-only t))
1018 (insert-before-markers "#+COLUMNS: " fmt "\n")))
1019 (org-set-local 'org-columns-default-format fmt))))))
1021 (defvar org-agenda-overriding-columns-format nil
1022 "When set, overrides any other format definition for the agenda.
1023 Don't set this, this is meant for dynamic scoping.")
1025 (defun org-columns-get-autowidth-alist (s cache)
1026 "Derive the maximum column widths from the format and the cache."
1027 (let ((start 0) rtn)
1028 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
1029 (push (cons (match-string 1 s) 1) rtn)
1030 (setq start (match-end 0)))
1031 (mapc (lambda (x)
1032 (setcdr x (apply 'max
1033 (mapcar
1034 (lambda (y)
1035 (length (or (cdr (assoc (car x) (cdr y))) " ")))
1036 cache))))
1037 rtn)
1038 rtn))
1040 (defun org-columns-compute-all ()
1041 "Compute all columns that have operators defined."
1042 (org-unmodified
1043 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
1044 (let ((columns org-columns-current-fmt-compiled) col)
1045 (while (setq col (pop columns))
1046 (when (nth 3 col)
1047 (save-excursion
1048 (org-columns-compute (car col)))))))
1050 (defun org-columns-update (property)
1051 "Recompute PROPERTY, and update the columns display for it."
1052 (org-columns-compute property)
1053 (let (fmt val pos face)
1054 (save-excursion
1055 (mapc (lambda (ov)
1056 (when (equal (org-overlay-get ov 'org-columns-key) property)
1057 (setq pos (org-overlay-start ov))
1058 (goto-char pos)
1059 (when (setq val (cdr (assoc property
1060 (get-text-property
1061 (point-at-bol) 'org-summaries))))
1062 (setq fmt (org-overlay-get ov 'org-columns-format))
1063 (org-overlay-put ov 'org-columns-value val)
1064 (if (featurep 'xemacs)
1065 (progn
1066 (setq face (glyph-face (extent-end-glyph ov)))
1067 (org-overlay-display ov (format fmt val) face))
1068 (org-overlay-display ov (format fmt val))))))
1069 org-columns-overlays))))
1071 (defun org-columns-compute (property)
1072 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
1073 (interactive)
1074 (let* ((re (concat "^" outline-regexp))
1075 (lmax 30) ; Does anyone use deeper levels???
1076 (lvals (make-vector lmax nil))
1077 (lflag (make-vector lmax nil))
1078 (level 0)
1079 (ass (assoc property org-columns-current-fmt-compiled))
1080 (format (nth 4 ass))
1081 (printf (nth 5 ass))
1082 (fun (nth 6 ass))
1083 (beg org-columns-top-level-marker)
1084 last-level val valflag flag end sumpos sum-alist sum str str1 useval)
1085 (save-excursion
1086 ;; Find the region to compute
1087 (goto-char beg)
1088 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
1089 (goto-char end)
1090 ;; Walk the tree from the back and do the computations
1091 (while (re-search-backward re beg t)
1092 (setq sumpos (match-beginning 0)
1093 last-level level
1094 level (org-outline-level)
1095 val (org-entry-get nil property)
1096 valflag (and val (string-match "\\S-" val)))
1097 (cond
1098 ((< level last-level)
1099 ;; put the sum of lower levels here as a property
1100 (setq sum (when (aref lvals last-level)
1101 (apply fun (aref lvals last-level)))
1102 flag (aref lflag last-level) ; any valid entries from children?
1103 str (org-columns-number-to-string sum format printf)
1104 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
1105 useval (if flag str1 (if valflag val ""))
1106 sum-alist (get-text-property sumpos 'org-summaries))
1107 (if (assoc property sum-alist)
1108 (setcdr (assoc property sum-alist) useval)
1109 (push (cons property useval) sum-alist)
1110 (org-unmodified
1111 (add-text-properties sumpos (1+ sumpos)
1112 (list 'org-summaries sum-alist))))
1113 (when (and val (not (equal val (if flag str val))))
1114 (org-entry-put nil property (if flag str val)))
1115 ;; add current to current level accumulator
1116 (when (or flag valflag)
1117 (push (if flag sum
1118 (org-column-string-to-number (if flag str val) format))
1119 (aref lvals level))
1120 (aset lflag level t))
1121 ;; clear accumulators for deeper levels
1122 (loop for l from (1+ level) to (1- lmax) do
1123 (aset lvals l nil)
1124 (aset lflag l nil)))
1125 ((>= level last-level)
1126 ;; add what we have here to the accumulator for this level
1127 (when valflag
1128 (push (org-column-string-to-number val format)
1129 (aref lvals level))
1130 (aset lflag level t)))
1131 (t (error "This should not happen")))))))
1133 (defun org-columns-redo ()
1134 "Construct the column display again."
1135 (interactive)
1136 (message "Recomputing columns...")
1137 (save-excursion
1138 (if (marker-position org-columns-begin-marker)
1139 (goto-char org-columns-begin-marker))
1140 (org-columns-remove-overlays)
1141 (if (org-mode-p)
1142 (call-interactively 'org-columns)
1143 (org-agenda-redo)
1144 (call-interactively 'org-agenda-columns)))
1145 (when (featurep 'xemacs)
1146 (while (not (or (eolp)
1147 (member (extent-at (point)) org-columns-overlays)))
1148 (forward-char)))
1149 (message "Recomputing columns...done"))
1151 (defun org-columns-not-in-agenda ()
1152 (if (eq major-mode 'org-agenda-mode)
1153 (error "This command is only allowed in Org-mode buffers")))
1156 (defun org-string-to-number (s)
1157 "Convert string to number, and interpret hh:mm:ss."
1158 (if (not (string-match ":" s))
1159 (string-to-number 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)))
1165 (defun org-columns-number-to-string (n fmt &optional printf)
1166 "Convert a computed column number to a string value, according to FMT."
1167 (cond
1168 ((not (numberp n)) "")
1169 ((memq fmt '(add_times max_times min_times mean_times))
1170 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
1171 (format org-time-clocksum-format h m)))
1172 ((eq fmt 'checkbox)
1173 (cond ((= n (floor n)) "[X]")
1174 ((> n 1.) "[-]")
1175 (t "[ ]")))
1176 ((memq fmt '(checkbox-n-of-m checkbox-percent))
1177 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
1178 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
1179 (printf (format printf n))
1180 ((eq fmt 'currency)
1181 (format "%.2f" n))
1182 (t (number-to-string n))))
1184 (defun org-nofm-to-completion (n m &optional percent)
1185 (if (not percent)
1186 (format "[%d/%d]" n m)
1187 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
1189 (defun org-column-string-to-number (s fmt)
1190 "Convert a column value to a number that can be used for column computing."
1191 (cond
1192 ((string-match ":" s)
1193 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1194 (while l
1195 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1196 sum))
1197 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
1198 (if (equal s "[X]") 1. 0.000001))
1199 (t (string-to-number s))))
1201 (defun org-columns-uncompile-format (cfmt)
1202 "Turn the compiled columns format back into a string representation."
1203 (let ((rtn "") e s prop title op op-match width fmt printf)
1204 (while (setq e (pop cfmt))
1205 (setq prop (car e)
1206 title (nth 1 e)
1207 width (nth 2 e)
1208 op (nth 3 e)
1209 fmt (nth 4 e)
1210 printf (nth 5 e)
1211 fun (nth 6 e))
1212 (when (setq op-match (rassoc (list fmt fun) org-columns-compile-map))
1213 (setq op (car op-match)))
1214 (if (and op printf) (setq op (concat op ";" printf)))
1215 (if (equal title prop) (setq title nil))
1216 (setq s (concat "%" (if width (number-to-string width))
1217 prop
1218 (if title (concat "(" title ")"))
1219 (if op (concat "{" op "}"))))
1220 (setq rtn (concat rtn " " s)))
1221 (org-trim rtn)))
1223 (defun org-columns-compile-format (fmt)
1224 "Turn a column format string into an alist of specifications.
1225 The alist has one entry for each column in the format. The elements of
1226 that list are:
1227 property the property
1228 title the title field for the columns
1229 width the column width in characters, can be nil for automatic
1230 operator the operator if any
1231 format the output format for computed results, derived from operator
1232 printf a printf format for computed values
1233 fun the lisp function to compute values, derived from operator"
1234 (let ((start 0) width prop title op op-match f printf fun)
1235 (setq org-columns-current-fmt-compiled nil)
1236 (while (string-match
1237 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
1238 fmt start)
1239 (setq start (match-end 0)
1240 width (match-string 1 fmt)
1241 prop (match-string 2 fmt)
1242 title (or (match-string 3 fmt) prop)
1243 op (match-string 4 fmt)
1244 f nil
1245 printf nil
1246 fun '+)
1247 (if width (setq width (string-to-number width)))
1248 (when (and op (string-match ";" op))
1249 (setq printf (substring op (match-end 0))
1250 op (substring op 0 (match-beginning 0))))
1251 (when (setq op-match (assoc op org-columns-compile-map))
1252 (setq f (cadr op-match)
1253 fun (caddr op-match)))
1254 (push (list prop title width op f printf fun) org-columns-current-fmt-compiled))
1255 (setq org-columns-current-fmt-compiled
1256 (nreverse org-columns-current-fmt-compiled))))
1259 ;;; Dynamic block for Column view
1261 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
1262 "Get the column view of the current buffer or subtree.
1263 The first optional argument MAXLEVEL sets the level limit. A
1264 second optional argument SKIP-EMPTY-ROWS tells whether to skip
1265 empty rows, an empty row being one where all the column view
1266 specifiers except ITEM are empty. This function returns a list
1267 containing the title row and all other rows. Each row is a list
1268 of fields."
1269 (if (featurep 'xemacs)
1270 (save-excursion
1271 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1272 (re-comment (concat "\\*+[ \t]+" org-comment-string "\\>"))
1273 (re-archive (concat ".*:" org-archive-tag ":"))
1274 (n (length title)) row tbl)
1275 (goto-char (point-min))
1277 (while (re-search-forward "^\\(\\*+\\) " nil t)
1278 (catch 'next
1279 (when (and (or (null maxlevel)
1280 (>= maxlevel
1281 (if org-odd-levels-only
1282 (/ (1+ (length (match-string 1))) 2)
1283 (length (match-string 1)))))
1284 (get-char-property (match-beginning 0) 'org-columns-key))
1285 (goto-char (match-beginning 0))
1286 (when (save-excursion
1287 (goto-char (point-at-bol))
1288 (or (looking-at re-comment)
1289 (looking-at re-archive)))
1290 (org-end-of-subtree t)
1291 (throw 'next t))
1292 (setq row nil)
1293 (loop for i from 0 to (1- n) do
1294 (push
1295 (org-quote-vert
1296 (or (get-char-property (point)
1297 'org-columns-value-modified)
1298 (get-char-property (point) 'org-columns-value)
1299 ""))
1300 row)
1301 (org-columns-forward-char))
1302 (setq row (nreverse row))
1303 (unless (and skip-empty-rows
1304 (eq 1 (length (delete "" (delete-dups (copy-sequence row))))))
1305 (push row tbl)))))
1306 (append (list title 'hline) (nreverse tbl))))
1307 (save-excursion
1308 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1309 (n (length title)) row tbl)
1310 (goto-char (point-min))
1311 (while (and (re-search-forward "^\\(\\*+\\) " nil t)
1312 (or (null maxlevel)
1313 (>= maxlevel
1314 (if org-odd-levels-only
1315 (/ (1+ (length (match-string 1))) 2)
1316 (length (match-string 1))))))
1317 (when (get-char-property (match-beginning 0) 'org-columns-key)
1318 (setq row nil)
1319 (loop for i from 0 to (1- n) do
1320 (push (or (get-char-property (+ (match-beginning 0) i)
1321 'org-columns-value-modified)
1322 (get-char-property (+ (match-beginning 0) i)
1323 'org-columns-value)
1325 row))
1326 (setq row (nreverse row))
1327 (unless (and skip-empty-rows
1328 (eq 1 (length (delete "" (delete-dups row)))))
1329 (push row tbl))))
1330 (append (list title 'hline) (nreverse tbl))))))
1332 (defun org-dblock-write:columnview (params)
1333 "Write the column view table.
1334 PARAMS is a property list of parameters:
1336 :width enforce same column widths with <N> specifiers.
1337 :id the :ID: property of the entry where the columns view
1338 should be built. When the symbol `local', call locally.
1339 When `global' call column view with the cursor at the beginning
1340 of the buffer (usually this means that the whole buffer switches
1341 to column view). When \"file:path/to/file.org\", invoke column
1342 view at the start of that file. Otherwise, the ID is located
1343 using `org-id-find'.
1344 :hlines When t, insert a hline before each item. When a number, insert
1345 a hline before each level <= that number.
1346 :vlines When t, make each column a colgroup to enforce vertical lines.
1347 :maxlevel When set to a number, don't capture headlines below this level.
1348 :skip-empty-rows
1349 When t, skip rows where all specifiers other than ITEM are empty."
1350 (let ((pos (move-marker (make-marker) (point)))
1351 (hlines (plist-get params :hlines))
1352 (vlines (plist-get params :vlines))
1353 (maxlevel (plist-get params :maxlevel))
1354 (content-lines (org-split-string (plist-get params :content) "\n"))
1355 (skip-empty-rows (plist-get params :skip-empty-rows))
1356 tbl id idpos nfields tmp recalc line
1357 id-as-string view-file view-pos)
1358 (when (setq id (plist-get params :id))
1359 (setq id-as-string (cond ((numberp id) (number-to-string id))
1360 ((symbolp id) (symbol-name id))
1361 ((stringp id) id)
1362 (t "")))
1363 (cond ((not id) nil)
1364 ((eq id 'global) (setq view-pos (point-min)))
1365 ((eq id 'local))
1366 ((string-match "^file:\\(.*\\)" id-as-string)
1367 (setq view-file (match-string 1 id-as-string)
1368 view-pos 1)
1369 (unless (file-exists-p view-file)
1370 (error "No such file: \"%s\"" id-as-string)))
1371 ((setq idpos (org-find-entry-with-id id))
1372 (setq view-pos idpos))
1373 ((setq idpos (org-id-find id))
1374 (setq view-file (car idpos))
1375 (setq view-pos (cdr idpos)))
1376 (t (error "Cannot find entry with :ID: %s" id))))
1377 (with-current-buffer (if view-file
1378 (get-file-buffer view-file)
1379 (current-buffer))
1380 (save-excursion
1381 (save-restriction
1382 (widen)
1383 (goto-char (or view-pos (point)))
1384 (org-columns)
1385 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1386 (setq nfields (length (car tbl)))
1387 (org-columns-quit))))
1388 (goto-char pos)
1389 (move-marker pos nil)
1390 (when tbl
1391 (when (plist-get params :hlines)
1392 (setq tmp nil)
1393 (while tbl
1394 (if (eq (car tbl) 'hline)
1395 (push (pop tbl) tmp)
1396 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1397 (if (and (not (eq (car tmp) 'hline))
1398 (or (eq hlines t)
1399 (and (numberp hlines)
1400 (<= (- (match-end 1) (match-beginning 1))
1401 hlines))))
1402 (push 'hline tmp)))
1403 (push (pop tbl) tmp)))
1404 (setq tbl (nreverse tmp)))
1405 (when vlines
1406 (setq tbl (mapcar (lambda (x)
1407 (if (eq 'hline x) x (cons "" x)))
1408 tbl))
1409 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1410 (setq pos (point))
1411 (when content-lines
1412 (while (string-match "^#" (car content-lines))
1413 (insert (pop content-lines) "\n")))
1414 (insert (org-listtable-to-string tbl))
1415 (when (plist-get params :width)
1416 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1417 org-columns-current-widths "|")))
1418 (while (setq line (pop content-lines))
1419 (when (string-match "^#" line)
1420 (insert "\n" line)
1421 (when (string-match "^[ \t]*#\\+TBLFM" line)
1422 (setq recalc t))))
1423 (if recalc
1424 (progn (goto-char pos) (org-table-recalculate 'all))
1425 (goto-char pos)
1426 (org-table-align)))))
1428 (defun org-listtable-to-string (tbl)
1429 "Convert a listtable TBL to a string that contains the Org-mode table.
1430 The table still need to be aligned. The resulting string has no leading
1431 and tailing newline characters."
1432 (mapconcat
1433 (lambda (x)
1434 (cond
1435 ((listp x)
1436 (concat "|" (mapconcat 'identity x "|") "|"))
1437 ((eq x 'hline) "|-|")
1438 (t (error "Garbage in listtable: %s" x))))
1439 tbl "\n"))
1441 (defun org-insert-columns-dblock ()
1442 "Create a dynamic block capturing a column view table."
1443 (interactive)
1444 (when (featurep 'xemacs) (org-columns-quit))
1445 (let ((defaults '(:name "columnview" :hlines 1))
1446 (id (org-ido-completing-read
1447 "Capture columns (local, global, entry with :ID: property) [local]: "
1448 (append '(("global") ("local"))
1449 (mapcar 'list (org-property-values "ID"))))))
1450 (if (equal id "") (setq id 'local))
1451 (if (equal id "global") (setq id 'global))
1452 (setq defaults (append defaults (list :id id)))
1453 (org-create-dblock defaults)
1454 (org-update-dblock)))
1456 ;;; Column view in the agenda
1458 (defvar org-agenda-view-columns-initially nil
1459 "When set, switch to columns view immediately after creating the agenda.")
1461 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1462 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1463 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1465 (defun org-agenda-columns ()
1466 "Turn on or update column view in the agenda."
1467 (interactive)
1468 (org-verify-version 'columns)
1469 (org-columns-remove-overlays)
1470 (move-marker org-columns-begin-marker (point))
1471 (let (fmt cache maxwidths m p a d)
1472 (cond
1473 ((and (boundp 'org-agenda-overriding-columns-format)
1474 org-agenda-overriding-columns-format)
1475 (setq fmt org-agenda-overriding-columns-format)
1476 (org-set-local 'org-agenda-overriding-columns-format fmt))
1477 ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
1478 (setq fmt (or (org-entry-get m "COLUMNS" t)
1479 (with-current-buffer (marker-buffer m)
1480 org-columns-default-format))))
1481 ((and (boundp 'org-columns-current-fmt)
1482 (local-variable-p 'org-columns-current-fmt (current-buffer))
1483 org-columns-current-fmt)
1484 (setq fmt org-columns-current-fmt))
1485 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
1486 (setq m (get-text-property m 'org-hd-marker))
1487 (setq fmt (or (org-entry-get m "COLUMNS" t)
1488 (with-current-buffer (marker-buffer m)
1489 org-columns-default-format)))))
1490 (setq fmt (or fmt org-columns-default-format))
1491 (org-set-local 'org-columns-current-fmt fmt)
1492 (org-columns-compile-format fmt)
1493 (when org-agenda-columns-compute-summary-properties
1494 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1495 (save-excursion
1496 ;; Get and cache the properties
1497 (goto-char (point-min))
1498 (while (not (eobp))
1499 (when (setq m (or (get-text-property (point) 'org-hd-marker)
1500 (get-text-property (point) 'org-marker)))
1501 (setq p (org-entry-properties m))
1503 (when (or (not (setq a (assoc org-effort-property p)))
1504 (not (string-match "\\S-" (or (cdr a) ""))))
1505 ;; OK, the property is not defined. Use appointment duration?
1506 (when (and org-agenda-columns-add-appointments-to-effort-sum
1507 (setq d (get-text-property (point) 'duration)))
1508 (setq d (org-minutes-to-hh:mm-string d))
1509 (put-text-property 0 (length d) 'face 'org-warning d)
1510 (push (cons org-effort-property d) p)))
1511 (push (cons (org-current-line) p) cache))
1512 (beginning-of-line 2))
1513 (when cache
1514 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
1515 (org-set-local 'org-columns-current-maxwidths maxwidths)
1516 (org-columns-display-here-title)
1517 (mapc (lambda (x)
1518 (goto-line (car x))
1519 (org-columns-display-here (cdr x)))
1520 cache)
1521 (when org-agenda-columns-show-summaries
1522 (org-agenda-colview-summarize cache))))))
1524 (defun org-agenda-colview-summarize (cache)
1525 "Summarize the summarizable columns in column view in the agenda.
1526 This will add overlays to the date lines, to show the summary for each day."
1527 (let* ((fmt (mapcar (lambda (x)
1528 (list (car x) (if (equal (car x) "CLOCKSUM")
1529 'add_times (nth 4 x))))
1530 org-columns-current-fmt-compiled))
1531 line c c1 stype props lsum entries prop v)
1532 (catch 'exit
1533 (when (delq nil (mapcar 'cadr fmt))
1534 ;; OK, at least one summation column, it makes sense to try this
1535 (goto-char (point-max))
1536 (while t
1537 (when (or (get-text-property (point) 'org-date-line)
1538 (eq (get-text-property (point) 'face)
1539 'org-agenda-structure))
1540 ;; OK, this is a date line that should be used
1541 (setq line (org-current-line))
1542 (setq entries nil c cache cache nil)
1543 (while (setq c1 (pop c))
1544 (if (> (car c1) line)
1545 (push c1 entries)
1546 (push c1 cache)))
1547 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1548 ;; Compute the summaries for the properties we want,
1549 ;; set nil properties for the rest.
1550 (when (setq entries (mapcar 'cdr entries))
1551 (setq props
1552 (mapcar
1553 (lambda (f)
1554 (setq prop (car f) stype (nth 1 f))
1555 (cond
1556 ((equal prop "ITEM")
1557 (cons prop (buffer-substring (point-at-bol)
1558 (point-at-eol))))
1559 ((not stype) (cons prop ""))
1561 ;; do the summary
1562 (setq lsum 0)
1563 (mapc (lambda (x)
1564 (setq v (cdr (assoc prop x)))
1565 (if v (setq lsum (+ lsum
1566 (org-column-string-to-number
1567 v stype)))))
1568 entries)
1569 (setq lsum (org-columns-number-to-string lsum stype))
1570 (put-text-property
1571 0 (length lsum) 'face 'bold lsum)
1572 (cons prop lsum))))
1573 fmt))
1574 (org-columns-display-here props)
1575 (org-set-local 'org-agenda-columns-active t)))
1576 (if (bobp) (throw 'exit t))
1577 (beginning-of-line 0))))))
1579 (defun org-agenda-colview-compute (fmt)
1580 "Compute the relevant columns in the contributing source buffers."
1581 (let ((files org-agenda-contributing-files)
1582 (org-columns-begin-marker (make-marker))
1583 (org-columns-top-level-marker (make-marker))
1584 f fm a b)
1585 (while (setq f (pop files))
1586 (setq b (find-buffer-visiting f))
1587 (with-current-buffer (or (buffer-base-buffer b) b)
1588 (save-excursion
1589 (save-restriction
1590 (widen)
1591 (org-unmodified
1592 (remove-text-properties (point-min) (point-max)
1593 '(org-summaries t)))
1594 (goto-char (point-min))
1595 (org-columns-get-format-and-top-level)
1596 (while (setq fm (pop fmt))
1597 (if (equal (car fm) "CLOCKSUM")
1598 (org-clock-sum)
1599 (when (and (nth 4 fm)
1600 (setq a (assoc (car fm)
1601 org-columns-current-fmt-compiled))
1602 (equal (nth 4 a) (nth 4 fm)))
1603 (org-columns-compute (car fm)))))))))))
1605 (provide 'org-colview)
1606 (provide 'org-colview-xemacs)
1608 ;;; org-colview-xemacs.el ends here