org-colview.el 66 KB

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