org-colview.el 59 KB

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