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