org-colview.el 59 KB

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