Release 6.21a
[org-mode/org-tableheadings.git] / lisp / org-clock.el
blobad18862d51701c6d06b11d595c98df91b336705d
1 ;;; org-clock.el --- The time clocking code for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 6.21a
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;; This file contains the time clocking code for Org-mode
31 (require 'org)
32 (eval-when-compile
33 (require 'cl)
34 (require 'calendar))
36 (declare-function calendar-absolute-from-iso "cal-iso" (&optional date))
37 (defvar org-time-stamp-formats)
39 (defgroup org-clock nil
40 "Options concerning clocking working time in Org-mode."
41 :tag "Org Clock"
42 :group 'org-progress)
44 (defcustom org-clock-into-drawer 2
45 "Should clocking info be wrapped into a drawer?
46 When t, clocking info will always be inserted into a :CLOCK: drawer.
47 If necessary, the drawer will be created.
48 When nil, the drawer will not be created, but used when present.
49 When an integer and the number of clocking entries in an item
50 reaches or exceeds this number, a drawer will be created."
51 :group 'org-todo
52 :group 'org-clock
53 :type '(choice
54 (const :tag "Always" t)
55 (const :tag "Only when drawer exists" nil)
56 (integer :tag "When at least N clock entries")))
58 (defcustom org-clock-out-when-done t
59 "When non-nil, the clock will be stopped when the relevant entry is marked DONE.
60 A nil value means, clock will keep running until stopped explicitly with
61 `C-c C-x C-o', or until the clock is started in a different item."
62 :group 'org-clock
63 :type 'boolean)
65 (defcustom org-clock-out-remove-zero-time-clocks nil
66 "Non-nil means, remove the clock line when the resulting time is zero."
67 :group 'org-clock
68 :type 'boolean)
70 (defcustom org-clock-in-switch-to-state nil
71 "Set task to a special todo state while clocking it.
72 The value should be the state to which the entry should be
73 switched. If the value is a function, it must take one
74 parameter (the current TODO state of the item) and return the
75 state to switch it to."
76 :group 'org-clock
77 :group 'org-todo
78 :type '(choice
79 (const :tag "Don't force a state" nil)
80 (string :tag "State")
81 (symbol :tag "Function")))
83 (defcustom org-clock-history-length 5
84 "Number of clock tasks to remember in history."
85 :group 'org-clock
86 :type 'integer)
88 (defcustom org-clock-heading-function nil
89 "When non-nil, should be a function to create `org-clock-heading'.
90 This is the string shown in the mode line when a clock is running.
91 The function is called with point at the beginning of the headline."
92 :group 'org-clock
93 :type 'function)
95 (defcustom org-clock-string-limit 0
96 "Maximum length of clock strings in the modeline. 0 means no limit"
97 :group 'org-clock
98 :type 'integer)
100 (defcustom org-clock-in-resume nil
101 "If non-nil, when clocking into a task with a clock entry which
102 has not been closed, resume the clock from that point"
103 :group 'org-clock
104 :type 'boolean)
106 (defcustom org-clock-persist nil
107 "When non-nil, save the running clock when emacs is closed, and
108 resume it next time emacs is started.
109 When this is t, both the running clock, and the entire clock
110 history are saved. When this is the symbol `clock', only the
111 running clock is saved.
113 When Emacs restarts with saved clock information, the file containing the
114 running clock as well as all files mentioned in the clock history will
115 be visited."
116 :group 'org-clock
117 :type '(choice
118 (const :tag "Just the running clock" clock)
119 (const :tag "Clock and history" t)
120 (const :tag "No persistence" nil)))
122 (defcustom org-clock-persist-file (convert-standard-filename
123 "~/.emacs.d/org-clock-save.el")
124 "File to save clock data to"
125 :group 'org-clock
126 :type 'string)
128 (defcustom org-clock-persist-query-save nil
129 "When non-nil, ask before saving the current clock on exit"
130 :group 'org-clock
131 :type 'boolean)
133 (defcustom org-clock-persist-query-resume t
134 "When non-nil, ask before resuming any stored clock during
135 load."
136 :group 'org-clock
137 :type 'boolean)
139 ;;; The clock for measuring work time.
141 (defvar org-mode-line-string "")
142 (put 'org-mode-line-string 'risky-local-variable t)
144 (defvar org-clock-mode-line-timer nil)
145 (defvar org-clock-heading "")
146 (defvar org-clock-heading-for-remember "")
147 (defvar org-clock-start-time "")
149 (defvar org-clock-history nil
150 "List of marker pointing to recent clocked tasks.")
152 (defvar org-clock-default-task (make-marker)
153 "Marker pointing to the default task that should clock time.
154 The clock can be made to switch to this task after clocking out
155 of a different task.")
157 (defvar org-clock-interrupted-task (make-marker)
158 "Marker pointing to the task that has been interrupted by the current clock.")
160 (defvar org-clock-mode-line-map (make-sparse-keymap))
161 (define-key org-clock-mode-line-map [mode-line mouse-2] 'org-clock-goto)
163 (defun org-clock-history-push (&optional pos buffer)
164 "Push a marker to the clock history."
165 (setq org-clock-history-length (max 1 (min 35 org-clock-history-length)))
166 (let ((m (move-marker (make-marker) (or pos (point)) buffer)) n l)
167 (while (setq n (member m org-clock-history))
168 (move-marker (car n) nil))
169 (setq org-clock-history
170 (delq nil
171 (mapcar (lambda (x) (if (marker-buffer x) x nil))
172 org-clock-history)))
173 (when (>= (setq l (length org-clock-history)) org-clock-history-length)
174 (setq org-clock-history
175 (nreverse
176 (nthcdr (- l org-clock-history-length -1)
177 (nreverse org-clock-history)))))
178 (push m org-clock-history)))
180 (defun org-clock-save-markers-for-cut-and-paste (beg end)
181 "Save relative positions of markers in region."
182 (org-check-and-save-marker org-clock-marker beg end)
183 (org-check-and-save-marker org-clock-default-task beg end)
184 (org-check-and-save-marker org-clock-interrupted-task beg end)
185 (mapc (lambda (m) (org-check-and-save-marker m beg end))
186 org-clock-history))
188 (defun org-clock-select-task (&optional prompt)
189 "Select a task that recently was associated with clocking."
190 (interactive)
191 (let (sel-list rpl (i 0) s)
192 (save-window-excursion
193 (org-switch-to-buffer-other-window
194 (get-buffer-create "*Clock Task Select*"))
195 (erase-buffer)
196 (when (marker-buffer org-clock-default-task)
197 (insert (org-add-props "Default Task\n" nil 'face 'bold))
198 (setq s (org-clock-insert-selection-line ?d org-clock-default-task))
199 (push s sel-list))
200 (when (marker-buffer org-clock-interrupted-task)
201 (insert (org-add-props "The task interrupted by starting the last one\n" nil 'face 'bold))
202 (setq s (org-clock-insert-selection-line ?i org-clock-interrupted-task))
203 (push s sel-list))
204 (when (marker-buffer org-clock-marker)
205 (insert (org-add-props "Current Clocking Task\n" nil 'face 'bold))
206 (setq s (org-clock-insert-selection-line ?c org-clock-marker))
207 (push s sel-list))
208 (insert (org-add-props "Recent Tasks\n" nil 'face 'bold))
209 (mapc
210 (lambda (m)
211 (when (marker-buffer m)
212 (setq i (1+ i)
213 s (org-clock-insert-selection-line
214 (if (< i 10)
215 (+ i ?0)
216 (+ i (- ?A 10))) m))
217 (push s sel-list)))
218 org-clock-history)
219 (org-fit-window-to-buffer)
220 (message (or prompt "Select task for clocking:"))
221 (setq rpl (read-char-exclusive))
222 (cond
223 ((eq rpl ?q) nil)
224 ((eq rpl ?x) nil)
225 ((assoc rpl sel-list) (cdr (assoc rpl sel-list)))
226 (t (error "Invalid task choice %c" rpl))))))
228 (defun org-clock-insert-selection-line (i marker)
229 (when (marker-buffer marker)
230 (let (file cat task)
231 (with-current-buffer (org-base-buffer (marker-buffer marker))
232 (save-excursion
233 (save-restriction
234 (widen)
235 (goto-char marker)
236 (setq file (buffer-file-name (marker-buffer marker))
237 cat (or (org-get-category)
238 (progn (org-refresh-category-properties)
239 (org-get-category)))
240 task (org-get-heading 'notags)))))
241 (when (and cat task)
242 (insert (format "[%c] %-15s %s\n" i cat task))
243 (cons i marker)))))
245 (defun org-clock-update-mode-line ()
246 (let* ((delta (- (time-to-seconds (current-time))
247 (time-to-seconds org-clock-start-time)))
248 (h (floor delta 3600))
249 (m (floor (- delta (* 3600 h)) 60)))
250 (setq org-mode-line-string
251 (org-propertize
252 (let ((clock-string (format (concat "-[" org-time-clocksum-format " (%s)]")
253 h m org-clock-heading))
254 (help-text "Org-mode clock is running. Mouse-2 to go there."))
255 (if (and (> org-clock-string-limit 0)
256 (> (length clock-string) org-clock-string-limit))
257 (org-propertize (substring clock-string 0 org-clock-string-limit)
258 'help-echo (concat help-text ": " org-clock-heading))
259 (org-propertize clock-string 'help-echo help-text)))
260 'local-map org-clock-mode-line-map
261 'mouse-face (if (featurep 'xemacs) 'highlight 'mode-line-highlight)))
262 (force-mode-line-update)))
264 (defvar org-clock-mode-line-entry nil
265 "Information for the modeline about the running clock.")
267 (defun org-clock-in (&optional select)
268 "Start the clock on the current item.
269 If necessary, clock-out of the currently active clock.
270 With prefix arg SELECT, offer a list of recently clocked tasks to
271 clock into. When SELECT is `C-u C-u', clock into the current task and mark
272 is as the default task, a special task that will always be offered in
273 the clocking selection, associated with the letter `d'."
274 (interactive "P")
275 (catch 'abort
276 (let ((interrupting (marker-buffer org-clock-marker))
277 ts selected-task target-pos)
278 (when (equal select '(4))
279 (setq selected-task (org-clock-select-task "Clock-in on task: "))
280 (if selected-task
281 (setq selected-task (copy-marker selected-task))
282 (error "Abort")))
283 (when interrupting
284 ;; We are interrupting the clocking of a different task.
285 ;; Save a marker to this task, so that we can go back.
286 (move-marker org-clock-interrupted-task
287 (marker-position org-clock-marker)
288 (marker-buffer org-clock-marker))
289 (org-clock-out t))
291 (when (equal select '(16))
292 ;; Mark as default clocking task
293 (save-excursion
294 (org-back-to-heading t)
295 (move-marker org-clock-default-task (point))))
297 (setq target-pos (point)) ;; we want to clock in at this location
298 (save-excursion
299 (when (and selected-task (marker-buffer selected-task))
300 ;; There is a selected task, move to the correct buffer
301 ;; and set the new target position.
302 (set-buffer (org-base-buffer (marker-buffer selected-task)))
303 (setq target-pos (marker-position selected-task))
304 (move-marker selected-task nil))
305 (save-excursion
306 (save-restriction
307 (widen)
308 (goto-char target-pos)
309 (org-back-to-heading t)
310 (or interrupting (move-marker org-clock-interrupted-task nil))
311 (org-clock-history-push)
312 (cond ((functionp org-clock-in-switch-to-state)
313 (looking-at org-complex-heading-regexp)
314 (let ((newstate (funcall org-clock-in-switch-to-state
315 (match-string 2))))
316 (if newstate (org-todo newstate))))
317 ((and org-clock-in-switch-to-state
318 (not (looking-at (concat outline-regexp "[ \t]*"
319 org-clock-in-switch-to-state
320 "\\>"))))
321 (org-todo org-clock-in-switch-to-state)))
322 (setq org-clock-heading-for-remember
323 (and (looking-at org-complex-heading-regexp)
324 (match-end 4)
325 (org-trim (buffer-substring (match-end 1)
326 (match-end 4)))))
327 (setq org-clock-heading
328 (cond ((and org-clock-heading-function
329 (functionp org-clock-heading-function))
330 (funcall org-clock-heading-function))
331 ((looking-at org-complex-heading-regexp)
332 (match-string 4))
333 (t "???")))
334 (setq org-clock-heading (org-propertize org-clock-heading
335 'face nil))
336 (org-clock-find-position)
337 (cond
338 ((and org-clock-in-resume
339 (looking-at
340 (concat "^[ \\t]* " org-clock-string
341 " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
342 " +\\sw+ +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")))
343 (message "Matched %s" (match-string 1))
344 (setq ts (concat "[" (match-string 1) "]"))
345 (goto-char (match-end 1))
346 (setq org-clock-start-time
347 (apply 'encode-time
348 (org-parse-time-string (match-string 1)))))
349 ((eq org-clock-in-resume 'auto-restart)
350 ;; called from org-clock-load during startup,
351 ;; do not interrupt, but warn!
352 (message "Cannot restart clock because task does not contain unfinished clock")
353 (ding)
354 (sit-for 2)
355 (throw 'abort nil))
357 (insert "\n") (backward-char 1)
358 (org-indent-line-function)
359 (insert org-clock-string " ")
360 (setq org-clock-start-time (current-time))
361 (setq ts (org-insert-time-stamp org-clock-start-time 'with-hm 'inactive))))
362 (move-marker org-clock-marker (point) (buffer-base-buffer))
363 (or global-mode-string (setq global-mode-string '("")))
364 (or (memq 'org-mode-line-string global-mode-string)
365 (setq global-mode-string
366 (append global-mode-string '(org-mode-line-string))))
367 (org-clock-update-mode-line)
368 (setq org-clock-mode-line-timer
369 (run-with-timer 60 60 'org-clock-update-mode-line))
370 (message "Clock started at %s" ts)))))))
372 (defun org-clock-find-position ()
373 "Find the location where the next clock line should be inserted."
374 (org-back-to-heading t)
375 (catch 'exit
376 (let ((beg (save-excursion
377 (beginning-of-line 2)
378 (or (bolp) (newline))
379 (point)))
380 (end (progn (outline-next-heading) (point)))
381 (re (concat "^[ \t]*" org-clock-string))
382 (cnt 0)
383 first last)
384 (goto-char beg)
385 (when (eobp) (newline) (setq end (max (point) end)))
386 (when (re-search-forward "^[ \t]*:CLOCK:" end t)
387 ;; we seem to have a CLOCK drawer, so go there.
388 (beginning-of-line 2)
389 (throw 'exit t))
390 ;; Lets count the CLOCK lines
391 (goto-char beg)
392 (while (re-search-forward re end t)
393 (setq first (or first (match-beginning 0))
394 last (match-beginning 0)
395 cnt (1+ cnt)))
396 (when (and (integerp org-clock-into-drawer)
397 (>= (1+ cnt) org-clock-into-drawer))
398 ;; Wrap current entries into a new drawer
399 (goto-char last)
400 (beginning-of-line 2)
401 (if (org-at-item-p) (org-end-of-item))
402 (insert ":END:\n")
403 (beginning-of-line 0)
404 (org-indent-line-function)
405 (goto-char first)
406 (insert ":CLOCK:\n")
407 (beginning-of-line 0)
408 (org-indent-line-function)
409 (org-flag-drawer t)
410 (beginning-of-line 2)
411 (throw 'exit nil))
413 (goto-char beg)
414 (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
415 (not (equal (match-string 1) org-clock-string)))
416 ;; Planning info, skip to after it
417 (beginning-of-line 2)
418 (or (bolp) (newline)))
419 (when (eq t org-clock-into-drawer)
420 (insert ":CLOCK:\n:END:\n")
421 (beginning-of-line 0)
422 (org-indent-line-function)
423 (beginning-of-line 0)
424 (org-flag-drawer t)
425 (org-indent-line-function)
426 (beginning-of-line 2)))))
428 (defun org-clock-out (&optional fail-quietly)
429 "Stop the currently running clock.
430 If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
431 (interactive)
432 (catch 'exit
433 (if (not (marker-buffer org-clock-marker))
434 (if fail-quietly (throw 'exit t) (error "No active clock")))
435 (let (ts te s h m remove)
436 (save-excursion
437 (set-buffer (marker-buffer org-clock-marker))
438 (save-restriction
439 (widen)
440 (goto-char org-clock-marker)
441 (beginning-of-line 1)
442 (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
443 (equal (match-string 1) org-clock-string))
444 (setq ts (match-string 2))
445 (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
446 (goto-char (match-end 0))
447 (delete-region (point) (point-at-eol))
448 (insert "--")
449 (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
450 (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
451 (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
452 h (floor (/ s 3600))
453 s (- s (* 3600 h))
454 m (floor (/ s 60))
455 s (- s (* 60 s)))
456 (insert " => " (format "%2d:%02d" h m))
457 (when (setq remove (and org-clock-out-remove-zero-time-clocks
458 (= (+ h m) 0)))
459 (beginning-of-line 1)
460 (delete-region (point) (point-at-eol))
461 (and (looking-at "\n") (> (point-max) (1+ (point)))
462 (delete-char 1)))
463 (move-marker org-clock-marker nil)
464 (when org-log-note-clock-out
465 (org-add-log-setup 'clock-out nil nil nil
466 (concat "# Task: " (org-get-heading t) "\n\n")))
467 (when org-clock-mode-line-timer
468 (cancel-timer org-clock-mode-line-timer)
469 (setq org-clock-mode-line-timer nil))
470 (setq global-mode-string
471 (delq 'org-mode-line-string global-mode-string))
472 (force-mode-line-update)
473 (message (concat "Clock stopped at %s after HH:MM = " org-time-clocksum-format "%s") te h m
474 (if remove " => LINE REMOVED" "")))))))
476 (defun org-clock-cancel ()
477 "Cancel the running clock be removing the start timestamp."
478 (interactive)
479 (if (not (marker-buffer org-clock-marker))
480 (error "No active clock"))
481 (save-excursion
482 (set-buffer (marker-buffer org-clock-marker))
483 (goto-char org-clock-marker)
484 (delete-region (1- (point-at-bol)) (point-at-eol)))
485 (setq global-mode-string
486 (delq 'org-mode-line-string global-mode-string))
487 (force-mode-line-update)
488 (message "Clock canceled"))
490 (defun org-clock-goto (&optional select)
491 "Go to the currently clocked-in entry.
492 With prefix arg SELECT, offer recently clocked tasks."
493 (interactive "P")
494 (let ((m (if select
495 (org-clock-select-task "Select task to go to: ")
496 org-clock-marker)))
497 (if (not (marker-buffer m))
498 (if select
499 (error "No task selected")
500 (error "No active clock")))
501 (switch-to-buffer (marker-buffer m))
502 (if (or (< m (point-min)) (> m (point-max))) (widen))
503 (goto-char m)
504 (org-show-entry)
505 (org-back-to-heading)
506 (org-cycle-hide-drawers 'children)
507 (recenter)))
509 (defvar org-clock-file-total-minutes nil
510 "Holds the file total time in minutes, after a call to `org-clock-sum'.")
511 (make-variable-buffer-local 'org-clock-file-total-minutes)
513 (defun org-clock-sum (&optional tstart tend)
514 "Sum the times for each subtree.
515 Puts the resulting times in minutes as a text property on each headline."
516 (interactive)
517 (let* ((bmp (buffer-modified-p))
518 (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
519 org-clock-string
520 "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
521 (lmax 30)
522 (ltimes (make-vector lmax 0))
523 (t1 0)
524 (level 0)
525 ts te dt
526 time)
527 (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
528 (save-excursion
529 (goto-char (point-max))
530 (while (re-search-backward re nil t)
531 (cond
532 ((match-end 2)
533 ;; Two time stamps
534 (setq ts (match-string 2)
535 te (match-string 3)
536 ts (time-to-seconds
537 (apply 'encode-time (org-parse-time-string ts)))
538 te (time-to-seconds
539 (apply 'encode-time (org-parse-time-string te)))
540 ts (if tstart (max ts tstart) ts)
541 te (if tend (min te tend) te)
542 dt (- te ts)
543 t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
544 ((match-end 4)
545 ;; A naked time
546 (setq t1 (+ t1 (string-to-number (match-string 5))
547 (* 60 (string-to-number (match-string 4))))))
548 (t ;; A headline
549 (setq level (- (match-end 1) (match-beginning 1)))
550 (when (or (> t1 0) (> (aref ltimes level) 0))
551 (loop for l from 0 to level do
552 (aset ltimes l (+ (aref ltimes l) t1)))
553 (setq t1 0 time (aref ltimes level))
554 (loop for l from level to (1- lmax) do
555 (aset ltimes l 0))
556 (goto-char (match-beginning 0))
557 (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
558 (setq org-clock-file-total-minutes (aref ltimes 0)))
559 (set-buffer-modified-p bmp)))
561 (defun org-clock-display (&optional total-only)
562 "Show subtree times in the entire buffer.
563 If TOTAL-ONLY is non-nil, only show the total time for the entire file
564 in the echo area."
565 (interactive)
566 (org-clock-remove-overlays)
567 (let (time h m p)
568 (org-clock-sum)
569 (unless total-only
570 (save-excursion
571 (goto-char (point-min))
572 (while (or (and (equal (setq p (point)) (point-min))
573 (get-text-property p :org-clock-minutes))
574 (setq p (next-single-property-change
575 (point) :org-clock-minutes)))
576 (goto-char p)
577 (when (setq time (get-text-property p :org-clock-minutes))
578 (org-clock-put-overlay time (funcall outline-level))))
579 (setq h (/ org-clock-file-total-minutes 60)
580 m (- org-clock-file-total-minutes (* 60 h)))
581 ;; Arrange to remove the overlays upon next change.
582 (when org-remove-highlights-with-change
583 (org-add-hook 'before-change-functions 'org-clock-remove-overlays
584 nil 'local))))
585 (message (concat "Total file time: " org-time-clocksum-format " (%d hours and %d minutes)") h m h m)))
587 (defvar org-clock-overlays nil)
588 (make-variable-buffer-local 'org-clock-overlays)
590 (defun org-clock-put-overlay (time &optional level)
591 "Put an overlays on the current line, displaying TIME.
592 If LEVEL is given, prefix time with a corresponding number of stars.
593 This creates a new overlay and stores it in `org-clock-overlays', so that it
594 will be easy to remove."
595 (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
596 (l (if level (org-get-valid-level level 0) 0))
597 (fmt (concat "%s " org-time-clocksum-format "%s"))
598 (off 0)
599 ov tx)
600 (org-move-to-column c)
601 (unless (eolp) (skip-chars-backward "^ \t"))
602 (skip-chars-backward " \t")
603 (setq ov (org-make-overlay (1- (point)) (point-at-eol))
604 tx (concat (buffer-substring (1- (point)) (point))
605 (make-string (+ off (max 0 (- c (current-column)))) ?.)
606 (org-add-props (format fmt
607 (make-string l ?*) h m
608 (make-string (- 16 l) ?\ ))
609 (list 'face 'org-clock-overlay))
610 ""))
611 (if (not (featurep 'xemacs))
612 (org-overlay-put ov 'display tx)
613 (org-overlay-put ov 'invisible t)
614 (org-overlay-put ov 'end-glyph (make-glyph tx)))
615 (push ov org-clock-overlays)))
617 (defun org-clock-remove-overlays (&optional beg end noremove)
618 "Remove the occur highlights from the buffer.
619 BEG and END are ignored. If NOREMOVE is nil, remove this function
620 from the `before-change-functions' in the current buffer."
621 (interactive)
622 (unless org-inhibit-highlight-removal
623 (mapc 'org-delete-overlay org-clock-overlays)
624 (setq org-clock-overlays nil)
625 (unless noremove
626 (remove-hook 'before-change-functions
627 'org-clock-remove-overlays 'local))))
629 (defvar state) ;; dynamically scoped into this function
630 (defun org-clock-out-if-current ()
631 "Clock out if the current entry contains the running clock.
632 This is used to stop the clock after a TODO entry is marked DONE,
633 and is only done if the variable `org-clock-out-when-done' is not nil."
634 (when (and org-clock-out-when-done
635 (member state org-done-keywords)
636 (equal (marker-buffer org-clock-marker) (current-buffer))
637 (< (point) org-clock-marker)
638 (> (save-excursion (outline-next-heading) (point))
639 org-clock-marker))
640 ;; Clock out, but don't accept a logging message for this.
641 (let ((org-log-note-clock-out nil))
642 (org-clock-out))))
644 (add-hook 'org-after-todo-state-change-hook
645 'org-clock-out-if-current)
647 ;;;###autoload
648 (defun org-get-clocktable (&rest props)
649 "Get a formatted clocktable with parameters according to PROPS.
650 The table is created in a temporary buffer, fully formatted and
651 fontified, and then returned."
652 ;; Set the defaults
653 (setq props (plist-put props :name "clocktable"))
654 (unless (plist-member props :maxlevel)
655 (setq props (plist-put props :maxlevel 2)))
656 (unless (plist-member props :scope)
657 (setq props (plist-put props :scope 'agenda)))
658 (with-temp-buffer
659 (org-mode)
660 (org-create-dblock props)
661 (org-update-dblock)
662 (font-lock-fontify-buffer)
663 (forward-line 2)
664 (buffer-substring (point) (progn
665 (re-search-forward "^#\\+END" nil t)
666 (point-at-bol)))))
668 (defun org-clock-report (&optional arg)
669 "Create a table containing a report about clocked time.
670 If the cursor is inside an existing clocktable block, then the table
671 will be updated. If not, a new clocktable will be inserted.
672 When called with a prefix argument, move to the first clock table in the
673 buffer and update it."
674 (interactive "P")
675 (org-clock-remove-overlays)
676 (when arg
677 (org-find-dblock "clocktable")
678 (org-show-entry))
679 (if (org-in-clocktable-p)
680 (goto-char (org-in-clocktable-p))
681 (org-create-dblock (list :name "clocktable"
682 :maxlevel 2 :scope 'file)))
683 (org-update-dblock))
685 (defun org-in-clocktable-p ()
686 "Check if the cursor is in a clocktable."
687 (let ((pos (point)) start)
688 (save-excursion
689 (end-of-line 1)
690 (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
691 (setq start (match-beginning 0))
692 (re-search-forward "^#\\+END:.*" nil t)
693 (>= (match-end 0) pos)
694 start))))
696 (defun org-clock-special-range (key &optional time as-strings)
697 "Return two times bordering a special time range.
698 Key is a symbol specifying the range and can be one of `today', `yesterday',
699 `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
700 A week starts Monday 0:00 and ends Sunday 24:00.
701 The range is determined relative to TIME. TIME defaults to the current time.
702 The return value is a cons cell with two internal times like the ones
703 returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
704 the returned times will be formatted strings."
705 (if (integerp key) (setq key (intern (number-to-string key))))
706 (let* ((tm (decode-time (or time (current-time))))
707 (s 0) (m (nth 1 tm)) (h (nth 2 tm))
708 (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
709 (dow (nth 6 tm))
710 (skey (symbol-name key))
711 (shift 0)
712 s1 m1 h1 d1 month1 y1 diff ts te fm txt w date)
713 (cond
714 ((string-match "^[0-9]+$" skey)
715 (setq y (string-to-number skey) m 1 d 1 key 'year))
716 ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)$" skey)
717 (setq y (string-to-number (match-string 1 skey))
718 month (string-to-number (match-string 2 skey))
719 d 1 key 'month))
720 ((string-match "^\\([0-9]+\\)-[wW]\\([0-9]\\{1,2\\}\\)$" skey)
721 (require 'cal-iso)
722 (setq y (string-to-number (match-string 1 skey))
723 w (string-to-number (match-string 2 skey)))
724 (setq date (calendar-gregorian-from-absolute
725 (calendar-absolute-from-iso (list w 1 y))))
726 (setq d (nth 1 date) month (car date) y (nth 2 date)
727 dow 1
728 key 'week))
729 ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)-\\([0-9]\\{1,2\\}\\)$" skey)
730 (setq y (string-to-number (match-string 1 skey))
731 month (string-to-number (match-string 2 skey))
732 d (string-to-number (match-string 3 skey))
733 key 'day))
734 ((string-match "\\([-+][0-9]+\\)$" skey)
735 (setq shift (string-to-number (match-string 1 skey))
736 key (intern (substring skey 0 (match-beginning 1))))))
737 (when (= shift 0)
738 (cond ((eq key 'yesterday) (setq key 'today shift -1))
739 ((eq key 'lastweek) (setq key 'week shift -1))
740 ((eq key 'lastmonth) (setq key 'month shift -1))
741 ((eq key 'lastyear) (setq key 'year shift -1))))
742 (cond
743 ((memq key '(day today))
744 (setq d (+ d shift) h 0 m 0 h1 24 m1 0))
745 ((memq key '(week thisweek))
746 (setq diff (+ (* -7 shift) (if (= dow 0) 6 (1- dow)))
747 m 0 h 0 d (- d diff) d1 (+ 7 d)))
748 ((memq key '(month thismonth))
749 (setq d 1 h 0 m 0 d1 1 month (+ month shift) month1 (1+ month) h1 0 m1 0))
750 ((memq key '(year thisyear))
751 (setq m 0 h 0 d 1 month 1 y (+ y shift) y1 (1+ y)))
752 (t (error "No such time block %s" key)))
753 (setq ts (encode-time s m h d month y)
754 te (encode-time (or s1 s) (or m1 m) (or h1 h)
755 (or d1 d) (or month1 month) (or y1 y)))
756 (setq fm (cdr org-time-stamp-formats))
757 (cond
758 ((memq key '(day today))
759 (setq txt (format-time-string "%A, %B %d, %Y" ts)))
760 ((memq key '(week thisweek))
761 (setq txt (format-time-string "week %G-W%V" ts)))
762 ((memq key '(month thismonth))
763 (setq txt (format-time-string "%B %Y" ts)))
764 ((memq key '(year thisyear))
765 (setq txt (format-time-string "the year %Y" ts))))
766 (if as-strings
767 (list (format-time-string fm ts) (format-time-string fm te) txt)
768 (list ts te txt))))
770 (defun org-clocktable-shift (dir n)
771 "Try to shift the :block date of the clocktable at point.
772 Point must be in the #+BEGIN: line of a clocktable, or this function
773 will throw an error.
774 DIR is a direction, a symbol `left', `right', `up', or `down'.
775 Both `left' and `down' shift the block toward the past, `up' and `right'
776 push it toward the future.
777 N is the number of shift steps to take. The size of the step depends on
778 the currently selected interval size."
779 (setq n (prefix-numeric-value n))
780 (and (memq dir '(left down)) (setq n (- n)))
781 (save-excursion
782 (goto-char (point-at-bol))
783 (if (not (looking-at "#\\+BEGIN: clocktable\\>.*?:block[ \t]+\\(\\S-+\\)"))
784 (error "Line needs a :block definition before this command works")
785 (let* ((b (match-beginning 1)) (e (match-end 1))
786 (s (match-string 1))
787 block shift ins y mw d date wp m)
788 (cond
789 ((equal s "yesterday") (setq s "today-1"))
790 ((equal s "lastweek") (setq s "thisweek-1"))
791 ((equal s "lastmonth") (setq s "thismonth-1"))
792 ((equal s "lastyear") (setq s "thisyear-1")))
793 (cond
794 ((string-match "^\\(today\\|thisweek\\|thismonth\\|thisyear\\)\\([-+][0-9]+\\)?$" s)
795 (setq block (match-string 1 s)
796 shift (if (match-end 2)
797 (string-to-number (match-string 2 s))
799 (setq shift (+ shift n))
800 (setq ins (if (= shift 0) block (format "%s%+d" block shift))))
801 ((string-match "\\([0-9]+\\)\\(-\\([wW]?\\)\\([0-9]\\{1,2\\}\\)\\(-\\([0-9]\\{1,2\\}\\)\\)?\\)?" s)
802 ;; 1 1 2 3 3 4 4 5 6 6 5 2
803 (setq y (string-to-number (match-string 1 s))
804 wp (and (match-end 3) (match-string 3 s))
805 mw (and (match-end 4) (string-to-number (match-string 4 s)))
806 d (and (match-end 6) (string-to-number (match-string 6 s))))
807 (cond
808 (d (setq ins (format-time-string
809 "%Y-%m-%d"
810 (encode-time 0 0 0 (+ d n) m y))))
811 ((and wp mw (> (length wp) 0))
812 (require 'cal-iso)
813 (setq date (calendar-gregorian-from-absolute (calendar-absolute-from-iso (list (+ mw n) 1 y))))
814 (setq ins (format-time-string
815 "%G-W%V"
816 (encode-time 0 0 0 (nth 1 date) (car date) (nth 2 date)))))
818 (setq ins (format-time-string
819 "%Y-%m"
820 (encode-time 0 0 0 1 (+ mw n) y))))
822 (setq ins (number-to-string (+ y n))))))
823 (t (error "Cannot shift clocktable block")))
824 (when ins
825 (goto-char b)
826 (insert ins)
827 (delete-region (point) (+ (point) (- e b)))
828 (beginning-of-line 1)
829 (org-update-dblock)
830 t)))))
832 (defun org-dblock-write:clocktable (params)
833 "Write the standard clocktable."
834 (catch 'exit
835 (let* ((hlchars '((1 . "*") (2 . "/")))
836 (ins (make-marker))
837 (total-time nil)
838 (scope (plist-get params :scope))
839 (tostring (plist-get params :tostring))
840 (multifile (plist-get params :multifile))
841 (header (plist-get params :header))
842 (maxlevel (or (plist-get params :maxlevel) 3))
843 (step (plist-get params :step))
844 (emph (plist-get params :emphasize))
845 (ts (plist-get params :tstart))
846 (te (plist-get params :tend))
847 (block (plist-get params :block))
848 (link (plist-get params :link))
849 ipos time p level hlc hdl content recalc formula pcol
850 cc beg end pos tbl tbl1 range-text rm-file-column scope-is-list st)
851 (setq org-clock-file-total-minutes nil)
852 (when step
853 (unless (or block (and ts te))
854 (error "Clocktable `:step' can only be used with `:block' or `:tstart,:end'"))
855 (org-clocktable-steps params)
856 (throw 'exit nil))
857 (when block
858 (setq cc (org-clock-special-range block nil t)
859 ts (car cc) te (nth 1 cc) range-text (nth 2 cc)))
860 (when (integerp ts) (setq ts (calendar-gregorian-from-absolute ts)))
861 (when (integerp te) (setq te (calendar-gregorian-from-absolute te)))
862 (when (and ts (listp ts))
863 (setq ts (format "%4d-%02d-%02d" (nth 2 ts) (car ts) (nth 1 ts))))
864 (when (and te (listp te))
865 (setq te (format "%4d-%02d-%02d" (nth 2 te) (car te) (nth 1 te))))
866 ;; Now the times are strings we can parse.
867 (if ts (setq ts (time-to-seconds
868 (apply 'encode-time (org-parse-time-string ts)))))
869 (if te (setq te (time-to-seconds
870 (apply 'encode-time (org-parse-time-string te)))))
871 (move-marker ins (point))
872 (setq ipos (point))
874 ;; Get the right scope
875 (setq pos (point))
876 (cond
877 ((and scope (listp scope) (symbolp (car scope)))
878 (setq scope (eval scope)))
879 ((eq scope 'agenda)
880 (setq scope (org-agenda-files t)))
881 ((eq scope 'agenda-with-archives)
882 (setq scope (org-agenda-files t))
883 (setq scope (org-add-archive-files scope)))
884 ((eq scope 'file-with-archives)
885 (setq scope (org-add-archive-files (list (buffer-file-name)))
886 rm-file-column t)))
887 (setq scope-is-list (and scope (listp scope)))
888 (save-restriction
889 (cond
890 ((not scope))
891 ((eq scope 'file) (widen))
892 ((eq scope 'subtree) (org-narrow-to-subtree))
893 ((eq scope 'tree)
894 (while (org-up-heading-safe))
895 (org-narrow-to-subtree))
896 ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
897 (symbol-name scope)))
898 (setq level (string-to-number (match-string 1 (symbol-name scope))))
899 (catch 'exit
900 (while (org-up-heading-safe)
901 (looking-at outline-regexp)
902 (if (<= (org-reduced-level (funcall outline-level)) level)
903 (throw 'exit nil))))
904 (org-narrow-to-subtree))
905 (scope-is-list
906 (let* ((files scope)
907 (scope 'agenda)
908 (p1 (copy-sequence params))
909 file)
910 (setq p1 (plist-put p1 :tostring t))
911 (setq p1 (plist-put p1 :multifile t))
912 (setq p1 (plist-put p1 :scope 'file))
913 (org-prepare-agenda-buffers files)
914 (while (setq file (pop files))
915 (with-current-buffer (find-buffer-visiting file)
916 (setq tbl1 (org-dblock-write:clocktable p1))
917 (when tbl1
918 (push (org-clocktable-add-file
919 file
920 (concat "| |*File time*|*"
921 (org-minutes-to-hh:mm-string
922 org-clock-file-total-minutes)
923 "*|\n"
924 tbl1)) tbl)
925 (setq total-time (+ (or total-time 0)
926 org-clock-file-total-minutes))))))))
927 (goto-char pos)
929 (unless scope-is-list
930 (org-clock-sum ts te)
931 (goto-char (point-min))
932 (setq st t)
933 (while (or (and (bobp) (prog1 st (setq st nil))
934 (get-text-property (point) :org-clock-minutes)
935 (setq p (point-min)))
936 (setq p (next-single-property-change (point) :org-clock-minutes)))
937 (goto-char p)
938 (when (setq time (get-text-property p :org-clock-minutes))
939 (save-excursion
940 (beginning-of-line 1)
941 (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
942 (setq level (org-reduced-level
943 (- (match-end 1) (match-beginning 1))))
944 (<= level maxlevel))
945 (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
946 hdl (if (not link)
947 (match-string 2)
948 (org-make-link-string
949 (format "file:%s::%s"
950 (buffer-file-name)
951 (save-match-data
952 (org-make-org-heading-search-string
953 (match-string 2))))
954 (match-string 2))))
955 (if (and (not multifile) (= level 1)) (push "|-" tbl))
956 (push (concat
957 "| " (int-to-string level) "|" hlc hdl hlc " |"
958 (make-string (1- level) ?|)
959 hlc (org-minutes-to-hh:mm-string time) hlc
960 " |") tbl))))))
961 (setq tbl (nreverse tbl))
962 (if tostring
963 (if tbl (mapconcat 'identity tbl "\n") nil)
964 (goto-char ins)
965 (insert-before-markers
966 (or header
967 (concat
968 "Clock summary at ["
969 (substring
970 (format-time-string (cdr org-time-stamp-formats))
971 1 -1)
973 (if block (concat ", for " range-text ".") "")
974 "\n\n"))
975 (if scope-is-list "|File" "")
976 "|L|Headline|Time|\n")
977 (setq total-time (or total-time org-clock-file-total-minutes))
978 (insert-before-markers
979 "|-\n|"
980 (if scope-is-list "|" "")
982 "*Total time*| *"
983 (org-minutes-to-hh:mm-string (or total-time 0))
984 "*|\n|-\n")
985 (setq tbl (delq nil tbl))
986 (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
987 (equal (substring (car tbl) 0 2) "|-"))
988 (pop tbl))
989 (insert-before-markers (mapconcat
990 'identity (delq nil tbl)
991 (if scope-is-list "\n|-\n" "\n")))
992 (backward-delete-char 1)
993 (if (setq formula (plist-get params :formula))
994 (cond
995 ((eq formula '%)
996 (setq pcol (+ (if scope-is-list 1 0) maxlevel 3))
997 (insert
998 (format
999 "\n#+TBLFM: $%d='(org-clock-time%% @%d$%d $%d..$%d);%%.1f"
1000 pcol
1002 (+ 3 (if scope-is-list 1 0))
1003 (+ (if scope-is-list 1 0) 3)
1004 (1- pcol)))
1005 (setq recalc t))
1006 ((stringp formula)
1007 (insert "\n#+TBLFM: " formula)
1008 (setq recalc t))
1009 (t (error "invalid formula in clocktable")))
1010 ;; Should we rescue an old formula?
1011 (when (stringp (setq content (plist-get params :content)))
1012 (when (string-match "^\\(#\\+TBLFM:.*\\)" content)
1013 (setq recalc t)
1014 (insert "\n" (match-string 1 (plist-get params :content)))
1015 (beginning-of-line 0))))
1016 (goto-char ipos)
1017 (skip-chars-forward "^|")
1018 (org-table-align)
1019 (when recalc
1020 (if (eq formula '%)
1021 (save-excursion (org-table-goto-column pcol nil 'force)
1022 (insert "%")))
1023 (org-table-recalculate 'all))
1024 (when rm-file-column
1025 (forward-char 1)
1026 (org-table-delete-column)))))))
1028 (defun org-clocktable-steps (params)
1029 (let* ((p1 (copy-sequence params))
1030 (ts (plist-get p1 :tstart))
1031 (te (plist-get p1 :tend))
1032 (step0 (plist-get p1 :step))
1033 (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
1034 (block (plist-get p1 :block))
1035 cc range-text)
1036 (when block
1037 (setq cc (org-clock-special-range block nil t)
1038 ts (car cc) te (nth 1 cc) range-text (nth 2 cc)))
1039 (if ts (setq ts (time-to-seconds
1040 (apply 'encode-time (org-parse-time-string ts)))))
1041 (if te (setq te (time-to-seconds
1042 (apply 'encode-time (org-parse-time-string te)))))
1043 (setq p1 (plist-put p1 :header ""))
1044 (setq p1 (plist-put p1 :step nil))
1045 (setq p1 (plist-put p1 :block nil))
1046 (while (< ts te)
1047 (or (bolp) (insert "\n"))
1048 (setq p1 (plist-put p1 :tstart (format-time-string
1049 (car org-time-stamp-formats)
1050 (seconds-to-time ts))))
1051 (setq p1 (plist-put p1 :tend (format-time-string
1052 (car org-time-stamp-formats)
1053 (seconds-to-time (setq ts (+ ts step))))))
1054 (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
1055 (plist-get p1 :tstart) "\n")
1056 (org-dblock-write:clocktable p1)
1057 (re-search-forward "#\\+END:")
1058 (end-of-line 0))))
1060 (defun org-clocktable-add-file (file table)
1061 (if table
1062 (let ((lines (org-split-string table "\n"))
1063 (ff (file-name-nondirectory file)))
1064 (mapconcat 'identity
1065 (mapcar (lambda (x)
1066 (if (string-match org-table-dataline-regexp x)
1067 (concat "|" ff x)
1069 lines)
1070 "\n"))))
1072 (defun org-clock-time% (total &rest strings)
1073 "Compute a time fraction in percent.
1074 TOTAL s a time string like 10:21 specifying the total times.
1075 STRINGS is a list of strings that should be checked for a time.
1076 The first string that does have a time will be used.
1077 This function is made for clock tables."
1078 (let ((re "\\([0-9]+\\):\\([0-9]+\\)")
1079 tot s)
1080 (save-match-data
1081 (catch 'exit
1082 (if (not (string-match re total))
1083 (throw 'exit 0.)
1084 (setq tot (+ (string-to-number (match-string 2 total))
1085 (* 60 (string-to-number (match-string 1 total)))))
1086 (if (= tot 0.) (throw 'exit 0.)))
1087 (while (setq s (pop strings))
1088 (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
1089 (throw 'exit
1090 (/ (* 100.0 (+ (string-to-number (match-string 2 s))
1091 (* 60 (string-to-number (match-string 1 s)))))
1092 tot))))
1093 0))))
1095 (defun org-clock-save ()
1096 "Persist various clock-related data to disk.
1097 The details of what will be saved are regulated by the variable
1098 `org-clock-persist'."
1099 (when org-clock-persist
1100 (let (b)
1101 (with-current-buffer (find-file (expand-file-name org-clock-persist-file))
1102 (progn
1103 (delete-region (point-min) (point-max))
1104 ;;Store clock
1105 (insert (format ";; org-persist.el - %s at %s\n"
1106 system-name (format-time-string
1107 (cdr org-time-stamp-formats))))
1108 (if (and (setq b (marker-buffer org-clock-marker))
1109 (setq b (or (buffer-base-buffer b) b))
1110 (buffer-live-p b)
1111 (buffer-file-name b)
1112 (or (not org-clock-persist-query-save)
1113 (y-or-n-p (concat "Save current clock ("
1114 (substring-no-properties org-clock-heading)
1115 ") "))))
1116 (insert "(setq resume-clock '(\""
1117 (buffer-file-name (marker-buffer org-clock-marker))
1118 "\" . " (int-to-string (marker-position org-clock-marker))
1119 "))\n"))
1120 ;; Store clocked task history. Tasks are stored reversed to make
1121 ;; reading simpler
1122 (when (and org-clock-history (eq org-clock-persist t))
1123 (insert
1124 "(setq stored-clock-history '("
1125 (mapconcat
1126 (lambda (m)
1127 (when (and (setq b (marker-buffer m))
1128 (setq b (or (buffer-base-buffer b) b))
1129 (buffer-live-p b)
1130 (buffer-file-name b))
1131 (concat "(\"" (buffer-file-name b)
1132 "\" . " (int-to-string (marker-position m))
1133 ")")))
1134 (reverse org-clock-history) " ") "))\n"))
1135 (save-buffer)
1136 (kill-buffer (current-buffer)))))))
1138 (defvar org-clock-loaded nil
1139 "Was the clock file loaded?")
1141 (defun org-clock-load ()
1142 "Load various clock-related data from disk, optionally resuming
1143 a stored clock"
1144 (when (and org-clock-persist (not org-clock-loaded))
1145 (let ((filename (expand-file-name org-clock-persist-file))
1146 (org-clock-in-resume 'auto-restart)
1147 resume-clock stored-clock-history)
1148 (if (not (file-readable-p filename))
1149 (message "Not restoring clock data; %s not found"
1150 org-clock-persist-file)
1151 (message "%s" "Restoring clock data")
1152 (setq org-clock-loaded t)
1153 (load-file filename)
1154 ;; load history
1155 (when stored-clock-history
1156 (save-window-excursion
1157 (mapc (lambda (task)
1158 (if (file-exists-p (car task))
1159 (org-clock-history-push (cdr task)
1160 (find-file (car task)))))
1161 stored-clock-history)))
1162 ;; resume clock
1163 (when (and resume-clock org-clock-persist
1164 (file-exists-p (car resume-clock))
1165 (or (not org-clock-persist-query-resume)
1166 (y-or-n-p
1167 (concat
1168 "Resume clock ("
1169 (with-current-buffer (find-file (car resume-clock))
1170 (save-excursion
1171 (goto-char (cdr resume-clock))
1172 (org-back-to-heading t)
1173 (and (looking-at org-complex-heading-regexp)
1174 (match-string 4))))
1175 ") "))))
1176 (when (file-exists-p (car resume-clock))
1177 (with-current-buffer (find-file (car resume-clock))
1178 (goto-char (cdr resume-clock))
1179 (org-clock-in)
1180 (if (org-invisible-p)
1181 (org-show-context)))))))))
1183 ;;;###autoload
1184 (defun org-clock-persistence-insinuate ()
1185 "Set up hooks for clock persistence"
1186 (add-hook 'org-mode-hook 'org-clock-load)
1187 (add-hook 'kill-emacs-hook 'org-clock-save))
1189 (provide 'org-clock)
1191 ;; arch-tag: 7b42c5d4-9b36-48be-97c0-66a869daed4c
1193 ;;; org-clock.el ends here