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