org-colview: Fix typo in a docstring
[org-mode/org-tableheadings.git] / lisp / org-colview.el
blob8ba8c97fec3049bcca3d426781d25b7cabc72a28
1 ;;; org-colview.el --- Column View in Org-mode
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-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--value (property pos)
150 "Return value for PROPERTY at buffer position POS."
151 (or (cdr (assoc-string property (get-text-property pos 'org-summaries) t))
152 (org-entry-get pos property 'selective t)))
154 (defun org-columns-new-overlay (beg end &optional string face)
155 "Create a new column overlay and add it to the list."
156 (let ((ov (make-overlay beg end)))
157 (overlay-put ov 'face (or face 'secondary-selection))
158 (org-overlay-display ov string face)
159 (push ov org-columns-overlays)
160 ov))
162 (defun org-columns-display-here (&optional props dateline)
163 "Overlay the current line with column display."
164 (interactive)
165 (save-excursion
166 (beginning-of-line)
167 (let* ((level-face (and (looking-at "\\(\\**\\)\\(\\* \\)")
168 (org-get-level-face 2)))
169 (ref-face (or level-face
170 (and (eq major-mode 'org-agenda-mode)
171 (org-get-at-bol 'face))
172 'default))
173 (color (list :foreground (face-attribute ref-face :foreground)))
174 (font (list :height (face-attribute 'default :height)
175 :family (face-attribute 'default :family)))
176 (face (list color font 'org-column ref-face))
177 (face1 (list color font 'org-agenda-column-dateline ref-face))
178 (pom (and (eq major-mode 'org-agenda-mode)
179 (or (org-get-at-bol 'org-hd-marker)
180 (org-get-at-bol 'org-marker))))
181 (props (cond (props)
182 ((eq major-mode 'org-agenda-mode)
183 (and pom (org-entry-properties pom)))
184 (t (org-entry-properties)))))
185 ;; Each column is an overlay on top of a character. So there has
186 ;; to be at least as many characters available on the line as
187 ;; columns to display.
188 (let ((columns (length org-columns-current-fmt-compiled))
189 (chars (- (line-end-position) (line-beginning-position))))
190 (when (> columns chars)
191 (save-excursion
192 (end-of-line)
193 (let ((inhibit-read-only t))
194 (insert (make-string (- columns chars) ?\s))))))
195 ;; Walk the format. Create and install the overlay for the
196 ;; current column on the next character.
197 (dolist (column org-columns-current-fmt-compiled)
198 (let* ((property (car column))
199 (title (nth 1 column))
200 (ass (assoc-string property props t))
201 (width
203 (cdr (assoc-string property org-columns-current-maxwidths t))
204 (nth 2 column)
205 (length property)))
206 (f (format "%%-%d.%ds | " width width))
207 (fm (nth 4 column))
208 (fc (nth 5 column))
209 (calc (nth 7 column))
210 (val (or (cdr ass) ""))
211 (modval
212 (cond
213 ((functionp org-columns-modify-value-for-display-function)
214 (funcall org-columns-modify-value-for-display-function
215 title val))
216 ((equal property "ITEM") (org-columns-compact-links val))
217 (fc (org-columns-number-to-string
218 (org-columns-string-to-number val fm) fm fc))
219 ((and calc (functionp calc)
220 (not (string= val ""))
221 (not (get-text-property 0 'org-computed val)))
222 (org-columns-number-to-string
223 (funcall calc (org-columns-string-to-number val fm)) fm))))
224 (string
225 (format f
226 (let ((v (org-columns-add-ellipses
227 (or modval val) width)))
228 (cond
229 ((equal property "PRIORITY")
230 (propertize v 'face (org-get-priority-face val)))
231 ((equal property "TAGS")
232 (if (not org-tags-special-faces-re)
233 (propertize v 'face 'org-tag)
234 (replace-regexp-in-string
235 org-tags-special-faces-re
236 (lambda (m)
237 (propertize m 'face (org-get-tag-face m)))
238 v nil nil 1)))
239 ((equal property "TODO")
240 (propertize v 'face (org-get-todo-face val)))
241 (t v)))))
242 (ov (org-columns-new-overlay
243 (point) (1+ (point)) string (if dateline face1 face))))
244 (overlay-put ov 'keymap org-columns-map)
245 (overlay-put ov 'org-columns-key property)
246 (overlay-put ov 'org-columns-value (cdr ass))
247 (overlay-put ov 'org-columns-value-modified modval)
248 (overlay-put ov 'org-columns-pom pom)
249 (overlay-put ov 'org-columns-format f)
250 (overlay-put ov 'line-prefix "")
251 (overlay-put ov 'wrap-prefix "")
252 (forward-char)))
253 ;; Make the rest of the line disappear.
254 (let ((ov (org-columns-new-overlay (point) (line-end-position))))
255 (overlay-put ov 'invisible t)
256 (overlay-put ov 'keymap org-columns-map)
257 (overlay-put ov 'line-prefix "")
258 (overlay-put ov 'wrap-prefix ""))
259 (let ((ov (make-overlay (1- (line-end-position))
260 (line-beginning-position 2))))
261 (overlay-put ov 'keymap org-columns-map)
262 (push ov org-columns-overlays))
263 (org-with-silent-modifications
264 (let ((inhibit-read-only t))
265 (put-text-property
266 (line-end-position 0)
267 (line-beginning-position 2)
268 'read-only
269 (substitute-command-keys
270 "Type \\<org-columns-map>\\[org-columns-edit-value] \
271 to edit property")))))))
273 (defun org-columns-add-ellipses (string width)
274 "Truncate STRING with WIDTH characters, with ellipses."
275 (cond
276 ((<= (length string) width) string)
277 ((<= width (length org-columns-ellipses))
278 (substring org-columns-ellipses 0 width))
279 (t (concat (substring string 0 (- width (length org-columns-ellipses)))
280 org-columns-ellipses))))
282 (defvar org-columns-full-header-line-format nil
283 "The full header line format, will be shifted by horizontal scrolling." )
284 (defvar org-previous-header-line-format nil
285 "The header line format before column view was turned on.")
286 (defvar org-columns-inhibit-recalculation nil
287 "Inhibit recomputing of columns on column view startup.")
288 (defvar org-columns-flyspell-was-active nil
289 "Remember the state of `flyspell-mode' before column view.
290 Flyspell-mode can cause problems in columns view, so it is turned off
291 for the duration of the command.")
293 (defvar header-line-format)
294 (defvar org-columns-previous-hscroll 0)
296 (defun org-columns-display-here-title ()
297 "Overlay the newline before the current line with the table title."
298 (interactive)
299 (let ((fmt org-columns-current-fmt-compiled)
300 string (title "")
301 property width f column str widths)
302 (while (setq column (pop fmt))
303 (setq property (car column)
304 str (or (nth 1 column) property)
305 width (or (cdr (assoc-string property
306 org-columns-current-maxwidths
308 (nth 2 column)
309 (length str))
310 widths (push width widths)
311 f (format "%%-%d.%ds | " width width)
312 string (format f str)
313 title (concat title string)))
314 (setq title (concat
315 (org-add-props " " nil 'display '(space :align-to 0))
316 ;;(org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
317 (org-add-props title nil 'face 'org-column-title)))
318 (org-set-local 'org-previous-header-line-format header-line-format)
319 (org-set-local 'org-columns-current-widths (nreverse widths))
320 (setq org-columns-full-header-line-format title)
321 (setq org-columns-previous-hscroll -1)
322 ; (org-columns-hscoll-title)
323 (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
325 (defun org-columns-hscoll-title ()
326 "Set the `header-line-format' so that it scrolls along with the table."
327 (sit-for .0001) ; need to force a redisplay to update window-hscroll
328 (when (not (= (window-hscroll) org-columns-previous-hscroll))
329 (setq header-line-format
330 (concat (substring org-columns-full-header-line-format 0 1)
331 (substring org-columns-full-header-line-format
332 (1+ (window-hscroll))))
333 org-columns-previous-hscroll (window-hscroll))
334 (force-mode-line-update)))
336 (defvar org-colview-initial-truncate-line-value nil
337 "Remember the value of `truncate-lines' across colview.")
339 ;;;###autoload
340 (defun org-columns-remove-overlays ()
341 "Remove all currently active column overlays."
342 (interactive)
343 (when (marker-buffer org-columns-begin-marker)
344 (with-current-buffer (marker-buffer org-columns-begin-marker)
345 (when (local-variable-p 'org-previous-header-line-format)
346 (setq header-line-format org-previous-header-line-format)
347 (kill-local-variable 'org-previous-header-line-format)
348 (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
349 (move-marker org-columns-begin-marker nil)
350 (move-marker org-columns-top-level-marker nil)
351 (org-with-silent-modifications
352 (mapc 'delete-overlay org-columns-overlays)
353 (setq org-columns-overlays nil)
354 (let ((inhibit-read-only t))
355 (remove-text-properties (point-min) (point-max) '(read-only t))))
356 (when org-columns-flyspell-was-active
357 (flyspell-mode 1))
358 (when (local-variable-p 'org-colview-initial-truncate-line-value)
359 (setq truncate-lines org-colview-initial-truncate-line-value)))))
361 (defun org-columns-compact-links (s)
362 "Replace [[link][desc]] with [desc] or [link]."
363 (while (string-match org-bracket-link-regexp s)
364 (setq s (replace-match
365 (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
366 t t s)))
369 (defun org-columns-show-value ()
370 "Show the full value of the property."
371 (interactive)
372 (let ((value (get-char-property (point) 'org-columns-value)))
373 (message "Value is: %s" (or value ""))))
375 (defvar org-agenda-columns-active) ;; defined in org-agenda.el
377 (defun org-columns-quit ()
378 "Remove the column overlays and in this way exit column editing."
379 (interactive)
380 (org-with-silent-modifications
381 (org-columns-remove-overlays)
382 (let ((inhibit-read-only t))
383 (remove-text-properties (point-min) (point-max) '(read-only t))))
384 (when (eq major-mode 'org-agenda-mode)
385 (setq org-agenda-columns-active nil)
386 (message
387 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
389 (defun org-columns-check-computed ()
390 "Check if this column value is computed.
391 If yes, throw an error indicating that changing it does not make sense."
392 (let ((val (get-char-property (point) 'org-columns-value)))
393 (when (and (stringp val)
394 (get-char-property 0 'org-computed val))
395 (error "This value is computed from the entry's children"))))
397 (defun org-columns-todo (&optional arg)
398 "Change the TODO state during column view."
399 (interactive "P")
400 (org-columns-edit-value "TODO"))
402 (defun org-columns-set-tags-or-toggle (&optional arg)
403 "Toggle checkbox at point, or set tags for current headline."
404 (interactive "P")
405 (if (string-match "\\`\\[[ xX-]\\]\\'"
406 (get-char-property (point) 'org-columns-value))
407 (org-columns-next-allowed-value)
408 (org-columns-edit-value "TAGS")))
410 (defvar org-agenda-overriding-columns-format nil
411 "When set, overrides any other format definition for the agenda.
412 Don't set this, this is meant for dynamic scoping.")
414 (defun org-columns-edit-value (&optional key)
415 "Edit the value of the property at point in column view.
416 Where possible, use the standard interface for changing this line."
417 (interactive)
418 (org-columns-check-computed)
419 (let* ((col (current-column))
420 (key (or key (get-char-property (point) 'org-columns-key)))
421 (value (get-char-property (point) 'org-columns-value))
422 (bol (point-at-bol)) (eol (point-at-eol))
423 (pom (or (get-text-property bol 'org-hd-marker)
424 (point))) ; keep despite of compiler waring
425 (line-overlays
426 (delq nil (mapcar (lambda (x)
427 (and (eq (overlay-buffer x) (current-buffer))
428 (>= (overlay-start x) bol)
429 (<= (overlay-start x) eol)
431 org-columns-overlays)))
432 (org-columns-time (time-to-number-of-days (current-time)))
433 nval eval allowed)
434 (cond
435 ((equal key "CLOCKSUM")
436 (error "This special column cannot be edited"))
437 ((equal key "ITEM")
438 (setq eval '(org-with-point-at pom
439 (org-edit-headline))))
440 ((equal key "TODO")
441 (setq eval '(org-with-point-at
443 (call-interactively 'org-todo))))
444 ((equal key "PRIORITY")
445 (setq eval '(org-with-point-at pom
446 (call-interactively 'org-priority))))
447 ((equal key "TAGS")
448 (setq eval '(org-with-point-at pom
449 (let ((org-fast-tag-selection-single-key
450 (if (eq org-fast-tag-selection-single-key 'expert)
451 t org-fast-tag-selection-single-key)))
452 (call-interactively 'org-set-tags)))))
453 ((equal key "DEADLINE")
454 (setq eval '(org-with-point-at pom
455 (call-interactively 'org-deadline))))
456 ((equal key "SCHEDULED")
457 (setq eval '(org-with-point-at pom
458 (call-interactively 'org-schedule))))
459 ((equal key "BEAMER_env")
460 (setq eval '(org-with-point-at pom
461 (call-interactively 'org-beamer-select-environment))))
463 (setq allowed (org-property-get-allowed-values pom key 'table))
464 (if allowed
465 (setq nval (org-icompleting-read
466 "Value: " allowed nil
467 (not (get-text-property 0 'org-unrestricted
468 (caar allowed)))))
469 (setq nval (read-string "Edit: " value)))
470 (setq nval (org-trim nval))
471 (when (not (equal nval value))
472 (setq eval '(org-entry-put pom key nval)))))
473 (when eval
475 (cond
476 ((equal major-mode 'org-agenda-mode)
477 (org-columns-eval eval)
478 ;; The following let preserves the current format, and makes sure
479 ;; that in only a single file things need to be updated.
480 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
481 (buffer (marker-buffer pom))
482 (org-agenda-contributing-files
483 (list (with-current-buffer buffer
484 (buffer-file-name (buffer-base-buffer))))))
485 (org-agenda-columns)))
487 (let ((inhibit-read-only t))
488 (org-with-silent-modifications
489 (remove-text-properties
490 (max (point-min) (1- bol)) eol '(read-only t)))
491 (unwind-protect
492 (progn
493 (setq org-columns-overlays
494 (org-delete-all line-overlays org-columns-overlays))
495 (mapc 'delete-overlay line-overlays)
496 (org-columns-eval eval))
497 (org-columns-display-here)))
498 (org-move-to-column col)
499 (if (and (derived-mode-p 'org-mode)
500 (nth 3 (assoc-string key org-columns-current-fmt-compiled t)))
501 (org-columns-update key)))))))
503 (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
504 "Edit the current headline, the part without TODO keyword, TAGS."
505 (org-back-to-heading)
506 (when (looking-at org-todo-line-regexp)
507 (let ((pos (point))
508 (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
509 (txt (match-string 3))
510 (post "")
511 txt2)
512 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@#%]+:[ \t]*$") txt)
513 (setq post (match-string 0 txt)
514 txt (substring txt 0 (match-beginning 0))))
515 (setq txt2 (read-string "Edit: " txt))
516 (when (not (equal txt txt2))
517 (goto-char pos)
518 (insert pre txt2 post)
519 (delete-region (point) (point-at-eol))
520 (org-set-tags nil t)))))
522 (defun org-columns-edit-allowed ()
523 "Edit the list of allowed values for the current property."
524 (interactive)
525 (let* ((pom (or (org-get-at-bol 'org-marker)
526 (org-get-at-bol 'org-hd-marker)
527 (point)))
528 (key (get-char-property (point) 'org-columns-key))
529 (key1 (concat key "_ALL"))
530 (allowed (org-entry-get pom key1 t))
531 nval)
532 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
533 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
534 (setq nval (read-string "Allowed: " allowed))
535 (org-entry-put
536 (cond ((marker-position org-entry-property-inherited-from)
537 org-entry-property-inherited-from)
538 ((marker-position org-columns-top-level-marker)
539 org-columns-top-level-marker)
540 (t pom))
541 key1 nval)))
543 (defun org-columns-eval (form)
544 (let (hidep)
545 (save-excursion
546 (beginning-of-line 1)
547 ;; `next-line' is needed here, because it skips invisible line.
548 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
549 (setq hidep (org-at-heading-p 1)))
550 (eval form)
551 (and hidep (hide-entry))))
553 (defun org-columns-previous-allowed-value ()
554 "Switch to the previous allowed value for this column."
555 (interactive)
556 (org-columns-next-allowed-value t))
558 (defun org-columns-next-allowed-value (&optional previous nth)
559 "Switch to the next allowed value for this column.
560 When PREVIOUS is set, go to the previous value. When NTH is
561 an integer, select that value."
562 (interactive)
563 (org-columns-check-computed)
564 (let* ((col (current-column))
565 (key (get-char-property (point) 'org-columns-key))
566 (value (get-char-property (point) 'org-columns-value))
567 (bol (point-at-bol)) (eol (point-at-eol))
568 (pom (or (get-text-property bol 'org-hd-marker)
569 (point))) ; keep despite of compiler waring
570 (line-overlays
571 (delq nil (mapcar (lambda (x)
572 (and (eq (overlay-buffer x) (current-buffer))
573 (>= (overlay-start x) bol)
574 (<= (overlay-start x) eol)
576 org-columns-overlays)))
577 (allowed (or (org-property-get-allowed-values pom key)
578 (and (memq
579 (nth 4 (assoc-string key
580 org-columns-current-fmt-compiled
582 '(checkbox checkbox-n-of-m checkbox-percent))
583 '("[ ]" "[X]"))
584 (org-colview-construct-allowed-dates value)))
585 nval)
586 (when (integerp nth)
587 (setq nth (1- nth))
588 (if (= nth -1) (setq nth 9)))
589 (when (equal key "ITEM")
590 (error "Cannot edit item headline from here"))
591 (unless (or allowed (member key '("SCHEDULED" "DEADLINE" "CLOCKSUM")))
592 (error "Allowed values for this property have not been defined"))
593 (if (member key '("SCHEDULED" "DEADLINE" "CLOCKSUM"))
594 (setq nval (if previous 'earlier 'later))
595 (if previous (setq allowed (reverse allowed)))
596 (cond
597 (nth
598 (setq nval (nth nth allowed))
599 (if (not nval)
600 (error "There are only %d allowed values for property `%s'"
601 (length allowed) key)))
602 ((member value allowed)
603 (setq nval (or (car (cdr (member value allowed)))
604 (car allowed)))
605 (if (equal nval value)
606 (error "Only one allowed value for this property")))
607 (t (setq nval (car allowed)))))
608 (cond
609 ((equal major-mode 'org-agenda-mode)
610 (org-columns-eval '(org-entry-put pom key nval))
611 ;; The following let preserves the current format, and makes sure
612 ;; that in only a single file things need to be updated.
613 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
614 (buffer (marker-buffer pom))
615 (org-agenda-contributing-files
616 (list (with-current-buffer buffer
617 (buffer-file-name (buffer-base-buffer))))))
618 (org-agenda-columns)))
620 (let ((inhibit-read-only t))
621 (remove-text-properties (1- bol) eol '(read-only t))
622 (unwind-protect
623 (progn
624 (setq org-columns-overlays
625 (org-delete-all line-overlays org-columns-overlays))
626 (mapc 'delete-overlay line-overlays)
627 (org-columns-eval '(org-entry-put pom key nval)))
628 (org-columns-display-here)))
629 (org-move-to-column col)
630 (and (nth 3 (assoc-string key org-columns-current-fmt-compiled t))
631 (org-columns-update key))))))
633 (defun org-colview-construct-allowed-dates (s)
634 "Construct a list of three dates around the date in S.
635 This respects the format of the time stamp in S, active or non-active,
636 and also including time or not. S must be just a time stamp, no text
637 around it."
638 (when (and s (string-match (concat "^" org-ts-regexp3 "$") s))
639 (let* ((time (org-parse-time-string s 'nodefaults))
640 (active (equal (string-to-char s) ?<))
641 (fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats))
642 time-before time-after)
643 (unless active (setq fmt (concat "[" (substring fmt 1 -1) "]")))
644 (setf (car time) (or (car time) 0))
645 (setf (nth 1 time) (or (nth 1 time) 0))
646 (setf (nth 2 time) (or (nth 2 time) 0))
647 (setq time-before (copy-sequence time))
648 (setq time-after (copy-sequence time))
649 (setf (nth 3 time-before) (1- (nth 3 time)))
650 (setf (nth 3 time-after) (1+ (nth 3 time)))
651 (mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x)))
652 (list time-before time time-after)))))
654 (defun org-verify-version (task)
655 (cond
656 ((eq task 'columns)
657 (if (or (featurep 'xemacs)
658 (< emacs-major-version 22))
659 (error "Emacs 22 is required for the columns feature")))))
661 (defun org-columns-open-link (&optional arg)
662 (interactive "P")
663 (let ((value (get-char-property (point) 'org-columns-value)))
664 (org-open-link-from-string value arg)))
666 ;;;###autoload
667 (defun org-columns-get-format-and-top-level ()
668 (let ((fmt (org-columns-get-format)))
669 (org-columns-goto-top-level)
670 fmt))
672 (defun org-columns-get-format (&optional fmt-string)
673 (interactive)
674 (let (fmt-as-property fmt)
675 (when (condition-case nil (org-back-to-heading) (error nil))
676 (setq fmt-as-property (org-entry-get nil "COLUMNS" t)))
677 (setq fmt (or fmt-string fmt-as-property org-columns-default-format))
678 (org-set-local 'org-columns-current-fmt fmt)
679 (org-columns-compile-format fmt)
680 fmt))
682 (defun org-columns-goto-top-level ()
683 (when (condition-case nil (org-back-to-heading) (error nil))
684 (org-entry-get nil "COLUMNS" t))
685 (if (marker-position org-entry-property-inherited-from)
686 (move-marker org-columns-top-level-marker org-entry-property-inherited-from)
687 (move-marker org-columns-top-level-marker (point))))
689 ;;;###autoload
690 (defun org-columns (&optional columns-fmt-string)
691 "Turn on column view on an org-mode file.
692 When COLUMNS-FMT-STRING is non-nil, use it as the column format."
693 (interactive)
694 (org-verify-version 'columns)
695 (org-columns-remove-overlays)
696 (move-marker org-columns-begin-marker (point))
697 (org-columns-goto-top-level)
698 ;; Initialize `org-columns-current-fmt' and
699 ;; `org-columns-current-fmt-compiled'.
700 (let ((org-columns-time (time-to-number-of-days (current-time))))
701 (org-columns-get-format columns-fmt-string))
702 (unless org-columns-inhibit-recalculation (org-columns-compute-all))
703 (save-excursion
704 (save-restriction
705 (narrow-to-region
706 org-columns-top-level-marker
707 (or (ignore-errors (org-end-of-subtree t t)) (point-max)))
708 (goto-char (point-min))
709 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
710 (org-clock-sum))
711 (when (assoc "CLOCKSUM_T" org-columns-current-fmt-compiled)
712 (org-clock-sum-today))
713 (let* ((column-names (mapcar #'car org-columns-current-fmt-compiled))
714 (cache
715 (org-map-entries
716 (lambda ()
717 (cons (point)
718 (mapcar (lambda (p)
719 (cons p (org-columns--value p (point))))
720 column-names)))
721 nil nil (and org-columns-skip-archived-trees 'archive))))
722 (when cache
723 (org-set-local 'org-columns-current-maxwidths
724 (org-columns-get-autowidth-alist
725 org-columns-current-fmt
726 cache))
727 (org-columns-display-here-title)
728 (when (org-set-local 'org-columns-flyspell-was-active
729 (org-bound-and-true-p flyspell-mode))
730 (flyspell-mode 0))
731 (unless (local-variable-p 'org-colview-initial-truncate-line-value)
732 (org-set-local 'org-colview-initial-truncate-line-value
733 truncate-lines))
734 (setq truncate-lines t)
735 (dolist (x cache)
736 (goto-char (car x))
737 (org-columns-display-here (cdr x))))))))
739 (eval-when-compile (defvar org-columns-time))
741 (defvar org-columns-compile-map
742 '(("none" none +)
743 (":" add_times +)
744 ("+" add_numbers +)
745 ("$" currency +)
746 ("X" checkbox +)
747 ("X/" checkbox-n-of-m +)
748 ("X%" checkbox-percent +)
749 ("max" max_numbers max)
750 ("min" min_numbers min)
751 ("mean" mean_numbers
752 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
753 (":max" max_times max)
754 (":min" min_times min)
755 (":mean" mean_times
756 (lambda (&rest x) (/ (apply '+ x) (float (length x)))))
757 ("@min" min_age min (lambda (x) (- org-columns-time x)))
758 ("@max" max_age max (lambda (x) (- org-columns-time x)))
759 ("@mean" mean_age
760 (lambda (&rest x) (/ (apply '+ x) (float (length x))))
761 (lambda (x) (- org-columns-time x)))
762 ("est+" estimate org-estimate-combine))
763 "Operator <-> format,function,calc map.
764 Used to compile/uncompile columns format and completing read in
765 interactive function `org-columns-new'.
767 operator string used in #+COLUMNS definition describing the
768 summary type
769 format symbol describing summary type selected interactively in
770 `org-columns-new' and internally in
771 `org-columns-number-to-string' and
772 `org-columns-string-to-number'
773 function called with a list of values as argument to calculate
774 the summary value
775 calc function called on every element before summarizing. This is
776 optional and should only be specified if needed")
778 (defun org-columns-new (&optional prop title width op fmt fun &rest rest)
779 "Insert a new column, to the left of the current column."
780 (interactive)
781 (let ((editp (and prop
782 (assoc-string prop org-columns-current-fmt-compiled t)))
783 cell)
784 (setq prop (org-icompleting-read
785 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
786 nil nil prop))
787 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
788 (setq width (read-string "Column width: " (if width (number-to-string width))))
789 (if (string-match "\\S-" width)
790 (setq width (string-to-number width))
791 (setq width nil))
792 (setq fmt (org-icompleting-read
793 "Summary [none]: "
794 (mapcar (lambda (x) (list (symbol-name (cadr x))))
795 org-columns-compile-map)
796 nil t))
797 (setq fmt (intern fmt)
798 fun (cdr (assoc fmt (mapcar 'cdr org-columns-compile-map))))
799 (if (eq fmt 'none) (setq fmt nil))
800 (if editp
801 (progn
802 (setcar editp prop)
803 (setcdr editp (list title width nil fmt nil fun)))
804 (setq cell (nthcdr (1- (current-column))
805 org-columns-current-fmt-compiled))
806 (setcdr cell (cons (list prop title width nil fmt nil
807 (car fun) (cadr fun))
808 (cdr cell))))
809 (org-columns-store-format)
810 (org-columns-redo)))
812 (defun org-columns-delete ()
813 "Delete the column at point from columns view."
814 (interactive)
815 (let* ((n (current-column))
816 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
817 (when (y-or-n-p
818 (format "Are you sure you want to remove column \"%s\"? " title))
819 (setq org-columns-current-fmt-compiled
820 (delq (nth n org-columns-current-fmt-compiled)
821 org-columns-current-fmt-compiled))
822 (org-columns-store-format)
823 (org-columns-redo)
824 (if (>= (current-column) (length org-columns-current-fmt-compiled))
825 (backward-char 1)))))
827 (defun org-columns-edit-attributes ()
828 "Edit the attributes of the current column."
829 (interactive)
830 (let* ((n (current-column))
831 (info (nth n org-columns-current-fmt-compiled)))
832 (apply 'org-columns-new info)))
834 (defun org-columns-widen (arg)
835 "Make the column wider by ARG characters."
836 (interactive "p")
837 (let* ((n (current-column))
838 (entry (nth n org-columns-current-fmt-compiled))
839 (width (or (nth 2 entry)
840 (cdr (assoc-string (car entry)
841 org-columns-current-maxwidths
842 t)))))
843 (setq width (max 1 (+ width arg)))
844 (setcar (nthcdr 2 entry) width)
845 (org-columns-store-format)
846 (org-columns-redo)))
848 (defun org-columns-narrow (arg)
849 "Make the column narrower by ARG characters."
850 (interactive "p")
851 (org-columns-widen (- arg)))
853 (defun org-columns-move-right ()
854 "Swap this column with the one to the right."
855 (interactive)
856 (let* ((n (current-column))
857 (cell (nthcdr n org-columns-current-fmt-compiled))
859 (when (>= n (1- (length org-columns-current-fmt-compiled)))
860 (error "Cannot shift this column further to the right"))
861 (setq e (car cell))
862 (setcar cell (car (cdr cell)))
863 (setcdr cell (cons e (cdr (cdr cell))))
864 (org-columns-store-format)
865 (org-columns-redo)
866 (forward-char 1)))
868 (defun org-columns-move-left ()
869 "Swap this column with the one to the left."
870 (interactive)
871 (let* ((n (current-column)))
872 (when (= n 0)
873 (error "Cannot shift this column further to the left"))
874 (backward-char 1)
875 (org-columns-move-right)
876 (backward-char 1)))
878 (defun org-columns-store-format ()
879 "Store the text version of the current columns format in appropriate place.
880 This is either in the COLUMNS property of the node starting the current column
881 display, or in the #+COLUMNS line of the current buffer."
882 (let (fmt (cnt 0))
883 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
884 (org-set-local 'org-columns-current-fmt fmt)
885 (if (marker-position org-columns-top-level-marker)
886 (save-excursion
887 (goto-char org-columns-top-level-marker)
888 (if (and (org-at-heading-p)
889 (org-entry-get nil "COLUMNS"))
890 (org-entry-put nil "COLUMNS" fmt)
891 (goto-char (point-min))
892 ;; Overwrite all #+COLUMNS lines....
893 (while (re-search-forward "^[ \t]*#\\+COLUMNS:.*" nil t)
894 (setq cnt (1+ cnt))
895 (replace-match (concat "#+COLUMNS: " fmt) t t))
896 (unless (> cnt 0)
897 (goto-char (point-min))
898 (or (org-at-heading-p t) (outline-next-heading))
899 (let ((inhibit-read-only t))
900 (insert-before-markers "#+COLUMNS: " fmt "\n")))
901 (org-set-local 'org-columns-default-format fmt))))))
903 (defun org-columns-get-autowidth-alist (s cache)
904 "Derive the maximum column widths from the format and the cache."
905 (let ((start 0) rtn)
906 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
907 (push (cons (match-string 1 s) 1) rtn)
908 (setq start (match-end 0)))
909 (mapc (lambda (x)
910 (setcdr x
911 (apply #'max
912 (let ((prop (car x)))
913 (mapcar
914 (lambda (y)
915 (length (or (cdr (assoc-string prop (cdr y) t))
916 " ")))
917 cache)))))
918 rtn)
919 rtn))
921 (defun org-columns-compute-all ()
922 "Compute all columns that have operators defined."
923 (org-with-silent-modifications
924 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
925 (let ((columns org-columns-current-fmt-compiled)
926 (org-columns-time (time-to-number-of-days (current-time)))
927 col)
928 (while (setq col (pop columns))
929 (when (nth 3 col)
930 (save-excursion
931 (org-columns-compute (car col)))))))
933 (defun org-columns-update (property)
934 "Recompute PROPERTY, and update the columns display for it."
935 (org-columns-compute property)
936 (let (fmt val pos)
937 (save-excursion
938 (mapc (lambda (ov)
939 (when (equal (overlay-get ov 'org-columns-key) property)
940 (setq pos (overlay-start ov))
941 (goto-char pos)
942 (when (setq val (cdr (assoc-string
943 property
944 (get-text-property
945 (point-at-bol) 'org-summaries)
946 t)))
947 (setq fmt (overlay-get ov 'org-columns-format))
948 (overlay-put ov 'org-columns-value val)
949 (overlay-put ov 'display (format fmt val)))))
950 org-columns-overlays))))
952 (defvar org-inlinetask-min-level
953 (if (featurep 'org-inlinetask) org-inlinetask-min-level 15))
955 ;;;###autoload
956 (defun org-columns-compute (property)
957 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
958 (interactive)
959 (let* ((re org-outline-regexp-bol)
960 (lmax 30) ; Does anyone use deeper levels???
961 (lvals (make-vector lmax nil))
962 (lflag (make-vector lmax nil))
963 (level 0)
964 (ass (assoc-string property org-columns-current-fmt-compiled t))
965 (format (nth 4 ass))
966 (printf (nth 5 ass))
967 (fun (nth 6 ass))
968 (calc (or (nth 7 ass) 'identity))
969 (beg org-columns-top-level-marker)
970 (inminlevel org-inlinetask-min-level)
971 (last-level org-inlinetask-min-level)
972 val valflag flag end sumpos sum-alist sum str str1 useval)
973 (save-excursion
974 ;; Find the region to compute
975 (goto-char beg)
976 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
977 (goto-char end)
978 ;; Walk the tree from the back and do the computations
979 (while (re-search-backward re beg t)
980 (setq sumpos (match-beginning 0)
981 last-level (if (not (or (zerop level) (eq level inminlevel)))
982 level last-level)
983 level (org-outline-level)
984 val (org-entry-get nil property)
985 valflag (and val (string-match "\\S-" val)))
986 (cond
987 ((< level last-level)
988 ;; Put the sum of lower levels here as a property. If
989 ;; values are estimate, use an appropriate sum function.
990 (setq sum (funcall
991 (if (eq fun 'org-estimate-combine) #'org-estimate-combine
992 #'+)
993 (if (and (/= last-level inminlevel)
994 (aref lvals last-level))
995 (apply fun (aref lvals last-level)) 0)
996 (if (aref lvals inminlevel)
997 (apply fun (aref lvals inminlevel)) 0))
998 flag (or (aref lflag last-level) ; any valid entries from children?
999 (aref lflag inminlevel)) ; or inline tasks?
1000 str (org-columns-number-to-string sum format printf)
1001 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
1002 useval (if flag str1 (if valflag val ""))
1003 sum-alist (get-text-property sumpos 'org-summaries))
1004 (let ((old (assoc-string property sum-alist t)))
1005 (if old (setcdr old useval)
1006 (push (cons property useval) sum-alist)
1007 (org-with-silent-modifications
1008 (add-text-properties sumpos (1+ sumpos)
1009 (list 'org-summaries sum-alist)))))
1010 (when (and val (not (equal val (if flag str val))))
1011 (org-entry-put nil property (if flag str val)))
1012 ;; add current to current level accumulator
1013 (when (or flag valflag)
1014 (push (if flag
1016 (funcall calc (org-columns-string-to-number
1017 (if flag str val) format)))
1018 (aref lvals level))
1019 (aset lflag level t))
1020 ;; clear accumulators for deeper levels
1021 (loop for l from (1+ level) to (1- lmax) do
1022 (aset lvals l nil)
1023 (aset lflag l nil)))
1024 ((>= level last-level)
1025 ;; add what we have here to the accumulator for this level
1026 (when valflag
1027 (push (funcall calc (org-columns-string-to-number val format))
1028 (aref lvals level))
1029 (aset lflag level t)))
1030 (t (error "This should not happen")))))))
1032 (defun org-columns-redo ()
1033 "Construct the column display again."
1034 (interactive)
1035 (message "Recomputing columns...")
1036 (let ((line (org-current-line))
1037 (col (current-column)))
1038 (save-excursion
1039 (if (marker-position org-columns-begin-marker)
1040 (goto-char org-columns-begin-marker))
1041 (org-columns-remove-overlays)
1042 (if (derived-mode-p 'org-mode)
1043 (call-interactively 'org-columns)
1044 (org-agenda-redo)
1045 (call-interactively 'org-agenda-columns)))
1046 (org-goto-line line)
1047 (move-to-column col))
1048 (message "Recomputing columns...done"))
1050 (defun org-columns-not-in-agenda ()
1051 (if (eq major-mode 'org-agenda-mode)
1052 (error "This command is only allowed in Org-mode buffers")))
1054 (defun org-string-to-number (s)
1055 "Convert string to number, and interpret hh:mm:ss."
1056 (if (not (string-match ":" s))
1057 (string-to-number s)
1058 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1059 (while l
1060 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1061 sum)))
1063 ;;;###autoload
1064 (defun org-columns-number-to-string (n fmt &optional printf)
1065 "Convert a computed column number to a string value, according to FMT."
1066 (cond
1067 ((memq fmt '(estimate)) (org-estimate-print n printf))
1068 ((not (numberp n)) "")
1069 ((memq fmt '(add_times max_times min_times mean_times))
1070 (org-hours-to-clocksum-string n))
1071 ((eq fmt 'checkbox)
1072 (cond ((= n (floor n)) "[X]")
1073 ((> n 1.) "[-]")
1074 (t "[ ]")))
1075 ((memq fmt '(checkbox-n-of-m checkbox-percent))
1076 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
1077 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
1078 (printf (format printf n))
1079 ((eq fmt 'currency)
1080 (format "%.2f" n))
1081 ((memq fmt '(min_age max_age mean_age))
1082 (org-format-time-period n))
1083 (t (number-to-string n))))
1085 (defun org-nofm-to-completion (n m &optional percent)
1086 (if (not percent)
1087 (format "[%d/%d]" n m)
1088 (format "[%d%%]" (round (* 100.0 n) m))))
1091 (defun org-columns-string-to-number (s fmt)
1092 "Convert a column value to a number that can be used for column computing."
1093 (if s
1094 (cond
1095 ((memq fmt '(min_age max_age mean_age))
1096 (cond ((string= s "") org-columns-time)
1097 ((string-match
1098 "\\([0-9]+\\)d \\([0-9]+\\)h \\([0-9]+\\)m \\([0-9]+\\)s"
1100 (+ (* 60 (+ (* 60 (+ (* 24 (string-to-number (match-string 1 s)))
1101 (string-to-number (match-string 2 s))))
1102 (string-to-number (match-string 3 s))))
1103 (string-to-number (match-string 4 s))))
1104 (t (time-to-number-of-days (apply 'encode-time
1105 (org-parse-time-string s t))))))
1106 ((string-match ":" s)
1107 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1108 (while l
1109 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1110 sum))
1111 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
1112 (if (equal s "[X]") 1. 0.000001))
1113 ((memq fmt '(estimate)) (org-string-to-estimate s))
1114 ((string-match (concat "\\([0-9.]+\\) *\\("
1115 (regexp-opt (mapcar 'car org-effort-durations))
1116 "\\)") s)
1117 (setq s (concat "0:" (org-duration-string-to-minutes s t)))
1118 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1119 (while l
1120 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1121 sum))
1122 (t (string-to-number s)))))
1124 (defun org-columns-uncompile-format (cfmt)
1125 "Turn the compiled columns format back into a string representation."
1126 (let ((rtn "") e s prop title op op-match width fmt printf fun calc ee map)
1127 (while (setq e (pop cfmt))
1128 (setq prop (car e)
1129 title (nth 1 e)
1130 width (nth 2 e)
1131 op (nth 3 e)
1132 fmt (nth 4 e)
1133 printf (nth 5 e)
1134 fun (nth 6 e)
1135 calc (nth 7 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 title)
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 title (nth 1 f)
1485 stype (nth 4 f)
1486 sumfunc (nth 6 f)
1487 calc (or (nth 7 f) 'identity))
1488 (cond
1489 ((equal prop "ITEM")
1490 (cons prop (buffer-substring (point-at-bol)
1491 (point-at-eol))))
1492 ((not stype) (cons prop ""))
1493 (t ;; do the summary
1494 (setq lsum nil)
1495 (dolist (x entries)
1496 (setq v (cdr (assoc-string prop x t)))
1497 (if v
1498 (push
1499 (funcall
1500 (if (not (get-text-property 0 'org-computed v))
1501 calc
1502 'identity)
1503 (org-columns-string-to-number
1504 v stype))
1505 lsum)))
1506 (setq lsum (remove nil lsum))
1507 (setq lsum
1508 (cond ((> (length lsum) 1)
1509 (org-columns-number-to-string
1510 (apply sumfunc lsum) stype))
1511 ((eq (length lsum) 1)
1512 (org-columns-number-to-string
1513 (car lsum) stype))
1514 (t "")))
1515 (put-text-property 0 (length lsum) 'face 'bold lsum)
1516 (unless (eq calc 'identity)
1517 (put-text-property 0 (length lsum) 'org-computed t lsum))
1518 (cons prop lsum))))
1519 fmt))
1520 (org-columns-display-here props 'dateline)
1521 (org-set-local 'org-agenda-columns-active t)))
1522 (if (bobp) (throw 'exit t))
1523 (beginning-of-line 0))))))
1525 (defun org-agenda-colview-compute (fmt)
1526 "Compute the relevant columns in the contributing source buffers."
1527 (let ((files org-agenda-contributing-files)
1528 (org-columns-begin-marker (make-marker))
1529 (org-columns-top-level-marker (make-marker))
1530 f fm a b)
1531 (while (setq f (pop files))
1532 (setq b (find-buffer-visiting f))
1533 (with-current-buffer (or (buffer-base-buffer b) b)
1534 (save-excursion
1535 (save-restriction
1536 (widen)
1537 (org-with-silent-modifications
1538 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
1539 (goto-char (point-min))
1540 (org-columns-get-format-and-top-level)
1541 (while (setq fm (pop fmt))
1542 (cond ((equal (car fm) "CLOCKSUM")
1543 (org-clock-sum))
1544 ((equal (car fm) "CLOCKSUM_T")
1545 (org-clock-sum-today))
1546 ((and (nth 4 fm)
1547 (setq a (assoc-string (car fm)
1548 org-columns-current-fmt-compiled
1550 (equal (nth 4 a) (nth 4 fm)))
1551 (org-columns-compute (car fm)))))))))))
1553 (defun org-format-time-period (interval)
1554 "Convert time in fractional days to days/hours/minutes/seconds."
1555 (if (numberp interval)
1556 (let* ((days (floor interval))
1557 (frac-hours (* 24 (- interval days)))
1558 (hours (floor frac-hours))
1559 (minutes (floor (* 60 (- frac-hours hours))))
1560 (seconds (floor (* 60 (- (* 60 (- frac-hours hours)) minutes)))))
1561 (format "%dd %02dh %02dm %02ds" days hours minutes seconds))
1562 ""))
1564 (defun org-estimate-mean-and-var (v)
1565 "Return the mean and variance of an estimate."
1566 (let* ((v (cond ((consp v) v)
1567 ((numberp v) (list v v))
1568 (t (error "Invalid estimate type"))))
1569 (low (float (car v)))
1570 (high (float (cadr v)))
1571 (mean (/ (+ low high) 2.0))
1572 (var (/ (+ (expt (- mean low) 2.0) (expt (- high mean) 2.0)) 2.0)))
1573 (list mean var)))
1575 (defun org-estimate-combine (&rest el)
1576 "Combine a list of estimates, using mean and variance.
1577 The mean and variance of the result will be the sum of the means
1578 and variances (respectively) of the individual estimates."
1579 (let ((mean 0)
1580 (var 0))
1581 (mapc (lambda (e)
1582 (let ((stats (org-estimate-mean-and-var e)))
1583 (setq mean (+ mean (car stats)))
1584 (setq var (+ var (cadr stats)))))
1586 (let ((stdev (sqrt var)))
1587 (list (- mean stdev) (+ mean stdev)))))
1589 (defun org-estimate-print (e &optional fmt)
1590 "Prepare a string representation of an estimate.
1591 This formats these numbers as two numbers with a \"-\" between them."
1592 (let ((fmt (or fmt "%.0f"))
1593 (e (cond ((consp e) e)
1594 ((numberp e) (list e e))
1595 (t (error "Invalid estimate type")))))
1596 (format "%s" (mapconcat (lambda (n) (format fmt n)) e "-"))))
1598 (defun org-string-to-estimate (s)
1599 "Convert a string to an estimate.
1600 The string should be two numbers joined with a \"-\"."
1601 (if (string-match "\\(.*\\)-\\(.*\\)" s)
1602 (list (string-to-number (match-string 1 s))
1603 (string-to-number(match-string 2 s)))
1604 (list (string-to-number s) (string-to-number s))))
1606 (provide 'org-colview)
1608 ;;; org-colview.el ends here