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 'diary' for this to work properly. ****
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.
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.
59 ;;; In order to use, the following should be in your .emacs file in addition to
60 ;;; creating a diary file and invoking calendar:
62 ;;; To enable appointment reminders, the following lines are required:
63 ;;; (add-hook 'diary-hook 'appt-make-list)
64 ;;; (let ((diary-display-hook 'ignore))
66 ;;; You can replace the last two with just (diary)
67 ;;; if you want to display the diary as well.
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)
73 ;;; This is an example of what can be in your diary file:
75 ;;; 9:30am Coffee break
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.
84 ;;; Use describe-function on appt-check for a description of other variables
85 ;;; that can be used to personalize the notification system.
87 ;;; In order to add or delete items from todays list, use appt-add
90 ;;; Additionally, the appointments list is recreated automatically
91 ;;; at 12:01am for those who do not logout every day or are programming
94 ;;; Brief internal description - Skip this if your not interested!
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.
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.
107 ;;; You can change the way the appointment window is created/deleted by
108 ;;; setting the variables
110 ;;; appt-disp-window-function
112 ;;; appt-delete-window-function
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.
121 ;; Make sure calendar is loaded when we compile this.
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."
135 (defcustom appt-message-warning-time
12
136 "*Time in minutes before an appointment that the warning begins."
141 (defcustom appt-audible t
142 "*Non-nil means beep to indicate appointment."
147 (defcustom appt-visible t
148 "*Non-nil means display appointment message in echo area."
153 (defcustom appt-display-mode-line t
154 "*Non-nil means display minutes to appointment and time on the mode line."
159 (defcustom appt-msg-window t
160 "*Non-nil means display appointment message in another window."
165 (defcustom appt-display-duration
10
166 "*The number of seconds an appointment message is displayed."
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."
177 (defcustom appt-interval
60
178 "*Interval in seconds between checking for appointments."
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.")
189 (defconst max-time
1439
190 "11:59pm in minutes - number of minutes in a day minus 1.")
192 (defcustom appt-display-interval
3
193 "*Number of minutes to wait between checking the appointment list."
197 (defvar appt-buffer-name
" *appt-buf*"
198 "Name of the appointments buffer.")
200 (defvar appt-disp-window-function
'appt-disp-window
201 "Function called to display appointment window.")
203 (defvar appt-delete-window-function
'appt-delete-window
204 "Function called to remove appointment window and buffer.")
206 (defvar appt-mode-string nil
207 "String to display in the mode line for an appointment.")
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.
214 The format of the time can be either 24 hour or am/pm.
221 11:45am Lunch meeting.
223 The following variables control the action of the notification:
226 If t, the diary buffer is checked for appointments.
228 `appt-message-warning-time'
229 Variable used to determine if appointment message
233 Variable used to determine if appointment is audible.
237 Variable used to determine if appointment message should be
238 displayed in the mini-buffer. Default is t.
241 Variable used to determine if appointment message
242 should temporarily appear in another window. Mutually exclusive
245 `appt-display-duration'
246 The number of seconds an appointment message
247 is displayed in another window.
249 `appt-display-interval'
250 The number of minutes to wait between checking the appointments
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.
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."
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)
271 ;; Get the current time and convert it to minutes
272 ;; from midnight. ie. 12:01am = 1, midnight = 0.
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
)))
279 ;; At the first check after 12:01am, we should update our
280 ;; appointments to today's list.
282 (if (and (>= cur-comp-time
1)
283 (<= cur-comp-time appt-display-interval
))
284 (if (and view-diary-entries-initially appt-display-diary
)
286 (let ((diary-display-hook 'appt-make-list
))
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
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
))
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
304 (car (car (car appt-time-msg-list
))))))
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.
315 (if (and (< appt-comp-time appt-message-warning-time
)
316 (> (+ cur-comp-time appt-message-warning-time
)
318 (setq min-to-app
(+ (- (1+ max-time
) cur-comp-time
))
321 ;; issue warning if the appointment time is
322 ;; within appt-message-warning time
324 (when (and (<= min-to-app appt-message-warning-time
)
328 (setq new-time
(format-time-string "%a %b %e "
331 appt-disp-window-function
333 (car (cdr (car appt-time-msg-list
))))
336 (format "%d sec" appt-display-duration
)
338 appt-delete-window-function
))
343 (car (cdr (car appt-time-msg-list
)))))
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
)
355 (setq appt-time-msg-list
356 (cdr appt-time-msg-list
)))))))))))
359 ;; Display appointment message in a separate buffer.
360 (defun appt-disp-window (min-to-app new-time appt-msg
)
363 ;; Make sure we're not in the minibuffer
364 ;; before splitting the window.
366 (if (equal (selected-window) (minibuffer-window))
368 (select-window (other-window 1))
370 (select-frame (other-frame 1)))))
372 (let* ((this-buffer (current-buffer))
373 (this-window (selected-window))
374 (appt-disp-buf (set-buffer (get-buffer-create appt-buffer-name
))))
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.
382 (pop-to-buffer appt-disp-buf
))
383 (setq mode-line-format
384 (concat "-------------------- Appointment in "
385 min-to-app
" minutes. " new-time
" %-"))
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
)
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
)))
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
)
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))
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
)
419 (setq bottom-edge next-bottom-edge
)
420 (setq lowest-window this-window
)))
422 (select-window this-window
)
423 (if (eq last-window this-window
)
425 (select-window lowest-window
)
426 (setq window-search nil
)))))))
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."
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
)
436 (error "Unacceptable time-string"))
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
443 (setq appt-time-msg-list
(appt-sort-list appt-time-msg-list
))))
445 (defun appt-delete ()
446 "Delete an appointment from the list of appointments."
448 (let* ((tmp-msg-list appt-time-msg-list
))
450 (let* ((element (car tmp-msg-list
))
451 (prompt-string (concat "Delete "
452 (prin1-to-string (car (cdr element
)))
454 (test-input (y-or-n-p prompt-string
)))
455 (setq tmp-msg-list
(cdr tmp-msg-list
))
457 (setq appt-time-msg-list
(delq element appt-time-msg-list
)))))
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.
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.
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
)
485 ;; Clear the appointments list, then fill it in from the diary.
486 (setq appt-time-msg-list nil
)
487 (if diary-entries-list
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.
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
504 (calendar-current-date) (car (car entry-list
))))
505 (let ((time-string (substring (prin1-to-string
506 (cdr (car entry-list
))) 2 -
2)))
509 "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?.*"
511 (let* ((appt-time-string (substring time-string
515 (if (< (match-end 0) (length time-string
))
516 (setq new-time-string
(substring time-string
519 (setq new-time-string
""))
521 (string-match "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?"
524 (let* ((appt-time (list (appt-convert-time
525 (substring time-string
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
))
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
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
)))))
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
))))))))))
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
))
561 (let* ((element (car appt-list
))
562 (element-time (car (car element
)))
563 (tmp-list (cdr appt-list
)))
565 (if (< element-time
(car (car (car tmp-list
))))
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
))))
575 (defun appt-convert-time (time2conv)
576 "Convert hour:min[am/pm] format to minutes from midnight."
582 (string-match ":[0-9][0-9]" time2conv
)
583 (setq min
(string-to-int
585 (+ (match-beginning 0) 1) (match-end 0))))
587 (string-match "[0-9]?[0-9]:" time2conv
)
588 (setq hr
(string-to-int
593 ;; convert the time appointment time into 24 hour time
595 (if (and (string-match "[p][m]" time2conv
) (< hr
12))
597 (string-match "[0-9]?[0-9]:" time2conv
)
598 (setq hr
(+ 12 hr
))))
600 ;; convert the actual time
601 ;; into minutes for comparison
602 ;; against the actual time.
604 (setq conv-time
(+ (* hr
60) min
))
607 (defvar appt-timer nil
608 "Timer used for diary appointment notifications (`appt-check').")
610 (setq appt-timer
(run-at-time t appt-interval
'appt-check
))
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))))
617 ;;; appt.el ends here