org-colview.el 59 KB

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