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