Allow per-appointment warning times.
[bpt/emacs.git] / lisp / calendar / appt.el
1 ;;; appt.el --- appointment notification functions
2
3 ;; Copyright (C) 1989-1990, 1994, 1998, 2001-2011
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Neil Mager <neilm@juliet.ll.mit.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; Keywords: calendar
9 ;; Package: calendar
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;;
29 ;; appt.el - visible and/or audible notification of
30 ;; appointments from diary file.
31 ;;
32 ;;
33 ;; Thanks to Edward M. Reingold for much help and many suggestions,
34 ;; And to many others for bug fixes and suggestions.
35 ;;
36 ;;
37 ;; This functions in this file will alert the user of a
38 ;; pending appointment based on his/her diary file. This package
39 ;; is documented in the Emacs manual.
40 ;;
41 ;; To activate this package, simply use (appt-activate 1).
42 ;; A `diary-file' with appointments of the format described in the
43 ;; documentation of the function `appt-check' is required.
44 ;; Relevant customizable variables are also listed in the
45 ;; documentation of that function.
46 ;;
47 ;; Today's appointment list is initialized from the diary when this
48 ;; package is activated. Additionally, the appointments list is
49 ;; recreated automatically at 12:01am for those who do not logout
50 ;; every day or are programming late. It is also updated when the
51 ;; `diary-file' (or a file it includes) is saved. Calling
52 ;; `appt-check' with an argument (or re-enabling the package) forces a
53 ;; re-initialization at any time.
54 ;;
55 ;; In order to add or delete items from today's list, without
56 ;; changing the diary file, use `appt-add' and `appt-delete'.
57 ;;
58
59 ;; Brief internal description - Skip this if you are not interested!
60 ;;
61 ;; The function `appt-make-list' creates the appointments list which
62 ;; `appt-check' reads.
63 ;;
64 ;; You can change the way the appointment window is created/deleted by
65 ;; setting the variables
66 ;;
67 ;; appt-disp-window-function
68 ;; and
69 ;; appt-delete-window-function
70 ;;
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.
74 ;;
75
76 ;;; Code:
77
78 (require 'diary-lib)
79
80
81 (defgroup appt nil
82 "Appointment notification."
83 :prefix "appt-"
84 :group 'calendar)
85
86 (defcustom appt-message-warning-time 12
87 "Default time in minutes before an appointment that the warning begins."
88 :type 'integer
89 :group 'appt)
90
91 (defcustom appt-warning-time-regexp "warntime \\([0-9]+\\)"
92 "Regexp matching a string giving the warning time for an appointment.
93 The first subexpression matches the time in minutes (an integer).
94 This overrides the default `appt-message-warning-time'.
95 You may want to put this inside a diary comment (see `diary-comment-start').
96 For example, to be warned 30 minutes in advance of an appointment:
97 2011/06/01 12:00 Do something ## warntime 30
98 "
99 :version "24.1"
100 :type 'regexp
101 :group 'appt)
102
103 (defcustom appt-audible t
104 "Non-nil means beep to indicate appointment."
105 :type 'boolean
106 :group 'appt)
107
108 ;; TODO - add popup.
109 (defcustom appt-display-format 'window
110 "How appointment reminders should be displayed.
111 The options are:
112 window - use a separate window
113 echo - use the echo area
114 nil - no visible reminder.
115 See also `appt-audible' and `appt-display-mode-line'."
116 :type '(choice
117 (const :tag "Separate window" window)
118 (const :tag "Echo-area" echo)
119 (const :tag "No visible display" nil))
120 :group 'appt
121 :version "24.1") ; no longer inherit from deleted obsolete variables
122
123 (defcustom appt-display-mode-line t
124 "Non-nil means display minutes to appointment and time on the mode line.
125 This is in addition to any other display of appointment messages."
126 :type 'boolean
127 :group 'appt)
128
129 (defcustom appt-display-duration 10
130 "The number of seconds an appointment message is displayed.
131 Only relevant if reminders are to be displayed in their own window."
132 :type 'integer
133 :group 'appt)
134
135 (defcustom appt-display-diary t
136 "Non-nil displays the diary when the appointment list is first initialized.
137 This will occur at midnight when the appointment list is updated."
138 :type 'boolean
139 :group 'appt)
140
141 (defcustom appt-display-interval 3
142 "Number of minutes to wait between checking the appointment list."
143 :type 'integer
144 :group 'appt)
145
146 (defcustom appt-disp-window-function 'appt-disp-window
147 "Function called to display appointment window.
148 Only relevant if reminders are being displayed in a window.
149 It should take three string arguments: the number of minutes till
150 the appointment, the current time, and the text of the appointment."
151 :type '(choice (const appt-disp-window)
152 function)
153 :group 'appt)
154
155 (defcustom appt-delete-window-function 'appt-delete-window
156 "Function called to remove appointment window and buffer.
157 Only relevant if reminders are being displayed in a window."
158 :type '(choice (const appt-delete-window)
159 function)
160 :group 'appt)
161
162
163 ;;; Internal variables below this point.
164
165 (defconst appt-buffer-name "*appt-buf*"
166 "Name of the appointments buffer.")
167
168 ;; TODO Turn this into an alist? It would be easier to add more
169 ;; optional elements.
170 ;; TODO There should be a way to set WARNTIME (and other properties)
171 ;; from the diary-file. Implementing that would be a good reason
172 ;; to change this to an alist.
173 (defvar appt-time-msg-list nil
174 "The list of appointments for today.
175 Use `appt-add' and `appt-delete' to add and delete appointments.
176 The original list is generated from today's `diary-entries-list', and
177 can be regenerated using the function `appt-check'.
178 Each element of the generated list has the form
179 \(MINUTES STRING [FLAG] [WARNTIME])
180 where MINUTES is the time in minutes of the appointment after midnight,
181 and STRING is the description of the appointment.
182 FLAG and WARNTIME can only be present if the element was made
183 with `appt-add'. A non-nil FLAG indicates that the element was made
184 with `appt-add', so calling `appt-make-list' again should preserve it.
185 If WARNTIME is non-nil, it is an integer to use in place
186 of `appt-message-warning-time'.")
187
188 (defconst appt-max-time (1- (* 24 60))
189 "11:59pm in minutes - number of minutes in a day minus 1.")
190
191 (defvar appt-mode-string nil
192 "String being displayed in the mode line saying you have an appointment.
193 The actual string includes the amount of time till the appointment.
194 Only used if `appt-display-mode-line' is non-nil.")
195 (put 'appt-mode-string 'risky-local-variable t) ; for 'face property
196
197 (defvar appt-prev-comp-time nil
198 "Time of day (mins since midnight) at which we last checked appointments.
199 A nil value forces the diary file to be (re-)checked for appointments.")
200
201 (defvar appt-now-displayed nil
202 "Non-nil when we have started notifying about a appointment that is near.")
203
204 (defvar appt-display-count nil
205 "Internal variable used to count number of consecutive reminders.")
206
207 (defvar appt-timer nil
208 "Timer used for diary appointment notifications (`appt-check').
209 If this is non-nil, appointment checking is active.")
210
211
212 ;;; Functions.
213
214 (defun appt-display-message (string mins)
215 "Display a reminder about an appointment.
216 The string STRING describes the appointment, due in integer MINS minutes.
217 The format of the visible reminder is controlled by `appt-display-format'.
218 The variable `appt-audible' controls the audible reminder."
219 (if appt-audible (beep 1))
220 (cond ((eq appt-display-format 'window)
221 (funcall appt-disp-window-function
222 (number-to-string mins)
223 ;; TODO - use calendar-month-abbrev-array rather than %b?
224 (format-time-string "%a %b %e " (current-time))
225 string)
226 (run-at-time (format "%d sec" appt-display-duration)
227 nil
228 appt-delete-window-function))
229 ((eq appt-display-format 'echo)
230 (message "%s" string))))
231
232
233 (defun appt-check (&optional force)
234 "Check for an appointment and update any reminder display.
235 If optional argument FORCE is non-nil, reparse the diary file for
236 appointments. Otherwise the diary file is only parsed once per day,
237 or when it (or a file it includes) is saved.
238
239 Note: the time must be the first thing in the line in the diary
240 for a warning to be issued. The format of the time can be either
241 24 hour or am/pm. For example:
242
243 02/23/89
244 18:00 Dinner
245
246 Thursday
247 11:45am Lunch meeting.
248
249 Appointments are checked every `appt-display-interval' minutes.
250 The following variables control appointment notification:
251
252 `appt-display-format'
253 Controls the format in which reminders are displayed.
254
255 `appt-audible'
256 Variable used to determine if reminder is audible.
257 Default is t.
258
259 `appt-message-warning-time'
260 Variable used to determine when appointment message
261 should first be displayed.
262
263 `appt-display-mode-line'
264 If non-nil, a generic message giving the time remaining
265 is shown in the mode-line when an appointment is due.
266
267 `appt-display-interval'
268 Interval in minutes at which to check for pending appointments.
269
270 `appt-display-diary'
271 Display the diary buffer when the appointment list is
272 initialized for the first time in a day.
273
274 The following variables are only relevant if reminders are being
275 displayed in a window:
276
277 `appt-display-duration'
278 The number of seconds an appointment message is displayed.
279
280 `appt-disp-window-function'
281 Function called to display appointment window.
282
283 `appt-delete-window-function'
284 Function called to remove appointment window and buffer."
285 (interactive "P") ; so people can force updates
286 (let* ((min-to-app -1)
287 (prev-appt-mode-string appt-mode-string)
288 (prev-appt-display-count (or appt-display-count 0))
289 ;; Non-nil means do a full check for pending appointments and
290 ;; display in whatever ways the user has selected. When no
291 ;; appointment is being displayed, we always do a full check.
292 (full-check
293 (or (not appt-now-displayed)
294 ;; This is true every appt-display-interval minutes.
295 (zerop (mod prev-appt-display-count appt-display-interval))))
296 ;; Non-nil means only update the interval displayed in the mode line.
297 (mode-line-only (unless full-check appt-now-displayed))
298 now cur-comp-time appt-comp-time appt-warn-time)
299 (when (or full-check mode-line-only)
300 (save-excursion ; FIXME ?
301 ;; Convert current time to minutes after midnight (12.01am = 1).
302 (setq now (decode-time)
303 cur-comp-time (+ (* 60 (nth 2 now)) (nth 1 now)))
304 ;; At first check in any day, update appointments to today's list.
305 (if (or force ; eg initialize, diary save
306 (null appt-prev-comp-time) ; first check
307 (< cur-comp-time appt-prev-comp-time)) ; new day
308 (ignore-errors
309 (let ((diary-hook (if (assoc 'appt-make-list diary-hook)
310 diary-hook
311 (cons 'appt-make-list diary-hook))))
312 (if appt-display-diary
313 (diary)
314 ;; Not displaying the diary, so we can ignore
315 ;; diary-number-of-entries. Since appt.el only
316 ;; works on a daily basis, no need for more entries.
317 (diary-list-entries (calendar-current-date) 1 t)))))
318 (setq appt-prev-comp-time cur-comp-time
319 appt-mode-string nil
320 appt-display-count nil)
321 ;; If there are entries in the list, and the user wants a
322 ;; message issued, get the first time off of the list and
323 ;; calculate the number of minutes until the appointment.
324 (when appt-time-msg-list
325 (setq appt-comp-time (caar (car appt-time-msg-list))
326 appt-warn-time (or (nth 3 (car appt-time-msg-list))
327 appt-message-warning-time)
328 min-to-app (- appt-comp-time cur-comp-time))
329 (while (and appt-time-msg-list
330 (< appt-comp-time cur-comp-time))
331 (setq appt-time-msg-list (cdr appt-time-msg-list))
332 (if appt-time-msg-list
333 (setq appt-comp-time (caar (car appt-time-msg-list)))))
334 ;; If we have an appointment between midnight and
335 ;; `appt-warn-time' minutes after midnight, we
336 ;; must begin to issue a message before midnight. Midnight
337 ;; is considered 0 minutes and 11:59pm is 1439
338 ;; minutes. Therefore we must recalculate the minutes to
339 ;; appointment variable. It is equal to the number of
340 ;; minutes before midnight plus the number of minutes after
341 ;; midnight our appointment is.
342 (if (and (< appt-comp-time appt-warn-time)
343 (> (+ cur-comp-time appt-warn-time)
344 appt-max-time))
345 (setq min-to-app (+ (- (1+ appt-max-time) cur-comp-time)
346 appt-comp-time)))
347 ;; Issue warning if the appointment time is within
348 ;; appt-message-warning time.
349 (when (and (<= min-to-app appt-warn-time)
350 (>= min-to-app 0))
351 (setq appt-now-displayed t
352 appt-display-count (1+ prev-appt-display-count))
353 (unless mode-line-only
354 (appt-display-message (cadr (car appt-time-msg-list))
355 min-to-app))
356 (when appt-display-mode-line
357 (setq appt-mode-string
358 (concat " " (propertize
359 (format "App't in %s min." min-to-app)
360 'face 'mode-line-emphasis))))
361 ;; When an appointment is reached, delete it from the
362 ;; list. Reset the count to 0 in case we display another
363 ;; appointment on the next cycle.
364 (if (zerop min-to-app)
365 (setq appt-time-msg-list (cdr appt-time-msg-list)
366 appt-display-count nil))))
367 ;; If we have changed the mode line string, redisplay all mode lines.
368 (and appt-display-mode-line
369 (not (string-equal appt-mode-string
370 prev-appt-mode-string))
371 (progn
372 (force-mode-line-update t)
373 ;; If the string now has a notification, redisplay right now.
374 (if appt-mode-string
375 (sit-for 0))))))))
376
377 (defun appt-disp-window (min-to-app new-time appt-msg)
378 "Display appointment due in MIN-TO-APP (a string) minutes.
379 NEW-TIME is a string giving the date. Displays the appointment
380 message APPT-MSG in a separate buffer."
381 (let ((this-window (selected-window))
382 (appt-disp-buf (get-buffer-create appt-buffer-name)))
383 ;; Make sure we're not in the minibuffer before splitting the window.
384 ;; FIXME this seems needlessly complicated?
385 (when (minibufferp)
386 (other-window 1)
387 (and (minibufferp) (display-multi-frame-p) (other-frame 1)))
388 (if (cdr (assq 'unsplittable (frame-parameters)))
389 ;; In an unsplittable frame, use something somewhere else.
390 (progn
391 (set-buffer appt-disp-buf)
392 (display-buffer appt-disp-buf))
393 (unless (or (special-display-p (buffer-name appt-disp-buf))
394 (same-window-p (buffer-name appt-disp-buf)))
395 ;; By default, split the bottom window and use the lower part.
396 (appt-select-lowest-window)
397 ;; Split the window, unless it's too small to do so.
398 (when (>= (window-height) (* 2 window-min-height))
399 (select-window (split-window))))
400 (switch-to-buffer appt-disp-buf))
401 ;; FIXME Link to diary entry?
402 (calendar-set-mode-line
403 (format " Appointment %s. %s "
404 (if (string-equal "0" min-to-app) "now"
405 (format "in %s minute%s" min-to-app
406 (if (string-equal "1" min-to-app) "" "s")))
407 new-time))
408 (setq buffer-read-only nil
409 buffer-undo-list t)
410 (erase-buffer)
411 (insert appt-msg)
412 (shrink-window-if-larger-than-buffer (get-buffer-window appt-disp-buf t))
413 (set-buffer-modified-p nil)
414 (setq buffer-read-only t)
415 (raise-frame (selected-frame))
416 (select-window this-window)))
417
418 (defun appt-delete-window ()
419 "Function called to undisplay appointment messages.
420 Usually just deletes the appointment buffer."
421 (let ((window (get-buffer-window appt-buffer-name t)))
422 (and window
423 (or (eq window (frame-root-window (window-frame window)))
424 (delete-window window))))
425 (kill-buffer appt-buffer-name)
426 (if appt-audible
427 (beep 1)))
428
429 (defun appt-select-lowest-window ()
430 "Select the lowest window on the frame."
431 (let ((lowest-window (selected-window))
432 (bottom-edge (nth 3 (window-edges)))
433 next-bottom-edge)
434 (walk-windows (lambda (w)
435 (when (< bottom-edge (setq next-bottom-edge
436 (nth 3 (window-edges w))))
437 (setq bottom-edge next-bottom-edge
438 lowest-window w))) 'nomini)
439 (select-window lowest-window)))
440
441 (defconst appt-time-regexp
442 "[0-9]?[0-9]\\(h\\([0-9][0-9]\\)?\\|[:.][0-9][0-9]\\)\\(am\\|pm\\)?")
443
444 ;;;###autoload
445 (defun appt-add (time msg &optional warntime)
446 "Add an appointment for today at TIME with message MSG.
447 The time should be in either 24 hour format or am/pm format.
448 Optional argument WARNTIME is an integer (or string) giving the number
449 of minutes before the appointment at which to start warning.
450 The default is `appt-message-warning-time'."
451 (interactive "sTime (hh:mm[am/pm]): \nsMessage:
452 sMinutes before the appointment to start warning: ")
453 (unless (string-match appt-time-regexp time)
454 (error "Unacceptable time-string"))
455 (and (stringp warntime)
456 (setq warntime (unless (string-equal warntime "")
457 (string-to-number warntime))))
458 (and warntime
459 (not (integerp warntime))
460 (error "Argument WARNTIME must be an integer, or nil"))
461 (or appt-timer (appt-activate))
462 (let ((time-msg (list (list (appt-convert-time time))
463 (concat time " " msg) t)))
464 ;; It is presently non-sensical to have multiple warnings about
465 ;; the same appointment with just different delays, but it might
466 ;; not always be so. TODO
467 (if warntime (setq time-msg (append time-msg (list warntime))))
468 (unless (member time-msg appt-time-msg-list)
469 (setq appt-time-msg-list
470 (appt-sort-list (nconc appt-time-msg-list (list time-msg)))))))
471
472 (defun appt-delete ()
473 "Delete an appointment from the list of appointments."
474 (interactive)
475 (let ((tmp-msg-list appt-time-msg-list))
476 (dolist (element tmp-msg-list)
477 (if (y-or-n-p (concat "Delete "
478 ;; We want to quote any doublequotes in the
479 ;; string, as well as put doublequotes around it.
480 (prin1-to-string
481 (substring-no-properties (cadr element) 0))
482 " from list? "))
483 (setq appt-time-msg-list (delq element appt-time-msg-list)))))
484 (appt-check)
485 (message ""))
486
487
488 (defvar number)
489 (defvar original-date)
490 (defvar diary-entries-list)
491
492 (defun appt-make-list ()
493 "Update the appointments list from today's diary buffer.
494 The time must be at the beginning of a line for it to be
495 put in the appointments list (see examples in documentation of
496 the function `appt-check'). We assume that the variables DATE and
497 NUMBER hold the arguments that `diary-list-entries' received.
498 They specify the range of dates that the diary is being processed for.
499
500 Any appointments made with `appt-add' are not affected by this function."
501 ;; We have something to do if the range of dates that the diary is
502 ;; considering includes the current date.
503 (if (and (not (calendar-date-compare
504 (list (calendar-current-date))
505 (list original-date)))
506 (calendar-date-compare
507 (list (calendar-current-date))
508 (list (calendar-gregorian-from-absolute
509 (+ (calendar-absolute-from-gregorian original-date)
510 number)))))
511 (save-excursion
512 ;; Clear the appointments list, then fill it in from the diary.
513 (dolist (elt appt-time-msg-list)
514 ;; Delete any entries that were not made with appt-add.
515 (unless (nth 2 elt)
516 (setq appt-time-msg-list
517 (delq elt appt-time-msg-list))))
518 (if diary-entries-list
519 ;; Cycle through the entry-list (diary-entries-list)
520 ;; looking for entries beginning with a time. If the
521 ;; entry begins with a time, add it to the
522 ;; appt-time-msg-list. Then sort the list.
523 (let ((entry-list diary-entries-list)
524 time-string literal)
525 ;; Below, we assume diary-entries-list was in date
526 ;; order. It is, unless something on
527 ;; diary-list-entries-hook has changed it, eg
528 ;; diary-include-other-files (bug#7019). It must be
529 ;; in date order if number = 1.
530 (and diary-list-entries-hook
531 appt-display-diary
532 (not (eq diary-number-of-entries 1))
533 (not (memq (car (last diary-list-entries-hook))
534 '(diary-sort-entries sort-diary-entries)))
535 (setq entry-list (sort entry-list 'diary-entry-compare)))
536 ;; Skip diary entries for dates before today.
537 (while (and entry-list
538 (calendar-date-compare
539 (car entry-list) (list (calendar-current-date))))
540 (setq entry-list (cdr entry-list)))
541 ;; Parse the entries for today.
542 (while (and entry-list
543 (calendar-date-equal
544 (calendar-current-date) (caar entry-list)))
545 (setq time-string (cadr (car entry-list))
546 ;; Including any comments.
547 literal (or (nth 2 (nth 3 (car entry-list)))
548 time-string))
549 (while (string-match appt-time-regexp time-string)
550 (let* ((beg (match-beginning 0))
551 ;; Get just the time for this appointment.
552 (only-time (match-string 0 time-string))
553 ;; Find the end of this appointment
554 ;; (the start of the next).
555 (end (string-match
556 (concat "\n[ \t]*" appt-time-regexp)
557 time-string
558 (match-end 0)))
559 (warntime
560 (if (string-match appt-warning-time-regexp literal)
561 (string-to-number (match-string 1 literal))))
562 ;; Get the whole string for this appointment.
563 (appt-time-string
564 (substring time-string beg end))
565 (appt-time (list (appt-convert-time only-time)))
566 (time-msg (append
567 (list appt-time appt-time-string)
568 (if warntime (list nil warntime)))))
569 ;; Add this appointment to appt-time-msg-list.
570 (setq appt-time-msg-list
571 (nconc appt-time-msg-list (list time-msg))
572 ;; Discard this appointment from the string.
573 ;; (This allows for multiple appts per entry.)
574 time-string
575 (if end (substring time-string end) ""))
576 ;; Similarly, discard the start of literal.
577 (and (> (length time-string) 0)
578 (string-match appt-time-regexp literal)
579 (setq end (string-match
580 (concat "\n[ \t]*" appt-time-regexp)
581 literal (match-end 0)))
582 (setq literal (substring literal end)))))
583 (setq entry-list (cdr entry-list)))))
584 (setq appt-time-msg-list (appt-sort-list appt-time-msg-list))
585 ;; Convert current time to minutes after midnight (12:01am = 1),
586 ;; so that elements in the list that are earlier than the
587 ;; present time can be removed.
588 (let* ((now (decode-time))
589 (cur-comp-time (+ (* 60 (nth 2 now)) (nth 1 now)))
590 (appt-comp-time (caar (car appt-time-msg-list))))
591 (while (and appt-time-msg-list (< appt-comp-time cur-comp-time))
592 (setq appt-time-msg-list (cdr appt-time-msg-list))
593 (if appt-time-msg-list
594 (setq appt-comp-time (caar (car appt-time-msg-list)))))))))
595
596
597 (defun appt-sort-list (appt-list)
598 "Sort an appointment list, putting earlier items at the front.
599 APPT-LIST is a list of the same format as `appt-time-msg-list'."
600 (sort appt-list (lambda (e1 e2) (< (caar e1) (caar e2)))))
601
602
603 (defun appt-convert-time (time2conv)
604 "Convert hour:min[am/pm] format TIME2CONV to minutes from midnight.
605 A period (.) can be used instead of a colon (:) to separate the
606 hour and minute parts."
607 ;; Formats that should be accepted:
608 ;; 10:00 10.00 10h00 10h 10am 10:00am 10.00am
609 (let ((min (if (string-match "[h:.]\\([0-9][0-9]\\)" time2conv)
610 (string-to-number (match-string 1 time2conv))
611 0))
612 (hr (if (string-match "[0-9]*[0-9]" time2conv)
613 (string-to-number (match-string 0 time2conv))
614 0)))
615 ;; Convert the time appointment time into 24 hour time.
616 (cond ((and (string-match "pm" time2conv) (< hr 12))
617 (setq hr (+ 12 hr)))
618 ((and (string-match "am" time2conv) (= hr 12))
619 (setq hr 0)))
620 ;; Convert the actual time into minutes.
621 (+ (* hr 60) min)))
622
623 (defun appt-update-list ()
624 "If the current buffer is visiting the diary, update appointments.
625 This function also acts on any file listed in `diary-included-files'.
626 It is intended for use with `write-file-functions'."
627 (and (member buffer-file-name (append diary-included-files
628 (list (expand-file-name diary-file))))
629 appt-timer
630 (let ((appt-display-diary nil))
631 (appt-check t)))
632 nil)
633
634 ;;;###autoload
635 (defun appt-activate (&optional arg)
636 "Toggle checking of appointments.
637 With optional numeric argument ARG, turn appointment checking on if
638 ARG is positive, otherwise off."
639 (interactive "P")
640 (let ((appt-active appt-timer))
641 (setq appt-active (if arg (> (prefix-numeric-value arg) 0)
642 (not appt-active)))
643 (remove-hook 'write-file-functions 'appt-update-list)
644 (or global-mode-string (setq global-mode-string '("")))
645 (delq 'appt-mode-string global-mode-string)
646 (when appt-timer
647 (cancel-timer appt-timer)
648 (setq appt-timer nil))
649 (if appt-active
650 (progn
651 (add-hook 'write-file-functions 'appt-update-list)
652 (setq appt-timer (run-at-time t 60 'appt-check)
653 global-mode-string
654 (append global-mode-string '(appt-mode-string)))
655 (appt-check t)
656 (message "Appointment reminders enabled%s"
657 ;; Someone might want to use appt-add without a diary.
658 (if (ignore-errors (diary-check-diary-file))
659 ""
660 " (no diary file found)")))
661 (message "Appointment reminders disabled"))))
662
663
664 (provide 'appt)
665
666 ;;; appt.el ends here