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