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 "Marker pointing to the previous task teking clock time.
101 This is used to find back to the previous task after interrupting work.
102 When clocking into a task and the clock is currently running, this marker
103 is moved to the position of the currently running task and continues
104 to point there even after the task is clocked out.")
106 (defvar org-clock-default-task
(make-marker)
107 "Marker pointing to the default task that should clock time.
108 The clock can be made to switch to this task after clocking out
109 of a different task.")
111 (defvar org-clock-interrupted-task
(make-marker)
112 "Marker pointing to the default task that should clock time.
113 The clock can be made to switch to this task after clocking out
114 of a different task.")
116 (defun org-clock-history-push (&optional pos buffer
)
117 "Push a marker to the clock history."
118 (let ((m (move-marker (make-marker) (or pos
(point)) buffer
)) n l
)
119 (while (setq n
(member m org-clock-history
))
120 (move-marker (car n
) nil
))
121 (setq org-clock-history
123 (mapcar (lambda (x) (if (marker-buffer x
) x nil
))
125 (when (>= (setq l
(length org-clock-history
)) org-clock-history-length
)
126 (setq org-clock-history
128 (nthcdr (- l org-clock-history-length -
1)
129 (nreverse org-clock-history
)))))
130 (push m org-clock-history
)))
132 (defun org-clock-select-task (&optional prompt
)
133 "Select a task that recently was associated with clocking."
135 (let (sel-list rpl file task
(i 0) s
)
136 (save-window-excursion
137 (org-switch-to-buffer-other-window
138 (get-buffer-create "*Clock Task Select*"))
140 (when (marker-buffer org-clock-default-task
)
141 (insert (org-add-props "Default Task\n" nil
'face
'bold
))
142 (setq s
(org-clock-insert-selection-line ?d org-clock-default-task
))
144 (when (marker-buffer org-clock-interrupted-task
)
145 (insert (org-add-props "The task interrupted by starting the last one\n" nil
'face
'bold
))
146 (setq s
(org-clock-insert-selection-line ?i org-clock-interrupted-task
))
148 (when (marker-buffer org-clock-marker
)
149 (insert (org-add-props "Current Clocking Task\n" nil
'face
'bold
))
150 (setq s
(org-clock-insert-selection-line ?c org-clock-marker
))
152 (insert (org-add-props "Recent Tasks\n" nil
'face
'bold
))
155 (when (marker-buffer m
)
157 s
(org-clock-insert-selection-line
158 (string-to-char (number-to-string i
)) m
))
161 (shrink-window-if-larger-than-buffer)
162 (message (or prompt
"Select task for clocking:"))
163 (setq rpl
(read-char-exclusive))
167 ((assoc rpl sel-list
) (cdr (assoc rpl sel-list
)))
168 (t (error "Invalid task choice %c" rpl
))))))
170 (defun org-clock-insert-selection-line (i marker
)
171 (when (marker-buffer marker
)
173 (with-current-buffer (marker-buffer marker
)
176 (setq file
(buffer-file-name (marker-buffer marker
))
177 cat
(or (org-get-category)
178 (progn (org-refresh-category-properties)
180 task
(org-get-heading 'notags
))))
182 (insert (format "[%c] %-15s %s\n" i cat task
))
185 (defun org-update-mode-line ()
186 (let* ((delta (- (time-to-seconds (current-time))
187 (time-to-seconds org-clock-start-time
)))
188 (h (floor delta
3600))
189 (m (floor (- delta
(* 3600 h
)) 60)))
190 (setq org-mode-line-string
191 (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading
)
192 'help-echo
"Org-mode clock is running"))
193 (force-mode-line-update)))
195 (defvar org-clock-mode-line-entry nil
196 "Information for the modeline about the running clock.")
198 (defun org-clock-in (&optional select
)
199 "Start the clock on the current item.
200 If necessary, clock-out of the currently active clock.
201 With prefix arg SELECT, offer a list of recently clocked ta sks to
202 clock into. When SELECT is `C-u C-u', clock into the current task and mark
203 is as the default task, a special task that will always be offered in
204 the clocking selection, associated with the letter `d'."
206 (let ((interrupting (marker-buffer org-clock-marker
))
208 (when (equal select
'(4))
209 (setq selected-task
(org-clock-select-task "Clock-in on task: "))
211 (setq selected-task
(copy-marker selected-task
))
213 ;; Are we interrupting the clocking of a differnt task?
216 (move-marker org-clock-interrupted-task
217 (marker-position org-clock-marker
)
218 (marker-buffer org-clock-marker
))
221 (when (equal select
'(16))
223 (org-back-to-heading t
)
224 (move-marker org-clock-default-task
(point))))
227 (org-back-to-heading t
)
228 (when (and selected-task
(marker-buffer selected-task
))
229 (set-buffer (marker-buffer selected-task
))
230 (goto-char selected-task
)
231 (move-marker selected-task nil
))
232 (or interrupting
(move-marker org-clock-interrupted-task nil
))
233 (org-clock-history-push)
234 (when (and org-clock-in-switch-to-state
235 (not (looking-at (concat outline-regexp
"[ \t]*"
236 org-clock-in-switch-to-state
238 (org-todo org-clock-in-switch-to-state
))
239 (if (and org-clock-heading-function
240 (functionp org-clock-heading-function
))
241 (setq org-clock-heading
(funcall org-clock-heading-function
))
242 (if (looking-at org-complex-heading-regexp
)
243 (setq org-clock-heading
(match-string 4))
244 (setq org-clock-heading
"???")))
245 (setq org-clock-heading
(propertize org-clock-heading
'face nil
))
246 (org-clock-find-position)
248 (insert "\n") (backward-char 1)
250 (insert org-clock-string
" ")
251 (setq org-clock-start-time
(current-time))
252 (setq ts
(org-insert-time-stamp (current-time) 'with-hm
'inactive
))
253 (move-marker org-clock-marker
(point) (buffer-base-buffer))
254 (or global-mode-string
(setq global-mode-string
'("")))
255 (or (memq 'org-mode-line-string global-mode-string
)
256 (setq global-mode-string
257 (append global-mode-string
'(org-mode-line-string))))
258 (org-update-mode-line)
259 (setq org-mode-line-timer
(run-with-timer 60 60 'org-update-mode-line
))
260 (message "Clock started at %s" ts
))))
262 (defun org-clock-find-position ()
263 "Find the location where the next clock line should be inserted."
264 (org-back-to-heading t
)
266 (let ((beg (save-excursion
267 (beginning-of-line 2)
268 (or (bolp) (newline))
270 (end (progn (outline-next-heading) (point)))
271 (re (concat "^[ \t]*" org-clock-string
))
275 (when (eobp) (newline) (setq end
(max (point) end
)))
276 (when (re-search-forward "^[ \t]*:CLOCK:" end t
)
277 ;; we seem to have a CLOCK drawer, so go there.
278 (beginning-of-line 2)
280 ;; Lets count the CLOCK lines
282 (while (re-search-forward re end t
)
283 (setq first
(or first
(match-beginning 0))
284 last
(match-beginning 0)
286 (when (and (integerp org-clock-into-drawer
)
287 (>= (1+ cnt
) org-clock-into-drawer
))
288 ;; Wrap current entries into a new drawer
290 (beginning-of-line 2)
291 (if (org-at-item-p) (org-end-of-item))
293 (beginning-of-line 0)
294 (org-indent-line-function)
297 (beginning-of-line 0)
298 (org-indent-line-function)
300 (beginning-of-line 2)
304 (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
305 (not (equal (match-string 1) org-clock-string
)))
306 ;; Planning info, skip to after it
307 (beginning-of-line 2)
308 (or (bolp) (newline)))
309 (when (eq t org-clock-into-drawer
)
310 (insert ":CLOCK:\n:END:\n")
311 (beginning-of-line -
1)
312 (org-indent-line-function)
314 (beginning-of-line 2)
315 (org-indent-line-function)))))
317 (defun org-clock-out (&optional fail-quietly
)
318 "Stop the currently running clock.
319 If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
322 (if (not (marker-buffer org-clock-marker
))
323 (if fail-quietly
(throw 'exit t
) (error "No active clock")))
324 (let (ts te s h m remove
)
326 (set-buffer (marker-buffer org-clock-marker
))
329 (goto-char org-clock-marker
)
330 (beginning-of-line 1)
331 (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
332 (equal (match-string 1) org-clock-string
))
333 (setq ts
(match-string 2))
334 (if fail-quietly
(throw 'exit nil
) (error "Clock start time is gone")))
335 (goto-char (match-end 0))
336 (delete-region (point) (point-at-eol))
338 (setq te
(org-insert-time-stamp (current-time) 'with-hm
'inactive
))
339 (setq s
(- (time-to-seconds (apply 'encode-time
(org-parse-time-string te
)))
340 (time-to-seconds (apply 'encode-time
(org-parse-time-string ts
))))
345 (insert " => " (format "%2d:%02d" h m
))
346 (when (setq remove
(and org-clock-out-remove-zero-time-clocks
348 (beginning-of-line 1)
349 (delete-region (point) (point-at-eol))
350 (and (looking-at "\n") (> (point-max) (1+ (point)))
352 (move-marker org-clock-marker nil
)
353 (when org-log-note-clock-out
354 (org-add-log-setup 'clock-out
))
355 (when org-mode-line-timer
356 (cancel-timer org-mode-line-timer
)
357 (setq org-mode-line-timer nil
))
358 (setq global-mode-string
359 (delq 'org-mode-line-string global-mode-string
))
360 (force-mode-line-update)
361 (message "Clock stopped at %s after HH:MM = %d:%02d%s" te h m
362 (if remove
" => LINE REMOVED" "")))))))
364 (defun org-clock-cancel ()
365 "Cancel the running clock be removing the start timestamp."
367 (if (not (marker-buffer org-clock-marker
))
368 (error "No active clock"))
370 (set-buffer (marker-buffer org-clock-marker
))
371 (goto-char org-clock-marker
)
372 (delete-region (1- (point-at-bol)) (point-at-eol)))
373 (setq global-mode-string
374 (delq 'org-mode-line-string global-mode-string
))
375 (force-mode-line-update)
376 (message "Clock canceled"))
378 (defun org-clock-goto (&optional select
)
379 "Go to the currently clocked-in entry.
380 With prefix arg SELECT, offer recently clocked tasks."
383 (org-clock-select-task "Select task to go to: ")
385 (if (not (marker-buffer m
))
387 (error "No task selected")
388 (error "No active clock")))
389 (switch-to-buffer (marker-buffer m
))
392 (org-back-to-heading)
393 (org-cycle-hide-drawers 'children
)
396 (defvar org-clock-file-total-minutes nil
397 "Holds the file total time in minutes, after a call to `org-clock-sum'.")
398 (make-variable-buffer-local 'org-clock-file-total-minutes
)
400 (defun org-clock-sum (&optional tstart tend
)
401 "Sum the times for each subtree.
402 Puts the resulting times in minutes as a text property on each headline."
404 (let* ((bmp (buffer-modified-p))
405 (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
407 "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
409 (ltimes (make-vector lmax
0))
414 (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t
))
416 (goto-char (point-max))
417 (while (re-search-backward re nil t
)
421 (setq ts
(match-string 2)
424 (apply 'encode-time
(org-parse-time-string ts
)))
426 (apply 'encode-time
(org-parse-time-string te
)))
427 ts
(if tstart
(max ts tstart
) ts
)
428 te
(if tend
(min te tend
) te
)
430 t1
(if (> dt
0) (+ t1
(floor (/ dt
60))) t1
)))
433 (setq t1
(+ t1
(string-to-number (match-string 5))
434 (* 60 (string-to-number (match-string 4))))))
436 (setq level
(- (match-end 1) (match-beginning 1)))
437 (when (or (> t1
0) (> (aref ltimes level
) 0))
438 (loop for l from
0 to level do
439 (aset ltimes l
(+ (aref ltimes l
) t1
)))
440 (setq t1
0 time
(aref ltimes level
))
441 (loop for l from level to
(1- lmax
) do
443 (goto-char (match-beginning 0))
444 (put-text-property (point) (point-at-eol) :org-clock-minutes time
)))))
445 (setq org-clock-file-total-minutes
(aref ltimes
0)))
446 (set-buffer-modified-p bmp
)))
448 (defun org-clock-display (&optional total-only
)
449 "Show subtree times in the entire buffer.
450 If TOTAL-ONLY is non-nil, only show the total time for the entire file
453 (org-remove-clock-overlays)
458 (goto-char (point-min))
459 (while (or (and (equal (setq p
(point)) (point-min))
460 (get-text-property p
:org-clock-minutes
))
461 (setq p
(next-single-property-change
462 (point) :org-clock-minutes
)))
464 (when (setq time
(get-text-property p
:org-clock-minutes
))
465 (org-put-clock-overlay time
(funcall outline-level
))))
466 (setq h
(/ org-clock-file-total-minutes
60)
467 m
(- org-clock-file-total-minutes
(* 60 h
)))
468 ;; Arrange to remove the overlays upon next change.
469 (when org-remove-highlights-with-change
470 (org-add-hook 'before-change-functions
'org-remove-clock-overlays
472 (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m
)))
474 (defvar org-clock-overlays nil
)
475 (make-variable-buffer-local 'org-clock-overlays
)
477 (defun org-put-clock-overlay (time &optional level
)
478 "Put an overlays on the current line, displaying TIME.
479 If LEVEL is given, prefix time with a corresponding number of stars.
480 This creates a new overlay and stores it in `org-clock-overlays', so that it
481 will be easy to remove."
482 (let* ((c 60) (h (floor (/ time
60))) (m (- time
(* 60 h
)))
483 (l (if level
(org-get-valid-level level
0) 0))
486 (org-move-to-column c
)
487 (unless (eolp) (skip-chars-backward "^ \t"))
488 (skip-chars-backward " \t")
489 (setq ov
(org-make-overlay (1- (point)) (point-at-eol))
490 tx
(concat (buffer-substring (1- (point)) (point))
491 (make-string (+ off
(max 0 (- c
(current-column)))) ?.
)
492 (org-add-props (format "%s %2d:%02d%s"
493 (make-string l ?
*) h m
494 (make-string (- 16 l
) ?\
))
495 '(face secondary-selection
))
497 (if (not (featurep 'xemacs
))
498 (org-overlay-put ov
'display tx
)
499 (org-overlay-put ov
'invisible t
)
500 (org-overlay-put ov
'end-glyph
(make-glyph tx
)))
501 (push ov org-clock-overlays
)))
503 (defun org-remove-clock-overlays (&optional beg end noremove
)
504 "Remove the occur highlights from the buffer.
505 BEG and END are ignored. If NOREMOVE is nil, remove this function
506 from the `before-change-functions' in the current buffer."
508 (unless org-inhibit-highlight-removal
509 (mapc 'org-delete-overlay org-clock-overlays
)
510 (setq org-clock-overlays nil
)
512 (remove-hook 'before-change-functions
513 'org-remove-clock-overlays
'local
))))
515 (defvar state
) ;; dynamically scoped into this function
516 (defun org-clock-out-if-current ()
517 "Clock out if the current entry contains the running clock.
518 This is used to stop the clock after a TODO entry is marked DONE,
519 and is only done if the variable `org-clock-out-when-done' is not nil."
520 (when (and org-clock-out-when-done
521 (member state org-done-keywords
)
522 (equal (marker-buffer org-clock-marker
) (current-buffer))
523 (< (point) org-clock-marker
)
524 (> (save-excursion (outline-next-heading) (point))
526 ;; Clock out, but don't accept a logging message for this.
527 (let ((org-log-note-clock-out nil
))
530 (add-hook 'org-after-todo-state-change-hook
531 'org-clock-out-if-current
)
534 (defun org-get-clocktable (&rest props
)
535 "Get a formatted clocktable with parameters according to PROPS.
536 The table is created in a temporary buffer, fully formatted and
537 fontified, and then returned."
539 (setq props
(plist-put props
:name
"clocktable"))
540 (unless (plist-member props
:maxlevel
)
541 (setq props
(plist-put props
:maxlevel
2)))
542 (unless (plist-member props
:scope
)
543 (setq props
(plist-put props
:scope
'agenda
)))
546 (org-create-dblock props
)
548 (font-lock-fontify-buffer)
550 (buffer-substring (point) (progn
551 (re-search-forward "^#\\+END" nil t
)
554 (defun org-clock-report (&optional arg
)
555 "Create a table containing a report about clocked time.
556 If the cursor is inside an existing clocktable block, then the table
557 will be updated. If not, a new clocktable will be inserted.
558 When called with a prefix argument, move to the first clock table in the
559 buffer and update it."
561 (org-remove-clock-overlays)
563 (org-find-dblock "clocktable")
565 (if (org-in-clocktable-p)
566 (goto-char (org-in-clocktable-p))
567 (org-create-dblock (list :name
"clocktable"
568 :maxlevel
2 :scope
'file
)))
571 (defun org-in-clocktable-p ()
572 "Check if the cursor is in a clocktable."
573 (let ((pos (point)) start
)
576 (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t
)
577 (setq start
(match-beginning 0))
578 (re-search-forward "^#\\+END:.*" nil t
)
579 (>= (match-end 0) pos
)
582 (defun org-clock-special-range (key &optional time as-strings
)
583 "Return two times bordering a special time range.
584 Key is a symbol specifying the range and can be one of `today', `yesterday',
585 `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
586 A week starts Monday 0:00 and ends Sunday 24:00.
587 The range is determined relative to TIME. TIME defaults to the current time.
588 The return value is a cons cell with two internal times like the ones
589 returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
590 the returned times will be formatted strings."
591 (if (integerp key
) (setq key
(intern (number-to-string key
))))
592 (let* ((tm (decode-time (or time
(current-time))))
593 (s 0) (m (nth 1 tm
)) (h (nth 2 tm
))
594 (d (nth 3 tm
)) (month (nth 4 tm
)) (y (nth 5 tm
))
596 (skey (symbol-name key
))
598 s1 m1 h1 d1 month1 y1 diff ts te fm txt w date
)
600 ((string-match "^[0-9]+$" skey
)
601 (setq y
(string-to-number skey
) m
1 d
1 key
'year
))
602 ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)$" skey
)
603 (setq y
(string-to-number (match-string 1 skey
))
604 month
(string-to-number (match-string 2 skey
))
606 ((string-match "^\\([0-9]+\\)-[wW]\\([0-9]\\{1,2\\}\\)$" skey
)
608 (setq y
(string-to-number (match-string 1 skey
))
609 w
(string-to-number (match-string 2 skey
)))
610 (setq date
(calendar-gregorian-from-absolute
611 (calendar-absolute-from-iso (list w
1 y
))))
612 (setq d
(nth 1 date
) month
(car date
) y
(nth 2 date
)
614 ((string-match "^\\([0-9]+\\)-\\([0-9]\\{1,2\\}\\)-\\([0-9]\\{1,2\\}\\)$" skey
)
615 (setq y
(string-to-number (match-string 1 skey
))
616 month
(string-to-number (match-string 2 skey
))
617 d
(string-to-number (match-string 3 skey
))
619 ((string-match "\\([-+][0-9]+\\)$" skey
)
620 (setq shift
(string-to-number (match-string 1 skey
))
621 key
(intern (substring skey
0 (match-beginning 1))))))
623 (cond ((eq key
'yesterday
) (setq key
'today shift -
1))
624 ((eq key
'lastweek
) (setq key
'week shift -
1))
625 ((eq key
'lastmonth
) (setq key
'month shift -
1))
626 ((eq key
'lastyear
) (setq key
'year shift -
1))))
628 ((memq key
'(day today
))
629 (setq d
(+ d shift
) h
0 m
0 h1
24 m1
0))
630 ((memq key
'(week thisweek
))
631 (setq diff
(+ (* -
7 shift
) (if (= dow
0) 6 (1- dow
)))
632 m
0 h
0 d
(- d diff
) d1
(+ 7 d
)))
633 ((memq key
'(month thismonth
))
634 (setq d
1 h
0 m
0 d1
1 month
(+ month shift
) month1
(1+ month
) h1
0 m1
0))
635 ((memq key
'(year thisyear
))
636 (setq m
0 h
0 d
1 month
1 y
(+ y shift
) y1
(1+ y
)))
637 (t (error "No such time block %s" key
)))
638 (setq ts
(encode-time s m h d month y
)
639 te
(encode-time (or s1 s
) (or m1 m
) (or h1 h
)
640 (or d1 d
) (or month1 month
) (or y1 y
)))
641 (setq fm
(cdr org-time-stamp-formats
))
643 ((memq key
'(day today
))
644 (setq txt
(format-time-string "%A, %B %d, %Y" ts
)))
645 ((memq key
'(week thisweek
))
646 (setq txt
(format-time-string "week %G-W%V" ts
)))
647 ((memq key
'(month thismonth
))
648 (setq txt
(format-time-string "%B %Y" ts
)))
649 ((memq key
'(year thisyear
))
650 (setq txt
(format-time-string "the year %Y" ts
))))
652 (list (format-time-string fm ts
) (format-time-string fm te
) txt
)
655 (defun org-clocktable-shift (dir n
)
656 "Try to shift the :block date of the clocktable at point.
657 Point must be in the #+BEGIN: line of a clocktable, or this function
659 DIR is a direction, a symbol `left', `right', `up', or `down'.
660 Both `left' and `down' shift the block toward the past, `up' and `right'
661 push it toward the future.
662 N is the number of shift steps to take. The size of the step depends on
663 the currently selected interval size."
664 (setq n
(prefix-numeric-value n
))
665 (and (memq dir
'(left down
)) (setq n
(- n
)))
667 (goto-char (point-at-bol))
668 (if (not (looking-at "#\\+BEGIN: clocktable\\>.*?:block[ \t]+\\(\\S-+\\)"))
669 (error "Line needs a :block definition before this command works")
670 (let* ((b (match-beginning 1)) (e (match-end 1))
672 block shift ins y mw d date wp m
)
674 ((string-match "^\\(today\\|thisweek\\|thismonth\\|thisyear\\)\\([-+][0-9]+\\)?$" s
)
675 (setq block
(match-string 1 s
)
676 shift
(if (match-end 2)
677 (string-to-number (match-string 2 s
))
679 (setq shift
(+ shift n
))
680 (setq ins
(if (= shift
0) block
(format "%s%+d" block shift
))))
681 ((string-match "\\([0-9]+\\)\\(-\\([wW]?\\)\\([0-9]\\{1,2\\}\\)\\(-\\([0-9]\\{1,2\\}\\)\\)?\\)?" s
)
682 ;; 1 1 2 3 3 4 4 5 6 6 5 2
683 (setq y
(string-to-number (match-string 1 s
))
684 wp
(and (match-end 3) (match-string 3 s
))
685 mw
(and (match-end 4) (string-to-number (match-string 4 s
)))
686 d
(and (match-end 6) (string-to-number (match-string 6 s
))))
688 (d (setq ins
(format-time-string
690 (encode-time 0 0 0 (+ d n
) m y
))))
691 ((and wp mw
(> (length wp
) 0))
693 (setq date
(calendar-gregorian-from-absolute (calendar-absolute-from-iso (list (+ mw n
) 1 y
))))
694 (setq ins
(format-time-string
696 (encode-time 0 0 0 (nth 1 date
) (car date
) (nth 2 date
)))))
698 (setq ins
(format-time-string
700 (encode-time 0 0 0 1 (+ mw n
) y
))))
702 (setq ins
(number-to-string (+ y n
))))))
703 (t (error "Cannot shift clocktable block")))
707 (delete-region (point) (+ (point) (- e b
)))
708 (beginning-of-line 1)
712 (defun org-dblock-write:clocktable
(params)
713 "Write the standard clocktable."
715 (let* ((hlchars '((1 .
"*") (2 .
"/")))
718 (scope (plist-get params
:scope
))
719 (tostring (plist-get params
:tostring
))
720 (multifile (plist-get params
:multifile
))
721 (header (plist-get params
:header
))
722 (maxlevel (or (plist-get params
:maxlevel
) 3))
723 (step (plist-get params
:step
))
724 (emph (plist-get params
:emphasize
))
725 (ts (plist-get params
:tstart
))
726 (te (plist-get params
:tend
))
727 (block (plist-get params
:block
))
728 (link (plist-get params
:link
))
729 ipos time p level hlc hdl
730 cc beg end pos tbl tbl1 range-text rm-file-column scope-is-list
)
731 (setq org-clock-file-total-minutes nil
)
733 (unless (or block
(and ts te
))
734 (error "Clocktable `:step' can only be used with `:block' or `:tstart,:end'"))
735 (org-clocktable-steps params
)
738 (setq cc
(org-clock-special-range block nil t
)
739 ts
(car cc
) te
(nth 1 cc
) range-text
(nth 2 cc
)))
740 (when (integerp ts
) (setq ts
(calendar-gregorian-from-absolute ts
)))
741 (when (integerp te
) (setq te
(calendar-gregorian-from-absolute te
)))
742 (when (and ts
(listp ts
))
743 (setq ts
(format "%4d-%02d-%02d" (nth 2 ts
) (car ts
) (nth 1 ts
))))
744 (when (and te
(listp te
))
745 (setq te
(format "%4d-%02d-%02d" (nth 2 te
) (car te
) (nth 1 te
))))
746 ;; Now the times are strings we can parse.
747 (if ts
(setq ts
(time-to-seconds
748 (apply 'encode-time
(org-parse-time-string ts
)))))
749 (if te
(setq te
(time-to-seconds
750 (apply 'encode-time
(org-parse-time-string te
)))))
751 (move-marker ins
(point))
754 ;; Get the right scope
757 ((and scope
(listp scope
) (symbolp (car scope
)))
758 (setq scope
(eval scope
)))
760 (setq scope
(org-agenda-files t
)))
761 ((eq scope
'agenda-with-archives
)
762 (setq scope
(org-agenda-files t
))
763 (setq scope
(org-add-archive-files scope
)))
764 ((eq scope
'file-with-archives
)
765 (setq scope
(org-add-archive-files (list (buffer-file-name)))
767 (setq scope-is-list
(and scope
(listp scope
)))
771 ((eq scope
'file
) (widen))
772 ((eq scope
'subtree
) (org-narrow-to-subtree))
774 (while (org-up-heading-safe))
775 (org-narrow-to-subtree))
776 ((and (symbolp scope
) (string-match "^tree\\([0-9]+\\)$"
777 (symbol-name scope
)))
778 (setq level
(string-to-number (match-string 1 (symbol-name scope
))))
780 (while (org-up-heading-safe)
781 (looking-at outline-regexp
)
782 (if (<= (org-reduced-level (funcall outline-level
)) level
)
784 (org-narrow-to-subtree))
788 (p1 (copy-sequence params
))
790 (setq p1
(plist-put p1
:tostring t
))
791 (setq p1
(plist-put p1
:multifile t
))
792 (setq p1
(plist-put p1
:scope
'file
))
793 (org-prepare-agenda-buffers files
)
794 (while (setq file
(pop files
))
795 (with-current-buffer (find-buffer-visiting file
)
796 (setq tbl1
(org-dblock-write:clocktable p1
))
798 (push (org-clocktable-add-file
800 (concat "| |*File time*|*"
801 (org-minutes-to-hh:mm-string
802 org-clock-file-total-minutes
)
805 (setq total-time
(+ (or total-time
0)
806 org-clock-file-total-minutes
))))))))
809 (unless scope-is-list
810 (org-clock-sum ts te
)
811 (goto-char (point-min))
812 (while (setq p
(next-single-property-change (point) :org-clock-minutes
))
814 (when (setq time
(get-text-property p
:org-clock-minutes
))
816 (beginning-of-line 1)
817 (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
818 (setq level
(org-reduced-level
819 (- (match-end 1) (match-beginning 1))))
821 (setq hlc
(if emph
(or (cdr (assoc level hlchars
)) "") "")
824 (org-make-link-string
825 (format "file:%s::%s"
828 (org-make-org-heading-search-string
831 (if (and (not multifile
) (= level
1)) (push "|-" tbl
))
833 "| " (int-to-string level
) "|" hlc hdl hlc
" |"
834 (make-string (1- level
) ?|
)
835 hlc
(org-minutes-to-hh:mm-string time
) hlc
837 (setq tbl
(nreverse tbl
))
839 (if tbl
(mapconcat 'identity tbl
"\n") nil
)
841 (insert-before-markers
846 (format-time-string (cdr org-time-stamp-formats
))
849 (if block
(concat ", for " range-text
".") "")
851 (if scope-is-list
"|File" "")
852 "|L|Headline|Time|\n")
853 (setq total-time
(or total-time org-clock-file-total-minutes
))
854 (insert-before-markers
856 (if scope-is-list
"|" "")
859 (org-minutes-to-hh:mm-string
(or total-time
0))
861 (setq tbl
(delq nil tbl
))
862 (if (and (stringp (car tbl
)) (> (length (car tbl
)) 1)
863 (equal (substring (car tbl
) 0 2) "|-"))
865 (insert-before-markers (mapconcat
866 'identity
(delq nil tbl
)
867 (if scope-is-list
"\n|-\n" "\n")))
868 (backward-delete-char 1)
870 (skip-chars-forward "^|")
874 (org-table-delete-column)))))))
876 (defun org-clocktable-steps (params)
877 (let* ((p1 (copy-sequence params
))
878 (ts (plist-get p1
:tstart
))
879 (te (plist-get p1
:tend
))
880 (step0 (plist-get p1
:step
))
881 (step (cdr (assoc step0
'((day .
86400) (week .
604800)))))
882 (block (plist-get p1
:block
))
885 (setq cc
(org-clock-special-range block nil t
)
886 ts
(car cc
) te
(nth 1 cc
) range-text
(nth 2 cc
)))
887 (if ts
(setq ts
(time-to-seconds
888 (apply 'encode-time
(org-parse-time-string ts
)))))
889 (if te
(setq te
(time-to-seconds
890 (apply 'encode-time
(org-parse-time-string te
)))))
891 (setq p1
(plist-put p1
:header
""))
892 (setq p1
(plist-put p1
:step nil
))
893 (setq p1
(plist-put p1
:block nil
))
895 (or (bolp) (insert "\n"))
896 (setq p1
(plist-put p1
:tstart
(format-time-string
897 (car org-time-stamp-formats
)
898 (seconds-to-time ts
))))
899 (setq p1
(plist-put p1
:tend
(format-time-string
900 (car org-time-stamp-formats
)
901 (seconds-to-time (setq ts
(+ ts step
))))))
902 (insert "\n" (if (eq step0
'day
) "Daily report: " "Weekly report starting on: ")
903 (plist-get p1
:tstart
) "\n")
904 (org-dblock-write:clocktable p1
)
905 (re-search-forward "#\\+END:")
909 (defun org-clocktable-add-file (file table
)
911 (let ((lines (org-split-string table
"\n"))
912 (ff (file-name-nondirectory file
)))
915 (if (string-match org-table-dataline-regexp x
)
923 ;;; org-clock.el ends here
926 ;; arch-tag: 7b42c5d4-9b36-48be-97c0-66a869daed4c