Version number set to 6.01a, release 6.01a.
[org-mode.git] / lisp / org-colview.el
blobb8fc598c608007a6c46f762bb5523473d27ea339
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
8 ;; Version: 6.01a
9 ;;
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, or (at your option)
15 ;; 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;; Commentary:
30 ;; This file contains the face definitons for Org.
32 ;;; Code:
34 (eval-when-compile (require 'cl))
35 (require 'org)
37 ;;; Column View
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 (defvar org-columns-current-fmt-compiled nil
45 "Local variable, holds the currently active column format.
46 This is the compiled version of the format.")
47 (defvar org-columns-current-widths nil
48 "Loval variable, holds the currently widths of fields.")
49 (defvar org-columns-current-maxwidths nil
50 "Loval variable, holds the currently active maximum column widths.")
51 (defvar org-columns-begin-marker (make-marker)
52 "Points to the position where last a column creation command was called.")
53 (defvar org-columns-top-level-marker (make-marker)
54 "Points to the position where current columns region starts.")
56 (defvar org-columns-map (make-sparse-keymap)
57 "The keymap valid in column display.")
59 (defun org-columns-content ()
60 "Switch to contents view while in columns view."
61 (interactive)
62 (org-overview)
63 (org-content))
65 (org-defkey org-columns-map "c" 'org-columns-content)
66 (org-defkey org-columns-map "o" 'org-overview)
67 (org-defkey org-columns-map "e" 'org-columns-edit-value)
68 (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
69 (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
70 (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
71 (org-defkey org-columns-map "v" 'org-columns-show-value)
72 (org-defkey org-columns-map "q" 'org-columns-quit)
73 (org-defkey org-columns-map "r" 'org-columns-redo)
74 (org-defkey org-columns-map "g" 'org-columns-redo)
75 (org-defkey org-columns-map [left] 'backward-char)
76 (org-defkey org-columns-map "\M-b" 'backward-char)
77 (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
78 (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
79 (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
80 (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
81 (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
82 (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
83 (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
84 (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
85 (org-defkey org-columns-map "<" 'org-columns-narrow)
86 (org-defkey org-columns-map ">" 'org-columns-widen)
87 (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
88 (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
89 (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
90 (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
92 (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
93 '("Column"
94 ["Edit property" org-columns-edit-value t]
95 ["Next allowed value" org-columns-next-allowed-value t]
96 ["Previous allowed value" org-columns-previous-allowed-value t]
97 ["Show full value" org-columns-show-value t]
98 ["Edit allowed values" org-columns-edit-allowed t]
99 "--"
100 ["Edit column attributes" org-columns-edit-attributes t]
101 ["Increase column width" org-columns-widen t]
102 ["Decrease column width" org-columns-narrow t]
103 "--"
104 ["Move column right" org-columns-move-right t]
105 ["Move column left" org-columns-move-left t]
106 ["Add column" org-columns-new t]
107 ["Delete column" org-columns-delete t]
108 "--"
109 ["CONTENTS" org-columns-content t]
110 ["OVERVIEW" org-overview t]
111 ["Refresh columns display" org-columns-redo t]
112 "--"
113 ["Open link" org-columns-open-link t]
114 "--"
115 ["Quit" org-columns-quit t]))
117 (defun org-columns-new-overlay (beg end &optional string face)
118 "Create a new column overlay and add it to the list."
119 (let ((ov (org-make-overlay beg end)))
120 (org-overlay-put ov 'face (or face 'secondary-selection))
121 (org-overlay-display ov string face)
122 (push ov org-columns-overlays)
123 ov))
125 (defun org-columns-display-here (&optional props)
126 "Overlay the current line with column display."
127 (interactive)
128 (let* ((fmt org-columns-current-fmt-compiled)
129 (beg (point-at-bol))
130 (level-face (save-excursion
131 (beginning-of-line 1)
132 (and (looking-at "\\(\\**\\)\\(\\* \\)")
133 (org-get-level-face 2))))
134 (color (list :foreground
135 (face-attribute (or level-face 'default) :foreground)))
136 props pom property ass width f string ov column val modval)
137 ;; Check if the entry is in another buffer.
138 (unless props
139 (if (eq major-mode 'org-agenda-mode)
140 (setq pom (or (get-text-property (point) 'org-hd-marker)
141 (get-text-property (point) 'org-marker))
142 props (if pom (org-entry-properties pom) nil))
143 (setq props (org-entry-properties nil))))
144 ;; Walk the format
145 (while (setq column (pop fmt))
146 (setq property (car column)
147 ass (if (equal property "ITEM")
148 (cons "ITEM"
149 (save-match-data
150 (org-no-properties
151 (org-remove-tabs
152 (buffer-substring-no-properties
153 (point-at-bol) (point-at-eol))))))
154 (assoc property props))
155 width (or (cdr (assoc property org-columns-current-maxwidths))
156 (nth 2 column)
157 (length property))
158 f (format "%%-%d.%ds | " width width)
159 val (or (cdr ass) "")
160 modval (if (equal property "ITEM")
161 (org-columns-cleanup-item val org-columns-current-fmt-compiled))
162 string (format f (or modval val)))
163 ;; Create the overlay
164 (org-unmodified
165 (setq ov (org-columns-new-overlay
166 beg (setq beg (1+ beg)) string
167 (list color 'org-column)))
168 (org-overlay-put ov 'keymap org-columns-map)
169 (org-overlay-put ov 'org-columns-key property)
170 (org-overlay-put ov 'org-columns-value (cdr ass))
171 (org-overlay-put ov 'org-columns-value-modified modval)
172 (org-overlay-put ov 'org-columns-pom pom)
173 (org-overlay-put ov 'org-columns-format f))
174 (if (or (not (char-after beg))
175 (equal (char-after beg) ?\n))
176 (let ((inhibit-read-only t))
177 (save-excursion
178 (goto-char beg)
179 (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
180 ;; Make the rest of the line disappear.
181 (org-unmodified
182 (setq ov (org-columns-new-overlay beg (point-at-eol)))
183 (org-overlay-put ov 'invisible t)
184 (org-overlay-put ov 'keymap org-columns-map)
185 (org-overlay-put ov 'intangible t)
186 (push ov org-columns-overlays)
187 (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
188 (org-overlay-put ov 'keymap org-columns-map)
189 (push ov org-columns-overlays)
190 (let ((inhibit-read-only t))
191 (put-text-property (max (point-min) (1- (point-at-bol)))
192 (min (point-max) (1+ (point-at-eol)))
193 'read-only "Type `e' to edit property")))))
195 (defvar org-columns-full-header-line-format nil
196 "Fthe full header line format, will be shifted by horizontal scrolling." )
197 (defvar org-previous-header-line-format nil
198 "The header line format before column view was turned on.")
199 (defvar org-columns-inhibit-recalculation nil
200 "Inhibit recomputing of columns on column view startup.")
203 (defvar header-line-format)
204 (defvar org-columns-previous-hscroll 0)
205 (defun org-columns-display-here-title ()
206 "Overlay the newline before the current line with the table title."
207 (interactive)
208 (let ((fmt org-columns-current-fmt-compiled)
209 string (title "")
210 property width f column str widths)
211 (while (setq column (pop fmt))
212 (setq property (car column)
213 str (or (nth 1 column) property)
214 width (or (cdr (assoc property org-columns-current-maxwidths))
215 (nth 2 column)
216 (length str))
217 widths (push width widths)
218 f (format "%%-%d.%ds | " width width)
219 string (format f str)
220 title (concat title string)))
221 (setq title (concat
222 (org-add-props " " nil 'display '(space :align-to 0))
223 (org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
224 (org-set-local 'org-previous-header-line-format header-line-format)
225 (org-set-local 'org-columns-current-widths (nreverse widths))
226 (setq org-columns-full-header-line-format title)
227 (setq org-columns-previous-hscroll -1)
228 ; (org-columns-hscoll-title)
229 (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
231 (defun org-columns-hscoll-title ()
232 "Set the header-line-format so that it scrolls along with the table."
233 (sit-for .0001) ; need to force a redisplay to update window-hscroll
234 (when (not (= (window-hscroll) org-columns-previous-hscroll))
235 (setq header-line-format
236 (concat (substring org-columns-full-header-line-format 0 1)
237 (substring org-columns-full-header-line-format
238 (1+ (window-hscroll))))
239 org-columns-previous-hscroll (window-hscroll))
240 (force-mode-line-update)))
242 (defun org-columns-remove-overlays ()
243 "Remove all currently active column overlays."
244 (interactive)
245 (when (marker-buffer org-columns-begin-marker)
246 (with-current-buffer (marker-buffer org-columns-begin-marker)
247 (when (local-variable-p 'org-previous-header-line-format)
248 (setq header-line-format org-previous-header-line-format)
249 (kill-local-variable 'org-previous-header-line-format)
250 (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
251 (move-marker org-columns-begin-marker nil)
252 (move-marker org-columns-top-level-marker nil)
253 (org-unmodified
254 (mapc 'org-delete-overlay org-columns-overlays)
255 (setq org-columns-overlays nil)
256 (let ((inhibit-read-only t))
257 (remove-text-properties (point-min) (point-max) '(read-only t)))))))
259 (defun org-columns-cleanup-item (item fmt)
260 "Remove from ITEM what is a column in the format FMT."
261 (if (not org-complex-heading-regexp)
262 item
263 (when (string-match org-complex-heading-regexp item)
264 (concat
265 (org-add-props (concat (match-string 1 item) " ") nil
266 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
267 (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
268 (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
269 " " (match-string 4 item)
270 (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
272 (defun org-columns-show-value ()
273 "Show the full value of the property."
274 (interactive)
275 (let ((value (get-char-property (point) 'org-columns-value)))
276 (message "Value is: %s" (or value ""))))
278 (defun org-columns-quit ()
279 "Remove the column overlays and in this way exit column editing."
280 (interactive)
281 (org-unmodified
282 (org-columns-remove-overlays)
283 (let ((inhibit-read-only t))
284 (remove-text-properties (point-min) (point-max) '(read-only t))))
285 (when (eq major-mode 'org-agenda-mode)
286 (message
287 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
289 (defun org-columns-check-computed ()
290 "Check if this column value is computed.
291 If yes, throw an error indicating that changing it does not make sense."
292 (let ((val (get-char-property (point) 'org-columns-value)))
293 (when (and (stringp val)
294 (get-char-property 0 'org-computed val))
295 (error "This value is computed from the entry's children"))))
297 (defun org-columns-todo (&optional arg)
298 "Change the TODO state during column view."
299 (interactive "P")
300 (org-columns-edit-value "TODO"))
302 (defun org-columns-set-tags-or-toggle (&optional arg)
303 "Toggle checkbox at point, or set tags for current headline."
304 (interactive "P")
305 (if (string-match "\\`\\[[ xX-]\\]\\'"
306 (get-char-property (point) 'org-columns-value))
307 (org-columns-next-allowed-value)
308 (org-columns-edit-value "TAGS")))
310 (defun org-columns-edit-value (&optional key)
311 "Edit the value of the property at point in column view.
312 Where possible, use the standard interface for changing this line."
313 (interactive)
314 (org-columns-check-computed)
315 (let* ((external-key key)
316 (col (current-column))
317 (key (or key (get-char-property (point) 'org-columns-key)))
318 (value (get-char-property (point) 'org-columns-value))
319 (bol (point-at-bol)) (eol (point-at-eol))
320 (pom (or (get-text-property bol 'org-hd-marker)
321 (point))) ; keep despite of compiler waring
322 (line-overlays
323 (delq nil (mapcar (lambda (x)
324 (and (eq (overlay-buffer x) (current-buffer))
325 (>= (overlay-start x) bol)
326 (<= (overlay-start x) eol)
328 org-columns-overlays)))
329 nval eval allowed)
330 (cond
331 ((equal key "CLOCKSUM")
332 (error "This special column cannot be edited"))
333 ((equal key "ITEM")
334 (setq eval '(org-with-point-at pom
335 (org-edit-headline))))
336 ((equal key "TODO")
337 (setq eval '(org-with-point-at pom
338 (let ((current-prefix-arg
339 (if external-key current-prefix-arg '(4))))
340 (call-interactively 'org-todo)))))
341 ((equal key "PRIORITY")
342 (setq eval '(org-with-point-at pom
343 (call-interactively 'org-priority))))
344 ((equal key "TAGS")
345 (setq eval '(org-with-point-at pom
346 (let ((org-fast-tag-selection-single-key
347 (if (eq org-fast-tag-selection-single-key 'expert)
348 t org-fast-tag-selection-single-key)))
349 (call-interactively 'org-set-tags)))))
350 ((equal key "DEADLINE")
351 (setq eval '(org-with-point-at pom
352 (call-interactively 'org-deadline))))
353 ((equal key "SCHEDULED")
354 (setq eval '(org-with-point-at pom
355 (call-interactively 'org-schedule))))
357 (setq allowed (org-property-get-allowed-values pom key 'table))
358 (if allowed
359 (setq nval (completing-read "Value: " allowed nil t))
360 (setq nval (read-string "Edit: " value)))
361 (setq nval (org-trim nval))
362 (when (not (equal nval value))
363 (setq eval '(org-entry-put pom key nval)))))
364 (when eval
365 (let ((inhibit-read-only t))
366 (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
367 (unwind-protect
368 (progn
369 (setq org-columns-overlays
370 (org-delete-all line-overlays org-columns-overlays))
371 (mapc 'org-delete-overlay line-overlays)
372 (org-columns-eval eval))
373 (org-columns-display-here))))
374 (move-to-column col)
375 (if (and (org-mode-p)
376 (nth 3 (assoc key org-columns-current-fmt-compiled)))
377 (org-columns-update key))))
379 (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
380 "Edit the current headline, the part without TODO keyword, TAGS."
381 (org-back-to-heading)
382 (when (looking-at org-todo-line-regexp)
383 (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
384 (txt (match-string 3))
385 (post "")
386 txt2)
387 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
388 (setq post (match-string 0 txt)
389 txt (substring txt 0 (match-beginning 0))))
390 (setq txt2 (read-string "Edit: " txt))
391 (when (not (equal txt txt2))
392 (beginning-of-line 1)
393 (insert pre txt2 post)
394 (delete-region (point) (point-at-eol))
395 (org-set-tags nil t)))))
397 (defun org-columns-edit-allowed ()
398 "Edit the list of allowed values for the current property."
399 (interactive)
400 (let* ((key (get-char-property (point) 'org-columns-key))
401 (key1 (concat key "_ALL"))
402 (allowed (org-entry-get (point) key1 t))
403 nval)
404 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
405 (setq nval (read-string "Allowed: " allowed))
406 (org-entry-put
407 (cond ((marker-position org-entry-property-inherited-from)
408 org-entry-property-inherited-from)
409 ((marker-position org-columns-top-level-marker)
410 org-columns-top-level-marker))
411 key1 nval)))
413 (defun org-columns-eval (form)
414 (let (hidep)
415 (save-excursion
416 (beginning-of-line 1)
417 ;; `next-line' is needed here, because it skips invisible line.
418 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
419 (setq hidep (org-on-heading-p 1)))
420 (eval form)
421 (and hidep (hide-entry))))
423 (defun org-columns-previous-allowed-value ()
424 "Switch to the previous allowed value for this column."
425 (interactive)
426 (org-columns-next-allowed-value t))
428 (defun org-columns-next-allowed-value (&optional previous)
429 "Switch to the next allowed value for this column."
430 (interactive)
431 (org-columns-check-computed)
432 (let* ((col (current-column))
433 (key (get-char-property (point) 'org-columns-key))
434 (value (get-char-property (point) 'org-columns-value))
435 (bol (point-at-bol)) (eol (point-at-eol))
436 (pom (or (get-text-property bol 'org-hd-marker)
437 (point))) ; keep despite of compiler waring
438 (line-overlays
439 (delq nil (mapcar (lambda (x)
440 (and (eq (overlay-buffer x) (current-buffer))
441 (>= (overlay-start x) bol)
442 (<= (overlay-start x) eol)
444 org-columns-overlays)))
445 (allowed (or (org-property-get-allowed-values pom key)
446 (and (memq
447 (nth 4 (assoc key org-columns-current-fmt-compiled))
448 '(checkbox checkbox-n-of-m checkbox-percent))
449 '("[ ]" "[X]"))))
450 nval)
451 (when (equal key "ITEM")
452 (error "Cannot edit item headline from here"))
453 (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
454 (error "Allowed values for this property have not been defined"))
455 (if (member key '("SCHEDULED" "DEADLINE"))
456 (setq nval (if previous 'earlier 'later))
457 (if previous (setq allowed (reverse allowed)))
458 (if (member value allowed)
459 (setq nval (car (cdr (member value allowed)))))
460 (setq nval (or nval (car allowed)))
461 (if (equal nval value)
462 (error "Only one allowed value for this property")))
463 (let ((inhibit-read-only t))
464 (remove-text-properties (1- bol) eol '(read-only t))
465 (unwind-protect
466 (progn
467 (setq org-columns-overlays
468 (org-delete-all line-overlays org-columns-overlays))
469 (mapc 'org-delete-overlay line-overlays)
470 (org-columns-eval '(org-entry-put pom key nval)))
471 (org-columns-display-here)))
472 (move-to-column col)
473 (if (and (org-mode-p)
474 (nth 3 (assoc key org-columns-current-fmt-compiled)))
475 (org-columns-update key))))
477 (defun org-verify-version (task)
478 (cond
479 ((eq task 'columns)
480 (if (or (featurep 'xemacs)
481 (< emacs-major-version 22))
482 (error "Emacs 22 is required for the columns feature")))))
484 (defun org-columns-open-link (&optional arg)
485 (interactive "P")
486 (let ((value (get-char-property (point) 'org-columns-value)))
487 (org-open-link-from-string value arg)))
489 (defun org-columns-get-format-and-top-level ()
490 (let (fmt)
491 (when (condition-case nil (org-back-to-heading) (error nil))
492 (move-marker org-entry-property-inherited-from nil)
493 (setq fmt (org-entry-get nil "COLUMNS" t)))
494 (setq fmt (or fmt org-columns-default-format))
495 (org-set-local 'org-columns-current-fmt fmt)
496 (org-columns-compile-format fmt)
497 (if (marker-position org-entry-property-inherited-from)
498 (move-marker org-columns-top-level-marker
499 org-entry-property-inherited-from)
500 (move-marker org-columns-top-level-marker (point)))
501 fmt))
503 (defun org-columns ()
504 "Turn on column view on an org-mode file."
505 (interactive)
506 (org-verify-version 'columns)
507 (org-columns-remove-overlays)
508 (move-marker org-columns-begin-marker (point))
509 (let (beg end fmt cache maxwidths)
510 (setq fmt (org-columns-get-format-and-top-level))
511 (save-excursion
512 (goto-char org-columns-top-level-marker)
513 (setq beg (point))
514 (unless org-columns-inhibit-recalculation
515 (org-columns-compute-all))
516 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
517 (point-max)))
518 ;; Get and cache the properties
519 (goto-char beg)
520 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
521 (save-excursion
522 (save-restriction
523 (narrow-to-region beg end)
524 (org-clock-sum))))
525 (while (re-search-forward (concat "^" outline-regexp) end t)
526 (push (cons (org-current-line) (org-entry-properties)) cache))
527 (when cache
528 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
529 (org-set-local 'org-columns-current-maxwidths maxwidths)
530 (org-columns-display-here-title)
531 (mapc (lambda (x)
532 (goto-line (car x))
533 (org-columns-display-here (cdr x)))
534 cache)))))
536 (defun org-columns-new (&optional prop title width op fmt &rest rest)
537 "Insert a new column, to the left of the current column."
538 (interactive)
539 (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
540 cell)
541 (setq prop (completing-read
542 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
543 nil nil prop))
544 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
545 (setq width (read-string "Column width: " (if width (number-to-string width))))
546 (if (string-match "\\S-" width)
547 (setq width (string-to-number width))
548 (setq width nil))
549 (setq fmt (completing-read "Summary [none]: "
550 '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
551 nil t))
552 (if (string-match "\\S-" fmt)
553 (setq fmt (intern fmt))
554 (setq fmt nil))
555 (if (eq fmt 'none) (setq fmt nil))
556 (if editp
557 (progn
558 (setcar editp prop)
559 (setcdr editp (list title width nil fmt)))
560 (setq cell (nthcdr (1- (current-column))
561 org-columns-current-fmt-compiled))
562 (setcdr cell (cons (list prop title width nil fmt)
563 (cdr cell))))
564 (org-columns-store-format)
565 (org-columns-redo)))
567 (defun org-columns-delete ()
568 "Delete the column at point from columns view."
569 (interactive)
570 (let* ((n (current-column))
571 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
572 (when (y-or-n-p
573 (format "Are you sure you want to remove column \"%s\"? " title))
574 (setq org-columns-current-fmt-compiled
575 (delq (nth n org-columns-current-fmt-compiled)
576 org-columns-current-fmt-compiled))
577 (org-columns-store-format)
578 (org-columns-redo)
579 (if (>= (current-column) (length org-columns-current-fmt-compiled))
580 (backward-char 1)))))
582 (defun org-columns-edit-attributes ()
583 "Edit the attributes of the current column."
584 (interactive)
585 (let* ((n (current-column))
586 (info (nth n org-columns-current-fmt-compiled)))
587 (apply 'org-columns-new info)))
589 (defun org-columns-widen (arg)
590 "Make the column wider by ARG characters."
591 (interactive "p")
592 (let* ((n (current-column))
593 (entry (nth n org-columns-current-fmt-compiled))
594 (width (or (nth 2 entry)
595 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
596 (setq width (max 1 (+ width arg)))
597 (setcar (nthcdr 2 entry) width)
598 (org-columns-store-format)
599 (org-columns-redo)))
601 (defun org-columns-narrow (arg)
602 "Make the column nrrower by ARG characters."
603 (interactive "p")
604 (org-columns-widen (- arg)))
606 (defun org-columns-move-right ()
607 "Swap this column with the one to the right."
608 (interactive)
609 (let* ((n (current-column))
610 (cell (nthcdr n org-columns-current-fmt-compiled))
612 (when (>= n (1- (length org-columns-current-fmt-compiled)))
613 (error "Cannot shift this column further to the right"))
614 (setq e (car cell))
615 (setcar cell (car (cdr cell)))
616 (setcdr cell (cons e (cdr (cdr cell))))
617 (org-columns-store-format)
618 (org-columns-redo)
619 (forward-char 1)))
621 (defun org-columns-move-left ()
622 "Swap this column with the one to the left."
623 (interactive)
624 (let* ((n (current-column)))
625 (when (= n 0)
626 (error "Cannot shift this column further to the left"))
627 (backward-char 1)
628 (org-columns-move-right)
629 (backward-char 1)))
631 (defun org-columns-store-format ()
632 "Store the text version of the current columns format in appropriate place.
633 This is either in the COLUMNS property of the node starting the current column
634 display, or in the #+COLUMNS line of the current buffer."
635 (let (fmt (cnt 0))
636 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
637 (org-set-local 'org-columns-current-fmt fmt)
638 (if (marker-position org-columns-top-level-marker)
639 (save-excursion
640 (goto-char org-columns-top-level-marker)
641 (if (and (org-at-heading-p)
642 (org-entry-get nil "COLUMNS"))
643 (org-entry-put nil "COLUMNS" fmt)
644 (goto-char (point-min))
645 ;; Overwrite all #+COLUMNS lines....
646 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
647 (setq cnt (1+ cnt))
648 (replace-match (concat "#+COLUMNS: " fmt) t t))
649 (unless (> cnt 0)
650 (goto-char (point-min))
651 (or (org-on-heading-p t) (outline-next-heading))
652 (let ((inhibit-read-only t))
653 (insert-before-markers "#+COLUMNS: " fmt "\n")))
654 (org-set-local 'org-columns-default-format fmt))))))
656 (defvar org-overriding-columns-format nil
657 "When set, overrides any other definition.")
658 (defvar org-agenda-view-columns-initially nil
659 "When set, switch to columns view immediately after creating the agenda.")
661 (defun org-agenda-columns ()
662 "Turn on column view in the agenda."
663 (interactive)
664 (org-verify-version 'columns)
665 (org-columns-remove-overlays)
666 (move-marker org-columns-begin-marker (point))
667 (let (fmt cache maxwidths m)
668 (cond
669 ((and (local-variable-p 'org-overriding-columns-format)
670 org-overriding-columns-format)
671 (setq fmt org-overriding-columns-format))
672 ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
673 (setq fmt (or (org-entry-get m "COLUMNS" t)
674 (with-current-buffer (marker-buffer m)
675 org-columns-default-format))))
676 ((and (boundp 'org-columns-current-fmt)
677 (local-variable-p 'org-columns-current-fmt)
678 org-columns-current-fmt)
679 (setq fmt org-columns-current-fmt))
680 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
681 (setq m (get-text-property m 'org-hd-marker))
682 (setq fmt (or (org-entry-get m "COLUMNS" t)
683 (with-current-buffer (marker-buffer m)
684 org-columns-default-format)))))
685 (setq fmt (or fmt org-columns-default-format))
686 (org-set-local 'org-columns-current-fmt fmt)
687 (org-columns-compile-format fmt)
688 (save-excursion
689 ;; Get and cache the properties
690 (goto-char (point-min))
691 (while (not (eobp))
692 (when (setq m (or (get-text-property (point) 'org-hd-marker)
693 (get-text-property (point) 'org-marker)))
694 (push (cons (org-current-line) (org-entry-properties m)) cache))
695 (beginning-of-line 2))
696 (when cache
697 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
698 (org-set-local 'org-columns-current-maxwidths maxwidths)
699 (org-columns-display-here-title)
700 (mapc (lambda (x)
701 (goto-line (car x))
702 (org-columns-display-here (cdr x)))
703 cache)))))
705 (defun org-columns-get-autowidth-alist (s cache)
706 "Derive the maximum column widths from the format and the cache."
707 (let ((start 0) rtn)
708 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
709 (push (cons (match-string 1 s) 1) rtn)
710 (setq start (match-end 0)))
711 (mapc (lambda (x)
712 (setcdr x (apply 'max
713 (mapcar
714 (lambda (y)
715 (length (or (cdr (assoc (car x) (cdr y))) " ")))
716 cache))))
717 rtn)
718 rtn))
720 (defun org-columns-compute-all ()
721 "Compute all columns that have operators defined."
722 (org-unmodified
723 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
724 (let ((columns org-columns-current-fmt-compiled) col)
725 (while (setq col (pop columns))
726 (when (nth 3 col)
727 (save-excursion
728 (org-columns-compute (car col)))))))
730 (defun org-columns-update (property)
731 "Recompute PROPERTY, and update the columns display for it."
732 (org-columns-compute property)
733 (let (fmt val pos)
734 (save-excursion
735 (mapc (lambda (ov)
736 (when (equal (org-overlay-get ov 'org-columns-key) property)
737 (setq pos (org-overlay-start ov))
738 (goto-char pos)
739 (when (setq val (cdr (assoc property
740 (get-text-property
741 (point-at-bol) 'org-summaries))))
742 (setq fmt (org-overlay-get ov 'org-columns-format))
743 (org-overlay-put ov 'org-columns-value val)
744 (org-overlay-put ov 'display (format fmt val)))))
745 org-columns-overlays))))
747 (defun org-columns-compute (property)
748 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
749 (interactive)
750 (let* ((re (concat "^" outline-regexp))
751 (lmax 30) ; Does anyone use deeper levels???
752 (lsum (make-vector lmax 0))
753 (lflag (make-vector lmax nil))
754 (level 0)
755 (ass (assoc property org-columns-current-fmt-compiled))
756 (format (nth 4 ass))
757 (printf (nth 5 ass))
758 (beg org-columns-top-level-marker)
759 last-level val valflag flag end sumpos sum-alist sum str str1 useval)
760 (save-excursion
761 ;; Find the region to compute
762 (goto-char beg)
763 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
764 (goto-char end)
765 ;; Walk the tree from the back and do the computations
766 (while (re-search-backward re beg t)
767 (setq sumpos (match-beginning 0)
768 last-level level
769 level (org-outline-level)
770 val (org-entry-get nil property)
771 valflag (and val (string-match "\\S-" val)))
772 (cond
773 ((< level last-level)
774 ;; put the sum of lower levels here as a property
775 (setq sum (aref lsum last-level) ; current sum
776 flag (aref lflag last-level) ; any valid entries from children?
777 str (org-columns-number-to-string sum format printf)
778 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
779 useval (if flag str1 (if valflag val ""))
780 sum-alist (get-text-property sumpos 'org-summaries))
781 (if (assoc property sum-alist)
782 (setcdr (assoc property sum-alist) useval)
783 (push (cons property useval) sum-alist)
784 (org-unmodified
785 (add-text-properties sumpos (1+ sumpos)
786 (list 'org-summaries sum-alist))))
787 (when val
788 (org-entry-put nil property (if flag str val)))
789 ;; add current to current level accumulator
790 (when (or flag valflag)
791 (aset lsum level (+ (aref lsum level)
792 (if flag sum (org-column-string-to-number
793 (if flag str val) format))))
794 (aset lflag level t))
795 ;; clear accumulators for deeper levels
796 (loop for l from (1+ level) to (1- lmax) do
797 (aset lsum l 0)
798 (aset lflag l nil)))
799 ((>= level last-level)
800 ;; add what we have here to the accumulator for this level
801 (aset lsum level (+ (aref lsum level)
802 (org-column-string-to-number (or val "0") format)))
803 (and valflag (aset lflag level t)))
804 (t (error "This should not happen")))))))
806 (defun org-columns-redo ()
807 "Construct the column display again."
808 (interactive)
809 (message "Recomputing columns...")
810 (save-excursion
811 (if (marker-position org-columns-begin-marker)
812 (goto-char org-columns-begin-marker))
813 (org-columns-remove-overlays)
814 (if (org-mode-p)
815 (call-interactively 'org-columns)
816 (call-interactively 'org-agenda-columns)))
817 (message "Recomputing columns...done"))
819 (defun org-columns-not-in-agenda ()
820 (if (eq major-mode 'org-agenda-mode)
821 (error "This command is only allowed in Org-mode buffers")))
824 (defun org-string-to-number (s)
825 "Convert string to number, and interpret hh:mm:ss."
826 (if (not (string-match ":" s))
827 (string-to-number s)
828 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
829 (while l
830 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
831 sum)))
833 (defun org-columns-number-to-string (n fmt &optional printf)
834 "Convert a computed column number to a string value, according to FMT."
835 (cond
836 ((eq fmt 'add_times)
837 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
838 (format "%d:%02d" h m)))
839 ((eq fmt 'checkbox)
840 (cond ((= n (floor n)) "[X]")
841 ((> n 1.) "[-]")
842 (t "[ ]")))
843 ((memq fmt '(checkbox-n-of-m checkbox-percent))
844 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
845 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
846 (printf (format printf n))
847 ((eq fmt 'currency)
848 (format "%.2f" n))
849 (t (number-to-string n))))
851 (defun org-nofm-to-completion (n m &optional percent)
852 (if (not percent)
853 (format "[%d/%d]" n m)
854 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
856 (defun org-column-string-to-number (s fmt)
857 "Convert a column value to a number that can be used for column computing."
858 (cond
859 ((string-match ":" s)
860 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
861 (while l
862 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
863 sum))
864 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
865 (if (equal s "[X]") 1. 0.000001))
866 (t (string-to-number s))))
868 (defun org-columns-uncompile-format (cfmt)
869 "Turn the compiled columns format back into a string representation."
870 (let ((rtn "") e s prop title op width fmt printf)
871 (while (setq e (pop cfmt))
872 (setq prop (car e)
873 title (nth 1 e)
874 width (nth 2 e)
875 op (nth 3 e)
876 fmt (nth 4 e)
877 printf (nth 5 e))
878 (cond
879 ((eq fmt 'add_times) (setq op ":"))
880 ((eq fmt 'checkbox) (setq op "X"))
881 ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
882 ((eq fmt 'checkbox-percent) (setq op "X%"))
883 ((eq fmt 'add_numbers) (setq op "+"))
884 ((eq fmt 'currency) (setq op "$")))
885 (if (and op printf) (setq op (concat op ";" printf)))
886 (if (equal title prop) (setq title nil))
887 (setq s (concat "%" (if width (number-to-string width))
888 prop
889 (if title (concat "(" title ")"))
890 (if op (concat "{" op "}"))))
891 (setq rtn (concat rtn " " s)))
892 (org-trim rtn)))
894 (defun org-columns-compile-format (fmt)
895 "Turn a column format string into an alist of specifications.
896 The alist has one entry for each column in the format. The elements of
897 that list are:
898 property the property
899 title the title field for the columns
900 width the column width in characters, can be nil for automatic
901 operator the operator if any
902 format the output format for computed results, derived from operator
903 printf a printf format for computed values"
904 (let ((start 0) width prop title op f printf)
905 (setq org-columns-current-fmt-compiled nil)
906 (while (string-match
907 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
908 fmt start)
909 (setq start (match-end 0)
910 width (match-string 1 fmt)
911 prop (match-string 2 fmt)
912 title (or (match-string 3 fmt) prop)
913 op (match-string 4 fmt)
914 f nil
915 printf nil)
916 (if width (setq width (string-to-number width)))
917 (when (and op (string-match ";" op))
918 (setq printf (substring op (match-end 0))
919 op (substring op 0 (match-beginning 0))))
920 (cond
921 ((equal op "+") (setq f 'add_numbers))
922 ((equal op "$") (setq f 'currency))
923 ((equal op ":") (setq f 'add_times))
924 ((equal op "X") (setq f 'checkbox))
925 ((equal op "X/") (setq f 'checkbox-n-of-m))
926 ((equal op "X%") (setq f 'checkbox-percent))
928 (push (list prop title width op f printf) org-columns-current-fmt-compiled))
929 (setq org-columns-current-fmt-compiled
930 (nreverse org-columns-current-fmt-compiled))))
933 ;;; Dynamic block for Column view
935 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
936 "Get the column view of the current buffer or subtree.
937 The first optional argument MAXLEVEL sets the level limit. A
938 second optional argument SKIP-EMPTY-ROWS tells whether to skip
939 empty rows, an empty row being one where all the column view
940 specifiers except ITEM are empty. This function returns a list
941 containing the title row and all other rows. Each row is a list
942 of fields."
943 (save-excursion
944 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
945 (n (length title)) row tbl)
946 (goto-char (point-min))
947 (while (and (re-search-forward "^\\(\\*+\\) " nil t)
948 (or (null maxlevel)
949 (>= maxlevel
950 (if org-odd-levels-only
951 (/ (1+ (length (match-string 1))) 2)
952 (length (match-string 1))))))
953 (when (get-char-property (match-beginning 0) 'org-columns-key)
954 (setq row nil)
955 (loop for i from 0 to (1- n) do
956 (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
957 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
959 row))
960 (setq row (nreverse row))
961 (unless (and skip-empty-rows
962 (eq 1 (length (delete "" (delete-dups row)))))
963 (push row tbl))))
964 (append (list title 'hline) (nreverse tbl)))))
966 (defun org-dblock-write:columnview (params)
967 "Write the column view table.
968 PARAMS is a property list of parameters:
970 :width enforce same column widths with <N> specifiers.
971 :id the :ID: property of the entry where the columns view
972 should be built, as a string. When `local', call locally.
973 When `global' call column view with the cursor at the beginning
974 of the buffer (usually this means that the whole buffer switches
975 to column view).
976 :hlines When t, insert a hline before each item. When a number, insert
977 a hline before each level <= that number.
978 :vlines When t, make each column a colgroup to enforce vertical lines.
979 :maxlevel When set to a number, don't capture headlines below this level.
980 :skip-empty-rows
981 When t, skip rows where all specifiers other than ITEM are empty."
982 (let ((pos (move-marker (make-marker) (point)))
983 (hlines (plist-get params :hlines))
984 (vlines (plist-get params :vlines))
985 (maxlevel (plist-get params :maxlevel))
986 (skip-empty-rows (plist-get params :skip-empty-rows))
987 tbl id idpos nfields tmp)
988 (save-excursion
989 (save-restriction
990 (when (setq id (plist-get params :id))
991 (cond ((not id) nil)
992 ((eq id 'global) (goto-char (point-min)))
993 ((eq id 'local) nil)
994 ((setq idpos (org-find-entry-with-id id))
995 (goto-char idpos))
996 (t (error "Cannot find entry with :ID: %s" id))))
997 (org-columns)
998 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
999 (setq nfields (length (car tbl)))
1000 (org-columns-quit)))
1001 (goto-char pos)
1002 (move-marker pos nil)
1003 (when tbl
1004 (when (plist-get params :hlines)
1005 (setq tmp nil)
1006 (while tbl
1007 (if (eq (car tbl) 'hline)
1008 (push (pop tbl) tmp)
1009 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1010 (if (and (not (eq (car tmp) 'hline))
1011 (or (eq hlines t)
1012 (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
1013 (push 'hline tmp)))
1014 (push (pop tbl) tmp)))
1015 (setq tbl (nreverse tmp)))
1016 (when vlines
1017 (setq tbl (mapcar (lambda (x)
1018 (if (eq 'hline x) x (cons "" x)))
1019 tbl))
1020 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1021 (setq pos (point))
1022 (insert (org-listtable-to-string tbl))
1023 (when (plist-get params :width)
1024 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1025 org-columns-current-widths "|")))
1026 (goto-char pos)
1027 (org-table-align))))
1029 (defun org-listtable-to-string (tbl)
1030 "Convert a listtable TBL to a string that contains the Org-mode table.
1031 The table still need to be alligned. The resulting string has no leading
1032 and tailing newline characters."
1033 (mapconcat
1034 (lambda (x)
1035 (cond
1036 ((listp x)
1037 (concat "|" (mapconcat 'identity x "|") "|"))
1038 ((eq x 'hline) "|-|")
1039 (t (error "Garbage in listtable: %s" x))))
1040 tbl "\n"))
1042 (defun org-insert-columns-dblock ()
1043 "Create a dynamic block capturing a column view table."
1044 (interactive)
1045 (let ((defaults '(:name "columnview" :hlines 1))
1046 (id (completing-read
1047 "Capture columns (local, global, entry with :ID: property) [local]: "
1048 (append '(("global") ("local"))
1049 (mapcar 'list (org-property-values "ID"))))))
1050 (if (equal id "") (setq id 'local))
1051 (if (equal id "global") (setq id 'global))
1052 (setq defaults (append defaults (list :id id)))
1053 (org-create-dblock defaults)
1054 (org-update-dblock)))
1056 (provide 'org-colview)
1058 ;;; org-colview.el ends here