1 ;;; time.el --- display time, load and mail indicator in mode line of Emacs -*-coding: utf-8 -*-
3 ;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1996, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008 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, 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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; Facilities to display current time/date and a new-mail indicator
28 ;; in the Emacs mode line. The entry point is `display-time'.
30 ;; Display time world in a buffer, the entry point is
31 ;; `display-time-world'.
35 (defgroup display-time nil
36 "Display time and load in mode line of Emacs."
41 (defcustom display-time-mail-file nil
42 "*File name of mail inbox file, for indicating existence of new mail.
43 Non-nil and not a string means don't check for mail; nil means use
44 default, which is system-dependent, and is the same as used by Rmail."
45 :type
'(choice (const :tag
"None" none
)
46 (const :tag
"Default" nil
)
50 (defcustom display-time-mail-directory nil
51 "*Name of mail inbox directory, for indicating existence of new mail.
52 Any nonempty regular file in the directory is regarded as newly arrived mail.
53 If nil, do not check a directory for arriving mail."
54 :type
'(choice (const :tag
"None" nil
)
55 (directory :format
"%v"))
58 (defcustom display-time-mail-function nil
59 "*Function to call, for indicating existence of new mail.
60 If nil, that means use the default method: check that the file
61 specified by `display-time-mail-file' is nonempty or that the
62 directory `display-time-mail-directory' contains nonempty files."
63 :type
'(choice (const :tag
"Default" nil
)
67 (defcustom display-time-default-load-average
0
68 "*Which load average value will be shown in the mode line.
69 Almost every system can provide values of load for past 1 minute, past 5 or
70 past 15 minutes. The default is to display 1 minute load average."
71 :type
'(choice (const :tag
"1 minute load" 0)
72 (const :tag
"5 minutes load" 1)
73 (const :tag
"15 minutes load" 2)
74 (const :tag
"None" nil
))
77 (defvar display-time-load-average nil
78 "Load average currently being shown in mode line.")
80 (defcustom display-time-load-average-threshold
0.1
81 "*Load-average values below this value won't be shown in the mode line."
86 (defcustom display-time-day-and-date nil
"\
87 *Non-nil means \\[display-time] should display day and date as well as time."
91 (defvar display-time-timer nil
)
93 (defcustom display-time-interval
60
94 "*Seconds between updates of time in the mode line."
98 (defcustom display-time-24hr-format nil
99 "*Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23.
100 A value of nil means 1 <= hh <= 12, and an AM/PM suffix is used."
102 :group
'display-time
)
104 (defvar display-time-string nil
)
106 (defcustom display-time-hook nil
107 "*List of functions to be called when the time is updated on the mode line."
109 :group
'display-time
)
111 (defvar display-time-server-down-time nil
112 "Time when mail file's file system was recorded to be down.
113 If that file system seems to be up, the value is nil.")
115 (defcustom zoneinfo-style-world-list
116 '(("America/Los_Angeles" "Seattle")
117 ("America/New_York" "New York")
118 ("Europe/London" "London")
119 ("Europe/Paris" "Paris")
120 ("Asia/Calcutta" "Bangalore")
121 ("Asia/Tokyo" "Tokyo"))
122 "Alist of zoneinfo-style time zones and places for `display-time-world'.
123 Each element has the form (TIMEZONE LABEL).
124 TIMEZONE should be a string of the form AREA/LOCATION, where AREA is
125 the name of a region -- a continent or ocean, and LOCATION is the name
126 of a specific location, e.g., a city, within that region.
127 LABEL is a string to display as the label of that TIMEZONE's time."
129 :type
'(repeat (list string string
))
132 (defcustom legacy-style-world-list
133 '(("PST8PDT" "Seattle")
134 ("EST5EDT" "New York")
137 ("IST-5:30" "Bangalore")
139 "Alist of traditional-style time zones and places for `display-time-world'.
140 Each element has the form (TIMEZONE LABEL).
141 TIMEZONE should be a string of the form:
143 std[+|-]offset[dst[offset][,date[/time],date[/time]]]
145 See the documentation of the TZ environment variable on your system,
146 for more details about the format of TIMEZONE.
147 LABEL is a string to display as the label of that TIMEZONE's time."
149 :type
'(repeat (list string string
))
152 (defcustom display-time-world-list
153 ;; Determine if zoneinfo style timezones are supported by testing that
154 ;; America/New York and Europe/London return different timezones.
156 (set-time-zone-rule "America/New York")
157 (setq nyt
(format-time-string "%z"))
158 (set-time-zone-rule "Europe/London")
159 (setq gmt
(format-time-string "%z"))
160 (set-time-zone-rule nil
)
161 (if (string-equal nyt gmt
)
162 legacy-style-world-list
163 zoneinfo-style-world-list
))
164 "Alist of time zones and places for `display-time-world' to display.
165 Each element has the form (TIMEZONE LABEL).
166 TIMEZONE should be in the format supported by `set-time-zone-rule' on
167 your system. See the documentation of `zoneinfo-style-world-list' and
168 \`legacy-style-world-list' for two widely used formats.
169 LABEL is a string to display as the label of that TIMEZONE's time."
171 :type
'(repeat (list string string
))
174 (defcustom display-time-world-time-format
"%A %d %B %R %Z"
175 "Format of the time displayed, see `format-time-string'."
180 (defcustom display-time-world-buffer-name
"*wclock*"
181 "Name of the wclock buffer."
186 (defcustom display-time-world-timer-enable t
187 "If non-nil, a timer will update the world clock."
192 (defcustom display-time-world-timer-second
60
193 "Interval in seconds for updating the world clock."
198 (defvar display-time-world-mode-map
199 (let ((map (make-sparse-keymap)))
200 (define-key map
"q" 'kill-this-buffer
)
202 "Keymap of Display Time World mode")
205 (defun display-time ()
206 "Enable display of time, load level, and mail flag in mode lines.
207 This display updates automatically every minute.
208 If `display-time-day-and-date' is non-nil, the current day and date
209 are displayed as well.
210 This runs the normal hook `display-time-hook' after each update."
212 (display-time-mode 1))
214 ;; This business used to be simpler when all mode lines had the same
215 ;; face and the image could just be pbm. Now we try to rely on an xpm
216 ;; image with a transparent background. Otherwise, set the background
219 (defcustom display-time-mail-face nil
220 "Face to use for `display-time-mail-string'.
221 If `display-time-use-mail-icon' is non-nil, the image's
222 background color is the background of this face. Set this to
223 make the mail indicator stand out on a color display."
224 :group
'mode-line-faces
227 :type
'(choice (const :tag
"None" nil
) face
))
229 (defvar display-time-mail-icon
230 (find-image '((:type xpm
:file
"letter.xpm" :ascent center
)
231 (:type pbm
:file
"letter.pbm" :ascent center
)))
232 "Image specification to offer as the mail indicator on a graphic display.
233 See `display-time-use-mail-icon' and `display-time-mail-face'.")
235 ;; Fixme: Default to icon on graphical display?
236 (defcustom display-time-use-mail-icon nil
237 "Non-nil means use an icon as mail indicator on a graphic display.
238 Otherwise use `display-time-mail-string'. The icon may consume less
239 of the mode line. It is specified by `display-time-mail-icon'."
243 ;; Fixme: maybe default to the character if we can display Unicode.
244 (defcustom display-time-mail-string
"Mail"
245 "String to use as the mail indicator in `display-time-string-forms'.
246 This can use the Unicode letter character if you can display it."
249 :type
'(choice (const "Mail")
250 ;; Use :tag here because the Lucid menu won't display
252 (const :tag
"Unicode letter character" "✉")
255 (defcustom display-time-format nil
256 "*String specifying format for displaying the time in the mode line.
257 See the function `format-time-string' for an explanation of
258 how to write this string. If this is nil, the defaults
259 depend on `display-time-day-and-date' and `display-time-24hr-format'."
260 :type
'(choice (const :tag
"Default" nil
)
262 :group
'display-time
)
264 (defcustom display-time-string-forms
265 '((if (and (not display-time-format
) display-time-day-and-date
)
266 (format-time-string "%a %b %e " now
)
269 (format-time-string (or display-time-format
270 (if display-time-24hr-format
"%H:%M" "%-I:%M%p"))
272 'help-echo
(format-time-string "%a %b %e, %Y" now
))
275 ;; Build the string every time to act on customization.
276 ;; :set-after doesn't help for `customize-option'. I think it
281 display-time-mail-string
282 'display
`(when (and display-time-use-mail-icon
284 ,@display-time-mail-icon
285 ,@(if (and display-time-mail-face
286 (memq (plist-get (cdr display-time-mail-icon
)
289 (let ((bg (face-attribute display-time-mail-face
292 (list :background bg
)))))
293 'face display-time-mail-face
294 'help-echo
"You have new mail; mouse-2: Read mail"
295 'mouse-face
'mode-line-highlight
296 'local-map
(make-mode-line-mouse-map 'mouse-2
299 "*List of expressions governing display of the time in the mode line.
300 For most purposes, you can control the time format using `display-time-format'
301 which is a more standard interface.
303 This expression is a list of expressions that can involve the keywords
304 `load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
305 `seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
306 and `time-zone' all alphabetic strings, and `mail' a true/nil value.
308 For example, the form
310 '((substring year -2) \"/\" month \"/\" day
311 \" \" 24-hours \":\" minutes \":\" seconds
312 (if time-zone \" (\") time-zone (if time-zone \")\")
313 (if mail \" Mail\" \"\"))
315 would give mode line times like `94/12/30 21:07:48 (UTC)'."
317 :group
'display-time
)
319 (defun display-time-event-handler ()
320 (display-time-update)
321 ;; Do redisplay right now, if no input pending.
323 (let* ((current (current-time))
324 (timer display-time-timer
)
325 ;; Compute the time when this timer will run again, next.
326 (next-time (timer-relative-time
327 (list (aref timer
1) (aref timer
2) (aref timer
3))
328 (* 5 (aref timer
4)) 0)))
329 ;; If the activation time is far in the past,
330 ;; skip executions until we reach a time in the future.
331 ;; This avoids a long pause if Emacs has been suspended for hours.
332 (or (> (nth 0 next-time
) (nth 0 current
))
333 (and (= (nth 0 next-time
) (nth 0 current
))
334 (> (nth 1 next-time
) (nth 1 current
)))
335 (and (= (nth 0 next-time
) (nth 0 current
))
336 (= (nth 1 next-time
) (nth 1 current
))
337 (> (nth 2 next-time
) (nth 2 current
)))
339 (timer-set-time timer
(timer-next-integral-multiple-of-time
340 current display-time-interval
)
341 display-time-interval
)
342 (timer-activate timer
)))))
344 (defun display-time-next-load-average ()
346 (if (= 3 (setq display-time-load-average
(1+ display-time-load-average
)))
347 (setq display-time-load-average
0))
348 (display-time-update)
351 (defun display-time-mail-check-directory ()
352 (let ((mail-files (directory-files display-time-mail-directory t
))
354 (while (and mail-files
(= size
0))
355 ;; Count size of regular files only.
356 (setq size
(+ size
(or (and (file-regular-p (car mail-files
))
357 (nth 7 (file-attributes (car mail-files
))))
359 (setq mail-files
(cdr mail-files
)))
364 (defun display-time-update ()
365 "Update the display-time info for the mode line.
366 However, don't redisplay right now.
368 This is used for things like Rmail `g' that want to force an
369 update which can wait for the next redisplay."
370 (let* ((now (current-time))
371 (time (current-time-string now
))
372 (load (if (null display-time-load-average
)
375 ;; Do not show values less than
376 ;; `display-time-load-average-threshold'.
377 (if (> (* display-time-load-average-threshold
100)
378 (nth display-time-load-average
(load-average)))
380 ;; The load average number is mysterious, so
381 ;; provide some help.
382 (let ((str (format " %03d"
383 (nth display-time-load-average
386 (concat (substring str
0 -
2) "." (substring str -
2))
387 'local-map
(make-mode-line-mouse-map
388 'mouse-2
'display-time-next-load-average
)
389 'mouse-face
'mode-line-highlight
391 "System load average for past "
392 (if (= 0 display-time-load-average
)
394 (if (= 1 display-time-load-average
)
397 "; mouse-2: next"))))
399 (mail-spool-file (or display-time-mail-file
401 (concat rmail-spool-directory
403 (mail (or (and display-time-mail-function
404 (funcall display-time-mail-function
))
405 (and display-time-mail-directory
406 (display-time-mail-check-directory))
407 (and (stringp mail-spool-file
)
408 (or (null display-time-server-down-time
)
409 ;; If have been down for 20 min, try again.
410 (> (- (nth 1 now
) display-time-server-down-time
)
412 (and (< (nth 1 now
) display-time-server-down-time
)
414 display-time-server-down-time
)
416 (let ((start-time (current-time)))
418 (display-time-file-nonempty-p mail-spool-file
)
419 (if (> (- (nth 1 (current-time))
422 ;; Record that mail file is not accessible.
423 (setq display-time-server-down-time
424 (nth 1 (current-time)))
425 ;; Record that mail file is accessible.
426 (setq display-time-server-down-time nil
)))))))
427 (24-hours (substring time
11 13))
428 (hour (string-to-number 24-hours
))
429 (12-hours (int-to-string (1+ (%
(+ hour
11) 12))))
430 (am-pm (if (>= hour
12) "pm" "am"))
431 (minutes (substring time
14 16))
432 (seconds (substring time
17 19))
433 (time-zone (car (cdr (current-time-zone now
))))
434 (day (substring time
8 10))
435 (year (substring time
20 24))
436 (monthname (substring time
4 7))
441 '(("Jan" .
"1") ("Feb" .
"2") ("Mar" .
"3") ("Apr" .
"4")
442 ("May" .
"5") ("Jun" .
"6") ("Jul" .
"7") ("Aug" .
"8")
443 ("Sep" .
"9") ("Oct" .
"10") ("Nov" .
"11") ("Dec" .
"12")))))
444 (dayname (substring time
0 3)))
445 (setq display-time-string
446 (mapconcat 'eval display-time-string-forms
""))
447 ;; This is inside the let binding, but we are not going to document
448 ;; what variables are available.
449 (run-hooks 'display-time-hook
))
450 (force-mode-line-update))
452 (defun display-time-file-nonempty-p (file)
453 (and (file-exists-p file
)
454 (< 0 (nth 7 (file-attributes (file-chase-links file
))))))
457 (define-minor-mode display-time-mode
458 "Toggle display of time, load level, and mail flag in mode lines.
459 With a numeric arg, enable this display if arg is positive.
461 When this display is enabled, it updates automatically every minute.
462 If `display-time-day-and-date' is non-nil, the current day and date
463 are displayed as well.
464 This runs the normal hook `display-time-hook' after each update."
465 :global t
:group
'display-time
466 (and display-time-timer
(cancel-timer display-time-timer
))
467 (setq display-time-timer nil
)
468 (setq display-time-string
"")
469 (or global-mode-string
(setq global-mode-string
'("")))
470 (setq display-time-load-average display-time-default-load-average
)
471 (if display-time-mode
473 (or (memq 'display-time-string global-mode-string
)
474 (setq global-mode-string
475 (append global-mode-string
'(display-time-string))))
476 ;; Set up the time timer.
477 (setq display-time-timer
478 (run-at-time t display-time-interval
479 'display-time-event-handler
))
480 ;; Make the time appear right away.
481 (display-time-update)
482 ;; When you get new mail, clear "Mail" from the mode line.
483 (add-hook 'rmail-after-get-new-mail-hook
484 'display-time-event-handler
))
485 (remove-hook 'rmail-after-get-new-mail-hook
486 'display-time-event-handler
)))
489 (defun display-time-world-mode ()
490 "Major mode for buffer that displays times in various time zones.
491 See `display-time-world'."
493 (kill-all-local-variables)
495 major-mode
'display-time-world-mode
496 mode-name
"World clock")
497 (use-local-map display-time-world-mode-map
))
499 (defun display-time-world-display (alist)
500 "Replace current buffer text with times in various zones, based on ALIST."
501 (let ((inhibit-read-only t
)
502 (buffer-undo-list t
))
508 (let* ((label (cadr zone
))
509 (width (string-width label
)))
510 (set-time-zone-rule (car zone
))
515 (format-time-string display-time-world-time-format
))))
516 (when (> width max-width
)
517 (setq max-width width
))))
518 (set-time-zone-rule nil
))
521 (make-string (1+ (- max-width
(pop result
))) ?\s
)
523 (delete-backward-char 1)))
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
)))))
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
)))))
579 ;;; arch-tag: b9c1623f-b5cb-48e4-b650-482a4d23c5a6
580 ;;; time.el ends here