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 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."
71 (defcustom org-clock-out-remove-zero-time-clocks nil
72 "Non-nil means, remove the clock line when the resulting time is zero."
76 (defcustom org-clock-in-switch-to-state nil
77 "Set task to a special todo state while clocking it.
78 The value should be the state to which the entry should be
79 switched. If the value is a function, it must take one
80 parameter (the current TODO state of the item) and return the
81 state to switch it to."
85 (const :tag
"Don't force a state" nil
)
87 (symbol :tag
"Function")))
89 (defcustom org-clock-out-switch-to-state nil
90 "Set task to a special todo state after clocking out.
91 The value should be the state to which the entry should be
92 switched. If the value is a function, it must take one
93 parameter (the current TODO state of the item) and return the
94 state to switch it to."
98 (const :tag
"Don't force a state" nil
)
100 (symbol :tag
"Function")))
102 (defcustom org-clock-history-length
5
103 "Number of clock tasks to remember in history."
107 (defcustom org-clock-goto-may-find-recent-task t
108 "Non-nil means, `org-clock-goto' can go to recent task if no active clock."
112 (defcustom org-clock-heading-function nil
113 "When non-nil, should be a function to create `org-clock-heading'.
114 This is the string shown in the mode line when a clock is running.
115 The function is called with point at the beginning of the headline."
119 (defcustom org-clock-string-limit
0
120 "Maximum length of clock strings in the modeline. 0 means no limit."
124 (defcustom org-clock-in-resume nil
125 "If non-nil, resume clock when clocking into task with open clock.
126 When clocking into a task with a clock entry which has not been closed,
127 the clock can be resumed from that point."
131 (defcustom org-clock-persist nil
132 "When non-nil, save the running clock when emacs is closed.
133 The clock is resumed when emacs restarts.
134 When this is t, both the running clock, and the entire clock
135 history are saved. When this is the symbol `clock', only the
136 running clock is saved.
138 When Emacs restarts with saved clock information, the file containing the
139 running clock as well as all files mentioned in the clock history will
141 All this depends on running `org-clock-persistence-insinuate' in .emacs"
144 (const :tag
"Just the running clock" clock
)
145 (const :tag
"Clock and history" t
)
146 (const :tag
"No persistence" nil
)))
148 (defcustom org-clock-persist-file
(convert-standard-filename
149 "~/.emacs.d/org-clock-save.el")
150 "File to save clock data to."
154 (defcustom org-clock-persist-query-save nil
155 "When non-nil, ask before saving the current clock on exit."
159 (defcustom org-clock-persist-query-resume t
160 "When non-nil, ask before resuming any stored clock during load."
164 (defcustom org-clock-sound nil
165 "Sound that will used for notifications.
169 t standard Emacs beep
170 file name play this sound file. If not possible, fall back to beep"
173 (const :tag
"No sound" nil
)
174 (const :tag
"Standard beep" t
)
175 (file :tag
"Play sound file")))
177 (defcustom org-clock-modeline-total
'auto
178 "Default setting for the time included for the modeline clock.
179 This can be overruled locally using the CLOCK_MODELINE_TOTAL property.
182 current Only the time in the current instance of the clock
183 today All time clocked inot this task today
184 repeat All time clocked into this task since last repeat
185 all All time ever recorded for this task
186 auto Automtically, either `all', or `repeat' for repeating tasks"
189 (const :tag
"Current clock" current
)
190 (const :tag
"Today's task time" today
)
191 (const :tag
"Since last repeat" repeat
)
192 (const :tag
"All task time" all
)
193 (const :tag
"Automatically, `all' or since `repeat'" auto
)))
195 ;;; The clock for measuring work time.
197 (defvar org-mode-line-string
"")
198 (put 'org-mode-line-string
'risky-local-variable t
)
200 (defvar org-clock-mode-line-timer nil
)
201 (defvar org-clock-heading
"")
202 (defvar org-clock-heading-for-remember
"")
203 (defvar org-clock-start-time
"")
205 (defvar org-clock-effort
""
206 "Effort estimate of the currently clocking task")
208 (defvar org-clock-total-time nil
209 "Holds total time, spent previously on currently clocked item.
210 This does not include the time in the currently running clock.")
212 (defvar org-clock-history nil
213 "List of marker pointing to recent clocked tasks.")
215 (defvar org-clock-default-task
(make-marker)
216 "Marker pointing to the default task that should clock time.
217 The clock can be made to switch to this task after clocking out
218 of a different task.")
220 (defvar org-clock-interrupted-task
(make-marker)
221 "Marker pointing to the task that has been interrupted by the current clock.")
223 (defvar org-clock-mode-line-map
(make-sparse-keymap))
224 (define-key org-clock-mode-line-map
[mode-line mouse-2
] 'org-clock-goto
)
226 (defun org-clock-history-push (&optional pos buffer
)
227 "Push a marker to the clock history."
228 (setq org-clock-history-length
(max 1 (min 35 org-clock-history-length
)))
229 (let ((m (move-marker (make-marker) (or pos
(point)) buffer
)) n l
)
230 (while (setq n
(member m org-clock-history
))
231 (move-marker (car n
) nil
))
232 (setq org-clock-history
234 (mapcar (lambda (x) (if (marker-buffer x
) x nil
))
236 (when (>= (setq l
(length org-clock-history
)) org-clock-history-length
)
237 (setq org-clock-history
239 (nthcdr (- l org-clock-history-length -
1)
240 (nreverse org-clock-history
)))))
241 (push m org-clock-history
)))
243 (defun org-clock-save-markers-for-cut-and-paste (beg end
)
244 "Save relative positions of markers in region."
245 (org-check-and-save-marker org-clock-marker beg end
)
246 (org-check-and-save-marker org-clock-default-task beg end
)
247 (org-check-and-save-marker org-clock-interrupted-task beg end
)
248 (mapc (lambda (m) (org-check-and-save-marker m beg end
))
251 (defun org-clock-select-task (&optional prompt
)
252 "Select a task that recently was associated with clocking."
254 (let (sel-list rpl
(i 0) s
)
255 (save-window-excursion
256 (org-switch-to-buffer-other-window
257 (get-buffer-create "*Clock Task Select*"))
259 (when (marker-buffer org-clock-default-task
)
260 (insert (org-add-props "Default Task\n" nil
'face
'bold
))
261 (setq s
(org-clock-insert-selection-line ?d org-clock-default-task
))
263 (when (marker-buffer org-clock-interrupted-task
)
264 (insert (org-add-props "The task interrupted by starting the last one\n" nil
'face
'bold
))
265 (setq s
(org-clock-insert-selection-line ?i org-clock-interrupted-task
))
267 (when (marker-buffer org-clock-marker
)
268 (insert (org-add-props "Current Clocking Task\n" nil
'face
'bold
))
269 (setq s
(org-clock-insert-selection-line ?c org-clock-marker
))
271 (insert (org-add-props "Recent Tasks\n" nil
'face
'bold
))
274 (when (marker-buffer m
)
276 s
(org-clock-insert-selection-line
282 (org-fit-window-to-buffer)
283 (message (or prompt
"Select task for clocking:"))
284 (setq rpl
(read-char-exclusive))
288 ((assoc rpl sel-list
) (cdr (assoc rpl sel-list
)))
289 (t (error "Invalid task choice %c" rpl
))))))
291 (defun org-clock-insert-selection-line (i marker
)
292 "Insert a line for the clock selection menu.
293 And return a cons cell with the selection character integer and the marker
295 (when (marker-buffer marker
)
296 (let (file cat task heading prefix
)
297 (with-current-buffer (org-base-buffer (marker-buffer marker
))
302 (setq file
(buffer-file-name (marker-buffer marker
))
303 cat
(or (org-get-category)
304 (progn (org-refresh-category-properties)
306 heading
(org-get-heading 'notags
)
307 prefix
(save-excursion
308 (org-back-to-heading t
)
312 (org-fontify-like-in-org-mode
313 (concat prefix heading
)
317 (insert (format "[%c] %-15s %s\n" i cat task
))
320 (defun org-clock-get-clock-string ()
321 "Form a clock-string, that will be show in the mode line.
322 If an effort estimate was defined for current item, use
323 01:30/01:50 format (clocked/estimated).
324 If not, show simply the clocked time like 01:50."
325 (let* ((clocked-time (org-clock-get-clocked-time))
326 (h (floor clocked-time
60))
327 (m (- clocked-time
(* 60 h
)))
329 (if (and org-clock-effort
)
330 (let* ((effort-in-minutes (org-hh:mm-string-to-minutes org-clock-effort
))
331 (effort-h (floor effort-in-minutes
60))
332 (effort-m (- effort-in-minutes
(* effort-h
60)))
334 (format (concat "-[" org-time-clocksum-format
"/" org-time-clocksum-format
" (%s)]")
335 h m effort-h effort-m org-clock-heading
)
337 (format (concat "-[" org-time-clocksum-format
" (%s)]")
338 h m org-clock-heading
))
341 (defun org-clock-update-mode-line ()
342 (setq org-mode-line-string
344 (let ((clock-string (org-clock-get-clock-string))
345 (help-text "Org-mode clock is running. Mouse-2 to go there."))
346 (if (and (> org-clock-string-limit
0)
347 (> (length clock-string
) org-clock-string-limit
))
348 (org-propertize (substring clock-string
0 org-clock-string-limit
)
349 'help-echo
(concat help-text
": " org-clock-heading
))
350 (org-propertize clock-string
'help-echo help-text
)))
351 'local-map org-clock-mode-line-map
352 'mouse-face
(if (featurep 'xemacs
) 'highlight
'mode-line-highlight
)
353 'face
'org-mode-line-clock
))
354 (if org-clock-effort
(org-clock-notify-once-if-expired))
355 (force-mode-line-update))
357 (defun org-clock-get-clocked-time ()
358 "Get the clocked time for the rrent item in minutes.
359 The time returned includes the the time spent on this task in
360 previous clocking intervals."
361 (let ((currently-clocked-time
362 (floor (- (time-to-seconds (current-time))
363 (time-to-seconds org-clock-start-time
)) 60)))
364 (+ currently-clocked-time
(or org-clock-total-time
0))))
366 (defvar org-clock-notification-was-shown nil
367 "Shows if we have shown notification already.")
369 (defun org-clock-notify-once-if-expired ()
370 "Show notification if we spent more time then we estimated before.
371 Notification is shown only once."
372 (let ((effort-in-minutes (org-hh:mm-string-to-minutes org-clock-effort
))
373 (clocked-time (org-clock-get-clocked-time)))
374 (if (>= clocked-time effort-in-minutes
)
375 (unless org-clock-notification-was-shown
376 (setq org-clock-notification-was-shown t
)
377 (org-clock-play-sound)
378 (org-show-notification
379 (format "Task '%s' should be finished by now. (%s)"
380 org-clock-heading org-clock-effort
)))
381 (setq org-clock-notification-was-shown nil
))))
383 (defun org-show-notification (notification)
384 "Show notification. Use libnotify, if available."
385 (if (org-program-exists "notify-send")
386 (start-process "emacs-timer-notification" nil
"notify-send" notification
))
387 ;; In any case, show in message area
388 (message notification
))
390 (defun org-clock-play-sound ()
391 "Play sound as configured by `org-clock-sound'.
392 Use alsa's aplay tool if available."
394 ((not org-clock-sound
))
395 ((eq org-clock-sound t
) (beep t
) (beep t
))
396 ((stringp org-clock-sound
)
397 (if (file-exists-p org-clock-sound
)
398 (if (org-program-exists "aplay")
399 (start-process "org-clock-play-notification" nil
400 "aplay" org-clock-sound
)
402 (play-sound-file org-clock-sound
)
403 (error (beep t
) (beep t
))))))))
405 (defun org-program-exists (program-name)
406 "Checks whenever we can locate program and launch it."
407 (if (eq system-type
'gnu
/linux
)
408 (= 0 (call-process "which" nil nil nil program-name
))
412 (defvar org-clock-mode-line-entry nil
413 "Information for the modeline about the running clock.")
415 (defun org-clock-in (&optional select
)
416 "Start the clock on the current item.
417 If necessary, clock-out of the currently active clock.
418 With prefix arg SELECT, offer a list of recently clocked tasks to
419 clock into. When SELECT is `C-u C-u', clock into the current task and mark
420 is as the default task, a special task that will always be offered in
421 the clocking selection, associated with the letter `d'."
424 (let ((interrupting (marker-buffer org-clock-marker
))
425 ts selected-task target-pos
(msg-extra ""))
426 (when (equal select
'(4))
427 (setq selected-task
(org-clock-select-task "Clock-in on task: "))
429 (setq selected-task
(copy-marker selected-task
))
432 ;; We are interrupting the clocking of a different task.
433 ;; Save a marker to this task, so that we can go back.
434 (move-marker org-clock-interrupted-task
435 (marker-position org-clock-marker
)
436 (marker-buffer org-clock-marker
))
439 (when (equal select
'(16))
440 ;; Mark as default clocking task
442 (org-back-to-heading t
)
443 (move-marker org-clock-default-task
(point))))
445 (setq target-pos
(point)) ;; we want to clock in at this location
447 (when (and selected-task
(marker-buffer selected-task
))
448 ;; There is a selected task, move to the correct buffer
449 ;; and set the new target position.
450 (set-buffer (org-base-buffer (marker-buffer selected-task
)))
451 (setq target-pos
(marker-position selected-task
))
452 (move-marker selected-task nil
))
456 (goto-char target-pos
)
457 (org-back-to-heading t
)
458 (or interrupting
(move-marker org-clock-interrupted-task nil
))
459 (org-clock-history-push)
460 (cond ((functionp org-clock-in-switch-to-state
)
461 (looking-at org-complex-heading-regexp
)
462 (let ((newstate (funcall org-clock-in-switch-to-state
464 (if newstate
(org-todo newstate
))))
465 ((and org-clock-in-switch-to-state
466 (not (looking-at (concat outline-regexp
"[ \t]*"
467 org-clock-in-switch-to-state
469 (org-todo org-clock-in-switch-to-state
)))
470 (setq org-clock-heading-for-remember
471 (and (looking-at org-complex-heading-regexp
)
473 (org-trim (buffer-substring (match-end 1)
475 (setq org-clock-heading
476 (cond ((and org-clock-heading-function
477 (functionp org-clock-heading-function
))
478 (funcall org-clock-heading-function
))
479 ((looking-at org-complex-heading-regexp
)
482 (setq org-clock-heading
(org-propertize org-clock-heading
484 (org-clock-find-position org-clock-in-resume
)
486 ((and org-clock-in-resume
488 (concat "^[ \t]* " org-clock-string
489 " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
490 " +\\sw+ +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")))
491 (message "Matched %s" (match-string 1))
492 (setq ts
(concat "[" (match-string 1) "]"))
493 (goto-char (match-end 1))
494 (setq org-clock-start-time
496 (org-parse-time-string (match-string 1))))
497 (setq org-clock-effort
(org-get-effort))
498 (setq org-clock-total-time
(org-clock-sum-current-item
499 (org-clock-get-sum-start))))
500 ((eq org-clock-in-resume
'auto-restart
)
501 ;; called from org-clock-load during startup,
502 ;; do not interrupt, but warn!
503 (message "Cannot restart clock because task does not contain unfinished clock")
508 (insert-before-markers "\n")
510 (org-indent-line-function)
511 (when (and (save-excursion
514 (beginning-of-line 1)
515 (org-indent-line-to (- (org-get-indentation) 2)))
516 (insert org-clock-string
" ")
517 (setq org-clock-effort
(org-get-effort))
518 (setq org-clock-total-time
(org-clock-sum-current-item
519 (org-clock-get-sum-start)))
520 (setq org-clock-start-time
(current-time))
521 (setq ts
(org-insert-time-stamp org-clock-start-time
522 'with-hm
'inactive
))))
523 (move-marker org-clock-marker
(point) (buffer-base-buffer))
524 (or global-mode-string
(setq global-mode-string
'("")))
525 (or (memq 'org-mode-line-string global-mode-string
)
526 (setq global-mode-string
527 (append global-mode-string
'(org-mode-line-string))))
528 (org-clock-update-mode-line)
529 (setq org-clock-mode-line-timer
530 (run-with-timer 60 60 'org-clock-update-mode-line
))
531 (message "Clock starts at %s - %s" ts msg-extra
)))))))
534 (defun org-clock-get-sum-start ()
535 "Return the time from which clock times should be counted.
536 This is for the currently running clock as it is displayed
537 in the mode line. This function looks at the properties
538 LAST_REPEAT and in particular CLOCK_MODELINE_TOTAL and the
539 corresponding variable `org-clock-modeline-total' and then
540 decides which time to use."
541 (let ((cmt (or (org-entry-get nil
"CLOCK_MODELINE_TOTAL")
542 (symbol-name org-clock-modeline-total
)))
543 (lr (org-entry-get nil
"LAST_REPEAT")))
545 ((equal cmt
"current")
546 (setq msg-extra
"showing time in current clock instance")
549 (setq msg-extra
"showing today's task time.")
550 (let* ((dt (decode-time (current-time))))
551 (setq dt
(append (list 0 0 0) (nthcdr 3 dt
)))
552 (if org-extend-today-until
553 (setf (nth 2 dt
) org-extend-today-until
))
554 (apply 'encode-time dt
)))
555 ((or (equal cmt
"all")
556 (and (or (not cmt
) (equal cmt
"auto"))
558 (setq msg-extra
"showing entire task time.")
560 ((or (equal cmt
"repeat")
561 (and (or (not cmt
) (equal cmt
"auto"))
563 (setq msg-extra
"showing task time since last repeat.")
566 (org-time-string-to-time lr
)))
569 (defun org-clock-find-position (find-unclosed)
570 "Find the location where the next clock line should be inserted.
571 When FIND-UNCLOSED is non-nil, first check if there is an unclosed clock
572 line and position cursor in that line."
573 (org-back-to-heading t
)
575 (let ((beg (save-excursion
576 (beginning-of-line 2)
577 (or (bolp) (newline))
579 (end (progn (outline-next-heading) (point)))
580 (re (concat "^[ \t]*" org-clock-string
))
582 (drawer (if (stringp org-clock-into-drawer
)
583 org-clock-into-drawer
"LOGBOOK"))
586 (when (and find-unclosed
588 (concat "^[ \t]* " org-clock-string
589 " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
590 " +\\sw+ +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")
592 (beginning-of-line 1)
594 (when (eobp) (newline) (setq end
(max (point) end
)))
595 (when (re-search-forward (concat "^[ \t]*:" drawer
":") end t
)
596 ;; we seem to have a CLOCK drawer, so go there.
597 (beginning-of-line 2)
598 (or org-log-states-order-reversed
599 (and (re-search-forward org-property-end-re nil t
)
600 (goto-char (match-beginning 0))))
602 ;; Lets count the CLOCK lines
604 (while (re-search-forward re end t
)
605 (setq first
(or first
(match-beginning 0))
606 last
(match-beginning 0)
608 (when (and (integerp org-clock-into-drawer
)
610 (>= (1+ cnt
) org-clock-into-drawer
))
611 ;; Wrap current entries into a new drawer
613 (setq ind-last
(org-get-indentation))
614 (beginning-of-line 2)
615 (if (and (>= (org-get-indentation) ind-last
)
619 (beginning-of-line 0)
620 (org-indent-line-to ind-last
)
622 (insert ":" drawer
":\n")
623 (beginning-of-line 0)
624 (org-indent-line-function)
626 (beginning-of-line 2)
627 (or org-log-states-order-reversed
628 (and (re-search-forward org-property-end-re nil t
)
629 (goto-char (match-beginning 0))))
633 (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
634 (not (equal (match-string 1) org-clock-string
)))
635 ;; Planning info, skip to after it
636 (beginning-of-line 2)
637 (or (bolp) (newline)))
638 (when (or (eq org-clock-into-drawer t
)
639 (stringp org-clock-into-drawer
)
640 (and (integerp org-clock-into-drawer
)
641 (< org-clock-into-drawer
2)))
642 (insert ":" drawer
":\n:END:\n")
643 (beginning-of-line -
1)
644 (org-indent-line-function)
646 (beginning-of-line 2)
647 (org-indent-line-function)
649 (or org-log-states-order-reversed
650 (and (re-search-forward org-property-end-re nil t
)
651 (goto-char (match-beginning 0))))))))
653 (defun org-clock-out (&optional fail-quietly
)
654 "Stop the currently running clock.
655 If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
658 (if (not (marker-buffer org-clock-marker
))
659 (if fail-quietly
(throw 'exit t
) (error "No active clock")))
660 (let (ts te s h m remove
)
662 (set-buffer (marker-buffer org-clock-marker
))
665 (goto-char org-clock-marker
)
666 (beginning-of-line 1)
667 (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
668 (equal (match-string 1) org-clock-string
))
669 (setq ts
(match-string 2))
670 (if fail-quietly
(throw 'exit nil
) (error "Clock start time is gone")))
671 (goto-char (match-end 0))
672 (delete-region (point) (point-at-eol))
674 (setq te
(org-insert-time-stamp (current-time) 'with-hm
'inactive
))
675 (setq s
(- (time-to-seconds (apply 'encode-time
(org-parse-time-string te
)))
676 (time-to-seconds (apply 'encode-time
(org-parse-time-string ts
))))
681 (insert " => " (format "%2d:%02d" h m
))
682 (when (setq remove
(and org-clock-out-remove-zero-time-clocks
684 (beginning-of-line 1)
685 (delete-region (point) (point-at-eol))
686 (and (looking-at "\n") (> (point-max) (1+ (point)))
688 (move-marker org-clock-marker nil
)
689 (when org-log-note-clock-out
690 (org-add-log-setup 'clock-out nil nil nil nil
691 (concat "# Task: " (org-get-heading t
) "\n\n")))
692 (when org-clock-mode-line-timer
693 (cancel-timer org-clock-mode-line-timer
)
694 (setq org-clock-mode-line-timer nil
))
695 (setq global-mode-string
696 (delq 'org-mode-line-string global-mode-string
))
697 (when org-clock-out-switch-to-state
699 (org-back-to-heading t
)
700 (let ((org-inhibit-logging t
))
702 ((functionp org-clock-out-switch-to-state
)
703 (looking-at org-complex-heading-regexp
)
704 (let ((newstate (funcall org-clock-out-switch-to-state
706 (if newstate
(org-todo newstate
))))
707 ((and org-clock-out-switch-to-state
708 (not (looking-at (concat outline-regexp
"[ \t]*"
709 org-clock-out-switch-to-state
711 (org-todo org-clock-out-switch-to-state
))))))
712 (force-mode-line-update)
713 (message (concat "Clock stopped at %s after HH:MM = " org-time-clocksum-format
"%s") te h m
714 (if remove
" => LINE REMOVED" "")))))))
716 (defun org-clock-cancel ()
717 "Cancel the running clock be removing the start timestamp."
719 (if (not (marker-buffer org-clock-marker
))
720 (error "No active clock"))
722 (set-buffer (marker-buffer org-clock-marker
))
723 (goto-char org-clock-marker
)
724 (delete-region (1- (point-at-bol)) (point-at-eol)))
725 (setq global-mode-string
726 (delq 'org-mode-line-string global-mode-string
))
727 (force-mode-line-update)
728 (message "Clock canceled"))
730 (defun org-clock-goto (&optional select
)
731 "Go to the currently clocked-in entry, or to the most recently clocked one.
732 With prefix arg SELECT, offer recently clocked tasks for selection."
737 (or (org-clock-select-task "Select task to go to: ")
738 (error "No task selected")))
739 ((marker-buffer org-clock-marker
) org-clock-marker
)
740 ((and org-clock-goto-may-find-recent-task
741 (car org-clock-history
)
742 (marker-buffer (car org-clock-history
)))
744 (car org-clock-history
))
745 (t (error "No active or recent clock task")))))
746 (switch-to-buffer (marker-buffer m
))
747 (if (or (< m
(point-min)) (> m
(point-max))) (widen))
750 (org-back-to-heading)
751 (org-cycle-hide-drawers 'children
)
754 (message "No running clock, this is the most recently clocked task"))))
757 (defvar org-clock-file-total-minutes nil
758 "Holds the file total time in minutes, after a call to `org-clock-sum'.")
759 (make-variable-buffer-local 'org-clock-file-total-minutes
)
761 (defun org-clock-sum (&optional tstart tend
)
762 "Sum the times for each subtree.
763 Puts the resulting times in minutes as a text property on each headline.
764 TSTART and TEND can mark a time range to be considered."
766 (let* ((bmp (buffer-modified-p))
767 (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
769 "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
771 (ltimes (make-vector lmax
0))
776 (if (stringp tstart
) (setq tstart
(org-time-string-to-seconds tstart
)))
777 (if (stringp tend
) (setq tend
(org-time-string-to-seconds tend
)))
778 (if (consp tstart
) (setq tstart
(time-to-seconds tstart
)))
779 (if (consp tend
) (setq tend
(time-to-seconds tend
)))
780 (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t
))
782 (goto-char (point-max))
783 (while (re-search-backward re nil t
)
787 (setq ts
(match-string 2)
790 (apply 'encode-time
(org-parse-time-string ts
)))
792 (apply 'encode-time
(org-parse-time-string te
)))
793 ts
(if tstart
(max ts tstart
) ts
)
794 te
(if tend
(min te tend
) te
)
796 t1
(if (> dt
0) (+ t1
(floor (/ dt
60))) t1
)))
799 (setq t1
(+ t1
(string-to-number (match-string 5))
800 (* 60 (string-to-number (match-string 4))))))
802 (setq level
(- (match-end 1) (match-beginning 1)))
803 (when (or (> t1
0) (> (aref ltimes level
) 0))
804 (loop for l from
0 to level do
805 (aset ltimes l
(+ (aref ltimes l
) t1
)))
806 (setq t1
0 time
(aref ltimes level
))
807 (loop for l from level to
(1- lmax
) do
809 (goto-char (match-beginning 0))
810 (put-text-property (point) (point-at-eol) :org-clock-minutes time
)))))
811 (setq org-clock-file-total-minutes
(aref ltimes
0)))
812 (set-buffer-modified-p bmp
)))
814 (defun org-clock-sum-current-item (&optional tstart
)
815 "Returns time, clocked on current item in total"
818 (org-narrow-to-subtree)
819 (org-clock-sum tstart
)
820 org-clock-file-total-minutes
)))
822 (defun org-clock-display (&optional total-only
)
823 "Show subtree times in the entire buffer.
824 If TOTAL-ONLY is non-nil, only show the total time for the entire file
827 (org-clock-remove-overlays)
832 (goto-char (point-min))
833 (while (or (and (equal (setq p
(point)) (point-min))
834 (get-text-property p
:org-clock-minutes
))
835 (setq p
(next-single-property-change
836 (point) :org-clock-minutes
)))
838 (when (setq time
(get-text-property p
:org-clock-minutes
))
839 (org-clock-put-overlay time
(funcall outline-level
))))
840 (setq h
(/ org-clock-file-total-minutes
60)
841 m
(- org-clock-file-total-minutes
(* 60 h
)))
842 ;; Arrange to remove the overlays upon next change.
843 (when org-remove-highlights-with-change
844 (org-add-hook 'before-change-functions
'org-clock-remove-overlays
846 (message (concat "Total file time: " org-time-clocksum-format
" (%d hours and %d minutes)") h m h m
)))
848 (defvar org-clock-overlays nil
)
849 (make-variable-buffer-local 'org-clock-overlays
)
851 (defun org-clock-put-overlay (time &optional level
)
852 "Put an overlays on the current line, displaying TIME.
853 If LEVEL is given, prefix time with a corresponding number of stars.
854 This creates a new overlay and stores it in `org-clock-overlays', so that it
855 will be easy to remove."
856 (let* ((c 60) (h (floor (/ time
60))) (m (- time
(* 60 h
)))
857 (l (if level
(org-get-valid-level level
0) 0))
858 (fmt (concat "%s " org-time-clocksum-format
"%s"))
861 (org-move-to-column c
)
862 (unless (eolp) (skip-chars-backward "^ \t"))
863 (skip-chars-backward " \t")
864 (setq ov
(org-make-overlay (1- (point)) (point-at-eol))
865 tx
(concat (buffer-substring (1- (point)) (point))
866 (make-string (+ off
(max 0 (- c
(current-column)))) ?.
)
867 (org-add-props (format fmt
868 (make-string l ?
*) h m
869 (make-string (- 16 l
) ?\
))
870 (list 'face
'org-clock-overlay
))
872 (if (not (featurep 'xemacs
))
873 (org-overlay-put ov
'display tx
)
874 (org-overlay-put ov
'invisible t
)
875 (org-overlay-put ov
'end-glyph
(make-glyph tx
)))
876 (push ov org-clock-overlays
)))
878 (defun org-clock-remove-overlays (&optional beg end noremove
)
879 "Remove the occur highlights from the buffer.
880 BEG and END are ignored. If NOREMOVE is nil, remove this function
881 from the `before-change-functions' in the current buffer."
883 (unless org-inhibit-highlight-removal
884 (mapc 'org-delete-overlay org-clock-overlays
)
885 (setq org-clock-overlays nil
)
887 (remove-hook 'before-change-functions
888 'org-clock-remove-overlays
'local
))))
890 (defvar state
) ;; dynamically scoped into this function
891 (defun org-clock-out-if-current ()
892 "Clock out if the current entry contains the running clock.
893 This is used to stop the clock after a TODO entry is marked DONE,
894 and is only done if the variable `org-clock-out-when-done' is not nil."
895 (when (and org-clock-out-when-done
896 (member state org-done-keywords
)
897 (equal (or (buffer-base-buffer (marker-buffer org-clock-marker
))
898 (marker-buffer org-clock-marker
))
899 (or (buffer-base-buffer (current-buffer))
901 (< (point) org-clock-marker
)
902 (> (save-excursion (outline-next-heading) (point))
904 ;; Clock out, but don't accept a logging message for this.
905 (let ((org-log-note-clock-out nil
))
908 (add-hook 'org-after-todo-state-change-hook
909 'org-clock-out-if-current
)
912 (defun org-get-clocktable (&rest props
)
913 "Get a formatted clocktable with parameters according to PROPS.
914 The table is created in a temporary buffer, fully formatted and
915 fontified, and then returned."
917 (setq props
(plist-put props
:name
"clocktable"))
918 (unless (plist-member props
:maxlevel
)
919 (setq props
(plist-put props
:maxlevel
2)))
920 (unless (plist-member props
:scope
)
921 (setq props
(plist-put props
:scope
'agenda
)))
924 (org-create-dblock props
)
926 (font-lock-fontify-buffer)
928 (buffer-substring (point) (progn
929 (re-search-forward "^#\\+END" nil t
)
932 (defun org-clock-report (&optional arg
)
933 "Create a table containing a report about clocked time.
934 If the cursor is inside an existing clocktable block, then the table
935 will be updated. If not, a new clocktable will be inserted.
936 When called with a prefix argument, move to the first clock table in the
937 buffer and update it."
939 (org-clock-remove-overlays)
941 (org-find-dblock "clocktable")
943 (if (org-in-clocktable-p)
944 (goto-char (org-in-clocktable-p))
945 (org-create-dblock (list :name
"clocktable"
946 :maxlevel
2 :scope
'file
)))
949 (defun org-in-clocktable-p ()
950 "Check if the cursor is in a clocktable."
951 (let ((pos (point)) start
)
954 (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t
)
955 (setq start
(match-beginning 0))
956 (re-search-forward "^#\\+END:.*" nil t
)
957 (>= (match-end 0) pos
)
960 (defun org-clock-special-range (key &optional time as-strings
)
961 "Return two times bordering a special time range.
962 Key is a symbol specifying the range and can be one of `today', `yesterday',
963 `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
964 A week starts Monday 0:00 and ends Sunday 24:00.
965 The range is determined relative to TIME. TIME defaults to the current time.
966 The return value is a cons cell with two internal times like the ones
967 returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
968 the returned times will be formatted strings."
969 (if (integerp key
) (setq key
(intern (number-to-string key
))))
970 (let* ((tm (decode-time (or time
(current-time))))
971 (s 0) (m (nth 1 tm
)) (h (nth 2 tm
))
972 (d (nth 3 tm
)) (month (nth 4 tm
)) (y (nth 5 tm
))
974 (skey (symbol-name key
))
976 s1 m1 h1 d1 month1 y1 diff ts te fm txt w date
)
978 ((string-match "^[0-9]+$" skey
)
979 (setq y
(string-to-number skey
) m
1 d
1 key
'year
))
980 ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)$" skey
)
981 (setq y
(string-to-number (match-string 1 skey
))
982 month
(string-to-number (match-string 2 skey
))
984 ((string-match "^\\([0-9]+\\)-[wW]\\([0-9]\\{1,2\\}\\)$" skey
)
986 (setq y
(string-to-number (match-string 1 skey
))
987 w
(string-to-number (match-string 2 skey
)))
988 (setq date
(calendar-gregorian-from-absolute
989 (calendar-absolute-from-iso (list w
1 y
))))
990 (setq d
(nth 1 date
) month
(car date
) y
(nth 2 date
)
993 ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)-\\([0-9]\\{1,2\\}\\)$" skey
)
994 (setq y
(string-to-number (match-string 1 skey
))
995 month
(string-to-number (match-string 2 skey
))
996 d
(string-to-number (match-string 3 skey
))
998 ((string-match "\\([-+][0-9]+\\)$" skey
)
999 (setq shift
(string-to-number (match-string 1 skey
))
1000 key
(intern (substring skey
0 (match-beginning 1))))))
1002 (cond ((eq key
'yesterday
) (setq key
'today shift -
1))
1003 ((eq key
'lastweek
) (setq key
'week shift -
1))
1004 ((eq key
'lastmonth
) (setq key
'month shift -
1))
1005 ((eq key
'lastyear
) (setq key
'year shift -
1))))
1007 ((memq key
'(day today
))
1008 (setq d
(+ d shift
) h
0 m
0 h1
24 m1
0))
1009 ((memq key
'(week thisweek
))
1010 (setq diff
(+ (* -
7 shift
) (if (= dow
0) 6 (1- dow
)))
1011 m
0 h
0 d
(- d diff
) d1
(+ 7 d
)))
1012 ((memq key
'(month thismonth
))
1013 (setq d
1 h
0 m
0 d1
1 month
(+ month shift
) month1
(1+ month
) h1
0 m1
0))
1014 ((memq key
'(year thisyear
))
1015 (setq m
0 h
0 d
1 month
1 y
(+ y shift
) y1
(1+ y
)))
1016 (t (error "No such time block %s" key
)))
1017 (setq ts
(encode-time s m h d month y
)
1018 te
(encode-time (or s1 s
) (or m1 m
) (or h1 h
)
1019 (or d1 d
) (or month1 month
) (or y1 y
)))
1020 (setq fm
(cdr org-time-stamp-formats
))
1022 ((memq key
'(day today
))
1023 (setq txt
(format-time-string "%A, %B %d, %Y" ts
)))
1024 ((memq key
'(week thisweek
))
1025 (setq txt
(format-time-string "week %G-W%V" ts
)))
1026 ((memq key
'(month thismonth
))
1027 (setq txt
(format-time-string "%B %Y" ts
)))
1028 ((memq key
'(year thisyear
))
1029 (setq txt
(format-time-string "the year %Y" ts
))))
1031 (list (format-time-string fm ts
) (format-time-string fm te
) txt
)
1034 (defun org-clocktable-shift (dir n
)
1035 "Try to shift the :block date of the clocktable at point.
1036 Point must be in the #+BEGIN: line of a clocktable, or this function
1037 will throw an error.
1038 DIR is a direction, a symbol `left', `right', `up', or `down'.
1039 Both `left' and `down' shift the block toward the past, `up' and `right'
1040 push it toward the future.
1041 N is the number of shift steps to take. The size of the step depends on
1042 the currently selected interval size."
1043 (setq n
(prefix-numeric-value n
))
1044 (and (memq dir
'(left down
)) (setq n
(- n
)))
1046 (goto-char (point-at-bol))
1047 (if (not (looking-at "#\\+BEGIN: clocktable\\>.*?:block[ \t]+\\(\\S-+\\)"))
1048 (error "Line needs a :block definition before this command works")
1049 (let* ((b (match-beginning 1)) (e (match-end 1))
1050 (s (match-string 1))
1051 block shift ins y mw d date wp m
)
1053 ((equal s
"yesterday") (setq s
"today-1"))
1054 ((equal s
"lastweek") (setq s
"thisweek-1"))
1055 ((equal s
"lastmonth") (setq s
"thismonth-1"))
1056 ((equal s
"lastyear") (setq s
"thisyear-1")))
1058 ((string-match "^\\(today\\|thisweek\\|thismonth\\|thisyear\\)\\([-+][0-9]+\\)?$" s
)
1059 (setq block
(match-string 1 s
)
1060 shift
(if (match-end 2)
1061 (string-to-number (match-string 2 s
))
1063 (setq shift
(+ shift n
))
1064 (setq ins
(if (= shift
0) block
(format "%s%+d" block shift
))))
1065 ((string-match "\\([0-9]+\\)\\(-\\([wW]?\\)\\([0-9]\\{1,2\\}\\)\\(-\\([0-9]\\{1,2\\}\\)\\)?\\)?" s
)
1066 ;; 1 1 2 3 3 4 4 5 6 6 5 2
1067 (setq y
(string-to-number (match-string 1 s
))
1068 wp
(and (match-end 3) (match-string 3 s
))
1069 mw
(and (match-end 4) (string-to-number (match-string 4 s
)))
1070 d
(and (match-end 6) (string-to-number (match-string 6 s
))))
1072 (d (setq ins
(format-time-string
1074 (encode-time 0 0 0 (+ d n
) m y
))))
1075 ((and wp mw
(> (length wp
) 0))
1077 (setq date
(calendar-gregorian-from-absolute (calendar-absolute-from-iso (list (+ mw n
) 1 y
))))
1078 (setq ins
(format-time-string
1080 (encode-time 0 0 0 (nth 1 date
) (car date
) (nth 2 date
)))))
1082 (setq ins
(format-time-string
1084 (encode-time 0 0 0 1 (+ mw n
) y
))))
1086 (setq ins
(number-to-string (+ y n
))))))
1087 (t (error "Cannot shift clocktable block")))
1091 (delete-region (point) (+ (point) (- e b
)))
1092 (beginning-of-line 1)
1096 (defun org-dblock-write:clocktable
(params)
1097 "Write the standard clocktable."
1099 (let* ((hlchars '((1 .
"*") (2 .
"/")))
1102 (scope (plist-get params
:scope
))
1103 (tostring (plist-get params
:tostring
))
1104 (multifile (plist-get params
:multifile
))
1105 (header (plist-get params
:header
))
1106 (maxlevel (or (plist-get params
:maxlevel
) 3))
1107 (step (plist-get params
:step
))
1108 (emph (plist-get params
:emphasize
))
1109 (ts (plist-get params
:tstart
))
1110 (te (plist-get params
:tend
))
1111 (block (plist-get params
:block
))
1112 (link (plist-get params
:link
))
1113 ipos time p level hlc hdl content recalc formula pcol
1114 cc beg end pos tbl tbl1 range-text rm-file-column scope-is-list st
)
1115 (setq org-clock-file-total-minutes nil
)
1117 (unless (or block
(and ts te
))
1118 (error "Clocktable `:step' can only be used with `:block' or `:tstart,:end'"))
1119 (org-clocktable-steps params
)
1122 (setq cc
(org-clock-special-range block nil t
)
1123 ts
(car cc
) te
(nth 1 cc
) range-text
(nth 2 cc
)))
1124 (when (integerp ts
) (setq ts
(calendar-gregorian-from-absolute ts
)))
1125 (when (integerp te
) (setq te
(calendar-gregorian-from-absolute te
)))
1126 (when (and ts
(listp ts
))
1127 (setq ts
(format "%4d-%02d-%02d" (nth 2 ts
) (car ts
) (nth 1 ts
))))
1128 (when (and te
(listp te
))
1129 (setq te
(format "%4d-%02d-%02d" (nth 2 te
) (car te
) (nth 1 te
))))
1130 ;; Now the times are strings we can parse.
1131 (if ts
(setq ts
(time-to-seconds
1132 (apply 'encode-time
(org-parse-time-string ts
)))))
1133 (if te
(setq te
(time-to-seconds
1134 (apply 'encode-time
(org-parse-time-string te
)))))
1135 (move-marker ins
(point))
1138 ;; Get the right scope
1141 ((and scope
(listp scope
) (symbolp (car scope
)))
1142 (setq scope
(eval scope
)))
1144 (setq scope
(org-agenda-files t
)))
1145 ((eq scope
'agenda-with-archives
)
1146 (setq scope
(org-agenda-files t
))
1147 (setq scope
(org-add-archive-files scope
)))
1148 ((eq scope
'file-with-archives
)
1149 (setq scope
(org-add-archive-files (list (buffer-file-name)))
1151 (setq scope-is-list
(and scope
(listp scope
)))
1155 ((eq scope
'file
) (widen))
1156 ((eq scope
'subtree
) (org-narrow-to-subtree))
1158 (while (org-up-heading-safe))
1159 (org-narrow-to-subtree))
1160 ((and (symbolp scope
) (string-match "^tree\\([0-9]+\\)$"
1161 (symbol-name scope
)))
1162 (setq level
(string-to-number (match-string 1 (symbol-name scope
))))
1164 (while (org-up-heading-safe)
1165 (looking-at outline-regexp
)
1166 (if (<= (org-reduced-level (funcall outline-level
)) level
)
1167 (throw 'exit nil
))))
1168 (org-narrow-to-subtree))
1170 (let* ((files scope
)
1172 (p1 (copy-sequence params
))
1174 (setq p1
(plist-put p1
:tostring t
))
1175 (setq p1
(plist-put p1
:multifile t
))
1176 (setq p1
(plist-put p1
:scope
'file
))
1177 (org-prepare-agenda-buffers files
)
1178 (while (setq file
(pop files
))
1179 (with-current-buffer (find-buffer-visiting file
)
1180 (setq tbl1
(org-dblock-write:clocktable p1
))
1182 (push (org-clocktable-add-file
1184 (concat "| |*File time*|*"
1185 (org-minutes-to-hh:mm-string
1186 org-clock-file-total-minutes
)
1189 (setq total-time
(+ (or total-time
0)
1190 org-clock-file-total-minutes
))))))))
1193 (unless scope-is-list
1194 (org-clock-sum ts te
)
1195 (goto-char (point-min))
1197 (while (or (and (bobp) (prog1 st
(setq st nil
))
1198 (get-text-property (point) :org-clock-minutes
)
1199 (setq p
(point-min)))
1200 (setq p
(next-single-property-change (point) :org-clock-minutes
)))
1202 (when (setq time
(get-text-property p
:org-clock-minutes
))
1204 (beginning-of-line 1)
1205 (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
1206 (setq level
(org-reduced-level
1207 (- (match-end 1) (match-beginning 1))))
1208 (<= level maxlevel
))
1209 (setq hlc
(if emph
(or (cdr (assoc level hlchars
)) "") "")
1212 (org-make-link-string
1213 (format "file:%s::%s"
1216 (org-make-org-heading-search-string
1219 (if (and (not multifile
) (= level
1)) (push "|-" tbl
))
1221 "| " (int-to-string level
) "|" hlc hdl hlc
" |"
1222 (make-string (1- level
) ?|
)
1223 hlc
(org-minutes-to-hh:mm-string time
) hlc
1225 (setq tbl
(nreverse tbl
))
1227 (if tbl
(mapconcat 'identity tbl
"\n") nil
)
1229 (insert-before-markers
1232 "Clock summary at ["
1234 (format-time-string (cdr org-time-stamp-formats
))
1237 (if block
(concat ", for " range-text
".") "")
1239 (if scope-is-list
"|File" "")
1240 "|L|Headline|Time|\n")
1241 (setq total-time
(or total-time org-clock-file-total-minutes
))
1242 (insert-before-markers
1244 (if scope-is-list
"|" "")
1247 (org-minutes-to-hh:mm-string
(or total-time
0))
1249 (setq tbl
(delq nil tbl
))
1250 (if (and (stringp (car tbl
)) (> (length (car tbl
)) 1)
1251 (equal (substring (car tbl
) 0 2) "|-"))
1253 (insert-before-markers (mapconcat
1254 'identity
(delq nil tbl
)
1255 (if scope-is-list
"\n|-\n" "\n")))
1256 (backward-delete-char 1)
1257 (if (setq formula
(plist-get params
:formula
))
1260 (setq pcol
(+ (if scope-is-list
1 0) maxlevel
3))
1263 "\n#+TBLFM: $%d='(org-clock-time%% @%d$%d $%d..$%d);%%.1f"
1266 (+ 3 (if scope-is-list
1 0))
1267 (+ (if scope-is-list
1 0) 3)
1271 (insert "\n#+TBLFM: " formula
)
1273 (t (error "invalid formula in clocktable")))
1274 ;; Should we rescue an old formula?
1275 (when (stringp (setq content
(plist-get params
:content
)))
1276 (when (string-match "^\\([ \t]*#\\+TBLFM:.*\\)" content
)
1278 (insert "\n" (match-string 1 (plist-get params
:content
)))
1279 (beginning-of-line 0))))
1281 (skip-chars-forward "^|")
1285 (save-excursion (org-table-goto-column pcol nil
'force
)
1287 (org-table-recalculate 'all
))
1288 (when rm-file-column
1290 (org-table-delete-column)))))))
1292 (defun org-clocktable-steps (params)
1293 (let* ((p1 (copy-sequence params
))
1294 (ts (plist-get p1
:tstart
))
1295 (te (plist-get p1
:tend
))
1296 (step0 (plist-get p1
:step
))
1297 (step (cdr (assoc step0
'((day .
86400) (week .
604800)))))
1298 (block (plist-get p1
:block
))
1301 (setq cc
(org-clock-special-range block nil t
)
1302 ts
(car cc
) te
(nth 1 cc
) range-text
(nth 2 cc
)))
1303 (if ts
(setq ts
(time-to-seconds
1304 (apply 'encode-time
(org-parse-time-string ts
)))))
1305 (if te
(setq te
(time-to-seconds
1306 (apply 'encode-time
(org-parse-time-string te
)))))
1307 (setq p1
(plist-put p1
:header
""))
1308 (setq p1
(plist-put p1
:step nil
))
1309 (setq p1
(plist-put p1
:block nil
))
1311 (or (bolp) (insert "\n"))
1312 (setq p1
(plist-put p1
:tstart
(format-time-string
1313 (org-time-stamp-format nil t
)
1314 (seconds-to-time ts
))))
1315 (setq p1
(plist-put p1
:tend
(format-time-string
1316 (org-time-stamp-format nil t
)
1317 (seconds-to-time (setq ts
(+ ts step
))))))
1318 (insert "\n" (if (eq step0
'day
) "Daily report: " "Weekly report starting on: ")
1319 (plist-get p1
:tstart
) "\n")
1320 (org-dblock-write:clocktable p1
)
1321 (re-search-forward "#\\+END:")
1324 (defun org-clocktable-add-file (file table
)
1326 (let ((lines (org-split-string table
"\n"))
1327 (ff (file-name-nondirectory file
)))
1328 (mapconcat 'identity
1330 (if (string-match org-table-dataline-regexp x
)
1336 (defun org-clock-time%
(total &rest strings
)
1337 "Compute a time fraction in percent.
1338 TOTAL s a time string like 10:21 specifying the total times.
1339 STRINGS is a list of strings that should be checked for a time.
1340 The first string that does have a time will be used.
1341 This function is made for clock tables."
1342 (let ((re "\\([0-9]+\\):\\([0-9]+\\)")
1346 (if (not (string-match re total
))
1348 (setq tot
(+ (string-to-number (match-string 2 total
))
1349 (* 60 (string-to-number (match-string 1 total
)))))
1350 (if (= tot
0.
) (throw 'exit
0.
)))
1351 (while (setq s
(pop strings
))
1352 (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s
)
1354 (/ (* 100.0 (+ (string-to-number (match-string 2 s
))
1355 (* 60 (string-to-number (match-string 1 s
)))))
1359 (defun org-clock-save ()
1360 "Persist various clock-related data to disk.
1361 The details of what will be saved are regulated by the variable
1362 `org-clock-persist'."
1363 (when org-clock-persist
1365 (with-current-buffer (find-file (expand-file-name org-clock-persist-file
))
1367 (delete-region (point-min) (point-max))
1369 (insert (format ";; org-persist.el - %s at %s\n"
1370 system-name
(format-time-string
1371 (cdr org-time-stamp-formats
))))
1372 (if (and (setq b
(marker-buffer org-clock-marker
))
1373 (setq b
(or (buffer-base-buffer b
) b
))
1375 (buffer-file-name b
)
1376 (or (not org-clock-persist-query-save
)
1377 (y-or-n-p (concat "Save current clock ("
1378 (substring-no-properties org-clock-heading
)
1380 (insert "(setq resume-clock '(\""
1381 (buffer-file-name (marker-buffer org-clock-marker
))
1382 "\" . " (int-to-string (marker-position org-clock-marker
))
1384 ;; Store clocked task history. Tasks are stored reversed to make
1386 (when (and org-clock-history
(eq org-clock-persist t
))
1388 "(setq stored-clock-history '("
1391 (when (and (setq b
(marker-buffer m
))
1392 (setq b
(or (buffer-base-buffer b
) b
))
1394 (buffer-file-name b
))
1395 (concat "(\"" (buffer-file-name b
)
1396 "\" . " (int-to-string (marker-position m
))
1398 (reverse org-clock-history
) " ") "))\n"))
1400 (kill-buffer (current-buffer)))))))
1402 (defvar org-clock-loaded nil
1403 "Was the clock file loaded?")
1405 (defun org-clock-load ()
1406 "Load clock-related data from disk, maybe resuming a stored clock."
1407 (when (and org-clock-persist
(not org-clock-loaded
))
1408 (let ((filename (expand-file-name org-clock-persist-file
))
1409 (org-clock-in-resume 'auto-restart
)
1410 resume-clock stored-clock-history
)
1411 (if (not (file-readable-p filename
))
1412 (message "Not restoring clock data; %s not found"
1413 org-clock-persist-file
)
1414 (message "%s" "Restoring clock data")
1415 (setq org-clock-loaded t
)
1416 (load-file filename
)
1418 (when stored-clock-history
1419 (save-window-excursion
1420 (mapc (lambda (task)
1421 (if (file-exists-p (car task
))
1422 (org-clock-history-push (cdr task
)
1423 (find-file (car task
)))))
1424 stored-clock-history
)))
1426 (when (and resume-clock org-clock-persist
1427 (file-exists-p (car resume-clock
))
1428 (or (not org-clock-persist-query-resume
)
1432 (with-current-buffer (find-file (car resume-clock
))
1434 (goto-char (cdr resume-clock
))
1435 (org-back-to-heading t
)
1436 (and (looking-at org-complex-heading-regexp
)
1439 (when (file-exists-p (car resume-clock
))
1440 (with-current-buffer (find-file (car resume-clock
))
1441 (goto-char (cdr resume-clock
))
1443 (if (org-invisible-p)
1444 (org-show-context)))))))))
1447 (defun org-clock-persistence-insinuate ()
1448 "Set up hooks for clock persistence"
1449 (add-hook 'org-mode-hook
'org-clock-load
)
1450 (add-hook 'kill-emacs-hook
'org-clock-save
))
1452 (provide 'org-clock
)
1454 ;; arch-tag: 7b42c5d4-9b36-48be-97c0-66a869daed4c
1456 ;;; org-clock.el ends here