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