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