Merge branch 'master' of git+ssh://repo.or.cz/srv/git/org-mode
[org-mode/org-tableheadings.git] / lisp / org-colview.el
blob7633b5bc48dd661feb321941990939b714604eb4
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,fuction 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)))) org-columns-compile-map)
734 nil t))
735 (setq fmt (intern fmt)
736 fun (cadr (assoc fmt (mapcar 'cdr org-columns-compile-map))))
737 (if (eq fmt 'none) (setq fmt nil))
738 (if editp
739 (progn
740 (setcar editp prop)
741 (setcdr editp (list title width nil fmt nil fun)))
742 (setq cell (nthcdr (1- (current-column))
743 org-columns-current-fmt-compiled))
744 (setcdr cell (cons (list prop title width nil fmt nil fun)
745 (cdr cell))))
746 (org-columns-store-format)
747 (org-columns-redo)))
749 (defun org-columns-delete ()
750 "Delete the column at point from columns view."
751 (interactive)
752 (let* ((n (current-column))
753 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
754 (when (y-or-n-p
755 (format "Are you sure you want to remove column \"%s\"? " title))
756 (setq org-columns-current-fmt-compiled
757 (delq (nth n org-columns-current-fmt-compiled)
758 org-columns-current-fmt-compiled))
759 (org-columns-store-format)
760 (org-columns-redo)
761 (if (>= (current-column) (length org-columns-current-fmt-compiled))
762 (backward-char 1)))))
764 (defun org-columns-edit-attributes ()
765 "Edit the attributes of the current column."
766 (interactive)
767 (let* ((n (current-column))
768 (info (nth n org-columns-current-fmt-compiled)))
769 (apply 'org-columns-new info)))
771 (defun org-columns-widen (arg)
772 "Make the column wider by ARG characters."
773 (interactive "p")
774 (let* ((n (current-column))
775 (entry (nth n org-columns-current-fmt-compiled))
776 (width (or (nth 2 entry)
777 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
778 (setq width (max 1 (+ width arg)))
779 (setcar (nthcdr 2 entry) width)
780 (org-columns-store-format)
781 (org-columns-redo)))
783 (defun org-columns-narrow (arg)
784 "Make the column narrower by ARG characters."
785 (interactive "p")
786 (org-columns-widen (- arg)))
788 (defun org-columns-move-right ()
789 "Swap this column with the one to the right."
790 (interactive)
791 (let* ((n (current-column))
792 (cell (nthcdr n org-columns-current-fmt-compiled))
794 (when (>= n (1- (length org-columns-current-fmt-compiled)))
795 (error "Cannot shift this column further to the right"))
796 (setq e (car cell))
797 (setcar cell (car (cdr cell)))
798 (setcdr cell (cons e (cdr (cdr cell))))
799 (org-columns-store-format)
800 (org-columns-redo)
801 (forward-char 1)))
803 (defun org-columns-move-left ()
804 "Swap this column with the one to the left."
805 (interactive)
806 (let* ((n (current-column)))
807 (when (= n 0)
808 (error "Cannot shift this column further to the left"))
809 (backward-char 1)
810 (org-columns-move-right)
811 (backward-char 1)))
813 (defun org-columns-store-format ()
814 "Store the text version of the current columns format in appropriate place.
815 This is either in the COLUMNS property of the node starting the current column
816 display, or in the #+COLUMNS line of the current buffer."
817 (let (fmt (cnt 0))
818 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
819 (org-set-local 'org-columns-current-fmt fmt)
820 (if (marker-position org-columns-top-level-marker)
821 (save-excursion
822 (goto-char org-columns-top-level-marker)
823 (if (and (org-at-heading-p)
824 (org-entry-get nil "COLUMNS"))
825 (org-entry-put nil "COLUMNS" fmt)
826 (goto-char (point-min))
827 ;; Overwrite all #+COLUMNS lines....
828 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
829 (setq cnt (1+ cnt))
830 (replace-match (concat "#+COLUMNS: " fmt) t t))
831 (unless (> cnt 0)
832 (goto-char (point-min))
833 (or (org-on-heading-p t) (outline-next-heading))
834 (let ((inhibit-read-only t))
835 (insert-before-markers "#+COLUMNS: " fmt "\n")))
836 (org-set-local 'org-columns-default-format fmt))))))
838 (defvar org-agenda-overriding-columns-format nil
839 "When set, overrides any other format definition for the agenda.
840 Don't set this, this is meant for dynamic scoping.")
842 (defun org-columns-get-autowidth-alist (s cache)
843 "Derive the maximum column widths from the format and the cache."
844 (let ((start 0) rtn)
845 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
846 (push (cons (match-string 1 s) 1) rtn)
847 (setq start (match-end 0)))
848 (mapc (lambda (x)
849 (setcdr x (apply 'max
850 (mapcar
851 (lambda (y)
852 (length (or (cdr (assoc (car x) (cdr y))) " ")))
853 cache))))
854 rtn)
855 rtn))
857 (defun org-columns-compute-all ()
858 "Compute all columns that have operators defined."
859 (org-unmodified
860 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
861 (let ((columns org-columns-current-fmt-compiled) col)
862 (while (setq col (pop columns))
863 (when (nth 3 col)
864 (save-excursion
865 (org-columns-compute (car col)))))))
867 (defun org-columns-update (property)
868 "Recompute PROPERTY, and update the columns display for it."
869 (org-columns-compute property)
870 (let (fmt val pos)
871 (save-excursion
872 (mapc (lambda (ov)
873 (when (equal (org-overlay-get ov 'org-columns-key) property)
874 (setq pos (org-overlay-start ov))
875 (goto-char pos)
876 (when (setq val (cdr (assoc property
877 (get-text-property
878 (point-at-bol) 'org-summaries))))
879 (setq fmt (org-overlay-get ov 'org-columns-format))
880 (org-overlay-put ov 'org-columns-value val)
881 (org-overlay-put ov 'display (format fmt val)))))
882 org-columns-overlays))))
884 (defun org-columns-compute (property)
885 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
886 (interactive)
887 (let* ((re (concat "^" outline-regexp))
888 (lmax 30) ; Does anyone use deeper levels???
889 (lvals (make-vector lmax nil))
890 (lflag (make-vector lmax nil))
891 (level 0)
892 (ass (assoc property org-columns-current-fmt-compiled))
893 (format (nth 4 ass))
894 (printf (nth 5 ass))
895 (fun (nth 6 ass))
896 (beg org-columns-top-level-marker)
897 last-level val valflag flag end sumpos sum-alist sum str str1 useval)
898 (save-excursion
899 ;; Find the region to compute
900 (goto-char beg)
901 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
902 (goto-char end)
903 ;; Walk the tree from the back and do the computations
904 (while (re-search-backward re beg t)
905 (setq sumpos (match-beginning 0)
906 last-level level
907 level (org-outline-level)
908 val (org-entry-get nil property)
909 valflag (and val (string-match "\\S-" val)))
910 (cond
911 ((< level last-level)
912 ;; put the sum of lower levels here as a property
913 (setq sum (when (aref lvals last-level)
914 (apply fun (aref lvals last-level)))
915 flag (aref lflag last-level) ; any valid entries from children?
916 str (org-columns-number-to-string sum format printf)
917 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
918 useval (if flag str1 (if valflag val ""))
919 sum-alist (get-text-property sumpos 'org-summaries))
920 (if (assoc property sum-alist)
921 (setcdr (assoc property sum-alist) useval)
922 (push (cons property useval) sum-alist)
923 (org-unmodified
924 (add-text-properties sumpos (1+ sumpos)
925 (list 'org-summaries sum-alist))))
926 (when (and val (not (equal val (if flag str val))))
927 (org-entry-put nil property (if flag str val)))
928 ;; add current to current level accumulator
929 (when (or flag valflag)
930 (push (if flag sum
931 (org-column-string-to-number (if flag str val) format))
932 (aref lvals level))
933 (aset lflag level t))
934 ;; clear accumulators for deeper levels
935 (loop for l from (1+ level) to (1- lmax) do
936 (aset lvals l nil)
937 (aset lflag l nil)))
938 ((>= level last-level)
939 ;; add what we have here to the accumulator for this level
940 (when valflag
941 (push (org-column-string-to-number val format)
942 (aref lvals level))
943 (aset lflag level t)))
944 (t (error "This should not happen")))))))
946 (defun org-columns-redo ()
947 "Construct the column display again."
948 (interactive)
949 (message "Recomputing columns...")
950 (let ((line (org-current-line))
951 (col (current-column)))
952 (save-excursion
953 (if (marker-position org-columns-begin-marker)
954 (goto-char org-columns-begin-marker))
955 (org-columns-remove-overlays)
956 (if (org-mode-p)
957 (call-interactively 'org-columns)
958 (org-agenda-redo)
959 (call-interactively 'org-agenda-columns)))
960 (goto-line line)
961 (move-to-column col))
962 (message "Recomputing columns...done"))
964 (defun org-columns-not-in-agenda ()
965 (if (eq major-mode 'org-agenda-mode)
966 (error "This command is only allowed in Org-mode buffers")))
969 (defun org-string-to-number (s)
970 "Convert string to number, and interpret hh:mm:ss."
971 (if (not (string-match ":" s))
972 (string-to-number s)
973 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
974 (while l
975 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
976 sum)))
978 (defun org-columns-number-to-string (n fmt &optional printf)
979 "Convert a computed column number to a string value, according to FMT."
980 (cond
981 ((not (numberp n)) "")
982 ((memq fmt '(add_times max_times min_times mean_times))
983 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
984 (format org-time-clocksum-format h m)))
985 ((eq fmt 'checkbox)
986 (cond ((= n (floor n)) "[X]")
987 ((> n 1.) "[-]")
988 (t "[ ]")))
989 ((memq fmt '(checkbox-n-of-m checkbox-percent))
990 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
991 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
992 (printf (format printf n))
993 ((eq fmt 'currency)
994 (format "%.2f" n))
995 (t (number-to-string n))))
997 (defun org-nofm-to-completion (n m &optional percent)
998 (if (not percent)
999 (format "[%d/%d]" n m)
1000 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
1002 (defun org-column-string-to-number (s fmt)
1003 "Convert a column value to a number that can be used for column computing."
1004 (cond
1005 ((string-match ":" s)
1006 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
1007 (while l
1008 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
1009 sum))
1010 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
1011 (if (equal s "[X]") 1. 0.000001))
1012 (t (string-to-number s))))
1014 (defun org-columns-uncompile-format (cfmt)
1015 "Turn the compiled columns format back into a string representation."
1016 (let ((rtn "") e s prop title op op-match width fmt printf)
1017 (while (setq e (pop cfmt))
1018 (setq prop (car e)
1019 title (nth 1 e)
1020 width (nth 2 e)
1021 op (nth 3 e)
1022 fmt (nth 4 e)
1023 printf (nth 5 e)
1024 fun (nth 6 e))
1025 (when (setq op-match (rassoc (list fmt fun) org-columns-compile-map))
1026 (setq op (car op-match)))
1027 (if (and op printf) (setq op (concat op ";" printf)))
1028 (if (equal title prop) (setq title nil))
1029 (setq s (concat "%" (if width (number-to-string width))
1030 prop
1031 (if title (concat "(" title ")"))
1032 (if op (concat "{" op "}"))))
1033 (setq rtn (concat rtn " " s)))
1034 (org-trim rtn)))
1036 (defun org-columns-compile-format (fmt)
1037 "Turn a column format string into an alist of specifications.
1038 The alist has one entry for each column in the format. The elements of
1039 that list are:
1040 property the property
1041 title the title field for the columns
1042 width the column width in characters, can be nil for automatic
1043 operator the operator if any
1044 format the output format for computed results, derived from operator
1045 printf a printf format for computed values
1046 fun the lisp function to compute values, derived from operator"
1047 (let ((start 0) width prop title op op-match f printf fun)
1048 (setq org-columns-current-fmt-compiled nil)
1049 (while (string-match
1050 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
1051 fmt start)
1052 (setq start (match-end 0)
1053 width (match-string 1 fmt)
1054 prop (match-string 2 fmt)
1055 title (or (match-string 3 fmt) prop)
1056 op (match-string 4 fmt)
1057 f nil
1058 printf nil
1059 fun '+)
1060 (if width (setq width (string-to-number width)))
1061 (when (and op (string-match ";" op))
1062 (setq printf (substring op (match-end 0))
1063 op (substring op 0 (match-beginning 0))))
1064 (when (setq op-match (assoc op org-columns-compile-map))
1065 (setq f (cadr op-match)
1066 fun (caddr op-match)))
1067 (push (list prop title width op f printf fun) org-columns-current-fmt-compiled))
1068 (setq org-columns-current-fmt-compiled
1069 (nreverse org-columns-current-fmt-compiled))))
1072 ;;; Dynamic block for Column view
1074 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
1075 "Get the column view of the current buffer or subtree.
1076 The first optional argument MAXLEVEL sets the level limit. A
1077 second optional argument SKIP-EMPTY-ROWS tells whether to skip
1078 empty rows, an empty row being one where all the column view
1079 specifiers except ITEM are empty. This function returns a list
1080 containing the title row and all other rows. Each row is a list
1081 of fields."
1082 (save-excursion
1083 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1084 (re-comment (concat "\\*+[ \t]+" org-comment-string "\\>"))
1085 (re-archive (concat ".*:" org-archive-tag ":"))
1086 (n (length title)) row tbl)
1087 (goto-char (point-min))
1088 (while (re-search-forward "^\\(\\*+\\) " nil t)
1089 (catch 'next
1090 (when (and (or (null maxlevel)
1091 (>= maxlevel
1092 (if org-odd-levels-only
1093 (/ (1+ (length (match-string 1))) 2)
1094 (length (match-string 1)))))
1095 (get-char-property (match-beginning 0) 'org-columns-key))
1096 (when (save-excursion
1097 (goto-char (point-at-bol))
1098 (or (looking-at re-comment)
1099 (looking-at re-archive)))
1100 (org-end-of-subtree t)
1101 (throw 'next t))
1102 (setq row nil)
1103 (loop for i from 0 to (1- n) do
1104 (push
1105 (org-quote-vert
1106 (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
1107 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
1108 ""))
1109 row))
1110 (setq row (nreverse row))
1111 (unless (and skip-empty-rows
1112 (eq 1 (length (delete "" (delete-dups (copy-sequence row))))))
1113 (push row tbl)))))
1114 (append (list title 'hline) (nreverse tbl)))))
1116 (defun org-dblock-write:columnview (params)
1117 "Write the column view table.
1118 PARAMS is a property list of parameters:
1120 :width enforce same column widths with <N> specifiers.
1121 :id the :ID: property of the entry where the columns view
1122 should be built. When the symbol `local', call locally.
1123 When `global' call column view with the cursor at the beginning
1124 of the buffer (usually this means that the whole buffer switches
1125 to column view). When \"file:path/to/file.org\", invoke column
1126 view at the start of that file. Otherwise, the ID is located
1127 using `org-id-find'.
1128 :hlines When t, insert a hline before each item. When a number, insert
1129 a hline before each level <= that number.
1130 :vlines When t, make each column a colgroup to enforce vertical lines.
1131 :maxlevel When set to a number, don't capture headlines below this level.
1132 :skip-empty-rows
1133 When t, skip rows where all specifiers other than ITEM are empty."
1134 (let ((pos (move-marker (make-marker) (point)))
1135 (hlines (plist-get params :hlines))
1136 (vlines (plist-get params :vlines))
1137 (maxlevel (plist-get params :maxlevel))
1138 (content-lines (org-split-string (plist-get params :content) "\n"))
1139 (skip-empty-rows (plist-get params :skip-empty-rows))
1140 tbl id idpos nfields tmp recalc line
1141 id-as-string view-file view-pos)
1142 (when (setq id (plist-get params :id))
1143 (setq id-as-string (cond ((numberp id) (number-to-string id))
1144 ((symbolp id) (symbol-name id))
1145 ((stringp id) id)
1146 (t "")))
1147 (cond ((not id) nil)
1148 ((eq id 'global) (setq view-pos (point-min)))
1149 ((eq id 'local))
1150 ((string-match "^file:\\(.*\\)" id-as-string)
1151 (setq view-file (match-string 1 id-as-string)
1152 view-pos 1)
1153 (unless (file-exists-p view-file)
1154 (error "No such file: \"%s\"" id-as-string)))
1155 ((setq idpos (org-find-entry-with-id id))
1156 (setq view-pos idpos))
1157 ((setq idpos (org-id-find id))
1158 (setq view-file (car idpos))
1159 (setq view-pos (cdr idpos)))
1160 (t (error "Cannot find entry with :ID: %s" id))))
1161 (with-current-buffer (if view-file
1162 (get-file-buffer view-file)
1163 (current-buffer))
1164 (save-excursion
1165 (save-restriction
1166 (widen)
1167 (goto-char (or view-pos (point)))
1168 (org-columns)
1169 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1170 (setq nfields (length (car tbl)))
1171 (org-columns-quit))))
1172 (goto-char pos)
1173 (move-marker pos nil)
1174 (when tbl
1175 (when (plist-get params :hlines)
1176 (setq tmp nil)
1177 (while tbl
1178 (if (eq (car tbl) 'hline)
1179 (push (pop tbl) tmp)
1180 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1181 (if (and (not (eq (car tmp) 'hline))
1182 (or (eq hlines t)
1183 (and (numberp hlines)
1184 (<= (- (match-end 1) (match-beginning 1))
1185 hlines))))
1186 (push 'hline tmp)))
1187 (push (pop tbl) tmp)))
1188 (setq tbl (nreverse tmp)))
1189 (when vlines
1190 (setq tbl (mapcar (lambda (x)
1191 (if (eq 'hline x) x (cons "" x)))
1192 tbl))
1193 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1194 (setq pos (point))
1195 (when content-lines
1196 (while (string-match "^#" (car content-lines))
1197 (insert (pop content-lines) "\n")))
1198 (insert (org-listtable-to-string tbl))
1199 (when (plist-get params :width)
1200 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1201 org-columns-current-widths "|")))
1202 (while (setq line (pop content-lines))
1203 (when (string-match "^#" line)
1204 (insert "\n" line)
1205 (when (string-match "^[ \t]*#\\+TBLFM" line)
1206 (setq recalc t))))
1207 (if recalc
1208 (progn (goto-char pos) (org-table-recalculate 'all))
1209 (goto-char pos)
1210 (org-table-align)))))
1212 (defun org-listtable-to-string (tbl)
1213 "Convert a listtable TBL to a string that contains the Org-mode table.
1214 The table still need to be aligned. The resulting string has no leading
1215 and tailing newline characters."
1216 (mapconcat
1217 (lambda (x)
1218 (cond
1219 ((listp x)
1220 (concat "|" (mapconcat 'identity x "|") "|"))
1221 ((eq x 'hline) "|-|")
1222 (t (error "Garbage in listtable: %s" x))))
1223 tbl "\n"))
1225 (defun org-insert-columns-dblock ()
1226 "Create a dynamic block capturing a column view table."
1227 (interactive)
1228 (let ((defaults '(:name "columnview" :hlines 1))
1229 (id (org-ido-completing-read
1230 "Capture columns (local, global, entry with :ID: property) [local]: "
1231 (append '(("global") ("local"))
1232 (mapcar 'list (org-property-values "ID"))))))
1233 (if (equal id "") (setq id 'local))
1234 (if (equal id "global") (setq id 'global))
1235 (setq defaults (append defaults (list :id id)))
1236 (org-create-dblock defaults)
1237 (org-update-dblock)))
1239 ;;; Column view in the agenda
1241 (defvar org-agenda-view-columns-initially nil
1242 "When set, switch to columns view immediately after creating the agenda.")
1244 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1245 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1246 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1248 (defun org-agenda-columns ()
1249 "Turn on or update column view in the agenda."
1250 (interactive)
1251 (org-verify-version 'columns)
1252 (org-columns-remove-overlays)
1253 (move-marker org-columns-begin-marker (point))
1254 (let (fmt cache maxwidths m p a d)
1255 (cond
1256 ((and (boundp 'org-agenda-overriding-columns-format)
1257 org-agenda-overriding-columns-format)
1258 (setq fmt org-agenda-overriding-columns-format)
1259 (org-set-local 'org-agenda-overriding-columns-format fmt))
1260 ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
1261 (setq fmt (or (org-entry-get m "COLUMNS" t)
1262 (with-current-buffer (marker-buffer m)
1263 org-columns-default-format))))
1264 ((and (boundp 'org-columns-current-fmt)
1265 (local-variable-p 'org-columns-current-fmt)
1266 org-columns-current-fmt)
1267 (setq fmt org-columns-current-fmt))
1268 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
1269 (setq m (get-text-property m 'org-hd-marker))
1270 (setq fmt (or (org-entry-get m "COLUMNS" t)
1271 (with-current-buffer (marker-buffer m)
1272 org-columns-default-format)))))
1273 (setq fmt (or fmt org-columns-default-format))
1274 (org-set-local 'org-columns-current-fmt fmt)
1275 (org-columns-compile-format fmt)
1276 (when org-agenda-columns-compute-summary-properties
1277 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1278 (save-excursion
1279 ;; Get and cache the properties
1280 (goto-char (point-min))
1281 (while (not (eobp))
1282 (when (setq m (or (get-text-property (point) 'org-hd-marker)
1283 (get-text-property (point) 'org-marker)))
1284 (setq p (org-entry-properties m))
1286 (when (or (not (setq a (assoc org-effort-property p)))
1287 (not (string-match "\\S-" (or (cdr a) ""))))
1288 ;; OK, the property is not defined. Use appointment duration?
1289 (when (and org-agenda-columns-add-appointments-to-effort-sum
1290 (setq d (get-text-property (point) 'duration)))
1291 (setq d (org-minutes-to-hh:mm-string d))
1292 (put-text-property 0 (length d) 'face 'org-warning d)
1293 (push (cons org-effort-property d) p)))
1294 (push (cons (org-current-line) p) cache))
1295 (beginning-of-line 2))
1296 (when cache
1297 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
1298 (org-set-local 'org-columns-current-maxwidths maxwidths)
1299 (org-columns-display-here-title)
1300 (when (org-set-local 'org-columns-flyspell-was-active
1301 (org-bound-and-true-p flyspell-mode))
1302 (flyspell-mode 0))
1303 (mapc (lambda (x)
1304 (goto-line (car x))
1305 (org-columns-display-here (cdr x)))
1306 cache)
1307 (when org-agenda-columns-show-summaries
1308 (org-agenda-colview-summarize cache))))))
1310 (defun org-agenda-colview-summarize (cache)
1311 "Summarize the summarizable columns in column view in the agenda.
1312 This will add overlays to the date lines, to show the summary for each day."
1313 (let* ((fmt (mapcar (lambda (x)
1314 (list (car x) (if (equal (car x) "CLOCKSUM")
1315 'add_times (nth 4 x))))
1316 org-columns-current-fmt-compiled))
1317 line c c1 stype props lsum entries prop v)
1318 (catch 'exit
1319 (when (delq nil (mapcar 'cadr fmt))
1320 ;; OK, at least one summation column, it makes sense to try this
1321 (goto-char (point-max))
1322 (while t
1323 (when (or (get-text-property (point) 'org-date-line)
1324 (eq (get-text-property (point) 'face)
1325 'org-agenda-structure))
1326 ;; OK, this is a date line that should be used
1327 (setq line (org-current-line))
1328 (setq entries nil c cache cache nil)
1329 (while (setq c1 (pop c))
1330 (if (> (car c1) line)
1331 (push c1 entries)
1332 (push c1 cache)))
1333 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1334 ;; Compute the summaries for the properties we want,
1335 ;; set nil properties for the rest.
1336 (when (setq entries (mapcar 'cdr entries))
1337 (setq props
1338 (mapcar
1339 (lambda (f)
1340 (setq prop (car f) stype (nth 1 f))
1341 (cond
1342 ((equal prop "ITEM")
1343 (cons prop (buffer-substring (point-at-bol)
1344 (point-at-eol))))
1345 ((not stype) (cons prop ""))
1347 ;; do the summary
1348 (setq lsum 0)
1349 (mapc (lambda (x)
1350 (setq v (cdr (assoc prop x)))
1351 (if v (setq lsum (+ lsum
1352 (org-column-string-to-number
1353 v stype)))))
1354 entries)
1355 (setq lsum (org-columns-number-to-string lsum stype))
1356 (put-text-property
1357 0 (length lsum) 'face 'bold lsum)
1358 (cons prop lsum))))
1359 fmt))
1360 (org-columns-display-here props 'dateline)
1361 (org-set-local 'org-agenda-columns-active t)))
1362 (if (bobp) (throw 'exit t))
1363 (beginning-of-line 0))))))
1365 (defun org-agenda-colview-compute (fmt)
1366 "Compute the relevant columns in the contributing source buffers."
1367 (let ((files org-agenda-contributing-files)
1368 (org-columns-begin-marker (make-marker))
1369 (org-columns-top-level-marker (make-marker))
1370 f fm a b)
1371 (while (setq f (pop files))
1372 (setq b (find-buffer-visiting f))
1373 (with-current-buffer (or (buffer-base-buffer b) b)
1374 (save-excursion
1375 (save-restriction
1376 (widen)
1377 (org-unmodified
1378 (remove-text-properties (point-min) (point-max)
1379 '(org-summaries t)))
1380 (goto-char (point-min))
1381 (org-columns-get-format-and-top-level)
1382 (while (setq fm (pop fmt))
1383 (if (equal (car fm) "CLOCKSUM")
1384 (org-clock-sum)
1385 (when (and (nth 4 fm)
1386 (setq a (assoc (car fm)
1387 org-columns-current-fmt-compiled))
1388 (equal (nth 4 a) (nth 4 fm)))
1389 (org-columns-compute (car fm)))))))))))
1391 (provide 'org-colview)
1393 ;; arch-tag: 61f5128d-747c-4983-9479-e3871fa3d73c
1395 ;;; org-colview.el ends here