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