org-colview.el 59 KB

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