]> code.delx.au - gnu-emacs/blob - lisp/calendar/timeclock.el
* test/automated/viper-tests.el (viper-test-undo-kmacro):
[gnu-emacs] / lisp / calendar / timeclock.el
1 ;;; timeclock.el --- mode for keeping track of how much you work
2
3 ;; Copyright (C) 1999-2016 Free Software Foundation, Inc.
4
5 ;; Author: John Wiegley <johnw@gnu.org>
6 ;; Created: 25 Mar 1999
7 ;; Version: 2.6.1
8 ;; Keywords: calendar data
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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
25 ;;; Commentary:
26
27 ;; This mode is for keeping track of time intervals. You can use it
28 ;; for whatever purpose you like, but the typical scenario is to keep
29 ;; track of how much time you spend working on certain projects.
30 ;;
31 ;; Use `timeclock-in' when you start on a project, and `timeclock-out'
32 ;; when you're done. Once you've collected some data, you can use
33 ;; `timeclock-workday-remaining' to see how much time is left to be
34 ;; worked today (where `timeclock-workday' specifies the length of the
35 ;; working day), and `timeclock-when-to-leave' to calculate when you're free.
36
37 ;; You'll probably want to bind the timeclock commands to some handy
38 ;; keystrokes. At the moment, C-x t is unused:
39 ;;
40 ;; (require 'timeclock)
41 ;;
42 ;; (define-key ctl-x-map "ti" 'timeclock-in)
43 ;; (define-key ctl-x-map "to" 'timeclock-out)
44 ;; (define-key ctl-x-map "tc" 'timeclock-change)
45 ;; (define-key ctl-x-map "tr" 'timeclock-reread-log)
46 ;; (define-key ctl-x-map "tu" 'timeclock-update-mode-line)
47 ;; (define-key ctl-x-map "tw" 'timeclock-when-to-leave-string)
48
49 ;; If you want Emacs to display the amount of time "left" to your
50 ;; workday in the mode-line, you can either set the value of
51 ;; `timeclock-mode-line-display' to t using M-x customize, or you can
52 ;; add this code to your init file:
53 ;;
54 ;; (require 'timeclock)
55 ;; (timeclock-mode-line-display)
56 ;;
57 ;; To cancel this mode line display at any time, just call
58 ;; `timeclock-mode-line-display' again.
59
60 ;; You may also want Emacs to ask you before exiting, if you are
61 ;; currently working on a project. This can be done either by setting
62 ;; `timeclock-ask-before-exiting' to t using M-x customize (this is
63 ;; the default), or by adding the following to your init file:
64 ;;
65 ;; (add-hook 'kill-emacs-query-functions 'timeclock-query-out)
66
67 ;; NOTE: If you change your timelog file without using timeclock's
68 ;; functions, or if you change the value of any of timeclock's
69 ;; customizable variables, you should run the command
70 ;; `timeclock-reread-log'. This will recompute any discrepancies in
71 ;; your average working time, and will make sure that the various
72 ;; display functions return the correct value.
73
74 ;;; History:
75
76 ;;; Code:
77
78 (defgroup timeclock nil
79 "Keeping track of the time that gets spent."
80 :group 'data)
81
82 ;;; User Variables:
83
84 (defcustom timeclock-file (locate-user-emacs-file "timelog" ".timelog")
85 "The file used to store timeclock data in."
86 :version "24.4" ; added locate-user-emacs-file
87 :type 'file
88 :group 'timeclock)
89
90 (defcustom timeclock-workday (* 8 60 60)
91 "The length of a work period in seconds."
92 :type 'integer
93 :group 'timeclock)
94
95 (defcustom timeclock-relative t
96 "Whether to make reported time relative to `timeclock-workday'.
97 For example, if the length of a normal workday is eight hours, and you
98 work four hours on Monday, then the amount of time \"remaining\" on
99 Tuesday is twelve hours -- relative to an averaged work period of
100 eight hours -- or eight hours, non-relative. So relative time takes
101 into account any discrepancy of time under-worked or over-worked on
102 previous days. This only affects the timeclock mode line display."
103 :type 'boolean
104 :group 'timeclock)
105
106 (defcustom timeclock-get-project-function 'timeclock-ask-for-project
107 "The function used to determine the name of the current project.
108 When clocking in, and no project is specified, this function will be
109 called to determine what is the current project to be worked on.
110 If this variable is nil, no questions will be asked."
111 :type 'function
112 :group 'timeclock)
113
114 (defcustom timeclock-get-reason-function 'timeclock-ask-for-reason
115 "A function used to determine the reason for clocking out.
116 When clocking out, and no reason is specified, this function will be
117 called to determine what is the reason.
118 If this variable is nil, no questions will be asked."
119 :type 'function
120 :group 'timeclock)
121
122 (defcustom timeclock-get-workday-function nil
123 "A function used to determine the length of today's workday.
124 The first time that a user clocks in each day, this function will be
125 called to determine what is the length of the current workday. If
126 the return value is nil, or equal to `timeclock-workday', nothing special
127 will be done. If it is a quantity different from `timeclock-workday',
128 however, a record will be output to the timelog file to note the fact that
129 that day has a length that is different from the norm."
130 :type '(choice (const nil) function)
131 :group 'timeclock)
132
133 (defcustom timeclock-ask-before-exiting t
134 "If non-nil, ask if the user wants to clock out before exiting Emacs.
135 This variable only has effect if set with \\[customize]."
136 :set (lambda (symbol value)
137 (if value
138 (add-hook 'kill-emacs-query-functions 'timeclock-query-out)
139 (remove-hook 'kill-emacs-query-functions 'timeclock-query-out))
140 (set symbol value))
141 :type 'boolean
142 :group 'timeclock)
143
144 (defvar timeclock-update-timer nil
145 "The timer used to update `timeclock-mode-string'.")
146
147 ;; For byte-compiler.
148 (defvar display-time-hook)
149 (defvar timeclock-mode-line-display)
150
151 (defcustom timeclock-use-display-time t
152 "If non-nil, use `display-time-hook' for doing mode line updates.
153 The advantage of this is that one less timer has to be set running
154 amok in Emacs's process space. The disadvantage is that it requires
155 you to have `display-time' running. If you don't want to use
156 `display-time', but still want the mode line to show how much time is
157 left, set this variable to nil. Changing the value of this variable
158 while timeclock information is being displayed in the mode line has no
159 effect. You should call the function `timeclock-mode-line-display' with
160 a positive argument to force an update."
161 :set (lambda (symbol value)
162 (let ((currently-displaying
163 (and (boundp 'timeclock-mode-line-display)
164 timeclock-mode-line-display)))
165 ;; if we're changing to the state that
166 ;; `timeclock-mode-line-display' is already using, don't
167 ;; bother toggling it. This happens on the initial loading
168 ;; of timeclock.el.
169 (if (and currently-displaying
170 (or (and value
171 (boundp 'display-time-hook)
172 (memq 'timeclock-update-mode-line
173 display-time-hook))
174 (and (not value)
175 timeclock-update-timer)))
176 (setq currently-displaying nil))
177 (and currently-displaying
178 (setq timeclock-mode-line-display nil))
179 (set symbol value)
180 (and currently-displaying
181 (setq timeclock-mode-line-display t))
182 ;; FIXME: The return value isn't used, AFAIK!
183 value))
184 :type 'boolean
185 :group 'timeclock
186 :require 'time)
187
188 (defcustom timeclock-first-in-hook nil
189 "A hook run for the first \"in\" event each day.
190 Note that this hook is run before recording any events. Thus the
191 value of `timeclock-hours-today', `timeclock-last-event' and the
192 return value of function `timeclock-last-period' are relative previous
193 to today."
194 :type 'hook
195 :group 'timeclock)
196
197 (defcustom timeclock-load-hook nil
198 "Hook that gets run after timeclock has been loaded."
199 :type 'hook
200 :group 'timeclock)
201
202 (defcustom timeclock-in-hook nil
203 "A hook run every time an \"in\" event is recorded."
204 :type 'hook
205 :group 'timeclock)
206
207 (defcustom timeclock-day-over-hook nil
208 "A hook that is run when the workday has been completed.
209 This hook is only run if the current time remaining is being displayed
210 in the mode line. See the variable `timeclock-mode-line-display'."
211 :type 'hook
212 :group 'timeclock)
213
214 (defcustom timeclock-out-hook nil
215 "A hook run every time an \"out\" event is recorded."
216 :type 'hook
217 :group 'timeclock)
218
219 (defcustom timeclock-done-hook nil
220 "A hook run every time a project is marked as completed."
221 :type 'hook
222 :group 'timeclock)
223
224 (defcustom timeclock-event-hook nil
225 "A hook run every time any event is recorded."
226 :type 'hook
227 :group 'timeclock)
228
229 (defvar timeclock-last-event nil
230 "A list containing the last event that was recorded.
231 The format of this list is (CODE TIME PROJECT).")
232
233 (defvar timeclock-last-event-workday nil
234 "The number of seconds in the workday of `timeclock-last-event'.")
235
236 ;;; Internal Variables:
237
238 (defvar timeclock-discrepancy nil
239 "A variable containing the time discrepancy before the last event.
240 Normally, timeclock assumes that you intend to work for
241 `timeclock-workday' seconds every day. Any days in which you work
242 more or less than this amount is considered either a positive or
243 a negative discrepancy. If you work in such a manner that the
244 discrepancy is always brought back to zero, then you will by
245 definition have worked an average amount equal to `timeclock-workday'
246 each day.")
247
248 (defvar timeclock-elapsed nil
249 "A variable containing the time elapsed for complete periods today.
250 This value is not accurate enough to be useful by itself. Rather,
251 call `timeclock-workday-elapsed', to determine how much time has been
252 worked so far today. Also, if `timeclock-relative' is nil, this value
253 will be the same as `timeclock-discrepancy'.")
254
255 (defvar timeclock-use-elapsed nil
256 "Non-nil if the mode line should display time elapsed, not remaining.")
257
258 (defvar timeclock-last-period nil
259 "Integer representing the number of seconds in the last period.
260 Note that you shouldn't access this value, but instead should use the
261 function `timeclock-last-period'.")
262
263 (defvar timeclock-mode-string nil
264 "The timeclock string (optionally) displayed in the mode line.
265 The time is bracketed by <> if you are clocked in, otherwise by [].")
266
267 (defvar timeclock-day-over nil
268 "The date of the last day when notified \"day over\" for.")
269
270 ;;; User Functions:
271
272 (define-obsolete-function-alias 'timeclock-modeline-display
273 'timeclock-mode-line-display "24.3")
274 (define-obsolete-variable-alias 'timeclock-modeline-display
275 'timeclock-mode-line-display "24.3")
276
277 ;;;###autoload
278 (define-minor-mode timeclock-mode-line-display
279 "Toggle display of the amount of time left today in the mode line.
280 If `timeclock-use-display-time' is non-nil (the default), then
281 the function `display-time-mode' must be active, and the mode line
282 will be updated whenever the time display is updated. Otherwise,
283 the timeclock will use its own sixty second timer to do its
284 updating. With prefix ARG, turn mode line display on if and only
285 if ARG is positive. Returns the new status of timeclock mode line
286 display (non-nil means on)."
287 :global t
288 ;; cf display-time-mode.
289 (setq timeclock-mode-string "")
290 (or global-mode-string (setq global-mode-string '("")))
291 (if timeclock-mode-line-display
292 (progn
293 (or (memq 'timeclock-mode-string global-mode-string)
294 (setq global-mode-string
295 (append global-mode-string '(timeclock-mode-string))))
296 (add-hook 'timeclock-event-hook 'timeclock-update-mode-line)
297 (when timeclock-update-timer
298 (cancel-timer timeclock-update-timer)
299 (setq timeclock-update-timer nil))
300 (if (boundp 'display-time-hook)
301 (remove-hook 'display-time-hook 'timeclock-update-mode-line))
302 (if timeclock-use-display-time
303 (progn
304 ;; Update immediately so there is a visible change
305 ;; on calling this function.
306 (if display-time-mode
307 (timeclock-update-mode-line)
308 (message "Activate `display-time-mode' or turn off \
309 `timeclock-use-display-time' to see timeclock information"))
310 (add-hook 'display-time-hook 'timeclock-update-mode-line))
311 (setq timeclock-update-timer
312 (run-at-time nil 60 'timeclock-update-mode-line))))
313 (setq global-mode-string
314 (delq 'timeclock-mode-string global-mode-string))
315 (remove-hook 'timeclock-event-hook 'timeclock-update-mode-line)
316 (if (boundp 'display-time-hook)
317 (remove-hook 'display-time-hook
318 'timeclock-update-mode-line))
319 (when timeclock-update-timer
320 (cancel-timer timeclock-update-timer)
321 (setq timeclock-update-timer nil))))
322
323 (defsubst timeclock-time-to-date (&optional time)
324 "Convert the TIME value to a textual date string."
325 (format-time-string "%Y/%m/%d" time))
326
327 ;;;###autoload
328 (defun timeclock-in (&optional arg project find-project)
329 "Clock in, recording the current time moment in the timelog.
330 With a numeric prefix ARG, record the fact that today has only that
331 many hours in it to be worked. If ARG is a non-numeric prefix argument
332 \(non-nil, but not a number), 0 is assumed (working on a holiday or
333 weekend). *If not called interactively, ARG should be the number of
334 _seconds_ worked today*. This feature only has effect the first time
335 this function is called within a day.
336
337 PROJECT is the project being clocked into. If PROJECT is nil, and
338 FIND-PROJECT is non-nil -- or the user calls `timeclock-in'
339 interactively -- call the function `timeclock-get-project-function' to
340 discover the name of the project."
341 (interactive
342 (list (and current-prefix-arg
343 (if (numberp current-prefix-arg)
344 (* current-prefix-arg 60 60)
345 0))))
346 (if (equal (car timeclock-last-event) "i")
347 (error "You've already clocked in!")
348 (unless timeclock-last-event
349 (timeclock-reread-log))
350 ;; Either no log file, or day has rolled over.
351 (unless (and timeclock-last-event
352 (equal (timeclock-time-to-date
353 (cadr timeclock-last-event))
354 (timeclock-time-to-date)))
355 (let ((workday (or (and (numberp arg) arg)
356 (and arg 0)
357 (and timeclock-get-workday-function
358 (funcall timeclock-get-workday-function))
359 timeclock-workday)))
360 (run-hooks 'timeclock-first-in-hook)
361 ;; settle the discrepancy for the new day
362 (setq timeclock-discrepancy
363 (- (or timeclock-discrepancy 0) workday))
364 (if (not (= workday timeclock-workday))
365 (timeclock-log "h" (number-to-string
366 (/ workday (if (zerop (% workday (* 60 60)))
367 60 60.0) 60))))))
368 (timeclock-log "i" (or project
369 (and timeclock-get-project-function
370 (or find-project
371 (called-interactively-p 'interactive))
372 (funcall timeclock-get-project-function))))
373 (run-hooks 'timeclock-in-hook)))
374
375 ;;;###autoload
376 (defun timeclock-out (&optional arg reason find-reason)
377 "Clock out, recording the current time moment in the timelog.
378 If a prefix ARG is given, the user has completed the project that was
379 begun during the last time segment.
380
381 REASON is the user's reason for clocking out. If REASON is nil, and
382 FIND-REASON is non-nil -- or the user calls `timeclock-out'
383 interactively -- call the function `timeclock-get-reason-function' to
384 discover the reason."
385 (interactive "P")
386 (or timeclock-last-event
387 (error "You haven't clocked in!"))
388 (if (equal (downcase (car timeclock-last-event)) "o")
389 (error "You've already clocked out!")
390 (timeclock-log
391 (if arg "O" "o")
392 (or reason
393 (and timeclock-get-reason-function
394 (or find-reason (called-interactively-p 'interactive))
395 (funcall timeclock-get-reason-function))))
396 (run-hooks 'timeclock-out-hook)
397 (if arg
398 (run-hooks 'timeclock-done-hook))))
399
400 ;; Should today-only be removed in favor of timeclock-relative? - gm
401 (defsubst timeclock-workday-remaining (&optional today-only)
402 "Return the number of seconds until the workday is complete.
403 The amount returned is relative to the value of `timeclock-workday'.
404 If TODAY-ONLY is non-nil, the value returned will be relative only to
405 the time worked today, and not to past time."
406 (let ((discrep (timeclock-find-discrep)))
407 (if discrep
408 (- (if today-only (cadr discrep)
409 (car discrep)))
410 0.0)))
411
412 ;;;###autoload
413 (defun timeclock-status-string (&optional show-seconds today-only)
414 "Report the overall timeclock status at the present moment.
415 If SHOW-SECONDS is non-nil, display second resolution.
416 If TODAY-ONLY is non-nil, the display will be relative only to time
417 worked today, ignoring the time worked on previous days."
418 (interactive "P")
419 (let ((remainder (timeclock-workday-remaining
420 (or today-only
421 (not timeclock-relative))))
422 (last-in (equal (car timeclock-last-event) "i"))
423 status)
424 (setq status
425 (format "Currently %s since %s (%s), %s %s, leave at %s"
426 (if last-in "IN" "OUT")
427 (if show-seconds
428 (format-time-string "%-I:%M:%S %p"
429 (nth 1 timeclock-last-event))
430 (format-time-string "%-I:%M %p"
431 (nth 1 timeclock-last-event)))
432 (or (nth 2 timeclock-last-event)
433 (if last-in "**UNKNOWN**" "workday over"))
434 (timeclock-seconds-to-string remainder show-seconds t)
435 (if (> remainder 0)
436 "remaining" "over")
437 (timeclock-when-to-leave-string show-seconds today-only)))
438 (if (called-interactively-p 'interactive)
439 (message "%s" status)
440 status)))
441
442 ;;;###autoload
443 (defun timeclock-change (&optional arg project)
444 "Change to working on a different project.
445 This clocks out of the current project, then clocks in on a new one.
446 With a prefix ARG, consider the previous project as finished at the
447 time of changeover. PROJECT is the name of the last project you were
448 working on."
449 (interactive "P")
450 (timeclock-out arg)
451 (timeclock-in nil project (called-interactively-p 'interactive)))
452
453 ;;;###autoload
454 (defun timeclock-query-out ()
455 "Ask the user whether to clock out.
456 This is a useful function for adding to `kill-emacs-query-functions'."
457 (and (equal (car timeclock-last-event) "i")
458 (y-or-n-p "You're currently clocking time, clock out? ")
459 (timeclock-out))
460 ;; Unconditionally return t for `kill-emacs-query-functions'.
461 t)
462
463 ;;;###autoload
464 (defun timeclock-reread-log ()
465 "Re-read the timeclock, to account for external changes.
466 Returns the new value of `timeclock-discrepancy'."
467 (interactive)
468 (setq timeclock-discrepancy nil)
469 (timeclock-find-discrep)
470 (if (and timeclock-discrepancy timeclock-mode-line-display)
471 (timeclock-update-mode-line))
472 timeclock-discrepancy)
473
474 (defun timeclock-seconds-to-string (seconds &optional show-seconds
475 reverse-leader)
476 "Convert SECONDS into a compact time string.
477 If SHOW-SECONDS is non-nil, make the resolution of the return string
478 include the second count. If REVERSE-LEADER is non-nil, it means to
479 output a \"+\" if the time value is negative, rather than a \"-\".
480 This is used when negative time values have an inverted meaning (such
481 as with time remaining, where negative time really means overtime)."
482 (if show-seconds
483 (format "%s%d:%02d:%02d"
484 (if (< seconds 0) (if reverse-leader "+" "-") "")
485 (truncate (/ (abs seconds) 60 60))
486 (% (truncate (/ (abs seconds) 60)) 60)
487 (% (truncate (abs seconds)) 60))
488 (format "%s%d:%02d"
489 (if (< seconds 0) (if reverse-leader "+" "-") "")
490 (truncate (/ (abs seconds) 60 60))
491 (% (truncate (/ (abs seconds) 60)) 60))))
492
493 (defsubst timeclock-currently-in-p ()
494 "Return non-nil if the user is currently clocked in."
495 (equal (car timeclock-last-event) "i"))
496
497 ;;;###autoload
498 (defun timeclock-workday-remaining-string (&optional show-seconds
499 today-only)
500 "Return a string representing the amount of time left today.
501 Display second resolution if SHOW-SECONDS is non-nil. If TODAY-ONLY
502 is non-nil, the display will be relative only to time worked today.
503 See `timeclock-relative' for more information about the meaning of
504 \"relative to today\"."
505 (interactive)
506 (let ((string (timeclock-seconds-to-string
507 (timeclock-workday-remaining today-only)
508 show-seconds t)))
509 (if (called-interactively-p 'interactive)
510 (message "%s" string)
511 string)))
512
513 (defsubst timeclock-workday-elapsed ()
514 "Return the number of seconds worked so far today.
515 If RELATIVE is non-nil, the amount returned will be relative to past
516 time worked. The default is to return only the time that has elapsed
517 so far today."
518 (let ((discrep (timeclock-find-discrep)))
519 (if discrep
520 (nth 2 discrep)
521 0.0)))
522
523 ;;;###autoload
524 (defun timeclock-workday-elapsed-string (&optional show-seconds)
525 "Return a string representing the amount of time worked today.
526 Display seconds resolution if SHOW-SECONDS is non-nil. If RELATIVE is
527 non-nil, the amount returned will be relative to past time worked."
528 (interactive)
529 (let ((string (timeclock-seconds-to-string (timeclock-workday-elapsed)
530 show-seconds)))
531 (if (called-interactively-p 'interactive)
532 (message "%s" string)
533 string)))
534
535 (defalias 'timeclock-time-to-seconds (if (fboundp 'float-time) 'float-time
536 'time-to-seconds))
537
538 (defalias 'timeclock-seconds-to-time 'seconds-to-time)
539
540 ;; Should today-only be removed in favor of timeclock-relative? - gm
541 (defsubst timeclock-when-to-leave (&optional today-only)
542 "Return a time value representing the end of today's workday.
543 If TODAY-ONLY is non-nil, the value returned will be relative only to
544 the time worked today, and not to past time."
545 (timeclock-seconds-to-time
546 (- (timeclock-time-to-seconds)
547 (let ((discrep (timeclock-find-discrep)))
548 (if discrep
549 (if today-only
550 (cadr discrep)
551 (car discrep))
552 0.0)))))
553
554 ;;;###autoload
555 (defun timeclock-when-to-leave-string (&optional show-seconds
556 today-only)
557 "Return a string representing the end of today's workday.
558 This string is relative to the value of `timeclock-workday'. If
559 SHOW-SECONDS is non-nil, the value printed/returned will include
560 seconds. If TODAY-ONLY is non-nil, the value returned will be
561 relative only to the time worked today, and not to past time."
562 ;; Should today-only be removed in favor of timeclock-relative? - gm
563 (interactive)
564 (let* ((then (timeclock-when-to-leave today-only))
565 (string
566 (if show-seconds
567 (format-time-string "%-I:%M:%S %p" then)
568 (format-time-string "%-I:%M %p" then))))
569 (if (called-interactively-p 'interactive)
570 (message "%s" string)
571 string)))
572
573 (defun timeclock-make-hours-explicit (old-default)
574 "Specify all workday lengths in `timeclock-file'.
575 OLD-DEFAULT hours are set for every day that has no number indicated."
576 (interactive "P")
577 (if old-default (setq old-default (prefix-numeric-value old-default))
578 (error "`timelog-make-hours-explicit' requires an explicit argument"))
579 (let ((extant-timelog (find-buffer-visiting timeclock-file))
580 current-date)
581 (with-current-buffer (find-file-noselect timeclock-file t)
582 (unwind-protect
583 (save-excursion
584 (save-restriction
585 (widen)
586 (goto-char (point-min))
587 (while (progn (skip-chars-forward "\n") (not (eobp)))
588 ;; This is just a variant of `timeclock-moment-regexp'.
589 (unless (looking-at
590 (concat "^\\([bhioO]\\) \\([0-9]+/[0-9]+/[0-9]+\\) "
591 "\\([0-9]+:[0-9]+:[0-9]+\\)"))
592 (error "Can't parse `%s'" timeclock-file))
593 (let ((this-date (match-string 2)))
594 (unless (or (and current-date
595 (string= this-date current-date))
596 (string= (match-string 1) "h"))
597 (insert (format "h %s %s %s\n" (match-string 2)
598 (match-string 3) old-default)))
599 (if (string-match "^[ih]" (match-string 1)) ; ignore logouts
600 (setq current-date this-date)))
601 (forward-line))
602 (save-buffer)))
603 (unless extant-timelog (kill-buffer (current-buffer)))))))
604
605 ;;; Internal Functions:
606
607 (defvar timeclock-project-list nil)
608 (defvar timeclock-last-project nil)
609
610 (defun timeclock-completing-read (prompt alist &optional default)
611 "A version of `completing-read' that works on both Emacs and XEmacs.
612 PROMPT, ALIST and DEFAULT are used for the PROMPT, COLLECTION and DEF
613 arguments of `completing-read'."
614 (if (featurep 'xemacs)
615 (let ((str (completing-read prompt alist)))
616 (if (or (null str) (zerop (length str)))
617 default
618 str))
619 (completing-read prompt alist nil nil nil nil default)))
620
621 (defun timeclock-ask-for-project ()
622 "Ask the user for the project they are clocking into."
623 (timeclock-completing-read
624 (format "Clock into which project (default %s): "
625 (or timeclock-last-project
626 (car timeclock-project-list)))
627 (mapcar 'list timeclock-project-list)
628 (or timeclock-last-project
629 (car timeclock-project-list))))
630
631 (defvar timeclock-reason-list nil)
632
633 (defun timeclock-ask-for-reason ()
634 "Ask the user for the reason they are clocking out."
635 (timeclock-completing-read "Reason for clocking out: "
636 (mapcar 'list timeclock-reason-list)))
637
638 (define-obsolete-function-alias 'timeclock-update-modeline
639 'timeclock-update-mode-line "24.3")
640
641 (defun timeclock-update-mode-line ()
642 "Update the `timeclock-mode-string' displayed in the mode line.
643 The value of `timeclock-relative' affects the display as described in
644 that variable's documentation."
645 (interactive)
646 (let ((remainder
647 (if timeclock-use-elapsed
648 (timeclock-workday-elapsed)
649 (timeclock-workday-remaining (not timeclock-relative))))
650 (last-in (equal (car timeclock-last-event) "i"))
651 (todays-date (timeclock-time-to-date)))
652 (when (and (< remainder 0)
653 (not (and timeclock-day-over
654 (equal timeclock-day-over todays-date))))
655 (setq timeclock-day-over todays-date)
656 (run-hooks 'timeclock-day-over-hook))
657 (setq timeclock-mode-string
658 (propertize
659 (format " %c%s%c "
660 (if last-in ?< ?[)
661 (timeclock-seconds-to-string remainder nil t)
662 (if last-in ?> ?]))
663 'help-echo "timeclock: time remaining"))))
664
665 (put 'timeclock-mode-string 'risky-local-variable t)
666
667 (defun timeclock-log (code &optional project)
668 "Log the event CODE to the timeclock log, at the time of call.
669 If PROJECT is a string, it represents the project which the event is
670 being logged for. Normally only \"in\" events specify a project."
671 (let ((extant-timelog (find-buffer-visiting timeclock-file)))
672 (with-current-buffer (find-file-noselect timeclock-file t)
673 (save-excursion
674 (save-restriction
675 (widen)
676 (goto-char (point-max))
677 (if (not (bolp))
678 (insert "\n"))
679 (let ((now (current-time)))
680 (insert code " "
681 (format-time-string "%Y/%m/%d %H:%M:%S" now)
682 (or (and (stringp project)
683 (> (length project) 0)
684 (concat " " project))
685 "")
686 "\n")
687 (if (equal (downcase code) "o")
688 (setq timeclock-last-period
689 (- (timeclock-time-to-seconds now)
690 (timeclock-time-to-seconds
691 (cadr timeclock-last-event)))
692 timeclock-discrepancy
693 (+ timeclock-discrepancy
694 timeclock-last-period)))
695 (setq timeclock-last-event (list code now project)))))
696 (save-buffer)
697 (unless extant-timelog (kill-buffer (current-buffer)))))
698 (run-hooks 'timeclock-event-hook))
699
700 (defvar timeclock-moment-regexp
701 (concat "\\([bhioO]\\)\\s-+"
702 "\\([0-9]+\\)/\\([0-9]+\\)/\\([0-9]+\\)\\s-+"
703 "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)[ \t]*" "\\([^\n]*\\)"))
704
705 (defsubst timeclock-read-moment ()
706 "Read the moment under point from the timelog."
707 (if (looking-at timeclock-moment-regexp)
708 (let ((code (match-string 1))
709 (year (string-to-number (match-string 2)))
710 (mon (string-to-number (match-string 3)))
711 (mday (string-to-number (match-string 4)))
712 (hour (string-to-number (match-string 5)))
713 (min (string-to-number (match-string 6)))
714 (sec (string-to-number (match-string 7)))
715 (project (match-string 8)))
716 (list code (encode-time sec min hour mday mon year) project))))
717
718 (defun timeclock-last-period (&optional moment)
719 "Return the value of the last event period.
720 If the last event was a clock-in, the period will be open ended, and
721 growing every second. Otherwise, it is a fixed amount which has been
722 recorded to disk. If MOMENT is non-nil, use that as the current time.
723 This is only provided for coherency when used by
724 `timeclock-discrepancy'."
725 (if (equal (car timeclock-last-event) "i")
726 (- (timeclock-time-to-seconds moment)
727 (timeclock-time-to-seconds (cadr timeclock-last-event)))
728 timeclock-last-period))
729
730 (defsubst timeclock-entry-length (entry)
731 "Return the length of ENTRY in seconds."
732 (- (timeclock-time-to-seconds (cadr entry))
733 (timeclock-time-to-seconds (car entry))))
734
735 (defsubst timeclock-entry-begin (entry)
736 "Return the start time of ENTRY."
737 (car entry))
738
739 (defsubst timeclock-entry-end (entry)
740 "Return the end time of ENTRY."
741 (cadr entry))
742
743 (defsubst timeclock-entry-project (entry)
744 "Return the project of ENTRY."
745 (nth 2 entry))
746
747 (defsubst timeclock-entry-comment (entry)
748 "Return the comment of ENTRY."
749 (nth 3 entry))
750
751 (defsubst timeclock-entry-list-length (entry-list)
752 "Return the total length of ENTRY-LIST in seconds."
753 (let ((length 0))
754 (dolist (entry entry-list)
755 (setq length (+ length (timeclock-entry-length entry))))
756 length))
757
758 (defsubst timeclock-entry-list-begin (entry-list)
759 "Return the start time of the first element of ENTRY-LIST."
760 (timeclock-entry-begin (car entry-list)))
761
762 (defsubst timeclock-entry-list-end (entry-list)
763 "Return the end time of the last element of ENTRY-LIST."
764 (timeclock-entry-end (car (last entry-list))))
765
766 (defsubst timeclock-entry-list-span (entry-list)
767 "Return the total time in seconds spanned by ENTRY-LIST."
768 (- (timeclock-time-to-seconds (timeclock-entry-list-end entry-list))
769 (timeclock-time-to-seconds (timeclock-entry-list-begin entry-list))))
770
771 (defsubst timeclock-entry-list-break (entry-list)
772 "Return the total break time (span - length) in ENTRY-LIST."
773 (- (timeclock-entry-list-span entry-list)
774 (timeclock-entry-list-length entry-list)))
775
776 (defsubst timeclock-entry-list-projects (entry-list)
777 "Return a list of all the projects in ENTRY-LIST."
778 (let (projects proj)
779 (dolist (entry entry-list)
780 (setq proj (timeclock-entry-project entry))
781 (if projects
782 (add-to-list 'projects proj)
783 (setq projects (list proj))))
784 projects))
785
786 (defsubst timeclock-day-required (day)
787 "Return the required length of DAY in seconds, default `timeclock-workday'."
788 (or (car day) timeclock-workday))
789
790 (defsubst timeclock-day-length (day)
791 "Return the actual length of DAY in seconds."
792 (timeclock-entry-list-length (cdr day)))
793
794 (defsubst timeclock-day-debt (day)
795 "Return the debt (required - actual) associated with DAY, in seconds."
796 (- (timeclock-day-required day)
797 (timeclock-day-length day)))
798
799 (defsubst timeclock-day-begin (day)
800 "Return the start time of DAY."
801 (timeclock-entry-list-begin (cdr day)))
802
803 (defsubst timeclock-day-end (day)
804 "Return the end time of DAY."
805 (timeclock-entry-list-end (cdr day)))
806
807 (defsubst timeclock-day-span (day)
808 "Return the span of DAY."
809 (timeclock-entry-list-span (cdr day)))
810
811 (defsubst timeclock-day-break (day)
812 "Return the total break time of DAY."
813 (timeclock-entry-list-break (cdr day)))
814
815 (defsubst timeclock-day-projects (day)
816 "Return a list of all the projects in DAY."
817 (timeclock-entry-list-projects (cddr day)))
818
819 (defun timeclock-day-list-template (func day-list)
820 "Template for summing the result of FUNC on each element of DAY-LIST."
821 (let ((length 0))
822 (dolist (day day-list)
823 (setq length (+ length (funcall func day))))
824 length))
825
826 (defun timeclock-day-list-required (day-list)
827 "Return total required length of DAY-LIST, in seconds."
828 (timeclock-day-list-template #'timeclock-day-required day-list))
829
830 (defun timeclock-day-list-length (day-list)
831 "Return actual length of DAY-LIST, in seconds."
832 (timeclock-day-list-template #'timeclock-day-length day-list))
833
834 (defun timeclock-day-list-debt (day-list)
835 "Return total debt (required - actual) of DAY-LIST."
836 (timeclock-day-list-template #'timeclock-day-debt day-list))
837
838 (defsubst timeclock-day-list-begin (day-list)
839 "Return the start time of DAY-LIST."
840 (timeclock-day-begin (car day-list)))
841
842 (defsubst timeclock-day-list-end (day-list)
843 "Return the end time of DAY-LIST."
844 (timeclock-day-end (car (last day-list))))
845
846 (defun timeclock-day-list-span (day-list)
847 "Return the span of DAY-LIST."
848 (timeclock-day-list-template #'timeclock-day-span day-list))
849
850 (defun timeclock-day-list-break (day-list)
851 "Return the total break of DAY-LIST."
852 (timeclock-day-list-template #'timeclock-day-break day-list))
853
854 (defun timeclock-day-list-projects (day-list)
855 "Return a list of all the projects in DAY-LIST."
856 (let (projects)
857 (dolist (day day-list)
858 (dolist (proj (timeclock-day-projects day))
859 (if projects
860 (add-to-list 'projects proj)
861 (setq projects (list proj)))))
862 projects))
863
864 (defsubst timeclock-current-debt (&optional log-data)
865 "Return the seconds debt from LOG-DATA, default `timeclock-log-data'."
866 (nth 0 (or log-data (timeclock-log-data))))
867
868 (defsubst timeclock-day-alist (&optional log-data)
869 "Return the date alist from LOG-DATA, default `timeclock-log-data'."
870 (nth 1 (or log-data (timeclock-log-data))))
871
872 (defun timeclock-day-list (&optional log-data)
873 "Return a list of the cdrs of the date alist from LOG-DATA."
874 (let (day-list)
875 (dolist (date-list (timeclock-day-alist log-data))
876 (setq day-list (cons (cdr date-list) day-list)))
877 day-list))
878
879 (defsubst timeclock-project-alist (&optional log-data)
880 "Return the project alist from LOG-DATA, default `timeclock-log-data'."
881 (nth 2 (or log-data (timeclock-log-data))))
882
883 (defun timeclock-log-data (&optional recent-only filename)
884 "Return the contents of the timelog file, in a useful format.
885 If the optional argument RECENT-ONLY is non-nil, only show the contents
886 from the last point where the time debt (see below) was set.
887 If the optional argument FILENAME is non-nil, it is used instead of
888 the file specified by `timeclock-file.'
889
890 A timelog contains data in the form of a single entry per line.
891 Each entry has the form:
892
893 CODE YYYY/MM/DD HH:MM:SS [COMMENT]
894
895 CODE is one of: b, h, i, o or O. COMMENT is optional when the code is
896 i, o or O. The meanings of the codes are:
897
898 b Set the current time balance, or \"time debt\". Useful when
899 archiving old log data, when a debt must be carried forward.
900 The COMMENT here is the number of seconds of debt.
901
902 h Set the required working time for the given day. This must
903 be the first entry for that day. The COMMENT in this case is
904 the number of hours in this workday. Floating point amounts
905 are allowed.
906
907 i Clock in. The COMMENT in this case should be the name of the
908 project worked on.
909
910 o Clock out. COMMENT is unnecessary, but can be used to provide
911 a description of how the period went, for example.
912
913 O Final clock out. Whatever project was being worked on, it is
914 now finished. Useful for creating summary reports.
915
916 When this function is called, it will return a data structure with the
917 following format:
918
919 (DEBT ENTRIES-BY-DAY ENTRIES-BY-PROJECT)
920
921 DEBT is a floating point number representing the number of seconds
922 “owed” before any work was done. For a new file (one without a `b'
923 entry), this is always zero.
924
925 The two entries lists have similar formats. They are both alists,
926 where the CAR is the index, and the CDR is a list of time entries.
927 For ENTRIES-BY-DAY, the CAR is a textual date string, of the form
928 YYYY/MM/DD. For ENTRIES-BY-PROJECT, it is the name of the project
929 worked on, or t for the default project.
930
931 The CDR for ENTRIES-BY-DAY is slightly different than for
932 ENTRIES-BY-PROJECT. It has the following form:
933
934 (DAY-LENGTH TIME-ENTRIES...)
935
936 For ENTRIES-BY-PROJECT, there is no DAY-LENGTH member. It is simply a
937 list of TIME-ENTRIES. Note that if DAY-LENGTH is nil, it means
938 whatever is the default should be used.
939
940 A TIME-ENTRY is a recorded time interval. It has the following format
941 \(although generally one does not have to manipulate these entries
942 directly; see below):
943
944 (BEGIN-TIME END-TIME PROJECT [COMMENT] [FINAL-P])
945
946 Anyway, suffice it to say there are a lot of structures. Typically
947 the user is expected to manipulate to the day(s) or project(s) that he
948 or she wants, at which point the following helper functions may be
949 used:
950
951 timeclock-day-required
952 timeclock-day-length
953 timeclock-day-debt
954 timeclock-day-begin
955 timeclock-day-end
956 timeclock-day-span
957 timeclock-day-break
958 timeclock-day-projects
959
960 timeclock-day-list-required
961 timeclock-day-list-length
962 timeclock-day-list-debt
963 timeclock-day-list-begin
964 timeclock-day-list-end
965 timeclock-day-list-span
966 timeclock-day-list-break
967 timeclock-day-list-projects
968
969 timeclock-entry-length
970 timeclock-entry-begin
971 timeclock-entry-end
972 timeclock-entry-project
973 timeclock-entry-comment
974
975 timeclock-entry-list-length
976 timeclock-entry-list-begin
977 timeclock-entry-list-end
978 timeclock-entry-list-span
979 timeclock-entry-list-break
980 timeclock-entry-list-projects
981
982 A few comments should make the use of the above functions obvious:
983
984 `required' is the amount of time that must be spent during a day, or
985 sequence of days, in order to have no debt.
986
987 `length' is the actual amount of time that was spent.
988
989 `debt' is the difference between required time and length. A
990 negative debt signifies overtime.
991
992 `begin' is the earliest moment at which work began.
993
994 `end' is the final moment work was done.
995
996 `span' is the difference between begin and end.
997
998 `break' is the difference between span and length.
999
1000 `project' is the project that was worked on, and `projects' is a
1001 list of all the projects that were worked on during a given period.
1002
1003 `comment', where it applies, could mean anything.
1004
1005 There are a few more functions available, for locating day and entry
1006 lists:
1007
1008 timeclock-day-alist LOG-DATA
1009 timeclock-project-alist LOG-DATA
1010 timeclock-current-debt LOG-DATA
1011
1012 See the documentation for the given function if more info is needed."
1013 (let ((log-data (list 0.0 nil nil))
1014 (now (current-time))
1015 last-date-limited last-date-seconds last-date
1016 (line 0) last beg day entry event)
1017 (with-temp-buffer
1018 (insert-file-contents (or filename timeclock-file))
1019 (when recent-only
1020 (goto-char (point-max))
1021 (unless (re-search-backward "^b\\s-+" nil t)
1022 (goto-char (point-min))))
1023 (while (or (setq event (timeclock-read-moment))
1024 (and beg (not last)
1025 (setq last t event (list "o" now))))
1026 (setq line (1+ line))
1027 (cond ((equal (car event) "b")
1028 (setcar log-data (string-to-number (nth 2 event))))
1029 ((equal (car event) "h")
1030 (setq last-date-limited (timeclock-time-to-date (cadr event))
1031 last-date-seconds (* (string-to-number (nth 2 event))
1032 3600.0)))
1033 ((equal (car event) "i")
1034 (if beg
1035 (error "Error in format of timelog file, line %d" line)
1036 (setq beg t))
1037 (setq entry (list (cadr event) nil
1038 (and (> (length (nth 2 event)) 0)
1039 (nth 2 event))))
1040 (let ((date (timeclock-time-to-date (cadr event))))
1041 (if (and last-date
1042 (not (equal date last-date)))
1043 (progn
1044 (setcar (cdr log-data)
1045 (cons (cons last-date day)
1046 (cadr log-data)))
1047 (setq day (list (and last-date-limited
1048 last-date-seconds))))
1049 (unless day
1050 (setq day (list (and last-date-limited
1051 last-date-seconds)))))
1052 (setq last-date date
1053 last-date-limited nil)))
1054 ((equal (downcase (car event)) "o")
1055 (if (not beg)
1056 (error "Error in format of timelog file, line %d" line)
1057 (setq beg nil))
1058 (setcar (cdr entry) (cadr event))
1059 (let ((desc (and (> (length (nth 2 event)) 0)
1060 (nth 2 event))))
1061 (if desc
1062 (nconc entry (list (nth 2 event))))
1063 (if (equal (car event) "O")
1064 (nconc entry (if desc
1065 (list t)
1066 (list nil t))))
1067 (nconc day (list entry))
1068 (setq desc (nth 2 entry))
1069 (let ((proj (assoc desc (nth 2 log-data))))
1070 (if (null proj)
1071 (setcar (cddr log-data)
1072 (cons (cons desc (list entry))
1073 (nth 2 log-data)))
1074 (nconc (cdr proj) (list entry)))))))
1075 (forward-line))
1076 (if day
1077 (setcar (cdr log-data)
1078 (cons (cons last-date day)
1079 (cadr log-data))))
1080 log-data)))
1081
1082 (defun timeclock-find-discrep ()
1083 "Calculate time discrepancies, in seconds.
1084 The result is a three element list, containing the total time
1085 discrepancy, today's discrepancy, and the time worked today."
1086 ;; This is not implemented in terms of the functions above, because
1087 ;; it's a bit wasteful to read all of that data in, just to throw
1088 ;; away more than 90% of the information afterwards.
1089 ;;
1090 ;; If it were implemented using those functions, it would look
1091 ;; something like this:
1092 ;; (let ((days (timeclock-day-alist (timeclock-log-data)))
1093 ;; (total 0.0))
1094 ;; (while days
1095 ;; (setq total (+ total (- (timeclock-day-length (cdar days))
1096 ;; (timeclock-day-required (cdar days))))
1097 ;; days (cdr days)))
1098 ;; total)
1099 (let* ((now (current-time))
1100 (todays-date (timeclock-time-to-date now))
1101 (first t) (accum 0) (elapsed 0)
1102 event beg last-date
1103 last-date-limited last-date-seconds)
1104 (unless timeclock-discrepancy
1105 (when (file-readable-p timeclock-file)
1106 (setq timeclock-project-list nil
1107 timeclock-last-project nil
1108 timeclock-reason-list nil
1109 timeclock-elapsed 0)
1110 (with-temp-buffer
1111 (insert-file-contents timeclock-file)
1112 (goto-char (point-max))
1113 (unless (re-search-backward "^b\\s-+" nil t)
1114 (goto-char (point-min)))
1115 (while (setq event (timeclock-read-moment))
1116 (cond ((equal (car event) "b")
1117 (setq accum (string-to-number (nth 2 event))))
1118 ((equal (car event) "h")
1119 (setq last-date-limited
1120 (timeclock-time-to-date (cadr event))
1121 last-date-seconds
1122 (* (string-to-number (nth 2 event)) 3600.0)))
1123 ((equal (car event) "i")
1124 (when (and (nth 2 event)
1125 (> (length (nth 2 event)) 0))
1126 (add-to-list 'timeclock-project-list (nth 2 event))
1127 (setq timeclock-last-project (nth 2 event)))
1128 (let ((date (timeclock-time-to-date (cadr event))))
1129 (if (if last-date
1130 (not (equal date last-date))
1131 first)
1132 (setq first nil
1133 accum (- accum (if last-date-limited
1134 last-date-seconds
1135 timeclock-workday))))
1136 (setq last-date date
1137 last-date-limited nil)
1138 (if beg
1139 (error "Error in format of timelog file!")
1140 (setq beg (timeclock-time-to-seconds (cadr event))))))
1141 ((equal (downcase (car event)) "o")
1142 (if (and (nth 2 event)
1143 (> (length (nth 2 event)) 0))
1144 (add-to-list 'timeclock-reason-list (nth 2 event)))
1145 (if (not beg)
1146 (error "Error in format of timelog file!")
1147 (setq timeclock-last-period
1148 (- (timeclock-time-to-seconds (cadr event)) beg)
1149 accum (+ timeclock-last-period accum)
1150 beg nil))
1151 (if (equal last-date todays-date)
1152 (setq timeclock-elapsed
1153 (+ timeclock-last-period timeclock-elapsed)))))
1154 (setq timeclock-last-event event
1155 timeclock-last-event-workday
1156 (if (equal todays-date last-date-limited)
1157 last-date-seconds
1158 timeclock-workday))
1159 (forward-line))
1160 (setq timeclock-discrepancy accum))))
1161 (unless timeclock-last-event-workday
1162 (setq timeclock-last-event-workday timeclock-workday))
1163 (setq accum (or timeclock-discrepancy 0)
1164 elapsed (or timeclock-elapsed elapsed))
1165 (if timeclock-last-event
1166 (if (equal (car timeclock-last-event) "i")
1167 (let ((last-period (timeclock-last-period now)))
1168 (setq accum (+ accum last-period)
1169 elapsed (+ elapsed last-period)))
1170 (if (not (equal (timeclock-time-to-date
1171 (cadr timeclock-last-event))
1172 (timeclock-time-to-date now)))
1173 (setq accum (- accum timeclock-last-event-workday)))))
1174 (list accum (- elapsed timeclock-last-event-workday)
1175 elapsed)))
1176
1177 ;;; A reporting function that uses timeclock-log-data
1178
1179 (defun timeclock-day-base (&optional time)
1180 "Given a time within a day, return 0:0:0 within that day.
1181 If optional argument TIME is non-nil, use that instead of the current time."
1182 (let ((decoded (decode-time time)))
1183 (setcar (nthcdr 0 decoded) 0)
1184 (setcar (nthcdr 1 decoded) 0)
1185 (setcar (nthcdr 2 decoded) 0)
1186 (apply 'encode-time decoded)))
1187
1188 (defun timeclock-mean (l)
1189 "Compute the arithmetic mean of the values in the list L."
1190 (let ((total 0)
1191 (count 0))
1192 (dolist (thisl l)
1193 (setq total (+ total thisl)
1194 count (1+ count)))
1195 (if (zerop count)
1196 0
1197 (/ total count))))
1198
1199 (defun timeclock-generate-report (&optional html-p)
1200 "Generate a summary report based on the current timelog file.
1201 By default, the report is in plain text, but if the optional argument
1202 HTML-P is non-nil, HTML markup is added."
1203 (interactive "P")
1204 (let ((log (timeclock-log-data))
1205 (today (timeclock-day-base)))
1206 (if html-p (insert "<p>"))
1207 (insert "Currently ")
1208 (let ((project (nth 2 timeclock-last-event))
1209 (begin (nth 1 timeclock-last-event))
1210 done)
1211 (if (timeclock-currently-in-p)
1212 (insert "IN")
1213 (if (zerop (length project))
1214 (progn (insert "Done Working Today")
1215 (setq done t))
1216 (insert "OUT")))
1217 (unless done
1218 (insert " since " (format-time-string "%Y/%m/%d %-I:%M %p" begin))
1219 (if html-p
1220 (insert "<br>\n<b>")
1221 (insert "\n*"))
1222 (if (timeclock-currently-in-p)
1223 (insert "Working on "))
1224 (if html-p
1225 (insert project "</b><br>\n")
1226 (insert project "*\n"))
1227 (let ((proj-data (cdr (assoc project (timeclock-project-alist log))))
1228 (two-weeks-ago (timeclock-seconds-to-time
1229 (- (timeclock-time-to-seconds today)
1230 (* 2 7 24 60 60))))
1231 two-week-len today-len)
1232 (while proj-data
1233 (if (not (time-less-p
1234 (timeclock-entry-begin (car proj-data)) today))
1235 (setq today-len (timeclock-entry-list-length proj-data)
1236 proj-data nil)
1237 (if (and (null two-week-len)
1238 (not (time-less-p
1239 (timeclock-entry-begin (car proj-data))
1240 two-weeks-ago)))
1241 (setq two-week-len (timeclock-entry-list-length proj-data)))
1242 (setq proj-data (cdr proj-data))))
1243 (if (null two-week-len)
1244 (setq two-week-len today-len))
1245 (if html-p (insert "<p>"))
1246 (if today-len
1247 (insert "\nTime spent on this task today: "
1248 (timeclock-seconds-to-string today-len)
1249 ". In the last two weeks: "
1250 (timeclock-seconds-to-string two-week-len))
1251 (if two-week-len
1252 (insert "\nTime spent on this task in the last two weeks: "
1253 (timeclock-seconds-to-string two-week-len))))
1254 (if html-p (insert "<br>"))
1255 (insert "\n"
1256 (timeclock-seconds-to-string (timeclock-workday-elapsed))
1257 " worked today, "
1258 (timeclock-seconds-to-string (timeclock-workday-remaining))
1259 " remaining, done at "
1260 (timeclock-when-to-leave-string) "\n")))
1261 (if html-p (insert "<p>"))
1262 (insert "\nThere have been "
1263 (number-to-string
1264 (length (timeclock-day-alist log)))
1265 " days of activity, starting "
1266 (caar (last (timeclock-day-alist log))))
1267 (if html-p (insert "</p>"))
1268 (when html-p
1269 (insert "<p>
1270 <table>
1271 <td width=\"25\"><br></td><td>
1272 <table border=1 cellpadding=3>
1273 <tr><th><i>Statistics</i></th>
1274 <th>Entire</th>
1275 <th>-30 days</th>
1276 <th>-3 mons</th>
1277 <th>-6 mons</th>
1278 <th>-1 year</th>
1279 </tr>")
1280 (let* ((day-list (timeclock-day-list))
1281 (thirty-days-ago (timeclock-seconds-to-time
1282 (- (timeclock-time-to-seconds today)
1283 (* 30 24 60 60))))
1284 (three-months-ago (timeclock-seconds-to-time
1285 (- (timeclock-time-to-seconds today)
1286 (* 90 24 60 60))))
1287 (six-months-ago (timeclock-seconds-to-time
1288 (- (timeclock-time-to-seconds today)
1289 (* 180 24 60 60))))
1290 (one-year-ago (timeclock-seconds-to-time
1291 (- (timeclock-time-to-seconds today)
1292 (* 365 24 60 60))))
1293 (time-in (vector (list t) (list t) (list t) (list t) (list t)))
1294 (time-out (vector (list t) (list t) (list t) (list t) (list t)))
1295 (breaks (vector (list t) (list t) (list t) (list t) (list t)))
1296 (workday (vector (list t) (list t) (list t) (list t) (list t)))
1297 (lengths (vector '(0 0) thirty-days-ago three-months-ago
1298 six-months-ago one-year-ago)))
1299 ;; collect statistics from complete timelog
1300 (dolist (day day-list)
1301 (let ((i 0) (l 5))
1302 (while (< i l)
1303 (unless (time-less-p
1304 (timeclock-day-begin day)
1305 (aref lengths i))
1306 (let ((base (timeclock-time-to-seconds
1307 (timeclock-day-base
1308 (timeclock-day-begin day)))))
1309 (nconc (aref time-in i)
1310 (list (- (timeclock-time-to-seconds
1311 (timeclock-day-begin day))
1312 base)))
1313 (let ((span (timeclock-day-span day))
1314 (len (timeclock-day-length day))
1315 (req (timeclock-day-required day)))
1316 ;; If the day's actual work length is less than
1317 ;; 70% of its span, then likely the exit time
1318 ;; and break amount are not worthwhile adding to
1319 ;; the statistic
1320 (when (and (> span 0)
1321 (> (/ (float len) (float span)) 0.70))
1322 (nconc (aref time-out i)
1323 (list (- (timeclock-time-to-seconds
1324 (timeclock-day-end day))
1325 base)))
1326 (nconc (aref breaks i) (list (- span len))))
1327 (if req
1328 (setq len (+ len (- timeclock-workday req))))
1329 (nconc (aref workday i) (list len)))))
1330 (setq i (1+ i)))))
1331 ;; average statistics
1332 (let ((i 0) (l 5))
1333 (while (< i l)
1334 (aset time-in i (timeclock-mean (cdr (aref time-in i))))
1335 (aset time-out i (timeclock-mean (cdr (aref time-out i))))
1336 (aset breaks i (timeclock-mean (cdr (aref breaks i))))
1337 (aset workday i (timeclock-mean (cdr (aref workday i))))
1338 (setq i (1+ i))))
1339 ;; Output the HTML table
1340 (insert "<tr>\n")
1341 (insert "<td align=\"center\">Time in</td>\n")
1342 (let ((i 0) (l 5))
1343 (while (< i l)
1344 (insert "<td align=\"right\">"
1345 (timeclock-seconds-to-string (aref time-in i))
1346 "</td>\n")
1347 (setq i (1+ i))))
1348 (insert "</tr>\n")
1349
1350 (insert "<tr>\n")
1351 (insert "<td align=\"center\">Time out</td>\n")
1352 (let ((i 0) (l 5))
1353 (while (< i l)
1354 (insert "<td align=\"right\">"
1355 (timeclock-seconds-to-string (aref time-out i))
1356 "</td>\n")
1357 (setq i (1+ i))))
1358 (insert "</tr>\n")
1359
1360 (insert "<tr>\n")
1361 (insert "<td align=\"center\">Break</td>\n")
1362 (let ((i 0) (l 5))
1363 (while (< i l)
1364 (insert "<td align=\"right\">"
1365 (timeclock-seconds-to-string (aref breaks i))
1366 "</td>\n")
1367 (setq i (1+ i))))
1368 (insert "</tr>\n")
1369
1370 (insert "<tr>\n")
1371 (insert "<td align=\"center\">Workday</td>\n")
1372 (let ((i 0) (l 5))
1373 (while (< i l)
1374 (insert "<td align=\"right\">"
1375 (timeclock-seconds-to-string (aref workday i))
1376 "</td>\n")
1377 (setq i (1+ i))))
1378 (insert "</tr>\n"))
1379 (insert "<tfoot>
1380 <td colspan=\"6\" align=\"center\">
1381 <i>These are approximate figures</i></td>
1382 </tfoot>
1383 </table>
1384 </td></table>")))))
1385
1386 ;;; A helpful little function
1387
1388 (defun timeclock-visit-timelog ()
1389 "Open the file named by `timeclock-file' in another window."
1390 (interactive)
1391 (find-file-other-window timeclock-file))
1392
1393 (provide 'timeclock)
1394
1395 (run-hooks 'timeclock-load-hook)
1396
1397 ;; make sure we know the list of reasons, projects, and have computed
1398 ;; the last event and current discrepancy.
1399 (if (file-readable-p timeclock-file)
1400 (timeclock-reread-log))
1401
1402 ;;; timeclock.el ends here