org-clock.el 43 KB

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