org-clock.el 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137
  1. ;;; org-clock.el --- The time clocking code for Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
  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.36trans
  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. (declare-function calendar-absolute-from-iso "cal-iso" (&optional date))
  29. (defvar org-time-stamp-formats)
  30. (defgroup org-clock nil
  31. "Options concerning clocking working time in Org-mode."
  32. :tag "Org Clock"
  33. :group 'org-progress)
  34. (defcustom org-clock-into-drawer org-log-into-drawer
  35. "Should clocking info be wrapped into a drawer?
  36. When t, clocking info will always be inserted into a :LOGBOOK: drawer.
  37. If necessary, the drawer will be created.
  38. When nil, the drawer will not be created, but used when present.
  39. When an integer and the number of clocking entries in an item
  40. reaches or exceeds this number, a drawer will be created.
  41. When a string, it names the drawer to be used.
  42. The default for this variable is the value of `org-log-into-drawer',
  43. which see."
  44. :group 'org-todo
  45. :group 'org-clock
  46. :type '(choice
  47. (const :tag "Always" t)
  48. (const :tag "Only when drawer exists" nil)
  49. (integer :tag "When at least N clock entries")
  50. (const :tag "Into LOGBOOK drawer" "LOGBOOK")
  51. (string :tag "Into Drawer named...")))
  52. (defcustom org-clock-out-when-done t
  53. "When non-nil, clock will be stopped when the clocked entry is marked DONE.
  54. DONE here means any DONE-like state.
  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. Instead of t, this can also be a list of TODO states that should trigger
  58. clocking out."
  59. :group 'org-clock
  60. :type '(choice
  61. (const :tag "No" nil)
  62. (const :tag "Yes, when done" t)
  63. (repeat :tag "State list"
  64. (string :tag "TODO keyword"))))
  65. (defcustom org-clock-out-remove-zero-time-clocks nil
  66. "Non-nil means remove the clock line when the resulting time is zero."
  67. :group 'org-clock
  68. :type 'boolean)
  69. (defcustom org-clock-in-switch-to-state nil
  70. "Set task to a special todo state while clocking it.
  71. The value should be the state to which the entry should be
  72. switched. If the value is a function, it must take one
  73. parameter (the current TODO state of the item) and return the
  74. state to switch it to."
  75. :group 'org-clock
  76. :group 'org-todo
  77. :type '(choice
  78. (const :tag "Don't force a state" nil)
  79. (string :tag "State")
  80. (symbol :tag "Function")))
  81. (defcustom org-clock-out-switch-to-state nil
  82. "Set task to a special todo state after clocking out.
  83. The value should be the state to which the entry should be
  84. switched. If the value is a function, it must take one
  85. parameter (the current TODO state of the item) and return the
  86. state to switch it to."
  87. :group 'org-clock
  88. :group 'org-todo
  89. :type '(choice
  90. (const :tag "Don't force a state" nil)
  91. (string :tag "State")
  92. (symbol :tag "Function")))
  93. (defcustom org-clock-history-length 5
  94. "Number of clock tasks to remember in history."
  95. :group 'org-clock
  96. :type 'integer)
  97. (defcustom org-clock-goto-may-find-recent-task t
  98. "Non-nil means `org-clock-goto' can go to recent task if no active clock."
  99. :group 'org-clock
  100. :type 'boolean)
  101. (defcustom org-clock-heading-function nil
  102. "When non-nil, should be a function to create `org-clock-heading'.
  103. This is the string shown in the mode line when a clock is running.
  104. The function is called with point at the beginning of the headline."
  105. :group 'org-clock
  106. :type 'function)
  107. (defcustom org-clock-string-limit 0
  108. "Maximum length of clock strings in the modeline. 0 means no limit."
  109. :group 'org-clock
  110. :type 'integer)
  111. (defcustom org-clock-in-resume nil
  112. "If non-nil, resume clock when clocking into task with open clock.
  113. When clocking into a task with a clock entry which has not been closed,
  114. the clock can be resumed from that point."
  115. :group 'org-clock
  116. :type 'boolean)
  117. (defcustom org-clock-persist nil
  118. "When non-nil, save the running clock when emacs is closed.
  119. The clock is resumed when emacs restarts.
  120. When this is t, both the running clock, and the entire clock
  121. history are saved. When this is the symbol `clock', only the
  122. running clock is saved.
  123. When Emacs restarts with saved clock information, the file containing the
  124. running clock as well as all files mentioned in the clock history will
  125. be visited.
  126. All this depends on running `org-clock-persistence-insinuate' in .emacs"
  127. :group 'org-clock
  128. :type '(choice
  129. (const :tag "Just the running clock" clock)
  130. (const :tag "Just the history" history)
  131. (const :tag "Clock and history" t)
  132. (const :tag "No persistence" nil)))
  133. (defcustom org-clock-persist-file (convert-standard-filename
  134. "~/.emacs.d/org-clock-save.el")
  135. "File to save clock data to."
  136. :group 'org-clock
  137. :type 'string)
  138. (defcustom org-clock-persist-query-save nil
  139. "When non-nil, ask before saving the current clock on exit."
  140. :group 'org-clock
  141. :type 'boolean)
  142. (defcustom org-clock-persist-query-resume t
  143. "When non-nil, ask before resuming any stored clock during load."
  144. :group 'org-clock
  145. :type 'boolean)
  146. (defcustom org-clock-sound nil
  147. "Sound that will used for notifications.
  148. Possible values:
  149. nil no sound played.
  150. t standard Emacs beep
  151. file name play this sound file. If not possible, fall back to beep"
  152. :group 'org-clock
  153. :type '(choice
  154. (const :tag "No sound" nil)
  155. (const :tag "Standard beep" t)
  156. (file :tag "Play sound file")))
  157. (defcustom org-clock-modeline-total 'auto
  158. "Default setting for the time included for the modeline clock.
  159. This can be overruled locally using the CLOCK_MODELINE_TOTAL property.
  160. Allowed values are:
  161. current Only the time in the current instance of the clock
  162. today All time clocked into this task today
  163. repeat All time clocked into this task since last repeat
  164. all All time ever recorded for this task
  165. auto Automatically, either `all', or `repeat' for repeating tasks"
  166. :group 'org-clock
  167. :type '(choice
  168. (const :tag "Current clock" current)
  169. (const :tag "Today's task time" today)
  170. (const :tag "Since last repeat" repeat)
  171. (const :tag "All task time" all)
  172. (const :tag "Automatically, `all' or since `repeat'" auto)))
  173. (defcustom org-task-overrun-text nil
  174. "The extra modeline text that should indicate that the clock is overrun.
  175. The can be nil to indicate that instead of adding text, the clock time
  176. should get a different face (`org-mode-line-clock-overrun').
  177. When this is a string, it is prepended to the clock string as an indication,
  178. also using the face `org-mode-line-clock-overrun'."
  179. :group 'org-clock
  180. :type '(choice
  181. (const :tag "Just mark the time string" nil)
  182. (string :tag "Text to prepend")))
  183. (defcustom org-show-notification-handler nil
  184. "Function or program to send notification with.
  185. The function or program will be called with the notification
  186. string as argument."
  187. :group 'org-clock
  188. :type '(choice
  189. (string :tag "Program")
  190. (function :tag "Function")))
  191. (defcustom org-clock-clocktable-default-properties '(:maxlevel 2 :scope file)
  192. "Default properties for new clocktables."
  193. :group 'org-clock
  194. :type 'plist)
  195. (defcustom org-clock-idle-time nil
  196. "When non-nil, resolve open clocks if the user is idle more than X minutes."
  197. :group 'org-clock
  198. :type '(choice
  199. (const :tag "Never" nil)
  200. (integer :tag "After N minutes")))
  201. (defcustom org-clock-auto-clock-resolution 'when-no-clock-is-running
  202. "When to automatically resolve open clocks found in Org buffers."
  203. :group 'org-clock
  204. :type '(choice
  205. (const :tag "Never" nil)
  206. (const :tag "Always" t)
  207. (const :tag "When no clock is running" when-no-clock-is-running)))
  208. (defcustom org-clock-report-include-clocking-task nil
  209. "When non-nil, include the current clocking task time in clock reports."
  210. :group 'org-clock
  211. :type 'boolean)
  212. (defcustom org-clock-resolve-expert nil
  213. "Non-nil means do not show the splash buffer with the clock resolver."
  214. :group 'org-clock
  215. :type 'boolean)
  216. (defvar org-clock-in-prepare-hook nil
  217. "Hook run when preparing the clock.
  218. This hook is run before anything happens to the task that
  219. you want to clock in. For example, you can use this hook
  220. to add an effort property.")
  221. (defvar org-clock-in-hook nil
  222. "Hook run when starting the clock.")
  223. (defvar org-clock-out-hook nil
  224. "Hook run when stopping the current clock.")
  225. (defvar org-clock-cancel-hook nil
  226. "Hook run when cancelling the current clock.")
  227. (defvar org-clock-goto-hook nil
  228. "Hook run when selecting the currently clocked-in entry.")
  229. (defvar org-clock-has-been-used nil
  230. "Has the clock been used during the current Emacs session?")
  231. ;;; The clock for measuring work time.
  232. (defvar org-mode-line-string "")
  233. (put 'org-mode-line-string 'risky-local-variable t)
  234. (defvar org-clock-mode-line-timer nil)
  235. (defvar org-clock-idle-timer nil)
  236. (defvar org-clock-heading) ; defined in org.el
  237. (defvar org-clock-heading-for-remember "")
  238. (defvar org-clock-start-time "")
  239. (defvar org-clock-leftover-time nil
  240. "If non-nil, user cancelled a clock; this is when leftover time started.")
  241. (defvar org-clock-effort ""
  242. "Effort estimate of the currently clocking task")
  243. (defvar org-clock-total-time nil
  244. "Holds total time, spent previously on currently clocked item.
  245. This does not include the time in the currently running clock.")
  246. (defvar org-clock-history nil
  247. "List of marker pointing to recent clocked tasks.")
  248. (defvar org-clock-default-task (make-marker)
  249. "Marker pointing to the default task that should clock time.
  250. The clock can be made to switch to this task after clocking out
  251. of a different task.")
  252. (defvar org-clock-interrupted-task (make-marker)
  253. "Marker pointing to the task that has been interrupted by the current clock.")
  254. (defvar org-clock-mode-line-map (make-sparse-keymap))
  255. (define-key org-clock-mode-line-map [mode-line mouse-2] 'org-clock-goto)
  256. (define-key org-clock-mode-line-map [mode-line mouse-1] 'org-clock-menu)
  257. (defun org-clock-menu ()
  258. (interactive)
  259. (popup-menu
  260. '("Clock"
  261. ["Clock out" org-clock-out t]
  262. ["Change effort estimate" org-clock-modify-effort-estimate t]
  263. ["Go to clock entry" org-clock-goto t]
  264. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"])))
  265. (defun org-clock-history-push (&optional pos buffer)
  266. "Push a marker to the clock history."
  267. (setq org-clock-history-length (max 1 (min 35 org-clock-history-length)))
  268. (let ((m (move-marker (make-marker)
  269. (or pos (point)) (org-base-buffer
  270. (or buffer (current-buffer)))))
  271. n l)
  272. (while (setq n (member m org-clock-history))
  273. (move-marker (car n) nil))
  274. (setq org-clock-history
  275. (delq nil
  276. (mapcar (lambda (x) (if (marker-buffer x) x nil))
  277. org-clock-history)))
  278. (when (>= (setq l (length org-clock-history)) org-clock-history-length)
  279. (setq org-clock-history
  280. (nreverse
  281. (nthcdr (- l org-clock-history-length -1)
  282. (nreverse org-clock-history)))))
  283. (push m org-clock-history)))
  284. (defun org-clock-save-markers-for-cut-and-paste (beg end)
  285. "Save relative positions of markers in region."
  286. (org-check-and-save-marker org-clock-marker beg end)
  287. (org-check-and-save-marker org-clock-hd-marker beg end)
  288. (org-check-and-save-marker org-clock-default-task beg end)
  289. (org-check-and-save-marker org-clock-interrupted-task beg end)
  290. (mapc (lambda (m) (org-check-and-save-marker m beg end))
  291. org-clock-history))
  292. (defun org-clocking-buffer ()
  293. "Returns clocking buffer if we are currently clocking a task or nil"
  294. (marker-buffer org-clock-marker))
  295. (defun org-clocking-p ()
  296. "Returns t when clocking a task"
  297. (not (equal (org-clocking-buffer) nil)))
  298. (defun org-clock-select-task (&optional prompt)
  299. "Select a task that recently was associated with clocking."
  300. (interactive)
  301. (let (sel-list rpl (i 0) s)
  302. (save-window-excursion
  303. (org-switch-to-buffer-other-window
  304. (get-buffer-create "*Clock Task Select*"))
  305. (erase-buffer)
  306. (when (marker-buffer org-clock-default-task)
  307. (insert (org-add-props "Default Task\n" nil 'face 'bold))
  308. (setq s (org-clock-insert-selection-line ?d org-clock-default-task))
  309. (push s sel-list))
  310. (when (marker-buffer org-clock-interrupted-task)
  311. (insert (org-add-props "The task interrupted by starting the last one\n" nil 'face 'bold))
  312. (setq s (org-clock-insert-selection-line ?i org-clock-interrupted-task))
  313. (push s sel-list))
  314. (when (org-clocking-p)
  315. (insert (org-add-props "Current Clocking Task\n" nil 'face 'bold))
  316. (setq s (org-clock-insert-selection-line ?c org-clock-marker))
  317. (push s sel-list))
  318. (insert (org-add-props "Recent Tasks\n" nil 'face 'bold))
  319. (mapc
  320. (lambda (m)
  321. (when (marker-buffer m)
  322. (setq i (1+ i)
  323. s (org-clock-insert-selection-line
  324. (if (< i 10)
  325. (+ i ?0)
  326. (+ i (- ?A 10))) m))
  327. (if (fboundp 'int-to-char) (setf (car s) (int-to-char (car s))))
  328. (push s sel-list)))
  329. org-clock-history)
  330. (org-fit-window-to-buffer)
  331. (message (or prompt "Select task for clocking:"))
  332. (setq rpl (read-char-exclusive))
  333. (cond
  334. ((eq rpl ?q) nil)
  335. ((eq rpl ?x) nil)
  336. ((assoc rpl sel-list) (cdr (assoc rpl sel-list)))
  337. (t (error "Invalid task choice %c" rpl))))))
  338. (defun org-clock-insert-selection-line (i marker)
  339. "Insert a line for the clock selection menu.
  340. And return a cons cell with the selection character integer and the marker
  341. pointing to it."
  342. (when (marker-buffer marker)
  343. (let (file cat task heading prefix)
  344. (with-current-buffer (org-base-buffer (marker-buffer marker))
  345. (save-excursion
  346. (save-restriction
  347. (widen)
  348. (ignore-errors
  349. (goto-char marker)
  350. (setq file (buffer-file-name (marker-buffer marker))
  351. cat (or (org-get-category)
  352. (progn (org-refresh-category-properties)
  353. (org-get-category)))
  354. heading (org-get-heading 'notags)
  355. prefix (save-excursion
  356. (org-back-to-heading t)
  357. (looking-at "\\*+ ")
  358. (match-string 0))
  359. task (substring
  360. (org-fontify-like-in-org-mode
  361. (concat prefix heading)
  362. org-odd-levels-only)
  363. (length prefix)))))))
  364. (when (and cat task)
  365. (insert (format "[%c] %-15s %s\n" i cat task))
  366. (cons i marker)))))
  367. (defvar org-task-overrun nil
  368. "Internal flag indicating if the clock has overrun the planned time.")
  369. (defvar org-clock-update-period 60
  370. "Number of seconds between mode line clock string updates.")
  371. (defun org-clock-get-clock-string ()
  372. "Form a clock-string, that will be shown in the mode line.
  373. If an effort estimate was defined for the current item, use
  374. 01:30/01:50 format (clocked/estimated).
  375. If not, show simply the clocked time like 01:50."
  376. (let* ((clocked-time (org-clock-get-clocked-time))
  377. (h (floor clocked-time 60))
  378. (m (- clocked-time (* 60 h))))
  379. (if org-clock-effort
  380. (let* ((effort-in-minutes
  381. (org-hh:mm-string-to-minutes org-clock-effort))
  382. (effort-h (floor effort-in-minutes 60))
  383. (effort-m (- effort-in-minutes (* effort-h 60)))
  384. (work-done-str
  385. (org-propertize
  386. (format org-time-clocksum-format h m)
  387. 'face (if (and org-task-overrun (not org-task-overrun-text))
  388. 'org-mode-line-clock-overrun 'org-mode-line-clock)))
  389. (effort-str (format org-time-clocksum-format effort-h effort-m))
  390. (clockstr (org-propertize
  391. (concat "[%s/" effort-str
  392. "] (" (replace-regexp-in-string "%" "%%" org-clock-heading) ")")
  393. 'face 'org-mode-line-clock)))
  394. (format clockstr work-done-str))
  395. (org-propertize (format
  396. (concat "[" org-time-clocksum-format " (%s)]")
  397. h m org-clock-heading)
  398. 'face 'org-mode-line-clock))))
  399. (defun org-clock-update-mode-line ()
  400. (if org-clock-effort
  401. (org-clock-notify-once-if-expired)
  402. (setq org-task-overrun nil))
  403. (setq org-mode-line-string
  404. (org-propertize
  405. (let ((clock-string (org-clock-get-clock-string))
  406. (help-text "Org-mode clock is running.\nmouse-1 shows a menu\nmouse-2 will jump to task"))
  407. (if (and (> org-clock-string-limit 0)
  408. (> (length clock-string) org-clock-string-limit))
  409. (org-propertize
  410. (substring clock-string 0 org-clock-string-limit)
  411. 'help-echo (concat help-text ": " org-clock-heading))
  412. (org-propertize clock-string 'help-echo help-text)))
  413. 'local-map org-clock-mode-line-map
  414. 'mouse-face (if (featurep 'xemacs) 'highlight 'mode-line-highlight)
  415. ))
  416. (if (and org-task-overrun org-task-overrun-text)
  417. (setq org-mode-line-string
  418. (concat (org-propertize
  419. org-task-overrun-text
  420. 'face 'org-mode-line-clock-overrun) org-mode-line-string)))
  421. (force-mode-line-update))
  422. (defun org-clock-get-clocked-time ()
  423. "Get the clocked time for the current item in minutes.
  424. The time returned includes the time spent on this task in
  425. previous clocking intervals."
  426. (let ((currently-clocked-time
  427. (floor (- (org-float-time)
  428. (org-float-time org-clock-start-time)) 60)))
  429. (+ currently-clocked-time (or org-clock-total-time 0))))
  430. (defun org-clock-modify-effort-estimate (&optional value)
  431. "Add to or set the effort estimate of the item currently being clocked.
  432. VALUE can be a number of minutes, or a string with format hh:mm or mm.
  433. When the string starts with a + or a - sign, the current value of the effort
  434. property will be changed by that amount.
  435. This will update the \"Effort\" property of currently clocked item, and
  436. the mode line."
  437. (interactive)
  438. (when (org-clock-is-active)
  439. (let ((current org-clock-effort) sign)
  440. (unless value
  441. ;; Prompt user for a value or a change
  442. (setq value
  443. (read-string
  444. (format "Set effort (hh:mm or mm%s): "
  445. (if current
  446. (format ", prefix + to add to %s" org-clock-effort)
  447. "")))))
  448. (when (stringp value)
  449. ;; A string. See if it is a delta
  450. (setq sign (string-to-char value))
  451. (if (member sign '(?- ?+))
  452. (setq current (org-hh:mm-string-to-minutes current)
  453. value (substring value 1))
  454. (setq current 0))
  455. (setq value (org-hh:mm-string-to-minutes value))
  456. (if (equal ?- sign)
  457. (setq value (- current value))
  458. (if (equal ?+ sign) (setq value (+ current value)))))
  459. (setq value (max 0 value)
  460. org-clock-effort (org-minutes-to-hh:mm-string value))
  461. (org-entry-put org-clock-marker "Effort" org-clock-effort)
  462. (org-clock-update-mode-line)
  463. (message "Effort is now %s" org-clock-effort))))
  464. (defvar org-clock-notification-was-shown nil
  465. "Shows if we have shown notification already.")
  466. (defun org-clock-notify-once-if-expired ()
  467. "Show notification if we spent more time than we estimated before.
  468. Notification is shown only once."
  469. (when (org-clocking-p)
  470. (let ((effort-in-minutes (org-hh:mm-string-to-minutes org-clock-effort))
  471. (clocked-time (org-clock-get-clocked-time)))
  472. (if (setq org-task-overrun
  473. (if (or (null effort-in-minutes) (zerop effort-in-minutes))
  474. nil
  475. (>= clocked-time effort-in-minutes)))
  476. (unless org-clock-notification-was-shown
  477. (setq org-clock-notification-was-shown t)
  478. (org-notify
  479. (format "Task '%s' should be finished by now. (%s)"
  480. org-clock-heading org-clock-effort) t))
  481. (setq org-clock-notification-was-shown nil)))))
  482. (defun org-notify (notification &optional play-sound)
  483. "Send a NOTIFICATION and maybe PLAY-SOUND."
  484. (org-show-notification notification)
  485. (if play-sound (org-clock-play-sound)))
  486. (defun org-show-notification (notification)
  487. "Show notification.
  488. Use `org-show-notification-handler' if defined,
  489. use libnotify if available, or fall back on a message."
  490. (cond ((functionp org-show-notification-handler)
  491. (funcall org-show-notification-handler notification))
  492. ((stringp org-show-notification-handler)
  493. (start-process "emacs-timer-notification" nil
  494. org-show-notification-handler notification))
  495. ((org-program-exists "notify-send")
  496. (start-process "emacs-timer-notification" nil
  497. "notify-send" notification))
  498. ;; Maybe the handler will send a message, so only use message as
  499. ;; a fall back option
  500. (t (message "%s" notification))))
  501. (defun org-clock-play-sound ()
  502. "Play sound as configured by `org-clock-sound'.
  503. Use alsa's aplay tool if available."
  504. (cond
  505. ((not org-clock-sound))
  506. ((eq org-clock-sound t) (beep t) (beep t))
  507. ((stringp org-clock-sound)
  508. (let ((file (expand-file-name org-clock-sound)))
  509. (if (file-exists-p file)
  510. (if (org-program-exists "aplay")
  511. (start-process "org-clock-play-notification" nil
  512. "aplay" file)
  513. (condition-case nil
  514. (play-sound-file file)
  515. (error (beep t) (beep t)))))))))
  516. (defun org-program-exists (program-name)
  517. "Checks whenever we can locate program and launch it."
  518. (if (eq system-type 'gnu/linux)
  519. (= 0 (call-process "which" nil nil nil program-name))))
  520. (defvar org-clock-mode-line-entry nil
  521. "Information for the modeline about the running clock.")
  522. (defun org-find-open-clocks (file)
  523. "Search through the given file and find all open clocks."
  524. (let ((buf (or (get-file-buffer file)
  525. (find-file-noselect file)))
  526. clocks)
  527. (with-current-buffer buf
  528. (save-excursion
  529. (goto-char (point-min))
  530. (while (re-search-forward "CLOCK: \\(\\[.*?\\]\\)$" nil t)
  531. (push (cons (copy-marker (match-end 1) t)
  532. (org-time-string-to-time (match-string 1))) clocks))))
  533. clocks))
  534. (defsubst org-is-active-clock (clock)
  535. "Return t if CLOCK is the currently active clock."
  536. (and (org-clock-is-active)
  537. (= org-clock-marker (car clock))))
  538. (defmacro org-with-clock-position (clock &rest forms)
  539. "Evaluate FORMS with CLOCK as the current active clock."
  540. `(with-current-buffer (marker-buffer (car ,clock))
  541. (save-excursion
  542. (save-restriction
  543. (widen)
  544. (goto-char (car ,clock))
  545. (beginning-of-line)
  546. ,@forms))))
  547. (put 'org-with-clock-position 'lisp-indent-function 1)
  548. (defmacro org-with-clock (clock &rest forms)
  549. "Evaluate FORMS with CLOCK as the current active clock.
  550. This macro also protects the current active clock from being altered."
  551. `(org-with-clock-position ,clock
  552. (let ((org-clock-start-time (cdr ,clock))
  553. (org-clock-total-time)
  554. (org-clock-history)
  555. (org-clock-effort)
  556. (org-clock-marker (car ,clock))
  557. (org-clock-hd-marker (save-excursion
  558. (outline-back-to-heading t)
  559. (point-marker))))
  560. ,@forms)))
  561. (put 'org-with-clock 'lisp-indent-function 1)
  562. (defsubst org-clock-clock-in (clock &optional resume start-time)
  563. "Clock in to the clock located by CLOCK.
  564. If necessary, clock-out of the currently active clock."
  565. (org-with-clock-position clock
  566. (let ((org-clock-in-resume (or resume org-clock-in-resume)))
  567. (org-clock-in nil start-time))))
  568. (defsubst org-clock-clock-out (clock &optional fail-quietly at-time)
  569. "Clock out of the clock located by CLOCK."
  570. (let ((temp (copy-marker (car clock)
  571. (marker-insertion-type (car clock)))))
  572. (if (org-is-active-clock clock)
  573. (org-clock-out fail-quietly at-time)
  574. (org-with-clock clock
  575. (org-clock-out fail-quietly at-time)))
  576. (setcar clock temp)))
  577. (defsubst org-clock-clock-cancel (clock)
  578. "Cancel the clock located by CLOCK."
  579. (let ((temp (copy-marker (car clock)
  580. (marker-insertion-type (car clock)))))
  581. (if (org-is-active-clock clock)
  582. (org-clock-cancel)
  583. (org-with-clock clock
  584. (org-clock-cancel)))
  585. (setcar clock temp)))
  586. (defvar org-clock-clocking-in nil)
  587. (defvar org-clock-resolving-clocks nil)
  588. (defvar org-clock-resolving-clocks-due-to-idleness nil)
  589. (defun org-clock-resolve-clock (clock resolve-to clock-out-time
  590. &optional close-p restart-p fail-quietly)
  591. "Resolve `CLOCK' given the time `RESOLVE-TO', and the present.
  592. `CLOCK' is a cons cell of the form (MARKER START-TIME)."
  593. (let ((org-clock-resolving-clocks t))
  594. (cond
  595. ((null resolve-to)
  596. (org-clock-clock-cancel clock)
  597. (if (and restart-p (not org-clock-clocking-in))
  598. (org-clock-clock-in clock)))
  599. ((eq resolve-to 'now)
  600. (if restart-p
  601. (error "RESTART-P is not valid here"))
  602. (if (or close-p org-clock-clocking-in)
  603. (org-clock-clock-out clock fail-quietly)
  604. (unless (org-is-active-clock clock)
  605. (org-clock-clock-in clock t))))
  606. ((not (time-less-p resolve-to (current-time)))
  607. (error "RESOLVE-TO must refer to a time in the past"))
  608. (t
  609. (if restart-p
  610. (error "RESTART-P is not valid here"))
  611. (org-clock-clock-out clock fail-quietly (or clock-out-time
  612. resolve-to))
  613. (unless org-clock-clocking-in
  614. (if close-p
  615. (setq org-clock-leftover-time (and (null clock-out-time)
  616. resolve-to))
  617. (org-clock-clock-in clock nil (and clock-out-time
  618. resolve-to))))))))
  619. (defun org-clock-jump-to-current-clock (&optional effective-clock)
  620. (interactive)
  621. (let ((clock (or effective-clock (cons org-clock-marker
  622. org-clock-start-time))))
  623. (unless (marker-buffer (car clock))
  624. (error "No clock is currently running"))
  625. (org-with-clock clock (org-clock-goto))
  626. (with-current-buffer (marker-buffer (car clock))
  627. (goto-char (car clock))
  628. (if org-clock-into-drawer
  629. (let ((logbook
  630. (if (stringp org-clock-into-drawer)
  631. (concat ":" org-clock-into-drawer ":")
  632. ":LOGBOOK:")))
  633. (ignore-errors
  634. (outline-flag-region
  635. (save-excursion
  636. (outline-back-to-heading t)
  637. (search-forward logbook)
  638. (goto-char (match-beginning 0)))
  639. (save-excursion
  640. (outline-back-to-heading t)
  641. (search-forward logbook)
  642. (search-forward ":END:")
  643. (goto-char (match-end 0)))
  644. nil)))))))
  645. (defun org-clock-resolve (clock &optional prompt-fn last-valid fail-quietly)
  646. "Resolve an open org-mode clock.
  647. An open clock was found, with `dangling' possibly being non-nil.
  648. If this function was invoked with a prefix argument, non-dangling
  649. open clocks are ignored. The given clock requires some sort of
  650. user intervention to resolve it, either because a clock was left
  651. dangling or due to an idle timeout. The clock resolution can
  652. either be:
  653. (a) deleted, the user doesn't care about the clock
  654. (b) restarted from the current time (if no other clock is open)
  655. (c) closed, giving the clock X minutes
  656. (d) closed and then restarted
  657. (e) resumed, as if the user had never left
  658. The format of clock is (CONS MARKER START-TIME), where MARKER
  659. identifies the buffer and position the clock is open at (and
  660. thus, the heading it's under), and START-TIME is when the clock
  661. was started."
  662. (assert clock)
  663. (let* ((ch
  664. (save-window-excursion
  665. (save-excursion
  666. (unless org-clock-resolving-clocks-due-to-idleness
  667. (org-clock-jump-to-current-clock clock))
  668. (unless org-clock-resolve-expert
  669. (with-output-to-temp-buffer "*Org Clock*"
  670. (princ "Select a Clock Resolution Command:
  671. i/q/C-g Ignore this question; the same as keeping all the idle time.
  672. k/K Keep X minutes of the idle time (default is all). If this
  673. amount is less than the default, you will be clocked out
  674. that many minutes after the time that idling began, and then
  675. clocked back in at the present time.
  676. g/G Indicate that you \"got back\" X minutes ago. This is quite
  677. different from 'k': it clocks you out from the beginning of
  678. the idle period and clock you back in X minutes ago.
  679. s/S Subtract the idle time from the current clock. This is the
  680. same as keeping 0 minutes.
  681. C Cancel the open timer altogether. It will be as though you
  682. never clocked in.
  683. j/J Jump to the current clock, to make manual adjustments.
  684. For all these options, using uppercase makes your final state
  685. to be CLOCKED OUT.")))
  686. (org-fit-window-to-buffer (get-buffer-window "*Org Clock*"))
  687. (let (char-pressed)
  688. (when (featurep 'xemacs)
  689. (message (concat (funcall prompt-fn clock)
  690. " [jkKgGsScCiq]? "))
  691. (setq char-pressed (read-char-exclusive)))
  692. (while (or (null char-pressed)
  693. (and (not (memq char-pressed
  694. '(?k ?K ?g ?G ?s ?S ?C
  695. ?j ?J ?i ?q)))
  696. (or (ding) t)))
  697. (setq char-pressed
  698. (read-char (concat (funcall prompt-fn clock)
  699. " [jkKgGSscCiq]? ")
  700. nil 45)))
  701. (and (not (memq char-pressed '(?i ?q))) char-pressed)))))
  702. (default
  703. (floor (/ (org-float-time
  704. (time-subtract (current-time) last-valid)) 60)))
  705. (keep
  706. (and (memq ch '(?k ?K))
  707. (read-number "Keep how many minutes? " default)))
  708. (gotback
  709. (and (memq ch '(?g ?G))
  710. (read-number "Got back how many minutes ago? " default)))
  711. (subtractp (memq ch '(?s ?S)))
  712. (barely-started-p (< (- (org-float-time last-valid)
  713. (org-float-time (cdr clock))) 45))
  714. (start-over (and subtractp barely-started-p)))
  715. (cond
  716. ((memq ch '(?j ?J))
  717. (if (eq ch ?J)
  718. (org-clock-resolve-clock clock 'now nil t nil fail-quietly))
  719. (org-clock-jump-to-current-clock clock))
  720. ((or (null ch)
  721. (not (memq ch '(?k ?K ?g ?G ?s ?S ?C))))
  722. (message ""))
  723. (t
  724. (org-clock-resolve-clock
  725. clock (cond
  726. ((or (eq ch ?C)
  727. ;; If the time on the clock was less than a minute before
  728. ;; the user went away, and they've ask to subtract all the
  729. ;; time...
  730. start-over)
  731. nil)
  732. ((or subtractp
  733. (and gotback (= gotback 0)))
  734. last-valid)
  735. ((or (and keep (= keep default))
  736. (and gotback (= gotback default)))
  737. 'now)
  738. (keep
  739. (time-add last-valid (seconds-to-time (* 60 keep))))
  740. (gotback
  741. (time-subtract (current-time)
  742. (seconds-to-time (* 60 gotback))))
  743. (t
  744. (error "Unexpected, please report this as a bug")))
  745. (and gotback last-valid)
  746. (memq ch '(?K ?G ?S))
  747. (and start-over
  748. (not (memq ch '(?K ?G ?S ?C))))
  749. fail-quietly)))))
  750. (defun org-resolve-clocks (&optional only-dangling-p prompt-fn last-valid)
  751. "Resolve all currently open org-mode clocks.
  752. If `only-dangling-p' is non-nil, only ask to resolve dangling
  753. \(i.e., not currently open and valid) clocks."
  754. (interactive "P")
  755. (unless org-clock-resolving-clocks
  756. (let ((org-clock-resolving-clocks t))
  757. (dolist (file (org-files-list))
  758. (let ((clocks (org-find-open-clocks file)))
  759. (dolist (clock clocks)
  760. (let ((dangling (or (not (org-clock-is-active))
  761. (/= (car clock) org-clock-marker))))
  762. (if (or (not only-dangling-p) dangling)
  763. (org-clock-resolve
  764. clock
  765. (or prompt-fn
  766. (function
  767. (lambda (clock)
  768. (format
  769. "Dangling clock started %d mins ago"
  770. (floor
  771. (/ (- (org-float-time (current-time))
  772. (org-float-time (cdr clock))) 60))))))
  773. (or last-valid
  774. (cdr clock)))))))))))
  775. (defun org-emacs-idle-seconds ()
  776. "Return the current Emacs idle time in seconds, or nil if not idle."
  777. (let ((idle-time (current-idle-time)))
  778. (if idle-time
  779. (org-float-time idle-time)
  780. 0)))
  781. (defun org-mac-idle-seconds ()
  782. "Return the current Mac idle time in seconds"
  783. (string-to-number (shell-command-to-string "ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle; last}'")))
  784. (defun org-x11-idle-seconds ()
  785. "Return the current X11 idle time in seconds"
  786. (/ (string-to-number (shell-command-to-string "x11idle")) 1000))
  787. (defun org-user-idle-seconds ()
  788. "Return the number of seconds the user has been idle for.
  789. This routine returns a floating point number."
  790. (cond
  791. ((eq system-type 'darwin)
  792. (org-mac-idle-seconds))
  793. ((eq window-system 'x)
  794. (org-x11-idle-seconds))
  795. (t
  796. (org-emacs-idle-seconds))))
  797. (defvar org-clock-user-idle-seconds)
  798. (defun org-resolve-clocks-if-idle ()
  799. "Resolve all currently open org-mode clocks.
  800. This is performed after `org-clock-idle-time' minutes, to check
  801. if the user really wants to stay clocked in after being idle for
  802. so long."
  803. (when (and org-clock-idle-time (not org-clock-resolving-clocks)
  804. org-clock-marker)
  805. (let* ((org-clock-user-idle-seconds (org-user-idle-seconds))
  806. (org-clock-user-idle-start
  807. (time-subtract (current-time)
  808. (seconds-to-time org-clock-user-idle-seconds)))
  809. (org-clock-resolving-clocks-due-to-idleness t))
  810. (if (> org-clock-user-idle-seconds (* 60 org-clock-idle-time))
  811. (org-clock-resolve
  812. (cons org-clock-marker
  813. org-clock-start-time)
  814. (function
  815. (lambda (clock)
  816. (format "Clocked in & idle for %.1f mins"
  817. (/ (org-float-time
  818. (time-subtract (current-time)
  819. org-clock-user-idle-start))
  820. 60.0))))
  821. org-clock-user-idle-start)))))
  822. (defun org-clock-in (&optional select start-time)
  823. "Start the clock on the current item.
  824. If necessary, clock-out of the currently active clock.
  825. With prefix arg SELECT, offer a list of recently clocked tasks to
  826. clock into. When SELECT is `C-u C-u', clock into the current task and mark
  827. is as the default task, a special task that will always be offered in
  828. the clocking selection, associated with the letter `d'."
  829. (interactive "P")
  830. (setq org-clock-notification-was-shown nil)
  831. (catch 'abort
  832. (let ((interrupting (and (not org-clock-resolving-clocks-due-to-idleness)
  833. (org-clocking-p)))
  834. ts selected-task target-pos (msg-extra "")
  835. (leftover (and (not org-clock-resolving-clocks)
  836. org-clock-leftover-time)))
  837. (when (and org-clock-auto-clock-resolution
  838. (or (not interrupting)
  839. (eq t org-clock-auto-clock-resolution))
  840. (not org-clock-clocking-in)
  841. (not org-clock-resolving-clocks))
  842. (setq org-clock-leftover-time nil)
  843. (let ((org-clock-clocking-in t))
  844. (org-resolve-clocks))) ; check if any clocks are dangling
  845. (when (equal select '(4))
  846. (setq selected-task (org-clock-select-task "Clock-in on task: "))
  847. (if selected-task
  848. (setq selected-task (copy-marker selected-task))
  849. (error "Abort")))
  850. (when interrupting
  851. ;; We are interrupting the clocking of a different task.
  852. ;; Save a marker to this task, so that we can go back.
  853. ;; First check if we are trying to clock into the same task!
  854. (if (save-excursion
  855. (unless selected-task
  856. (org-back-to-heading t))
  857. (and (equal (marker-buffer org-clock-hd-marker)
  858. (if selected-task
  859. (marker-buffer selected-task)
  860. (current-buffer)))
  861. (= (marker-position org-clock-hd-marker)
  862. (if selected-task
  863. (marker-position selected-task)
  864. (point)))))
  865. (message "Clock continues in \"%s\"" org-clock-heading)
  866. (progn
  867. (move-marker org-clock-interrupted-task
  868. (marker-position org-clock-marker)
  869. (org-clocking-buffer))
  870. (let ((org-clock-clocking-in t))
  871. (org-clock-out t)))))
  872. (when (equal select '(16))
  873. ;; Mark as default clocking task
  874. (org-clock-mark-default-task))
  875. ;; Clock in at which position?
  876. (setq target-pos
  877. (if (and (eobp) (not (org-on-heading-p)))
  878. (point-at-bol 0)
  879. (point)))
  880. (run-hooks 'org-clock-in-prepare-hook)
  881. (save-excursion
  882. (when (and selected-task (marker-buffer selected-task))
  883. ;; There is a selected task, move to the correct buffer
  884. ;; and set the new target position.
  885. (set-buffer (org-base-buffer (marker-buffer selected-task)))
  886. (setq target-pos (marker-position selected-task))
  887. (move-marker selected-task nil))
  888. (save-excursion
  889. (save-restriction
  890. (widen)
  891. (goto-char target-pos)
  892. (org-back-to-heading t)
  893. (or interrupting (move-marker org-clock-interrupted-task nil))
  894. (org-clock-history-push)
  895. (org-clock-set-current)
  896. (cond ((functionp org-clock-in-switch-to-state)
  897. (looking-at org-complex-heading-regexp)
  898. (let ((newstate (funcall org-clock-in-switch-to-state
  899. (match-string 2))))
  900. (if newstate (org-todo newstate))))
  901. ((and org-clock-in-switch-to-state
  902. (not (looking-at (concat outline-regexp "[ \t]*"
  903. org-clock-in-switch-to-state
  904. "\\>"))))
  905. (org-todo org-clock-in-switch-to-state)))
  906. (setq org-clock-heading-for-remember
  907. (and (looking-at org-complex-heading-regexp)
  908. (match-end 4)
  909. (org-trim (buffer-substring (match-end 1)
  910. (match-end 4)))))
  911. (setq org-clock-heading
  912. (cond ((and org-clock-heading-function
  913. (functionp org-clock-heading-function))
  914. (funcall org-clock-heading-function))
  915. ((looking-at org-complex-heading-regexp)
  916. (replace-regexp-in-string
  917. "\\[\\[.*?\\]\\[\\(.*?\\)\\]\\]" "\\1"
  918. (match-string 4)))
  919. (t "???")))
  920. (setq org-clock-heading (org-propertize org-clock-heading
  921. 'face nil))
  922. (org-clock-find-position org-clock-in-resume)
  923. (cond
  924. ((and org-clock-in-resume
  925. (looking-at
  926. (concat "^[ \t]* " org-clock-string
  927. " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
  928. " +\\sw+\.? +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")))
  929. (message "Matched %s" (match-string 1))
  930. (setq ts (concat "[" (match-string 1) "]"))
  931. (goto-char (match-end 1))
  932. (setq org-clock-start-time
  933. (apply 'encode-time
  934. (org-parse-time-string (match-string 1))))
  935. (setq org-clock-effort (org-get-effort))
  936. (setq org-clock-total-time (org-clock-sum-current-item
  937. (org-clock-get-sum-start))))
  938. ((eq org-clock-in-resume 'auto-restart)
  939. ;; called from org-clock-load during startup,
  940. ;; do not interrupt, but warn!
  941. (message "Cannot restart clock because task does not contain unfinished clock")
  942. (ding)
  943. (sit-for 2)
  944. (throw 'abort nil))
  945. (t
  946. (insert-before-markers "\n")
  947. (backward-char 1)
  948. (org-indent-line-function)
  949. (when (and (save-excursion
  950. (end-of-line 0)
  951. (org-in-item-p)))
  952. (beginning-of-line 1)
  953. (org-indent-line-to (- (org-get-indentation) 2)))
  954. (insert org-clock-string " ")
  955. (setq org-clock-effort (org-get-effort))
  956. (setq org-clock-total-time (org-clock-sum-current-item
  957. (org-clock-get-sum-start)))
  958. (setq org-clock-start-time
  959. (or (and leftover
  960. (y-or-n-p
  961. (format
  962. "You stopped another clock %d mins ago; start this one from then? "
  963. (/ (- (org-float-time (current-time))
  964. (org-float-time leftover)) 60)))
  965. leftover)
  966. start-time
  967. (current-time)))
  968. (setq ts (org-insert-time-stamp org-clock-start-time
  969. 'with-hm 'inactive))))
  970. (move-marker org-clock-marker (point) (buffer-base-buffer))
  971. (move-marker org-clock-hd-marker
  972. (save-excursion (org-back-to-heading t) (point))
  973. (buffer-base-buffer))
  974. (setq org-clock-has-been-used t)
  975. (or global-mode-string (setq global-mode-string '("")))
  976. (or (memq 'org-mode-line-string global-mode-string)
  977. (setq global-mode-string
  978. (append global-mode-string '(org-mode-line-string))))
  979. (org-clock-update-mode-line)
  980. (when org-clock-mode-line-timer
  981. (cancel-timer org-clock-mode-line-timer)
  982. (setq org-clock-mode-line-timer nil))
  983. (setq org-clock-mode-line-timer
  984. (run-with-timer org-clock-update-period
  985. org-clock-update-period
  986. 'org-clock-update-mode-line))
  987. (when org-clock-idle-timer
  988. (cancel-timer org-clock-idle-timer)
  989. (setq org-clock-idle-timer nil))
  990. (setq org-clock-idle-timer
  991. (run-with-timer 60 60 'org-resolve-clocks-if-idle))
  992. (message "Clock starts at %s - %s" ts msg-extra)
  993. (run-hooks 'org-clock-in-hook)))))))
  994. (defvar org-clock-current-task nil
  995. "Task currently clocked in.")
  996. (defun org-clock-set-current ()
  997. "Set `org-clock-current-task' to the task currently clocked in."
  998. (setq org-clock-current-task (nth 4 (org-heading-components))))
  999. (defun org-clock-delete-current ()
  1000. "Reset `org-clock-current-task' to nil."
  1001. (setq org-clock-current-task nil))
  1002. (defun org-clock-mark-default-task ()
  1003. "Mark current task as default task."
  1004. (interactive)
  1005. (save-excursion
  1006. (org-back-to-heading t)
  1007. (move-marker org-clock-default-task (point))))
  1008. (defvar msg-extra)
  1009. (defun org-clock-get-sum-start ()
  1010. "Return the time from which clock times should be counted.
  1011. This is for the currently running clock as it is displayed
  1012. in the mode line. This function looks at the properties
  1013. LAST_REPEAT and in particular CLOCK_MODELINE_TOTAL and the
  1014. corresponding variable `org-clock-modeline-total' and then
  1015. decides which time to use."
  1016. (let ((cmt (or (org-entry-get nil "CLOCK_MODELINE_TOTAL")
  1017. (symbol-name org-clock-modeline-total)))
  1018. (lr (org-entry-get nil "LAST_REPEAT")))
  1019. (cond
  1020. ((equal cmt "current")
  1021. (setq msg-extra "showing time in current clock instance")
  1022. (current-time))
  1023. ((equal cmt "today")
  1024. (setq msg-extra "showing today's task time.")
  1025. (let* ((dt (decode-time (current-time))))
  1026. (setq dt (append (list 0 0 0) (nthcdr 3 dt)))
  1027. (if org-extend-today-until
  1028. (setf (nth 2 dt) org-extend-today-until))
  1029. (apply 'encode-time dt)))
  1030. ((or (equal cmt "all")
  1031. (and (or (not cmt) (equal cmt "auto"))
  1032. (not lr)))
  1033. (setq msg-extra "showing entire task time.")
  1034. nil)
  1035. ((or (equal cmt "repeat")
  1036. (and (or (not cmt) (equal cmt "auto"))
  1037. lr))
  1038. (setq msg-extra "showing task time since last repeat.")
  1039. (if (not lr)
  1040. nil
  1041. (org-time-string-to-time lr)))
  1042. (t nil))))
  1043. (defun org-clock-find-position (find-unclosed)
  1044. "Find the location where the next clock line should be inserted.
  1045. When FIND-UNCLOSED is non-nil, first check if there is an unclosed clock
  1046. line and position cursor in that line."
  1047. (org-back-to-heading t)
  1048. (catch 'exit
  1049. (let ((beg (save-excursion
  1050. (beginning-of-line 2)
  1051. (or (bolp) (newline))
  1052. (point)))
  1053. (end (progn (outline-next-heading) (point)))
  1054. (re (concat "^[ \t]*" org-clock-string))
  1055. (cnt 0)
  1056. (drawer (if (stringp org-clock-into-drawer)
  1057. org-clock-into-drawer "LOGBOOK"))
  1058. first last ind-last)
  1059. (goto-char beg)
  1060. (when (and find-unclosed
  1061. (re-search-forward
  1062. (concat "^[ \t]* " org-clock-string
  1063. " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
  1064. " +\\sw+ +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")
  1065. end t))
  1066. (beginning-of-line 1)
  1067. (throw 'exit t))
  1068. (when (eobp) (newline) (setq end (max (point) end)))
  1069. (when (re-search-forward (concat "^[ \t]*:" drawer ":") end t)
  1070. ;; we seem to have a CLOCK drawer, so go there.
  1071. (beginning-of-line 2)
  1072. (or org-log-states-order-reversed
  1073. (and (re-search-forward org-property-end-re nil t)
  1074. (goto-char (match-beginning 0))))
  1075. (throw 'exit t))
  1076. ;; Lets count the CLOCK lines
  1077. (goto-char beg)
  1078. (while (re-search-forward re end t)
  1079. (setq first (or first (match-beginning 0))
  1080. last (match-beginning 0)
  1081. cnt (1+ cnt)))
  1082. (when (and (integerp org-clock-into-drawer)
  1083. last
  1084. (>= (1+ cnt) org-clock-into-drawer))
  1085. ;; Wrap current entries into a new drawer
  1086. (goto-char last)
  1087. (setq ind-last (org-get-indentation))
  1088. (beginning-of-line 2)
  1089. (if (and (>= (org-get-indentation) ind-last)
  1090. (org-at-item-p))
  1091. (org-end-of-item))
  1092. (insert ":END:\n")
  1093. (beginning-of-line 0)
  1094. (org-indent-line-to ind-last)
  1095. (goto-char first)
  1096. (insert ":" drawer ":\n")
  1097. (beginning-of-line 0)
  1098. (org-indent-line-function)
  1099. (org-flag-drawer t)
  1100. (beginning-of-line 2)
  1101. (or org-log-states-order-reversed
  1102. (and (re-search-forward org-property-end-re nil t)
  1103. (goto-char (match-beginning 0))))
  1104. (throw 'exit nil))
  1105. (goto-char beg)
  1106. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  1107. (not (equal (match-string 1) org-clock-string)))
  1108. ;; Planning info, skip to after it
  1109. (beginning-of-line 2)
  1110. (or (bolp) (newline)))
  1111. (when (or (eq org-clock-into-drawer t)
  1112. (stringp org-clock-into-drawer)
  1113. (and (integerp org-clock-into-drawer)
  1114. (< org-clock-into-drawer 2)))
  1115. (insert ":" drawer ":\n:END:\n")
  1116. (beginning-of-line -1)
  1117. (org-indent-line-function)
  1118. (org-flag-drawer t)
  1119. (beginning-of-line 2)
  1120. (org-indent-line-function)
  1121. (beginning-of-line)
  1122. (or org-log-states-order-reversed
  1123. (and (re-search-forward org-property-end-re nil t)
  1124. (goto-char (match-beginning 0))))))))
  1125. (defun org-clock-out (&optional fail-quietly at-time)
  1126. "Stop the currently running clock.
  1127. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  1128. (interactive)
  1129. (catch 'exit
  1130. (when (not (org-clocking-p))
  1131. (setq global-mode-string
  1132. (delq 'org-mode-line-string global-mode-string))
  1133. (force-mode-line-update)
  1134. (if fail-quietly (throw 'exit t) (error "No active clock")))
  1135. (let (ts te s h m remove)
  1136. (save-excursion ; Do not replace this with `with-current-buffer'.
  1137. (with-no-warnings (set-buffer (org-clocking-buffer)))
  1138. (save-restriction
  1139. (widen)
  1140. (goto-char org-clock-marker)
  1141. (beginning-of-line 1)
  1142. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  1143. (equal (match-string 1) org-clock-string))
  1144. (setq ts (match-string 2))
  1145. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  1146. (goto-char (match-end 0))
  1147. (delete-region (point) (point-at-eol))
  1148. (insert "--")
  1149. (setq te (org-insert-time-stamp (or at-time (current-time))
  1150. 'with-hm 'inactive))
  1151. (setq s (- (org-float-time (apply 'encode-time (org-parse-time-string te)))
  1152. (org-float-time (apply 'encode-time (org-parse-time-string ts))))
  1153. h (floor (/ s 3600))
  1154. s (- s (* 3600 h))
  1155. m (floor (/ s 60))
  1156. s (- s (* 60 s)))
  1157. (insert " => " (format "%2d:%02d" h m))
  1158. (when (setq remove (and org-clock-out-remove-zero-time-clocks
  1159. (= (+ h m) 0)))
  1160. (beginning-of-line 1)
  1161. (delete-region (point) (point-at-eol))
  1162. (and (looking-at "\n") (> (point-max) (1+ (point)))
  1163. (delete-char 1)))
  1164. (move-marker org-clock-marker nil)
  1165. (move-marker org-clock-hd-marker nil)
  1166. (when org-log-note-clock-out
  1167. (org-add-log-setup 'clock-out nil nil nil nil
  1168. (concat "# Task: " (org-get-heading t) "\n\n")))
  1169. (when org-clock-mode-line-timer
  1170. (cancel-timer org-clock-mode-line-timer)
  1171. (setq org-clock-mode-line-timer nil))
  1172. (when org-clock-idle-timer
  1173. (cancel-timer org-clock-idle-timer)
  1174. (setq org-clock-idle-timer nil))
  1175. (setq global-mode-string
  1176. (delq 'org-mode-line-string global-mode-string))
  1177. (when org-clock-out-switch-to-state
  1178. (save-excursion
  1179. (org-back-to-heading t)
  1180. (let ((org-inhibit-logging t)
  1181. (org-clock-out-when-done nil))
  1182. (cond
  1183. ((functionp org-clock-out-switch-to-state)
  1184. (looking-at org-complex-heading-regexp)
  1185. (let ((newstate (funcall org-clock-out-switch-to-state
  1186. (match-string 2))))
  1187. (if newstate (org-todo newstate))))
  1188. ((and org-clock-out-switch-to-state
  1189. (not (looking-at (concat outline-regexp "[ \t]*"
  1190. org-clock-out-switch-to-state
  1191. "\\>"))))
  1192. (org-todo org-clock-out-switch-to-state))))))
  1193. (force-mode-line-update)
  1194. (message (concat "Clock stopped at %s after HH:MM = " org-time-clocksum-format "%s") te h m
  1195. (if remove " => LINE REMOVED" ""))
  1196. (run-hooks 'org-clock-out-hook)
  1197. (org-clock-delete-current))))))
  1198. (defun org-clock-cancel ()
  1199. "Cancel the running clock by removing the start timestamp."
  1200. (interactive)
  1201. (when (not (org-clocking-p))
  1202. (setq global-mode-string
  1203. (delq 'org-mode-line-string global-mode-string))
  1204. (force-mode-line-update)
  1205. (error "No active clock"))
  1206. (save-excursion ; Do not replace this with `with-current-buffer'.
  1207. (with-no-warnings (set-buffer (org-clocking-buffer)))
  1208. (goto-char org-clock-marker)
  1209. (delete-region (1- (point-at-bol)) (point-at-eol))
  1210. ;; Just in case, remove any empty LOGBOOK left over
  1211. (org-remove-empty-drawer-at "LOGBOOK" (point)))
  1212. (move-marker org-clock-marker nil)
  1213. (move-marker org-clock-hd-marker nil)
  1214. (setq global-mode-string
  1215. (delq 'org-mode-line-string global-mode-string))
  1216. (force-mode-line-update)
  1217. (message "Clock canceled")
  1218. (run-hooks 'org-clock-cancel-hook))
  1219. (defun org-clock-goto (&optional select)
  1220. "Go to the currently clocked-in entry, or to the most recently clocked one.
  1221. With prefix arg SELECT, offer recently clocked tasks for selection."
  1222. (interactive "@P")
  1223. (let* ((recent nil)
  1224. (m (cond
  1225. (select
  1226. (or (org-clock-select-task "Select task to go to: ")
  1227. (error "No task selected")))
  1228. ((org-clocking-p) org-clock-marker)
  1229. ((and org-clock-goto-may-find-recent-task
  1230. (car org-clock-history)
  1231. (marker-buffer (car org-clock-history)))
  1232. (setq recent t)
  1233. (car org-clock-history))
  1234. (t (error "No active or recent clock task")))))
  1235. (switch-to-buffer (marker-buffer m))
  1236. (if (or (< m (point-min)) (> m (point-max))) (widen))
  1237. (goto-char m)
  1238. (org-show-entry)
  1239. (org-back-to-heading t)
  1240. (org-cycle-hide-drawers 'children)
  1241. (recenter)
  1242. (org-reveal)
  1243. (if recent
  1244. (message "No running clock, this is the most recently clocked task"))
  1245. (run-hooks 'org-clock-goto-hook)))
  1246. (defvar org-clock-file-total-minutes nil
  1247. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  1248. (make-variable-buffer-local 'org-clock-file-total-minutes)
  1249. (defun org-clock-sum (&optional tstart tend headline-filter)
  1250. "Sum the times for each subtree.
  1251. Puts the resulting times in minutes as a text property on each headline.
  1252. TSTART and TEND can mark a time range to be considered. HEADLINE-FILTER is a
  1253. zero-arg function that, if specified, is called for each headline in the time
  1254. range with point at the headline. Headlines for which HEADLINE-FILTER returns
  1255. nil are excluded from the clock summation."
  1256. (interactive)
  1257. (let* ((bmp (buffer-modified-p))
  1258. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  1259. org-clock-string
  1260. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  1261. (lmax 30)
  1262. (ltimes (make-vector lmax 0))
  1263. (t1 0)
  1264. (level 0)
  1265. ts te dt
  1266. time)
  1267. (if (stringp tstart) (setq tstart (org-time-string-to-seconds tstart)))
  1268. (if (stringp tend) (setq tend (org-time-string-to-seconds tend)))
  1269. (if (consp tstart) (setq tstart (org-float-time tstart)))
  1270. (if (consp tend) (setq tend (org-float-time tend)))
  1271. (remove-text-properties (point-min) (point-max)
  1272. '(:org-clock-minutes t
  1273. :org-clock-force-headline-inclusion t))
  1274. (save-excursion
  1275. (goto-char (point-max))
  1276. (while (re-search-backward re nil t)
  1277. (cond
  1278. ((match-end 2)
  1279. ;; Two time stamps
  1280. (setq ts (match-string 2)
  1281. te (match-string 3)
  1282. ts (org-float-time
  1283. (apply 'encode-time (org-parse-time-string ts)))
  1284. te (org-float-time
  1285. (apply 'encode-time (org-parse-time-string te)))
  1286. ts (if tstart (max ts tstart) ts)
  1287. te (if tend (min te tend) te)
  1288. dt (- te ts)
  1289. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  1290. ((match-end 4)
  1291. ;; A naked time
  1292. (setq t1 (+ t1 (string-to-number (match-string 5))
  1293. (* 60 (string-to-number (match-string 4))))))
  1294. (t ;; A headline
  1295. ;; Add the currently clocking item time to the total
  1296. (when (and org-clock-report-include-clocking-task
  1297. (equal (org-clocking-buffer) (current-buffer))
  1298. (equal (marker-position org-clock-hd-marker) (point))
  1299. tstart
  1300. tend
  1301. (>= (org-float-time org-clock-start-time) tstart)
  1302. (<= (org-float-time org-clock-start-time) tend))
  1303. (let ((time (floor (- (org-float-time)
  1304. (org-float-time org-clock-start-time)) 60)))
  1305. (setq t1 (+ t1 time))))
  1306. (let* ((headline-forced
  1307. (get-text-property (point)
  1308. :org-clock-force-headline-inclusion))
  1309. (headline-included
  1310. (or (null headline-filter)
  1311. (save-excursion
  1312. (save-match-data (funcall headline-filter))))))
  1313. (setq level (- (match-end 1) (match-beginning 1)))
  1314. (when (or (> t1 0) (> (aref ltimes level) 0))
  1315. (when (or headline-included headline-forced)
  1316. (if headline-included
  1317. (loop for l from 0 to level do
  1318. (aset ltimes l (+ (aref ltimes l) t1))))
  1319. (setq time (aref ltimes level))
  1320. (goto-char (match-beginning 0))
  1321. (put-text-property (point) (point-at-eol) :org-clock-minutes time)
  1322. (if headline-filter
  1323. (save-excursion
  1324. (save-match-data
  1325. (while
  1326. (> (funcall outline-level) 1)
  1327. (outline-up-heading 1 t)
  1328. (put-text-property
  1329. (point) (point-at-eol)
  1330. :org-clock-force-headline-inclusion t))))))
  1331. (setq t1 0)
  1332. (loop for l from level to (1- lmax) do
  1333. (aset ltimes l 0)))))))
  1334. (setq org-clock-file-total-minutes (aref ltimes 0)))
  1335. (set-buffer-modified-p bmp)))
  1336. (defun org-clock-sum-current-item (&optional tstart)
  1337. "Returns time, clocked on current item in total"
  1338. (save-excursion
  1339. (save-restriction
  1340. (org-narrow-to-subtree)
  1341. (org-clock-sum tstart)
  1342. org-clock-file-total-minutes)))
  1343. (defun org-clock-display (&optional total-only)
  1344. "Show subtree times in the entire buffer.
  1345. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  1346. in the echo area."
  1347. (interactive)
  1348. (org-clock-remove-overlays)
  1349. (let (time h m p)
  1350. (org-clock-sum)
  1351. (unless total-only
  1352. (save-excursion
  1353. (goto-char (point-min))
  1354. (while (or (and (equal (setq p (point)) (point-min))
  1355. (get-text-property p :org-clock-minutes))
  1356. (setq p (next-single-property-change
  1357. (point) :org-clock-minutes)))
  1358. (goto-char p)
  1359. (when (setq time (get-text-property p :org-clock-minutes))
  1360. (org-clock-put-overlay time (funcall outline-level))))
  1361. (setq h (/ org-clock-file-total-minutes 60)
  1362. m (- org-clock-file-total-minutes (* 60 h)))
  1363. ;; Arrange to remove the overlays upon next change.
  1364. (when org-remove-highlights-with-change
  1365. (org-add-hook 'before-change-functions 'org-clock-remove-overlays
  1366. nil 'local))))
  1367. (if org-time-clocksum-use-fractional
  1368. (message (concat "Total file time: " org-time-clocksum-fractional-format
  1369. " (%d hours and %d minutes)")
  1370. (/ (+ (* h 60.0) m) 60.0) h m)
  1371. (message (concat "Total file time: " org-time-clocksum-format
  1372. " (%d hours and %d minutes)") h m h m))))
  1373. (defvar org-clock-overlays nil)
  1374. (make-variable-buffer-local 'org-clock-overlays)
  1375. (defun org-clock-put-overlay (time &optional level)
  1376. "Put an overlays on the current line, displaying TIME.
  1377. If LEVEL is given, prefix time with a corresponding number of stars.
  1378. This creates a new overlay and stores it in `org-clock-overlays', so that it
  1379. will be easy to remove."
  1380. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  1381. (l (if level (org-get-valid-level level 0) 0))
  1382. (fmt (concat "%s " (if org-time-clocksum-use-fractional
  1383. org-time-clocksum-fractional-format
  1384. org-time-clocksum-format) "%s"))
  1385. (off 0)
  1386. ov tx)
  1387. (org-move-to-column c)
  1388. (unless (eolp) (skip-chars-backward "^ \t"))
  1389. (skip-chars-backward " \t")
  1390. (setq ov (make-overlay (1- (point)) (point-at-eol))
  1391. tx (concat (buffer-substring (1- (point)) (point))
  1392. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  1393. (org-add-props (if org-time-clocksum-use-fractional
  1394. (format fmt
  1395. (make-string l ?*)
  1396. (/ (+ (* h 60.0) m) 60.0)
  1397. (make-string (- 16 l) ?\ ))
  1398. (format fmt
  1399. (make-string l ?*) h m
  1400. (make-string (- 16 l) ?\ )))
  1401. (list 'face 'org-clock-overlay))
  1402. ""))
  1403. (if (not (featurep 'xemacs))
  1404. (overlay-put ov 'display tx)
  1405. (overlay-put ov 'invisible t)
  1406. (overlay-put ov 'end-glyph (make-glyph tx)))
  1407. (push ov org-clock-overlays)))
  1408. (defun org-clock-remove-overlays (&optional beg end noremove)
  1409. "Remove the occur highlights from the buffer.
  1410. BEG and END are ignored. If NOREMOVE is nil, remove this function
  1411. from the `before-change-functions' in the current buffer."
  1412. (interactive)
  1413. (unless org-inhibit-highlight-removal
  1414. (mapc 'delete-overlay org-clock-overlays)
  1415. (setq org-clock-overlays nil)
  1416. (unless noremove
  1417. (remove-hook 'before-change-functions
  1418. 'org-clock-remove-overlays 'local))))
  1419. (defvar state) ;; dynamically scoped into this function
  1420. (defun org-clock-out-if-current ()
  1421. "Clock out if the current entry contains the running clock.
  1422. This is used to stop the clock after a TODO entry is marked DONE,
  1423. and is only done if the variable `org-clock-out-when-done' is not nil."
  1424. (when (and org-clock-out-when-done
  1425. (or (and (eq t org-clock-out-when-done)
  1426. (member state org-done-keywords))
  1427. (and (listp org-clock-out-when-done)
  1428. (member state org-clock-out-when-done)))
  1429. (equal (or (buffer-base-buffer (org-clocking-buffer))
  1430. (org-clocking-buffer))
  1431. (or (buffer-base-buffer (current-buffer))
  1432. (current-buffer)))
  1433. (< (point) org-clock-marker)
  1434. (> (save-excursion (outline-next-heading) (point))
  1435. org-clock-marker))
  1436. ;; Clock out, but don't accept a logging message for this.
  1437. (let ((org-log-note-clock-out nil)
  1438. (org-clock-out-switch-to-state nil))
  1439. (org-clock-out))))
  1440. (add-hook 'org-after-todo-state-change-hook
  1441. 'org-clock-out-if-current)
  1442. ;;;###autoload
  1443. (defun org-get-clocktable (&rest props)
  1444. "Get a formatted clocktable with parameters according to PROPS.
  1445. The table is created in a temporary buffer, fully formatted and
  1446. fontified, and then returned."
  1447. ;; Set the defaults
  1448. (setq props (plist-put props :name "clocktable"))
  1449. (unless (plist-member props :maxlevel)
  1450. (setq props (plist-put props :maxlevel 2)))
  1451. (unless (plist-member props :scope)
  1452. (setq props (plist-put props :scope 'agenda)))
  1453. (with-temp-buffer
  1454. (org-mode)
  1455. (org-create-dblock props)
  1456. (org-update-dblock)
  1457. (font-lock-fontify-buffer)
  1458. (forward-line 2)
  1459. (buffer-substring (point) (progn
  1460. (re-search-forward "^#\\+END" nil t)
  1461. (point-at-bol)))))
  1462. (defun org-clock-report (&optional arg)
  1463. "Create a table containing a report about clocked time.
  1464. If the cursor is inside an existing clocktable block, then the table
  1465. will be updated. If not, a new clocktable will be inserted.
  1466. When called with a prefix argument, move to the first clock table in the
  1467. buffer and update it."
  1468. (interactive "P")
  1469. (org-clock-remove-overlays)
  1470. (when arg
  1471. (org-find-dblock "clocktable")
  1472. (org-show-entry))
  1473. (if (org-in-clocktable-p)
  1474. (goto-char (org-in-clocktable-p))
  1475. (org-create-dblock (append (list :name "clocktable")
  1476. org-clock-clocktable-default-properties)))
  1477. (org-update-dblock))
  1478. (defun org-in-clocktable-p ()
  1479. "Check if the cursor is in a clocktable."
  1480. (let ((pos (point)) start)
  1481. (save-excursion
  1482. (end-of-line 1)
  1483. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  1484. (setq start (match-beginning 0))
  1485. (re-search-forward "^#\\+END:.*" nil t)
  1486. (>= (match-end 0) pos)
  1487. start))))
  1488. (defun org-clock-special-range (key &optional time as-strings)
  1489. "Return two times bordering a special time range.
  1490. Key is a symbol specifying the range and can be one of `today', `yesterday',
  1491. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  1492. A week starts Monday 0:00 and ends Sunday 24:00.
  1493. The range is determined relative to TIME. TIME defaults to the current time.
  1494. The return value is a cons cell with two internal times like the ones
  1495. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  1496. the returned times will be formatted strings."
  1497. (if (integerp key) (setq key (intern (number-to-string key))))
  1498. (let* ((tm (decode-time (or time (current-time))))
  1499. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  1500. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  1501. (dow (nth 6 tm))
  1502. (skey (symbol-name key))
  1503. (shift 0)
  1504. s1 m1 h1 d1 month1 y1 diff ts te fm txt w date)
  1505. (cond
  1506. ((string-match "^[0-9]+$" skey)
  1507. (setq y (string-to-number skey) m 1 d 1 key 'year))
  1508. ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)$" skey)
  1509. (setq y (string-to-number (match-string 1 skey))
  1510. month (string-to-number (match-string 2 skey))
  1511. d 1 key 'month))
  1512. ((string-match "^\\([0-9]+\\)-[wW]\\([0-9]\\{1,2\\}\\)$" skey)
  1513. (require 'cal-iso)
  1514. (setq y (string-to-number (match-string 1 skey))
  1515. w (string-to-number (match-string 2 skey)))
  1516. (setq date (calendar-gregorian-from-absolute
  1517. (calendar-absolute-from-iso (list w 1 y))))
  1518. (setq d (nth 1 date) month (car date) y (nth 2 date)
  1519. dow 1
  1520. key 'week))
  1521. ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)-\\([0-9]\\{1,2\\}\\)$" skey)
  1522. (setq y (string-to-number (match-string 1 skey))
  1523. month (string-to-number (match-string 2 skey))
  1524. d (string-to-number (match-string 3 skey))
  1525. key 'day))
  1526. ((string-match "\\([-+][0-9]+\\)$" skey)
  1527. (setq shift (string-to-number (match-string 1 skey))
  1528. key (intern (substring skey 0 (match-beginning 1))))))
  1529. (when (= shift 0)
  1530. (cond ((eq key 'yesterday) (setq key 'today shift -1))
  1531. ((eq key 'lastweek) (setq key 'week shift -1))
  1532. ((eq key 'lastmonth) (setq key 'month shift -1))
  1533. ((eq key 'lastyear) (setq key 'year shift -1))))
  1534. (cond
  1535. ((memq key '(day today))
  1536. (setq d (+ d shift) h 0 m 0 h1 24 m1 0))
  1537. ((memq key '(week thisweek))
  1538. (setq diff (+ (* -7 shift) (if (= dow 0) 6 (1- dow)))
  1539. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  1540. ((memq key '(month thismonth))
  1541. (setq d 1 h 0 m 0 d1 1 month (+ month shift) month1 (1+ month) h1 0 m1 0))
  1542. ((memq key '(year thisyear))
  1543. (setq m 0 h 0 d 1 month 1 y (+ y shift) y1 (1+ y)))
  1544. (t (error "No such time block %s" key)))
  1545. (setq ts (encode-time s m h d month y)
  1546. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  1547. (or d1 d) (or month1 month) (or y1 y)))
  1548. (setq fm (cdr org-time-stamp-formats))
  1549. (cond
  1550. ((memq key '(day today))
  1551. (setq txt (format-time-string "%A, %B %d, %Y" ts)))
  1552. ((memq key '(week thisweek))
  1553. (setq txt (format-time-string "week %G-W%V" ts)))
  1554. ((memq key '(month thismonth))
  1555. (setq txt (format-time-string "%B %Y" ts)))
  1556. ((memq key '(year thisyear))
  1557. (setq txt (format-time-string "the year %Y" ts))))
  1558. (if as-strings
  1559. (list (format-time-string fm ts) (format-time-string fm te) txt)
  1560. (list ts te txt))))
  1561. (defun org-clocktable-shift (dir n)
  1562. "Try to shift the :block date of the clocktable at point.
  1563. Point must be in the #+BEGIN: line of a clocktable, or this function
  1564. will throw an error.
  1565. DIR is a direction, a symbol `left', `right', `up', or `down'.
  1566. Both `left' and `down' shift the block toward the past, `up' and `right'
  1567. push it toward the future.
  1568. N is the number of shift steps to take. The size of the step depends on
  1569. the currently selected interval size."
  1570. (setq n (prefix-numeric-value n))
  1571. (and (memq dir '(left down)) (setq n (- n)))
  1572. (save-excursion
  1573. (goto-char (point-at-bol))
  1574. (if (not (looking-at "#\\+BEGIN: clocktable\\>.*?:block[ \t]+\\(\\S-+\\)"))
  1575. (error "Line needs a :block definition before this command works")
  1576. (let* ((b (match-beginning 1)) (e (match-end 1))
  1577. (s (match-string 1))
  1578. block shift ins y mw d date wp m)
  1579. (cond
  1580. ((equal s "yesterday") (setq s "today-1"))
  1581. ((equal s "lastweek") (setq s "thisweek-1"))
  1582. ((equal s "lastmonth") (setq s "thismonth-1"))
  1583. ((equal s "lastyear") (setq s "thisyear-1")))
  1584. (cond
  1585. ((string-match "^\\(today\\|thisweek\\|thismonth\\|thisyear\\)\\([-+][0-9]+\\)?$" s)
  1586. (setq block (match-string 1 s)
  1587. shift (if (match-end 2)
  1588. (string-to-number (match-string 2 s))
  1589. 0))
  1590. (setq shift (+ shift n))
  1591. (setq ins (if (= shift 0) block (format "%s%+d" block shift))))
  1592. ((string-match "\\([0-9]+\\)\\(-\\([wW]?\\)\\([0-9]\\{1,2\\}\\)\\(-\\([0-9]\\{1,2\\}\\)\\)?\\)?" s)
  1593. ;; 1 1 2 3 3 4 4 5 6 6 5 2
  1594. (setq y (string-to-number (match-string 1 s))
  1595. wp (and (match-end 3) (match-string 3 s))
  1596. mw (and (match-end 4) (string-to-number (match-string 4 s)))
  1597. d (and (match-end 6) (string-to-number (match-string 6 s))))
  1598. (cond
  1599. (d (setq ins (format-time-string
  1600. "%Y-%m-%d"
  1601. (encode-time 0 0 0 (+ d n) m y))))
  1602. ((and wp mw (> (length wp) 0))
  1603. (require 'cal-iso)
  1604. (setq date (calendar-gregorian-from-absolute (calendar-absolute-from-iso (list (+ mw n) 1 y))))
  1605. (setq ins (format-time-string
  1606. "%G-W%V"
  1607. (encode-time 0 0 0 (nth 1 date) (car date) (nth 2 date)))))
  1608. (mw
  1609. (setq ins (format-time-string
  1610. "%Y-%m"
  1611. (encode-time 0 0 0 1 (+ mw n) y))))
  1612. (y
  1613. (setq ins (number-to-string (+ y n))))))
  1614. (t (error "Cannot shift clocktable block")))
  1615. (when ins
  1616. (goto-char b)
  1617. (insert ins)
  1618. (delete-region (point) (+ (point) (- e b)))
  1619. (beginning-of-line 1)
  1620. (org-update-dblock)
  1621. t)))))
  1622. (defun org-dblock-write:clocktable (params)
  1623. "Write the standard clocktable."
  1624. (catch 'exit
  1625. (let* ((hlchars '((1 . "*") (2 . "/")))
  1626. (ins (make-marker))
  1627. (total-time nil)
  1628. (scope (plist-get params :scope))
  1629. (tostring (plist-get params :tostring))
  1630. (multifile (plist-get params :multifile))
  1631. (header (plist-get params :header))
  1632. (maxlevel (or (plist-get params :maxlevel) 3))
  1633. (step (plist-get params :step))
  1634. (emph (plist-get params :emphasize))
  1635. (timestamp (plist-get params :timestamp))
  1636. (ts (plist-get params :tstart))
  1637. (te (plist-get params :tend))
  1638. (block (plist-get params :block))
  1639. (link (plist-get params :link))
  1640. (tags (plist-get params :tags))
  1641. (matcher (if tags (cdr (org-make-tags-matcher tags))))
  1642. ipos time p level hlc hdl tsp props content recalc formula pcol
  1643. cc beg end pos tbl tbl1 range-text rm-file-column scope-is-list st)
  1644. (setq org-clock-file-total-minutes nil)
  1645. (when step
  1646. (unless (or block (and ts te))
  1647. (error "Clocktable `:step' can only be used with `:block' or `:tstart,:end'"))
  1648. (org-clocktable-steps params)
  1649. (throw 'exit nil))
  1650. (when block
  1651. (setq cc (org-clock-special-range block nil t)
  1652. ts (car cc) te (nth 1 cc) range-text (nth 2 cc)))
  1653. (when (integerp ts) (setq ts (calendar-gregorian-from-absolute ts)))
  1654. (when (integerp te) (setq te (calendar-gregorian-from-absolute te)))
  1655. (when (and ts (listp ts))
  1656. (setq ts (format "%4d-%02d-%02d" (nth 2 ts) (car ts) (nth 1 ts))))
  1657. (when (and te (listp te))
  1658. (setq te (format "%4d-%02d-%02d" (nth 2 te) (car te) (nth 1 te))))
  1659. ;; Now the times are strings we can parse.
  1660. (if ts (setq ts (org-float-time
  1661. (apply 'encode-time (org-parse-time-string ts)))))
  1662. (if te (setq te (org-float-time
  1663. (apply 'encode-time (org-parse-time-string te)))))
  1664. (move-marker ins (point))
  1665. (setq ipos (point))
  1666. ;; Get the right scope
  1667. (setq pos (point))
  1668. (cond
  1669. ((and scope (listp scope) (symbolp (car scope)))
  1670. (setq scope (eval scope)))
  1671. ((eq scope 'agenda)
  1672. (setq scope (org-agenda-files t)))
  1673. ((eq scope 'agenda-with-archives)
  1674. (setq scope (org-agenda-files t))
  1675. (setq scope (org-add-archive-files scope)))
  1676. ((eq scope 'file-with-archives)
  1677. (setq scope (org-add-archive-files (list (buffer-file-name)))
  1678. rm-file-column t)))
  1679. (setq scope-is-list (and scope (listp scope)))
  1680. (save-restriction
  1681. (cond
  1682. ((not scope))
  1683. ((eq scope 'file) (widen))
  1684. ((eq scope 'subtree) (org-narrow-to-subtree))
  1685. ((eq scope 'tree)
  1686. (while (org-up-heading-safe))
  1687. (org-narrow-to-subtree))
  1688. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  1689. (symbol-name scope)))
  1690. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  1691. (catch 'exit
  1692. (while (org-up-heading-safe)
  1693. (looking-at outline-regexp)
  1694. (if (<= (org-reduced-level (funcall outline-level)) level)
  1695. (throw 'exit nil))))
  1696. (org-narrow-to-subtree))
  1697. (scope-is-list
  1698. (let* ((files scope)
  1699. (scope 'agenda)
  1700. (p1 (copy-sequence params))
  1701. file)
  1702. (setq p1 (plist-put p1 :tostring t))
  1703. (setq p1 (plist-put p1 :multifile t))
  1704. (setq p1 (plist-put p1 :scope 'file))
  1705. (org-prepare-agenda-buffers files)
  1706. (while (setq file (pop files))
  1707. (with-current-buffer (find-buffer-visiting file)
  1708. (setq tbl1 (org-dblock-write:clocktable p1))
  1709. (when tbl1
  1710. (push (org-clocktable-add-file
  1711. file
  1712. (concat "| |*File time*|*"
  1713. (org-minutes-to-hh:mm-string
  1714. org-clock-file-total-minutes)
  1715. "*|\n"
  1716. tbl1)) tbl)
  1717. (setq total-time (+ (or total-time 0)
  1718. org-clock-file-total-minutes))))))))
  1719. (goto-char pos)
  1720. (unless scope-is-list
  1721. (org-clock-sum ts te
  1722. (unless (null matcher)
  1723. (lambda ()
  1724. (let ((tags-list
  1725. (org-split-string
  1726. (or (org-entry-get (point) "ALLTAGS") "")
  1727. ":")))
  1728. (eval matcher)))))
  1729. (goto-char (point-min))
  1730. (setq st t)
  1731. (while (or (and (bobp) (prog1 st (setq st nil))
  1732. (get-text-property (point) :org-clock-minutes)
  1733. (setq p (point-min)))
  1734. (setq p (next-single-property-change (point) :org-clock-minutes)))
  1735. (goto-char p)
  1736. (when (setq time (get-text-property p :org-clock-minutes))
  1737. (save-excursion
  1738. (beginning-of-line 1)
  1739. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  1740. (setq level (org-reduced-level
  1741. (- (match-end 1) (match-beginning 1))))
  1742. (<= level maxlevel))
  1743. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  1744. hdl (if (not link)
  1745. (match-string 2)
  1746. (org-make-link-string
  1747. (format "file:%s::%s"
  1748. (buffer-file-name)
  1749. (save-match-data
  1750. (org-make-org-heading-search-string
  1751. (match-string 2))))
  1752. (match-string 2)))
  1753. tsp (when timestamp
  1754. (setq props (org-entry-properties (point)))
  1755. (or (cdr (assoc "SCHEDULED" props))
  1756. (cdr (assoc "TIMESTAMP" props))
  1757. (cdr (assoc "DEADLINE" props))
  1758. (cdr (assoc "TIMESTAMP_IA" props)))))
  1759. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  1760. (push (concat
  1761. "| " (int-to-string level) "|"
  1762. (if timestamp (concat tsp "|") "")
  1763. hlc hdl hlc " |"
  1764. (make-string (1- level) ?|)
  1765. hlc (org-minutes-to-hh:mm-string time) hlc
  1766. " |") tbl))))))
  1767. (setq tbl (nreverse tbl))
  1768. (if tostring
  1769. (if tbl (mapconcat 'identity tbl "\n") nil)
  1770. (goto-char ins)
  1771. (insert-before-markers
  1772. (or header
  1773. (concat
  1774. "Clock summary at ["
  1775. (substring
  1776. (format-time-string (cdr org-time-stamp-formats))
  1777. 1 -1)
  1778. "]"
  1779. (if block (concat ", for " range-text ".") "")
  1780. "\n\n"))
  1781. (if scope-is-list "|File" "")
  1782. "|L|" (if timestamp "Timestamp|" "") "Headline|Time|\n")
  1783. (setq total-time (or total-time org-clock-file-total-minutes))
  1784. (insert-before-markers
  1785. "|-\n|"
  1786. (if scope-is-list "|" "")
  1787. (if timestamp "|Timestamp|" "|")
  1788. "*Total time*| *"
  1789. (org-minutes-to-hh:mm-string (or total-time 0))
  1790. "*|\n|-\n")
  1791. (setq tbl (delq nil tbl))
  1792. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  1793. (equal (substring (car tbl) 0 2) "|-"))
  1794. (pop tbl))
  1795. (insert-before-markers (mapconcat
  1796. 'identity (delq nil tbl)
  1797. (if scope-is-list "\n|-\n" "\n")))
  1798. (backward-delete-char 1)
  1799. (if (setq formula (plist-get params :formula))
  1800. (cond
  1801. ((eq formula '%)
  1802. (setq pcol (+ (if scope-is-list 1 0) maxlevel 3))
  1803. (insert
  1804. (format
  1805. "\n#+TBLFM: $%d='(org-clock-time%% @%d$%d $%d..$%d);%%.1f"
  1806. pcol
  1807. 2
  1808. (+ 3 (if scope-is-list 1 0))
  1809. (+ (if scope-is-list 1 0) 3)
  1810. (1- pcol)))
  1811. (setq recalc t))
  1812. ((stringp formula)
  1813. (insert "\n#+TBLFM: " formula)
  1814. (setq recalc t))
  1815. (t (error "invalid formula in clocktable")))
  1816. ;; Should we rescue an old formula?
  1817. (when (stringp (setq content (plist-get params :content)))
  1818. (when (string-match "^\\([ \t]*#\\+TBLFM:.*\\)" content)
  1819. (setq recalc t)
  1820. (insert "\n" (match-string 1 (plist-get params :content)))
  1821. (beginning-of-line 0))))
  1822. (goto-char ipos)
  1823. (skip-chars-forward "^|")
  1824. (org-table-align)
  1825. (when recalc
  1826. (if (eq formula '%)
  1827. (save-excursion (org-table-goto-column pcol nil 'force)
  1828. (insert "%")))
  1829. (org-table-recalculate 'all))
  1830. (when rm-file-column
  1831. (forward-char 1)
  1832. (org-table-delete-column))
  1833. total-time)))))
  1834. (defun org-clocktable-steps (params)
  1835. (let* ((p1 (copy-sequence params))
  1836. (ts (plist-get p1 :tstart))
  1837. (te (plist-get p1 :tend))
  1838. (step0 (plist-get p1 :step))
  1839. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  1840. (stepskip0 (plist-get p1 :stepskip0))
  1841. (block (plist-get p1 :block))
  1842. cc range-text step-time)
  1843. (when block
  1844. (setq cc (org-clock-special-range block nil t)
  1845. ts (car cc) te (nth 1 cc) range-text (nth 2 cc)))
  1846. (if ts (setq ts (org-float-time
  1847. (apply 'encode-time (org-parse-time-string ts)))))
  1848. (if te (setq te (org-float-time
  1849. (apply 'encode-time (org-parse-time-string te)))))
  1850. (setq p1 (plist-put p1 :header ""))
  1851. (setq p1 (plist-put p1 :step nil))
  1852. (setq p1 (plist-put p1 :block nil))
  1853. (while (< ts te)
  1854. (or (bolp) (insert "\n"))
  1855. (setq p1 (plist-put p1 :tstart (format-time-string
  1856. (org-time-stamp-format nil t)
  1857. (seconds-to-time ts))))
  1858. (setq p1 (plist-put p1 :tend (format-time-string
  1859. (org-time-stamp-format nil t)
  1860. (seconds-to-time (setq ts (+ ts step))))))
  1861. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  1862. (plist-get p1 :tstart) "\n")
  1863. (setq step-time (org-dblock-write:clocktable p1))
  1864. (re-search-forward "#\\+END:")
  1865. (when (and (equal step-time 0) stepskip0)
  1866. ;; Remove the empty table
  1867. (delete-region (point-at-bol)
  1868. (save-excursion
  1869. (re-search-backward "^\\(Daily\\|Weekly\\) report" nil t)
  1870. (point))))
  1871. (end-of-line 0))))
  1872. (defun org-clocktable-add-file (file table)
  1873. (if table
  1874. (let ((lines (org-split-string table "\n"))
  1875. (ff (file-name-nondirectory file)))
  1876. (mapconcat 'identity
  1877. (mapcar (lambda (x)
  1878. (if (string-match org-table-dataline-regexp x)
  1879. (concat "|" ff x)
  1880. x))
  1881. lines)
  1882. "\n"))))
  1883. (defun org-clock-time% (total &rest strings)
  1884. "Compute a time fraction in percent.
  1885. TOTAL s a time string like 10:21 specifying the total times.
  1886. STRINGS is a list of strings that should be checked for a time.
  1887. The first string that does have a time will be used.
  1888. This function is made for clock tables."
  1889. (let ((re "\\([0-9]+\\):\\([0-9]+\\)")
  1890. tot s)
  1891. (save-match-data
  1892. (catch 'exit
  1893. (if (not (string-match re total))
  1894. (throw 'exit 0.)
  1895. (setq tot (+ (string-to-number (match-string 2 total))
  1896. (* 60 (string-to-number (match-string 1 total)))))
  1897. (if (= tot 0.) (throw 'exit 0.)))
  1898. (while (setq s (pop strings))
  1899. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  1900. (throw 'exit
  1901. (/ (* 100.0 (+ (string-to-number (match-string 2 s))
  1902. (* 60 (string-to-number (match-string 1 s)))))
  1903. tot))))
  1904. 0))))
  1905. (defvar org-clock-loaded nil
  1906. "Was the clock file loaded?")
  1907. (defun org-clock-save ()
  1908. "Persist various clock-related data to disk.
  1909. The details of what will be saved are regulated by the variable
  1910. `org-clock-persist'."
  1911. (when (and org-clock-persist
  1912. (or org-clock-loaded
  1913. org-clock-has-been-used
  1914. (not (file-exists-p org-clock-persist-file))))
  1915. (let (b)
  1916. (with-current-buffer (find-file (expand-file-name org-clock-persist-file))
  1917. (progn
  1918. (delete-region (point-min) (point-max))
  1919. ;;Store clock
  1920. (insert (format ";; org-persist.el - %s at %s\n"
  1921. system-name (format-time-string
  1922. (cdr org-time-stamp-formats))))
  1923. (if (and (memq org-clock-persist '(t clock))
  1924. (setq b (org-clocking-buffer))
  1925. (setq b (or (buffer-base-buffer b) b))
  1926. (buffer-live-p b)
  1927. (buffer-file-name b)
  1928. (or (not org-clock-persist-query-save)
  1929. (y-or-n-p (concat "Save current clock ("
  1930. (substring-no-properties org-clock-heading)
  1931. ") "))))
  1932. (insert "(setq resume-clock '(\""
  1933. (buffer-file-name (org-clocking-buffer))
  1934. "\" . " (int-to-string (marker-position org-clock-marker))
  1935. "))\n"))
  1936. ;; Store clocked task history. Tasks are stored reversed to make
  1937. ;; reading simpler
  1938. (when (and (memq org-clock-persist '(t history))
  1939. org-clock-history)
  1940. (insert
  1941. "(setq stored-clock-history '("
  1942. (mapconcat
  1943. (lambda (m)
  1944. (when (and (setq b (marker-buffer m))
  1945. (setq b (or (buffer-base-buffer b) b))
  1946. (buffer-live-p b)
  1947. (buffer-file-name b))
  1948. (concat "(\"" (buffer-file-name b)
  1949. "\" . " (int-to-string (marker-position m))
  1950. ")")))
  1951. (reverse org-clock-history) " ") "))\n"))
  1952. (save-buffer)
  1953. (kill-buffer (current-buffer)))))))
  1954. (defun org-clock-load ()
  1955. "Load clock-related data from disk, maybe resuming a stored clock."
  1956. (when (and org-clock-persist (not org-clock-loaded))
  1957. (let ((filename (expand-file-name org-clock-persist-file))
  1958. (org-clock-in-resume 'auto-restart)
  1959. resume-clock stored-clock-history)
  1960. (if (not (file-readable-p filename))
  1961. (message "Not restoring clock data; %s not found"
  1962. org-clock-persist-file)
  1963. (message "%s" "Restoring clock data")
  1964. (setq org-clock-loaded t)
  1965. (load-file filename)
  1966. ;; load history
  1967. (when stored-clock-history
  1968. (save-window-excursion
  1969. (mapc (lambda (task)
  1970. (if (file-exists-p (car task))
  1971. (org-clock-history-push (cdr task)
  1972. (find-file (car task)))))
  1973. stored-clock-history)))
  1974. ;; resume clock
  1975. (when (and resume-clock org-clock-persist
  1976. (file-exists-p (car resume-clock))
  1977. (or (not org-clock-persist-query-resume)
  1978. (y-or-n-p
  1979. (concat
  1980. "Resume clock ("
  1981. (with-current-buffer (find-file (car resume-clock))
  1982. (save-excursion
  1983. (goto-char (cdr resume-clock))
  1984. (org-back-to-heading t)
  1985. (and (looking-at org-complex-heading-regexp)
  1986. (match-string 4))))
  1987. ") "))))
  1988. (when (file-exists-p (car resume-clock))
  1989. (with-current-buffer (find-file (car resume-clock))
  1990. (goto-char (cdr resume-clock))
  1991. (let ((org-clock-auto-clock-resolution nil))
  1992. (org-clock-in)
  1993. (if (org-invisible-p)
  1994. (org-show-context))))))))))
  1995. ;;;###autoload
  1996. (defun org-clock-persistence-insinuate ()
  1997. "Set up hooks for clock persistence"
  1998. (add-hook 'org-mode-hook 'org-clock-load)
  1999. (add-hook 'kill-emacs-hook 'org-clock-save))
  2000. ;; Suggested bindings
  2001. (org-defkey org-mode-map "\C-c\C-x\C-e" 'org-clock-modify-effort-estimate)
  2002. (provide 'org-clock)
  2003. ;; arch-tag: 7b42c5d4-9b36-48be-97c0-66a869daed4c
  2004. ;;; org-clock.el ends here