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