org-clock.el 57 KB

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