1 ;;; time.el --- display time, load and mail indicator in mode line of Emacs
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 96, 2000, 2001, 2002
4 ;; Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
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)
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.
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.
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'.
32 (defgroup display-time nil
33 "Display time and load in mode line of Emacs."
38 (defcustom display-time-mail-file nil
39 "*File name of mail inbox file, for indicating existence of new mail.
40 Non-nil and not a string means don't check for mail. nil means use
41 default, which is system-dependent, and is the same as used by Rmail."
42 :type
'(choice (const :tag
"(None)" none
)
43 (const :tag
"Default" nil
)
47 (defcustom display-time-mail-directory nil
48 "*Name of mail inbox directory, for indicating existence of new mail.
49 Non-nil and not a string means don't check for mail.
50 When nil, no checking for mail in directory is done."
51 :type
'(choice (const :tag
"None" nil
)
52 (directory :format
"%v"))
55 (defcustom display-time-mail-function nil
56 "*Function to call, for indicating existence of new mail.
57 nil means use the default method of checking `display-time-mail-file'
58 or files in `display-time-mail-directory'."
59 :type
'(choice (const :tag
"Default" nil
)
63 (defcustom display-time-default-load-average
0
64 "*Which load-average value will be shown in the mode line.
65 Almost every system can provide values of load for past 1 minute, past 5 or
66 past 15 minutes. The default is to display 1 minute load average."
67 :type
'(choice (const :tag
"1 minute load" 0)
68 (const :tag
"5 minutes load" 1)
69 (const :tag
"15 minutes load" 2))
72 (defvar display-time-load-average display-time-default-load-average
)
74 (defcustom display-time-load-average-threshold
0.1
75 "*Load-average values below this value won't be shown in the mode line."
80 (defcustom display-time-day-and-date nil
"\
81 *Non-nil means \\[display-time] should display day and date as well as time."
85 (defvar display-time-timer nil
)
87 (defcustom display-time-interval
60
88 "*Seconds between updates of time in the mode line."
92 (defcustom display-time-24hr-format nil
93 "*Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23.
94 nil means 1 <= hh <= 12, and an AM/PM suffix is used."
98 (defvar display-time-string nil
)
100 (defcustom display-time-hook nil
101 "*List of functions to be called when the time is updated on the mode line."
103 :group
'display-time
)
105 (defvar display-time-server-down-time nil
106 "Time when mail file's file system was recorded to be down.
107 If that file system seems to be up, the value is nil.")
110 (defun display-time ()
111 "Enable display of time, load level, and mail flag in mode lines.
112 This display updates automatically every minute.
113 If `display-time-day-and-date' is non-nil, the current day and date
114 are displayed as well.
115 This runs the normal hook `display-time-hook' after each update."
117 (display-time-mode 1))
119 (defcustom display-time-mail-face
'mode-line
120 "Face to use for `display-time-mail-string'.
121 If `display-time-use-mail-icon' is non-nil, the image's background
122 colour is the background of this face. Set this to a face other than
123 `mode-line' to make the mail indicator stand out on a suitable
129 (defvar display-time-mail-icon
130 (find-image '((:type xpm
:file
"letter.xpm" :ascent center
)
131 (:type xbm
:file
"letter.xbm" :ascent center
)))
132 "Image specification to offer as the mail indicator on a graphic
133 display. See `display-time-use-mail-icon' and
134 `display-time-mail-face'.")
136 (defcustom display-time-use-mail-icon nil
137 "Non-nil means use an icon as the mail indicator on a graphic display.
138 Otherwise use the string \"Mail\". The icon may consume less of the
139 mode line. It is specified by `display-time-mail-icon'."
143 (defcustom display-time-format nil
144 "*A string specifying the format for displaying the time in the mode line.
145 See the function `format-time-string' for an explanation of
146 how to write this string. If this is nil, the defaults
147 depend on `display-time-day-and-date' and `display-time-24hr-format'."
148 :type
'(choice (const :tag
"Default" nil
)
150 :group
'display-time
)
152 (defcustom display-time-string-forms
153 '((if (and (not display-time-format
) display-time-day-and-date
)
154 (format-time-string "%a %b %e " now
)
156 (format-time-string (or display-time-format
157 (if display-time-24hr-format
"%H:%M" "%-I:%M%p"))
161 ;; Build the string every time to act on customization.
165 'display
`(when (and display-time-use-mail-icon
167 ,@display-time-mail-icon
168 ,@(list :background
(face-attribute
169 display-time-mail-face
171 'help-echo
"You have new mail; mouse-2: Read mail"
172 'local-map
(make-mode-line-mouse-map 'mouse-2
175 "*A list of expressions governing display of the time in the mode line.
176 For most purposes, you can control the time format using `display-time-format'
177 which is a more standard interface.
179 This expression is a list of expressions that can involve the keywords
180 `load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
181 `seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
182 and `time-zone' all alphabetic strings, and `mail' a true/nil value.
184 For example, the form
186 '((substring year -2) \"/\" month \"/\" day
187 \" \" 24-hours \":\" minutes \":\" seconds
188 (if time-zone \" (\") time-zone (if time-zone \")\")
189 (if mail \" Mail\" \"\"))
191 would give mode line times like `94/12/30 21:07:48 (UTC)'."
193 :group
'display-time
)
195 (defun display-time-event-handler ()
196 (display-time-update)
197 ;; Do redisplay right now, if no input pending.
199 (let* ((current (current-time))
200 (timer display-time-timer
)
201 ;; Compute the time when this timer will run again, next.
202 (next-time (timer-relative-time
203 (list (aref timer
1) (aref timer
2) (aref timer
3))
204 (* 5 (aref timer
4)) 0)))
205 ;; If the activation time is far in the past,
206 ;; skip executions until we reach a time in the future.
207 ;; This avoids a long pause if Emacs has been suspended for hours.
208 (or (> (nth 0 next-time
) (nth 0 current
))
209 (and (= (nth 0 next-time
) (nth 0 current
))
210 (> (nth 1 next-time
) (nth 1 current
)))
211 (and (= (nth 0 next-time
) (nth 0 current
))
212 (= (nth 1 next-time
) (nth 1 current
))
213 (> (nth 2 next-time
) (nth 2 current
)))
215 (timer-set-time timer
(timer-next-integral-multiple-of-time
216 current display-time-interval
)
217 display-time-interval
)
218 (timer-activate timer
)))))
220 (defun display-time-next-load-average ()
222 (if (= 3 (setq display-time-load-average
(1+ display-time-load-average
)))
223 (setq display-time-load-average
0))
224 (display-time-update)
227 (defun display-time-mail-check-directory ()
228 (let ((mail-files (directory-files display-time-mail-directory t
))
230 (while (and mail-files
(= size
0))
231 ;; Count size of regular files only.
232 (setq size
(+ size
(or (and (file-regular-p (car mail-files
))
233 (nth 7 (file-attributes (car mail-files
))))
235 (setq mail-files
(cdr mail-files
)))
240 ;; Update the display-time info for the mode line
241 ;; but don't redisplay right now. This is used for
242 ;; things like Rmail `g' that want to force an update
243 ;; which can wait for the next redisplay.
244 (defun display-time-update ()
245 (let* ((now (current-time))
246 (time (current-time-string now
))
247 (load (condition-case ()
248 ;; Do not show values less than
249 ;; `display-time-load-average-threshold'.
250 (if (> (* display-time-load-average-threshold
100)
251 (nth display-time-load-average
(load-average)))
253 ;; The load average number is mysterious, so
254 ;; provide some help.
255 (let ((str (format " %03d" (nth display-time-load-average
(load-average)))))
257 (concat (substring str
0 -
2) "." (substring str -
2))
258 'local-map
(make-mode-line-mouse-map 'mouse-2
259 'display-time-next-load-average
)
260 'help-echo
(concat "System load average for past "
261 (if (= 0 display-time-load-average
)
263 (if (= 1 display-time-load-average
)
265 "15 minutes")) "; mouse-2: next" ))))
267 (mail-spool-file (or display-time-mail-file
269 (concat rmail-spool-directory
271 (mail (or (and display-time-mail-function
272 (funcall display-time-mail-function
))
273 (and display-time-mail-directory
274 (display-time-mail-check-directory))
275 (and (stringp mail-spool-file
)
276 (or (null display-time-server-down-time
)
277 ;; If have been down for 20 min, try again.
278 (> (- (nth 1 now
) display-time-server-down-time
)
280 (and (< (nth 1 now
) display-time-server-down-time
)
281 (> (- (nth 1 now
) display-time-server-down-time
)
283 (let ((start-time (current-time)))
285 (display-time-file-nonempty-p mail-spool-file
)
286 (if (> (- (nth 1 (current-time)) (nth 1 start-time
))
288 ;; Record that mail file is not accessible.
289 (setq display-time-server-down-time
290 (nth 1 (current-time)))
291 ;; Record that mail file is accessible.
292 (setq display-time-server-down-time nil
)))))))
293 (24-hours (substring time
11 13))
294 (hour (string-to-int 24-hours
))
295 (12-hours (int-to-string (1+ (%
(+ hour
11) 12))))
296 (am-pm (if (>= hour
12) "pm" "am"))
297 (minutes (substring time
14 16))
298 (seconds (substring time
17 19))
299 (time-zone (car (cdr (current-time-zone now
))))
300 (day (substring time
8 10))
301 (year (substring time
20 24))
302 (monthname (substring time
4 7))
307 '(("Jan" .
"1") ("Feb" .
"2") ("Mar" .
"3") ("Apr" .
"4")
308 ("May" .
"5") ("Jun" .
"6") ("Jul" .
"7") ("Aug" .
"8")
309 ("Sep" .
"9") ("Oct" .
"10") ("Nov" .
"11") ("Dec" .
"12")))))
310 (dayname (substring time
0 3)))
311 (setq display-time-string
312 (mapconcat 'eval display-time-string-forms
""))
313 ;; This is inside the let binding, but we are not going to document
314 ;; what variables are available.
315 (run-hooks 'display-time-hook
))
316 (force-mode-line-update))
318 (defun display-time-file-nonempty-p (file)
319 (and (file-exists-p file
)
320 (< 0 (nth 7 (file-attributes (file-chase-links file
))))))
323 (define-minor-mode display-time-mode
324 "Toggle display of time, load level, and mail flag in mode lines.
325 With a numeric arg, enable this display if arg is positive.
327 When this display is enabled, it updates automatically every minute.
328 If `display-time-day-and-date' is non-nil, the current day and date
329 are displayed as well.
330 This runs the normal hook `display-time-hook' after each update."
331 :global t
:group
'display-time
332 (and display-time-timer
(cancel-timer display-time-timer
))
333 (setq display-time-timer nil
)
334 (setq display-time-string
"")
335 (or global-mode-string
(setq global-mode-string
'("")))
336 (if display-time-mode
338 (or (memq 'display-time-string global-mode-string
)
339 (setq global-mode-string
340 (append global-mode-string
'(display-time-string))))
341 ;; Set up the time timer.
342 (setq display-time-timer
343 (run-at-time t display-time-interval
344 'display-time-event-handler
))
345 ;; Make the time appear right away.
346 (display-time-update)
347 ;; When you get new mail, clear "Mail" from the mode line.
348 (add-hook 'rmail-after-get-new-mail-hook
349 'display-time-event-handler
))
350 (remove-hook 'rmail-after-get-new-mail-hook
351 'display-time-event-handler
)))
355 ;;; time.el ends here