org-colview.el 57 KB

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