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