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 title)
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)
162 ass (if (equal property "ITEM")
167 (buffer-substring-no-properties
168 (point-at-bol) (point-at-eol))))))
169 (assoc property props))
170 width (or (cdr (assoc property org-columns-current-maxwidths))
173 f (format "%%-%d.%ds | " width width)
174 val (or (cdr ass) "")
175 modval (or (and org-columns-modify-value-for-display-function
177 org-columns-modify-value-for-display-function)
179 org-columns-modify-value-for-display-function
181 (if (equal property "ITEM")
183 (org-columns-cleanup-item
184 val org-columns-current-fmt-compiled)
185 (org-agenda-columns-cleanup-item
186 val pl cphr org-columns-current-fmt-compiled)))))
187 (setq s2 (org-columns-add-ellipses (or modval val) width))
188 (setq string (format f s2))
189 ;; Create the overlay
191 (setq ov (org-columns-new-overlay
192 beg (setq beg (1+ beg)) string face))
193 (org-overlay-put ov 'keymap org-columns-map)
194 (org-overlay-put ov 'org-columns-key property)
195 (org-overlay-put ov 'org-columns-value (cdr ass))
196 (org-overlay-put ov 'org-columns-value-modified modval)
197 (org-overlay-put ov 'org-columns-pom pom)
198 (org-overlay-put ov 'org-columns-format f))
199 (if (or (not (char-after beg))
200 (equal (char-after beg) ?\n))
201 (let ((inhibit-read-only t))
204 (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
205 ;; Make the rest of the line disappear.
207 (setq ov (org-columns-new-overlay beg (point-at-eol)))
208 (org-overlay-put ov 'invisible t)
209 (org-overlay-put ov 'keymap org-columns-map)
210 (org-overlay-put ov 'intangible t)
211 (push ov org-columns-overlays)
212 (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
213 (org-overlay-put ov 'keymap org-columns-map)
214 (push ov org-columns-overlays)
215 (let ((inhibit-read-only t))
216 (put-text-property (max (point-min) (1- (point-at-bol)))
217 (min (point-max) (1+ (point-at-eol)))
218 'read-only "Type `e' to edit property")))))
220 (defun org-columns-add-ellipses (string width)
221 "Truncate STRING with WIDTH characters, with ellipses."
223 ((<= (length string) width) string)
224 ((<= width (length org-columns-ellipses))
225 (substring org-columns-ellipses 0 width))
226 (t (concat (substring string 0 (- width (length org-columns-ellipses)))
227 org-columns-ellipses))))
229 (defvar org-columns-full-header-line-format nil
230 "Fthe full header line format, will be shifted by horizontal scrolling." )
231 (defvar org-previous-header-line-format nil
232 "The header line format before column view was turned on.")
233 (defvar org-columns-inhibit-recalculation nil
234 "Inhibit recomputing of columns on column view startup.")
235 (defvar org-columns-flyspell-was-active nil
236 "Remember the state of `flyspell-mode' before column view.
237 Flyspell-mode can cause problems in columns view, so it is turned off
238 for the duration of the command.")
240 (defvar header-line-format)
241 (defvar org-columns-previous-hscroll 0)
242 (defun org-columns-display-here-title ()
243 "Overlay the newline before the current line with the table title."
245 (let ((fmt org-columns-current-fmt-compiled)
247 property width f column str widths)
248 (while (setq column (pop fmt))
249 (setq property (car column)
250 str (or (nth 1 column) property)
251 width (or (cdr (assoc property org-columns-current-maxwidths))
254 widths (push width widths)
255 f (format "%%-%d.%ds | " width width)
256 string (format f str)
257 title (concat title string)))
259 (org-add-props " " nil 'display '(space :align-to 0))
260 ;;(org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
261 (org-add-props title nil 'face 'org-column-title)))
262 (org-set-local 'org-previous-header-line-format header-line-format)
263 (org-set-local 'org-columns-current-widths (nreverse widths))
264 (setq org-columns-full-header-line-format title)
265 (setq org-columns-previous-hscroll -1)
266 ; (org-columns-hscoll-title)
267 (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
269 (defun org-columns-hscoll-title ()
270 "Set the header-line-format so that it scrolls along with the table."
271 (sit-for .0001) ; need to force a redisplay to update window-hscroll
272 (when (not (= (window-hscroll) org-columns-previous-hscroll))
273 (setq header-line-format
274 (concat (substring org-columns-full-header-line-format 0 1)
275 (substring org-columns-full-header-line-format
276 (1+ (window-hscroll))))
277 org-columns-previous-hscroll (window-hscroll))
278 (force-mode-line-update)))
280 (defun org-columns-remove-overlays ()
281 "Remove all currently active column overlays."
283 (when (marker-buffer org-columns-begin-marker)
284 (with-current-buffer (marker-buffer org-columns-begin-marker)
285 (when (local-variable-p 'org-previous-header-line-format)
286 (setq header-line-format org-previous-header-line-format)
287 (kill-local-variable 'org-previous-header-line-format)
288 (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
289 (move-marker org-columns-begin-marker nil)
290 (move-marker org-columns-top-level-marker nil)
292 (mapc 'org-delete-overlay org-columns-overlays)
293 (setq org-columns-overlays nil)
294 (let ((inhibit-read-only t))
295 (remove-text-properties (point-min) (point-max) '(read-only t))))
296 (when org-columns-flyspell-was-active
297 (flyspell-mode 1)))))
299 (defun org-columns-cleanup-item (item fmt)
300 "Remove from ITEM what is a column in the format FMT."
301 (if (not org-complex-heading-regexp)
303 (when (string-match org-complex-heading-regexp item)
306 (org-add-props (match-string 1 item) nil
307 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
308 (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
309 (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
310 " " (save-match-data (org-columns-compact-links (match-string 4 item)))
311 (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))
314 (list 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
318 (defun org-columns-compact-links (s)
319 "Replace [[link][desc]] with [desc] or [link]."
320 (while (string-match org-bracket-link-regexp s)
321 (setq s (replace-match
322 (concat "[" (match-string (if (match-end 3) 3 1) s) "]")
326 (defvar org-agenda-columns-remove-prefix-from-item)
327 (defun org-agenda-columns-cleanup-item (item pl cphr fmt)
328 "Cleanup the tiem property for agenda column view.
329 See also the variable `org-agenda-columns-remove-prefix-from-item'."
330 (let* ((org-complex-heading-regexp cphr)
331 (prefix (substring item 0 pl))
332 (rest (substring item pl))
333 (fake (concat "* " rest))
334 (cleaned (org-trim (substring (org-columns-cleanup-item fake fmt) 1))))
335 (if org-agenda-columns-remove-prefix-from-item
337 (concat prefix cleaned))))
339 (defun org-columns-show-value ()
340 "Show the full value of the property."
342 (let ((value (get-char-property (point) 'org-columns-value)))
343 (message "Value is: %s" (or value ""))))
345 (defvar org-agenda-columns-active) ;; defined in org-agenda.el
346 (defun org-columns-quit ()
347 "Remove the column overlays and in this way exit column editing."
350 (org-columns-remove-overlays)
351 (let ((inhibit-read-only t))
352 (remove-text-properties (point-min) (point-max) '(read-only t))))
353 (when (eq major-mode 'org-agenda-mode)
354 (setq org-agenda-columns-active nil)
356 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
358 (defun org-columns-check-computed ()
359 "Check if this column value is computed.
360 If yes, throw an error indicating that changing it does not make sense."
361 (let ((val (get-char-property (point) 'org-columns-value)))
362 (when (and (stringp val)
363 (get-char-property 0 'org-computed val))
364 (error "This value is computed from the entry's children"))))
366 (defun org-columns-todo (&optional arg)
367 "Change the TODO state during column view."
369 (org-columns-edit-value "TODO"))
371 (defun org-columns-set-tags-or-toggle (&optional arg)
372 "Toggle checkbox at point, or set tags for current headline."
374 (if (string-match "\\`\\[[ xX-]\\]\\'"
375 (get-char-property (point) 'org-columns-value))
376 (org-columns-next-allowed-value)
377 (org-columns-edit-value "TAGS")))
379 (defun org-columns-edit-value (&optional key)
380 "Edit the value of the property at point in column view.
381 Where possible, use the standard interface for changing this line."
383 (org-columns-check-computed)
384 (let* ((external-key key)
385 (col (current-column))
386 (key (or key (get-char-property (point) 'org-columns-key)))
387 (value (get-char-property (point) 'org-columns-value))
388 (bol (point-at-bol)) (eol (point-at-eol))
389 (pom (or (get-text-property bol 'org-hd-marker)
390 (point))) ; keep despite of compiler waring
392 (delq nil (mapcar (lambda (x)
393 (and (eq (overlay-buffer x) (current-buffer))
394 (>= (overlay-start x) bol)
395 (<= (overlay-start x) eol)
397 org-columns-overlays)))
400 ((equal key "CLOCKSUM")
401 (error "This special column cannot be edited"))
403 (setq eval '(org-with-point-at pom
404 (org-edit-headline))))
406 (setq eval '(org-with-point-at pom
407 (let ((current-prefix-arg
408 (if external-key current-prefix-arg '(4))))
409 (call-interactively 'org-todo)))))
410 ((equal key "PRIORITY")
411 (setq eval '(org-with-point-at pom
412 (call-interactively 'org-priority))))
414 (setq eval '(org-with-point-at pom
415 (let ((org-fast-tag-selection-single-key
416 (if (eq org-fast-tag-selection-single-key 'expert)
417 t org-fast-tag-selection-single-key)))
418 (call-interactively 'org-set-tags)))))
419 ((equal key "DEADLINE")
420 (setq eval '(org-with-point-at pom
421 (call-interactively 'org-deadline))))
422 ((equal key "SCHEDULED")
423 (setq eval '(org-with-point-at pom
424 (call-interactively 'org-schedule))))
426 (setq allowed (org-property-get-allowed-values pom key 'table))
428 (setq nval (completing-read "Value: " allowed nil t))
429 (setq nval (read-string "Edit: " value)))
430 (setq nval (org-trim nval))
431 (when (not (equal nval value))
432 (setq eval '(org-entry-put pom key nval)))))
436 ((equal major-mode 'org-agenda-mode)
437 (org-columns-eval eval)
438 ;; The following let preserves the current format, and makes sure
439 ;; that in only a single file things need to be upated.
440 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
441 (buffer (marker-buffer pom))
442 (org-agenda-contributing-files
443 (list (with-current-buffer buffer
444 (buffer-file-name (buffer-base-buffer))))))
445 (org-agenda-columns)))
447 (let ((inhibit-read-only t))
449 (remove-text-properties
450 (max (point-min) (1- bol)) eol '(read-only t)))
453 (setq org-columns-overlays
454 (org-delete-all line-overlays org-columns-overlays))
455 (mapc 'org-delete-overlay line-overlays)
456 (org-columns-eval eval))
457 (org-columns-display-here)))
458 (org-move-to-column col)
459 (if (and (org-mode-p)
460 (nth 3 (assoc key org-columns-current-fmt-compiled)))
461 (org-columns-update key)))))))
463 (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
464 "Edit the current headline, the part without TODO keyword, TAGS."
465 (org-back-to-heading)
466 (when (looking-at org-todo-line-regexp)
468 (pre (buffer-substring (match-beginning 0) (match-beginning 3)))
469 (txt (match-string 3))
472 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
473 (setq post (match-string 0 txt)
474 txt (substring txt 0 (match-beginning 0))))
475 (setq txt2 (read-string "Edit: " txt))
476 (when (not (equal txt txt2))
478 (insert pre txt2 post)
479 (delete-region (point) (point-at-eol))
480 (org-set-tags nil t)))))
482 (defun org-columns-edit-allowed ()
483 "Edit the list of allowed values for the current property."
485 (let* ((pom (or (get-text-property (point-at-bol) 'org-marker)
486 (get-text-property (point-at-bol) 'org-hd-marker)
488 (key (get-char-property (point) 'org-columns-key))
489 (key1 (concat key "_ALL"))
490 (allowed (org-entry-get pom key1 t))
492 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
493 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
494 (setq nval (read-string "Allowed: " allowed))
496 (cond ((marker-position org-entry-property-inherited-from)
497 org-entry-property-inherited-from)
498 ((marker-position org-columns-top-level-marker)
499 org-columns-top-level-marker)
503 (defun org-columns-eval (form)
506 (beginning-of-line 1)
507 ;; `next-line' is needed here, because it skips invisible line.
508 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
509 (setq hidep (org-on-heading-p 1)))
511 (and hidep (hide-entry))))
513 (defun org-columns-previous-allowed-value ()
514 "Switch to the previous allowed value for this column."
516 (org-columns-next-allowed-value t))
518 (defun org-columns-next-allowed-value (&optional previous nth)
519 "Switch to the next allowed value for this column.
520 When PREVIOUS is set, go to the previous value. When NTH is
521 an integer, select that value."
523 (org-columns-check-computed)
524 (let* ((col (current-column))
525 (key (get-char-property (point) 'org-columns-key))
526 (value (get-char-property (point) 'org-columns-value))
527 (bol (point-at-bol)) (eol (point-at-eol))
528 (pom (or (get-text-property bol 'org-hd-marker)
529 (point))) ; keep despite of compiler waring
531 (delq nil (mapcar (lambda (x)
532 (and (eq (overlay-buffer x) (current-buffer))
533 (>= (overlay-start x) bol)
534 (<= (overlay-start x) eol)
536 org-columns-overlays)))
537 (allowed (or (org-property-get-allowed-values pom key)
539 (nth 4 (assoc key org-columns-current-fmt-compiled))
540 '(checkbox checkbox-n-of-m checkbox-percent))
545 (if (= nth -1) (setq nth 9)))
546 (when (equal key "ITEM")
547 (error "Cannot edit item headline from here"))
548 (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
549 (error "Allowed values for this property have not been defined"))
550 (if (member key '("SCHEDULED" "DEADLINE"))
551 (setq nval (if previous 'earlier 'later))
552 (if previous (setq allowed (reverse allowed)))
555 (setq nval (nth nth allowed))
557 (error "There are only %d allowed values for property `%s'"
558 (length allowed) key)))
559 ((member value allowed)
560 (setq nval (or (car (cdr (member value allowed)))
562 (if (equal nval value)
563 (error "Only one allowed value for this property")))
564 (t (setq nval (car allowed)))))
566 ((equal major-mode 'org-agenda-mode)
567 (org-columns-eval '(org-entry-put pom key nval))
568 ;; The following let preserves the current format, and makes sure
569 ;; that in only a single file things need to be upated.
570 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
571 (buffer (marker-buffer pom))
572 (org-agenda-contributing-files
573 (list (with-current-buffer buffer
574 (buffer-file-name (buffer-base-buffer))))))
575 (org-agenda-columns)))
577 (let ((inhibit-read-only t))
578 (remove-text-properties (1- bol) eol '(read-only t))
581 (setq org-columns-overlays
582 (org-delete-all line-overlays org-columns-overlays))
583 (mapc 'org-delete-overlay line-overlays)
584 (org-columns-eval '(org-entry-put pom key nval)))
585 (org-columns-display-here)))
586 (org-move-to-column col)
587 (and (nth 3 (assoc key org-columns-current-fmt-compiled))
588 (org-columns-update key))))))
590 (defun org-verify-version (task)
593 (if (or (featurep 'xemacs)
594 (< emacs-major-version 22))
595 (error "Emacs 22 is required for the columns feature")))))
597 (defun org-columns-open-link (&optional arg)
599 (let ((value (get-char-property (point) 'org-columns-value)))
600 (org-open-link-from-string value arg)))
602 (defun org-columns-get-format-and-top-level ()
604 (when (condition-case nil (org-back-to-heading) (error nil))
605 (move-marker org-entry-property-inherited-from nil)
606 (setq fmt (org-entry-get nil "COLUMNS" t)))
607 (setq fmt (or fmt org-columns-default-format))
608 (org-set-local 'org-columns-current-fmt fmt)
609 (org-columns-compile-format fmt)
610 (if (marker-position org-entry-property-inherited-from)
611 (move-marker org-columns-top-level-marker
612 org-entry-property-inherited-from)
613 (move-marker org-columns-top-level-marker (point)))
616 (defun org-columns ()
617 "Turn on column view on an org-mode file."
619 (org-verify-version 'columns)
620 (org-columns-remove-overlays)
621 (move-marker org-columns-begin-marker (point))
622 (let (beg end fmt cache maxwidths)
623 (setq fmt (org-columns-get-format-and-top-level))
625 (goto-char org-columns-top-level-marker)
627 (unless org-columns-inhibit-recalculation
628 (org-columns-compute-all))
629 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
631 ;; Get and cache the properties
633 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
636 (narrow-to-region beg end)
638 (while (re-search-forward (concat "^" outline-regexp) end t)
639 (push (cons (org-current-line) (org-entry-properties)) cache))
641 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
642 (org-set-local 'org-columns-current-maxwidths maxwidths)
643 (org-columns-display-here-title)
644 (when (org-set-local 'org-columns-flyspell-was-active
645 (org-bound-and-true-p flyspell-mode))
649 (org-columns-display-here (cdr x)))
652 (defun org-columns-new (&optional prop title width op fmt &rest rest)
653 "Insert a new column, to the left of the current column."
655 (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
657 (setq prop (completing-read
658 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
660 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
661 (setq width (read-string "Column width: " (if width (number-to-string width))))
662 (if (string-match "\\S-" width)
663 (setq width (string-to-number width))
665 (setq fmt (completing-read "Summary [none]: "
666 '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
668 (if (string-match "\\S-" fmt)
669 (setq fmt (intern fmt))
671 (if (eq fmt 'none) (setq fmt nil))
675 (setcdr editp (list title width nil fmt)))
676 (setq cell (nthcdr (1- (current-column))
677 org-columns-current-fmt-compiled))
678 (setcdr cell (cons (list prop title width nil fmt)
680 (org-columns-store-format)
683 (defun org-columns-delete ()
684 "Delete the column at point from columns view."
686 (let* ((n (current-column))
687 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
689 (format "Are you sure you want to remove column \"%s\"? " title))
690 (setq org-columns-current-fmt-compiled
691 (delq (nth n org-columns-current-fmt-compiled)
692 org-columns-current-fmt-compiled))
693 (org-columns-store-format)
695 (if (>= (current-column) (length org-columns-current-fmt-compiled))
696 (backward-char 1)))))
698 (defun org-columns-edit-attributes ()
699 "Edit the attributes of the current column."
701 (let* ((n (current-column))
702 (info (nth n org-columns-current-fmt-compiled)))
703 (apply 'org-columns-new info)))
705 (defun org-columns-widen (arg)
706 "Make the column wider by ARG characters."
708 (let* ((n (current-column))
709 (entry (nth n org-columns-current-fmt-compiled))
710 (width (or (nth 2 entry)
711 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
712 (setq width (max 1 (+ width arg)))
713 (setcar (nthcdr 2 entry) width)
714 (org-columns-store-format)
717 (defun org-columns-narrow (arg)
718 "Make the column nrrower by ARG characters."
720 (org-columns-widen (- arg)))
722 (defun org-columns-move-right ()
723 "Swap this column with the one to the right."
725 (let* ((n (current-column))
726 (cell (nthcdr n org-columns-current-fmt-compiled))
728 (when (>= n (1- (length org-columns-current-fmt-compiled)))
729 (error "Cannot shift this column further to the right"))
731 (setcar cell (car (cdr cell)))
732 (setcdr cell (cons e (cdr (cdr cell))))
733 (org-columns-store-format)
737 (defun org-columns-move-left ()
738 "Swap this column with the one to the left."
740 (let* ((n (current-column)))
742 (error "Cannot shift this column further to the left"))
744 (org-columns-move-right)
747 (defun org-columns-store-format ()
748 "Store the text version of the current columns format in appropriate place.
749 This is either in the COLUMNS property of the node starting the current column
750 display, or in the #+COLUMNS line of the current buffer."
752 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
753 (org-set-local 'org-columns-current-fmt fmt)
754 (if (marker-position org-columns-top-level-marker)
756 (goto-char org-columns-top-level-marker)
757 (if (and (org-at-heading-p)
758 (org-entry-get nil "COLUMNS"))
759 (org-entry-put nil "COLUMNS" fmt)
760 (goto-char (point-min))
761 ;; Overwrite all #+COLUMNS lines....
762 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
764 (replace-match (concat "#+COLUMNS: " fmt) t t))
766 (goto-char (point-min))
767 (or (org-on-heading-p t) (outline-next-heading))
768 (let ((inhibit-read-only t))
769 (insert-before-markers "#+COLUMNS: " fmt "\n")))
770 (org-set-local 'org-columns-default-format fmt))))))
772 (defvar org-agenda-overriding-columns-format nil
773 "When set, overrides any other format definition for the agenda.
774 Don't set this, this is meant for dynamic scoping.")
776 (defun org-columns-get-autowidth-alist (s cache)
777 "Derive the maximum column widths from the format and the cache."
779 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
780 (push (cons (match-string 1 s) 1) rtn)
781 (setq start (match-end 0)))
783 (setcdr x (apply 'max
786 (length (or (cdr (assoc (car x) (cdr y))) " ")))
791 (defun org-columns-compute-all ()
792 "Compute all columns that have operators defined."
794 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
795 (let ((columns org-columns-current-fmt-compiled) col)
796 (while (setq col (pop columns))
799 (org-columns-compute (car col)))))))
801 (defun org-columns-update (property)
802 "Recompute PROPERTY, and update the columns display for it."
803 (org-columns-compute property)
807 (when (equal (org-overlay-get ov 'org-columns-key) property)
808 (setq pos (org-overlay-start ov))
810 (when (setq val (cdr (assoc property
812 (point-at-bol) 'org-summaries))))
813 (setq fmt (org-overlay-get ov 'org-columns-format))
814 (org-overlay-put ov 'org-columns-value val)
815 (org-overlay-put ov 'display (format fmt val)))))
816 org-columns-overlays))))
818 (defun org-columns-compute (property)
819 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
821 (let* ((re (concat "^" outline-regexp))
822 (lmax 30) ; Does anyone use deeper levels???
823 (lsum (make-vector lmax 0))
824 (lflag (make-vector lmax nil))
826 (ass (assoc property org-columns-current-fmt-compiled))
829 (beg org-columns-top-level-marker)
830 last-level val valflag flag end sumpos sum-alist sum str str1 useval)
832 ;; Find the region to compute
834 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
836 ;; Walk the tree from the back and do the computations
837 (while (re-search-backward re beg t)
838 (setq sumpos (match-beginning 0)
840 level (org-outline-level)
841 val (org-entry-get nil property)
842 valflag (and val (string-match "\\S-" val)))
844 ((< level last-level)
845 ;; put the sum of lower levels here as a property
846 (setq sum (aref lsum last-level) ; current sum
847 flag (aref lflag last-level) ; any valid entries from children?
848 str (org-columns-number-to-string sum format printf)
849 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
850 useval (if flag str1 (if valflag val ""))
851 sum-alist (get-text-property sumpos 'org-summaries))
852 (if (assoc property sum-alist)
853 (setcdr (assoc property sum-alist) useval)
854 (push (cons property useval) sum-alist)
856 (add-text-properties sumpos (1+ sumpos)
857 (list 'org-summaries sum-alist))))
858 (when (and val (not (equal val (if flag str val))))
859 (org-entry-put nil property (if flag str val)))
860 ;; add current to current level accumulator
861 (when (or flag valflag)
862 (aset lsum level (+ (aref lsum level)
863 (if flag sum (org-column-string-to-number
864 (if flag str val) format))))
865 (aset lflag level t))
866 ;; clear accumulators for deeper levels
867 (loop for l from (1+ level) to (1- lmax) do
870 ((>= level last-level)
871 ;; add what we have here to the accumulator for this level
872 (aset lsum level (+ (aref lsum level)
873 (org-column-string-to-number (or val "0") format)))
874 (and valflag (aset lflag level t)))
875 (t (error "This should not happen")))))))
877 (defun org-columns-redo ()
878 "Construct the column display again."
880 (message "Recomputing columns...")
881 (let ((line (org-current-line))
882 (col (current-column)))
884 (if (marker-position org-columns-begin-marker)
885 (goto-char org-columns-begin-marker))
886 (org-columns-remove-overlays)
888 (call-interactively 'org-columns)
890 (call-interactively 'org-agenda-columns)))
892 (move-to-column col))
893 (message "Recomputing columns...done"))
895 (defun org-columns-not-in-agenda ()
896 (if (eq major-mode 'org-agenda-mode)
897 (error "This command is only allowed in Org-mode buffers")))
900 (defun org-string-to-number (s)
901 "Convert string to number, and interpret hh:mm:ss."
902 (if (not (string-match ":" s))
904 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
906 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
909 (defun org-columns-number-to-string (n fmt &optional printf)
910 "Convert a computed column number to a string value, according to FMT."
913 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
914 (format org-time-clocksum-format h m)))
916 (cond ((= n (floor n)) "[X]")
919 ((memq fmt '(checkbox-n-of-m checkbox-percent))
920 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
921 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
922 (printf (format printf n))
925 (t (number-to-string n))))
927 (defun org-nofm-to-completion (n m &optional percent)
929 (format "[%d/%d]" n m)
930 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
932 (defun org-column-string-to-number (s fmt)
933 "Convert a column value to a number that can be used for column computing."
935 ((string-match ":" s)
936 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
938 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
940 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
941 (if (equal s "[X]") 1. 0.000001))
942 (t (string-to-number s))))
944 (defun org-columns-uncompile-format (cfmt)
945 "Turn the compiled columns format back into a string representation."
946 (let ((rtn "") e s prop title op width fmt printf)
947 (while (setq e (pop cfmt))
955 ((eq fmt 'add_times) (setq op ":"))
956 ((eq fmt 'checkbox) (setq op "X"))
957 ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
958 ((eq fmt 'checkbox-percent) (setq op "X%"))
959 ((eq fmt 'add_numbers) (setq op "+"))
960 ((eq fmt 'currency) (setq op "$")))
961 (if (and op printf) (setq op (concat op ";" printf)))
962 (if (equal title prop) (setq title nil))
963 (setq s (concat "%" (if width (number-to-string width))
965 (if title (concat "(" title ")"))
966 (if op (concat "{" op "}"))))
967 (setq rtn (concat rtn " " s)))
970 (defun org-columns-compile-format (fmt)
971 "Turn a column format string into an alist of specifications.
972 The alist has one entry for each column in the format. The elements of
974 property the property
975 title the title field for the columns
976 width the column width in characters, can be nil for automatic
977 operator the operator if any
978 format the output format for computed results, derived from operator
979 printf a printf format for computed values"
980 (let ((start 0) width prop title op f printf)
981 (setq org-columns-current-fmt-compiled nil)
983 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
985 (setq start (match-end 0)
986 width (match-string 1 fmt)
987 prop (match-string 2 fmt)
988 title (or (match-string 3 fmt) prop)
989 op (match-string 4 fmt)
992 (if width (setq width (string-to-number width)))
993 (when (and op (string-match ";" op))
994 (setq printf (substring op (match-end 0))
995 op (substring op 0 (match-beginning 0))))
997 ((equal op "+") (setq f 'add_numbers))
998 ((equal op "$") (setq f 'currency))
999 ((equal op ":") (setq f 'add_times))
1000 ((equal op "X") (setq f 'checkbox))
1001 ((equal op "X/") (setq f 'checkbox-n-of-m))
1002 ((equal op "X%") (setq f 'checkbox-percent))
1004 (push (list prop title width op f printf) org-columns-current-fmt-compiled))
1005 (setq org-columns-current-fmt-compiled
1006 (nreverse org-columns-current-fmt-compiled))))
1009 ;;; Dynamic block for Column view
1011 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
1012 "Get the column view of the current buffer or subtree.
1013 The first optional argument MAXLEVEL sets the level limit. A
1014 second optional argument SKIP-EMPTY-ROWS tells whether to skip
1015 empty rows, an empty row being one where all the column view
1016 specifiers except ITEM are empty. This function returns a list
1017 containing the title row and all other rows. Each row is a list
1020 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
1021 (n (length title)) row tbl)
1022 (goto-char (point-min))
1023 (while (re-search-forward "^\\(\\*+\\) " nil t)
1024 (when (and (or (null maxlevel)
1026 (if org-odd-levels-only
1027 (/ (1+ (length (match-string 1))) 2)
1028 (length (match-string 1)))))
1029 (get-char-property (match-beginning 0) 'org-columns-key))
1031 (loop for i from 0 to (1- n) do
1032 (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
1033 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
1036 (setq row (nreverse row))
1037 (unless (and skip-empty-rows
1038 (eq 1 (length (delete "" (delete-dups row)))))
1040 (append (list title 'hline) (nreverse tbl)))))
1042 (defun org-dblock-write:columnview (params)
1043 "Write the column view table.
1044 PARAMS is a property list of parameters:
1046 :width enforce same column widths with <N> specifiers.
1047 :id the :ID: property of the entry where the columns view
1048 should be built, as a string. When `local', call locally.
1049 When `global' call column view with the cursor at the beginning
1050 of the buffer (usually this means that the whole buffer switches
1052 :hlines When t, insert a hline before each item. When a number, insert
1053 a hline before each level <= that number.
1054 :vlines When t, make each column a colgroup to enforce vertical lines.
1055 :maxlevel When set to a number, don't capture headlines below this level.
1057 When t, skip rows where all specifiers other than ITEM are empty."
1058 (let ((pos (move-marker (make-marker) (point)))
1059 (hlines (plist-get params :hlines))
1060 (vlines (plist-get params :vlines))
1061 (maxlevel (plist-get params :maxlevel))
1062 (skip-empty-rows (plist-get params :skip-empty-rows))
1063 tbl id idpos nfields tmp)
1066 (when (setq id (plist-get params :id))
1067 (cond ((not id) nil)
1068 ((eq id 'global) (goto-char (point-min)))
1069 ((eq id 'local) nil)
1070 ((setq idpos (org-find-entry-with-id id))
1072 (t (error "Cannot find entry with :ID: %s" id))))
1074 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1075 (setq nfields (length (car tbl)))
1076 (org-columns-quit)))
1078 (move-marker pos nil)
1080 (when (plist-get params :hlines)
1083 (if (eq (car tbl) 'hline)
1084 (push (pop tbl) tmp)
1085 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1086 (if (and (not (eq (car tmp) 'hline))
1088 (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
1090 (push (pop tbl) tmp)))
1091 (setq tbl (nreverse tmp)))
1093 (setq tbl (mapcar (lambda (x)
1094 (if (eq 'hline x) x (cons "" x)))
1096 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1098 (insert (org-listtable-to-string tbl))
1099 (when (plist-get params :width)
1100 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1101 org-columns-current-widths "|")))
1103 (org-table-align))))
1105 (defun org-listtable-to-string (tbl)
1106 "Convert a listtable TBL to a string that contains the Org-mode table.
1107 The table still need to be alligned. The resulting string has no leading
1108 and tailing newline characters."
1113 (concat "|" (mapconcat 'identity x "|") "|"))
1114 ((eq x 'hline) "|-|")
1115 (t (error "Garbage in listtable: %s" x))))
1118 (defun org-insert-columns-dblock ()
1119 "Create a dynamic block capturing a column view table."
1121 (let ((defaults '(:name "columnview" :hlines 1))
1122 (id (completing-read
1123 "Capture columns (local, global, entry with :ID: property) [local]: "
1124 (append '(("global") ("local"))
1125 (mapcar 'list (org-property-values "ID"))))))
1126 (if (equal id "") (setq id 'local))
1127 (if (equal id "global") (setq id 'global))
1128 (setq defaults (append defaults (list :id id)))
1129 (org-create-dblock defaults)
1130 (org-update-dblock)))
1132 ;;; Column view in the agenda
1134 (defvar org-agenda-view-columns-initially nil
1135 "When set, switch to columns view immediately after creating the agenda.")
1137 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1138 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1139 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1141 (defun org-agenda-columns ()
1142 "Turn on or update column view in the agenda."
1144 (org-verify-version 'columns)
1145 (org-columns-remove-overlays)
1146 (move-marker org-columns-begin-marker (point))
1147 (let (fmt cache maxwidths m p a d)
1149 ((and (boundp 'org-agenda-overriding-columns-format)
1150 org-agenda-overriding-columns-format)
1151 (setq fmt org-agenda-overriding-columns-format)
1152 (org-set-local 'org-agenda-overriding-columns-format fmt))
1153 ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
1154 (setq fmt (or (org-entry-get m "COLUMNS" t)
1155 (with-current-buffer (marker-buffer m)
1156 org-columns-default-format))))
1157 ((and (boundp 'org-columns-current-fmt)
1158 (local-variable-p 'org-columns-current-fmt)
1159 org-columns-current-fmt)
1160 (setq fmt org-columns-current-fmt))
1161 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
1162 (setq m (get-text-property m 'org-hd-marker))
1163 (setq fmt (or (org-entry-get m "COLUMNS" t)
1164 (with-current-buffer (marker-buffer m)
1165 org-columns-default-format)))))
1166 (setq fmt (or fmt org-columns-default-format))
1167 (org-set-local 'org-columns-current-fmt fmt)
1168 (org-columns-compile-format fmt)
1169 (when org-agenda-columns-compute-summary-properties
1170 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1172 ;; Get and cache the properties
1173 (goto-char (point-min))
1175 (when (setq m (or (get-text-property (point) 'org-hd-marker)
1176 (get-text-property (point) 'org-marker)))
1177 (setq p (org-entry-properties m))
1179 (when (or (not (setq a (assoc org-effort-property p)))
1180 (not (string-match "\\S-" (or (cdr a) ""))))
1181 ;; OK, the property is not defined. Use appointment duration?
1182 (when (and org-agenda-columns-add-appointments-to-effort-sum
1183 (setq d (get-text-property (point) 'duration)))
1184 (setq d (org-minutes-to-hh:mm-string d))
1185 (put-text-property 0 (length d) 'face 'org-warning d)
1186 (push (cons org-effort-property d) p)))
1187 (push (cons (org-current-line) p) cache))
1188 (beginning-of-line 2))
1190 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
1191 (org-set-local 'org-columns-current-maxwidths maxwidths)
1192 (org-columns-display-here-title)
1193 (when (org-set-local 'org-columns-flyspell-was-active
1194 (org-bound-and-true-p flyspell-mode))
1198 (org-columns-display-here (cdr x)))
1200 (when org-agenda-columns-show-summaries
1201 (org-agenda-colview-summarize cache))))))
1203 (defun org-agenda-colview-summarize (cache)
1204 "Summarize the summarizable columns in column view in the agenda.
1205 This will add overlays to the date lines, to show the summary for each day."
1206 (let* ((fmt (mapcar (lambda (x)
1207 (list (car x) (if (equal (car x) "CLOCKSUM")
1208 'add_times (nth 4 x))))
1209 org-columns-current-fmt-compiled))
1210 line c c1 stype props lsum entries prop v)
1212 (when (delq nil (mapcar 'cadr fmt))
1213 ;; OK, at least one summation column, it makes sense to try this
1214 (goto-char (point-max))
1216 (when (or (get-text-property (point) 'org-date-line)
1217 (eq (get-text-property (point) 'face)
1218 'org-agenda-structure))
1219 ;; OK, this is a date line that should be used
1220 (setq line (org-current-line))
1221 (setq entries nil c cache cache nil)
1222 (while (setq c1 (pop c))
1223 (if (> (car c1) line)
1226 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1227 ;; Compute the summaries for the properties we want,
1228 ;; set nil properties for the rest.
1229 (when (setq entries (mapcar 'cdr entries))
1233 (setq prop (car f) stype (nth 1 f))
1235 ((equal prop "ITEM")
1236 (cons prop (buffer-substring (point-at-bol)
1238 ((not stype) (cons prop ""))
1243 (setq v (cdr (assoc prop x)))
1244 (if v (setq lsum (+ lsum
1245 (org-column-string-to-number
1248 (setq lsum (org-columns-number-to-string lsum stype))
1250 0 (length lsum) 'face 'bold lsum)
1253 (org-columns-display-here props)
1254 (org-set-local 'org-agenda-columns-active t)))
1255 (if (bobp) (throw 'exit t))
1256 (beginning-of-line 0))))))
1258 (defun org-agenda-colview-compute (fmt)
1259 "Compute the relevant columns in the contributing source buffers."
1260 (let ((files org-agenda-contributing-files)
1261 (org-columns-begin-marker (make-marker))
1262 (org-columns-top-level-marker (make-marker))
1264 (while (setq f (pop files))
1265 (setq b (find-buffer-visiting f))
1266 (with-current-buffer (or (buffer-base-buffer b) b)
1271 (remove-text-properties (point-min) (point-max)
1272 '(org-summaries t)))
1273 (goto-char (point-min))
1274 (org-columns-get-format-and-top-level)
1275 (while (setq fm (pop fmt))
1276 (if (equal (car fm) "CLOCKSUM")
1278 (when (and (nth 4 fm)
1279 (setq a (assoc (car fm)
1280 org-columns-current-fmt-compiled))
1281 (equal (nth 4 a) (nth 4 fm)))
1282 (org-columns-compute (car fm)))))))))))
1284 (provide 'org-colview)
1286 ;; arch-tag: 61f5128d-747c-4983-9479-e3871fa3d73c
1288 ;;; org-colview.el ends here