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