org-clock.el 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  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.31trans
  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 org-log-into-drawer
  36. "Should clocking info be wrapped into a drawer?
  37. When t, clocking info will always be inserted into a :LOGBOOK: 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. When a string, it names the drawer to be used.
  43. The default for this variable is the value of `org-log-into-drawer',
  44. which see."
  45. :group 'org-todo
  46. :group 'org-clock
  47. :type '(choice
  48. (const :tag "Always" t)
  49. (const :tag "Only when drawer exists" nil)
  50. (integer :tag "When at least N clock entries")
  51. (const :tag "Into LOGBOOK drawer" "LOGBOOK")
  52. (string :tag "Into Drawer named...")))
  53. (defcustom org-clock-out-when-done t
  54. "When non-nil, clock will be stopped when the clocked entry is marked DONE.
  55. A nil value means, clock will keep running until stopped explicitly with
  56. `C-c C-x C-o', or until the clock is started in a different item."
  57. :group 'org-clock
  58. :type 'boolean)
  59. (defcustom org-clock-out-remove-zero-time-clocks nil
  60. "Non-nil means, remove the clock line when the resulting time is zero."
  61. :group 'org-clock
  62. :type 'boolean)
  63. (defcustom org-clock-in-switch-to-state nil
  64. "Set task to a special todo state while clocking it.
  65. The value should be the state to which the entry should be
  66. switched. If the value is a function, it must take one
  67. parameter (the current TODO state of the item) and return the
  68. state to switch it to."
  69. :group 'org-clock
  70. :group 'org-todo
  71. :type '(choice
  72. (const :tag "Don't force a state" nil)
  73. (string :tag "State")
  74. (symbol :tag "Function")))
  75. (defcustom org-clock-out-switch-to-state nil
  76. "Set task to a special todo state after clocking out.
  77. The value should be the state to which the entry should be
  78. switched. If the value is a function, it must take one
  79. parameter (the current TODO state of the item) and return the
  80. state to switch it to."
  81. :group 'org-clock
  82. :group 'org-todo
  83. :type '(choice
  84. (const :tag "Don't force a state" nil)
  85. (string :tag "State")
  86. (symbol :tag "Function")))
  87. (defcustom org-clock-history-length 5
  88. "Number of clock tasks to remember in history."
  89. :group 'org-clock
  90. :type 'integer)
  91. (defcustom org-clock-goto-may-find-recent-task t
  92. "Non-nil means, `org-clock-goto' can go to recent task if no active clock."
  93. :group 'org-clock
  94. :type 'boolean)
  95. (defcustom org-clock-heading-function nil
  96. "When non-nil, should be a function to create `org-clock-heading'.
  97. This is the string shown in the mode line when a clock is running.
  98. The function is called with point at the beginning of the headline."
  99. :group 'org-clock
  100. :type 'function)
  101. (defcustom org-clock-string-limit 0
  102. "Maximum length of clock strings in the modeline. 0 means no limit."
  103. :group 'org-clock
  104. :type 'integer)
  105. (defcustom org-clock-in-resume nil
  106. "If non-nil, resume clock when clocking into task with open clock.
  107. When clocking into a task with a clock entry which has not been closed,
  108. the clock can be resumed from that point."
  109. :group 'org-clock
  110. :type 'boolean)
  111. (defcustom org-clock-persist nil
  112. "When non-nil, save the running clock when emacs is closed.
  113. The clock is resumed when emacs restarts.
  114. When this is t, both the running clock, and the entire clock
  115. history are saved. When this is the symbol `clock', only the
  116. running clock is saved.
  117. When Emacs restarts with saved clock information, the file containing the
  118. running clock as well as all files mentioned in the clock history will
  119. be visited.
  120. All this depends on running `org-clock-persistence-insinuate' in .emacs"
  121. :group 'org-clock
  122. :type '(choice
  123. (const :tag "Just the running clock" clock)
  124. (const :tag "Clock and history" t)
  125. (const :tag "No persistence" nil)))
  126. (defcustom org-clock-persist-file (convert-standard-filename
  127. "~/.emacs.d/org-clock-save.el")
  128. "File to save clock data to."
  129. :group 'org-clock
  130. :type 'string)
  131. (defcustom org-clock-persist-query-save nil
  132. "When non-nil, ask before saving the current clock on exit."
  133. :group 'org-clock
  134. :type 'boolean)
  135. (defcustom org-clock-persist-query-resume t
  136. "When non-nil, ask before resuming any stored clock during load."
  137. :group 'org-clock
  138. :type 'boolean)
  139. (defcustom org-clock-sound nil
  140. "Sound that will used for notifications.
  141. Possible values:
  142. nil no sound played.
  143. t standard Emacs beep
  144. file name play this sound file. If not possible, fall back to beep"
  145. :group 'org-clock
  146. :type '(choice
  147. (const :tag "No sound" nil)
  148. (const :tag "Standard beep" t)
  149. (file :tag "Play sound file")))
  150. (defcustom org-clock-modeline-total 'auto
  151. "Default setting for the time included for the modeline clock.
  152. This can be overruled locally using the CLOCK_MODELINE_TOTAL property.
  153. Allowed values are:
  154. current Only the time in the current instance of the clock
  155. today All time clocked inot this task today
  156. repeat All time clocked into this task since last repeat
  157. all All time ever recorded for this task
  158. auto Automatically, either `all', or `repeat' for repeating tasks"
  159. :group 'org-clock
  160. :type '(choice
  161. (const :tag "Current clock" current)
  162. (const :tag "Today's task time" today)
  163. (const :tag "Since last repeat" repeat)
  164. (const :tag "All task time" all)
  165. (const :tag "Automatically, `all' or since `repeat'" auto)))
  166. (defcustom org-show-notification-handler nil
  167. "Function or program to send notification with.
  168. The function or program will be called with the notification
  169. string as argument."
  170. :group 'org-clock
  171. :type '(choice
  172. (string :tag "Program")
  173. (function :tag "Function")))
  174. (defcustom org-clock-clocktable-default-properties '(:maxlevel 2 :scope file)
  175. "Default properties for new clocktables."
  176. :group 'org-clock
  177. :type 'plist)
  178. (defvar org-clock-in-prepare-hook nil
  179. "Hook run when preparing the clock.
  180. This hook is run before anything happens to the task that
  181. you want to clock in. For example, you can use this hook
  182. to add an effort property.")
  183. (defvar org-clock-in-hook nil
  184. "Hook run when starting the clock.")
  185. (defvar org-clock-out-hook nil
  186. "Hook run when stopping the current clock.")
  187. (defvar org-clock-cancel-hook nil
  188. "Hook run when cancelling the current clock.")
  189. (defvar org-clock-goto-hook nil
  190. "Hook run when selecting the currently clocked-in entry.")
  191. ;;; The clock for measuring work time.
  192. (defvar org-mode-line-string "")
  193. (put 'org-mode-line-string 'risky-local-variable t)
  194. (defvar org-clock-mode-line-timer nil)
  195. (defvar org-clock-heading "")
  196. (defvar org-clock-heading-for-remember "")
  197. (defvar org-clock-start-time "")
  198. (defvar org-clock-effort ""
  199. "Effort estimate of the currently clocking task")
  200. (defvar org-clock-total-time nil
  201. "Holds total time, spent previously on currently clocked item.
  202. This does not include the time in the currently running clock.")
  203. (defvar org-clock-history nil
  204. "List of marker pointing to recent clocked tasks.")
  205. (defvar org-clock-default-task (make-marker)
  206. "Marker pointing to the default task that should clock time.
  207. The clock can be made to switch to this task after clocking out
  208. of a different task.")
  209. (defvar org-clock-interrupted-task (make-marker)
  210. "Marker pointing to the task that has been interrupted by the current clock.")
  211. (defvar org-clock-mode-line-map (make-sparse-keymap))
  212. (define-key org-clock-mode-line-map [mode-line mouse-2] 'org-clock-goto)
  213. (define-key org-clock-mode-line-map [mode-line mouse-1] 'org-clock-menu)
  214. (defun org-clock-menu ()
  215. (interactive)
  216. (popup-menu
  217. '("Clock"
  218. ["Clock out" org-clock-out t]
  219. ["Change effort estimate" org-clock-modify-effort-estimate t]
  220. ["Go to clock entry" org-clock-goto t]
  221. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"])))
  222. (defun org-clock-history-push (&optional pos buffer)
  223. "Push a marker to the clock history."
  224. (setq org-clock-history-length (max 1 (min 35 org-clock-history-length)))
  225. (let ((m (move-marker (make-marker) (or pos (point)) buffer)) n l)
  226. (while (setq n (member m org-clock-history))
  227. (move-marker (car n) nil))
  228. (setq org-clock-history
  229. (delq nil
  230. (mapcar (lambda (x) (if (marker-buffer x) x nil))
  231. org-clock-history)))
  232. (when (>= (setq l (length org-clock-history)) org-clock-history-length)
  233. (setq org-clock-history
  234. (nreverse
  235. (nthcdr (- l org-clock-history-length -1)
  236. (nreverse org-clock-history)))))
  237. (push m org-clock-history)))
  238. (defun org-clock-save-markers-for-cut-and-paste (beg end)
  239. "Save relative positions of markers in region."
  240. (org-check-and-save-marker org-clock-marker beg end)
  241. (org-check-and-save-marker org-clock-hd-marker beg end)
  242. (org-check-and-save-marker org-clock-default-task beg end)
  243. (org-check-and-save-marker org-clock-interrupted-task beg end)
  244. (mapc (lambda (m) (org-check-and-save-marker m beg end))
  245. org-clock-history))
  246. (defun org-clock-select-task (&optional prompt)
  247. "Select a task that recently was associated with clocking."
  248. (interactive)
  249. (let (sel-list rpl (i 0) s)
  250. (save-window-excursion
  251. (org-switch-to-buffer-other-window
  252. (get-buffer-create "*Clock Task Select*"))
  253. (erase-buffer)
  254. (when (marker-buffer org-clock-default-task)
  255. (insert (org-add-props "Default Task\n" nil 'face 'bold))
  256. (setq s (org-clock-insert-selection-line ?d org-clock-default-task))
  257. (push s sel-list))
  258. (when (marker-buffer org-clock-interrupted-task)
  259. (insert (org-add-props "The task interrupted by starting the last one\n" nil 'face 'bold))
  260. (setq s (org-clock-insert-selection-line ?i org-clock-interrupted-task))
  261. (push s sel-list))
  262. (when (marker-buffer org-clock-marker)
  263. (insert (org-add-props "Current Clocking Task\n" nil 'face 'bold))
  264. (setq s (org-clock-insert-selection-line ?c org-clock-marker))
  265. (push s sel-list))
  266. (insert (org-add-props "Recent Tasks\n" nil 'face 'bold))
  267. (mapc
  268. (lambda (m)
  269. (when (marker-buffer m)
  270. (setq i (1+ i)
  271. s (org-clock-insert-selection-line
  272. (if (< i 10)
  273. (+ i ?0)
  274. (+ i (- ?A 10))) m))
  275. (push s sel-list)))
  276. org-clock-history)
  277. (org-fit-window-to-buffer)
  278. (message (or prompt "Select task for clocking:"))
  279. (setq rpl (read-char-exclusive))
  280. (cond
  281. ((eq rpl ?q) nil)
  282. ((eq rpl ?x) nil)
  283. ((assoc rpl sel-list) (cdr (assoc rpl sel-list)))
  284. (t (error "Invalid task choice %c" rpl))))))
  285. (defun org-clock-insert-selection-line (i marker)
  286. "Insert a line for the clock selection menu.
  287. And return a cons cell with the selection character integer and the marker
  288. pointing to it."
  289. (when (marker-buffer marker)
  290. (let (file cat task heading prefix)
  291. (with-current-buffer (org-base-buffer (marker-buffer marker))
  292. (save-excursion
  293. (save-restriction
  294. (widen)
  295. (goto-char marker)
  296. (setq file (buffer-file-name (marker-buffer marker))
  297. cat (or (org-get-category)
  298. (progn (org-refresh-category-properties)
  299. (org-get-category)))
  300. heading (org-get-heading 'notags)
  301. prefix (save-excursion
  302. (org-back-to-heading t)
  303. (looking-at "\\*+ ")
  304. (match-string 0))
  305. task (substring
  306. (org-fontify-like-in-org-mode
  307. (concat prefix heading)
  308. org-odd-levels-only)
  309. (length prefix))))))
  310. (when (and cat task)
  311. (insert (format "[%c] %-15s %s\n" i cat task))
  312. (cons i marker)))))
  313. (defun org-clock-get-clock-string ()
  314. "Form a clock-string, that will be show in the mode line.
  315. If an effort estimate was defined for current item, use
  316. 01:30/01:50 format (clocked/estimated).
  317. If not, show simply the clocked time like 01:50."
  318. (let* ((clocked-time (org-clock-get-clocked-time))
  319. (h (floor clocked-time 60))
  320. (m (- clocked-time (* 60 h))))
  321. (if (and org-clock-effort)
  322. (let* ((effort-in-minutes (org-hh:mm-string-to-minutes org-clock-effort))
  323. (effort-h (floor effort-in-minutes 60))
  324. (effort-m (- effort-in-minutes (* effort-h 60))))
  325. (format (concat "-[" org-time-clocksum-format "/" org-time-clocksum-format " (%s)]")
  326. h m effort-h effort-m org-clock-heading))
  327. (format (concat "-[" org-time-clocksum-format " (%s)]")
  328. h m org-clock-heading))))
  329. (defun org-clock-update-mode-line ()
  330. (setq org-mode-line-string
  331. (org-propertize
  332. (let ((clock-string (org-clock-get-clock-string))
  333. (help-text "Org-mode clock is running.\nmouse-1 shows a menu\nmouse-2 will jump to task"))
  334. (if (and (> org-clock-string-limit 0)
  335. (> (length clock-string) org-clock-string-limit))
  336. (org-propertize (substring clock-string 0 org-clock-string-limit)
  337. 'help-echo (concat help-text ": " org-clock-heading))
  338. (org-propertize clock-string 'help-echo help-text)))
  339. 'local-map org-clock-mode-line-map
  340. 'mouse-face (if (featurep 'xemacs) 'highlight 'mode-line-highlight)
  341. 'face 'org-mode-line-clock))
  342. (if org-clock-effort (org-clock-notify-once-if-expired))
  343. (force-mode-line-update))
  344. (defun org-clock-get-clocked-time ()
  345. "Get the clocked time for the current item in minutes.
  346. The time returned includes the time spent on this task in
  347. previous clocking intervals."
  348. (let ((currently-clocked-time
  349. (floor (- (org-float-time)
  350. (org-float-time org-clock-start-time)) 60)))
  351. (+ currently-clocked-time (or org-clock-total-time 0))))
  352. (defun org-clock-modify-effort-estimate (&optional value)
  353. "Add to or set the effort estimate of the item currently being clocked.
  354. VALUE can be a number of minutes, or a string with forat hh:mm or mm.
  355. WHen the strig starts with a + or a - sign, the current value of the effort
  356. property will be changed by that amount.
  357. This will update the \"Effort\" property of currently clocked item, and
  358. the mode line."
  359. (interactive)
  360. (when (org-clock-is-active)
  361. (let ((current org-clock-effort) sign)
  362. (unless value
  363. ;; Prompt user for a value or a change
  364. (setq value
  365. (read-string
  366. (format "Set effort (hh:mm or mm%s): "
  367. (if current
  368. (format ", prefix + to add to %s" org-clock-effort)
  369. "")))))
  370. (when (stringp value)
  371. ;; A string. See if it is a delta
  372. (setq sign (string-to-char value))
  373. (if (member sign '(?- ?+))
  374. (setq current (org-hh:mm-string-to-minutes (substring current 1)))
  375. (setq current 0))
  376. (setq value (org-hh:mm-string-to-minutes value))
  377. (if (equal ?- sign)
  378. (setq value (- current value))
  379. (if (equal ?+ sign) (setq value (+ current value)))))
  380. (setq value (max 0 value)
  381. org-clock-effort (org-minutes-to-hh:mm-string value))
  382. (org-entry-put org-clock-marker "Effort" org-clock-effort)
  383. (org-clock-update-mode-line)
  384. (message "Effort is now %s" org-clock-effort))))
  385. (defvar org-clock-notification-was-shown nil
  386. "Shows if we have shown notification already.")
  387. (defun org-clock-notify-once-if-expired ()
  388. "Show notification if we spent more time than we estimated before.
  389. Notification is shown only once."
  390. (when (marker-buffer org-clock-marker)
  391. (let ((effort-in-minutes (org-hh:mm-string-to-minutes org-clock-effort))
  392. (clocked-time (org-clock-get-clocked-time)))
  393. (if (>= clocked-time effort-in-minutes)
  394. (unless org-clock-notification-was-shown
  395. (setq org-clock-notification-was-shown t)
  396. (org-notify
  397. (format "Task '%s' should be finished by now. (%s)"
  398. org-clock-heading org-clock-effort) t))
  399. (setq org-clock-notification-was-shown nil)))))
  400. (defun org-notify (notification &optional play-sound)
  401. "Send a NOTIFICATION and maybe PLAY-SOUND."
  402. (org-show-notification notification)
  403. (if play-sound (org-clock-play-sound)))
  404. (defun org-show-notification (notification)
  405. "Show notification.
  406. Use `org-show-notification-handler' if defined,
  407. use libnotify if available, or fall back on a message."
  408. (cond ((functionp org-show-notification-handler)
  409. (funcall org-show-notification-handler notification))
  410. ((stringp org-show-notification-handler)
  411. (start-process "emacs-timer-notification" nil
  412. org-show-notification-handler notification))
  413. ((org-program-exists "notify-send")
  414. (start-process "emacs-timer-notification" nil
  415. "notify-send" notification))
  416. ;; Maybe the handler will send a message, so only use message as
  417. ;; a fall back option
  418. (t (message notification))))
  419. (defun org-clock-play-sound ()
  420. "Play sound as configured by `org-clock-sound'.
  421. Use alsa's aplay tool if available."
  422. (cond
  423. ((not org-clock-sound))
  424. ((eq org-clock-sound t) (beep t) (beep t))
  425. ((stringp org-clock-sound)
  426. (if (file-exists-p org-clock-sound)
  427. (if (org-program-exists "aplay")
  428. (start-process "org-clock-play-notification" nil
  429. "aplay" org-clock-sound)
  430. (condition-case nil
  431. (play-sound-file org-clock-sound)
  432. (error (beep t) (beep t))))))))
  433. (defun org-program-exists (program-name)
  434. "Checks whenever we can locate program and launch it."
  435. (if (eq system-type 'gnu/linux)
  436. (= 0 (call-process "which" nil nil nil program-name))))
  437. (defvar org-clock-mode-line-entry nil
  438. "Information for the modeline about the running clock.")
  439. (defun org-find-open-clocks (file)
  440. "Search through the given file and find all open clocks."
  441. (let ((buf (or (get-file-buffer file)
  442. (find-file-noselect file)))
  443. clocks)
  444. (with-current-buffer buf
  445. (save-excursion
  446. (goto-char (point-min))
  447. (while (re-search-forward "CLOCK: \\(\\[.*?\\]\\)$" nil t)
  448. (push (cons (copy-marker (1- (match-end 1)) t)
  449. (org-time-string-to-time (match-string 1))) clocks))))
  450. clocks))
  451. (defsubst org-is-active-clock (clock)
  452. "Return t if CLOCK is the currently active clock."
  453. (and (org-clock-is-active)
  454. (= org-clock-marker (car clock))))
  455. (defmacro org-with-clock-position (clock &rest forms)
  456. "Evaluate FORMS with CLOCK as the current active clock."
  457. `(with-current-buffer (marker-buffer (car ,clock))
  458. (save-excursion
  459. (save-restriction
  460. (widen)
  461. (goto-char (car ,clock))
  462. (beginning-of-line)
  463. ,@forms))))
  464. (put 'org-with-clock-position 'lisp-indent-function 1)
  465. (defmacro org-with-clock (clock &rest forms)
  466. "Evaluate FORMS with CLOCK as the current active clock.
  467. This macro also protects the current active clock from being altered."
  468. `(org-with-clock-position ,clock
  469. (let ((org-clock-start-time (cdr ,clock))
  470. (org-clock-total-time)
  471. (org-clock-history)
  472. (org-clock-effort)
  473. (org-clock-marker (car ,clock))
  474. (org-clock-hd-marker (save-excursion
  475. (outline-back-to-heading t)
  476. (point-marker))))
  477. ,@forms)))
  478. (put 'org-with-clock 'lisp-indent-function 1)
  479. (defsubst org-clock-clock-in (clock &optional resume)
  480. "Clock in to the clock located by CLOCK.
  481. If necessary, clock-out of the currently active clock."
  482. (org-with-clock-position clock
  483. (let ((org-clock-in-resume (or resume org-clock-in-resume)))
  484. (org-clock-in))))
  485. (defsubst org-clock-clock-out (clock &optional fail-quietly at-time)
  486. "Clock out of the clock located by CLOCK."
  487. (let ((temp (copy-marker (car clock)
  488. (marker-insertion-type (car clock)))))
  489. (if (org-is-active-clock clock)
  490. (org-clock-out fail-quietly at-time)
  491. (org-with-clock clock
  492. (org-clock-out fail-quietly at-time)))
  493. (setcar clock temp)))
  494. (defsubst org-clock-clock-cancel (clock)
  495. "Cancel the clock located by CLOCK."
  496. (let ((temp (copy-marker (car clock)
  497. (marker-insertion-type (car clock)))))
  498. (if (org-is-active-clock clock)
  499. (org-clock-cancel)
  500. (org-with-clock clock
  501. (org-clock-cancel)))
  502. (setcar clock temp)))
  503. (defun org-clock-in (&optional select)
  504. "Start the clock on the current item.
  505. If necessary, clock-out of the currently active clock.
  506. With prefix arg SELECT, offer a list of recently clocked tasks to
  507. clock into. When SELECT is `C-u C-u', clock into the current task and mark
  508. is as the default task, a special task that will always be offered in
  509. the clocking selection, associated with the letter `d'."
  510. (interactive "P")
  511. (setq org-clock-notification-was-shown nil)
  512. (catch 'abort
  513. (let ((interrupting (marker-buffer org-clock-marker))
  514. ts selected-task target-pos (msg-extra ""))
  515. (when (equal select '(4))
  516. (setq selected-task (org-clock-select-task "Clock-in on task: "))
  517. (if selected-task
  518. (setq selected-task (copy-marker selected-task))
  519. (error "Abort")))
  520. (when interrupting
  521. ;; We are interrupting the clocking of a different task.
  522. ;; Save a marker to this task, so that we can go back.
  523. (move-marker org-clock-interrupted-task
  524. (marker-position org-clock-marker)
  525. (marker-buffer org-clock-marker))
  526. (org-clock-out t))
  527. (when (equal select '(16))
  528. ;; Mark as default clocking task
  529. (org-clock-mark-default-task))
  530. ;; Clock in at which position?
  531. (setq target-pos
  532. (if (and (eobp) (not (org-on-heading-p)))
  533. (point-at-bol 0)
  534. (point)))
  535. (run-hooks 'org-clock-in-prepare-hook)
  536. (save-excursion
  537. (when (and selected-task (marker-buffer selected-task))
  538. ;; There is a selected task, move to the correct buffer
  539. ;; and set the new target position.
  540. (set-buffer (org-base-buffer (marker-buffer selected-task)))
  541. (setq target-pos (marker-position selected-task))
  542. (move-marker selected-task nil))
  543. (save-excursion
  544. (save-restriction
  545. (widen)
  546. (goto-char target-pos)
  547. (org-back-to-heading t)
  548. (or interrupting (move-marker org-clock-interrupted-task nil))
  549. (org-clock-history-push)
  550. (cond ((functionp org-clock-in-switch-to-state)
  551. (looking-at org-complex-heading-regexp)
  552. (let ((newstate (funcall org-clock-in-switch-to-state
  553. (match-string 2))))
  554. (if newstate (org-todo newstate))))
  555. ((and org-clock-in-switch-to-state
  556. (not (looking-at (concat outline-regexp "[ \t]*"
  557. org-clock-in-switch-to-state
  558. "\\>"))))
  559. (org-todo org-clock-in-switch-to-state)))
  560. (setq org-clock-heading-for-remember
  561. (and (looking-at org-complex-heading-regexp)
  562. (match-end 4)
  563. (org-trim (buffer-substring (match-end 1)
  564. (match-end 4)))))
  565. (setq org-clock-heading
  566. (cond ((and org-clock-heading-function
  567. (functionp org-clock-heading-function))
  568. (funcall org-clock-heading-function))
  569. ((looking-at org-complex-heading-regexp)
  570. (match-string 4))
  571. (t "???")))
  572. (setq org-clock-heading (org-propertize org-clock-heading
  573. 'face nil))
  574. (org-clock-find-position org-clock-in-resume)
  575. (cond
  576. ((and org-clock-in-resume
  577. (looking-at
  578. (concat "^[ \t]* " org-clock-string
  579. " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
  580. " +\\sw+\.? +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")))
  581. (message "Matched %s" (match-string 1))
  582. (setq ts (concat "[" (match-string 1) "]"))
  583. (goto-char (match-end 1))
  584. (setq org-clock-start-time
  585. (apply 'encode-time
  586. (org-parse-time-string (match-string 1))))
  587. (setq org-clock-effort (org-get-effort))
  588. (setq org-clock-total-time (org-clock-sum-current-item
  589. (org-clock-get-sum-start))))
  590. ((eq org-clock-in-resume 'auto-restart)
  591. ;; called from org-clock-load during startup,
  592. ;; do not interrupt, but warn!
  593. (message "Cannot restart clock because task does not contain unfinished clock")
  594. (ding)
  595. (sit-for 2)
  596. (throw 'abort nil))
  597. (t
  598. (insert-before-markers "\n")
  599. (backward-char 1)
  600. (org-indent-line-function)
  601. (when (and (save-excursion
  602. (end-of-line 0)
  603. (org-in-item-p)))
  604. (beginning-of-line 1)
  605. (org-indent-line-to (- (org-get-indentation) 2)))
  606. (insert org-clock-string " ")
  607. (setq org-clock-effort (org-get-effort))
  608. (setq org-clock-total-time (org-clock-sum-current-item
  609. (org-clock-get-sum-start)))
  610. (setq org-clock-start-time (current-time))
  611. (setq ts (org-insert-time-stamp org-clock-start-time
  612. 'with-hm 'inactive))))
  613. (move-marker org-clock-marker (point) (buffer-base-buffer))
  614. (move-marker org-clock-hd-marker
  615. (save-excursion (org-back-to-heading t) (point))
  616. (buffer-base-buffer))
  617. (or global-mode-string (setq global-mode-string '("")))
  618. (or (memq 'org-mode-line-string global-mode-string)
  619. (setq global-mode-string
  620. (append global-mode-string '(org-mode-line-string))))
  621. (org-clock-update-mode-line)
  622. (when org-clock-mode-line-timer
  623. (cancel-timer org-clock-mode-line-timer)
  624. (setq org-clock-mode-line-timer nil))
  625. (setq org-clock-mode-line-timer
  626. (run-with-timer 60 60 'org-clock-update-mode-line))
  627. (message "Clock starts at %s - %s" ts msg-extra)
  628. (run-hooks 'org-clock-in-hook)))))))
  629. (defun org-clock-mark-default-task ()
  630. "Mark current task as default task."
  631. (interactive)
  632. (save-excursion
  633. (org-back-to-heading t)
  634. (move-marker org-clock-default-task (point))))
  635. (defvar msg-extra)
  636. (defun org-clock-get-sum-start ()
  637. "Return the time from which clock times should be counted.
  638. This is for the currently running clock as it is displayed
  639. in the mode line. This function looks at the properties
  640. LAST_REPEAT and in particular CLOCK_MODELINE_TOTAL and the
  641. corresponding variable `org-clock-modeline-total' and then
  642. decides which time to use."
  643. (let ((cmt (or (org-entry-get nil "CLOCK_MODELINE_TOTAL")
  644. (symbol-name org-clock-modeline-total)))
  645. (lr (org-entry-get nil "LAST_REPEAT")))
  646. (cond
  647. ((equal cmt "current")
  648. (setq msg-extra "showing time in current clock instance")
  649. (current-time))
  650. ((equal cmt "today")
  651. (setq msg-extra "showing today's task time.")
  652. (let* ((dt (decode-time (current-time))))
  653. (setq dt (append (list 0 0 0) (nthcdr 3 dt)))
  654. (if org-extend-today-until
  655. (setf (nth 2 dt) org-extend-today-until))
  656. (apply 'encode-time dt)))
  657. ((or (equal cmt "all")
  658. (and (or (not cmt) (equal cmt "auto"))
  659. (not lr)))
  660. (setq msg-extra "showing entire task time.")
  661. nil)
  662. ((or (equal cmt "repeat")
  663. (and (or (not cmt) (equal cmt "auto"))
  664. lr))
  665. (setq msg-extra "showing task time since last repeat.")
  666. (if (not lr)
  667. nil
  668. (org-time-string-to-time lr)))
  669. (t nil))))
  670. (defun org-clock-find-position (find-unclosed)
  671. "Find the location where the next clock line should be inserted.
  672. When FIND-UNCLOSED is non-nil, first check if there is an unclosed clock
  673. line and position cursor in that line."
  674. (org-back-to-heading t)
  675. (catch 'exit
  676. (let ((beg (save-excursion
  677. (beginning-of-line 2)
  678. (or (bolp) (newline))
  679. (point)))
  680. (end (progn (outline-next-heading) (point)))
  681. (re (concat "^[ \t]*" org-clock-string))
  682. (cnt 0)
  683. (drawer (if (stringp org-clock-into-drawer)
  684. org-clock-into-drawer "LOGBOOK"))
  685. first last ind-last)
  686. (goto-char beg)
  687. (when (and find-unclosed
  688. (re-search-forward
  689. (concat "^[ \t]* " org-clock-string
  690. " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
  691. " +\\sw+ +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")
  692. end t))
  693. (beginning-of-line 1)
  694. (throw 'exit t))
  695. (when (eobp) (newline) (setq end (max (point) end)))
  696. (when (re-search-forward (concat "^[ \t]*:" drawer ":") end t)
  697. ;; we seem to have a CLOCK drawer, so go there.
  698. (beginning-of-line 2)
  699. (or org-log-states-order-reversed
  700. (and (re-search-forward org-property-end-re nil t)
  701. (goto-char (match-beginning 0))))
  702. (throw 'exit t))
  703. ;; Lets count the CLOCK lines
  704. (goto-char beg)
  705. (while (re-search-forward re end t)
  706. (setq first (or first (match-beginning 0))
  707. last (match-beginning 0)
  708. cnt (1+ cnt)))
  709. (when (and (integerp org-clock-into-drawer)
  710. last
  711. (>= (1+ cnt) org-clock-into-drawer))
  712. ;; Wrap current entries into a new drawer
  713. (goto-char last)
  714. (setq ind-last (org-get-indentation))
  715. (beginning-of-line 2)
  716. (if (and (>= (org-get-indentation) ind-last)
  717. (org-at-item-p))
  718. (org-end-of-item))
  719. (insert ":END:\n")
  720. (beginning-of-line 0)
  721. (org-indent-line-to ind-last)
  722. (goto-char first)
  723. (insert ":" drawer ":\n")
  724. (beginning-of-line 0)
  725. (org-indent-line-function)
  726. (org-flag-drawer t)
  727. (beginning-of-line 2)
  728. (or org-log-states-order-reversed
  729. (and (re-search-forward org-property-end-re nil t)
  730. (goto-char (match-beginning 0))))
  731. (throw 'exit nil))
  732. (goto-char beg)
  733. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  734. (not (equal (match-string 1) org-clock-string)))
  735. ;; Planning info, skip to after it
  736. (beginning-of-line 2)
  737. (or (bolp) (newline)))
  738. (when (or (eq org-clock-into-drawer t)
  739. (stringp org-clock-into-drawer)
  740. (and (integerp org-clock-into-drawer)
  741. (< org-clock-into-drawer 2)))
  742. (insert ":" drawer ":\n:END:\n")
  743. (beginning-of-line -1)
  744. (org-indent-line-function)
  745. (org-flag-drawer t)
  746. (beginning-of-line 2)
  747. (org-indent-line-function)
  748. (beginning-of-line)
  749. (or org-log-states-order-reversed
  750. (and (re-search-forward org-property-end-re nil t)
  751. (goto-char (match-beginning 0))))))))
  752. (defun org-clock-out (&optional fail-quietly at-time)
  753. "Stop the currently running clock.
  754. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  755. (interactive)
  756. (catch 'exit
  757. (if (not (marker-buffer org-clock-marker))
  758. (if fail-quietly (throw 'exit t) (error "No active clock")))
  759. (let (ts te s h m remove)
  760. (save-excursion
  761. (set-buffer (marker-buffer org-clock-marker))
  762. (save-restriction
  763. (widen)
  764. (goto-char org-clock-marker)
  765. (beginning-of-line 1)
  766. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  767. (equal (match-string 1) org-clock-string))
  768. (setq ts (match-string 2))
  769. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  770. (goto-char (match-end 0))
  771. (delete-region (point) (point-at-eol))
  772. (insert "--")
  773. (setq te (org-insert-time-stamp (or at-time (current-time))
  774. 'with-hm 'inactive))
  775. (setq s (- (org-float-time (apply 'encode-time (org-parse-time-string te)))
  776. (org-float-time (apply 'encode-time (org-parse-time-string ts))))
  777. h (floor (/ s 3600))
  778. s (- s (* 3600 h))
  779. m (floor (/ s 60))
  780. s (- s (* 60 s)))
  781. (insert " => " (format "%2d:%02d" h m))
  782. (when (setq remove (and org-clock-out-remove-zero-time-clocks
  783. (= (+ h m) 0)))
  784. (beginning-of-line 1)
  785. (delete-region (point) (point-at-eol))
  786. (and (looking-at "\n") (> (point-max) (1+ (point)))
  787. (delete-char 1)))
  788. (move-marker org-clock-marker nil)
  789. (move-marker org-clock-hd-marker nil)
  790. (when org-log-note-clock-out
  791. (org-add-log-setup 'clock-out nil nil nil nil
  792. (concat "# Task: " (org-get-heading t) "\n\n")))
  793. (when org-clock-mode-line-timer
  794. (cancel-timer org-clock-mode-line-timer)
  795. (setq org-clock-mode-line-timer nil))
  796. (setq global-mode-string
  797. (delq 'org-mode-line-string global-mode-string))
  798. (when org-clock-out-switch-to-state
  799. (save-excursion
  800. (org-back-to-heading t)
  801. (let ((org-inhibit-logging t))
  802. (cond
  803. ((functionp org-clock-out-switch-to-state)
  804. (looking-at org-complex-heading-regexp)
  805. (let ((newstate (funcall org-clock-out-switch-to-state
  806. (match-string 2))))
  807. (if newstate (org-todo newstate))))
  808. ((and org-clock-out-switch-to-state
  809. (not (looking-at (concat outline-regexp "[ \t]*"
  810. org-clock-out-switch-to-state
  811. "\\>"))))
  812. (org-todo org-clock-out-switch-to-state))))))
  813. (force-mode-line-update)
  814. (message (concat "Clock stopped at %s after HH:MM = " org-time-clocksum-format "%s") te h m
  815. (if remove " => LINE REMOVED" ""))
  816. (run-hooks 'org-clock-out-hook))))))
  817. (defun org-clock-cancel ()
  818. "Cancel the running clock be removing the start timestamp."
  819. (interactive)
  820. (if (not (marker-buffer org-clock-marker))
  821. (error "No active clock"))
  822. (save-excursion
  823. (set-buffer (marker-buffer org-clock-marker))
  824. (goto-char org-clock-marker)
  825. (delete-region (1- (point-at-bol)) (point-at-eol)))
  826. (move-marker org-clock-marker nil)
  827. (move-marker org-clock-hd-marker nil)
  828. (setq global-mode-string
  829. (delq 'org-mode-line-string global-mode-string))
  830. (force-mode-line-update)
  831. (message "Clock canceled")
  832. (run-hooks 'org-clock-cancel-hook))
  833. (defun org-clock-goto (&optional select)
  834. "Go to the currently clocked-in entry, or to the most recently clocked one.
  835. With prefix arg SELECT, offer recently clocked tasks for selection."
  836. (interactive "@P")
  837. (let* ((recent nil)
  838. (m (cond
  839. (select
  840. (or (org-clock-select-task "Select task to go to: ")
  841. (error "No task selected")))
  842. ((marker-buffer org-clock-marker) org-clock-marker)
  843. ((and org-clock-goto-may-find-recent-task
  844. (car org-clock-history)
  845. (marker-buffer (car org-clock-history)))
  846. (setq recent t)
  847. (car org-clock-history))
  848. (t (error "No active or recent clock task")))))
  849. (switch-to-buffer (marker-buffer m))
  850. (if (or (< m (point-min)) (> m (point-max))) (widen))
  851. (goto-char m)
  852. (org-show-entry)
  853. (org-back-to-heading t)
  854. (org-cycle-hide-drawers 'children)
  855. (recenter)
  856. (if recent
  857. (message "No running clock, this is the most recently clocked task"))
  858. (run-hooks 'org-clock-goto-hook)))
  859. (defvar org-clock-file-total-minutes nil
  860. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  861. (make-variable-buffer-local 'org-clock-file-total-minutes)
  862. (defun org-clock-sum (&optional tstart tend)
  863. "Sum the times for each subtree.
  864. Puts the resulting times in minutes as a text property on each headline.
  865. TSTART and TEND can mark a time range to be considered."
  866. (interactive)
  867. (let* ((bmp (buffer-modified-p))
  868. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  869. org-clock-string
  870. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  871. (lmax 30)
  872. (ltimes (make-vector lmax 0))
  873. (t1 0)
  874. (level 0)
  875. ts te dt
  876. time)
  877. (if (stringp tstart) (setq tstart (org-time-string-to-seconds tstart)))
  878. (if (stringp tend) (setq tend (org-time-string-to-seconds tend)))
  879. (if (consp tstart) (setq tstart (org-float-time tstart)))
  880. (if (consp tend) (setq tend (org-float-time tend)))
  881. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  882. (save-excursion
  883. (goto-char (point-max))
  884. (while (re-search-backward re nil t)
  885. (cond
  886. ((match-end 2)
  887. ;; Two time stamps
  888. (setq ts (match-string 2)
  889. te (match-string 3)
  890. ts (org-float-time
  891. (apply 'encode-time (org-parse-time-string ts)))
  892. te (org-float-time
  893. (apply 'encode-time (org-parse-time-string te)))
  894. ts (if tstart (max ts tstart) ts)
  895. te (if tend (min te tend) te)
  896. dt (- te ts)
  897. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  898. ((match-end 4)
  899. ;; A naked time
  900. (setq t1 (+ t1 (string-to-number (match-string 5))
  901. (* 60 (string-to-number (match-string 4))))))
  902. (t ;; A headline
  903. (setq level (- (match-end 1) (match-beginning 1)))
  904. (when (or (> t1 0) (> (aref ltimes level) 0))
  905. (loop for l from 0 to level do
  906. (aset ltimes l (+ (aref ltimes l) t1)))
  907. (setq t1 0 time (aref ltimes level))
  908. (loop for l from level to (1- lmax) do
  909. (aset ltimes l 0))
  910. (goto-char (match-beginning 0))
  911. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  912. (setq org-clock-file-total-minutes (aref ltimes 0)))
  913. (set-buffer-modified-p bmp)))
  914. (defun org-clock-sum-current-item (&optional tstart)
  915. "Returns time, clocked on current item in total"
  916. (save-excursion
  917. (save-restriction
  918. (org-narrow-to-subtree)
  919. (org-clock-sum tstart)
  920. org-clock-file-total-minutes)))
  921. (defun org-clock-display (&optional total-only)
  922. "Show subtree times in the entire buffer.
  923. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  924. in the echo area."
  925. (interactive)
  926. (org-clock-remove-overlays)
  927. (let (time h m p)
  928. (org-clock-sum)
  929. (unless total-only
  930. (save-excursion
  931. (goto-char (point-min))
  932. (while (or (and (equal (setq p (point)) (point-min))
  933. (get-text-property p :org-clock-minutes))
  934. (setq p (next-single-property-change
  935. (point) :org-clock-minutes)))
  936. (goto-char p)
  937. (when (setq time (get-text-property p :org-clock-minutes))
  938. (org-clock-put-overlay time (funcall outline-level))))
  939. (setq h (/ org-clock-file-total-minutes 60)
  940. m (- org-clock-file-total-minutes (* 60 h)))
  941. ;; Arrange to remove the overlays upon next change.
  942. (when org-remove-highlights-with-change
  943. (org-add-hook 'before-change-functions 'org-clock-remove-overlays
  944. nil 'local))))
  945. (message (concat "Total file time: " org-time-clocksum-format " (%d hours and %d minutes)") h m h m)))
  946. (defvar org-clock-overlays nil)
  947. (make-variable-buffer-local 'org-clock-overlays)
  948. (defun org-clock-put-overlay (time &optional level)
  949. "Put an overlays on the current line, displaying TIME.
  950. If LEVEL is given, prefix time with a corresponding number of stars.
  951. This creates a new overlay and stores it in `org-clock-overlays', so that it
  952. will be easy to remove."
  953. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  954. (l (if level (org-get-valid-level level 0) 0))
  955. (fmt (concat "%s " org-time-clocksum-format "%s"))
  956. (off 0)
  957. ov tx)
  958. (org-move-to-column c)
  959. (unless (eolp) (skip-chars-backward "^ \t"))
  960. (skip-chars-backward " \t")
  961. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  962. tx (concat (buffer-substring (1- (point)) (point))
  963. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  964. (org-add-props (format fmt
  965. (make-string l ?*) h m
  966. (make-string (- 16 l) ?\ ))
  967. (list 'face 'org-clock-overlay))
  968. ""))
  969. (if (not (featurep 'xemacs))
  970. (org-overlay-put ov 'display tx)
  971. (org-overlay-put ov 'invisible t)
  972. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  973. (push ov org-clock-overlays)))
  974. (defun org-clock-remove-overlays (&optional beg end noremove)
  975. "Remove the occur highlights from the buffer.
  976. BEG and END are ignored. If NOREMOVE is nil, remove this function
  977. from the `before-change-functions' in the current buffer."
  978. (interactive)
  979. (unless org-inhibit-highlight-removal
  980. (mapc 'org-delete-overlay org-clock-overlays)
  981. (setq org-clock-overlays nil)
  982. (unless noremove
  983. (remove-hook 'before-change-functions
  984. 'org-clock-remove-overlays 'local))))
  985. (defvar state) ;; dynamically scoped into this function
  986. (defun org-clock-out-if-current ()
  987. "Clock out if the current entry contains the running clock.
  988. This is used to stop the clock after a TODO entry is marked DONE,
  989. and is only done if the variable `org-clock-out-when-done' is not nil."
  990. (when (and org-clock-out-when-done
  991. (member state org-done-keywords)
  992. (equal (or (buffer-base-buffer (marker-buffer org-clock-marker))
  993. (marker-buffer org-clock-marker))
  994. (or (buffer-base-buffer (current-buffer))
  995. (current-buffer)))
  996. (< (point) org-clock-marker)
  997. (> (save-excursion (outline-next-heading) (point))
  998. org-clock-marker))
  999. ;; Clock out, but don't accept a logging message for this.
  1000. (let ((org-log-note-clock-out nil))
  1001. (org-clock-out))))
  1002. (add-hook 'org-after-todo-state-change-hook
  1003. 'org-clock-out-if-current)
  1004. ;;;###autoload
  1005. (defun org-get-clocktable (&rest props)
  1006. "Get a formatted clocktable with parameters according to PROPS.
  1007. The table is created in a temporary buffer, fully formatted and
  1008. fontified, and then returned."
  1009. ;; Set the defaults
  1010. (setq props (plist-put props :name "clocktable"))
  1011. (unless (plist-member props :maxlevel)
  1012. (setq props (plist-put props :maxlevel 2)))
  1013. (unless (plist-member props :scope)
  1014. (setq props (plist-put props :scope 'agenda)))
  1015. (with-temp-buffer
  1016. (org-mode)
  1017. (org-create-dblock props)
  1018. (org-update-dblock)
  1019. (font-lock-fontify-buffer)
  1020. (forward-line 2)
  1021. (buffer-substring (point) (progn
  1022. (re-search-forward "^#\\+END" nil t)
  1023. (point-at-bol)))))
  1024. (defun org-clock-report (&optional arg)
  1025. "Create a table containing a report about clocked time.
  1026. If the cursor is inside an existing clocktable block, then the table
  1027. will be updated. If not, a new clocktable will be inserted.
  1028. When called with a prefix argument, move to the first clock table in the
  1029. buffer and update it."
  1030. (interactive "P")
  1031. (org-clock-remove-overlays)
  1032. (when arg
  1033. (org-find-dblock "clocktable")
  1034. (org-show-entry))
  1035. (if (org-in-clocktable-p)
  1036. (goto-char (org-in-clocktable-p))
  1037. (org-create-dblock (append (list :name "clocktable")
  1038. org-clock-clocktable-default-properties)))
  1039. (org-update-dblock))
  1040. (defun org-in-clocktable-p ()
  1041. "Check if the cursor is in a clocktable."
  1042. (let ((pos (point)) start)
  1043. (save-excursion
  1044. (end-of-line 1)
  1045. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  1046. (setq start (match-beginning 0))
  1047. (re-search-forward "^#\\+END:.*" nil t)
  1048. (>= (match-end 0) pos)
  1049. start))))
  1050. (defun org-clock-special-range (key &optional time as-strings)
  1051. "Return two times bordering a special time range.
  1052. Key is a symbol specifying the range and can be one of `today', `yesterday',
  1053. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  1054. A week starts Monday 0:00 and ends Sunday 24:00.
  1055. The range is determined relative to TIME. TIME defaults to the current time.
  1056. The return value is a cons cell with two internal times like the ones
  1057. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  1058. the returned times will be formatted strings."
  1059. (if (integerp key) (setq key (intern (number-to-string key))))
  1060. (let* ((tm (decode-time (or time (current-time))))
  1061. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  1062. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  1063. (dow (nth 6 tm))
  1064. (skey (symbol-name key))
  1065. (shift 0)
  1066. s1 m1 h1 d1 month1 y1 diff ts te fm txt w date)
  1067. (cond
  1068. ((string-match "^[0-9]+$" skey)
  1069. (setq y (string-to-number skey) m 1 d 1 key 'year))
  1070. ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)$" skey)
  1071. (setq y (string-to-number (match-string 1 skey))
  1072. month (string-to-number (match-string 2 skey))
  1073. d 1 key 'month))
  1074. ((string-match "^\\([0-9]+\\)-[wW]\\([0-9]\\{1,2\\}\\)$" skey)
  1075. (require 'cal-iso)
  1076. (setq y (string-to-number (match-string 1 skey))
  1077. w (string-to-number (match-string 2 skey)))
  1078. (setq date (calendar-gregorian-from-absolute
  1079. (calendar-absolute-from-iso (list w 1 y))))
  1080. (setq d (nth 1 date) month (car date) y (nth 2 date)
  1081. dow 1
  1082. key 'week))
  1083. ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)-\\([0-9]\\{1,2\\}\\)$" skey)
  1084. (setq y (string-to-number (match-string 1 skey))
  1085. month (string-to-number (match-string 2 skey))
  1086. d (string-to-number (match-string 3 skey))
  1087. key 'day))
  1088. ((string-match "\\([-+][0-9]+\\)$" skey)
  1089. (setq shift (string-to-number (match-string 1 skey))
  1090. key (intern (substring skey 0 (match-beginning 1))))))
  1091. (when (= shift 0)
  1092. (cond ((eq key 'yesterday) (setq key 'today shift -1))
  1093. ((eq key 'lastweek) (setq key 'week shift -1))
  1094. ((eq key 'lastmonth) (setq key 'month shift -1))
  1095. ((eq key 'lastyear) (setq key 'year shift -1))))
  1096. (cond
  1097. ((memq key '(day today))
  1098. (setq d (+ d shift) h 0 m 0 h1 24 m1 0))
  1099. ((memq key '(week thisweek))
  1100. (setq diff (+ (* -7 shift) (if (= dow 0) 6 (1- dow)))
  1101. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  1102. ((memq key '(month thismonth))
  1103. (setq d 1 h 0 m 0 d1 1 month (+ month shift) month1 (1+ month) h1 0 m1 0))
  1104. ((memq key '(year thisyear))
  1105. (setq m 0 h 0 d 1 month 1 y (+ y shift) y1 (1+ y)))
  1106. (t (error "No such time block %s" key)))
  1107. (setq ts (encode-time s m h d month y)
  1108. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  1109. (or d1 d) (or month1 month) (or y1 y)))
  1110. (setq fm (cdr org-time-stamp-formats))
  1111. (cond
  1112. ((memq key '(day today))
  1113. (setq txt (format-time-string "%A, %B %d, %Y" ts)))
  1114. ((memq key '(week thisweek))
  1115. (setq txt (format-time-string "week %G-W%V" ts)))
  1116. ((memq key '(month thismonth))
  1117. (setq txt (format-time-string "%B %Y" ts)))
  1118. ((memq key '(year thisyear))
  1119. (setq txt (format-time-string "the year %Y" ts))))
  1120. (if as-strings
  1121. (list (format-time-string fm ts) (format-time-string fm te) txt)
  1122. (list ts te txt))))
  1123. (defun org-clocktable-shift (dir n)
  1124. "Try to shift the :block date of the clocktable at point.
  1125. Point must be in the #+BEGIN: line of a clocktable, or this function
  1126. will throw an error.
  1127. DIR is a direction, a symbol `left', `right', `up', or `down'.
  1128. Both `left' and `down' shift the block toward the past, `up' and `right'
  1129. push it toward the future.
  1130. N is the number of shift steps to take. The size of the step depends on
  1131. the currently selected interval size."
  1132. (setq n (prefix-numeric-value n))
  1133. (and (memq dir '(left down)) (setq n (- n)))
  1134. (save-excursion
  1135. (goto-char (point-at-bol))
  1136. (if (not (looking-at "#\\+BEGIN: clocktable\\>.*?:block[ \t]+\\(\\S-+\\)"))
  1137. (error "Line needs a :block definition before this command works")
  1138. (let* ((b (match-beginning 1)) (e (match-end 1))
  1139. (s (match-string 1))
  1140. block shift ins y mw d date wp m)
  1141. (cond
  1142. ((equal s "yesterday") (setq s "today-1"))
  1143. ((equal s "lastweek") (setq s "thisweek-1"))
  1144. ((equal s "lastmonth") (setq s "thismonth-1"))
  1145. ((equal s "lastyear") (setq s "thisyear-1")))
  1146. (cond
  1147. ((string-match "^\\(today\\|thisweek\\|thismonth\\|thisyear\\)\\([-+][0-9]+\\)?$" s)
  1148. (setq block (match-string 1 s)
  1149. shift (if (match-end 2)
  1150. (string-to-number (match-string 2 s))
  1151. 0))
  1152. (setq shift (+ shift n))
  1153. (setq ins (if (= shift 0) block (format "%s%+d" block shift))))
  1154. ((string-match "\\([0-9]+\\)\\(-\\([wW]?\\)\\([0-9]\\{1,2\\}\\)\\(-\\([0-9]\\{1,2\\}\\)\\)?\\)?" s)
  1155. ;; 1 1 2 3 3 4 4 5 6 6 5 2
  1156. (setq y (string-to-number (match-string 1 s))
  1157. wp (and (match-end 3) (match-string 3 s))
  1158. mw (and (match-end 4) (string-to-number (match-string 4 s)))
  1159. d (and (match-end 6) (string-to-number (match-string 6 s))))
  1160. (cond
  1161. (d (setq ins (format-time-string
  1162. "%Y-%m-%d"
  1163. (encode-time 0 0 0 (+ d n) m y))))
  1164. ((and wp mw (> (length wp) 0))
  1165. (require 'cal-iso)
  1166. (setq date (calendar-gregorian-from-absolute (calendar-absolute-from-iso (list (+ mw n) 1 y))))
  1167. (setq ins (format-time-string
  1168. "%G-W%V"
  1169. (encode-time 0 0 0 (nth 1 date) (car date) (nth 2 date)))))
  1170. (mw
  1171. (setq ins (format-time-string
  1172. "%Y-%m"
  1173. (encode-time 0 0 0 1 (+ mw n) y))))
  1174. (y
  1175. (setq ins (number-to-string (+ y n))))))
  1176. (t (error "Cannot shift clocktable block")))
  1177. (when ins
  1178. (goto-char b)
  1179. (insert ins)
  1180. (delete-region (point) (+ (point) (- e b)))
  1181. (beginning-of-line 1)
  1182. (org-update-dblock)
  1183. t)))))
  1184. (defun org-dblock-write:clocktable (params)
  1185. "Write the standard clocktable."
  1186. (catch 'exit
  1187. (let* ((hlchars '((1 . "*") (2 . "/")))
  1188. (ins (make-marker))
  1189. (total-time nil)
  1190. (scope (plist-get params :scope))
  1191. (tostring (plist-get params :tostring))
  1192. (multifile (plist-get params :multifile))
  1193. (header (plist-get params :header))
  1194. (maxlevel (or (plist-get params :maxlevel) 3))
  1195. (step (plist-get params :step))
  1196. (emph (plist-get params :emphasize))
  1197. (timestamp (plist-get params :timestamp))
  1198. (ts (plist-get params :tstart))
  1199. (te (plist-get params :tend))
  1200. (block (plist-get params :block))
  1201. (link (plist-get params :link))
  1202. ipos time p level hlc hdl tsp props content recalc formula pcol
  1203. cc beg end pos tbl tbl1 range-text rm-file-column scope-is-list st)
  1204. (setq org-clock-file-total-minutes nil)
  1205. (when step
  1206. (unless (or block (and ts te))
  1207. (error "Clocktable `:step' can only be used with `:block' or `:tstart,:end'"))
  1208. (org-clocktable-steps params)
  1209. (throw 'exit nil))
  1210. (when block
  1211. (setq cc (org-clock-special-range block nil t)
  1212. ts (car cc) te (nth 1 cc) range-text (nth 2 cc)))
  1213. (when (integerp ts) (setq ts (calendar-gregorian-from-absolute ts)))
  1214. (when (integerp te) (setq te (calendar-gregorian-from-absolute te)))
  1215. (when (and ts (listp ts))
  1216. (setq ts (format "%4d-%02d-%02d" (nth 2 ts) (car ts) (nth 1 ts))))
  1217. (when (and te (listp te))
  1218. (setq te (format "%4d-%02d-%02d" (nth 2 te) (car te) (nth 1 te))))
  1219. ;; Now the times are strings we can parse.
  1220. (if ts (setq ts (org-float-time
  1221. (apply 'encode-time (org-parse-time-string ts)))))
  1222. (if te (setq te (org-float-time
  1223. (apply 'encode-time (org-parse-time-string te)))))
  1224. (move-marker ins (point))
  1225. (setq ipos (point))
  1226. ;; Get the right scope
  1227. (setq pos (point))
  1228. (cond
  1229. ((and scope (listp scope) (symbolp (car scope)))
  1230. (setq scope (eval scope)))
  1231. ((eq scope 'agenda)
  1232. (setq scope (org-agenda-files t)))
  1233. ((eq scope 'agenda-with-archives)
  1234. (setq scope (org-agenda-files t))
  1235. (setq scope (org-add-archive-files scope)))
  1236. ((eq scope 'file-with-archives)
  1237. (setq scope (org-add-archive-files (list (buffer-file-name)))
  1238. rm-file-column t)))
  1239. (setq scope-is-list (and scope (listp scope)))
  1240. (save-restriction
  1241. (cond
  1242. ((not scope))
  1243. ((eq scope 'file) (widen))
  1244. ((eq scope 'subtree) (org-narrow-to-subtree))
  1245. ((eq scope 'tree)
  1246. (while (org-up-heading-safe))
  1247. (org-narrow-to-subtree))
  1248. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  1249. (symbol-name scope)))
  1250. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  1251. (catch 'exit
  1252. (while (org-up-heading-safe)
  1253. (looking-at outline-regexp)
  1254. (if (<= (org-reduced-level (funcall outline-level)) level)
  1255. (throw 'exit nil))))
  1256. (org-narrow-to-subtree))
  1257. (scope-is-list
  1258. (let* ((files scope)
  1259. (scope 'agenda)
  1260. (p1 (copy-sequence params))
  1261. file)
  1262. (setq p1 (plist-put p1 :tostring t))
  1263. (setq p1 (plist-put p1 :multifile t))
  1264. (setq p1 (plist-put p1 :scope 'file))
  1265. (org-prepare-agenda-buffers files)
  1266. (while (setq file (pop files))
  1267. (with-current-buffer (find-buffer-visiting file)
  1268. (setq tbl1 (org-dblock-write:clocktable p1))
  1269. (when tbl1
  1270. (push (org-clocktable-add-file
  1271. file
  1272. (concat "| |*File time*|*"
  1273. (org-minutes-to-hh:mm-string
  1274. org-clock-file-total-minutes)
  1275. "*|\n"
  1276. tbl1)) tbl)
  1277. (setq total-time (+ (or total-time 0)
  1278. org-clock-file-total-minutes))))))))
  1279. (goto-char pos)
  1280. (unless scope-is-list
  1281. (org-clock-sum ts te)
  1282. (goto-char (point-min))
  1283. (setq st t)
  1284. (while (or (and (bobp) (prog1 st (setq st nil))
  1285. (get-text-property (point) :org-clock-minutes)
  1286. (setq p (point-min)))
  1287. (setq p (next-single-property-change (point) :org-clock-minutes)))
  1288. (goto-char p)
  1289. (when (setq time (get-text-property p :org-clock-minutes))
  1290. (save-excursion
  1291. (beginning-of-line 1)
  1292. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  1293. (setq level (org-reduced-level
  1294. (- (match-end 1) (match-beginning 1))))
  1295. (<= level maxlevel))
  1296. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  1297. hdl (if (not link)
  1298. (match-string 2)
  1299. (org-make-link-string
  1300. (format "file:%s::%s"
  1301. (buffer-file-name)
  1302. (save-match-data
  1303. (org-make-org-heading-search-string
  1304. (match-string 2))))
  1305. (match-string 2)))
  1306. tsp (when timestamp
  1307. (setq props (org-entry-properties (point)))
  1308. (or (cdr (assoc "SCHEDULED" props))
  1309. (cdr (assoc "TIMESTAMP" props))
  1310. (cdr (assoc "DEADLINE" props))
  1311. (cdr (assoc "TIMESTAMP_IA" props)))))
  1312. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  1313. (push (concat
  1314. "| " (int-to-string level) "|"
  1315. (if timestamp (concat tsp "|") "")
  1316. hlc hdl hlc " |"
  1317. (make-string (1- level) ?|)
  1318. hlc (org-minutes-to-hh:mm-string time) hlc
  1319. " |") tbl))))))
  1320. (setq tbl (nreverse tbl))
  1321. (if tostring
  1322. (if tbl (mapconcat 'identity tbl "\n") nil)
  1323. (goto-char ins)
  1324. (insert-before-markers
  1325. (or header
  1326. (concat
  1327. "Clock summary at ["
  1328. (substring
  1329. (format-time-string (cdr org-time-stamp-formats))
  1330. 1 -1)
  1331. "]"
  1332. (if block (concat ", for " range-text ".") "")
  1333. "\n\n"))
  1334. (if scope-is-list "|File" "")
  1335. "|L|" (if timestamp "Timestamp|" "") "Headline|Time|\n")
  1336. (setq total-time (or total-time org-clock-file-total-minutes))
  1337. (insert-before-markers
  1338. "|-\n|"
  1339. (if scope-is-list "|" "")
  1340. (if timestamp "|Timestamp|" "|")
  1341. "*Total time*| *"
  1342. (org-minutes-to-hh:mm-string (or total-time 0))
  1343. "*|\n|-\n")
  1344. (setq tbl (delq nil tbl))
  1345. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  1346. (equal (substring (car tbl) 0 2) "|-"))
  1347. (pop tbl))
  1348. (insert-before-markers (mapconcat
  1349. 'identity (delq nil tbl)
  1350. (if scope-is-list "\n|-\n" "\n")))
  1351. (backward-delete-char 1)
  1352. (if (setq formula (plist-get params :formula))
  1353. (cond
  1354. ((eq formula '%)
  1355. (setq pcol (+ (if scope-is-list 1 0) maxlevel 3))
  1356. (insert
  1357. (format
  1358. "\n#+TBLFM: $%d='(org-clock-time%% @%d$%d $%d..$%d);%%.1f"
  1359. pcol
  1360. 2
  1361. (+ 3 (if scope-is-list 1 0))
  1362. (+ (if scope-is-list 1 0) 3)
  1363. (1- pcol)))
  1364. (setq recalc t))
  1365. ((stringp formula)
  1366. (insert "\n#+TBLFM: " formula)
  1367. (setq recalc t))
  1368. (t (error "invalid formula in clocktable")))
  1369. ;; Should we rescue an old formula?
  1370. (when (stringp (setq content (plist-get params :content)))
  1371. (when (string-match "^\\([ \t]*#\\+TBLFM:.*\\)" content)
  1372. (setq recalc t)
  1373. (insert "\n" (match-string 1 (plist-get params :content)))
  1374. (beginning-of-line 0))))
  1375. (goto-char ipos)
  1376. (skip-chars-forward "^|")
  1377. (org-table-align)
  1378. (when recalc
  1379. (if (eq formula '%)
  1380. (save-excursion (org-table-goto-column pcol nil 'force)
  1381. (insert "%")))
  1382. (org-table-recalculate 'all))
  1383. (when rm-file-column
  1384. (forward-char 1)
  1385. (org-table-delete-column)))))))
  1386. (defun org-clocktable-steps (params)
  1387. (let* ((p1 (copy-sequence params))
  1388. (ts (plist-get p1 :tstart))
  1389. (te (plist-get p1 :tend))
  1390. (step0 (plist-get p1 :step))
  1391. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  1392. (block (plist-get p1 :block))
  1393. cc range-text)
  1394. (when block
  1395. (setq cc (org-clock-special-range block nil t)
  1396. ts (car cc) te (nth 1 cc) range-text (nth 2 cc)))
  1397. (if ts (setq ts (org-float-time
  1398. (apply 'encode-time (org-parse-time-string ts)))))
  1399. (if te (setq te (org-float-time
  1400. (apply 'encode-time (org-parse-time-string te)))))
  1401. (setq p1 (plist-put p1 :header ""))
  1402. (setq p1 (plist-put p1 :step nil))
  1403. (setq p1 (plist-put p1 :block nil))
  1404. (while (< ts te)
  1405. (or (bolp) (insert "\n"))
  1406. (setq p1 (plist-put p1 :tstart (format-time-string
  1407. (org-time-stamp-format nil t)
  1408. (seconds-to-time ts))))
  1409. (setq p1 (plist-put p1 :tend (format-time-string
  1410. (org-time-stamp-format nil t)
  1411. (seconds-to-time (setq ts (+ ts step))))))
  1412. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  1413. (plist-get p1 :tstart) "\n")
  1414. (org-dblock-write:clocktable p1)
  1415. (re-search-forward "#\\+END:")
  1416. (end-of-line 0))))
  1417. (defun org-clocktable-add-file (file table)
  1418. (if table
  1419. (let ((lines (org-split-string table "\n"))
  1420. (ff (file-name-nondirectory file)))
  1421. (mapconcat 'identity
  1422. (mapcar (lambda (x)
  1423. (if (string-match org-table-dataline-regexp x)
  1424. (concat "|" ff x)
  1425. x))
  1426. lines)
  1427. "\n"))))
  1428. (defun org-clock-time% (total &rest strings)
  1429. "Compute a time fraction in percent.
  1430. TOTAL s a time string like 10:21 specifying the total times.
  1431. STRINGS is a list of strings that should be checked for a time.
  1432. The first string that does have a time will be used.
  1433. This function is made for clock tables."
  1434. (let ((re "\\([0-9]+\\):\\([0-9]+\\)")
  1435. tot s)
  1436. (save-match-data
  1437. (catch 'exit
  1438. (if (not (string-match re total))
  1439. (throw 'exit 0.)
  1440. (setq tot (+ (string-to-number (match-string 2 total))
  1441. (* 60 (string-to-number (match-string 1 total)))))
  1442. (if (= tot 0.) (throw 'exit 0.)))
  1443. (while (setq s (pop strings))
  1444. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  1445. (throw 'exit
  1446. (/ (* 100.0 (+ (string-to-number (match-string 2 s))
  1447. (* 60 (string-to-number (match-string 1 s)))))
  1448. tot))))
  1449. 0))))
  1450. (defun org-clock-save ()
  1451. "Persist various clock-related data to disk.
  1452. The details of what will be saved are regulated by the variable
  1453. `org-clock-persist'."
  1454. (when org-clock-persist
  1455. (let (b)
  1456. (with-current-buffer (find-file (expand-file-name org-clock-persist-file))
  1457. (progn
  1458. (delete-region (point-min) (point-max))
  1459. ;;Store clock
  1460. (insert (format ";; org-persist.el - %s at %s\n"
  1461. system-name (format-time-string
  1462. (cdr org-time-stamp-formats))))
  1463. (if (and (setq b (marker-buffer org-clock-marker))
  1464. (setq b (or (buffer-base-buffer b) b))
  1465. (buffer-live-p b)
  1466. (buffer-file-name b)
  1467. (or (not org-clock-persist-query-save)
  1468. (y-or-n-p (concat "Save current clock ("
  1469. (substring-no-properties org-clock-heading)
  1470. ") "))))
  1471. (insert "(setq resume-clock '(\""
  1472. (buffer-file-name (marker-buffer org-clock-marker))
  1473. "\" . " (int-to-string (marker-position org-clock-marker))
  1474. "))\n"))
  1475. ;; Store clocked task history. Tasks are stored reversed to make
  1476. ;; reading simpler
  1477. (when (and org-clock-history (eq org-clock-persist t))
  1478. (insert
  1479. "(setq stored-clock-history '("
  1480. (mapconcat
  1481. (lambda (m)
  1482. (when (and (setq b (marker-buffer m))
  1483. (setq b (or (buffer-base-buffer b) b))
  1484. (buffer-live-p b)
  1485. (buffer-file-name b))
  1486. (concat "(\"" (buffer-file-name b)
  1487. "\" . " (int-to-string (marker-position m))
  1488. ")")))
  1489. (reverse org-clock-history) " ") "))\n"))
  1490. (save-buffer)
  1491. (kill-buffer (current-buffer)))))))
  1492. (defvar org-clock-loaded nil
  1493. "Was the clock file loaded?")
  1494. (defun org-clock-load ()
  1495. "Load clock-related data from disk, maybe resuming a stored clock."
  1496. (when (and org-clock-persist (not org-clock-loaded))
  1497. (let ((filename (expand-file-name org-clock-persist-file))
  1498. (org-clock-in-resume 'auto-restart)
  1499. resume-clock stored-clock-history)
  1500. (if (not (file-readable-p filename))
  1501. (message "Not restoring clock data; %s not found"
  1502. org-clock-persist-file)
  1503. (message "%s" "Restoring clock data")
  1504. (setq org-clock-loaded t)
  1505. (load-file filename)
  1506. ;; load history
  1507. (when stored-clock-history
  1508. (save-window-excursion
  1509. (mapc (lambda (task)
  1510. (if (file-exists-p (car task))
  1511. (org-clock-history-push (cdr task)
  1512. (find-file (car task)))))
  1513. stored-clock-history)))
  1514. ;; resume clock
  1515. (when (and resume-clock org-clock-persist
  1516. (file-exists-p (car resume-clock))
  1517. (or (not org-clock-persist-query-resume)
  1518. (y-or-n-p
  1519. (concat
  1520. "Resume clock ("
  1521. (with-current-buffer (find-file (car resume-clock))
  1522. (save-excursion
  1523. (goto-char (cdr resume-clock))
  1524. (org-back-to-heading t)
  1525. (and (looking-at org-complex-heading-regexp)
  1526. (match-string 4))))
  1527. ") "))))
  1528. (when (file-exists-p (car resume-clock))
  1529. (with-current-buffer (find-file (car resume-clock))
  1530. (goto-char (cdr resume-clock))
  1531. (org-clock-in)
  1532. (if (org-invisible-p)
  1533. (org-show-context)))))))))
  1534. ;;;###autoload
  1535. (defun org-clock-persistence-insinuate ()
  1536. "Set up hooks for clock persistence"
  1537. (add-hook 'org-mode-hook 'org-clock-load)
  1538. (add-hook 'kill-emacs-hook 'org-clock-save))
  1539. ;; Suggested bindings
  1540. (org-defkey org-mode-map "\C-c\C-x\C-e" 'org-clock-modify-effort-estimate)
  1541. (provide 'org-clock)
  1542. ;; arch-tag: 7b42c5d4-9b36-48be-97c0-66a869daed4c
  1543. ;;; org-clock.el ends here