appt.el 31 KB

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