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