]> code.delx.au - gnu-emacs/blob - lisp/calendar/appt.el
d1483c5445daea51757b9dc13746c211d8a33ce8
[gnu-emacs] / 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 are not always present. A non-nil FLAG
183 indicates that the element was made with `appt-add', so calling
184 `appt-make-list' again should preserve it. If WARNTIME is non-nil,
185 it is an integer to use in place of `appt-message-warning-time'.")
186
187 (defconst appt-max-time (1- (* 24 60))
188 "11:59pm in minutes - number of minutes in a day minus 1.")
189
190 (defvar appt-mode-string nil
191 "String being displayed in the mode line saying you have an appointment.
192 The actual string includes the amount of time till the appointment.
193 Only used if `appt-display-mode-line' is non-nil.")
194 (put 'appt-mode-string 'risky-local-variable t) ; for 'face property
195
196 (defvar appt-prev-comp-time nil
197 "Time of day (mins since midnight) at which we last checked appointments.
198 A nil value forces the diary file to be (re-)checked for appointments.")
199
200 (defvar appt-now-displayed nil
201 "Non-nil when we have started notifying about a appointment that is near.")
202
203 (defvar appt-display-count nil
204 "Internal variable used to count number of consecutive reminders.")
205
206 (defvar appt-timer nil
207 "Timer used for diary appointment notifications (`appt-check').
208 If this is non-nil, appointment checking is active.")
209
210
211 ;;; Functions.
212
213 (defun appt-display-message (string mins)
214 "Display a reminder about an appointment.
215 The string STRING describes the appointment, due in integer MINS minutes.
216 The format of the visible reminder is controlled by `appt-display-format'.
217 The variable `appt-audible' controls the audible reminder."
218 (if appt-audible (beep 1))
219 (cond ((eq appt-display-format 'window)
220 (funcall appt-disp-window-function
221 (number-to-string mins)
222 ;; TODO - use calendar-month-abbrev-array rather than %b?
223 (format-time-string "%a %b %e " (current-time))
224 string)
225 (run-at-time (format "%d sec" appt-display-duration)
226 nil
227 appt-delete-window-function))
228 ((eq appt-display-format 'echo)
229 (message "%s" string))))
230
231
232 (defun appt-check (&optional force)
233 "Check for an appointment and update any reminder display.
234 If optional argument FORCE is non-nil, reparse the diary file for
235 appointments. Otherwise the diary file is only parsed once per day,
236 or when it (or a file it includes) is saved.
237
238 Note: the time must be the first thing in the line in the diary
239 for a warning to be issued. The format of the time can be either
240 24 hour or am/pm. For example:
241
242 02/23/89
243 18:00 Dinner
244
245 Thursday
246 11:45am Lunch meeting.
247
248 Appointments are checked every `appt-display-interval' minutes.
249 The following variables control appointment notification:
250
251 `appt-display-format'
252 Controls the format in which reminders are displayed.
253
254 `appt-audible'
255 Variable used to determine if reminder is audible.
256 Default is t.
257
258 `appt-message-warning-time'
259 Variable used to determine when appointment message
260 should first be displayed.
261
262 `appt-display-mode-line'
263 If non-nil, a generic message giving the time remaining
264 is shown in the mode-line when an appointment is due.
265
266 `appt-display-interval'
267 Interval in minutes at which to check for pending appointments.
268
269 `appt-display-diary'
270 Display the diary buffer when the appointment list is
271 initialized for the first time in a day.
272
273 The following variables are only relevant if reminders are being
274 displayed in a window:
275
276 `appt-display-duration'
277 The number of seconds an appointment message is displayed.
278
279 `appt-disp-window-function'
280 Function called to display appointment window.
281
282 `appt-delete-window-function'
283 Function called to remove appointment window and buffer."
284 (interactive "P") ; so people can force updates
285 (let* ((min-to-app -1)
286 (prev-appt-mode-string appt-mode-string)
287 (prev-appt-display-count (or appt-display-count 0))
288 ;; Non-nil means do a full check for pending appointments and
289 ;; display in whatever ways the user has selected. When no
290 ;; appointment is being displayed, we always do a full check.
291 (full-check
292 (or (not appt-now-displayed)
293 ;; This is true every appt-display-interval minutes.
294 (zerop (mod prev-appt-display-count appt-display-interval))))
295 ;; Non-nil means only update the interval displayed in the mode line.
296 (mode-line-only (unless full-check appt-now-displayed))
297 now cur-comp-time appt-comp-time appt-warn-time)
298 (when (or full-check mode-line-only)
299 (save-excursion ; FIXME ?
300 ;; Convert current time to minutes after midnight (12.01am = 1).
301 (setq now (decode-time)
302 cur-comp-time (+ (* 60 (nth 2 now)) (nth 1 now)))
303 ;; At first check in any day, update appointments to today's list.
304 (if (or force ; eg initialize, diary save
305 (null appt-prev-comp-time) ; first check
306 (< cur-comp-time appt-prev-comp-time)) ; new day
307 (ignore-errors
308 (let ((diary-hook (if (assoc 'appt-make-list diary-hook)
309 diary-hook
310 (cons 'appt-make-list diary-hook))))
311 (if appt-display-diary
312 (diary)
313 ;; Not displaying the diary, so we can ignore
314 ;; diary-number-of-entries. Since appt.el only
315 ;; works on a daily basis, no need for more entries.
316 (diary-list-entries (calendar-current-date) 1 t)))))
317 (setq appt-prev-comp-time cur-comp-time
318 appt-mode-string nil
319 appt-display-count nil)
320 ;; If there are entries in the list, and the user wants a
321 ;; message issued, get the first time off of the list and
322 ;; calculate the number of minutes until the appointment.
323 (when appt-time-msg-list
324 (setq appt-comp-time (caar (car appt-time-msg-list))
325 appt-warn-time (or (nth 3 (car appt-time-msg-list))
326 appt-message-warning-time)
327 min-to-app (- appt-comp-time cur-comp-time))
328 (while (and appt-time-msg-list
329 (< appt-comp-time cur-comp-time))
330 (setq appt-time-msg-list (cdr appt-time-msg-list))
331 (if appt-time-msg-list
332 (setq appt-comp-time (caar (car appt-time-msg-list)))))
333 ;; If we have an appointment between midnight and
334 ;; `appt-warn-time' minutes after midnight, we
335 ;; must begin to issue a message before midnight. Midnight
336 ;; is considered 0 minutes and 11:59pm is 1439
337 ;; minutes. Therefore we must recalculate the minutes to
338 ;; appointment variable. It is equal to the number of
339 ;; minutes before midnight plus the number of minutes after
340 ;; midnight our appointment is.
341 (if (and (< appt-comp-time appt-warn-time)
342 (> (+ cur-comp-time appt-warn-time)
343 appt-max-time))
344 (setq min-to-app (+ (- (1+ appt-max-time) cur-comp-time)
345 appt-comp-time)))
346 ;; Issue warning if the appointment time is within
347 ;; appt-message-warning time.
348 (when (and (<= min-to-app appt-warn-time)
349 (>= min-to-app 0))
350 (setq appt-now-displayed t
351 appt-display-count (1+ prev-appt-display-count))
352 (unless mode-line-only
353 (appt-display-message (cadr (car appt-time-msg-list))
354 min-to-app))
355 (when appt-display-mode-line
356 (setq appt-mode-string
357 (concat " " (propertize
358 (format "App't in %s min." min-to-app)
359 'face 'mode-line-emphasis))))
360 ;; When an appointment is reached, delete it from the
361 ;; list. Reset the count to 0 in case we display another
362 ;; appointment on the next cycle.
363 (if (zerop min-to-app)
364 (setq appt-time-msg-list (cdr appt-time-msg-list)
365 appt-display-count nil))))
366 ;; If we have changed the mode line string, redisplay all mode lines.
367 (and appt-display-mode-line
368 (not (string-equal appt-mode-string
369 prev-appt-mode-string))
370 (progn
371 (force-mode-line-update t)
372 ;; If the string now has a notification, redisplay right now.
373 (if appt-mode-string
374 (sit-for 0))))))))
375
376 (defun appt-disp-window (min-to-app new-time appt-msg)
377 "Display appointment due in MIN-TO-APP (a string) minutes.
378 NEW-TIME is a string giving the date. Displays the appointment
379 message APPT-MSG in a separate buffer."
380 (let ((this-window (selected-window))
381 (appt-disp-buf (get-buffer-create appt-buffer-name)))
382 ;; Make sure we're not in the minibuffer before splitting the window.
383 ;; FIXME this seems needlessly complicated?
384 (when (minibufferp)
385 (other-window 1)
386 (and (minibufferp) (display-multi-frame-p) (other-frame 1)))
387 (if (cdr (assq 'unsplittable (frame-parameters)))
388 ;; In an unsplittable frame, use something somewhere else.
389 (progn
390 (set-buffer appt-disp-buf)
391 (display-buffer appt-disp-buf))
392 (unless (or (special-display-p (buffer-name appt-disp-buf))
393 (same-window-p (buffer-name appt-disp-buf)))
394 ;; By default, split the bottom window and use the lower part.
395 (appt-select-lowest-window)
396 ;; Split the window, unless it's too small to do so.
397 (when (>= (window-height) (* 2 window-min-height))
398 (select-window (split-window))))
399 (switch-to-buffer appt-disp-buf))
400 ;; FIXME Link to diary entry?
401 (calendar-set-mode-line
402 (format " Appointment %s. %s "
403 (if (string-equal "0" min-to-app) "now"
404 (format "in %s minute%s" min-to-app
405 (if (string-equal "1" min-to-app) "" "s")))
406 new-time))
407 (setq buffer-read-only nil
408 buffer-undo-list t)
409 (erase-buffer)
410 (insert appt-msg)
411 (shrink-window-if-larger-than-buffer (get-buffer-window appt-disp-buf t))
412 (set-buffer-modified-p nil)
413 (setq buffer-read-only t)
414 (raise-frame (selected-frame))
415 (select-window this-window)))
416
417 (defun appt-delete-window ()
418 "Function called to undisplay appointment messages.
419 Usually just deletes the appointment buffer."
420 (let ((window (get-buffer-window appt-buffer-name t)))
421 (and window
422 (or (eq window (frame-root-window (window-frame window)))
423 (delete-window window))))
424 (kill-buffer appt-buffer-name)
425 (if appt-audible
426 (beep 1)))
427
428 (defun appt-select-lowest-window ()
429 "Select the lowest window on the frame."
430 (let ((lowest-window (selected-window))
431 (bottom-edge (nth 3 (window-edges)))
432 next-bottom-edge)
433 (walk-windows (lambda (w)
434 (when (< bottom-edge (setq next-bottom-edge
435 (nth 3 (window-edges w))))
436 (setq bottom-edge next-bottom-edge
437 lowest-window w))) 'nomini)
438 (select-window lowest-window)))
439
440 (defconst appt-time-regexp
441 "[0-9]?[0-9]\\(h\\([0-9][0-9]\\)?\\|[:.][0-9][0-9]\\)\\(am\\|pm\\)?")
442
443 ;;;###autoload
444 (defun appt-add (time msg &optional warntime)
445 "Add an appointment for today at TIME with message MSG.
446 The time should be in either 24 hour format or am/pm format.
447 Optional argument WARNTIME is an integer (or string) giving the number
448 of minutes before the appointment at which to start warning.
449 The default is `appt-message-warning-time'."
450 (interactive "sTime (hh:mm[am/pm]): \nsMessage:
451 sMinutes before the appointment to start warning: ")
452 (unless (string-match appt-time-regexp time)
453 (error "Unacceptable time-string"))
454 (and (stringp warntime)
455 (setq warntime (unless (string-equal warntime "")
456 (string-to-number warntime))))
457 (and warntime
458 (not (integerp warntime))
459 (error "Argument WARNTIME must be an integer, or nil"))
460 (or appt-timer (appt-activate))
461 (let ((time-msg (list (list (appt-convert-time time))
462 (concat time " " msg) t)))
463 ;; It is presently non-sensical to have multiple warnings about
464 ;; the same appointment with just different delays, but it might
465 ;; not always be so. TODO
466 (if warntime (setq time-msg (append time-msg (list warntime))))
467 (unless (member time-msg appt-time-msg-list)
468 (setq appt-time-msg-list
469 (appt-sort-list (nconc appt-time-msg-list (list time-msg)))))))
470
471 (defun appt-delete ()
472 "Delete an appointment from the list of appointments."
473 (interactive)
474 (let ((tmp-msg-list appt-time-msg-list))
475 (dolist (element tmp-msg-list)
476 (if (y-or-n-p (concat "Delete "
477 ;; We want to quote any doublequotes in the
478 ;; string, as well as put doublequotes around it.
479 (prin1-to-string
480 (substring-no-properties (cadr element) 0))
481 " from list? "))
482 (setq appt-time-msg-list (delq element appt-time-msg-list)))))
483 (appt-check)
484 (message ""))
485
486
487 (defvar number)
488 (defvar original-date)
489 (defvar diary-entries-list)
490
491 (defun appt-make-list ()
492 "Update the appointments list from today's diary buffer.
493 The time must be at the beginning of a line for it to be
494 put in the appointments list (see examples in documentation of
495 the function `appt-check'). We assume that the variables DATE and
496 NUMBER hold the arguments that `diary-list-entries' received.
497 They specify the range of dates that the diary is being processed for.
498
499 Any appointments made with `appt-add' are not affected by this function."
500 ;; We have something to do if the range of dates that the diary is
501 ;; considering includes the current date.
502 (if (and (not (calendar-date-compare
503 (list (calendar-current-date))
504 (list original-date)))
505 (calendar-date-compare
506 (list (calendar-current-date))
507 (list (calendar-gregorian-from-absolute
508 (+ (calendar-absolute-from-gregorian original-date)
509 number)))))
510 (save-excursion
511 ;; Clear the appointments list, then fill it in from the diary.
512 (dolist (elt appt-time-msg-list)
513 ;; Delete any entries that were not made with appt-add.
514 (unless (nth 2 elt)
515 (setq appt-time-msg-list
516 (delq elt appt-time-msg-list))))
517 (if diary-entries-list
518 ;; Cycle through the entry-list (diary-entries-list)
519 ;; looking for entries beginning with a time. If the
520 ;; entry begins with a time, add it to the
521 ;; appt-time-msg-list. Then sort the list.
522 (let ((entry-list diary-entries-list)
523 time-string literal)
524 ;; Below, we assume diary-entries-list was in date
525 ;; order. It is, unless something on
526 ;; diary-list-entries-hook has changed it, eg
527 ;; diary-include-other-files (bug#7019). It must be
528 ;; in date order if number = 1.
529 (and diary-list-entries-hook
530 appt-display-diary
531 (not (eq diary-number-of-entries 1))
532 (not (memq (car (last diary-list-entries-hook))
533 '(diary-sort-entries sort-diary-entries)))
534 (setq entry-list (sort entry-list 'diary-entry-compare)))
535 ;; Skip diary entries for dates before today.
536 (while (and entry-list
537 (calendar-date-compare
538 (car entry-list) (list (calendar-current-date))))
539 (setq entry-list (cdr entry-list)))
540 ;; Parse the entries for today.
541 (while (and entry-list
542 (calendar-date-equal
543 (calendar-current-date) (caar entry-list)))
544 (setq time-string (cadr (car entry-list))
545 ;; Including any comments.
546 literal (or (nth 2 (nth 3 (car entry-list)))
547 time-string))
548 (while (string-match appt-time-regexp time-string)
549 (let* ((beg (match-beginning 0))
550 ;; Get just the time for this appointment.
551 (only-time (match-string 0 time-string))
552 ;; Find the end of this appointment
553 ;; (the start of the next).
554 (end (string-match
555 (concat "\n[ \t]*" appt-time-regexp)
556 time-string
557 (match-end 0)))
558 (warntime
559 (if (string-match appt-warning-time-regexp literal)
560 (string-to-number (match-string 1 literal))))
561 ;; Get the whole string for this appointment.
562 (appt-time-string
563 (substring time-string beg end))
564 (appt-time (list (appt-convert-time only-time)))
565 (time-msg (append
566 (list appt-time appt-time-string)
567 (if warntime (list nil warntime)))))
568 ;; Add this appointment to appt-time-msg-list.
569 (setq appt-time-msg-list
570 (nconc appt-time-msg-list (list time-msg))
571 ;; Discard this appointment from the string.
572 ;; (This allows for multiple appts per entry.)
573 time-string
574 (if end (substring time-string end) ""))
575 ;; Similarly, discard the start of literal.
576 (and (> (length time-string) 0)
577 (string-match appt-time-regexp literal)
578 (setq end (string-match
579 (concat "\n[ \t]*" appt-time-regexp)
580 literal (match-end 0)))
581 (setq literal (substring literal end)))))
582 (setq entry-list (cdr entry-list)))))
583 (setq appt-time-msg-list (appt-sort-list appt-time-msg-list))
584 ;; Convert current time to minutes after midnight (12:01am = 1),
585 ;; so that elements in the list that are earlier than the
586 ;; present time can be removed.
587 (let* ((now (decode-time))
588 (cur-comp-time (+ (* 60 (nth 2 now)) (nth 1 now)))
589 (appt-comp-time (caar (car appt-time-msg-list))))
590 (while (and appt-time-msg-list (< appt-comp-time cur-comp-time))
591 (setq appt-time-msg-list (cdr appt-time-msg-list))
592 (if appt-time-msg-list
593 (setq appt-comp-time (caar (car appt-time-msg-list)))))))))
594
595
596 (defun appt-sort-list (appt-list)
597 "Sort an appointment list, putting earlier items at the front.
598 APPT-LIST is a list of the same format as `appt-time-msg-list'."
599 (sort appt-list (lambda (e1 e2) (< (caar e1) (caar e2)))))
600
601
602 (defun appt-convert-time (time2conv)
603 "Convert hour:min[am/pm] format TIME2CONV to minutes from midnight.
604 A period (.) can be used instead of a colon (:) to separate the
605 hour and minute parts."
606 ;; Formats that should be accepted:
607 ;; 10:00 10.00 10h00 10h 10am 10:00am 10.00am
608 (let ((min (if (string-match "[h:.]\\([0-9][0-9]\\)" time2conv)
609 (string-to-number (match-string 1 time2conv))
610 0))
611 (hr (if (string-match "[0-9]*[0-9]" time2conv)
612 (string-to-number (match-string 0 time2conv))
613 0)))
614 ;; Convert the time appointment time into 24 hour time.
615 (cond ((and (string-match "pm" time2conv) (< hr 12))
616 (setq hr (+ 12 hr)))
617 ((and (string-match "am" time2conv) (= hr 12))
618 (setq hr 0)))
619 ;; Convert the actual time into minutes.
620 (+ (* hr 60) min)))
621
622 (defun appt-update-list ()
623 "If the current buffer is visiting the diary, update appointments.
624 This function also acts on any file listed in `diary-included-files'.
625 It is intended for use with `write-file-functions'."
626 (and (member buffer-file-name (append diary-included-files
627 (list (expand-file-name diary-file))))
628 appt-timer
629 (let ((appt-display-diary nil))
630 (appt-check t)))
631 nil)
632
633 ;;;###autoload
634 (defun appt-activate (&optional arg)
635 "Toggle checking of appointments.
636 With optional numeric argument ARG, turn appointment checking on if
637 ARG is positive, otherwise off."
638 (interactive "P")
639 (let ((appt-active appt-timer))
640 (setq appt-active (if arg (> (prefix-numeric-value arg) 0)
641 (not appt-active)))
642 (remove-hook 'write-file-functions 'appt-update-list)
643 (or global-mode-string (setq global-mode-string '("")))
644 (delq 'appt-mode-string global-mode-string)
645 (when appt-timer
646 (cancel-timer appt-timer)
647 (setq appt-timer nil))
648 (if appt-active
649 (progn
650 (add-hook 'write-file-functions 'appt-update-list)
651 (setq appt-timer (run-at-time t 60 'appt-check)
652 global-mode-string
653 (append global-mode-string '(appt-mode-string)))
654 (appt-check t)
655 (message "Appointment reminders enabled%s"
656 ;; Someone might want to use appt-add without a diary.
657 (if (ignore-errors (diary-check-diary-file))
658 ""
659 " (no diary file found)")))
660 (message "Appointment reminders disabled"))))
661
662
663 (provide 'appt)
664
665 ;;; appt.el ends here