(display-time): Use rmail-after-get-new-mail-hook.
[emacs.git] / lisp / time.el
blobf3453c0bff10affaedec9edd7d5b2c6a0e86cd05
1 ;;; time.el --- display time and load in mode line of Emacs.
3 ;; Copyright (C) 1985, 1986, 1987, 1993, 1994 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
7 ;; 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 2, or (at your option)
12 ;; any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
24 ;;; Commentary:
26 ;; Facilities to display current time/date and a new-mail indicator
27 ;; in the Emacs mode line. The single entry point is `display-time'.
29 ;;; Code:
31 (defvar display-time-mail-file nil
32 "*File name of mail inbox file, for indicating existence of new mail.
33 Non-nil and not a string means don't check for mail. nil means use
34 default, which is system-dependent, and is the same as used by Rmail.")
36 ;;;###autoload
37 (defvar display-time-day-and-date nil "\
38 *Non-nil means \\[display-time] should display day and date as well as time.")
40 (defvar display-time-timer nil)
42 (defvar display-time-interval 60
43 "*Seconds between updates of time in the mode line.")
45 (defvar display-time-24hr-format nil
46 "*Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23.
47 Nil means 1 <= hh <= 12, and an AM/PM suffix is used.")
49 (defvar display-time-string nil)
51 (defvar display-time-hook nil
52 "* List of functions to be called when the time is updated on the mode line.")
54 (defvar display-time-server-down-time nil
55 "Time when mail file's file system was recorded to be down.
56 If that file system seems to be up, the value is nil.")
58 ;;;###autoload
59 (defun display-time ()
60 "Display current time, load level, and mail flag in mode line of each buffer.
61 Updates automatically every minute.
62 If `display-time-day-and-date' is non-nil, the current day and date
63 are displayed as well.
64 After each update, `display-time-hook' is run with `run-hooks'."
65 (interactive)
66 (setq display-time-string "")
67 (or global-mode-string (setq global-mode-string '("")))
68 (or (memq 'display-time-string global-mode-string)
69 (setq global-mode-string
70 (append global-mode-string '(display-time-string))))
71 ;; Setup the time timer.
72 (and display-time-timer (cancel-timer display-time-timer))
73 (setq display-time-timer
74 (run-at-time nil display-time-interval 'display-time-event-handler))
75 ;; When you get new mail, clear "Mail" from the mode line.
76 (add-hook 'rmail-after-get-new-mail-hook 'display-time-event-handler))
78 (defvar display-time-string-forms
79 '((if display-time-day-and-date
80 (format "%s %s %s " dayname monthname day)
81 "")
82 (format "%s:%s%s"
83 (if display-time-24hr-format 24-hours 12-hours)
84 minutes
85 (if display-time-24hr-format "" am-pm))
86 load
87 (if mail " Mail" ""))
88 "*A list of expressions governing display of the time in the mode line.
89 This expression is a list of expressions that can involve the keywords
90 `load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
91 `seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
92 and `time-zone' all alphabetic strings, and `mail' a true/nil value.
94 For example, the form
96 '((substring year -2) \"/\" month \"/\" day
97 \" \" 24-hours \":\" minutes \":\" seconds
98 (if time-zone \" (\") time-zone (if time-zone \")\")
99 (if mail \" Mail\" \"\"))
101 would give mode line times like `94/12/30 21:07:48 (UTC)'.")
103 (defun display-time-event-handler ()
104 (display-time-update)
105 ;; Do redisplay right now, if no input pending.
106 (sit-for 0))
108 ;; Update the display-time info for the mode line
109 ;; but don't redisplay right now. This is used for
110 ;; things like Rmail `g' that want to force an update
111 ;; which can wait for the next redisplay.
112 (defun display-time-update ()
113 (let* ((now (current-time))
114 (time (current-time-string now))
115 (load (condition-case ()
116 (if (zerop (car (load-average))) ""
117 (let ((str (format " %03d" (car (load-average)))))
118 (concat (substring str 0 -2) "." (substring str -2))))
119 (error "")))
120 (mail-spool-file (or display-time-mail-file
121 (getenv "MAIL")
122 (concat rmail-spool-directory
123 (user-login-name))))
124 (mail (and (stringp mail-spool-file)
125 (or (null display-time-server-down-time)
126 ;; If have been down for 20 min, try again.
127 (> (- (nth 1 (current-time))
128 display-time-server-down-time)
129 1200))
130 (let ((start-time (current-time)))
131 (prog1
132 (display-time-file-nonempty-p mail-spool-file)
133 (if (> (- (nth 1 (current-time)) (nth 1 start-time))
135 ;; Record that mail file is not accessible.
136 (setq display-time-server-down-time
137 (nth 1 (current-time)))
138 ;; Record that mail file is accessible.
139 (setq display-time-server-down-time nil))))))
140 (24-hours (substring time 11 13))
141 (hour (string-to-int 24-hours))
142 (12-hours (int-to-string (1+ (% (+ hour 11) 12))))
143 (am-pm (if (>= hour 12) "pm" "am"))
144 (minutes (substring time 14 16))
145 (seconds (substring time 17 19))
146 (time-zone (car (cdr (current-time-zone now))))
147 (day (substring time 8 10))
148 (year (substring time 20 24))
149 (monthname (substring time 4 7))
150 (month
151 (cdr
152 (assoc
153 monthname
154 '(("Jan" . "1") ("Feb" . "2") ("Mar" . "3") ("Apr" . "4")
155 ("May" . "5") ("Jun" . "6") ("Jul" . "7") ("Aug" . "8")
156 ("Sep" . "9") ("Oct" . "10") ("Nov" . "11") ("Dec" . "12")))))
157 (dayname (substring time 0 3)))
158 (setq display-time-string
159 (mapconcat 'eval display-time-string-forms ""))
160 ;; This is inside the let binding, but we are not going to document
161 ;; what variables are available.
162 (run-hooks 'display-time-hook))
163 (force-mode-line-update))
165 (defun display-time-file-nonempty-p (file)
166 (and (file-exists-p file)
167 (< 0 (nth 7 (file-attributes (file-chase-links file))))))
169 ;;; time.el ends here