org-clock.el 61 KB

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