home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / calendar / appt.el next >
Encoding:
Text File  |  1993-01-11  |  27.6 KB  |  724 lines

  1. ;;; -*- Mode:Emacs-Lisp -*-
  2. ;; Appointment notification functions.
  3. ;; Copyright (C) 1989, 1990, 1992, 1993 Free Software Foundation, Inc.
  4.  
  5. ;; This file is part of GNU Emacs.
  6.  
  7. ;; GNU Emacs is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 2, or (at your option)
  10. ;; any later version.
  11.  
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ;; GNU General Public License for more details.
  16.  
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  19. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. ;;; 29-nov-89    created by Neil Mager <neilm@juliet.ll.mit.edu>.
  22. ;;; 23-feb-91    hacked upon by Jamie Zawinski <jwz@lucid.com>.
  23. ;;;  1-apr-91    some more.
  24. ;;;
  25. ;; appt.el - visible and/or audible notification of
  26. ;;           appointments from ~/diary file generated from
  27. ;;           Edward M. Reingold's calendar.el.
  28. ;;
  29. ;; Version 2.1
  30. ;;
  31. ;; Comments, corrections, and improvements should be sent to
  32. ;; Neil M. Mager
  33. ;; Net                     <neilm@juliet.ll.mit.edu>
  34. ;; Voice                   (617) 981-4803
  35. ;;;
  36. ;;; Thanks to  Edward M. Reingold for much help and many suggestions, 
  37. ;;; And to many others for bug fixes and suggestions.
  38. ;;;
  39. ;;;
  40. ;;; This functions in this file will alert the user of a 
  41. ;;; pending appointment based on their diary file.
  42. ;;;
  43. ;;; ******* It is necessary to invoke 'display-time' and ********
  44. ;;; ******* 'appt-initialize' for this to work properly. ********
  45. ;;; 
  46. ;;; A message will be displayed in the mode line of the emacs buffer and (if
  47. ;;; the user desires) the terminal will beep and display a message from the
  48. ;;; diary in the mini-buffer, or the user may select to have a message
  49. ;;; displayed in a new buffer.
  50. ;;;
  51. ;;; Variables of note:
  52. ;;;
  53. ;;; appt-issue-message        If this variable is nil, then the code in this
  54. ;;;                file does nothing.
  55. ;;; appt-msg-countdown-list    Specifies how much warning you want before 
  56. ;;;                appointments.
  57. ;;; appt-audible        Whether to beep when it's notification-time.
  58. ;;; appt-display-mode-line    Whether to display a countdown to the next 
  59. ;;;                appointment in the mode-line.
  60. ;;; appt-announce-method    The function used to do the notifications.
  61. ;;;    'appt-window-announce           do it in a pop-up window.
  62. ;;;     'appt-screen-announce           do it in a pop-up screen (v19 only)
  63. ;;;    'appt-message-announce           do it in the echo area.
  64. ;;;    'appt-persistent-message-announce  do it in the echo area, but make the
  65. ;;;                    messages not go away at the next keystroke.
  66. ;;; appt-display-duration    If appt-announce-method is set to the function
  67. ;;;                'appt-window-announce, this specifies how many
  68. ;;;                seconds the pop-up window should stick around.
  69. ;;;
  70. ;;; In order to use this, create a diary file, and add the following to your
  71. ;;; .emacs file:
  72. ;;;
  73. ;;;    (require 'appt)
  74. ;;;    (display-time)
  75. ;;;    (appt-initialize)
  76. ;;;
  77. ;;; If you wish to see a list of appointments, or a full calendar, when emacs
  78. ;;; starts up, you can add a call to (diary) or (calendar) after this.
  79. ;;;
  80. ;;;  This is an example of what can be in your diary file:
  81. ;;;     Monday
  82. ;;;       9:30am Coffee break
  83. ;;;      12:00pm Lunch
  84. ;;; 
  85. ;;; Based upon the above lines in your .emacs and diary files, the calendar
  86. ;;; and/or diary will be displayed when you enter emacs and your appointments
  87. ;;; list will automatically be created.  You will then be reminded at 9:20am
  88. ;;; about your coffee break and at 11:50am to go to lunch.
  89. ;;;
  90. ;;; In order to interactively add or delete items from today's list, use 
  91. ;;; Meta-x appt-add and Meta-x appt-delete.  (This does not modify your 
  92. ;;; diary file, so these will be forgotten when you exit emacs.)
  93. ;;;
  94. ;;; Additionally, the appointments list is recreated automatically at 12:01am 
  95. ;;; for those who do not logout every day or are programming late.
  96. ;;;
  97. ;;; You can have special appointments which execute arbitrary code rather than
  98. ;;; simply notifying you -- sort of like the unix "cron" facility.  The syntax
  99. ;;; for this is borrowed from the Calendar's special-date format.  If you have
  100. ;;; a diary entry like
  101. ;;;
  102. ;;;  Monday
  103. ;;;    3:00am    %%(save-all-modified-buffers)
  104. ;;;
  105. ;;; then on monday at 3AM, the function `save-all-modified-buffers' will be
  106. ;;; invoked.  (Presumably this function is defined in your .emacs file.)
  107. ;;; There will be no notification that these "special" appointments are being
  108. ;;; triggered, unless the form evaluated produces a notification.
  109. ;;;
  110. ;;; It is necessary for the entire list after the "%%" to be on one line in 
  111. ;;; your .diary file -- there may not be embedded newlines in it.  This is a
  112. ;;; bit of a misfeature.
  113. ;;;
  114. ;;; This also interacts correctly with Benjamin Pierce's reportmail.el package.
  115. ;;;
  116. ;;; Brief internal description - Skip this if your not interested!
  117. ;;;
  118. ;;; The function appt-initialize invokes 'diary' to get a list of today's
  119. ;;; appointments, and parses the lines beginning with date descriptions.
  120. ;;; This list is cached away.  'diary' is invoked in such a way so as to
  121. ;;; not pop up a window displaying the diary buffer.
  122. ;;;
  123. ;;; The function appt-check is run from the 'loadst' process (or the 'wakeup'
  124. ;;; process in emacs 18.57 or newer) which is started by invoking display-time.
  125. ;;; It checks this cached list, and announces as appropriate.  At midnight,
  126. ;;; appt-initialize is called again to rebuild this list.
  127. ;;;
  128. ;;; display-time-filter is modified to invoke appt-check.
  129. ;;;
  130. ;;; TO DO:
  131. ;;;
  132. ;;;  o  multiple adjascent appointments are not handled gracefully.  If there 
  133. ;;;     is an appointment at 3:30 and another at 3:35, and you have set things
  134. ;;;     up so that you get a notification twenty minutes before each appt,
  135. ;;;     then a notification should come at 3:10 for the first appt, and at
  136. ;;;     3:15 for the second.  Currently, no notifications are generated for an
  137. ;;;     appointment until all preceeding appointments have completely expired.
  138. ;;;
  139. ;;;  o  If there are two appointments at the same time, all but the first are
  140. ;;;     ignored (not announced.)
  141. ;;;
  142. ;;;  o  Appointments which are early enough in the morning that their 
  143. ;;;     announcements should begin before midnight are not announced until
  144. ;;;     midnight.
  145. ;;;
  146. ;;;  o  There should be some way to mark certain appointments as "important,"
  147. ;;;     so that you will be harassed about them even after they have expired.
  148.  
  149.  
  150. (require 'calendar)
  151. (require 'diary)
  152.  
  153. (defvar appt-issue-message t
  154.   "*If T, the diary buffer is checked for appointments.  For an
  155.  appointment warning to be made, the time must be the first thing on
  156.  the line.")
  157.  
  158. (defvar appt-msg-countdown-list '(20 15 10 5 3 1)
  159.   "*A list of the intervals in minutes before the appointment when
  160.  the warnings will be given.  That is, if this were the list '(5 3 1),
  161.  then a notification would be given five minutes, three minutes, and
  162.  one minute before the appointment.")
  163.  
  164. (defvar appt-check-time-syntax nil
  165.   "*Whether all diary entries are intended to beging with time specifications.
  166. Appt will beep and issue a warning message when encountering unparsable 
  167. lines.")
  168.  
  169. (defvar appt-audible t
  170.   "*Controls whether appointment announcements should beep.")
  171.  
  172. (defvar appt-display-mode-line t
  173.   "*Controls if minutes-to-appointment should be displayed on the mode line.")
  174.  
  175. (defvar appt-announce-method 'appt-window-announce
  176.   "*The name of the function used to notify the user of an impending 
  177. appointment.  This is called with two arguments, the number of minutes
  178. until the appointment, and the appointment description list.
  179.  
  180. Reasonable values for this variable are 'appt-window-announce,
  181. 'appt-message-announce, or 'appt-persistent-message-announce.")
  182.  
  183.  
  184. (defvar appt-time-msg-list nil
  185.   "The list of appointments for today.  Use appt-add and appt-delete
  186.  to add and delete appointments from list.  The original list is generated
  187.  from the today's diary-entries-list. The number before each time/message
  188.  is the time in minutes after midnight.")
  189.  
  190. (defconst max-time 1439
  191.   "11:59pm in minutes - number of minutes in a day minus 1.")
  192.  
  193. (defconst appt-check-tick -1)
  194.  
  195.  
  196. ;;; Announcement methods
  197.  
  198. (defun appt-message-announce (min-to-app appt)
  199.   "Set appt-announce-method to the name of this function to cause appointment
  200. notifications to be given via messages in the minibuffer."
  201.   (message (if (eq min-to-app 0) "App't NOW."
  202.            (format "App't in %d minute%s -- %s"
  203.                min-to-app
  204.                (if (eq 1 min-to-app) "" "s")
  205.                (car (cdr appt))))))
  206.  
  207.  
  208. (defun appt-persistent-message-announce (min-to-app appt)
  209.   "Set appt-announce-method to the name of this function to cause appointment
  210. notifications to be given via messages in the minibuffer, but have those 
  211. messages stay around even if you type something (unlike normal messages)."
  212.   (let ((str (if (eq min-to-app 0)
  213.          (format "App't NOW -- %s" (car (cdr appt)))
  214.          (format "App't in %d minute%s -- %s"
  215.              min-to-app
  216.              (if (eq 1 min-to-app) "" "s")
  217.              (car (cdr appt)))))
  218.     (in-echo-area-already (eq (selected-window) (minibuffer-window))))
  219.     (if (not in-echo-area-already)
  220.     ;; don't stomp the echo-area-buffer if reading from the minibuffer now.
  221.     (save-excursion
  222.       (save-window-excursion
  223.         (select-window (minibuffer-window))
  224.         (delete-region (point-min) (point-max))
  225.         (insert str))))
  226.     ;; if we're reading from the echo-area, and all we were going to do is
  227.     ;; clear the thing, like, don't bother, that's annoying.
  228.     (if (and in-echo-area-already (string= "" str))
  229.     nil
  230.       (message "%s" str))
  231.     ))
  232.  
  233.  
  234. (defvar appt-display-duration 5
  235.   "*The number of seconds an appointment message is displayed in its own 
  236.  window if appt-announce-method is 'appt-window-announce.")
  237.  
  238. (defun appt-window-announce (min-to-app appt)
  239.   "Set appt-announce-method to the name of this function to cause appointment 
  240. notifications to be given via messages in a pop-up window.  The variable
  241. appt-display-duration controls how long this window should be left up."
  242.   (require 'electric)
  243.   (save-excursion
  244.    (save-window-excursion
  245.     ;; Make sure we're not in the minibuffer
  246.     ;; before splitting the window.
  247.     (if (= (screen-height)
  248.            (nth 3 (window-edges (selected-window))))
  249.         nil
  250.       (appt-select-lowest-window)
  251.       (split-window))
  252.     (let ((this-buffer (current-buffer))
  253.       appt-disp-buf)
  254.       (unwind-protect
  255.        (progn
  256.          (setq appt-disp-buf (set-buffer (get-buffer-create "*appt-buf*")))
  257.          ;; set the mode-line of the pop-up window
  258.          (setq mode-line-format 
  259.            (concat "-------------------- Appointment "
  260.          (if (eq min-to-app 0)
  261.              "NOW"
  262.            (concat "in " min-to-app
  263.              (if (eq min-to-app 1) " minute" " minutes")))
  264.          ". ("
  265.          (let ((h (string-to-int
  266.                 (substring (current-time-string) 11 13))))
  267.            (concat (if (> h 12) (- h 12) h) ":"
  268.                (substring (current-time-string) 14 16)
  269.                (if (< h 12) "am" "pm")))
  270.          ") %-"))
  271.          (pop-to-buffer appt-disp-buf)
  272.          (insert-string (car (cdr appt)))
  273.          (shrink-window-if-larger-than-buffer
  274.            (get-buffer-window appt-disp-buf))
  275.          (set-buffer-modified-p nil)
  276.          (sit-for appt-display-duration))
  277.     (and appt-disp-buf (kill-buffer appt-disp-buf)))))))
  278.  
  279. (defvar appt-screen-defaults nil)
  280.  
  281. (defun appt-screen-announce (min-to-app appt)
  282.   "Set appt-announce-method to the name of this function to cause appointment 
  283. notifications to be given via messages in a pop-up screen."
  284.   (save-excursion
  285.     (setq appt-disp-buf (set-buffer (get-buffer-create "*appt-buf*")))
  286.     (erase-buffer)
  287.     ;; set the mode-line of the pop-up window
  288.     (setq mode-line-format 
  289.       (concat "-------------------- Appointment "
  290.           (if (eq min-to-app 0)
  291.               "NOW"
  292.             (concat "in " min-to-app
  293.                 (if (eq min-to-app 1) " minute" " minutes")))
  294.           ". ("
  295.           (let ((h (string-to-int
  296.                 (substring (current-time-string) 11 13))))
  297.             (concat (if (> h 12) (- h 12) h) ":"
  298.                 (substring (current-time-string) 14 16)
  299.                 (if (< h 12) "am" "pm")))
  300.           ") %-"))
  301.     (insert-string (car (cdr appt)))
  302.     (let ((height (max 10 (min 20 (+ 2 (count-lines (point-min)
  303.                             (point-max)))))))
  304.       (if (and (boundp 'appt-disp-screen) appt-disp-screen)
  305.       (let ((s (selected-screen)))
  306.         (select-screen appt-disp-screen)
  307.         (make-screen-visible appt-disp-screen)
  308.         (set-screen-height height)
  309.         (sit-for 0)
  310.         (select-screen s))
  311.     (setq appt-disp-screen
  312.           (x-create-screen
  313.            (append appt-screen-defaults
  314.                (list '(width . 80) (cons 'height height)))))))
  315.     ))
  316.  
  317. ;;; used by appt-window-announce
  318. (defun appt-select-lowest-window ()
  319.   " Determines which window is the lowest one being displayed and 
  320. selects that one."
  321.   (setq lowest-window (selected-window))
  322.   (let* ((bottom-edge (car (cdr (cdr (cdr (window-edges))))))
  323.          (last-window (previous-window))
  324.          (window-search t))
  325.     (while window-search
  326.       (let* ((this-window (next-window))
  327.              (next-bottom-edge (nth 3 (window-edges this-window))))
  328.         (if (< bottom-edge next-bottom-edge)
  329.             (progn
  330.               (setq bottom-edge next-bottom-edge)
  331.               (setq lowest-window this-window)))
  332.         (select-window this-window)
  333.         (if (eq last-window this-window)
  334.             (progn
  335.               (select-window lowest-window)
  336.               (setq window-search nil)))))))
  337.  
  338.  
  339. ;;; To display stuff in the mode line, we use a new variable instead of
  340. ;;; just adding stuff to the display-time-string -- this causes less
  341. ;;; flicker.
  342.  
  343. (defvar appt-mode-line-string ""
  344.   "*The string displayed in the mode line by the appointment package.")
  345.  
  346. (defun appt-display-mode-line (min-to-app)
  347.   "Add an appointment annotation to the mode line."
  348.   (setq appt-mode-line-string
  349.     (if (and appt-display-mode-line min-to-app)
  350.         (if (eq 0 min-to-app)
  351.         "App't NOW "
  352.         (concat "App't in " min-to-app
  353.             (if (eq 1 min-to-app) " minute  " " minutes ")))
  354.         ""))
  355.   ;; make sure our variable is visible in global-mode-string.
  356.   (cond ((not appt-display-mode-line) nil)
  357.     ((null global-mode-string)
  358.      (setq global-mode-string (list "" 'appt-mode-line-string)))
  359.     ((stringp global-mode-string)
  360.      (setq global-mode-string
  361.            (list global-mode-string 'appt-mode-line-string)))
  362.     ((not (memq 'appt-mode-line-string global-mode-string))
  363.      (setq global-mode-string
  364.            (append global-mode-string (list 'appt-mode-line-string)))))
  365.   ;; force mode line updates - from time.el
  366.   (save-excursion (set-buffer (other-buffer)))
  367.   (set-buffer-modified-p (buffer-modified-p))
  368.   (sit-for 0))
  369.  
  370.  
  371. ;;; Internal stuff
  372.  
  373. (defun appt-convert-time (time2conv)
  374.   " Convert hour:min[am/pm] format to minutes from midnight."
  375.   (cond ((string-match "^[ \t]*midni\\(ght\\|te\\)[ \t]*\\'" time2conv)
  376.      0)
  377.     ((string-match "^[ \t]*noon[ \t]*\\'" time2conv)
  378.      (* 12 60))
  379.     (t
  380.      (let ((hr 0)
  381.            (min 0))
  382.        (or (string-match
  383.          "\\`[ \t]*\\([0-9][0-9]?\\)[ \t]*\\(:[ \t]*\\([0-9][0-9]\\)\\)?[ \t]*\\(am\\|pm\\)?"
  384.          time2conv)
  385.            (error "unparsable time \"%s\"" time2conv))
  386.        (setq hr (string-to-int
  387.               (substring time2conv
  388.                  (match-beginning 1) (match-end 1))))
  389.        (if (match-beginning 3)
  390.            (setq min (string-to-int 
  391.                (substring time2conv 
  392.                       (match-beginning 3) (match-end 3)))))
  393.        ;; convert the time appointment time into 24 hour time
  394.        (if (match-beginning 4)
  395.            (progn
  396.          (if (or (= hr 0) (> hr 12))
  397.              (error "mixing 12hr and 24 hr time!  %s" time2conv))
  398.          (if (string-match "am"
  399.                    (substring time2conv (match-beginning 4)))
  400.              (if (= hr 12) (setq hr 0))
  401.            (if (< hr 12) (setq hr (+ 12 hr))))))
  402.        (if (> min 59) (error "minutes outa bounds - %s" time2conv))
  403.        (+ (* hr 60) min)))))
  404.  
  405.  
  406. (defun appt-current-time-in-seconds ()
  407.   "returns the current time in seconds since midnight."
  408.   (let* ((str (current-time-string))
  409.      (hour (string-to-int (substring str 11 13)))
  410.      (min  (string-to-int (substring str 14 16))))
  411.     (+ (* hour 60) min)))
  412.  
  413.  
  414. (defun appt-sort-list (appt-list)
  415.   (sort (copy-sequence appt-list)
  416.     (function (lambda (x y)
  417.       (< (car (car x)) (car (car y)))))))
  418.  
  419. (defun appt-diary-entries ()
  420.   (let ((list-diary-entries-hook '(appt-make-list))
  421.     (diary-display-hook nil)
  422.     (diary-list-include-blanks nil))
  423.     ;; this will set appt-time-msg-list.
  424.     (diary 1)
  425.     appt-time-msg-list))
  426.  
  427. (defun appt-initialize ()
  428.   " Read your `diary-file' and remember today's appointments.  Call this from 
  429.  your .emacs file, or any time you want your .diary file re-read (this happens 
  430.  automatically at midnight to move to notice the next day's appointments).
  431.  
  432.  The time must be at the beginning of a line for it to be put in the 
  433.  appointments list.
  434.                02/23/89
  435.                   12:00pm    lunch
  436.                 Wednesday
  437.                   10:00am    group meeting"
  438.   (install-display-time-hook)
  439.   (let ((n (length (appt-diary-entries))))
  440.     (cond ((= n 0) (message "no appointments today."))
  441.       ((= n 1) (message "1 appointment today."))
  442.       (t (message (format "%d appointments today." n))))))
  443.  
  444. (defun appt-make-list ()
  445.   "Don't call this directly; call appt-initialize or appt-diary-entries."
  446.   (setq appt-time-msg-list nil)
  447.   (if diary-entries-list
  448.       ;; Cycle through the entry-list (diary-entries-list) looking for
  449.       ;; entries beginning with a time. If the entry begins with a time,
  450.       ;; add it to the appt-time-msg-list. Then sort the list.
  451.       ;;
  452.       (let ((entry-list diary-entries-list)
  453.         (new-appts '()))
  454.     (while (and entry-list
  455.             (calendar-date-equal
  456.               (calendar-current-date) (car (car entry-list))))
  457.       (let ((time-string (car (cdr (car entry-list)))))
  458.         (while (string-match
  459.             "\\`[ \t\n]*\\([0-9]?[0-9]\\(:[0-9][0-9]\\)?[ \t]*\\(am\\|pm\\)?\\|noon\\|midnight\\|midnite\\).*$"
  460.              time-string)
  461.           (let* ((eol (match-end 0))
  462.              (appt-time-string
  463.               (substring time-string (match-beginning 1)
  464.                  (match-end 1)))
  465.              (appt-msg-string
  466.               (substring time-string (match-end 1) eol))
  467.              (appt-time (list (appt-convert-time appt-time-string))))
  468.         (setq time-string (substring time-string eol)
  469.               new-appts (cons (cons appt-time
  470.                         (list (concat appt-time-string ":"
  471.                               appt-msg-string)))
  472.                       new-appts))))
  473.         (if appt-check-time-syntax
  474.         (while (string-match "\n*\\([^\n]+\\)$" time-string)
  475.           (beep)
  476.           (message "Unparsable time: %s"
  477.                (substring time-string (match-beginning 1)
  478.                       (match-end 1)))
  479.           (sit-for 3)
  480.           (setq time-string (substring time-string (match-end 0)))))
  481.                            
  482.         )
  483.       (setq entry-list (cdr entry-list)))
  484.     (setq appt-time-msg-list ; seems we can't nconc this list...
  485.           (append (nreverse new-appts) appt-time-msg-list))))
  486.   (setq appt-time-msg-list (appt-sort-list appt-time-msg-list))
  487.   ;;
  488.   ;; Get the current time and convert it to minutes from midnight. ie. 12:01am
  489.   ;; = 1, midnight = 0, so that the elements in the list that are earlier than
  490.   ;; the present time can be removed.
  491.   ;;
  492.   (let ((cur-comp-time (appt-current-time-in-seconds))
  493.     (appt-comp-time (car (car (car appt-time-msg-list)))))
  494.     (while (and appt-time-msg-list (< appt-comp-time cur-comp-time))
  495.       (setq appt-time-msg-list (cdr appt-time-msg-list)) 
  496.       (if appt-time-msg-list
  497.           (setq appt-comp-time (car (car (car appt-time-msg-list)))))))
  498.   appt-time-msg-list)
  499.  
  500.  
  501. (defun appt-beep ()
  502.   (cond ((null appt-audible) nil)
  503.     ((numberp appt-audible)
  504.      (let ((i appt-audible))
  505.        (while (> i 0) (beep) (setq i (1- i)))))
  506.     ((consp appt-audible)
  507.      (let ((i (car appt-audible))
  508.            (j (cdr appt-audible)))
  509.        (if (consp j) (setq j (car j)))
  510.        (while (> i 0)
  511.          (beep)
  512.          (sleep-for-millisecs j)
  513.          (setq i (1- i)))))
  514.     (t (beep))))
  515.  
  516.  
  517. (defun appt-check ()
  518.   "Check for an appointment and update the mode line and minibuffer if
  519.  desired. Note: the time must be the first thing in the line in the diary
  520.  for a warning to be issued.
  521.   The format of the time can be either 24 hour or am/pm.  Example: 
  522.  
  523.                02/23/89
  524.                  18:00 Dinner
  525.               Thursday
  526.                 11:45am Lunch meeting.
  527.   
  528.  The following variables control the action of the notification:
  529.  
  530.  appt-issue-message        If this variable is nil, then the code in this
  531.                 file does nothing.
  532.  appt-msg-countdown-list    Specifies how much warning you want before 
  533.                 appointments.
  534.  appt-audible            Whether to beep when it's notification-time.
  535.  appt-display-mode-line        Whether to display a countdown to the next 
  536.                 appointment in the mode-line.
  537.  appt-announce-method       The function used to do the notifications.
  538.                 'appt-window-announce to do it in a pop-up
  539.                 window, 'appt-message-announce or 
  540.                 'appt-persistent-message-announce to do it 
  541.                 in the echo-area.
  542.  appt-display-duration      If appt-announce-method is set to the function
  543.                 'appt-window-announce, this specifies how many
  544.                 seconds the pop-up window should stick around.
  545.  
  546.  This function is run from the `loadst' or `wakeup' process for display-time.
  547.  Therefore, you need to have (display-time) in your .emacs file."
  548.   (if appt-issue-message
  549.    (let ((min-to-app -1)
  550.      (new-time ""))
  551.      ;; Get the current time and convert it to minutes
  552.      ;; from midnight. ie. 12:01am = 1, midnight = 0.
  553.      (let* ((cur-comp-time (appt-current-time-in-seconds))
  554.         ;; If the current time is the same as the tick, just return.
  555.         ;; This means that this function has been called more than once
  556.         ;; in the current minute, which is not useful.
  557.         (shut-up-this-time (= cur-comp-time appt-check-tick))
  558.         (turnover-p (> appt-check-tick cur-comp-time)))
  559.        (setq appt-check-tick cur-comp-time)
  560.        ;;
  561.        ;; If it is now the next day (we have crossed midnight since the last
  562.        ;; time this was called) then we should update our appointments to
  563.        ;; today's list.
  564.        (if turnover-p (appt-diary-entries))
  565.        ;;
  566.        ;; Get the first time off of the list and calculate the number
  567.        ;; of minutes until the appointment.
  568.        (if appt-time-msg-list
  569.        (let ((appt-comp-time (car (car (car appt-time-msg-list)))))
  570.          (setq min-to-app (- appt-comp-time cur-comp-time))
  571.          (while (and appt-time-msg-list (< appt-comp-time cur-comp-time))
  572.            (setq appt-time-msg-list (cdr appt-time-msg-list)) 
  573.            (if appt-time-msg-list
  574.            (setq appt-comp-time (car (car (car appt-time-msg-list))))))
  575.          ;;
  576.          ;; If we have an appointment between midnight and warning-time
  577.          ;; minutes after midnight, we must begin to issue a message
  578.          ;; before midnight.  Midnight is considered 0 minutes and 11:59pm
  579.          ;; is 1439 minutes. Therefore we must recalculate the minutes to
  580.          ;; appointment variable. It is equal to the number of minutes
  581.          ;; before midnight plus the number of minutes after midnight our
  582.          ;; appointment is.
  583.          ;;
  584.          ;; ## I don't think this does anything -- it would if it were
  585.          ;; (for example) a 12:01am appt on the list at 11:55pm, but that
  586.          ;; can't ever happen, because the applicable 12:01am appt is for
  587.          ;; tomorrow, not today, and we only have today's diary list.
  588.          ;; It's not simply a matter of concatenating two days together,
  589.          ;; either, because then tuesday's appts would be signalled on
  590.          ;; monday.  We have to do a real one-day lookahead -- keep a list
  591.          ;; of tomorrow's appts, and check it when near midnight.
  592.          ;;
  593.          (if (and (< appt-comp-time (apply 'max appt-msg-countdown-list))
  594.               (> (+ cur-comp-time (apply 'max appt-msg-countdown-list))
  595.              max-time))
  596.          (setq min-to-app (+ (- (1+ max-time) cur-comp-time))
  597.                appt-comp-time))
  598.          ;;
  599.          ;; issue warning if the appointment time is within warning-time
  600.          (cond
  601.            ;; if there should not be any notifications in the mode-line,
  602.            ;; clear it.
  603.            ((> min-to-app (apply 'max appt-msg-countdown-list))
  604.         (appt-display-mode-line nil))
  605.            ;; do nothing if this is the second time this minute we've
  606.            ;; gotten here, of if we shouldn't be notifying right now.
  607.            ((or shut-up-this-time
  608.             (and (not (= min-to-app 0))
  609.              (not (memq min-to-app appt-msg-countdown-list))))
  610.         nil)
  611.  
  612.            ((and (= min-to-app 0)
  613.              (string-match "%%(" (nth 1 (car appt-time-msg-list))))
  614.         ;;
  615.         ;; If this is a magic evaluating-notification, evaluate it.
  616.         ;; these kinds of notifications aren't subject to the
  617.         ;; appt-msg-countdown-list.
  618.         ;;
  619.         (let* ((list-string (substring (nth 1 (car appt-time-msg-list))
  620.                            (1- (match-end 0))))
  621.                (form (condition-case ()
  622.                  (read list-string)
  623.                    (error
  624.                  (ding)
  625.                  (message "Appt: error reading from \"%s\""
  626.                       (nth 1 (car appt-time-msg-list)))
  627.                  (sit-for 2)
  628.                  nil))))
  629.           (eval form)))
  630.  
  631.            ((and (<= min-to-app (apply 'max appt-msg-countdown-list))
  632.              (>= min-to-app 0))
  633.         ;;
  634.         ;; produce a notification.
  635.         (appt-beep)
  636.         (funcall appt-announce-method min-to-app
  637.              (car appt-time-msg-list))
  638.         ;; update mode line and expire if necessary
  639.         (appt-display-mode-line min-to-app)
  640.         ;; if it's expired, remove it.
  641.         (if (= min-to-app 0)
  642.             (setq appt-time-msg-list (cdr appt-time-msg-list))))
  643.            (t
  644.         ;; else we're not near any appointment, or there are no
  645.         ;; apointments; make sure mode line is clear.
  646.         (appt-display-mode-line nil))))
  647.        (appt-display-mode-line nil))))))
  648.  
  649.  
  650.  
  651. ;;; Interactively adding and deleting appointments
  652.  
  653. (defun appt-add (new-appt-time new-appt-msg)
  654.   "Adds an appointment to the list of appointments for the day at TIME
  655.  and issue MESSAGE. The time should be in either 24 hour format or
  656.  am/pm format. "
  657.  
  658.   (interactive "sTime (hh:mm[am/pm]): \nsMessage: ")
  659.   (if (string-match "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?" new-appt-time)
  660.       nil
  661.     (error "Unacceptable time-string"))
  662.   
  663.   (let* ((appt-time-string (concat new-appt-time " " new-appt-msg))
  664.          (appt-time (list (appt-convert-time new-appt-time)))
  665.          (time-msg (cons appt-time (list appt-time-string))))
  666.     (setq appt-time-msg-list (append appt-time-msg-list
  667.                                      (list time-msg)))
  668.     (setq appt-time-msg-list (appt-sort-list appt-time-msg-list)))) 
  669.  
  670. (defun appt-delete ()
  671.   "Deletes an appointment from the list of appointments."
  672.   (interactive)
  673.   (let* ((tmp-msg-list appt-time-msg-list))
  674.     (while tmp-msg-list
  675.       (let* ((element (car tmp-msg-list))
  676.              (prompt-string (concat "Delete " 
  677.                                     (prin1-to-string (car (cdr element))) 
  678.                                     " from list? "))
  679.              (test-input (y-or-n-p prompt-string)))
  680.         (setq tmp-msg-list (cdr tmp-msg-list))
  681.         (if test-input
  682.             (setq appt-time-msg-list (delq element appt-time-msg-list)))
  683.         (setq tmp-appt-msg-list nil)))
  684.     (message "")))
  685.  
  686.  
  687. ;;; Patching in to existing time code to install our hook.
  688.  
  689. (defvar display-time-hook nil
  690.   "*List of functions to be called when the time is updated on the mode line.")
  691.  
  692. (setq display-time-hook 'appt-check)
  693.  
  694. (defvar display-time-hook-installed nil)
  695.  
  696. (defun install-display-time-hook ()
  697.  (if display-time-hook-installed         ;; only do this stuff once!
  698.     nil
  699.   (let ((old-fn (if (and (fboundp 'display-time-filter-18-55) ; reportmail.el
  700.              (fboundp 'display-time-filter-18-57))
  701.             (if (and (featurep 'timer)  ; Lucid GNU Emacs reportmail.el
  702.                  (fboundp 'display-time-timer-function))
  703.             'display-time-timer-function
  704.               ;; older reportmail, or no timer.el.
  705.               (if (string-match "18\\.5[0-5]" (emacs-version))
  706.               'display-time-filter-18-55
  707.             'display-time-filter-18-57))
  708.           ;; othewise, time.el
  709.           (if (and (featurep 'timer)
  710.                (fboundp 'display-time-function)) ; Lucid GNU Emacs
  711.               'display-time-function
  712.             'display-time-filter))))
  713.     ;; we're about to redefine it...
  714.     (fset 'old-display-time-filter (symbol-function old-fn))
  715.     (fset old-fn
  716.       (function (lambda (&rest args)  ;; ...here's the revised definition
  717.         "Revised version of the original function: this version calls a hook."
  718.         (apply 'old-display-time-filter args)
  719.         (run-hooks 'display-time-hook)))))
  720.   (setq display-time-hook-installed t)
  721.   ))
  722.  
  723. (provide 'appt)
  724.