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