Merge branch 'maint'
[org-mode.git] / lisp / org-colview.el
blob3838531ab86e04e61145a9a4679bf3e51b2c421e
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 "Move to the beginning of the column view area.
684 Also sets `org-columns-top-level-marker' to the new position."
685 (goto-char
686 (move-marker
687 org-columns-top-level-marker
688 (cond ((org-before-first-heading-p) (point-min))
689 ((org-entry-get nil "COLUMNS" t) org-entry-property-inherited-from)
690 (t (org-back-to-heading) (point))))))
692 ;;;###autoload
693 (defun org-columns (&optional columns-fmt-string)
694 "Turn on column view on an org-mode file.
695 When COLUMNS-FMT-STRING is non-nil, use it as the column format."
696 (interactive)
697 (org-verify-version 'columns)
698 (org-columns-remove-overlays)
699 (move-marker org-columns-begin-marker (point))
700 (org-columns-goto-top-level)
701 ;; Initialize `org-columns-current-fmt' and
702 ;; `org-columns-current-fmt-compiled'.
703 (let ((org-columns-time (time-to-number-of-days (current-time))))
704 (org-columns-get-format columns-fmt-string))
705 (unless org-columns-inhibit-recalculation (org-columns-compute-all))
706 (save-excursion
707 (save-restriction
708 (narrow-to-region
709 (point)
710 (if (org-at-heading-p) (org-end-of-subtree t t) (point-max)))
711 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
712 (org-clock-sum))
713 (when (assoc "CLOCKSUM_T" org-columns-current-fmt-compiled)
714 (org-clock-sum-today))
715 (let* ((column-names (mapcar #'car org-columns-current-fmt-compiled))
716 (cache
717 (org-map-entries
718 (lambda ()
719 (cons (point)
720 (mapcar (lambda (p)
721 (cons p (org-columns--value p (point))))
722 column-names)))
723 nil nil (and org-columns-skip-archived-trees 'archive))))
724 (when cache
725 (org-set-local 'org-columns-current-maxwidths
726 (org-columns-get-autowidth-alist
727 org-columns-current-fmt
728 cache))
729 (org-columns-display-here-title)
730 (when (org-set-local 'org-columns-flyspell-was-active
731 (org-bound-and-true-p flyspell-mode))
732 (flyspell-mode 0))
733 (unless (local-variable-p 'org-colview-initial-truncate-line-value)
734 (org-set-local 'org-colview-initial-truncate-line-value
735 truncate-lines))
736 (setq truncate-lines t)
737 (dolist (x cache)
738 (goto-char (car x))
739 (org-columns-display-here (cdr x))))))))
741 (eval-when-compile (defvar org-columns-time))
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 op-match width fmt printf fun calc 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 fun (nth 6 e)
1137 calc (nth 7 e))
1138 (setq map (copy-sequence org-columns-compile-map))
1139 (while (setq ee (pop map))
1140 (if (equal fmt (nth 1 ee))
1141 (setq op (car ee) map nil)))
1142 (if (and op printf) (setq op (concat op ";" printf)))
1143 (if (equal title prop) (setq title nil))
1144 (setq s (concat "%" (if width (number-to-string width))
1145 prop
1146 (if title (concat "(" title ")"))
1147 (if op (concat "{" op "}"))))
1148 (setq rtn (concat rtn " " s)))
1149 (org-trim rtn)))
1151 (defun org-columns-compile-format (fmt)
1152 "Turn a column format string FMT into an alist of specifications.
1154 The alist has one entry for each column in the format. The elements of
1155 that list are:
1156 property the property
1157 title the title field for the columns
1158 width the column width in characters, can be nil for automatic
1159 operator the operator if any
1160 format the output format for computed results, derived from operator
1161 printf a printf format for computed values
1162 fun the lisp function to compute summary values, derived from operator
1163 calc function to get values from base elements
1165 This function updates `org-columns-current-fmt-compiled'."
1166 (let ((start 0) width prop title op op-match f printf fun calc)
1167 (setq org-columns-current-fmt-compiled nil)
1168 (while (string-match
1169 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
1170 fmt start)
1171 (setq start (match-end 0)
1172 width (match-string 1 fmt)
1173 prop (match-string 2 fmt)
1174 title (or (match-string 3 fmt) prop)
1175 op (match-string 4 fmt)
1176 f nil
1177 printf nil
1178 fun '+
1179 calc nil)
1180 (if width (setq width (string-to-number width)))
1181 (when (and op (string-match ";" op))
1182 (setq printf (substring op (match-end 0))
1183 op (substring op 0 (match-beginning 0))))
1184 (when (setq op-match (assoc op org-columns-compile-map))
1185 (setq f (cadr op-match)
1186 fun (caddr op-match)
1187 calc (cadddr op-match)))
1188 (push (list prop title width op f printf fun calc)
1189 org-columns-current-fmt-compiled))
1190 (setq org-columns-current-fmt-compiled
1191 (nreverse org-columns-current-fmt-compiled))))
1194 ;;; Dynamic block for Column view
1196 (defvar org-heading-regexp) ; defined in org.el
1197 (defvar org-heading-keyword-regexp-format) ; defined in org.el
1198 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
1199 "Get the column view of the current buffer or subtree.
1200 The first optional argument MAXLEVEL sets the level limit. A
1201 second optional argument SKIP-EMPTY-ROWS tells whether to skip
1202 empty rows, an empty row being one where all the column view
1203 specifiers except ITEM are empty. This function returns a list
1204 containing the title row and all other rows. Each row is a list
1205 of fields."
1206 (save-excursion
1207 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1208 (re-archive (concat ".*:" org-archive-tag ":"))
1209 (n (length title)) row tbl)
1210 (goto-char (point-min))
1211 (while (re-search-forward org-heading-regexp nil t)
1212 (catch 'next
1213 (when (and (or (null maxlevel)
1214 (>= maxlevel
1215 (if org-odd-levels-only
1216 (/ (1+ (length (match-string 1))) 2)
1217 (length (match-string 1)))))
1218 (get-char-property (match-beginning 0) 'org-columns-key))
1219 (when (or (org-in-commented-heading-p t)
1220 (save-excursion
1221 (beginning-of-line)
1222 (looking-at re-archive)))
1223 (org-end-of-subtree t)
1224 (throw 'next t))
1225 (setq row nil)
1226 (loop for i from 0 to (1- n) do
1227 (push
1228 (org-quote-vert
1229 (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
1230 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
1231 ""))
1232 row))
1233 (setq row (nreverse row))
1234 (unless (and skip-empty-rows
1235 (eq 1 (length (delete "" (delete-dups (copy-sequence row))))))
1236 (push row tbl)))))
1237 (append (list title 'hline) (nreverse tbl)))))
1239 ;;;###autoload
1240 (defun org-dblock-write:columnview (params)
1241 "Write the column view table.
1242 PARAMS is a property list of parameters:
1244 :width enforce same column widths with <N> specifiers.
1245 :id the :ID: property of the entry where the columns view
1246 should be built. When the symbol `local', call locally.
1247 When `global' call column view with the cursor at the beginning
1248 of the buffer (usually this means that the whole buffer switches
1249 to column view). When \"file:path/to/file.org\", invoke column
1250 view at the start of that file. Otherwise, the ID is located
1251 using `org-id-find'.
1252 :hlines When t, insert a hline before each item. When a number, insert
1253 a hline before each level <= that number.
1254 :vlines When t, make each column a colgroup to enforce vertical lines.
1255 :maxlevel When set to a number, don't capture headlines below this level.
1256 :skip-empty-rows
1257 When t, skip rows where all specifiers other than ITEM are empty.
1258 :format When non-nil, specify the column view format to use."
1259 (let ((pos (point-marker))
1260 (hlines (plist-get params :hlines))
1261 (vlines (plist-get params :vlines))
1262 (maxlevel (plist-get params :maxlevel))
1263 (content-lines (org-split-string (plist-get params :content) "\n"))
1264 (skip-empty-rows (plist-get params :skip-empty-rows))
1265 (columns-fmt (plist-get params :format))
1266 (case-fold-search t)
1267 tbl id idpos nfields tmp recalc line
1268 id-as-string view-file view-pos)
1269 (when (setq id (plist-get params :id))
1270 (setq id-as-string (cond ((numberp id) (number-to-string id))
1271 ((symbolp id) (symbol-name id))
1272 ((stringp id) id)
1273 (t "")))
1274 (cond ((not id) nil)
1275 ((eq id 'global) (setq view-pos (point-min)))
1276 ((eq id 'local))
1277 ((string-match "^file:\\(.*\\)" id-as-string)
1278 (setq view-file (match-string 1 id-as-string)
1279 view-pos 1)
1280 (unless (file-exists-p view-file)
1281 (error "No such file: \"%s\"" id-as-string)))
1282 ((setq idpos (org-find-entry-with-id id))
1283 (setq view-pos idpos))
1284 ((setq idpos (org-id-find id))
1285 (setq view-file (car idpos))
1286 (setq view-pos (cdr idpos)))
1287 (t (error "Cannot find entry with :ID: %s" id))))
1288 (with-current-buffer (if view-file
1289 (get-file-buffer view-file)
1290 (current-buffer))
1291 (save-excursion
1292 (save-restriction
1293 (widen)
1294 (goto-char (or view-pos (point)))
1295 (org-columns columns-fmt)
1296 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1297 (setq nfields (length (car tbl)))
1298 (org-columns-quit))))
1299 (goto-char pos)
1300 (move-marker pos nil)
1301 (when tbl
1302 (when (plist-get params :hlines)
1303 (setq tmp nil)
1304 (while tbl
1305 (if (eq (car tbl) 'hline)
1306 (push (pop tbl) tmp)
1307 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1308 (if (and (not (eq (car tmp) 'hline))
1309 (or (eq hlines t)
1310 (and (numberp hlines)
1311 (<= (- (match-end 1) (match-beginning 1))
1312 hlines))))
1313 (push 'hline tmp)))
1314 (push (pop tbl) tmp)))
1315 (setq tbl (nreverse tmp)))
1316 (when vlines
1317 (setq tbl (mapcar (lambda (x)
1318 (if (eq 'hline x) x (cons "" x)))
1319 tbl))
1320 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1321 (when content-lines
1322 (while (string-match "^#" (car content-lines))
1323 (insert (pop content-lines) "\n")))
1324 (setq pos (point))
1325 (insert (org-listtable-to-string tbl))
1326 (when (plist-get params :width)
1327 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1328 org-columns-current-widths "|")))
1329 (while (setq line (pop content-lines))
1330 (when (string-match "^#" line)
1331 (insert "\n" line)
1332 (when (string-match "^[ \t]*#\\+tblfm" line)
1333 (setq recalc t))))
1334 (if recalc
1335 (progn (goto-char pos) (org-table-recalculate 'all))
1336 (goto-char pos)
1337 (org-table-align)))))
1339 (defun org-listtable-to-string (tbl)
1340 "Convert a listtable TBL to a string that contains the Org-mode table.
1341 The table still need to be aligned. The resulting string has no leading
1342 and tailing newline characters."
1343 (mapconcat
1344 (lambda (x)
1345 (cond
1346 ((listp x)
1347 (concat "|" (mapconcat 'identity x "|") "|"))
1348 ((eq x 'hline) "|-|")
1349 (t (error "Garbage in listtable: %s" x))))
1350 tbl "\n"))
1352 ;;;###autoload
1353 (defun org-insert-columns-dblock ()
1354 "Create a dynamic block capturing a column view table."
1355 (interactive)
1356 (let ((defaults '(:name "columnview" :hlines 1))
1357 (id (org-icompleting-read
1358 "Capture columns (local, global, entry with :ID: property) [local]: "
1359 (append '(("global") ("local"))
1360 (mapcar 'list (org-property-values "ID"))))))
1361 (if (equal id "") (setq id 'local))
1362 (if (equal id "global") (setq id 'global))
1363 (setq defaults (append defaults (list :id id)))
1364 (org-create-dblock defaults)
1365 (org-update-dblock)))
1367 ;;; Column view in the agenda
1369 (defvar org-agenda-view-columns-initially nil
1370 "When set, switch to columns view immediately after creating the agenda.")
1372 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1373 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1374 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1376 ;;;###autoload
1377 (defun org-agenda-columns ()
1378 "Turn on or update column view in the agenda."
1379 (interactive)
1380 (org-verify-version 'columns)
1381 (org-columns-remove-overlays)
1382 (move-marker org-columns-begin-marker (point))
1383 (let ((org-columns-time (time-to-number-of-days (current-time)))
1384 (fmt
1385 (cond
1386 ((org-bound-and-true-p org-agenda-overriding-columns-format))
1387 ((let ((m (org-get-at-bol 'org-hd-marker)))
1388 (and m
1389 (or (org-entry-get m "COLUMNS" t)
1390 (with-current-buffer (marker-buffer m)
1391 org-columns-default-format)))))
1392 ((and (local-variable-p 'org-columns-current-fmt)
1393 org-columns-current-fmt))
1394 ((let ((m (next-single-property-change (point-min) 'org-hd-marker)))
1395 (and m
1396 (let ((m (get-text-property m 'org-hd-marker)))
1397 (or (org-entry-get m "COLUMNS" t)
1398 (with-current-buffer (marker-buffer m)
1399 org-columns-default-format))))))
1400 (t org-columns-default-format))))
1401 (org-set-local 'org-columns-current-fmt fmt)
1402 (org-columns-compile-format fmt)
1403 (when org-agenda-columns-compute-summary-properties
1404 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1405 (save-excursion
1406 ;; Collect properties for each headline in current view.
1407 (goto-char (point-min))
1408 (let (cache)
1409 (let ((names (mapcar #'car org-columns-current-fmt-compiled)) m)
1410 (while (not (eobp))
1411 (when (setq m (or (org-get-at-bol 'org-hd-marker)
1412 (org-get-at-bol 'org-marker)))
1413 (push
1414 (cons
1415 (line-beginning-position)
1416 (org-with-point-at m
1417 (mapcar
1418 (lambda (name)
1419 (let ((value (org-columns--value name (point))))
1420 (cons
1421 name
1422 (if (and org-agenda-columns-add-appointments-to-effort-sum
1423 (not value)
1424 (eq (compare-strings name nil nil
1425 org-effort-property nil nil
1428 ;; Effort property is not defined. Try
1429 ;; to use appointment duration.
1430 (get-text-property (point) 'duration))
1431 (org-propertize
1432 (org-minutes-to-clocksum-string
1433 (get-text-property (point) 'duration))
1434 'face 'org-warning)
1435 value))))
1436 names)))
1437 cache))
1438 (forward-line)))
1439 (when cache
1440 (org-set-local 'org-columns-current-maxwidths
1441 (org-columns-get-autowidth-alist fmt cache))
1442 (org-columns-display-here-title)
1443 (when (org-set-local 'org-columns-flyspell-was-active
1444 (org-bound-and-true-p flyspell-mode))
1445 (flyspell-mode 0))
1446 (dolist (x cache)
1447 (goto-char (car x))
1448 (org-columns-display-here (cdr x)))
1449 (when org-agenda-columns-show-summaries
1450 (org-agenda-colview-summarize cache)))))))
1452 (defun org-agenda-colview-summarize (cache)
1453 "Summarize the summarizable columns in column view in the agenda.
1454 This will add overlays to the date lines, to show the summary for each day."
1455 (let* ((fmt (mapcar (lambda (x)
1456 (if (string-match "CLOCKSUM.*" (car x))
1457 (list (match-string 0 (car x))
1458 (nth 1 x) (nth 2 x) ":" 'add_times
1459 nil '+ nil)
1461 org-columns-current-fmt-compiled))
1462 line c c1 stype calc sumfunc props lsum entries prop v title)
1463 (catch 'exit
1464 (when (delq nil (mapcar 'cadr fmt))
1465 ;; OK, at least one summation column, it makes sense to try this
1466 (goto-char (point-max))
1467 (while t
1468 (when (or (get-text-property (point) 'org-date-line)
1469 (eq (get-text-property (point) 'face)
1470 'org-agenda-structure))
1471 ;; OK, this is a date line that should be used
1472 (setq line (org-current-line))
1473 (setq entries nil c cache cache nil)
1474 (while (setq c1 (pop c))
1475 (if (> (car c1) line)
1476 (push c1 entries)
1477 (push c1 cache)))
1478 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1479 ;; Compute the summaries for the properties we want,
1480 ;; set nil properties for the rest.
1481 (when (setq entries (mapcar 'cdr entries))
1482 (setq props
1483 (mapcar
1484 (lambda (f)
1485 (setq prop (car f)
1486 title (nth 1 f)
1487 stype (nth 4 f)
1488 sumfunc (nth 6 f)
1489 calc (or (nth 7 f) 'identity))
1490 (cond
1491 ((equal prop "ITEM")
1492 (cons prop (buffer-substring (point-at-bol)
1493 (point-at-eol))))
1494 ((not stype) (cons prop ""))
1495 (t ;; do the summary
1496 (setq lsum nil)
1497 (dolist (x entries)
1498 (setq v (cdr (assoc-string prop x t)))
1499 (if v
1500 (push
1501 (funcall
1502 (if (not (get-text-property 0 'org-computed v))
1503 calc
1504 'identity)
1505 (org-columns-string-to-number
1506 v stype))
1507 lsum)))
1508 (setq lsum (remove nil lsum))
1509 (setq lsum
1510 (cond ((> (length lsum) 1)
1511 (org-columns-number-to-string
1512 (apply sumfunc lsum) stype))
1513 ((eq (length lsum) 1)
1514 (org-columns-number-to-string
1515 (car lsum) stype))
1516 (t "")))
1517 (put-text-property 0 (length lsum) 'face 'bold lsum)
1518 (unless (eq calc 'identity)
1519 (put-text-property 0 (length lsum) 'org-computed t lsum))
1520 (cons prop lsum))))
1521 fmt))
1522 (org-columns-display-here props 'dateline)
1523 (org-set-local 'org-agenda-columns-active t)))
1524 (if (bobp) (throw 'exit t))
1525 (beginning-of-line 0))))))
1527 (defun org-agenda-colview-compute (fmt)
1528 "Compute the relevant columns in the contributing source buffers."
1529 (let ((files org-agenda-contributing-files)
1530 (org-columns-begin-marker (make-marker))
1531 (org-columns-top-level-marker (make-marker))
1532 f fm a b)
1533 (while (setq f (pop files))
1534 (setq b (find-buffer-visiting f))
1535 (with-current-buffer (or (buffer-base-buffer b) b)
1536 (save-excursion
1537 (save-restriction
1538 (widen)
1539 (org-with-silent-modifications
1540 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
1541 (goto-char (point-min))
1542 (org-columns-get-format-and-top-level)
1543 (while (setq fm (pop fmt))
1544 (cond ((equal (car fm) "CLOCKSUM")
1545 (org-clock-sum))
1546 ((equal (car fm) "CLOCKSUM_T")
1547 (org-clock-sum-today))
1548 ((and (nth 4 fm)
1549 (setq a (assoc-string (car fm)
1550 org-columns-current-fmt-compiled
1552 (equal (nth 4 a) (nth 4 fm)))
1553 (org-columns-compute (car fm)))))))))))
1555 (defun org-format-time-period (interval)
1556 "Convert time in fractional days to days/hours/minutes/seconds."
1557 (if (numberp interval)
1558 (let* ((days (floor interval))
1559 (frac-hours (* 24 (- interval days)))
1560 (hours (floor frac-hours))
1561 (minutes (floor (* 60 (- frac-hours hours))))
1562 (seconds (floor (* 60 (- (* 60 (- frac-hours hours)) minutes)))))
1563 (format "%dd %02dh %02dm %02ds" days hours minutes seconds))
1564 ""))
1566 (defun org-estimate-mean-and-var (v)
1567 "Return the mean and variance of an estimate."
1568 (let* ((v (cond ((consp v) v)
1569 ((numberp v) (list v v))
1570 (t (error "Invalid estimate type"))))
1571 (low (float (car v)))
1572 (high (float (cadr v)))
1573 (mean (/ (+ low high) 2.0))
1574 (var (/ (+ (expt (- mean low) 2.0) (expt (- high mean) 2.0)) 2.0)))
1575 (list mean var)))
1577 (defun org-estimate-combine (&rest el)
1578 "Combine a list of estimates, using mean and variance.
1579 The mean and variance of the result will be the sum of the means
1580 and variances (respectively) of the individual estimates."
1581 (let ((mean 0)
1582 (var 0))
1583 (mapc (lambda (e)
1584 (let ((stats (org-estimate-mean-and-var e)))
1585 (setq mean (+ mean (car stats)))
1586 (setq var (+ var (cadr stats)))))
1588 (let ((stdev (sqrt var)))
1589 (list (- mean stdev) (+ mean stdev)))))
1591 (defun org-estimate-print (e &optional fmt)
1592 "Prepare a string representation of an estimate.
1593 This formats these numbers as two numbers with a \"-\" between them."
1594 (let ((fmt (or fmt "%.0f"))
1595 (e (cond ((consp e) e)
1596 ((numberp e) (list e e))
1597 (t (error "Invalid estimate type")))))
1598 (format "%s" (mapconcat (lambda (n) (format fmt n)) e "-"))))
1600 (defun org-string-to-estimate (s)
1601 "Convert a string to an estimate.
1602 The string should be two numbers joined with a \"-\"."
1603 (if (string-match "\\(.*\\)-\\(.*\\)" s)
1604 (list (string-to-number (match-string 1 s))
1605 (string-to-number(match-string 2 s)))
1606 (list (string-to-number s) (string-to-number s))))
1608 (provide 'org-colview)
1610 ;;; org-colview.el ends here