1 ;;; appt.el --- appointment notification functions.
3 ;; Copyright (C) 1989, 1990, 1994 Free Software Foundation, Inc.
5 ;; Author: Neil Mager <neilm@juliet.ll.mit.edu>
9 ;; This file is part of GNU Emacs.
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)
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.
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.
29 ;; appt.el - visible and/or audible notification of
30 ;; appointments from ~/diary file generated from
31 ;; Edward M. Reingold's calendar.el.
34 ;; Comments, corrections, and improvements should be sent to
36 ;; Net <neilm@juliet.ll.mit.edu>
37 ;; Voice (617) 981-4803
39 ;;; Thanks to Edward M. Reingold for much help and many suggestions,
40 ;;; And to many others for bug fixes and suggestions.
43 ;;; This functions in this file will alert the user of a
44 ;;; pending appointment based on their diary file.
47 ;;; ******* It is necessary to invoke 'display-time' ********
48 ;;; ******* and 'diary' for this to work properly. ********
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.
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.
60 ;;; In order to use, the following should be in your .emacs file in addition to
61 ;;; creating a diary file and invoking calendar:
64 ;;; (setq view-diary-entries-initially t)
65 ;;; (setq appt-issue-message t)
67 ;;; The following three lines are required:
69 ;;; (add-hook 'diary-hook 'appt-make-list)
72 ;;; This is an example of what can be in your diary file:
74 ;;; 9:30am Coffee break
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.
83 ;;; Use describe-function on appt-check for a description of other variables
84 ;;; that can be used to personalize the notification system.
86 ;;; In order to add or delete items from todays list, use appt-add
89 ;;; Additionally, the appointments list is recreated automatically
90 ;;; at 12:01am for those who do not logout every day or are programming
93 ;;; Brief internal description - Skip this if your not interested!
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.
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.
106 ;;; You can change the way the appointment window is created/deleted by
107 ;;; setting the variables
109 ;;; appt-disp-window-function
111 ;;; appt-delete-window-function
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.
120 ;; Make sure calendar is loaded when we compile this.
126 (defvar 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.")
132 (defvar appt-message-warning-time 12
133 "*Time in minutes before an appointment that the warning begins.")
136 (defvar appt-audible t
137 "*Non-nil means beep to indicate appointment.")
140 (defvar appt-visible t
141 "*Non-nil means display appointment message in echo area.")
144 (defvar appt-display-mode-line t
145 "*Non-nil means display minutes to appointment and time on the mode line.")
148 (defvar appt-msg-window t
149 "*Non-nil means display appointment message in another window.")
152 (defvar appt-display-duration 10
153 "*The number of seconds an appointment message is displayed.")
156 (defvar appt-display-diary t
157 "*Non-nil means to display the next days diary on the screen.
158 This will occur at midnight when the appointment list is updated.")
160 (defvar appt-time-msg-list nil
161 "The list of appointments for today.
162 Use `appt-add' and `appt-delete' to add and delete appointments from list.
163 The original list is generated from the today's `diary-entries-list'.
164 The number before each time/message is the time in minutes from midnight.")
166 (defconst max-time 1439
167 "11:59pm in minutes - number of minutes in a day minus 1.")
169 (defvar appt-display-interval 3
170 "*Number of minutes to wait between checking the appointment list.")
172 (defvar appt-buffer-name " *appt-buf*"
173 "Name of the appointments buffer.")
175 (defvar appt-disp-window-function 'appt-disp-window
176 "Function called to display appointment window.")
178 (defvar appt-delete-window-function 'appt-delete-window
179 "Function called to remove appointment window and buffer.")
182 "Check for an appointment and update the mode line.
183 Note: the time must be the first thing in the line in the diary
184 for a warning to be issued.
186 The format of the time can be either 24 hour or am/pm.
193 11:45am Lunch meeting.
195 The following variables control the action of the notification:
198 If T, the diary buffer is checked for appointments.
200 appt-message-warning-time
201 Variable used to determine if appointment message
205 Variable used to determine if appointment is audible.
209 Variable used to determine if appointment message should be
210 displayed in the mini-buffer. Default is t.
213 Variable used to determine if appointment message
214 should temporarily appear in another window. Mutually exclusive
217 appt-display-duration
218 The number of seconds an appointment message
219 is displayed in another window.
221 appt-display-interval
222 The number of minutes to wait between checking the appointments
225 appt-disp-window-function
226 Function called to display appointment window. You can customize
227 appt.el by setting this variable to a function different from the
228 one provided with this package.
230 appt-delete-window-function
231 Function called to remove appointment window and buffer. You can
232 customize appt.el by setting this variable to a function different
233 from the one provided with this package.
235 This function is run from the loadst process for display time.
236 Therefore, you need to have `(display-time)' in your .emacs file."
239 (if (or (= appt-display-interval 1)
240 ;; This is true every appt-display-interval minutes.
241 (= 0 (mod (/ (nth 1 (current-time)) 60) appt-display-interval)))
242 (let ((min-to-app -1)
246 ;; Get the current time and convert it to minutes
247 ;; from midnight. ie. 12:01am = 1, midnight = 0.
249 (let* ((cur-hour(string-to-int
250 (substring (current-time-string) 11 13)))
251 (cur-min (string-to-int
252 (substring (current-time-string) 14 16)))
253 (cur-comp-time (+ (* cur-hour 60) cur-min)))
255 ;; At the first check after 12:01am, we should update our
256 ;; appointments to today's list.
258 (if (and (>= cur-comp-time 1)
259 (<= cur-comp-time appt-display-interval))
260 (if (and view-diary-entries-initially appt-display-diary)
262 (let ((diary-display-hook 'appt-make-list))
265 ;; If there are entries in the list, and the
266 ;; user wants a message issued
267 ;; get the first time off of the list
268 ;; and calculate the number of minutes until
271 (if (and appt-issue-message appt-time-msg-list)
272 (let ((appt-comp-time (car (car (car appt-time-msg-list)))))
273 (setq min-to-app (- appt-comp-time cur-comp-time))
275 (while (and appt-time-msg-list
276 (< appt-comp-time cur-comp-time))
277 (setq appt-time-msg-list (cdr appt-time-msg-list))
278 (if appt-time-msg-list
280 (car (car (car appt-time-msg-list))))))
282 ;; If we have an appointment between midnight and
283 ;; 'appt-message-warning-time' minutes after midnight,
284 ;; we must begin to issue a message before midnight.
285 ;; Midnight is considered 0 minutes and 11:59pm is
286 ;; 1439 minutes. Therefore we must recalculate the minutes
287 ;; to appointment variable. It is equal to the number of
288 ;; minutes before midnight plus the number of
289 ;; minutes after midnight our appointment is.
291 (if (and (< appt-comp-time appt-message-warning-time)
292 (> (+ cur-comp-time appt-message-warning-time)
294 (setq min-to-app (+ (- (1+ max-time) cur-comp-time))
297 ;; issue warning if the appointment time is
298 ;; within appt-message-warning time
300 (if (and (<= min-to-app appt-message-warning-time)
306 "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?"
309 (setq new-time (substring display-time-string
313 appt-disp-window-function
315 (car (cdr (car appt-time-msg-list))))
318 (format "%d sec" appt-display-duration)
320 appt-delete-window-function))
325 (car (cdr (car appt-time-msg-list)))))
330 (if appt-display-mode-line
333 "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?"
336 (setq new-time (substring display-time-string
339 (setq display-time-string
341 min-to-app " min. " new-time " "))
343 (force-mode-line-update t)
347 (setq appt-time-msg-list
348 (cdr appt-time-msg-list))))))))))))
351 ;; Display appointment message in a separate buffer.
352 (defun appt-disp-window (min-to-app new-time appt-msg)
355 ;; Make sure we're not in the minibuffer
356 ;; before splitting the window.
358 (if (equal (selected-window) (minibuffer-window))
360 (select-window (other-window 1))
362 (select-frame (other-frame 1)))))
364 (let* ((this-buffer (current-buffer))
365 (this-window (selected-window))
366 (appt-disp-buf (set-buffer (get-buffer-create appt-buffer-name))))
368 (appt-select-lowest-window)
369 (if (cdr (assq 'unsplittable (frame-parameters)))
370 ;; In an unsplittable frame, use something somewhere else.
371 (display-buffer appt-disp-buf)
372 ;; Otherwise, split the bottom window and use the lower part.
374 (pop-to-buffer appt-disp-buf))
375 (setq mode-line-format
376 (concat "-------------------- Appointment in "
377 min-to-app " minutes. " new-time " %-"))
378 (insert-string appt-msg)
379 (shrink-window-if-larger-than-buffer (get-buffer-window appt-disp-buf t))
380 (set-buffer-modified-p nil)
381 (raise-frame (selected-frame))
382 (select-window this-window)
386 (defun appt-delete-window ()
387 "Function called to undisplay appointment messages.
388 Usually just deletes the appointment buffer."
389 (let ((window (get-buffer-window appt-buffer-name t)))
391 (or (and (fboundp 'frame-root-window)
392 (eq window (frame-root-window (window-frame window))))
393 (delete-window window))))
394 (kill-buffer appt-buffer-name)
398 ;; Select the lowest window on the frame.
399 (defun appt-select-lowest-window ()
400 (let* ((lowest-window (selected-window))
401 (bottom-edge (car (cdr (cdr (cdr (window-edges))))))
402 (last-window (previous-window))
405 (let* ((this-window (next-window))
406 (next-bottom-edge (car (cdr (cdr (cdr
407 (window-edges this-window)))))))
408 (if (< bottom-edge next-bottom-edge)
410 (setq bottom-edge next-bottom-edge)
411 (setq lowest-window this-window)))
413 (select-window this-window)
414 (if (eq last-window this-window)
416 (select-window lowest-window)
417 (setq window-search nil)))))))
420 (defun appt-add (new-appt-time new-appt-msg)
421 "Add an appointment for the day at TIME and issue MESSAGE.
422 The time should be in either 24 hour format or am/pm format."
424 (interactive "sTime (hh:mm[am/pm]): \nsMessage: ")
425 (if (string-match "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?" new-appt-time)
427 (error "Unacceptable time-string"))
429 (let* ((appt-time-string (concat new-appt-time " " new-appt-msg))
430 (appt-time (list (appt-convert-time new-appt-time)))
431 (time-msg (cons appt-time (list appt-time-string))))
432 (setq appt-time-msg-list (append appt-time-msg-list
434 (setq appt-time-msg-list (appt-sort-list appt-time-msg-list))))
436 (defun appt-delete ()
437 "Delete an appointment from the list of appointments."
439 (let* ((tmp-msg-list appt-time-msg-list))
441 (let* ((element (car tmp-msg-list))
442 (prompt-string (concat "Delete "
443 (prin1-to-string (car (cdr element)))
445 (test-input (y-or-n-p prompt-string)))
446 (setq tmp-msg-list (cdr tmp-msg-list))
448 (setq appt-time-msg-list (delq element appt-time-msg-list)))))
452 ;; Create the appointments list from todays diary buffer.
453 ;; The time must be at the beginning of a line for it to be
454 ;; put in the appointments list.
458 ;; 10:00am group meeting
459 ;; We assume that the variables DATE and NUMBER
460 ;; hold the arguments that list-diary-entries received.
461 ;; They specify the range of dates that the diary is being processed for.
464 (defun appt-make-list ()
465 ;; We have something to do if the range of dates that the diary is
466 ;; considering includes the current date.
467 (if (and (not (calendar-date-compare
468 (list (calendar-current-date))
469 (list original-date)))
470 (calendar-date-compare
471 (list (calendar-current-date))
472 (list (calendar-gregorian-from-absolute
473 (+ (calendar-absolute-from-gregorian original-date)
476 ;; Clear the appointments list, then fill it in from the diary.
477 (setq appt-time-msg-list nil)
478 (if diary-entries-list
480 ;; Cycle through the entry-list (diary-entries-list)
481 ;; looking for entries beginning with a time. If
482 ;; the entry begins with a time, add it to the
483 ;; appt-time-msg-list. Then sort the list.
485 (let ((entry-list diary-entries-list)
486 (new-time-string ""))
487 ;; Skip diary entries for dates before today.
488 (while (and entry-list
489 (calendar-date-compare
490 (car entry-list) (list (calendar-current-date))))
491 (setq entry-list (cdr entry-list)))
492 ;; Parse the entries for today.
493 (while (and entry-list
495 (calendar-current-date) (car (car entry-list))))
496 (let ((time-string (substring (prin1-to-string
497 (cdr (car entry-list))) 2 -2)))
500 "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?.*"
502 (let* ((appt-time-string (substring time-string
506 (if (< (match-end 0) (length time-string))
507 (setq new-time-string (substring time-string
510 (setq new-time-string ""))
512 (string-match "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?"
515 (let* ((appt-time (list (appt-convert-time
516 (substring time-string
519 (time-msg (cons appt-time
520 (list appt-time-string))))
521 (setq time-string new-time-string)
522 (setq appt-time-msg-list (append appt-time-msg-list
523 (list time-msg)))))))
524 (setq entry-list (cdr entry-list)))))
525 (setq appt-time-msg-list (appt-sort-list appt-time-msg-list))
527 ;; Get the current time and convert it to minutes
528 ;; from midnight. ie. 12:01am = 1, midnight = 0,
529 ;; so that the elements in the list
530 ;; that are earlier than the present time can
533 (let* ((cur-hour(string-to-int
534 (substring (current-time-string) 11 13)))
535 (cur-min (string-to-int
536 (substring (current-time-string) 14 16)))
537 (cur-comp-time (+ (* cur-hour 60) cur-min))
538 (appt-comp-time (car (car (car appt-time-msg-list)))))
540 (while (and appt-time-msg-list (< appt-comp-time cur-comp-time))
541 (setq appt-time-msg-list (cdr appt-time-msg-list))
542 (if appt-time-msg-list
543 (setq appt-comp-time (car (car (car appt-time-msg-list))))))))))
546 ;;Simple sort to put the appointments list in order.
547 ;;Scan the list for the smallest element left in the list.
548 ;;Append the smallest element left into the new list, and remove
549 ;;it from the original list.
550 (defun appt-sort-list (appt-list)
551 (let ((order-list nil))
553 (let* ((element (car appt-list))
554 (element-time (car (car element)))
555 (tmp-list (cdr appt-list)))
557 (if (< element-time (car (car (car tmp-list))))
559 (setq element (car tmp-list))
560 (setq element-time (car (car element))))
561 (setq tmp-list (cdr tmp-list)))
562 (setq order-list (append order-list (list element)))
563 (setq appt-list (delq element appt-list))))
567 (defun appt-convert-time (time2conv)
568 "Convert hour:min[am/pm] format to minutes from midnight."
574 (string-match ":[0-9][0-9]" time2conv)
575 (setq min (string-to-int
577 (+ (match-beginning 0) 1) (match-end 0))))
579 (string-match "[0-9]?[0-9]:" time2conv)
580 (setq hr (string-to-int
585 ;; convert the time appointment time into 24 hour time
587 (if (and (string-match "[p][m]" time2conv) (< hr 12))
589 (string-match "[0-9]?[0-9]:" time2conv)
590 (setq hr (+ 12 hr))))
592 ;; convert the actual time
593 ;; into minutes for comparison
594 ;; against the actual time.
596 (setq conv-time (+ (* hr 60) min))
599 (add-hook 'display-time-hook 'appt-check)
601 ;;; appt.el ends here