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