home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / emacs-19.28-src.tgz / tar.out / fsf / emacs / lisp / appt.el < prev    next >
Lisp/Scheme  |  1996-09-28  |  20KB  |  596 lines

  1. ;;; appt.el --- appointment notification functions.
  2.  
  3. ;; Copyright (C) 1989, 1990, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Neil Mager <neilm@juliet.ll.mit.edu>
  6. ;; Maintainer: FSF
  7. ;; Keywords: calendar
  8.  
  9. ;; This file is part of GNU Emacs.
  10.  
  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)
  14. ;; any later version.
  15.  
  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.
  20.  
  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
  23. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;;
  28. ;; appt.el - visible and/or audible notification of
  29. ;;           appointments from ~/diary file generated from
  30. ;;           Edward M. Reingold's calendar.el.
  31. ;;
  32. ;;
  33. ;; Comments, corrections, and improvements should be sent to
  34. ;; Neil M. Mager
  35. ;; Net                     <neilm@juliet.ll.mit.edu>
  36. ;; Voice                   (617) 981-4803
  37. ;;;
  38. ;;; Thanks to  Edward M. Reingold for much help and many suggestions, 
  39. ;;; And to many others for bug fixes and suggestions.
  40. ;;;
  41. ;;;
  42. ;;; This functions in this file will alert the user of a 
  43. ;;; pending appointment based on their diary file.
  44. ;;;
  45. ;;;
  46. ;;; ******* It is necessary to invoke 'display-time' ********
  47. ;;; *******  and 'diary' for this to work properly.  ********
  48. ;;; 
  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.
  53. ;;;
  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.
  58. ;;; 
  59. ;;; In order to use, the following should be in your .emacs file in addition to
  60. ;;; creating a diary file and invoking calendar:
  61. ;;;
  62. ;;;    Set some options
  63. ;;; (setq view-diary-entries-initially t)
  64. ;;; (setq appt-issue-message t)
  65. ;;;
  66. ;;;   The following three lines are required:
  67. ;;; (display-time)
  68. ;;; (add-hook 'diary-hook 'appt-make-list)
  69. ;;;
  70. ;;; 
  71. ;;;  This is an example of what can be in your diary file:
  72. ;;; Monday
  73. ;;;   9:30am Coffee break
  74. ;;;  12:00pm Lunch        
  75. ;;; 
  76. ;;; Based upon the above lines in your .emacs and diary files, 
  77. ;;; the calendar and diary will be displayed when you enter
  78. ;;; emacs and your appointments list will automatically be created.
  79. ;;; You will then be reminded at 9:20am about your coffee break
  80. ;;; and at 11:50am to go to lunch. 
  81. ;;;
  82. ;;; Use describe-function on appt-check for a description of other variables
  83. ;;; that can be used to personalize the notification system.
  84. ;;;
  85. ;;;  In order to add or delete items from todays list, use appt-add
  86. ;;;  and appt-delete.
  87. ;;;
  88. ;;;  Additionally, the appointments list is recreated automatically
  89. ;;;  at 12:01am for those who do not logout every day or are programming
  90. ;;;  late.
  91. ;;;
  92. ;;; Brief internal description - Skip this if your not interested!
  93. ;;;
  94. ;;; The function appt-check is run from the 'loadst' process which is started
  95. ;;; by invoking (display-time). A temporary function below modifies
  96. ;;; display-time-filter 
  97. ;;; (from original time.el) to include a hook which will invoke appt-check.
  98. ;;; This will not be necessary in the next version of gnuemacs.
  99. ;;;
  100. ;;;
  101. ;;; The function appt-make-list creates the appointments list which appt-check
  102. ;;; reads. This is all done automatically.
  103. ;;; It is invoked from the function list-diary-entries.
  104. ;;;
  105. ;;; You can change the way the appointment window is created/deleted by
  106. ;;; setting  the variables
  107. ;;;
  108. ;;;         appt-disp-window-function
  109. ;;; and
  110. ;;;          appt-delete-window-function
  111. ;;;
  112. ;;; For instance, these variables can be set to functions that display
  113. ;;; appointments in pop-up frames, which are lowered or iconified after
  114. ;;; appt-display-interval seconds.
  115. ;;;
  116.  
  117. ;;; Code:
  118.  
  119. ;; Make sure calendar is loaded when we compile this.
  120. (require 'calendar)
  121.  
  122. ;;;###autoload
  123. (defvar appt-issue-message t
  124.   "*Non-nil means check for appointments in the diary buffer.
  125. To be detected, the diary entry must have the time
  126. as the first thing on a line.")
  127.  
  128. ;;;###autoload
  129. (defvar appt-message-warning-time 12
  130.   "*Time in minutes before an appointment that the warning begins.")
  131.  
  132. ;;;###autoload
  133. (defvar appt-audible t
  134.   "*Non-nil means beep to indicate appointment.")
  135.  
  136. ;;;###autoload
  137. (defvar appt-visible t
  138.   "*Non-nil means display appointment message in echo area.")
  139.  
  140. ;;;###autoload
  141. (defvar appt-display-mode-line t
  142.   "*Non-nil means display minutes to appointment and time on the mode line.")
  143.  
  144. ;;;###autoload
  145. (defvar appt-msg-window t
  146.   "*Non-nil means display appointment message in another window.")
  147.  
  148. ;;;###autoload
  149. (defvar appt-display-duration 10
  150.   "*The number of seconds an appointment message is displayed.")
  151.  
  152. ;;;###autoload
  153. (defvar appt-display-diary t
  154.   "*Non-nil means to display the next days diary on the screen. 
  155. This will occur at midnight when the appointment list is updated.")
  156.  
  157. (defvar appt-time-msg-list nil
  158.   "The list of appointments for today.
  159. Use `appt-add' and `appt-delete' to add and delete appointments from list.
  160. The original list is generated from the today's `diary-entries-list'.
  161. The number before each time/message is the time in minutes from midnight.")
  162.  
  163. (defconst max-time 1439
  164.   "11:59pm in minutes - number of minutes in a day minus 1.")
  165.  
  166. (defvar appt-display-interval 3
  167.   "*Number of minutes to wait between checking the appointment list.")
  168.   
  169. (defvar appt-buffer-name " *appt-buf*"
  170.   "Name of the appointments buffer.")
  171.   
  172. (defvar appt-disp-window-function 'appt-disp-window
  173.   "Function called to display appointment window.")
  174.   
  175. (defvar appt-delete-window-function 'appt-delete-window
  176.   "Function called to remove appointment window and buffer.")
  177.  
  178. (defun appt-check ()
  179.   "Check for an appointment and update the mode line.
  180. Note: the time must be the first thing in the line in the diary
  181. for a warning to be issued.
  182.  
  183. The format of the time can be either 24 hour or am/pm.
  184. Example: 
  185.  
  186.                02/23/89
  187.                  18:00 Dinner
  188.             
  189.               Thursday
  190.                 11:45am Lunch meeting.
  191.  
  192. The following variables control the action of the notification:
  193.  
  194. appt-issue-message
  195.     If T, the diary buffer is checked for appointments.
  196.  
  197. appt-message-warning-time
  198.     Variable used to determine if appointment message
  199.     should be displayed.
  200.  
  201. appt-audible
  202.     Variable used to determine if appointment is audible.
  203.     Default is t.
  204.  
  205. appt-visible
  206.     Variable used to determine if appointment message should be
  207.     displayed in the mini-buffer. Default is t.
  208.  
  209. appt-msg-window
  210.     Variable used to determine if appointment message
  211.     should temporarily appear in another window. Mutually exclusive
  212.     to appt-visible.
  213.  
  214. appt-display-duration
  215.     The number of seconds an appointment message
  216.     is displayed in another window.
  217.  
  218. appt-display-interval
  219.     The number of minutes to wait between checking the appointments
  220.     list.
  221.  
  222. appt-disp-window-function 
  223.         Function called to display appointment window. You can customize
  224.     appt.el by setting this variable to a function different from the
  225.     one provided with this package.
  226.   
  227. appt-delete-window-function 
  228.         Function called to remove appointment window and buffer.  You can
  229.     customize appt.el by setting this variable to a function different
  230.     from the one provided with this package.
  231.  
  232. This function is run from the loadst process for display time.
  233. Therefore, you need to have `(display-time)' in your .emacs file."
  234.  
  235.  
  236.   (if (or (= appt-display-interval 1)
  237.       ;; This is true every appt-display-interval minutes.
  238.       (= 0 (mod (/ (nth 1 (current-time)) 60) appt-display-interval)))
  239.       (let ((min-to-app -1)
  240.         (new-time ""))
  241.     (save-excursion
  242.  
  243.       ;; Get the current time and convert it to minutes
  244.       ;; from midnight. ie. 12:01am = 1, midnight = 0.
  245.  
  246.       (let* ((cur-hour(string-to-int 
  247.                (substring (current-time-string) 11 13)))
  248.          (cur-min (string-to-int 
  249.                (substring (current-time-string) 14 16)))
  250.          (cur-comp-time (+ (* cur-hour 60) cur-min)))
  251.  
  252.         ;; At the first check after 12:01am, we should update our 
  253.         ;; appointments to today's list.
  254.  
  255.         (if (and (>= cur-comp-time 1)
  256.              (<= cur-comp-time appt-display-interval))
  257.         (if (and view-diary-entries-initially appt-display-diary)
  258.             (diary)
  259.           (let ((diary-display-hook 'appt-make-list))
  260.             (diary))))
  261.  
  262.         ;; If there are entries in the list, and the
  263.         ;; user wants a message issued
  264.         ;; get the first time off of the list
  265.         ;; and calculate the number of minutes until
  266.         ;; the appointment.
  267.  
  268.         (if (and appt-issue-message appt-time-msg-list)
  269.         (let ((appt-comp-time (car (car (car appt-time-msg-list)))))
  270.           (setq min-to-app (- appt-comp-time cur-comp-time))
  271.  
  272.           (while (and appt-time-msg-list 
  273.                   (< appt-comp-time cur-comp-time))
  274.             (setq appt-time-msg-list (cdr appt-time-msg-list)) 
  275.             (if appt-time-msg-list
  276.             (setq appt-comp-time 
  277.                   (car (car (car appt-time-msg-list))))))
  278.  
  279.           ;; If we have an appointment between midnight and
  280.           ;; 'appt-message-warning-time' minutes after midnight,
  281.           ;; we must begin to issue a message before midnight.
  282.           ;; Midnight is considered 0 minutes and 11:59pm is
  283.           ;; 1439 minutes. Therefore we must recalculate the minutes
  284.           ;; to appointment variable. It is equal to the number of 
  285.           ;; minutes before midnight plus the number of 
  286.           ;; minutes after midnight our appointment is.
  287.  
  288.           (if (and (< appt-comp-time appt-message-warning-time)
  289.                (> (+ cur-comp-time appt-message-warning-time)
  290.                   max-time))
  291.               (setq min-to-app (+ (- (1+ max-time) cur-comp-time))
  292.                 appt-comp-time))
  293.  
  294.           ;; issue warning if the appointment time is 
  295.           ;; within appt-message-warning time
  296.  
  297.           (if (and (<= min-to-app appt-message-warning-time)
  298.                (>= min-to-app 0))
  299.               (progn
  300.             (if appt-msg-window
  301.                 (progn
  302.                   (string-match
  303.                    "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?" 
  304.                    display-time-string)
  305.  
  306.                   (setq new-time (substring display-time-string 
  307.                             (match-beginning 0)
  308.                             (match-end 0)))
  309.                   (funcall
  310.                    appt-disp-window-function
  311.                    min-to-app new-time
  312.                    (car (cdr (car appt-time-msg-list))))
  313.                   
  314.                   (run-at-time
  315.                    (format "%d sec" appt-display-duration)
  316.                    nil
  317.                    appt-delete-window-function))
  318.               ;;; else
  319.  
  320.               (if appt-visible
  321.                   (message "%s" 
  322.                        (car (cdr (car appt-time-msg-list)))))
  323.  
  324.               (if appt-audible
  325.                   (beep 1)))
  326.  
  327.             (if appt-display-mode-line
  328.                 (progn
  329.                   (string-match
  330.                    "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?" 
  331.                    display-time-string)
  332.  
  333.                   (setq new-time (substring display-time-string 
  334.                             (match-beginning 0)
  335.                             (match-end 0)))
  336.                   (setq display-time-string
  337.                     (concat  "App't in "
  338.                          min-to-app " min. " new-time " "))
  339.  
  340.                   ;; force mode line updates - from time.el
  341.  
  342.                   (save-excursion (set-buffer (other-buffer)))
  343.                   (set-buffer-modified-p (buffer-modified-p))
  344.                   (sit-for 0)))
  345.  
  346.             (if (= min-to-app 0)
  347.                 (setq appt-time-msg-list
  348.                   (cdr appt-time-msg-list))))))))))))
  349.  
  350.  
  351. ;; Display appointment message in a separate buffer.
  352. (defun appt-disp-window (min-to-app new-time appt-msg)
  353.   (require 'electric)
  354.  
  355.   ;; Make sure we're not in the minibuffer
  356.   ;; before splitting the window.
  357.  
  358.   (if (equal (selected-window) (minibuffer-window))
  359.       (if (other-window 1) 
  360.       (select-window (other-window 1))
  361.     (if window-system
  362.         (select-frame (other-frame 1)))))
  363.       
  364.   (let* ((this-buffer (current-buffer))
  365.      (this-window (selected-window))
  366.      (appt-disp-buf (set-buffer (get-buffer-create appt-buffer-name))))
  367.  
  368.     (appt-select-lowest-window)
  369.     (split-window)
  370.       
  371.     (pop-to-buffer appt-disp-buf)
  372.     (setq mode-line-format 
  373.       (concat "-------------------- Appointment in "
  374.           min-to-app " minutes. " new-time " %-"))
  375.     (insert-string appt-msg)
  376.     (shrink-window-if-larger-than-buffer (get-buffer-window appt-disp-buf))
  377.     (set-buffer-modified-p nil)
  378.     (select-window this-window)
  379.     (if appt-audible
  380.     (beep 1))))
  381.       
  382. (defun appt-delete-window ()
  383.   "Function called to undisplay appointment messages.
  384. Usually just deletes the appointment buffer."
  385.   (delete-window (get-buffer-window appt-buffer-name))
  386.   (kill-buffer appt-buffer-name)
  387.   (if appt-audible
  388.       (beep 1)))
  389.  
  390. ;; Select the lowest window on the frame.
  391. (defun appt-select-lowest-window ()
  392.   (setq lowest-window (selected-window))
  393.   (let* ((bottom-edge (car (cdr (cdr (cdr (window-edges))))))
  394.          (last-window (previous-window))
  395.          (window-search t))
  396.     (while window-search
  397.       (let* ((this-window (next-window))
  398.              (next-bottom-edge (car (cdr (cdr (cdr 
  399.                                                (window-edges this-window)))))))
  400.         (if (< bottom-edge next-bottom-edge)
  401.             (progn
  402.               (setq bottom-edge next-bottom-edge)
  403.               (setq lowest-window this-window)))
  404.  
  405.         (select-window this-window)
  406.         (if (eq last-window this-window)
  407.             (progn
  408.               (select-window lowest-window)
  409.               (setq window-search nil)))))))
  410.  
  411.  
  412. (defun appt-add (new-appt-time new-appt-msg)
  413.   "Add an appointment for the day at TIME and issue MESSAGE.
  414. The time should be in either 24 hour format or am/pm format."
  415.  
  416.   (interactive "sTime (hh:mm[am/pm]): \nsMessage: ")
  417.   (if (string-match "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?" new-appt-time)
  418.       nil
  419.     (error "Unacceptable time-string"))
  420.   
  421.   (let* ((appt-time-string (concat new-appt-time " " new-appt-msg))
  422.          (appt-time (list (appt-convert-time new-appt-time)))
  423.          (time-msg (cons appt-time (list appt-time-string))))
  424.     (setq appt-time-msg-list (append appt-time-msg-list
  425.                                      (list time-msg)))
  426.     (setq appt-time-msg-list (appt-sort-list appt-time-msg-list)))) 
  427.  
  428. (defun appt-delete ()
  429.   "Delete an appointment from the list of appointments."
  430.   (interactive)
  431.   (let* ((tmp-msg-list appt-time-msg-list))
  432.     (while tmp-msg-list
  433.       (let* ((element (car tmp-msg-list))
  434.              (prompt-string (concat "Delete " 
  435.                                     (prin1-to-string (car (cdr element))) 
  436.                                     " from list? "))
  437.              (test-input (y-or-n-p prompt-string)))
  438.         (setq tmp-msg-list (cdr tmp-msg-list))
  439.         (if test-input
  440.             (setq appt-time-msg-list (delq element appt-time-msg-list)))
  441.         (setq tmp-appt-msg-list nil)))
  442.     (message "")))
  443.                  
  444.  
  445. ;; Create the appointments list from todays diary buffer.
  446. ;; The time must be at the beginning of a line for it to be
  447. ;; put in the appointments list.
  448. ;;                02/23/89
  449. ;;                  12:00pm lunch
  450. ;;                 Wednesday
  451. ;;                   10:00am group meeting
  452. ;; We assume that the variables DATE and NUMBER
  453. ;; hold the arguments that list-diary-entries received.
  454. ;; They specify the range of dates that the diary is being processed for.
  455.  
  456. ;;;###autoload
  457. (defun appt-make-list ()
  458.   ;; We have something to do if the range of dates that the diary is
  459.   ;; considering includes the current date.
  460.   (if (and (not (calendar-date-compare
  461.          (list (calendar-current-date))
  462.          (list original-date)))
  463.        (calendar-date-compare
  464.         (list (calendar-current-date))
  465.             (list (calendar-gregorian-from-absolute
  466.            (+ (calendar-absolute-from-gregorian original-date)
  467.               number)))))
  468.       (save-excursion
  469.     ;; Clear the appointments list, then fill it in from the diary.
  470.     (setq appt-time-msg-list nil)
  471.     (if diary-entries-list
  472.  
  473.         ;; Cycle through the entry-list (diary-entries-list)
  474.         ;; looking for entries beginning with a time. If 
  475.         ;; the entry begins with a time, add it to the
  476.         ;; appt-time-msg-list. Then sort the list.
  477.  
  478.         (let ((entry-list diary-entries-list)
  479.           (new-time-string ""))
  480.           ;; Skip diary entries for dates before today.
  481.           (while (and entry-list
  482.               (calendar-date-compare
  483.                (car entry-list) (list (calendar-current-date))))
  484.         (setq entry-list (cdr entry-list)))
  485.           ;; Parse the entries for today.
  486.           (while (and entry-list 
  487.               (calendar-date-equal 
  488.                (calendar-current-date) (car (car entry-list))))
  489.         (let ((time-string (substring (prin1-to-string 
  490.                            (cdr (car entry-list))) 2 -2)))
  491.  
  492.           (while (string-match
  493.               "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?.*" 
  494.               time-string)
  495.             (let* ((appt-time-string (substring time-string
  496.                             (match-beginning 0)
  497.                             (match-end 0))))
  498.  
  499.               (if (< (match-end 0) (length time-string))
  500.               (setq new-time-string (substring time-string 
  501.                                (+ (match-end 0) 1)
  502.                                nil))
  503.             (setq new-time-string ""))
  504.  
  505.               (string-match "[0-9]?[0-9]:[0-9][0-9]\\(am\\|pm\\)?"
  506.                     time-string)
  507.  
  508.               (let* ((appt-time (list (appt-convert-time 
  509.                            (substring time-string
  510.                               (match-beginning 0)
  511.                               (match-end 0)))))
  512.                  (time-msg (cons appt-time
  513.                          (list appt-time-string))))
  514.             (setq time-string new-time-string)
  515.             (setq appt-time-msg-list (append appt-time-msg-list
  516.                              (list time-msg)))))))
  517.         (setq entry-list (cdr entry-list)))))
  518.     (setq appt-time-msg-list (appt-sort-list appt-time-msg-list))
  519.  
  520.     ;; Get the current time and convert it to minutes
  521.     ;; from midnight. ie. 12:01am = 1, midnight = 0,
  522.     ;; so that the elements in the list
  523.     ;; that are earlier than the present time can
  524.     ;; be removed.
  525.  
  526.     (let* ((cur-hour(string-to-int 
  527.              (substring (current-time-string) 11 13)))
  528.            (cur-min (string-to-int 
  529.              (substring (current-time-string) 14 16)))
  530.            (cur-comp-time (+ (* cur-hour 60) cur-min))
  531.            (appt-comp-time (car (car (car appt-time-msg-list)))))
  532.  
  533.       (while (and appt-time-msg-list (< appt-comp-time cur-comp-time))
  534.         (setq appt-time-msg-list (cdr appt-time-msg-list)) 
  535.         (if appt-time-msg-list
  536.         (setq appt-comp-time (car (car (car appt-time-msg-list))))))))))
  537.   
  538.  
  539. ;;Simple sort to put the appointments list in order.
  540. ;;Scan the list for the smallest element left in the list.
  541. ;;Append the smallest element left into the new list, and remove
  542. ;;it from the original list.
  543. (defun appt-sort-list (appt-list)
  544.   (let ((order-list nil))
  545.     (while appt-list
  546.       (let* ((element (car appt-list))
  547.              (element-time (car (car element)))
  548.              (tmp-list (cdr appt-list)))
  549.         (while tmp-list
  550.           (if (< element-time (car (car (car tmp-list))))
  551.               nil
  552.             (setq element (car tmp-list))
  553.             (setq element-time (car (car element))))
  554.           (setq tmp-list (cdr tmp-list)))
  555.         (setq order-list (append order-list (list element)))
  556.         (setq appt-list (delq element appt-list))))
  557.     order-list))
  558.  
  559.  
  560. (defun appt-convert-time (time2conv)
  561.   "Convert hour:min[am/pm] format to minutes from midnight."
  562.  
  563.   (let ((conv-time 0)
  564.         (hr 0)
  565.         (min 0))
  566.  
  567.     (string-match ":[0-9][0-9]" time2conv)
  568.     (setq min (string-to-int 
  569.                (substring time2conv 
  570.                           (+ (match-beginning 0) 1) (match-end 0))))
  571.   
  572.     (string-match "[0-9]?[0-9]:" time2conv)
  573.     (setq hr (string-to-int 
  574.               (substring time2conv 
  575.                          (match-beginning 0)
  576.                          (match-end 0))))
  577.   
  578.     ;; convert the time appointment time into 24 hour time
  579.   
  580.     (if (and (string-match  "[p][m]" time2conv) (< hr 12))
  581.         (progn
  582.           (string-match "[0-9]?[0-9]:" time2conv)
  583.           (setq hr (+ 12 hr))))
  584.   
  585.     ;; convert the actual time
  586.     ;; into minutes for comparison
  587.     ;; against the actual time.
  588.   
  589.     (setq conv-time (+ (* hr 60) min))
  590.     conv-time))
  591.  
  592. (add-hook 'display-time-hook 'appt-check)
  593.  
  594. ;;; appt.el ends here
  595.  
  596.