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