org-clock.el 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. ;;; org-clock.el --- The time clocking code for Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 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. ;; Version: 6.13pre02
  7. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;; Commentary:
  23. ;; This file contains the time clocking code for Org-mode
  24. (require 'org)
  25. (eval-when-compile
  26. (require 'cl)
  27. (require 'calendar))
  28. (declare-function calendar-absolute-from-iso "cal-iso" (&optional date))
  29. (defvar org-time-stamp-formats)
  30. (defgroup org-clock nil
  31. "Options concerning clocking working time in Org-mode."
  32. :tag "Org Clock"
  33. :group 'org-progress)
  34. (defcustom org-clock-into-drawer 2
  35. "Should clocking info be wrapped into a drawer?
  36. When t, clocking info will always be inserted into a :CLOCK: drawer.
  37. If necessary, the drawer will be created.
  38. When nil, the drawer will not be created, but used when present.
  39. When an integer and the number of clocking entries in an item
  40. reaches or exceeds this number, a drawer will be created."
  41. :group 'org-todo
  42. :group 'org-clock
  43. :type '(choice
  44. (const :tag "Always" t)
  45. (const :tag "Only when drawer exists" nil)
  46. (integer :tag "When at least N clock entries")))
  47. (defcustom org-clock-out-when-done t
  48. "When non-nil, the clock will be stopped when the relevant entry is marked DONE.
  49. A nil value means, clock will keep running until stopped explicitly with
  50. `C-c C-x C-o', or until the clock is started in a different item."
  51. :group 'org-clock
  52. :type 'boolean)
  53. (defcustom org-clock-out-remove-zero-time-clocks nil
  54. "Non-nil means, remove the clock line when the resulting time is zero."
  55. :group 'org-clock
  56. :type 'boolean)
  57. (defcustom org-clock-in-switch-to-state nil
  58. "Set task to a special todo state while clocking it.
  59. The value should be the state to which the entry should be
  60. switched. If the value is a function, it must take one
  61. parameter (the current TODO state of the item) and return the
  62. state to switch it to."
  63. :group 'org-clock
  64. :group 'org-todo
  65. :type '(choice
  66. (const :tag "Don't force a state" nil)
  67. (string :tag "State")
  68. (symbol :tag "Function")))
  69. (defcustom org-clock-history-length 5
  70. "Number of clock tasks to remember in history."
  71. :group 'org-clock
  72. :type 'integer)
  73. (defcustom org-clock-heading-function nil
  74. "When non-nil, should be a function to create `org-clock-heading'.
  75. This is the string shown in the mode line when a clock is running.
  76. The function is called with point at the beginning of the headline."
  77. :group 'org-clock
  78. :type 'function)
  79. (defcustom org-clock-string-limit 0
  80. "Maximum length of clock strings in the modeline. 0 means no limit"
  81. :group 'org-clock
  82. :type 'integer)
  83. (defcustom org-clock-in-resume nil
  84. "If non-nil, when clocking into a task with a clock entry which
  85. has not been closed, resume the clock from that point"
  86. :group 'org-clock
  87. :type 'boolean)
  88. (defcustom org-clock-persist nil
  89. "When non-nil, save the running clock when emacs is closed, and
  90. resume it next time emacs is started."
  91. :group 'org-clock
  92. :type 'boolean)
  93. (defcustom org-clock-persist-file "~/.emacs.d/org-clock-save.el"
  94. "File to save clock data to"
  95. :group 'org-clock
  96. :type 'string)
  97. (defcustom org-clock-persist-query-save nil
  98. "When non-nil, ask before saving the current clock on exit"
  99. :group 'org-clock
  100. :type 'boolean)
  101. (defcustom org-clock-persist-query-resume t
  102. "When non-nil, ask before resuming any stored clock during
  103. load."
  104. :group 'org-clock
  105. :type 'boolean)
  106. ;;; The clock for measuring work time.
  107. (defvar org-mode-line-string "")
  108. (put 'org-mode-line-string 'risky-local-variable t)
  109. (defvar org-mode-line-timer nil)
  110. (defvar org-clock-heading "")
  111. (defvar org-clock-heading-for-remember "")
  112. (defvar org-clock-start-time "")
  113. (defvar org-clock-history nil
  114. "List of marker pointing to recent clocked tasks.")
  115. (defvar org-clock-default-task (make-marker)
  116. "Marker pointing to the default task that should clock time.
  117. The clock can be made to switch to this task after clocking out
  118. of a different task.")
  119. (defvar org-clock-interrupted-task (make-marker)
  120. "Marker pointing to the task that has been interrupted by the current clock.")
  121. (defvar org-clock-mode-map (make-sparse-keymap))
  122. (define-key org-clock-mode-map [mode-line mouse-2] 'org-clock-goto)
  123. (defun org-clock-history-push (&optional pos buffer)
  124. "Push a marker to the clock history."
  125. (setq org-clock-history-length (max 1 (min 35 org-clock-history-length)))
  126. (let ((m (move-marker (make-marker) (or pos (point)) buffer)) n l)
  127. (while (setq n (member m org-clock-history))
  128. (move-marker (car n) nil))
  129. (setq org-clock-history
  130. (delq nil
  131. (mapcar (lambda (x) (if (marker-buffer x) x nil))
  132. org-clock-history)))
  133. (when (>= (setq l (length org-clock-history)) org-clock-history-length)
  134. (setq org-clock-history
  135. (nreverse
  136. (nthcdr (- l org-clock-history-length -1)
  137. (nreverse org-clock-history)))))
  138. (push m org-clock-history)))
  139. (defun org-clock-save-markers-for-cut-and-paste (beg end)
  140. "Save relative positions of markers in region."
  141. (org-check-and-save-marker org-clock-marker beg end)
  142. (org-check-and-save-marker org-clock-default-task beg end)
  143. (org-check-and-save-marker org-clock-interrupted-task beg end)
  144. (mapc (lambda (m) (org-check-and-save-marker m beg end))
  145. org-clock-history))
  146. (defun org-clock-select-task (&optional prompt)
  147. "Select a task that recently was associated with clocking."
  148. (interactive)
  149. (let (sel-list rpl file task (i 0) s)
  150. (save-window-excursion
  151. (org-switch-to-buffer-other-window
  152. (get-buffer-create "*Clock Task Select*"))
  153. (erase-buffer)
  154. (when (marker-buffer org-clock-default-task)
  155. (insert (org-add-props "Default Task\n" nil 'face 'bold))
  156. (setq s (org-clock-insert-selection-line ?d org-clock-default-task))
  157. (push s sel-list))
  158. (when (marker-buffer org-clock-interrupted-task)
  159. (insert (org-add-props "The task interrupted by starting the last one\n" nil 'face 'bold))
  160. (setq s (org-clock-insert-selection-line ?i org-clock-interrupted-task))
  161. (push s sel-list))
  162. (when (marker-buffer org-clock-marker)
  163. (insert (org-add-props "Current Clocking Task\n" nil 'face 'bold))
  164. (setq s (org-clock-insert-selection-line ?c org-clock-marker))
  165. (push s sel-list))
  166. (insert (org-add-props "Recent Tasks\n" nil 'face 'bold))
  167. (mapc
  168. (lambda (m)
  169. (when (marker-buffer m)
  170. (setq i (1+ i)
  171. s (org-clock-insert-selection-line
  172. (if (< i 10)
  173. (+ i ?0)
  174. (+ i (- ?A 10))) m))
  175. (push s sel-list)))
  176. org-clock-history)
  177. (org-fit-window-to-buffer)
  178. (message (or prompt "Select task for clocking:"))
  179. (setq rpl (read-char-exclusive))
  180. (cond
  181. ((eq rpl ?q) nil)
  182. ((eq rpl ?x) nil)
  183. ((assoc rpl sel-list) (cdr (assoc rpl sel-list)))
  184. (t (error "Invalid task choice %c" rpl))))))
  185. (defun org-clock-insert-selection-line (i marker)
  186. (when (marker-buffer marker)
  187. (let (file cat task)
  188. (with-current-buffer (org-base-buffer (marker-buffer marker))
  189. (save-excursion
  190. (save-restriction
  191. (widen)
  192. (goto-char marker)
  193. (setq file (buffer-file-name (marker-buffer marker))
  194. cat (or (org-get-category)
  195. (progn (org-refresh-category-properties)
  196. (org-get-category)))
  197. task (org-get-heading 'notags)))))
  198. (when (and cat task)
  199. (insert (format "[%c] %-15s %s\n" i cat task))
  200. (cons i marker)))))
  201. (defun org-update-mode-line ()
  202. (let* ((delta (- (time-to-seconds (current-time))
  203. (time-to-seconds org-clock-start-time)))
  204. (h (floor delta 3600))
  205. (m (floor (- delta (* 3600 h)) 60)))
  206. (setq org-mode-line-string
  207. (org-propertize
  208. (let ((clock-string (format (concat "-[" org-time-clocksum-format " (%s)]")
  209. h m org-clock-heading))
  210. (help-text "Org-mode clock is running. Mouse-2 to go there."))
  211. (if (and (> org-clock-string-limit 0)
  212. (> (length clock-string) org-clock-string-limit))
  213. (org-propertize (substring clock-string 0 org-clock-string-limit)
  214. 'help-echo (concat help-text ": " org-clock-heading))
  215. (org-propertize clock-string 'help-echo help-text)))
  216. 'local-map org-clock-mode-map
  217. 'mouse-face '(face mode-line-highlight)))
  218. (force-mode-line-update)))
  219. (defvar org-clock-mode-line-entry nil
  220. "Information for the modeline about the running clock.")
  221. (defun org-clock-in (&optional select)
  222. "Start the clock on the current item.
  223. If necessary, clock-out of the currently active clock.
  224. With prefix arg SELECT, offer a list of recently clocked tasks to
  225. clock into. When SELECT is `C-u C-u', clock into the current task and mark
  226. is as the default task, a special task that will always be offered in
  227. the clocking selection, associated with the letter `d'."
  228. (interactive "P")
  229. (let ((interrupting (marker-buffer org-clock-marker))
  230. ts selected-task target-pos)
  231. (when (equal select '(4))
  232. (setq selected-task (org-clock-select-task "Clock-in on task: "))
  233. (if selected-task
  234. (setq selected-task (copy-marker selected-task))
  235. (error "Abort")))
  236. (when interrupting
  237. ;; We are interrupting the clocking of a differnt task.
  238. ;; Save a marker to this task, so that we can go back.
  239. (move-marker org-clock-interrupted-task
  240. (marker-position org-clock-marker)
  241. (marker-buffer org-clock-marker))
  242. (org-clock-out t))
  243. (when (equal select '(16))
  244. ;; Mark as default clocking task
  245. (save-excursion
  246. (org-back-to-heading t)
  247. (move-marker org-clock-default-task (point))))
  248. (setq target-pos (point)) ;; we want to clock in at this location
  249. (save-excursion
  250. (when (and selected-task (marker-buffer selected-task))
  251. ;; There is a selected task, move to the correct buffer
  252. ;; and set the new target position.
  253. (set-buffer (org-base-buffer (marker-buffer selected-task)))
  254. (setq target-pos (marker-position selected-task))
  255. (move-marker selected-task nil))
  256. (save-excursion
  257. (save-restriction
  258. (widen)
  259. (goto-char target-pos)
  260. (org-back-to-heading t)
  261. (or interrupting (move-marker org-clock-interrupted-task nil))
  262. (org-clock-history-push)
  263. (cond ((functionp org-clock-in-switch-to-state)
  264. (looking-at org-complex-heading-regexp)
  265. (let ((newstate (funcall org-clock-in-switch-to-state (match-string 2))))
  266. (if newstate (org-todo newstate))))
  267. ((and org-clock-in-switch-to-state
  268. (not (looking-at (concat outline-regexp "[ \t]*"
  269. org-clock-in-switch-to-state
  270. "\\>"))))
  271. (org-todo org-clock-in-switch-to-state)))
  272. (setq org-clock-heading-for-remember
  273. (and (looking-at org-complex-heading-regexp)
  274. (match-end 4)
  275. (org-trim (buffer-substring (match-end 1) (match-end 4)))))
  276. (setq org-clock-heading
  277. (cond ((and org-clock-heading-function
  278. (functionp org-clock-heading-function))
  279. (funcall org-clock-heading-function))
  280. ((looking-at org-complex-heading-regexp)
  281. (match-string 4))
  282. (t "???")))
  283. (setq org-clock-heading (org-propertize org-clock-heading 'face nil))
  284. (org-clock-find-position)
  285. (if (and org-clock-in-resume
  286. (looking-at (concat "^[ \\t]* " org-clock-string
  287. " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
  288. " +\\sw+ +[012][0-9]:[0-5][0-9]\\)\\]$")))
  289. (progn (message "Matched %s" (match-string 1))
  290. (setq ts (concat "[" (match-string 1) "]"))
  291. (goto-char (match-end 1))
  292. (setq org-clock-start-time
  293. (apply 'encode-time (org-parse-time-string (match-string 1)))))
  294. (progn
  295. (insert "\n") (backward-char 1)
  296. (org-indent-line-function)
  297. (insert org-clock-string " ")
  298. (setq org-clock-start-time (current-time))
  299. (setq ts (org-insert-time-stamp org-clock-start-time 'with-hm 'inactive))))
  300. (move-marker org-clock-marker (point) (buffer-base-buffer))
  301. (or global-mode-string (setq global-mode-string '("")))
  302. (or (memq 'org-mode-line-string global-mode-string)
  303. (setq global-mode-string
  304. (append global-mode-string '(org-mode-line-string))))
  305. (org-update-mode-line)
  306. (setq org-mode-line-timer
  307. (run-with-timer 60 60 'org-update-mode-line))
  308. (message "Clock started at %s" ts))))))
  309. (defun org-clock-find-position ()
  310. "Find the location where the next clock line should be inserted."
  311. (org-back-to-heading t)
  312. (catch 'exit
  313. (let ((beg (save-excursion
  314. (beginning-of-line 2)
  315. (or (bolp) (newline))
  316. (point)))
  317. (end (progn (outline-next-heading) (point)))
  318. (re (concat "^[ \t]*" org-clock-string))
  319. (cnt 0)
  320. first last)
  321. (goto-char beg)
  322. (when (eobp) (newline) (setq end (max (point) end)))
  323. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  324. ;; we seem to have a CLOCK drawer, so go there.
  325. (beginning-of-line 2)
  326. (throw 'exit t))
  327. ;; Lets count the CLOCK lines
  328. (goto-char beg)
  329. (while (re-search-forward re end t)
  330. (setq first (or first (match-beginning 0))
  331. last (match-beginning 0)
  332. cnt (1+ cnt)))
  333. (when (and (integerp org-clock-into-drawer)
  334. (>= (1+ cnt) org-clock-into-drawer))
  335. ;; Wrap current entries into a new drawer
  336. (goto-char last)
  337. (beginning-of-line 2)
  338. (if (org-at-item-p) (org-end-of-item))
  339. (insert ":END:\n")
  340. (beginning-of-line 0)
  341. (org-indent-line-function)
  342. (goto-char first)
  343. (insert ":CLOCK:\n")
  344. (beginning-of-line 0)
  345. (org-indent-line-function)
  346. (org-flag-drawer t)
  347. (beginning-of-line 2)
  348. (throw 'exit nil))
  349. (goto-char beg)
  350. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  351. (not (equal (match-string 1) org-clock-string)))
  352. ;; Planning info, skip to after it
  353. (beginning-of-line 2)
  354. (or (bolp) (newline)))
  355. (when (eq t org-clock-into-drawer)
  356. (insert ":CLOCK:\n:END:\n")
  357. (beginning-of-line 0)
  358. (org-indent-line-function)
  359. (beginning-of-line 0)
  360. (org-flag-drawer t)
  361. (org-indent-line-function)
  362. (beginning-of-line 2)))))
  363. (defun org-clock-out (&optional fail-quietly)
  364. "Stop the currently running clock.
  365. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  366. (interactive)
  367. (catch 'exit
  368. (if (not (marker-buffer org-clock-marker))
  369. (if fail-quietly (throw 'exit t) (error "No active clock")))
  370. (let (ts te s h m remove)
  371. (save-excursion
  372. (set-buffer (marker-buffer org-clock-marker))
  373. (save-restriction
  374. (widen)
  375. (goto-char org-clock-marker)
  376. (beginning-of-line 1)
  377. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  378. (equal (match-string 1) org-clock-string))
  379. (setq ts (match-string 2))
  380. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  381. (goto-char (match-end 0))
  382. (delete-region (point) (point-at-eol))
  383. (insert "--")
  384. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  385. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  386. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  387. h (floor (/ s 3600))
  388. s (- s (* 3600 h))
  389. m (floor (/ s 60))
  390. s (- s (* 60 s)))
  391. (insert " => " (format "%2d:%02d" h m))
  392. (when (setq remove (and org-clock-out-remove-zero-time-clocks
  393. (= (+ h m) 0)))
  394. (beginning-of-line 1)
  395. (delete-region (point) (point-at-eol))
  396. (and (looking-at "\n") (> (point-max) (1+ (point)))
  397. (delete-char 1)))
  398. (move-marker org-clock-marker nil)
  399. (when org-log-note-clock-out
  400. (org-add-log-setup 'clock-out nil nil nil
  401. (concat "# Task: " (org-get-heading t) "\n\n")))
  402. (when org-mode-line-timer
  403. (cancel-timer org-mode-line-timer)
  404. (setq org-mode-line-timer nil))
  405. (setq global-mode-string
  406. (delq 'org-mode-line-string global-mode-string))
  407. (force-mode-line-update)
  408. (message (concat "Clock stopped at %s after HH:MM = " org-time-clocksum-format "%s") te h m
  409. (if remove " => LINE REMOVED" "")))))))
  410. (defun org-clock-cancel ()
  411. "Cancel the running clock be removing the start timestamp."
  412. (interactive)
  413. (if (not (marker-buffer org-clock-marker))
  414. (error "No active clock"))
  415. (save-excursion
  416. (set-buffer (marker-buffer org-clock-marker))
  417. (goto-char org-clock-marker)
  418. (delete-region (1- (point-at-bol)) (point-at-eol)))
  419. (setq global-mode-string
  420. (delq 'org-mode-line-string global-mode-string))
  421. (force-mode-line-update)
  422. (message "Clock canceled"))
  423. (defun org-clock-goto (&optional select)
  424. "Go to the currently clocked-in entry.
  425. With prefix arg SELECT, offer recently clocked tasks."
  426. (interactive "P")
  427. (let ((m (if select
  428. (org-clock-select-task "Select task to go to: ")
  429. org-clock-marker)))
  430. (if (not (marker-buffer m))
  431. (if select
  432. (error "No task selected")
  433. (error "No active clock")))
  434. (switch-to-buffer (marker-buffer m))
  435. (if (or (< m (point-min)) (> m (point-max))) (widen))
  436. (goto-char m)
  437. (org-show-entry)
  438. (org-back-to-heading)
  439. (org-cycle-hide-drawers 'children)
  440. (recenter)))
  441. (defvar org-clock-file-total-minutes nil
  442. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  443. (make-variable-buffer-local 'org-clock-file-total-minutes)
  444. (defun org-clock-sum (&optional tstart tend)
  445. "Sum the times for each subtree.
  446. Puts the resulting times in minutes as a text property on each headline."
  447. (interactive)
  448. (let* ((bmp (buffer-modified-p))
  449. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  450. org-clock-string
  451. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  452. (lmax 30)
  453. (ltimes (make-vector lmax 0))
  454. (t1 0)
  455. (level 0)
  456. ts te dt
  457. time)
  458. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  459. (save-excursion
  460. (goto-char (point-max))
  461. (while (re-search-backward re nil t)
  462. (cond
  463. ((match-end 2)
  464. ;; Two time stamps
  465. (setq ts (match-string 2)
  466. te (match-string 3)
  467. ts (time-to-seconds
  468. (apply 'encode-time (org-parse-time-string ts)))
  469. te (time-to-seconds
  470. (apply 'encode-time (org-parse-time-string te)))
  471. ts (if tstart (max ts tstart) ts)
  472. te (if tend (min te tend) te)
  473. dt (- te ts)
  474. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  475. ((match-end 4)
  476. ;; A naket time
  477. (setq t1 (+ t1 (string-to-number (match-string 5))
  478. (* 60 (string-to-number (match-string 4))))))
  479. (t ;; A headline
  480. (setq level (- (match-end 1) (match-beginning 1)))
  481. (when (or (> t1 0) (> (aref ltimes level) 0))
  482. (loop for l from 0 to level do
  483. (aset ltimes l (+ (aref ltimes l) t1)))
  484. (setq t1 0 time (aref ltimes level))
  485. (loop for l from level to (1- lmax) do
  486. (aset ltimes l 0))
  487. (goto-char (match-beginning 0))
  488. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  489. (setq org-clock-file-total-minutes (aref ltimes 0)))
  490. (set-buffer-modified-p bmp)))
  491. (defun org-clock-display (&optional total-only)
  492. "Show subtree times in the entire buffer.
  493. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  494. in the echo area."
  495. (interactive)
  496. (org-remove-clock-overlays)
  497. (let (time h m p)
  498. (org-clock-sum)
  499. (unless total-only
  500. (save-excursion
  501. (goto-char (point-min))
  502. (while (or (and (equal (setq p (point)) (point-min))
  503. (get-text-property p :org-clock-minutes))
  504. (setq p (next-single-property-change
  505. (point) :org-clock-minutes)))
  506. (goto-char p)
  507. (when (setq time (get-text-property p :org-clock-minutes))
  508. (org-put-clock-overlay time (funcall outline-level))))
  509. (setq h (/ org-clock-file-total-minutes 60)
  510. m (- org-clock-file-total-minutes (* 60 h)))
  511. ;; Arrange to remove the overlays upon next change.
  512. (when org-remove-highlights-with-change
  513. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  514. nil 'local))))
  515. (message (concat "Total file time: " org-time-clocksum-format " (%d hours and %d minutes)") h m h m)))
  516. (defvar org-clock-overlays nil)
  517. (make-variable-buffer-local 'org-clock-overlays)
  518. (defun org-put-clock-overlay (time &optional level)
  519. "Put an overlays on the current line, displaying TIME.
  520. If LEVEL is given, prefix time with a corresponding number of stars.
  521. This creates a new overlay and stores it in `org-clock-overlays', so that it
  522. will be easy to remove."
  523. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  524. (l (if level (org-get-valid-level level 0) 0))
  525. (fmt (concat "%s " org-time-clocksum-format "%s"))
  526. (off 0)
  527. ov tx)
  528. (org-move-to-column c)
  529. (unless (eolp) (skip-chars-backward "^ \t"))
  530. (skip-chars-backward " \t")
  531. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  532. tx (concat (buffer-substring (1- (point)) (point))
  533. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  534. (org-add-props (format fmt
  535. (make-string l ?*) h m
  536. (make-string (- 16 l) ?\ ))
  537. '(face secondary-selection))
  538. ""))
  539. (if (not (featurep 'xemacs))
  540. (org-overlay-put ov 'display tx)
  541. (org-overlay-put ov 'invisible t)
  542. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  543. (push ov org-clock-overlays)))
  544. (defun org-remove-clock-overlays (&optional beg end noremove)
  545. "Remove the occur highlights from the buffer.
  546. BEG and END are ignored. If NOREMOVE is nil, remove this function
  547. from the `before-change-functions' in the current buffer."
  548. (interactive)
  549. (unless org-inhibit-highlight-removal
  550. (mapc 'org-delete-overlay org-clock-overlays)
  551. (setq org-clock-overlays nil)
  552. (unless noremove
  553. (remove-hook 'before-change-functions
  554. 'org-remove-clock-overlays 'local))))
  555. (defvar state) ;; dynamically scoped into this function
  556. (defun org-clock-out-if-current ()
  557. "Clock out if the current entry contains the running clock.
  558. This is used to stop the clock after a TODO entry is marked DONE,
  559. and is only done if the variable `org-clock-out-when-done' is not nil."
  560. (when (and org-clock-out-when-done
  561. (member state org-done-keywords)
  562. (equal (marker-buffer org-clock-marker) (current-buffer))
  563. (< (point) org-clock-marker)
  564. (> (save-excursion (outline-next-heading) (point))
  565. org-clock-marker))
  566. ;; Clock out, but don't accept a logging message for this.
  567. (let ((org-log-note-clock-out nil))
  568. (org-clock-out))))
  569. (add-hook 'org-after-todo-state-change-hook
  570. 'org-clock-out-if-current)
  571. ;;;###autoload
  572. (defun org-get-clocktable (&rest props)
  573. "Get a formatted clocktable with parameters according to PROPS.
  574. The table is created in a temporary buffer, fully formatted and
  575. fontified, and then returned."
  576. ;; Set the defaults
  577. (setq props (plist-put props :name "clocktable"))
  578. (unless (plist-member props :maxlevel)
  579. (setq props (plist-put props :maxlevel 2)))
  580. (unless (plist-member props :scope)
  581. (setq props (plist-put props :scope 'agenda)))
  582. (with-temp-buffer
  583. (org-mode)
  584. (org-create-dblock props)
  585. (org-update-dblock)
  586. (font-lock-fontify-buffer)
  587. (forward-line 2)
  588. (buffer-substring (point) (progn
  589. (re-search-forward "^#\\+END" nil t)
  590. (point-at-bol)))))
  591. (defun org-clock-report (&optional arg)
  592. "Create a table containing a report about clocked time.
  593. If the cursor is inside an existing clocktable block, then the table
  594. will be updated. If not, a new clocktable will be inserted.
  595. When called with a prefix argument, move to the first clock table in the
  596. buffer and update it."
  597. (interactive "P")
  598. (org-remove-clock-overlays)
  599. (when arg
  600. (org-find-dblock "clocktable")
  601. (org-show-entry))
  602. (if (org-in-clocktable-p)
  603. (goto-char (org-in-clocktable-p))
  604. (org-create-dblock (list :name "clocktable"
  605. :maxlevel 2 :scope 'file)))
  606. (org-update-dblock))
  607. (defun org-in-clocktable-p ()
  608. "Check if the cursor is in a clocktable."
  609. (let ((pos (point)) start)
  610. (save-excursion
  611. (end-of-line 1)
  612. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  613. (setq start (match-beginning 0))
  614. (re-search-forward "^#\\+END:.*" nil t)
  615. (>= (match-end 0) pos)
  616. start))))
  617. (defun org-clock-special-range (key &optional time as-strings)
  618. "Return two times bordering a special time range.
  619. Key is a symbol specifying the range and can be one of `today', `yesterday',
  620. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  621. A week starts Monday 0:00 and ends Sunday 24:00.
  622. The range is determined relative to TIME. TIME defaults to the current time.
  623. The return value is a cons cell with two internal times like the ones
  624. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  625. the returned times will be formatted strings."
  626. (if (integerp key) (setq key (intern (number-to-string key))))
  627. (let* ((tm (decode-time (or time (current-time))))
  628. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  629. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  630. (dow (nth 6 tm))
  631. (skey (symbol-name key))
  632. (shift 0)
  633. s1 m1 h1 d1 month1 y1 diff ts te fm txt w date)
  634. (cond
  635. ((string-match "^[0-9]+$" skey)
  636. (setq y (string-to-number skey) m 1 d 1 key 'year))
  637. ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)$" skey)
  638. (setq y (string-to-number (match-string 1 skey))
  639. month (string-to-number (match-string 2 skey))
  640. d 1 key 'month))
  641. ((string-match "^\\([0-9]+\\)-[wW]\\([0-9]\\{1,2\\}\\)$" skey)
  642. (require 'cal-iso)
  643. (setq y (string-to-number (match-string 1 skey))
  644. w (string-to-number (match-string 2 skey)))
  645. (setq date (calendar-gregorian-from-absolute
  646. (calendar-absolute-from-iso (list w 1 y))))
  647. (setq d (nth 1 date) month (car date) y (nth 2 date)
  648. key 'week))
  649. ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)-\\([0-9]\\{1,2\\}\\)$" skey)
  650. (setq y (string-to-number (match-string 1 skey))
  651. month (string-to-number (match-string 2 skey))
  652. d (string-to-number (match-string 3 skey))
  653. key 'day))
  654. ((string-match "\\([-+][0-9]+\\)$" skey)
  655. (setq shift (string-to-number (match-string 1 skey))
  656. key (intern (substring skey 0 (match-beginning 1))))))
  657. (when (= shift 0)
  658. (cond ((eq key 'yesterday) (setq key 'today shift -1))
  659. ((eq key 'lastweek) (setq key 'week shift -1))
  660. ((eq key 'lastmonth) (setq key 'month shift -1))
  661. ((eq key 'lastyear) (setq key 'year shift -1))))
  662. (cond
  663. ((memq key '(day today))
  664. (setq d (+ d shift) h 0 m 0 h1 24 m1 0))
  665. ((memq key '(week thisweek))
  666. (setq diff (+ (* -7 shift) (if (= dow 0) 6 (1- dow)))
  667. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  668. ((memq key '(month thismonth))
  669. (setq d 1 h 0 m 0 d1 1 month (+ month shift) month1 (1+ month) h1 0 m1 0))
  670. ((memq key '(year thisyear))
  671. (setq m 0 h 0 d 1 month 1 y (+ y shift) y1 (1+ y)))
  672. (t (error "No such time block %s" key)))
  673. (setq ts (encode-time s m h d month y)
  674. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  675. (or d1 d) (or month1 month) (or y1 y)))
  676. (setq fm (cdr org-time-stamp-formats))
  677. (cond
  678. ((memq key '(day today))
  679. (setq txt (format-time-string "%A, %B %d, %Y" ts)))
  680. ((memq key '(week thisweek))
  681. (setq txt (format-time-string "week %G-W%V" ts)))
  682. ((memq key '(month thismonth))
  683. (setq txt (format-time-string "%B %Y" ts)))
  684. ((memq key '(year thisyear))
  685. (setq txt (format-time-string "the year %Y" ts))))
  686. (if as-strings
  687. (list (format-time-string fm ts) (format-time-string fm te) txt)
  688. (list ts te txt))))
  689. (defun org-clocktable-shift (dir n)
  690. "Try to shift the :block date of the clocktable at point.
  691. Point must be in the #+BEGIN: line of a clocktable, or this function
  692. will throw an error.
  693. DIR is a direction, a symbol `left', `right', `up', or `down'.
  694. Both `left' and `down' shift the block toward the past, `up' and `right'
  695. push it toward the future.
  696. N is the number of shift steps to take. The size of the step depends on
  697. the currently selected interval size."
  698. (setq n (prefix-numeric-value n))
  699. (and (memq dir '(left down)) (setq n (- n)))
  700. (save-excursion
  701. (goto-char (point-at-bol))
  702. (if (not (looking-at "#\\+BEGIN: clocktable\\>.*?:block[ \t]+\\(\\S-+\\)"))
  703. (error "Line needs a :block definition before this command works")
  704. (let* ((b (match-beginning 1)) (e (match-end 1))
  705. (s (match-string 1))
  706. block shift ins y mw d date wp m)
  707. (cond
  708. ((equal s "yesterday") (setq s "today-1"))
  709. ((equal s "lastweek") (setq s "thisweek-1"))
  710. ((equal s "lastmonth") (setq s "thismonth-1"))
  711. ((equal s "lastyear") (setq s "thisyear-1")))
  712. (cond
  713. ((string-match "^\\(today\\|thisweek\\|thismonth\\|thisyear\\)\\([-+][0-9]+\\)?$" s)
  714. (setq block (match-string 1 s)
  715. shift (if (match-end 2)
  716. (string-to-number (match-string 2 s))
  717. 0))
  718. (setq shift (+ shift n))
  719. (setq ins (if (= shift 0) block (format "%s%+d" block shift))))
  720. ((string-match "\\([0-9]+\\)\\(-\\([wW]?\\)\\([0-9]\\{1,2\\}\\)\\(-\\([0-9]\\{1,2\\}\\)\\)?\\)?" s)
  721. ;; 1 1 2 3 3 4 4 5 6 6 5 2
  722. (setq y (string-to-number (match-string 1 s))
  723. wp (and (match-end 3) (match-string 3 s))
  724. mw (and (match-end 4) (string-to-number (match-string 4 s)))
  725. d (and (match-end 6) (string-to-number (match-string 6 s))))
  726. (cond
  727. (d (setq ins (format-time-string
  728. "%Y-%m-%d"
  729. (encode-time 0 0 0 (+ d n) m y))))
  730. ((and wp mw (> (length wp) 0))
  731. (require 'cal-iso)
  732. (setq date (calendar-gregorian-from-absolute (calendar-absolute-from-iso (list (+ mw n) 1 y))))
  733. (setq ins (format-time-string
  734. "%G-W%V"
  735. (encode-time 0 0 0 (nth 1 date) (car date) (nth 2 date)))))
  736. (mw
  737. (setq ins (format-time-string
  738. "%Y-%m"
  739. (encode-time 0 0 0 1 (+ mw n) y))))
  740. (y
  741. (setq ins (number-to-string (+ y n))))))
  742. (t (error "Cannot shift clocktable block")))
  743. (when ins
  744. (goto-char b)
  745. (insert ins)
  746. (delete-region (point) (+ (point) (- e b)))
  747. (beginning-of-line 1)
  748. (org-update-dblock)
  749. t)))))
  750. (defun org-dblock-write:clocktable (params)
  751. "Write the standard clocktable."
  752. (catch 'exit
  753. (let* ((hlchars '((1 . "*") (2 . "/")))
  754. (ins (make-marker))
  755. (total-time nil)
  756. (scope (plist-get params :scope))
  757. (tostring (plist-get params :tostring))
  758. (multifile (plist-get params :multifile))
  759. (header (plist-get params :header))
  760. (maxlevel (or (plist-get params :maxlevel) 3))
  761. (step (plist-get params :step))
  762. (emph (plist-get params :emphasize))
  763. (ts (plist-get params :tstart))
  764. (te (plist-get params :tend))
  765. (block (plist-get params :block))
  766. (link (plist-get params :link))
  767. ipos time p level hlc hdl content recalc formula pcol
  768. cc beg end pos tbl tbl1 range-text rm-file-column scope-is-list)
  769. (setq org-clock-file-total-minutes nil)
  770. (when step
  771. (unless (or block (and ts te))
  772. (error "Clocktable `:step' can only be used with `:block' or `:tstart,:end'"))
  773. (org-clocktable-steps params)
  774. (throw 'exit nil))
  775. (when block
  776. (setq cc (org-clock-special-range block nil t)
  777. ts (car cc) te (nth 1 cc) range-text (nth 2 cc)))
  778. (when (integerp ts) (setq ts (calendar-gregorian-from-absolute ts)))
  779. (when (integerp te) (setq te (calendar-gregorian-from-absolute te)))
  780. (when (and ts (listp ts))
  781. (setq ts (format "%4d-%02d-%02d" (nth 2 ts) (car ts) (nth 1 ts))))
  782. (when (and te (listp te))
  783. (setq te (format "%4d-%02d-%02d" (nth 2 te) (car te) (nth 1 te))))
  784. ;; Now the times are strings we can parse.
  785. (if ts (setq ts (time-to-seconds
  786. (apply 'encode-time (org-parse-time-string ts)))))
  787. (if te (setq te (time-to-seconds
  788. (apply 'encode-time (org-parse-time-string te)))))
  789. (move-marker ins (point))
  790. (setq ipos (point))
  791. ;; Get the right scope
  792. (setq pos (point))
  793. (cond
  794. ((and scope (listp scope) (symbolp (car scope)))
  795. (setq scope (eval scope)))
  796. ((eq scope 'agenda)
  797. (setq scope (org-agenda-files t)))
  798. ((eq scope 'agenda-with-archives)
  799. (setq scope (org-agenda-files t))
  800. (setq scope (org-add-archive-files scope)))
  801. ((eq scope 'file-with-archives)
  802. (setq scope (org-add-archive-files (list (buffer-file-name)))
  803. rm-file-column t)))
  804. (setq scope-is-list (and scope (listp scope)))
  805. (save-restriction
  806. (cond
  807. ((not scope))
  808. ((eq scope 'file) (widen))
  809. ((eq scope 'subtree) (org-narrow-to-subtree))
  810. ((eq scope 'tree)
  811. (while (org-up-heading-safe))
  812. (org-narrow-to-subtree))
  813. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  814. (symbol-name scope)))
  815. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  816. (catch 'exit
  817. (while (org-up-heading-safe)
  818. (looking-at outline-regexp)
  819. (if (<= (org-reduced-level (funcall outline-level)) level)
  820. (throw 'exit nil))))
  821. (org-narrow-to-subtree))
  822. (scope-is-list
  823. (let* ((files scope)
  824. (scope 'agenda)
  825. (p1 (copy-sequence params))
  826. file)
  827. (setq p1 (plist-put p1 :tostring t))
  828. (setq p1 (plist-put p1 :multifile t))
  829. (setq p1 (plist-put p1 :scope 'file))
  830. (org-prepare-agenda-buffers files)
  831. (while (setq file (pop files))
  832. (with-current-buffer (find-buffer-visiting file)
  833. (setq tbl1 (org-dblock-write:clocktable p1))
  834. (when tbl1
  835. (push (org-clocktable-add-file
  836. file
  837. (concat "| |*File time*|*"
  838. (org-minutes-to-hh:mm-string
  839. org-clock-file-total-minutes)
  840. "*|\n"
  841. tbl1)) tbl)
  842. (setq total-time (+ (or total-time 0)
  843. org-clock-file-total-minutes))))))))
  844. (goto-char pos)
  845. (unless scope-is-list
  846. (org-clock-sum ts te)
  847. (goto-char (point-min))
  848. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  849. (goto-char p)
  850. (when (setq time (get-text-property p :org-clock-minutes))
  851. (save-excursion
  852. (beginning-of-line 1)
  853. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  854. (setq level (org-reduced-level
  855. (- (match-end 1) (match-beginning 1))))
  856. (<= level maxlevel))
  857. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  858. hdl (if (not link)
  859. (match-string 2)
  860. (org-make-link-string
  861. (format "file:%s::%s"
  862. (buffer-file-name)
  863. (save-match-data
  864. (org-make-org-heading-search-string
  865. (match-string 2))))
  866. (match-string 2))))
  867. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  868. (push (concat
  869. "| " (int-to-string level) "|" hlc hdl hlc " |"
  870. (make-string (1- level) ?|)
  871. hlc (org-minutes-to-hh:mm-string time) hlc
  872. " |") tbl))))))
  873. (setq tbl (nreverse tbl))
  874. (if tostring
  875. (if tbl (mapconcat 'identity tbl "\n") nil)
  876. (goto-char ins)
  877. (insert-before-markers
  878. (or header
  879. (concat
  880. "Clock summary at ["
  881. (substring
  882. (format-time-string (cdr org-time-stamp-formats))
  883. 1 -1)
  884. "]"
  885. (if block (concat ", for " range-text ".") "")
  886. "\n\n"))
  887. (if scope-is-list "|File" "")
  888. "|L|Headline|Time|\n")
  889. (setq total-time (or total-time org-clock-file-total-minutes))
  890. (insert-before-markers
  891. "|-\n|"
  892. (if scope-is-list "|" "")
  893. "|"
  894. "*Total time*| *"
  895. (org-minutes-to-hh:mm-string (or total-time 0))
  896. "*|\n|-\n")
  897. (setq tbl (delq nil tbl))
  898. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  899. (equal (substring (car tbl) 0 2) "|-"))
  900. (pop tbl))
  901. (insert-before-markers (mapconcat
  902. 'identity (delq nil tbl)
  903. (if scope-is-list "\n|-\n" "\n")))
  904. (backward-delete-char 1)
  905. (if (setq formula (plist-get params :formula))
  906. (cond
  907. ((eq formula '%)
  908. (setq pcol (+ (if scope-is-list 1 0) maxlevel 3))
  909. (insert
  910. (format
  911. "\n#+TBLFM: $%d='(org-clock-time%% @%d$%d $%d..$%d);%%.1f"
  912. pcol
  913. 2
  914. (+ 3 (if scope-is-list 1 0))
  915. (+ (if scope-is-list 1 0) 3)
  916. (1- pcol)))
  917. (setq recalc t))
  918. ((stringp formula)
  919. (insert "\n#+TBLFM: " formula)
  920. (setq recalc t))
  921. (t (error "invalid formula in clocktable")))
  922. ;; Should we rescue an old formula?
  923. (when (stringp (setq content (plist-get params :content)))
  924. (when (string-match "^\\(#\\+TBLFM:.*\\)" content)
  925. (setq recalc t)
  926. (insert "\n" (match-string 1 (plist-get params :content)))
  927. (beginning-of-line 0))))
  928. (goto-char ipos)
  929. (skip-chars-forward "^|")
  930. (org-table-align)
  931. (when recalc
  932. (if (eq formula '%)
  933. (save-excursion (org-table-goto-column pcol nil 'force)
  934. (insert "%")))
  935. (org-table-recalculate 'all))
  936. (when rm-file-column
  937. (forward-char 1)
  938. (org-table-delete-column)))))))
  939. (defun org-clocktable-steps (params)
  940. (let* ((p1 (copy-sequence params))
  941. (ts (plist-get p1 :tstart))
  942. (te (plist-get p1 :tend))
  943. (step0 (plist-get p1 :step))
  944. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  945. (block (plist-get p1 :block))
  946. cc range-text)
  947. (when block
  948. (setq cc (org-clock-special-range block nil t)
  949. ts (car cc) te (nth 1 cc) range-text (nth 2 cc)))
  950. (if ts (setq ts (time-to-seconds
  951. (apply 'encode-time (org-parse-time-string ts)))))
  952. (if te (setq te (time-to-seconds
  953. (apply 'encode-time (org-parse-time-string te)))))
  954. (setq p1 (plist-put p1 :header ""))
  955. (setq p1 (plist-put p1 :step nil))
  956. (setq p1 (plist-put p1 :block nil))
  957. (while (< ts te)
  958. (or (bolp) (insert "\n"))
  959. (setq p1 (plist-put p1 :tstart (format-time-string
  960. (car org-time-stamp-formats)
  961. (seconds-to-time ts))))
  962. (setq p1 (plist-put p1 :tend (format-time-string
  963. (car org-time-stamp-formats)
  964. (seconds-to-time (setq ts (+ ts step))))))
  965. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  966. (plist-get p1 :tstart) "\n")
  967. (org-dblock-write:clocktable p1)
  968. (re-search-forward "#\\+END:")
  969. (end-of-line 0))))
  970. (defun org-clocktable-add-file (file table)
  971. (if table
  972. (let ((lines (org-split-string table "\n"))
  973. (ff (file-name-nondirectory file)))
  974. (mapconcat 'identity
  975. (mapcar (lambda (x)
  976. (if (string-match org-table-dataline-regexp x)
  977. (concat "|" ff x)
  978. x))
  979. lines)
  980. "\n"))))
  981. (defun org-clock-time% (total &rest strings)
  982. "Compute a time fraction in percent.
  983. TOTAL s a time string like 10:21 specifying the total times.
  984. STRINGS is a list of strings that should be checked for a time.
  985. The first string that does have a time will be used.
  986. This function is made for clock tables."
  987. (let ((re "\\([0-9]+\\):\\([0-9]+\\)")
  988. tot s)
  989. (save-match-data
  990. (catch 'exit
  991. (if (not (string-match re total))
  992. (throw 'exit 0.)
  993. (setq tot (+ (string-to-number (match-string 2 total))
  994. (* 60 (string-to-number (match-string 1 total)))))
  995. (if (= tot 0.) (throw 'exit 0.)))
  996. (while (setq s (pop strings))
  997. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  998. (throw 'exit
  999. (/ (* 100.0 (+ (string-to-number (match-string 2 s))
  1000. (* 60 (string-to-number (match-string 1 s)))))
  1001. tot))))
  1002. 0))))
  1003. (defun org-clock-save ()
  1004. "Persist various clock-related data to disk"
  1005. (with-current-buffer (find-file (expand-file-name org-clock-persist-file))
  1006. (progn (delete-region (point-min) (point-max))
  1007. ;;Store clock
  1008. (insert (format ";; org-persist.el - %s at %s\n"
  1009. system-name (format-time-string
  1010. (cdr org-time-stamp-formats))))
  1011. (if (and org-clock-persist (marker-buffer org-clock-marker)
  1012. (or (not org-clock-persist-query-save)
  1013. (y-or-n-p (concat "Save current clock ("
  1014. (substring-no-properties org-clock-heading)
  1015. ")"))))
  1016. (insert "(setq resume-clock '(\""
  1017. (buffer-file-name (marker-buffer org-clock-marker))
  1018. "\" . " (int-to-string (marker-position org-clock-marker))
  1019. "))\n"))
  1020. ;;Store clocked task history. Tasks are stored reversed to make
  1021. ;;reading simpler
  1022. (if org-clock-history
  1023. (insert "(setq stored-clock-history '("
  1024. (mapconcat
  1025. (lambda (m)
  1026. (when (marker-buffer m)
  1027. (concat "(\"" (buffer-file-name (marker-buffer m))
  1028. "\" . " (int-to-string (marker-position m))
  1029. ")")))
  1030. (reverse org-clock-history) " ") "))\n"))
  1031. (save-buffer)
  1032. (kill-buffer (current-buffer)))))
  1033. (defvar org-clock-loaded nil)
  1034. (defun org-clock-load ()
  1035. "Load various clock-related data from disk, optionally resuming
  1036. a stored clock"
  1037. (if (not org-clock-loaded)
  1038. (let ((filename (expand-file-name org-clock-persist-file))
  1039. (org-clock-in-resume t))
  1040. (if (file-readable-p filename)
  1041. (progn
  1042. (message "%s" "Restoring clock data")
  1043. (setq org-clock-loaded t)
  1044. (load-file filename)
  1045. ;; load history
  1046. (if (boundp 'stored-clock-history)
  1047. (save-window-excursion
  1048. (mapc (lambda (task)
  1049. (org-clock-history-push (cdr task)
  1050. (find-file (car task))))
  1051. stored-clock-history)))
  1052. ;; resume clock
  1053. (if (and (boundp 'resume-clock) org-clock-persist
  1054. (or (not org-clock-persist-query-resume)
  1055. (y-or-n-p
  1056. (concat
  1057. "Resume clock ("
  1058. (with-current-buffer (find-file (car resume-clock))
  1059. (progn (goto-char (cdr resume-clock))
  1060. (looking-at org-complex-heading-regexp)
  1061. (match-string 4))) ")"))))
  1062. (with-current-buffer (find-file (car resume-clock))
  1063. (progn (goto-char (cdr resume-clock))
  1064. (org-clock-in)))))
  1065. (message "Not restoring clock data; %s not found"
  1066. org-clock-persist-file)))))
  1067. ;;;###autoload
  1068. (defun org-clock-persistence-insinuate ()
  1069. "Set up hooks for clock persistence"
  1070. (add-hook 'org-mode-hook 'org-clock-load)
  1071. (add-hook 'kill-emacs-hook 'org-clock-save))
  1072. (provide 'org-clock)
  1073. ;; arch-tag: 7b42c5d4-9b36-48be-97c0-66a869daed4c
  1074. ;;; org-clock.el ends here