1 ;;; org-colview-xemacs.el --- Column View in Org-mode, XEmacs-specific version
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; This file contains the column view for Org.
34 (eval-when-compile (require 'cl
))
37 (declare-function org-agenda-redo
"org-agenda" ())
40 ;;; Define additional faces for column view
42 (when (featurep 'xemacs
)
44 (defface org-columns-level-1
;; font-lock-function-name-face
47 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1" :background
"grey90"))
48 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue" :background
"grey30"))
49 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue" :background
"grey90"))
50 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue" :background
"grey30"))
51 (((class color
) (min-colors 8)) (:foreground
"blue" :bold t
))
53 "Face used for columns-level 1 headlines."
56 (defface org-columns-level-2
;; font-lock-variable-name-face
59 '((((class color
) (min-colors 16) (background light
)) (:foreground
"DarkGoldenrod" :background
"grey90"))
60 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightGoldenrod" :background
"grey30"))
61 (((class color
) (min-colors 8) (background light
)) (:foreground
"yellow" :background
"grey90"))
62 (((class color
) (min-colors 8) (background dark
)) (:foreground
"yellow" :bold t
))
64 "Face used for columns-level 2 headlines."
67 (defface org-columns-level-3
;; font-lock-keyword-face
70 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Purple" :background
"grey90"))
71 (((class color
) (min-colors 88) (background dark
)) (:foreground
"Cyan1" :background
"grey30"))
72 (((class color
) (min-colors 16) (background light
)) (:foreground
"Purple" :background
"grey90"))
73 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Cyan" :background
"grey30"))
74 (((class color
) (min-colors 8) (background light
)) (:foreground
"purple" :bold t
))
75 (((class color
) (min-colors 8) (background dark
)) (:foreground
"cyan" :bold t
))
77 "Face used for columns-level 3 headlines."
80 (defface org-columns-level-4
;; font-lock-comment-face
83 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick" :background
"grey90"))
84 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1" :background
"grey30"))
85 (((class color
) (min-colors 16) (background light
)) (:foreground
"red"))
86 (((class color
) (min-colors 16) (background dark
)) (:foreground
"red1"))
87 (((class color
) (min-colors 8) (background light
)) (:foreground
"red" :bold t
))
88 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
90 "Face used for columns-level 4 headlines."
93 (defface org-columns-level-5
;; font-lock-type-face
96 '((((class color
) (min-colors 16) (background light
)) (:foreground
"ForestGreen" :background
"grey90"))
97 (((class color
) (min-colors 16) (background dark
)) (:foreground
"PaleGreen" :background
"grey30"))
98 (((class color
) (min-colors 8)) (:foreground
"green"))))
99 "Face used for columns-level 5 headlines."
102 (defface org-columns-level-6
;; font-lock-constant-face
105 '((((class color
) (min-colors 16) (background light
)) (:foreground
"CadetBlue" :background
"grey90"))
106 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Aquamarine" :background
"grey30"))
107 (((class color
) (min-colors 8)) (:foreground
"magenta"))))
108 "Face used for columns-level 6 headlines."
111 (defface org-columns-level-7
;; font-lock-builtin-face
114 '((((class color
) (min-colors 16) (background light
)) (:foreground
"Orchid" :background
"grey90"))
115 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSteelBlue" :background
"grey30"))
116 (((class color
) (min-colors 8)) (:foreground
"blue"))))
117 "Face used for columns-level 7 headlines."
120 (defface org-columns-level-8
;; font-lock-string-face
123 '((((class color
) (min-colors 16) (background light
)) (:foreground
"RosyBrown" :background
"grey90"))
124 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSalmon" :background
"grey30"))
125 (((class color
) (min-colors 8)) (:foreground
"green"))))
126 "Face used for columns-level 8 headlines."
130 (defface org-columns-space
;; font-lock-function-name-face
133 '((((class color
) (min-colors 88) (background light
)) (:background
"grey90"))
134 (((class color
) (min-colors 88) (background dark
)) (:background
"grey30"))
135 (((class color
) (min-colors 16) (background light
)) (:background
"grey90"))
136 (((class color
) (min-colors 16) (background dark
)) (:background
"grey30"))
137 (((class color
) (min-colors 8)) (:bold t
:underline t
))))
138 "Face used for columns space headlines."
141 (defface org-columns-space1
;; font-lock-function-name-face
144 '((((class color
) (min-colors 88) (background light
)) (:background
"grey90"))
145 (((class color
) (min-colors 88) (background dark
)) (:background
"grey30"))
146 (((class color
) (min-colors 16) (background light
)) (:background
"grey90"))
147 (((class color
) (min-colors 16) (background dark
)) (:background
"grey30"))
148 (((class color
) (min-colors 8)) (:bold t
:underline t
))))
149 "Face used for columns space headlines."
153 (when (featurep 'xemacs
)
154 (defconst org-columns-level-faces
155 '(org-columns-level-1
156 org-columns-level-2 org-columns-level-3
157 org-columns-level-4 org-columns-level-5 org-columns-level-6
158 org-columns-level-7 org-columns-level-8
161 (defun org-get-columns-level-face (n)
162 "Get the right face for match N in font-lock matching of headlines."
163 (setq org-l
(- (match-end 2) (match-beginning 1) 1))
164 (if org-odd-levels-only
(setq org-l
(1+ (/ org-l
2))))
165 (setq org-f
(nth (%
(1- org-l
) org-n-level-faces
) org-columns-level-faces
))
167 ((eq n
1) (if org-hide-leading-stars
'org-hide org-f
))
169 (t (if org-level-color-stars-only nil org-f
))))
175 (defvar org-columns-overlays nil
176 "Holds the list of current column overlays.")
178 (defvar org-columns-current-fmt nil
179 "Local variable, holds the currently active column format.")
180 (make-variable-buffer-local 'org-columns-current-fmt
)
181 (defvar org-columns-current-fmt-compiled nil
182 "Local variable, holds the currently active column format.
183 This is the compiled version of the format.")
184 (make-variable-buffer-local 'org-columns-current-fmt-compiled
)
185 (defvar org-columns-current-widths nil
186 "Loval variable, holds the currently widths of fields.")
187 (make-variable-buffer-local 'org-columns-current-widths
)
188 (defvar org-columns-current-maxwidths nil
189 "Loval variable, holds the currently active maximum column widths.")
190 (make-variable-buffer-local 'org-columns-current-maxwidths
)
191 (defvar org-columns-begin-marker
(make-marker)
192 "Points to the position where last a column creation command was called.")
193 (defvar org-columns-top-level-marker
(make-marker)
194 "Points to the position where current columns region starts.")
196 (defvar org-columns-map
(make-sparse-keymap)
197 "The keymap valid in column display.")
199 (defun org-columns-content ()
200 "Switch to contents view while in columns view."
205 (org-defkey org-columns-map
"c" 'org-columns-content
)
206 (org-defkey org-columns-map
"o" 'org-overview
)
207 (org-defkey org-columns-map
"e" 'org-columns-edit-value
)
208 (org-defkey org-columns-map
"\C-c\C-t" 'org-columns-todo
)
209 (org-defkey org-columns-map
"\C-c\C-c" 'org-columns-set-tags-or-toggle
)
210 (org-defkey org-columns-map
"\C-c\C-o" 'org-columns-open-link
)
211 (org-defkey org-columns-map
"v" 'org-columns-show-value
)
212 (org-defkey org-columns-map
"q" 'org-columns-quit
)
213 (org-defkey org-columns-map
"r" 'org-columns-redo
)
214 (org-defkey org-columns-map
"g" 'org-columns-redo
)
215 (org-defkey org-columns-map
[left] 'org-columns-backward-char)
216 (org-defkey org-columns-map "\M-b" 'org-columns-backward-char)
217 (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
218 (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
219 (org-defkey org-columns-map "\M-f" 'org-columns-forward-char)
220 (org-defkey org-columns-map [right] 'org-columns-forward-char)
221 (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
222 (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
223 (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
224 (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
225 (org-defkey org-columns-map "<" 'org-columns-narrow)
226 (org-defkey org-columns-map ">" 'org-columns-widen)
227 (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
228 (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
229 (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
230 (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
232 (org-defkey org-columns-map (number-to-string i)
233 `(lambda () (interactive)
234 (org-columns-next-allowed-value nil ,i))))
236 (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
238 ["Edit property" org-columns-edit-value t]
239 ["Next allowed value" org-columns-next-allowed-value t]
240 ["Previous allowed value" org-columns-previous-allowed-value t]
241 ["Show full value" org-columns-show-value t]
242 ["Edit allowed values" org-columns-edit-allowed t]
244 ["Edit column attributes" org-columns-edit-attributes t]
245 ["Increase column width" org-columns-widen t]
246 ["Decrease column width" org-columns-narrow t]
248 ["Move column right" org-columns-move-right t]
249 ["Move column left" org-columns-move-left t]
250 ["Add column" org-columns-new t]
251 ["Delete column" org-columns-delete t]
253 ["CONTENTS" org-columns-content t]
254 ["OVERVIEW" org-overview t]
255 ["Refresh columns display" org-columns-redo t]
257 ["Open link" org-columns-open-link t]
259 ["Quit" org-columns-quit t]))
261 (defun org-columns-current-column ()
262 (if (featurep 'xemacs)
263 (/ (current-column) 2)
266 (defun org-columns-forward-char ()
269 (if (featurep 'xemacs)
270 (while (not (or (eolp)
272 (point) (current-buffer)
273 'org-columns-key) org-columns-overlays)))
276 (defun org-columns-backward-char ()
279 (if (featurep 'xemacs)
280 (while (not (or (bolp)
281 (member (extent-at (point) (current-buffer) 'org-columns-key) org-columns-overlays)))
284 (defun org-columns-new-overlay (beg end &optional string face)
285 "Create a new column overlay and add it to the list."
286 (let ((ov (org-make-overlay beg end)))
287 (if (featurep 'xemacs)
289 (org-overlay-put ov 'face (or face 'org-columns-space1))
290 (org-overlay-put ov 'start-open t)
292 (org-overlay-display ov string (or face 'org-columns-space1))))
293 (org-overlay-put ov 'face (or face 'secondary-selection))
294 (org-overlay-display ov string face))
295 (push ov org-columns-overlays)
298 (defun org-columns-display-here (&optional props)
299 "Overlay the current line with column display."
301 (let* ((fmt org-columns-current-fmt-compiled)
303 (level-face (save-excursion
304 (beginning-of-line 1)
305 (and (looking-at "\\(\\**\\)\\(\\* \\)")
306 (org-get-level-face 2))))
307 (item (save-match-data
310 (buffer-substring-no-properties
311 (point-at-bol) (point-at-eol))))))
312 (color (if (featurep 'xemacs)
314 (beginning-of-line 1)
315 (and (looking-at "\\(\\**\\)\\(\\* \\)")
316 (org-get-columns-level-face 2)))
320 (and (eq major-mode 'org-agenda-mode)
321 (get-text-property (point-at-bol) 'face))
322 'default) :foreground))))
323 (face (if (featurep 'xemacs) color (list color 'org-column)))
324 (pl (or (get-text-property (point-at-bol) 'prefix-length) 0))
325 (cphr (get-text-property (point-at-bol) 'org-complex-heading-regexp))
326 pom property ass width f string ov column val modval s1 s2 title)
327 ;; Check if the entry is in another buffer.
329 (if (eq major-mode 'org-agenda-mode)
330 (setq pom (or (get-text-property (point) 'org-hd-marker)
331 (get-text-property (point) 'org-marker))
332 props (if pom (org-entry-properties pom) nil))
333 (setq props (org-entry-properties nil))))
335 (while (setq column (pop fmt))
336 (setq property (car column)
338 ass (if (equal property "ITEM")
340 (assoc property props))
341 width (or (cdr (assoc property org-columns-current-maxwidths))
344 f (format (if (featurep 'xemacs) "%%-%d.%ds |" "%%-%d.%ds | ")
346 val (or (cdr ass) "")
347 modval (or (and org-columns-modify-value-for-display-function
349 org-columns-modify-value-for-display-function)
351 org-columns-modify-value-for-display-function
353 (if (equal property "ITEM")
355 (org-columns-cleanup-item
356 val org-columns-current-fmt-compiled)
357 (org-agenda-columns-cleanup-item
358 val pl cphr org-columns-current-fmt-compiled)))))
359 (setq s2 (org-columns-add-ellipses (or modval val) width))
360 (setq string (format f s2))
361 ;; Create the overlay
363 (setq ov (org-columns-new-overlay
364 beg (setq beg (1+ beg)) string face))
365 (org-overlay-put ov 'keymap org-columns-map)
366 (org-overlay-put ov 'org-columns-key property)
367 (org-overlay-put ov 'org-columns-value (cdr ass))
368 (org-overlay-put ov 'org-columns-value-modified modval)
369 (org-overlay-put ov 'org-columns-pom pom)
370 (org-overlay-put ov 'org-columns-format f)
371 (when (featurep 'xemacs)
372 (if (or (not (char-after beg))
373 (equal (char-after beg) ?\n))
374 (let ((inhibit-read-only t))
377 (org-unmodified (insert " "))
378 ;; FIXME: add props and remove later?
381 (org-columns-new-overlay
382 beg (1+ beg) nil 'org-columns-space)
383 (setq beg (1+ beg))))
385 (if (or (not (char-after beg))
386 (equal (char-after beg) ?\n))
387 (let ((inhibit-read-only t))
390 ;; FIXME: add props and remove later?
391 (org-unmodified (insert " "))))))
392 ;; Make the rest of the line disappear.
394 (setq ov (org-columns-new-overlay beg (point-at-eol)))
395 (org-overlay-put ov 'invisible t)
396 (org-overlay-put ov 'keymap org-columns-map)
397 (org-overlay-put ov 'intangible t)
398 (push ov org-columns-overlays)
399 (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
400 (org-overlay-put ov 'keymap org-columns-map)
401 (push ov org-columns-overlays)
402 (let ((inhibit-read-only t))
403 (put-text-property (max (point-min) (1- (point-at-bol)))
404 (min (point-max) (1+ (point-at-eol)))
405 'read-only "Type `e' to edit property")))))
407 (defun org-columns-add-ellipses (string width)
408 "Truncate STRING with WIDTH characters, with ellipses."
410 ((<= (length string) width) string)
411 ((<= width (length org-columns-ellipses))
412 (substring org-columns-ellipses 0 width))
413 (t (concat (substring string 0 (- width (length org-columns-ellipses)))
414 org-columns-ellipses))))
416 (defvar org-columns-full-header-line-format nil
417 "The full header line format, will be shifted by horizontal scrolling." )
418 (defvar org-previous-header-line-format nil
419 "The header line format before column view was turned on.")
420 (defvar org-columns-inhibit-recalculation nil
421 "Inhibit recomputing of columns on column view startup.")
424 (defvar header-line-format)
425 (defvar org-columns-previous-hscroll 0)
426 (defun org-columns-display-here-title ()
427 "Overlay the newline before the current line with the table title."
429 (let ((fmt org-columns-current-fmt-compiled)
431 property width f column str widths)
432 (while (setq column (pop fmt))
433 (setq property (car column)
434 str (or (nth 1 column) property)
435 width (or (cdr (assoc property org-columns-current-maxwidths))
438 widths (push width widths)
439 f (format "%%-%d.%ds | " width width)
440 string (format f str)
441 title (concat title string)))
442 (if (featurep 'xemacs)
443 (let ((ext (make-extent nil nil)))
444 (set-extent-endpoints ext 0 (length title) title)
445 (set-extent-face ext (list 'bold 'underline 'org-columns-space1))
446 (org-set-local 'org-previous-header-line-format
447 (specifier-specs top-gutter))
448 (org-set-local 'org-columns-current-widths (nreverse widths))
449 (set-specifier top-gutter (make-gutter-specifier
450 (cons (current-buffer) title))))
452 (org-add-props " " nil 'display '(space :align-to 0))
453 (org-add-props title nil 'face '(:weight bold :underline t))))
454 (org-set-local 'org-previous-header-line-format header-line-format)
455 (org-set-local 'org-columns-current-widths (nreverse widths))
456 (setq org-columns-full-header-line-format title)
457 (setq org-columns-previous-hscroll -1)
458 (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local))))
460 (defun org-columns-hscoll-title ()
461 "Set the header-line-format so that it scrolls along with the table."
462 (sit-for .0001) ; need to force a redisplay to update window-hscroll
463 (when (not (= (window-hscroll) org-columns-previous-hscroll))
464 (setq header-line-format
465 (concat (substring org-columns-full-header-line-format 0 1)
466 (substring org-columns-full-header-line-format
467 (1+ (window-hscroll))))
468 org-columns-previous-hscroll (window-hscroll))
469 (force-mode-line-update)))
471 (defun org-columns-remove-overlays ()
472 "Remove all currently active column overlays."
474 (when (marker-buffer org-columns-begin-marker)
475 (with-current-buffer (marker-buffer org-columns-begin-marker)
476 (when (local-variable-p 'org-previous-header-line-format (current-buffer))
477 (if (featurep 'xemacs)
478 (set-specifier top-gutter
479 (make-gutter-specifier
480 (cons (current-buffer)
481 (cdar org-previous-header-line-format))))
482 (setq header-line-format org-previous-header-line-format)
483 (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
484 (kill-local-variable 'org-previous-header-line-format))
485 (move-marker org-columns-begin-marker nil)
486 (move-marker org-columns-top-level-marker nil)
488 (mapc 'org-delete-overlay org-columns-overlays)
489 (setq org-columns-overlays nil)
490 (let ((inhibit-read-only t))
491 (remove-text-properties (point-min) (point-max) '(read-only t)))))))
493 (defun org-columns-cleanup-item (item fmt)
494 "Remove from ITEM what is a column in the format FMT."
495 (if (not org-complex-heading-regexp)
497 (when (string-match org-complex-heading-regexp item)
500 (org-add-props (match-string 1 item) nil
501 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
502 (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
503 (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
504 " " (save-match-data (org-columns-compact-links (match-string 4 item)))
505 (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))
508 (list 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
512 (defun org-columns-compact-links (s)
513 "Replace [[link][desc]] with [desc] or [link]."
514 (while (string-match org-bracket-link-regexp s)
515 (setq s (replace-match
516 (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
520 (defvar org-agenda-columns-remove-prefix-from-item)
521 (defun org-agenda-columns-cleanup-item (item pl cphr fmt)
522 "Cleanup the time property for agenda column view.
523 See also the variable `org-agenda-columns-remove-prefix-from-item'."
524 (let* ((org-complex-heading-regexp cphr)
525 (prefix (substring item 0 pl))
526 (rest (substring item pl))
527 (fake (concat "* " rest))
528 (cleaned (org-trim (substring (org-columns-cleanup-item fake fmt) 1))))
529 (if org-agenda-columns-remove-prefix-from-item
531 (concat prefix cleaned))))
533 (defun org-columns-show-value ()
534 "Show the full value of the property."
536 (let ((value (get-char-property (point) 'org-columns-value)))
537 (message "Value is: %s" (or value ""))))
539 (defvar org-agenda-columns-active) ;; defined in org-agenda.el
540 (defun org-columns-quit ()
541 "Remove the column overlays and in this way exit column editing."
544 (org-columns-remove-overlays)
545 (let ((inhibit-read-only t))
546 (remove-text-properties (point-min) (point-max) '(read-only t))))
547 (when (eq major-mode 'org-agenda-mode)
548 (setq org-agenda-columns-active nil)
550 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
552 (defun org-columns-check-computed ()
553 "Check if this column value is computed.
554 If yes, throw an error indicating that changing it does not make sense."
555 (let ((val (get-char-property (point) 'org-columns-value)))
556 (when (and (stringp val)
557 (get-char-property 0 'org-computed val))
558 (error "This value is computed from the entry's children"))))
560 (defun org-columns-todo (&optional arg)
561 "Change the TODO state during column view."
563 (org-columns-edit-value "TODO"))
565 (defun org-columns-set-tags-or-toggle (&optional arg)
566 "Toggle checkbox at point, or set tags for current headline."
568 (if (string-match "\\`\\[[ xX-]\\]\\'"
569 (get-char-property (point) 'org-columns-value))
570 (org-columns-next-allowed-value)
571 (org-columns-edit-value "TAGS")))
573 (defun org-columns-edit-value (&optional key)
574 "Edit the value of the property at point in column view.
575 Where possible, use the standard interface for changing this line."
577 (org-columns-check-computed)
578 (let* ((external-key key)
579 (col (current-column))
580 (key (or key (get-char-property (point) 'org-columns-key)))
581 (value (get-char-property (point) 'org-columns-value))
582 (bol (point-at-bol)) (eol (point-at-eol))
583 (pom (or (get-text-property bol 'org-hd-marker)
584 (point))) ; keep despite of compiler warning
586 (delq nil (mapcar (lambda (x)
587 (and (eq (org-overlay-buffer x) (current-buffer))
588 (>= (org-overlay-start x) bol)
589 (<= (org-overlay-start x) eol)
591 org-columns-overlays)))
594 ((equal key "CLOCKSUM")
595 (error "This special column cannot be edited"))
597 (setq eval '(org-with-point-at pom (org-edit-headline))))
599 (setq eval '(org-with-point-at
601 (let ((current-prefix-arg
602 (if external-key current-prefix-arg '(4))))
603 (call-interactively 'org-todo)))))
604 ((equal key "PRIORITY")
605 (setq eval '(org-with-point-at pom
606 (call-interactively 'org-priority))))
608 (setq eval '(org-with-point-at
610 (let ((org-fast-tag-selection-single-key
611 (if (eq org-fast-tag-selection-single-key 'expert)
612 t org-fast-tag-selection-single-key)))
613 (call-interactively 'org-set-tags)))))
614 ((equal key "DEADLINE")
615 (setq eval '(org-with-point-at
617 (call-interactively 'org-deadline))))
618 ((equal key "SCHEDULED")
619 (setq eval '(org-with-point-at
621 (call-interactively 'org-schedule))))
623 (setq allowed (org-property-get-allowed-values pom key 'table))
625 (setq nval (org-ido-completing-read "Value: " allowed nil t))
626 (setq nval (read-string "Edit: " value)))
627 (setq nval (org-trim nval))
628 (when (not (equal nval value))
629 (setq eval '(org-entry-put pom key nval)))))
633 ((equal major-mode 'org-agenda-mode)
634 (org-columns-eval eval)
635 ;; The following let preserves the current format, and makes sure
636 ;; that in only a single file things need to be upated.
637 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
638 (buffer (marker-buffer pom))
639 (org-agenda-contributing-files
640 (list (with-current-buffer buffer
641 (buffer-file-name (buffer-base-buffer))))))
642 (org-agenda-columns)))
644 (let ((inhibit-read-only t))
646 (remove-text-properties
647 (max (point-min) (1- bol)) eol '(read-only t)))
650 (setq org-columns-overlays
651 (org-delete-all line-overlays org-columns-overlays))
652 (mapc 'org-delete-overlay line-overlays)
653 (org-columns-eval eval))
654 (org-columns-display-here)))
655 (org-move-to-column col)
656 (if (and (org-mode-p)
657 (nth 3 (assoc key org-columns-current-fmt-compiled)))
658 (org-columns-update key)))))))
660 (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
661 "Edit the current headline, the part without TODO keyword, TAGS."
662 (org-back-to-heading)
663 (when (looking-at org-todo-line-regexp)
665 (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
666 (txt (match-string 3))
669 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
670 (setq post (match-string 0 txt)
671 txt (substring txt 0 (match-beginning 0))))
672 (setq txt2 (read-string "Edit: " txt))
673 (when (not (equal txt txt2))
675 (insert pre txt2 post)
676 (delete-region (point) (point-at-eol))
677 (org-set-tags nil t)))))
679 (defun org-columns-edit-allowed ()
680 "Edit the list of allowed values for the current property."
682 (let* ((pom (or (get-text-property (point-at-bol) 'org-marker)
683 (get-text-property (point-at-bol) 'org-hd-marker)
685 (key (get-char-property (point) 'org-columns-key))
686 (key1 (concat key "_ALL"))
687 (allowed (org-entry-get pom key1 t))
689 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
690 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
691 (setq nval (read-string "Allowed: " allowed))
693 (cond ((marker-position org-entry-property-inherited-from)
694 org-entry-property-inherited-from)
695 ((marker-position org-columns-top-level-marker)
696 org-columns-top-level-marker)
700 (defun org-columns-eval (form)
703 (beginning-of-line 1)
704 ;; `next-line' is needed here, because it skips invisible line.
705 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
706 (setq hidep (org-on-heading-p 1)))
708 (and hidep (hide-entry))))
710 (defun org-columns-previous-allowed-value ()
711 "Switch to the previous allowed value for this column."
713 (org-columns-next-allowed-value t))
715 (defun org-columns-next-allowed-value (&optional previous nth)
716 "Switch to the next allowed value for this column.
717 When PREVIOUS is set, go to the previous value. When NTH is
718 an integer, select that value."
720 (org-columns-check-computed)
721 (let* ((col (current-column))
722 (key (get-char-property (point) 'org-columns-key))
723 (value (get-char-property (point) 'org-columns-value))
724 (bol (point-at-bol)) (eol (point-at-eol))
725 (pom (or (get-text-property bol 'org-hd-marker)
726 (point))) ; keep despite of compiler waring
728 (delq nil (mapcar (lambda (x)
729 (and (eq (org-overlay-buffer x) (current-buffer))
730 (>= (org-overlay-start x) bol)
731 (<= (org-overlay-start x) eol)
733 org-columns-overlays)))
734 (allowed (or (org-property-get-allowed-values pom key)
736 (nth 4 (assoc key org-columns-current-fmt-compiled))
737 '(checkbox checkbox-n-of-m checkbox-percent))
739 (org-colview-construct-allowed-dates value)))
743 (if (= nth -1) (setq nth 9)))
744 (when (equal key "ITEM")
745 (error "Cannot edit item headline from here"))
746 (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
747 (error "Allowed values for this property have not been defined"))
748 (if (member key '("SCHEDULED" "DEADLINE"))
749 (setq nval (if previous 'earlier 'later))
750 (if previous (setq allowed (reverse allowed)))
753 (setq nval (nth nth allowed))
755 (error "There are only %d allowed values for property `%s'"
756 (length allowed) key)))
757 ((member value allowed)
758 (setq nval (or (car (cdr (member value allowed)))
760 (if (equal nval value)
761 (error "Only one allowed value for this property")))
762 (t (setq nval (car allowed)))))
764 ((equal major-mode 'org-agenda-mode)
765 (org-columns-eval '(org-entry-put pom key nval))
766 ;; The following let preserves the current format, and makes sure
767 ;; that in only a single file things need to be upated.
768 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
769 (buffer (marker-buffer pom))
770 (org-agenda-contributing-files
771 (list (with-current-buffer buffer
772 (buffer-file-name (buffer-base-buffer))))))
773 (org-agenda-columns)))
775 (let ((inhibit-read-only t))
776 (remove-text-properties (1- bol) eol '(read-only t))
779 (setq org-columns-overlays
780 (org-delete-all line-overlays org-columns-overlays))
781 (mapc 'org-delete-overlay line-overlays)
782 (org-columns-eval '(org-entry-put pom key nval)))
783 (org-columns-display-here)))
784 (org-move-to-column col)
785 (and (nth 3 (assoc key org-columns-current-fmt-compiled))
786 (org-columns-update key))))))
788 (defun org-colview-construct-allowed-dates (s)
789 "Construct a list of three dates around the date in S.
790 This respects the format of the time stamp in S, active or non-active,
791 and also including time or not. S must be just a time stamp, no text
793 (when (string-match (concat "^" org-ts-regexp3 "$") s)
794 (let* ((time (org-parse-time-string s 'nodefaults))
795 (active (equal (string-to-char s) ?<))
796 (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
797 time-before time-after)
798 (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
799 (setf (car time) (or (car time) 0))
800 (setf (nth 1 time) (or (nth 1 time) 0))
801 (setf (nth 2 time) (or (nth 2 time) 0))
802 (setq time-before (copy-sequence time))
803 (setq time-after (copy-sequence time))
804 (setf (nth 3 time-before) (1- (nth 3 time)))
805 (setf (nth 3 time-after) (1+ (nth 3 time)))
806 (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
807 (list time-before time time-after)))))
809 (defun org-verify-version (task)
812 (if (or (and (featurep 'xemacs) (not (featurep 'org-colview-xemacs)))
813 (and (not (featurep 'xemacs)) (< emacs-major-version 22)))
814 (error "This version of Emacs cannot run Column View")))))
816 (defun org-columns-open-link (&optional arg)
818 (let ((value (get-char-property (point) 'org-columns-value)))
819 (org-open-link-from-string value arg)))
821 (defun org-columns-get-format-and-top-level ()
823 (when (condition-case nil (org-back-to-heading) (error nil))
824 (setq fmt (org-entry-get nil "COLUMNS" t)))
825 (setq fmt (or fmt org-columns-default-format))
826 (org-set-local 'org-columns-current-fmt fmt)
827 (org-columns-compile-format fmt)
828 (if (marker-position org-entry-property-inherited-from)
829 (move-marker org-columns-top-level-marker
830 org-entry-property-inherited-from)
831 (move-marker org-columns-top-level-marker (point)))
834 (defun org-columns ()
835 "Turn on column view on an org-mode file."
837 (org-verify-version 'columns)
838 (when (featurep 'xemacs)
839 (set-face-foreground 'org-columns-space
840 (face-background 'org-columns-space)))
841 (org-columns-remove-overlays)
842 (move-marker org-columns-begin-marker (point))
843 (let (beg end fmt cache maxwidths)
844 (setq fmt (org-columns-get-format-and-top-level))
846 (goto-char org-columns-top-level-marker)
848 (unless org-columns-inhibit-recalculation
849 (org-columns-compute-all))
850 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
852 ;; Get and cache the properties
854 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
857 (narrow-to-region beg end)
859 (while (re-search-forward (concat "^" outline-regexp) end t)
860 (push (cons (org-current-line) (org-entry-properties)) cache))
862 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
863 (org-set-local 'org-columns-current-maxwidths maxwidths)
864 (org-columns-display-here-title)
867 (org-columns-display-here (cdr x)))
870 (defun org-columns-new (&optional prop title width op fmt &rest rest)
871 "Insert a new column, to the left of the current column."
873 (let ((n (org-columns-current-column))
874 (editp (and prop (assoc prop org-columns-current-fmt-compiled)))
876 (setq prop (org-ido-completing-read
877 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
879 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
880 (setq width (read-string "Column width: " (if width (number-to-string width))))
881 (if (string-match "\\S-" width)
882 (setq width (string-to-number width))
884 (setq fmt (org-ido-completing-read "Summary [none]: "
885 '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
887 (if (string-match "\\S-" fmt)
888 (setq fmt (intern fmt))
890 (if (eq fmt 'none) (setq fmt nil))
894 (setcdr editp (list title width nil fmt)))
895 (setq cell (nthcdr (1- n) org-columns-current-fmt-compiled))
896 (setcdr cell (cons (list prop title width nil fmt)
898 (org-columns-store-format)
901 (defun org-columns-delete ()
902 "Delete the column at point from columns view."
904 (let* ((n (org-columns-current-column))
905 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
907 (format "Are you sure you want to remove column \"%s\"? " title))
908 (setq org-columns-current-fmt-compiled
909 (delq (nth n org-columns-current-fmt-compiled)
910 org-columns-current-fmt-compiled))
911 (org-columns-store-format)
913 (if (>= (org-columns-current-column)
914 (length org-columns-current-fmt-compiled))
915 (org-columns-backward-char)))))
917 (defun org-columns-edit-attributes ()
918 "Edit the attributes of the current column."
920 (let* ((n (org-columns-current-column))
921 (info (nth n org-columns-current-fmt-compiled)))
922 (apply 'org-columns-new info)))
924 (defun org-columns-widen (arg)
925 "Make the column wider by ARG characters."
927 (let* ((n (org-columns-current-column))
928 (entry (nth n org-columns-current-fmt-compiled))
929 (width (or (nth 2 entry)
930 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
931 (setq width (max 1 (+ width arg)))
932 (setcar (nthcdr 2 entry) width)
933 (org-columns-store-format)
936 (defun org-columns-narrow (arg)
937 "Make the column narrower by ARG characters."
939 (org-columns-widen (- arg)))
941 (defun org-columns-move-right ()
942 "Swap this column with the one to the right."
944 (let* ((n (org-columns-current-column))
945 (cell (nthcdr n org-columns-current-fmt-compiled))
947 (when (>= n (1- (length org-columns-current-fmt-compiled)))
948 (error "Cannot shift this column further to the right"))
950 (setcar cell (car (cdr cell)))
951 (setcdr cell (cons e (cdr (cdr cell))))
952 (org-columns-store-format)
954 (org-columns-forward-char)))
956 (defun org-columns-move-left ()
957 "Swap this column with the one to the left."
959 (let* ((n (org-columns-current-column)))
961 (error "Cannot shift this column further to the left"))
962 (org-columns-backward-char)
963 (org-columns-move-right)
964 (org-columns-backward-char)))
966 (defun org-columns-store-format ()
967 "Store the text version of the current columns format in appropriate place.
968 This is either in the COLUMNS property of the node starting the current column
969 display, or in the #+COLUMNS line of the current buffer."
971 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
972 (org-set-local 'org-columns-current-fmt fmt)
973 (if (marker-position org-columns-top-level-marker)
975 (goto-char org-columns-top-level-marker)
976 (if (and (org-at-heading-p)
977 (org-entry-get nil "COLUMNS"))
978 (org-entry-put nil "COLUMNS" fmt)
979 (goto-char (point-min))
980 ;; Overwrite all #+COLUMNS lines....
981 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
983 (replace-match (concat "#+COLUMNS: " fmt) t t))
985 (goto-char (point-min))
986 (or (org-on-heading-p t) (outline-next-heading))
987 (let ((inhibit-read-only t))
988 (insert-before-markers "#+COLUMNS: " fmt "\n")))
989 (org-set-local 'org-columns-default-format fmt))))))
991 (defvar org-agenda-overriding-columns-format nil
992 "When set, overrides any other format definition for the agenda.
993 Don't set this, this is meant for dynamic scoping.")
995 (defun org-columns-get-autowidth-alist (s cache)
996 "Derive the maximum column widths from the format and the cache."
998 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
999 (push (cons (match-string 1 s) 1) rtn)
1000 (setq start (match-end 0)))
1002 (setcdr x (apply 'max
1005 (length (or (cdr (assoc (car x) (cdr y))) " ")))
1010 (defun org-columns-compute-all ()
1011 "Compute all columns that have operators defined."
1013 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
1014 (let ((columns org-columns-current-fmt-compiled) col)
1015 (while (setq col (pop columns))
1018 (org-columns-compute (car col)))))))
1020 (defun org-columns-update (property)
1021 "Recompute PROPERTY, and update the columns display for it."
1022 (org-columns-compute property)
1023 (let (fmt val pos face)
1026 (when (equal (org-overlay-get ov 'org-columns-key) property)
1027 (setq pos (org-overlay-start ov))
1029 (when (setq val (cdr (assoc property
1031 (point-at-bol) 'org-summaries))))
1032 (setq fmt (org-overlay-get ov 'org-columns-format))
1033 (org-overlay-put ov 'org-columns-value val)
1034 (if (featurep 'xemacs)
1036 (setq face (glyph-face (extent-end-glyph ov)))
1037 (org-overlay-display ov (format fmt val) face))
1038 (org-overlay-display ov (format fmt val))))))
1039 org-columns-overlays))))
1041 (defun org-columns-compute (property)
1042 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
1044 (let* ((re (concat "^" outline-regexp))
1045 (lmax 30) ; Does anyone use deeper levels???
1046 (lsum (make-vector lmax 0))
1047 (lflag (make-vector lmax nil))
1049 (ass (assoc property org-columns-current-fmt-compiled))
1050 (format (nth 4 ass))
1051 (printf (nth 5 ass))
1052 (beg org-columns-top-level-marker)
1053 last-level val valflag flag end sumpos sum-alist sum str str1 useval)
1055 ;; Find the region to compute
1057 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
1059 ;; Walk the tree from the back and do the computations
1060 (while (re-search-backward re beg t)
1061 (setq sumpos (match-beginning 0)
1063 level (org-outline-level)
1064 val (org-entry-get nil property)
1065 valflag (and val (string-match "\\S-" val)))
1067 ((< level last-level)
1068 ;; put the sum of lower levels here as a property
1069 (setq sum (aref lsum last-level) ; current sum
1070 flag (aref lflag last-level) ; any valid entries from children?
1071 str (org-columns-number-to-string sum format printf)
1072 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
1073 useval (if flag str1 (if valflag val ""))
1074 sum-alist (get-text-property sumpos 'org-summaries))
1075 (if (assoc property sum-alist)
1076 (setcdr (assoc property sum-alist) useval)
1077 (push (cons property useval) sum-alist)
1079 (add-text-properties sumpos (1+ sumpos)
1080 (list 'org-summaries sum-alist))))
1081 (when (and val (not (equal val (if flag str val))))
1082 (org-entry-put nil property (if flag str val)))
1083 ;; add current to current level accumulator
1084 (when (or flag valflag)
1085 (aset lsum level (+ (aref lsum level)
1086 (if flag sum (org-column-string-to-number
1087 (if flag str val) format))))
1088 (aset lflag level t))
1089 ;; clear accumulators for deeper levels
1090 (loop for l from (1+ level) to (1- lmax) do
1092 (aset lflag l nil)))
1093 ((>= level last-level)
1094 ;; add what we have here to the accumulator for this level
1095 (aset lsum level (+ (aref lsum level)
1096 (org-column-string-to-number (or val "0") format)))
1097 (and valflag (aset lflag level t)))
1098 (t (error "This should not happen")))))))
1100 (defun org-columns-redo ()
1101 "Construct the column display again."
1103 (message "Recomputing columns...")
1105 (if (marker-position org-columns-begin-marker)
1106 (goto-char org-columns-begin-marker))
1107 (org-columns-remove-overlays)
1109 (call-interactively 'org-columns)
1111 (call-interactively 'org-agenda-columns)))
1112 (when (featurep 'xemacs)
1113 (while (not (or (eolp)
1114 (member (extent-at (point)) org-columns-overlays)))
1116 (message "Recomputing columns...done"))
1118 (defun org-columns-not-in-agenda ()
1119 (if (eq major-mode 'org-agenda-mode)
1120 (error "This command is only allowed in Org-mode buffers")))
1123 (defun org-string-to-number (s)
1124 "Convert string to number, and interpret hh:mm:ss."
1125 (if (not (string-match ":" s))
1126 (string-to-number s)
1127 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1129 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1132 (defun org-columns-number-to-string (n fmt &optional printf)
1133 "Convert a computed column number to a string value, according to FMT."
1135 ((eq fmt 'add_times)
1136 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
1137 (format org-time-clocksum-format h m)))
1139 (cond ((= n (floor n)) "[X]")
1142 ((memq fmt '(checkbox-n-of-m checkbox-percent))
1143 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
1144 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
1145 (printf (format printf n))
1148 (t (number-to-string n))))
1150 (defun org-nofm-to-completion (n m &optional percent)
1152 (format "[%d/%d]" n m)
1153 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
1155 (defun org-column-string-to-number (s fmt)
1156 "Convert a column value to a number that can be used for column computing."
1158 ((string-match ":" s)
1159 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1161 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1163 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
1164 (if (equal s "[X]") 1. 0.000001))
1165 (t (string-to-number s))))
1167 (defun org-columns-uncompile-format (cfmt)
1168 "Turn the compiled columns format back into a string representation."
1169 (let ((rtn "") e s prop title op width fmt printf)
1170 (while (setq e (pop cfmt))
1178 ((eq fmt 'add_times) (setq op ":"))
1179 ((eq fmt 'checkbox) (setq op "X"))
1180 ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
1181 ((eq fmt 'checkbox-percent) (setq op "X%"))
1182 ((eq fmt 'add_numbers) (setq op "+"))
1183 ((eq fmt 'currency) (setq op "$")))
1184 (if (and op printf) (setq op (concat op ";" printf)))
1185 (if (equal title prop) (setq title nil))
1186 (setq s (concat "%" (if width (number-to-string width))
1188 (if title (concat "(" title ")"))
1189 (if op (concat "{" op "}"))))
1190 (setq rtn (concat rtn " " s)))
1193 (defun org-columns-compile-format (fmt)
1194 "Turn a column format string into an alist of specifications.
1195 The alist has one entry for each column in the format. The elements of
1197 property the property
1198 title the title field for the columns
1199 width the column width in characters, can be nil for automatic
1200 operator the operator if any
1201 format the output format for computed results, derived from operator
1202 printf a printf format for computed values"
1203 (let ((start 0) width prop title op f printf)
1204 (setq org-columns-current-fmt-compiled nil)
1205 (while (string-match
1206 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
1208 (setq start (match-end 0)
1209 width (match-string 1 fmt)
1210 prop (match-string 2 fmt)
1211 title (or (match-string 3 fmt) prop)
1212 op (match-string 4 fmt)
1215 (if width (setq width (string-to-number width)))
1216 (when (and op (string-match ";" op))
1217 (setq printf (substring op (match-end 0))
1218 op (substring op 0 (match-beginning 0))))
1220 ((equal op "+") (setq f 'add_numbers))
1221 ((equal op "$") (setq f 'currency))
1222 ((equal op ":") (setq f 'add_times))
1223 ((equal op "X") (setq f 'checkbox))
1224 ((equal op "X/") (setq f 'checkbox-n-of-m))
1225 ((equal op "X%") (setq f 'checkbox-percent))
1227 (push (list prop title width op f printf) org-columns-current-fmt-compiled))
1228 (setq org-columns-current-fmt-compiled
1229 (nreverse org-columns-current-fmt-compiled))))
1232 ;;; Dynamic block for Column view
1234 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
1235 "Get the column view of the current buffer or subtree.
1236 The first optional argument MAXLEVEL sets the level limit. A
1237 second optional argument SKIP-EMPTY-ROWS tells whether to skip
1238 empty rows, an empty row being one where all the column view
1239 specifiers except ITEM are empty. This function returns a list
1240 containing the title row and all other rows. Each row is a list
1242 (if (featurep 'xemacs)
1244 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1245 (n (length title)) row tbl)
1246 (goto-char (point-min))
1248 (while (re-search-forward "^\\(\\*+\\) " nil t)
1249 (when (and (or (null maxlevel)
1251 (if org-odd-levels-only
1252 (/ (1+ (length (match-string 1))) 2)
1253 (length (match-string 1)))))
1254 (get-char-property (match-beginning 0) 'org-columns-key))
1255 (goto-char (match-beginning 0))
1257 (loop for i from 0 to (1- n) do
1258 (push (or (get-char-property (point)
1259 'org-columns-value-modified)
1260 (get-char-property (point) 'org-columns-value)
1263 (org-columns-forward-char))
1264 (setq row (nreverse row))
1265 (unless (and skip-empty-rows
1266 (eq 1 (length (delete "" (delete-dups row)))))
1268 (append (list title 'hline) (nreverse tbl))))
1270 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1271 (n (length title)) row tbl)
1272 (goto-char (point-min))
1273 (while (and (re-search-forward "^\\(\\*+\\) " nil t)
1276 (if org-odd-levels-only
1277 (/ (1+ (length (match-string 1))) 2)
1278 (length (match-string 1))))))
1279 (when (get-char-property (match-beginning 0) 'org-columns-key)
1281 (loop for i from 0 to (1- n) do
1282 (push (or (get-char-property (+ (match-beginning 0) i)
1283 'org-columns-value-modified)
1284 (get-char-property (+ (match-beginning 0) i)
1288 (setq row (nreverse row))
1289 (unless (and skip-empty-rows
1290 (eq 1 (length (delete "" (delete-dups row)))))
1292 (append (list title 'hline) (nreverse tbl))))))
1294 (defun org-dblock-write:columnview (params)
1295 "Write the column view table.
1296 PARAMS is a property list of parameters:
1298 :width enforce same column widths with <N> specifiers.
1299 :id the :ID: property of the entry where the columns view
1300 should be built, as a string. When `local', call locally.
1301 When `global' call column view with the cursor at the beginning
1302 of the buffer (usually this means that the whole buffer switches
1304 :hlines When t, insert a hline before each item. When a number, insert
1305 a hline before each level <= that number.
1306 :vlines When t, make each column a colgroup to enforce vertical lines.
1307 :maxlevel When set to a number, don't capture headlines below this level.
1309 When t, skip rows where all specifiers other than ITEM are empty."
1310 (let ((pos (move-marker (make-marker) (point)))
1311 (hlines (plist-get params :hlines))
1312 (vlines (plist-get params :vlines))
1313 (maxlevel (plist-get params :maxlevel))
1314 (skip-empty-rows (plist-get params :skip-empty-rows))
1315 tbl id idpos nfields tmp)
1318 (when (setq id (plist-get params :id))
1319 (cond ((not id) nil)
1320 ((eq id 'global) (goto-char (point-min)))
1321 ((eq id 'local) nil)
1322 ((setq idpos (org-find-entry-with-id id))
1324 (t (error "Cannot find entry with :ID: %s" id))))
1326 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1327 (setq nfields (length (car tbl)))
1328 (org-columns-quit)))
1330 (move-marker pos nil)
1332 (when (plist-get params :hlines)
1335 (if (eq (car tbl) 'hline)
1336 (push (pop tbl) tmp)
1337 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1338 (if (and (not (eq (car tmp) 'hline))
1340 (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
1342 (push (pop tbl) tmp)))
1343 (setq tbl (nreverse tmp)))
1345 (setq tbl (mapcar (lambda (x)
1346 (if (eq 'hline x) x (cons "" x)))
1348 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1350 (insert (org-listtable-to-string tbl))
1351 (when (plist-get params :width)
1352 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1353 org-columns-current-widths "|")))
1355 (org-table-align))))
1357 (defun org-listtable-to-string (tbl)
1358 "Convert a listtable TBL to a string that contains the Org-mode table.
1359 The table still need to be aligned. The resulting string has no leading
1360 and tailing newline characters."
1365 (concat "|" (mapconcat 'identity x "|") "|"))
1366 ((eq x 'hline) "|-|")
1367 (t (error "Garbage in listtable: %s" x))))
1370 (defun org-insert-columns-dblock ()
1371 "Create a dynamic block capturing a column view table."
1373 (when (featurep 'xemacs) (org-columns-quit))
1374 (let ((defaults '(:name "columnview" :hlines 1))
1375 (id (org-ido-completing-read
1376 "Capture columns (local, global, entry with :ID: property) [local]: "
1377 (append '(("global") ("local"))
1378 (mapcar 'list (org-property-values "ID"))))))
1379 (if (equal id "") (setq id 'local))
1380 (if (equal id "global") (setq id 'global))
1381 (setq defaults (append defaults (list :id id)))
1382 (org-create-dblock defaults)
1383 (org-update-dblock)))
1385 ;;; Column view in the agenda
1387 (defvar org-agenda-view-columns-initially nil
1388 "When set, switch to columns view immediately after creating the agenda.")
1390 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1391 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1392 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1394 (defun org-agenda-columns ()
1395 "Turn on or update column view in the agenda."
1397 (org-verify-version 'columns)
1398 (org-columns-remove-overlays)
1399 (move-marker org-columns-begin-marker (point))
1400 (let (fmt cache maxwidths m p a d)
1402 ((and (boundp 'org-agenda-overriding-columns-format)
1403 org-agenda-overriding-columns-format)
1404 (setq fmt org-agenda-overriding-columns-format)
1405 (org-set-local 'org-agenda-overriding-columns-format fmt))
1406 ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
1407 (setq fmt (or (org-entry-get m "COLUMNS" t)
1408 (with-current-buffer (marker-buffer m)
1409 org-columns-default-format))))
1410 ((and (boundp 'org-columns-current-fmt)
1411 (local-variable-p 'org-columns-current-fmt (current-buffer))
1412 org-columns-current-fmt)
1413 (setq fmt org-columns-current-fmt))
1414 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
1415 (setq m (get-text-property m 'org-hd-marker))
1416 (setq fmt (or (org-entry-get m "COLUMNS" t)
1417 (with-current-buffer (marker-buffer m)
1418 org-columns-default-format)))))
1419 (setq fmt (or fmt org-columns-default-format))
1420 (org-set-local 'org-columns-current-fmt fmt)
1421 (org-columns-compile-format fmt)
1422 (when org-agenda-columns-compute-summary-properties
1423 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1425 ;; Get and cache the properties
1426 (goto-char (point-min))
1428 (when (setq m (or (get-text-property (point) 'org-hd-marker)
1429 (get-text-property (point) 'org-marker)))
1430 (setq p (org-entry-properties m))
1432 (when (or (not (setq a (assoc org-effort-property p)))
1433 (not (string-match "\\S-" (or (cdr a) ""))))
1434 ;; OK, the property is not defined. Use appointment duration?
1435 (when (and org-agenda-columns-add-appointments-to-effort-sum
1436 (setq d (get-text-property (point) 'duration)))
1437 (setq d (org-minutes-to-hh:mm-string d))
1438 (put-text-property 0 (length d) 'face 'org-warning d)
1439 (push (cons org-effort-property d) p)))
1440 (push (cons (org-current-line) p) cache))
1441 (beginning-of-line 2))
1443 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
1444 (org-set-local 'org-columns-current-maxwidths maxwidths)
1445 (org-columns-display-here-title)
1448 (org-columns-display-here (cdr x)))
1450 (when org-agenda-columns-show-summaries
1451 (org-agenda-colview-summarize cache))))))
1453 (defun org-agenda-colview-summarize (cache)
1454 "Summarize the summarizable columns in column view in the agenda.
1455 This will add overlays to the date lines, to show the summary for each day."
1456 (let* ((fmt (mapcar (lambda (x)
1457 (list (car x) (if (equal (car x) "CLOCKSUM")
1458 'add_times (nth 4 x))))
1459 org-columns-current-fmt-compiled))
1460 line c c1 stype props lsum entries prop v)
1462 (when (delq nil (mapcar 'cadr fmt))
1463 ;; OK, at least one summation column, it makes sense to try this
1464 (goto-char (point-max))
1466 (when (or (get-text-property (point) 'org-date-line)
1467 (eq (get-text-property (point) 'face)
1468 'org-agenda-structure))
1469 ;; OK, this is a date line that should be used
1470 (setq line (org-current-line))
1471 (setq entries nil c cache cache nil)
1472 (while (setq c1 (pop c))
1473 (if (> (car c1) line)
1476 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1477 ;; Compute the summaries for the properties we want,
1478 ;; set nil properties for the rest.
1479 (when (setq entries (mapcar 'cdr entries))
1483 (setq prop (car f) stype (nth 1 f))
1485 ((equal prop "ITEM")
1486 (cons prop (buffer-substring (point-at-bol)
1488 ((not stype) (cons prop ""))
1493 (setq v (cdr (assoc prop x)))
1494 (if v (setq lsum (+ lsum
1495 (org-column-string-to-number
1498 (setq lsum (org-columns-number-to-string lsum stype))
1500 0 (length lsum) 'face 'bold lsum)
1503 (org-columns-display-here props)
1504 (org-set-local 'org-agenda-columns-active t)))
1505 (if (bobp) (throw 'exit t))
1506 (beginning-of-line 0))))))
1508 (defun org-agenda-colview-compute (fmt)
1509 "Compute the relevant columns in the contributing source buffers."
1510 (let ((files org-agenda-contributing-files)
1511 (org-columns-begin-marker (make-marker))
1512 (org-columns-top-level-marker (make-marker))
1514 (while (setq f (pop files))
1515 (setq b (find-buffer-visiting f))
1516 (with-current-buffer (or (buffer-base-buffer b) b)
1521 (remove-text-properties (point-min) (point-max)
1522 '(org-summaries t)))
1523 (goto-char (point-min))
1524 (org-columns-get-format-and-top-level)
1525 (while (setq fm (pop fmt))
1526 (if (equal (car fm) "CLOCKSUM")
1528 (when (and (nth 4 fm)
1529 (setq a (assoc (car fm)
1530 org-columns-current-fmt-compiled))
1531 (equal (nth 4 a) (nth 4 fm)))
1532 (org-columns-compute (car fm)))))))))))
1534 (provide 'org-colview)
1535 (provide 'org-colview-xemacs)
1537 ;;; org-colview-xemacs.el ends here