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