org-clock.el 77 KB

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