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