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