Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; appt.el --- appointment notification functions |
c0274f38 | 2 | |
f3e7c0dc | 3 | ;; Copyright (C) 1989, 1990, 1994, 1998 Free Software Foundation, Inc. |
3a801d0c | 4 | |
e5167999 | 5 | ;; Author: Neil Mager <neilm@juliet.ll.mit.edu> |
0dba5606 | 6 | ;; Maintainer: FSF |
e5167999 ER |
7 | ;; Keywords: calendar |
8 | ||
902a0e3c JB |
9 | ;; This file is part of GNU Emacs. |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
e5167999 | 13 | ;; the Free Software Foundation; either version 2, or (at your option) |
902a0e3c JB |
14 | ;; any later version. |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
902a0e3c | 25 | |
e5167999 ER |
26 | ;;; Commentary: |
27 | ||
902a0e3c JB |
28 | ;; |
29 | ;; appt.el - visible and/or audible notification of | |
f3e7c0dc | 30 | ;; appointments from ~/diary file. |
902a0e3c | 31 | ;; |
902a0e3c | 32 | ;;; |
d073fa5b | 33 | ;;; Thanks to Edward M. Reingold for much help and many suggestions, |
902a0e3c JB |
34 | ;;; And to many others for bug fixes and suggestions. |
35 | ;;; | |
36 | ;;; | |
37 | ;;; This functions in this file will alert the user of a | |
38 | ;;; pending appointment based on their diary file. | |
39 | ;;; | |
f3e7c0dc KH |
40 | ;;; A message will be displayed in the mode line of the Emacs buffer |
41 | ;;; and (if you request) the terminal will beep and display a message | |
42 | ;;; from the diary in the mini-buffer, or you can choose to | |
902a0e3c JB |
43 | ;;; have a message displayed in a new buffer. |
44 | ;;; | |
f3e7c0dc | 45 | ;;; The variable `appt-message-warning-time' allows the |
902a0e3c | 46 | ;;; user to specify how much notice they want before the appointment. The |
f3e7c0dc | 47 | ;;; variable `appt-issue-message' specifies whether the user wants |
902a0e3c JB |
48 | ;;; to to be notified of a pending appointment. |
49 | ;;; | |
f3e7c0dc KH |
50 | ;;; In order to use the appt package, you only need |
51 | ;;; to load it---provided you have appointments. | |
902a0e3c | 52 | ;;; |
efa434d9 RS |
53 | ;;; Before that, you can also set some options if you want |
54 | ;;; (setq view-diary-entries-initially t) | |
55 | ;;; (setq appt-issue-message t) | |
902a0e3c JB |
56 | ;;; |
57 | ;;; This is an example of what can be in your diary file: | |
58 | ;;; Monday | |
59 | ;;; 9:30am Coffee break | |
60 | ;;; 12:00pm Lunch | |
61 | ;;; | |
62 | ;;; Based upon the above lines in your .emacs and diary files, | |
63 | ;;; the calendar and diary will be displayed when you enter | |
f3e7c0dc | 64 | ;;; Emacs and your appointments list will automatically be created. |
902a0e3c JB |
65 | ;;; You will then be reminded at 9:20am about your coffee break |
66 | ;;; and at 11:50am to go to lunch. | |
67 | ;;; | |
68 | ;;; Use describe-function on appt-check for a description of other variables | |
69 | ;;; that can be used to personalize the notification system. | |
70 | ;;; | |
71 | ;;; In order to add or delete items from todays list, use appt-add | |
72 | ;;; and appt-delete. | |
73 | ;;; | |
74 | ;;; Additionally, the appointments list is recreated automatically | |
75 | ;;; at 12:01am for those who do not logout every day or are programming | |
76 | ;;; late. | |
77 | ;;; | |
d073fa5b | 78 | ;;; Brief internal description - Skip this if you are not interested! |
902a0e3c | 79 | ;;; |
902a0e3c JB |
80 | ;;; The function appt-make-list creates the appointments list which appt-check |
81 | ;;; reads. This is all done automatically. | |
82 | ;;; It is invoked from the function list-diary-entries. | |
83 | ;;; | |
5b586155 RS |
84 | ;;; You can change the way the appointment window is created/deleted by |
85 | ;;; setting the variables | |
86 | ;;; | |
87 | ;;; appt-disp-window-function | |
88 | ;;; and | |
89 | ;;; appt-delete-window-function | |
90 | ;;; | |
91 | ;;; For instance, these variables can be set to functions that display | |
92 | ;;; appointments in pop-up frames, which are lowered or iconified after | |
f3e7c0dc | 93 | ;;; appt-display-interval minutes. |
5b586155 | 94 | ;;; |
e5167999 ER |
95 | |
96 | ;;; Code: | |
97 | ||
6afadb57 RS |
98 | ;; Make sure calendar is loaded when we compile this. |
99 | (require 'calendar) | |
100 | ||
cc3511de KH |
101 | (provide 'appt) |
102 | ||
7e1dae73 | 103 | ;;;###autoload |
8db540c5 | 104 | (defcustom appt-issue-message t |
902a0e3c JB |
105 | "*Non-nil means check for appointments in the diary buffer. |
106 | To be detected, the diary entry must have the time | |
8db540c5 RS |
107 | as the first thing on a line." |
108 | :type 'boolean | |
109 | :group 'appt) | |
902a0e3c | 110 | |
7e1dae73 | 111 | ;;;###autoload |
8db540c5 RS |
112 | (defcustom appt-message-warning-time 12 |
113 | "*Time in minutes before an appointment that the warning begins." | |
114 | :type 'integer | |
115 | :group 'appt) | |
902a0e3c | 116 | |
7e1dae73 | 117 | ;;;###autoload |
8db540c5 RS |
118 | (defcustom appt-audible t |
119 | "*Non-nil means beep to indicate appointment." | |
120 | :type 'boolean | |
121 | :group 'appt) | |
902a0e3c | 122 | |
7e1dae73 | 123 | ;;;###autoload |
8db540c5 RS |
124 | (defcustom appt-visible t |
125 | "*Non-nil means display appointment message in echo area." | |
126 | :type 'boolean | |
127 | :group 'appt) | |
902a0e3c | 128 | |
7e1dae73 | 129 | ;;;###autoload |
8db540c5 RS |
130 | (defcustom appt-display-mode-line t |
131 | "*Non-nil means display minutes to appointment and time on the mode line." | |
132 | :type 'boolean | |
133 | :group 'appt) | |
902a0e3c | 134 | |
7e1dae73 | 135 | ;;;###autoload |
8db540c5 RS |
136 | (defcustom appt-msg-window t |
137 | "*Non-nil means display appointment message in another window." | |
138 | :type 'boolean | |
139 | :group 'appt) | |
902a0e3c | 140 | |
7e1dae73 | 141 | ;;;###autoload |
8db540c5 RS |
142 | (defcustom appt-display-duration 10 |
143 | "*The number of seconds an appointment message is displayed." | |
144 | :type 'integer | |
145 | :group 'appt) | |
902a0e3c | 146 | |
7e1dae73 | 147 | ;;;###autoload |
8db540c5 | 148 | (defcustom appt-display-diary t |
902a0e3c | 149 | "*Non-nil means to display the next days diary on the screen. |
8db540c5 RS |
150 | This will occur at midnight when the appointment list is updated." |
151 | :type 'boolean | |
152 | :group 'appt) | |
902a0e3c JB |
153 | |
154 | (defvar appt-time-msg-list nil | |
155 | "The list of appointments for today. | |
156 | Use `appt-add' and `appt-delete' to add and delete appointments from list. | |
157 | The original list is generated from the today's `diary-entries-list'. | |
158 | The number before each time/message is the time in minutes from midnight.") | |
159 | ||
f3e7c0dc | 160 | (defconst appt-max-time 1439 |
902a0e3c JB |
161 | "11:59pm in minutes - number of minutes in a day minus 1.") |
162 | ||
8db540c5 RS |
163 | (defcustom appt-display-interval 3 |
164 | "*Number of minutes to wait between checking the appointment list." | |
165 | :type 'integer | |
166 | :group 'appt) | |
5b586155 RS |
167 | |
168 | (defvar appt-buffer-name " *appt-buf*" | |
169 | "Name of the appointments buffer.") | |
170 | ||
171 | (defvar appt-disp-window-function 'appt-disp-window | |
172 | "Function called to display appointment window.") | |
173 | ||
174 | (defvar appt-delete-window-function 'appt-delete-window | |
175 | "Function called to remove appointment window and buffer.") | |
b570e652 | 176 | |
efa434d9 | 177 | (defvar appt-mode-string nil |
f3e7c0dc KH |
178 | "String being displayed in the mode line saying you have an appointment. |
179 | The actual string includes the amount of time till the appointment.") | |
180 | ||
181 | (defvar appt-prev-comp-time nil | |
182 | "Time of day (mins since midnight) at which we last checked appointments.") | |
183 | ||
184 | (defvar appt-now-displayed nil | |
185 | "Non-nil when we have started notifying about a appointment that is near.") | |
186 | ||
187 | (defvar appt-display-count nil) | |
efa434d9 | 188 | |
902a0e3c JB |
189 | (defun appt-check () |
190 | "Check for an appointment and update the mode line. | |
191 | Note: the time must be the first thing in the line in the diary | |
192 | for a warning to be issued. | |
193 | ||
194 | The format of the time can be either 24 hour or am/pm. | |
195 | Example: | |
196 | ||
197 | 02/23/89 | |
198 | 18:00 Dinner | |
199 | ||
200 | Thursday | |
201 | 11:45am Lunch meeting. | |
202 | ||
f3e7c0dc KH |
203 | Appointments are checked every `appt-display-interval' minutes. |
204 | The following variables control appointment notification: | |
902a0e3c | 205 | |
efa434d9 RS |
206 | `appt-issue-message' |
207 | If t, the diary buffer is checked for appointments. | |
902a0e3c | 208 | |
efa434d9 | 209 | `appt-message-warning-time' |
b570e652 RS |
210 | Variable used to determine if appointment message |
211 | should be displayed. | |
902a0e3c | 212 | |
efa434d9 | 213 | `appt-audible' |
b570e652 RS |
214 | Variable used to determine if appointment is audible. |
215 | Default is t. | |
902a0e3c | 216 | |
efa434d9 | 217 | `appt-visible' |
b570e652 | 218 | Variable used to determine if appointment message should be |
efa434d9 | 219 | displayed in the mini-buffer. Default is t. |
902a0e3c | 220 | |
efa434d9 | 221 | `appt-msg-window' |
b570e652 | 222 | Variable used to determine if appointment message |
efa434d9 RS |
223 | should temporarily appear in another window. Mutually exclusive |
224 | to `appt-visible'. | |
902a0e3c | 225 | |
efa434d9 | 226 | `appt-display-duration' |
b570e652 RS |
227 | The number of seconds an appointment message |
228 | is displayed in another window. | |
229 | ||
f3e7c0dc | 230 | `appt-disp-window-function' |
efa434d9 | 231 | Function called to display appointment window. You can customize |
adae5323 RS |
232 | appt.el by setting this variable to a function different from the |
233 | one provided with this package. | |
234 | ||
f3e7c0dc | 235 | `appt-delete-window-function' |
adae5323 RS |
236 | Function called to remove appointment window and buffer. You can |
237 | customize appt.el by setting this variable to a function different | |
efa434d9 | 238 | from the one provided with this package." |
902a0e3c | 239 | |
f3e7c0dc KH |
240 | (let* ((min-to-app -1) |
241 | (new-time "") | |
242 | (prev-appt-mode-string appt-mode-string) | |
243 | (prev-appt-display-count (or appt-display-count 0)) | |
244 | ;; Non-nil means do a full check for pending appointments | |
245 | ;; and display in whatever ways the user has selected. | |
246 | ;; When no appointment is being displayed, | |
247 | ;; we always do a full check. | |
248 | (full-check | |
249 | (or (not appt-now-displayed) | |
250 | ;; This is true every appt-display-interval minutes. | |
251 | (= 0 (mod prev-appt-display-count appt-display-interval)))) | |
252 | ;; Non-nil means only update the interval displayed in the mode line. | |
253 | (mode-line-only | |
254 | (and (not full-check) appt-now-displayed))) | |
255 | ||
256 | (when (or full-check mode-line-only) | |
257 | (save-excursion | |
258 | ||
259 | ;; Get the current time and convert it to minutes | |
260 | ;; from midnight. ie. 12:01am = 1, midnight = 0. | |
902a0e3c | 261 | |
f3e7c0dc KH |
262 | (let* ((now (decode-time)) |
263 | (cur-hour (nth 2 now)) | |
264 | (cur-min (nth 1 now)) | |
265 | (cur-comp-time (+ (* cur-hour 60) cur-min))) | |
266 | ||
267 | ;; At the first check in any given day, update our | |
268 | ;; appointments to today's list. | |
269 | ||
270 | (if (or (null appt-prev-comp-time) | |
271 | (< cur-comp-time appt-prev-comp-time)) | |
272 | (condition-case nil | |
273 | (progn | |
274 | (if (and view-diary-entries-initially appt-display-diary) | |
275 | (diary) | |
276 | (let ((diary-display-hook 'appt-make-list)) | |
277 | (diary)))) | |
278 | (error nil))) | |
279 | (setq appt-prev-comp-time cur-comp-time) | |
280 | ||
281 | (setq appt-mode-string nil) | |
282 | (setq appt-display-count nil) | |
283 | ||
284 | ;; If there are entries in the list, and the | |
285 | ;; user wants a message issued, | |
286 | ;; get the first time off of the list | |
287 | ;; and calculate the number of minutes until the appointment. | |
288 | ||
289 | (if (and appt-issue-message appt-time-msg-list) | |
290 | (let ((appt-comp-time (car (car (car appt-time-msg-list))))) | |
291 | (setq min-to-app (- appt-comp-time cur-comp-time)) | |
292 | ||
293 | (while (and appt-time-msg-list | |
294 | (< appt-comp-time cur-comp-time)) | |
295 | (setq appt-time-msg-list (cdr appt-time-msg-list)) | |
296 | (if appt-time-msg-list | |
297 | (setq appt-comp-time | |
298 | (car (car (car appt-time-msg-list)))))) | |
299 | ||
300 | ;; If we have an appointment between midnight and | |
301 | ;; 'appt-message-warning-time' minutes after midnight, | |
302 | ;; we must begin to issue a message before midnight. | |
303 | ;; Midnight is considered 0 minutes and 11:59pm is | |
304 | ;; 1439 minutes. Therefore we must recalculate the minutes | |
305 | ;; to appointment variable. It is equal to the number of | |
306 | ;; minutes before midnight plus the number of | |
307 | ;; minutes after midnight our appointment is. | |
308 | ||
309 | (if (and (< appt-comp-time appt-message-warning-time) | |
310 | (> (+ cur-comp-time appt-message-warning-time) | |
311 | appt-max-time)) | |
312 | (setq min-to-app (+ (- (1+ appt-max-time) cur-comp-time)) | |
313 | appt-comp-time)) | |
314 | ||
315 | ;; issue warning if the appointment time is | |
316 | ;; within appt-message-warning time | |
317 | ||
318 | (when (and (<= min-to-app appt-message-warning-time) | |
319 | (>= min-to-app 0)) | |
320 | (setq appt-now-displayed t) | |
321 | (setq appt-display-count | |
322 | (1+ prev-appt-display-count)) | |
323 | (unless mode-line-only | |
efa434d9 RS |
324 | (if appt-msg-window |
325 | (progn | |
326 | (setq new-time (format-time-string "%a %b %e " | |
327 | (current-time))) | |
328 | (funcall | |
329 | appt-disp-window-function | |
d073fa5b | 330 | (number-to-string min-to-app) new-time |
efa434d9 | 331 | (car (cdr (car appt-time-msg-list)))) |
f3e7c0dc | 332 | |
efa434d9 RS |
333 | (run-at-time |
334 | (format "%d sec" appt-display-duration) | |
335 | nil | |
336 | appt-delete-window-function)) | |
f3e7c0dc | 337 | ;;; else |
b570e652 | 338 | |
efa434d9 RS |
339 | (if appt-visible |
340 | (message "%s" | |
341 | (car (cdr (car appt-time-msg-list))))) | |
b570e652 | 342 | |
efa434d9 | 343 | (if appt-audible |
f3e7c0dc KH |
344 | (beep 1)))) |
345 | ||
346 | (when appt-display-mode-line | |
347 | (setq appt-mode-string | |
d073fa5b DL |
348 | (concat " App't in " |
349 | (number-to-string min-to-app) | |
350 | " min. "))) | |
f3e7c0dc KH |
351 | |
352 | ;; When an appointment is reached, | |
353 | ;; delete it from the list. | |
354 | ;; Reset the count to 0 in case we display another | |
355 | ;; appointment on the next cycle. | |
356 | (if (= min-to-app 0) | |
357 | (setq appt-time-msg-list | |
358 | (cdr appt-time-msg-list) | |
359 | appt-display-count nil))))) | |
360 | ||
361 | ;; If we have changed the mode line string, | |
362 | ;; redisplay all mode lines. | |
363 | (and appt-display-mode-line | |
364 | (not (equal appt-mode-string | |
365 | prev-appt-mode-string)) | |
366 | (progn | |
367 | (force-mode-line-update t) | |
368 | ;; If the string now has a notification, | |
369 | ;; redisplay right now. | |
370 | (if appt-mode-string | |
371 | (sit-for 0))))))))) | |
902a0e3c JB |
372 | |
373 | ||
902a0e3c | 374 | (defun appt-disp-window (min-to-app new-time appt-msg) |
d073fa5b | 375 | "Display appointment message APPT-MSG in a separate buffer." |
902a0e3c | 376 | (require 'electric) |
5b586155 RS |
377 | |
378 | ;; Make sure we're not in the minibuffer | |
379 | ;; before splitting the window. | |
380 | ||
381 | (if (equal (selected-window) (minibuffer-window)) | |
382 | (if (other-window 1) | |
383 | (select-window (other-window 1)) | |
fbebec27 | 384 | (if (display-multi-frame-p) |
5b586155 RS |
385 | (select-frame (other-frame 1))))) |
386 | ||
387 | (let* ((this-buffer (current-buffer)) | |
388 | (this-window (selected-window)) | |
389 | (appt-disp-buf (set-buffer (get-buffer-create appt-buffer-name)))) | |
390 | ||
d5b22d88 RS |
391 | (if (cdr (assq 'unsplittable (frame-parameters))) |
392 | ;; In an unsplittable frame, use something somewhere else. | |
393 | (display-buffer appt-disp-buf) | |
058961dd RS |
394 | (unless (or (special-display-p (buffer-name appt-disp-buf)) |
395 | (same-window-p (buffer-name appt-disp-buf))) | |
396 | ;; By default, split the bottom window and use the lower part. | |
397 | (appt-select-lowest-window) | |
398 | (split-window)) | |
d5b22d88 | 399 | (pop-to-buffer appt-disp-buf)) |
5b586155 RS |
400 | (setq mode-line-format |
401 | (concat "-------------------- Appointment in " | |
402 | min-to-app " minutes. " new-time " %-")) | |
efa434d9 | 403 | (erase-buffer) |
5b586155 | 404 | (insert-string appt-msg) |
d5b22d88 | 405 | (shrink-window-if-larger-than-buffer (get-buffer-window appt-disp-buf t)) |
5b586155 | 406 | (set-buffer-modified-p nil) |
725ec4bc | 407 | (raise-frame (selected-frame)) |
5b586155 RS |
408 | (select-window this-window) |
409 | (if appt-audible | |
410 | (beep 1)))) | |
411 | ||
412 | (defun appt-delete-window () | |
413 | "Function called to undisplay appointment messages. | |
414 | Usually just deletes the appointment buffer." | |
95cdbff5 RS |
415 | (let ((window (get-buffer-window appt-buffer-name t))) |
416 | (and window | |
d073fa5b | 417 | (or (eq window (frame-root-window (window-frame window))) |
95cdbff5 | 418 | (delete-window window)))) |
5b586155 RS |
419 | (kill-buffer appt-buffer-name) |
420 | (if appt-audible | |
421 | (beep 1))) | |
902a0e3c | 422 | |
902a0e3c | 423 | (defun appt-select-lowest-window () |
d073fa5b | 424 | "Select the lowest window on the frame." |
7c0d9b89 GM |
425 | (let ((lowest-window (selected-window)) |
426 | (bottom-edge (nth 3 (window-edges)))) | |
427 | (walk-windows (lambda (w) | |
428 | (let ((next-bottom-edge (nth 3 (window-edges w)))) | |
429 | (when (< bottom-edge next-bottom-edge) | |
430 | (setq bottom-edge next-bottom-edge | |
431 | lowest-window w))))) | |
432 | (select-window lowest-window))) | |
902a0e3c | 433 | |
f3e7c0dc | 434 | ;;;###autoload |
902a0e3c | 435 | (defun appt-add (new-appt-time new-appt-msg) |
d073fa5b | 436 | "Add an appointment for the day at NEW-APPT-TIME and issue message NEW-APPT-MSG. |
902a0e3c JB |
437 | The time should be in either 24 hour format or am/pm format." |
438 | ||
439 | (interactive "sTime (hh:mm[am/pm]): \nsMessage: ") | |
440 | (if (string-match "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?" new-appt-time) | |
441 | nil | |
442 | (error "Unacceptable time-string")) | |
443 | ||
444 | (let* ((appt-time-string (concat new-appt-time " " new-appt-msg)) | |
445 | (appt-time (list (appt-convert-time new-appt-time))) | |
446 | (time-msg (cons appt-time (list appt-time-string)))) | |
d073fa5b | 447 | (setq appt-time-msg-list (nconc appt-time-msg-list (list time-msg))) |
902a0e3c JB |
448 | (setq appt-time-msg-list (appt-sort-list appt-time-msg-list)))) |
449 | ||
f3e7c0dc | 450 | ;;;###autoload |
902a0e3c JB |
451 | (defun appt-delete () |
452 | "Delete an appointment from the list of appointments." | |
453 | (interactive) | |
454 | (let* ((tmp-msg-list appt-time-msg-list)) | |
455 | (while tmp-msg-list | |
456 | (let* ((element (car tmp-msg-list)) | |
457 | (prompt-string (concat "Delete " | |
458 | (prin1-to-string (car (cdr element))) | |
459 | " from list? ")) | |
460 | (test-input (y-or-n-p prompt-string))) | |
461 | (setq tmp-msg-list (cdr tmp-msg-list)) | |
462 | (if test-input | |
95cdbff5 | 463 | (setq appt-time-msg-list (delq element appt-time-msg-list))))) |
85bbde63 | 464 | (appt-check) |
902a0e3c JB |
465 | (message ""))) |
466 | ||
467 | ||
ea04824c DL |
468 | (eval-when-compile (defvar number) |
469 | (defvar original-date) | |
470 | (defvar diary-entries-list)) | |
637a8ae9 | 471 | ;;;###autoload |
902a0e3c | 472 | (defun appt-make-list () |
d073fa5b DL |
473 | "Create the appointments list from todays diary buffer. |
474 | The time must be at the beginning of a line for it to be | |
475 | put in the appointments list. | |
476 | 02/23/89 | |
477 | 12:00pm lunch | |
478 | Wednesday | |
479 | 10:00am group meeting | |
480 | We assume that the variables DATE and NUMBER | |
481 | hold the arguments that `list-diary-entries' received. | |
482 | They specify the range of dates that the diary is being processed for." | |
483 | ||
9c197f24 RS |
484 | ;; We have something to do if the range of dates that the diary is |
485 | ;; considering includes the current date. | |
486 | (if (and (not (calendar-date-compare | |
487 | (list (calendar-current-date)) | |
488 | (list original-date))) | |
489 | (calendar-date-compare | |
490 | (list (calendar-current-date)) | |
491 | (list (calendar-gregorian-from-absolute | |
492 | (+ (calendar-absolute-from-gregorian original-date) | |
493 | number))))) | |
494 | (save-excursion | |
495 | ;; Clear the appointments list, then fill it in from the diary. | |
496 | (setq appt-time-msg-list nil) | |
497 | (if diary-entries-list | |
498 | ||
499 | ;; Cycle through the entry-list (diary-entries-list) | |
500 | ;; looking for entries beginning with a time. If | |
501 | ;; the entry begins with a time, add it to the | |
502 | ;; appt-time-msg-list. Then sort the list. | |
503 | ||
504 | (let ((entry-list diary-entries-list) | |
505 | (new-time-string "")) | |
506 | ;; Skip diary entries for dates before today. | |
507 | (while (and entry-list | |
508 | (calendar-date-compare | |
509 | (car entry-list) (list (calendar-current-date)))) | |
510 | (setq entry-list (cdr entry-list))) | |
511 | ;; Parse the entries for today. | |
512 | (while (and entry-list | |
513 | (calendar-date-equal | |
514 | (calendar-current-date) (car (car entry-list)))) | |
515 | (let ((time-string (substring (prin1-to-string | |
50f53db3 | 516 | (cadr (car entry-list))) 1 -1))) |
9c197f24 RS |
517 | |
518 | (while (string-match | |
dae6cb9f | 519 | "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?\\(.*\n\\)*.*" |
9c197f24 | 520 | time-string) |
d073fa5b | 521 | (let* ((appt-time-string (match-string 0 time-string))) |
9c197f24 RS |
522 | |
523 | (if (< (match-end 0) (length time-string)) | |
524 | (setq new-time-string (substring time-string | |
525 | (+ (match-end 0) 1) | |
526 | nil)) | |
527 | (setq new-time-string "")) | |
528 | ||
529 | (string-match "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?" | |
530 | time-string) | |
531 | ||
532 | (let* ((appt-time (list (appt-convert-time | |
d073fa5b | 533 | (match-string 0 time-string)))) |
9c197f24 RS |
534 | (time-msg (cons appt-time |
535 | (list appt-time-string)))) | |
536 | (setq time-string new-time-string) | |
d073fa5b DL |
537 | (setq appt-time-msg-list (nconc appt-time-msg-list |
538 | (list time-msg))))))) | |
9c197f24 RS |
539 | (setq entry-list (cdr entry-list))))) |
540 | (setq appt-time-msg-list (appt-sort-list appt-time-msg-list)) | |
541 | ||
542 | ;; Get the current time and convert it to minutes | |
543 | ;; from midnight. ie. 12:01am = 1, midnight = 0, | |
544 | ;; so that the elements in the list | |
545 | ;; that are earlier than the present time can | |
546 | ;; be removed. | |
547 | ||
0d26feea KH |
548 | (let* ((now (decode-time)) |
549 | (cur-hour (nth 2 now)) | |
550 | (cur-min (nth 1 now)) | |
9c197f24 RS |
551 | (cur-comp-time (+ (* cur-hour 60) cur-min)) |
552 | (appt-comp-time (car (car (car appt-time-msg-list))))) | |
553 | ||
554 | (while (and appt-time-msg-list (< appt-comp-time cur-comp-time)) | |
555 | (setq appt-time-msg-list (cdr appt-time-msg-list)) | |
556 | (if appt-time-msg-list | |
557 | (setq appt-comp-time (car (car (car appt-time-msg-list)))))))))) | |
902a0e3c JB |
558 | |
559 | ||
902a0e3c | 560 | (defun appt-sort-list (appt-list) |
d073fa5b DL |
561 | "Simple sort to put the appointments list APPT-LIST in order. |
562 | Scan the list for the smallest element left in the list. | |
563 | Append the smallest element left into the new list, and remove | |
564 | it from the original list." | |
902a0e3c JB |
565 | (let ((order-list nil)) |
566 | (while appt-list | |
567 | (let* ((element (car appt-list)) | |
568 | (element-time (car (car element))) | |
569 | (tmp-list (cdr appt-list))) | |
570 | (while tmp-list | |
571 | (if (< element-time (car (car (car tmp-list)))) | |
572 | nil | |
573 | (setq element (car tmp-list)) | |
574 | (setq element-time (car (car element)))) | |
575 | (setq tmp-list (cdr tmp-list))) | |
d073fa5b | 576 | (setq order-list (nconc order-list (list element))) |
902a0e3c JB |
577 | (setq appt-list (delq element appt-list)))) |
578 | order-list)) | |
579 | ||
580 | ||
581 | (defun appt-convert-time (time2conv) | |
582 | "Convert hour:min[am/pm] format to minutes from midnight." | |
583 | ||
584 | (let ((conv-time 0) | |
585 | (hr 0) | |
586 | (min 0)) | |
587 | ||
d073fa5b | 588 | (string-match ":\\([0-9][0-9]\\)" time2conv) |
902a0e3c | 589 | (setq min (string-to-int |
d073fa5b | 590 | (match-string 1 time2conv))) |
902a0e3c JB |
591 | |
592 | (string-match "[0-9]?[0-9]:" time2conv) | |
593 | (setq hr (string-to-int | |
d073fa5b | 594 | (match-string 0 time2conv))) |
902a0e3c JB |
595 | |
596 | ;; convert the time appointment time into 24 hour time | |
597 | ||
85bbde63 GM |
598 | (cond ((and (string-match "pm" time2conv) (< hr 12)) |
599 | (setq hr (+ 12 hr))) | |
600 | ((and (string-match "am" time2conv) (= hr 12)) | |
601 | (setq hr 0))) | |
902a0e3c JB |
602 | |
603 | ;; convert the actual time | |
604 | ;; into minutes for comparison | |
605 | ;; against the actual time. | |
606 | ||
607 | (setq conv-time (+ (* hr 60) min)) | |
608 | conv-time)) | |
609 | ||
efa434d9 RS |
610 | (defvar appt-timer nil |
611 | "Timer used for diary appointment notifications (`appt-check').") | |
902a0e3c | 612 | |
f3e7c0dc KH |
613 | (unless appt-timer |
614 | (setq appt-timer (run-at-time t 60 'appt-check))) | |
efa434d9 RS |
615 | |
616 | (or global-mode-string (setq global-mode-string '(""))) | |
617 | (or (memq 'appt-mode-string global-mode-string) | |
618 | (setq global-mode-string | |
619 | (append global-mode-string '(appt-mode-string)))) | |
5b586155 | 620 | |
efa434d9 | 621 | ;;; appt.el ends here |