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
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;; This file contains the time clocking code for Org-mode
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."
44 (defcustom org-clock-into-drawer org-log-into-drawer
45 "Should clocking info be wrapped into a drawer?
46 When t, clocking info will always be inserted into a :LOGBOOK: 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 When a string, it names the drawer to be used.
53 The default for this variable is the value of `org-log-into-drawer',
58 (const :tag
"Always" t
)
59 (const :tag
"Only when drawer exists" nil
)
60 (integer :tag
"When at least N clock entries")
61 (const :tag
"Into LOGBOOK drawer" "LOGBOOK")
62 (string :tag
"Into Drawer named...")))
64 (defcustom org-clock-out-when-done t
65 "When non-nil, clock will be stopped when the clocked entry is marked DONE.
66 DONE here means any DONE-like state.
67 A nil value means clock will keep running until stopped explicitly with
68 `C-c C-x C-o', or until the clock is started in a different item.
69 Instead of t, this can also be a list of TODO states that should trigger
74 (const :tag
"Yes, when done" t
)
75 (repeat :tag
"State list"
76 (string :tag
"TODO keyword"))))
78 (defcustom org-clock-out-remove-zero-time-clocks nil
79 "Non-nil means remove the clock line when the resulting time is zero."
83 (defcustom org-clock-in-switch-to-state nil
84 "Set task to a special todo state while clocking it.
85 The value should be the state to which the entry should be
86 switched. If the value is a function, it must take one
87 parameter (the current TODO state of the item) and return the
88 state to switch it to."
92 (const :tag
"Don't force a state" nil
)
94 (symbol :tag
"Function")))
96 (defcustom org-clock-out-switch-to-state nil
97 "Set task to a special todo state after clocking out.
98 The value should be the state to which the entry should be
99 switched. If the value is a function, it must take one
100 parameter (the current TODO state of the item) and return the
101 state to switch it to."
105 (const :tag
"Don't force a state" nil
)
106 (string :tag
"State")
107 (symbol :tag
"Function")))
109 (defcustom org-clock-history-length
5
110 "Number of clock tasks to remember in history."
114 (defcustom org-clock-goto-may-find-recent-task t
115 "Non-nil means `org-clock-goto' can go to recent task if no active clock."
119 (defcustom org-clock-heading-function nil
120 "When non-nil, should be a function to create `org-clock-heading'.
121 This is the string shown in the mode line when a clock is running.
122 The function is called with point at the beginning of the headline."
126 (defcustom org-clock-string-limit
0
127 "Maximum length of clock strings in the modeline. 0 means no limit."
131 (defcustom org-clock-in-resume nil
132 "If non-nil, resume clock when clocking into task with open clock.
133 When clocking into a task with a clock entry which has not been closed,
134 the clock can be resumed from that point."
138 (defcustom org-clock-persist nil
139 "When non-nil, save the running clock when emacs is closed.
140 The clock is resumed when emacs restarts.
141 When this is t, both the running clock, and the entire clock
142 history are saved. When this is the symbol `clock', only the
143 running clock is saved.
145 When Emacs restarts with saved clock information, the file containing the
146 running clock as well as all files mentioned in the clock history will
148 All this depends on running `org-clock-persistence-insinuate' in .emacs"
151 (const :tag
"Just the running clock" clock
)
152 (const :tag
"Just the history" history
)
153 (const :tag
"Clock and history" t
)
154 (const :tag
"No persistence" nil
)))
156 (defcustom org-clock-persist-file
(convert-standard-filename
157 "~/.emacs.d/org-clock-save.el")
158 "File to save clock data to."
162 (defcustom org-clock-persist-query-save nil
163 "When non-nil, ask before saving the current clock on exit."
167 (defcustom org-clock-persist-query-resume t
168 "When non-nil, ask before resuming any stored clock during load."
172 (defcustom org-clock-sound nil
173 "Sound that will used for notifications.
177 t standard Emacs beep
178 file name play this sound file. If not possible, fall back to beep"
181 (const :tag
"No sound" nil
)
182 (const :tag
"Standard beep" t
)
183 (file :tag
"Play sound file")))
185 (defcustom org-clock-modeline-total
'auto
186 "Default setting for the time included for the modeline clock.
187 This can be overruled locally using the CLOCK_MODELINE_TOTAL property.
190 current Only the time in the current instance of the clock
191 today All time clocked into this task today
192 repeat All time clocked into this task since last repeat
193 all All time ever recorded for this task
194 auto Automatically, either `all', or `repeat' for repeating tasks"
197 (const :tag
"Current clock" current
)
198 (const :tag
"Today's task time" today
)
199 (const :tag
"Since last repeat" repeat
)
200 (const :tag
"All task time" all
)
201 (const :tag
"Automatically, `all' or since `repeat'" auto
)))
203 (defcustom org-task-overrun-text nil
204 "The extra modeline text that should indicate that the clock is overrun.
205 The can be nil to indicate that instead of adding text, the clock time
206 should get a different face (`org-mode-line-clock-overrun').
207 When this is a string, it is prepended to the clock string as an indication,
208 also using the face `org-mode-line-clock-overrun'."
211 (const :tag
"Just mark the time string" nil
)
212 (string :tag
"Text to prepend")))
214 (defcustom org-show-notification-handler nil
215 "Function or program to send notification with.
216 The function or program will be called with the notification
220 (string :tag
"Program")
221 (function :tag
"Function")))
223 (defcustom org-clock-clocktable-default-properties
'(:maxlevel
2 :scope file
)
224 "Default properties for new clocktables."
228 (defcustom org-clock-idle-time nil
229 "When non-nil, resolve open clocks if the user is idle more than X minutes."
232 (const :tag
"Never" nil
)
233 (integer :tag
"After N minutes")))
235 (defcustom org-clock-auto-clock-resolution
'when-no-clock-is-running
236 "When to automatically resolve open clocks found in Org buffers."
239 (const :tag
"Never" nil
)
240 (const :tag
"Always" t
)
241 (const :tag
"When no clock is running" when-no-clock-is-running
)))
243 (defcustom org-clock-report-include-clocking-task nil
244 "When non-nil, include the current clocking task time in clock reports."
248 (defvar org-clock-in-prepare-hook nil
249 "Hook run when preparing the clock.
250 This hook is run before anything happens to the task that
251 you want to clock in. For example, you can use this hook
252 to add an effort property.")
253 (defvar org-clock-in-hook nil
254 "Hook run when starting the clock.")
255 (defvar org-clock-out-hook nil
256 "Hook run when stopping the current clock.")
258 (defvar org-clock-cancel-hook nil
259 "Hook run when cancelling the current clock.")
260 (defvar org-clock-goto-hook nil
261 "Hook run when selecting the currently clocked-in entry.")
262 (defvar org-clock-has-been-used nil
263 "Has the clock been used during the current Emacs session?")
265 ;;; The clock for measuring work time.
267 (defvar org-mode-line-string
"")
268 (put 'org-mode-line-string
'risky-local-variable t
)
270 (defvar org-clock-mode-line-timer nil
)
271 (defvar org-clock-idle-timer nil
)
272 (defvar org-clock-heading
) ; defined in org.el
273 (defvar org-clock-heading-for-remember
"")
274 (defvar org-clock-start-time
"")
276 (defvar org-clock-left-over-time nil
277 "If non-nil, user cancelled a clock; this is when leftover time started.")
279 (defvar org-clock-effort
""
280 "Effort estimate of the currently clocking task")
282 (defvar org-clock-total-time nil
283 "Holds total time, spent previously on currently clocked item.
284 This does not include the time in the currently running clock.")
286 (defvar org-clock-history nil
287 "List of marker pointing to recent clocked tasks.")
289 (defvar org-clock-default-task
(make-marker)
290 "Marker pointing to the default task that should clock time.
291 The clock can be made to switch to this task after clocking out
292 of a different task.")
294 (defvar org-clock-interrupted-task
(make-marker)
295 "Marker pointing to the task that has been interrupted by the current clock.")
297 (defvar org-clock-mode-line-map
(make-sparse-keymap))
298 (define-key org-clock-mode-line-map
[mode-line mouse-2
] 'org-clock-goto
)
299 (define-key org-clock-mode-line-map
[mode-line mouse-1
] 'org-clock-menu
)
301 (defun org-clock-menu ()
305 ["Clock out" org-clock-out t
]
306 ["Change effort estimate" org-clock-modify-effort-estimate t
]
307 ["Go to clock entry" org-clock-goto t
]
308 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t
:keys
"C-u C-c C-x C-i"])))
310 (defun org-clock-history-push (&optional pos buffer
)
311 "Push a marker to the clock history."
312 (setq org-clock-history-length
(max 1 (min 35 org-clock-history-length
)))
313 (let ((m (move-marker (make-marker) (or pos
(point)) buffer
)) n l
)
314 (while (setq n
(member m org-clock-history
))
315 (move-marker (car n
) nil
))
316 (setq org-clock-history
318 (mapcar (lambda (x) (if (marker-buffer x
) x nil
))
320 (when (>= (setq l
(length org-clock-history
)) org-clock-history-length
)
321 (setq org-clock-history
323 (nthcdr (- l org-clock-history-length -
1)
324 (nreverse org-clock-history
)))))
325 (push m org-clock-history
)))
327 (defun org-clock-save-markers-for-cut-and-paste (beg end
)
328 "Save relative positions of markers in region."
329 (org-check-and-save-marker org-clock-marker beg end
)
330 (org-check-and-save-marker org-clock-hd-marker beg end
)
331 (org-check-and-save-marker org-clock-default-task beg end
)
332 (org-check-and-save-marker org-clock-interrupted-task beg end
)
333 (mapc (lambda (m) (org-check-and-save-marker m beg end
))
336 (defun org-clocking-buffer ()
337 "Returns clocking buffer if we are currently clocking a task or nil"
338 (marker-buffer org-clock-marker
))
340 (defun org-clocking-p ()
341 "Returns t when clocking a task"
342 (not (equal (org-clocking-buffer) nil
)))
344 (defun org-clock-select-task (&optional prompt
)
345 "Select a task that recently was associated with clocking."
347 (let (sel-list rpl
(i 0) s
)
348 (save-window-excursion
349 (org-switch-to-buffer-other-window
350 (get-buffer-create "*Clock Task Select*"))
352 (when (marker-buffer org-clock-default-task
)
353 (insert (org-add-props "Default Task\n" nil
'face
'bold
))
354 (setq s
(org-clock-insert-selection-line ?d org-clock-default-task
))
356 (when (marker-buffer org-clock-interrupted-task
)
357 (insert (org-add-props "The task interrupted by starting the last one\n" nil
'face
'bold
))
358 (setq s
(org-clock-insert-selection-line ?i org-clock-interrupted-task
))
360 (when (org-clocking-p)
361 (insert (org-add-props "Current Clocking Task\n" nil
'face
'bold
))
362 (setq s
(org-clock-insert-selection-line ?c org-clock-marker
))
364 (insert (org-add-props "Recent Tasks\n" nil
'face
'bold
))
367 (when (marker-buffer m
)
369 s
(org-clock-insert-selection-line
373 (if (fboundp 'int-to-char
) (setf (car s
) (int-to-char (car s
))))
376 (org-fit-window-to-buffer)
377 (message (or prompt
"Select task for clocking:"))
378 (setq rpl
(read-char-exclusive))
382 ((assoc rpl sel-list
) (cdr (assoc rpl sel-list
)))
383 (t (error "Invalid task choice %c" rpl
))))))
385 (defun org-clock-insert-selection-line (i marker
)
386 "Insert a line for the clock selection menu.
387 And return a cons cell with the selection character integer and the marker
389 (when (marker-buffer marker
)
390 (let (file cat task heading prefix
)
391 (with-current-buffer (org-base-buffer (marker-buffer marker
))
397 (setq file
(buffer-file-name (marker-buffer marker
))
398 cat
(or (org-get-category)
399 (progn (org-refresh-category-properties)
401 heading
(org-get-heading 'notags
)
402 prefix
(save-excursion
403 (org-back-to-heading t
)
407 (org-fontify-like-in-org-mode
408 (concat prefix heading
)
410 (length prefix
)))))))
412 (insert (format "[%c] %-15s %s\n" i cat task
))
415 (defvar org-task-overrun nil
416 "Internal flag indicating if the clock has overrun the planned time.")
417 (defvar org-clock-update-period
60
418 "Number of seconds between mode line clock string updates.")
420 (defun org-clock-get-clock-string ()
421 "Form a clock-string, that will be shown in the mode line.
422 If an effort estimate was defined for the current item, use
423 01:30/01:50 format (clocked/estimated).
424 If not, show simply the clocked time like 01:50."
425 (let* ((clocked-time (org-clock-get-clocked-time))
426 (h (floor clocked-time
60))
427 (m (- clocked-time
(* 60 h
))))
429 (let* ((effort-in-minutes
430 (org-hh:mm-string-to-minutes org-clock-effort
))
431 (effort-h (floor effort-in-minutes
60))
432 (effort-m (- effort-in-minutes
(* effort-h
60)))
435 (format org-time-clocksum-format h m
)
436 'face
(if (and org-task-overrun
(not org-task-overrun-text
))
437 'org-mode-line-clock-overrun
'org-mode-line-clock
)))
438 (effort-str (format org-time-clocksum-format effort-h effort-m
))
439 (clockstr (org-propertize
440 (concat "[%s/" effort-str
441 "] (" (replace-regexp-in-string "%" "%%" org-clock-heading
) ")")
442 'face
'org-mode-line-clock
)))
443 (format clockstr work-done-str
))
444 (org-propertize (format
445 (concat "[" org-time-clocksum-format
" (%s)]")
446 h m org-clock-heading
)
447 'face
'org-mode-line-clock
))))
449 (defun org-clock-update-mode-line ()
451 (org-clock-notify-once-if-expired)
452 (setq org-task-overrun nil
))
453 (setq org-mode-line-string
455 (let ((clock-string (org-clock-get-clock-string))
456 (help-text "Org-mode clock is running.\nmouse-1 shows a menu\nmouse-2 will jump to task"))
457 (if (and (> org-clock-string-limit
0)
458 (> (length clock-string
) org-clock-string-limit
))
460 (substring clock-string
0 org-clock-string-limit
)
461 'help-echo
(concat help-text
": " org-clock-heading
))
462 (org-propertize clock-string
'help-echo help-text
)))
463 'local-map org-clock-mode-line-map
464 'mouse-face
(if (featurep 'xemacs
) 'highlight
'mode-line-highlight
)
466 (if (and org-task-overrun org-task-overrun-text
)
467 (setq org-mode-line-string
468 (concat (org-propertize
469 org-task-overrun-text
470 'face
'org-mode-line-clock-overrun
) org-mode-line-string
)))
472 (force-mode-line-update))
474 (defun org-clock-get-clocked-time ()
475 "Get the clocked time for the current item in minutes.
476 The time returned includes the time spent on this task in
477 previous clocking intervals."
478 (let ((currently-clocked-time
479 (floor (- (org-float-time)
480 (org-float-time org-clock-start-time
)) 60)))
481 (+ currently-clocked-time
(or org-clock-total-time
0))))
483 (defun org-clock-modify-effort-estimate (&optional value
)
484 "Add to or set the effort estimate of the item currently being clocked.
485 VALUE can be a number of minutes, or a string with format hh:mm or mm.
486 When the string starts with a + or a - sign, the current value of the effort
487 property will be changed by that amount.
488 This will update the \"Effort\" property of currently clocked item, and
491 (when (org-clock-is-active)
492 (let ((current org-clock-effort
) sign
)
494 ;; Prompt user for a value or a change
497 (format "Set effort (hh:mm or mm%s): "
499 (format ", prefix + to add to %s" org-clock-effort
)
501 (when (stringp value
)
502 ;; A string. See if it is a delta
503 (setq sign
(string-to-char value
))
504 (if (member sign
'(?- ?
+))
505 (setq current
(org-hh:mm-string-to-minutes
(substring current
1)))
507 (setq value
(org-hh:mm-string-to-minutes value
))
509 (setq value
(- current value
))
510 (if (equal ?
+ sign
) (setq value
(+ current value
)))))
511 (setq value
(max 0 value
)
512 org-clock-effort
(org-minutes-to-hh:mm-string value
))
513 (org-entry-put org-clock-marker
"Effort" org-clock-effort
)
514 (org-clock-update-mode-line)
515 (message "Effort is now %s" org-clock-effort
))))
517 (defvar org-clock-notification-was-shown nil
518 "Shows if we have shown notification already.")
520 (defun org-clock-notify-once-if-expired ()
521 "Show notification if we spent more time than we estimated before.
522 Notification is shown only once."
523 (when (org-clocking-p)
524 (let ((effort-in-minutes (org-hh:mm-string-to-minutes org-clock-effort
))
525 (clocked-time (org-clock-get-clocked-time)))
526 (if (setq org-task-overrun
527 (if (or (null effort-in-minutes
) (zerop effort-in-minutes
))
529 (>= clocked-time effort-in-minutes
)))
530 (unless org-clock-notification-was-shown
531 (setq org-clock-notification-was-shown t
)
533 (format "Task '%s' should be finished by now. (%s)"
534 org-clock-heading org-clock-effort
) t
))
535 (setq org-clock-notification-was-shown nil
)))))
537 (defun org-notify (notification &optional play-sound
)
538 "Send a NOTIFICATION and maybe PLAY-SOUND."
539 (org-show-notification notification
)
540 (if play-sound
(org-clock-play-sound)))
542 (defun org-show-notification (notification)
544 Use `org-show-notification-handler' if defined,
545 use libnotify if available, or fall back on a message."
546 (cond ((functionp org-show-notification-handler
)
547 (funcall org-show-notification-handler notification
))
548 ((stringp org-show-notification-handler
)
549 (start-process "emacs-timer-notification" nil
550 org-show-notification-handler notification
))
551 ((org-program-exists "notify-send")
552 (start-process "emacs-timer-notification" nil
553 "notify-send" notification
))
554 ;; Maybe the handler will send a message, so only use message as
555 ;; a fall back option
556 (t (message "%s" notification
))))
558 (defun org-clock-play-sound ()
559 "Play sound as configured by `org-clock-sound'.
560 Use alsa's aplay tool if available."
562 ((not org-clock-sound
))
563 ((eq org-clock-sound t
) (beep t
) (beep t
))
564 ((stringp org-clock-sound
)
565 (let ((file (expand-file-name org-clock-sound
)))
566 (if (file-exists-p file
)
567 (if (org-program-exists "aplay")
568 (start-process "org-clock-play-notification" nil
571 (play-sound-file file
)
572 (error (beep t
) (beep t
)))))))))
574 (defun org-program-exists (program-name)
575 "Checks whenever we can locate program and launch it."
576 (if (eq system-type
'gnu
/linux
)
577 (= 0 (call-process "which" nil nil nil program-name
))))
579 (defvar org-clock-mode-line-entry nil
580 "Information for the modeline about the running clock.")
582 (defun org-find-open-clocks (file)
583 "Search through the given file and find all open clocks."
584 (let ((buf (or (get-file-buffer file
)
585 (find-file-noselect file
)))
587 (with-current-buffer buf
589 (goto-char (point-min))
590 (while (re-search-forward "CLOCK: \\(\\[.*?\\]\\)$" nil t
)
591 (push (cons (copy-marker (1- (match-end 1)) t
)
592 (org-time-string-to-time (match-string 1))) clocks
))))
595 (defsubst org-is-active-clock
(clock)
596 "Return t if CLOCK is the currently active clock."
597 (and (org-clock-is-active)
598 (= org-clock-marker
(car clock
))))
600 (defmacro org-with-clock-position
(clock &rest forms
)
601 "Evaluate FORMS with CLOCK as the current active clock."
602 `(with-current-buffer (marker-buffer (car ,clock
))
606 (goto-char (car ,clock
))
610 (put 'org-with-clock-position
'lisp-indent-function
1)
612 (defmacro org-with-clock
(clock &rest forms
)
613 "Evaluate FORMS with CLOCK as the current active clock.
614 This macro also protects the current active clock from being altered."
615 `(org-with-clock-position ,clock
616 (let ((org-clock-start-time (cdr ,clock
))
617 (org-clock-total-time)
620 (org-clock-marker (car ,clock
))
621 (org-clock-hd-marker (save-excursion
622 (outline-back-to-heading t
)
626 (put 'org-with-clock
'lisp-indent-function
1)
628 (defsubst org-clock-clock-in
(clock &optional resume
)
629 "Clock in to the clock located by CLOCK.
630 If necessary, clock-out of the currently active clock."
631 (org-with-clock-position clock
632 (let ((org-clock-in-resume (or resume org-clock-in-resume
)))
635 (defsubst org-clock-clock-out
(clock &optional fail-quietly at-time
)
636 "Clock out of the clock located by CLOCK."
637 (let ((temp (copy-marker (car clock
)
638 (marker-insertion-type (car clock
)))))
639 (if (org-is-active-clock clock
)
640 (org-clock-out fail-quietly at-time
)
641 (org-with-clock clock
642 (org-clock-out fail-quietly at-time
)))
643 (setcar clock temp
)))
645 (defsubst org-clock-clock-cancel
(clock)
646 "Cancel the clock located by CLOCK."
647 (let ((temp (copy-marker (car clock
)
648 (marker-insertion-type (car clock
)))))
649 (if (org-is-active-clock clock
)
651 (org-with-clock clock
653 (setcar clock temp
)))
655 (defvar org-clock-clocking-in nil
)
656 (defvar org-clock-resolving-clocks nil
)
657 (defvar org-clock-resolving-clocks-due-to-idleness nil
)
659 (defun org-clock-resolve-clock (clock resolve-to
&optional close-p
660 restart-p fail-quietly
)
661 "Resolve `CLOCK' given the time `RESOLVE-TO', and the present.
662 `CLOCK' is a cons cell of the form (MARKER START-TIME).
663 This routine can do one of many things:
665 if `RESOLVE-TO' is nil
666 if `CLOSE-P' is non-nil, give an error
667 if this clock is the active clock, cancel it
668 else delete the clock line (as if it never happened)
669 if `RESTART-P' is non-nil, start a new clock
671 else if `RESOLVE-TO' is the symbol `now'
672 if `RESTART-P' is non-nil, give an error
673 if `CLOSE-P' is non-nil, clock out the entry and
674 if this clock is the active clock, stop it
675 else if this clock is the active clock, do nothing
676 else if there is no active clock, resume this clock
677 else ask to cancel the active clock, and if so,
678 resume this clock after cancelling it
680 else if `RESOLVE-TO' is some date in the future
681 give an error about `RESOLVE-TO' being invalid
683 else if `RESOLVE-TO' is some date in the past
684 if `RESTART-P' is non-nil, give an error
685 if `CLOSE-P' is non-nil, enter a closing time and
686 if this clock is the active clock, stop it
687 else if this clock is the active clock, enter a
688 closing time, stop the current clock, then
689 start a new clock for the same item
690 else just enter a closing time for this clock
691 and then start a new clock for the same item"
692 (let ((org-clock-resolving-clocks t
))
695 (org-clock-clock-cancel clock
)
696 (if (and restart-p
(not org-clock-clocking-in
))
697 (org-clock-clock-in clock
)))
699 ((eq resolve-to
'now
)
701 (error "RESTART-P is not valid here"))
702 (if (or close-p org-clock-clocking-in
)
703 (org-clock-clock-out clock fail-quietly
)
704 (unless (org-is-active-clock clock
)
705 (org-clock-clock-in clock t
))))
707 ((not (time-less-p resolve-to
(current-time)))
708 (error "RESOLVE-TO must refer to a time in the past"))
712 (error "RESTART-P is not valid here"))
713 (org-clock-clock-out clock fail-quietly resolve-to
)
714 (unless org-clock-clocking-in
716 (setq org-clock-left-over-time resolve-to
)
717 (org-clock-clock-in clock
)))))))
719 (defun org-clock-resolve (clock &optional prompt-fn last-valid fail-quietly
)
720 "Resolve an open org-mode clock.
721 An open clock was found, with `dangling' possibly being non-nil.
722 If this function was invoked with a prefix argument, non-dangling
723 open clocks are ignored. The given clock requires some sort of
724 user intervention to resolve it, either because a clock was left
725 dangling or due to an idle timeout. The clock resolution can
728 (a) deleted, the user doesn't care about the clock
729 (b) restarted from the current time (if no other clock is open)
730 (c) closed, giving the clock X minutes
731 (d) closed and then restarted
732 (e) resumed, as if the user had never left
734 The format of clock is (CONS MARKER START-TIME), where MARKER
735 identifies the buffer and position the clock is open at (and
736 thus, the heading it's under), and START-TIME is when the clock
740 (save-window-excursion
742 (unless org-clock-resolving-clocks-due-to-idleness
743 (org-with-clock clock
(org-clock-goto))
744 (with-current-buffer (marker-buffer (car clock
))
745 (goto-char (car clock
))
746 (if org-clock-into-drawer
748 (if (stringp org-clock-into-drawer
)
749 (concat ":" org-clock-into-drawer
":")
754 (outline-back-to-heading t
)
755 (search-forward logbook
)
756 (goto-char (match-beginning 0)))
758 (outline-back-to-heading t
)
759 (search-forward logbook
)
760 (search-forward ":END:")
761 (goto-char (match-end 0)))
764 (if (featurep 'xemacs
)
766 (message (concat (funcall prompt-fn clock
)
767 " [(kK)eep (sS)ubtract (C)ancel]? "))
768 (setq char-pressed
(read-char-exclusive)))
769 (while (or (null char-pressed
)
770 (and (not (memq char-pressed
'(?k ?K ?s ?S ?C ?i
)))
773 (read-char (concat (funcall prompt-fn clock
)
774 " [(kK)p (sS)ub (C)ncl (i)gn]? ")
776 (and (not (eq char-pressed ?i
)) char-pressed
))))))
777 (default (floor (/ (org-float-time
778 (time-subtract (current-time) last-valid
)) 60)))
779 (keep (and (memq ch
'(?k ?K
))
780 (read-number "Keep how many minutes? " default
)))
781 (subtractp (memq ch
'(?s ?S
)))
782 (barely-started-p (< (- (org-float-time last-valid
)
783 (org-float-time (cdr clock
))) 45))
784 (start-over (and subtractp barely-started-p
)))
786 (not (memq ch
'(?k ?K ?s ?S ?C
))))
788 (org-clock-resolve-clock
791 ;; If the time on the clock was less than a minute before
792 ;; the user went away, and they've ask to subtract all the
801 (time-add last-valid
(seconds-to-time (* 60 keep
)))))
804 (not (memq ch
'(?K ?S ?C
))))
807 (defun org-resolve-clocks (&optional also-non-dangling-p prompt-fn last-valid
)
808 "Resolve all currently open org-mode clocks.
809 If `also-non-dangling-p' is non-nil, also ask to resolve
810 non-dangling (i.e., currently open and valid) clocks."
812 (unless org-clock-resolving-clocks
813 (let ((org-clock-resolving-clocks t
))
814 (dolist (file (org-files-list))
815 (let ((clocks (org-find-open-clocks file
)))
816 (dolist (clock clocks
)
817 (let ((dangling (or (not (org-clock-is-active))
818 (/= (car clock
) org-clock-marker
))))
819 (unless (and (not dangling
) (not also-non-dangling-p
))
826 "Dangling clock started %d mins ago"
828 (/ (- (org-float-time (current-time))
829 (org-float-time (cdr clock
))) 60))))))
831 (cdr clock
)))))))))))
833 (defun org-emacs-idle-seconds ()
834 "Return the current Emacs idle time in seconds, or nil if not idle."
835 (let ((idle-time (current-idle-time)))
837 (org-float-time idle-time
)
840 (defun org-mac-idle-seconds ()
841 "Return the current Mac idle time in seconds"
842 (string-to-number (shell-command-to-string "ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle; last}'")))
844 (defun org-x11-idle-seconds ()
845 "Return the current X11 idle time in seconds"
846 (/ (string-to-number (shell-command-to-string "x11idle")) 1000))
848 (defun org-user-idle-seconds ()
849 "Return the number of seconds the user has been idle for.
850 This routine returns a floating point number."
852 ((eq system-type
'darwin
)
853 (org-mac-idle-seconds))
854 ((eq window-system
'x
)
855 (org-x11-idle-seconds))
857 (org-emacs-idle-seconds))))
859 (defvar org-clock-user-idle-seconds
)
861 (defun org-resolve-clocks-if-idle ()
862 "Resolve all currently open org-mode clocks.
863 This is performed after `org-clock-idle-time' minutes, to check
864 if the user really wants to stay clocked in after being idle for
866 (when (and org-clock-idle-time
(not org-clock-resolving-clocks
)
868 (let* ((org-clock-user-idle-seconds (org-user-idle-seconds))
869 (org-clock-user-idle-start
870 (time-subtract (current-time)
871 (seconds-to-time org-clock-user-idle-seconds
)))
872 (org-clock-resolving-clocks-due-to-idleness t
))
873 (if (> org-clock-user-idle-seconds
(* 60 org-clock-idle-time
))
875 (cons org-clock-marker
876 org-clock-start-time
)
879 (format "Clocked in & idle for %.1f mins"
881 (time-subtract (current-time)
882 org-clock-user-idle-start
))
884 org-clock-user-idle-start
)))))
886 (defun org-clock-in (&optional select
)
887 "Start the clock on the current item.
888 If necessary, clock-out of the currently active clock.
889 With prefix arg SELECT, offer a list of recently clocked tasks to
890 clock into. When SELECT is `C-u C-u', clock into the current task and mark
891 is as the default task, a special task that will always be offered in
892 the clocking selection, associated with the letter `d'."
894 (setq org-clock-notification-was-shown nil
)
896 (let ((interrupting (and (not org-clock-resolving-clocks-due-to-idleness
)
898 ts selected-task target-pos
(msg-extra "")
899 (left-over (and (not org-clock-resolving-clocks
)
900 org-clock-left-over-time
)))
901 (when (and org-clock-auto-clock-resolution
902 (or (not interrupting
)
903 (eq t org-clock-auto-clock-resolution
))
904 (not org-clock-clocking-in
)
905 (not org-clock-resolving-clocks
))
906 (setq org-clock-left-over-time nil
)
907 (let ((org-clock-clocking-in t
))
908 (org-resolve-clocks))) ; check if any clocks are dangling
909 (when (equal select
'(4))
910 (setq selected-task
(org-clock-select-task "Clock-in on task: "))
912 (setq selected-task
(copy-marker selected-task
))
915 ;; We are interrupting the clocking of a different task.
916 ;; Save a marker to this task, so that we can go back.
917 ;; First check if we are trying to clock into the same task!
919 (unless selected-task
920 (org-back-to-heading t
))
921 (and (equal (marker-buffer org-clock-hd-marker
)
923 (marker-buffer selected-task
)
925 (= (marker-position org-clock-hd-marker
)
927 (marker-position selected-task
)
929 (message "Clock continues in \"%s\"" org-clock-heading
)
931 (move-marker org-clock-interrupted-task
932 (marker-position org-clock-marker
)
933 (org-clocking-buffer))
936 (when (equal select
'(16))
937 ;; Mark as default clocking task
938 (org-clock-mark-default-task))
940 ;; Clock in at which position?
942 (if (and (eobp) (not (org-on-heading-p)))
945 (run-hooks 'org-clock-in-prepare-hook
)
947 (when (and selected-task
(marker-buffer selected-task
))
948 ;; There is a selected task, move to the correct buffer
949 ;; and set the new target position.
950 (set-buffer (org-base-buffer (marker-buffer selected-task
)))
951 (setq target-pos
(marker-position selected-task
))
952 (move-marker selected-task nil
))
956 (goto-char target-pos
)
957 (org-back-to-heading t
)
958 (or interrupting
(move-marker org-clock-interrupted-task nil
))
959 (org-clock-history-push)
960 (org-clock-set-current)
961 (cond ((functionp org-clock-in-switch-to-state
)
962 (looking-at org-complex-heading-regexp
)
963 (let ((newstate (funcall org-clock-in-switch-to-state
965 (if newstate
(org-todo newstate
))))
966 ((and org-clock-in-switch-to-state
967 (not (looking-at (concat outline-regexp
"[ \t]*"
968 org-clock-in-switch-to-state
970 (org-todo org-clock-in-switch-to-state
)))
971 (setq org-clock-heading-for-remember
972 (and (looking-at org-complex-heading-regexp
)
974 (org-trim (buffer-substring (match-end 1)
976 (setq org-clock-heading
977 (cond ((and org-clock-heading-function
978 (functionp org-clock-heading-function
))
979 (funcall org-clock-heading-function
))
980 ((looking-at org-complex-heading-regexp
)
981 (replace-regexp-in-string
982 "\\[\\[.*?\\]\\[\\(.*?\\)\\]\\]" "\\1"
985 (setq org-clock-heading
(org-propertize org-clock-heading
987 (org-clock-find-position org-clock-in-resume
)
989 ((and org-clock-in-resume
991 (concat "^[ \t]* " org-clock-string
992 " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
993 " +\\sw+\.? +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")))
994 (message "Matched %s" (match-string 1))
995 (setq ts
(concat "[" (match-string 1) "]"))
996 (goto-char (match-end 1))
997 (setq org-clock-start-time
999 (org-parse-time-string (match-string 1))))
1000 (setq org-clock-effort
(org-get-effort))
1001 (setq org-clock-total-time
(org-clock-sum-current-item
1002 (org-clock-get-sum-start))))
1003 ((eq org-clock-in-resume
'auto-restart
)
1004 ;; called from org-clock-load during startup,
1005 ;; do not interrupt, but warn!
1006 (message "Cannot restart clock because task does not contain unfinished clock")
1011 (insert-before-markers "\n")
1013 (org-indent-line-function)
1014 (when (and (save-excursion
1017 (beginning-of-line 1)
1018 (org-indent-line-to (- (org-get-indentation) 2)))
1019 (insert org-clock-string
" ")
1020 (setq org-clock-effort
(org-get-effort))
1021 (setq org-clock-total-time
(org-clock-sum-current-item
1022 (org-clock-get-sum-start)))
1023 (setq org-clock-start-time
1027 "You stopped another clock %d mins ago; start this one from then? "
1028 (/ (- (org-float-time (current-time))
1029 (org-float-time left-over
)) 60)))
1032 (setq ts
(org-insert-time-stamp org-clock-start-time
1033 'with-hm
'inactive
))))
1034 (move-marker org-clock-marker
(point) (buffer-base-buffer))
1035 (move-marker org-clock-hd-marker
1036 (save-excursion (org-back-to-heading t
) (point))
1037 (buffer-base-buffer))
1038 (setq org-clock-has-been-used t
)
1039 (or global-mode-string
(setq global-mode-string
'("")))
1040 (or (memq 'org-mode-line-string global-mode-string
)
1041 (setq global-mode-string
1042 (append global-mode-string
'(org-mode-line-string))))
1043 (org-clock-update-mode-line)
1044 (when org-clock-mode-line-timer
1045 (cancel-timer org-clock-mode-line-timer
)
1046 (setq org-clock-mode-line-timer nil
))
1047 (setq org-clock-mode-line-timer
1048 (run-with-timer org-clock-update-period
1049 org-clock-update-period
1050 'org-clock-update-mode-line
))
1051 (when org-clock-idle-timer
1052 (cancel-timer org-clock-idle-timer
)
1053 (setq org-clock-idle-timer nil
))
1054 (setq org-clock-idle-timer
1055 (run-with-timer 60 60 'org-resolve-clocks-if-idle
))
1056 (message "Clock starts at %s - %s" ts msg-extra
)
1057 (run-hooks 'org-clock-in-hook
)))))))
1059 (defvar org-clock-current-task nil
1060 "Task currently clocked in.")
1061 (defun org-clock-set-current ()
1062 "Set `org-clock-current-task' to the task currently clocked in."
1063 (setq org-clock-current-task
(org-get-heading)))
1064 (defun org-clock-delete-current ()
1065 "Reset `org-clock-current-task' to nil."
1066 (setq org-clock-current-task nil
))
1068 (defun org-clock-mark-default-task ()
1069 "Mark current task as default task."
1072 (org-back-to-heading t
)
1073 (move-marker org-clock-default-task
(point))))
1076 (defun org-clock-get-sum-start ()
1077 "Return the time from which clock times should be counted.
1078 This is for the currently running clock as it is displayed
1079 in the mode line. This function looks at the properties
1080 LAST_REPEAT and in particular CLOCK_MODELINE_TOTAL and the
1081 corresponding variable `org-clock-modeline-total' and then
1082 decides which time to use."
1083 (let ((cmt (or (org-entry-get nil
"CLOCK_MODELINE_TOTAL")
1084 (symbol-name org-clock-modeline-total
)))
1085 (lr (org-entry-get nil
"LAST_REPEAT")))
1087 ((equal cmt
"current")
1088 (setq msg-extra
"showing time in current clock instance")
1090 ((equal cmt
"today")
1091 (setq msg-extra
"showing today's task time.")
1092 (let* ((dt (decode-time (current-time))))
1093 (setq dt
(append (list 0 0 0) (nthcdr 3 dt
)))
1094 (if org-extend-today-until
1095 (setf (nth 2 dt
) org-extend-today-until
))
1096 (apply 'encode-time dt
)))
1097 ((or (equal cmt
"all")
1098 (and (or (not cmt
) (equal cmt
"auto"))
1100 (setq msg-extra
"showing entire task time.")
1102 ((or (equal cmt
"repeat")
1103 (and (or (not cmt
) (equal cmt
"auto"))
1105 (setq msg-extra
"showing task time since last repeat.")
1108 (org-time-string-to-time lr
)))
1111 (defun org-clock-find-position (find-unclosed)
1112 "Find the location where the next clock line should be inserted.
1113 When FIND-UNCLOSED is non-nil, first check if there is an unclosed clock
1114 line and position cursor in that line."
1115 (org-back-to-heading t
)
1117 (let ((beg (save-excursion
1118 (beginning-of-line 2)
1119 (or (bolp) (newline))
1121 (end (progn (outline-next-heading) (point)))
1122 (re (concat "^[ \t]*" org-clock-string
))
1124 (drawer (if (stringp org-clock-into-drawer
)
1125 org-clock-into-drawer
"LOGBOOK"))
1126 first last ind-last
)
1128 (when (and find-unclosed
1130 (concat "^[ \t]* " org-clock-string
1131 " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
1132 " +\\sw+ +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")
1134 (beginning-of-line 1)
1136 (when (eobp) (newline) (setq end
(max (point) end
)))
1137 (when (re-search-forward (concat "^[ \t]*:" drawer
":") end t
)
1138 ;; we seem to have a CLOCK drawer, so go there.
1139 (beginning-of-line 2)
1140 (or org-log-states-order-reversed
1141 (and (re-search-forward org-property-end-re nil t
)
1142 (goto-char (match-beginning 0))))
1144 ;; Lets count the CLOCK lines
1146 (while (re-search-forward re end t
)
1147 (setq first
(or first
(match-beginning 0))
1148 last
(match-beginning 0)
1150 (when (and (integerp org-clock-into-drawer
)
1152 (>= (1+ cnt
) org-clock-into-drawer
))
1153 ;; Wrap current entries into a new drawer
1155 (setq ind-last
(org-get-indentation))
1156 (beginning-of-line 2)
1157 (if (and (>= (org-get-indentation) ind-last
)
1161 (beginning-of-line 0)
1162 (org-indent-line-to ind-last
)
1164 (insert ":" drawer
":\n")
1165 (beginning-of-line 0)
1166 (org-indent-line-function)
1168 (beginning-of-line 2)
1169 (or org-log-states-order-reversed
1170 (and (re-search-forward org-property-end-re nil t
)
1171 (goto-char (match-beginning 0))))
1175 (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
1176 (not (equal (match-string 1) org-clock-string
)))
1177 ;; Planning info, skip to after it
1178 (beginning-of-line 2)
1179 (or (bolp) (newline)))
1180 (when (or (eq org-clock-into-drawer t
)
1181 (stringp org-clock-into-drawer
)
1182 (and (integerp org-clock-into-drawer
)
1183 (< org-clock-into-drawer
2)))
1184 (insert ":" drawer
":\n:END:\n")
1185 (beginning-of-line -
1)
1186 (org-indent-line-function)
1188 (beginning-of-line 2)
1189 (org-indent-line-function)
1191 (or org-log-states-order-reversed
1192 (and (re-search-forward org-property-end-re nil t
)
1193 (goto-char (match-beginning 0))))))))
1195 (defun org-clock-out (&optional fail-quietly at-time
)
1196 "Stop the currently running clock.
1197 If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
1200 (if (not (org-clocking-p))
1201 (if fail-quietly
(throw 'exit t
) (error "No active clock")))
1202 (let (ts te s h m remove
)
1204 (set-buffer (org-clocking-buffer))
1207 (goto-char org-clock-marker
)
1208 (beginning-of-line 1)
1209 (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
1210 (equal (match-string 1) org-clock-string
))
1211 (setq ts
(match-string 2))
1212 (if fail-quietly
(throw 'exit nil
) (error "Clock start time is gone")))
1213 (goto-char (match-end 0))
1214 (delete-region (point) (point-at-eol))
1216 (setq te
(org-insert-time-stamp (or at-time
(current-time))
1217 'with-hm
'inactive
))
1218 (setq s
(- (org-float-time (apply 'encode-time
(org-parse-time-string te
)))
1219 (org-float-time (apply 'encode-time
(org-parse-time-string ts
))))
1220 h
(floor (/ s
3600))
1224 (insert " => " (format "%2d:%02d" h m
))
1225 (when (setq remove
(and org-clock-out-remove-zero-time-clocks
1227 (beginning-of-line 1)
1228 (delete-region (point) (point-at-eol))
1229 (and (looking-at "\n") (> (point-max) (1+ (point)))
1231 (move-marker org-clock-marker nil
)
1232 (move-marker org-clock-hd-marker nil
)
1233 (when org-log-note-clock-out
1234 (org-add-log-setup 'clock-out nil nil nil nil
1235 (concat "# Task: " (org-get-heading t
) "\n\n")))
1236 (when org-clock-mode-line-timer
1237 (cancel-timer org-clock-mode-line-timer
)
1238 (setq org-clock-mode-line-timer nil
))
1239 (when org-clock-idle-timer
1240 (cancel-timer org-clock-idle-timer
)
1241 (setq org-clock-idle-timer nil
))
1242 (setq global-mode-string
1243 (delq 'org-mode-line-string global-mode-string
))
1244 (when org-clock-out-switch-to-state
1246 (org-back-to-heading t
)
1247 (let ((org-inhibit-logging t
)
1248 (org-clock-out-when-done nil
))
1250 ((functionp org-clock-out-switch-to-state
)
1251 (looking-at org-complex-heading-regexp
)
1252 (let ((newstate (funcall org-clock-out-switch-to-state
1254 (if newstate
(org-todo newstate
))))
1255 ((and org-clock-out-switch-to-state
1256 (not (looking-at (concat outline-regexp
"[ \t]*"
1257 org-clock-out-switch-to-state
1259 (org-todo org-clock-out-switch-to-state
))))))
1260 (force-mode-line-update)
1261 (message (concat "Clock stopped at %s after HH:MM = " org-time-clocksum-format
"%s") te h m
1262 (if remove
" => LINE REMOVED" ""))
1263 (run-hooks 'org-clock-out-hook
)
1264 (org-clock-delete-current))))))
1266 (defun org-clock-cancel ()
1267 "Cancel the running clock be removing the start timestamp."
1269 (if (not (org-clocking-p))
1270 (error "No active clock"))
1272 (set-buffer (org-clocking-buffer))
1273 (goto-char org-clock-marker
)
1274 (delete-region (1- (point-at-bol)) (point-at-eol))
1275 ;; Just in case, remove any empty LOGBOOK left over
1276 (org-remove-empty-drawer-at "LOGBOOK" (point)))
1277 (move-marker org-clock-marker nil
)
1278 (move-marker org-clock-hd-marker nil
)
1279 (setq global-mode-string
1280 (delq 'org-mode-line-string global-mode-string
))
1281 (force-mode-line-update)
1282 (message "Clock canceled")
1283 (run-hooks 'org-clock-cancel-hook
))
1285 (defun org-clock-goto (&optional select
)
1286 "Go to the currently clocked-in entry, or to the most recently clocked one.
1287 With prefix arg SELECT, offer recently clocked tasks for selection."
1292 (or (org-clock-select-task "Select task to go to: ")
1293 (error "No task selected")))
1294 ((org-clocking-p) org-clock-marker
)
1295 ((and org-clock-goto-may-find-recent-task
1296 (car org-clock-history
)
1297 (marker-buffer (car org-clock-history
)))
1299 (car org-clock-history
))
1300 (t (error "No active or recent clock task")))))
1301 (switch-to-buffer (marker-buffer m
))
1302 (if (or (< m
(point-min)) (> m
(point-max))) (widen))
1305 (org-back-to-heading t
)
1306 (org-cycle-hide-drawers 'children
)
1310 (message "No running clock, this is the most recently clocked task"))
1311 (run-hooks 'org-clock-goto-hook
)))
1313 (defvar org-clock-file-total-minutes nil
1314 "Holds the file total time in minutes, after a call to `org-clock-sum'.")
1315 (make-variable-buffer-local 'org-clock-file-total-minutes
)
1317 (defun org-clock-sum (&optional tstart tend
)
1318 "Sum the times for each subtree.
1319 Puts the resulting times in minutes as a text property on each headline.
1320 TSTART and TEND can mark a time range to be considered."
1322 (let* ((bmp (buffer-modified-p))
1323 (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
1325 "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
1327 (ltimes (make-vector lmax
0))
1332 (if (stringp tstart
) (setq tstart
(org-time-string-to-seconds tstart
)))
1333 (if (stringp tend
) (setq tend
(org-time-string-to-seconds tend
)))
1334 (if (consp tstart
) (setq tstart
(org-float-time tstart
)))
1335 (if (consp tend
) (setq tend
(org-float-time tend
)))
1336 (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t
))
1338 (goto-char (point-max))
1339 (while (re-search-backward re nil t
)
1343 (setq ts
(match-string 2)
1346 (apply 'encode-time
(org-parse-time-string ts
)))
1348 (apply 'encode-time
(org-parse-time-string te
)))
1349 ts
(if tstart
(max ts tstart
) ts
)
1350 te
(if tend
(min te tend
) te
)
1352 t1
(if (> dt
0) (+ t1
(floor (/ dt
60))) t1
)))
1355 (setq t1
(+ t1
(string-to-number (match-string 5))
1356 (* 60 (string-to-number (match-string 4))))))
1358 ;; Add the currently clocking item time to the total
1359 (when (and org-clock-report-include-clocking-task
1360 (equal (org-clocking-buffer) (current-buffer))
1361 (equal (marker-position org-clock-hd-marker
) (point)))
1362 (let ((time (floor (- (org-float-time)
1363 (org-float-time org-clock-start-time
)) 60)))
1364 (setq t1
(+ t1 time
))))
1365 (setq level
(- (match-end 1) (match-beginning 1)))
1366 (when (or (> t1
0) (> (aref ltimes level
) 0))
1367 (loop for l from
0 to level do
1368 (aset ltimes l
(+ (aref ltimes l
) t1
)))
1369 (setq t1
0 time
(aref ltimes level
))
1370 (loop for l from level to
(1- lmax
) do
1372 (goto-char (match-beginning 0))
1373 (put-text-property (point) (point-at-eol) :org-clock-minutes time
)))))
1374 (setq org-clock-file-total-minutes
(aref ltimes
0)))
1375 (set-buffer-modified-p bmp
)))
1377 (defun org-clock-sum-current-item (&optional tstart
)
1378 "Returns time, clocked on current item in total"
1381 (org-narrow-to-subtree)
1382 (org-clock-sum tstart
)
1383 org-clock-file-total-minutes
)))
1385 (defun org-clock-display (&optional total-only
)
1386 "Show subtree times in the entire buffer.
1387 If TOTAL-ONLY is non-nil, only show the total time for the entire file
1390 (org-clock-remove-overlays)
1395 (goto-char (point-min))
1396 (while (or (and (equal (setq p
(point)) (point-min))
1397 (get-text-property p
:org-clock-minutes
))
1398 (setq p
(next-single-property-change
1399 (point) :org-clock-minutes
)))
1401 (when (setq time
(get-text-property p
:org-clock-minutes
))
1402 (org-clock-put-overlay time
(funcall outline-level
))))
1403 (setq h
(/ org-clock-file-total-minutes
60)
1404 m
(- org-clock-file-total-minutes
(* 60 h
)))
1405 ;; Arrange to remove the overlays upon next change.
1406 (when org-remove-highlights-with-change
1407 (org-add-hook 'before-change-functions
'org-clock-remove-overlays
1409 (if org-time-clocksum-use-fractional
1410 (message (concat "Total file time: " org-time-clocksum-fractional-format
1411 " (%d hours and %d minutes)")
1412 (/ (+ (* h
60.0) m
) 60.0) h m
)
1413 (message (concat "Total file time: " org-time-clocksum-format
1414 " (%d hours and %d minutes)") h m h m
))))
1416 (defvar org-clock-overlays nil
)
1417 (make-variable-buffer-local 'org-clock-overlays
)
1419 (defun org-clock-put-overlay (time &optional level
)
1420 "Put an overlays on the current line, displaying TIME.
1421 If LEVEL is given, prefix time with a corresponding number of stars.
1422 This creates a new overlay and stores it in `org-clock-overlays', so that it
1423 will be easy to remove."
1424 (let* ((c 60) (h (floor (/ time
60))) (m (- time
(* 60 h
)))
1425 (l (if level
(org-get-valid-level level
0) 0))
1426 (fmt (concat "%s " (if org-time-clocksum-use-fractional
1427 org-time-clocksum-fractional-format
1428 org-time-clocksum-format
) "%s"))
1431 (org-move-to-column c
)
1432 (unless (eolp) (skip-chars-backward "^ \t"))
1433 (skip-chars-backward " \t")
1434 (setq ov
(org-make-overlay (1- (point)) (point-at-eol))
1435 tx
(concat (buffer-substring (1- (point)) (point))
1436 (make-string (+ off
(max 0 (- c
(current-column)))) ?.
)
1437 (org-add-props (if org-time-clocksum-use-fractional
1440 (/ (+ (* h
60.0) m
) 60.0)
1441 (make-string (- 16 l
) ?\
))
1443 (make-string l ?
*) h m
1444 (make-string (- 16 l
) ?\
)))
1445 (list 'face
'org-clock-overlay
))
1447 (if (not (featurep 'xemacs
))
1448 (org-overlay-put ov
'display tx
)
1449 (org-overlay-put ov
'invisible t
)
1450 (org-overlay-put ov
'end-glyph
(make-glyph tx
)))
1451 (push ov org-clock-overlays
)))
1453 (defun org-clock-remove-overlays (&optional beg end noremove
)
1454 "Remove the occur highlights from the buffer.
1455 BEG and END are ignored. If NOREMOVE is nil, remove this function
1456 from the `before-change-functions' in the current buffer."
1458 (unless org-inhibit-highlight-removal
1459 (mapc 'org-delete-overlay org-clock-overlays
)
1460 (setq org-clock-overlays nil
)
1462 (remove-hook 'before-change-functions
1463 'org-clock-remove-overlays
'local
))))
1465 (defvar state
) ;; dynamically scoped into this function
1466 (defun org-clock-out-if-current ()
1467 "Clock out if the current entry contains the running clock.
1468 This is used to stop the clock after a TODO entry is marked DONE,
1469 and is only done if the variable `org-clock-out-when-done' is not nil."
1470 (when (and org-clock-out-when-done
1471 (or (and (eq t org-clock-out-when-done
)
1472 (member state org-done-keywords
))
1473 (and (listp org-clock-out-when-done
)
1474 (member state org-clock-out-when-done
)))
1475 (equal (or (buffer-base-buffer (org-clocking-buffer))
1476 (org-clocking-buffer))
1477 (or (buffer-base-buffer (current-buffer))
1479 (< (point) org-clock-marker
)
1480 (> (save-excursion (outline-next-heading) (point))
1482 ;; Clock out, but don't accept a logging message for this.
1483 (let ((org-log-note-clock-out nil
)
1484 (org-clock-out-switch-to-state nil
))
1487 (add-hook 'org-after-todo-state-change-hook
1488 'org-clock-out-if-current
)
1491 (defun org-get-clocktable (&rest props
)
1492 "Get a formatted clocktable with parameters according to PROPS.
1493 The table is created in a temporary buffer, fully formatted and
1494 fontified, and then returned."
1496 (setq props
(plist-put props
:name
"clocktable"))
1497 (unless (plist-member props
:maxlevel
)
1498 (setq props
(plist-put props
:maxlevel
2)))
1499 (unless (plist-member props
:scope
)
1500 (setq props
(plist-put props
:scope
'agenda
)))
1503 (org-create-dblock props
)
1505 (font-lock-fontify-buffer)
1507 (buffer-substring (point) (progn
1508 (re-search-forward "^#\\+END" nil t
)
1511 (defun org-clock-report (&optional arg
)
1512 "Create a table containing a report about clocked time.
1513 If the cursor is inside an existing clocktable block, then the table
1514 will be updated. If not, a new clocktable will be inserted.
1515 When called with a prefix argument, move to the first clock table in the
1516 buffer and update it."
1518 (org-clock-remove-overlays)
1520 (org-find-dblock "clocktable")
1522 (if (org-in-clocktable-p)
1523 (goto-char (org-in-clocktable-p))
1524 (org-create-dblock (append (list :name
"clocktable")
1525 org-clock-clocktable-default-properties
)))
1526 (org-update-dblock))
1528 (defun org-in-clocktable-p ()
1529 "Check if the cursor is in a clocktable."
1530 (let ((pos (point)) start
)
1533 (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t
)
1534 (setq start
(match-beginning 0))
1535 (re-search-forward "^#\\+END:.*" nil t
)
1536 (>= (match-end 0) pos
)
1539 (defun org-clock-special-range (key &optional time as-strings
)
1540 "Return two times bordering a special time range.
1541 Key is a symbol specifying the range and can be one of `today', `yesterday',
1542 `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
1543 A week starts Monday 0:00 and ends Sunday 24:00.
1544 The range is determined relative to TIME. TIME defaults to the current time.
1545 The return value is a cons cell with two internal times like the ones
1546 returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
1547 the returned times will be formatted strings."
1548 (if (integerp key
) (setq key
(intern (number-to-string key
))))
1549 (let* ((tm (decode-time (or time
(current-time))))
1550 (s 0) (m (nth 1 tm
)) (h (nth 2 tm
))
1551 (d (nth 3 tm
)) (month (nth 4 tm
)) (y (nth 5 tm
))
1553 (skey (symbol-name key
))
1555 s1 m1 h1 d1 month1 y1 diff ts te fm txt w date
)
1557 ((string-match "^[0-9]+$" skey
)
1558 (setq y
(string-to-number skey
) m
1 d
1 key
'year
))
1559 ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)$" skey
)
1560 (setq y
(string-to-number (match-string 1 skey
))
1561 month
(string-to-number (match-string 2 skey
))
1563 ((string-match "^\\([0-9]+\\)-[wW]\\([0-9]\\{1,2\\}\\)$" skey
)
1565 (setq y
(string-to-number (match-string 1 skey
))
1566 w
(string-to-number (match-string 2 skey
)))
1567 (setq date
(calendar-gregorian-from-absolute
1568 (calendar-absolute-from-iso (list w
1 y
))))
1569 (setq d
(nth 1 date
) month
(car date
) y
(nth 2 date
)
1572 ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)-\\([0-9]\\{1,2\\}\\)$" skey
)
1573 (setq y
(string-to-number (match-string 1 skey
))
1574 month
(string-to-number (match-string 2 skey
))
1575 d
(string-to-number (match-string 3 skey
))
1577 ((string-match "\\([-+][0-9]+\\)$" skey
)
1578 (setq shift
(string-to-number (match-string 1 skey
))
1579 key
(intern (substring skey
0 (match-beginning 1))))))
1581 (cond ((eq key
'yesterday
) (setq key
'today shift -
1))
1582 ((eq key
'lastweek
) (setq key
'week shift -
1))
1583 ((eq key
'lastmonth
) (setq key
'month shift -
1))
1584 ((eq key
'lastyear
) (setq key
'year shift -
1))))
1586 ((memq key
'(day today
))
1587 (setq d
(+ d shift
) h
0 m
0 h1
24 m1
0))
1588 ((memq key
'(week thisweek
))
1589 (setq diff
(+ (* -
7 shift
) (if (= dow
0) 6 (1- dow
)))
1590 m
0 h
0 d
(- d diff
) d1
(+ 7 d
)))
1591 ((memq key
'(month thismonth
))
1592 (setq d
1 h
0 m
0 d1
1 month
(+ month shift
) month1
(1+ month
) h1
0 m1
0))
1593 ((memq key
'(year thisyear
))
1594 (setq m
0 h
0 d
1 month
1 y
(+ y shift
) y1
(1+ y
)))
1595 (t (error "No such time block %s" key
)))
1596 (setq ts
(encode-time s m h d month y
)
1597 te
(encode-time (or s1 s
) (or m1 m
) (or h1 h
)
1598 (or d1 d
) (or month1 month
) (or y1 y
)))
1599 (setq fm
(cdr org-time-stamp-formats
))
1601 ((memq key
'(day today
))
1602 (setq txt
(format-time-string "%A, %B %d, %Y" ts
)))
1603 ((memq key
'(week thisweek
))
1604 (setq txt
(format-time-string "week %G-W%V" ts
)))
1605 ((memq key
'(month thismonth
))
1606 (setq txt
(format-time-string "%B %Y" ts
)))
1607 ((memq key
'(year thisyear
))
1608 (setq txt
(format-time-string "the year %Y" ts
))))
1610 (list (format-time-string fm ts
) (format-time-string fm te
) txt
)
1613 (defun org-clocktable-shift (dir n
)
1614 "Try to shift the :block date of the clocktable at point.
1615 Point must be in the #+BEGIN: line of a clocktable, or this function
1616 will throw an error.
1617 DIR is a direction, a symbol `left', `right', `up', or `down'.
1618 Both `left' and `down' shift the block toward the past, `up' and `right'
1619 push it toward the future.
1620 N is the number of shift steps to take. The size of the step depends on
1621 the currently selected interval size."
1622 (setq n
(prefix-numeric-value n
))
1623 (and (memq dir
'(left down
)) (setq n
(- n
)))
1625 (goto-char (point-at-bol))
1626 (if (not (looking-at "#\\+BEGIN: clocktable\\>.*?:block[ \t]+\\(\\S-+\\)"))
1627 (error "Line needs a :block definition before this command works")
1628 (let* ((b (match-beginning 1)) (e (match-end 1))
1629 (s (match-string 1))
1630 block shift ins y mw d date wp m
)
1632 ((equal s
"yesterday") (setq s
"today-1"))
1633 ((equal s
"lastweek") (setq s
"thisweek-1"))
1634 ((equal s
"lastmonth") (setq s
"thismonth-1"))
1635 ((equal s
"lastyear") (setq s
"thisyear-1")))
1637 ((string-match "^\\(today\\|thisweek\\|thismonth\\|thisyear\\)\\([-+][0-9]+\\)?$" s
)
1638 (setq block
(match-string 1 s
)
1639 shift
(if (match-end 2)
1640 (string-to-number (match-string 2 s
))
1642 (setq shift
(+ shift n
))
1643 (setq ins
(if (= shift
0) block
(format "%s%+d" block shift
))))
1644 ((string-match "\\([0-9]+\\)\\(-\\([wW]?\\)\\([0-9]\\{1,2\\}\\)\\(-\\([0-9]\\{1,2\\}\\)\\)?\\)?" s
)
1645 ;; 1 1 2 3 3 4 4 5 6 6 5 2
1646 (setq y
(string-to-number (match-string 1 s
))
1647 wp
(and (match-end 3) (match-string 3 s
))
1648 mw
(and (match-end 4) (string-to-number (match-string 4 s
)))
1649 d
(and (match-end 6) (string-to-number (match-string 6 s
))))
1651 (d (setq ins
(format-time-string
1653 (encode-time 0 0 0 (+ d n
) m y
))))
1654 ((and wp mw
(> (length wp
) 0))
1656 (setq date
(calendar-gregorian-from-absolute (calendar-absolute-from-iso (list (+ mw n
) 1 y
))))
1657 (setq ins
(format-time-string
1659 (encode-time 0 0 0 (nth 1 date
) (car date
) (nth 2 date
)))))
1661 (setq ins
(format-time-string
1663 (encode-time 0 0 0 1 (+ mw n
) y
))))
1665 (setq ins
(number-to-string (+ y n
))))))
1666 (t (error "Cannot shift clocktable block")))
1670 (delete-region (point) (+ (point) (- e b
)))
1671 (beginning-of-line 1)
1675 (defun org-dblock-write:clocktable
(params)
1676 "Write the standard clocktable."
1678 (let* ((hlchars '((1 .
"*") (2 .
"/")))
1681 (scope (plist-get params
:scope
))
1682 (tostring (plist-get params
:tostring
))
1683 (multifile (plist-get params
:multifile
))
1684 (header (plist-get params
:header
))
1685 (maxlevel (or (plist-get params
:maxlevel
) 3))
1686 (step (plist-get params
:step
))
1687 (emph (plist-get params
:emphasize
))
1688 (timestamp (plist-get params
:timestamp
))
1689 (ts (plist-get params
:tstart
))
1690 (te (plist-get params
:tend
))
1691 (block (plist-get params
:block
))
1692 (link (plist-get params
:link
))
1693 ipos time p level hlc hdl tsp props content recalc formula pcol
1694 cc beg end pos tbl tbl1 range-text rm-file-column scope-is-list st
)
1695 (setq org-clock-file-total-minutes nil
)
1697 (unless (or block
(and ts te
))
1698 (error "Clocktable `:step' can only be used with `:block' or `:tstart,:end'"))
1699 (org-clocktable-steps params
)
1702 (setq cc
(org-clock-special-range block nil t
)
1703 ts
(car cc
) te
(nth 1 cc
) range-text
(nth 2 cc
)))
1704 (when (integerp ts
) (setq ts
(calendar-gregorian-from-absolute ts
)))
1705 (when (integerp te
) (setq te
(calendar-gregorian-from-absolute te
)))
1706 (when (and ts
(listp ts
))
1707 (setq ts
(format "%4d-%02d-%02d" (nth 2 ts
) (car ts
) (nth 1 ts
))))
1708 (when (and te
(listp te
))
1709 (setq te
(format "%4d-%02d-%02d" (nth 2 te
) (car te
) (nth 1 te
))))
1710 ;; Now the times are strings we can parse.
1711 (if ts
(setq ts
(org-float-time
1712 (apply 'encode-time
(org-parse-time-string ts
)))))
1713 (if te
(setq te
(org-float-time
1714 (apply 'encode-time
(org-parse-time-string te
)))))
1715 (move-marker ins
(point))
1718 ;; Get the right scope
1721 ((and scope
(listp scope
) (symbolp (car scope
)))
1722 (setq scope
(eval scope
)))
1724 (setq scope
(org-agenda-files t
)))
1725 ((eq scope
'agenda-with-archives
)
1726 (setq scope
(org-agenda-files t
))
1727 (setq scope
(org-add-archive-files scope
)))
1728 ((eq scope
'file-with-archives
)
1729 (setq scope
(org-add-archive-files (list (buffer-file-name)))
1731 (setq scope-is-list
(and scope
(listp scope
)))
1735 ((eq scope
'file
) (widen))
1736 ((eq scope
'subtree
) (org-narrow-to-subtree))
1738 (while (org-up-heading-safe))
1739 (org-narrow-to-subtree))
1740 ((and (symbolp scope
) (string-match "^tree\\([0-9]+\\)$"
1741 (symbol-name scope
)))
1742 (setq level
(string-to-number (match-string 1 (symbol-name scope
))))
1744 (while (org-up-heading-safe)
1745 (looking-at outline-regexp
)
1746 (if (<= (org-reduced-level (funcall outline-level
)) level
)
1747 (throw 'exit nil
))))
1748 (org-narrow-to-subtree))
1750 (let* ((files scope
)
1752 (p1 (copy-sequence params
))
1754 (setq p1
(plist-put p1
:tostring t
))
1755 (setq p1
(plist-put p1
:multifile t
))
1756 (setq p1
(plist-put p1
:scope
'file
))
1757 (org-prepare-agenda-buffers files
)
1758 (while (setq file
(pop files
))
1759 (with-current-buffer (find-buffer-visiting file
)
1760 (setq tbl1
(org-dblock-write:clocktable p1
))
1762 (push (org-clocktable-add-file
1764 (concat "| |*File time*|*"
1765 (org-minutes-to-hh:mm-string
1766 org-clock-file-total-minutes
)
1769 (setq total-time
(+ (or total-time
0)
1770 org-clock-file-total-minutes
))))))))
1773 (unless scope-is-list
1774 (org-clock-sum ts te
)
1775 (goto-char (point-min))
1777 (while (or (and (bobp) (prog1 st
(setq st nil
))
1778 (get-text-property (point) :org-clock-minutes
)
1779 (setq p
(point-min)))
1780 (setq p
(next-single-property-change (point) :org-clock-minutes
)))
1782 (when (setq time
(get-text-property p
:org-clock-minutes
))
1784 (beginning-of-line 1)
1785 (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
1786 (setq level
(org-reduced-level
1787 (- (match-end 1) (match-beginning 1))))
1788 (<= level maxlevel
))
1789 (setq hlc
(if emph
(or (cdr (assoc level hlchars
)) "") "")
1792 (org-make-link-string
1793 (format "file:%s::%s"
1796 (org-make-org-heading-search-string
1800 (setq props
(org-entry-properties (point)))
1801 (or (cdr (assoc "SCHEDULED" props
))
1802 (cdr (assoc "TIMESTAMP" props
))
1803 (cdr (assoc "DEADLINE" props
))
1804 (cdr (assoc "TIMESTAMP_IA" props
)))))
1805 (if (and (not multifile
) (= level
1)) (push "|-" tbl
))
1807 "| " (int-to-string level
) "|"
1808 (if timestamp
(concat tsp
"|") "")
1810 (make-string (1- level
) ?|
)
1811 hlc
(org-minutes-to-hh:mm-string time
) hlc
1813 (setq tbl
(nreverse tbl
))
1815 (if tbl
(mapconcat 'identity tbl
"\n") nil
)
1817 (insert-before-markers
1820 "Clock summary at ["
1822 (format-time-string (cdr org-time-stamp-formats
))
1825 (if block
(concat ", for " range-text
".") "")
1827 (if scope-is-list
"|File" "")
1828 "|L|" (if timestamp
"Timestamp|" "") "Headline|Time|\n")
1829 (setq total-time
(or total-time org-clock-file-total-minutes
))
1830 (insert-before-markers
1832 (if scope-is-list
"|" "")
1833 (if timestamp
"|Timestamp|" "|")
1835 (org-minutes-to-hh:mm-string
(or total-time
0))
1837 (setq tbl
(delq nil tbl
))
1838 (if (and (stringp (car tbl
)) (> (length (car tbl
)) 1)
1839 (equal (substring (car tbl
) 0 2) "|-"))
1841 (insert-before-markers (mapconcat
1842 'identity
(delq nil tbl
)
1843 (if scope-is-list
"\n|-\n" "\n")))
1844 (backward-delete-char 1)
1845 (if (setq formula
(plist-get params
:formula
))
1848 (setq pcol
(+ (if scope-is-list
1 0) maxlevel
3))
1851 "\n#+TBLFM: $%d='(org-clock-time%% @%d$%d $%d..$%d);%%.1f"
1854 (+ 3 (if scope-is-list
1 0))
1855 (+ (if scope-is-list
1 0) 3)
1859 (insert "\n#+TBLFM: " formula
)
1861 (t (error "invalid formula in clocktable")))
1862 ;; Should we rescue an old formula?
1863 (when (stringp (setq content
(plist-get params
:content
)))
1864 (when (string-match "^\\([ \t]*#\\+TBLFM:.*\\)" content
)
1866 (insert "\n" (match-string 1 (plist-get params
:content
)))
1867 (beginning-of-line 0))))
1869 (skip-chars-forward "^|")
1873 (save-excursion (org-table-goto-column pcol nil
'force
)
1875 (org-table-recalculate 'all
))
1876 (when rm-file-column
1878 (org-table-delete-column)))))))
1880 (defun org-clocktable-steps (params)
1881 (let* ((p1 (copy-sequence params
))
1882 (ts (plist-get p1
:tstart
))
1883 (te (plist-get p1
:tend
))
1884 (step0 (plist-get p1
:step
))
1885 (step (cdr (assoc step0
'((day .
86400) (week .
604800)))))
1886 (block (plist-get p1
:block
))
1889 (setq cc
(org-clock-special-range block nil t
)
1890 ts
(car cc
) te
(nth 1 cc
) range-text
(nth 2 cc
)))
1891 (if ts
(setq ts
(org-float-time
1892 (apply 'encode-time
(org-parse-time-string ts
)))))
1893 (if te
(setq te
(org-float-time
1894 (apply 'encode-time
(org-parse-time-string te
)))))
1895 (setq p1
(plist-put p1
:header
""))
1896 (setq p1
(plist-put p1
:step nil
))
1897 (setq p1
(plist-put p1
:block nil
))
1899 (or (bolp) (insert "\n"))
1900 (setq p1
(plist-put p1
:tstart
(format-time-string
1901 (org-time-stamp-format nil t
)
1902 (seconds-to-time ts
))))
1903 (setq p1
(plist-put p1
:tend
(format-time-string
1904 (org-time-stamp-format nil t
)
1905 (seconds-to-time (setq ts
(+ ts step
))))))
1906 (insert "\n" (if (eq step0
'day
) "Daily report: " "Weekly report starting on: ")
1907 (plist-get p1
:tstart
) "\n")
1908 (org-dblock-write:clocktable p1
)
1909 (re-search-forward "#\\+END:")
1912 (defun org-clocktable-add-file (file table
)
1914 (let ((lines (org-split-string table
"\n"))
1915 (ff (file-name-nondirectory file
)))
1916 (mapconcat 'identity
1918 (if (string-match org-table-dataline-regexp x
)
1924 (defun org-clock-time%
(total &rest strings
)
1925 "Compute a time fraction in percent.
1926 TOTAL s a time string like 10:21 specifying the total times.
1927 STRINGS is a list of strings that should be checked for a time.
1928 The first string that does have a time will be used.
1929 This function is made for clock tables."
1930 (let ((re "\\([0-9]+\\):\\([0-9]+\\)")
1934 (if (not (string-match re total
))
1936 (setq tot
(+ (string-to-number (match-string 2 total
))
1937 (* 60 (string-to-number (match-string 1 total
)))))
1938 (if (= tot
0.
) (throw 'exit
0.
)))
1939 (while (setq s
(pop strings
))
1940 (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s
)
1942 (/ (* 100.0 (+ (string-to-number (match-string 2 s
))
1943 (* 60 (string-to-number (match-string 1 s
)))))
1947 (defvar org-clock-loaded nil
1948 "Was the clock file loaded?")
1950 (defun org-clock-save ()
1951 "Persist various clock-related data to disk.
1952 The details of what will be saved are regulated by the variable
1953 `org-clock-persist'."
1954 (when (and org-clock-persist
1955 (or org-clock-loaded
1956 org-clock-has-been-used
1957 (not (file-exists-p org-clock-persist-file
))))
1959 (with-current-buffer (find-file (expand-file-name org-clock-persist-file
))
1961 (delete-region (point-min) (point-max))
1963 (insert (format ";; org-persist.el - %s at %s\n"
1964 system-name
(format-time-string
1965 (cdr org-time-stamp-formats
))))
1966 (if (and (memq org-clock-persist
'(t clock
))
1967 (setq b
(org-clocking-buffer))
1968 (setq b
(or (buffer-base-buffer b
) b
))
1970 (buffer-file-name b
)
1971 (or (not org-clock-persist-query-save
)
1972 (y-or-n-p (concat "Save current clock ("
1973 (substring-no-properties org-clock-heading
)
1975 (insert "(setq resume-clock '(\""
1976 (buffer-file-name (org-clocking-buffer))
1977 "\" . " (int-to-string (marker-position org-clock-marker
))
1979 ;; Store clocked task history. Tasks are stored reversed to make
1981 (when (and (memq org-clock-persist
'(t history
))
1984 "(setq stored-clock-history '("
1987 (when (and (setq b
(marker-buffer m
))
1988 (setq b
(or (buffer-base-buffer b
) b
))
1990 (buffer-file-name b
))
1991 (concat "(\"" (buffer-file-name b
)
1992 "\" . " (int-to-string (marker-position m
))
1994 (reverse org-clock-history
) " ") "))\n"))
1996 (kill-buffer (current-buffer)))))))
1998 (defun org-clock-load ()
1999 "Load clock-related data from disk, maybe resuming a stored clock."
2000 (when (and org-clock-persist
(not org-clock-loaded
))
2001 (let ((filename (expand-file-name org-clock-persist-file
))
2002 (org-clock-in-resume 'auto-restart
)
2003 resume-clock stored-clock-history
)
2004 (if (not (file-readable-p filename
))
2005 (message "Not restoring clock data; %s not found"
2006 org-clock-persist-file
)
2007 (message "%s" "Restoring clock data")
2008 (setq org-clock-loaded t
)
2009 (load-file filename
)
2011 (when stored-clock-history
2012 (save-window-excursion
2013 (mapc (lambda (task)
2014 (if (file-exists-p (car task
))
2015 (org-clock-history-push (cdr task
)
2016 (find-file (car task
)))))
2017 stored-clock-history
)))
2019 (when (and resume-clock org-clock-persist
2020 (file-exists-p (car resume-clock
))
2021 (or (not org-clock-persist-query-resume
)
2025 (with-current-buffer (find-file (car resume-clock
))
2027 (goto-char (cdr resume-clock
))
2028 (org-back-to-heading t
)
2029 (and (looking-at org-complex-heading-regexp
)
2032 (when (file-exists-p (car resume-clock
))
2033 (with-current-buffer (find-file (car resume-clock
))
2034 (goto-char (cdr resume-clock
))
2035 (let ((org-clock-auto-clock-resolution nil
))
2037 (if (org-invisible-p)
2038 (org-show-context))))))))))
2041 (defun org-clock-persistence-insinuate ()
2042 "Set up hooks for clock persistence"
2043 (add-hook 'org-mode-hook
'org-clock-load
)
2044 (add-hook 'kill-emacs-hook
'org-clock-save
))
2046 ;; Suggested bindings
2047 (org-defkey org-mode-map
"\C-c\C-x\C-e" 'org-clock-modify-effort-estimate
)
2049 (provide 'org-clock
)
2051 ;; arch-tag: 7b42c5d4-9b36-48be-97c0-66a869daed4c
2053 ;;; org-clock.el ends here