]> code.delx.au - gnu-emacs/blob - lisp/calendar/timeclock.el
Doc, comment, etc updates for increased use of locate-user-emacs-file
[gnu-emacs] / lisp / calendar / timeclock.el
1 ;;; timeclock.el --- mode for keeping track of how much you work
2
3 ;; Copyright (C) 1999-2014 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 (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 (current-time))))
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 (current-time))
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 (when (and (< remainder 0)
652 (not (and timeclock-day-over
653 (equal timeclock-day-over
654 (timeclock-time-to-date
655 (current-time))))))
656 (setq timeclock-day-over
657 (timeclock-time-to-date (current-time)))
658 (run-hooks 'timeclock-day-over-hook))
659 (setq timeclock-mode-string
660 (propertize
661 (format " %c%s%c "
662 (if last-in ?< ?[)
663 (timeclock-seconds-to-string remainder nil t)
664 (if last-in ?> ?]))
665 'help-echo "timeclock: time remaining"))))
666
667 (put 'timeclock-mode-string 'risky-local-variable t)
668
669 (defun timeclock-log (code &optional project)
670 "Log the event CODE to the timeclock log, at the time of call.
671 If PROJECT is a string, it represents the project which the event is
672 being logged for. Normally only \"in\" events specify a project."
673 (let ((extant-timelog (find-buffer-visiting timeclock-file)))
674 (with-current-buffer (find-file-noselect timeclock-file t)
675 (save-excursion
676 (save-restriction
677 (widen)
678 (goto-char (point-max))
679 (if (not (bolp))
680 (insert "\n"))
681 (let ((now (current-time)))
682 (insert code " "
683 (format-time-string "%Y/%m/%d %H:%M:%S" now)
684 (or (and (stringp project)
685 (> (length project) 0)
686 (concat " " project))
687 "")
688 "\n")
689 (if (equal (downcase code) "o")
690 (setq timeclock-last-period
691 (- (timeclock-time-to-seconds now)
692 (timeclock-time-to-seconds
693 (cadr timeclock-last-event)))
694 timeclock-discrepancy
695 (+ timeclock-discrepancy
696 timeclock-last-period)))
697 (setq timeclock-last-event (list code now project)))))
698 (save-buffer)
699 (unless extant-timelog (kill-buffer (current-buffer)))))
700 (run-hooks 'timeclock-event-hook))
701
702 (defvar timeclock-moment-regexp
703 (concat "\\([bhioO]\\)\\s-+"
704 "\\([0-9]+\\)/\\([0-9]+\\)/\\([0-9]+\\)\\s-+"
705 "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)[ \t]*" "\\([^\n]*\\)"))
706
707 (defsubst timeclock-read-moment ()
708 "Read the moment under point from the timelog."
709 (if (looking-at timeclock-moment-regexp)
710 (let ((code (match-string 1))
711 (year (string-to-number (match-string 2)))
712 (mon (string-to-number (match-string 3)))
713 (mday (string-to-number (match-string 4)))
714 (hour (string-to-number (match-string 5)))
715 (min (string-to-number (match-string 6)))
716 (sec (string-to-number (match-string 7)))
717 (project (match-string 8)))
718 (list code (encode-time sec min hour mday mon year) project))))
719
720 (defun timeclock-last-period (&optional moment)
721 "Return the value of the last event period.
722 If the last event was a clock-in, the period will be open ended, and
723 growing every second. Otherwise, it is a fixed amount which has been
724 recorded to disk. If MOMENT is non-nil, use that as the current time.
725 This is only provided for coherency when used by
726 `timeclock-discrepancy'."
727 (if (equal (car timeclock-last-event) "i")
728 (- (timeclock-time-to-seconds (or moment (current-time)))
729 (timeclock-time-to-seconds
730 (cadr timeclock-last-event)))
731 timeclock-last-period))
732
733 (defsubst timeclock-entry-length (entry)
734 "Return the length of ENTRY in seconds."
735 (- (timeclock-time-to-seconds (cadr entry))
736 (timeclock-time-to-seconds (car entry))))
737
738 (defsubst timeclock-entry-begin (entry)
739 "Return the start time of ENTRY."
740 (car entry))
741
742 (defsubst timeclock-entry-end (entry)
743 "Return the end time of ENTRY."
744 (cadr entry))
745
746 (defsubst timeclock-entry-project (entry)
747 "Return the project of ENTRY."
748 (nth 2 entry))
749
750 (defsubst timeclock-entry-comment (entry)
751 "Return the comment of ENTRY."
752 (nth 3 entry))
753
754 (defsubst timeclock-entry-list-length (entry-list)
755 "Return the total length of ENTRY-LIST in seconds."
756 (let ((length 0))
757 (dolist (entry entry-list)
758 (setq length (+ length (timeclock-entry-length entry))))
759 length))
760
761 (defsubst timeclock-entry-list-begin (entry-list)
762 "Return the start time of the first element of ENTRY-LIST."
763 (timeclock-entry-begin (car entry-list)))
764
765 (defsubst timeclock-entry-list-end (entry-list)
766 "Return the end time of the last element of ENTRY-LIST."
767 (timeclock-entry-end (car (last entry-list))))
768
769 (defsubst timeclock-entry-list-span (entry-list)
770 "Return the total time in seconds spanned by ENTRY-LIST."
771 (- (timeclock-time-to-seconds (timeclock-entry-list-end entry-list))
772 (timeclock-time-to-seconds (timeclock-entry-list-begin entry-list))))
773
774 (defsubst timeclock-entry-list-break (entry-list)
775 "Return the total break time (span - length) in ENTRY-LIST."
776 (- (timeclock-entry-list-span entry-list)
777 (timeclock-entry-list-length entry-list)))
778
779 (defsubst timeclock-entry-list-projects (entry-list)
780 "Return a list of all the projects in ENTRY-LIST."
781 (let (projects proj)
782 (dolist (entry entry-list)
783 (setq proj (timeclock-entry-project entry))
784 (if projects
785 (add-to-list 'projects proj)
786 (setq projects (list proj))))
787 projects))
788
789 (defsubst timeclock-day-required (day)
790 "Return the required length of DAY in seconds, default `timeclock-workday'."
791 (or (car day) timeclock-workday))
792
793 (defsubst timeclock-day-length (day)
794 "Return the actual length of DAY in seconds."
795 (timeclock-entry-list-length (cdr day)))
796
797 (defsubst timeclock-day-debt (day)
798 "Return the debt (required - actual) associated with DAY, in seconds."
799 (- (timeclock-day-required day)
800 (timeclock-day-length day)))
801
802 (defsubst timeclock-day-begin (day)
803 "Return the start time of DAY."
804 (timeclock-entry-list-begin (cdr day)))
805
806 (defsubst timeclock-day-end (day)
807 "Return the end time of DAY."
808 (timeclock-entry-list-end (cdr day)))
809
810 (defsubst timeclock-day-span (day)
811 "Return the span of DAY."
812 (timeclock-entry-list-span (cdr day)))
813
814 (defsubst timeclock-day-break (day)
815 "Return the total break time of DAY."
816 (timeclock-entry-list-break (cdr day)))
817
818 (defsubst timeclock-day-projects (day)
819 "Return a list of all the projects in DAY."
820 (timeclock-entry-list-projects (cddr day)))
821
822 (defun timeclock-day-list-template (func day-list)
823 "Template for summing the result of FUNC on each element of DAY-LIST."
824 (let ((length 0))
825 (dolist (day day-list)
826 (setq length (+ length (funcall func day))))
827 length))
828
829 (defun timeclock-day-list-required (day-list)
830 "Return total required length of DAY-LIST, in seconds."
831 (timeclock-day-list-template #'timeclock-day-required day-list))
832
833 (defun timeclock-day-list-length (day-list)
834 "Return actual length of DAY-LIST, in seconds."
835 (timeclock-day-list-template #'timeclock-day-length day-list))
836
837 (defun timeclock-day-list-debt (day-list)
838 "Return total debt (required - actual) of DAY-LIST."
839 (timeclock-day-list-template #'timeclock-day-debt day-list))
840
841 (defsubst timeclock-day-list-begin (day-list)
842 "Return the start time of DAY-LIST."
843 (timeclock-day-begin (car day-list)))
844
845 (defsubst timeclock-day-list-end (day-list)
846 "Return the end time of DAY-LIST."
847 (timeclock-day-end (car (last day-list))))
848
849 (defun timeclock-day-list-span (day-list)
850 "Return the span of DAY-LIST."
851 (timeclock-day-list-template #'timeclock-day-span day-list))
852
853 (defun timeclock-day-list-break (day-list)
854 "Return the total break of DAY-LIST."
855 (timeclock-day-list-template #'timeclock-day-break day-list))
856
857 (defun timeclock-day-list-projects (day-list)
858 "Return a list of all the projects in DAY-LIST."
859 (let (projects)
860 (dolist (day day-list)
861 (dolist (proj (timeclock-day-projects day))
862 (if projects
863 (add-to-list 'projects proj)
864 (setq projects (list proj)))))
865 projects))
866
867 (defsubst timeclock-current-debt (&optional log-data)
868 "Return the seconds debt from LOG-DATA, default `timeclock-log-data'."
869 (nth 0 (or log-data (timeclock-log-data))))
870
871 (defsubst timeclock-day-alist (&optional log-data)
872 "Return the date alist from LOG-DATA, default `timeclock-log-data'."
873 (nth 1 (or log-data (timeclock-log-data))))
874
875 (defun timeclock-day-list (&optional log-data)
876 "Return a list of the cdrs of the date alist from LOG-DATA."
877 (let (day-list)
878 (dolist (date-list (timeclock-day-alist log-data))
879 (setq day-list (cons (cdr date-list) day-list)))
880 day-list))
881
882 (defsubst timeclock-project-alist (&optional log-data)
883 "Return the project alist from LOG-DATA, default `timeclock-log-data'."
884 (nth 2 (or log-data (timeclock-log-data))))
885
886 (defun timeclock-log-data (&optional recent-only filename)
887 "Return the contents of the timelog file, in a useful format.
888 If the optional argument RECENT-ONLY is non-nil, only show the contents
889 from the last point where the time debt (see below) was set.
890 If the optional argument FILENAME is non-nil, it is used instead of
891 the file specified by `timeclock-file.'
892
893 A timelog contains data in the form of a single entry per line.
894 Each entry has the form:
895
896 CODE YYYY/MM/DD HH:MM:SS [COMMENT]
897
898 CODE is one of: b, h, i, o or O. COMMENT is optional when the code is
899 i, o or O. The meanings of the codes are:
900
901 b Set the current time balance, or \"time debt\". Useful when
902 archiving old log data, when a debt must be carried forward.
903 The COMMENT here is the number of seconds of debt.
904
905 h Set the required working time for the given day. This must
906 be the first entry for that day. The COMMENT in this case is
907 the number of hours in this workday. Floating point amounts
908 are allowed.
909
910 i Clock in. The COMMENT in this case should be the name of the
911 project worked on.
912
913 o Clock out. COMMENT is unnecessary, but can be used to provide
914 a description of how the period went, for example.
915
916 O Final clock out. Whatever project was being worked on, it is
917 now finished. Useful for creating summary reports.
918
919 When this function is called, it will return a data structure with the
920 following format:
921
922 (DEBT ENTRIES-BY-DAY ENTRIES-BY-PROJECT)
923
924 DEBT is a floating point number representing the number of seconds
925 \"owed\" before any work was done. For a new file (one without a 'b'
926 entry), this is always zero.
927
928 The two entries lists have similar formats. They are both alists,
929 where the CAR is the index, and the CDR is a list of time entries.
930 For ENTRIES-BY-DAY, the CAR is a textual date string, of the form
931 YYYY/MM/DD. For ENTRIES-BY-PROJECT, it is the name of the project
932 worked on, or t for the default project.
933
934 The CDR for ENTRIES-BY-DAY is slightly different than for
935 ENTRIES-BY-PROJECT. It has the following form:
936
937 (DAY-LENGTH TIME-ENTRIES...)
938
939 For ENTRIES-BY-PROJECT, there is no DAY-LENGTH member. It is simply a
940 list of TIME-ENTRIES. Note that if DAY-LENGTH is nil, it means
941 whatever is the default should be used.
942
943 A TIME-ENTRY is a recorded time interval. It has the following format
944 \(although generally one does not have to manipulate these entries
945 directly; see below):
946
947 (BEGIN-TIME END-TIME PROJECT [COMMENT] [FINAL-P])
948
949 Anyway, suffice it to say there are a lot of structures. Typically
950 the user is expected to manipulate to the day(s) or project(s) that he
951 or she wants, at which point the following helper functions may be
952 used:
953
954 timeclock-day-required
955 timeclock-day-length
956 timeclock-day-debt
957 timeclock-day-begin
958 timeclock-day-end
959 timeclock-day-span
960 timeclock-day-break
961 timeclock-day-projects
962
963 timeclock-day-list-required
964 timeclock-day-list-length
965 timeclock-day-list-debt
966 timeclock-day-list-begin
967 timeclock-day-list-end
968 timeclock-day-list-span
969 timeclock-day-list-break
970 timeclock-day-list-projects
971
972 timeclock-entry-length
973 timeclock-entry-begin
974 timeclock-entry-end
975 timeclock-entry-project
976 timeclock-entry-comment
977
978 timeclock-entry-list-length
979 timeclock-entry-list-begin
980 timeclock-entry-list-end
981 timeclock-entry-list-span
982 timeclock-entry-list-break
983 timeclock-entry-list-projects
984
985 A few comments should make the use of the above functions obvious:
986
987 `required' is the amount of time that must be spent during a day, or
988 sequence of days, in order to have no debt.
989
990 `length' is the actual amount of time that was spent.
991
992 `debt' is the difference between required time and length. A
993 negative debt signifies overtime.
994
995 `begin' is the earliest moment at which work began.
996
997 `end' is the final moment work was done.
998
999 `span' is the difference between begin and end.
1000
1001 `break' is the difference between span and length.
1002
1003 `project' is the project that was worked on, and `projects' is a
1004 list of all the projects that were worked on during a given period.
1005
1006 `comment', where it applies, could mean anything.
1007
1008 There are a few more functions available, for locating day and entry
1009 lists:
1010
1011 timeclock-day-alist LOG-DATA
1012 timeclock-project-alist LOG-DATA
1013 timeclock-current-debt LOG-DATA
1014
1015 See the documentation for the given function if more info is needed."
1016 (let ((log-data (list 0.0 nil nil))
1017 (now (current-time))
1018 last-date-limited last-date-seconds last-date
1019 (line 0) last beg day entry event)
1020 (with-temp-buffer
1021 (insert-file-contents (or filename timeclock-file))
1022 (when recent-only
1023 (goto-char (point-max))
1024 (unless (re-search-backward "^b\\s-+" nil t)
1025 (goto-char (point-min))))
1026 (while (or (setq event (timeclock-read-moment))
1027 (and beg (not last)
1028 (setq last t event (list "o" now))))
1029 (setq line (1+ line))
1030 (cond ((equal (car event) "b")
1031 (setcar log-data (string-to-number (nth 2 event))))
1032 ((equal (car event) "h")
1033 (setq last-date-limited (timeclock-time-to-date (cadr event))
1034 last-date-seconds (* (string-to-number (nth 2 event))
1035 3600.0)))
1036 ((equal (car event) "i")
1037 (if beg
1038 (error "Error in format of timelog file, line %d" line)
1039 (setq beg t))
1040 (setq entry (list (cadr event) nil
1041 (and (> (length (nth 2 event)) 0)
1042 (nth 2 event))))
1043 (let ((date (timeclock-time-to-date (cadr event))))
1044 (if (and last-date
1045 (not (equal date last-date)))
1046 (progn
1047 (setcar (cdr log-data)
1048 (cons (cons last-date day)
1049 (cadr log-data)))
1050 (setq day (list (and last-date-limited
1051 last-date-seconds))))
1052 (unless day
1053 (setq day (list (and last-date-limited
1054 last-date-seconds)))))
1055 (setq last-date date
1056 last-date-limited nil)))
1057 ((equal (downcase (car event)) "o")
1058 (if (not beg)
1059 (error "Error in format of timelog file, line %d" line)
1060 (setq beg nil))
1061 (setcar (cdr entry) (cadr event))
1062 (let ((desc (and (> (length (nth 2 event)) 0)
1063 (nth 2 event))))
1064 (if desc
1065 (nconc entry (list (nth 2 event))))
1066 (if (equal (car event) "O")
1067 (nconc entry (if desc
1068 (list t)
1069 (list nil t))))
1070 (nconc day (list entry))
1071 (setq desc (nth 2 entry))
1072 (let ((proj (assoc desc (nth 2 log-data))))
1073 (if (null proj)
1074 (setcar (cddr log-data)
1075 (cons (cons desc (list entry))
1076 (nth 2 log-data)))
1077 (nconc (cdr proj) (list entry)))))))
1078 (forward-line))
1079 (if day
1080 (setcar (cdr log-data)
1081 (cons (cons last-date day)
1082 (cadr log-data))))
1083 log-data)))
1084
1085 (defun timeclock-find-discrep ()
1086 "Calculate time discrepancies, in seconds.
1087 The result is a three element list, containing the total time
1088 discrepancy, today's discrepancy, and the time worked today."
1089 ;; This is not implemented in terms of the functions above, because
1090 ;; it's a bit wasteful to read all of that data in, just to throw
1091 ;; away more than 90% of the information afterwards.
1092 ;;
1093 ;; If it were implemented using those functions, it would look
1094 ;; something like this:
1095 ;; (let ((days (timeclock-day-alist (timeclock-log-data)))
1096 ;; (total 0.0))
1097 ;; (while days
1098 ;; (setq total (+ total (- (timeclock-day-length (cdar days))
1099 ;; (timeclock-day-required (cdar days))))
1100 ;; days (cdr days)))
1101 ;; total)
1102 (let* ((now (current-time))
1103 (todays-date (timeclock-time-to-date now))
1104 (first t) (accum 0) (elapsed 0)
1105 event beg last-date
1106 last-date-limited last-date-seconds)
1107 (unless timeclock-discrepancy
1108 (when (file-readable-p timeclock-file)
1109 (setq timeclock-project-list nil
1110 timeclock-last-project nil
1111 timeclock-reason-list nil
1112 timeclock-elapsed 0)
1113 (with-temp-buffer
1114 (insert-file-contents timeclock-file)
1115 (goto-char (point-max))
1116 (unless (re-search-backward "^b\\s-+" nil t)
1117 (goto-char (point-min)))
1118 (while (setq event (timeclock-read-moment))
1119 (cond ((equal (car event) "b")
1120 (setq accum (string-to-number (nth 2 event))))
1121 ((equal (car event) "h")
1122 (setq last-date-limited
1123 (timeclock-time-to-date (cadr event))
1124 last-date-seconds
1125 (* (string-to-number (nth 2 event)) 3600.0)))
1126 ((equal (car event) "i")
1127 (when (and (nth 2 event)
1128 (> (length (nth 2 event)) 0))
1129 (add-to-list 'timeclock-project-list (nth 2 event))
1130 (setq timeclock-last-project (nth 2 event)))
1131 (let ((date (timeclock-time-to-date (cadr event))))
1132 (if (if last-date
1133 (not (equal date last-date))
1134 first)
1135 (setq first nil
1136 accum (- accum (if last-date-limited
1137 last-date-seconds
1138 timeclock-workday))))
1139 (setq last-date date
1140 last-date-limited nil)
1141 (if beg
1142 (error "Error in format of timelog file!")
1143 (setq beg (timeclock-time-to-seconds (cadr event))))))
1144 ((equal (downcase (car event)) "o")
1145 (if (and (nth 2 event)
1146 (> (length (nth 2 event)) 0))
1147 (add-to-list 'timeclock-reason-list (nth 2 event)))
1148 (if (not beg)
1149 (error "Error in format of timelog file!")
1150 (setq timeclock-last-period
1151 (- (timeclock-time-to-seconds (cadr event)) beg)
1152 accum (+ timeclock-last-period accum)
1153 beg nil))
1154 (if (equal last-date todays-date)
1155 (setq timeclock-elapsed
1156 (+ timeclock-last-period timeclock-elapsed)))))
1157 (setq timeclock-last-event event
1158 timeclock-last-event-workday
1159 (if (equal (timeclock-time-to-date now) last-date-limited)
1160 last-date-seconds
1161 timeclock-workday))
1162 (forward-line))
1163 (setq timeclock-discrepancy accum))))
1164 (unless timeclock-last-event-workday
1165 (setq timeclock-last-event-workday timeclock-workday))
1166 (setq accum (or timeclock-discrepancy 0)
1167 elapsed (or timeclock-elapsed elapsed))
1168 (if timeclock-last-event
1169 (if (equal (car timeclock-last-event) "i")
1170 (let ((last-period (timeclock-last-period now)))
1171 (setq accum (+ accum last-period)
1172 elapsed (+ elapsed last-period)))
1173 (if (not (equal (timeclock-time-to-date
1174 (cadr timeclock-last-event))
1175 (timeclock-time-to-date now)))
1176 (setq accum (- accum timeclock-last-event-workday)))))
1177 (list accum (- elapsed timeclock-last-event-workday)
1178 elapsed)))
1179
1180 ;;; A reporting function that uses timeclock-log-data
1181
1182 (defun timeclock-day-base (&optional time)
1183 "Given a time within a day, return 0:0:0 within that day.
1184 If optional argument TIME is non-nil, use that instead of the current time."
1185 (let ((decoded (decode-time (or time (current-time)))))
1186 (setcar (nthcdr 0 decoded) 0)
1187 (setcar (nthcdr 1 decoded) 0)
1188 (setcar (nthcdr 2 decoded) 0)
1189 (apply 'encode-time decoded)))
1190
1191 (defun timeclock-mean (l)
1192 "Compute the arithmetic mean of the values in the list L."
1193 (let ((total 0)
1194 (count 0))
1195 (dolist (thisl l)
1196 (setq total (+ total thisl)
1197 count (1+ count)))
1198 (if (zerop count)
1199 0
1200 (/ total count))))
1201
1202 (defun timeclock-generate-report (&optional html-p)
1203 "Generate a summary report based on the current timelog file.
1204 By default, the report is in plain text, but if the optional argument
1205 HTML-P is non-nil, HTML markup is added."
1206 (interactive "P")
1207 (let ((log (timeclock-log-data))
1208 (today (timeclock-day-base)))
1209 (if html-p (insert "<p>"))
1210 (insert "Currently ")
1211 (let ((project (nth 2 timeclock-last-event))
1212 (begin (nth 1 timeclock-last-event))
1213 done)
1214 (if (timeclock-currently-in-p)
1215 (insert "IN")
1216 (if (zerop (length project))
1217 (progn (insert "Done Working Today")
1218 (setq done t))
1219 (insert "OUT")))
1220 (unless done
1221 (insert " since " (format-time-string "%Y/%m/%d %-I:%M %p" begin))
1222 (if html-p
1223 (insert "<br>\n<b>")
1224 (insert "\n*"))
1225 (if (timeclock-currently-in-p)
1226 (insert "Working on "))
1227 (if html-p
1228 (insert project "</b><br>\n")
1229 (insert project "*\n"))
1230 (let ((proj-data (cdr (assoc project (timeclock-project-alist log))))
1231 (two-weeks-ago (timeclock-seconds-to-time
1232 (- (timeclock-time-to-seconds today)
1233 (* 2 7 24 60 60))))
1234 two-week-len today-len)
1235 (while proj-data
1236 (if (not (time-less-p
1237 (timeclock-entry-begin (car proj-data)) today))
1238 (setq today-len (timeclock-entry-list-length proj-data)
1239 proj-data nil)
1240 (if (and (null two-week-len)
1241 (not (time-less-p
1242 (timeclock-entry-begin (car proj-data))
1243 two-weeks-ago)))
1244 (setq two-week-len (timeclock-entry-list-length proj-data)))
1245 (setq proj-data (cdr proj-data))))
1246 (if (null two-week-len)
1247 (setq two-week-len today-len))
1248 (if html-p (insert "<p>"))
1249 (if today-len
1250 (insert "\nTime spent on this task today: "
1251 (timeclock-seconds-to-string today-len)
1252 ". In the last two weeks: "
1253 (timeclock-seconds-to-string two-week-len))
1254 (if two-week-len
1255 (insert "\nTime spent on this task in the last two weeks: "
1256 (timeclock-seconds-to-string two-week-len))))
1257 (if html-p (insert "<br>"))
1258 (insert "\n"
1259 (timeclock-seconds-to-string (timeclock-workday-elapsed))
1260 " worked today, "
1261 (timeclock-seconds-to-string (timeclock-workday-remaining))
1262 " remaining, done at "
1263 (timeclock-when-to-leave-string) "\n")))
1264 (if html-p (insert "<p>"))
1265 (insert "\nThere have been "
1266 (number-to-string
1267 (length (timeclock-day-alist log)))
1268 " days of activity, starting "
1269 (caar (last (timeclock-day-alist log))))
1270 (if html-p (insert "</p>"))
1271 (when html-p
1272 (insert "<p>
1273 <table>
1274 <td width=\"25\"><br></td><td>
1275 <table border=1 cellpadding=3>
1276 <tr><th><i>Statistics</i></th>
1277 <th>Entire</th>
1278 <th>-30 days</th>
1279 <th>-3 mons</th>
1280 <th>-6 mons</th>
1281 <th>-1 year</th>
1282 </tr>")
1283 (let* ((day-list (timeclock-day-list))
1284 (thirty-days-ago (timeclock-seconds-to-time
1285 (- (timeclock-time-to-seconds today)
1286 (* 30 24 60 60))))
1287 (three-months-ago (timeclock-seconds-to-time
1288 (- (timeclock-time-to-seconds today)
1289 (* 90 24 60 60))))
1290 (six-months-ago (timeclock-seconds-to-time
1291 (- (timeclock-time-to-seconds today)
1292 (* 180 24 60 60))))
1293 (one-year-ago (timeclock-seconds-to-time
1294 (- (timeclock-time-to-seconds today)
1295 (* 365 24 60 60))))
1296 (time-in (vector (list t) (list t) (list t) (list t) (list t)))
1297 (time-out (vector (list t) (list t) (list t) (list t) (list t)))
1298 (breaks (vector (list t) (list t) (list t) (list t) (list t)))
1299 (workday (vector (list t) (list t) (list t) (list t) (list t)))
1300 (lengths (vector '(0 0) thirty-days-ago three-months-ago
1301 six-months-ago one-year-ago)))
1302 ;; collect statistics from complete timelog
1303 (dolist (day day-list)
1304 (let ((i 0) (l 5))
1305 (while (< i l)
1306 (unless (time-less-p
1307 (timeclock-day-begin day)
1308 (aref lengths i))
1309 (let ((base (timeclock-time-to-seconds
1310 (timeclock-day-base
1311 (timeclock-day-begin day)))))
1312 (nconc (aref time-in i)
1313 (list (- (timeclock-time-to-seconds
1314 (timeclock-day-begin day))
1315 base)))
1316 (let ((span (timeclock-day-span day))
1317 (len (timeclock-day-length day))
1318 (req (timeclock-day-required day)))
1319 ;; If the day's actual work length is less than
1320 ;; 70% of its span, then likely the exit time
1321 ;; and break amount are not worthwhile adding to
1322 ;; the statistic
1323 (when (and (> span 0)
1324 (> (/ (float len) (float span)) 0.70))
1325 (nconc (aref time-out i)
1326 (list (- (timeclock-time-to-seconds
1327 (timeclock-day-end day))
1328 base)))
1329 (nconc (aref breaks i) (list (- span len))))
1330 (if req
1331 (setq len (+ len (- timeclock-workday req))))
1332 (nconc (aref workday i) (list len)))))
1333 (setq i (1+ i)))))
1334 ;; average statistics
1335 (let ((i 0) (l 5))
1336 (while (< i l)
1337 (aset time-in i (timeclock-mean (cdr (aref time-in i))))
1338 (aset time-out i (timeclock-mean (cdr (aref time-out i))))
1339 (aset breaks i (timeclock-mean (cdr (aref breaks i))))
1340 (aset workday i (timeclock-mean (cdr (aref workday i))))
1341 (setq i (1+ i))))
1342 ;; Output the HTML table
1343 (insert "<tr>\n")
1344 (insert "<td align=\"center\">Time in</td>\n")
1345 (let ((i 0) (l 5))
1346 (while (< i l)
1347 (insert "<td align=\"right\">"
1348 (timeclock-seconds-to-string (aref time-in i))
1349 "</td>\n")
1350 (setq i (1+ i))))
1351 (insert "</tr>\n")
1352
1353 (insert "<tr>\n")
1354 (insert "<td align=\"center\">Time out</td>\n")
1355 (let ((i 0) (l 5))
1356 (while (< i l)
1357 (insert "<td align=\"right\">"
1358 (timeclock-seconds-to-string (aref time-out i))
1359 "</td>\n")
1360 (setq i (1+ i))))
1361 (insert "</tr>\n")
1362
1363 (insert "<tr>\n")
1364 (insert "<td align=\"center\">Break</td>\n")
1365 (let ((i 0) (l 5))
1366 (while (< i l)
1367 (insert "<td align=\"right\">"
1368 (timeclock-seconds-to-string (aref breaks i))
1369 "</td>\n")
1370 (setq i (1+ i))))
1371 (insert "</tr>\n")
1372
1373 (insert "<tr>\n")
1374 (insert "<td align=\"center\">Workday</td>\n")
1375 (let ((i 0) (l 5))
1376 (while (< i l)
1377 (insert "<td align=\"right\">"
1378 (timeclock-seconds-to-string (aref workday i))
1379 "</td>\n")
1380 (setq i (1+ i))))
1381 (insert "</tr>\n"))
1382 (insert "<tfoot>
1383 <td colspan=\"6\" align=\"center\">
1384 <i>These are approximate figures</i></td>
1385 </tfoot>
1386 </table>
1387 </td></table>")))))
1388
1389 ;;; A helpful little function
1390
1391 (defun timeclock-visit-timelog ()
1392 "Open the file named by `timeclock-file' in another window."
1393 (interactive)
1394 (find-file-other-window timeclock-file))
1395
1396 (provide 'timeclock)
1397
1398 (run-hooks 'timeclock-load-hook)
1399
1400 ;; make sure we know the list of reasons, projects, and have computed
1401 ;; the last event and current discrepancy.
1402 (if (file-readable-p timeclock-file)
1403 (timeclock-reread-log))
1404
1405 ;;; timeclock.el ends here