1 ;;; org-colview.el --- Column View in Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; This file contains the face definitons for Org.
32 (eval-when-compile (require 'cl
))
35 (declare-function org-agenda-redo
"org-agenda" ())
39 (defvar org-columns-overlays nil
40 "Holds the list of current column overlays.")
42 (defvar org-columns-current-fmt nil
43 "Local variable, holds the currently active column format.")
44 (make-variable-buffer-local 'org-columns-current-fmt
)
45 (defvar org-columns-current-fmt-compiled nil
46 "Local variable, holds the currently active column format.
47 This is the compiled version of the format.")
48 (make-variable-buffer-local 'org-columns-current-fmt-compiled
)
49 (defvar org-columns-current-widths nil
50 "Loval variable, holds the currently widths of fields.")
51 (make-variable-buffer-local 'org-columns-current-widths
)
52 (defvar org-columns-current-maxwidths nil
53 "Loval variable, holds the currently active maximum column widths.")
54 (make-variable-buffer-local 'org-columns-current-maxwidths
)
55 (defvar org-columns-begin-marker
(make-marker)
56 "Points to the position where last a column creation command was called.")
57 (defvar org-columns-top-level-marker
(make-marker)
58 "Points to the position where current columns region starts.")
60 (defvar org-columns-map
(make-sparse-keymap)
61 "The keymap valid in column display.")
63 (defun org-columns-content ()
64 "Switch to contents view while in columns view."
69 (org-defkey org-columns-map
"c" 'org-columns-content
)
70 (org-defkey org-columns-map
"o" 'org-overview
)
71 (org-defkey org-columns-map
"e" 'org-columns-edit-value
)
72 (org-defkey org-columns-map
"\C-c\C-t" 'org-columns-todo
)
73 (org-defkey org-columns-map
"\C-c\C-c" 'org-columns-set-tags-or-toggle
)
74 (org-defkey org-columns-map
"\C-c\C-o" 'org-columns-open-link
)
75 (org-defkey org-columns-map
"v" 'org-columns-show-value
)
76 (org-defkey org-columns-map
"q" 'org-columns-quit
)
77 (org-defkey org-columns-map
"r" 'org-columns-redo
)
78 (org-defkey org-columns-map
"g" 'org-columns-redo
)
79 (org-defkey org-columns-map
[left] 'backward-char)
80 (org-defkey org-columns-map "\M-b" 'backward-char)
81 (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
82 (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
83 (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
84 (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
85 (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
86 (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
87 (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
88 (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
89 (org-defkey org-columns-map "<" 'org-columns-narrow)
90 (org-defkey org-columns-map ">" 'org-columns-widen)
91 (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
92 (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
93 (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
94 (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
96 (org-defkey org-columns-map (number-to-string i)
97 `(lambda () (interactive)
98 (org-columns-next-allowed-value nil ,i))))
100 (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
102 ["Edit property" org-columns-edit-value t]
103 ["Next allowed value" org-columns-next-allowed-value t]
104 ["Previous allowed value" org-columns-previous-allowed-value t]
105 ["Show full value" org-columns-show-value t]
106 ["Edit allowed values" org-columns-edit-allowed t]
108 ["Edit column attributes" org-columns-edit-attributes t]
109 ["Increase column width" org-columns-widen t]
110 ["Decrease column width" org-columns-narrow t]
112 ["Move column right" org-columns-move-right t]
113 ["Move column left" org-columns-move-left t]
114 ["Add column" org-columns-new t]
115 ["Delete column" org-columns-delete t]
117 ["CONTENTS" org-columns-content t]
118 ["OVERVIEW" org-overview t]
119 ["Refresh columns display" org-columns-redo t]
121 ["Open link" org-columns-open-link t]
123 ["Quit" org-columns-quit t]))
125 (defun org-columns-new-overlay (beg end &optional string face)
126 "Create a new column overlay and add it to the list."
127 (let ((ov (org-make-overlay beg end)))
128 (org-overlay-put ov 'face (or face 'secondary-selection))
129 (org-overlay-display ov string face)
130 (push ov org-columns-overlays)
133 (defun org-columns-display-here (&optional props)
134 "Overlay the current line with column display."
136 (let* ((fmt org-columns-current-fmt-compiled)
138 (level-face (save-excursion
139 (beginning-of-line 1)
140 (and (looking-at "\\(\\**\\)\\(\\* \\)")
141 (org-get-level-face 2))))
142 (ref-face (or level-face
143 (and (eq major-mode 'org-agenda-mode)
144 (get-text-property (point-at-bol) 'face))
146 (color (list :foreground (face-attribute ref-face :foreground)))
147 (face (list color 'org-column ref-face))
148 (pl (or (get-text-property (point-at-bol) 'prefix-length) 0))
149 (cphr (get-text-property (point-at-bol) 'org-complex-heading-regexp))
150 pom property ass width f string ov column val modval s1 s2)
151 ;; Check if the entry is in another buffer.
153 (if (eq major-mode 'org-agenda-mode)
154 (setq pom (or (get-text-property (point) 'org-hd-marker)
155 (get-text-property (point) 'org-marker))
156 props (if pom (org-entry-properties pom) nil))
157 (setq props (org-entry-properties nil))))
159 (while (setq column (pop fmt))
160 (setq property (car column)
161 ass (if (equal property "ITEM")
166 (buffer-substring-no-properties
167 (point-at-bol) (point-at-eol))))))
168 (assoc property props))
169 width (or (cdr (assoc property org-columns-current-maxwidths))
172 f (format "%%-%d.%ds | " width width)
173 val (or (cdr ass) "")
174 modval (if (equal property "ITEM")
176 (org-columns-cleanup-item
177 val org-columns-current-fmt-compiled)
178 (org-agenda-columns-cleanup-item
179 val pl cphr org-columns-current-fmt-compiled))))
180 (setq s2 (org-columns-add-ellipses (or modval val) width))
181 (setq string (format f s2))
182 ;; Create the overlay
184 (setq ov (org-columns-new-overlay
185 beg (setq beg (1+ beg)) string face))
186 (org-overlay-put ov 'keymap org-columns-map)
187 (org-overlay-put ov 'org-columns-key property)
188 (org-overlay-put ov 'org-columns-value (cdr ass))
189 (org-overlay-put ov 'org-columns-value-modified modval)
190 (org-overlay-put ov 'org-columns-pom pom)
191 (org-overlay-put ov 'org-columns-format f))
192 (if (or (not (char-after beg))
193 (equal (char-after beg) ?\n))
194 (let ((inhibit-read-only t))
197 (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
198 ;; Make the rest of the line disappear.
200 (setq ov (org-columns-new-overlay beg (point-at-eol)))
201 (org-overlay-put ov 'invisible t)
202 (org-overlay-put ov 'keymap org-columns-map)
203 (org-overlay-put ov 'intangible t)
204 (push ov org-columns-overlays)
205 (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
206 (org-overlay-put ov 'keymap org-columns-map)
207 (push ov org-columns-overlays)
208 (let ((inhibit-read-only t))
209 (put-text-property (max (point-min) (1- (point-at-bol)))
210 (min (point-max) (1+ (point-at-eol)))
211 'read-only "Type `e' to edit property")))))
213 (defun org-columns-add-ellipses (string width)
214 "Truncate STRING with WIDTH characters, with ellipses."
216 ((<= (length string) width) string)
217 ((<= width (length org-columns-ellipses))
218 (substring org-columns-ellipses 0 width))
219 (t (concat (substring string 0 (- width (length org-columns-ellipses)))
220 org-columns-ellipses))))
222 (defvar org-columns-full-header-line-format nil
223 "Fthe full header line format, will be shifted by horizontal scrolling." )
224 (defvar org-previous-header-line-format nil
225 "The header line format before column view was turned on.")
226 (defvar org-columns-inhibit-recalculation nil
227 "Inhibit recomputing of columns on column view startup.")
228 (defvar org-columns-flyspell-was-active nil
229 "Remember the state of `flyspell-mode' before column view.
230 Flyspell-mode can cause problems in columns view, so it is turned off
231 for the duration of the command.")
233 (defvar header-line-format)
234 (defvar org-columns-previous-hscroll 0)
235 (defun org-columns-display-here-title ()
236 "Overlay the newline before the current line with the table title."
238 (let ((fmt org-columns-current-fmt-compiled)
240 property width f column str widths)
241 (while (setq column (pop fmt))
242 (setq property (car column)
243 str (or (nth 1 column) property)
244 width (or (cdr (assoc property org-columns-current-maxwidths))
247 widths (push width widths)
248 f (format "%%-%d.%ds | " width width)
249 string (format f str)
250 title (concat title string)))
252 (org-add-props " " nil 'display '(space :align-to 0))
253 ;;(org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
254 (org-add-props title nil 'face 'org-column-title)))
255 (org-set-local 'org-previous-header-line-format header-line-format)
256 (org-set-local 'org-columns-current-widths (nreverse widths))
257 (setq org-columns-full-header-line-format title)
258 (setq org-columns-previous-hscroll -1)
259 ; (org-columns-hscoll-title)
260 (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
262 (defun org-columns-hscoll-title ()
263 "Set the header-line-format so that it scrolls along with the table."
264 (sit-for .0001) ; need to force a redisplay to update window-hscroll
265 (when (not (= (window-hscroll) org-columns-previous-hscroll))
266 (setq header-line-format
267 (concat (substring org-columns-full-header-line-format 0 1)
268 (substring org-columns-full-header-line-format
269 (1+ (window-hscroll))))
270 org-columns-previous-hscroll (window-hscroll))
271 (force-mode-line-update)))
273 (defun org-columns-remove-overlays ()
274 "Remove all currently active column overlays."
276 (when (marker-buffer org-columns-begin-marker)
277 (with-current-buffer (marker-buffer org-columns-begin-marker)
278 (when (local-variable-p 'org-previous-header-line-format)
279 (setq header-line-format org-previous-header-line-format)
280 (kill-local-variable 'org-previous-header-line-format)
281 (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
282 (move-marker org-columns-begin-marker nil)
283 (move-marker org-columns-top-level-marker nil)
285 (mapc 'org-delete-overlay org-columns-overlays)
286 (setq org-columns-overlays nil)
287 (let ((inhibit-read-only t))
288 (remove-text-properties (point-min) (point-max) '(read-only t))))
289 (when org-columns-flyspell-was-active
290 (flyspell-mode 1)))))
292 (defun org-columns-cleanup-item (item fmt)
293 "Remove from ITEM what is a column in the format FMT."
294 (if (not org-complex-heading-regexp)
296 (when (string-match org-complex-heading-regexp item)
299 (org-add-props (match-string 1 item) nil
300 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
301 (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
302 (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
303 " " (save-match-data (org-columns-compact-links (match-string 4 item)))
304 (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))
307 (list 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
311 (defun org-columns-compact-links (s)
312 "Replace [[link][desc]] with [desc] or [link]."
313 (while (string-match org-bracket-link-regexp s)
314 (setq s (replace-match
315 (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
319 (defvar org-agenda-columns-remove-prefix-from-item)
320 (defun org-agenda-columns-cleanup-item (item pl cphr fmt)
321 "Cleanup the tiem property for agenda column view.
322 See also the variable `org-agenda-columns-remove-prefix-from-item'."
323 (let* ((org-complex-heading-regexp cphr)
324 (prefix (substring item 0 pl))
325 (rest (substring item pl))
326 (fake (concat "* " rest))
327 (cleaned (org-trim (substring (org-columns-cleanup-item fake fmt) 1))))
328 (if org-agenda-columns-remove-prefix-from-item
330 (concat prefix cleaned))))
332 (defun org-columns-show-value ()
333 "Show the full value of the property."
335 (let ((value (get-char-property (point) 'org-columns-value)))
336 (message "Value is: %s" (or value ""))))
338 (defvar org-agenda-columns-active) ;; defined in org-agenda.el
339 (defun org-columns-quit ()
340 "Remove the column overlays and in this way exit column editing."
343 (org-columns-remove-overlays)
344 (let ((inhibit-read-only t))
345 (remove-text-properties (point-min) (point-max) '(read-only t))))
346 (when (eq major-mode 'org-agenda-mode)
347 (setq org-agenda-columns-active nil)
349 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
351 (defun org-columns-check-computed ()
352 "Check if this column value is computed.
353 If yes, throw an error indicating that changing it does not make sense."
354 (let ((val (get-char-property (point) 'org-columns-value)))
355 (when (and (stringp val)
356 (get-char-property 0 'org-computed val))
357 (error "This value is computed from the entry's children"))))
359 (defun org-columns-todo (&optional arg)
360 "Change the TODO state during column view."
362 (org-columns-edit-value "TODO"))
364 (defun org-columns-set-tags-or-toggle (&optional arg)
365 "Toggle checkbox at point, or set tags for current headline."
367 (if (string-match "\\`\\[[ xX-]\\]\\'"
368 (get-char-property (point) 'org-columns-value))
369 (org-columns-next-allowed-value)
370 (org-columns-edit-value "TAGS")))
372 (defun org-columns-edit-value (&optional key)
373 "Edit the value of the property at point in column view.
374 Where possible, use the standard interface for changing this line."
376 (org-columns-check-computed)
377 (let* ((external-key key)
378 (col (current-column))
379 (key (or key (get-char-property (point) 'org-columns-key)))
380 (value (get-char-property (point) 'org-columns-value))
381 (bol (point-at-bol)) (eol (point-at-eol))
382 (pom (or (get-text-property bol 'org-hd-marker)
383 (point))) ; keep despite of compiler waring
385 (delq nil (mapcar (lambda (x)
386 (and (eq (overlay-buffer x) (current-buffer))
387 (>= (overlay-start x) bol)
388 (<= (overlay-start x) eol)
390 org-columns-overlays)))
393 ((equal key "CLOCKSUM")
394 (error "This special column cannot be edited"))
396 (setq eval '(org-with-point-at pom
397 (org-edit-headline))))
399 (setq eval '(org-with-point-at pom
400 (let ((current-prefix-arg
401 (if external-key current-prefix-arg '(4))))
402 (call-interactively 'org-todo)))))
403 ((equal key "PRIORITY")
404 (setq eval '(org-with-point-at pom
405 (call-interactively 'org-priority))))
407 (setq eval '(org-with-point-at pom
408 (let ((org-fast-tag-selection-single-key
409 (if (eq org-fast-tag-selection-single-key 'expert)
410 t org-fast-tag-selection-single-key)))
411 (call-interactively 'org-set-tags)))))
412 ((equal key "DEADLINE")
413 (setq eval '(org-with-point-at pom
414 (call-interactively 'org-deadline))))
415 ((equal key "SCHEDULED")
416 (setq eval '(org-with-point-at pom
417 (call-interactively 'org-schedule))))
419 (setq allowed (org-property-get-allowed-values pom key 'table))
421 (setq nval (completing-read "Value: " allowed nil t))
422 (setq nval (read-string "Edit: " value)))
423 (setq nval (org-trim nval))
424 (when (not (equal nval value))
425 (setq eval '(org-entry-put pom key nval)))))
429 ((equal major-mode 'org-agenda-mode)
430 (org-columns-eval eval)
431 ;; The following let preserves the current format, and makes sure
432 ;; that in only a single file things need to be upated.
433 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
434 (buffer (marker-buffer pom))
435 (org-agenda-contributing-files
436 (list (with-current-buffer buffer
437 (buffer-file-name (buffer-base-buffer))))))
438 (org-agenda-columns)))
440 (let ((inhibit-read-only t))
442 (remove-text-properties
443 (max (point-min) (1- bol)) eol '(read-only t)))
446 (setq org-columns-overlays
447 (org-delete-all line-overlays org-columns-overlays))
448 (mapc 'org-delete-overlay line-overlays)
449 (org-columns-eval eval))
450 (org-columns-display-here)))
451 (org-move-to-column col)
452 (if (and (org-mode-p)
453 (nth 3 (assoc key org-columns-current-fmt-compiled)))
454 (org-columns-update key)))))))
456 (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
457 "Edit the current headline, the part without TODO keyword, TAGS."
458 (org-back-to-heading)
459 (when (looking-at org-todo-line-regexp)
461 (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
462 (txt (match-string 3))
465 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
466 (setq post (match-string 0 txt)
467 txt (substring txt 0 (match-beginning 0))))
468 (setq txt2 (read-string "Edit: " txt))
469 (when (not (equal txt txt2))
471 (insert pre txt2 post)
472 (delete-region (point) (point-at-eol))
473 (org-set-tags nil t)))))
475 (defun org-columns-edit-allowed ()
476 "Edit the list of allowed values for the current property."
478 (let* ((pom (or (get-text-property (point-at-bol) 'org-marker)
479 (get-text-property (point-at-bol) 'org-hd-marker)
481 (key (get-char-property (point) 'org-columns-key))
482 (key1 (concat key "_ALL"))
483 (allowed (org-entry-get pom key1 t))
485 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
486 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
487 (setq nval (read-string "Allowed: " allowed))
489 (cond ((marker-position org-entry-property-inherited-from)
490 org-entry-property-inherited-from)
491 ((marker-position org-columns-top-level-marker)
492 org-columns-top-level-marker)
496 (defun org-columns-eval (form)
499 (beginning-of-line 1)
500 ;; `next-line' is needed here, because it skips invisible line.
501 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
502 (setq hidep (org-on-heading-p 1)))
504 (and hidep (hide-entry))))
506 (defun org-columns-previous-allowed-value ()
507 "Switch to the previous allowed value for this column."
509 (org-columns-next-allowed-value t))
511 (defun org-columns-next-allowed-value (&optional previous nth)
512 "Switch to the next allowed value for this column.
513 When PREVIOUS is set, go to the previous value. When NTH is
514 an integer, select that value."
516 (org-columns-check-computed)
517 (let* ((col (current-column))
518 (key (get-char-property (point) 'org-columns-key))
519 (value (get-char-property (point) 'org-columns-value))
520 (bol (point-at-bol)) (eol (point-at-eol))
521 (pom (or (get-text-property bol 'org-hd-marker)
522 (point))) ; keep despite of compiler waring
524 (delq nil (mapcar (lambda (x)
525 (and (eq (overlay-buffer x) (current-buffer))
526 (>= (overlay-start x) bol)
527 (<= (overlay-start x) eol)
529 org-columns-overlays)))
530 (allowed (or (org-property-get-allowed-values pom key)
532 (nth 4 (assoc key org-columns-current-fmt-compiled))
533 '(checkbox checkbox-n-of-m checkbox-percent))
538 (if (= nth -1) (setq nth 9)))
539 (when (equal key "ITEM")
540 (error "Cannot edit item headline from here"))
541 (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
542 (error "Allowed values for this property have not been defined"))
543 (if (member key '("SCHEDULED" "DEADLINE"))
544 (setq nval (if previous 'earlier 'later))
545 (if previous (setq allowed (reverse allowed)))
548 (setq nval (nth nth allowed))
550 (error "There are only %d allowed values for property `%s'"
551 (length allowed) key)))
552 ((member value allowed)
553 (setq nval (or (car (cdr (member value allowed)))
555 (if (equal nval value)
556 (error "Only one allowed value for this property")))
557 (t (setq nval (car allowed)))))
559 ((equal major-mode 'org-agenda-mode)
560 (org-columns-eval '(org-entry-put pom key nval))
561 ;; The following let preserves the current format, and makes sure
562 ;; that in only a single file things need to be upated.
563 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
564 (buffer (marker-buffer pom))
565 (org-agenda-contributing-files
566 (list (with-current-buffer buffer
567 (buffer-file-name (buffer-base-buffer))))))
568 (org-agenda-columns)))
570 (let ((inhibit-read-only t))
571 (remove-text-properties (1- bol) eol '(read-only t))
574 (setq org-columns-overlays
575 (org-delete-all line-overlays org-columns-overlays))
576 (mapc 'org-delete-overlay line-overlays)
577 (org-columns-eval '(org-entry-put pom key nval)))
578 (org-columns-display-here)))
579 (org-move-to-column col)
580 (and (nth 3 (assoc key org-columns-current-fmt-compiled))
581 (org-columns-update key))))))
583 (defun org-verify-version (task)
586 (if (or (featurep 'xemacs)
587 (< emacs-major-version 22))
588 (error "Emacs 22 is required for the columns feature")))))
590 (defun org-columns-open-link (&optional arg)
592 (let ((value (get-char-property (point) 'org-columns-value)))
593 (org-open-link-from-string value arg)))
595 (defun org-columns-get-format-and-top-level ()
597 (when (condition-case nil (org-back-to-heading) (error nil))
598 (move-marker org-entry-property-inherited-from nil)
599 (setq fmt (org-entry-get nil "COLUMNS" t)))
600 (setq fmt (or fmt org-columns-default-format))
601 (org-set-local 'org-columns-current-fmt fmt)
602 (org-columns-compile-format fmt)
603 (if (marker-position org-entry-property-inherited-from)
604 (move-marker org-columns-top-level-marker
605 org-entry-property-inherited-from)
606 (move-marker org-columns-top-level-marker (point)))
609 (defun org-columns ()
610 "Turn on column view on an org-mode file."
612 (org-verify-version 'columns)
613 (org-columns-remove-overlays)
614 (move-marker org-columns-begin-marker (point))
615 (let (beg end fmt cache maxwidths)
616 (setq fmt (org-columns-get-format-and-top-level))
618 (goto-char org-columns-top-level-marker)
620 (unless org-columns-inhibit-recalculation
621 (org-columns-compute-all))
622 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
624 ;; Get and cache the properties
626 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
629 (narrow-to-region beg end)
631 (while (re-search-forward (concat "^" outline-regexp) end t)
632 (push (cons (org-current-line) (org-entry-properties)) cache))
634 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
635 (org-set-local 'org-columns-current-maxwidths maxwidths)
636 (org-columns-display-here-title)
637 (when (org-set-local 'org-columns-flyspell-was-active
638 (org-bound-and-true-p flyspell-mode))
642 (org-columns-display-here (cdr x)))
645 (defun org-columns-new (&optional prop title width op fmt &rest rest)
646 "Insert a new column, to the left of the current column."
648 (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
650 (setq prop (completing-read
651 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
653 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
654 (setq width (read-string "Column width: " (if width (number-to-string width))))
655 (if (string-match "\\S-" width)
656 (setq width (string-to-number width))
658 (setq fmt (completing-read "Summary [none]: "
659 '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
661 (if (string-match "\\S-" fmt)
662 (setq fmt (intern fmt))
664 (if (eq fmt 'none) (setq fmt nil))
668 (setcdr editp (list title width nil fmt)))
669 (setq cell (nthcdr (1- (current-column))
670 org-columns-current-fmt-compiled))
671 (setcdr cell (cons (list prop title width nil fmt)
673 (org-columns-store-format)
676 (defun org-columns-delete ()
677 "Delete the column at point from columns view."
679 (let* ((n (current-column))
680 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
682 (format "Are you sure you want to remove column \"%s\"? " title))
683 (setq org-columns-current-fmt-compiled
684 (delq (nth n org-columns-current-fmt-compiled)
685 org-columns-current-fmt-compiled))
686 (org-columns-store-format)
688 (if (>= (current-column) (length org-columns-current-fmt-compiled))
689 (backward-char 1)))))
691 (defun org-columns-edit-attributes ()
692 "Edit the attributes of the current column."
694 (let* ((n (current-column))
695 (info (nth n org-columns-current-fmt-compiled)))
696 (apply 'org-columns-new info)))
698 (defun org-columns-widen (arg)
699 "Make the column wider by ARG characters."
701 (let* ((n (current-column))
702 (entry (nth n org-columns-current-fmt-compiled))
703 (width (or (nth 2 entry)
704 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
705 (setq width (max 1 (+ width arg)))
706 (setcar (nthcdr 2 entry) width)
707 (org-columns-store-format)
710 (defun org-columns-narrow (arg)
711 "Make the column nrrower by ARG characters."
713 (org-columns-widen (- arg)))
715 (defun org-columns-move-right ()
716 "Swap this column with the one to the right."
718 (let* ((n (current-column))
719 (cell (nthcdr n org-columns-current-fmt-compiled))
721 (when (>= n (1- (length org-columns-current-fmt-compiled)))
722 (error "Cannot shift this column further to the right"))
724 (setcar cell (car (cdr cell)))
725 (setcdr cell (cons e (cdr (cdr cell))))
726 (org-columns-store-format)
730 (defun org-columns-move-left ()
731 "Swap this column with the one to the left."
733 (let* ((n (current-column)))
735 (error "Cannot shift this column further to the left"))
737 (org-columns-move-right)
740 (defun org-columns-store-format ()
741 "Store the text version of the current columns format in appropriate place.
742 This is either in the COLUMNS property of the node starting the current column
743 display, or in the #+COLUMNS line of the current buffer."
745 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
746 (org-set-local 'org-columns-current-fmt fmt)
747 (if (marker-position org-columns-top-level-marker)
749 (goto-char org-columns-top-level-marker)
750 (if (and (org-at-heading-p)
751 (org-entry-get nil "COLUMNS"))
752 (org-entry-put nil "COLUMNS" fmt)
753 (goto-char (point-min))
754 ;; Overwrite all #+COLUMNS lines....
755 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
757 (replace-match (concat "#+COLUMNS: " fmt) t t))
759 (goto-char (point-min))
760 (or (org-on-heading-p t) (outline-next-heading))
761 (let ((inhibit-read-only t))
762 (insert-before-markers "#+COLUMNS: " fmt "\n")))
763 (org-set-local 'org-columns-default-format fmt))))))
765 (defvar org-agenda-overriding-columns-format nil
766 "When set, overrides any other format definition for the agenda.
767 Don't set this, this is meant for dynamic scoping.")
769 (defun org-columns-get-autowidth-alist (s cache)
770 "Derive the maximum column widths from the format and the cache."
772 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
773 (push (cons (match-string 1 s) 1) rtn)
774 (setq start (match-end 0)))
776 (setcdr x (apply 'max
779 (length (or (cdr (assoc (car x) (cdr y))) " ")))
784 (defun org-columns-compute-all ()
785 "Compute all columns that have operators defined."
787 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
788 (let ((columns org-columns-current-fmt-compiled) col)
789 (while (setq col (pop columns))
792 (org-columns-compute (car col)))))))
794 (defun org-columns-update (property)
795 "Recompute PROPERTY, and update the columns display for it."
796 (org-columns-compute property)
800 (when (equal (org-overlay-get ov 'org-columns-key) property)
801 (setq pos (org-overlay-start ov))
803 (when (setq val (cdr (assoc property
805 (point-at-bol) 'org-summaries))))
806 (setq fmt (org-overlay-get ov 'org-columns-format))
807 (org-overlay-put ov 'org-columns-value val)
808 (org-overlay-put ov 'display (format fmt val)))))
809 org-columns-overlays))))
811 (defun org-columns-compute (property)
812 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
814 (let* ((re (concat "^" outline-regexp))
815 (lmax 30) ; Does anyone use deeper levels???
816 (lsum (make-vector lmax 0))
817 (lflag (make-vector lmax nil))
819 (ass (assoc property org-columns-current-fmt-compiled))
822 (beg org-columns-top-level-marker)
823 last-level val valflag flag end sumpos sum-alist sum str str1 useval)
825 ;; Find the region to compute
827 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
829 ;; Walk the tree from the back and do the computations
830 (while (re-search-backward re beg t)
831 (setq sumpos (match-beginning 0)
833 level (org-outline-level)
834 val (org-entry-get nil property)
835 valflag (and val (string-match "\\S-" val)))
837 ((< level last-level)
838 ;; put the sum of lower levels here as a property
839 (setq sum (aref lsum last-level) ; current sum
840 flag (aref lflag last-level) ; any valid entries from children?
841 str (org-columns-number-to-string sum format printf)
842 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
843 useval (if flag str1 (if valflag val ""))
844 sum-alist (get-text-property sumpos 'org-summaries))
845 (if (assoc property sum-alist)
846 (setcdr (assoc property sum-alist) useval)
847 (push (cons property useval) sum-alist)
849 (add-text-properties sumpos (1+ sumpos)
850 (list 'org-summaries sum-alist))))
851 (when (and val (not (equal val (if flag str val))))
852 (org-entry-put nil property (if flag str val)))
853 ;; add current to current level accumulator
854 (when (or flag valflag)
855 (aset lsum level (+ (aref lsum level)
856 (if flag sum (org-column-string-to-number
857 (if flag str val) format))))
858 (aset lflag level t))
859 ;; clear accumulators for deeper levels
860 (loop for l from (1+ level) to (1- lmax) do
863 ((>= level last-level)
864 ;; add what we have here to the accumulator for this level
865 (aset lsum level (+ (aref lsum level)
866 (org-column-string-to-number (or val "0") format)))
867 (and valflag (aset lflag level t)))
868 (t (error "This should not happen")))))))
870 (defun org-columns-redo ()
871 "Construct the column display again."
873 (message "Recomputing columns...")
874 (let ((line (org-current-line))
875 (col (current-column)))
877 (if (marker-position org-columns-begin-marker)
878 (goto-char org-columns-begin-marker))
879 (org-columns-remove-overlays)
881 (call-interactively 'org-columns)
883 (call-interactively 'org-agenda-columns)))
885 (move-to-column col))
886 (message "Recomputing columns...done"))
888 (defun org-columns-not-in-agenda ()
889 (if (eq major-mode 'org-agenda-mode)
890 (error "This command is only allowed in Org-mode buffers")))
893 (defun org-string-to-number (s)
894 "Convert string to number, and interpret hh:mm:ss."
895 (if (not (string-match ":" s))
897 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
899 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
902 (defun org-columns-number-to-string (n fmt &optional printf)
903 "Convert a computed column number to a string value, according to FMT."
906 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
907 (format org-time-clocksum-format h m)))
909 (cond ((= n (floor n)) "[X]")
912 ((memq fmt '(checkbox-n-of-m checkbox-percent))
913 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
914 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
915 (printf (format printf n))
918 (t (number-to-string n))))
920 (defun org-nofm-to-completion (n m &optional percent)
922 (format "[%d/%d]" n m)
923 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
925 (defun org-column-string-to-number (s fmt)
926 "Convert a column value to a number that can be used for column computing."
928 ((string-match ":" s)
929 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
931 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
933 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
934 (if (equal s "[X]") 1. 0.000001))
935 (t (string-to-number s))))
937 (defun org-columns-uncompile-format (cfmt)
938 "Turn the compiled columns format back into a string representation."
939 (let ((rtn "") e s prop title op width fmt printf)
940 (while (setq e (pop cfmt))
948 ((eq fmt 'add_times) (setq op ":"))
949 ((eq fmt 'checkbox) (setq op "X"))
950 ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
951 ((eq fmt 'checkbox-percent) (setq op "X%"))
952 ((eq fmt 'add_numbers) (setq op "+"))
953 ((eq fmt 'currency) (setq op "$")))
954 (if (and op printf) (setq op (concat op ";" printf)))
955 (if (equal title prop) (setq title nil))
956 (setq s (concat "%" (if width (number-to-string width))
958 (if title (concat "(" title ")"))
959 (if op (concat "{" op "}"))))
960 (setq rtn (concat rtn " " s)))
963 (defun org-columns-compile-format (fmt)
964 "Turn a column format string into an alist of specifications.
965 The alist has one entry for each column in the format. The elements of
967 property the property
968 title the title field for the columns
969 width the column width in characters, can be nil for automatic
970 operator the operator if any
971 format the output format for computed results, derived from operator
972 printf a printf format for computed values"
973 (let ((start 0) width prop title op f printf)
974 (setq org-columns-current-fmt-compiled nil)
976 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
978 (setq start (match-end 0)
979 width (match-string 1 fmt)
980 prop (match-string 2 fmt)
981 title (or (match-string 3 fmt) prop)
982 op (match-string 4 fmt)
985 (if width (setq width (string-to-number width)))
986 (when (and op (string-match ";" op))
987 (setq printf (substring op (match-end 0))
988 op (substring op 0 (match-beginning 0))))
990 ((equal op "+") (setq f 'add_numbers))
991 ((equal op "$") (setq f 'currency))
992 ((equal op ":") (setq f 'add_times))
993 ((equal op "X") (setq f 'checkbox))
994 ((equal op "X/") (setq f 'checkbox-n-of-m))
995 ((equal op "X%") (setq f 'checkbox-percent))
997 (push (list prop title width op f printf) org-columns-current-fmt-compiled))
998 (setq org-columns-current-fmt-compiled
999 (nreverse org-columns-current-fmt-compiled))))
1002 ;;; Dynamic block for Column view
1004 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
1005 "Get the column view of the current buffer or subtree.
1006 The first optional argument MAXLEVEL sets the level limit. A
1007 second optional argument SKIP-EMPTY-ROWS tells whether to skip
1008 empty rows, an empty row being one where all the column view
1009 specifiers except ITEM are empty. This function returns a list
1010 containing the title row and all other rows. Each row is a list
1013 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1014 (n (length title)) row tbl)
1015 (goto-char (point-min))
1016 (while (and (re-search-forward "^\\(\\*+\\) " nil t)
1019 (if org-odd-levels-only
1020 (/ (1+ (length (match-string 1))) 2)
1021 (length (match-string 1))))))
1022 (when (get-char-property (match-beginning 0) 'org-columns-key)
1024 (loop for i from 0 to (1- n) do
1025 (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
1026 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
1029 (setq row (nreverse row))
1030 (unless (and skip-empty-rows
1031 (eq 1 (length (delete "" (delete-dups row)))))
1033 (append (list title 'hline) (nreverse tbl)))))
1035 (defun org-dblock-write:columnview (params)
1036 "Write the column view table.
1037 PARAMS is a property list of parameters:
1039 :width enforce same column widths with <N> specifiers.
1040 :id the :ID: property of the entry where the columns view
1041 should be built, as a string. When `local', call locally.
1042 When `global' call column view with the cursor at the beginning
1043 of the buffer (usually this means that the whole buffer switches
1045 :hlines When t, insert a hline before each item. When a number, insert
1046 a hline before each level <= that number.
1047 :vlines When t, make each column a colgroup to enforce vertical lines.
1048 :maxlevel When set to a number, don't capture headlines below this level.
1050 When t, skip rows where all specifiers other than ITEM are empty."
1051 (let ((pos (move-marker (make-marker) (point)))
1052 (hlines (plist-get params :hlines))
1053 (vlines (plist-get params :vlines))
1054 (maxlevel (plist-get params :maxlevel))
1055 (skip-empty-rows (plist-get params :skip-empty-rows))
1056 tbl id idpos nfields tmp)
1059 (when (setq id (plist-get params :id))
1060 (cond ((not id) nil)
1061 ((eq id 'global) (goto-char (point-min)))
1062 ((eq id 'local) nil)
1063 ((setq idpos (org-find-entry-with-id id))
1065 (t (error "Cannot find entry with :ID: %s" id))))
1067 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1068 (setq nfields (length (car tbl)))
1069 (org-columns-quit)))
1071 (move-marker pos nil)
1073 (when (plist-get params :hlines)
1076 (if (eq (car tbl) 'hline)
1077 (push (pop tbl) tmp)
1078 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1079 (if (and (not (eq (car tmp) 'hline))
1081 (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
1083 (push (pop tbl) tmp)))
1084 (setq tbl (nreverse tmp)))
1086 (setq tbl (mapcar (lambda (x)
1087 (if (eq 'hline x) x (cons "" x)))
1089 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1091 (insert (org-listtable-to-string tbl))
1092 (when (plist-get params :width)
1093 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1094 org-columns-current-widths "|")))
1096 (org-table-align))))
1098 (defun org-listtable-to-string (tbl)
1099 "Convert a listtable TBL to a string that contains the Org-mode table.
1100 The table still need to be alligned. The resulting string has no leading
1101 and tailing newline characters."
1106 (concat "|" (mapconcat 'identity x "|") "|"))
1107 ((eq x 'hline) "|-|")
1108 (t (error "Garbage in listtable: %s" x))))
1111 (defun org-insert-columns-dblock ()
1112 "Create a dynamic block capturing a column view table."
1114 (let ((defaults '(:name "columnview" :hlines 1))
1115 (id (completing-read
1116 "Capture columns (local, global, entry with :ID: property) [local]: "
1117 (append '(("global") ("local"))
1118 (mapcar 'list (org-property-values "ID"))))))
1119 (if (equal id "") (setq id 'local))
1120 (if (equal id "global") (setq id 'global))
1121 (setq defaults (append defaults (list :id id)))
1122 (org-create-dblock defaults)
1123 (org-update-dblock)))
1125 ;;; Column view in the agenda
1127 (defvar org-agenda-view-columns-initially nil
1128 "When set, switch to columns view immediately after creating the agenda.")
1130 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1131 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1132 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1134 (defun org-agenda-columns ()
1135 "Turn on or update column view in the agenda."
1137 (org-verify-version 'columns)
1138 (org-columns-remove-overlays)
1139 (move-marker org-columns-begin-marker (point))
1140 (let (fmt cache maxwidths m p a d)
1142 ((and (boundp 'org-agenda-overriding-columns-format)
1143 org-agenda-overriding-columns-format)
1144 (setq fmt org-agenda-overriding-columns-format)
1145 (org-set-local 'org-agenda-overriding-columns-format fmt))
1146 ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
1147 (setq fmt (or (org-entry-get m "COLUMNS" t)
1148 (with-current-buffer (marker-buffer m)
1149 org-columns-default-format))))
1150 ((and (boundp 'org-columns-current-fmt)
1151 (local-variable-p 'org-columns-current-fmt)
1152 org-columns-current-fmt)
1153 (setq fmt org-columns-current-fmt))
1154 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
1155 (setq m (get-text-property m 'org-hd-marker))
1156 (setq fmt (or (org-entry-get m "COLUMNS" t)
1157 (with-current-buffer (marker-buffer m)
1158 org-columns-default-format)))))
1159 (setq fmt (or fmt org-columns-default-format))
1160 (org-set-local 'org-columns-current-fmt fmt)
1161 (org-columns-compile-format fmt)
1162 (when org-agenda-columns-compute-summary-properties
1163 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1165 ;; Get and cache the properties
1166 (goto-char (point-min))
1168 (when (setq m (or (get-text-property (point) 'org-hd-marker)
1169 (get-text-property (point) 'org-marker)))
1170 (setq p (org-entry-properties m))
1172 (when (or (not (setq a (assoc org-effort-property p)))
1173 (not (string-match "\\S-" (or (cdr a) ""))))
1174 ;; OK, the property is not defined. Use appointment duration?
1175 (when (and org-agenda-columns-add-appointments-to-effort-sum
1176 (setq d (get-text-property (point) 'duration)))
1177 (setq d (org-minutes-to-hh:mm-string d))
1178 (put-text-property 0 (length d) 'face 'org-warning d)
1179 (push (cons org-effort-property d) p)))
1180 (push (cons (org-current-line) p) cache))
1181 (beginning-of-line 2))
1183 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
1184 (org-set-local 'org-columns-current-maxwidths maxwidths)
1185 (org-columns-display-here-title)
1186 (when (org-set-local 'org-columns-flyspell-was-active
1187 (org-bound-and-true-p flyspell-mode))
1191 (org-columns-display-here (cdr x)))
1193 (when org-agenda-columns-show-summaries
1194 (org-agenda-colview-summarize cache))))))
1196 (defun org-agenda-colview-summarize (cache)
1197 "Summarize the summarizable columns in column view in the agenda.
1198 This will add overlays to the date lines, to show the summary for each day."
1199 (let* ((fmt (mapcar (lambda (x)
1200 (list (car x) (if (equal (car x) "CLOCKSUM")
1201 'add_times (nth 4 x))))
1202 org-columns-current-fmt-compiled))
1203 line c c1 stype props lsum entries prop v)
1205 (when (delq nil (mapcar 'cadr fmt))
1206 ;; OK, at least one summation column, it makes sense to try this
1207 (goto-char (point-max))
1209 (when (or (get-text-property (point) 'org-date-line)
1210 (eq (get-text-property (point) 'face)
1211 'org-agenda-structure))
1212 ;; OK, this is a date line that should be used
1213 (setq line (org-current-line))
1214 (setq entries nil c cache cache nil)
1215 (while (setq c1 (pop c))
1216 (if (> (car c1) line)
1219 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1220 ;; Compute the summaries for the properties we want,
1221 ;; set nil properties for the rest.
1222 (when (setq entries (mapcar 'cdr entries))
1226 (setq prop (car f) stype (nth 1 f))
1228 ((equal prop "ITEM")
1229 (cons prop (buffer-substring (point-at-bol)
1231 ((not stype) (cons prop ""))
1236 (setq v (cdr (assoc prop x)))
1237 (if v (setq lsum (+ lsum
1238 (org-column-string-to-number
1241 (setq lsum (org-columns-number-to-string lsum stype))
1243 0 (length lsum) 'face 'bold lsum)
1246 (org-columns-display-here props)
1247 (org-set-local 'org-agenda-columns-active t)))
1248 (if (bobp) (throw 'exit t))
1249 (beginning-of-line 0))))))
1251 (defun org-agenda-colview-compute (fmt)
1252 "Compute the relevant columns in the contributing source buffers."
1253 (let ((files org-agenda-contributing-files)
1254 (org-columns-begin-marker (make-marker))
1255 (org-columns-top-level-marker (make-marker))
1257 (while (setq f (pop files))
1258 (setq b (find-buffer-visiting f))
1259 (with-current-buffer (or (buffer-base-buffer b) b)
1264 (remove-text-properties (point-min) (point-max)
1265 '(org-summaries t)))
1266 (goto-char (point-min))
1267 (org-columns-get-format-and-top-level)
1268 (while (setq fm (pop fmt))
1269 (if (equal (car fm) "CLOCKSUM")
1271 (when (and (nth 4 fm)
1272 (setq a (assoc (car fm)
1273 org-columns-current-fmt-compiled))
1274 (equal (nth 4 a) (nth 4 fm)))
1275 (org-columns-compute (car fm)))))))))))
1277 (provide 'org-colview)
1279 ;; arch-tag: 61f5128d-747c-4983-9479-e3871fa3d73c
1281 ;;; org-colview.el ends here