1 ;;; org-clock.el --- The time clocking code for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
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
35 (declare-function calendar-absolute-from-iso
"cal-iso" (&optional date
))
36 (defvar org-time-stamp-formats
)
38 (defgroup org-clock nil
39 "Options concerning clocking working time in Org-mode."
43 (defcustom org-clock-into-drawer org-log-into-drawer
44 "Should clocking info be wrapped into a drawer?
45 When t, clocking info will always be inserted into a :LOGBOOK: drawer.
46 If necessary, the drawer will be created.
47 When nil, the drawer will not be created, but used when present.
48 When an integer and the number of clocking entries in an item
49 reaches or exceeds this number, a drawer will be created.
50 When a string, it names the drawer to be used.
52 The default for this variable is the value of `org-log-into-drawer',
57 (const :tag
"Always" t
)
58 (const :tag
"Only when drawer exists" nil
)
59 (integer :tag
"When at least N clock entries")
60 (const :tag
"Into LOGBOOK drawer" "LOGBOOK")
61 (string :tag
"Into Drawer named...")))
63 (defcustom org-clock-out-when-done t
64 "When non-nil, clock will be stopped when the clocked entry is marked DONE.
65 DONE here means any DONE-like state.
66 A nil value means clock will keep running until stopped explicitly with
67 `C-c C-x C-o', or until the clock is started in a different item.
68 Instead of t, this can also be a list of TODO states that should trigger
73 (const :tag
"Yes, when done" t
)
74 (repeat :tag
"State list"
75 (string :tag
"TODO keyword"))))
77 (defcustom org-clock-out-remove-zero-time-clocks nil
78 "Non-nil means remove the clock line when the resulting time is zero."
82 (defcustom org-clock-in-switch-to-state nil
83 "Set task to a special todo state while clocking it.
84 The value should be the state to which the entry should be
85 switched. If the value is a function, it must take one
86 parameter (the current TODO state of the item) and return the
87 state to switch it to."
91 (const :tag
"Don't force a state" nil
)
93 (symbol :tag
"Function")))
95 (defcustom org-clock-out-switch-to-state nil
96 "Set task to a special todo state after clocking out.
97 The value should be the state to which the entry should be
98 switched. If the value is a function, it must take one
99 parameter (the current TODO state of the item) and return the
100 state to switch it to."
104 (const :tag
"Don't force a state" nil
)
105 (string :tag
"State")
106 (symbol :tag
"Function")))
108 (defcustom org-clock-history-length
5
109 "Number of clock tasks to remember in history."
113 (defcustom org-clock-goto-may-find-recent-task t
114 "Non-nil means `org-clock-goto' can go to recent task if no active clock."
118 (defcustom org-clock-heading-function nil
119 "When non-nil, should be a function to create `org-clock-heading'.
120 This is the string shown in the mode line when a clock is running.
121 The function is called with point at the beginning of the headline."
125 (defcustom org-clock-string-limit
0
126 "Maximum length of clock strings in the modeline. 0 means no limit."
130 (defcustom org-clock-in-resume nil
131 "If non-nil, resume clock when clocking into task with open clock.
132 When clocking into a task with a clock entry which has not been closed,
133 the clock can be resumed from that point."
137 (defcustom org-clock-persist nil
138 "When non-nil, save the running clock when emacs is closed.
139 The clock is resumed when emacs restarts.
140 When this is t, both the running clock, and the entire clock
141 history are saved. When this is the symbol `clock', only the
142 running clock is saved.
144 When Emacs restarts with saved clock information, the file containing the
145 running clock as well as all files mentioned in the clock history will
147 All this depends on running `org-clock-persistence-insinuate' in .emacs"
150 (const :tag
"Just the running clock" clock
)
151 (const :tag
"Just the history" history
)
152 (const :tag
"Clock and history" t
)
153 (const :tag
"No persistence" nil
)))
155 (defcustom org-clock-persist-file
(convert-standard-filename
156 "~/.emacs.d/org-clock-save.el")
157 "File to save clock data to."
161 (defcustom org-clock-persist-query-save nil
162 "When non-nil, ask before saving the current clock on exit."
166 (defcustom org-clock-persist-query-resume t
167 "When non-nil, ask before resuming any stored clock during load."
171 (defcustom org-clock-sound nil
172 "Sound that will used for notifications.
176 t standard Emacs beep
177 file name play this sound file. If not possible, fall back to beep"
180 (const :tag
"No sound" nil
)
181 (const :tag
"Standard beep" t
)
182 (file :tag
"Play sound file")))
184 (defcustom org-clock-modeline-total
'auto
185 "Default setting for the time included for the modeline clock.
186 This can be overruled locally using the CLOCK_MODELINE_TOTAL property.
189 current Only the time in the current instance of the clock
190 today All time clocked into this task today
191 repeat All time clocked into this task since last repeat
192 all All time ever recorded for this task
193 auto Automatically, either `all', or `repeat' for repeating tasks"
196 (const :tag
"Current clock" current
)
197 (const :tag
"Today's task time" today
)
198 (const :tag
"Since last repeat" repeat
)
199 (const :tag
"All task time" all
)
200 (const :tag
"Automatically, `all' or since `repeat'" auto
)))
202 (defcustom org-task-overrun-text nil
203 "The extra modeline text that should indicate that the clock is overrun.
204 The can be nil to indicate that instead of adding text, the clock time
205 should get a different face (`org-mode-line-clock-overrun').
206 When this is a string, it is prepended to the clock string as an indication,
207 also using the face `org-mode-line-clock-overrun'."
210 (const :tag
"Just mark the time string" nil
)
211 (string :tag
"Text to prepend")))
213 (defcustom org-show-notification-handler nil
214 "Function or program to send notification with.
215 The function or program will be called with the notification
219 (string :tag
"Program")
220 (function :tag
"Function")))
222 (defcustom org-clock-clocktable-default-properties
'(:maxlevel
2 :scope file
)
223 "Default properties for new clocktables."
227 (defcustom org-clock-idle-time nil
228 "When non-nil, resolve open clocks if the user is idle more than X minutes."
231 (const :tag
"Never" nil
)
232 (integer :tag
"After N minutes")))
234 (defcustom org-clock-auto-clock-resolution
'when-no-clock-is-running
235 "When to automatically resolve open clocks found in Org buffers."
238 (const :tag
"Never" nil
)
239 (const :tag
"Always" t
)
240 (const :tag
"When no clock is running" when-no-clock-is-running
)))
242 (defcustom org-clock-report-include-clocking-task nil
243 "When non-nil, include the current clocking task time in clock reports."
247 (defvar org-clock-in-prepare-hook nil
248 "Hook run when preparing the clock.
249 This hook is run before anything happens to the task that
250 you want to clock in. For example, you can use this hook
251 to add an effort property.")
252 (defvar org-clock-in-hook nil
253 "Hook run when starting the clock.")
254 (defvar org-clock-out-hook nil
255 "Hook run when stopping the current clock.")
257 (defvar org-clock-cancel-hook nil
258 "Hook run when cancelling the current clock.")
259 (defvar org-clock-goto-hook nil
260 "Hook run when selecting the currently clocked-in entry.")
261 (defvar org-clock-has-been-used nil
262 "Has the clock been used during the current Emacs session?")
264 ;;; The clock for measuring work time.
266 (defvar org-mode-line-string
"")
267 (put 'org-mode-line-string
'risky-local-variable t
)
269 (defvar org-clock-mode-line-timer nil
)
270 (defvar org-clock-idle-timer nil
)
271 (defvar org-clock-heading
) ; defined in org.el
272 (defvar org-clock-heading-for-remember
"")
273 (defvar org-clock-start-time
"")
275 (defvar org-clock-leftover-time nil
276 "If non-nil, user cancelled a clock; this is when leftover time started.")
278 (defvar org-clock-effort
""
279 "Effort estimate of the currently clocking task")
281 (defvar org-clock-total-time nil
282 "Holds total time, spent previously on currently clocked item.
283 This does not include the time in the currently running clock.")
285 (defvar org-clock-history nil
286 "List of marker pointing to recent clocked tasks.")
288 (defvar org-clock-default-task
(make-marker)
289 "Marker pointing to the default task that should clock time.
290 The clock can be made to switch to this task after clocking out
291 of a different task.")
293 (defvar org-clock-interrupted-task
(make-marker)
294 "Marker pointing to the task that has been interrupted by the current clock.")
296 (defvar org-clock-mode-line-map
(make-sparse-keymap))
297 (define-key org-clock-mode-line-map
[mode-line mouse-2
] 'org-clock-goto
)
298 (define-key org-clock-mode-line-map
[mode-line mouse-1
] 'org-clock-menu
)
300 (defun org-clock-menu ()
304 ["Clock out" org-clock-out t
]
305 ["Change effort estimate" org-clock-modify-effort-estimate t
]
306 ["Go to clock entry" org-clock-goto t
]
307 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t
:keys
"C-u C-c C-x C-i"])))
309 (defun org-clock-history-push (&optional pos buffer
)
310 "Push a marker to the clock history."
311 (setq org-clock-history-length
(max 1 (min 35 org-clock-history-length
)))
312 (let ((m (move-marker (make-marker) (or pos
(point)) buffer
)) n l
)
313 (while (setq n
(member m org-clock-history
))
314 (move-marker (car n
) nil
))
315 (setq org-clock-history
317 (mapcar (lambda (x) (if (marker-buffer x
) x nil
))
319 (when (>= (setq l
(length org-clock-history
)) org-clock-history-length
)
320 (setq org-clock-history
322 (nthcdr (- l org-clock-history-length -
1)
323 (nreverse org-clock-history
)))))
324 (push m org-clock-history
)))
326 (defun org-clock-save-markers-for-cut-and-paste (beg end
)
327 "Save relative positions of markers in region."
328 (org-check-and-save-marker org-clock-marker beg end
)
329 (org-check-and-save-marker org-clock-hd-marker beg end
)
330 (org-check-and-save-marker org-clock-default-task beg end
)
331 (org-check-and-save-marker org-clock-interrupted-task beg end
)
332 (mapc (lambda (m) (org-check-and-save-marker m beg end
))
335 (defun org-clocking-buffer ()
336 "Returns clocking buffer if we are currently clocking a task or nil"
337 (marker-buffer org-clock-marker
))
339 (defun org-clocking-p ()
340 "Returns t when clocking a task"
341 (not (equal (org-clocking-buffer) nil
)))
343 (defun org-clock-select-task (&optional prompt
)
344 "Select a task that recently was associated with clocking."
346 (let (sel-list rpl
(i 0) s
)
347 (save-window-excursion
348 (org-switch-to-buffer-other-window
349 (get-buffer-create "*Clock Task Select*"))
351 (when (marker-buffer org-clock-default-task
)
352 (insert (org-add-props "Default Task\n" nil
'face
'bold
))
353 (setq s
(org-clock-insert-selection-line ?d org-clock-default-task
))
355 (when (marker-buffer org-clock-interrupted-task
)
356 (insert (org-add-props "The task interrupted by starting the last one\n" nil
'face
'bold
))
357 (setq s
(org-clock-insert-selection-line ?i org-clock-interrupted-task
))
359 (when (org-clocking-p)
360 (insert (org-add-props "Current Clocking Task\n" nil
'face
'bold
))
361 (setq s
(org-clock-insert-selection-line ?c org-clock-marker
))
363 (insert (org-add-props "Recent Tasks\n" nil
'face
'bold
))
366 (when (marker-buffer m
)
368 s
(org-clock-insert-selection-line
372 (if (fboundp 'int-to-char
) (setf (car s
) (int-to-char (car s
))))
375 (org-fit-window-to-buffer)
376 (message (or prompt
"Select task for clocking:"))
377 (setq rpl
(read-char-exclusive))
381 ((assoc rpl sel-list
) (cdr (assoc rpl sel-list
)))
382 (t (error "Invalid task choice %c" rpl
))))))
384 (defun org-clock-insert-selection-line (i marker
)
385 "Insert a line for the clock selection menu.
386 And return a cons cell with the selection character integer and the marker
388 (when (marker-buffer marker
)
389 (let (file cat task heading prefix
)
390 (with-current-buffer (org-base-buffer (marker-buffer marker
))
396 (setq file
(buffer-file-name (marker-buffer marker
))
397 cat
(or (org-get-category)
398 (progn (org-refresh-category-properties)
400 heading
(org-get-heading 'notags
)
401 prefix
(save-excursion
402 (org-back-to-heading t
)
406 (org-fontify-like-in-org-mode
407 (concat prefix heading
)
409 (length prefix
)))))))
411 (insert (format "[%c] %-15s %s\n" i cat task
))
414 (defvar org-task-overrun nil
415 "Internal flag indicating if the clock has overrun the planned time.")
416 (defvar org-clock-update-period
60
417 "Number of seconds between mode line clock string updates.")
419 (defun org-clock-get-clock-string ()
420 "Form a clock-string, that will be shown in the mode line.
421 If an effort estimate was defined for the current item, use
422 01:30/01:50 format (clocked/estimated).
423 If not, show simply the clocked time like 01:50."
424 (let* ((clocked-time (org-clock-get-clocked-time))
425 (h (floor clocked-time
60))
426 (m (- clocked-time
(* 60 h
))))
428 (let* ((effort-in-minutes
429 (org-hh:mm-string-to-minutes org-clock-effort
))
430 (effort-h (floor effort-in-minutes
60))
431 (effort-m (- effort-in-minutes
(* effort-h
60)))
434 (format org-time-clocksum-format h m
)
435 'face
(if (and org-task-overrun
(not org-task-overrun-text
))
436 'org-mode-line-clock-overrun
'org-mode-line-clock
)))
437 (effort-str (format org-time-clocksum-format effort-h effort-m
))
438 (clockstr (org-propertize
439 (concat "[%s/" effort-str
440 "] (" (replace-regexp-in-string "%" "%%" org-clock-heading
) ")")
441 'face
'org-mode-line-clock
)))
442 (format clockstr work-done-str
))
443 (org-propertize (format
444 (concat "[" org-time-clocksum-format
" (%s)]")
445 h m org-clock-heading
)
446 'face
'org-mode-line-clock
))))
448 (defun org-clock-update-mode-line ()
450 (org-clock-notify-once-if-expired)
451 (setq org-task-overrun nil
))
452 (setq org-mode-line-string
454 (let ((clock-string (org-clock-get-clock-string))
455 (help-text "Org-mode clock is running.\nmouse-1 shows a menu\nmouse-2 will jump to task"))
456 (if (and (> org-clock-string-limit
0)
457 (> (length clock-string
) org-clock-string-limit
))
459 (substring clock-string
0 org-clock-string-limit
)
460 'help-echo
(concat help-text
": " org-clock-heading
))
461 (org-propertize clock-string
'help-echo help-text
)))
462 'local-map org-clock-mode-line-map
463 'mouse-face
(if (featurep 'xemacs
) 'highlight
'mode-line-highlight
)
465 (if (and org-task-overrun org-task-overrun-text
)
466 (setq org-mode-line-string
467 (concat (org-propertize
468 org-task-overrun-text
469 'face
'org-mode-line-clock-overrun
) org-mode-line-string
)))
470 (force-mode-line-update))
472 (defun org-clock-get-clocked-time ()
473 "Get the clocked time for the current item in minutes.
474 The time returned includes the time spent on this task in
475 previous clocking intervals."
476 (let ((currently-clocked-time
477 (floor (- (org-float-time)
478 (org-float-time org-clock-start-time
)) 60)))
479 (+ currently-clocked-time
(or org-clock-total-time
0))))
481 (defun org-clock-modify-effort-estimate (&optional value
)
482 "Add to or set the effort estimate of the item currently being clocked.
483 VALUE can be a number of minutes, or a string with format hh:mm or mm.
484 When the string starts with a + or a - sign, the current value of the effort
485 property will be changed by that amount.
486 This will update the \"Effort\" property of currently clocked item, and
489 (when (org-clock-is-active)
490 (let ((current org-clock-effort
) sign
)
492 ;; Prompt user for a value or a change
495 (format "Set effort (hh:mm or mm%s): "
497 (format ", prefix + to add to %s" org-clock-effort
)
499 (when (stringp value
)
500 ;; A string. See if it is a delta
501 (setq sign
(string-to-char value
))
502 (if (member sign
'(?- ?
+))
503 (setq current
(org-hh:mm-string-to-minutes
(substring current
1)))
505 (setq value
(org-hh:mm-string-to-minutes value
))
507 (setq value
(- current value
))
508 (if (equal ?
+ sign
) (setq value
(+ current value
)))))
509 (setq value
(max 0 value
)
510 org-clock-effort
(org-minutes-to-hh:mm-string value
))
511 (org-entry-put org-clock-marker
"Effort" org-clock-effort
)
512 (org-clock-update-mode-line)
513 (message "Effort is now %s" org-clock-effort
))))
515 (defvar org-clock-notification-was-shown nil
516 "Shows if we have shown notification already.")
518 (defun org-clock-notify-once-if-expired ()
519 "Show notification if we spent more time than we estimated before.
520 Notification is shown only once."
521 (when (org-clocking-p)
522 (let ((effort-in-minutes (org-hh:mm-string-to-minutes org-clock-effort
))
523 (clocked-time (org-clock-get-clocked-time)))
524 (if (setq org-task-overrun
525 (if (or (null effort-in-minutes
) (zerop effort-in-minutes
))
527 (>= clocked-time effort-in-minutes
)))
528 (unless org-clock-notification-was-shown
529 (setq org-clock-notification-was-shown t
)
531 (format "Task '%s' should be finished by now. (%s)"
532 org-clock-heading org-clock-effort
) t
))
533 (setq org-clock-notification-was-shown nil
)))))
535 (defun org-notify (notification &optional play-sound
)
536 "Send a NOTIFICATION and maybe PLAY-SOUND."
537 (org-show-notification notification
)
538 (if play-sound
(org-clock-play-sound)))
540 (defun org-show-notification (notification)
542 Use `org-show-notification-handler' if defined,
543 use libnotify if available, or fall back on a message."
544 (cond ((functionp org-show-notification-handler
)
545 (funcall org-show-notification-handler notification
))
546 ((stringp org-show-notification-handler
)
547 (start-process "emacs-timer-notification" nil
548 org-show-notification-handler notification
))
549 ((org-program-exists "notify-send")
550 (start-process "emacs-timer-notification" nil
551 "notify-send" notification
))
552 ;; Maybe the handler will send a message, so only use message as
553 ;; a fall back option
554 (t (message "%s" notification
))))
556 (defun org-clock-play-sound ()
557 "Play sound as configured by `org-clock-sound'.
558 Use alsa's aplay tool if available."
560 ((not org-clock-sound
))
561 ((eq org-clock-sound t
) (beep t
) (beep t
))
562 ((stringp org-clock-sound
)
563 (let ((file (expand-file-name org-clock-sound
)))
564 (if (file-exists-p file
)
565 (if (org-program-exists "aplay")
566 (start-process "org-clock-play-notification" nil
569 (play-sound-file file
)
570 (error (beep t
) (beep t
)))))))))
572 (defun org-program-exists (program-name)
573 "Checks whenever we can locate program and launch it."
574 (if (eq system-type
'gnu
/linux
)
575 (= 0 (call-process "which" nil nil nil program-name
))))
577 (defvar org-clock-mode-line-entry nil
578 "Information for the modeline about the running clock.")
580 (defun org-find-open-clocks (file)
581 "Search through the given file and find all open clocks."
582 (let ((buf (or (get-file-buffer file
)
583 (find-file-noselect file
)))
585 (with-current-buffer buf
587 (goto-char (point-min))
588 (while (re-search-forward "CLOCK: \\(\\[.*?\\]\\)$" nil t
)
589 (push (cons (copy-marker (1- (match-end 1)) t
)
590 (org-time-string-to-time (match-string 1))) clocks
))))
593 (defsubst org-is-active-clock
(clock)
594 "Return t if CLOCK is the currently active clock."
595 (and (org-clock-is-active)
596 (= org-clock-marker
(car clock
))))
598 (defmacro org-with-clock-position
(clock &rest forms
)
599 "Evaluate FORMS with CLOCK as the current active clock."
600 `(with-current-buffer (marker-buffer (car ,clock
))
604 (goto-char (car ,clock
))
608 (put 'org-with-clock-position
'lisp-indent-function
1)
610 (defmacro org-with-clock
(clock &rest forms
)
611 "Evaluate FORMS with CLOCK as the current active clock.
612 This macro also protects the current active clock from being altered."
613 `(org-with-clock-position ,clock
614 (let ((org-clock-start-time (cdr ,clock
))
615 (org-clock-total-time)
618 (org-clock-marker (car ,clock
))
619 (org-clock-hd-marker (save-excursion
620 (outline-back-to-heading t
)
624 (put 'org-with-clock
'lisp-indent-function
1)
626 (defsubst org-clock-clock-in
(clock &optional resume
)
627 "Clock in to the clock located by CLOCK.
628 If necessary, clock-out of the currently active clock."
629 (org-with-clock-position clock
630 (let ((org-clock-in-resume (or resume org-clock-in-resume
)))
633 (defsubst org-clock-clock-out
(clock &optional fail-quietly at-time
)
634 "Clock out of the clock located by CLOCK."
635 (let ((temp (copy-marker (car clock
)
636 (marker-insertion-type (car clock
)))))
637 (if (org-is-active-clock clock
)
638 (org-clock-out fail-quietly at-time
)
639 (org-with-clock clock
640 (org-clock-out fail-quietly at-time
)))
641 (setcar clock temp
)))
643 (defsubst org-clock-clock-cancel
(clock)
644 "Cancel the clock located by CLOCK."
645 (let ((temp (copy-marker (car clock
)
646 (marker-insertion-type (car clock
)))))
647 (if (org-is-active-clock clock
)
649 (org-with-clock clock
651 (setcar clock temp
)))
653 (defvar org-clock-clocking-in nil
)
654 (defvar org-clock-resolving-clocks nil
)
655 (defvar org-clock-resolving-clocks-due-to-idleness nil
)
657 (defun org-clock-resolve-clock (clock resolve-to
&optional close-p
658 restart-p fail-quietly
)
659 "Resolve `CLOCK' given the time `RESOLVE-TO', and the present.
660 `CLOCK' is a cons cell of the form (MARKER START-TIME).
661 This routine can do one of many things:
663 if `RESOLVE-TO' is nil
664 if `CLOSE-P' is non-nil, give an error
665 if this clock is the active clock, cancel it
666 else delete the clock line (as if it never happened)
667 if `RESTART-P' is non-nil, start a new clock
669 else if `RESOLVE-TO' is the symbol `now'
670 if `RESTART-P' is non-nil, give an error
671 if `CLOSE-P' is non-nil, clock out the entry and
672 if this clock is the active clock, stop it
673 else if this clock is the active clock, do nothing
674 else if there is no active clock, resume this clock
675 else ask to cancel the active clock, and if so,
676 resume this clock after cancelling it
678 else if `RESOLVE-TO' is some date in the future
679 give an error about `RESOLVE-TO' being invalid
681 else if `RESOLVE-TO' is some date in the past
682 if `RESTART-P' is non-nil, give an error
683 if `CLOSE-P' is non-nil, enter a closing time and
684 if this clock is the active clock, stop it
685 else if this clock is the active clock, enter a
686 closing time, stop the current clock, then
687 start a new clock for the same item
688 else just enter a closing time for this clock
689 and then start a new clock for the same item"
690 (let ((org-clock-resolving-clocks t
))
693 (org-clock-clock-cancel clock
)
694 (if (and restart-p
(not org-clock-clocking-in
))
695 (org-clock-clock-in clock
)))
697 ((eq resolve-to
'now
)
699 (error "RESTART-P is not valid here"))
700 (if (or close-p org-clock-clocking-in
)
701 (org-clock-clock-out clock fail-quietly
)
702 (unless (org-is-active-clock clock
)
703 (org-clock-clock-in clock t
))))
705 ((not (time-less-p resolve-to
(current-time)))
706 (error "RESOLVE-TO must refer to a time in the past"))
710 (error "RESTART-P is not valid here"))
711 (org-clock-clock-out clock fail-quietly resolve-to
)
712 (unless org-clock-clocking-in
714 (setq org-clock-leftover-time resolve-to
)
715 (org-clock-clock-in clock
)))))))
717 (defun org-clock-resolve (clock &optional prompt-fn last-valid fail-quietly
)
718 "Resolve an open org-mode clock.
719 An open clock was found, with `dangling' possibly being non-nil.
720 If this function was invoked with a prefix argument, non-dangling
721 open clocks are ignored. The given clock requires some sort of
722 user intervention to resolve it, either because a clock was left
723 dangling or due to an idle timeout. The clock resolution can
726 (a) deleted, the user doesn't care about the clock
727 (b) restarted from the current time (if no other clock is open)
728 (c) closed, giving the clock X minutes
729 (d) closed and then restarted
730 (e) resumed, as if the user had never left
732 The format of clock is (CONS MARKER START-TIME), where MARKER
733 identifies the buffer and position the clock is open at (and
734 thus, the heading it's under), and START-TIME is when the clock
738 (save-window-excursion
740 (unless org-clock-resolving-clocks-due-to-idleness
741 (org-with-clock clock
(org-clock-goto))
742 (with-current-buffer (marker-buffer (car clock
))
743 (goto-char (car clock
))
744 (if org-clock-into-drawer
746 (if (stringp org-clock-into-drawer
)
747 (concat ":" org-clock-into-drawer
":")
752 (outline-back-to-heading t
)
753 (search-forward logbook
)
754 (goto-char (match-beginning 0)))
756 (outline-back-to-heading t
)
757 (search-forward logbook
)
758 (search-forward ":END:")
759 (goto-char (match-end 0)))
762 (when (featurep 'xemacs
)
763 (message (concat (funcall prompt-fn clock
)
764 " [(kK)eep (sS)ubtract (C)ancel]? "))
765 (setq char-pressed
(read-char-exclusive)))
766 (while (or (null char-pressed
)
767 (and (not (memq char-pressed
'(?k ?K ?s ?S ?C ?i
)))
770 (read-char (concat (funcall prompt-fn clock
)
771 " [(kK)p (sS)ub (C)ncl (i)gn]? ")
773 (and (not (eq char-pressed ?i
)) char-pressed
)))))
774 (default (floor (/ (org-float-time
775 (time-subtract (current-time) last-valid
)) 60)))
776 (keep (and (memq ch
'(?k ?K
))
777 (read-number "Keep how many minutes? " default
)))
778 (subtractp (memq ch
'(?s ?S
)))
779 (barely-started-p (< (- (org-float-time last-valid
)
780 (org-float-time (cdr clock
))) 45))
781 (start-over (and subtractp barely-started-p
)))
783 (not (memq ch
'(?k ?K ?s ?S ?C
))))
785 (org-clock-resolve-clock
788 ;; If the time on the clock was less than a minute before
789 ;; the user went away, and they've ask to subtract all the
798 (time-add last-valid
(seconds-to-time (* 60 keep
)))))
801 (not (memq ch
'(?K ?S ?C
))))
804 (defun org-resolve-clocks (&optional also-non-dangling-p prompt-fn last-valid
)
805 "Resolve all currently open org-mode clocks.
806 If `also-non-dangling-p' is non-nil, also ask to resolve
807 non-dangling (i.e., currently open and valid) clocks."
809 (unless org-clock-resolving-clocks
810 (let ((org-clock-resolving-clocks t
))
811 (dolist (file (org-files-list))
812 (let ((clocks (org-find-open-clocks file
)))
813 (dolist (clock clocks
)
814 (let ((dangling (or (not (org-clock-is-active))
815 (/= (car clock
) org-clock-marker
))))
816 (unless (and (not dangling
) (not also-non-dangling-p
))
823 "Dangling clock started %d mins ago"
825 (/ (- (org-float-time (current-time))
826 (org-float-time (cdr clock
))) 60))))))
828 (cdr clock
)))))))))))
830 (defun org-emacs-idle-seconds ()
831 "Return the current Emacs idle time in seconds, or nil if not idle."
832 (let ((idle-time (current-idle-time)))
834 (org-float-time idle-time
)
837 (defun org-mac-idle-seconds ()
838 "Return the current Mac idle time in seconds"
839 (string-to-number (shell-command-to-string "ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle; last}'")))
841 (defun org-x11-idle-seconds ()
842 "Return the current X11 idle time in seconds"
843 (/ (string-to-number (shell-command-to-string "x11idle")) 1000))
845 (defun org-user-idle-seconds ()
846 "Return the number of seconds the user has been idle for.
847 This routine returns a floating point number."
849 ((eq system-type
'darwin
)
850 (org-mac-idle-seconds))
851 ((eq window-system
'x
)
852 (org-x11-idle-seconds))
854 (org-emacs-idle-seconds))))
856 (defvar org-clock-user-idle-seconds
)
858 (defun org-resolve-clocks-if-idle ()
859 "Resolve all currently open org-mode clocks.
860 This is performed after `org-clock-idle-time' minutes, to check
861 if the user really wants to stay clocked in after being idle for
863 (when (and org-clock-idle-time
(not org-clock-resolving-clocks
)
865 (let* ((org-clock-user-idle-seconds (org-user-idle-seconds))
866 (org-clock-user-idle-start
867 (time-subtract (current-time)
868 (seconds-to-time org-clock-user-idle-seconds
)))
869 (org-clock-resolving-clocks-due-to-idleness t
))
870 (if (> org-clock-user-idle-seconds
(* 60 org-clock-idle-time
))
872 (cons org-clock-marker
873 org-clock-start-time
)
876 (format "Clocked in & idle for %.1f mins"
878 (time-subtract (current-time)
879 org-clock-user-idle-start
))
881 org-clock-user-idle-start
)))))
883 (defun org-clock-in (&optional select
)
884 "Start the clock on the current item.
885 If necessary, clock-out of the currently active clock.
886 With prefix arg SELECT, offer a list of recently clocked tasks to
887 clock into. When SELECT is `C-u C-u', clock into the current task and mark
888 is as the default task, a special task that will always be offered in
889 the clocking selection, associated with the letter `d'."
891 (setq org-clock-notification-was-shown nil
)
893 (let ((interrupting (and (not org-clock-resolving-clocks-due-to-idleness
)
895 ts selected-task target-pos
(msg-extra "")
896 (leftover (and (not org-clock-resolving-clocks
)
897 org-clock-leftover-time
)))
898 (when (and org-clock-auto-clock-resolution
899 (or (not interrupting
)
900 (eq t org-clock-auto-clock-resolution
))
901 (not org-clock-clocking-in
)
902 (not org-clock-resolving-clocks
))
903 (setq org-clock-leftover-time nil
)
904 (let ((org-clock-clocking-in t
))
905 (org-resolve-clocks))) ; check if any clocks are dangling
906 (when (equal select
'(4))
907 (setq selected-task
(org-clock-select-task "Clock-in on task: "))
909 (setq selected-task
(copy-marker selected-task
))
912 ;; We are interrupting the clocking of a different task.
913 ;; Save a marker to this task, so that we can go back.
914 ;; First check if we are trying to clock into the same task!
916 (unless selected-task
917 (org-back-to-heading t
))
918 (and (equal (marker-buffer org-clock-hd-marker
)
920 (marker-buffer selected-task
)
922 (= (marker-position org-clock-hd-marker
)
924 (marker-position selected-task
)
926 (message "Clock continues in \"%s\"" org-clock-heading
)
928 (move-marker org-clock-interrupted-task
929 (marker-position org-clock-marker
)
930 (org-clocking-buffer))
933 (when (equal select
'(16))
934 ;; Mark as default clocking task
935 (org-clock-mark-default-task))
937 ;; Clock in at which position?
939 (if (and (eobp) (not (org-on-heading-p)))
942 (run-hooks 'org-clock-in-prepare-hook
)
944 (when (and selected-task
(marker-buffer selected-task
))
945 ;; There is a selected task, move to the correct buffer
946 ;; and set the new target position.
947 (set-buffer (org-base-buffer (marker-buffer selected-task
)))
948 (setq target-pos
(marker-position selected-task
))
949 (move-marker selected-task nil
))
953 (goto-char target-pos
)
954 (org-back-to-heading t
)
955 (or interrupting
(move-marker org-clock-interrupted-task nil
))
956 (org-clock-history-push)
957 (org-clock-set-current)
958 (cond ((functionp org-clock-in-switch-to-state
)
959 (looking-at org-complex-heading-regexp
)
960 (let ((newstate (funcall org-clock-in-switch-to-state
962 (if newstate
(org-todo newstate
))))
963 ((and org-clock-in-switch-to-state
964 (not (looking-at (concat outline-regexp
"[ \t]*"
965 org-clock-in-switch-to-state
967 (org-todo org-clock-in-switch-to-state
)))
968 (setq org-clock-heading-for-remember
969 (and (looking-at org-complex-heading-regexp
)
971 (org-trim (buffer-substring (match-end 1)
973 (setq org-clock-heading
974 (cond ((and org-clock-heading-function
975 (functionp org-clock-heading-function
))
976 (funcall org-clock-heading-function
))
977 ((looking-at org-complex-heading-regexp
)
978 (replace-regexp-in-string
979 "\\[\\[.*?\\]\\[\\(.*?\\)\\]\\]" "\\1"
982 (setq org-clock-heading
(org-propertize org-clock-heading
984 (org-clock-find-position org-clock-in-resume
)
986 ((and org-clock-in-resume
988 (concat "^[ \t]* " org-clock-string
989 " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
990 " +\\sw+\.? +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")))
991 (message "Matched %s" (match-string 1))
992 (setq ts
(concat "[" (match-string 1) "]"))
993 (goto-char (match-end 1))
994 (setq org-clock-start-time
996 (org-parse-time-string (match-string 1))))
997 (setq org-clock-effort
(org-get-effort))
998 (setq org-clock-total-time
(org-clock-sum-current-item
999 (org-clock-get-sum-start))))
1000 ((eq org-clock-in-resume
'auto-restart
)
1001 ;; called from org-clock-load during startup,
1002 ;; do not interrupt, but warn!
1003 (message "Cannot restart clock because task does not contain unfinished clock")
1008 (insert-before-markers "\n")
1010 (org-indent-line-function)
1011 (when (and (save-excursion
1014 (beginning-of-line 1)
1015 (org-indent-line-to (- (org-get-indentation) 2)))
1016 (insert org-clock-string
" ")
1017 (setq org-clock-effort
(org-get-effort))
1018 (setq org-clock-total-time
(org-clock-sum-current-item
1019 (org-clock-get-sum-start)))
1020 (setq org-clock-start-time
1024 "You stopped another clock %d mins ago; start this one from then? "
1025 (/ (- (org-float-time (current-time))
1026 (org-float-time leftover
)) 60)))
1029 (setq ts
(org-insert-time-stamp org-clock-start-time
1030 'with-hm
'inactive
))))
1031 (move-marker org-clock-marker
(point) (buffer-base-buffer))
1032 (move-marker org-clock-hd-marker
1033 (save-excursion (org-back-to-heading t
) (point))
1034 (buffer-base-buffer))
1035 (setq org-clock-has-been-used t
)
1036 (or global-mode-string
(setq global-mode-string
'("")))
1037 (or (memq 'org-mode-line-string global-mode-string
)
1038 (setq global-mode-string
1039 (append global-mode-string
'(org-mode-line-string))))
1040 (org-clock-update-mode-line)
1041 (when org-clock-mode-line-timer
1042 (cancel-timer org-clock-mode-line-timer
)
1043 (setq org-clock-mode-line-timer nil
))
1044 (setq org-clock-mode-line-timer
1045 (run-with-timer org-clock-update-period
1046 org-clock-update-period
1047 'org-clock-update-mode-line
))
1048 (when org-clock-idle-timer
1049 (cancel-timer org-clock-idle-timer
)
1050 (setq org-clock-idle-timer nil
))
1051 (setq org-clock-idle-timer
1052 (run-with-timer 60 60 'org-resolve-clocks-if-idle
))
1053 (message "Clock starts at %s - %s" ts msg-extra
)
1054 (run-hooks 'org-clock-in-hook
)))))))
1056 (defvar org-clock-current-task nil
1057 "Task currently clocked in.")
1058 (defun org-clock-set-current ()
1059 "Set `org-clock-current-task' to the task currently clocked in."
1060 (setq org-clock-current-task
(nth 4 (org-heading-components))))
1061 (defun org-clock-delete-current ()
1062 "Reset `org-clock-current-task' to nil."
1063 (setq org-clock-current-task nil
))
1065 (defun org-clock-mark-default-task ()
1066 "Mark current task as default task."
1069 (org-back-to-heading t
)
1070 (move-marker org-clock-default-task
(point))))
1073 (defun org-clock-get-sum-start ()
1074 "Return the time from which clock times should be counted.
1075 This is for the currently running clock as it is displayed
1076 in the mode line. This function looks at the properties
1077 LAST_REPEAT and in particular CLOCK_MODELINE_TOTAL and the
1078 corresponding variable `org-clock-modeline-total' and then
1079 decides which time to use."
1080 (let ((cmt (or (org-entry-get nil
"CLOCK_MODELINE_TOTAL")
1081 (symbol-name org-clock-modeline-total
)))
1082 (lr (org-entry-get nil
"LAST_REPEAT")))
1084 ((equal cmt
"current")
1085 (setq msg-extra
"showing time in current clock instance")
1087 ((equal cmt
"today")
1088 (setq msg-extra
"showing today's task time.")
1089 (let* ((dt (decode-time (current-time))))
1090 (setq dt
(append (list 0 0 0) (nthcdr 3 dt
)))
1091 (if org-extend-today-until
1092 (setf (nth 2 dt
) org-extend-today-until
))
1093 (apply 'encode-time dt
)))
1094 ((or (equal cmt
"all")
1095 (and (or (not cmt
) (equal cmt
"auto"))
1097 (setq msg-extra
"showing entire task time.")
1099 ((or (equal cmt
"repeat")
1100 (and (or (not cmt
) (equal cmt
"auto"))
1102 (setq msg-extra
"showing task time since last repeat.")
1105 (org-time-string-to-time lr
)))
1108 (defun org-clock-find-position (find-unclosed)
1109 "Find the location where the next clock line should be inserted.
1110 When FIND-UNCLOSED is non-nil, first check if there is an unclosed clock
1111 line and position cursor in that line."
1112 (org-back-to-heading t
)
1114 (let ((beg (save-excursion
1115 (beginning-of-line 2)
1116 (or (bolp) (newline))
1118 (end (progn (outline-next-heading) (point)))
1119 (re (concat "^[ \t]*" org-clock-string
))
1121 (drawer (if (stringp org-clock-into-drawer
)
1122 org-clock-into-drawer
"LOGBOOK"))
1123 first last ind-last
)
1125 (when (and find-unclosed
1127 (concat "^[ \t]* " org-clock-string
1128 " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
1129 " +\\sw+ +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")
1131 (beginning-of-line 1)
1133 (when (eobp) (newline) (setq end
(max (point) end
)))
1134 (when (re-search-forward (concat "^[ \t]*:" drawer
":") end t
)
1135 ;; we seem to have a CLOCK drawer, so go there.
1136 (beginning-of-line 2)
1137 (or org-log-states-order-reversed
1138 (and (re-search-forward org-property-end-re nil t
)
1139 (goto-char (match-beginning 0))))
1141 ;; Lets count the CLOCK lines
1143 (while (re-search-forward re end t
)
1144 (setq first
(or first
(match-beginning 0))
1145 last
(match-beginning 0)
1147 (when (and (integerp org-clock-into-drawer
)
1149 (>= (1+ cnt
) org-clock-into-drawer
))
1150 ;; Wrap current entries into a new drawer
1152 (setq ind-last
(org-get-indentation))
1153 (beginning-of-line 2)
1154 (if (and (>= (org-get-indentation) ind-last
)
1158 (beginning-of-line 0)
1159 (org-indent-line-to ind-last
)
1161 (insert ":" drawer
":\n")
1162 (beginning-of-line 0)
1163 (org-indent-line-function)
1165 (beginning-of-line 2)
1166 (or org-log-states-order-reversed
1167 (and (re-search-forward org-property-end-re nil t
)
1168 (goto-char (match-beginning 0))))
1172 (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
1173 (not (equal (match-string 1) org-clock-string
)))
1174 ;; Planning info, skip to after it
1175 (beginning-of-line 2)
1176 (or (bolp) (newline)))
1177 (when (or (eq org-clock-into-drawer t
)
1178 (stringp org-clock-into-drawer
)
1179 (and (integerp org-clock-into-drawer
)
1180 (< org-clock-into-drawer
2)))
1181 (insert ":" drawer
":\n:END:\n")
1182 (beginning-of-line -
1)
1183 (org-indent-line-function)
1185 (beginning-of-line 2)
1186 (org-indent-line-function)
1188 (or org-log-states-order-reversed
1189 (and (re-search-forward org-property-end-re nil t
)
1190 (goto-char (match-beginning 0))))))))
1192 (defun org-clock-out (&optional fail-quietly at-time
)
1193 "Stop the currently running clock.
1194 If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
1197 (if (not (org-clocking-p))
1198 (if fail-quietly
(throw 'exit t
) (error "No active clock")))
1199 (let (ts te s h m remove
)
1200 (save-excursion ; Do not replace this with `with-current-buffer'.
1201 (with-no-warnings (set-buffer (org-clocking-buffer)))
1204 (goto-char org-clock-marker
)
1205 (beginning-of-line 1)
1206 (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
1207 (equal (match-string 1) org-clock-string
))
1208 (setq ts
(match-string 2))
1209 (if fail-quietly
(throw 'exit nil
) (error "Clock start time is gone")))
1210 (goto-char (match-end 0))
1211 (delete-region (point) (point-at-eol))
1213 (setq te
(org-insert-time-stamp (or at-time
(current-time))
1214 'with-hm
'inactive
))
1215 (setq s
(- (org-float-time (apply 'encode-time
(org-parse-time-string te
)))
1216 (org-float-time (apply 'encode-time
(org-parse-time-string ts
))))
1217 h
(floor (/ s
3600))
1221 (insert " => " (format "%2d:%02d" h m
))
1222 (when (setq remove
(and org-clock-out-remove-zero-time-clocks
1224 (beginning-of-line 1)
1225 (delete-region (point) (point-at-eol))
1226 (and (looking-at "\n") (> (point-max) (1+ (point)))
1228 (move-marker org-clock-marker nil
)
1229 (move-marker org-clock-hd-marker nil
)
1230 (when org-log-note-clock-out
1231 (org-add-log-setup 'clock-out nil nil nil nil
1232 (concat "# Task: " (org-get-heading t
) "\n\n")))
1233 (when org-clock-mode-line-timer
1234 (cancel-timer org-clock-mode-line-timer
)
1235 (setq org-clock-mode-line-timer nil
))
1236 (when org-clock-idle-timer
1237 (cancel-timer org-clock-idle-timer
)
1238 (setq org-clock-idle-timer nil
))
1239 (setq global-mode-string
1240 (delq 'org-mode-line-string global-mode-string
))
1241 (when org-clock-out-switch-to-state
1243 (org-back-to-heading t
)
1244 (let ((org-inhibit-logging t
)
1245 (org-clock-out-when-done nil
))
1247 ((functionp org-clock-out-switch-to-state
)
1248 (looking-at org-complex-heading-regexp
)
1249 (let ((newstate (funcall org-clock-out-switch-to-state
1251 (if newstate
(org-todo newstate
))))
1252 ((and org-clock-out-switch-to-state
1253 (not (looking-at (concat outline-regexp
"[ \t]*"
1254 org-clock-out-switch-to-state
1256 (org-todo org-clock-out-switch-to-state
))))))
1257 (force-mode-line-update)
1258 (message (concat "Clock stopped at %s after HH:MM = " org-time-clocksum-format
"%s") te h m
1259 (if remove
" => LINE REMOVED" ""))
1260 (run-hooks 'org-clock-out-hook
)
1261 (org-clock-delete-current))))))
1263 (defun org-clock-cancel ()
1264 "Cancel the running clock be removing the start timestamp."
1266 (if (not (org-clocking-p))
1267 (error "No active clock"))
1268 (save-excursion ; Do not replace this with `with-current-buffer'.
1269 (with-no-warnings (set-buffer (org-clocking-buffer)))
1270 (goto-char org-clock-marker
)
1271 (delete-region (1- (point-at-bol)) (point-at-eol))
1272 ;; Just in case, remove any empty LOGBOOK left over
1273 (org-remove-empty-drawer-at "LOGBOOK" (point)))
1274 (move-marker org-clock-marker nil
)
1275 (move-marker org-clock-hd-marker nil
)
1276 (setq global-mode-string
1277 (delq 'org-mode-line-string global-mode-string
))
1278 (force-mode-line-update)
1279 (message "Clock canceled")
1280 (run-hooks 'org-clock-cancel-hook
))
1282 (defun org-clock-goto (&optional select
)
1283 "Go to the currently clocked-in entry, or to the most recently clocked one.
1284 With prefix arg SELECT, offer recently clocked tasks for selection."
1289 (or (org-clock-select-task "Select task to go to: ")
1290 (error "No task selected")))
1291 ((org-clocking-p) org-clock-marker
)
1292 ((and org-clock-goto-may-find-recent-task
1293 (car org-clock-history
)
1294 (marker-buffer (car org-clock-history
)))
1296 (car org-clock-history
))
1297 (t (error "No active or recent clock task")))))
1298 (switch-to-buffer (marker-buffer m
))
1299 (if (or (< m
(point-min)) (> m
(point-max))) (widen))
1302 (org-back-to-heading t
)
1303 (org-cycle-hide-drawers 'children
)
1307 (message "No running clock, this is the most recently clocked task"))
1308 (run-hooks 'org-clock-goto-hook
)))
1310 (defvar org-clock-file-total-minutes nil
1311 "Holds the file total time in minutes, after a call to `org-clock-sum'.")
1312 (make-variable-buffer-local 'org-clock-file-total-minutes
)
1314 (defun org-clock-sum (&optional tstart tend headline-filter
)
1315 "Sum the times for each subtree.
1316 Puts the resulting times in minutes as a text property on each headline.
1317 TSTART and TEND can mark a time range to be considered. HEADLINE-FILTER is a
1318 zero-arg function that, if specified, is called for each headline in the time
1319 range with point at the headline. Headlines for which HEADLINE-FILTER returns
1320 nil are excluded from the clock summation."
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)
1337 '(:org-clock-minutes t
1338 :org-clock-force-headline-inclusion t
))
1340 (goto-char (point-max))
1341 (while (re-search-backward re nil t
)
1345 (setq ts
(match-string 2)
1348 (apply 'encode-time
(org-parse-time-string ts
)))
1350 (apply 'encode-time
(org-parse-time-string te
)))
1351 ts
(if tstart
(max ts tstart
) ts
)
1352 te
(if tend
(min te tend
) te
)
1354 t1
(if (> dt
0) (+ t1
(floor (/ dt
60))) t1
)))
1357 (setq t1
(+ t1
(string-to-number (match-string 5))
1358 (* 60 (string-to-number (match-string 4))))))
1360 ;; Add the currently clocking item time to the total
1361 (when (and org-clock-report-include-clocking-task
1362 (equal (org-clocking-buffer) (current-buffer))
1363 (equal (marker-position org-clock-hd-marker
) (point)))
1364 (let ((time (floor (- (org-float-time)
1365 (org-float-time org-clock-start-time
)) 60)))
1366 (setq t1
(+ t1 time
))))
1367 (let* ((headline-forced
1368 (get-text-property (point)
1369 :org-clock-force-headline-inclusion
))
1371 (or (null headline-filter
)
1373 (save-match-data (funcall headline-filter
))))))
1374 (setq level
(- (match-end 1) (match-beginning 1)))
1375 (when (or (> t1
0) (> (aref ltimes level
) 0))
1376 (when (or headline-included headline-forced
)
1377 (if headline-included
1378 (loop for l from
0 to level do
1379 (aset ltimes l
(+ (aref ltimes l
) t1
))))
1380 (setq time
(aref ltimes level
))
1381 (goto-char (match-beginning 0))
1382 (put-text-property (point) (point-at-eol) :org-clock-minutes time
)
1387 (> (funcall outline-level
) 1)
1388 (outline-up-heading 1 t
)
1390 (point) (point-at-eol)
1391 :org-clock-force-headline-inclusion t
))))))
1393 (loop for l from level to
(1- lmax
) do
1394 (aset ltimes l
0)))))))
1395 (setq org-clock-file-total-minutes
(aref ltimes
0)))
1396 (set-buffer-modified-p bmp
)))
1398 (defun org-clock-sum-current-item (&optional tstart
)
1399 "Returns time, clocked on current item in total"
1402 (org-narrow-to-subtree)
1403 (org-clock-sum tstart
)
1404 org-clock-file-total-minutes
)))
1406 (defun org-clock-display (&optional total-only
)
1407 "Show subtree times in the entire buffer.
1408 If TOTAL-ONLY is non-nil, only show the total time for the entire file
1411 (org-clock-remove-overlays)
1416 (goto-char (point-min))
1417 (while (or (and (equal (setq p
(point)) (point-min))
1418 (get-text-property p
:org-clock-minutes
))
1419 (setq p
(next-single-property-change
1420 (point) :org-clock-minutes
)))
1422 (when (setq time
(get-text-property p
:org-clock-minutes
))
1423 (org-clock-put-overlay time
(funcall outline-level
))))
1424 (setq h
(/ org-clock-file-total-minutes
60)
1425 m
(- org-clock-file-total-minutes
(* 60 h
)))
1426 ;; Arrange to remove the overlays upon next change.
1427 (when org-remove-highlights-with-change
1428 (org-add-hook 'before-change-functions
'org-clock-remove-overlays
1430 (if org-time-clocksum-use-fractional
1431 (message (concat "Total file time: " org-time-clocksum-fractional-format
1432 " (%d hours and %d minutes)")
1433 (/ (+ (* h
60.0) m
) 60.0) h m
)
1434 (message (concat "Total file time: " org-time-clocksum-format
1435 " (%d hours and %d minutes)") h m h m
))))
1437 (defvar org-clock-overlays nil
)
1438 (make-variable-buffer-local 'org-clock-overlays
)
1440 (defun org-clock-put-overlay (time &optional level
)
1441 "Put an overlays on the current line, displaying TIME.
1442 If LEVEL is given, prefix time with a corresponding number of stars.
1443 This creates a new overlay and stores it in `org-clock-overlays', so that it
1444 will be easy to remove."
1445 (let* ((c 60) (h (floor (/ time
60))) (m (- time
(* 60 h
)))
1446 (l (if level
(org-get-valid-level level
0) 0))
1447 (fmt (concat "%s " (if org-time-clocksum-use-fractional
1448 org-time-clocksum-fractional-format
1449 org-time-clocksum-format
) "%s"))
1452 (org-move-to-column c
)
1453 (unless (eolp) (skip-chars-backward "^ \t"))
1454 (skip-chars-backward " \t")
1455 (setq ov
(make-overlay (1- (point)) (point-at-eol))
1456 tx
(concat (buffer-substring (1- (point)) (point))
1457 (make-string (+ off
(max 0 (- c
(current-column)))) ?.
)
1458 (org-add-props (if org-time-clocksum-use-fractional
1461 (/ (+ (* h
60.0) m
) 60.0)
1462 (make-string (- 16 l
) ?\
))
1464 (make-string l ?
*) h m
1465 (make-string (- 16 l
) ?\
)))
1466 (list 'face
'org-clock-overlay
))
1468 (if (not (featurep 'xemacs
))
1469 (overlay-put ov
'display tx
)
1470 (overlay-put ov
'invisible t
)
1471 (overlay-put ov
'end-glyph
(make-glyph tx
)))
1472 (push ov org-clock-overlays
)))
1474 (defun org-clock-remove-overlays (&optional beg end noremove
)
1475 "Remove the occur highlights from the buffer.
1476 BEG and END are ignored. If NOREMOVE is nil, remove this function
1477 from the `before-change-functions' in the current buffer."
1479 (unless org-inhibit-highlight-removal
1480 (mapc 'delete-overlay org-clock-overlays
)
1481 (setq org-clock-overlays nil
)
1483 (remove-hook 'before-change-functions
1484 'org-clock-remove-overlays
'local
))))
1486 (defvar state
) ;; dynamically scoped into this function
1487 (defun org-clock-out-if-current ()
1488 "Clock out if the current entry contains the running clock.
1489 This is used to stop the clock after a TODO entry is marked DONE,
1490 and is only done if the variable `org-clock-out-when-done' is not nil."
1491 (when (and org-clock-out-when-done
1492 (or (and (eq t org-clock-out-when-done
)
1493 (member state org-done-keywords
))
1494 (and (listp org-clock-out-when-done
)
1495 (member state org-clock-out-when-done
)))
1496 (equal (or (buffer-base-buffer (org-clocking-buffer))
1497 (org-clocking-buffer))
1498 (or (buffer-base-buffer (current-buffer))
1500 (< (point) org-clock-marker
)
1501 (> (save-excursion (outline-next-heading) (point))
1503 ;; Clock out, but don't accept a logging message for this.
1504 (let ((org-log-note-clock-out nil
)
1505 (org-clock-out-switch-to-state nil
))
1508 (add-hook 'org-after-todo-state-change-hook
1509 'org-clock-out-if-current
)
1512 (defun org-get-clocktable (&rest props
)
1513 "Get a formatted clocktable with parameters according to PROPS.
1514 The table is created in a temporary buffer, fully formatted and
1515 fontified, and then returned."
1517 (setq props
(plist-put props
:name
"clocktable"))
1518 (unless (plist-member props
:maxlevel
)
1519 (setq props
(plist-put props
:maxlevel
2)))
1520 (unless (plist-member props
:scope
)
1521 (setq props
(plist-put props
:scope
'agenda
)))
1524 (org-create-dblock props
)
1526 (font-lock-fontify-buffer)
1528 (buffer-substring (point) (progn
1529 (re-search-forward "^#\\+END" nil t
)
1532 (defun org-clock-report (&optional arg
)
1533 "Create a table containing a report about clocked time.
1534 If the cursor is inside an existing clocktable block, then the table
1535 will be updated. If not, a new clocktable will be inserted.
1536 When called with a prefix argument, move to the first clock table in the
1537 buffer and update it."
1539 (org-clock-remove-overlays)
1541 (org-find-dblock "clocktable")
1543 (if (org-in-clocktable-p)
1544 (goto-char (org-in-clocktable-p))
1545 (org-create-dblock (append (list :name
"clocktable")
1546 org-clock-clocktable-default-properties
)))
1547 (org-update-dblock))
1549 (defun org-in-clocktable-p ()
1550 "Check if the cursor is in a clocktable."
1551 (let ((pos (point)) start
)
1554 (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t
)
1555 (setq start
(match-beginning 0))
1556 (re-search-forward "^#\\+END:.*" nil t
)
1557 (>= (match-end 0) pos
)
1560 (defun org-clock-special-range (key &optional time as-strings
)
1561 "Return two times bordering a special time range.
1562 Key is a symbol specifying the range and can be one of `today', `yesterday',
1563 `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
1564 A week starts Monday 0:00 and ends Sunday 24:00.
1565 The range is determined relative to TIME. TIME defaults to the current time.
1566 The return value is a cons cell with two internal times like the ones
1567 returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
1568 the returned times will be formatted strings."
1569 (if (integerp key
) (setq key
(intern (number-to-string key
))))
1570 (let* ((tm (decode-time (or time
(current-time))))
1571 (s 0) (m (nth 1 tm
)) (h (nth 2 tm
))
1572 (d (nth 3 tm
)) (month (nth 4 tm
)) (y (nth 5 tm
))
1574 (skey (symbol-name key
))
1576 s1 m1 h1 d1 month1 y1 diff ts te fm txt w date
)
1578 ((string-match "^[0-9]+$" skey
)
1579 (setq y
(string-to-number skey
) m
1 d
1 key
'year
))
1580 ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)$" skey
)
1581 (setq y
(string-to-number (match-string 1 skey
))
1582 month
(string-to-number (match-string 2 skey
))
1584 ((string-match "^\\([0-9]+\\)-[wW]\\([0-9]\\{1,2\\}\\)$" skey
)
1586 (setq y
(string-to-number (match-string 1 skey
))
1587 w
(string-to-number (match-string 2 skey
)))
1588 (setq date
(calendar-gregorian-from-absolute
1589 (calendar-absolute-from-iso (list w
1 y
))))
1590 (setq d
(nth 1 date
) month
(car date
) y
(nth 2 date
)
1593 ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)-\\([0-9]\\{1,2\\}\\)$" skey
)
1594 (setq y
(string-to-number (match-string 1 skey
))
1595 month
(string-to-number (match-string 2 skey
))
1596 d
(string-to-number (match-string 3 skey
))
1598 ((string-match "\\([-+][0-9]+\\)$" skey
)
1599 (setq shift
(string-to-number (match-string 1 skey
))
1600 key
(intern (substring skey
0 (match-beginning 1))))))
1602 (cond ((eq key
'yesterday
) (setq key
'today shift -
1))
1603 ((eq key
'lastweek
) (setq key
'week shift -
1))
1604 ((eq key
'lastmonth
) (setq key
'month shift -
1))
1605 ((eq key
'lastyear
) (setq key
'year shift -
1))))
1607 ((memq key
'(day today
))
1608 (setq d
(+ d shift
) h
0 m
0 h1
24 m1
0))
1609 ((memq key
'(week thisweek
))
1610 (setq diff
(+ (* -
7 shift
) (if (= dow
0) 6 (1- dow
)))
1611 m
0 h
0 d
(- d diff
) d1
(+ 7 d
)))
1612 ((memq key
'(month thismonth
))
1613 (setq d
1 h
0 m
0 d1
1 month
(+ month shift
) month1
(1+ month
) h1
0 m1
0))
1614 ((memq key
'(year thisyear
))
1615 (setq m
0 h
0 d
1 month
1 y
(+ y shift
) y1
(1+ y
)))
1616 (t (error "No such time block %s" key
)))
1617 (setq ts
(encode-time s m h d month y
)
1618 te
(encode-time (or s1 s
) (or m1 m
) (or h1 h
)
1619 (or d1 d
) (or month1 month
) (or y1 y
)))
1620 (setq fm
(cdr org-time-stamp-formats
))
1622 ((memq key
'(day today
))
1623 (setq txt
(format-time-string "%A, %B %d, %Y" ts
)))
1624 ((memq key
'(week thisweek
))
1625 (setq txt
(format-time-string "week %G-W%V" ts
)))
1626 ((memq key
'(month thismonth
))
1627 (setq txt
(format-time-string "%B %Y" ts
)))
1628 ((memq key
'(year thisyear
))
1629 (setq txt
(format-time-string "the year %Y" ts
))))
1631 (list (format-time-string fm ts
) (format-time-string fm te
) txt
)
1634 (defun org-clocktable-shift (dir n
)
1635 "Try to shift the :block date of the clocktable at point.
1636 Point must be in the #+BEGIN: line of a clocktable, or this function
1637 will throw an error.
1638 DIR is a direction, a symbol `left', `right', `up', or `down'.
1639 Both `left' and `down' shift the block toward the past, `up' and `right'
1640 push it toward the future.
1641 N is the number of shift steps to take. The size of the step depends on
1642 the currently selected interval size."
1643 (setq n
(prefix-numeric-value n
))
1644 (and (memq dir
'(left down
)) (setq n
(- n
)))
1646 (goto-char (point-at-bol))
1647 (if (not (looking-at "#\\+BEGIN: clocktable\\>.*?:block[ \t]+\\(\\S-+\\)"))
1648 (error "Line needs a :block definition before this command works")
1649 (let* ((b (match-beginning 1)) (e (match-end 1))
1650 (s (match-string 1))
1651 block shift ins y mw d date wp m
)
1653 ((equal s
"yesterday") (setq s
"today-1"))
1654 ((equal s
"lastweek") (setq s
"thisweek-1"))
1655 ((equal s
"lastmonth") (setq s
"thismonth-1"))
1656 ((equal s
"lastyear") (setq s
"thisyear-1")))
1658 ((string-match "^\\(today\\|thisweek\\|thismonth\\|thisyear\\)\\([-+][0-9]+\\)?$" s
)
1659 (setq block
(match-string 1 s
)
1660 shift
(if (match-end 2)
1661 (string-to-number (match-string 2 s
))
1663 (setq shift
(+ shift n
))
1664 (setq ins
(if (= shift
0) block
(format "%s%+d" block shift
))))
1665 ((string-match "\\([0-9]+\\)\\(-\\([wW]?\\)\\([0-9]\\{1,2\\}\\)\\(-\\([0-9]\\{1,2\\}\\)\\)?\\)?" s
)
1666 ;; 1 1 2 3 3 4 4 5 6 6 5 2
1667 (setq y
(string-to-number (match-string 1 s
))
1668 wp
(and (match-end 3) (match-string 3 s
))
1669 mw
(and (match-end 4) (string-to-number (match-string 4 s
)))
1670 d
(and (match-end 6) (string-to-number (match-string 6 s
))))
1672 (d (setq ins
(format-time-string
1674 (encode-time 0 0 0 (+ d n
) m y
))))
1675 ((and wp mw
(> (length wp
) 0))
1677 (setq date
(calendar-gregorian-from-absolute (calendar-absolute-from-iso (list (+ mw n
) 1 y
))))
1678 (setq ins
(format-time-string
1680 (encode-time 0 0 0 (nth 1 date
) (car date
) (nth 2 date
)))))
1682 (setq ins
(format-time-string
1684 (encode-time 0 0 0 1 (+ mw n
) y
))))
1686 (setq ins
(number-to-string (+ y n
))))))
1687 (t (error "Cannot shift clocktable block")))
1691 (delete-region (point) (+ (point) (- e b
)))
1692 (beginning-of-line 1)
1696 (defun org-dblock-write:clocktable
(params)
1697 "Write the standard clocktable."
1699 (let* ((hlchars '((1 .
"*") (2 .
"/")))
1702 (scope (plist-get params
:scope
))
1703 (tostring (plist-get params
:tostring
))
1704 (multifile (plist-get params
:multifile
))
1705 (header (plist-get params
:header
))
1706 (maxlevel (or (plist-get params
:maxlevel
) 3))
1707 (step (plist-get params
:step
))
1708 (emph (plist-get params
:emphasize
))
1709 (timestamp (plist-get params
:timestamp
))
1710 (ts (plist-get params
:tstart
))
1711 (te (plist-get params
:tend
))
1712 (block (plist-get params
:block
))
1713 (link (plist-get params
:link
))
1714 (tags (plist-get params
:tags
))
1715 (matcher (if tags
(cdr (org-make-tags-matcher tags
))))
1716 ipos time p level hlc hdl tsp props content recalc formula pcol
1717 cc beg end pos tbl tbl1 range-text rm-file-column scope-is-list st
)
1718 (setq org-clock-file-total-minutes nil
)
1720 (unless (or block
(and ts te
))
1721 (error "Clocktable `:step' can only be used with `:block' or `:tstart,:end'"))
1722 (org-clocktable-steps params
)
1725 (setq cc
(org-clock-special-range block nil t
)
1726 ts
(car cc
) te
(nth 1 cc
) range-text
(nth 2 cc
)))
1727 (when (integerp ts
) (setq ts
(calendar-gregorian-from-absolute ts
)))
1728 (when (integerp te
) (setq te
(calendar-gregorian-from-absolute te
)))
1729 (when (and ts
(listp ts
))
1730 (setq ts
(format "%4d-%02d-%02d" (nth 2 ts
) (car ts
) (nth 1 ts
))))
1731 (when (and te
(listp te
))
1732 (setq te
(format "%4d-%02d-%02d" (nth 2 te
) (car te
) (nth 1 te
))))
1733 ;; Now the times are strings we can parse.
1734 (if ts
(setq ts
(org-float-time
1735 (apply 'encode-time
(org-parse-time-string ts
)))))
1736 (if te
(setq te
(org-float-time
1737 (apply 'encode-time
(org-parse-time-string te
)))))
1738 (move-marker ins
(point))
1741 ;; Get the right scope
1744 ((and scope
(listp scope
) (symbolp (car scope
)))
1745 (setq scope
(eval scope
)))
1747 (setq scope
(org-agenda-files t
)))
1748 ((eq scope
'agenda-with-archives
)
1749 (setq scope
(org-agenda-files t
))
1750 (setq scope
(org-add-archive-files scope
)))
1751 ((eq scope
'file-with-archives
)
1752 (setq scope
(org-add-archive-files (list (buffer-file-name)))
1754 (setq scope-is-list
(and scope
(listp scope
)))
1758 ((eq scope
'file
) (widen))
1759 ((eq scope
'subtree
) (org-narrow-to-subtree))
1761 (while (org-up-heading-safe))
1762 (org-narrow-to-subtree))
1763 ((and (symbolp scope
) (string-match "^tree\\([0-9]+\\)$"
1764 (symbol-name scope
)))
1765 (setq level
(string-to-number (match-string 1 (symbol-name scope
))))
1767 (while (org-up-heading-safe)
1768 (looking-at outline-regexp
)
1769 (if (<= (org-reduced-level (funcall outline-level
)) level
)
1770 (throw 'exit nil
))))
1771 (org-narrow-to-subtree))
1773 (let* ((files scope
)
1775 (p1 (copy-sequence params
))
1777 (setq p1
(plist-put p1
:tostring t
))
1778 (setq p1
(plist-put p1
:multifile t
))
1779 (setq p1
(plist-put p1
:scope
'file
))
1780 (org-prepare-agenda-buffers files
)
1781 (while (setq file
(pop files
))
1782 (with-current-buffer (find-buffer-visiting file
)
1783 (setq tbl1
(org-dblock-write:clocktable p1
))
1785 (push (org-clocktable-add-file
1787 (concat "| |*File time*|*"
1788 (org-minutes-to-hh:mm-string
1789 org-clock-file-total-minutes
)
1792 (setq total-time
(+ (or total-time
0)
1793 org-clock-file-total-minutes
))))))))
1796 (unless scope-is-list
1797 (org-clock-sum ts te
1798 (unless (null matcher
)
1802 (or (org-entry-get (point) "ALLTAGS") "")
1805 (goto-char (point-min))
1807 (while (or (and (bobp) (prog1 st
(setq st nil
))
1808 (get-text-property (point) :org-clock-minutes
)
1809 (setq p
(point-min)))
1810 (setq p
(next-single-property-change (point) :org-clock-minutes
)))
1812 (when (setq time
(get-text-property p
:org-clock-minutes
))
1814 (beginning-of-line 1)
1815 (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
1816 (setq level
(org-reduced-level
1817 (- (match-end 1) (match-beginning 1))))
1818 (<= level maxlevel
))
1819 (setq hlc
(if emph
(or (cdr (assoc level hlchars
)) "") "")
1822 (org-make-link-string
1823 (format "file:%s::%s"
1826 (org-make-org-heading-search-string
1830 (setq props
(org-entry-properties (point)))
1831 (or (cdr (assoc "SCHEDULED" props
))
1832 (cdr (assoc "TIMESTAMP" props
))
1833 (cdr (assoc "DEADLINE" props
))
1834 (cdr (assoc "TIMESTAMP_IA" props
)))))
1835 (if (and (not multifile
) (= level
1)) (push "|-" tbl
))
1837 "| " (int-to-string level
) "|"
1838 (if timestamp
(concat tsp
"|") "")
1840 (make-string (1- level
) ?|
)
1841 hlc
(org-minutes-to-hh:mm-string time
) hlc
1843 (setq tbl
(nreverse tbl
))
1845 (if tbl
(mapconcat 'identity tbl
"\n") nil
)
1847 (insert-before-markers
1850 "Clock summary at ["
1852 (format-time-string (cdr org-time-stamp-formats
))
1855 (if block
(concat ", for " range-text
".") "")
1857 (if scope-is-list
"|File" "")
1858 "|L|" (if timestamp
"Timestamp|" "") "Headline|Time|\n")
1859 (setq total-time
(or total-time org-clock-file-total-minutes
))
1860 (insert-before-markers
1862 (if scope-is-list
"|" "")
1863 (if timestamp
"|Timestamp|" "|")
1865 (org-minutes-to-hh:mm-string
(or total-time
0))
1867 (setq tbl
(delq nil tbl
))
1868 (if (and (stringp (car tbl
)) (> (length (car tbl
)) 1)
1869 (equal (substring (car tbl
) 0 2) "|-"))
1871 (insert-before-markers (mapconcat
1872 'identity
(delq nil tbl
)
1873 (if scope-is-list
"\n|-\n" "\n")))
1874 (backward-delete-char 1)
1875 (if (setq formula
(plist-get params
:formula
))
1878 (setq pcol
(+ (if scope-is-list
1 0) maxlevel
3))
1881 "\n#+TBLFM: $%d='(org-clock-time%% @%d$%d $%d..$%d);%%.1f"
1884 (+ 3 (if scope-is-list
1 0))
1885 (+ (if scope-is-list
1 0) 3)
1889 (insert "\n#+TBLFM: " formula
)
1891 (t (error "invalid formula in clocktable")))
1892 ;; Should we rescue an old formula?
1893 (when (stringp (setq content
(plist-get params
:content
)))
1894 (when (string-match "^\\([ \t]*#\\+TBLFM:.*\\)" content
)
1896 (insert "\n" (match-string 1 (plist-get params
:content
)))
1897 (beginning-of-line 0))))
1899 (skip-chars-forward "^|")
1903 (save-excursion (org-table-goto-column pcol nil
'force
)
1905 (org-table-recalculate 'all
))
1906 (when rm-file-column
1908 (org-table-delete-column)))))))
1910 (defun org-clocktable-steps (params)
1911 (let* ((p1 (copy-sequence params
))
1912 (ts (plist-get p1
:tstart
))
1913 (te (plist-get p1
:tend
))
1914 (step0 (plist-get p1
:step
))
1915 (step (cdr (assoc step0
'((day .
86400) (week .
604800)))))
1916 (block (plist-get p1
:block
))
1919 (setq cc
(org-clock-special-range block nil t
)
1920 ts
(car cc
) te
(nth 1 cc
) range-text
(nth 2 cc
)))
1921 (if ts
(setq ts
(org-float-time
1922 (apply 'encode-time
(org-parse-time-string ts
)))))
1923 (if te
(setq te
(org-float-time
1924 (apply 'encode-time
(org-parse-time-string te
)))))
1925 (setq p1
(plist-put p1
:header
""))
1926 (setq p1
(plist-put p1
:step nil
))
1927 (setq p1
(plist-put p1
:block nil
))
1929 (or (bolp) (insert "\n"))
1930 (setq p1
(plist-put p1
:tstart
(format-time-string
1931 (org-time-stamp-format nil t
)
1932 (seconds-to-time ts
))))
1933 (setq p1
(plist-put p1
:tend
(format-time-string
1934 (org-time-stamp-format nil t
)
1935 (seconds-to-time (setq ts
(+ ts step
))))))
1936 (insert "\n" (if (eq step0
'day
) "Daily report: " "Weekly report starting on: ")
1937 (plist-get p1
:tstart
) "\n")
1938 (org-dblock-write:clocktable p1
)
1939 (re-search-forward "#\\+END:")
1942 (defun org-clocktable-add-file (file table
)
1944 (let ((lines (org-split-string table
"\n"))
1945 (ff (file-name-nondirectory file
)))
1946 (mapconcat 'identity
1948 (if (string-match org-table-dataline-regexp x
)
1954 (defun org-clock-time%
(total &rest strings
)
1955 "Compute a time fraction in percent.
1956 TOTAL s a time string like 10:21 specifying the total times.
1957 STRINGS is a list of strings that should be checked for a time.
1958 The first string that does have a time will be used.
1959 This function is made for clock tables."
1960 (let ((re "\\([0-9]+\\):\\([0-9]+\\)")
1964 (if (not (string-match re total
))
1966 (setq tot
(+ (string-to-number (match-string 2 total
))
1967 (* 60 (string-to-number (match-string 1 total
)))))
1968 (if (= tot
0.
) (throw 'exit
0.
)))
1969 (while (setq s
(pop strings
))
1970 (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s
)
1972 (/ (* 100.0 (+ (string-to-number (match-string 2 s
))
1973 (* 60 (string-to-number (match-string 1 s
)))))
1977 (defvar org-clock-loaded nil
1978 "Was the clock file loaded?")
1980 (defun org-clock-save ()
1981 "Persist various clock-related data to disk.
1982 The details of what will be saved are regulated by the variable
1983 `org-clock-persist'."
1984 (when (and org-clock-persist
1985 (or org-clock-loaded
1986 org-clock-has-been-used
1987 (not (file-exists-p org-clock-persist-file
))))
1989 (with-current-buffer (find-file (expand-file-name org-clock-persist-file
))
1991 (delete-region (point-min) (point-max))
1993 (insert (format ";; org-persist.el - %s at %s\n"
1994 system-name
(format-time-string
1995 (cdr org-time-stamp-formats
))))
1996 (if (and (memq org-clock-persist
'(t clock
))
1997 (setq b
(org-clocking-buffer))
1998 (setq b
(or (buffer-base-buffer b
) b
))
2000 (buffer-file-name b
)
2001 (or (not org-clock-persist-query-save
)
2002 (y-or-n-p (concat "Save current clock ("
2003 (substring-no-properties org-clock-heading
)
2005 (insert "(setq resume-clock '(\""
2006 (buffer-file-name (org-clocking-buffer))
2007 "\" . " (int-to-string (marker-position org-clock-marker
))
2009 ;; Store clocked task history. Tasks are stored reversed to make
2011 (when (and (memq org-clock-persist
'(t history
))
2014 "(setq stored-clock-history '("
2017 (when (and (setq b
(marker-buffer m
))
2018 (setq b
(or (buffer-base-buffer b
) b
))
2020 (buffer-file-name b
))
2021 (concat "(\"" (buffer-file-name b
)
2022 "\" . " (int-to-string (marker-position m
))
2024 (reverse org-clock-history
) " ") "))\n"))
2026 (kill-buffer (current-buffer)))))))
2028 (defun org-clock-load ()
2029 "Load clock-related data from disk, maybe resuming a stored clock."
2030 (when (and org-clock-persist
(not org-clock-loaded
))
2031 (let ((filename (expand-file-name org-clock-persist-file
))
2032 (org-clock-in-resume 'auto-restart
)
2033 resume-clock stored-clock-history
)
2034 (if (not (file-readable-p filename
))
2035 (message "Not restoring clock data; %s not found"
2036 org-clock-persist-file
)
2037 (message "%s" "Restoring clock data")
2038 (setq org-clock-loaded t
)
2039 (load-file filename
)
2041 (when stored-clock-history
2042 (save-window-excursion
2043 (mapc (lambda (task)
2044 (if (file-exists-p (car task
))
2045 (org-clock-history-push (cdr task
)
2046 (find-file (car task
)))))
2047 stored-clock-history
)))
2049 (when (and resume-clock org-clock-persist
2050 (file-exists-p (car resume-clock
))
2051 (or (not org-clock-persist-query-resume
)
2055 (with-current-buffer (find-file (car resume-clock
))
2057 (goto-char (cdr resume-clock
))
2058 (org-back-to-heading t
)
2059 (and (looking-at org-complex-heading-regexp
)
2062 (when (file-exists-p (car resume-clock
))
2063 (with-current-buffer (find-file (car resume-clock
))
2064 (goto-char (cdr resume-clock
))
2065 (let ((org-clock-auto-clock-resolution nil
))
2067 (if (org-invisible-p)
2068 (org-show-context))))))))))
2071 (defun org-clock-persistence-insinuate ()
2072 "Set up hooks for clock persistence"
2073 (add-hook 'org-mode-hook
'org-clock-load
)
2074 (add-hook 'kill-emacs-hook
'org-clock-save
))
2076 ;; Suggested bindings
2077 (org-defkey org-mode-map
"\C-c\C-x\C-e" 'org-clock-modify-effort-estimate
)
2079 (provide 'org-clock
)
2081 ;; arch-tag: 7b42c5d4-9b36-48be-97c0-66a869daed4c
2083 ;;; org-clock.el ends here