commit | author | age
|
76bbd0
|
1 |
;;; org-colview.el --- Column View in Org -*- lexical-binding: t; -*- |
C |
2 |
|
|
3 |
;; Copyright (C) 2004-2018 Free Software Foundation, Inc. |
|
4 |
|
|
5 |
;; Author: Carsten Dominik <carsten at orgmode dot org> |
|
6 |
;; Keywords: outlines, hypermedia, calendar, wp |
|
7 |
;; Homepage: https://orgmode.org |
|
8 |
;; |
|
9 |
;; This file is part of GNU Emacs. |
|
10 |
;; |
|
11 |
;; GNU Emacs is free software: you can redistribute it and/or modify |
|
12 |
;; it under the terms of the GNU General Public License as published by |
|
13 |
;; the Free Software Foundation, either version 3 of the License, or |
|
14 |
;; (at your option) any later version. |
|
15 |
|
|
16 |
;; GNU Emacs is distributed in the hope that it will be useful, |
|
17 |
;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19 |
;; GNU General Public License for more details. |
|
20 |
|
|
21 |
;; You should have received a copy of the GNU General Public License |
|
22 |
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. |
|
23 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
|
24 |
;; |
|
25 |
;;; Commentary: |
|
26 |
|
|
27 |
;; This file contains the column view for Org. |
|
28 |
|
|
29 |
;;; Code: |
|
30 |
|
|
31 |
(require 'cl-lib) |
|
32 |
(require 'org) |
|
33 |
|
|
34 |
(declare-function org-agenda-redo "org-agenda" (&optional all)) |
|
35 |
(declare-function org-agenda-do-context-action "org-agenda" ()) |
|
36 |
(declare-function org-clock-sum-today "org-clock" (&optional headline-filter)) |
|
37 |
(declare-function org-element-extract-element "org-element" (element)) |
|
38 |
(declare-function org-element-interpret-data "org-element" (data)) |
|
39 |
(declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion with-affiliated)) |
|
40 |
(declare-function org-element-parse-secondary-string "org-element" (string restriction &optional parent)) |
|
41 |
(declare-function org-element-property "org-element" (property element)) |
|
42 |
(declare-function org-element-restriction "org-element" (element)) |
|
43 |
(declare-function org-element-type "org-element" (element)) |
|
44 |
|
|
45 |
(defvar org-agenda-columns-add-appointments-to-effort-sum) |
|
46 |
(defvar org-agenda-columns-compute-summary-properties) |
|
47 |
(defvar org-agenda-columns-show-summaries) |
|
48 |
(defvar org-agenda-view-columns-initially) |
|
49 |
(defvar org-inlinetask-min-level) |
|
50 |
|
|
51 |
|
|
52 |
;;; Configuration |
|
53 |
|
|
54 |
(defcustom org-columns-modify-value-for-display-function nil |
|
55 |
"Function that modifies values for display in column view. |
|
56 |
For example, it can be used to cut out a certain part from a time stamp. |
|
57 |
The function must take 2 arguments: |
|
58 |
|
|
59 |
column-title The title of the column (*not* the property name) |
|
60 |
value The value that should be modified. |
|
61 |
|
|
62 |
The function should return the value that should be displayed, |
|
63 |
or nil if the normal value should be used." |
|
64 |
:group 'org-properties |
|
65 |
:type '(choice (const nil) (function))) |
|
66 |
|
|
67 |
(defcustom org-columns-summary-types nil |
|
68 |
"Alist between operators and summarize functions. |
|
69 |
|
|
70 |
Each association follows the pattern (LABEL . SUMMARIZE) where |
|
71 |
|
|
72 |
LABEL is a string used in #+COLUMNS definition describing the |
|
73 |
summary type. It can contain any character but \"}\". It is |
|
74 |
case-sensitive. |
|
75 |
|
|
76 |
SUMMARIZE is a function called with two arguments. The first |
|
77 |
argument is a non-empty list of values, as non-empty strings. |
|
78 |
The second one is a format string or nil. It has to return |
|
79 |
a string summarizing the list of values. |
|
80 |
|
|
81 |
Note that the return value can become one value for an higher |
|
82 |
order summary, so the function is expected to handle its own |
|
83 |
output. |
|
84 |
|
|
85 |
Types defined in this variable take precedence over those defined |
|
86 |
in `org-columns-summary-types-default', which see." |
|
87 |
:group 'org-properties |
|
88 |
:version "26.1" |
|
89 |
:package-version '(Org . "9.0") |
|
90 |
:type '(alist :key-type (string :tag " Label") |
|
91 |
:value-type (function :tag "Summarize"))) |
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
;;; Column View |
|
96 |
|
|
97 |
(defvar-local org-columns-overlays nil |
|
98 |
"Holds the list of current column overlays.") |
|
99 |
|
|
100 |
(defvar-local org-columns-current-fmt nil |
|
101 |
"Local variable, holds the currently active column format.") |
|
102 |
|
|
103 |
(defvar-local org-columns-current-fmt-compiled nil |
|
104 |
"Local variable, holds the currently active column format. |
|
105 |
This is the compiled version of the format.") |
|
106 |
|
|
107 |
(defvar-local org-columns-current-maxwidths nil |
|
108 |
"Currently active maximum column widths, as a vector.") |
|
109 |
|
|
110 |
(defvar-local org-columns-begin-marker nil |
|
111 |
"Points to the position where last a column creation command was called.") |
|
112 |
|
|
113 |
(defvar-local org-columns-top-level-marker nil |
|
114 |
"Points to the position where current columns region starts.") |
|
115 |
|
|
116 |
(defvar org-columns--time 0.0 |
|
117 |
"Number of seconds since the epoch, as a floating point number.") |
|
118 |
|
|
119 |
(defvar org-columns-map (make-sparse-keymap) |
|
120 |
"The keymap valid in column display.") |
|
121 |
|
|
122 |
(defconst org-columns-summary-types-default |
|
123 |
'(("+" . org-columns--summary-sum) |
|
124 |
("$" . org-columns--summary-currencies) |
|
125 |
("X" . org-columns--summary-checkbox) |
|
126 |
("X/" . org-columns--summary-checkbox-count) |
|
127 |
("X%" . org-columns--summary-checkbox-percent) |
|
128 |
("max" . org-columns--summary-max) |
|
129 |
("mean" . org-columns--summary-mean) |
|
130 |
("min" . org-columns--summary-min) |
|
131 |
(":" . org-columns--summary-sum-times) |
|
132 |
(":max" . org-columns--summary-max-time) |
|
133 |
(":mean" . org-columns--summary-mean-time) |
|
134 |
(":min" . org-columns--summary-min-time) |
|
135 |
("@max" . org-columns--summary-max-age) |
|
136 |
("@mean" . org-columns--summary-mean-age) |
|
137 |
("@min" . org-columns--summary-min-age) |
|
138 |
("est+" . org-columns--summary-estimate)) |
|
139 |
"Map operators to summarize functions. |
|
140 |
See `org-columns-summary-types' for details.") |
|
141 |
|
|
142 |
(defun org-columns-content () |
|
143 |
"Switch to contents view while in columns view." |
|
144 |
(interactive) |
|
145 |
(org-overview) |
|
146 |
(org-content)) |
|
147 |
|
|
148 |
(org-defkey org-columns-map "c" 'org-columns-content) |
|
149 |
(org-defkey org-columns-map "o" 'org-overview) |
|
150 |
(org-defkey org-columns-map "e" 'org-columns-edit-value) |
|
151 |
(org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo) |
|
152 |
(org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle) |
|
153 |
(org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link) |
|
154 |
(org-defkey org-columns-map "v" 'org-columns-show-value) |
|
155 |
(org-defkey org-columns-map "q" 'org-columns-quit) |
|
156 |
(org-defkey org-columns-map "r" 'org-columns-redo) |
|
157 |
(org-defkey org-columns-map "g" 'org-columns-redo) |
|
158 |
(org-defkey org-columns-map [left] 'backward-char) |
|
159 |
(org-defkey org-columns-map "\M-b" 'backward-char) |
|
160 |
(org-defkey org-columns-map "a" 'org-columns-edit-allowed) |
|
161 |
(org-defkey org-columns-map "s" 'org-columns-edit-attributes) |
|
162 |
(org-defkey org-columns-map "\M-f" |
|
163 |
(lambda () (interactive) (goto-char (1+ (point))))) |
|
164 |
(org-defkey org-columns-map [right] |
|
165 |
(lambda () (interactive) (goto-char (1+ (point))))) |
|
166 |
(org-defkey org-columns-map [down] |
|
167 |
(lambda () (interactive) |
|
168 |
(let ((col (current-column))) |
|
169 |
(beginning-of-line 2) |
|
170 |
(while (and (org-invisible-p2) (not (eobp))) |
|
171 |
(beginning-of-line 2)) |
|
172 |
(move-to-column col) |
|
173 |
(if (eq major-mode 'org-agenda-mode) |
|
174 |
(org-agenda-do-context-action))))) |
|
175 |
(org-defkey org-columns-map [up] |
|
176 |
(lambda () (interactive) |
|
177 |
(let ((col (current-column))) |
|
178 |
(beginning-of-line 0) |
|
179 |
(while (and (org-invisible-p2) (not (bobp))) |
|
180 |
(beginning-of-line 0)) |
|
181 |
(move-to-column col) |
|
182 |
(if (eq major-mode 'org-agenda-mode) |
|
183 |
(org-agenda-do-context-action))))) |
|
184 |
(org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value) |
|
185 |
(org-defkey org-columns-map "n" 'org-columns-next-allowed-value) |
|
186 |
(org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value) |
|
187 |
(org-defkey org-columns-map "p" 'org-columns-previous-allowed-value) |
|
188 |
(org-defkey org-columns-map "<" 'org-columns-narrow) |
|
189 |
(org-defkey org-columns-map ">" 'org-columns-widen) |
|
190 |
(org-defkey org-columns-map [(meta right)] 'org-columns-move-right) |
|
191 |
(org-defkey org-columns-map [(meta left)] 'org-columns-move-left) |
|
192 |
(org-defkey org-columns-map [(shift meta right)] 'org-columns-new) |
|
193 |
(org-defkey org-columns-map [(shift meta left)] 'org-columns-delete) |
|
194 |
(dotimes (i 10) |
|
195 |
(org-defkey org-columns-map (number-to-string i) |
|
196 |
`(lambda () (interactive) |
|
197 |
(org-columns-next-allowed-value nil ,i)))) |
|
198 |
|
|
199 |
(easy-menu-define org-columns-menu org-columns-map "Org Column Menu" |
|
200 |
'("Column" |
|
201 |
["Edit property" org-columns-edit-value t] |
|
202 |
["Next allowed value" org-columns-next-allowed-value t] |
|
203 |
["Previous allowed value" org-columns-previous-allowed-value t] |
|
204 |
["Show full value" org-columns-show-value t] |
|
205 |
["Edit allowed values" org-columns-edit-allowed t] |
|
206 |
"--" |
|
207 |
["Edit column attributes" org-columns-edit-attributes t] |
|
208 |
["Increase column width" org-columns-widen t] |
|
209 |
["Decrease column width" org-columns-narrow t] |
|
210 |
"--" |
|
211 |
["Move column right" org-columns-move-right t] |
|
212 |
["Move column left" org-columns-move-left t] |
|
213 |
["Add column" org-columns-new t] |
|
214 |
["Delete column" org-columns-delete t] |
|
215 |
"--" |
|
216 |
["CONTENTS" org-columns-content t] |
|
217 |
["OVERVIEW" org-overview t] |
|
218 |
["Refresh columns display" org-columns-redo t] |
|
219 |
"--" |
|
220 |
["Open link" org-columns-open-link t] |
|
221 |
"--" |
|
222 |
["Quit" org-columns-quit t])) |
|
223 |
|
|
224 |
(defun org-columns--displayed-value (spec value) |
|
225 |
"Return displayed value for specification SPEC in current entry. |
|
226 |
SPEC is a column format specification as stored in |
|
227 |
`org-columns-current-fmt-compiled'. VALUE is the real value to |
|
228 |
display, as a string." |
|
229 |
(or (and (functionp org-columns-modify-value-for-display-function) |
|
230 |
(funcall org-columns-modify-value-for-display-function |
|
231 |
(nth 1 spec) ;column name |
|
232 |
value)) |
|
233 |
(pcase spec |
|
234 |
(`("ITEM" . ,_) |
|
235 |
(concat (make-string (1- (org-current-level)) |
|
236 |
(if org-hide-leading-stars ?\s ?*)) |
|
237 |
"* " |
|
238 |
(org-columns-compact-links value))) |
|
239 |
(`(,_ ,_ ,_ ,_ nil) value) |
|
240 |
;; If PRINTF is set, assume we are displaying a number and |
|
241 |
;; obey to the format string. |
|
242 |
(`(,_ ,_ ,_ ,_ ,printf) (format printf (string-to-number value))) |
|
243 |
(_ (error "Invalid column specification format: %S" spec))))) |
|
244 |
|
|
245 |
(defun org-columns--collect-values (&optional compiled-fmt) |
|
246 |
"Collect values for columns on the current line. |
|
247 |
|
|
248 |
Return a list of triplets (SPEC VALUE DISPLAYED) suitable for |
|
249 |
`org-columns--display-here'. |
|
250 |
|
|
251 |
This function assumes `org-columns-current-fmt-compiled' is |
|
252 |
initialized is set in the current buffer. However, it is |
|
253 |
possible to override it with optional argument COMPILED-FMT." |
|
254 |
(let ((summaries (get-text-property (point) 'org-summaries))) |
|
255 |
(mapcar |
|
256 |
(lambda (spec) |
|
257 |
(pcase spec |
|
258 |
(`(,p . ,_) |
|
259 |
(let* ((v (or (cdr (assoc spec summaries)) |
|
260 |
(org-entry-get (point) p 'selective t) |
|
261 |
(and compiled-fmt ;assume `org-agenda-columns' |
|
262 |
;; Effort property is not defined. Try |
|
263 |
;; to use appointment duration. |
|
264 |
org-agenda-columns-add-appointments-to-effort-sum |
|
265 |
(string= p (upcase org-effort-property)) |
|
266 |
(get-text-property (point) 'duration) |
|
267 |
(propertize (org-duration-from-minutes |
|
268 |
(get-text-property (point) 'duration)) |
|
269 |
'face 'org-warning)) |
|
270 |
""))) |
|
271 |
(list spec v (org-columns--displayed-value spec v)))))) |
|
272 |
(or compiled-fmt org-columns-current-fmt-compiled)))) |
|
273 |
|
|
274 |
(defun org-columns--set-widths (cache) |
|
275 |
"Compute the maximum column widths from the format and CACHE. |
|
276 |
This function sets `org-columns-current-maxwidths' as a vector of |
|
277 |
integers greater than 0." |
|
278 |
(setq org-columns-current-maxwidths |
|
279 |
(apply #'vector |
|
280 |
(mapcar |
|
281 |
(lambda (spec) |
|
282 |
(pcase spec |
|
283 |
(`(,_ ,_ ,(and width (pred wholenump)) . ,_) width) |
|
284 |
(`(,_ ,name . ,_) |
|
285 |
;; No width is specified in the columns format. |
|
286 |
;; Compute it by checking all possible values for |
|
287 |
;; PROPERTY. |
|
288 |
(let ((width (length name))) |
|
289 |
(dolist (entry cache width) |
|
290 |
(let ((value (nth 2 (assoc spec (cdr entry))))) |
|
291 |
(setq width (max (length value) width)))))))) |
|
292 |
org-columns-current-fmt-compiled)))) |
|
293 |
|
|
294 |
(defun org-columns--new-overlay (beg end &optional string face) |
|
295 |
"Create a new column overlay and add it to the list." |
|
296 |
(let ((ov (make-overlay beg end))) |
|
297 |
(overlay-put ov 'face (or face 'secondary-selection)) |
|
298 |
(org-overlay-display ov string face) |
|
299 |
(push ov org-columns-overlays) |
|
300 |
ov)) |
|
301 |
|
|
302 |
(defun org-columns--summarize (operator) |
|
303 |
"Return summary function associated to string OPERATOR." |
|
304 |
(if (not operator) nil |
|
305 |
(cdr (or (assoc operator org-columns-summary-types) |
|
306 |
(assoc operator org-columns-summary-types-default) |
|
307 |
(error "Unknown %S operator" operator))))) |
|
308 |
|
|
309 |
(defun org-columns--overlay-text (value fmt width property original) |
|
310 |
"Return text " |
|
311 |
(format fmt |
|
312 |
(let ((v (org-columns-add-ellipses value width))) |
|
313 |
(pcase property |
|
314 |
("PRIORITY" |
|
315 |
(propertize v 'face (org-get-priority-face original))) |
|
316 |
("TAGS" |
|
317 |
(if (not org-tags-special-faces-re) |
|
318 |
(propertize v 'face 'org-tag) |
|
319 |
(replace-regexp-in-string |
|
320 |
org-tags-special-faces-re |
|
321 |
(lambda (m) (propertize m 'face (org-get-tag-face m))) |
|
322 |
v nil nil 1))) |
|
323 |
("TODO" (propertize v 'face (org-get-todo-face original))) |
|
324 |
(_ v))))) |
|
325 |
|
|
326 |
(defun org-columns--display-here (columns &optional dateline) |
|
327 |
"Overlay the current line with column display. |
|
328 |
COLUMNS is an alist (SPEC VALUE DISPLAYED). Optional argument |
|
329 |
DATELINE is non-nil when the face used should be |
|
330 |
`org-agenda-column-dateline'." |
|
331 |
(save-excursion |
|
332 |
(beginning-of-line) |
|
333 |
(let* ((level-face (and (looking-at "\\(\\**\\)\\(\\* \\)") |
|
334 |
(org-get-level-face 2))) |
|
335 |
(ref-face (or level-face |
|
336 |
(and (eq major-mode 'org-agenda-mode) |
|
337 |
(org-get-at-bol 'face)) |
|
338 |
'default)) |
|
339 |
(color (list :foreground (face-attribute ref-face :foreground))) |
|
340 |
(font (list :height (face-attribute 'default :height) |
|
341 |
:family (face-attribute 'default :family))) |
|
342 |
(face (list color font 'org-column ref-face)) |
|
343 |
(face1 (list color font 'org-agenda-column-dateline ref-face))) |
|
344 |
;; Each column is an overlay on top of a character. So there has |
|
345 |
;; to be at least as many characters available on the line as |
|
346 |
;; columns to display. |
|
347 |
(let ((columns (length org-columns-current-fmt-compiled)) |
|
348 |
(chars (- (line-end-position) (line-beginning-position)))) |
|
349 |
(when (> columns chars) |
|
350 |
(save-excursion |
|
351 |
(end-of-line) |
|
352 |
(let ((inhibit-read-only t)) |
|
353 |
(insert (make-string (- columns chars) ?\s)))))) |
|
354 |
;; Display columns. Create and install the overlay for the |
|
355 |
;; current column on the next character. |
|
356 |
(let ((i 0) |
|
357 |
(last (1- (length columns)))) |
|
358 |
(dolist (column columns) |
|
359 |
(pcase column |
|
360 |
(`(,spec ,original ,value) |
|
361 |
(let* ((property (car spec)) |
|
362 |
(width (aref org-columns-current-maxwidths i)) |
|
363 |
(fmt (format (if (= i last) "%%-%d.%ds |" |
|
364 |
"%%-%d.%ds | ") |
|
365 |
width width)) |
|
366 |
(ov (org-columns--new-overlay |
|
367 |
(point) (1+ (point)) |
|
368 |
(org-columns--overlay-text |
|
369 |
value fmt width property original) |
|
370 |
(if dateline face1 face)))) |
|
371 |
(overlay-put ov 'keymap org-columns-map) |
|
372 |
(overlay-put ov 'org-columns-key property) |
|
373 |
(overlay-put ov 'org-columns-value original) |
|
374 |
(overlay-put ov 'org-columns-value-modified value) |
|
375 |
(overlay-put ov 'org-columns-format fmt) |
|
376 |
(overlay-put ov 'line-prefix "") |
|
377 |
(overlay-put ov 'wrap-prefix "") |
|
378 |
(forward-char)))) |
|
379 |
(cl-incf i))) |
|
380 |
;; Make the rest of the line disappear. |
|
381 |
(let ((ov (org-columns--new-overlay (point) (line-end-position)))) |
|
382 |
(overlay-put ov 'invisible t) |
|
383 |
(overlay-put ov 'keymap org-columns-map) |
|
384 |
(overlay-put ov 'line-prefix "") |
|
385 |
(overlay-put ov 'wrap-prefix "")) |
|
386 |
(let ((ov (make-overlay (1- (line-end-position)) |
|
387 |
(line-beginning-position 2)))) |
|
388 |
(overlay-put ov 'keymap org-columns-map) |
|
389 |
(push ov org-columns-overlays)) |
|
390 |
(org-with-silent-modifications |
|
391 |
(let ((inhibit-read-only t)) |
|
392 |
(put-text-property |
|
393 |
(line-end-position 0) |
|
394 |
(line-beginning-position 2) |
|
395 |
'read-only |
|
396 |
(substitute-command-keys |
|
397 |
"Type \\<org-columns-map>`\\[org-columns-edit-value]' \ |
|
398 |
to edit property"))))))) |
|
399 |
|
|
400 |
(defun org-columns-add-ellipses (string width) |
|
401 |
"Truncate STRING with WIDTH characters, with ellipses." |
|
402 |
(cond |
|
403 |
((<= (length string) width) string) |
|
404 |
((<= width (length org-columns-ellipses)) |
|
405 |
(substring org-columns-ellipses 0 width)) |
|
406 |
(t (concat (substring string 0 (- width (length org-columns-ellipses))) |
|
407 |
org-columns-ellipses)))) |
|
408 |
|
|
409 |
(defvar org-columns-full-header-line-format nil |
|
410 |
"The full header line format, will be shifted by horizontal scrolling." ) |
|
411 |
(defvar org-previous-header-line-format nil |
|
412 |
"The header line format before column view was turned on.") |
|
413 |
(defvar org-columns-inhibit-recalculation nil |
|
414 |
"Inhibit recomputing of columns on column view startup.") |
|
415 |
(defvar org-columns-flyspell-was-active nil |
|
416 |
"Remember the state of `flyspell-mode' before column view. |
|
417 |
Flyspell-mode can cause problems in columns view, so it is turned off |
|
418 |
for the duration of the command.") |
|
419 |
|
|
420 |
(defvar header-line-format) |
|
421 |
(defvar org-columns-previous-hscroll 0) |
|
422 |
|
|
423 |
(defun org-columns--display-here-title () |
|
424 |
"Overlay the newline before the current line with the table title." |
|
425 |
(interactive) |
|
426 |
(let ((title "") |
|
427 |
(i 0)) |
|
428 |
(dolist (column org-columns-current-fmt-compiled) |
|
429 |
(pcase column |
|
430 |
(`(,property ,name . ,_) |
|
431 |
(let* ((width (aref org-columns-current-maxwidths i)) |
|
432 |
(fmt (format "%%-%d.%ds | " width width))) |
|
433 |
(setq title (concat title (format fmt (or name property))))))) |
|
434 |
(cl-incf i)) |
|
435 |
(setq-local org-previous-header-line-format header-line-format) |
|
436 |
(setq org-columns-full-header-line-format |
|
437 |
(concat |
|
438 |
(org-add-props " " nil 'display '(space :align-to 0)) |
|
439 |
(org-add-props (substring title 0 -1) nil 'face 'org-column-title))) |
|
440 |
(setq org-columns-previous-hscroll -1) |
|
441 |
(add-hook 'post-command-hook 'org-columns-hscroll-title nil 'local))) |
|
442 |
|
|
443 |
(defun org-columns-hscroll-title () |
|
444 |
"Set the `header-line-format' so that it scrolls along with the table." |
|
445 |
(sit-for .0001) ; need to force a redisplay to update window-hscroll |
|
446 |
(when (not (= (window-hscroll) org-columns-previous-hscroll)) |
|
447 |
(setq header-line-format |
|
448 |
(concat (substring org-columns-full-header-line-format 0 1) |
|
449 |
(substring org-columns-full-header-line-format |
|
450 |
(1+ (window-hscroll)))) |
|
451 |
org-columns-previous-hscroll (window-hscroll)) |
|
452 |
(force-mode-line-update))) |
|
453 |
|
|
454 |
(defvar org-colview-initial-truncate-line-value nil |
|
455 |
"Remember the value of `truncate-lines' across colview.") |
|
456 |
|
|
457 |
;;;###autoload |
|
458 |
(defun org-columns-remove-overlays () |
|
459 |
"Remove all currently active column overlays." |
|
460 |
(interactive) |
|
461 |
(when org-columns-overlays |
|
462 |
(when (local-variable-p 'org-previous-header-line-format) |
|
463 |
(setq header-line-format org-previous-header-line-format) |
|
464 |
(kill-local-variable 'org-previous-header-line-format) |
|
465 |
(remove-hook 'post-command-hook 'org-columns-hscroll-title 'local)) |
|
466 |
(set-marker org-columns-begin-marker nil) |
|
467 |
(when (markerp org-columns-top-level-marker) |
|
468 |
(set-marker org-columns-top-level-marker nil)) |
|
469 |
(org-with-silent-modifications |
|
470 |
(mapc #'delete-overlay org-columns-overlays) |
|
471 |
(setq org-columns-overlays nil) |
|
472 |
(let ((inhibit-read-only t)) |
|
473 |
(remove-text-properties (point-min) (point-max) '(read-only t)))) |
|
474 |
(when org-columns-flyspell-was-active |
|
475 |
(flyspell-mode 1)) |
|
476 |
(when (local-variable-p 'org-colview-initial-truncate-line-value) |
|
477 |
(setq truncate-lines org-colview-initial-truncate-line-value)))) |
|
478 |
|
|
479 |
(defun org-columns-compact-links (s) |
|
480 |
"Replace [[link][desc]] with [desc] or [link]." |
|
481 |
(while (string-match org-bracket-link-regexp s) |
|
482 |
(setq s (replace-match |
|
483 |
(concat "[" (match-string (if (match-end 3) 3 1) s) "]") |
|
484 |
t t s))) |
|
485 |
s) |
|
486 |
|
|
487 |
(defun org-columns-show-value () |
|
488 |
"Show the full value of the property." |
|
489 |
(interactive) |
|
490 |
(let ((value (get-char-property (point) 'org-columns-value))) |
|
491 |
(message "Value is: %s" (or value "")))) |
|
492 |
|
|
493 |
(defvar org-agenda-columns-active) ;; defined in org-agenda.el |
|
494 |
|
|
495 |
(defun org-columns-quit () |
|
496 |
"Remove the column overlays and in this way exit column editing." |
|
497 |
(interactive) |
|
498 |
(org-with-silent-modifications |
|
499 |
(org-columns-remove-overlays) |
|
500 |
(let ((inhibit-read-only t)) |
|
501 |
(remove-text-properties (point-min) (point-max) '(read-only t)))) |
|
502 |
(if (not (eq major-mode 'org-agenda-mode)) |
|
503 |
(setq org-columns-current-fmt nil) |
|
504 |
(setq org-agenda-columns-active nil) |
|
505 |
(message |
|
506 |
"Modification not yet reflected in Agenda buffer, use `r' to refresh"))) |
|
507 |
|
|
508 |
(defun org-columns-check-computed () |
|
509 |
"Throw an error if current column value is computed." |
|
510 |
(let ((spec (nth (current-column) org-columns-current-fmt-compiled))) |
|
511 |
(and |
|
512 |
(nth 3 spec) |
|
513 |
(assoc spec (get-text-property (line-beginning-position) 'org-summaries)) |
|
514 |
(error "This value is computed from the entry's children")))) |
|
515 |
|
|
516 |
(defun org-columns-todo (&optional _arg) |
|
517 |
"Change the TODO state during column view." |
|
518 |
(interactive "P") |
|
519 |
(org-columns-edit-value "TODO")) |
|
520 |
|
|
521 |
(defun org-columns-set-tags-or-toggle (&optional _arg) |
|
522 |
"Toggle checkbox at point, or set tags for current headline." |
|
523 |
(interactive "P") |
|
524 |
(if (string-match "\\`\\[[ xX-]\\]\\'" |
|
525 |
(get-char-property (point) 'org-columns-value)) |
|
526 |
(org-columns-next-allowed-value) |
|
527 |
(org-columns-edit-value "TAGS"))) |
|
528 |
|
|
529 |
(defvar org-agenda-overriding-columns-format nil |
|
530 |
"When set, overrides any other format definition for the agenda. |
|
531 |
Don't set this, this is meant for dynamic scoping.") |
|
532 |
|
|
533 |
(defun org-columns-edit-value (&optional key) |
|
534 |
"Edit the value of the property at point in column view. |
|
535 |
Where possible, use the standard interface for changing this line." |
|
536 |
(interactive) |
|
537 |
(org-columns-check-computed) |
|
538 |
(let* ((col (current-column)) |
|
539 |
(bol (line-beginning-position)) |
|
540 |
(eol (line-end-position)) |
|
541 |
(pom (or (get-text-property bol 'org-hd-marker) (point))) |
|
542 |
(key (or key (get-char-property (point) 'org-columns-key))) |
|
543 |
(org-columns--time (float-time (current-time))) |
|
544 |
(action |
|
545 |
(pcase key |
|
546 |
("CLOCKSUM" |
|
547 |
(error "This special column cannot be edited")) |
|
548 |
("ITEM" |
|
549 |
(lambda () (org-with-point-at pom (org-edit-headline)))) |
|
550 |
("TODO" |
|
551 |
(lambda () |
|
552 |
(org-with-point-at pom (call-interactively #'org-todo)))) |
|
553 |
("PRIORITY" |
|
554 |
(lambda () |
|
555 |
(org-with-point-at pom |
|
556 |
(call-interactively #'org-priority)))) |
|
557 |
("TAGS" |
|
558 |
(lambda () |
|
559 |
(org-with-point-at pom |
|
560 |
(let ((org-fast-tag-selection-single-key |
|
561 |
(if (eq org-fast-tag-selection-single-key 'expert) |
|
562 |
t |
|
563 |
org-fast-tag-selection-single-key))) |
|
564 |
(call-interactively #'org-set-tags))))) |
|
565 |
("DEADLINE" |
|
566 |
(lambda () |
|
567 |
(org-with-point-at pom (call-interactively #'org-deadline)))) |
|
568 |
("SCHEDULED" |
|
569 |
(lambda () |
|
570 |
(org-with-point-at pom (call-interactively #'org-schedule)))) |
|
571 |
("BEAMER_ENV" |
|
572 |
(lambda () |
|
573 |
(org-with-point-at pom |
|
574 |
(call-interactively #'org-beamer-select-environment)))) |
|
575 |
(_ |
|
576 |
(let* ((allowed (org-property-get-allowed-values pom key 'table)) |
|
577 |
(value (get-char-property (point) 'org-columns-value)) |
|
578 |
(nval (org-trim |
|
579 |
(if (null allowed) (read-string "Edit: " value) |
|
580 |
(completing-read |
|
581 |
"Value: " allowed nil |
|
582 |
(not (get-text-property |
|
583 |
0 'org-unrestricted (caar allowed)))))))) |
|
584 |
(and (not (equal nval value)) |
|
585 |
(lambda () (org-entry-put pom key nval)))))))) |
|
586 |
(cond |
|
587 |
((null action)) |
|
588 |
((eq major-mode 'org-agenda-mode) |
|
589 |
(org-columns--call action) |
|
590 |
;; The following let preserves the current format, and makes |
|
591 |
;; sure that in only a single file things need to be updated. |
|
592 |
(let* ((org-agenda-overriding-columns-format org-columns-current-fmt) |
|
593 |
(buffer (marker-buffer pom)) |
|
594 |
(org-agenda-contributing-files |
|
595 |
(list (with-current-buffer buffer |
|
596 |
(buffer-file-name (buffer-base-buffer)))))) |
|
597 |
(org-agenda-columns))) |
|
598 |
(t |
|
599 |
(let ((inhibit-read-only t)) |
|
600 |
(org-with-silent-modifications |
|
601 |
(remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))) |
|
602 |
(org-columns--call action)) |
|
603 |
;; Some properties can modify headline (e.g., "TODO"), and |
|
604 |
;; possible shuffle overlays. Make sure they are still all at |
|
605 |
;; the right place on the current line. |
|
606 |
(let ((org-columns-inhibit-recalculation)) (org-columns-redo)) |
|
607 |
(org-columns-update key) |
|
608 |
(org-move-to-column col))))) |
|
609 |
|
|
610 |
(defun org-columns-edit-allowed () |
|
611 |
"Edit the list of allowed values for the current property." |
|
612 |
(interactive) |
|
613 |
(let* ((pom (or (org-get-at-bol 'org-marker) |
|
614 |
(org-get-at-bol 'org-hd-marker) |
|
615 |
(point))) |
|
616 |
(key (concat (or (get-char-property (point) 'org-columns-key) |
|
617 |
(user-error "No column to edit at point")) |
|
618 |
"_ALL")) |
|
619 |
(allowed (org-entry-get pom key t)) |
|
620 |
(new-value (read-string "Allowed: " allowed))) |
|
621 |
;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.???? |
|
622 |
;; FIXME: Write back to #+PROPERTY setting if that is needed. |
|
623 |
(org-entry-put |
|
624 |
(cond ((marker-position org-entry-property-inherited-from) |
|
625 |
org-entry-property-inherited-from) |
|
626 |
((marker-position org-columns-top-level-marker) |
|
627 |
org-columns-top-level-marker) |
|
628 |
(t pom)) |
|
629 |
key new-value))) |
|
630 |
|
|
631 |
(defun org-columns--call (fun) |
|
632 |
"Call function FUN while preserving heading visibility. |
|
633 |
FUN is a function called with no argument." |
|
634 |
(let ((hide-body (and (/= (line-end-position) (point-max)) |
|
635 |
(save-excursion |
|
636 |
(move-beginning-of-line 2) |
|
637 |
(org-at-heading-p t))))) |
|
638 |
(unwind-protect (funcall fun) |
|
639 |
(when hide-body (outline-hide-entry))))) |
|
640 |
|
|
641 |
(defun org-columns-previous-allowed-value () |
|
642 |
"Switch to the previous allowed value for this column." |
|
643 |
(interactive) |
|
644 |
(org-columns-next-allowed-value t)) |
|
645 |
|
|
646 |
(defun org-columns-next-allowed-value (&optional previous nth) |
|
647 |
"Switch to the next allowed value for this column. |
|
648 |
When PREVIOUS is set, go to the previous value. When NTH is |
|
649 |
an integer, select that value." |
|
650 |
(interactive) |
|
651 |
(org-columns-check-computed) |
|
652 |
(let* ((column (current-column)) |
|
653 |
(key (get-char-property (point) 'org-columns-key)) |
|
654 |
(value (get-char-property (point) 'org-columns-value)) |
|
655 |
(pom (or (get-text-property (line-beginning-position) 'org-hd-marker) |
|
656 |
(point))) |
|
657 |
(allowed |
|
658 |
(let ((all |
|
659 |
(or (org-property-get-allowed-values pom key) |
|
660 |
(pcase (nth column org-columns-current-fmt-compiled) |
|
661 |
(`(,_ ,_ ,_ ,(or "X" "X/" "X%") ,_) '("[ ]" "[X]"))) |
|
662 |
(org-colview-construct-allowed-dates value)))) |
|
663 |
(if previous (reverse all) all)))) |
|
664 |
(when (equal key "ITEM") (error "Cannot edit item headline from here")) |
|
665 |
(unless (or allowed (member key '("SCHEDULED" "DEADLINE" "CLOCKSUM"))) |
|
666 |
(error "Allowed values for this property have not been defined")) |
|
667 |
(let* ((l (length allowed)) |
|
668 |
(new |
|
669 |
(cond |
|
670 |
((member key '("SCHEDULED" "DEADLINE" "CLOCKSUM")) |
|
671 |
(if previous 'earlier 'later)) |
|
672 |
((integerp nth) |
|
673 |
(when (> (abs nth) l) |
|
674 |
(user-error "Only %d allowed values for property `%s'" l key)) |
|
675 |
(nth (mod (1- nth) l) allowed)) |
|
676 |
((member value allowed) |
|
677 |
(when (= l 1) (error "Only one allowed value for this property")) |
|
678 |
(or (nth 1 (member value allowed)) (car allowed))) |
|
679 |
(t (car allowed)))) |
|
680 |
(action (lambda () (org-entry-put pom key new)))) |
|
681 |
(cond |
|
682 |
((eq major-mode 'org-agenda-mode) |
|
683 |
(org-columns--call action) |
|
684 |
;; The following let preserves the current format, and makes |
|
685 |
;; sure that in only a single file things need to be updated. |
|
686 |
(let* ((org-agenda-overriding-columns-format org-columns-current-fmt) |
|
687 |
(buffer (marker-buffer pom)) |
|
688 |
(org-agenda-contributing-files |
|
689 |
(list (with-current-buffer buffer |
|
690 |
(buffer-file-name (buffer-base-buffer)))))) |
|
691 |
(org-agenda-columns))) |
|
692 |
(t |
|
693 |
(let ((inhibit-read-only t)) |
|
694 |
(remove-text-properties (line-end-position 0) (line-end-position) |
|
695 |
'(read-only t)) |
|
696 |
(org-columns--call action)) |
|
697 |
;; Some properties can modify headline (e.g., "TODO"), and |
|
698 |
;; possible shuffle overlays. Make sure they are still all at |
|
699 |
;; the right place on the current line. |
|
700 |
(let ((org-columns-inhibit-recalculation)) (org-columns-redo)) |
|
701 |
(org-columns-update key) |
|
702 |
(org-move-to-column column)))))) |
|
703 |
|
|
704 |
(defun org-colview-construct-allowed-dates (s) |
|
705 |
"Construct a list of three dates around the date in S. |
|
706 |
This respects the format of the time stamp in S, active or non-active, |
|
707 |
and also including time or not. S must be just a time stamp, no text |
|
708 |
around it." |
|
709 |
(when (and s (string-match (concat "^" org-ts-regexp3 "$") s)) |
|
710 |
(let* ((time (org-parse-time-string s 'nodefaults)) |
|
711 |
(active (equal (string-to-char s) ?<)) |
|
712 |
(fmt (funcall (if (nth 1 time) 'cdr 'car) org-time-stamp-formats)) |
|
713 |
time-before time-after) |
|
714 |
(unless active (setq fmt (concat "[" (substring fmt 1 -1) "]"))) |
|
715 |
(setf (car time) (or (car time) 0)) |
|
716 |
(setf (nth 1 time) (or (nth 1 time) 0)) |
|
717 |
(setf (nth 2 time) (or (nth 2 time) 0)) |
|
718 |
(setq time-before (copy-sequence time)) |
|
719 |
(setq time-after (copy-sequence time)) |
|
720 |
(setf (nth 3 time-before) (1- (nth 3 time))) |
|
721 |
(setf (nth 3 time-after) (1+ (nth 3 time))) |
|
722 |
(mapcar (lambda (x) (format-time-string fmt (apply 'encode-time x))) |
|
723 |
(list time-before time time-after))))) |
|
724 |
|
|
725 |
(defun org-columns-open-link (&optional arg) |
|
726 |
(interactive "P") |
|
727 |
(let ((value (get-char-property (point) 'org-columns-value))) |
|
728 |
(org-open-link-from-string value arg))) |
|
729 |
|
|
730 |
;;;###autoload |
|
731 |
(defun org-columns-get-format-and-top-level () |
|
732 |
(let ((fmt (org-columns-get-format))) |
|
733 |
(org-columns-goto-top-level) |
|
734 |
fmt)) |
|
735 |
|
|
736 |
(defun org-columns-get-format (&optional fmt-string) |
|
737 |
"Return columns format specifications. |
|
738 |
When optional argument FMT-STRING is non-nil, use it as the |
|
739 |
current specifications. This function also sets |
|
740 |
`org-columns-current-fmt-compiled' and |
|
741 |
`org-columns-current-fmt'." |
|
742 |
(interactive) |
|
743 |
(let ((format |
|
744 |
(or fmt-string |
|
745 |
(org-entry-get nil "COLUMNS" t) |
|
746 |
(org-with-wide-buffer |
|
747 |
(goto-char (point-min)) |
|
748 |
(catch :found |
|
749 |
(let ((case-fold-search t)) |
|
750 |
(while (re-search-forward "^[ \t]*#\\+COLUMNS: .+$" nil t) |
|
751 |
(let ((element (org-element-at-point))) |
|
752 |
(when (eq (org-element-type element) 'keyword) |
|
753 |
(throw :found (org-element-property :value element))))) |
|
754 |
nil))) |
|
755 |
org-columns-default-format))) |
|
756 |
(setq org-columns-current-fmt format) |
|
757 |
(org-columns-compile-format format) |
|
758 |
format)) |
|
759 |
|
|
760 |
(defun org-columns-goto-top-level () |
|
761 |
"Move to the beginning of the column view area. |
|
762 |
Also sets `org-columns-top-level-marker' to the new position." |
|
763 |
(unless (markerp org-columns-top-level-marker) |
|
764 |
(setq org-columns-top-level-marker (make-marker))) |
|
765 |
(goto-char |
|
766 |
(move-marker |
|
767 |
org-columns-top-level-marker |
|
768 |
(cond ((org-before-first-heading-p) (point-min)) |
|
769 |
((org-entry-get nil "COLUMNS" t) org-entry-property-inherited-from) |
|
770 |
(t (org-back-to-heading) (point)))))) |
|
771 |
|
|
772 |
;;;###autoload |
|
773 |
(defun org-columns (&optional global columns-fmt-string) |
|
774 |
"Turn on column view on an Org mode file. |
|
775 |
|
|
776 |
Column view applies to the whole buffer if point is before the |
|
777 |
first headline. Otherwise, it applies to the first ancestor |
|
778 |
setting \"COLUMNS\" property. If there is none, it defaults to |
|
779 |
the current headline. With a `\\[universal-argument]' prefix \ |
|
780 |
argument, turn on column |
|
781 |
view for the whole buffer unconditionally. |
|
782 |
|
|
783 |
When COLUMNS-FMT-STRING is non-nil, use it as the column format." |
|
784 |
(interactive "P") |
|
785 |
(org-columns-remove-overlays) |
|
786 |
(when global (goto-char (point-min))) |
|
787 |
(if (markerp org-columns-begin-marker) |
|
788 |
(move-marker org-columns-begin-marker (point)) |
|
789 |
(setq org-columns-begin-marker (point-marker))) |
|
790 |
(org-columns-goto-top-level) |
|
791 |
;; Initialize `org-columns-current-fmt' and |
|
792 |
;; `org-columns-current-fmt-compiled'. |
|
793 |
(let ((org-columns--time (float-time (current-time)))) |
|
794 |
(org-columns-get-format columns-fmt-string) |
|
795 |
(unless org-columns-inhibit-recalculation (org-columns-compute-all)) |
|
796 |
(save-excursion |
|
797 |
(save-restriction |
|
798 |
(when (and (not global) (org-at-heading-p)) |
|
799 |
(narrow-to-region (point) (org-end-of-subtree t t))) |
|
800 |
(when (assoc "CLOCKSUM" org-columns-current-fmt-compiled) |
|
801 |
(org-clock-sum)) |
|
802 |
(when (assoc "CLOCKSUM_T" org-columns-current-fmt-compiled) |
|
803 |
(org-clock-sum-today)) |
|
804 |
(let ((cache |
|
805 |
;; Collect contents of columns ahead of time so as to |
|
806 |
;; compute their maximum width. |
|
807 |
(org-map-entries |
|
808 |
(lambda () (cons (point) (org-columns--collect-values))) |
|
809 |
nil nil (and org-columns-skip-archived-trees 'archive)))) |
|
810 |
(when cache |
|
811 |
(org-columns--set-widths cache) |
|
812 |
(org-columns--display-here-title) |
|
813 |
(when (setq-local org-columns-flyspell-was-active |
|
814 |
(bound-and-true-p flyspell-mode)) |
|
815 |
(flyspell-mode 0)) |
|
816 |
(unless (local-variable-p 'org-colview-initial-truncate-line-value) |
|
817 |
(setq-local org-colview-initial-truncate-line-value |
|
818 |
truncate-lines)) |
|
819 |
(setq truncate-lines t) |
|
820 |
(dolist (entry cache) |
|
821 |
(goto-char (car entry)) |
|
822 |
(org-columns--display-here (cdr entry))))))))) |
|
823 |
|
|
824 |
(defun org-columns-new (&optional spec &rest attributes) |
|
825 |
"Insert a new column, to the left of the current column. |
|
826 |
Interactively fill attributes for new column. When column format |
|
827 |
specification SPEC is provided, edit it instead. |
|
828 |
|
|
829 |
When optional argument attributes can be a list of columns |
|
830 |
specifications attributes to create the new column |
|
831 |
non-interactively. See `org-columns-compile-format' for |
|
832 |
details." |
|
833 |
(interactive) |
|
834 |
(let ((new (or attributes |
|
835 |
(let ((prop |
|
836 |
(completing-read |
|
837 |
"Property: " |
|
838 |
(mapcar #'list (org-buffer-property-keys t nil t)) |
|
839 |
nil nil (nth 0 spec)))) |
|
840 |
(list prop |
|
841 |
(read-string (format "Column title [%s]: " prop) |
|
842 |
(nth 1 spec)) |
|
843 |
;; Use `read-string' instead of `read-number' |
|
844 |
;; to allow empty width. |
|
845 |
(let ((w (read-string |
|
846 |
"Column width: " |
|
847 |
(and (nth 2 spec) |
|
848 |
(number-to-string (nth 2 spec)))))) |
|
849 |
(and (org-string-nw-p w) (string-to-number w))) |
|
850 |
(org-string-nw-p |
|
851 |
(completing-read |
|
852 |
"Summary: " |
|
853 |
(delete-dups |
|
854 |
(cons '("") ;Allow empty operator. |
|
855 |
(mapcar (lambda (x) (list (car x))) |
|
856 |
(append |
|
857 |
org-columns-summary-types |
|
858 |
org-columns-summary-types-default)))) |
|
859 |
nil t (nth 3 spec))) |
|
860 |
(org-string-nw-p |
|
861 |
(read-string "Format: " (nth 4 spec)))))))) |
|
862 |
(if spec |
|
863 |
(progn (setcar spec (car new)) |
|
864 |
(setcdr spec (cdr new))) |
|
865 |
(push new (nthcdr (current-column) org-columns-current-fmt-compiled))) |
|
866 |
(org-columns-store-format) |
|
867 |
(org-columns-redo))) |
|
868 |
|
|
869 |
(defun org-columns-delete () |
|
870 |
"Delete the column at point from columns view." |
|
871 |
(interactive) |
|
872 |
(let ((spec (nth (current-column) org-columns-current-fmt-compiled))) |
|
873 |
(when (y-or-n-p (format "Are you sure you want to remove column %S? " |
|
874 |
(nth 1 spec))) |
|
875 |
(setq org-columns-current-fmt-compiled |
|
876 |
(delq spec org-columns-current-fmt-compiled)) |
|
877 |
(org-columns-store-format) |
|
878 |
;; This may leave a now wrong value in a node property. However |
|
879 |
;; updating it may prove counter-intuitive. See comments in |
|
880 |
;; `org-columns-move-right' for details. |
|
881 |
(let ((org-columns-inhibit-recalculation t)) (org-columns-redo)) |
|
882 |
(when (>= (current-column) (length org-columns-current-fmt-compiled)) |
|
883 |
(backward-char))))) |
|
884 |
|
|
885 |
(defun org-columns-edit-attributes () |
|
886 |
"Edit the attributes of the current column." |
|
887 |
(interactive) |
|
888 |
(org-columns-new (nth (current-column) org-columns-current-fmt-compiled))) |
|
889 |
|
|
890 |
(defun org-columns-widen (arg) |
|
891 |
"Make the column wider by ARG characters." |
|
892 |
(interactive "p") |
|
893 |
(let* ((n (current-column)) |
|
894 |
(entry (nth n org-columns-current-fmt-compiled)) |
|
895 |
(width (aref org-columns-current-maxwidths n))) |
|
896 |
(setq width (max 1 (+ width arg))) |
|
897 |
(setcar (nthcdr 2 entry) width) |
|
898 |
(org-columns-store-format) |
|
899 |
(let ((org-columns-inhibit-recalculation t)) (org-columns-redo)))) |
|
900 |
|
|
901 |
(defun org-columns-narrow (arg) |
|
902 |
"Make the column narrower by ARG characters." |
|
903 |
(interactive "p") |
|
904 |
(org-columns-widen (- arg))) |
|
905 |
|
|
906 |
(defun org-columns-move-right () |
|
907 |
"Swap this column with the one to the right." |
|
908 |
(interactive) |
|
909 |
(let* ((n (current-column)) |
|
910 |
(cell (nthcdr n org-columns-current-fmt-compiled)) |
|
911 |
e) |
|
912 |
(when (>= n (1- (length org-columns-current-fmt-compiled))) |
|
913 |
(error "Cannot shift this column further to the right")) |
|
914 |
(setq e (car cell)) |
|
915 |
(setcar cell (car (cdr cell))) |
|
916 |
(setcdr cell (cons e (cdr (cdr cell)))) |
|
917 |
(org-columns-store-format) |
|
918 |
;; Do not compute again properties, since we're just moving |
|
919 |
;; columns around. It can put a property value a bit off when |
|
920 |
;; switching between an non-computed and a computed value for the |
|
921 |
;; same property, e.g. from "%A %A{+}" to "%A{+} %A". |
|
922 |
;; |
|
923 |
;; In this case, the value needs to be updated since the first |
|
924 |
;; column related to a property determines how its value is |
|
925 |
;; computed. However, (correctly) updating the value could be |
|
926 |
;; surprising, so we leave it as-is nonetheless. |
|
927 |
(let ((org-columns-inhibit-recalculation t)) (org-columns-redo)) |
|
928 |
(forward-char 1))) |
|
929 |
|
|
930 |
(defun org-columns-move-left () |
|
931 |
"Swap this column with the one to the left." |
|
932 |
(interactive) |
|
933 |
(let* ((n (current-column))) |
|
934 |
(when (= n 0) |
|
935 |
(error "Cannot shift this column further to the left")) |
|
936 |
(backward-char 1) |
|
937 |
(org-columns-move-right) |
|
938 |
(backward-char 1))) |
|
939 |
|
|
940 |
(defun org-columns-store-format () |
|
941 |
"Store the text version of the current columns format. |
|
942 |
The format is stored either in the COLUMNS property of the node |
|
943 |
starting the current column display, or in a #+COLUMNS line of |
|
944 |
the current buffer." |
|
945 |
(let ((fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))) |
|
946 |
(setq-local org-columns-current-fmt fmt) |
|
947 |
(when org-columns-overlays |
|
948 |
(org-with-point-at org-columns-top-level-marker |
|
949 |
(if (and (org-at-heading-p) (org-entry-get nil "COLUMNS")) |
|
950 |
(org-entry-put nil "COLUMNS" fmt) |
|
951 |
(goto-char (point-min)) |
|
952 |
(let ((case-fold-search t)) |
|
953 |
;; Try to replace the first COLUMNS keyword available. |
|
954 |
(catch :found |
|
955 |
(while (re-search-forward "^[ \t]*#\\+COLUMNS:\\(.*\\)" nil t) |
|
956 |
(let ((element (save-match-data (org-element-at-point)))) |
|
957 |
(when (and (eq (org-element-type element) 'keyword) |
|
958 |
(equal (org-element-property :key element) |
|
959 |
"COLUMNS")) |
|
960 |
(replace-match (concat " " fmt) t t nil 1) |
|
961 |
(throw :found nil)))) |
|
962 |
;; No COLUMNS keyword in the buffer. Insert one at the |
|
963 |
;; beginning, right before the first heading, if any. |
|
964 |
(goto-char (point-min)) |
|
965 |
(unless (org-at-heading-p t) (outline-next-heading)) |
|
966 |
(let ((inhibit-read-only t)) |
|
967 |
(insert-before-markers "#+COLUMNS: " fmt "\n")))) |
|
968 |
(setq-local org-columns-default-format fmt)))))) |
|
969 |
|
|
970 |
(defun org-columns-update (property) |
|
971 |
"Recompute PROPERTY, and update the columns display for it." |
|
972 |
(org-columns-compute property) |
|
973 |
(org-with-wide-buffer |
|
974 |
(let ((p (upcase property))) |
|
975 |
(dolist (ov org-columns-overlays) |
|
976 |
(let ((key (overlay-get ov 'org-columns-key))) |
|
977 |
(when (and key (equal key p) (overlay-start ov)) |
|
978 |
(goto-char (overlay-start ov)) |
|
979 |
(let* ((spec (nth (current-column) org-columns-current-fmt-compiled)) |
|
980 |
(value |
|
981 |
(or (cdr (assoc spec |
|
982 |
(get-text-property (line-beginning-position) |
|
983 |
'org-summaries))) |
|
984 |
(org-entry-get (point) key)))) |
|
985 |
(when value |
|
986 |
(let ((displayed (org-columns--displayed-value spec value)) |
|
987 |
(format (overlay-get ov 'org-columns-format)) |
|
988 |
(width |
|
989 |
(aref org-columns-current-maxwidths (current-column)))) |
|
990 |
(overlay-put ov 'org-columns-value value) |
|
991 |
(overlay-put ov 'org-columns-value-modified displayed) |
|
992 |
(overlay-put ov |
|
993 |
'display |
|
994 |
(org-columns--overlay-text |
|
995 |
displayed format width property value))))))))))) |
|
996 |
|
|
997 |
(defun org-columns-redo () |
|
998 |
"Construct the column display again." |
|
999 |
(interactive) |
|
1000 |
(when org-columns-overlays |
|
1001 |
(message "Recomputing columns...") |
|
1002 |
(org-with-point-at org-columns-begin-marker |
|
1003 |
(org-columns-remove-overlays) |
|
1004 |
(if (derived-mode-p 'org-mode) |
|
1005 |
;; Since we already know the columns format, provide it |
|
1006 |
;; instead of computing again. |
|
1007 |
(call-interactively #'org-columns org-columns-current-fmt) |
|
1008 |
(org-agenda-redo) |
|
1009 |
(call-interactively #'org-agenda-columns))) |
|
1010 |
(message "Recomputing columns...done"))) |
|
1011 |
|
|
1012 |
(defun org-columns-uncompile-format (compiled) |
|
1013 |
"Turn the compiled columns format back into a string representation. |
|
1014 |
COMPILED is an alist, as returned by |
|
1015 |
`org-columns-compile-format', which see." |
|
1016 |
(mapconcat |
|
1017 |
(lambda (spec) |
|
1018 |
(pcase spec |
|
1019 |
(`(,prop ,title ,width ,op ,printf) |
|
1020 |
(concat "%" |
|
1021 |
(and width (number-to-string width)) |
|
1022 |
prop |
|
1023 |
(and title (not (equal prop title)) (format "(%s)" title)) |
|
1024 |
(cond ((not op) nil) |
|
1025 |
(printf (format "{%s;%s}" op printf)) |
|
1026 |
(t (format "{%s}" op))))))) |
|
1027 |
compiled " ")) |
|
1028 |
|
|
1029 |
(defun org-columns-compile-format (fmt) |
|
1030 |
"Turn a column format string FMT into an alist of specifications. |
|
1031 |
|
|
1032 |
The alist has one entry for each column in the format. The elements of |
|
1033 |
that list are: |
|
1034 |
property the property name, as an upper-case string |
|
1035 |
title the title field for the columns, as a string |
|
1036 |
width the column width in characters, can be nil for automatic width |
|
1037 |
operator the summary operator, as a string, or nil |
|
1038 |
printf a printf format for computed values, as a string, or nil |
|
1039 |
|
|
1040 |
This function updates `org-columns-current-fmt-compiled'." |
|
1041 |
(setq org-columns-current-fmt-compiled nil) |
|
1042 |
(let ((start 0)) |
|
1043 |
(while (string-match |
|
1044 |
"%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\ |
|
1045 |
\\(?:{\\([^}]+\\)}\\)?\\s-*" |
|
1046 |
fmt start) |
|
1047 |
(setq start (match-end 0)) |
|
1048 |
(let* ((width (and (match-end 1) (string-to-number (match-string 1 fmt)))) |
|
1049 |
(prop (match-string-no-properties 2 fmt)) |
|
1050 |
(title (or (match-string-no-properties 3 fmt) prop)) |
|
1051 |
(operator (match-string-no-properties 4 fmt))) |
|
1052 |
(push (if (not operator) (list (upcase prop) title width nil nil) |
|
1053 |
(let (printf) |
|
1054 |
(when (string-match ";" operator) |
|
1055 |
(setq printf (substring operator (match-end 0))) |
|
1056 |
(setq operator (substring operator 0 (match-beginning 0)))) |
|
1057 |
(list (upcase prop) title width operator printf))) |
|
1058 |
org-columns-current-fmt-compiled))) |
|
1059 |
(setq org-columns-current-fmt-compiled |
|
1060 |
(nreverse org-columns-current-fmt-compiled)))) |
|
1061 |
|
|
1062 |
|
|
1063 |
;;;; Column View Summary |
|
1064 |
|
|
1065 |
(defun org-columns--age-to-minutes (s) |
|
1066 |
"Turn age string S into a number of minutes. |
|
1067 |
An age is either computed from a given time-stamp, or indicated |
|
1068 |
as a canonical duration, i.e., using units defined in |
|
1069 |
`org-duration-canonical-units'." |
|
1070 |
(cond |
|
1071 |
((string-match-p org-ts-regexp s) |
|
1072 |
(/ (- org-columns--time |
|
1073 |
(float-time (apply #'encode-time (org-parse-time-string s)))) |
|
1074 |
60)) |
|
1075 |
((org-duration-p s) (org-duration-to-minutes s t)) ;skip user units |
|
1076 |
(t (user-error "Invalid age: %S" s)))) |
|
1077 |
|
|
1078 |
(defun org-columns--format-age (minutes) |
|
1079 |
"Format MINUTES float as an age string." |
|
1080 |
(org-duration-from-minutes minutes |
|
1081 |
'(("d" . nil) ("h" . nil) ("min" . nil)) |
|
1082 |
t)) ;ignore user's custom units |
|
1083 |
|
|
1084 |
(defun org-columns--summary-apply-times (fun times) |
|
1085 |
"Apply FUN to time values TIMES. |
|
1086 |
Return the result as a duration." |
|
1087 |
(org-duration-from-minutes |
|
1088 |
(apply fun |
|
1089 |
(mapcar (lambda (time) |
|
1090 |
;; Unlike to `org-duration-to-minutes' standard |
|
1091 |
;; behavior, we want to consider plain numbers as |
|
1092 |
;; hours. As a consequence, we treat them |
|
1093 |
;; differently. |
|
1094 |
(if (string-match-p "\\`[0-9]+\\(?:\\.[0-9]*\\)?\\'" time) |
|
1095 |
(* 60 (string-to-number time)) |
|
1096 |
(org-duration-to-minutes time))) |
|
1097 |
times)) |
|
1098 |
(org-duration-h:mm-only-p times))) |
|
1099 |
|
|
1100 |
(defun org-columns--compute-spec (spec &optional update) |
|
1101 |
"Update tree according to SPEC. |
|
1102 |
SPEC is a column format specification. When optional argument |
|
1103 |
UPDATE is non-nil, summarized values can replace existing ones in |
|
1104 |
properties drawers." |
|
1105 |
(let* ((lmax (if (bound-and-true-p org-inlinetask-min-level) |
|
1106 |
org-inlinetask-min-level |
|
1107 |
29)) ;Hard-code deepest level. |
|
1108 |
(lvals (make-vector (1+ lmax) nil)) |
|
1109 |
(level 0) |
|
1110 |
(inminlevel lmax) |
|
1111 |
(last-level lmax) |
|
1112 |
(property (car spec)) |
|
1113 |
(printf (nth 4 spec)) |
|
1114 |
(summarize (org-columns--summarize (nth 3 spec)))) |
|
1115 |
(org-with-wide-buffer |
|
1116 |
;; Find the region to compute. |
|
1117 |
(goto-char org-columns-top-level-marker) |
|
1118 |
(goto-char (condition-case nil (org-end-of-subtree t) (error (point-max)))) |
|
1119 |
;; Walk the tree from the back and do the computations. |
|
1120 |
(while (re-search-backward |
|
1121 |
org-outline-regexp-bol org-columns-top-level-marker t) |
|
1122 |
(unless (or (= level 0) (eq level inminlevel)) |
|
1123 |
(setq last-level level)) |
|
1124 |
(setq level (org-reduced-level (org-outline-level))) |
|
1125 |
(let* ((pos (match-beginning 0)) |
|
1126 |
(value (org-entry-get nil property)) |
|
1127 |
(value-set (org-string-nw-p value))) |
|
1128 |
(cond |
|
1129 |
((< level last-level) |
|
1130 |
;; Collect values from lower levels and inline tasks here |
|
1131 |
;; and summarize them using SUMMARIZE. Store them in text |
|
1132 |
;; property `org-summaries', in alist whose key is SPEC. |
|
1133 |
(let* ((summary |
|
1134 |
(and summarize |
|
1135 |
(let ((values (append (and (/= last-level inminlevel) |
|
1136 |
(aref lvals last-level)) |
|
1137 |
(aref lvals inminlevel)))) |
|
1138 |
(and values (funcall summarize values printf)))))) |
|
1139 |
;; Leaf values are not summaries: do not mark them. |
|
1140 |
(when summary |
|
1141 |
(let* ((summaries-alist (get-text-property pos 'org-summaries)) |
|
1142 |
(old (assoc spec summaries-alist))) |
|
1143 |
(if old (setcdr old summary) |
|
1144 |
(push (cons spec summary) summaries-alist) |
|
1145 |
(org-with-silent-modifications |
|
1146 |
(add-text-properties |
|
1147 |
pos (1+ pos) (list 'org-summaries summaries-alist))))) |
|
1148 |
;; When PROPERTY exists in current node, even if empty, |
|
1149 |
;; but its value doesn't match the one computed, use |
|
1150 |
;; the latter instead. |
|
1151 |
;; |
|
1152 |
;; Ignore leading or trailing white spaces that might |
|
1153 |
;; have been introduced in summary, since those are not |
|
1154 |
;; significant in properties value. |
|
1155 |
(let ((new-value (org-trim summary))) |
|
1156 |
(when (and update value (not (equal value new-value))) |
|
1157 |
(org-entry-put (point) property new-value)))) |
|
1158 |
;; Add current to current level accumulator. |
|
1159 |
(when (or summary value-set) |
|
1160 |
(push (or summary value) (aref lvals level))) |
|
1161 |
;; Clear accumulators for deeper levels. |
|
1162 |
(cl-loop for l from (1+ level) to lmax do (aset lvals l nil)))) |
|
1163 |
(value-set (push value (aref lvals level))) |
|
1164 |
(t nil))))))) |
|
1165 |
|
|
1166 |
;;;###autoload |
|
1167 |
(defun org-columns-compute (property) |
|
1168 |
"Summarize the values of PROPERTY hierarchically. |
|
1169 |
Also update existing values for PROPERTY according to the first |
|
1170 |
column specification." |
|
1171 |
(interactive) |
|
1172 |
(let ((main-flag t) |
|
1173 |
(upcase-prop (upcase property))) |
|
1174 |
(dolist (spec org-columns-current-fmt-compiled) |
|
1175 |
(pcase spec |
|
1176 |
(`(,(pred (equal upcase-prop)) . ,_) |
|
1177 |
(org-columns--compute-spec spec main-flag) |
|
1178 |
;; Only the first summary can update the property value. |
|
1179 |
(when main-flag (setq main-flag nil))))))) |
|
1180 |
|
|
1181 |
(defun org-columns-compute-all () |
|
1182 |
"Compute all columns that have operators defined." |
|
1183 |
(org-with-silent-modifications |
|
1184 |
(remove-text-properties (point-min) (point-max) '(org-summaries t))) |
|
1185 |
(let ((org-columns--time (float-time (current-time))) |
|
1186 |
seen) |
|
1187 |
(dolist (spec org-columns-current-fmt-compiled) |
|
1188 |
(let ((property (car spec))) |
|
1189 |
;; Property value is updated only the first time a given |
|
1190 |
;; property is encountered. |
|
1191 |
(org-columns--compute-spec spec (not (member property seen))) |
|
1192 |
(push property seen))))) |
|
1193 |
|
|
1194 |
(defun org-columns--summary-sum (values printf) |
|
1195 |
"Compute the sum of VALUES. |
|
1196 |
When PRINTF is non-nil, use it to format the result." |
|
1197 |
(format (or printf "%s") (apply #'+ (mapcar #'string-to-number values)))) |
|
1198 |
|
|
1199 |
(defun org-columns--summary-currencies (values _) |
|
1200 |
"Compute the sum of VALUES, with two decimals." |
|
1201 |
(format "%.2f" (apply #'+ (mapcar #'string-to-number values)))) |
|
1202 |
|
|
1203 |
(defun org-columns--summary-checkbox (check-boxes _) |
|
1204 |
"Summarize CHECK-BOXES with a check-box." |
|
1205 |
(let ((done (cl-count "[X]" check-boxes :test #'equal)) |
|
1206 |
(all (length check-boxes))) |
|
1207 |
(cond ((= done all) "[X]") |
|
1208 |
((> done 0) "[-]") |
|
1209 |
(t "[ ]")))) |
|
1210 |
|
|
1211 |
(defun org-columns--summary-checkbox-count (check-boxes _) |
|
1212 |
"Summarize CHECK-BOXES with a check-box cookie." |
|
1213 |
(format "[%d/%d]" |
|
1214 |
(cl-count-if (lambda (b) (or (equal b "[X]") |
|
1215 |
(string-match-p "\\[\\([1-9]\\)/\\1\\]" b))) |
|
1216 |
check-boxes) |
|
1217 |
(length check-boxes))) |
|
1218 |
|
|
1219 |
(defun org-columns--summary-checkbox-percent (check-boxes _) |
|
1220 |
"Summarize CHECK-BOXES with a check-box percent." |
|
1221 |
(format "[%d%%]" |
|
1222 |
(round (* 100.0 (cl-count-if (lambda (b) (member b '("[X]" "[100%]"))) |
|
1223 |
check-boxes)) |
|
1224 |
(length check-boxes)))) |
|
1225 |
|
|
1226 |
(defun org-columns--summary-min (values printf) |
|
1227 |
"Compute the minimum of VALUES. |
|
1228 |
When PRINTF is non-nil, use it to format the result." |
|
1229 |
(format (or printf "%s") |
|
1230 |
(apply #'min (mapcar #'string-to-number values)))) |
|
1231 |
|
|
1232 |
(defun org-columns--summary-max (values printf) |
|
1233 |
"Compute the maximum of VALUES. |
|
1234 |
When PRINTF is non-nil, use it to format the result." |
|
1235 |
(format (or printf "%s") |
|
1236 |
(apply #'max (mapcar #'string-to-number values)))) |
|
1237 |
|
|
1238 |
(defun org-columns--summary-mean (values printf) |
|
1239 |
"Compute the mean of VALUES. |
|
1240 |
When PRINTF is non-nil, use it to format the result." |
|
1241 |
(format (or printf "%s") |
|
1242 |
(/ (apply #'+ (mapcar #'string-to-number values)) |
|
1243 |
(float (length values))))) |
|
1244 |
|
|
1245 |
(defun org-columns--summary-sum-times (times _) |
|
1246 |
"Sum TIMES." |
|
1247 |
(org-columns--summary-apply-times #'+ times)) |
|
1248 |
|
|
1249 |
(defun org-columns--summary-min-time (times _) |
|
1250 |
"Compute the minimum time among TIMES." |
|
1251 |
(org-columns--summary-apply-times #'min times)) |
|
1252 |
|
|
1253 |
(defun org-columns--summary-max-time (times _) |
|
1254 |
"Compute the maximum time among TIMES." |
|
1255 |
(org-columns--summary-apply-times #'max times)) |
|
1256 |
|
|
1257 |
(defun org-columns--summary-mean-time (times _) |
|
1258 |
"Compute the mean time among TIMES." |
|
1259 |
(org-columns--summary-apply-times |
|
1260 |
(lambda (&rest values) (/ (apply #'+ values) (float (length values)))) |
|
1261 |
times)) |
|
1262 |
|
|
1263 |
(defun org-columns--summary-min-age (ages _) |
|
1264 |
"Compute the minimum time among AGES." |
|
1265 |
(org-columns--format-age |
|
1266 |
(apply #'min (mapcar #'org-columns--age-to-minutes ages)))) |
|
1267 |
|
|
1268 |
(defun org-columns--summary-max-age (ages _) |
|
1269 |
"Compute the maximum time among AGES." |
|
1270 |
(org-columns--format-age |
|
1271 |
(apply #'max (mapcar #'org-columns--age-to-minutes ages)))) |
|
1272 |
|
|
1273 |
(defun org-columns--summary-mean-age (ages _) |
|
1274 |
"Compute the minimum time among AGES." |
|
1275 |
(org-columns--format-age |
|
1276 |
(/ (apply #'+ (mapcar #'org-columns--age-to-minutes ages)) |
|
1277 |
(float (length ages))))) |
|
1278 |
|
|
1279 |
(defun org-columns--summary-estimate (estimates _) |
|
1280 |
"Combine a list of estimates, using mean and variance. |
|
1281 |
The mean and variance of the result will be the sum of the means |
|
1282 |
and variances (respectively) of the individual estimates." |
|
1283 |
(let ((mean 0) |
|
1284 |
(var 0)) |
|
1285 |
(dolist (e estimates) |
|
1286 |
(pcase (mapcar #'string-to-number (split-string e "-")) |
|
1287 |
(`(,low ,high) |
|
1288 |
(let ((m (/ (+ low high) 2.0))) |
|
1289 |
(cl-incf mean m) |
|
1290 |
(cl-incf var (- (/ (+ (* low low) (* high high)) 2.0) (* m m))))) |
|
1291 |
(`(,value) (cl-incf mean value)))) |
|
1292 |
(let ((sd (sqrt var))) |
|
1293 |
(format "%s-%s" |
|
1294 |
(format "%.0f" (- mean sd)) |
|
1295 |
(format "%.0f" (+ mean sd)))))) |
|
1296 |
|
|
1297 |
|
|
1298 |
|
|
1299 |
;;; Dynamic block for Column view |
|
1300 |
|
|
1301 |
(defun org-columns--capture-view (maxlevel skip-empty format local) |
|
1302 |
"Get the column view of the current buffer. |
|
1303 |
|
|
1304 |
MAXLEVEL sets the level limit. SKIP-EMPTY tells whether to skip |
|
1305 |
empty rows, an empty row being one where all the column view |
|
1306 |
specifiers but ITEM are empty. FORMAT is a format string for |
|
1307 |
columns, or nil. When LOCAL is non-nil, only capture headings in |
|
1308 |
current subtree. |
|
1309 |
|
|
1310 |
This function returns a list containing the title row and all |
|
1311 |
other rows. Each row is a list of fields, as strings, or |
|
1312 |
`hline'." |
|
1313 |
(org-columns (not local) format) |
|
1314 |
(goto-char org-columns-top-level-marker) |
|
1315 |
(let ((columns (length org-columns-current-fmt-compiled)) |
|
1316 |
(has-item (assoc "ITEM" org-columns-current-fmt-compiled)) |
|
1317 |
table) |
|
1318 |
(org-map-entries |
|
1319 |
(lambda () |
|
1320 |
(when (get-char-property (point) 'org-columns-key) |
|
1321 |
(let (row) |
|
1322 |
(dotimes (i columns) |
|
1323 |
(let* ((col (+ (line-beginning-position) i)) |
|
1324 |
(p (get-char-property col 'org-columns-key))) |
|
1325 |
(push (org-quote-vert |
|
1326 |
(get-char-property col |
|
1327 |
(if (string= p "ITEM") |
|
1328 |
'org-columns-value |
|
1329 |
'org-columns-value-modified))) |
|
1330 |
row))) |
|
1331 |
(unless (and skip-empty |
|
1332 |
(let ((r (delete-dups (remove "" row)))) |
|
1333 |
(or (null r) (and has-item (= (length r) 1))))) |
|
1334 |
(push (cons (org-reduced-level (org-current-level)) (nreverse row)) |
|
1335 |
table))))) |
|
1336 |
(and maxlevel (format "LEVEL<=%d" maxlevel)) |
|
1337 |
(and local 'tree) |
|
1338 |
'archive 'comment) |
|
1339 |
(org-columns-quit) |
|
1340 |
;; Add column titles and a horizontal rule in front of the table. |
|
1341 |
(cons (mapcar #'cadr org-columns-current-fmt-compiled) |
|
1342 |
(cons 'hline (nreverse table))))) |
|
1343 |
|
|
1344 |
(defun org-columns--clean-item (item) |
|
1345 |
"Remove sensitive contents from string ITEM. |
|
1346 |
This includes objects that may not be duplicated within |
|
1347 |
a document, e.g., a target, or those forbidden in tables, e.g., |
|
1348 |
an inline src-block." |
|
1349 |
(let ((data (org-element-parse-secondary-string |
|
1350 |
item (org-element-restriction 'headline)))) |
|
1351 |
(org-element-map data |
|
1352 |
'(footnote-reference inline-babel-call inline-src-block target |
|
1353 |
radio-target statistics-cookie) |
|
1354 |
#'org-element-extract-element) |
|
1355 |
(org-no-properties (org-element-interpret-data data)))) |
|
1356 |
|
|
1357 |
;;;###autoload |
|
1358 |
(defun org-dblock-write:columnview (params) |
|
1359 |
"Write the column view table. |
|
1360 |
PARAMS is a property list of parameters: |
|
1361 |
|
|
1362 |
:id the :ID: property of the entry where the columns view |
|
1363 |
should be built. When the symbol `local', call locally. |
|
1364 |
When `global' call column view with the cursor at the beginning |
|
1365 |
of the buffer (usually this means that the whole buffer switches |
|
1366 |
to column view). When \"file:path/to/file.org\", invoke column |
|
1367 |
view at the start of that file. Otherwise, the ID is located |
|
1368 |
using `org-id-find'. |
|
1369 |
:hlines When t, insert a hline before each item. When a number, insert |
|
1370 |
a hline before each level <= that number. |
|
1371 |
:indent When non-nil, indent each ITEM field according to its level. |
|
1372 |
:vlines When t, make each column a colgroup to enforce vertical lines. |
|
1373 |
:maxlevel When set to a number, don't capture headlines below this level. |
|
1374 |
:skip-empty-rows |
|
1375 |
When t, skip rows where all specifiers other than ITEM are empty. |
|
1376 |
:width apply widths specified in columns format using <N> specifiers. |
|
1377 |
:format When non-nil, specify the column view format to use." |
|
1378 |
(let ((table |
|
1379 |
(let ((id (plist-get params :id)) |
|
1380 |
view-file view-pos) |
|
1381 |
(pcase id |
|
1382 |
(`global nil) |
|
1383 |
((or `local `nil) (setq view-pos (point))) |
|
1384 |
((and (let id-string (format "%s" id)) |
|
1385 |
(guard (string-match "^file:\\(.*\\)" id-string))) |
|
1386 |
(setq view-file (match-string-no-properties 1 id-string)) |
|
1387 |
(unless (file-exists-p view-file) |
|
1388 |
(user-error "No such file: %S" id-string))) |
|
1389 |
((and (let idpos (org-find-entry-with-id id)) (guard idpos)) |
|
1390 |
(setq view-pos idpos)) |
|
1391 |
((let `(,filename . ,position) (org-id-find id)) |
|
1392 |
(setq view-file filename) |
|
1393 |
(setq view-pos position)) |
|
1394 |
(_ (user-error "Cannot find entry with :ID: %s" id))) |
|
1395 |
(with-current-buffer (if view-file (get-file-buffer view-file) |
|
1396 |
(current-buffer)) |
|
1397 |
(org-with-wide-buffer |
|
1398 |
(when view-pos (goto-char view-pos)) |
|
1399 |
(org-columns--capture-view (plist-get params :maxlevel) |
|
1400 |
(plist-get params :skip-empty-rows) |
|
1401 |
(plist-get params :format) |
|
1402 |
view-pos)))))) |
|
1403 |
(when table |
|
1404 |
;; Prune level information from the table. Also normalize |
|
1405 |
;; headings: remove stars, add indentation entities, if |
|
1406 |
;; required, and possibly precede some of them with a horizontal |
|
1407 |
;; rule. |
|
1408 |
(let ((item-index |
|
1409 |
(let ((p (assoc "ITEM" org-columns-current-fmt-compiled))) |
|
1410 |
(and p (cl-position p |
|
1411 |
org-columns-current-fmt-compiled |
|
1412 |
:test #'equal)))) |
|
1413 |
(hlines (plist-get params :hlines)) |
|
1414 |
(indent (plist-get params :indent)) |
|
1415 |
new-table) |
|
1416 |
;; Copy header and first rule. |
|
1417 |
(push (pop table) new-table) |
|
1418 |
(push (pop table) new-table) |
|
1419 |
(dolist (row table (setq table (nreverse new-table))) |
|
1420 |
(let ((level (car row))) |
|
1421 |
(when (and (not (eq (car new-table) 'hline)) |
|
1422 |
(or (eq hlines t) |
|
1423 |
(and (numberp hlines) (<= level hlines)))) |
|
1424 |
(push 'hline new-table)) |
|
1425 |
(when item-index |
|
1426 |
(let ((item (org-columns--clean-item (nth item-index (cdr row))))) |
|
1427 |
(setf (nth item-index (cdr row)) |
|
1428 |
(if (and indent (> level 1)) |
|
1429 |
(concat "\\_" (make-string (* 2 (1- level)) ?\s) item) |
|
1430 |
item)))) |
|
1431 |
(push (cdr row) new-table)))) |
|
1432 |
(when (plist-get params :width) |
|
1433 |
(setq table |
|
1434 |
(append table |
|
1435 |
(list |
|
1436 |
(mapcar (lambda (spec) |
|
1437 |
(let ((w (nth 2 spec))) |
|
1438 |
(if w (format "<%d>" (max 3 w)) ""))) |
|
1439 |
org-columns-current-fmt-compiled))))) |
|
1440 |
(when (plist-get params :vlines) |
|
1441 |
(setq table |
|
1442 |
(let ((size (length org-columns-current-fmt-compiled))) |
|
1443 |
(append (mapcar (lambda (x) (if (eq 'hline x) x (cons "" x))) |
|
1444 |
table) |
|
1445 |
(list (cons "/" (make-list size "<>"))))))) |
|
1446 |
(let ((content-lines (org-split-string (plist-get params :content) "\n")) |
|
1447 |
recalc) |
|
1448 |
;; Insert affiliated keywords before the table. |
|
1449 |
(when content-lines |
|
1450 |
(while (string-match-p "\\`[ \t]*#\\+" (car content-lines)) |
|
1451 |
(insert (pop content-lines) "\n"))) |
|
1452 |
(save-excursion |
|
1453 |
;; Insert table at point. |
|
1454 |
(insert |
|
1455 |
(mapconcat (lambda (row) |
|
1456 |
(if (eq row 'hline) "|-|" |
|
1457 |
(format "|%s|" (mapconcat #'identity row "|")))) |
|
1458 |
table |
|
1459 |
"\n")) |
|
1460 |
;; Insert TBLFM lines following table. |
|
1461 |
(let ((case-fold-search t)) |
|
1462 |
(dolist (line content-lines) |
|
1463 |
(when (string-match-p "\\`[ \t]*#\\+TBLFM:" line) |
|
1464 |
(insert "\n" line) |
|
1465 |
(unless recalc (setq recalc t)))))) |
|
1466 |
(when recalc (org-table-recalculate 'all t)) |
|
1467 |
(org-table-align))))) |
|
1468 |
|
|
1469 |
;;;###autoload |
|
1470 |
(defun org-columns-insert-dblock () |
|
1471 |
"Create a dynamic block capturing a column view table." |
|
1472 |
(interactive) |
|
1473 |
(let ((id (completing-read |
|
1474 |
"Capture columns (local, global, entry with :ID: property) [local]: " |
|
1475 |
(append '(("global") ("local")) |
|
1476 |
(mapcar #'list (org-property-values "ID")))))) |
|
1477 |
(org-create-dblock |
|
1478 |
(list :name "columnview" |
|
1479 |
:hlines 1 |
|
1480 |
:id (cond ((string= id "global") 'global) |
|
1481 |
((member id '("" "local")) 'local) |
|
1482 |
(id))))) |
|
1483 |
(org-update-dblock)) |
|
1484 |
|
|
1485 |
|
|
1486 |
|
|
1487 |
;;; Column view in the agenda |
|
1488 |
|
|
1489 |
;;;###autoload |
|
1490 |
(defun org-agenda-columns () |
|
1491 |
"Turn on or update column view in the agenda." |
|
1492 |
(interactive) |
|
1493 |
(org-columns-remove-overlays) |
|
1494 |
(if (markerp org-columns-begin-marker) |
|
1495 |
(move-marker org-columns-begin-marker (point)) |
|
1496 |
(setq org-columns-begin-marker (point-marker))) |
|
1497 |
(let* ((org-columns--time (float-time (current-time))) |
|
1498 |
(fmt |
|
1499 |
(cond |
|
1500 |
((bound-and-true-p org-agenda-overriding-columns-format)) |
|
1501 |
((let ((m (org-get-at-bol 'org-hd-marker))) |
|
1502 |
(and m |
|
1503 |
(or (org-entry-get m "COLUMNS" t) |
|
1504 |
(with-current-buffer (marker-buffer m) |
|
1505 |
org-columns-default-format))))) |
|
1506 |
((and (local-variable-p 'org-columns-current-fmt) |
|
1507 |
org-columns-current-fmt)) |
|
1508 |
((let ((m (next-single-property-change (point-min) 'org-hd-marker))) |
|
1509 |
(and m |
|
1510 |
(let ((m (get-text-property m 'org-hd-marker))) |
|
1511 |
(or (org-entry-get m "COLUMNS" t) |
|
1512 |
(with-current-buffer (marker-buffer m) |
|
1513 |
org-columns-default-format)))))) |
|
1514 |
(t org-columns-default-format))) |
|
1515 |
(compiled-fmt (org-columns-compile-format fmt))) |
|
1516 |
(setq org-columns-current-fmt fmt) |
|
1517 |
(when org-agenda-columns-compute-summary-properties |
|
1518 |
(org-agenda-colview-compute org-columns-current-fmt-compiled)) |
|
1519 |
(save-excursion |
|
1520 |
;; Collect properties for each headline in current view. |
|
1521 |
(goto-char (point-min)) |
|
1522 |
(let (cache) |
|
1523 |
(while (not (eobp)) |
|
1524 |
(let ((m (org-get-at-bol 'org-hd-marker))) |
|
1525 |
(when m |
|
1526 |
(push (cons (line-beginning-position) |
|
1527 |
;; `org-columns-current-fmt-compiled' is |
|
1528 |
;; initialized but only set locally to the |
|
1529 |
;; agenda buffer. Since current buffer is |
|
1530 |
;; changing, we need to force the original |
|
1531 |
;; compiled-fmt there. |
|
1532 |
(org-with-point-at m |
|
1533 |
(org-columns--collect-values compiled-fmt))) |
|
1534 |
cache))) |
|
1535 |
(forward-line)) |
|
1536 |
(when cache |
|
1537 |
(org-columns--set-widths cache) |
|
1538 |
(org-columns--display-here-title) |
|
1539 |
(when (setq-local org-columns-flyspell-was-active |
|
1540 |
(bound-and-true-p flyspell-mode)) |
|
1541 |
(flyspell-mode 0)) |
|
1542 |
(dolist (entry cache) |
|
1543 |
(goto-char (car entry)) |
|
1544 |
(org-columns--display-here (cdr entry))) |
|
1545 |
(when org-agenda-columns-show-summaries |
|
1546 |
(org-agenda-colview-summarize cache))))))) |
|
1547 |
|
|
1548 |
(defun org-agenda-colview-summarize (cache) |
|
1549 |
"Summarize the summarizable columns in column view in the agenda. |
|
1550 |
This will add overlays to the date lines, to show the summary for each day." |
|
1551 |
(let ((fmt (mapcar |
|
1552 |
(lambda (spec) |
|
1553 |
(pcase spec |
|
1554 |
(`(,property ,title ,width . ,_) |
|
1555 |
(if (member property '("CLOCKSUM" "CLOCKSUM_T")) |
|
1556 |
(list property title width ":" nil) |
|
1557 |
spec)))) |
|
1558 |
org-columns-current-fmt-compiled))) |
|
1559 |
;; Ensure there's at least one summation column. |
|
1560 |
(when (cl-some (lambda (spec) (nth 3 spec)) fmt) |
|
1561 |
(goto-char (point-max)) |
|
1562 |
(catch :complete |
|
1563 |
(while t |
|
1564 |
(when (or (get-text-property (point) 'org-date-line) |
|
1565 |
(eq (get-text-property (point) 'face) |
|
1566 |
'org-agenda-structure)) |
|
1567 |
;; OK, this is a date line that should be used. |
|
1568 |
(let (entries) |
|
1569 |
(let (rest) |
|
1570 |
(dolist (c cache) |
|
1571 |
(if (> (car c) (point)) |
|
1572 |
(push c entries) |
|
1573 |
(push c rest))) |
|
1574 |
(setq cache rest)) |
|
1575 |
;; ENTRIES contains entries below the current one. |
|
1576 |
;; CACHE is the rest. Compute the summaries for the |
|
1577 |
;; properties we want, set nil properties for the rest. |
|
1578 |
(when (setq entries (mapcar #'cdr entries)) |
|
1579 |
(org-columns--display-here |
|
1580 |
(mapcar |
|
1581 |
(lambda (spec) |
|
1582 |
(pcase spec |
|
1583 |
(`("ITEM" . ,_) |
|
1584 |
;; Replace ITEM with current date. Preserve |
|
1585 |
;; properties for fontification. |
|
1586 |
(let ((date (buffer-substring |
|
1587 |
(line-beginning-position) |
|
1588 |
(line-end-position)))) |
|
1589 |
(list spec date date))) |
|
1590 |
(`(,_ ,_ ,_ nil ,_) (list spec "" "")) |
|
1591 |
(`(,_ ,_ ,_ ,operator ,printf) |
|
1592 |
(let* ((summarize (org-columns--summarize operator)) |
|
1593 |
(values |
|
1594 |
;; Use real values for summary, not |
|
1595 |
;; those prepared for display. |
|
1596 |
(delq nil |
|
1597 |
(mapcar |
|
1598 |
(lambda (e) (org-string-nw-p |
|
1599 |
(nth 1 (assoc spec e)))) |
|
1600 |
entries))) |
|
1601 |
(final (if values |
|
1602 |
(funcall summarize values printf) |
|
1603 |
""))) |
|
1604 |
(unless (equal final "") |
|
1605 |
(put-text-property 0 (length final) |
|
1606 |
'face 'bold final)) |
|
1607 |
(list spec final final))))) |
|
1608 |
fmt) |
|
1609 |
'dateline) |
|
1610 |
(setq-local org-agenda-columns-active t)))) |
|
1611 |
(if (bobp) (throw :complete t) (forward-line -1))))))) |
|
1612 |
|
|
1613 |
(defun org-agenda-colview-compute (fmt) |
|
1614 |
"Compute the relevant columns in the contributing source buffers." |
|
1615 |
(dolist (file org-agenda-contributing-files) |
|
1616 |
(let ((b (find-buffer-visiting file))) |
|
1617 |
(with-current-buffer (or (buffer-base-buffer b) b) |
|
1618 |
(org-with-wide-buffer |
|
1619 |
(org-with-silent-modifications |
|
1620 |
(remove-text-properties (point-min) (point-max) '(org-summaries t))) |
|
1621 |
(goto-char (point-min)) |
|
1622 |
(org-columns-get-format-and-top-level) |
|
1623 |
(dolist (spec fmt) |
|
1624 |
(let ((prop (car spec))) |
|
1625 |
(cond |
|
1626 |
((equal prop "CLOCKSUM") (org-clock-sum)) |
|
1627 |
((equal prop "CLOCKSUM_T") (org-clock-sum-today)) |
|
1628 |
((and (nth 3 spec) |
|
1629 |
(let ((a (assoc prop org-columns-current-fmt-compiled))) |
|
1630 |
(equal (nth 3 a) (nth 3 spec)))) |
|
1631 |
(org-columns-compute prop)))))))))) |
|
1632 |
|
|
1633 |
|
|
1634 |
(provide 'org-colview) |
|
1635 |
|
|
1636 |
;;; org-colview.el ends here |