org-colview.el 59 KB

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