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