org-colview.el 59 KB

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