org-colview.el 59 KB

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