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 of the License, or
15 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; This file contains the time clocking code for Org-mode
35 (declare-function calendar-absolute-from-iso
"cal-iso" (&optional date
))
37 (defgroup org-clock nil
38 "Options concerning clocking working time in Org-mode."
42 (defcustom org-clock-into-drawer
2
43 "Should clocking info be wrapped into a drawer?
44 When t, clocking info will always be inserted into a :CLOCK: drawer.
45 If necessary, the drawer will be created.
46 When nil, the drawer will not be created, but used when present.
47 When an integer and the number of clocking entries in an item
48 reaches or exceeds this number, a drawer will be created."
52 (const :tag
"Always" t
)
53 (const :tag
"Only when drawer exists" nil
)
54 (integer :tag
"When at least N clock entries")))
56 (defcustom org-clock-out-when-done t
57 "When t, the clock will be stopped when the relevant entry is marked DONE.
58 Nil means, clock will keep running until stopped explicitly with
59 `C-c C-x C-o', or until the clock is started in a different item."
63 (defcustom org-clock-out-remove-zero-time-clocks nil
64 "Non-nil means, remove the clock line when the resulting time is zero."
68 (defcustom org-clock-in-switch-to-state nil
69 "Set task to a special todo state while clocking it.
70 The value should be the state to which the entry should be switched."
74 (const :tag
"Don't force a state" nil
)
75 (string :tag
"State")))
77 (defcustom org-clock-history-length
5
78 "Number of clock tasks to remember in history."
82 (defcustom org-clock-heading-function nil
83 "When non-nil, should be a function to create `org-clock-heading'.
84 This is the string shown in the mode line when a clock is running.
85 The function is called with point at the beginning of the headline."
90 ;;; The clock for measuring work time.
92 (defvar org-mode-line-string
"")
93 (put 'org-mode-line-string
'risky-local-variable t
)
95 (defvar org-mode-line-timer nil
)
96 (defvar org-clock-heading
"")
97 (defvar org-clock-start-time
"")
99 (defvar org-clock-history nil
100 "List of marker pointing to recent clocked tasks.")
102 (defvar org-clock-default-task
(make-marker)
103 "Marker pointing to the default task that should clock time.
104 The clock can be made to switch to this task after clocking out
105 of a different task.")
107 (defvar org-clock-interrupted-task
(make-marker)
108 "Marker pointing to the task that has been interrupted by the current clock.")
110 (defun org-clock-history-push (&optional pos buffer
)
111 "Push a marker to the clock history."
112 (let ((m (move-marker (make-marker) (or pos
(point)) buffer
)) n l
)
113 (while (setq n
(member m org-clock-history
))
114 (move-marker (car n
) nil
))
115 (setq org-clock-history
117 (mapcar (lambda (x) (if (marker-buffer x
) x nil
))
119 (when (>= (setq l
(length org-clock-history
)) org-clock-history-length
)
120 (setq org-clock-history
122 (nthcdr (- l org-clock-history-length -
1)
123 (nreverse org-clock-history
)))))
124 (push m org-clock-history
)))
126 (defun org-clock-save-markers-for-cut-and-paste (beg end
)
127 "Save relative positions of markers in region."
128 (org-check-and-save-marker org-clock-marker beg end
)
129 (org-check-and-save-marker org-clock-default-task beg end
)
130 (org-check-and-save-marker org-clock-interrupted-task beg end
)
131 (mapc (lambda (m) (org-check-and-save-marker m beg end
))
134 (defun org-clock-select-task (&optional prompt
)
135 "Select a task that recently was associated with clocking."
137 (let (sel-list rpl file task
(i 0) s
)
138 (save-window-excursion
139 (org-switch-to-buffer-other-window
140 (get-buffer-create "*Clock Task Select*"))
142 (when (marker-buffer org-clock-default-task
)
143 (insert (org-add-props "Default Task\n" nil
'face
'bold
))
144 (setq s
(org-clock-insert-selection-line ?d org-clock-default-task
))
146 (when (marker-buffer org-clock-interrupted-task
)
147 (insert (org-add-props "The task interrupted by starting the last one\n" nil
'face
'bold
))
148 (setq s
(org-clock-insert-selection-line ?i org-clock-interrupted-task
))
150 (when (marker-buffer org-clock-marker
)
151 (insert (org-add-props "Current Clocking Task\n" nil
'face
'bold
))
152 (setq s
(org-clock-insert-selection-line ?c org-clock-marker
))
154 (insert (org-add-props "Recent Tasks\n" nil
'face
'bold
))
157 (when (marker-buffer m
)
159 s
(org-clock-insert-selection-line
160 (string-to-char (number-to-string i
)) m
))
163 (shrink-window-if-larger-than-buffer)
164 (message (or prompt
"Select task for clocking:"))
165 (setq rpl
(read-char-exclusive))
169 ((assoc rpl sel-list
) (cdr (assoc rpl sel-list
)))
170 (t (error "Invalid task choice %c" rpl
))))))
172 (defun org-clock-insert-selection-line (i marker
)
173 (when (marker-buffer marker
)
175 (with-current-buffer (org-base-buffer (marker-buffer marker
))
180 (setq file
(buffer-file-name (marker-buffer marker
))
181 cat
(or (org-get-category)
182 (progn (org-refresh-category-properties)
184 task
(org-get-heading 'notags
)))))
186 (insert (format "[%c] %-15s %s\n" i cat task
))
189 (defun org-update-mode-line ()
190 (let* ((delta (- (time-to-seconds (current-time))
191 (time-to-seconds org-clock-start-time
)))
192 (h (floor delta
3600))
193 (m (floor (- delta
(* 3600 h
)) 60)))
194 (setq org-mode-line-string
195 (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading
)
196 'help-echo
"Org-mode clock is running"))
197 (force-mode-line-update)))
199 (defvar org-clock-mode-line-entry nil
200 "Information for the modeline about the running clock.")
202 (defun org-clock-in (&optional select
)
203 "Start the clock on the current item.
204 If necessary, clock-out of the currently active clock.
205 With prefix arg SELECT, offer a list of recently clocked ta sks to
206 clock into. When SELECT is `C-u C-u', clock into the current task and mark
207 is as the default task, a special task that will always be offered in
208 the clocking selection, associated with the letter `d'."
210 (let ((interrupting (marker-buffer org-clock-marker
))
211 ts selected-task target-pos
)
212 (when (equal select
'(4))
213 (setq selected-task
(org-clock-select-task "Clock-in on task: "))
215 (setq selected-task
(copy-marker selected-task
))
218 ;; We are interrupting the clocking of a differnt task.
219 ;; Save a marker to this task, so that we can go back.
220 (move-marker org-clock-interrupted-task
221 (marker-position org-clock-marker
)
222 (marker-buffer org-clock-marker
))
225 (when (equal select
'(16))
226 ;; Mark as default clocking task
228 (org-back-to-heading t
)
229 (move-marker org-clock-default-task
(point))))
231 (setq target-pos
(point)) ;; we want to clock in at this location
233 (when (and selected-task
(marker-buffer selected-task
))
234 ;; There is a selected task, move to the correct buffer
235 ;; and set the new target position.
236 (set-buffer (org-base-buffer (marker-buffer selected-task
)))
237 (setq target-pos
(marker-position selected-task
))
238 (move-marker selected-task nil
))
242 (goto-char target-pos
)
243 (org-back-to-heading t
)
244 (or interrupting
(move-marker org-clock-interrupted-task nil
))
245 (org-clock-history-push)
246 (when (and org-clock-in-switch-to-state
247 (not (looking-at (concat outline-regexp
"[ \t]*"
248 org-clock-in-switch-to-state
250 (org-todo org-clock-in-switch-to-state
))
251 (if (and org-clock-heading-function
252 (functionp org-clock-heading-function
))
253 (setq org-clock-heading
(funcall org-clock-heading-function
))
254 (if (looking-at org-complex-heading-regexp
)
255 (setq org-clock-heading
(match-string 4))
256 (setq org-clock-heading
"???")))
257 (setq org-clock-heading
(propertize org-clock-heading
'face nil
))
258 (org-clock-find-position)
260 (insert "\n") (backward-char 1)
262 (insert org-clock-string
" ")
263 (setq org-clock-start-time
(current-time))
264 (setq ts
(org-insert-time-stamp (current-time) 'with-hm
'inactive
))
265 (move-marker org-clock-marker
(point) (buffer-base-buffer))
266 (or global-mode-string
(setq global-mode-string
'("")))
267 (or (memq 'org-mode-line-string global-mode-string
)
268 (setq global-mode-string
269 (append global-mode-string
'(org-mode-line-string))))
270 (org-update-mode-line)
271 (setq org-mode-line-timer
272 (run-with-timer 60 60 'org-update-mode-line
))
273 (message "Clock started at %s" ts
))))))
275 (defun org-clock-find-position ()
276 "Find the location where the next clock line should be inserted."
277 (org-back-to-heading t
)
279 (let ((beg (save-excursion
280 (beginning-of-line 2)
281 (or (bolp) (newline))
283 (end (progn (outline-next-heading) (point)))
284 (re (concat "^[ \t]*" org-clock-string
))
288 (when (eobp) (newline) (setq end
(max (point) end
)))
289 (when (re-search-forward "^[ \t]*:CLOCK:" end t
)
290 ;; we seem to have a CLOCK drawer, so go there.
291 (beginning-of-line 2)
293 ;; Lets count the CLOCK lines
295 (while (re-search-forward re end t
)
296 (setq first
(or first
(match-beginning 0))
297 last
(match-beginning 0)
299 (when (and (integerp org-clock-into-drawer
)
300 (>= (1+ cnt
) org-clock-into-drawer
))
301 ;; Wrap current entries into a new drawer
303 (beginning-of-line 2)
305 (beginning-of-line 0)
306 (org-indent-line-function)
309 (beginning-of-line 0)
310 (org-indent-line-function)
312 (beginning-of-line 2)
316 (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
317 (not (equal (match-string 1) org-clock-string
)))
318 ;; Planning info, skip to after it
319 (beginning-of-line 2)
320 (or (bolp) (newline)))
321 (when (eq t org-clock-into-drawer
)
322 (insert ":CLOCK:\n:END:\n")
323 (beginning-of-line -
1)
324 (org-indent-line-function)
326 (beginning-of-line 2)
327 (org-indent-line-function)))))
329 (defun org-clock-out (&optional fail-quietly
)
330 "Stop the currently running clock.
331 If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
334 (if (not (marker-buffer org-clock-marker
))
335 (if fail-quietly
(throw 'exit t
) (error "No active clock")))
336 (let (ts te s h m remove
)
338 (set-buffer (marker-buffer org-clock-marker
))
341 (goto-char org-clock-marker
)
342 (beginning-of-line 1)
343 (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
344 (equal (match-string 1) org-clock-string
))
345 (setq ts
(match-string 2))
346 (if fail-quietly
(throw 'exit nil
) (error "Clock start time is gone")))
347 (goto-char (match-end 0))
348 (delete-region (point) (point-at-eol))
350 (setq te
(org-insert-time-stamp (current-time) 'with-hm
'inactive
))
351 (setq s
(- (time-to-seconds (apply 'encode-time
(org-parse-time-string te
)))
352 (time-to-seconds (apply 'encode-time
(org-parse-time-string ts
))))
357 (insert " => " (format "%2d:%02d" h m
))
358 (when (setq remove
(and org-clock-out-remove-zero-time-clocks
360 (beginning-of-line 1)
361 (delete-region (point) (point-at-eol))
362 (and (looking-at "\n") (> (point-max) (1+ (point)))
364 (move-marker org-clock-marker nil
)
365 (when org-log-note-clock-out
366 (org-add-log-setup 'clock-out
))
367 (when org-mode-line-timer
368 (cancel-timer org-mode-line-timer
)
369 (setq org-mode-line-timer nil
))
370 (setq global-mode-string
371 (delq 'org-mode-line-string global-mode-string
))
372 (force-mode-line-update)
373 (message "Clock stopped at %s after HH:MM = %d:%02d%s" te h m
374 (if remove
" => LINE REMOVED" "")))))))
376 (defun org-clock-cancel ()
377 "Cancel the running clock be removing the start timestamp."
379 (if (not (marker-buffer org-clock-marker
))
380 (error "No active clock"))
382 (set-buffer (marker-buffer org-clock-marker
))
383 (goto-char org-clock-marker
)
384 (delete-region (1- (point-at-bol)) (point-at-eol)))
385 (setq global-mode-string
386 (delq 'org-mode-line-string global-mode-string
))
387 (force-mode-line-update)
388 (message "Clock canceled"))
390 (defun org-clock-goto (&optional select
)
391 "Go to the currently clocked-in entry.
392 With prefix arg SELECT, offer recently clocked tasks."
395 (org-clock-select-task "Select task to go to: ")
397 (if (not (marker-buffer m
))
399 (error "No task selected")
400 (error "No active clock")))
401 (switch-to-buffer (marker-buffer m
))
402 (if (or (< m
(point-min)) (> m
(point-max))) (widen))
405 (org-back-to-heading)
406 (org-cycle-hide-drawers 'children
)
409 (defvar org-clock-file-total-minutes nil
410 "Holds the file total time in minutes, after a call to `org-clock-sum'.")
411 (make-variable-buffer-local 'org-clock-file-total-minutes
)
413 (defun org-clock-sum (&optional tstart tend
)
414 "Sum the times for each subtree.
415 Puts the resulting times in minutes as a text property on each headline."
417 (let* ((bmp (buffer-modified-p))
418 (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
420 "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
422 (ltimes (make-vector lmax
0))
427 (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t
))
429 (goto-char (point-max))
430 (while (re-search-backward re nil t
)
434 (setq ts
(match-string 2)
437 (apply 'encode-time
(org-parse-time-string ts
)))
439 (apply 'encode-time
(org-parse-time-string te
)))
440 ts
(if tstart
(max ts tstart
) ts
)
441 te
(if tend
(min te tend
) te
)
443 t1
(if (> dt
0) (+ t1
(floor (/ dt
60))) t1
)))
446 (setq t1
(+ t1
(string-to-number (match-string 5))
447 (* 60 (string-to-number (match-string 4))))))
449 (setq level
(- (match-end 1) (match-beginning 1)))
450 (when (or (> t1
0) (> (aref ltimes level
) 0))
451 (loop for l from
0 to level do
452 (aset ltimes l
(+ (aref ltimes l
) t1
)))
453 (setq t1
0 time
(aref ltimes level
))
454 (loop for l from level to
(1- lmax
) do
456 (goto-char (match-beginning 0))
457 (put-text-property (point) (point-at-eol) :org-clock-minutes time
)))))
458 (setq org-clock-file-total-minutes
(aref ltimes
0)))
459 (set-buffer-modified-p bmp
)))
461 (defun org-clock-display (&optional total-only
)
462 "Show subtree times in the entire buffer.
463 If TOTAL-ONLY is non-nil, only show the total time for the entire file
466 (org-remove-clock-overlays)
471 (goto-char (point-min))
472 (while (or (and (equal (setq p
(point)) (point-min))
473 (get-text-property p
:org-clock-minutes
))
474 (setq p
(next-single-property-change
475 (point) :org-clock-minutes
)))
477 (when (setq time
(get-text-property p
:org-clock-minutes
))
478 (org-put-clock-overlay time
(funcall outline-level
))))
479 (setq h
(/ org-clock-file-total-minutes
60)
480 m
(- org-clock-file-total-minutes
(* 60 h
)))
481 ;; Arrange to remove the overlays upon next change.
482 (when org-remove-highlights-with-change
483 (org-add-hook 'before-change-functions
'org-remove-clock-overlays
485 (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m
)))
487 (defvar org-clock-overlays nil
)
488 (make-variable-buffer-local 'org-clock-overlays
)
490 (defun org-put-clock-overlay (time &optional level
)
491 "Put an overlays on the current line, displaying TIME.
492 If LEVEL is given, prefix time with a corresponding number of stars.
493 This creates a new overlay and stores it in `org-clock-overlays', so that it
494 will be easy to remove."
495 (let* ((c 60) (h (floor (/ time
60))) (m (- time
(* 60 h
)))
496 (l (if level
(org-get-valid-level level
0) 0))
499 (org-move-to-column c
)
500 (unless (eolp) (skip-chars-backward "^ \t"))
501 (skip-chars-backward " \t")
502 (setq ov
(org-make-overlay (1- (point)) (point-at-eol))
503 tx
(concat (buffer-substring (1- (point)) (point))
504 (make-string (+ off
(max 0 (- c
(current-column)))) ?.
)
505 (org-add-props (format "%s %2d:%02d%s"
506 (make-string l ?
*) h m
507 (make-string (- 16 l
) ?\
))
508 '(face secondary-selection
))
510 (if (not (featurep 'xemacs
))
511 (org-overlay-put ov
'display tx
)
512 (org-overlay-put ov
'invisible t
)
513 (org-overlay-put ov
'end-glyph
(make-glyph tx
)))
514 (push ov org-clock-overlays
)))
516 (defun org-remove-clock-overlays (&optional beg end noremove
)
517 "Remove the occur highlights from the buffer.
518 BEG and END are ignored. If NOREMOVE is nil, remove this function
519 from the `before-change-functions' in the current buffer."
521 (unless org-inhibit-highlight-removal
522 (mapc 'org-delete-overlay org-clock-overlays
)
523 (setq org-clock-overlays nil
)
525 (remove-hook 'before-change-functions
526 'org-remove-clock-overlays
'local
))))
528 (defvar state
) ;; dynamically scoped into this function
529 (defun org-clock-out-if-current ()
530 "Clock out if the current entry contains the running clock.
531 This is used to stop the clock after a TODO entry is marked DONE,
532 and is only done if the variable `org-clock-out-when-done' is not nil."
533 (when (and org-clock-out-when-done
534 (member state org-done-keywords
)
535 (equal (marker-buffer org-clock-marker
) (current-buffer))
536 (< (point) org-clock-marker
)
537 (> (save-excursion (outline-next-heading) (point))
539 ;; Clock out, but don't accept a logging message for this.
540 (let ((org-log-note-clock-out nil
))
543 (add-hook 'org-after-todo-state-change-hook
544 'org-clock-out-if-current
)
547 (defun org-get-clocktable (&rest props
)
548 "Get a formatted clocktable with parameters according to PROPS.
549 The table is created in a temporary buffer, fully formatted and
550 fontified, and then returned."
552 (setq props
(plist-put props
:name
"clocktable"))
553 (unless (plist-member props
:maxlevel
)
554 (setq props
(plist-put props
:maxlevel
2)))
555 (unless (plist-member props
:scope
)
556 (setq props
(plist-put props
:scope
'agenda
)))
559 (org-create-dblock props
)
561 (font-lock-fontify-buffer)
563 (buffer-substring (point) (progn
564 (re-search-forward "^#\\+END" nil t
)
567 (defun org-clock-report (&optional arg
)
568 "Create a table containing a report about clocked time.
569 If the cursor is inside an existing clocktable block, then the table
570 will be updated. If not, a new clocktable will be inserted.
571 When called with a prefix argument, move to the first clock table in the
572 buffer and update it."
574 (org-remove-clock-overlays)
576 (org-find-dblock "clocktable")
578 (if (org-in-clocktable-p)
579 (goto-char (org-in-clocktable-p))
580 (org-create-dblock (list :name
"clocktable"
581 :maxlevel
2 :scope
'file
)))
584 (defun org-in-clocktable-p ()
585 "Check if the cursor is in a clocktable."
586 (let ((pos (point)) start
)
589 (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t
)
590 (setq start
(match-beginning 0))
591 (re-search-forward "^#\\+END:.*" nil t
)
592 (>= (match-end 0) pos
)
595 (defun org-clock-special-range (key &optional time as-strings
)
596 "Return two times bordering a special time range.
597 Key is a symbol specifying the range and can be one of `today', `yesterday',
598 `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
599 A week starts Monday 0:00 and ends Sunday 24:00.
600 The range is determined relative to TIME. TIME defaults to the current time.
601 The return value is a cons cell with two internal times like the ones
602 returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
603 the returned times will be formatted strings."
604 (if (integerp key
) (setq key
(intern (number-to-string key
))))
605 (let* ((tm (decode-time (or time
(current-time))))
606 (s 0) (m (nth 1 tm
)) (h (nth 2 tm
))
607 (d (nth 3 tm
)) (month (nth 4 tm
)) (y (nth 5 tm
))
609 (skey (symbol-name key
))
611 s1 m1 h1 d1 month1 y1 diff ts te fm txt w date
)
613 ((string-match "^[0-9]+$" skey
)
614 (setq y
(string-to-number skey
) m
1 d
1 key
'year
))
615 ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)$" skey
)
616 (setq y
(string-to-number (match-string 1 skey
))
617 month
(string-to-number (match-string 2 skey
))
619 ((string-match "^\\([0-9]+\\)-[wW]\\([0-9]\\{1,2\\}\\)$" skey
)
621 (setq y
(string-to-number (match-string 1 skey
))
622 w
(string-to-number (match-string 2 skey
)))
623 (setq date
(calendar-gregorian-from-absolute
624 (calendar-absolute-from-iso (list w
1 y
))))
625 (setq d
(nth 1 date
) month
(car date
) y
(nth 2 date
)
627 ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)-\\([0-9]\\{1,2\\}\\)$" skey
)
628 (setq y
(string-to-number (match-string 1 skey
))
629 month
(string-to-number (match-string 2 skey
))
630 d
(string-to-number (match-string 3 skey
))
632 ((string-match "\\([-+][0-9]+\\)$" skey
)
633 (setq shift
(string-to-number (match-string 1 skey
))
634 key
(intern (substring skey
0 (match-beginning 1))))))
636 (cond ((eq key
'yesterday
) (setq key
'today shift -
1))
637 ((eq key
'lastweek
) (setq key
'week shift -
1))
638 ((eq key
'lastmonth
) (setq key
'month shift -
1))
639 ((eq key
'lastyear
) (setq key
'year shift -
1))))
641 ((memq key
'(day today
))
642 (setq d
(+ d shift
) h
0 m
0 h1
24 m1
0))
643 ((memq key
'(week thisweek
))
644 (setq diff
(+ (* -
7 shift
) (if (= dow
0) 6 (1- dow
)))
645 m
0 h
0 d
(- d diff
) d1
(+ 7 d
)))
646 ((memq key
'(month thismonth
))
647 (setq d
1 h
0 m
0 d1
1 month
(+ month shift
) month1
(1+ month
) h1
0 m1
0))
648 ((memq key
'(year thisyear
))
649 (setq m
0 h
0 d
1 month
1 y
(+ y shift
) y1
(1+ y
)))
650 (t (error "No such time block %s" key
)))
651 (setq ts
(encode-time s m h d month y
)
652 te
(encode-time (or s1 s
) (or m1 m
) (or h1 h
)
653 (or d1 d
) (or month1 month
) (or y1 y
)))
654 (setq fm
(cdr org-time-stamp-formats
))
656 ((memq key
'(day today
))
657 (setq txt
(format-time-string "%A, %B %d, %Y" ts
)))
658 ((memq key
'(week thisweek
))
659 (setq txt
(format-time-string "week %G-W%V" ts
)))
660 ((memq key
'(month thismonth
))
661 (setq txt
(format-time-string "%B %Y" ts
)))
662 ((memq key
'(year thisyear
))
663 (setq txt
(format-time-string "the year %Y" ts
))))
665 (list (format-time-string fm ts
) (format-time-string fm te
) txt
)
668 (defun org-clocktable-shift (dir n
)
669 "Try to shift the :block date of the clocktable at point.
670 Point must be in the #+BEGIN: line of a clocktable, or this function
672 DIR is a direction, a symbol `left', `right', `up', or `down'.
673 Both `left' and `down' shift the block toward the past, `up' and `right'
674 push it toward the future.
675 N is the number of shift steps to take. The size of the step depends on
676 the currently selected interval size."
677 (setq n
(prefix-numeric-value n
))
678 (and (memq dir
'(left down
)) (setq n
(- n
)))
680 (goto-char (point-at-bol))
681 (if (not (looking-at "#\\+BEGIN: clocktable\\>.*?:block[ \t]+\\(\\S-+\\)"))
682 (error "Line needs a :block definition before this command works")
683 (let* ((b (match-beginning 1)) (e (match-end 1))
685 block shift ins y mw d date wp m
)
687 ((string-match "^\\(today\\|thisweek\\|thismonth\\|thisyear\\)\\([-+][0-9]+\\)?$" s
)
688 (setq block
(match-string 1 s
)
689 shift
(if (match-end 2)
690 (string-to-number (match-string 2 s
))
692 (setq shift
(+ shift n
))
693 (setq ins
(if (= shift
0) block
(format "%s%+d" block shift
))))
694 ((string-match "\\([0-9]+\\)\\(-\\([wW]?\\)\\([0-9]\\{1,2\\}\\)\\(-\\([0-9]\\{1,2\\}\\)\\)?\\)?" s
)
695 ;; 1 1 2 3 3 4 4 5 6 6 5 2
696 (setq y
(string-to-number (match-string 1 s
))
697 wp
(and (match-end 3) (match-string 3 s
))
698 mw
(and (match-end 4) (string-to-number (match-string 4 s
)))
699 d
(and (match-end 6) (string-to-number (match-string 6 s
))))
701 (d (setq ins
(format-time-string
703 (encode-time 0 0 0 (+ d n
) m y
))))
704 ((and wp mw
(> (length wp
) 0))
706 (setq date
(calendar-gregorian-from-absolute (calendar-absolute-from-iso (list (+ mw n
) 1 y
))))
707 (setq ins
(format-time-string
709 (encode-time 0 0 0 (nth 1 date
) (car date
) (nth 2 date
)))))
711 (setq ins
(format-time-string
713 (encode-time 0 0 0 1 (+ mw n
) y
))))
715 (setq ins
(number-to-string (+ y n
))))))
716 (t (error "Cannot shift clocktable block")))
720 (delete-region (point) (+ (point) (- e b
)))
721 (beginning-of-line 1)
725 (defun org-dblock-write:clocktable
(params)
726 "Write the standard clocktable."
728 (let* ((hlchars '((1 .
"*") (2 .
"/")))
731 (scope (plist-get params
:scope
))
732 (tostring (plist-get params
:tostring
))
733 (multifile (plist-get params
:multifile
))
734 (header (plist-get params
:header
))
735 (maxlevel (or (plist-get params
:maxlevel
) 3))
736 (step (plist-get params
:step
))
737 (emph (plist-get params
:emphasize
))
738 (ts (plist-get params
:tstart
))
739 (te (plist-get params
:tend
))
740 (block (plist-get params
:block
))
741 (link (plist-get params
:link
))
742 ipos time p level hlc hdl
743 cc beg end pos tbl tbl1 range-text rm-file-column scope-is-list
)
744 (setq org-clock-file-total-minutes nil
)
746 (unless (or block
(and ts te
))
747 (error "Clocktable `:step' can only be used with `:block' or `:tstart,:end'"))
748 (org-clocktable-steps params
)
751 (setq cc
(org-clock-special-range block nil t
)
752 ts
(car cc
) te
(nth 1 cc
) range-text
(nth 2 cc
)))
753 (when (integerp ts
) (setq ts
(calendar-gregorian-from-absolute ts
)))
754 (when (integerp te
) (setq te
(calendar-gregorian-from-absolute te
)))
755 (when (and ts
(listp ts
))
756 (setq ts
(format "%4d-%02d-%02d" (nth 2 ts
) (car ts
) (nth 1 ts
))))
757 (when (and te
(listp te
))
758 (setq te
(format "%4d-%02d-%02d" (nth 2 te
) (car te
) (nth 1 te
))))
759 ;; Now the times are strings we can parse.
760 (if ts
(setq ts
(time-to-seconds
761 (apply 'encode-time
(org-parse-time-string ts
)))))
762 (if te
(setq te
(time-to-seconds
763 (apply 'encode-time
(org-parse-time-string te
)))))
764 (move-marker ins
(point))
767 ;; Get the right scope
770 ((and scope
(listp scope
) (symbolp (car scope
)))
771 (setq scope
(eval scope
)))
773 (setq scope
(org-agenda-files t
)))
774 ((eq scope
'agenda-with-archives
)
775 (setq scope
(org-agenda-files t
))
776 (setq scope
(org-add-archive-files scope
)))
777 ((eq scope
'file-with-archives
)
778 (setq scope
(org-add-archive-files (list (buffer-file-name)))
780 (setq scope-is-list
(and scope
(listp scope
)))
784 ((eq scope
'file
) (widen))
785 ((eq scope
'subtree
) (org-narrow-to-subtree))
787 (while (org-up-heading-safe))
788 (org-narrow-to-subtree))
789 ((and (symbolp scope
) (string-match "^tree\\([0-9]+\\)$"
790 (symbol-name scope
)))
791 (setq level
(string-to-number (match-string 1 (symbol-name scope
))))
793 (while (org-up-heading-safe)
794 (looking-at outline-regexp
)
795 (if (<= (org-reduced-level (funcall outline-level
)) level
)
797 (org-narrow-to-subtree))
801 (p1 (copy-sequence params
))
803 (setq p1
(plist-put p1
:tostring t
))
804 (setq p1
(plist-put p1
:multifile t
))
805 (setq p1
(plist-put p1
:scope
'file
))
806 (org-prepare-agenda-buffers files
)
807 (while (setq file
(pop files
))
808 (with-current-buffer (find-buffer-visiting file
)
809 (setq tbl1
(org-dblock-write:clocktable p1
))
811 (push (org-clocktable-add-file
813 (concat "| |*File time*|*"
814 (org-minutes-to-hh:mm-string
815 org-clock-file-total-minutes
)
818 (setq total-time
(+ (or total-time
0)
819 org-clock-file-total-minutes
))))))))
822 (unless scope-is-list
823 (org-clock-sum ts te
)
824 (goto-char (point-min))
825 (while (setq p
(next-single-property-change (point) :org-clock-minutes
))
827 (when (setq time
(get-text-property p
:org-clock-minutes
))
829 (beginning-of-line 1)
830 (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
831 (setq level
(org-reduced-level
832 (- (match-end 1) (match-beginning 1))))
834 (setq hlc
(if emph
(or (cdr (assoc level hlchars
)) "") "")
837 (org-make-link-string
838 (format "file:%s::%s"
841 (org-make-org-heading-search-string
844 (if (and (not multifile
) (= level
1)) (push "|-" tbl
))
846 "| " (int-to-string level
) "|" hlc hdl hlc
" |"
847 (make-string (1- level
) ?|
)
848 hlc
(org-minutes-to-hh:mm-string time
) hlc
850 (setq tbl
(nreverse tbl
))
852 (if tbl
(mapconcat 'identity tbl
"\n") nil
)
854 (insert-before-markers
859 (format-time-string (cdr org-time-stamp-formats
))
862 (if block
(concat ", for " range-text
".") "")
864 (if scope-is-list
"|File" "")
865 "|L|Headline|Time|\n")
866 (setq total-time
(or total-time org-clock-file-total-minutes
))
867 (insert-before-markers
869 (if scope-is-list
"|" "")
872 (org-minutes-to-hh:mm-string
(or total-time
0))
874 (setq tbl
(delq nil tbl
))
875 (if (and (stringp (car tbl
)) (> (length (car tbl
)) 1)
876 (equal (substring (car tbl
) 0 2) "|-"))
878 (insert-before-markers (mapconcat
879 'identity
(delq nil tbl
)
880 (if scope-is-list
"\n|-\n" "\n")))
881 (backward-delete-char 1)
883 (skip-chars-forward "^|")
887 (org-table-delete-column)))))))
889 (defun org-clocktable-steps (params)
890 (let* ((p1 (copy-sequence params
))
891 (ts (plist-get p1
:tstart
))
892 (te (plist-get p1
:tend
))
893 (step0 (plist-get p1
:step
))
894 (step (cdr (assoc step0
'((day .
86400) (week .
604800)))))
895 (block (plist-get p1
:block
))
898 (setq cc
(org-clock-special-range block nil t
)
899 ts
(car cc
) te
(nth 1 cc
) range-text
(nth 2 cc
)))
900 (if ts
(setq ts
(time-to-seconds
901 (apply 'encode-time
(org-parse-time-string ts
)))))
902 (if te
(setq te
(time-to-seconds
903 (apply 'encode-time
(org-parse-time-string te
)))))
904 (setq p1
(plist-put p1
:header
""))
905 (setq p1
(plist-put p1
:step nil
))
906 (setq p1
(plist-put p1
:block nil
))
908 (or (bolp) (insert "\n"))
909 (setq p1
(plist-put p1
:tstart
(format-time-string
910 (car org-time-stamp-formats
)
911 (seconds-to-time ts
))))
912 (setq p1
(plist-put p1
:tend
(format-time-string
913 (car org-time-stamp-formats
)
914 (seconds-to-time (setq ts
(+ ts step
))))))
915 (insert "\n" (if (eq step0
'day
) "Daily report: " "Weekly report starting on: ")
916 (plist-get p1
:tstart
) "\n")
917 (org-dblock-write:clocktable p1
)
918 (re-search-forward "#\\+END:")
922 (defun org-clocktable-add-file (file table
)
924 (let ((lines (org-split-string table
"\n"))
925 (ff (file-name-nondirectory file
)))
928 (if (string-match org-table-dataline-regexp x
)
936 ;; arch-tag: 7b42c5d4-9b36-48be-97c0-66a869daed4c
938 ;;; org-clock.el ends here