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