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
))
37 (defvar org-columns-overlays nil
38 "Holds the list of current column overlays.")
40 (defvar org-columns-current-fmt nil
41 "Local variable, holds the currently active column format.")
42 (make-variable-buffer-local 'org-columns-current-fmt
)
43 (defvar org-columns-current-fmt-compiled nil
44 "Local variable, holds the currently active column format.
45 This is the compiled version of the format.")
46 (make-variable-buffer-local 'org-columns-current-fmt-compiled
)
47 (defvar org-columns-current-widths nil
48 "Loval variable, holds the currently widths of fields.")
49 (make-variable-buffer-local 'org-columns-current-widths
)
50 (defvar org-columns-current-maxwidths nil
51 "Loval variable, holds the currently active maximum column widths.")
52 (make-variable-buffer-local 'org-columns-current-maxwidths
)
53 (defvar org-columns-begin-marker
(make-marker)
54 "Points to the position where last a column creation command was called.")
55 (defvar org-columns-top-level-marker
(make-marker)
56 "Points to the position where current columns region starts.")
58 (defvar org-columns-map
(make-sparse-keymap)
59 "The keymap valid in column display.")
61 (defun org-columns-content ()
62 "Switch to contents view while in columns view."
67 (org-defkey org-columns-map
"c" 'org-columns-content
)
68 (org-defkey org-columns-map
"o" 'org-overview
)
69 (org-defkey org-columns-map
"e" 'org-columns-edit-value
)
70 (org-defkey org-columns-map
"\C-c\C-t" 'org-columns-todo
)
71 (org-defkey org-columns-map
"\C-c\C-c" 'org-columns-set-tags-or-toggle
)
72 (org-defkey org-columns-map
"\C-c\C-o" 'org-columns-open-link
)
73 (org-defkey org-columns-map
"v" 'org-columns-show-value
)
74 (org-defkey org-columns-map
"q" 'org-columns-quit
)
75 (org-defkey org-columns-map
"r" 'org-columns-redo
)
76 (org-defkey org-columns-map
"g" 'org-columns-redo
)
77 (org-defkey org-columns-map
[left] 'backward-char)
78 (org-defkey org-columns-map "\M-b" 'backward-char)
79 (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
80 (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
81 (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
82 (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
83 (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
84 (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
85 (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
86 (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
87 (org-defkey org-columns-map "<" 'org-columns-narrow)
88 (org-defkey org-columns-map ">" 'org-columns-widen)
89 (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
90 (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
91 (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
92 (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
94 (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
96 ["Edit property" org-columns-edit-value t]
97 ["Next allowed value" org-columns-next-allowed-value t]
98 ["Previous allowed value" org-columns-previous-allowed-value t]
99 ["Show full value" org-columns-show-value t]
100 ["Edit allowed values" org-columns-edit-allowed t]
102 ["Edit column attributes" org-columns-edit-attributes t]
103 ["Increase column width" org-columns-widen t]
104 ["Decrease column width" org-columns-narrow t]
106 ["Move column right" org-columns-move-right t]
107 ["Move column left" org-columns-move-left t]
108 ["Add column" org-columns-new t]
109 ["Delete column" org-columns-delete t]
111 ["CONTENTS" org-columns-content t]
112 ["OVERVIEW" org-overview t]
113 ["Refresh columns display" org-columns-redo t]
115 ["Open link" org-columns-open-link t]
117 ["Quit" org-columns-quit t]))
119 (defun org-columns-new-overlay (beg end &optional string face)
120 "Create a new column overlay and add it to the list."
121 (let ((ov (org-make-overlay beg end)))
122 (org-overlay-put ov 'face (or face 'secondary-selection))
123 (org-overlay-display ov string face)
124 (push ov org-columns-overlays)
127 (defun org-columns-display-here (&optional props)
128 "Overlay the current line with column display."
130 (let* ((fmt org-columns-current-fmt-compiled)
132 (level-face (save-excursion
133 (beginning-of-line 1)
134 (and (looking-at "\\(\\**\\)\\(\\* \\)")
135 (org-get-level-face 2))))
136 (ref-face (or level-face
137 (and (eq major-mode 'org-agenda-mode)
138 (get-text-property (point-at-bol) 'face))
140 (color (list :foreground
141 (face-attribute ref-face :foreground)
142 :weight 'normal :strike-through nil
144 (face (list color 'org-column level-face))
145 pom property ass width f string ov column val modval)
146 ;; Check if the entry is in another buffer.
148 (if (eq major-mode 'org-agenda-mode)
149 (setq pom (or (get-text-property (point) 'org-hd-marker)
150 (get-text-property (point) 'org-marker))
151 props (if pom (org-entry-properties pom) nil))
152 (setq props (org-entry-properties nil))))
154 (while (setq column (pop fmt))
155 (setq property (car column)
156 ass (if (equal property "ITEM")
161 (buffer-substring-no-properties
162 (point-at-bol) (point-at-eol))))))
163 (assoc property props))
164 width (or (cdr (assoc property org-columns-current-maxwidths))
167 f (format "%%-%d.%ds | " width width)
168 val (or (cdr ass) "")
169 modval (if (equal property "ITEM")
170 (org-columns-cleanup-item val org-columns-current-fmt-compiled))
171 string (format f (or modval val)))
172 ;; Create the overlay
174 (setq ov (org-columns-new-overlay
175 beg (setq beg (1+ beg)) string face))
176 (org-overlay-put ov 'keymap org-columns-map)
177 (org-overlay-put ov 'org-columns-key property)
178 (org-overlay-put ov 'org-columns-value (cdr ass))
179 (org-overlay-put ov 'org-columns-value-modified modval)
180 (org-overlay-put ov 'org-columns-pom pom)
181 (org-overlay-put ov 'org-columns-format f))
182 (if (or (not (char-after beg))
183 (equal (char-after beg) ?\n))
184 (let ((inhibit-read-only t))
187 (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
188 ;; Make the rest of the line disappear.
190 (setq ov (org-columns-new-overlay beg (point-at-eol)))
191 (org-overlay-put ov 'invisible t)
192 (org-overlay-put ov 'keymap org-columns-map)
193 (org-overlay-put ov 'intangible t)
194 (push ov org-columns-overlays)
195 (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
196 (org-overlay-put ov 'keymap org-columns-map)
197 (push ov org-columns-overlays)
198 (let ((inhibit-read-only t))
199 (put-text-property (max (point-min) (1- (point-at-bol)))
200 (min (point-max) (1+ (point-at-eol)))
201 'read-only "Type `e' to edit property")))))
203 (defvar org-columns-full-header-line-format nil
204 "Fthe full header line format, will be shifted by horizontal scrolling." )
205 (defvar org-previous-header-line-format nil
206 "The header line format before column view was turned on.")
207 (defvar org-columns-inhibit-recalculation nil
208 "Inhibit recomputing of columns on column view startup.")
209 (defvar org-columns-flyspell-was-active nil
210 "Remember the state of `flyspell-mode' before column view.
211 Flyspell-mode can cause problems in columns view, so it is turned off
212 for the duration of the command.")
214 (defvar header-line-format)
215 (defvar org-columns-previous-hscroll 0)
216 (defun org-columns-display-here-title ()
217 "Overlay the newline before the current line with the table title."
219 (let ((fmt org-columns-current-fmt-compiled)
221 property width f column str widths)
222 (while (setq column (pop fmt))
223 (setq property (car column)
224 str (or (nth 1 column) property)
225 width (or (cdr (assoc property org-columns-current-maxwidths))
228 widths (push width widths)
229 f (format "%%-%d.%ds | " width width)
230 string (format f str)
231 title (concat title string)))
233 (org-add-props " " nil 'display '(space :align-to 0))
234 ;;(org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
235 (org-add-props title nil 'face 'org-column-title)))
236 (org-set-local 'org-previous-header-line-format header-line-format)
237 (org-set-local 'org-columns-current-widths (nreverse widths))
238 (setq org-columns-full-header-line-format title)
239 (setq org-columns-previous-hscroll -1)
240 ; (org-columns-hscoll-title)
241 (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
243 (defun org-columns-hscoll-title ()
244 "Set the header-line-format so that it scrolls along with the table."
245 (sit-for .0001) ; need to force a redisplay to update window-hscroll
246 (when (not (= (window-hscroll) org-columns-previous-hscroll))
247 (setq header-line-format
248 (concat (substring org-columns-full-header-line-format 0 1)
249 (substring org-columns-full-header-line-format
250 (1+ (window-hscroll))))
251 org-columns-previous-hscroll (window-hscroll))
252 (force-mode-line-update)))
254 (defun org-columns-remove-overlays ()
255 "Remove all currently active column overlays."
257 (when (marker-buffer org-columns-begin-marker)
258 (with-current-buffer (marker-buffer org-columns-begin-marker)
259 (when (local-variable-p 'org-previous-header-line-format)
260 (setq header-line-format org-previous-header-line-format)
261 (kill-local-variable 'org-previous-header-line-format)
262 (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
263 (move-marker org-columns-begin-marker nil)
264 (move-marker org-columns-top-level-marker nil)
266 (mapc 'org-delete-overlay org-columns-overlays)
267 (setq org-columns-overlays nil)
268 (let ((inhibit-read-only t))
269 (remove-text-properties (point-min) (point-max) '(read-only t))))
270 (when org-columns-flyspell-was-active
271 (flyspell-mode 1)))))
273 (defun org-columns-cleanup-item (item fmt)
274 "Remove from ITEM what is a column in the format FMT."
275 (if (not org-complex-heading-regexp)
277 (when (string-match org-complex-heading-regexp item)
279 (org-add-props (concat (match-string 1 item) " ") nil
280 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
281 (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
282 (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
283 " " (match-string 4 item)
284 (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
286 (defun org-columns-show-value ()
287 "Show the full value of the property."
289 (let ((value (get-char-property (point) 'org-columns-value)))
290 (message "Value is: %s" (or value ""))))
292 (defvar org-agenda-columns-active) ;; defined in org-agenda.el
293 (defun org-columns-quit ()
294 "Remove the column overlays and in this way exit column editing."
297 (org-columns-remove-overlays)
298 (let ((inhibit-read-only t))
299 (remove-text-properties (point-min) (point-max) '(read-only t))))
300 (when (eq major-mode 'org-agenda-mode)
301 (setq org-agenda-columns-active nil)
303 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
305 (defun org-columns-check-computed ()
306 "Check if this column value is computed.
307 If yes, throw an error indicating that changing it does not make sense."
308 (let ((val (get-char-property (point) 'org-columns-value)))
309 (when (and (stringp val)
310 (get-char-property 0 'org-computed val))
311 (error "This value is computed from the entry's children"))))
313 (defun org-columns-todo (&optional arg)
314 "Change the TODO state during column view."
316 (org-columns-edit-value "TODO"))
318 (defun org-columns-set-tags-or-toggle (&optional arg)
319 "Toggle checkbox at point, or set tags for current headline."
321 (if (string-match "\\`\\[[ xX-]\\]\\'"
322 (get-char-property (point) 'org-columns-value))
323 (org-columns-next-allowed-value)
324 (org-columns-edit-value "TAGS")))
326 (defun org-columns-edit-value (&optional key)
327 "Edit the value of the property at point in column view.
328 Where possible, use the standard interface for changing this line."
330 (org-columns-check-computed)
331 (let* ((external-key key)
332 (col (current-column))
333 (key (or key (get-char-property (point) 'org-columns-key)))
334 (value (get-char-property (point) 'org-columns-value))
335 (bol (point-at-bol)) (eol (point-at-eol))
336 (pom (or (get-text-property bol 'org-hd-marker)
337 (point))) ; keep despite of compiler waring
339 (delq nil (mapcar (lambda (x)
340 (and (eq (overlay-buffer x) (current-buffer))
341 (>= (overlay-start x) bol)
342 (<= (overlay-start x) eol)
344 org-columns-overlays)))
347 ((equal key "CLOCKSUM")
348 (error "This special column cannot be edited"))
350 (setq eval '(org-with-point-at pom
351 (org-edit-headline))))
353 (setq eval '(org-with-point-at pom
354 (let ((current-prefix-arg
355 (if external-key current-prefix-arg '(4))))
356 (call-interactively 'org-todo)))))
357 ((equal key "PRIORITY")
358 (setq eval '(org-with-point-at pom
359 (call-interactively 'org-priority))))
361 (setq eval '(org-with-point-at pom
362 (let ((org-fast-tag-selection-single-key
363 (if (eq org-fast-tag-selection-single-key 'expert)
364 t org-fast-tag-selection-single-key)))
365 (call-interactively 'org-set-tags)))))
366 ((equal key "DEADLINE")
367 (setq eval '(org-with-point-at pom
368 (call-interactively 'org-deadline))))
369 ((equal key "SCHEDULED")
370 (setq eval '(org-with-point-at pom
371 (call-interactively 'org-schedule))))
373 (setq allowed (org-property-get-allowed-values pom key 'table))
375 (setq nval (completing-read "Value: " allowed nil t))
376 (setq nval (read-string "Edit: " value)))
377 (setq nval (org-trim nval))
378 (when (not (equal nval value))
379 (setq eval '(org-entry-put pom key nval)))))
383 ((equal major-mode 'org-agenda-mode)
384 (org-columns-eval '(org-entry-put pom key nval))
385 ;; The following let preserves the current format, and makes sure
386 ;; that in only a single file things need to be upated.
387 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
388 (buffer (marker-buffer pom))
389 (org-agenda-contributing-files
390 (list (with-current-buffer buffer
391 (buffer-file-name (buffer-base-buffer))))))
392 (org-agenda-columns)))
394 (let ((inhibit-read-only t))
396 (remove-text-properties
397 (max (point-min) (1- bol)) eol '(read-only t)))
400 (setq org-columns-overlays
401 (org-delete-all line-overlays org-columns-overlays))
402 (mapc 'org-delete-overlay line-overlays)
403 (org-columns-eval eval))
404 (org-columns-display-here)))
405 (org-move-to-column col)
406 (if (and (org-mode-p)
407 (nth 3 (assoc key org-columns-current-fmt-compiled)))
408 (org-columns-update key)))))))
410 (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
411 "Edit the current headline, the part without TODO keyword, TAGS."
412 (org-back-to-heading)
413 (when (looking-at org-todo-line-regexp)
414 (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
415 (txt (match-string 3))
418 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
419 (setq post (match-string 0 txt)
420 txt (substring txt 0 (match-beginning 0))))
421 (setq txt2 (read-string "Edit: " txt))
422 (when (not (equal txt txt2))
423 (beginning-of-line 1)
424 (insert pre txt2 post)
425 (delete-region (point) (point-at-eol))
426 (org-set-tags nil t)))))
428 (defun org-columns-edit-allowed ()
429 "Edit the list of allowed values for the current property."
431 (let* ((pom (or (get-text-property (point-at-bol) 'org-marker)
432 (get-text-property (point-at-bol) 'org-hd-marker)
434 (key (get-char-property (point) 'org-columns-key))
435 (key1 (concat key "_ALL"))
436 (allowed (org-entry-get pom key1 t))
438 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
439 ;; FIXME: Write back to #+PROPERTY setting if that is needed.
440 (setq nval (read-string "Allowed: " allowed))
442 (cond ((marker-position org-entry-property-inherited-from)
443 org-entry-property-inherited-from)
444 ((marker-position org-columns-top-level-marker)
445 org-columns-top-level-marker)
449 (defun org-columns-eval (form)
452 (beginning-of-line 1)
453 ;; `next-line' is needed here, because it skips invisible line.
454 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
455 (setq hidep (org-on-heading-p 1)))
457 (and hidep (hide-entry))))
459 (defun org-columns-previous-allowed-value ()
460 "Switch to the previous allowed value for this column."
462 (org-columns-next-allowed-value t))
464 (defun org-columns-next-allowed-value (&optional previous)
465 "Switch to the next allowed value for this column."
467 (org-columns-check-computed)
468 (let* ((col (current-column))
469 (key (get-char-property (point) 'org-columns-key))
470 (value (get-char-property (point) 'org-columns-value))
471 (bol (point-at-bol)) (eol (point-at-eol))
472 (pom (or (get-text-property bol 'org-hd-marker)
473 (point))) ; keep despite of compiler waring
475 (delq nil (mapcar (lambda (x)
476 (and (eq (overlay-buffer x) (current-buffer))
477 (>= (overlay-start x) bol)
478 (<= (overlay-start x) eol)
480 org-columns-overlays)))
481 (allowed (or (org-property-get-allowed-values pom key)
483 (nth 4 (assoc key org-columns-current-fmt-compiled))
484 '(checkbox checkbox-n-of-m checkbox-percent))
487 (when (equal key "ITEM")
488 (error "Cannot edit item headline from here"))
489 (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
490 (error "Allowed values for this property have not been defined"))
491 (if (member key '("SCHEDULED" "DEADLINE"))
492 (setq nval (if previous 'earlier 'later))
493 (if previous (setq allowed (reverse allowed)))
494 (if (member value allowed)
495 (setq nval (car (cdr (member value allowed)))))
496 (setq nval (or nval (car allowed)))
497 (if (equal nval value)
498 (error "Only one allowed value for this property")))
500 ((equal major-mode 'org-agenda-mode)
501 (org-columns-eval '(org-entry-put pom key nval))
502 ;; The following let preserves the current format, and makes sure
503 ;; that in only a single file things need to be upated.
504 (let* ((org-agenda-overriding-columns-format org-columns-current-fmt)
505 (buffer (marker-buffer pom))
506 (org-agenda-contributing-files
507 (list (with-current-buffer buffer
508 (buffer-file-name (buffer-base-buffer))))))
509 (org-agenda-columns)))
511 (let ((inhibit-read-only t))
512 (remove-text-properties (1- bol) eol '(read-only t))
515 (setq org-columns-overlays
516 (org-delete-all line-overlays org-columns-overlays))
517 (mapc 'org-delete-overlay line-overlays)
518 (org-columns-eval '(org-entry-put pom key nval)))
519 (org-columns-display-here)))
520 (org-move-to-column col)
521 (and (nth 3 (assoc key org-columns-current-fmt-compiled))
522 (org-columns-update key))))))
524 (defun org-verify-version (task)
527 (if (or (featurep 'xemacs)
528 (< emacs-major-version 22))
529 (error "Emacs 22 is required for the columns feature")))))
531 (defun org-columns-open-link (&optional arg)
533 (let ((value (get-char-property (point) 'org-columns-value)))
534 (org-open-link-from-string value arg)))
536 (defun org-columns-get-format-and-top-level ()
538 (when (condition-case nil (org-back-to-heading) (error nil))
539 (move-marker org-entry-property-inherited-from nil)
540 (setq fmt (org-entry-get nil "COLUMNS" t)))
541 (setq fmt (or fmt org-columns-default-format))
542 (org-set-local 'org-columns-current-fmt fmt)
543 (org-columns-compile-format fmt)
544 (if (marker-position org-entry-property-inherited-from)
545 (move-marker org-columns-top-level-marker
546 org-entry-property-inherited-from)
547 (move-marker org-columns-top-level-marker (point)))
550 (defun org-columns ()
551 "Turn on column view on an org-mode file."
553 (org-verify-version 'columns)
554 (org-columns-remove-overlays)
555 (move-marker org-columns-begin-marker (point))
556 (let (beg end fmt cache maxwidths)
557 (setq fmt (org-columns-get-format-and-top-level))
559 (goto-char org-columns-top-level-marker)
561 (unless org-columns-inhibit-recalculation
562 (org-columns-compute-all))
563 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
565 ;; Get and cache the properties
567 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
570 (narrow-to-region beg end)
572 (while (re-search-forward (concat "^" outline-regexp) end t)
573 (push (cons (org-current-line) (org-entry-properties)) cache))
575 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
576 (org-set-local 'org-columns-current-maxwidths maxwidths)
577 (org-columns-display-here-title)
578 (when (org-set-local 'org-columns-flyspell-was-active
579 (org-bound-and-true-p flyspell-mode))
583 (org-columns-display-here (cdr x)))
586 (defun org-columns-new (&optional prop title width op fmt &rest rest)
587 "Insert a new column, to the left of the current column."
589 (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
591 (setq prop (completing-read
592 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
594 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
595 (setq width (read-string "Column width: " (if width (number-to-string width))))
596 (if (string-match "\\S-" width)
597 (setq width (string-to-number width))
599 (setq fmt (completing-read "Summary [none]: "
600 '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
602 (if (string-match "\\S-" fmt)
603 (setq fmt (intern fmt))
605 (if (eq fmt 'none) (setq fmt nil))
609 (setcdr editp (list title width nil fmt)))
610 (setq cell (nthcdr (1- (current-column))
611 org-columns-current-fmt-compiled))
612 (setcdr cell (cons (list prop title width nil fmt)
614 (org-columns-store-format)
617 (defun org-columns-delete ()
618 "Delete the column at point from columns view."
620 (let* ((n (current-column))
621 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
623 (format "Are you sure you want to remove column \"%s\"? " title))
624 (setq org-columns-current-fmt-compiled
625 (delq (nth n org-columns-current-fmt-compiled)
626 org-columns-current-fmt-compiled))
627 (org-columns-store-format)
629 (if (>= (current-column) (length org-columns-current-fmt-compiled))
630 (backward-char 1)))))
632 (defun org-columns-edit-attributes ()
633 "Edit the attributes of the current column."
635 (let* ((n (current-column))
636 (info (nth n org-columns-current-fmt-compiled)))
637 (apply 'org-columns-new info)))
639 (defun org-columns-widen (arg)
640 "Make the column wider by ARG characters."
642 (let* ((n (current-column))
643 (entry (nth n org-columns-current-fmt-compiled))
644 (width (or (nth 2 entry)
645 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
646 (setq width (max 1 (+ width arg)))
647 (setcar (nthcdr 2 entry) width)
648 (org-columns-store-format)
651 (defun org-columns-narrow (arg)
652 "Make the column nrrower by ARG characters."
654 (org-columns-widen (- arg)))
656 (defun org-columns-move-right ()
657 "Swap this column with the one to the right."
659 (let* ((n (current-column))
660 (cell (nthcdr n org-columns-current-fmt-compiled))
662 (when (>= n (1- (length org-columns-current-fmt-compiled)))
663 (error "Cannot shift this column further to the right"))
665 (setcar cell (car (cdr cell)))
666 (setcdr cell (cons e (cdr (cdr cell))))
667 (org-columns-store-format)
671 (defun org-columns-move-left ()
672 "Swap this column with the one to the left."
674 (let* ((n (current-column)))
676 (error "Cannot shift this column further to the left"))
678 (org-columns-move-right)
681 (defun org-columns-store-format ()
682 "Store the text version of the current columns format in appropriate place.
683 This is either in the COLUMNS property of the node starting the current column
684 display, or in the #+COLUMNS line of the current buffer."
686 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
687 (org-set-local 'org-columns-current-fmt fmt)
688 (if (marker-position org-columns-top-level-marker)
690 (goto-char org-columns-top-level-marker)
691 (if (and (org-at-heading-p)
692 (org-entry-get nil "COLUMNS"))
693 (org-entry-put nil "COLUMNS" fmt)
694 (goto-char (point-min))
695 ;; Overwrite all #+COLUMNS lines....
696 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
698 (replace-match (concat "#+COLUMNS: " fmt) t t))
700 (goto-char (point-min))
701 (or (org-on-heading-p t) (outline-next-heading))
702 (let ((inhibit-read-only t))
703 (insert-before-markers "#+COLUMNS: " fmt "\n")))
704 (org-set-local 'org-columns-default-format fmt))))))
706 (defvar org-agenda-overriding-columns-format nil
707 "When set, overrides any other format definition for the agenda.
708 Don't set this, this is meant for dynamic scoping.")
710 (defun org-columns-get-autowidth-alist (s cache)
711 "Derive the maximum column widths from the format and the cache."
713 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
714 (push (cons (match-string 1 s) 1) rtn)
715 (setq start (match-end 0)))
717 (setcdr x (apply 'max
720 (length (or (cdr (assoc (car x) (cdr y))) " ")))
725 (defun org-columns-compute-all ()
726 "Compute all columns that have operators defined."
728 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
729 (let ((columns org-columns-current-fmt-compiled) col)
730 (while (setq col (pop columns))
733 (org-columns-compute (car col)))))))
735 (defun org-columns-update (property)
736 "Recompute PROPERTY, and update the columns display for it."
737 (org-columns-compute property)
741 (when (equal (org-overlay-get ov 'org-columns-key) property)
742 (setq pos (org-overlay-start ov))
744 (when (setq val (cdr (assoc property
746 (point-at-bol) 'org-summaries))))
747 (setq fmt (org-overlay-get ov 'org-columns-format))
748 (org-overlay-put ov 'org-columns-value val)
749 (org-overlay-put ov 'display (format fmt val)))))
750 org-columns-overlays))))
752 (defun org-columns-compute (property)
753 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
755 (let* ((re (concat "^" outline-regexp))
756 (lmax 30) ; Does anyone use deeper levels???
757 (lsum (make-vector lmax 0))
758 (lflag (make-vector lmax nil))
760 (ass (assoc property org-columns-current-fmt-compiled))
763 (beg org-columns-top-level-marker)
764 last-level val valflag flag end sumpos sum-alist sum str str1 useval)
766 ;; Find the region to compute
768 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
770 ;; Walk the tree from the back and do the computations
771 (while (re-search-backward re beg t)
772 (setq sumpos (match-beginning 0)
774 level (org-outline-level)
775 val (org-entry-get nil property)
776 valflag (and val (string-match "\\S-" val)))
778 ((< level last-level)
779 ;; put the sum of lower levels here as a property
780 (setq sum (aref lsum last-level) ; current sum
781 flag (aref lflag last-level) ; any valid entries from children?
782 str (org-columns-number-to-string sum format printf)
783 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
784 useval (if flag str1 (if valflag val ""))
785 sum-alist (get-text-property sumpos 'org-summaries))
786 (if (assoc property sum-alist)
787 (setcdr (assoc property sum-alist) useval)
788 (push (cons property useval) sum-alist)
790 (add-text-properties sumpos (1+ sumpos)
791 (list 'org-summaries sum-alist))))
792 (when (and val (not (equal val (if flag str val))))
793 (org-entry-put nil property (if flag str val)))
794 ;; add current to current level accumulator
795 (when (or flag valflag)
796 (aset lsum level (+ (aref lsum level)
797 (if flag sum (org-column-string-to-number
798 (if flag str val) format))))
799 (aset lflag level t))
800 ;; clear accumulators for deeper levels
801 (loop for l from (1+ level) to (1- lmax) do
804 ((>= level last-level)
805 ;; add what we have here to the accumulator for this level
806 (aset lsum level (+ (aref lsum level)
807 (org-column-string-to-number (or val "0") format)))
808 (and valflag (aset lflag level t)))
809 (t (error "This should not happen")))))))
811 (defun org-columns-redo ()
812 "Construct the column display again."
814 (message "Recomputing columns...")
816 (if (marker-position org-columns-begin-marker)
817 (goto-char org-columns-begin-marker))
818 (org-columns-remove-overlays)
820 (call-interactively 'org-columns)
821 (call-interactively 'org-agenda-columns)))
822 (message "Recomputing columns...done"))
824 (defun org-columns-not-in-agenda ()
825 (if (eq major-mode 'org-agenda-mode)
826 (error "This command is only allowed in Org-mode buffers")))
829 (defun org-string-to-number (s)
830 "Convert string to number, and interpret hh:mm:ss."
831 (if (not (string-match ":" s))
833 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
835 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
838 (defun org-columns-number-to-string (n fmt &optional printf)
839 "Convert a computed column number to a string value, according to FMT."
842 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
843 (format "%d:%02d" h m)))
845 (cond ((= n (floor n)) "[X]")
848 ((memq fmt '(checkbox-n-of-m checkbox-percent))
849 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
850 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
851 (printf (format printf n))
854 (t (number-to-string n))))
856 (defun org-nofm-to-completion (n m &optional percent)
858 (format "[%d/%d]" n m)
859 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
861 (defun org-column-string-to-number (s fmt)
862 "Convert a column value to a number that can be used for column computing."
864 ((string-match ":" s)
865 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
867 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
869 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
870 (if (equal s "[X]") 1. 0.000001))
871 (t (string-to-number s))))
873 (defun org-columns-uncompile-format (cfmt)
874 "Turn the compiled columns format back into a string representation."
875 (let ((rtn "") e s prop title op width fmt printf)
876 (while (setq e (pop cfmt))
884 ((eq fmt 'add_times) (setq op ":"))
885 ((eq fmt 'checkbox) (setq op "X"))
886 ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
887 ((eq fmt 'checkbox-percent) (setq op "X%"))
888 ((eq fmt 'add_numbers) (setq op "+"))
889 ((eq fmt 'currency) (setq op "$")))
890 (if (and op printf) (setq op (concat op ";" printf)))
891 (if (equal title prop) (setq title nil))
892 (setq s (concat "%" (if width (number-to-string width))
894 (if title (concat "(" title ")"))
895 (if op (concat "{" op "}"))))
896 (setq rtn (concat rtn " " s)))
899 (defun org-columns-compile-format (fmt)
900 "Turn a column format string into an alist of specifications.
901 The alist has one entry for each column in the format. The elements of
903 property the property
904 title the title field for the columns
905 width the column width in characters, can be nil for automatic
906 operator the operator if any
907 format the output format for computed results, derived from operator
908 printf a printf format for computed values"
909 (let ((start 0) width prop title op f printf)
910 (setq org-columns-current-fmt-compiled nil)
912 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
914 (setq start (match-end 0)
915 width (match-string 1 fmt)
916 prop (match-string 2 fmt)
917 title (or (match-string 3 fmt) prop)
918 op (match-string 4 fmt)
921 (if width (setq width (string-to-number width)))
922 (when (and op (string-match ";" op))
923 (setq printf (substring op (match-end 0))
924 op (substring op 0 (match-beginning 0))))
926 ((equal op "+") (setq f 'add_numbers))
927 ((equal op "$") (setq f 'currency))
928 ((equal op ":") (setq f 'add_times))
929 ((equal op "X") (setq f 'checkbox))
930 ((equal op "X/") (setq f 'checkbox-n-of-m))
931 ((equal op "X%") (setq f 'checkbox-percent))
933 (push (list prop title width op f printf) org-columns-current-fmt-compiled))
934 (setq org-columns-current-fmt-compiled
935 (nreverse org-columns-current-fmt-compiled))))
938 ;;; Dynamic block for Column view
940 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
941 "Get the column view of the current buffer or subtree.
942 The first optional argument MAXLEVEL sets the level limit. A
943 second optional argument SKIP-EMPTY-ROWS tells whether to skip
944 empty rows, an empty row being one where all the column view
945 specifiers except ITEM are empty. This function returns a list
946 containing the title row and all other rows. Each row is a list
949 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
950 (n (length title)) row tbl)
951 (goto-char (point-min))
952 (while (and (re-search-forward "^\\(\\*+\\) " nil t)
955 (if org-odd-levels-only
956 (/ (1+ (length (match-string 1))) 2)
957 (length (match-string 1))))))
958 (when (get-char-property (match-beginning 0) 'org-columns-key)
960 (loop for i from 0 to (1- n) do
961 (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
962 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
965 (setq row (nreverse row))
966 (unless (and skip-empty-rows
967 (eq 1 (length (delete "" (delete-dups row)))))
969 (append (list title 'hline) (nreverse tbl)))))
971 (defun org-dblock-write:columnview (params)
972 "Write the column view table.
973 PARAMS is a property list of parameters:
975 :width enforce same column widths with <N> specifiers.
976 :id the :ID: property of the entry where the columns view
977 should be built, as a string. When `local', call locally.
978 When `global' call column view with the cursor at the beginning
979 of the buffer (usually this means that the whole buffer switches
981 :hlines When t, insert a hline before each item. When a number, insert
982 a hline before each level <= that number.
983 :vlines When t, make each column a colgroup to enforce vertical lines.
984 :maxlevel When set to a number, don't capture headlines below this level.
986 When t, skip rows where all specifiers other than ITEM are empty."
987 (let ((pos (move-marker (make-marker) (point)))
988 (hlines (plist-get params :hlines))
989 (vlines (plist-get params :vlines))
990 (maxlevel (plist-get params :maxlevel))
991 (skip-empty-rows (plist-get params :skip-empty-rows))
992 tbl id idpos nfields tmp)
995 (when (setq id (plist-get params :id))
997 ((eq id 'global) (goto-char (point-min)))
999 ((setq idpos (org-find-entry-with-id id))
1001 (t (error "Cannot find entry with :ID: %s" id))))
1003 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
1004 (setq nfields (length (car tbl)))
1005 (org-columns-quit)))
1007 (move-marker pos nil)
1009 (when (plist-get params :hlines)
1012 (if (eq (car tbl) 'hline)
1013 (push (pop tbl) tmp)
1014 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
1015 (if (and (not (eq (car tmp) 'hline))
1017 (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
1019 (push (pop tbl) tmp)))
1020 (setq tbl (nreverse tmp)))
1022 (setq tbl (mapcar (lambda (x)
1023 (if (eq 'hline x) x (cons "" x)))
1025 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
1027 (insert (org-listtable-to-string tbl))
1028 (when (plist-get params :width)
1029 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
1030 org-columns-current-widths "|")))
1032 (org-table-align))))
1034 (defun org-listtable-to-string (tbl)
1035 "Convert a listtable TBL to a string that contains the Org-mode table.
1036 The table still need to be alligned. The resulting string has no leading
1037 and tailing newline characters."
1042 (concat "|" (mapconcat 'identity x "|") "|"))
1043 ((eq x 'hline) "|-|")
1044 (t (error "Garbage in listtable: %s" x))))
1047 (defun org-insert-columns-dblock ()
1048 "Create a dynamic block capturing a column view table."
1050 (let ((defaults '(:name "columnview" :hlines 1))
1051 (id (completing-read
1052 "Capture columns (local, global, entry with :ID: property) [local]: "
1053 (append '(("global") ("local"))
1054 (mapcar 'list (org-property-values "ID"))))))
1055 (if (equal id "") (setq id 'local))
1056 (if (equal id "global") (setq id 'global))
1057 (setq defaults (append defaults (list :id id)))
1058 (org-create-dblock defaults)
1059 (org-update-dblock)))
1061 ;;; Column view in the agenda
1063 (defvar org-agenda-view-columns-initially nil
1064 "When set, switch to columns view immediately after creating the agenda.")
1066 (defvar org-agenda-columns-show-summaries) ; defined in org-agenda.el
1067 (defvar org-agenda-columns-compute-summary-properties); defined in org-agenda.el
1068 (defvar org-agenda-columns-add-appointments-to-effort-sum); as well
1070 (defun org-agenda-columns ()
1071 "Turn on or update column view in the agenda."
1073 (org-verify-version 'columns)
1074 (org-columns-remove-overlays)
1075 (move-marker org-columns-begin-marker (point))
1076 (let (fmt cache maxwidths m p a d)
1078 ((and (boundp 'org-agenda-overriding-columns-format)
1079 org-agenda-overriding-columns-format)
1080 (setq fmt org-agenda-overriding-columns-format)
1081 (org-set-local 'org-agenda-overriding-columns-format fmt))
1082 ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
1083 (setq fmt (or (org-entry-get m "COLUMNS" t)
1084 (with-current-buffer (marker-buffer m)
1085 org-columns-default-format))))
1086 ((and (boundp 'org-columns-current-fmt)
1087 (local-variable-p 'org-columns-current-fmt)
1088 org-columns-current-fmt)
1089 (setq fmt org-columns-current-fmt))
1090 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
1091 (setq m (get-text-property m 'org-hd-marker))
1092 (setq fmt (or (org-entry-get m "COLUMNS" t)
1093 (with-current-buffer (marker-buffer m)
1094 org-columns-default-format)))))
1095 (setq fmt (or fmt org-columns-default-format))
1096 (org-set-local 'org-columns-current-fmt fmt)
1097 (org-columns-compile-format fmt)
1098 (when org-agenda-columns-compute-summary-properties
1099 (org-agenda-colview-compute org-columns-current-fmt-compiled))
1101 ;; Get and cache the properties
1102 (goto-char (point-min))
1104 (when (setq m (or (get-text-property (point) 'org-hd-marker)
1105 (get-text-property (point) 'org-marker)))
1106 (setq p (org-entry-properties m))
1108 (when (or (not (setq a (assoc org-effort-property p)))
1109 (not (string-match "\\S-" (or (cdr a) ""))))
1110 ;; OK, the property is not defined. Use appointment duration?
1111 (when (and org-agenda-columns-add-appointments-to-effort-sum
1112 (setq d (get-text-property (point) 'duration)))
1113 (setq d (org-minutes-to-hh:mm-string d))
1114 (put-text-property 0 (length d) 'face 'org-warning d)
1115 (push (cons org-effort-property d) p)))
1116 (push (cons (org-current-line) p) cache))
1117 (beginning-of-line 2))
1119 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
1120 (org-set-local 'org-columns-current-maxwidths maxwidths)
1121 (org-columns-display-here-title)
1122 (when (org-set-local 'org-columns-flyspell-was-active
1123 (org-bound-and-true-p flyspell-mode))
1127 (org-columns-display-here (cdr x)))
1129 (when org-agenda-columns-show-summaries
1130 (org-agenda-colview-summarize cache))))))
1132 (defun org-agenda-colview-summarize (cache)
1133 "Summarize the summarizable columns in column view in the agenda.
1134 This will add overlays to the date lines, to show the summary for each day."
1135 (let* ((fmt (mapcar (lambda (x)
1136 (list (car x) (if (equal (car x) "CLOCKSUM")
1137 'add_times (nth 4 x))))
1138 org-columns-current-fmt-compiled))
1139 line c c1 stype props lsum entries prop v)
1141 (when (delq nil (mapcar 'cadr fmt))
1142 ;; OK, at least one summation column, it makes sense to try this
1143 (goto-char (point-max))
1145 (when (or (get-text-property (point) 'org-date-line)
1146 (eq (get-text-property (point) 'face)
1147 'org-agenda-structure))
1148 ;; OK, this is a date line that should be used
1149 (setq line (org-current-line))
1150 (setq entries nil c cache cache nil)
1151 (while (setq c1 (pop c))
1152 (if (> (car c1) line)
1155 ;; now ENTRIES are the ones we want to use, CACHE is the rest
1156 ;; Compute the summaries for the properties we want,
1157 ;; set nil properties for the rest.
1158 (when (setq entries (mapcar 'cdr entries))
1162 (setq prop (car f) stype (nth 1 f))
1164 ((equal prop "ITEM")
1165 (cons prop (buffer-substring (point-at-bol)
1167 ((not stype) (cons prop ""))
1172 (setq v (cdr (assoc prop x)))
1173 (if v (setq lsum (+ lsum
1174 (org-column-string-to-number
1177 (setq lsum (org-columns-number-to-string lsum stype))
1179 0 (length lsum) 'face 'bold lsum)
1182 (org-columns-display-here props)
1183 (org-set-local 'org-agenda-columns-active t)))
1184 (if (bobp) (throw 'exit t))
1185 (beginning-of-line 0))))))
1187 (defun org-agenda-colview-compute (fmt)
1188 "Compute the relevant columns in the contributing source buffers."
1189 (let ((files org-agenda-contributing-files)
1190 (org-columns-begin-marker (make-marker))
1191 (org-columns-top-level-marker (make-marker))
1193 (while (setq f (pop files))
1194 (setq b (find-buffer-visiting f))
1195 (with-current-buffer (or (buffer-base-buffer b) b)
1200 (remove-text-properties (point-min) (point-max)
1201 '(org-summaries t)))
1202 (goto-char (point-min))
1203 (org-columns-get-format-and-top-level)
1204 (while (setq fm (pop fmt))
1205 (if (equal (car fm) "CLOCKSUM")
1207 (when (and (nth 4 fm)
1208 (setq a (assoc (car fm)
1209 org-columns-current-fmt-compiled))
1210 (equal (nth 4 a) (nth 4 fm)))
1211 (org-columns-compute (car fm)))))))))))
1213 (provide 'org-colview)
1215 ;;; org-colview.el ends here
1217 ;; arch-tag: 61f5128d-747c-4983-9479-e3871fa3d73c