Fix typos.
[bpt/emacs.git] / lisp / time.el
CommitLineData
55535639 1;;; time.el --- display time, load and mail indicator in mode line of Emacs
c2acf685 2
a7850723
GM
3;; Copyright (C) 1985, 86, 87, 93, 94, 96, 2000, 2001
4;; Free Software Foundation, Inc.
c2acf685
SM
5
6;; Maintainer: FSF
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
24
25;;; Commentary:
26
27;; Facilities to display current time/date and a new-mail indicator
28;; in the Emacs mode line. The single entry point is `display-time'.
29
30;;; Code:
31
32(defgroup display-time nil
33 "Display time and load in mode line of Emacs."
34 :group 'modeline
35 :group 'mail)
36
37
c2acf685
SM
38(defcustom display-time-mail-file nil
39 "*File name of mail inbox file, for indicating existence of new mail.
40Non-nil and not a string means don't check for mail. nil means use
41default, which is system-dependent, and is the same as used by Rmail."
42 :type '(choice (const :tag "(None)" none)
43 (const :tag "Default" nil)
44 (file :format "%v"))
45 :group 'display-time)
46
47(defcustom display-time-mail-function nil
48 "*Function to call, for indicating existence of new mail.
49nil means use the default method of checking `display-time-mail-file'."
50 :type '(choice (const :tag "Default" nil)
51 (function))
52 :group 'display-time)
53
4b05e68d
PJ
54(defcustom display-time-default-load-average 0
55 "*Which load-average value will be shown in the mode line.
56Almost every system can provide values of load for past 1 minute, past 5 or
57past 15 minutes. The default is to display 1 minute load average."
58 :type '(choice (const :tag "1 minute load" 0)
59 (const :tag "5 minutes load" 1)
60 (const :tag "15 minutes load" 2))
61 :group 'display-time)
62
63(defvar display-time-load-average display-time-default-load-average)
64
65(defcustom display-time-load-average-threshold 0.1
b7b4e4d4
PJ
66 "*Load-average values below this value won't be shown in the mode line."
67 :type 'number
68 :group 'display-time)
4b05e68d 69
c2acf685
SM
70;;;###autoload
71(defcustom display-time-day-and-date nil "\
72*Non-nil means \\[display-time] should display day and date as well as time."
73 :type 'boolean
74 :group 'display-time)
75
76(defvar display-time-timer nil)
77
78(defcustom display-time-interval 60
79 "*Seconds between updates of time in the mode line."
80 :type 'integer
81 :group 'display-time)
82
83(defcustom display-time-24hr-format nil
84 "*Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23.
4b05e68d 85nil means 1 <= hh <= 12, and an AM/PM suffix is used."
c2acf685
SM
86 :type 'boolean
87 :group 'display-time)
88
89(defvar display-time-string nil)
90
91(defcustom display-time-hook nil
92 "*List of functions to be called when the time is updated on the mode line."
93 :type 'hook
94 :group 'display-time)
95
96(defvar display-time-server-down-time nil
97 "Time when mail file's file system was recorded to be down.
98If that file system seems to be up, the value is nil.")
99
100;;;###autoload
101(defun display-time ()
102 "Enable display of time, load level, and mail flag in mode lines.
103This display updates automatically every minute.
104If `display-time-day-and-date' is non-nil, the current day and date
105are displayed as well.
106This runs the normal hook `display-time-hook' after each update."
107 (interactive)
108 (display-time-mode 1))
109
c2acf685
SM
110(defcustom display-time-mail-face 'mode-line
111 "Face to use for `display-time-mail-string'.
112If `display-time-use-mail-icon' is non-nil, the image's background
113colour is the background of this face. Set this to a face other than
114`mode-line' to make the mail indicator stand out on a suitable
115display."
116 :group 'faces
117 :group 'display-time
118 :type 'face)
119
120(defvar display-time-mail-icon
121 (find-image '((:type xbm :file "letter.xbm" :ascent center)))
122 "Image specification to offer as the mail indicator on a graphic
123display. See `display-time-use-mail-icon' and
124`display-time-mail-face'.")
125
126(defcustom display-time-use-mail-icon nil
127 "Non-nil means use an icon as the mail indicator on a graphic display.
128Otherwise use the string \"Mail\". The icon may consume less of the
129mode line. It is specified by `display-time-mail-icon'."
130 :group 'display-time
131 :type 'boolean)
132
133(defcustom display-time-format nil
134 "*A string specifying the format for displaying the time in the mode line.
135See the function `format-time-string' for an explanation of
136how to write this string. If this is nil, the defaults
137depend on `display-time-day-and-date' and `display-time-24hr-format'."
138 :type '(choice (const :tag "Default" nil)
139 string)
140 :group 'display-time)
141
142(defcustom display-time-string-forms
143 '((if (and (not display-time-format) display-time-day-and-date)
144 (format-time-string "%a %b %e " now)
145 "")
146 (format-time-string (or display-time-format
147 (if display-time-24hr-format "%H:%M" "%-I:%M%p"))
148 now)
149 load
150 (if mail
151 ;; Build the string every time to act on customization.
152 (concat " "
153 (propertize
154 "Mail"
155 'display `(when (and display-time-use-mail-icon
156 (display-graphic-p))
157 ,@display-time-mail-icon
158 ,@(list :background (face-attribute
159 display-time-mail-face
160 :background)))
161 'help-echo "mouse-2: Read mail"
b03e7e2f 162 'local-map (make-mode-line-mouse-map 'mouse-2
39fce518 163 read-mail-command)))
c2acf685
SM
164 ""))
165 "*A list of expressions governing display of the time in the mode line.
166For most purposes, you can control the time format using `display-time-format'
167which is a more standard interface.
168
169This expression is a list of expressions that can involve the keywords
170`load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
171`seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
172and `time-zone' all alphabetic strings, and `mail' a true/nil value.
173
174For example, the form
175
176 '((substring year -2) \"/\" month \"/\" day
177 \" \" 24-hours \":\" minutes \":\" seconds
178 (if time-zone \" (\") time-zone (if time-zone \")\")
179 (if mail \" Mail\" \"\"))
180
181would give mode line times like `94/12/30 21:07:48 (UTC)'."
182 :type 'sexp
183 :group 'display-time)
184
185(defun display-time-event-handler ()
186 (display-time-update)
187 ;; Do redisplay right now, if no input pending.
188 (sit-for 0)
189 (let* ((current (current-time))
190 (timer display-time-timer)
191 ;; Compute the time when this timer will run again, next.
192 (next-time (timer-relative-time
193 (list (aref timer 1) (aref timer 2) (aref timer 3))
194 (* 5 (aref timer 4)) 0)))
195 ;; If the activation time is far in the past,
196 ;; skip executions until we reach a time in the future.
197 ;; This avoids a long pause if Emacs has been suspended for hours.
198 (or (> (nth 0 next-time) (nth 0 current))
199 (and (= (nth 0 next-time) (nth 0 current))
200 (> (nth 1 next-time) (nth 1 current)))
201 (and (= (nth 0 next-time) (nth 0 current))
202 (= (nth 1 next-time) (nth 1 current))
203 (> (nth 2 next-time) (nth 2 current)))
204 (progn
205 (timer-set-time timer (timer-next-integral-multiple-of-time
206 current display-time-interval)
207 display-time-interval)
208 (timer-activate timer)))))
209
4b05e68d
PJ
210(defun display-time-next-load-average ()
211 (interactive)
212 (if (= 3 (setq display-time-load-average (1+ display-time-load-average)))
213 (setq display-time-load-average 0))
214 (display-time-update)
215 (sit-for 0))
216
c2acf685
SM
217;; Update the display-time info for the mode line
218;; but don't redisplay right now. This is used for
219;; things like Rmail `g' that want to force an update
220;; which can wait for the next redisplay.
221(defun display-time-update ()
222 (let* ((now (current-time))
223 (time (current-time-string now))
224 (load (condition-case ()
4b05e68d
PJ
225 ;; Do not show values less than
226 ;; `display-time-load-average-threshold'.
227 (if (> (* display-time-load-average-threshold 100)
228 (nth display-time-load-average (load-average)))
229 ""
c2acf685 230 ;; The load average number is mysterious, so
4b05e68d
PJ
231 ;; provide some help.
232 (let ((str (format " %03d" (nth display-time-load-average (load-average)))))
c2acf685
SM
233 (propertize
234 (concat (substring str 0 -2) "." (substring str -2))
b03e7e2f 235 'local-map (make-mode-line-mouse-map 'mouse-2
4b05e68d
PJ
236 'display-time-next-load-average)
237 'help-echo (concat "System load average for past "
238 (if (= 0 display-time-load-average)
239 "1 minute"
240 (if (= 1 display-time-load-average)
241 "5 minutes"
242 "15 minutes")) "; mouse-2: next" ))))
c2acf685
SM
243 (error "")))
244 (mail-spool-file (or display-time-mail-file
245 (getenv "MAIL")
246 (concat rmail-spool-directory
247 (user-login-name))))
248 (mail (or (and display-time-mail-function
249 (funcall display-time-mail-function))
250 (and (stringp mail-spool-file)
251 (or (null display-time-server-down-time)
252 ;; If have been down for 20 min, try again.
253 (> (- (nth 1 now) display-time-server-down-time)
254 1200)
255 (and (< (nth 1 now) display-time-server-down-time)
256 (> (- (nth 1 now) display-time-server-down-time)
257 -64336)))
258 (let ((start-time (current-time)))
259 (prog1
260 (display-time-file-nonempty-p mail-spool-file)
261 (if (> (- (nth 1 (current-time)) (nth 1 start-time))
262 20)
263 ;; Record that mail file is not accessible.
264 (setq display-time-server-down-time
265 (nth 1 (current-time)))
266 ;; Record that mail file is accessible.
267 (setq display-time-server-down-time nil)))))))
268 (24-hours (substring time 11 13))
269 (hour (string-to-int 24-hours))
270 (12-hours (int-to-string (1+ (% (+ hour 11) 12))))
271 (am-pm (if (>= hour 12) "pm" "am"))
272 (minutes (substring time 14 16))
273 (seconds (substring time 17 19))
274 (time-zone (car (cdr (current-time-zone now))))
275 (day (substring time 8 10))
276 (year (substring time 20 24))
277 (monthname (substring time 4 7))
278 (month
279 (cdr
280 (assoc
281 monthname
282 '(("Jan" . "1") ("Feb" . "2") ("Mar" . "3") ("Apr" . "4")
283 ("May" . "5") ("Jun" . "6") ("Jul" . "7") ("Aug" . "8")
284 ("Sep" . "9") ("Oct" . "10") ("Nov" . "11") ("Dec" . "12")))))
285 (dayname (substring time 0 3)))
286 (setq display-time-string
287 (mapconcat 'eval display-time-string-forms ""))
288 ;; This is inside the let binding, but we are not going to document
289 ;; what variables are available.
290 (run-hooks 'display-time-hook))
291 (force-mode-line-update))
292
293(defun display-time-file-nonempty-p (file)
294 (and (file-exists-p file)
295 (< 0 (nth 7 (file-attributes (file-chase-links file))))))
296
57ce63c4
SM
297;;;###autoload
298(define-minor-mode display-time-mode
299 "Toggle display of time, load level, and mail flag in mode lines.
300With a numeric arg, enable this display if arg is positive.
301
302When this display is enabled, it updates automatically every minute.
303If `display-time-day-and-date' is non-nil, the current day and date
304are displayed as well.
305This runs the normal hook `display-time-hook' after each update."
bb7a71c5 306 :global t :group 'display-time
57ce63c4
SM
307 (and display-time-timer (cancel-timer display-time-timer))
308 (setq display-time-timer nil)
309 (setq display-time-string "")
310 (or global-mode-string (setq global-mode-string '("")))
311 (if display-time-mode
312 (progn
313 (or (memq 'display-time-string global-mode-string)
314 (setq global-mode-string
315 (append global-mode-string '(display-time-string))))
316 ;; Set up the time timer.
317 (setq display-time-timer
318 (run-at-time t display-time-interval
319 'display-time-event-handler))
320 ;; Make the time appear right away.
321 (display-time-update)
322 ;; When you get new mail, clear "Mail" from the mode line.
323 (add-hook 'rmail-after-get-new-mail-hook
324 'display-time-event-handler))
325 (remove-hook 'rmail-after-get-new-mail-hook
326 'display-time-event-handler)))
c2acf685
SM
327
328(provide 'time)
329
330;;; time.el ends here