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 (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."
134 (defcustom appt-message-warning-time 12
135 "*Time in minutes before an appointment that the warning begins."
140 (defcustom appt-audible t
141 "*Non-nil means beep to indicate appointment."
146 (defcustom appt-visible t
147 "*Non-nil means display appointment message in echo area."
152 (defcustom appt-display-mode-line t
153 "*Non-nil means display minutes to appointment and time on the mode line."
158 (defcustom appt-msg-window t
159 "*Non-nil means display appointment message in another window."
164 (defcustom appt-display-duration 10
165 "*The number of seconds an appointment message is displayed."
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."
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.")
182 (defconst max-time 1439
183 "11:59pm in minutes - number of minutes in a day minus 1.")
185 (defcustom appt-display-interval 3
186 "*Number of minutes to wait between checking the appointment list."
190 (defvar appt-buffer-name " *appt-buf*"
191 "Name of the appointments buffer.")
193 (defvar appt-disp-window-function 'appt-disp-window
194 "Function called to display appointment window.")
196 (defvar appt-delete-window-function 'appt-delete-window
197 "Function called to remove appointment window and buffer.")
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.
204 The format of the time can be either 24 hour or am/pm.
211 11:45am Lunch meeting.
213 The following variables control the action of the notification:
216 If T, the diary buffer is checked for appointments.
218 appt-message-warning-time
219 Variable used to determine if appointment message
223 Variable used to determine if appointment is audible.
227 Variable used to determine if appointment message should be
228 displayed in the mini-buffer. Default is t.
231 Variable used to determine if appointment message
232 should temporarily appear in another window. Mutually exclusive
235 appt-display-duration
236 The number of seconds an appointment message
237 is displayed in another window.
239 appt-display-interval
240 The number of minutes to wait between checking the appointments
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.
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.
253 This function is run from the loadst process for display time.
254 Therefore, you need to have `(display-time)' in your .emacs file."
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)
264 ;; Get the current time and convert it to minutes
265 ;; from midnight. ie. 12:01am = 1, midnight = 0.
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)))
272 ;; At the first check after 12:01am, we should update our
273 ;; appointments to today's list.
275 (if (and (>= cur-comp-time 1)
276 (<= cur-comp-time appt-display-interval))
277 (if (and view-diary-entries-initially appt-display-diary)
279 (let ((diary-display-hook 'appt-make-list))
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
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))
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
297 (car (car (car appt-time-msg-list))))))
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.
308 (if (and (< appt-comp-time appt-message-warning-time)
309 (> (+ cur-comp-time appt-message-warning-time)
311 (setq min-to-app (+ (- (1+ max-time) cur-comp-time))
314 ;; issue warning if the appointment time is
315 ;; within appt-message-warning time
317 (if (and (<= min-to-app appt-message-warning-time)
323 "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?"
326 (setq new-time (substring display-time-string
330 appt-disp-window-function
332 (car (cdr (car appt-time-msg-list))))
335 (format "%d sec" appt-display-duration)
337 appt-delete-window-function))
342 (car (cdr (car appt-time-msg-list)))))
347 (if appt-display-mode-line
350 "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?"
353 (setq new-time (substring display-time-string
356 (setq display-time-string
358 min-to-app " min. " new-time " "))
360 (force-mode-line-update t)
364 (setq appt-time-msg-list
365 (cdr appt-time-msg-list))))))))))))
368 ;; Display appointment message in a separate buffer.
369 (defun appt-disp-window (min-to-app new-time appt-msg)
372 ;; Make sure we're not in the minibuffer
373 ;; before splitting the window.
375 (if (equal (selected-window) (minibuffer-window))
377 (select-window (other-window 1))
379 (select-frame (other-frame 1)))))
381 (let* ((this-buffer (current-buffer))
382 (this-window (selected-window))
383 (appt-disp-buf (set-buffer (get-buffer-create appt-buffer-name))))
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.
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)
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)))
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)
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))
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)
427 (setq bottom-edge next-bottom-edge)
428 (setq lowest-window this-window)))
430 (select-window this-window)
431 (if (eq last-window this-window)
433 (select-window lowest-window)
434 (setq window-search nil)))))))
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."
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)
444 (error "Unacceptable time-string"))
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
451 (setq appt-time-msg-list (appt-sort-list appt-time-msg-list))))
453 (defun appt-delete ()
454 "Delete an appointment from the list of appointments."
456 (let* ((tmp-msg-list appt-time-msg-list))
458 (let* ((element (car tmp-msg-list))
459 (prompt-string (concat "Delete "
460 (prin1-to-string (car (cdr element)))
462 (test-input (y-or-n-p prompt-string)))
463 (setq tmp-msg-list (cdr tmp-msg-list))
465 (setq appt-time-msg-list (delq element appt-time-msg-list)))))
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.
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.
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)
493 ;; Clear the appointments list, then fill it in from the diary.
494 (setq appt-time-msg-list nil)
495 (if diary-entries-list
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.
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
512 (calendar-current-date) (car (car entry-list))))
513 (let ((time-string (substring (prin1-to-string
514 (cdr (car entry-list))) 2 -2)))
517 "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?.*"
519 (let* ((appt-time-string (substring time-string
523 (if (< (match-end 0) (length time-string))
524 (setq new-time-string (substring time-string
527 (setq new-time-string ""))
529 (string-match "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?"
532 (let* ((appt-time (list (appt-convert-time
533 (substring time-string
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))
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
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)))))
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))))))))))
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))
569 (let* ((element (car appt-list))
570 (element-time (car (car element)))
571 (tmp-list (cdr appt-list)))
573 (if (< element-time (car (car (car tmp-list))))
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))))
583 (defun appt-convert-time (time2conv)
584 "Convert hour:min[am/pm] format to minutes from midnight."
590 (string-match ":[0-9][0-9]" time2conv)
591 (setq min (string-to-int
593 (+ (match-beginning 0) 1) (match-end 0))))
595 (string-match "[0-9]?[0-9]:" time2conv)
596 (setq hr (string-to-int
601 ;; convert the time appointment time into 24 hour time
603 (if (and (string-match "[p][m]" time2conv) (< hr 12))
605 (string-match "[0-9]?[0-9]:" time2conv)
606 (setq hr (+ 12 hr))))
608 ;; convert the actual time
609 ;; into minutes for comparison
610 ;; against the actual time.
612 (setq conv-time (+ (* hr 60) min))
615 (add-hook 'display-time-hook 'appt-check)
617 ;;; appt.el ends here