org-clock.el 75 KB

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