org-colview.el 59 KB

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