org-colview.el 65 KB

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