Add "Package:" file headers to denote built-in packages.
[bpt/emacs.git] / 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 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' is saved. Calling `appt-check' with an argument (or
52 ;; re-enabling the package) forces a 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 and when 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 (condition-case nil
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 (diary)
350 ;; If the diary buffer existed before this command,
351 ;; restore its display state. Otherwise, kill it.
352 (if d-buff
353 ;; Displays the diary buffer.
354 (or selective (diary-show-all-entries))
355 (and (setq d-buff (find-buffer-visiting diary-file))
356 (kill-buffer d-buff)))))
357 (error nil)))
358 (setq appt-prev-comp-time cur-comp-time
359 appt-mode-string nil
360 appt-display-count nil)
361 ;; If there are entries in the list, and the user wants a
362 ;; message issued, get the first time off of the list and
363 ;; calculate the number of minutes until the appointment.
364 (when (and appt-issue-message appt-time-msg-list)
365 (setq appt-comp-time (caar (car appt-time-msg-list))
366 appt-warn-time (or (nth 3 (car appt-time-msg-list))
367 appt-message-warning-time)
368 min-to-app (- appt-comp-time cur-comp-time))
369 (while (and appt-time-msg-list
370 (< appt-comp-time cur-comp-time))
371 (setq appt-time-msg-list (cdr appt-time-msg-list))
372 (if appt-time-msg-list
373 (setq appt-comp-time (caar (car appt-time-msg-list)))))
374 ;; If we have an appointment between midnight and
375 ;; `appt-warn-time' minutes after midnight, we
376 ;; must begin to issue a message before midnight. Midnight
377 ;; is considered 0 minutes and 11:59pm is 1439
378 ;; minutes. Therefore we must recalculate the minutes to
379 ;; appointment variable. It is equal to the number of
380 ;; minutes before midnight plus the number of minutes after
381 ;; midnight our appointment is.
382 (if (and (< appt-comp-time appt-warn-time)
383 (> (+ cur-comp-time appt-warn-time)
384 appt-max-time))
385 (setq min-to-app (+ (- (1+ appt-max-time) cur-comp-time)
386 appt-comp-time)))
387 ;; Issue warning if the appointment time is within
388 ;; appt-message-warning time.
389 (when (and (<= min-to-app appt-warn-time)
390 (>= min-to-app 0))
391 (setq appt-now-displayed t
392 appt-display-count (1+ prev-appt-display-count))
393 (unless mode-line-only
394 (appt-display-message (cadr (car appt-time-msg-list))
395 min-to-app))
396 (when appt-display-mode-line
397 (setq appt-mode-string
398 (concat " " (propertize
399 (format "App't in %s min." min-to-app)
400 'face 'mode-line-emphasis))))
401 ;; When an appointment is reached, delete it from the
402 ;; list. Reset the count to 0 in case we display another
403 ;; appointment on the next cycle.
404 (if (zerop min-to-app)
405 (setq appt-time-msg-list (cdr appt-time-msg-list)
406 appt-display-count nil))))
407 ;; If we have changed the mode line string, redisplay all mode lines.
408 (and appt-display-mode-line
409 (not (string-equal appt-mode-string
410 prev-appt-mode-string))
411 (progn
412 (force-mode-line-update t)
413 ;; If the string now has a notification, redisplay right now.
414 (if appt-mode-string
415 (sit-for 0))))))))
416
417 (defun appt-disp-window (min-to-app new-time appt-msg)
418 "Display appointment due in MIN-TO-APP (a string) minutes.
419 NEW-TIME is a string giving the date. Displays the appointment
420 message APPT-MSG in a separate buffer."
421 (let ((this-window (selected-window))
422 (appt-disp-buf (get-buffer-create appt-buffer-name)))
423 ;; Make sure we're not in the minibuffer before splitting the window.
424 ;; FIXME this seems needlessly complicated?
425 (when (minibufferp)
426 (other-window 1)
427 (and (minibufferp) (display-multi-frame-p) (other-frame 1)))
428 (if (cdr (assq 'unsplittable (frame-parameters)))
429 ;; In an unsplittable frame, use something somewhere else.
430 (progn
431 (set-buffer appt-disp-buf)
432 (display-buffer appt-disp-buf))
433 (unless (or (special-display-p (buffer-name appt-disp-buf))
434 (same-window-p (buffer-name appt-disp-buf)))
435 ;; By default, split the bottom window and use the lower part.
436 (appt-select-lowest-window)
437 ;; Split the window, unless it's too small to do so.
438 (when (>= (window-height) (* 2 window-min-height))
439 (select-window (split-window))))
440 (switch-to-buffer appt-disp-buf))
441 ;; FIXME Link to diary entry?
442 (calendar-set-mode-line
443 (format " Appointment %s. %s "
444 (if (string-equal "0" min-to-app) "now"
445 (format "in %s minute%s" min-to-app
446 (if (string-equal "1" min-to-app) "" "s")))
447 new-time))
448 (setq buffer-read-only nil
449 buffer-undo-list t)
450 (erase-buffer)
451 (insert appt-msg)
452 (shrink-window-if-larger-than-buffer (get-buffer-window appt-disp-buf t))
453 (set-buffer-modified-p nil)
454 (setq buffer-read-only t)
455 (raise-frame (selected-frame))
456 (select-window this-window)))
457
458 (defun appt-delete-window ()
459 "Function called to undisplay appointment messages.
460 Usually just deletes the appointment buffer."
461 (let ((window (get-buffer-window appt-buffer-name t)))
462 (and window
463 (or (eq window (frame-root-window (window-frame window)))
464 (delete-window window))))
465 (kill-buffer appt-buffer-name)
466 (if appt-audible
467 (beep 1)))
468
469 (defun appt-select-lowest-window ()
470 "Select the lowest window on the frame."
471 (let ((lowest-window (selected-window))
472 (bottom-edge (nth 3 (window-edges)))
473 next-bottom-edge)
474 (walk-windows (lambda (w)
475 (when (< bottom-edge (setq next-bottom-edge
476 (nth 3 (window-edges w))))
477 (setq bottom-edge next-bottom-edge
478 lowest-window w))) 'nomini)
479 (select-window lowest-window)))
480
481 (defconst appt-time-regexp
482 "[0-9]?[0-9]\\(h\\([0-9][0-9]\\)?\\|[:.][0-9][0-9]\\)\\(am\\|pm\\)?")
483
484 ;;;###autoload
485 (defun appt-add (time msg &optional warntime)
486 "Add an appointment for today at TIME with message MSG.
487 The time should be in either 24 hour format or am/pm format.
488 Optional argument WARNTIME is an integer (or string) giving the number
489 of minutes before the appointment at which to start warning.
490 The default is `appt-message-warning-time'."
491 (interactive "sTime (hh:mm[am/pm]): \nsMessage:
492 sMinutes before the appointment to start warning: ")
493 (unless (string-match appt-time-regexp time)
494 (error "Unacceptable time-string"))
495 (and (stringp warntime)
496 (setq warntime (unless (string-equal warntime "")
497 (string-to-number warntime))))
498 (and warntime
499 (not (integerp warntime))
500 (error "Argument WARNTIME must be an integer, or nil"))
501 (let ((time-msg (list (list (appt-convert-time time))
502 (concat time " " msg) t)))
503 ;; It is presently non-sensical to have multiple warnings about
504 ;; the same appointment with just different delays, but it might
505 ;; not always be so. TODO
506 (if warntime (setq time-msg (append time-msg (list warntime))))
507 (unless (member time-msg appt-time-msg-list)
508 (setq appt-time-msg-list
509 (appt-sort-list (nconc appt-time-msg-list (list time-msg)))))))
510
511 ;;;###autoload
512 (defun appt-delete ()
513 "Delete an appointment from the list of appointments."
514 (interactive)
515 (let ((tmp-msg-list appt-time-msg-list))
516 (dolist (element tmp-msg-list)
517 (if (y-or-n-p (concat "Delete "
518 ;; We want to quote any doublequotes in the
519 ;; string, as well as put doublequotes around it.
520 (prin1-to-string
521 (substring-no-properties (cadr element) 0))
522 " from list? "))
523 (setq appt-time-msg-list (delq element appt-time-msg-list)))))
524 (appt-check)
525 (message ""))
526
527
528 (defvar number)
529 (defvar original-date)
530 (defvar diary-entries-list)
531 ;; Autoload for the old way of using this package. Can be removed sometime.
532 ;;;###autoload
533 (defun appt-make-list ()
534 "Update the appointments list from today's diary buffer.
535 The time must be at the beginning of a line for it to be
536 put in the appointments list (see examples in documentation of
537 the function `appt-check'). We assume that the variables DATE and
538 NUMBER hold the arguments that `diary-list-entries' received.
539 They specify the range of dates that the diary is being processed for.
540
541 Any appointments made with `appt-add' are not affected by this function.
542
543 For backwards compatibility, this function activates the
544 appointment package (if it is not already active)."
545 ;; See comments above appt-activate defun.
546 (if (not appt-timer)
547 (appt-activate 1)
548 ;; We have something to do if the range of dates that the diary is
549 ;; considering includes the current date.
550 (if (and (not (calendar-date-compare
551 (list (calendar-current-date))
552 (list original-date)))
553 (calendar-date-compare
554 (list (calendar-current-date))
555 (list (calendar-gregorian-from-absolute
556 (+ (calendar-absolute-from-gregorian original-date)
557 number)))))
558 (save-excursion
559 ;; Clear the appointments list, then fill it in from the diary.
560 (dolist (elt appt-time-msg-list)
561 ;; Delete any entries that were not made with appt-add.
562 (unless (nth 2 elt)
563 (setq appt-time-msg-list
564 (delq elt appt-time-msg-list))))
565 (if diary-entries-list
566 ;; Cycle through the entry-list (diary-entries-list)
567 ;; looking for entries beginning with a time. If the
568 ;; entry begins with a time, add it to the
569 ;; appt-time-msg-list. Then sort the list.
570 (let ((entry-list diary-entries-list)
571 (new-time-string "")
572 time-string)
573 ;; Skip diary entries for dates before today.
574 (while (and entry-list
575 (calendar-date-compare
576 (car entry-list) (list (calendar-current-date))))
577 (setq entry-list (cdr entry-list)))
578 ;; Parse the entries for today.
579 (while (and entry-list
580 (calendar-date-equal
581 (calendar-current-date) (caar entry-list)))
582 (setq time-string (cadr (car entry-list)))
583 (while (string-match appt-time-regexp time-string)
584 (let* ((beg (match-beginning 0))
585 ;; Get just the time for this appointment.
586 (only-time (match-string 0 time-string))
587 ;; Find the end of this appointment
588 ;; (the start of the next).
589 (end (string-match
590 (concat "\n[ \t]*" appt-time-regexp)
591 time-string
592 (match-end 0)))
593 ;; Get the whole string for this appointment.
594 (appt-time-string
595 (substring time-string beg end))
596 (appt-time (list (appt-convert-time only-time)))
597 (time-msg (list appt-time appt-time-string)))
598 ;; Add this appointment to appt-time-msg-list.
599 (setq appt-time-msg-list
600 (nconc appt-time-msg-list (list time-msg))
601 ;; Discard this appointment from the string.
602 time-string
603 (if end (substring time-string end) ""))))
604 (setq entry-list (cdr entry-list)))))
605 (setq appt-time-msg-list (appt-sort-list appt-time-msg-list))
606 ;; Convert current time to minutes after midnight (12:01am = 1),
607 ;; so that elements in the list that are earlier than the
608 ;; present time can be removed.
609 (let* ((now (decode-time))
610 (cur-comp-time (+ (* 60 (nth 2 now)) (nth 1 now)))
611 (appt-comp-time (caar (car appt-time-msg-list))))
612 (while (and appt-time-msg-list (< appt-comp-time cur-comp-time))
613 (setq appt-time-msg-list (cdr appt-time-msg-list))
614 (if appt-time-msg-list
615 (setq appt-comp-time (caar (car appt-time-msg-list))))))))))
616
617
618 (defun appt-sort-list (appt-list)
619 "Sort an appointment list, putting earlier items at the front.
620 APPT-LIST is a list of the same format as `appt-time-msg-list'."
621 (sort appt-list (lambda (e1 e2) (< (caar e1) (caar e2)))))
622
623
624 (defun appt-convert-time (time2conv)
625 "Convert hour:min[am/pm] format TIME2CONV to minutes from midnight.
626 A period (.) can be used instead of a colon (:) to separate the
627 hour and minute parts."
628 ;; Formats that should be accepted:
629 ;; 10:00 10.00 10h00 10h 10am 10:00am 10.00am
630 (let ((min (if (string-match "[h:.]\\([0-9][0-9]\\)" time2conv)
631 (string-to-number (match-string 1 time2conv))
632 0))
633 (hr (if (string-match "[0-9]*[0-9]" time2conv)
634 (string-to-number (match-string 0 time2conv))
635 0)))
636 ;; Convert the time appointment time into 24 hour time.
637 (cond ((and (string-match "pm" time2conv) (< hr 12))
638 (setq hr (+ 12 hr)))
639 ((and (string-match "am" time2conv) (= hr 12))
640 (setq hr 0)))
641 ;; Convert the actual time into minutes.
642 (+ (* hr 60) min)))
643
644 (defun appt-update-list ()
645 "If the current buffer is visiting the diary, update appointments.
646 This function is intended for use with `write-file-functions'."
647 (and (string-equal buffer-file-name (expand-file-name diary-file))
648 appt-timer
649 (let ((appt-display-diary nil))
650 (appt-check t)))
651 nil)
652
653 ;; In Emacs-21.3, the manual documented the following procedure to
654 ;; activate this package:
655 ;; (display-time)
656 ;; (add-hook 'diary-hook 'appt-make-list)
657 ;; (diary 0)
658 ;; The display-time call was not necessary, AFAICS.
659 ;; What was really needed was to add the hook and load this file.
660 ;; Calling (diary 0) once the hook had been added was in some sense a
661 ;; roundabout way of loading this file. This file used to have code at
662 ;; the top-level that set up the appt-timer and global-mode-string.
663 ;; One way to maintain backwards compatibility would be to call
664 ;; (appt-activate 1) at top-level. However, this goes against the
665 ;; convention that just loading an Emacs package should not activate
666 ;; it. Instead, we make appt-make-list activate the package (after a
667 ;; suggestion from rms). This means that one has to call diary in
668 ;; order to get it to work, but that is in line with the old (weird,
669 ;; IMO) documented behavior for activating the package.
670 ;; Actually, since (diary 0) does not run diary-hook, I don't think
671 ;; the documented behavior in Emacs-21.3 would ever have worked.
672 ;; Oh well, at least with the changes to appt-make-list it will now
673 ;; work as well as it ever did.
674 ;; The new method is just to use (appt-activate 1).
675 ;; -- gmorris
676
677 ;;;###autoload
678 (defun appt-activate (&optional arg)
679 "Toggle checking of appointments.
680 With optional numeric argument ARG, turn appointment checking on if
681 ARG is positive, otherwise off."
682 (interactive "P")
683 (let ((appt-active appt-timer))
684 (setq appt-active (if arg (> (prefix-numeric-value arg) 0)
685 (not appt-active)))
686 (remove-hook 'write-file-functions 'appt-update-list)
687 (or global-mode-string (setq global-mode-string '("")))
688 (delq 'appt-mode-string global-mode-string)
689 (when appt-timer
690 (cancel-timer appt-timer)
691 (setq appt-timer nil))
692 (when appt-active
693 (add-hook 'write-file-functions 'appt-update-list)
694 (setq appt-timer (run-at-time t 60 'appt-check)
695 global-mode-string
696 (append global-mode-string '(appt-mode-string)))
697 (appt-check t))))
698
699
700 (provide 'appt)
701
702 ;; arch-tag: bf5791c4-8921-499e-a26f-772b1788d347
703 ;;; appt.el ends here