1 ;;; org-clock.el --- The time clocking code for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; This file contains the time clocking code for Org-mode
37 (declare-function calendar-absolute-from-iso
"cal-iso" (&optional date
))
39 (defgroup org-clock nil
40 "Options concerning clocking working time in Org-mode."
44 (defcustom org-clock-into-drawer
2
45 "Should clocking info be wrapped into a drawer?
46 When t, clocking info will always be inserted into a :CLOCK: drawer.
47 If necessary, the drawer will be created.
48 When nil, the drawer will not be created, but used when present.
49 When an integer and the number of clocking entries in an item
50 reaches or exceeds this number, a drawer will be created."
54 (const :tag
"Always" t
)
55 (const :tag
"Only when drawer exists" nil
)
56 (integer :tag
"When at least N clock entries")))
58 (defcustom org-clock-out-when-done t
59 "When t, the clock will be stopped when the relevant entry is marked DONE.
60 Nil means, clock will keep running until stopped explicitly with
61 `C-c C-x C-o', or until the clock is started in a different item."
65 (defcustom org-clock-out-remove-zero-time-clocks nil
66 "Non-nil means, remove the clock line when the resulting time is zero."
70 (defcustom org-clock-in-switch-to-state nil
71 "Set task to a special todo state while clocking it.
72 The value should be the state to which the entry should be switched."
76 (const :tag
"Don't force a state" nil
)
77 (string :tag
"State")))
79 (defcustom org-clock-heading-function nil
80 "When non-nil, should be a function to create `org-clock-heading'.
81 This is the string shown in the mode line when a clock is running.
82 The function is called with point at the beginning of the headline."
87 ;;; The clock for measuring work time.
89 (defvar org-mode-line-string
"")
90 (put 'org-mode-line-string
'risky-local-variable t
)
92 (defvar org-mode-line-timer nil
)
93 (defvar org-clock-heading
"")
94 (defvar org-clock-start-time
"")
96 (defun org-update-mode-line ()
97 (let* ((delta (- (time-to-seconds (current-time))
98 (time-to-seconds org-clock-start-time
)))
99 (h (floor delta
3600))
100 (m (floor (- delta
(* 3600 h
)) 60)))
101 (setq org-mode-line-string
102 (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading
)
103 'help-echo
"Org-mode clock is running"))
104 (force-mode-line-update)))
106 (defvar org-clock-mode-line-entry nil
107 "Information for the modeline about the running clock.")
109 (defun org-clock-in ()
110 "Start the clock on the current item.
111 If necessary, clock-out of the currently active clock."
116 (org-back-to-heading t
)
117 (when (and org-clock-in-switch-to-state
118 (not (looking-at (concat outline-regexp
"[ \t]*"
119 org-clock-in-switch-to-state
121 (org-todo org-clock-in-switch-to-state
))
122 (if (and org-clock-heading-function
123 (functionp org-clock-heading-function
))
124 (setq org-clock-heading
(funcall org-clock-heading-function
))
125 (if (looking-at org-complex-heading-regexp
)
126 (setq org-clock-heading
(match-string 4))
127 (setq org-clock-heading
"???")))
128 (setq org-clock-heading
(propertize org-clock-heading
'face nil
))
129 (org-clock-find-position)
131 (insert "\n") (backward-char 1)
133 (insert org-clock-string
" ")
134 (setq org-clock-start-time
(current-time))
135 (setq ts
(org-insert-time-stamp (current-time) 'with-hm
'inactive
))
136 (move-marker org-clock-marker
(point) (buffer-base-buffer))
137 (or global-mode-string
(setq global-mode-string
'("")))
138 (or (memq 'org-mode-line-string global-mode-string
)
139 (setq global-mode-string
140 (append global-mode-string
'(org-mode-line-string))))
141 (org-update-mode-line)
142 (setq org-mode-line-timer
(run-with-timer 60 60 'org-update-mode-line
))
143 (message "Clock started at %s" ts
))))
145 (defun org-clock-find-position ()
146 "Find the location where the next clock line should be inserted."
147 (org-back-to-heading t
)
149 (let ((beg (save-excursion
150 (beginning-of-line 2)
151 (or (bolp) (newline))
153 (end (progn (outline-next-heading) (point)))
154 (re (concat "^[ \t]*" org-clock-string
))
158 (when (eobp) (newline) (setq end
(max (point) end
)))
159 (when (re-search-forward "^[ \t]*:CLOCK:" end t
)
160 ;; we seem to have a CLOCK drawer, so go there.
161 (beginning-of-line 2)
163 ;; Lets count the CLOCK lines
165 (while (re-search-forward re end t
)
166 (setq first
(or first
(match-beginning 0))
167 last
(match-beginning 0)
169 (when (and (integerp org-clock-into-drawer
)
170 (>= (1+ cnt
) org-clock-into-drawer
))
171 ;; Wrap current entries into a new drawer
173 (beginning-of-line 2)
174 (if (org-at-item-p) (org-end-of-item))
176 (beginning-of-line 0)
177 (org-indent-line-function)
180 (beginning-of-line 0)
181 (org-indent-line-function)
183 (beginning-of-line 2)
187 (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
188 (not (equal (match-string 1) org-clock-string
)))
189 ;; Planning info, skip to after it
190 (beginning-of-line 2)
191 (or (bolp) (newline)))
192 (when (eq t org-clock-into-drawer
)
193 (insert ":CLOCK:\n:END:\n")
194 (beginning-of-line -
1)
195 (org-indent-line-function)
197 (beginning-of-line 2)
198 (org-indent-line-function)))))
200 (defun org-clock-out (&optional fail-quietly
)
201 "Stop the currently running clock.
202 If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
205 (if (not (marker-buffer org-clock-marker
))
206 (if fail-quietly
(throw 'exit t
) (error "No active clock")))
207 (let (ts te s h m remove
)
209 (set-buffer (marker-buffer org-clock-marker
))
212 (goto-char org-clock-marker
)
213 (beginning-of-line 1)
214 (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
215 (equal (match-string 1) org-clock-string
))
216 (setq ts
(match-string 2))
217 (if fail-quietly
(throw 'exit nil
) (error "Clock start time is gone")))
218 (goto-char (match-end 0))
219 (delete-region (point) (point-at-eol))
221 (setq te
(org-insert-time-stamp (current-time) 'with-hm
'inactive
))
222 (setq s
(- (time-to-seconds (apply 'encode-time
(org-parse-time-string te
)))
223 (time-to-seconds (apply 'encode-time
(org-parse-time-string ts
))))
228 (insert " => " (format "%2d:%02d" h m
))
229 (setq remove
(and (= (+ h m
) 0) org-clock-out-remove-zero-time-clocks
))
230 (move-marker org-clock-marker nil
)
231 (when org-log-note-clock-out
232 (org-add-log-setup 'clock-out
))
233 (when org-mode-line-timer
234 (cancel-timer org-mode-line-timer
)
235 (setq org-mode-line-timer nil
))
236 (setq global-mode-string
237 (delq 'org-mode-line-string global-mode-string
))
238 (force-mode-line-update)
239 (message "Clock stopped at %s after HH:MM = %d:%02d%s" te h m
240 (if remove
" => REMOVING" ""))
242 (beginning-of-line 1)
243 (let ((kill-whole-line t
))
245 (beginning-of-line 0)))))))
247 (defun org-clock-cancel ()
248 "Cancel the running clock be removing the start timestamp."
250 (if (not (marker-buffer org-clock-marker
))
251 (error "No active clock"))
253 (set-buffer (marker-buffer org-clock-marker
))
254 (goto-char org-clock-marker
)
255 (delete-region (1- (point-at-bol)) (point-at-eol)))
256 (setq global-mode-string
257 (delq 'org-mode-line-string global-mode-string
))
258 (force-mode-line-update)
259 (message "Clock canceled"))
261 (defun org-clock-goto (&optional delete-windows
)
262 "Go to the currently clocked-in entry."
264 (if (not (marker-buffer org-clock-marker
))
265 (error "No active clock"))
266 (switch-to-buffer-other-window
267 (marker-buffer org-clock-marker
))
268 (if delete-windows
(delete-other-windows))
269 (goto-char org-clock-marker
)
271 (org-back-to-heading)
272 (org-cycle-hide-drawers 'children
)
275 (defvar org-clock-file-total-minutes nil
276 "Holds the file total time in minutes, after a call to `org-clock-sum'.")
277 (make-variable-buffer-local 'org-clock-file-total-minutes
)
279 (defun org-clock-sum (&optional tstart tend
)
280 "Sum the times for each subtree.
281 Puts the resulting times in minutes as a text property on each headline."
283 (let* ((bmp (buffer-modified-p))
284 (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
286 "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
288 (ltimes (make-vector lmax
0))
293 (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t
))
295 (goto-char (point-max))
296 (while (re-search-backward re nil t
)
300 (setq ts
(match-string 2)
303 (apply 'encode-time
(org-parse-time-string ts
)))
305 (apply 'encode-time
(org-parse-time-string te
)))
306 ts
(if tstart
(max ts tstart
) ts
)
307 te
(if tend
(min te tend
) te
)
309 t1
(if (> dt
0) (+ t1
(floor (/ dt
60))) t1
)))
312 (setq t1
(+ t1
(string-to-number (match-string 5))
313 (* 60 (string-to-number (match-string 4))))))
315 (setq level
(- (match-end 1) (match-beginning 1)))
316 (when (or (> t1
0) (> (aref ltimes level
) 0))
317 (loop for l from
0 to level do
318 (aset ltimes l
(+ (aref ltimes l
) t1
)))
319 (setq t1
0 time
(aref ltimes level
))
320 (loop for l from level to
(1- lmax
) do
322 (goto-char (match-beginning 0))
323 (put-text-property (point) (point-at-eol) :org-clock-minutes time
)))))
324 (setq org-clock-file-total-minutes
(aref ltimes
0)))
325 (set-buffer-modified-p bmp
)))
327 (defun org-clock-display (&optional total-only
)
328 "Show subtree times in the entire buffer.
329 If TOTAL-ONLY is non-nil, only show the total time for the entire file
332 (org-remove-clock-overlays)
337 (goto-char (point-min))
338 (while (or (and (equal (setq p
(point)) (point-min))
339 (get-text-property p
:org-clock-minutes
))
340 (setq p
(next-single-property-change
341 (point) :org-clock-minutes
)))
343 (when (setq time
(get-text-property p
:org-clock-minutes
))
344 (org-put-clock-overlay time
(funcall outline-level
))))
345 (setq h
(/ org-clock-file-total-minutes
60)
346 m
(- org-clock-file-total-minutes
(* 60 h
)))
347 ;; Arrange to remove the overlays upon next change.
348 (when org-remove-highlights-with-change
349 (org-add-hook 'before-change-functions
'org-remove-clock-overlays
351 (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m
)))
353 (defvar org-clock-overlays nil
)
354 (make-variable-buffer-local 'org-clock-overlays
)
356 (defun org-put-clock-overlay (time &optional level
)
357 "Put an overlays on the current line, displaying TIME.
358 If LEVEL is given, prefix time with a corresponding number of stars.
359 This creates a new overlay and stores it in `org-clock-overlays', so that it
360 will be easy to remove."
361 (let* ((c 60) (h (floor (/ time
60))) (m (- time
(* 60 h
)))
362 (l (if level
(org-get-valid-level level
0) 0))
366 (unless (eolp) (skip-chars-backward "^ \t"))
367 (skip-chars-backward " \t")
368 (setq ov
(org-make-overlay (1- (point)) (point-at-eol))
369 tx
(concat (buffer-substring (1- (point)) (point))
370 (make-string (+ off
(max 0 (- c
(current-column)))) ?.
)
371 (org-add-props (format "%s %2d:%02d%s"
372 (make-string l ?
*) h m
373 (make-string (- 16 l
) ?\
))
374 '(face secondary-selection
))
376 (if (not (featurep 'xemacs
))
377 (org-overlay-put ov
'display tx
)
378 (org-overlay-put ov
'invisible t
)
379 (org-overlay-put ov
'end-glyph
(make-glyph tx
)))
380 (push ov org-clock-overlays
)))
382 (defun org-remove-clock-overlays (&optional beg end noremove
)
383 "Remove the occur highlights from the buffer.
384 BEG and END are ignored. If NOREMOVE is nil, remove this function
385 from the `before-change-functions' in the current buffer."
387 (unless org-inhibit-highlight-removal
388 (mapc 'org-delete-overlay org-clock-overlays
)
389 (setq org-clock-overlays nil
)
391 (remove-hook 'before-change-functions
392 'org-remove-clock-overlays
'local
))))
394 (defvar state
) ;; dynamically scoped into this function
395 (defun org-clock-out-if-current ()
396 "Clock out if the current entry contains the running clock.
397 This is used to stop the clock after a TODO entry is marked DONE,
398 and is only done if the variable `org-clock-out-when-done' is not nil."
399 (when (and org-clock-out-when-done
400 (member state org-done-keywords
)
401 (equal (marker-buffer org-clock-marker
) (current-buffer))
402 (< (point) org-clock-marker
)
403 (> (save-excursion (outline-next-heading) (point))
405 ;; Clock out, but don't accept a logging message for this.
406 (let ((org-log-note-clock-out nil
))
409 (add-hook 'org-after-todo-state-change-hook
410 'org-clock-out-if-current
)
413 (defun org-get-clocktable (&rest props
)
414 "Get a formatted clocktable with parameters according to PROPS.
415 The table is created in a temporary buffer, fully formatted and
416 fontified, and then returned."
418 (setq props
(plist-put props
:name
"clocktable"))
419 (unless (plist-member props
:maxlevel
)
420 (setq props
(plist-put props
:maxlevel
2)))
421 (unless (plist-member props
:scope
)
422 (setq props
(plist-put props
:scope
'agenda
)))
425 (org-create-dblock props
)
427 (font-lock-fontify-buffer)
429 (buffer-substring (point) (progn
430 (re-search-forward "^#\\+END" nil t
)
433 (defun org-clock-report (&optional arg
)
434 "Create a table containing a report about clocked time.
435 If the cursor is inside an existing clocktable block, then the table
436 will be updated. If not, a new clocktable will be inserted.
437 When called with a prefix argument, move to the first clock table in the
438 buffer and update it."
440 (org-remove-clock-overlays)
442 (org-find-dblock "clocktable")
444 (if (org-in-clocktable-p)
445 (goto-char (org-in-clocktable-p))
446 (org-create-dblock (list :name
"clocktable"
447 :maxlevel
2 :scope
'file
)))
450 (defun org-in-clocktable-p ()
451 "Check if the cursor is in a clocktable."
452 (let ((pos (point)) start
)
455 (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t
)
456 (setq start
(match-beginning 0))
457 (re-search-forward "^#\\+END:.*" nil t
)
458 (>= (match-end 0) pos
)
461 (defun org-clock-special-range (key &optional time as-strings
)
462 "Return two times bordering a special time range.
463 Key is a symbol specifying the range and can be one of `today', `yesterday',
464 `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
465 A week starts Monday 0:00 and ends Sunday 24:00.
466 The range is determined relative to TIME. TIME defaults to the current time.
467 The return value is a cons cell with two internal times like the ones
468 returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
469 the returned times will be formatted strings."
470 (if (integerp key
) (setq key
(intern (number-to-string key
))))
471 (let* ((tm (decode-time (or time
(current-time))))
472 (s 0) (m (nth 1 tm
)) (h (nth 2 tm
))
473 (d (nth 3 tm
)) (month (nth 4 tm
)) (y (nth 5 tm
))
475 (skey (symbol-name key
))
477 s1 m1 h1 d1 month1 y1 diff ts te fm txt w date
)
479 ((string-match "^[0-9]+$" skey
)
480 (setq y
(string-to-number skey
) m
1 d
1 key
'year
))
481 ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)$" skey
)
482 (setq y
(string-to-number (match-string 1 skey
))
483 month
(string-to-number (match-string 2 skey
))
485 ((string-match "^\\([0-9]+\\)-[wW]\\([0-9]\\{1,2\\}\\)$" skey
)
487 (setq y
(string-to-number (match-string 1 skey
))
488 w
(string-to-number (match-string 2 skey
)))
489 (setq date
(calendar-gregorian-from-absolute
490 (calendar-absolute-from-iso (list w
1 y
))))
491 (setq d
(nth 1 date
) month
(car date
) y
(nth 2 date
)
493 ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)-\\([0-9]\\{1,2\\}\\)$" skey
)
494 (setq y
(string-to-number (match-string 1 skey
))
495 month
(string-to-number (match-string 2 skey
))
496 d
(string-to-number (match-string 3 skey
))
498 ((string-match "\\([-+][0-9]+\\)$" skey
)
499 (setq shift
(string-to-number (match-string 1 skey
))
500 key
(intern (substring skey
0 (match-beginning 1))))))
502 (cond ((eq key
'yesterday
) (setq key
'today shift -
1))
503 ((eq key
'lastweek
) (setq key
'week shift -
1))
504 ((eq key
'lastmonth
) (setq key
'month shift -
1))
505 ((eq key
'lastyear
) (setq key
'year shift -
1))))
507 ((memq key
'(day today
))
508 (setq d
(+ d shift
) h
0 m
0 h1
24 m1
0))
509 ((memq key
'(week thisweek
))
510 (setq diff
(+ (* -
7 shift
) (if (= dow
0) 6 (1- dow
)))
511 m
0 h
0 d
(- d diff
) d1
(+ 7 d
)))
512 ((memq key
'(month thismonth
))
513 (setq d
1 h
0 m
0 d1
1 month
(+ month shift
) month1
(1+ month
) h1
0 m1
0))
514 ((memq key
'(year thisyear
))
515 (setq m
0 h
0 d
1 month
1 y
(+ y shift
) y1
(1+ y
)))
516 (t (error "No such time block %s" key
)))
517 (setq ts
(encode-time s m h d month y
)
518 te
(encode-time (or s1 s
) (or m1 m
) (or h1 h
)
519 (or d1 d
) (or month1 month
) (or y1 y
)))
520 (setq fm
(cdr org-time-stamp-formats
))
522 ((memq key
'(day today
))
523 (setq txt
(format-time-string "%A, %B %d, %Y" ts
)))
524 ((memq key
'(week thisweek
))
525 (setq txt
(format-time-string "week %G-W%V" ts
)))
526 ((memq key
'(month thismonth
))
527 (setq txt
(format-time-string "%B %Y" ts
)))
528 ((memq key
'(year thisyear
))
529 (setq txt
(format-time-string "the year %Y" ts
))))
531 (list (format-time-string fm ts
) (format-time-string fm te
) txt
)
534 (defun org-clocktable-shift (dir n
)
535 "Try to shift the :block date of the clocktable at point.
536 Point must be in the #+BEGIN: line of a clocktable, or this function
538 DIR is a direction, a symbol `left', `right', `up', or `down'.
539 Both `left' and `down' shift the block toward the past, `up' and `right'
540 push it toward the future.
541 N is the number of shift steps to take. The size of the step depends on
542 the currently selected interval size."
543 (setq n
(prefix-numeric-value n
))
544 (and (memq dir
'(left down
)) (setq n
(- n
)))
546 (goto-char (point-at-bol))
547 (if (not (looking-at "#\\+BEGIN: clocktable\\>.*?:block[ \t]+\\(\\S-+\\)"))
548 (error "Line needs a :block definition before this command works")
549 (let* ((b (match-beginning 1)) (e (match-end 1))
551 block shift ins y mw d date wp m
)
553 ((string-match "^\\(today\\|thisweek\\|thismonth\\|thisyear\\)\\([-+][0-9]+\\)?$" s
)
554 (setq block
(match-string 1 s
)
555 shift
(if (match-end 2)
556 (string-to-number (match-string 2 s
))
558 (setq shift
(+ shift n
))
559 (setq ins
(if (= shift
0) block
(format "%s%+d" block shift
))))
560 ((string-match "\\([0-9]+\\)\\(-\\([wW]?\\)\\([0-9]\\{1,2\\}\\)\\(-\\([0-9]\\{1,2\\}\\)\\)?\\)?" s
)
561 ;; 1 1 2 3 3 4 4 5 6 6 5 2
562 (setq y
(string-to-number (match-string 1 s
))
563 wp
(and (match-end 3) (match-string 3 s
))
564 mw
(and (match-end 4) (string-to-number (match-string 4 s
)))
565 d
(and (match-end 6) (string-to-number (match-string 6 s
))))
567 (d (setq ins
(format-time-string
569 (encode-time 0 0 0 (+ d n
) m y
))))
570 ((and wp mw
(> (length wp
) 0))
572 (setq date
(calendar-gregorian-from-absolute (calendar-absolute-from-iso (list (+ mw n
) 1 y
))))
573 (setq ins
(format-time-string
575 (encode-time 0 0 0 (nth 1 date
) (car date
) (nth 2 date
)))))
577 (setq ins
(format-time-string
579 (encode-time 0 0 0 1 (+ mw n
) y
))))
581 (setq ins
(number-to-string (+ y n
))))))
582 (t (error "Cannot shift clocktable block")))
586 (delete-region (point) (+ (point) (- e b
)))
587 (beginning-of-line 1)
591 (defun org-dblock-write:clocktable
(params)
592 "Write the standard clocktable."
594 (let* ((hlchars '((1 .
"*") (2 .
"/")))
597 (scope (plist-get params
:scope
))
598 (tostring (plist-get params
:tostring
))
599 (multifile (plist-get params
:multifile
))
600 (header (plist-get params
:header
))
601 (maxlevel (or (plist-get params
:maxlevel
) 3))
602 (step (plist-get params
:step
))
603 (emph (plist-get params
:emphasize
))
604 (ts (plist-get params
:tstart
))
605 (te (plist-get params
:tend
))
606 (block (plist-get params
:block
))
607 (link (plist-get params
:link
))
608 ipos time p level hlc hdl
609 cc beg end pos tbl tbl1 range-text rm-file-column scope-is-list
)
610 (setq org-clock-file-total-minutes nil
)
612 (org-clocktable-steps params
)
615 (setq cc
(org-clock-special-range block nil t
)
616 ts
(car cc
) te
(nth 1 cc
) range-text
(nth 2 cc
)))
617 (when (integerp ts
) (setq ts
(calendar-gregorian-from-absolute ts
)))
618 (when (integerp te
) (setq te
(calendar-gregorian-from-absolute te
)))
619 (when (and ts
(listp ts
))
620 (setq ts
(format "%4d-%02d-%02d" (nth 2 ts
) (car ts
) (nth 1 ts
))))
621 (when (and te
(listp te
))
622 (setq te
(format "%4d-%02d-%02d" (nth 2 te
) (car te
) (nth 1 te
))))
623 ;; Now the times are strings we can parse.
624 (if ts
(setq ts
(time-to-seconds
625 (apply 'encode-time
(org-parse-time-string ts
)))))
626 (if te
(setq te
(time-to-seconds
627 (apply 'encode-time
(org-parse-time-string te
)))))
628 (move-marker ins
(point))
631 ;; Get the right scope
634 ((and scope
(listp scope
) (symbolp (car scope
)))
635 (setq scope
(eval scope
)))
637 (setq scope
(org-agenda-files t
)))
638 ((eq scope
'agenda-with-archives
)
639 (setq scope
(org-agenda-files t
))
640 (setq scope
(org-add-archive-files scope
)))
641 ((eq scope
'file-with-archives
)
642 (setq scope
(org-add-archive-files (list (buffer-file-name)))
644 (setq scope-is-list
(and scope
(listp scope
)))
648 ((eq scope
'file
) (widen))
649 ((eq scope
'subtree
) (org-narrow-to-subtree))
651 (while (org-up-heading-safe))
652 (org-narrow-to-subtree))
653 ((and (symbolp scope
) (string-match "^tree\\([0-9]+\\)$"
654 (symbol-name scope
)))
655 (setq level
(string-to-number (match-string 1 (symbol-name scope
))))
657 (while (org-up-heading-safe)
658 (looking-at outline-regexp
)
659 (if (<= (org-reduced-level (funcall outline-level
)) level
)
661 (org-narrow-to-subtree))
665 (p1 (copy-sequence params
))
667 (plist-put p1
:tostring t
)
668 (plist-put p1
:multifile t
)
669 (plist-put p1
:scope
'file
)
670 (org-prepare-agenda-buffers files
)
671 (while (setq file
(pop files
))
672 (with-current-buffer (find-buffer-visiting file
)
673 (setq tbl1
(org-dblock-write:clocktable p1
))
675 (push (org-clocktable-add-file
677 (concat "| |*File time*|*"
678 (org-minutes-to-hh:mm-string
679 org-clock-file-total-minutes
)
682 (setq total-time
(+ (or total-time
0)
683 org-clock-file-total-minutes
))))))))
686 (unless scope-is-list
687 (org-clock-sum ts te
)
688 (goto-char (point-min))
689 (while (setq p
(next-single-property-change (point) :org-clock-minutes
))
691 (when (setq time
(get-text-property p
:org-clock-minutes
))
693 (beginning-of-line 1)
694 (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
695 (setq level
(org-reduced-level
696 (- (match-end 1) (match-beginning 1))))
698 (setq hlc
(if emph
(or (cdr (assoc level hlchars
)) "") "")
701 (org-make-link-string
702 (format "file:%s::%s"
705 (org-make-org-heading-search-string
708 (if (and (not multifile
) (= level
1)) (push "|-" tbl
))
710 "| " (int-to-string level
) "|" hlc hdl hlc
" |"
711 (make-string (1- level
) ?|
)
712 hlc
(org-minutes-to-hh:mm-string time
) hlc
714 (setq tbl
(nreverse tbl
))
716 (if tbl
(mapconcat 'identity tbl
"\n") nil
)
718 (insert-before-markers
723 (format-time-string (cdr org-time-stamp-formats
))
726 (if block
(concat ", for " range-text
".") "")
728 (if scope-is-list
"|File" "")
729 "|L|Headline|Time|\n")
730 (setq total-time
(or total-time org-clock-file-total-minutes
))
731 (insert-before-markers
733 (if scope-is-list
"|" "")
736 (org-minutes-to-hh:mm-string
(or total-time
0))
738 (setq tbl
(delq nil tbl
))
739 (if (and (stringp (car tbl
)) (> (length (car tbl
)) 1)
740 (equal (substring (car tbl
) 0 2) "|-"))
742 (insert-before-markers (mapconcat
743 'identity
(delq nil tbl
)
744 (if scope-is-list
"\n|-\n" "\n")))
745 (backward-delete-char 1)
747 (skip-chars-forward "^|")
751 (org-table-delete-column)))))))
753 (defun org-clocktable-steps (params)
754 (let* ((p1 (copy-sequence params
))
755 (ts (plist-get p1
:tstart
))
756 (te (plist-get p1
:tend
))
757 (step0 (plist-get p1
:step
))
758 (step (cdr (assoc step0
'((day .
86400) (week .
604800)))))
759 (block (plist-get p1
:block
))
762 (setq cc
(org-clock-special-range block nil t
)
763 ts
(car cc
) te
(nth 1 cc
) range-text
(nth 2 cc
)))
764 (if ts
(setq ts
(time-to-seconds
765 (apply 'encode-time
(org-parse-time-string ts
)))))
766 (if te
(setq te
(time-to-seconds
767 (apply 'encode-time
(org-parse-time-string te
)))))
768 (plist-put p1
:header
"")
769 (plist-put p1
:step nil
)
770 (plist-put p1
:block nil
)
772 (or (bolp) (insert "\n"))
773 (plist-put p1
:tstart
(format-time-string
774 (car org-time-stamp-formats
)
775 (seconds-to-time ts
)))
776 (plist-put p1
:tend
(format-time-string
777 (car org-time-stamp-formats
)
778 (seconds-to-time (setq ts
(+ ts step
)))))
779 (insert "\n" (if (eq step0
'day
) "Daily report: " "Weekly report starting on: ")
780 (plist-get p1
:tstart
) "\n")
781 (org-dblock-write:clocktable p1
)
782 (re-search-forward "#\\+END:")
786 (defun org-clocktable-add-file (file table
)
788 (let ((lines (org-split-string table
"\n"))
789 (ff (file-name-nondirectory file
)))
792 (if (string-match org-table-dataline-regexp x
)
800 ;;; org-clock.el ends here