1 ;;; org-clock.el --- The time clocking code for Org-mode
3 ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
9 ;; 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 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; 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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; This file contains the time clocking code for Org-mode
36 (declare-function calendar-absolute-from-iso
"cal-iso" (&optional date
))
37 (declare-function notifications-notify
"notifications" (&rest params
))
38 (declare-function org-pop-to-buffer-same-window
"org-compat" (&optional buffer-or-name norecord label
))
39 (defvar org-time-stamp-formats
)
42 (defgroup org-clock nil
43 "Options concerning clocking working time in Org-mode."
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 If necessary, the drawer will be created.
51 When nil, the drawer will not be created, but used when present.
52 When an integer and the number of clocking entries in an item
53 reaches or exceeds this number, a drawer will be created.
54 When a string, it names the drawer to be used.
56 The default for this variable is the value of `org-log-into-drawer',
61 (const :tag
"Always" t
)
62 (const :tag
"Only when drawer exists" nil
)
63 (integer :tag
"When at least N clock entries")
64 (const :tag
"Into LOGBOOK drawer" "LOGBOOK")
65 (string :tag
"Into Drawer named...")))
67 (defun org-clock-into-drawer ()
68 "Return the value of `org-clock-into-drawer', but let properties overrule.
69 If the current entry has or inherits a CLOCK_INTO_DRAWER
70 property, it will be used instead of the default value; otherwise
71 if the current entry has or inherits a LOG_INTO_DRAWER property,
72 it will be used instead of the default value.
73 The default is the value of the customizable variable `org-clock-into-drawer',
75 (let ((p (org-entry-get nil
"CLOCK_INTO_DRAWER" 'inherit
))
76 (q (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit
)))
78 ((or (not (or p q
)) (equal p
"nil") (equal q
"nil")) org-clock-into-drawer
)
79 ((or (equal p
"t") (equal q
"t")) "LOGBOOK")
83 (defcustom org-clock-out-when-done t
84 "When non-nil, clock will be stopped when the clocked entry is marked DONE.
85 DONE here means any DONE-like state.
86 A nil value means clock will keep running until stopped explicitly with
87 `C-c C-x C-o', or until the clock is started in a different item.
88 Instead of t, this can also be a list of TODO states that should trigger
93 (const :tag
"Yes, when done" t
)
94 (repeat :tag
"State list"
95 (string :tag
"TODO keyword"))))
97 (defcustom org-clock-out-remove-zero-time-clocks nil
98 "Non-nil means remove the clock line when the resulting time is zero."
102 (defcustom org-clock-in-switch-to-state nil
103 "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 parameter (the current TODO state of the item) and return the
107 state to switch it to."
111 (const :tag
"Don't force a state" nil
)
112 (string :tag
"State")
113 (symbol :tag
"Function")))
115 (defcustom org-clock-out-switch-to-state nil
116 "Set task to a special todo state after clocking out.
117 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 parameter (the current TODO state of the item) and return the
120 state to switch it to."
124 (const :tag
"Don't force a state" nil
)
125 (string :tag
"State")
126 (symbol :tag
"Function")))
128 (defcustom org-clock-history-length
5
129 "Number of clock tasks to remember in history."
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."
138 (defcustom org-clock-heading-function nil
139 "When non-nil, should be a function to create `org-clock-heading'.
140 This is the string shown in the mode line when a clock is running.
141 The function is called with point at the beginning of the headline."
145 (defcustom org-clock-string-limit
0
146 "Maximum length of clock strings in the modeline. 0 means no limit."
150 (defcustom org-clock-in-resume nil
151 "If non-nil, resume clock when clocking into task with open clock.
152 When clocking into a task with a clock entry which has not been closed,
153 the clock can be resumed from that point."
157 (defcustom org-clock-persist nil
158 "When non-nil, save the running clock when Emacs is closed.
159 The clock is resumed when Emacs restarts.
160 When this is t, both the running clock, and the entire clock
161 history are saved. When this is the symbol `clock', only the
162 running clock is saved.
164 When Emacs restarts with saved clock information, the file containing the
165 running clock as well as all files mentioned in the clock history will
167 All this depends on running `org-clock-persistence-insinuate' in .emacs"
170 (const :tag
"Just the running clock" clock
)
171 (const :tag
"Just the history" history
)
172 (const :tag
"Clock and history" t
)
173 (const :tag
"No persistence" nil
)))
175 (defcustom org-clock-persist-file
(convert-standard-filename
176 "~/.emacs.d/org-clock-save.el")
177 "File to save clock data to."
181 (defcustom org-clock-persist-query-save nil
182 "When non-nil, ask before saving the current clock on exit."
186 (defcustom org-clock-persist-query-resume t
187 "When non-nil, ask before resuming any stored clock during load."
191 (defcustom org-clock-sound nil
192 "Sound that will used for notifications.
196 t standard Emacs beep
197 file name play this sound file. If not possible, fall back to beep"
200 (const :tag
"No sound" nil
)
201 (const :tag
"Standard beep" t
)
202 (file :tag
"Play sound file")))
204 (defcustom org-clock-modeline-total
'auto
205 "Default setting for the time included for the modeline clock.
206 This can be overruled locally using the CLOCK_MODELINE_TOTAL property.
209 current Only the time in the current instance of the clock
210 today All time clocked into this task today
211 repeat All time clocked into this task since last repeat
212 all All time ever recorded for this task
213 auto Automatically, either `all', or `repeat' for repeating tasks"
216 (const :tag
"Current clock" current
)
217 (const :tag
"Today's task time" today
)
218 (const :tag
"Since last repeat" repeat
)
219 (const :tag
"All task time" all
)
220 (const :tag
"Automatically, `all' or since `repeat'" auto
)))
222 (defvaralias 'org-task-overrun-text
'org-clock-task-overrun-text
)
223 (defcustom org-clock-task-overrun-text nil
224 "The extra modeline text that should indicate that the clock is overrun.
225 The can be nil to indicate that instead of adding text, the clock time
226 should get a different face (`org-mode-line-clock-overrun').
227 When this is a string, it is prepended to the clock string as an indication,
228 also using the face `org-mode-line-clock-overrun'."
232 (const :tag
"Just mark the time string" nil
)
233 (string :tag
"Text to prepend")))
235 (defcustom org-show-notification-handler nil
236 "Function or program to send notification with.
237 The function or program will be called with the notification
241 (string :tag
"Program")
242 (function :tag
"Function")))
244 (defgroup org-clocktable nil
245 "Options concerning the clock table in Org-mode."
246 :tag
"Org Clock Table"
249 (defcustom org-clocktable-defaults
252 :lang
,org-export-default-language
270 "Default properties for clock tables."
275 (defcustom org-clock-clocktable-formatter
'org-clocktable-write-default
276 "Function to turn clocking data into a table.
277 For more information, see `org-clocktable-write-default'."
278 :group
'org-clocktable
282 ;; FIXME: translate es and nl last string "Clock summary at"
283 (defcustom org-clock-clocktable-language-setup
284 '(("en" "File" "L" "Timestamp" "Headline" "Time" "ALL" "Total time" "File time" "Clock summary at")
285 ("es" "Archivo" "N" "Fecha y hora" "Tarea" "Tiempo" "TODO" "Tiempo total" "Tiempo archivo" "Clock summary at")
286 ("fr" "Fichier" "N" "Horodatage" "En-tête" "Durée" "TOUT" "Durée totale" "Durée fichier" "Horodatage sommaire à ")
287 ("nl" "Bestand" "N" "Tijdstip" "Hoofding" "Duur" "ALLES" "Totale duur" "Bestandstijd" "Clock summary at"))
288 "Terms used in clocktable, translated to different languages."
289 :group
'org-clocktable
293 (defcustom org-clock-clocktable-default-properties
'(:maxlevel
2 :scope file
)
294 "Default properties for new clocktables.
295 These will be inserted into the BEGIN line, to make it easy for users to
297 :group
'org-clocktable
300 (defcustom org-clock-idle-time nil
301 "When non-nil, resolve open clocks if the user is idle more than X minutes."
304 (const :tag
"Never" nil
)
305 (integer :tag
"After N minutes")))
307 (defcustom org-clock-auto-clock-resolution
'when-no-clock-is-running
308 "When to automatically resolve open clocks found in Org buffers."
311 (const :tag
"Never" nil
)
312 (const :tag
"Always" t
)
313 (const :tag
"When no clock is running" when-no-clock-is-running
)))
315 (defcustom org-clock-report-include-clocking-task nil
316 "When non-nil, include the current clocking task time in clock reports."
321 (defcustom org-clock-resolve-expert nil
322 "Non-nil means do not show the splash buffer with the clock resolver."
327 (defcustom org-clock-total-time-cell-format
"*%s*"
328 "Format string for the total time cells."
333 (defcustom org-clock-file-time-cell-format
"*%s*"
334 "Format string for the file time cells."
339 (defcustom org-clock-clocked-in-display
'mode-line
340 "When clocked in for a task, org-mode can display the current
341 task and accumulated time in the mode line and/or frame title.
344 both displays in both mode line and frame title
345 mode-line displays only in mode line (default)
346 frame-title displays only in frame title
347 nil current clock is not displayed"
350 (const :tag
"Mode line" mode-line
)
351 (const :tag
"Frame title" frame-title
)
352 (const :tag
"Both" both
)
353 (const :tag
"None" nil
)))
355 (defvar org-clock-in-prepare-hook nil
356 "Hook run when preparing the clock.
357 This hook is run before anything happens to the task that
358 you want to clock in. For example, you can use this hook
359 to add an effort property.")
360 (defvar org-clock-in-hook nil
361 "Hook run when starting the clock.")
362 (defvar org-clock-out-hook nil
363 "Hook run when stopping the current clock.")
365 (defvar org-clock-cancel-hook nil
366 "Hook run when cancelling the current clock.")
367 (defvar org-clock-goto-hook nil
368 "Hook run when selecting the currently clocked-in entry.")
369 (defvar org-clock-has-been-used nil
370 "Has the clock been used during the current Emacs session?")
372 ;;; The clock for measuring work time.
374 (defvar org-mode-line-string
"")
375 (put 'org-mode-line-string
'risky-local-variable t
)
377 (defvar org-frame-title-string
'(" " org-mode-line-string
))
379 (defvar org-clock-mode-line-timer nil
)
380 (defvar org-clock-idle-timer nil
)
381 (defvar org-clock-heading
) ; defined in org.el
382 (defvar org-clock-heading-for-remember
"")
383 (defvar org-clock-start-time
"")
385 (defvar org-clock-leftover-time nil
386 "If non-nil, user cancelled a clock; this is when leftover time started.")
388 (defvar org-clock-effort
""
389 "Effort estimate of the currently clocking task.")
391 (defvar org-clock-total-time nil
392 "Holds total time, spent previously on currently clocked item.
393 This does not include the time in the currently running clock.")
395 (defvar org-clock-history nil
396 "List of marker pointing to recent clocked tasks.")
398 (defvar org-clock-default-task
(make-marker)
399 "Marker pointing to the default task that should clock time.
400 The clock can be made to switch to this task after clocking out
401 of a different task.")
403 (defvar org-clock-interrupted-task
(make-marker)
404 "Marker pointing to the task that has been interrupted by the current clock.")
406 (defvar org-clock-mode-line-map
(make-sparse-keymap))
407 (define-key org-clock-mode-line-map
[mode-line mouse-2
] 'org-clock-goto
)
408 (define-key org-clock-mode-line-map
[mode-line mouse-1
] 'org-clock-menu
)
410 (defun org-clock-menu ()
414 ["Clock out" org-clock-out t
]
415 ["Change effort estimate" org-clock-modify-effort-estimate t
]
416 ["Go to clock entry" org-clock-goto t
]
417 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t
:keys
"C-u C-c C-x C-i"])))
419 (defun org-clock-history-push (&optional pos buffer
)
420 "Push a marker to the clock history."
421 (setq org-clock-history-length
(max 1 (min 35 org-clock-history-length
)))
422 (let ((m (move-marker (make-marker)
423 (or pos
(point)) (org-base-buffer
424 (or buffer
(current-buffer)))))
426 (while (setq n
(member m org-clock-history
))
427 (move-marker (car n
) nil
))
428 (setq org-clock-history
430 (mapcar (lambda (x) (if (marker-buffer x
) x nil
))
432 (when (>= (setq l
(length org-clock-history
)) org-clock-history-length
)
433 (setq org-clock-history
435 (nthcdr (- l org-clock-history-length -
1)
436 (nreverse org-clock-history
)))))
437 (push m org-clock-history
)))
439 (defun org-clock-save-markers-for-cut-and-paste (beg end
)
440 "Save relative positions of markers in region."
441 (org-check-and-save-marker org-clock-marker beg end
)
442 (org-check-and-save-marker org-clock-hd-marker beg end
)
443 (org-check-and-save-marker org-clock-default-task beg end
)
444 (org-check-and-save-marker org-clock-interrupted-task beg end
)
445 (mapc (lambda (m) (org-check-and-save-marker m beg end
))
448 (defun org-clocking-buffer ()
449 "Return the clocking buffer if we are currently clocking a task or nil."
450 (marker-buffer org-clock-marker
))
452 (defun org-clocking-p ()
453 "Return t when clocking a task."
454 (not (equal (org-clocking-buffer) nil
)))
456 (defvar org-clock-before-select-task-hook nil
457 "Hook called in task selection just before prompting the user.")
459 (defun org-clock-select-task (&optional prompt
)
460 "Select a task that recently was associated with clocking."
462 (let (sel-list rpl
(i 0) s
)
463 (save-window-excursion
464 (org-switch-to-buffer-other-window
465 (get-buffer-create "*Clock Task Select*"))
467 (when (marker-buffer org-clock-default-task
)
468 (insert (org-add-props "Default Task\n" nil
'face
'bold
))
469 (setq s
(org-clock-insert-selection-line ?d org-clock-default-task
))
471 (when (marker-buffer org-clock-interrupted-task
)
472 (insert (org-add-props "The task interrupted by starting the last one\n" nil
'face
'bold
))
473 (setq s
(org-clock-insert-selection-line ?i org-clock-interrupted-task
))
475 (when (org-clocking-p)
476 (insert (org-add-props "Current Clocking Task\n" nil
'face
'bold
))
477 (setq s
(org-clock-insert-selection-line ?c org-clock-marker
))
479 (insert (org-add-props "Recent Tasks\n" nil
'face
'bold
))
482 (when (marker-buffer m
)
484 s
(org-clock-insert-selection-line
488 (if (fboundp 'int-to-char
) (setf (car s
) (int-to-char (car s
))))
491 (run-hooks 'org-clock-before-select-task-hook
)
492 (org-fit-window-to-buffer)
493 (message (or prompt
"Select task for clocking:"))
494 (setq rpl
(read-char-exclusive))
498 ((assoc rpl sel-list
) (cdr (assoc rpl sel-list
)))
499 (t (error "Invalid task choice %c" rpl
))))))
501 (defun org-clock-insert-selection-line (i marker
)
502 "Insert a line for the clock selection menu.
503 And return a cons cell with the selection character integer and the marker
505 (when (marker-buffer marker
)
506 (let (file cat task heading prefix
)
507 (with-current-buffer (org-base-buffer (marker-buffer marker
))
513 (setq file
(buffer-file-name (marker-buffer marker
))
514 cat
(org-get-category)
515 heading
(org-get-heading 'notags
)
516 prefix
(save-excursion
517 (org-back-to-heading t
)
518 (looking-at org-outline-regexp
)
521 (org-fontify-like-in-org-mode
522 (concat prefix heading
)
524 (length prefix
)))))))
526 (insert (format "[%c] %-15s %s\n" i cat task
))
529 (defvar org-clock-task-overrun nil
530 "Internal flag indicating if the clock has overrun the planned time.")
531 (defvar org-clock-update-period
60
532 "Number of seconds between mode line clock string updates.")
534 (defun org-clock-get-clock-string ()
535 "Form a clock-string, that will be shown in the mode line.
536 If an effort estimate was defined for the current item, use
537 01:30/01:50 format (clocked/estimated).
538 If not, show simply the clocked time like 01:50."
539 (let* ((clocked-time (org-clock-get-clocked-time))
540 (h (floor clocked-time
60))
541 (m (- clocked-time
(* 60 h
))))
543 (let* ((effort-in-minutes
544 (org-duration-string-to-minutes org-clock-effort
))
545 (effort-h (floor effort-in-minutes
60))
546 (effort-m (- effort-in-minutes
(* effort-h
60)))
549 (format org-time-clocksum-format h m
)
550 'face
(if (and org-clock-task-overrun
(not org-clock-task-overrun-text
))
551 'org-mode-line-clock-overrun
'org-mode-line-clock
)))
552 (effort-str (format org-time-clocksum-format effort-h effort-m
))
553 (clockstr (org-propertize
554 (concat "[%s/" effort-str
555 "] (" (replace-regexp-in-string "%" "%%" org-clock-heading
) ")")
556 'face
'org-mode-line-clock
)))
557 (format clockstr work-done-str
))
558 (org-propertize (format
559 (concat "[" org-time-clocksum-format
" (%s)]")
560 h m org-clock-heading
)
561 'face
'org-mode-line-clock
))))
563 (defun org-clock-update-mode-line ()
565 (org-clock-notify-once-if-expired)
566 (setq org-clock-task-overrun nil
))
567 (setq org-mode-line-string
569 (let ((clock-string (org-clock-get-clock-string))
570 (help-text "Org-mode clock is running.\nmouse-1 shows a menu\nmouse-2 will jump to task"))
571 (if (and (> org-clock-string-limit
0)
572 (> (length clock-string
) org-clock-string-limit
))
574 (substring clock-string
0 org-clock-string-limit
)
575 'help-echo
(concat help-text
": " org-clock-heading
))
576 (org-propertize clock-string
'help-echo help-text
)))
577 'local-map org-clock-mode-line-map
578 'mouse-face
(if (featurep 'xemacs
) 'highlight
'mode-line-highlight
)
580 (if (and org-clock-task-overrun org-clock-task-overrun-text
)
581 (setq org-mode-line-string
582 (concat (org-propertize
583 org-clock-task-overrun-text
584 'face
'org-mode-line-clock-overrun
) org-mode-line-string
)))
585 (force-mode-line-update))
587 (defun org-clock-get-clocked-time ()
588 "Get the clocked time for the current item in minutes.
589 The time returned includes the time spent on this task in
590 previous clocking intervals."
591 (let ((currently-clocked-time
592 (floor (- (org-float-time)
593 (org-float-time org-clock-start-time
)) 60)))
594 (+ currently-clocked-time
(or org-clock-total-time
0))))
596 (defun org-clock-modify-effort-estimate (&optional value
)
597 "Add to or set the effort estimate of the item currently being clocked.
598 VALUE can be a number of minutes, or a string with format hh:mm or mm.
599 When the string starts with a + or a - sign, the current value of the effort
600 property will be changed by that amount.
601 This will update the \"Effort\" property of currently clocked item, and
604 (if (org-clock-is-active)
605 (let ((current org-clock-effort
) sign
)
607 ;; Prompt user for a value or a change
610 (format "Set effort (hh:mm or mm%s): "
612 (format ", prefix + to add to %s" org-clock-effort
)
614 (when (stringp value
)
615 ;; A string. See if it is a delta
616 (setq sign
(string-to-char value
))
617 (if (member sign
'(?- ?
+))
618 (setq current
(org-duration-string-to-minutes current
)
619 value
(substring value
1))
621 (setq value
(org-duration-string-to-minutes value
))
623 (setq value
(- current value
))
624 (if (equal ?
+ sign
) (setq value
(+ current value
)))))
625 (setq value
(max 0 value
)
626 org-clock-effort
(org-minutes-to-hh:mm-string value
))
627 (org-entry-put org-clock-marker
"Effort" org-clock-effort
)
628 (org-clock-update-mode-line)
629 (message "Effort is now %s" org-clock-effort
))
630 (message "Clock is not currently active")))
632 (defvar org-clock-notification-was-shown nil
633 "Shows if we have shown notification already.")
635 (defun org-clock-notify-once-if-expired ()
636 "Show notification if we spent more time than we estimated before.
637 Notification is shown only once."
638 (when (org-clocking-p)
639 (let ((effort-in-minutes (org-duration-string-to-minutes org-clock-effort
))
640 (clocked-time (org-clock-get-clocked-time)))
641 (if (setq org-clock-task-overrun
642 (if (or (null effort-in-minutes
) (zerop effort-in-minutes
))
644 (>= clocked-time effort-in-minutes
)))
645 (unless org-clock-notification-was-shown
646 (setq org-clock-notification-was-shown t
)
648 (format "Task '%s' should be finished by now. (%s)"
649 org-clock-heading org-clock-effort
) t
))
650 (setq org-clock-notification-was-shown nil
)))))
652 (defun org-notify (notification &optional play-sound
)
653 "Send a NOTIFICATION and maybe PLAY-SOUND."
654 (org-show-notification notification
)
655 (if play-sound
(org-clock-play-sound)))
657 (defun org-show-notification (notification)
659 Use `org-show-notification-handler' if defined,
660 use libnotify if available, or fall back on a message."
661 (cond ((functionp org-show-notification-handler
)
662 (funcall org-show-notification-handler notification
))
663 ((stringp org-show-notification-handler
)
664 (start-process "emacs-timer-notification" nil
665 org-show-notification-handler notification
))
666 ((fboundp 'notifications-notify
)
667 (notifications-notify
668 :title
"Org-mode message"
670 ;; FIXME how to link to the Org icon?
671 ;; :app-icon "~/.emacs.d/icons/mail.png"
673 ((executable-find "notify-send")
674 (start-process "emacs-timer-notification" nil
675 "notify-send" notification
))
676 ;; Maybe the handler will send a message, so only use message as
677 ;; a fall back option
678 (t (message "%s" notification
))))
680 (defun org-clock-play-sound ()
681 "Play sound as configured by `org-clock-sound'.
682 Use alsa's aplay tool if available."
684 ((not org-clock-sound
))
685 ((eq org-clock-sound t
) (beep t
) (beep t
))
686 ((stringp org-clock-sound
)
687 (let ((file (expand-file-name org-clock-sound
)))
688 (if (file-exists-p file
)
689 (if (executable-find "aplay")
690 (start-process "org-clock-play-notification" nil
693 (play-sound-file file
)
694 (error (beep t
) (beep t
)))))))))
696 (defvar org-clock-mode-line-entry nil
697 "Information for the modeline about the running clock.")
699 (defun org-find-open-clocks (file)
700 "Search through the given file and find all open clocks."
701 (let ((buf (or (get-file-buffer file
)
702 (find-file-noselect file
)))
704 (with-current-buffer buf
706 (goto-char (point-min))
707 (while (re-search-forward "CLOCK: \\(\\[.*?\\]\\)$" nil t
)
708 (push (cons (copy-marker (match-end 1) t
)
709 (org-time-string-to-time (match-string 1))) clocks
))))
712 (defsubst org-is-active-clock
(clock)
713 "Return t if CLOCK is the currently active clock."
714 (and (org-clock-is-active)
715 (= org-clock-marker
(car clock
))))
717 (defmacro org-with-clock-position
(clock &rest forms
)
718 "Evaluate FORMS with CLOCK as the current active clock."
719 `(with-current-buffer (marker-buffer (car ,clock
))
723 (goto-char (car ,clock
))
726 (def-edebug-spec org-with-clock-position
(form body
))
727 (put 'org-with-clock-position
'lisp-indent-function
1)
729 (defmacro org-with-clock
(clock &rest forms
)
730 "Evaluate FORMS with CLOCK as the current active clock.
731 This macro also protects the current active clock from being altered."
732 `(org-with-clock-position ,clock
733 (let ((org-clock-start-time (cdr ,clock
))
734 (org-clock-total-time)
737 (org-clock-marker (car ,clock
))
738 (org-clock-hd-marker (save-excursion
739 (outline-back-to-heading t
)
742 (def-edebug-spec org-with-clock
(form body
))
743 (put 'org-with-clock
'lisp-indent-function
1)
745 (defsubst org-clock-clock-in
(clock &optional resume start-time
)
746 "Clock in to the clock located by CLOCK.
747 If necessary, clock-out of the currently active clock."
748 (org-with-clock-position clock
749 (let ((org-clock-in-resume (or resume org-clock-in-resume
)))
750 (org-clock-in nil start-time
))))
752 (defsubst org-clock-clock-out
(clock &optional fail-quietly at-time
)
753 "Clock out of the clock located by CLOCK."
754 (let ((temp (copy-marker (car clock
)
755 (marker-insertion-type (car clock
)))))
756 (if (org-is-active-clock clock
)
757 (org-clock-out fail-quietly at-time
)
758 (org-with-clock clock
759 (org-clock-out fail-quietly at-time
)))
760 (setcar clock temp
)))
762 (defsubst org-clock-clock-cancel
(clock)
763 "Cancel the clock located by CLOCK."
764 (let ((temp (copy-marker (car clock
)
765 (marker-insertion-type (car clock
)))))
766 (if (org-is-active-clock clock
)
768 (org-with-clock clock
770 (setcar clock temp
)))
772 (defvar org-clock-clocking-in nil
)
773 (defvar org-clock-resolving-clocks nil
)
774 (defvar org-clock-resolving-clocks-due-to-idleness nil
)
776 (defun org-clock-resolve-clock (clock resolve-to clock-out-time
777 &optional close-p restart-p fail-quietly
)
778 "Resolve `CLOCK' given the time `RESOLVE-TO', and the present.
779 `CLOCK' is a cons cell of the form (MARKER START-TIME)."
780 (let ((org-clock-resolving-clocks t
))
783 (org-clock-clock-cancel clock
)
784 (if (and restart-p
(not org-clock-clocking-in
))
785 (org-clock-clock-in clock
)))
787 ((eq resolve-to
'now
)
789 (error "RESTART-P is not valid here"))
790 (if (or close-p org-clock-clocking-in
)
791 (org-clock-clock-out clock fail-quietly
)
792 (unless (org-is-active-clock clock
)
793 (org-clock-clock-in clock t
))))
795 ((not (time-less-p resolve-to
(current-time)))
796 (error "RESOLVE-TO must refer to a time in the past"))
800 (error "RESTART-P is not valid here"))
801 (org-clock-clock-out clock fail-quietly
(or clock-out-time
803 (unless org-clock-clocking-in
805 (setq org-clock-leftover-time
(and (null clock-out-time
)
807 (org-clock-clock-in clock nil
(and clock-out-time
810 (defun org-clock-jump-to-current-clock (&optional effective-clock
)
812 (let ((org-clock-into-drawer (org-clock-into-drawer))
813 (clock (or effective-clock
(cons org-clock-marker
814 org-clock-start-time
))))
815 (unless (marker-buffer (car clock
))
816 (error "No clock is currently running"))
817 (org-with-clock clock
(org-clock-goto))
818 (with-current-buffer (marker-buffer (car clock
))
819 (goto-char (car clock
))
820 (if org-clock-into-drawer
822 (if (stringp org-clock-into-drawer
)
823 (concat ":" org-clock-into-drawer
":")
828 (outline-back-to-heading t
)
829 (search-forward logbook
)
830 (goto-char (match-beginning 0)))
832 (outline-back-to-heading t
)
833 (search-forward logbook
)
834 (search-forward ":END:")
835 (goto-char (match-end 0)))
838 (defun org-clock-resolve (clock &optional prompt-fn last-valid fail-quietly
)
839 "Resolve an open org-mode clock.
840 An open clock was found, with `dangling' possibly being non-nil.
841 If this function was invoked with a prefix argument, non-dangling
842 open clocks are ignored. The given clock requires some sort of
843 user intervention to resolve it, either because a clock was left
844 dangling or due to an idle timeout. The clock resolution can
847 (a) deleted, the user doesn't care about the clock
848 (b) restarted from the current time (if no other clock is open)
849 (c) closed, giving the clock X minutes
850 (d) closed and then restarted
851 (e) resumed, as if the user had never left
853 The format of clock is (CONS MARKER START-TIME), where MARKER
854 identifies the buffer and position the clock is open at (and
855 thus, the heading it's under), and START-TIME is when the clock
859 (save-window-excursion
861 (unless org-clock-resolving-clocks-due-to-idleness
862 (org-clock-jump-to-current-clock clock
))
863 (unless org-clock-resolve-expert
864 (with-output-to-temp-buffer "*Org Clock*"
865 (princ "Select a Clock Resolution Command:
867 i/q/C-g Ignore this question; the same as keeping all the idle time.
869 k/K Keep X minutes of the idle time (default is all). If this
870 amount is less than the default, you will be clocked out
871 that many minutes after the time that idling began, and then
872 clocked back in at the present time.
873 g/G Indicate that you \"got back\" X minutes ago. This is quite
874 different from 'k': it clocks you out from the beginning of
875 the idle period and clock you back in X minutes ago.
876 s/S Subtract the idle time from the current clock. This is the
877 same as keeping 0 minutes.
878 C Cancel the open timer altogether. It will be as though you
880 j/J Jump to the current clock, to make manual adjustments.
882 For all these options, using uppercase makes your final state
883 to be CLOCKED OUT.")))
884 (org-fit-window-to-buffer (get-buffer-window "*Org Clock*"))
886 (when (featurep 'xemacs
)
887 (message (concat (funcall prompt-fn clock
)
889 (setq char-pressed
(read-char-exclusive)))
890 (while (or (null char-pressed
)
891 (and (not (memq char-pressed
892 '(?k ?K ?g ?G ?s ?S ?C
896 (read-char (concat (funcall prompt-fn clock
)
899 (and (not (memq char-pressed
'(?i ?q
))) char-pressed
)))))
901 (floor (/ (org-float-time
902 (time-subtract (current-time) last-valid
)) 60)))
904 (and (memq ch
'(?k ?K
))
905 (read-number "Keep how many minutes? " default
)))
907 (and (memq ch
'(?g ?G
))
908 (read-number "Got back how many minutes ago? " default
)))
909 (subtractp (memq ch
'(?s ?S
)))
910 (barely-started-p (< (- (org-float-time last-valid
)
911 (org-float-time (cdr clock
))) 45))
912 (start-over (and subtractp barely-started-p
)))
916 (org-clock-resolve-clock clock
'now nil t nil fail-quietly
))
917 (org-clock-jump-to-current-clock clock
))
919 (not (memq ch
'(?k ?K ?g ?G ?s ?S ?C
))))
922 (org-clock-resolve-clock
925 ;; If the time on the clock was less than a minute before
926 ;; the user went away, and they've ask to subtract all the
931 (and gotback
(= gotback
0)))
933 ((or (and keep
(= keep default
))
934 (and gotback
(= gotback default
)))
937 (time-add last-valid
(seconds-to-time (* 60 keep
))))
939 (time-subtract (current-time)
940 (seconds-to-time (* 60 gotback
))))
942 (error "Unexpected, please report this as a bug")))
943 (and gotback last-valid
)
944 (memq ch
'(?K ?G ?S
))
946 (not (memq ch
'(?K ?G ?S ?C
))))
949 (defun org-resolve-clocks (&optional only-dangling-p prompt-fn last-valid
)
950 "Resolve all currently open org-mode clocks.
951 If `only-dangling-p' is non-nil, only ask to resolve dangling
952 \(i.e., not currently open and valid) clocks."
954 (unless org-clock-resolving-clocks
955 (let ((org-clock-resolving-clocks t
))
956 (dolist (file (org-files-list))
957 (let ((clocks (org-find-open-clocks file
)))
958 (dolist (clock clocks
)
959 (let ((dangling (or (not (org-clock-is-active))
960 (/= (car clock
) org-clock-marker
))))
961 (if (or (not only-dangling-p
) dangling
)
968 "Dangling clock started %d mins ago"
970 (/ (- (org-float-time (current-time))
971 (org-float-time (cdr clock
))) 60))))))
973 (cdr clock
)))))))))))
975 (defun org-emacs-idle-seconds ()
976 "Return the current Emacs idle time in seconds, or nil if not idle."
977 (let ((idle-time (current-idle-time)))
979 (org-float-time idle-time
)
982 (defun org-mac-idle-seconds ()
983 "Return the current Mac idle time in seconds."
984 (string-to-number (shell-command-to-string "ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle; last}'")))
986 (defun org-x11-idle-seconds ()
987 "Return the current X11 idle time in seconds."
988 (/ (string-to-number (shell-command-to-string "x11idle")) 1000))
990 (defun org-user-idle-seconds ()
991 "Return the number of seconds the user has been idle for.
992 This routine returns a floating point number."
994 ((eq system-type
'darwin
)
995 (org-mac-idle-seconds))
996 ((eq window-system
'x
)
997 (org-x11-idle-seconds))
999 (org-emacs-idle-seconds))))
1001 (defvar org-clock-user-idle-seconds
)
1003 (defun org-resolve-clocks-if-idle ()
1004 "Resolve all currently open org-mode clocks.
1005 This is performed after `org-clock-idle-time' minutes, to check
1006 if the user really wants to stay clocked in after being idle for
1008 (when (and org-clock-idle-time
(not org-clock-resolving-clocks
)
1010 (let* ((org-clock-user-idle-seconds (org-user-idle-seconds))
1011 (org-clock-user-idle-start
1012 (time-subtract (current-time)
1013 (seconds-to-time org-clock-user-idle-seconds
)))
1014 (org-clock-resolving-clocks-due-to-idleness t
))
1015 (if (> org-clock-user-idle-seconds
(* 60 org-clock-idle-time
))
1017 (cons org-clock-marker
1018 org-clock-start-time
)
1021 (format "Clocked in & idle for %.1f mins"
1023 (time-subtract (current-time)
1024 org-clock-user-idle-start
))
1026 org-clock-user-idle-start
)))))
1028 (defvar org-clock-current-task nil
1029 "Task currently clocked in.")
1030 (defun org-clock-set-current ()
1031 "Set `org-clock-current-task' to the task currently clocked in."
1032 (setq org-clock-current-task
(nth 4 (org-heading-components))))
1034 (defun org-clock-delete-current ()
1035 "Reset `org-clock-current-task' to nil."
1036 (setq org-clock-current-task nil
))
1038 (defun org-clock-in (&optional select start-time
)
1039 "Start the clock on the current item.
1040 If necessary, clock-out of the currently active clock.
1041 With a prefix argument SELECT (\\[universal-argument]), offer a list of \
1042 recently clocked tasks to
1043 clock into. When SELECT is \\[universal-argument] \\[universal-argument], \
1044 clock into the current task and mark
1045 is as the default task, a special task that will always be offered in
1046 the clocking selection, associated with the letter `d'."
1048 (setq org-clock-notification-was-shown nil
)
1050 (let ((interrupting (and (not org-clock-resolving-clocks-due-to-idleness
)
1052 ts selected-task target-pos
(msg-extra "")
1053 (leftover (and (not org-clock-resolving-clocks
)
1054 org-clock-leftover-time
)))
1056 (when (and org-clock-auto-clock-resolution
1057 (or (not interrupting
)
1058 (eq t org-clock-auto-clock-resolution
))
1059 (not org-clock-clocking-in
)
1060 (not org-clock-resolving-clocks
))
1061 (setq org-clock-leftover-time nil
)
1062 (let ((org-clock-clocking-in t
))
1063 (org-resolve-clocks))) ; check if any clocks are dangling
1065 (when (equal select
'(4))
1066 (setq selected-task
(org-clock-select-task "Clock-in on task: "))
1068 (setq selected-task
(copy-marker selected-task
))
1071 (when (equal select
'(16))
1072 ;; Mark as default clocking task
1073 (org-clock-mark-default-task))
1076 ;; We are interrupting the clocking of a different task.
1077 ;; Save a marker to this task, so that we can go back.
1078 ;; First check if we are trying to clock into the same task!
1079 (when (save-excursion
1080 (unless selected-task
1081 (org-back-to-heading t
))
1082 (and (equal (marker-buffer org-clock-hd-marker
)
1084 (marker-buffer selected-task
)
1086 (= (marker-position org-clock-hd-marker
)
1088 (marker-position selected-task
)
1090 (equal org-clock-current-task
(nth 4 (org-heading-components)))))
1091 (message "Clock continues in \"%s\"" org-clock-heading
)
1093 (move-marker org-clock-interrupted-task
1094 (marker-position org-clock-marker
)
1095 (marker-buffer org-clock-marker
))
1096 (let ((org-clock-clocking-in t
))
1099 ;; Clock in at which position?
1101 (if (and (eobp) (not (org-at-heading-p)))
1104 (run-hooks 'org-clock-in-prepare-hook
)
1106 (when (and selected-task
(marker-buffer selected-task
))
1107 ;; There is a selected task, move to the correct buffer
1108 ;; and set the new target position.
1109 (set-buffer (org-base-buffer (marker-buffer selected-task
)))
1110 (setq target-pos
(marker-position selected-task
))
1111 (move-marker selected-task nil
))
1115 (goto-char target-pos
)
1116 (org-back-to-heading t
)
1117 (or interrupting
(move-marker org-clock-interrupted-task nil
))
1118 (org-clock-history-push)
1119 (org-clock-set-current)
1120 (cond ((functionp org-clock-in-switch-to-state
)
1121 (looking-at org-complex-heading-regexp
)
1122 (let ((newstate (funcall org-clock-in-switch-to-state
1124 (if newstate
(org-todo newstate
))))
1125 ((and org-clock-in-switch-to-state
1126 (not (looking-at (concat org-outline-regexp
"[ \t]*"
1127 org-clock-in-switch-to-state
1129 (org-todo org-clock-in-switch-to-state
)))
1130 (setq org-clock-heading-for-remember
1131 (and (looking-at org-complex-heading-regexp
)
1133 (org-trim (buffer-substring (match-end 1)
1135 (setq org-clock-heading
1136 (cond ((and org-clock-heading-function
1137 (functionp org-clock-heading-function
))
1138 (funcall org-clock-heading-function
))
1139 ((looking-at org-complex-heading-regexp
)
1140 (replace-regexp-in-string
1141 "\\[\\[.*?\\]\\[\\(.*?\\)\\]\\]" "\\1"
1144 (setq org-clock-heading
(org-propertize org-clock-heading
1146 (org-clock-find-position org-clock-in-resume
)
1148 ((and org-clock-in-resume
1150 (concat "^[ \t]*" org-clock-string
1151 " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
1152 " *\\sw+\.? +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")))
1153 (message "Matched %s" (match-string 1))
1154 (setq ts
(concat "[" (match-string 1) "]"))
1155 (goto-char (match-end 1))
1156 (setq org-clock-start-time
1158 (org-parse-time-string (match-string 1))))
1159 (setq org-clock-effort
(org-get-effort))
1160 (setq org-clock-total-time
(org-clock-sum-current-item
1161 (org-clock-get-sum-start))))
1162 ((eq org-clock-in-resume
'auto-restart
)
1163 ;; called from org-clock-load during startup,
1164 ;; do not interrupt, but warn!
1165 (message "Cannot restart clock because task does not contain unfinished clock")
1170 (insert-before-markers "\n")
1172 (org-indent-line-function)
1173 (when (and (save-excursion
1176 (beginning-of-line 1)
1177 (org-indent-line-to (- (org-get-indentation) 2)))
1178 (insert org-clock-string
" ")
1179 (setq org-clock-effort
(org-get-effort))
1180 (setq org-clock-total-time
(org-clock-sum-current-item
1181 (org-clock-get-sum-start)))
1182 (setq org-clock-start-time
1186 "You stopped another clock %d mins ago; start this one from then? "
1187 (/ (- (org-float-time (current-time))
1188 (org-float-time leftover
)) 60)))
1192 (setq ts
(org-insert-time-stamp org-clock-start-time
1193 'with-hm
'inactive
))))
1194 (move-marker org-clock-marker
(point) (buffer-base-buffer))
1195 (move-marker org-clock-hd-marker
1196 (save-excursion (org-back-to-heading t
) (point))
1197 (buffer-base-buffer))
1198 (setq org-clock-has-been-used t
)
1200 (when (or (eq org-clock-clocked-in-display
'mode-line
)
1201 (eq org-clock-clocked-in-display
'both
))
1202 (or global-mode-string
(setq global-mode-string
'("")))
1203 (or (memq 'org-mode-line-string global-mode-string
)
1204 (setq global-mode-string
1205 (append global-mode-string
'(org-mode-line-string)))))
1206 ;; add to frame title
1207 (when (and (or (eq org-clock-clocked-in-display
'frame-title
)
1208 (eq org-clock-clocked-in-display
'both
))
1209 (listp frame-title-format
))
1210 (or (memq 'org-frame-title-string frame-title-format
)
1211 (setq frame-title-format
1212 (append frame-title-format
'(org-frame-title-string)))))
1213 (org-clock-update-mode-line)
1214 (when org-clock-mode-line-timer
1215 (cancel-timer org-clock-mode-line-timer
)
1216 (setq org-clock-mode-line-timer nil
))
1217 (when org-clock-clocked-in-display
1218 (setq org-clock-mode-line-timer
1219 (run-with-timer org-clock-update-period
1220 org-clock-update-period
1221 'org-clock-update-mode-line
)))
1222 (when org-clock-idle-timer
1223 (cancel-timer org-clock-idle-timer
)
1224 (setq org-clock-idle-timer nil
))
1225 (setq org-clock-idle-timer
1226 (run-with-timer 60 60 'org-resolve-clocks-if-idle
))
1227 (message "Clock starts at %s - %s" ts msg-extra
)
1228 (run-hooks 'org-clock-in-hook
)))))))
1230 (defun org-clock-mark-default-task ()
1231 "Mark current task as default task."
1234 (org-back-to-heading t
)
1235 (move-marker org-clock-default-task
(point))))
1238 (defun org-clock-get-sum-start ()
1239 "Return the time from which clock times should be counted.
1240 This is for the currently running clock as it is displayed
1241 in the mode line. This function looks at the properties
1242 LAST_REPEAT and in particular CLOCK_MODELINE_TOTAL and the
1243 corresponding variable `org-clock-modeline-total' and then
1244 decides which time to use."
1245 (let ((cmt (or (org-entry-get nil
"CLOCK_MODELINE_TOTAL")
1246 (symbol-name org-clock-modeline-total
)))
1247 (lr (org-entry-get nil
"LAST_REPEAT")))
1249 ((equal cmt
"current")
1250 (setq msg-extra
"showing time in current clock instance")
1252 ((equal cmt
"today")
1253 (setq msg-extra
"showing today's task time.")
1254 (let* ((dt (decode-time (current-time))))
1255 (setq dt
(append (list 0 0 0) (nthcdr 3 dt
)))
1256 (if org-extend-today-until
1257 (setf (nth 2 dt
) org-extend-today-until
))
1258 (apply 'encode-time dt
)))
1259 ((or (equal cmt
"all")
1260 (and (or (not cmt
) (equal cmt
"auto"))
1262 (setq msg-extra
"showing entire task time.")
1264 ((or (equal cmt
"repeat")
1265 (and (or (not cmt
) (equal cmt
"auto"))
1267 (setq msg-extra
"showing task time since last repeat.")
1270 (org-time-string-to-time lr
)))
1273 (defun org-clock-find-position (find-unclosed)
1274 "Find the location where the next clock line should be inserted.
1275 When FIND-UNCLOSED is non-nil, first check if there is an unclosed clock
1276 line and position cursor in that line."
1277 (org-back-to-heading t
)
1279 (let* ((org-clock-into-drawer (org-clock-into-drawer))
1280 (beg (save-excursion
1281 (beginning-of-line 2)
1282 (or (bolp) (newline))
1284 (end (progn (outline-next-heading) (point)))
1285 (re (concat "^[ \t]*" org-clock-string
))
1287 (drawer (if (stringp org-clock-into-drawer
)
1288 org-clock-into-drawer
"LOGBOOK"))
1289 first last ind-last
)
1291 (when (and find-unclosed
1293 (concat "^[ \t]*" org-clock-string
1294 " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
1295 " *\\sw+ +[012][0-9]:[0-5][0-9]\\)\\][ \t]*$")
1297 (beginning-of-line 1)
1299 (when (eobp) (newline) (setq end
(max (point) end
)))
1300 (when (re-search-forward (concat "^[ \t]*:" drawer
":") end t
)
1301 ;; we seem to have a CLOCK drawer, so go there.
1302 (beginning-of-line 2)
1303 (or org-log-states-order-reversed
1304 (and (re-search-forward org-property-end-re nil t
)
1305 (goto-char (match-beginning 0))))
1307 ;; Lets count the CLOCK lines
1309 (while (re-search-forward re end t
)
1310 (setq first
(or first
(match-beginning 0))
1311 last
(match-beginning 0)
1313 (when (and (integerp org-clock-into-drawer
)
1315 (>= (1+ cnt
) org-clock-into-drawer
))
1316 ;; Wrap current entries into a new drawer
1318 (setq ind-last
(org-get-indentation))
1319 (beginning-of-line 2)
1320 (if (and (>= (org-get-indentation) ind-last
)
1322 (when (and (>= (org-get-indentation) ind-last
)
1324 (let ((struct (org-list-struct)))
1325 (goto-char (org-list-get-bottom-point struct
)))))
1327 (beginning-of-line 0)
1328 (org-indent-line-to ind-last
)
1330 (insert ":" drawer
":\n")
1331 (beginning-of-line 0)
1332 (org-indent-line-function)
1334 (beginning-of-line 2)
1335 (or org-log-states-order-reversed
1336 (and (re-search-forward org-property-end-re nil t
)
1337 (goto-char (match-beginning 0))))
1341 (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
1342 (not (equal (match-string 1) org-clock-string
)))
1343 ;; Planning info, skip to after it
1344 (beginning-of-line 2)
1345 (or (bolp) (newline)))
1346 (when (or (eq org-clock-into-drawer t
)
1347 (stringp org-clock-into-drawer
)
1348 (and (integerp org-clock-into-drawer
)
1349 (< org-clock-into-drawer
2)))
1350 (insert ":" drawer
":\n:END:\n")
1351 (beginning-of-line -
1)
1352 (org-indent-line-function)
1354 (beginning-of-line 2)
1355 (org-indent-line-function)
1357 (or org-log-states-order-reversed
1358 (and (re-search-forward org-property-end-re nil t
)
1359 (goto-char (match-beginning 0))))))))
1361 (defun org-clock-out (&optional fail-quietly at-time
)
1362 "Stop the currently running clock.
1363 If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
1366 (when (not (org-clocking-p))
1367 (setq global-mode-string
1368 (delq 'org-mode-line-string global-mode-string
))
1369 (when (listp frame-title-format
)
1370 (setq frame-title-format
1371 (delq 'org-frame-title-string frame-title-format
)))
1372 (force-mode-line-update)
1373 (if fail-quietly
(throw 'exit t
) (error "No active clock")))
1374 (let (ts te s h m remove
)
1375 (save-excursion ; Do not replace this with `with-current-buffer'.
1376 (with-no-warnings (set-buffer (org-clocking-buffer)))
1379 (goto-char org-clock-marker
)
1380 (beginning-of-line 1)
1381 (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
1382 (equal (match-string 1) org-clock-string
))
1383 (setq ts
(match-string 2))
1384 (if fail-quietly
(throw 'exit nil
) (error "Clock start time is gone")))
1385 (goto-char (match-end 0))
1386 (delete-region (point) (point-at-eol))
1388 (setq te
(org-insert-time-stamp (or at-time
(current-time))
1389 'with-hm
'inactive
))
1390 (setq s
(- (org-float-time (apply 'encode-time
(org-parse-time-string te
)))
1391 (org-float-time (apply 'encode-time
(org-parse-time-string ts
))))
1392 h
(floor (/ s
3600))
1396 (insert " => " (format "%2d:%02d" h m
))
1397 (when (setq remove
(and org-clock-out-remove-zero-time-clocks
1399 (beginning-of-line 1)
1400 (delete-region (point) (point-at-eol))
1401 (and (looking-at "\n") (> (point-max) (1+ (point)))
1403 (move-marker org-clock-marker nil
)
1404 (move-marker org-clock-hd-marker nil
)
1405 (when org-log-note-clock-out
1406 (org-add-log-setup 'clock-out nil nil nil nil
1407 (concat "# Task: " (org-get-heading t
) "\n\n")))
1408 (when org-clock-mode-line-timer
1409 (cancel-timer org-clock-mode-line-timer
)
1410 (setq org-clock-mode-line-timer nil
))
1411 (when org-clock-idle-timer
1412 (cancel-timer org-clock-idle-timer
)
1413 (setq org-clock-idle-timer nil
))
1414 (setq global-mode-string
1415 (delq 'org-mode-line-string global-mode-string
))
1416 (when (listp frame-title-format
)
1417 (setq frame-title-format
1418 (delq 'org-frame-title-string frame-title-format
)))
1419 (when org-clock-out-switch-to-state
1421 (org-back-to-heading t
)
1422 (let ((org-inhibit-logging t
)
1423 (org-clock-out-when-done nil
))
1425 ((functionp org-clock-out-switch-to-state
)
1426 (looking-at org-complex-heading-regexp
)
1427 (let ((newstate (funcall org-clock-out-switch-to-state
1429 (if newstate
(org-todo newstate
))))
1430 ((and org-clock-out-switch-to-state
1431 (not (looking-at (concat org-outline-regexp
"[ \t]*"
1432 org-clock-out-switch-to-state
1434 (org-todo org-clock-out-switch-to-state
))))))
1435 (force-mode-line-update)
1436 (message (concat "Clock stopped at %s after HH:MM = " org-time-clocksum-format
"%s") te h m
1437 (if remove
" => LINE REMOVED" ""))
1438 (run-hooks 'org-clock-out-hook
)
1439 (unless (org-clocking-p)
1440 (org-clock-delete-current)))))))
1442 (add-hook 'org-clock-out-hook
'org-clock-remove-empty-clock-drawer
)
1444 (defun org-clock-remove-empty-clock-drawer nil
1445 "Remove empty clock drawer in the current subtree."
1446 (let* ((olid (or (org-entry-get (point) "LOG_INTO_DRAWER")
1447 org-log-into-drawer
))
1448 (clock-drawer (if (eq t olid
) "LOGBOOK" olid
))
1449 (end (save-excursion (org-end-of-subtree t t
))))
1452 (org-back-to-heading t
)
1453 (while (search-forward clock-drawer end t
)
1454 (goto-char (match-beginning 0))
1455 (org-remove-empty-drawer-at clock-drawer
(point))
1456 (forward-line 1))))))
1458 (defun org-at-clock-log-p nil
1459 "Is the cursor on the clock log line?"
1461 (move-beginning-of-line 1)
1462 (looking-at "^[ \t]*CLOCK:")))
1464 (defun org-clock-timestamps-up nil
1465 "Increase CLOCK timestamps at cursor."
1467 (org-clock-timestamps-change 'up
))
1469 (defun org-clock-timestamps-down nil
1470 "Increase CLOCK timestamps at cursor."
1472 (org-clock-timestamps-change 'down
))
1474 (defun org-clock-timestamps-change (updown)
1475 "Change CLOCK timestamps synchronously at cursor.
1476 UPDOWN tells whether to change 'up or 'down."
1477 (setq org-ts-what nil
)
1478 (when (org-at-timestamp-p t
)
1479 (let ((tschange (if (eq updown
'up
) 'org-timestamp-up
1480 'org-timestamp-down
))
1481 ts1 begts1 ts2 begts2 updatets1 tdiff
)
1483 (move-beginning-of-line 1)
1484 (re-search-forward org-ts-regexp3 nil t
)
1485 (setq ts1
(match-string 0) begts1
(match-beginning 0))
1486 (when (re-search-forward org-ts-regexp3 nil t
)
1487 (setq ts2
(match-string 0) begts2
(match-beginning 0))))
1488 ;; Are we on the second timestamp?
1489 (if (<= begts2
(point)) (setq updatets1 t
))
1491 ;; fall back on org-timestamp-up if there is only one
1493 ;; setq this so that (boundp 'org-ts-what is non-nil)
1495 (let ((ts (if updatets1 ts2 ts1
))
1496 (begts (if updatets1 begts1 begts2
)))
1499 (org-time-string-to-time org-last-changed-timestamp
)
1500 (org-time-string-to-time ts
)))
1503 (org-timestamp-change
1504 (round (/ (org-float-time tdiff
)
1505 (cond ((eq org-ts-what
'minute
) 60)
1506 ((eq org-ts-what
'hour
) 3600)
1507 ((eq org-ts-what
'day
) (* 24 3600))
1508 ((eq org-ts-what
'month
) (* 24 3600 31))
1509 ((eq org-ts-what
'year
) (* 24 3600 365.2)))))
1510 org-ts-what
'updown
)))))))
1512 (defun org-clock-cancel ()
1513 "Cancel the running clock by removing the start timestamp."
1515 (when (not (org-clocking-p))
1516 (setq global-mode-string
1517 (delq 'org-mode-line-string global-mode-string
))
1518 (when (listp frame-title-format
)
1519 (setq frame-title-format
1520 (delq 'org-frame-title-string frame-title-format
)))
1521 (force-mode-line-update)
1522 (error "No active clock"))
1523 (save-excursion ; Do not replace this with `with-current-buffer'.
1524 (with-no-warnings (set-buffer (org-clocking-buffer)))
1525 (goto-char org-clock-marker
)
1526 (delete-region (1- (point-at-bol)) (point-at-eol))
1527 ;; Just in case, remove any empty LOGBOOK left over
1528 (org-remove-empty-drawer-at "LOGBOOK" (point)))
1529 (move-marker org-clock-marker nil
)
1530 (move-marker org-clock-hd-marker nil
)
1531 (setq global-mode-string
1532 (delq 'org-mode-line-string global-mode-string
))
1533 (when (listp frame-title-format
)
1534 (setq frame-title-format
1535 (delq 'org-frame-title-string frame-title-format
)))
1536 (force-mode-line-update)
1537 (message "Clock canceled")
1538 (run-hooks 'org-clock-cancel-hook
))
1540 (defun org-clock-goto (&optional select
)
1541 "Go to the currently clocked-in entry, or to the most recently clocked one.
1542 With prefix arg SELECT, offer recently clocked tasks for selection."
1547 (or (org-clock-select-task "Select task to go to: ")
1548 (error "No task selected")))
1549 ((org-clocking-p) org-clock-marker
)
1550 ((and org-clock-goto-may-find-recent-task
1551 (car org-clock-history
)
1552 (marker-buffer (car org-clock-history
)))
1554 (car org-clock-history
))
1555 (t (error "No active or recent clock task")))))
1556 (org-pop-to-buffer-same-window (marker-buffer m
))
1557 (if (or (< m
(point-min)) (> m
(point-max))) (widen))
1560 (org-back-to-heading t
)
1561 (org-cycle-hide-drawers 'children
)
1565 (message "No running clock, this is the most recently clocked task"))
1566 (run-hooks 'org-clock-goto-hook
)))
1568 (defvar org-clock-file-total-minutes nil
1569 "Holds the file total time in minutes, after a call to `org-clock-sum'.")
1570 (make-variable-buffer-local 'org-clock-file-total-minutes
)
1572 (defun org-clock-sum (&optional tstart tend headline-filter
)
1573 "Sum the times for each subtree.
1574 Puts the resulting times in minutes as a text property on each headline.
1575 TSTART and TEND can mark a time range to be considered. HEADLINE-FILTER is a
1576 zero-arg function that, if specified, is called for each headline in the time
1577 range with point at the headline. Headlines for which HEADLINE-FILTER returns
1578 nil are excluded from the clock summation."
1580 (let* ((bmp (buffer-modified-p))
1581 (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
1583 "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
1585 (ltimes (make-vector lmax
0))
1590 (if (stringp tstart
) (setq tstart
(org-time-string-to-seconds tstart
)))
1591 (if (stringp tend
) (setq tend
(org-time-string-to-seconds tend
)))
1592 (if (consp tstart
) (setq tstart
(org-float-time tstart
)))
1593 (if (consp tend
) (setq tend
(org-float-time tend
)))
1594 (remove-text-properties (point-min) (point-max)
1595 '(:org-clock-minutes t
1596 :org-clock-force-headline-inclusion t
))
1598 (goto-char (point-max))
1599 (while (re-search-backward re nil t
)
1603 (setq ts
(match-string 2)
1606 (apply 'encode-time
(org-parse-time-string ts
)))
1608 (apply 'encode-time
(org-parse-time-string te
)))
1609 ts
(if tstart
(max ts tstart
) ts
)
1610 te
(if tend
(min te tend
) te
)
1612 t1
(if (> dt
0) (+ t1
(floor (/ dt
60))) t1
)))
1615 (setq t1
(+ t1
(string-to-number (match-string 5))
1616 (* 60 (string-to-number (match-string 4))))))
1618 ;; Add the currently clocking item time to the total
1619 (when (and org-clock-report-include-clocking-task
1620 (equal (org-clocking-buffer) (current-buffer))
1621 (equal (marker-position org-clock-hd-marker
) (point))
1624 (>= (org-float-time org-clock-start-time
) tstart
)
1625 (<= (org-float-time org-clock-start-time
) tend
))
1626 (let ((time (floor (- (org-float-time)
1627 (org-float-time org-clock-start-time
)) 60)))
1628 (setq t1
(+ t1 time
))))
1629 (let* ((headline-forced
1630 (get-text-property (point)
1631 :org-clock-force-headline-inclusion
))
1633 (or (null headline-filter
)
1635 (save-match-data (funcall headline-filter
))))))
1636 (setq level
(- (match-end 1) (match-beginning 1)))
1637 (when (or (> t1
0) (> (aref ltimes level
) 0))
1638 (when (or headline-included headline-forced
)
1639 (if headline-included
1640 (loop for l from
0 to level do
1641 (aset ltimes l
(+ (aref ltimes l
) t1
))))
1642 (setq time
(aref ltimes level
))
1643 (goto-char (match-beginning 0))
1644 (put-text-property (point) (point-at-eol) :org-clock-minutes time
)
1649 (> (funcall outline-level
) 1)
1650 (outline-up-heading 1 t
)
1652 (point) (point-at-eol)
1653 :org-clock-force-headline-inclusion t
))))))
1655 (loop for l from level to
(1- lmax
) do
1656 (aset ltimes l
0)))))))
1657 (setq org-clock-file-total-minutes
(aref ltimes
0)))
1658 (set-buffer-modified-p bmp
)))
1660 (defun org-clock-sum-current-item (&optional tstart
)
1661 "Return time, clocked on current item in total."
1664 (org-narrow-to-subtree)
1665 (org-clock-sum tstart
)
1666 org-clock-file-total-minutes
)))
1668 (defun org-clock-display (&optional total-only
)
1669 "Show subtree times in the entire buffer.
1670 If TOTAL-ONLY is non-nil, only show the total time for the entire file
1673 Use \\[org-clock-remove-overlays] to remove the subtree times."
1675 (org-clock-remove-overlays)
1680 (goto-char (point-min))
1681 (while (or (and (equal (setq p
(point)) (point-min))
1682 (get-text-property p
:org-clock-minutes
))
1683 (setq p
(next-single-property-change
1684 (point) :org-clock-minutes
)))
1686 (when (setq time
(get-text-property p
:org-clock-minutes
))
1687 (org-clock-put-overlay time
(funcall outline-level
))))
1688 (setq h
(/ org-clock-file-total-minutes
60)
1689 m
(- org-clock-file-total-minutes
(* 60 h
)))
1690 ;; Arrange to remove the overlays upon next change.
1691 (when org-remove-highlights-with-change
1692 (org-add-hook 'before-change-functions
'org-clock-remove-overlays
1694 (if org-time-clocksum-use-fractional
1695 (message (concat "Total file time: " org-time-clocksum-fractional-format
1696 " (%d hours and %d minutes)")
1697 (/ (+ (* h
60.0) m
) 60.0) h m
)
1698 (message (concat "Total file time: " org-time-clocksum-format
1699 " (%d hours and %d minutes)") h m h m
))))
1701 (defvar org-clock-overlays nil
)
1702 (make-variable-buffer-local 'org-clock-overlays
)
1704 (defun org-clock-put-overlay (time &optional level
)
1705 "Put an overlays on the current line, displaying TIME.
1706 If LEVEL is given, prefix time with a corresponding number of stars.
1707 This creates a new overlay and stores it in `org-clock-overlays', so that it
1708 will be easy to remove."
1709 (let* ((c 60) (h (floor (/ time
60))) (m (- time
(* 60 h
)))
1710 (l (if level
(org-get-valid-level level
0) 0))
1711 (fmt (concat "%s " (if org-time-clocksum-use-fractional
1712 org-time-clocksum-fractional-format
1713 org-time-clocksum-format
) "%s"))
1716 (org-move-to-column c
)
1717 (unless (eolp) (skip-chars-backward "^ \t"))
1718 (skip-chars-backward " \t")
1719 (setq ov
(make-overlay (1- (point)) (point-at-eol))
1720 tx
(concat (buffer-substring (1- (point)) (point))
1721 (make-string (+ off
(max 0 (- c
(current-column)))) ?.
)
1722 (org-add-props (if org-time-clocksum-use-fractional
1725 (/ (+ (* h
60.0) m
) 60.0)
1726 (make-string (- 16 l
) ?\
))
1728 (make-string l ?
*) h m
1729 (make-string (- 16 l
) ?\
)))
1730 (list 'face
'org-clock-overlay
))
1732 (if (not (featurep 'xemacs
))
1733 (overlay-put ov
'display tx
)
1734 (overlay-put ov
'invisible t
)
1735 (overlay-put ov
'end-glyph
(make-glyph tx
)))
1736 (push ov org-clock-overlays
)))
1738 (defun org-clock-remove-overlays (&optional beg end noremove
)
1739 "Remove the occur highlights from the buffer.
1740 BEG and END are ignored. If NOREMOVE is nil, remove this function
1741 from the `before-change-functions' in the current buffer."
1743 (unless org-inhibit-highlight-removal
1744 (mapc 'delete-overlay org-clock-overlays
)
1745 (setq org-clock-overlays nil
)
1747 (remove-hook 'before-change-functions
1748 'org-clock-remove-overlays
'local
))))
1750 (defvar org-state
) ;; dynamically scoped into this function
1751 (defun org-clock-out-if-current ()
1752 "Clock out if the current entry contains the running clock.
1753 This is used to stop the clock after a TODO entry is marked DONE,
1754 and is only done if the variable `org-clock-out-when-done' is not nil."
1755 (when (and (org-clocking-p)
1756 org-clock-out-when-done
1757 (marker-buffer org-clock-marker
)
1758 (or (and (eq t org-clock-out-when-done
)
1759 (member org-state org-done-keywords
))
1760 (and (listp org-clock-out-when-done
)
1761 (member org-state org-clock-out-when-done
)))
1762 (equal (or (buffer-base-buffer (org-clocking-buffer))
1763 (org-clocking-buffer))
1764 (or (buffer-base-buffer (current-buffer))
1766 (< (point) org-clock-marker
)
1767 (> (save-excursion (outline-next-heading) (point))
1769 ;; Clock out, but don't accept a logging message for this.
1770 (let ((org-log-note-clock-out nil
)
1771 (org-clock-out-switch-to-state nil
))
1774 (add-hook 'org-after-todo-state-change-hook
1775 'org-clock-out-if-current
)
1778 (defun org-get-clocktable (&rest props
)
1779 "Get a formatted clocktable with parameters according to PROPS.
1780 The table is created in a temporary buffer, fully formatted and
1781 fontified, and then returned."
1783 (setq props
(plist-put props
:name
"clocktable"))
1784 (unless (plist-member props
:maxlevel
)
1785 (setq props
(plist-put props
:maxlevel
2)))
1786 (unless (plist-member props
:scope
)
1787 (setq props
(plist-put props
:scope
'agenda
)))
1790 (org-create-dblock props
)
1792 (font-lock-fontify-buffer)
1794 (buffer-substring (point) (progn
1795 (re-search-forward "^[ \t]*#\\+END" nil t
)
1798 (defun org-clock-report (&optional arg
)
1799 "Create a table containing a report about clocked time.
1800 If the cursor is inside an existing clocktable block, then the table
1801 will be updated. If not, a new clocktable will be inserted. The scope
1802 of the new clock will be subtree when called from within a subtree, and
1805 When called with a prefix argument, move to the first clock table in the
1806 buffer and update it."
1808 (org-clock-remove-overlays)
1810 (org-find-dblock "clocktable")
1812 (if (org-in-clocktable-p)
1813 (goto-char (org-in-clocktable-p))
1814 (let ((props (if (ignore-errors
1815 (save-excursion (org-back-to-heading)))
1816 (list :name
"clocktable" :scope
'subtree
)
1817 (list :name
"clocktable"))))
1819 (org-combine-plists org-clock-clocktable-default-properties props
))))
1820 (org-update-dblock))
1822 (defun org-day-of-week (day month year
)
1823 "Returns the day of the week as an integer."
1827 (format "%d-%02d-%02dT00:00:00" year month day
)))))
1829 (defun org-quarter-to-date (quarter year
)
1830 "Get the date (week day year) of the first day of a given quarter."
1834 (setq startday
(org-day-of-week 1 1 year
))
1837 (list 52 7 (- year
1)))
1839 (list 52 6 (- year
1)))
1841 (list 1 startday year
))
1843 (list 53 startday
(- year
1)))
1847 (setq startday
(org-day-of-week 1 4 year
))
1850 (list 13 startday year
))
1852 (list 14 startday year
))
1854 (list 13 startday year
))
1858 (setq startday
(org-day-of-week 1 7 year
))
1861 (list 26 startday year
))
1863 (list 27 startday year
))
1865 (list 26 startday year
))
1869 (setq startday
(org-day-of-week 1 10 year
))
1872 (list 39 startday year
))
1874 (list 40 startday year
))
1876 (list 39 startday year
)))))))
1878 (defun org-clock-special-range (key &optional time as-strings
)
1879 "Return two times bordering a special time range.
1880 Key is a symbol specifying the range and can be one of `today', `yesterday',
1881 `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
1882 A week starts Monday 0:00 and ends Sunday 24:00.
1883 The range is determined relative to TIME. TIME defaults to the current time.
1884 The return value is a cons cell with two internal times like the ones
1885 returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
1886 the returned times will be formatted strings."
1887 (if (integerp key
) (setq key
(intern (number-to-string key
))))
1888 (let* ((tm (decode-time (or time
(current-time))))
1889 (s 0) (m (nth 1 tm
)) (h (nth 2 tm
))
1890 (d (nth 3 tm
)) (month (nth 4 tm
)) (y (nth 5 tm
))
1892 (skey (symbol-name key
))
1894 (q (cond ((>= (nth 4 tm
) 10) 4)
1895 ((>= (nth 4 tm
) 7) 3)
1896 ((>= (nth 4 tm
) 4) 2)
1897 ((>= (nth 4 tm
) 1) 1)))
1898 s1 m1 h1 d1 month1 y1 diff ts te fm txt w date
1899 interval tmp shiftedy shiftedm shiftedq
)
1901 ((string-match "^[0-9]+$" skey
)
1902 (setq y
(string-to-number skey
) m
1 d
1 key
'year
))
1903 ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)$" skey
)
1904 (setq y
(string-to-number (match-string 1 skey
))
1905 month
(string-to-number (match-string 2 skey
))
1907 ((string-match "^\\([0-9]+\\)-[wW]\\([0-9]\\{1,2\\}\\)$" skey
)
1909 (setq y
(string-to-number (match-string 1 skey
))
1910 w
(string-to-number (match-string 2 skey
)))
1911 (setq date
(calendar-gregorian-from-absolute
1912 (calendar-absolute-from-iso (list w
1 y
))))
1913 (setq d
(nth 1 date
) month
(car date
) y
(nth 2 date
)
1916 ((string-match "^\\([0-9]+\\)-[qQ]\\([1-4]\\)$" skey
)
1918 (setq y
(string-to-number (match-string 1 skey
)))
1919 (setq q
(string-to-number (match-string 2 skey
)))
1920 (setq date
(calendar-gregorian-from-absolute
1921 (calendar-absolute-from-iso (org-quarter-to-date q y
))))
1922 (setq d
(nth 1 date
) month
(car date
) y
(nth 2 date
)
1925 ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)-\\([0-9]\\{1,2\\}\\)$" skey
)
1926 (setq y
(string-to-number (match-string 1 skey
))
1927 month
(string-to-number (match-string 2 skey
))
1928 d
(string-to-number (match-string 3 skey
))
1930 ((string-match "\\([-+][0-9]+\\)$" skey
)
1931 (setq shift
(string-to-number (match-string 1 skey
))
1932 key
(intern (substring skey
0 (match-beginning 1))))
1933 (if(and (memq key
'(quarter thisq
)) (> shift
0))
1934 (error "Looking forward with quarters isn't implemented.")
1938 (cond ((eq key
'yesterday
) (setq key
'today shift -
1))
1939 ((eq key
'lastweek
) (setq key
'week shift -
1))
1940 ((eq key
'lastmonth
) (setq key
'month shift -
1))
1941 ((eq key
'lastyear
) (setq key
'year shift -
1))
1942 ((eq key
'lastq
) (setq key
'quarter shift -
1))))
1944 ((memq key
'(day today
))
1945 (setq d
(+ d shift
) h
0 m
0 h1
24 m1
0))
1946 ((memq key
'(week thisweek
))
1947 (setq diff
(+ (* -
7 shift
) (if (= dow
0) 6 (1- dow
)))
1948 m
0 h
0 d
(- d diff
) d1
(+ 7 d
)))
1949 ((memq key
'(month thismonth
))
1950 (setq d
1 h
0 m
0 d1
1 month
(+ month shift
) month1
(1+ month
) h1
0 m1
0))
1951 ((memq key
'(quarter thisq
))
1952 ; compute if this shift remains in this year
1953 ; if not, compute how many years and quarters we have to shift (via floor*)
1954 ; and compute the shifted years, months and quarters
1956 ((< (+ (- q
1) shift
) 0) ; shift not in this year
1957 (setq interval
(* -
1 (+ (- q
1) shift
)))
1958 ; set tmp to ((years to shift) (quarters to shift))
1959 (setq tmp
(org-floor* interval
4))
1960 ; due to the use of floor, 0 quarters actually means 4
1961 (if (= 0 (nth 1 tmp
))
1962 (setq shiftedy
(- y
(nth 0 tmp
))
1965 (setq shiftedy
(- y
(+ 1 (nth 0 tmp
)))
1966 shiftedm
(- 13 (* 3 (nth 1 tmp
)))
1967 shiftedq
(- 5 (nth 1 tmp
))))
1968 (setq d
1 h
0 m
0 d1
1 month shiftedm month1
(+ 3 shiftedm
) h1
0 m1
0 y shiftedy
))
1969 ((> (+ q shift
) 0) ; shift is within this year
1970 (setq shiftedq
(+ q shift
))
1972 (setq d
1 h
0 m
0 d1
1 month
(+ 1 (* 3 (- (+ q shift
) 1))) month1
(+ 4 (* 3 (- (+ q shift
) 1))) h1
0 m1
0))))
1973 ((memq key
'(year thisyear
))
1974 (setq m
0 h
0 d
1 month
1 y
(+ y shift
) y1
(1+ y
)))
1975 (t (error "No such time block %s" key
)))
1976 (setq ts
(encode-time s m h d month y
)
1977 te
(encode-time (or s1 s
) (or m1 m
) (or h1 h
)
1978 (or d1 d
) (or month1 month
) (or y1 y
)))
1979 (setq fm
(cdr org-time-stamp-formats
))
1981 ((memq key
'(day today
))
1982 (setq txt
(format-time-string "%A, %B %d, %Y" ts
)))
1983 ((memq key
'(week thisweek
))
1984 (setq txt
(format-time-string "week %G-W%V" ts
)))
1985 ((memq key
'(month thismonth
))
1986 (setq txt
(format-time-string "%B %Y" ts
)))
1987 ((memq key
'(year thisyear
))
1988 (setq txt
(format-time-string "the year %Y" ts
)))
1989 ((memq key
'(quarter thisq
))
1990 (setq txt
(concatenate 'string
(org-count-quarter shiftedq
) " quarter of " (number-to-string shiftedy
))))
1993 (list (format-time-string fm ts
) (format-time-string fm te
) txt
)
1996 (defun org-count-quarter (n)
2003 (defun org-clocktable-shift (dir n
)
2004 "Try to shift the :block date of the clocktable at point.
2005 Point must be in the #+BEGIN: line of a clocktable, or this function
2006 will throw an error.
2007 DIR is a direction, a symbol `left', `right', `up', or `down'.
2008 Both `left' and `down' shift the block toward the past, `up' and `right'
2009 push it toward the future.
2010 N is the number of shift steps to take. The size of the step depends on
2011 the currently selected interval size."
2012 (setq n
(prefix-numeric-value n
))
2013 (and (memq dir
'(left down
)) (setq n
(- n
)))
2015 (goto-char (point-at-bol))
2016 (if (not (looking-at "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>.*?:block[ \t]+\\(\\S-+\\)"))
2017 (error "Line needs a :block definition before this command works")
2018 (let* ((b (match-beginning 1)) (e (match-end 1))
2019 (s (match-string 1))
2020 block shift ins y mw d date wp m
)
2022 ((equal s
"yesterday") (setq s
"today-1"))
2023 ((equal s
"lastweek") (setq s
"thisweek-1"))
2024 ((equal s
"lastmonth") (setq s
"thismonth-1"))
2025 ((equal s
"lastyear") (setq s
"thisyear-1"))
2026 ((equal s
"lastq") (setq s
"thisq-1")))
2029 ((string-match "^\\(today\\|thisweek\\|thismonth\\|thisyear\\|thisq\\)\\([-+][0-9]+\\)?$" s
)
2030 (setq block
(match-string 1 s
)
2031 shift
(if (match-end 2)
2032 (string-to-number (match-string 2 s
))
2034 (setq shift
(+ shift n
))
2035 (setq ins
(if (= shift
0) block
(format "%s%+d" block shift
))))
2036 ((string-match "\\([0-9]+\\)\\(-\\([wWqQ]?\\)\\([0-9]\\{1,2\\}\\)\\(-\\([0-9]\\{1,2\\}\\)\\)?\\)?" s
)
2037 ;; 1 1 2 3 3 4 4 5 6 6 5 2
2038 (setq y
(string-to-number (match-string 1 s
))
2039 wp
(and (match-end 3) (match-string 3 s
))
2040 mw
(and (match-end 4) (string-to-number (match-string 4 s
)))
2041 d
(and (match-end 6) (string-to-number (match-string 6 s
))))
2043 (d (setq ins
(format-time-string
2045 (encode-time 0 0 0 (+ d n
) m y
))))
2046 ((and wp
(string-match "w\\|W" wp
) mw
(> (length wp
) 0))
2048 (setq date
(calendar-gregorian-from-absolute
2049 (calendar-absolute-from-iso (list (+ mw n
) 1 y
))))
2050 (setq ins
(format-time-string
2052 (encode-time 0 0 0 (nth 1 date
) (car date
) (nth 2 date
)))))
2053 ((and wp
(string-match "q\\|Q" wp
) mw
(> (length wp
) 0))
2055 ; if the 4th + 1 quarter is requested we flip to the 1st quarter of the next year
2060 ; if the 1st - 1 quarter is requested we flip to the 4th quarter of the previous year
2065 (setq date
(calendar-gregorian-from-absolute
2066 (calendar-absolute-from-iso (org-quarter-to-date (+ mw n
) y
))))
2067 (setq ins
(format-time-string
2068 (concatenate 'string
(number-to-string y
) "-Q" (number-to-string (+ mw n
)))
2069 (encode-time 0 0 0 (nth 1 date
) (car date
) (nth 2 date
)))))
2071 (setq ins
(format-time-string
2073 (encode-time 0 0 0 1 (+ mw n
) y
))))
2075 (setq ins
(number-to-string (+ y n
))))))
2076 (t (error "Cannot shift clocktable block")))
2080 (delete-region (point) (+ (point) (- e b
)))
2081 (beginning-of-line 1)
2085 (defun org-dblock-write:clocktable
(params)
2086 "Write the standard clocktable."
2087 (setq params
(org-combine-plists org-clocktable-defaults params
))
2089 (let* ((scope (plist-get params
:scope
))
2090 (block (plist-get params
:block
))
2091 (ts (plist-get params
:tstart
))
2092 (te (plist-get params
:tend
))
2093 (link (plist-get params
:link
))
2094 (maxlevel (or (plist-get params
:maxlevel
) 3))
2095 (step (plist-get params
:step
))
2096 (timestamp (plist-get params
:timestamp
))
2097 (formatter (or (plist-get params
:formatter
)
2098 org-clock-clocktable-formatter
2099 'org-clocktable-write-default
))
2100 cc range-text ipos pos one-file-with-archives
2101 scope-is-list tbls level
)
2102 ;; Check if we need to do steps
2104 ;; Get the range text for the header
2105 (setq cc
(org-clock-special-range block nil t
)
2106 ts
(car cc
) te
(nth 1 cc
) range-text
(nth 2 cc
)))
2108 ;; Write many tables, in steps
2109 (unless (or block
(and ts te
))
2110 (error "Clocktable `:step' can only be used with `:block' or `:tstart,:end'"))
2111 (org-clocktable-steps params
)
2114 (setq ipos
(point)) ; remember the insertion position
2116 ;; Get the right scope
2119 ((and scope
(listp scope
) (symbolp (car scope
)))
2120 (setq scope
(eval scope
)))
2122 (setq scope
(org-agenda-files t
)))
2123 ((eq scope
'agenda-with-archives
)
2124 (setq scope
(org-agenda-files t
))
2125 (setq scope
(org-add-archive-files scope
)))
2126 ((eq scope
'file-with-archives
)
2127 (setq scope
(org-add-archive-files (list (buffer-file-name)))
2128 one-file-with-archives t
)))
2129 (setq scope-is-list
(and scope
(listp scope
)))
2131 ;; we collect from several files
2132 (let* ((files scope
)
2134 (org-prepare-agenda-buffers files
)
2135 (while (setq file
(pop files
))
2136 (with-current-buffer (find-buffer-visiting file
)
2139 (push (org-clock-get-table-data file params
) tbls
))))))
2140 ;; Just from the current file
2142 ;; get the right range into the restriction
2143 (org-prepare-agenda-buffers (list (buffer-file-name)))
2145 ((not scope
)) ; use the restriction as it is now
2146 ((eq scope
'file
) (widen))
2147 ((eq scope
'subtree
) (org-narrow-to-subtree))
2149 (while (org-up-heading-safe))
2150 (org-narrow-to-subtree))
2151 ((and (symbolp scope
) (string-match "^tree\\([0-9]+\\)$"
2152 (symbol-name scope
)))
2153 (setq level
(string-to-number (match-string 1 (symbol-name scope
))))
2155 (while (org-up-heading-safe)
2156 (looking-at org-outline-regexp
)
2157 (if (<= (org-reduced-level (funcall outline-level
)) level
)
2158 (throw 'exit nil
))))
2159 (org-narrow-to-subtree)))
2160 ;; do the table, with no file name.
2161 (push (org-clock-get-table-data nil params
) tbls
)))
2163 ;; OK, at this point we tbls as a list of tables, one per file
2164 (setq tbls
(nreverse tbls
))
2166 (setq params
(plist-put params
:multifile scope-is-list
))
2167 (setq params
(plist-put params
:one-file-with-archives
2168 one-file-with-archives
))
2170 (funcall formatter ipos tbls params
))))
2172 (defun org-clocktable-write-default (ipos tables params
)
2173 "Write out a clock table at position IPOS in the current buffer.
2174 TABLES is a list of tables with clocking data as produced by
2175 `org-clock-get-table-data'. PARAMS is the parameter property list obtained
2176 from the dynamic block definition."
2177 ;; This function looks quite complicated, mainly because there are a
2178 ;; lot of options which can add or remove columns. I have massively
2179 ;; commented this function, the I hope it is understandable. If
2180 ;; someone wants to write their own special formatter, this maybe
2181 ;; much easier because there can be a fixed format with a
2182 ;; well-defined number of columns...
2183 (let* ((hlchars '((1 .
"*") (2 .
"/")))
2184 (lwords (assoc (or (plist-get params
:lang
)
2185 org-export-default-language
)
2186 org-clock-clocktable-language-setup
))
2187 (multifile (plist-get params
:multifile
))
2188 (block (plist-get params
:block
))
2189 (ts (plist-get params
:tstart
))
2190 (te (plist-get params
:tend
))
2191 (header (plist-get params
:header
))
2192 (narrow (plist-get params
:narrow
))
2193 (link (plist-get params
:link
))
2194 (maxlevel (or (plist-get params
:maxlevel
) 3))
2195 (emph (plist-get params
:emphasize
))
2196 (level-p (plist-get params
:level
))
2197 (timestamp (plist-get params
:timestamp
))
2198 (properties (plist-get params
:properties
))
2199 (ntcol (max 1 (or (plist-get params
:tcolumns
) 100)))
2200 (rm-file-column (plist-get params
:one-file-with-archives
))
2201 (indent (plist-get params
:indent
))
2202 (case-fold-search t
)
2203 range-text total-time tbl level hlc formula pcol
2204 file-time entries entry headline
2205 recalc content narrow-cut-p tcol
)
2207 ;; Implement abbreviations
2208 (when (plist-get params
:compact
)
2209 (setq level nil indent t narrow
(or narrow
'40!) ntcol
1))
2211 ;; Some consistency test for parameters
2212 (unless (integerp ntcol
)
2213 (setq params
(plist-put params
:tcolumns
(setq ntcol
100))))
2215 (when (and narrow
(integerp narrow
) link
)
2216 ;; We cannot have both integer narrow and link
2218 "Using hard narrowing in clocktable to allow for links")
2219 (setq narrow
(intern (format "%d!" narrow
))))
2224 ((and (symbolp narrow
)
2225 (string-match "\\`[0-9]+!\\'" (symbol-name narrow
)))
2226 (setq narrow-cut-p t
2227 narrow
(string-to-number (substring (symbol-name narrow
)
2230 (error "Invalid value %s of :narrow property in clock table"
2234 ;; Get the range text for the header
2235 (setq range-text
(nth 2 (org-clock-special-range block nil t
))))
2237 ;; Compute the total time
2238 (setq total-time
(apply '+ (mapcar 'cadr tables
)))
2240 ;; Now we need to output this tsuff
2243 ;; Insert the text *before* the actual table
2244 (insert-before-markers
2246 ;; Format the standard header
2250 (format-time-string (cdr org-time-stamp-formats
))
2253 (if block
(concat ", for " range-text
".") "")
2256 ;; Insert the narrowing line
2257 (when (and narrow
(integerp narrow
) (not narrow-cut-p
))
2258 (insert-before-markers
2259 "|" ; table line starter
2260 (if multifile
"|" "") ; file column, maybe
2261 (if level-p
"|" "") ; level column, maybe
2262 (if timestamp
"|" "") ; timestamp column, maybe
2263 (if properties
(make-string (length properties
) ?|
) "") ;properties columns, maybe
2264 (format "<%d>| |\n" narrow
))) ; headline and time columns
2266 ;; Insert the table header line
2267 (insert-before-markers
2268 "|" ; table line starter
2269 (if multifile
(concat (nth 1 lwords
) "|") "") ; file column, maybe
2270 (if level-p
(concat (nth 2 lwords
) "|") "") ; level column, maybe
2271 (if timestamp
(concat (nth 3 lwords
) "|") "") ; timestamp column, maybe
2272 (if properties
(concat (mapconcat 'identity properties
"|") "|") "") ;properties columns, maybe
2273 (concat (nth 4 lwords
) "|"
2274 (nth 5 lwords
) "|\n")) ; headline and time columns
2276 ;; Insert the total time in the table
2277 (insert-before-markers
2279 "|" ; table line starter
2280 (if multifile
(concat "| " (nth 6 lwords
) " ") "")
2281 ; file column, maybe
2282 (if level-p
"|" "") ; level column, maybe
2283 (if timestamp
"|" "") ; timestamp column, maybe
2284 (if properties
(make-string (length properties
) ?|
) "") ; properties columns, maybe
2285 (concat (format org-clock-total-time-cell-format
(nth 7 lwords
)) "| ") ; instead of a headline
2286 (format org-clock-total-time-cell-format
2287 (org-minutes-to-hh:mm-string
(or total-time
0))) ; the time
2290 ;; Now iterate over the tables and insert the data
2291 ;; but only if any time has been collected
2292 (when (and total-time
(> total-time
0))
2294 (while (setq tbl
(pop tables
))
2295 ;; now tbl is the table resulting from one file.
2296 (setq file-time
(nth 1 tbl
))
2297 (when (or (and file-time
(> file-time
0))
2298 (not (plist-get params
:fileskip0
)))
2299 (insert-before-markers "|-\n") ; a hline because a new file starts
2300 ;; First the file time, if we have multiple files
2302 ;; Summarize the time collected from this file
2303 (insert-before-markers
2304 (format (concat "| %s %s | %s%s"
2305 (format org-clock-file-time-cell-format
(nth 8 lwords
))
2307 (file-name-nondirectory (car tbl
))
2308 (if level-p
"| " "") ; level column, maybe
2309 (if timestamp
"| " "") ; timestamp column, maybe
2310 (if properties
(make-string (length properties
) ?|
) "") ;properties columns, maybe
2311 (org-minutes-to-hh:mm-string
(nth 1 tbl
))))) ; the time
2313 ;; Get the list of node entries and iterate over it
2314 (setq entries
(nth 2 tbl
))
2315 (while (setq entry
(pop entries
))
2316 (setq level
(car entry
)
2317 headline
(nth 1 entry
)
2318 hlc
(if emph
(or (cdr (assoc level hlchars
)) "") ""))
2320 (if (and (string-match (concat "\\`" org-bracket-link-regexp
2325 (format "[[%s][%s]]"
2326 (match-string 1 headline
)
2327 (org-shorten-string (match-string 3 headline
)
2329 (setq headline
(org-shorten-string headline narrow
))))
2330 (insert-before-markers
2331 "|" ; start the table line
2332 (if multifile
"|" "") ; free space for file name column?
2333 (if level-p
(format "%d|" (car entry
)) "") ; level, maybe
2334 (if timestamp
(concat (nth 2 entry
) "|") "") ; timestamp, maybe
2338 (lambda (p) (or (cdr (assoc p
(nth 4 entry
))) ""))
2339 properties
"|") "|") "") ;properties columns, maybe
2340 (if indent
(org-clocktable-indent-string level
) "") ; indentation
2341 hlc headline hlc
"|" ; headline
2342 (make-string (min (1- ntcol
) (or (- level
1))) ?|
)
2343 ; empty fields for higher levels
2344 hlc
(org-minutes-to-hh:mm-string
(nth 3 entry
)) hlc
; time
2347 ;; When exporting subtrees or regions the region might be
2348 ;; activated, so let's disable ̀delete-active-region'
2349 (let ((delete-active-region nil
)) (backward-delete-char 1))
2350 (if (setq formula
(plist-get params
:formula
))
2353 ;; compute the column where the % numbers need to go
2358 (min maxlevel
(or ntcol
100))))
2359 ;; compute the column where the total time is
2363 (if timestamp
1 0)))
2366 "\n#+TBLFM: $%d='(org-clock-time%% @%d$%d $%d..$%d);%%.1f"
2367 pcol
; the column where the % numbers should go
2368 (if (and narrow
(not narrow-cut-p
)) 3 2) ; row of the total time
2369 tcol
; column of the total time
2370 tcol
(1- pcol
) ; range of columns where times can be found
2374 (insert "\n#+TBLFM: " formula
)
2376 (t (error "invalid formula in clocktable")))
2377 ;; Should we rescue an old formula?
2378 (when (stringp (setq content
(plist-get params
:content
)))
2379 (when (string-match "^\\([ \t]*#\\+tblfm:.*\\)" content
)
2381 (insert "\n" (match-string 1 (plist-get params
:content
)))
2382 (beginning-of-line 0))))
2383 ;; Back to beginning, align the table, recalculate if necessary
2385 (skip-chars-forward "^|")
2387 (when org-hide-emphasis-markers
2388 ;; we need to align a second time
2393 (if (and narrow
(not narrow-cut-p
)) (beginning-of-line 2))
2394 (org-table-goto-column pcol nil
'force
)
2396 (org-table-recalculate 'all
))
2397 (when rm-file-column
2398 ;; The file column is actually not wanted
2400 (org-table-delete-column))
2403 (defun org-clocktable-indent-string (level)
2408 (setq level
(1- level
)
2409 str
(concat str
"___")))
2412 (defun org-clocktable-steps (params)
2413 "Step through the range to make a number of clock tables."
2414 (let* ((p1 (copy-sequence params
))
2415 (ts (plist-get p1
:tstart
))
2416 (te (plist-get p1
:tend
))
2417 (step0 (plist-get p1
:step
))
2418 (step (cdr (assoc step0
'((day .
86400) (week .
604800)))))
2419 (stepskip0 (plist-get p1
:stepskip0
))
2420 (block (plist-get p1
:block
))
2421 cc range-text step-time
)
2423 (setq cc
(org-clock-special-range block nil t
)
2424 ts
(car cc
) te
(nth 1 cc
) range-text
(nth 2 cc
)))
2427 ;; If ts is a number, it's an absolute day number from org-agenda.
2428 (destructuring-bind (month day year
) (calendar-gregorian-from-absolute ts
)
2429 (setq ts
(org-float-time (encode-time 0 0 0 day month year
)))))
2431 (setq ts
(org-float-time
2432 (apply 'encode-time
(org-parse-time-string ts
))))))
2436 (destructuring-bind (month day year
) (calendar-gregorian-from-absolute te
)
2437 (setq te
(org-float-time (encode-time 0 0 0 day month year
)))))
2439 (setq te
(org-float-time
2440 (apply 'encode-time
(org-parse-time-string te
))))))
2441 (setq p1
(plist-put p1
:header
""))
2442 (setq p1
(plist-put p1
:step nil
))
2443 (setq p1
(plist-put p1
:block nil
))
2445 (or (bolp) (insert "\n"))
2446 (setq p1
(plist-put p1
:tstart
(format-time-string
2447 (org-time-stamp-format nil t
)
2448 (seconds-to-time ts
))))
2449 (setq p1
(plist-put p1
:tend
(format-time-string
2450 (org-time-stamp-format nil t
)
2451 (seconds-to-time (setq ts
(+ ts step
))))))
2452 (insert "\n" (if (eq step0
'day
) "Daily report: "
2453 "Weekly report starting on: ")
2454 (plist-get p1
:tstart
) "\n")
2455 (setq step-time
(org-dblock-write:clocktable p1
))
2456 (re-search-forward "^[ \t]*#\\+END:")
2457 (when (and (equal step-time
0) stepskip0
)
2458 ;; Remove the empty table
2459 (delete-region (point-at-bol)
2461 (re-search-backward "^\\(Daily\\|Weekly\\) report"
2466 (defun org-clock-get-table-data (file params
)
2467 "Get the clocktable data for file FILE, with parameters PARAMS.
2468 FILE is only for identification - this function assumes that
2469 the correct buffer is current, and that the wanted restriction is
2471 The return value will be a list with the file name and the total
2472 file time (in minutes) as 1st and 2nd elements. The third element
2473 of this list will be a list of headline entries. Each entry has the
2474 following structure:
2476 (LEVEL HEADLINE TIMESTAMP TIME)
2478 LEVEL: The level of the headline, as an integer. This will be
2479 the reduced leve, so 1,2,3,... even if only odd levels
2481 HEADLINE: The text of the headline. Depending on PARAMS, this may
2482 already be formatted like a link.
2483 TIMESTAMP: If PARAMS require it, this will be a time stamp found in the
2484 entry, any of SCHEDULED, DEADLINE, NORMAL, or first inactive,
2486 TIME: The sum of all time spend in this tree, in minutes. This time
2487 will of cause be restricted to the time block and tags match
2488 specified in PARAMS."
2489 (let* ((maxlevel (or (plist-get params
:maxlevel
) 3))
2490 (timestamp (plist-get params
:timestamp
))
2491 (ts (plist-get params
:tstart
))
2492 (te (plist-get params
:tend
))
2493 (block (plist-get params
:block
))
2494 (link (plist-get params
:link
))
2495 (tags (plist-get params
:tags
))
2496 (properties (plist-get params
:properties
))
2497 (inherit-property-p (plist-get params
:inherit-props
))
2499 (matcher (if tags
(cdr (org-make-tags-matcher tags
))))
2500 cc range-text st p time level hdl props tsp tbl
)
2502 (setq org-clock-file-total-minutes nil
)
2504 (setq cc
(org-clock-special-range block nil t
)
2505 ts
(car cc
) te
(nth 1 cc
) range-text
(nth 2 cc
)))
2506 (when (integerp ts
) (setq ts
(calendar-gregorian-from-absolute ts
)))
2507 (when (integerp te
) (setq te
(calendar-gregorian-from-absolute te
)))
2508 (when (and ts
(listp ts
))
2509 (setq ts
(format "%4d-%02d-%02d" (nth 2 ts
) (car ts
) (nth 1 ts
))))
2510 (when (and te
(listp te
))
2511 (setq te
(format "%4d-%02d-%02d" (nth 2 te
) (car te
) (nth 1 te
))))
2512 ;; Now the times are strings we can parse.
2513 (if ts
(setq ts
(org-float-time
2514 (apply 'encode-time
(org-parse-time-string ts
)))))
2515 (if te
(setq te
(org-float-time
2516 (apply 'encode-time
(org-parse-time-string te
)))))
2518 (org-clock-sum ts te
2519 (unless (null matcher
)
2521 (let* ((tags-list (org-get-tags-at))
2522 (org-scanner-tags tags-list
)
2523 (org-trust-scanner-tags t
))
2525 (goto-char (point-min))
2527 (while (or (and (bobp) (prog1 st
(setq st nil
))
2528 (get-text-property (point) :org-clock-minutes
)
2529 (setq p
(point-min)))
2530 (setq p
(next-single-property-change
2531 (point) :org-clock-minutes
)))
2533 (when (setq time
(get-text-property p
:org-clock-minutes
))
2535 (beginning-of-line 1)
2536 (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@#%:]+:\\)?[ \t]*$"))
2537 (setq level
(org-reduced-level
2538 (- (match-end 1) (match-beginning 1))))
2539 (<= level maxlevel
))
2540 (setq hdl
(if (not link
)
2542 (org-make-link-string
2543 (format "file:%s::%s"
2546 (org-make-org-heading-search-string
2550 (setq props
(org-entry-properties (point)))
2551 (or (cdr (assoc "SCHEDULED" props
))
2552 (cdr (assoc "DEADLINE" props
))
2553 (cdr (assoc "TIMESTAMP" props
))
2554 (cdr (assoc "TIMESTAMP_IA" props
))))
2555 props
(when properties
2559 (when (org-entry-get (point) p inherit-property-p
)
2560 (cons p
(org-entry-get (point) p inherit-property-p
))))
2562 (when (> time
0) (push (list level hdl tsp time props
) tbl
))))))
2563 (setq tbl
(nreverse tbl
))
2564 (list file org-clock-file-total-minutes tbl
))))
2566 (defun org-clock-time%
(total &rest strings
)
2567 "Compute a time fraction in percent.
2568 TOTAL s a time string like 10:21 specifying the total times.
2569 STRINGS is a list of strings that should be checked for a time.
2570 The first string that does have a time will be used.
2571 This function is made for clock tables."
2572 (let ((re "\\([0-9]+\\):\\([0-9]+\\)")
2576 (if (not (string-match re total
))
2578 (setq tot
(+ (string-to-number (match-string 2 total
))
2579 (* 60 (string-to-number (match-string 1 total
)))))
2580 (if (= tot
0.
) (throw 'exit
0.
)))
2581 (while (setq s
(pop strings
))
2582 (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s
)
2584 (/ (* 100.0 (+ (string-to-number (match-string 2 s
))
2585 (* 60 (string-to-number
2586 (match-string 1 s
)))))
2590 ;; Saving and loading the clock
2592 (defvar org-clock-loaded nil
2593 "Was the clock file loaded?")
2595 (defun org-clock-save ()
2596 "Persist various clock-related data to disk.
2597 The details of what will be saved are regulated by the variable
2598 `org-clock-persist'."
2599 (when (and org-clock-persist
2600 (or org-clock-loaded
2601 org-clock-has-been-used
2602 (not (file-exists-p org-clock-persist-file
))))
2604 (with-current-buffer (find-file (expand-file-name org-clock-persist-file
))
2606 (delete-region (point-min) (point-max))
2608 (insert (format ";; org-persist.el - %s at %s\n"
2609 system-name
(format-time-string
2610 (cdr org-time-stamp-formats
))))
2611 (if (and (memq org-clock-persist
'(t clock
))
2612 (setq b
(org-clocking-buffer))
2613 (setq b
(or (buffer-base-buffer b
) b
))
2615 (buffer-file-name b
)
2616 (or (not org-clock-persist-query-save
)
2617 (y-or-n-p (concat "Save current clock ("
2618 (substring-no-properties
2621 (insert "(setq resume-clock '(\""
2622 (buffer-file-name (org-clocking-buffer))
2623 "\" . " (int-to-string (marker-position org-clock-marker
))
2625 ;; Store clocked task history. Tasks are stored reversed to make
2627 (when (and (memq org-clock-persist
'(t history
))
2630 "(setq stored-clock-history '("
2633 (when (and (setq b
(marker-buffer m
))
2634 (setq b
(or (buffer-base-buffer b
) b
))
2636 (buffer-file-name b
))
2637 (concat "(\"" (buffer-file-name b
)
2638 "\" . " (int-to-string (marker-position m
))
2640 (reverse org-clock-history
) " ") "))\n"))
2642 (kill-buffer (current-buffer)))))))
2644 (defun org-clock-load ()
2645 "Load clock-related data from disk, maybe resuming a stored clock."
2646 (when (and org-clock-persist
(not org-clock-loaded
))
2647 (let ((filename (expand-file-name org-clock-persist-file
))
2648 (org-clock-in-resume 'auto-restart
)
2649 resume-clock stored-clock-history
)
2650 (if (not (file-readable-p filename
))
2651 (message "Not restoring clock data; %s not found"
2652 org-clock-persist-file
)
2653 (message "%s" "Restoring clock data")
2654 (setq org-clock-loaded t
)
2655 (load-file filename
)
2657 (when stored-clock-history
2658 (save-window-excursion
2659 (mapc (lambda (task)
2660 (if (file-exists-p (car task
))
2661 (org-clock-history-push (cdr task
)
2662 (find-file (car task
)))))
2663 stored-clock-history
)))
2665 (when (and resume-clock org-clock-persist
2666 (file-exists-p (car resume-clock
))
2667 (or (not org-clock-persist-query-resume
)
2671 (with-current-buffer (find-file (car resume-clock
))
2673 (goto-char (cdr resume-clock
))
2674 (org-back-to-heading t
)
2675 (and (looking-at org-complex-heading-regexp
)
2678 (when (file-exists-p (car resume-clock
))
2679 (with-current-buffer (find-file (car resume-clock
))
2680 (goto-char (cdr resume-clock
))
2681 (let ((org-clock-auto-clock-resolution nil
))
2683 (if (outline-invisible-p)
2684 (org-show-context))))))))))
2687 (defun org-clock-persistence-insinuate ()
2688 "Set up hooks for clock persistence."
2689 (add-hook 'org-mode-hook
'org-clock-load
)
2690 (add-hook 'kill-emacs-hook
'org-clock-save
))
2692 ;; Suggested bindings
2693 (org-defkey org-mode-map
"\C-c\C-x\C-e" 'org-clock-modify-effort-estimate
)
2695 (provide 'org-clock
)
2697 ;;; org-clock.el ends here