1 ;;; time.el --- display time, load and mail indicator in mode line of Emacs -*-coding: utf-8 -*-
3 ;; Copyright (C) 1985-1987, 1993-1994, 1996, 2000-2011
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 3 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
25 ;; Facilities to display current time/date and a new-mail indicator
26 ;; in the Emacs mode line. The entry point is `display-time'.
28 ;; Display time world in a buffer, the entry point is
29 ;; `display-time-world'.
33 (defgroup display-time nil
34 "Display time and load in mode line of Emacs."
39 (defcustom display-time-mail-file nil
40 "File name of mail inbox file, for indicating existence of new mail.
41 Non-nil and not a string means don't check for mail; nil means use
42 default, which is system-dependent, and is the same as used by Rmail."
43 :type
'(choice (const :tag
"None" none
)
44 (const :tag
"Default" nil
)
48 (defcustom display-time-mail-directory nil
49 "Name of mail inbox directory, for indicating existence of new mail.
50 Any nonempty regular file in the directory is regarded as newly arrived mail.
51 If nil, do not check a directory for arriving mail."
52 :type
'(choice (const :tag
"None" nil
)
53 (directory :format
"%v"))
56 (defcustom display-time-mail-function nil
57 "Function to call, for indicating existence of new mail.
58 If nil, that means use the default method: check that the file
59 specified by `display-time-mail-file' is nonempty or that the
60 directory `display-time-mail-directory' contains nonempty files."
61 :type
'(choice (const :tag
"Default" nil
)
65 (defcustom display-time-default-load-average
0
66 "Which load average value will be shown in the mode line.
67 Almost every system can provide values of load for past 1 minute, past 5 or
68 past 15 minutes. The default is to display 1 minute load average.
69 The value can be one of:
74 :type
'(choice (const :tag
"1 minute load" 0)
75 (const :tag
"5 minutes load" 1)
76 (const :tag
"15 minutes load" 2)
77 (const :tag
"None" nil
))
80 (defvar display-time-load-average nil
81 "Load average currently being shown in mode line.")
83 (defcustom display-time-load-average-threshold
0.1
84 "Load-average values below this value won't be shown in the mode line."
89 (defcustom display-time-day-and-date nil
"\
90 Non-nil means \\[display-time] should display day and date as well as time."
94 (defvar display-time-timer nil
)
96 (defcustom display-time-interval
60
97 "Seconds between updates of time in the mode line."
101 (defcustom display-time-24hr-format nil
102 "Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23.
103 A value of nil means 1 <= hh <= 12, and an AM/PM suffix is used."
105 :group
'display-time
)
107 (defvar display-time-string nil
)
108 ;;;###autoload(put 'display-time-string 'risky-local-variable t)
110 (defcustom display-time-hook nil
111 "List of functions to be called when the time is updated on the mode line."
113 :group
'display-time
)
115 (defvar display-time-server-down-time nil
116 "Time when mail file's file system was recorded to be down.
117 If that file system seems to be up, the value is nil.")
119 (defcustom zoneinfo-style-world-list
120 '(("America/Los_Angeles" "Seattle")
121 ("America/New_York" "New York")
122 ("Europe/London" "London")
123 ("Europe/Paris" "Paris")
124 ("Asia/Calcutta" "Bangalore")
125 ("Asia/Tokyo" "Tokyo"))
126 "Alist of zoneinfo-style time zones and places for `display-time-world'.
127 Each element has the form (TIMEZONE LABEL).
128 TIMEZONE should be a string of the form AREA/LOCATION, where AREA is
129 the name of a region -- a continent or ocean, and LOCATION is the name
130 of a specific location, e.g., a city, within that region.
131 LABEL is a string to display as the label of that TIMEZONE's time."
133 :type
'(repeat (list string string
))
136 (defcustom legacy-style-world-list
137 '(("PST8PDT" "Seattle")
138 ("EST5EDT" "New York")
141 ("IST-5:30" "Bangalore")
143 "Alist of traditional-style time zones and places for `display-time-world'.
144 Each element has the form (TIMEZONE LABEL).
145 TIMEZONE should be a string of the form:
147 std[+|-]offset[dst[offset][,date[/time],date[/time]]]
149 See the documentation of the TZ environment variable on your system,
150 for more details about the format of TIMEZONE.
151 LABEL is a string to display as the label of that TIMEZONE's time."
153 :type
'(repeat (list string string
))
156 (defcustom display-time-world-list
157 ;; Determine if zoneinfo style timezones are supported by testing that
158 ;; America/New York and Europe/London return different timezones.
160 (set-time-zone-rule "America/New_York")
161 (setq nyt
(format-time-string "%z"))
162 (set-time-zone-rule "Europe/London")
163 (setq gmt
(format-time-string "%z"))
164 (set-time-zone-rule nil
)
165 (if (string-equal nyt gmt
)
166 legacy-style-world-list
167 zoneinfo-style-world-list
))
168 "Alist of time zones and places for `display-time-world' to display.
169 Each element has the form (TIMEZONE LABEL).
170 TIMEZONE should be in the format supported by `set-time-zone-rule' on
171 your system. See the documentation of `zoneinfo-style-world-list' and
172 \`legacy-style-world-list' for two widely used formats.
173 LABEL is a string to display as the label of that TIMEZONE's time."
175 :type
'(repeat (list string string
))
178 (defcustom display-time-world-time-format
"%A %d %B %R %Z"
179 "Format of the time displayed, see `format-time-string'."
184 (defcustom display-time-world-buffer-name
"*wclock*"
185 "Name of the world clock buffer."
190 (defcustom display-time-world-timer-enable t
191 "If non-nil, a timer will update the world clock."
196 (defcustom display-time-world-timer-second
60
197 "Interval in seconds for updating the world clock."
202 (defvar display-time-world-mode-map
203 (let ((map (make-sparse-keymap)))
204 (define-key map
"q" 'kill-this-buffer
)
206 "Keymap of Display Time World mode.")
209 (defun display-time ()
210 "Enable display of time, load level, and mail flag in mode lines.
211 This display updates automatically every minute.
212 If `display-time-day-and-date' is non-nil, the current day and date
213 are displayed as well.
214 This runs the normal hook `display-time-hook' after each update."
216 (display-time-mode 1))
218 ;; This business used to be simpler when all mode lines had the same
219 ;; face and the image could just be pbm. Now we try to rely on an xpm
220 ;; image with a transparent background. Otherwise, set the background
223 (defcustom display-time-mail-face nil
224 "Face to use for `display-time-mail-string'.
225 If `display-time-use-mail-icon' is non-nil, the image's
226 background color is the background of this face. Set this to
227 make the mail indicator stand out on a color display."
228 :group
'mode-line-faces
231 :type
'(choice (const :tag
"None" nil
) face
))
233 (defvar display-time-mail-icon
234 (find-image '((:type xpm
:file
"letter.xpm" :ascent center
)
235 (:type pbm
:file
"letter.pbm" :ascent center
)))
236 "Image specification to offer as the mail indicator on a graphic display.
237 See `display-time-use-mail-icon' and `display-time-mail-face'.")
239 ;; Fixme: Default to icon on graphical display?
240 (defcustom display-time-use-mail-icon nil
241 "Non-nil means use an icon as mail indicator on a graphic display.
242 Otherwise use `display-time-mail-string'. The icon may consume less
243 of the mode line. It is specified by `display-time-mail-icon'."
247 ;; Fixme: maybe default to the character if we can display Unicode.
248 (defcustom display-time-mail-string
"Mail"
249 "String to use as the mail indicator in `display-time-string-forms'.
250 This can use the Unicode letter character if you can display it."
253 :type
'(choice (const "Mail")
254 ;; Use :tag here because the Lucid menu won't display
256 (const :tag
"Unicode letter character" "✉")
259 (defcustom display-time-format nil
260 "String specifying format for displaying the time in the mode line.
261 See the function `format-time-string' for an explanation of
262 how to write this string. If this is nil, the defaults
263 depend on `display-time-day-and-date' and `display-time-24hr-format'."
264 :type
'(choice (const :tag
"Default" nil
)
266 :group
'display-time
)
268 (defcustom display-time-string-forms
269 '((if (and (not display-time-format
) display-time-day-and-date
)
270 (format-time-string "%a %b %e " now
)
273 (format-time-string (or display-time-format
274 (if display-time-24hr-format
"%H:%M" "%-I:%M%p"))
276 'help-echo
(format-time-string "%a %b %e, %Y" now
))
279 ;; Build the string every time to act on customization.
280 ;; :set-after doesn't help for `customize-option'. I think it
285 display-time-mail-string
286 'display
`(when (and display-time-use-mail-icon
288 ,@display-time-mail-icon
289 ,@(if (and display-time-mail-face
290 (memq (plist-get (cdr display-time-mail-icon
)
293 (let ((bg (face-attribute display-time-mail-face
296 (list :background bg
)))))
297 'face display-time-mail-face
298 'help-echo
"You have new mail; mouse-2: Read mail"
299 'mouse-face
'mode-line-highlight
300 'local-map
(make-mode-line-mouse-map 'mouse-2
303 "List of expressions governing display of the time in the mode line.
304 For most purposes, you can control the time format using `display-time-format'
305 which is a more standard interface.
307 This expression is a list of expressions that can involve the keywords
308 `load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
309 `seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
310 and `time-zone' all alphabetic strings, and `mail' a true/nil value.
312 For example, the form
314 '((substring year -2) \"/\" month \"/\" day
315 \" \" 24-hours \":\" minutes \":\" seconds
316 (if time-zone \" (\") time-zone (if time-zone \")\")
317 (if mail \" Mail\" \"\"))
319 would give mode line times like `94/12/30 21:07:48 (UTC)'."
321 :group
'display-time
)
323 (defun display-time-event-handler ()
324 (display-time-update)
325 ;; Do redisplay right now, if no input pending.
327 (let* ((current (current-time))
328 (timer display-time-timer
)
329 ;; Compute the time when this timer will run again, next.
330 (next-time (timer-relative-time
331 (list (aref timer
1) (aref timer
2) (aref timer
3))
332 (* 5 (aref timer
4)) 0)))
333 ;; If the activation time is far in the past,
334 ;; skip executions until we reach a time in the future.
335 ;; This avoids a long pause if Emacs has been suspended for hours.
336 (or (> (nth 0 next-time
) (nth 0 current
))
337 (and (= (nth 0 next-time
) (nth 0 current
))
338 (> (nth 1 next-time
) (nth 1 current
)))
339 (and (= (nth 0 next-time
) (nth 0 current
))
340 (= (nth 1 next-time
) (nth 1 current
))
341 (> (nth 2 next-time
) (nth 2 current
)))
343 (timer-set-time timer
(timer-next-integral-multiple-of-time
344 current display-time-interval
)
345 display-time-interval
)
346 (timer-activate timer
)))))
348 (defun display-time-next-load-average ()
350 (if (= 3 (setq display-time-load-average
(1+ display-time-load-average
)))
351 (setq display-time-load-average
0))
352 (display-time-update)
355 (defun display-time-mail-check-directory ()
356 (let ((mail-files (directory-files display-time-mail-directory t
))
358 (while (and mail-files
(= size
0))
359 ;; Count size of regular files only.
360 (setq size
(+ size
(or (and (file-regular-p (car mail-files
))
361 (nth 7 (file-attributes (car mail-files
))))
363 (setq mail-files
(cdr mail-files
)))
368 (defun display-time-update ()
369 "Update the display-time info for the mode line.
370 However, don't redisplay right now.
372 This is used for things like Rmail `g' that want to force an
373 update which can wait for the next redisplay."
374 (let* ((now (current-time))
375 (time (current-time-string now
))
376 (load (if (null display-time-load-average
)
379 ;; Do not show values less than
380 ;; `display-time-load-average-threshold'.
381 (if (> (* display-time-load-average-threshold
100)
382 (nth display-time-load-average
(load-average)))
384 ;; The load average number is mysterious, so
385 ;; provide some help.
386 (let ((str (format " %03d"
387 (nth display-time-load-average
390 (concat (substring str
0 -
2) "." (substring str -
2))
391 'local-map
(make-mode-line-mouse-map
392 'mouse-2
'display-time-next-load-average
)
393 'mouse-face
'mode-line-highlight
395 "System load average for past "
396 (if (= 0 display-time-load-average
)
398 (if (= 1 display-time-load-average
)
401 "; mouse-2: next"))))
403 (mail-spool-file (or display-time-mail-file
405 (concat rmail-spool-directory
407 (mail (or (and display-time-mail-function
408 (funcall display-time-mail-function
))
409 (and display-time-mail-directory
410 (display-time-mail-check-directory))
411 (and (stringp mail-spool-file
)
412 (or (null display-time-server-down-time
)
413 ;; If have been down for 20 min, try again.
414 (> (- (nth 1 now
) display-time-server-down-time
)
416 (and (< (nth 1 now
) display-time-server-down-time
)
418 display-time-server-down-time
)
420 (let ((start-time (current-time)))
422 (display-time-file-nonempty-p mail-spool-file
)
423 (if (> (- (nth 1 (current-time))
426 ;; Record that mail file is not accessible.
427 (setq display-time-server-down-time
428 (nth 1 (current-time)))
429 ;; Record that mail file is accessible.
430 (setq display-time-server-down-time nil
)))))))
431 (24-hours (substring time
11 13))
432 (hour (string-to-number 24-hours
))
433 (12-hours (int-to-string (1+ (%
(+ hour
11) 12))))
434 (am-pm (if (>= hour
12) "pm" "am"))
435 (minutes (substring time
14 16))
436 (seconds (substring time
17 19))
437 (time-zone (car (cdr (current-time-zone now
))))
438 (day (substring time
8 10))
439 (year (substring time
20 24))
440 (monthname (substring time
4 7))
445 '(("Jan" .
"1") ("Feb" .
"2") ("Mar" .
"3") ("Apr" .
"4")
446 ("May" .
"5") ("Jun" .
"6") ("Jul" .
"7") ("Aug" .
"8")
447 ("Sep" .
"9") ("Oct" .
"10") ("Nov" .
"11") ("Dec" .
"12")))))
448 (dayname (substring time
0 3)))
449 (setq display-time-string
450 (mapconcat 'eval display-time-string-forms
""))
451 ;; This is inside the let binding, but we are not going to document
452 ;; what variables are available.
453 (run-hooks 'display-time-hook
))
454 (force-mode-line-update))
456 (defun display-time-file-nonempty-p (file)
457 (let ((remote-file-name-inhibit-cache (- display-time-interval
5)))
458 (and (file-exists-p file
)
459 (< 0 (nth 7 (file-attributes (file-chase-links file
)))))))
462 (define-minor-mode display-time-mode
463 "Toggle display of time, load level, and mail flag in mode lines.
464 With a numeric arg, enable this display if arg is positive.
466 When this display is enabled, it updates automatically every minute
467 \(you can control the number of seconds between updates by
468 customizing `display-time-interval').
469 If `display-time-day-and-date' is non-nil, the current day and date
470 are displayed as well.
471 This runs the normal hook `display-time-hook' after each update."
472 :global t
:group
'display-time
473 (and display-time-timer
(cancel-timer display-time-timer
))
474 (setq display-time-timer nil
)
475 (setq display-time-string
"")
476 (or global-mode-string
(setq global-mode-string
'("")))
477 (setq display-time-load-average display-time-default-load-average
)
478 (if display-time-mode
480 (or (memq 'display-time-string global-mode-string
)
481 (setq global-mode-string
482 (append global-mode-string
'(display-time-string))))
483 ;; Set up the time timer.
484 (setq display-time-timer
485 (run-at-time t display-time-interval
486 'display-time-event-handler
))
487 ;; Make the time appear right away.
488 (display-time-update)
489 ;; When you get new mail, clear "Mail" from the mode line.
490 (add-hook 'rmail-after-get-new-mail-hook
491 'display-time-event-handler
))
492 (remove-hook 'rmail-after-get-new-mail-hook
493 'display-time-event-handler
)))
496 (define-derived-mode display-time-world-mode nil
"World clock"
497 "Major mode for buffer that displays times in various time zones.
498 See `display-time-world'."
499 (setq show-trailing-whitespace nil
))
501 (defun display-time-world-display (alist)
502 "Replace current buffer text with times in various zones, based on ALIST."
503 (let ((inhibit-read-only t
)
504 (buffer-undo-list t
))
511 (let* ((label (cadr zone
))
512 (width (string-width label
)))
513 (set-time-zone-rule (car zone
))
515 (format-time-string display-time-world-time-format
))
517 (when (> width max-width
)
518 (setq max-width width
))))
519 (set-time-zone-rule nil
))
520 (setq fmt
(concat "%-" (int-to-string max-width
) "s %s\n"))
521 (dolist (timedata (nreverse result
))
522 (insert (format fmt
(car timedata
) (cdr timedata
)))))
526 (defun display-time-world ()
527 "Enable updating display of times in various time zones.
528 `display-time-world-list' specifies the zones.
529 To turn off the world time display, go to that window and type `q'."
531 (when (and display-time-world-timer-enable
532 (not (get-buffer display-time-world-buffer-name
)))
533 (run-at-time t display-time-world-timer-second
'display-time-world-timer
))
534 (with-current-buffer (get-buffer-create display-time-world-buffer-name
)
535 (display-time-world-display display-time-world-list
))
536 (pop-to-buffer display-time-world-buffer-name
)
537 (fit-window-to-buffer)
538 (display-time-world-mode))
540 (defun display-time-world-timer ()
541 (if (get-buffer display-time-world-buffer-name
)
542 (with-current-buffer (get-buffer display-time-world-buffer-name
)
543 (display-time-world-display display-time-world-list
))
545 (let ((list timer-list
))
547 (let ((elt (pop list
)))
548 (when (equal (symbol-name (aref elt
5)) "display-time-world-timer")
549 (cancel-timer elt
)))))))
552 (defun emacs-uptime (&optional format
)
553 "Return a string giving the uptime of this instance of Emacs.
554 FORMAT is a string to format the result, using `format-seconds'.
555 For example, the Unix uptime command format is \"%D, %z%2h:%.2m\"."
558 (format-seconds (or format
"%Y, %D, %H, %M, %z%S")
560 (time-subtract (current-time) before-init-time
)))))
561 (if (called-interactively-p 'interactive
)
566 (defun emacs-init-time ()
567 "Return a string giving the duration of the Emacs initialization."
570 (format "%.1f seconds"
572 (time-subtract after-init-time before-init-time
)))))
573 (if (called-interactively-p 'interactive
)
579 ;;; time.el ends here