1 ;;; appt.el --- appointment notification functions
3 ;; Copyright (C) 1989, 1990, 1994, 1998, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006 Free Software Foundation, Inc.
6 ;; Author: Neil Mager <neilm@juliet.ll.mit.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
30 ;; appt.el - visible and/or audible notification of
31 ;; appointments from diary file.
34 ;;; Thanks to Edward M. Reingold for much help and many suggestions,
35 ;;; And to many others for bug fixes and suggestions.
38 ;;; This functions in this file will alert the user of a
39 ;;; pending appointment based on his/her diary file. This package
40 ;;; is documented in the Emacs manual.
42 ;;; To activate this package, simply use (appt-activate 1).
43 ;;; A `diary-file' with appointments of the format described in the
44 ;;; documentation of the function `appt-check' is required.
45 ;;; Relevant customizable variables are also listed in the
46 ;;; documentation of that function.
48 ;;; Today's appointment list is initialized from the diary when this
49 ;;; package is activated. Additionally, the appointments list is
50 ;;; recreated automatically at 12:01am for those who do not logout
51 ;;; every day or are programming late. It is also updated when the
52 ;;; `diary-file' is saved. Calling `appt-check' with an argument forces
53 ;;; a re-initialization at any time.
55 ;;; In order to add or delete items from today's list, without
56 ;;; changing the diary file, use `appt-add' and `appt-delete'.
59 ;;; Brief internal description - Skip this if you are not interested!
61 ;;; The function `appt-make-list' creates the appointments list which
62 ;;; `appt-check' reads.
64 ;;; You can change the way the appointment window is created/deleted by
65 ;;; setting the variables
67 ;;; appt-disp-window-function
69 ;;; appt-delete-window-function
71 ;;; For instance, these variables could be set to functions that display
72 ;;; appointments in pop-up frames, which are lowered or iconified after
73 ;;; `appt-display-interval' minutes.
78 ;; Make sure calendar is loaded when we compile this.
81 (defvar diary-selective-display)
84 (defcustom appt-issue-message t
85 "*Non-nil means check for appointments in the diary buffer.
86 To be detected, the diary entry must have the format described in the
87 documentation of the function `appt-check'."
91 (make-obsolete-variable 'appt-issue-message
92 "use the function `appt-activate', and the \
93 variable `appt-display-format' instead." "22.1")
96 (defcustom appt-message-warning-time 12
97 "*Time in minutes before an appointment that the warning begins."
102 (defcustom appt-audible t
103 "*Non-nil means beep to indicate appointment."
108 (defcustom appt-visible t
109 "*Non-nil means display appointment message in echo area.
110 This variable is only relevant if `appt-msg-window' is nil."
114 (make-obsolete-variable 'appt-visible 'appt-display-format "22.1")
117 (defcustom appt-msg-window t
118 "*Non-nil means display appointment message in another window.
119 If non-nil, this variable overrides `appt-visible'."
123 (make-obsolete-variable 'appt-msg-window 'appt-display-format "22.1")
126 (defcustom appt-display-format 'ignore
127 "How appointment reminders should be displayed.
129 window - use a separate window
130 echo - use the echo area
131 nil - no visible reminder.
132 See also `appt-audible' and `appt-display-mode-line'.
134 The default value is 'ignore, which means to fall back on the value
135 of the (obsolete) variables `appt-msg-window' and `appt-visible'."
137 (const :tag "Separate window" window)
138 (const :tag "Echo-area" echo)
139 (const :tag "No visible display" nil))
144 (defcustom appt-display-mode-line t
145 "*Non-nil means display minutes to appointment and time on the mode line.
146 This is in addition to any other display of appointment messages."
151 (defcustom appt-display-duration 10
152 "*The number of seconds an appointment message is displayed.
153 Only relevant if reminders are to be displayed in their own window."
158 (defcustom appt-display-diary t
159 "*Non-nil displays the diary when the appointment list is first initialized.
160 This will occur at midnight when the appointment list is updated."
164 (defcustom appt-display-interval 3
165 "*Number of minutes to wait between checking the appointment list."
169 (defcustom appt-disp-window-function 'appt-disp-window
170 "Function called to display appointment window.
171 Only relevant if reminders are being displayed in a window."
172 :type '(choice (const appt-disp-window)
176 (defcustom appt-delete-window-function 'appt-delete-window
177 "Function called to remove appointment window and buffer.
178 Only relevant if reminders are being displayed in a window."
179 :type '(choice (const appt-delete-window)
184 ;;; Internal variables below this point.
186 (defconst appt-buffer-name " *appt-buf*"
187 "Name of the appointments buffer.")
189 (defvar appt-time-msg-list nil
190 "The list of appointments for today.
191 Use `appt-add' and `appt-delete' to add and delete appointments.
192 The original list is generated from today's `diary-entries-list', and
193 can be regenerated using the function `appt-check'.
194 Each element of the generated list has the form (MINUTES STRING [FLAG]); where
195 MINUTES is the time in minutes of the appointment after midnight, and
196 STRING is the description of the appointment.
197 FLAG, if non-nil, says that the element was made with `appt-add'
198 so calling `appt-make-list' again should preserve it.")
200 (defconst appt-max-time (1- (* 24 60))
201 "11:59pm in minutes - number of minutes in a day minus 1.")
203 (defvar appt-mode-string nil
204 "String being displayed in the mode line saying you have an appointment.
205 The actual string includes the amount of time till the appointment.
206 Only used if `appt-display-mode-line' is non-nil.")
208 (defvar appt-prev-comp-time nil
209 "Time of day (mins since midnight) at which we last checked appointments.
210 A nil value forces the diary file to be (re-)checked for appointments.")
212 (defvar appt-now-displayed nil
213 "Non-nil when we have started notifying about a appointment that is near.")
215 (defvar appt-display-count nil
216 "Internal variable used to count number of consecutive reminders.")
218 (defvar appt-timer nil
219 "Timer used for diary appointment notifications (`appt-check').
220 If this is non-nil, appointment checking is active.")
225 (defun appt-display-message (string mins)
226 "Display a reminder about an appointment.
227 The string STRING describes the appointment, due in integer MINS minutes.
228 The format of the visible reminder is controlled by `appt-display-format'.
229 The variable `appt-audible' controls the audible reminder."
230 ;; let binding for backwards compatability. Remove when obsolete
231 ;; vars appt-msg-window and appt-visible are dropped.
232 (let ((appt-display-format
233 (if (eq appt-display-format 'ignore)
234 (cond (appt-msg-window 'window)
235 (appt-visible 'echo))
236 appt-display-format)))
237 (cond ((eq appt-display-format 'window)
238 (funcall appt-disp-window-function
239 (number-to-string mins)
240 ;; TODO - use calendar-month-abbrev-array rather
242 (format-time-string "%a %b %e " (current-time))
244 (run-at-time (format "%d sec" appt-display-duration)
246 appt-delete-window-function))
247 ((eq appt-display-format 'echo)
248 (message "%s" string)))
249 (if appt-audible (beep 1))))
252 (defun appt-check (&optional force)
253 "Check for an appointment and update any reminder display.
254 If optional argument FORCE is non-nil, reparse the diary file for
255 appointments. Otherwise the diary file is only parsed once per day,
258 Note: the time must be the first thing in the line in the diary
259 for a warning to be issued. The format of the time can be either
260 24 hour or am/pm. For example:
266 11:45am Lunch meeting.
268 Appointments are checked every `appt-display-interval' minutes.
269 The following variables control appointment notification:
271 `appt-display-format'
272 Controls the format in which reminders are displayed.
275 Variable used to determine if reminder is audible.
278 `appt-message-warning-time'
279 Variable used to determine when appointment message
280 should first be displayed.
282 `appt-display-mode-line'
283 If non-nil, a generic message giving the time remaining
284 is shown in the mode-line when an appointment is due.
286 `appt-display-interval'
287 Interval in minutes at which to check for pending appointments.
290 Display the diary buffer when the appointment list is
291 initialized for the first time in a day.
293 The following variables are only relevant if reminders are being
294 displayed in a window:
296 `appt-display-duration'
297 The number of seconds an appointment message is displayed.
299 `appt-disp-window-function'
300 Function called to display appointment window.
302 `appt-delete-window-function'
303 Function called to remove appointment window and buffer."
305 (let* ((min-to-app -1)
306 (prev-appt-mode-string appt-mode-string)
307 (prev-appt-display-count (or appt-display-count 0))
308 ;; Non-nil means do a full check for pending appointments
309 ;; and display in whatever ways the user has selected.
310 ;; When no appointment is being displayed,
311 ;; we always do a full check.
313 (or (not appt-now-displayed)
314 ;; This is true every appt-display-interval minutes.
315 (zerop (mod prev-appt-display-count appt-display-interval))))
316 ;; Non-nil means only update the interval displayed in the mode line.
318 (and (not full-check) appt-now-displayed)))
320 (when (or full-check mode-line-only)
323 ;; Get the current time and convert it to minutes
324 ;; from midnight. ie. 12:01am = 1, midnight = 0.
326 (let* ((now (decode-time))
327 (cur-hour (nth 2 now))
328 (cur-min (nth 1 now))
329 (cur-comp-time (+ (* cur-hour 60) cur-min)))
331 ;; At the first check in any given day, update our
332 ;; appointments to today's list.
334 (if (or force ; eg initialize, diary save
335 (null appt-prev-comp-time) ; first check
336 (< cur-comp-time appt-prev-comp-time)) ; new day
338 (if appt-display-diary
340 (if (assoc 'appt-make-list diary-hook)
342 (cons 'appt-make-list diary-hook))))
344 (let* ((diary-display-hook 'appt-make-list)
345 (d-buff (find-buffer-visiting
346 (substitute-in-file-name diary-file)))
348 (if d-buff ; Diary buffer exists.
349 (with-current-buffer d-buff
350 diary-selective-display))))
352 ;; If the diary buffer existed before this command,
353 ;; restore its display state. Otherwise, kill it.
355 ;; Displays the diary buffer.
356 (or selective (show-all-diary-entries))
358 (setq d-buff (find-buffer-visiting
359 (substitute-in-file-name diary-file)))
360 (kill-buffer d-buff)))))
363 (setq appt-prev-comp-time cur-comp-time
365 appt-display-count nil)
367 ;; If there are entries in the list, and the
368 ;; user wants a message issued,
369 ;; get the first time off of the list
370 ;; and calculate the number of minutes until the appointment.
372 (if (and appt-issue-message appt-time-msg-list)
373 (let ((appt-comp-time (car (car (car appt-time-msg-list)))))
374 (setq min-to-app (- appt-comp-time cur-comp-time))
376 (while (and appt-time-msg-list
377 (< appt-comp-time cur-comp-time))
378 (setq appt-time-msg-list (cdr appt-time-msg-list))
379 (if appt-time-msg-list
381 (car (car (car appt-time-msg-list))))))
383 ;; If we have an appointment between midnight and
384 ;; 'appt-message-warning-time' minutes after midnight,
385 ;; we must begin to issue a message before midnight.
386 ;; Midnight is considered 0 minutes and 11:59pm is
387 ;; 1439 minutes. Therefore we must recalculate the minutes
388 ;; to appointment variable. It is equal to the number of
389 ;; minutes before midnight plus the number of
390 ;; minutes after midnight our appointment is.
392 (if (and (< appt-comp-time appt-message-warning-time)
393 (> (+ cur-comp-time appt-message-warning-time)
395 (setq min-to-app (+ (- (1+ appt-max-time) cur-comp-time))
398 ;; issue warning if the appointment time is
399 ;; within appt-message-warning time
401 (when (and (<= min-to-app appt-message-warning-time)
403 (setq appt-now-displayed t
404 appt-display-count (1+ prev-appt-display-count))
405 (unless mode-line-only
406 (appt-display-message (cadr (car appt-time-msg-list))
408 (when appt-display-mode-line
409 (setq appt-mode-string
410 (format " App't in %s min." min-to-app)))
412 ;; When an appointment is reached,
413 ;; delete it from the list.
414 ;; Reset the count to 0 in case we display another
415 ;; appointment on the next cycle.
416 (if (zerop min-to-app)
417 (setq appt-time-msg-list (cdr appt-time-msg-list)
418 appt-display-count nil)))))
420 ;; If we have changed the mode line string,
421 ;; redisplay all mode lines.
422 (and appt-display-mode-line
423 (not (equal appt-mode-string
424 prev-appt-mode-string))
426 (force-mode-line-update t)
427 ;; If the string now has a notification,
428 ;; redisplay right now.
433 (defun appt-disp-window (min-to-app new-time appt-msg)
434 "Display appointment message APPT-MSG in a separate buffer.
435 The appointment is due in MIN-TO-APP (a string) minutes.
436 NEW-TIME is a string giving the date."
439 ;; Make sure we're not in the minibuffer
440 ;; before splitting the window.
442 (if (equal (selected-window) (minibuffer-window))
444 (select-window (other-window 1))
445 (if (display-multi-frame-p)
446 (select-frame (other-frame 1)))))
448 (let ((this-window (selected-window))
449 (appt-disp-buf (set-buffer (get-buffer-create appt-buffer-name))))
451 (if (cdr (assq 'unsplittable (frame-parameters)))
452 ;; In an unsplittable frame, use something somewhere else.
453 (display-buffer appt-disp-buf)
454 (unless (or (special-display-p (buffer-name appt-disp-buf))
455 (same-window-p (buffer-name appt-disp-buf)))
456 ;; By default, split the bottom window and use the lower part.
457 (appt-select-lowest-window)
458 (select-window (split-window)))
459 (switch-to-buffer appt-disp-buf))
460 (calendar-set-mode-line
461 (format " Appointment in %s minutes. %s " min-to-app new-time))
464 (shrink-window-if-larger-than-buffer (get-buffer-window appt-disp-buf t))
465 (set-buffer-modified-p nil)
466 (raise-frame (selected-frame))
467 (select-window this-window)))
469 (defun appt-delete-window ()
470 "Function called to undisplay appointment messages.
471 Usually just deletes the appointment buffer."
472 (let ((window (get-buffer-window appt-buffer-name t)))
474 (or (eq window (frame-root-window (window-frame window)))
475 (delete-window window))))
476 (kill-buffer appt-buffer-name)
480 (defun appt-select-lowest-window ()
481 "Select the lowest window on the frame."
482 (let ((lowest-window (selected-window))
483 (bottom-edge (nth 3 (window-edges))))
484 (walk-windows (lambda (w)
485 (let ((next-bottom-edge (nth 3 (window-edges w))))
486 (when (< bottom-edge next-bottom-edge)
487 (setq bottom-edge next-bottom-edge
489 (select-window lowest-window)))
491 (defconst appt-time-regexp
492 "[0-9]?[0-9]\\(h\\([0-9][0-9]\\)?\\|[:.][0-9][0-9]\\)\\(am\\|pm\\)?")
495 (defun appt-add (new-appt-time new-appt-msg)
496 "Add an appointment for today at NEW-APPT-TIME with message NEW-APPT-MSG.
497 The time should be in either 24 hour format or am/pm format."
498 (interactive "sTime (hh:mm[am/pm]): \nsMessage: ")
499 (unless (string-match appt-time-regexp new-appt-time)
500 (error "Unacceptable time-string"))
501 (let ((time-msg (list (list (appt-convert-time new-appt-time))
502 (concat new-appt-time " " new-appt-msg) t)))
503 (unless (member time-msg appt-time-msg-list)
504 (setq appt-time-msg-list
505 (appt-sort-list (nconc appt-time-msg-list (list time-msg)))))))
508 (defun appt-delete ()
509 "Delete an appointment from the list of appointments."
511 (let ((tmp-msg-list appt-time-msg-list))
513 (let* ((element (car tmp-msg-list))
514 (prompt-string (concat "Delete "
515 ;; We want to quote any doublequotes
516 ;; in the string, as well as put
517 ;; doublequotes around it.
519 (substring-no-properties
520 (car (cdr element)) 0))
522 (test-input (y-or-n-p prompt-string)))
523 (setq tmp-msg-list (cdr tmp-msg-list))
525 (setq appt-time-msg-list (delq element appt-time-msg-list)))))
530 (eval-when-compile (defvar number)
531 (defvar original-date)
532 (defvar diary-entries-list))
534 (defun appt-make-list ()
535 "Update the appointments list from today's diary buffer.
536 The time must be at the beginning of a line for it to be
537 put in the appointments list (see examples in documentation of
538 the function `appt-check'). We assume that the variables DATE and
539 NUMBER hold the arguments that `list-diary-entries' received.
540 They specify the range of dates that the diary is being processed for.
542 Any appointments made with `appt-add' are not affected by this
545 For backwards compatibility, this function activates the
546 appointment package (if it is not already active)."
547 ;; See comments above appt-activate defun.
550 ;; We have something to do if the range of dates that the diary is
551 ;; considering includes the current date.
552 (if (and (not (calendar-date-compare
553 (list (calendar-current-date))
554 (list original-date)))
555 (calendar-date-compare
556 (list (calendar-current-date))
557 (list (calendar-gregorian-from-absolute
558 (+ (calendar-absolute-from-gregorian original-date)
561 ;; Clear the appointments list, then fill it in from the diary.
562 (dolist (elt appt-time-msg-list)
563 ;; Delete any entries that were not made with appt-add.
565 (setq appt-time-msg-list
566 (delq elt appt-time-msg-list))))
567 (if diary-entries-list
569 ;; Cycle through the entry-list (diary-entries-list)
570 ;; looking for entries beginning with a time. If
571 ;; the entry begins with a time, add it to the
572 ;; appt-time-msg-list. Then sort the list.
574 (let ((entry-list diary-entries-list)
575 (new-time-string ""))
576 ;; Skip diary entries for dates before today.
577 (while (and entry-list
578 (calendar-date-compare
579 (car entry-list) (list (calendar-current-date))))
580 (setq entry-list (cdr entry-list)))
581 ;; Parse the entries for today.
582 (while (and entry-list
584 (calendar-current-date) (car (car entry-list))))
585 (let ((time-string (cadr (car entry-list))))
586 (while (string-match appt-time-regexp time-string)
587 (let* ((beg (match-beginning 0))
588 ;; Get just the time for this appointment.
589 (only-time (match-string 0 time-string))
590 ;; Find the end of this appointment
591 ;; (the start of the next).
593 (concat "\n[ \t]*" appt-time-regexp)
596 ;; Get the whole string for this appointment.
598 (substring time-string beg (if end (1- end)))))
600 ;; Add this appointment to appt-time-msg-list.
601 (let* ((appt-time (list (appt-convert-time only-time)))
602 (time-msg (list appt-time appt-time-string)))
603 (setq appt-time-msg-list
604 (nconc appt-time-msg-list (list time-msg))))
606 ;; Discard this appointment from the string.
608 (if end (substring time-string end) "")))))
609 (setq entry-list (cdr entry-list)))))
610 (setq appt-time-msg-list (appt-sort-list appt-time-msg-list))
612 ;; Get the current time and convert it to minutes
613 ;; from midnight. ie. 12:01am = 1, midnight = 0,
614 ;; so that the elements in the list
615 ;; that are earlier than the present time can
618 (let* ((now (decode-time))
619 (cur-hour (nth 2 now))
620 (cur-min (nth 1 now))
621 (cur-comp-time (+ (* cur-hour 60) cur-min))
622 (appt-comp-time (car (caar appt-time-msg-list))))
624 (while (and appt-time-msg-list (< appt-comp-time cur-comp-time))
625 (setq appt-time-msg-list (cdr appt-time-msg-list))
626 (if appt-time-msg-list
627 (setq appt-comp-time (car (caar appt-time-msg-list))))))))))
630 (defun appt-sort-list (appt-list)
631 "Sort an appointment list, putting earlier items at the front.
632 APPT-LIST is a list of the same format as `appt-time-msg-list'."
633 (sort appt-list (lambda (e1 e2) (< (caar e1) (caar e2)))))
636 (defun appt-convert-time (time2conv)
637 "Convert hour:min[am/pm] format to minutes from midnight.
638 A period (.) can be used instead of a colon (:) to separate the
639 hour and minute parts."
640 ;; Formats that should be accepted:
641 ;; 10:00 10.00 10h00 10h 10am 10:00am 10.00am
642 (let ((min (if (string-match "[h:.]\\([0-9][0-9]\\)" time2conv)
643 (string-to-number (match-string 1 time2conv))
645 (hr (if (string-match "[0-9]*[0-9]" time2conv)
646 (string-to-number (match-string 0 time2conv))
649 ;; convert the time appointment time into 24 hour time
650 (cond ((and (string-match "pm" time2conv) (< hr 12))
652 ((and (string-match "am" time2conv) (= hr 12))
655 ;; convert the actual time into minutes.
659 (defun appt-update-list ()
660 "If the current buffer is visiting the diary, update appointments.
661 This function is intended for use with `write-file-functions'."
662 (and (string-equal buffer-file-name (expand-file-name diary-file))
664 (let ((appt-display-diary nil))
669 ;; In Emacs-21.3, the manual documented the following procedure to
670 ;; activate this package:
672 ;; (add-hook 'diary-hook 'appt-make-list)
674 ;; The display-time call was not necessary, AFAICS.
675 ;; What was really needed was to add the hook and load this file.
676 ;; Calling (diary 0) once the hook had been added was in some sense a
677 ;; roundabout way of loading this file. This file used to have code at
678 ;; the top-level that set up the appt-timer and global-mode-string.
679 ;; One way to maintain backwards compatibility would be to call
680 ;; (appt-activate 1) at top-level. However, this goes against the
681 ;; convention that just loading an Emacs package should not activate
682 ;; it. Instead, we make appt-make-list activate the package (after a
683 ;; suggestion from rms). This means that one has to call diary in
684 ;; order to get it to work, but that is in line with the old (weird,
685 ;; IMO) documented behavior for activating the package.
686 ;; Actually, since (diary 0) does not run diary-hook, I don't think
687 ;; the documented behavior in Emacs-21.3 would ever have worked.
688 ;; Oh well, at least with the changes to appt-make-list it will now
689 ;; work as well as it ever did.
690 ;; The new method is just to use (appt-activate 1).
694 (defun appt-activate (&optional arg)
695 "Toggle checking of appointments.
696 With optional numeric argument ARG, turn appointment checking on if
697 ARG is positive, otherwise off."
699 (let ((appt-active appt-timer))
700 (setq appt-active (if arg (> (prefix-numeric-value arg) 0)
702 (remove-hook 'write-file-functions 'appt-update-list)
703 (or global-mode-string (setq global-mode-string '("")))
704 (delq 'appt-mode-string global-mode-string)
706 (cancel-timer appt-timer)
707 (setq appt-timer nil))
709 (add-hook 'write-file-functions 'appt-update-list)
710 (setq appt-timer (run-at-time t 60 'appt-check)
712 (append global-mode-string '(appt-mode-string)))
718 ;; arch-tag: bf5791c4-8921-499e-a26f-772b1788d347
719 ;;; appt.el ends here