org-colview.el 58 KB

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