org-clock.el 96.7 KB
Newer Older
1 2
;;; org-clock.el --- The time clocking code for Org-mode

3
;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
4 5 6 7 8 9 10

;; Author: Carsten Dominik <carsten at orgmode dot org>
;; Keywords: outlines, hypermedia, calendar, wp
;; Homepage: http://orgmode.org
;;
;; This file is part of GNU Emacs.
;;
11
;; GNU Emacs is free software: you can redistribute it and/or modify
12
;; it under the terms of the GNU General Public License as published by
13 14
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
15 16 17 18 19 20 21

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
22
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23 24 25 26 27 28 29
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:

;; This file contains the time clocking code for Org-mode

(require 'org)
30
(require 'org-exp)
31 32
;;; Code:

33
(eval-when-compile
34
  (require 'cl))
35

Carsten Dominik's avatar
Carsten Dominik committed
36
(declare-function calendar-absolute-from-iso "cal-iso" (&optional date))
Carsten Dominik's avatar
Carsten Dominik committed
37
(declare-function notifications-notify "notifications" (&rest params))
Bastien Guerry's avatar
Bastien Guerry committed
38
(declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
39
(defvar org-time-stamp-formats)
40
(defvar org-ts-what)
41 42 43 44 45 46

(defgroup org-clock nil
  "Options concerning clocking working time in Org-mode."
  :tag "Org Clock"
  :group 'org-progress)

47
(defcustom org-clock-into-drawer org-log-into-drawer
48
  "Should clocking info be wrapped into a drawer?
49
When t, clocking info will always be inserted into a :LOGBOOK: drawer.
50 51 52
If necessary, the drawer will be created.
When nil, the drawer will not be created, but used when present.
When an integer and the number of clocking entries in an item
53 54 55 56 57
reaches or exceeds this number, a drawer will be created.
When a string, it names the drawer to be used.

The default for this variable is the value of `org-log-into-drawer',
which see."
58 59 60 61 62
  :group 'org-todo
  :group 'org-clock
  :type '(choice
	  (const :tag "Always" t)
	  (const :tag "Only when drawer exists" nil)
63 64 65
	  (integer :tag "When at least N clock entries")
	  (const :tag "Into LOGBOOK drawer" "LOGBOOK")
	  (string :tag "Into Drawer named...")))
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
(defun org-clock-into-drawer ()
  "Return the value of `org-clock-into-drawer', but let properties overrule.
If the current entry has or inherits a CLOCK_INTO_DRAWER
property, it will be used instead of the default value; otherwise
if the current entry has or inherits a LOG_INTO_DRAWER property,
it will be used instead of the default value.
The default is the value of the customizable variable `org-clock-into-drawer',
which see."
  (let ((p (org-entry-get nil "CLOCK_INTO_DRAWER" 'inherit))
	(q (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
    (cond
     ((or (not (or p q)) (equal p "nil") (equal q "nil")) org-clock-into-drawer)
     ((or (equal p "t") (equal q "t")) "LOGBOOK")
     ((not p) q)
     (t p))))

83
(defcustom org-clock-out-when-done t
84
  "When non-nil, clock will be stopped when the clocked entry is marked DONE.
85 86 87 88 89
DONE here means any DONE-like state.
A nil value means clock will keep running until stopped explicitly with
`C-c C-x C-o', or until the clock is started in a different item.
Instead of t, this can also be a list of TODO states that should trigger
clocking out."
90
  :group 'org-clock
91 92 93 94 95
  :type '(choice
	  (const :tag "No" nil)
	  (const :tag "Yes, when done" t)
	  (repeat :tag "State list"
		  (string :tag "TODO keyword"))))
96 97

(defcustom org-clock-out-remove-zero-time-clocks nil
98
  "Non-nil means remove the clock line when the resulting time is zero."
99 100 101 102 103
  :group 'org-clock
  :type 'boolean)

(defcustom org-clock-in-switch-to-state nil
  "Set task to a special todo state while clocking it.
104
The value should be the state to which the entry should be
105
switched.  If the value is a function, it must take one
106 107
parameter (the current TODO state of the item) and return the
state to switch it to."
108 109 110 111
  :group 'org-clock
  :group 'org-todo
  :type '(choice
	  (const :tag "Don't force a state" nil)
112 113
	  (string :tag "State")
	  (symbol :tag "Function")))
114

115 116 117
(defcustom org-clock-out-switch-to-state nil
  "Set task to a special todo state after clocking out.
The value should be the state to which the entry should be
118
switched.  If the value is a function, it must take one
119 120 121 122 123 124 125 126 127
parameter (the current TODO state of the item) and return the
state to switch it to."
  :group 'org-clock
  :group 'org-todo
  :type '(choice
	  (const :tag "Don't force a state" nil)
	  (string :tag "State")
	  (symbol :tag "Function")))

128 129 130 131 132
(defcustom org-clock-history-length 5
  "Number of clock tasks to remember in history."
  :group 'org-clock
  :type 'integer)

133
(defcustom org-clock-goto-may-find-recent-task t
134
  "Non-nil means `org-clock-goto' can go to recent task if no active clock."
135 136 137
  :group 'org-clock
  :type 'boolean)

138 139 140 141 142 143 144
(defcustom org-clock-heading-function nil
  "When non-nil, should be a function to create `org-clock-heading'.
This is the string shown in the mode line when a clock is running.
The function is called with point at the beginning of the headline."
  :group 'org-clock
  :type 'function)

145
(defcustom org-clock-string-limit 0
146
  "Maximum length of clock strings in the modeline.  0 means no limit."
147 148
  :group 'org-clock
  :type 'integer)
149

150
(defcustom org-clock-in-resume nil
151 152 153
  "If non-nil, resume clock when clocking into task with open clock.
When clocking into a task with a clock entry which has not been closed,
the clock can be resumed from that point."
154 155 156 157
  :group 'org-clock
  :type 'boolean)

(defcustom org-clock-persist nil
158 159
  "When non-nil, save the running clock when Emacs is closed.
The clock is resumed when Emacs restarts.
160 161 162 163 164 165
When this is t, both the running clock, and the entire clock
history are saved.  When this is the symbol `clock', only the
running clock is saved.

When Emacs restarts with saved clock information, the file containing the
running clock as well as all files mentioned in the clock history will
166 167
be visited.
All this depends on running `org-clock-persistence-insinuate' in .emacs"
168
  :group 'org-clock
169 170
  :type '(choice
	  (const :tag "Just the running clock" clock)
171
	  (const :tag "Just the history" history)
172 173
	  (const :tag "Clock and history" t)
	  (const :tag "No persistence" nil)))
174

175 176
(defcustom org-clock-persist-file (convert-standard-filename
				   "~/.emacs.d/org-clock-save.el")
177
  "File to save clock data to."
178 179 180 181
  :group 'org-clock
  :type 'string)

(defcustom org-clock-persist-query-save nil
182
  "When non-nil, ask before saving the current clock on exit."
183 184 185 186
  :group 'org-clock
  :type 'boolean)

(defcustom org-clock-persist-query-resume t
187
  "When non-nil, ask before resuming any stored clock during load."
188 189 190
  :group 'org-clock
  :type 'boolean)

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
(defcustom org-clock-sound nil
  "Sound that will used for notifications.
Possible values:

nil        no sound played.
t          standard Emacs beep
file name  play this sound file.  If not possible, fall back to beep"
  :group 'org-clock
  :type '(choice
	  (const :tag "No sound" nil)
	  (const :tag "Standard beep" t)
	  (file :tag "Play sound file")))

(defcustom org-clock-modeline-total 'auto
  "Default setting for the time included for the modeline clock.
This can be overruled locally using the CLOCK_MODELINE_TOTAL property.
Allowed values are:

current  Only the time in the current instance of the clock
210
today    All time clocked into this task today
211 212
repeat   All time clocked into this task since last repeat
all      All time ever recorded for this task
213
auto     Automatically, either `all', or `repeat' for repeating tasks"
214 215 216 217 218 219 220 221
  :group 'org-clock
  :type '(choice
	  (const :tag "Current clock" current)
	  (const :tag "Today's task time" today)
	  (const :tag "Since last repeat" repeat)
	  (const :tag "All task time" all)
	  (const :tag "Automatically, `all' or since `repeat'" auto)))

222 223 224 225 226 227 228 229 230 231 232
(defcustom org-task-overrun-text nil
  "The extra modeline text that should indicate that the clock is overrun.
The can be nil to indicate that instead of adding text, the clock time
should get a different face (`org-mode-line-clock-overrun').
When this is a string, it is prepended to the clock string as an indication,
also using the face `org-mode-line-clock-overrun'."
  :group 'org-clock
  :type '(choice
	  (const :tag "Just mark the time string" nil)
	  (string :tag "Text to prepend")))

233 234 235 236 237 238 239 240 241
(defcustom org-show-notification-handler nil
  "Function or program to send notification with.
The function or program will be called with the notification
string as argument."
  :group 'org-clock
  :type '(choice
	  (string :tag "Program")
	  (function :tag "Function")))

Carsten Dominik's avatar
Carsten Dominik committed
242 243 244 245 246 247
(defgroup org-clocktable nil
  "Options concerning the clock table in Org-mode."
  :tag "Org Clock Table"
  :group 'org-clock)

(defcustom org-clocktable-defaults
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
  `(list
    :maxlevel 2
    :lang ,org-export-default-language
    :scope 'file
    :block nil
    :tstart nil
    :tend nil
    :step nil
    :stepskip0 nil
    :fileskip0 nil
    :tags nil
    :emphasize nil
    :link nil
    :narrow '40!
    :indent t
    :formula nil
    :timestamp nil
    :level nil
    :tcolumns nil
    :formatter nil)
Carsten Dominik's avatar
Carsten Dominik committed
268
  "Default properties for clock tables."
269 270 271
  :group 'org-clock
  :type 'plist)

Carsten Dominik's avatar
Carsten Dominik committed
272 273 274 275 276 277
(defcustom org-clock-clocktable-formatter 'org-clocktable-write-default
  "Function to turn clocking data into a table.
For more information, see `org-clocktable-write-default'."
  :group 'org-clocktable
  :type 'function)

278 279 280 281 282 283 284 285 286 287
;; FIXME: translate es and nl last string "Clock summary at"
(defcustom org-clock-clocktable-language-setup
  '(("en" "File"     "L"  "Timestamp"  "Headline" "Time"  "ALL"   "Total time"   "File time" "Clock summary at")
    ("es" "Archivo"  "N"  "Fecha y hora" "Tarea" "Tiempo" "TODO" "Tiempo total" "Tiempo archivo" "Clock summary at")
    ("fr" "Fichier"  "N"  "Horodatage" "En-tête"  "Durée" "TOUT"  "Durée totale" "Durée fichier" "Horodatage sommaire à")
    ("nl" "Bestand"  "N"  "Tijdstip"   "Hoofding" "Duur"  "ALLES" "Totale duur"  "Bestandstijd" "Clock summary at"))
  "Terms used in clocktable, translated to different languages."
  :group 'org-clocktable
  :type 'alist)

Carsten Dominik's avatar
Carsten Dominik committed
288 289 290 291 292 293 294
(defcustom org-clock-clocktable-default-properties '(:maxlevel 2 :scope file)
  "Default properties for new clocktables.
These will be inserted into the BEGIN line, to make it easy for users to
play with them."
  :group 'org-clocktable
  :type 'plist)

295 296 297 298 299 300 301 302 303 304 305 306 307 308
(defcustom org-clock-idle-time nil
  "When non-nil, resolve open clocks if the user is idle more than X minutes."
  :group 'org-clock
  :type '(choice
	  (const :tag "Never" nil)
	  (integer :tag "After N minutes")))

(defcustom org-clock-auto-clock-resolution 'when-no-clock-is-running
  "When to automatically resolve open clocks found in Org buffers."
  :group 'org-clock
  :type '(choice
	  (const :tag "Never" nil)
	  (const :tag "Always" t)
	  (const :tag "When no clock is running" when-no-clock-is-running)))
309

310 311 312 313 314
(defcustom org-clock-report-include-clocking-task nil
  "When non-nil, include the current clocking task time in clock reports."
  :group 'org-clock
  :type 'boolean)

315 316 317 318 319
(defcustom org-clock-resolve-expert nil
  "Non-nil means do not show the splash buffer with the clock resolver."
  :group 'org-clock
  :type 'boolean)

320 321 322 323 324 325 326 327 328 329 330
(defvar org-clock-in-prepare-hook nil
  "Hook run when preparing the clock.
This hook is run before anything happens to the task that
you want to clock in.  For example, you can use this hook
to add an effort property.")
(defvar org-clock-in-hook nil
  "Hook run when starting the clock.")
(defvar org-clock-out-hook nil
  "Hook run when stopping the current clock.")

(defvar org-clock-cancel-hook nil
Bastien Guerry's avatar
Bastien Guerry committed
331
  "Hook run when cancelling the current clock.")
332 333
(defvar org-clock-goto-hook nil
  "Hook run when selecting the currently clocked-in entry.")
334 335
(defvar org-clock-has-been-used nil
  "Has the clock been used during the current Emacs session?")
336

337 338 339 340 341
;;; The clock for measuring work time.

(defvar org-mode-line-string "")
(put 'org-mode-line-string 'risky-local-variable t)

342
(defvar org-clock-mode-line-timer nil)
343 344
(defvar org-clock-idle-timer nil)
(defvar org-clock-heading) ; defined in org.el
345
(defvar org-clock-heading-for-remember "")
346 347
(defvar org-clock-start-time "")

348
(defvar org-clock-leftover-time nil
Bastien Guerry's avatar
Bastien Guerry committed
349
  "If non-nil, user cancelled a clock; this is when leftover time started.")
350

351
(defvar org-clock-effort ""
352
  "Effort estimate of the currently clocking task.")
353 354 355 356 357

(defvar org-clock-total-time nil
  "Holds total time, spent previously on currently clocked item.
This does not include the time in the currently running clock.")

358
(defvar org-clock-history nil
359
  "List of marker pointing to recent clocked tasks.")
360 361 362 363 364 365 366

(defvar org-clock-default-task (make-marker)
  "Marker pointing to the default task that should clock time.
The clock can be made to switch to this task after clocking out
of a different task.")

(defvar org-clock-interrupted-task (make-marker)
367
  "Marker pointing to the task that has been interrupted by the current clock.")
368

369 370
(defvar org-clock-mode-line-map (make-sparse-keymap))
(define-key org-clock-mode-line-map [mode-line mouse-2] 'org-clock-goto)
371 372 373 374 375 376 377 378 379 380
(define-key org-clock-mode-line-map [mode-line mouse-1] 'org-clock-menu)

(defun org-clock-menu ()
  (interactive)
  (popup-menu
   '("Clock"
     ["Clock out" org-clock-out t]
     ["Change effort estimate" org-clock-modify-effort-estimate t]
     ["Go to clock entry" org-clock-goto t]
     ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"])))
381

382 383
(defun org-clock-history-push (&optional pos buffer)
  "Push a marker to the clock history."
384
  (setq org-clock-history-length (max 1 (min 35 org-clock-history-length)))
385 386 387 388
  (let ((m (move-marker (make-marker)
			(or pos (point)) (org-base-buffer
					  (or buffer (current-buffer)))))
	n l)
389 390 391 392 393 394 395 396 397 398 399 400 401
    (while (setq n (member m org-clock-history))
      (move-marker (car n) nil))
    (setq org-clock-history
	  (delq nil
		(mapcar (lambda (x) (if (marker-buffer x) x nil))
			org-clock-history)))
    (when (>= (setq l (length org-clock-history)) org-clock-history-length)
      (setq org-clock-history
	    (nreverse
	     (nthcdr (- l org-clock-history-length -1)
		     (nreverse org-clock-history)))))
    (push m org-clock-history)))

402 403 404
(defun org-clock-save-markers-for-cut-and-paste (beg end)
  "Save relative positions of markers in region."
  (org-check-and-save-marker org-clock-marker beg end)
405
  (org-check-and-save-marker org-clock-hd-marker beg end)
406 407 408 409 410
  (org-check-and-save-marker org-clock-default-task beg end)
  (org-check-and-save-marker org-clock-interrupted-task beg end)
  (mapc (lambda (m) (org-check-and-save-marker m beg end))
	org-clock-history))

411
(defun org-clocking-buffer ()
412
  "Return the clocking buffer if we are currently clocking a task or nil."
413 414 415
  (marker-buffer org-clock-marker))

(defun org-clocking-p ()
416
  "Return t when clocking a task."
417 418
  (not (equal (org-clocking-buffer) nil)))

419 420 421
(defvar org-clock-before-select-task-hook nil
  "Hook called in task selection just before prompting the user.")

422 423 424
(defun org-clock-select-task (&optional prompt)
  "Select a task that recently was associated with clocking."
  (interactive)
425
  (let (sel-list rpl (i 0) s)
426
    (save-window-excursion
427 428 429 430 431 432 433 434 435 436 437
      (org-switch-to-buffer-other-window
       (get-buffer-create "*Clock Task Select*"))
      (erase-buffer)
      (when (marker-buffer org-clock-default-task)
	(insert (org-add-props "Default Task\n" nil 'face 'bold))
	(setq s (org-clock-insert-selection-line ?d org-clock-default-task))
	(push s sel-list))
      (when (marker-buffer org-clock-interrupted-task)
	(insert (org-add-props "The task interrupted by starting the last one\n" nil 'face 'bold))
	(setq s (org-clock-insert-selection-line ?i org-clock-interrupted-task))
	(push s sel-list))
438
      (when (org-clocking-p)
439 440 441 442 443 444 445 446 447
	(insert (org-add-props "Current Clocking Task\n" nil 'face 'bold))
	(setq s (org-clock-insert-selection-line ?c org-clock-marker))
	(push s sel-list))
      (insert (org-add-props "Recent Tasks\n" nil 'face 'bold))
      (mapc
       (lambda (m)
	 (when (marker-buffer m)
	   (setq i (1+ i)
		 s (org-clock-insert-selection-line
448 449 450
		    (if (< i 10)
			(+ i ?0)
		      (+ i (- ?A 10))) m))
451
	   (if (fboundp 'int-to-char) (setf (car s) (int-to-char (car s))))
452 453
	   (push s sel-list)))
       org-clock-history)
454
      (run-hooks 'org-clock-before-select-task-hook)
455
      (org-fit-window-to-buffer)
456 457 458 459 460 461 462 463 464
      (message (or prompt "Select task for clocking:"))
      (setq rpl (read-char-exclusive))
      (cond
       ((eq rpl ?q) nil)
       ((eq rpl ?x) nil)
       ((assoc rpl sel-list) (cdr (assoc rpl sel-list)))
       (t (error "Invalid task choice %c" rpl))))))

(defun org-clock-insert-selection-line (i marker)
465 466 467
  "Insert a line for the clock selection menu.
And return a cons cell with the selection character integer and the marker
pointing to it."
468
  (when (marker-buffer marker)
469
    (let (file cat task heading prefix)
470
      (with-current-buffer (org-base-buffer (marker-buffer marker))
471
	(save-excursion
472 473
	  (save-restriction
	    (widen)
474 475 476
	    (ignore-errors
	      (goto-char marker)
	      (setq file (buffer-file-name (marker-buffer marker))
477
		    cat (org-get-category)
478 479 480
		    heading (org-get-heading 'notags)
		    prefix (save-excursion
			     (org-back-to-heading t)
481
			     (looking-at org-outline-regexp)
482 483 484 485 486 487
			     (match-string 0))
		    task (substring
			  (org-fontify-like-in-org-mode
			   (concat prefix heading)
			   org-odd-levels-only)
			  (length prefix)))))))
488 489 490
      (when (and cat task)
	(insert (format "[%c] %-15s %s\n" i cat task))
	(cons i marker)))))
491

492 493 494 495 496
(defvar org-task-overrun nil
  "Internal flag indicating if the clock has overrun the planned time.")
(defvar org-clock-update-period 60
  "Number of seconds between mode line clock string updates.")

497
(defun org-clock-get-clock-string ()
498 499
  "Form a clock-string, that will be shown in the mode line.
If an effort estimate was defined for the current item, use
500 501 502 503 504
01:30/01:50 format (clocked/estimated).
If not, show simply the clocked time like 01:50."
  (let* ((clocked-time (org-clock-get-clocked-time))
	 (h (floor clocked-time 60))
	 (m (- clocked-time (* 60 h))))
505 506
    (if org-clock-effort
	(let* ((effort-in-minutes
507
		(org-duration-string-to-minutes org-clock-effort))
508
	       (effort-h (floor effort-in-minutes 60))
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
	       (effort-m (- effort-in-minutes (* effort-h 60)))
	       (work-done-str
		(org-propertize
		 (format org-time-clocksum-format h m)
		 'face (if (and org-task-overrun (not org-task-overrun-text))
			   'org-mode-line-clock-overrun 'org-mode-line-clock)))
	       (effort-str (format org-time-clocksum-format effort-h effort-m))
	       (clockstr (org-propertize
			  (concat  "[%s/" effort-str
				   "] (" (replace-regexp-in-string "%" "%%" org-clock-heading) ")")
			  'face 'org-mode-line-clock)))
	  (format clockstr work-done-str))
      (org-propertize (format
		       (concat "[" org-time-clocksum-format " (%s)]")
		       h m org-clock-heading)
		      'face 'org-mode-line-clock))))
525

526
(defun org-clock-update-mode-line ()
527 528 529
  (if org-clock-effort
      (org-clock-notify-once-if-expired)
    (setq org-task-overrun nil))
530 531 532 533 534 535
  (setq org-mode-line-string
	(org-propertize
	 (let ((clock-string (org-clock-get-clock-string))
	       (help-text "Org-mode clock is running.\nmouse-1 shows a menu\nmouse-2 will jump to task"))
	   (if (and (> org-clock-string-limit 0)
		    (> (length clock-string) org-clock-string-limit))
536 537 538
	       (org-propertize
		(substring clock-string 0 org-clock-string-limit)
		'help-echo (concat help-text ": " org-clock-heading))
539 540 541
	     (org-propertize clock-string 'help-echo help-text)))
	 'local-map org-clock-mode-line-map
	 'mouse-face (if (featurep 'xemacs) 'highlight 'mode-line-highlight)
542 543 544 545 546 547
	 ))
  (if (and org-task-overrun org-task-overrun-text)
      (setq org-mode-line-string
	    (concat (org-propertize
		     org-task-overrun-text
		     'face 'org-mode-line-clock-overrun) org-mode-line-string)))
548 549 550 551
  (force-mode-line-update))

(defun org-clock-get-clocked-time ()
  "Get the clocked time for the current item in minutes.
552
The time returned includes the time spent on this task in
553 554
previous clocking intervals."
  (let ((currently-clocked-time
555 556
	 (floor (- (org-float-time)
		   (org-float-time org-clock-start-time)) 60)))
557 558 559 560
    (+ currently-clocked-time (or org-clock-total-time 0))))

(defun org-clock-modify-effort-estimate (&optional value)
 "Add to or set the effort estimate of the item currently being clocked.
561 562
VALUE can be a number of minutes, or a string with format hh:mm or mm.
When the string starts with a + or a - sign, the current value of the effort
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
property will be changed by that amount.
This will update the \"Effort\" property of currently clocked item, and
the mode line."
 (interactive)
 (when (org-clock-is-active)
   (let ((current org-clock-effort) sign)
     (unless value
       ;; Prompt user for a value or a change
       (setq value
	     (read-string
	      (format "Set effort (hh:mm or mm%s): "
		      (if current
			  (format ", prefix + to add to %s" org-clock-effort)
			"")))))
     (when (stringp value)
       ;; A string.  See if it is a delta
       (setq sign (string-to-char value))
       (if (member sign '(?- ?+))
581
	   (setq current (org-duration-string-to-minutes current)
582
		 value (substring value 1))
583
	 (setq current 0))
584
       (setq value (org-duration-string-to-minutes value))
585 586 587 588 589 590
       (if (equal ?- sign)
	   (setq value (- current value))
	 (if (equal ?+ sign) (setq value (+ current value)))))
     (setq value (max 0 value)
	   org-clock-effort (org-minutes-to-hh:mm-string value))
     (org-entry-put org-clock-marker "Effort" org-clock-effort)
591 592
     (org-clock-update-mode-line)
     (message "Effort is now %s" org-clock-effort))))
593 594 595 596 597 598 599

(defvar org-clock-notification-was-shown nil
  "Shows if we have shown notification already.")

(defun org-clock-notify-once-if-expired ()
  "Show notification if we spent more time than we estimated before.
Notification is shown only once."
600
  (when (org-clocking-p)
601
    (let ((effort-in-minutes (org-duration-string-to-minutes org-clock-effort))
602
	  (clocked-time (org-clock-get-clocked-time)))
603 604 605 606
      (if (setq org-task-overrun
		(if (or (null effort-in-minutes) (zerop effort-in-minutes))
		    nil
		  (>= clocked-time effort-in-minutes)))
607 608
	  (unless org-clock-notification-was-shown
	    (setq org-clock-notification-was-shown t)
609
	    (org-notify
610
	     (format "Task '%s' should be finished by now. (%s)"
611
		     org-clock-heading org-clock-effort) t))
612 613
	(setq org-clock-notification-was-shown nil)))))

614 615 616 617 618
(defun org-notify (notification &optional play-sound)
  "Send a NOTIFICATION and maybe PLAY-SOUND."
  (org-show-notification notification)
  (if play-sound (org-clock-play-sound)))

619 620 621 622 623 624 625 626 627
(defun org-show-notification (notification)
  "Show notification.
Use `org-show-notification-handler' if defined,
use libnotify if available, or fall back on a message."
  (cond ((functionp org-show-notification-handler)
	 (funcall org-show-notification-handler notification))
	((stringp org-show-notification-handler)
	 (start-process "emacs-timer-notification" nil
			org-show-notification-handler notification))
Carsten Dominik's avatar
Carsten Dominik committed
628 629 630 631 632 633 634 635
	((featurep 'notifications)
	 (require 'notifications)
	 (notifications-notify
	  :title "Org-mode message"
	  :body notification
	  ;; FIXME how to link to the Org icon?
	  ;; :app-icon "~/.emacs.d/icons/mail.png"
	  :urgency 'low))
636 637 638 639 640
	((org-program-exists "notify-send")
	 (start-process "emacs-timer-notification" nil
			"notify-send" notification))
	;; Maybe the handler will send a message, so only use message as
	;; a fall back option
641
	(t (message "%s" notification))))
642 643 644 645 646 647 648 649

(defun org-clock-play-sound ()
  "Play sound as configured by `org-clock-sound'.
Use alsa's aplay tool if available."
  (cond
   ((not org-clock-sound))
   ((eq org-clock-sound t) (beep t) (beep t))
   ((stringp org-clock-sound)
650 651 652 653 654 655 656 657
    (let ((file (expand-file-name org-clock-sound)))
      (if (file-exists-p file)
	  (if (org-program-exists "aplay")
	      (start-process "org-clock-play-notification" nil
			     "aplay" file)
	    (condition-case nil
		(play-sound-file file)
	      (error (beep t) (beep t)))))))))
658 659 660

(defun org-program-exists (program-name)
  "Checks whenever we can locate program and launch it."
Bastien Guerry's avatar
Bastien Guerry committed
661
  (if (member system-type '(gnu/linux darwin))
662
      (= 0 (call-process "which" nil nil nil program-name))))
663 664 665 666

(defvar org-clock-mode-line-entry nil
  "Information for the modeline about the running clock.")

667 668 669 670 671 672 673 674 675
(defun org-find-open-clocks (file)
  "Search through the given file and find all open clocks."
  (let ((buf (or (get-file-buffer file)
		 (find-file-noselect file)))
	clocks)
    (with-current-buffer buf
      (save-excursion
	(goto-char (point-min))
	(while (re-search-forward "CLOCK: \\(\\[.*?\\]\\)$" nil t)
676
	  (push (cons (copy-marker (match-end 1) t)
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
		      (org-time-string-to-time (match-string 1))) clocks))))
    clocks))

(defsubst org-is-active-clock (clock)
  "Return t if CLOCK is the currently active clock."
  (and (org-clock-is-active)
       (= org-clock-marker (car clock))))

(defmacro org-with-clock-position (clock &rest forms)
  "Evaluate FORMS with CLOCK as the current active clock."
  `(with-current-buffer (marker-buffer (car ,clock))
     (save-excursion
       (save-restriction
	 (widen)
	 (goto-char (car ,clock))
	 (beginning-of-line)
	 ,@forms))))
Bastien Guerry's avatar
Bastien Guerry committed
694
(def-edebug-spec org-with-clock-position (form body))
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
(put 'org-with-clock-position 'lisp-indent-function 1)

(defmacro org-with-clock (clock &rest forms)
  "Evaluate FORMS with CLOCK as the current active clock.
This macro also protects the current active clock from being altered."
  `(org-with-clock-position ,clock
     (let ((org-clock-start-time (cdr ,clock))
	   (org-clock-total-time)
	   (org-clock-history)
	   (org-clock-effort)
	   (org-clock-marker (car ,clock))
	   (org-clock-hd-marker (save-excursion
				  (outline-back-to-heading t)
				  (point-marker))))
       ,@forms)))
Bastien Guerry's avatar
Bastien Guerry committed
710
(def-edebug-spec org-with-clock (form body))
711 712
(put 'org-with-clock 'lisp-indent-function 1)

713
(defsubst org-clock-clock-in (clock &optional resume start-time)
714 715 716 717
  "Clock in to the clock located by CLOCK.
If necessary, clock-out of the currently active clock."
  (org-with-clock-position clock
    (let ((org-clock-in-resume (or resume org-clock-in-resume)))
718
      (org-clock-in nil start-time))))
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743

(defsubst org-clock-clock-out (clock &optional fail-quietly at-time)
  "Clock out of the clock located by CLOCK."
  (let ((temp (copy-marker (car clock)
			   (marker-insertion-type (car clock)))))
    (if (org-is-active-clock clock)
	(org-clock-out fail-quietly at-time)
      (org-with-clock clock
	(org-clock-out fail-quietly at-time)))
    (setcar clock temp)))

(defsubst org-clock-clock-cancel (clock)
  "Cancel the clock located by CLOCK."
  (let ((temp (copy-marker (car clock)
			   (marker-insertion-type (car clock)))))
    (if (org-is-active-clock clock)
	(org-clock-cancel)
      (org-with-clock clock
	(org-clock-cancel)))
    (setcar clock temp)))

(defvar org-clock-clocking-in nil)
(defvar org-clock-resolving-clocks nil)
(defvar org-clock-resolving-clocks-due-to-idleness nil)

744 745
(defun org-clock-resolve-clock (clock resolve-to clock-out-time
				      &optional close-p restart-p fail-quietly)
746
  "Resolve `CLOCK' given the time `RESOLVE-TO', and the present.
747
`CLOCK' is a cons cell of the form (MARKER START-TIME)."
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
  (let ((org-clock-resolving-clocks t))
    (cond
     ((null resolve-to)
      (org-clock-clock-cancel clock)
      (if (and restart-p (not org-clock-clocking-in))
	  (org-clock-clock-in clock)))

     ((eq resolve-to 'now)
      (if restart-p
	  (error "RESTART-P is not valid here"))
      (if (or close-p org-clock-clocking-in)
	  (org-clock-clock-out clock fail-quietly)
	(unless (org-is-active-clock clock)
	  (org-clock-clock-in clock t))))

     ((not (time-less-p resolve-to (current-time)))
      (error "RESOLVE-TO must refer to a time in the past"))

     (t
      (if restart-p
	  (error "RESTART-P is not valid here"))
769 770
      (org-clock-clock-out clock fail-quietly (or clock-out-time
						  resolve-to))
771 772
      (unless org-clock-clocking-in
	(if close-p
773 774 775 776 777 778 779
	    (setq org-clock-leftover-time (and (null clock-out-time)
					       resolve-to))
	  (org-clock-clock-in clock nil (and clock-out-time
					     resolve-to))))))))

(defun org-clock-jump-to-current-clock (&optional effective-clock)
  (interactive)
780 781
  (let ((org-clock-into-drawer (org-clock-into-drawer))
	(clock (or effective-clock (cons org-clock-marker
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
					 org-clock-start-time))))
    (unless (marker-buffer (car clock))
      (error "No clock is currently running"))
    (org-with-clock clock (org-clock-goto))
    (with-current-buffer (marker-buffer (car clock))
      (goto-char (car clock))
      (if org-clock-into-drawer
	  (let ((logbook
		 (if (stringp org-clock-into-drawer)
		     (concat ":" org-clock-into-drawer ":")
		   ":LOGBOOK:")))
	    (ignore-errors
	      (outline-flag-region
	       (save-excursion
		 (outline-back-to-heading t)
		 (search-forward logbook)
		 (goto-char (match-beginning 0)))
	       (save-excursion
		 (outline-back-to-heading t)
		 (search-forward logbook)
		 (search-forward ":END:")
		 (goto-char (match-end 0)))
	       nil)))))))
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829

(defun org-clock-resolve (clock &optional prompt-fn last-valid fail-quietly)
  "Resolve an open org-mode clock.
An open clock was found, with `dangling' possibly being non-nil.
If this function was invoked with a prefix argument, non-dangling
open clocks are ignored.  The given clock requires some sort of
user intervention to resolve it, either because a clock was left
dangling or due to an idle timeout.  The clock resolution can
either be:

  (a) deleted, the user doesn't care about the clock
  (b) restarted from the current time (if no other clock is open)
  (c) closed, giving the clock X minutes
  (d) closed and then restarted
  (e) resumed, as if the user had never left

The format of clock is (CONS MARKER START-TIME), where MARKER
identifies the buffer and position the clock is open at (and
thus, the heading it's under), and START-TIME is when the clock
was started."
  (assert clock)
  (let* ((ch
	  (save-window-excursion
	    (save-excursion
	      (unless org-clock-resolving-clocks-due-to-idleness
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
		(org-clock-jump-to-current-clock clock))
	      (unless org-clock-resolve-expert
		(with-output-to-temp-buffer "*Org Clock*"
		  (princ "Select a Clock Resolution Command:

i/q/C-g  Ignore this question; the same as keeping all the idle time.

k/K      Keep X minutes of the idle time (default is all).  If this
         amount is less than the default, you will be clocked out
         that many minutes after the time that idling began, and then
         clocked back in at the present time.
g/G      Indicate that you \"got back\" X minutes ago.  This is quite
         different from 'k': it clocks you out from the beginning of
         the idle period and clock you back in X minutes ago.
s/S      Subtract the idle time from the current clock.  This is the
         same as keeping 0 minutes.
C        Cancel the open timer altogether.  It will be as though you
         never clocked in.
j/J      Jump to the current clock, to make manual adjustments.

For all these options, using uppercase makes your final state
to be CLOCKED OUT.")))
	      (org-fit-window-to-buffer (get-buffer-window "*Org Clock*"))
853
	      (let (char-pressed)
854 855 856 857
		(when (featurep 'xemacs)
		  (message (concat (funcall prompt-fn clock)
				   " [jkKgGsScCiq]? "))
		  (setq char-pressed (read-char-exclusive)))
858
		(while (or (null char-pressed)
859 860 861
			   (and (not (memq char-pressed
					   '(?k ?K ?g ?G ?s ?S ?C
						?j ?J ?i ?q)))
862
				(or (ding) t)))
863 864
		  (setq char-pressed
			(read-char (concat (funcall prompt-fn clock)
865
					   " [jkKgGSscCiq]? ")
866
				   nil 45)))
867 868 869 870 871 872 873 874 875 876
		(and (not (memq char-pressed '(?i ?q))) char-pressed)))))
	 (default
	   (floor (/ (org-float-time
		      (time-subtract (current-time) last-valid)) 60)))
	 (keep
	  (and (memq ch '(?k ?K))
	       (read-number "Keep how many minutes? " default)))
	 (gotback
	  (and (memq ch '(?g ?G))
	       (read-number "Got back how many minutes ago? " default)))
877 878 879 880
	 (subtractp (memq ch '(?s ?S)))
	 (barely-started-p (< (- (org-float-time last-valid)
				 (org-float-time (cdr clock))) 45))
	 (start-over (and subtractp barely-started-p)))
881 882 883 884 885 886 887 888 889
    (cond
     ((memq ch '(?j ?J))
      (if (eq ch ?J)
	  (org-clock-resolve-clock clock 'now nil t nil fail-quietly))
      (org-clock-jump-to-current-clock clock))
     ((or (null ch)
	  (not (memq ch '(?k ?K ?g ?G ?s ?S ?C))))
      (message ""))
     (t
890 891 892 893 894 895 896 897
      (org-clock-resolve-clock
       clock (cond
	      ((or (eq ch ?C)
		   ;; If the time on the clock was less than a minute before
		   ;; the user went away, and they've ask to subtract all the
		   ;; time...
		   start-over)
	       nil)
898 899
	      ((or subtractp
		   (and gotback (= gotback 0)))
900
	       last-valid)
901 902
	      ((or (and keep (= keep default))
		   (and gotback (= gotback default)))
903
	       'now)
904 905 906 907 908
	      (keep
	       (time-add last-valid (seconds-to-time (* 60 keep))))
	      (gotback
	       (time-subtract (current-time)
			      (seconds-to-time (* 60 gotback))))
909
	      (t
910 911 912
	       (error "Unexpected, please report this as a bug")))
       (and gotback last-valid)
       (memq ch '(?K ?G ?S))
913
       (and start-over
914 915
	    (not (memq ch '(?K ?G ?S ?C))))
       fail-quietly)))))
916

917
(defun org-resolve-clocks (&optional only-dangling-p prompt-fn last-valid)
918
  "Resolve all currently open org-mode clocks.
919 920
If `only-dangling-p' is non-nil, only ask to resolve dangling
\(i.e., not currently open and valid) clocks."
921 922 923 924 925 926 927 928
  (interactive "P")
  (unless org-clock-resolving-clocks
    (let ((org-clock-resolving-clocks t))
      (dolist (file (org-files-list))
	(let ((clocks (org-find-open-clocks file)))
	  (dolist (clock clocks)
	    (let ((dangling (or (not (org-clock-is-active))
				(/= (car clock) org-clock-marker))))
929
	      (if (or (not only-dangling-p) dangling)
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
		(org-clock-resolve
		 clock
		 (or prompt-fn
		     (function
		      (lambda (clock)
			(format
			 "Dangling clock started %d mins ago"
			 (floor
			  (/ (- (org-float-time (current-time))
				(org-float-time (cdr clock))) 60))))))
		 (or last-valid
		     (cdr clock)))))))))))

(defun org-emacs-idle-seconds ()
  "Return the current Emacs idle time in seconds, or nil if not idle."
  (let ((idle-time (current-idle-time)))
    (if idle-time
	(org-float-time idle-time)
      0)))

(defun org-mac-idle-seconds ()
951
  "Return the current Mac idle time in seconds."
952 953 954
  (string-to-number (shell-command-to-string "ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle; last}'")))

(defun org-x11-idle-seconds ()
955
  "Return the current X11 idle time in seconds."
956 957 958 959 960
  (/ (string-to-number (shell-command-to-string "x11idle")) 1000))

(defun org-user-idle-seconds ()
  "Return the number of seconds the user has been idle for.
This routine returns a floating point number."
961 962 963 964 965 966 967
  (cond
   ((eq system-type 'darwin)
    (org-mac-idle-seconds))
   ((eq window-system 'x)
    (org-x11-idle-seconds))
   (t
    (org-emacs-idle-seconds))))
968 969 970 971 972 973 974 975 976 977

(defvar org-clock-user-idle-seconds)

(defun org-resolve-clocks-if-idle ()
  "Resolve all currently open org-mode clocks.
This is performed after `org-clock-idle-time' minutes, to check
if the user really wants to stay clocked in after being idle for
so long."
  (when (and org-clock-idle-time (not org-clock-resolving-clocks)
	     org-clock-marker)
978 979 980 981 982
    (let* ((org-clock-user-idle-seconds (org-user-idle-seconds))
	   (org-clock-user-idle-start
	    (time-subtract (current-time)
			   (seconds-to-time org-clock-user-idle-seconds)))
	   (org-clock-resolving-clocks-due-to-idleness t))
983 984 985 986 987 988 989 990 991 992 993 994 995
      (if (> org-clock-user-idle-seconds (* 60 org-clock-idle-time))
	  (org-clock-resolve
	   (cons org-clock-marker
		 org-clock-start-time)
	   (function
	    (lambda (clock)
	      (format "Clocked in & idle for %.1f mins"
		      (/ (org-float-time
			  (time-subtract (current-time)
					 org-clock-user-idle-start))
			 60.0))))
	   org-clock-user-idle-start)))))

996 997 998 999 1000 1001 1002 1003 1004 1005
(defvar org-clock-current-task nil
  "Task currently clocked in.")
(defun org-clock-set-current ()
  "Set `org-clock-current-task' to the task currently clocked in."
  (setq org-clock-current-task (nth 4 (org-heading-components))))

(defun org-clock-delete-current ()
  "Reset `org-clock-current-task' to nil."
  (setq org-clock-current-task nil))

1006
(defun org-clock-in (&optional select start-time)
1007 1008
  "Start the clock on the current item.
If necessary, clock-out of the currently active clock.
1009 1010 1011 1012
With a prefix argument SELECT (\\[universal-argument]), offer a list of \
recently clocked tasks to
clock into.  When SELECT is \\[universal-argument] \\[universal-argument], \
clock into the current task and mark
1013 1014 1015
is as the default task, a special task that will always be offered in
the clocking selection, associated with the letter `d'."
  (interactive "P")
1016
  (setq org-clock-notification-was-shown nil)
1017
  (catch 'abort
1018
    (let ((interrupting (and (not org-clock-resolving-clocks-due-to-idleness)
1019
			     (org-clocking-p)))
1020
	  ts selected-task target-pos (msg-extra "")
1021 1022
	  (leftover (and (not org-clock-resolving-clocks)
			  org-clock-leftover-time)))
1023

1024 1025 1026 1027 1028
      (when (and org-clock-auto-clock-resolution
		 (or (not interrupting)
		     (eq t org-clock-auto-clock-resolution))
		 (not org-clock-clocking-in)
		 (not org-clock-resolving-clocks))
1029
	(setq org-clock-leftover-time nil)
1030 1031
	(let ((org-clock-clocking-in t))
	  (org-resolve-clocks)))	; check if any clocks are dangling
1032

1033 1034 1035 1036 1037
      (when (equal select '(4))
	(setq selected-task (org-clock-select-task "Clock-in on task: "))
	(if selected-task
	    (setq selected-task (copy-marker selected-task))
	  (error "Abort")))
1038 1039 1040 1041 1042

      (when (equal select '(16))
	;; Mark as default clocking task
	(org-clock-mark-default-task))

1043
      (when interrupting
1044
	;; We are interrupting the clocking of a different task.
1045
	;; Save a marker to this task, so that we can go back.
1046
	;; First check if we are trying to clock into the same task!
Carsten Dominik's avatar
Carsten Dominik committed
1047
	(when (save-excursion
1048 1049 1050 1051 1052 1053 1054 1055 1056
		(unless selected-task
		  (org-back-to-heading t))
		(and (equal (marker-buffer org-clock-hd-marker)
			    (if selected-task
				(marker-buffer selected-task)
			      (current-buffer)))
		     (= (marker-position org-clock-hd-marker)
			(if selected-task
			    (marker-position selected-task)
1057 1058
			  (point)))
		     (equal org-clock-current-task (nth 4 (org-heading-components)))))
Carsten Dominik's avatar
Carsten Dominik committed
1059 1060 1061 1062 1063 1064 1065
	  (message "Clock continues in \"%s\"" org-clock-heading)
	  (throw 'abort nil))
	(move-marker org-clock-interrupted-task
		     (marker-position org-clock-marker)
		     (marker-buffer org-clock-marker))
	(let ((org-clock-clocking-in t))
	  (org-clock-out t)))
1066

1067 1068
      ;; Clock in at which position?
      (setq target-pos
Bastien Guerry's avatar
Bastien Guerry committed
1069
	    (if (and (eobp) (not (org-at-heading-p)))
1070 1071
		(point-at-bol 0)
	      (point)))
1072
      (run-hooks 'org-clock-in-prepare-hook)
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
      (save-excursion
	(when (and selected-task (marker-buffer selected-task))
	  ;; There is a selected task, move to the correct buffer
	  ;; and set the new target position.
	  (set-buffer (org-base-buffer (marker-buffer selected-task)))
	  (setq target-pos (marker-position selected-task))
	  (move-marker selected-task nil))
	(save-excursion
	  (save-restriction
	    (widen)
	    (goto-char target-pos)
	    (org-back-to-heading t)
	    (or interrupting (move-marker org-clock-interrupted-task nil))
	    (org-clock-history-push)
1087
	    (org-clock-set-current)
1088 1089 1090 1091 1092 1093
	    (cond ((functionp org-clock-in-switch-to-state)
		   (looking-at org-complex-heading-regexp)
		   (let ((newstate (funcall org-clock-in-switch-to-state
					    (match-string 2))))
		     (if newstate (org-todo newstate))))
		  ((and org-clock-in-switch-to-state
1094
			(not (looking-at (concat org-outline-regexp "[ \t]*"
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
						 org-clock-in-switch-to-state
						 "\\>"))))
		   (org-todo org-clock-in-switch-to-state)))
	    (setq org-clock-heading-for-remember
		  (and (looking-at org-complex-heading-regexp)
		       (match-end 4)
		       (org-trim (buffer-substring (match-end 1)
						   (match-end 4)))))
	    (setq org-clock-heading
		  (cond ((and org-clock-heading-function
			      (functionp org-clock-heading-function))
			 (funcall org-clock-heading-function))
			((looking-at org-complex-heading-regexp)
1108 1109 1110
			 (replace-regexp-in-string
			  "\\[\\[.*?\\]\\[\\(.*?\\)\\]\\]" "\\1"
			  (match-string 4)))
1111 1112 1113
			(t "???")))
	    (setq org-clock-heading (org-propertize org-clock-heading
						    'face nil))
1114
	    (org-clock-find-position org-clock-in-resume)
1115 1116 1117
	    (cond
	     ((and org-clock-in-resume
		   (looking-at
Bastien Guerry's avatar
Bastien Guerry committed
1118
		    (concat "^[ \t]*" org-clock-string
1119
			    " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
Bastien Guerry's avatar
Bastien Guerry committed
1120
			    " *\\sw+\.? +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")))
1121 1122 1123 1124 1125
	      (message "Matched %s" (match-string 1))
	      (setq ts (concat "[" (match-string 1) "]"))
	      (goto-char (match-end 1))
	      (setq org-clock-start-time
		    (apply 'encode-time
1126 1127 1128 1129
			   (org-parse-time-string (match-string 1))))
	      (setq org-clock-effort (org-get-effort))
	      (setq org-clock-total-time (org-clock-sum-current-item
					  (org-clock-get-sum-start))))
1130 1131 1132 1133 1134 1135 1136 1137
	     ((eq org-clock-in-resume 'auto-restart)
	      ;; called from org-clock