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