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