org-colview.el 60 KB

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