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