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