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 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 display-time-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 specifying time zones and places for `display-time-world'.
123 Each element has the form (TIMEZONE LABEL).
124 TIMEZONE should be a valid argument for `set-time-zone-rule'.
125 LABEL is a string to display to label that zone's time."
127 :type
'(repeat (list string string
))
130 (defcustom display-time-world-time-format
"%A %m %B %R %Z"
131 "Format of the time displayed, see `format-time-string'."
136 (defcustom display-time-world-buffer-name
"*wclock*"
137 "Name of the wclock buffer."
142 (defcustom display-time-world-timer-enable t
143 "If non-nil, a timer will update the world clock."
148 (defcustom display-time-world-timer-second
60
149 "Interval in seconds for updating the world clock."
154 (defvar display-time-world-mode-map
155 (let ((map (make-sparse-keymap)))
156 (define-key map
"q" 'kill-this-buffer
)
158 "Keymap of Display Time World mode")
161 (defun display-time ()
162 "Enable display of time, load level, and mail flag in mode lines.
163 This display updates automatically every minute.
164 If `display-time-day-and-date' is non-nil, the current day and date
165 are displayed as well.
166 This runs the normal hook `display-time-hook' after each update."
168 (display-time-mode 1))
170 ;; This business used to be simpler when all mode lines had the same
171 ;; face and the image could just be pbm. Now we try to rely on an xpm
172 ;; image with a transparent background. Otherwise, set the background
175 (defcustom display-time-mail-face nil
176 "Face to use for `display-time-mail-string'.
177 If `display-time-use-mail-icon' is non-nil, the image's
178 background color is the background of this face. Set this to
179 make the mail indicator stand out on a color display."
180 :group
'mode-line-faces
183 :type
'(choice (const :tag
"None" nil
) face
))
185 (defvar display-time-mail-icon
186 (find-image '((:type xpm
:file
"letter.xpm" :ascent center
)
187 (:type pbm
:file
"letter.pbm" :ascent center
)))
188 "Image specification to offer as the mail indicator on a graphic display.
189 See `display-time-use-mail-icon' and `display-time-mail-face'.")
191 ;; Fixme: Default to icon on graphical display?
192 (defcustom display-time-use-mail-icon nil
193 "Non-nil means use an icon as mail indicator on a graphic display.
194 Otherwise use `display-time-mail-string'. The icon may consume less
195 of the mode line. It is specified by `display-time-mail-icon'."
199 ;; Fixme: maybe default to the character if we can display Unicode.
200 (defcustom display-time-mail-string
"Mail"
201 "String to use as the mail indicator in `display-time-string-forms'.
202 This can use the Unicode letter character if you can display it."
205 :type
'(choice (const "Mail")
206 ;; Use :tag here because the Lucid menu won't display
208 (const :tag
"Unicode letter character" "✉")
211 (defcustom display-time-format nil
212 "*String specifying format for displaying the time in the mode line.
213 See the function `format-time-string' for an explanation of
214 how to write this string. If this is nil, the defaults
215 depend on `display-time-day-and-date' and `display-time-24hr-format'."
216 :type
'(choice (const :tag
"Default" nil
)
218 :group
'display-time
)
220 (defcustom display-time-string-forms
221 '((if (and (not display-time-format
) display-time-day-and-date
)
222 (format-time-string "%a %b %e " now
)
225 (format-time-string (or display-time-format
226 (if display-time-24hr-format
"%H:%M" "%-I:%M%p"))
228 'help-echo
(format-time-string "%a %b %e, %Y" now
))
231 ;; Build the string every time to act on customization.
232 ;; :set-after doesn't help for `customize-option'. I think it
237 display-time-mail-string
238 'display
`(when (and display-time-use-mail-icon
240 ,@display-time-mail-icon
241 ,@(if (and display-time-mail-face
242 (memq (plist-get (cdr display-time-mail-icon
)
245 (let ((bg (face-attribute display-time-mail-face
248 (list :background bg
)))))
249 'face display-time-mail-face
250 'help-echo
"You have new mail; mouse-2: Read mail"
251 'mouse-face
'mode-line-highlight
252 'local-map
(make-mode-line-mouse-map 'mouse-2
255 "*List of expressions governing display of the time in the mode line.
256 For most purposes, you can control the time format using `display-time-format'
257 which is a more standard interface.
259 This expression is a list of expressions that can involve the keywords
260 `load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
261 `seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
262 and `time-zone' all alphabetic strings, and `mail' a true/nil value.
264 For example, the form
266 '((substring year -2) \"/\" month \"/\" day
267 \" \" 24-hours \":\" minutes \":\" seconds
268 (if time-zone \" (\") time-zone (if time-zone \")\")
269 (if mail \" Mail\" \"\"))
271 would give mode line times like `94/12/30 21:07:48 (UTC)'."
273 :group
'display-time
)
275 (defun display-time-event-handler ()
276 (display-time-update)
277 ;; Do redisplay right now, if no input pending.
279 (let* ((current (current-time))
280 (timer display-time-timer
)
281 ;; Compute the time when this timer will run again, next.
282 (next-time (timer-relative-time
283 (list (aref timer
1) (aref timer
2) (aref timer
3))
284 (* 5 (aref timer
4)) 0)))
285 ;; If the activation time is far in the past,
286 ;; skip executions until we reach a time in the future.
287 ;; This avoids a long pause if Emacs has been suspended for hours.
288 (or (> (nth 0 next-time
) (nth 0 current
))
289 (and (= (nth 0 next-time
) (nth 0 current
))
290 (> (nth 1 next-time
) (nth 1 current
)))
291 (and (= (nth 0 next-time
) (nth 0 current
))
292 (= (nth 1 next-time
) (nth 1 current
))
293 (> (nth 2 next-time
) (nth 2 current
)))
295 (timer-set-time timer
(timer-next-integral-multiple-of-time
296 current display-time-interval
)
297 display-time-interval
)
298 (timer-activate timer
)))))
300 (defun display-time-next-load-average ()
302 (if (= 3 (setq display-time-load-average
(1+ display-time-load-average
)))
303 (setq display-time-load-average
0))
304 (display-time-update)
307 (defun display-time-mail-check-directory ()
308 (let ((mail-files (directory-files display-time-mail-directory t
))
310 (while (and mail-files
(= size
0))
311 ;; Count size of regular files only.
312 (setq size
(+ size
(or (and (file-regular-p (car mail-files
))
313 (nth 7 (file-attributes (car mail-files
))))
315 (setq mail-files
(cdr mail-files
)))
320 (defun display-time-update ()
321 "Update the display-time info for the mode line.
322 However, don't redisplay right now.
324 This is used for things like Rmail `g' that want to force an
325 update which can wait for the next redisplay."
326 (let* ((now (current-time))
327 (time (current-time-string now
))
328 (load (if (null display-time-load-average
)
331 ;; Do not show values less than
332 ;; `display-time-load-average-threshold'.
333 (if (> (* display-time-load-average-threshold
100)
334 (nth display-time-load-average
(load-average)))
336 ;; The load average number is mysterious, so
337 ;; provide some help.
338 (let ((str (format " %03d"
339 (nth display-time-load-average
342 (concat (substring str
0 -
2) "." (substring str -
2))
343 'local-map
(make-mode-line-mouse-map
344 'mouse-2
'display-time-next-load-average
)
345 'mouse-face
'mode-line-highlight
347 "System load average for past "
348 (if (= 0 display-time-load-average
)
350 (if (= 1 display-time-load-average
)
353 "; mouse-2: next"))))
355 (mail-spool-file (or display-time-mail-file
357 (concat rmail-spool-directory
359 (mail (or (and display-time-mail-function
360 (funcall display-time-mail-function
))
361 (and display-time-mail-directory
362 (display-time-mail-check-directory))
363 (and (stringp mail-spool-file
)
364 (or (null display-time-server-down-time
)
365 ;; If have been down for 20 min, try again.
366 (> (- (nth 1 now
) display-time-server-down-time
)
368 (and (< (nth 1 now
) display-time-server-down-time
)
370 display-time-server-down-time
)
372 (let ((start-time (current-time)))
374 (display-time-file-nonempty-p mail-spool-file
)
375 (if (> (- (nth 1 (current-time))
378 ;; Record that mail file is not accessible.
379 (setq display-time-server-down-time
380 (nth 1 (current-time)))
381 ;; Record that mail file is accessible.
382 (setq display-time-server-down-time nil
)))))))
383 (24-hours (substring time
11 13))
384 (hour (string-to-number 24-hours
))
385 (12-hours (int-to-string (1+ (%
(+ hour
11) 12))))
386 (am-pm (if (>= hour
12) "pm" "am"))
387 (minutes (substring time
14 16))
388 (seconds (substring time
17 19))
389 (time-zone (car (cdr (current-time-zone now
))))
390 (day (substring time
8 10))
391 (year (substring time
20 24))
392 (monthname (substring time
4 7))
397 '(("Jan" .
"1") ("Feb" .
"2") ("Mar" .
"3") ("Apr" .
"4")
398 ("May" .
"5") ("Jun" .
"6") ("Jul" .
"7") ("Aug" .
"8")
399 ("Sep" .
"9") ("Oct" .
"10") ("Nov" .
"11") ("Dec" .
"12")))))
400 (dayname (substring time
0 3)))
401 (setq display-time-string
402 (mapconcat 'eval display-time-string-forms
""))
403 ;; This is inside the let binding, but we are not going to document
404 ;; what variables are available.
405 (run-hooks 'display-time-hook
))
406 (force-mode-line-update))
408 (defun display-time-file-nonempty-p (file)
409 (and (file-exists-p file
)
410 (< 0 (nth 7 (file-attributes (file-chase-links file
))))))
413 (define-minor-mode display-time-mode
414 "Toggle display of time, load level, and mail flag in mode lines.
415 With a numeric arg, enable this display if arg is positive.
417 When this display is enabled, it updates automatically every minute.
418 If `display-time-day-and-date' is non-nil, the current day and date
419 are displayed as well.
420 This runs the normal hook `display-time-hook' after each update."
421 :global t
:group
'display-time
422 (and display-time-timer
(cancel-timer display-time-timer
))
423 (setq display-time-timer nil
)
424 (setq display-time-string
"")
425 (or global-mode-string
(setq global-mode-string
'("")))
426 (setq display-time-load-average display-time-default-load-average
)
427 (if display-time-mode
429 (or (memq 'display-time-string global-mode-string
)
430 (setq global-mode-string
431 (append global-mode-string
'(display-time-string))))
432 ;; Set up the time timer.
433 (setq display-time-timer
434 (run-at-time t display-time-interval
435 'display-time-event-handler
))
436 ;; Make the time appear right away.
437 (display-time-update)
438 ;; When you get new mail, clear "Mail" from the mode line.
439 (add-hook 'rmail-after-get-new-mail-hook
440 'display-time-event-handler
))
441 (remove-hook 'rmail-after-get-new-mail-hook
442 'display-time-event-handler
)))
445 (defun display-time-world-mode ()
446 "Major mode for buffer that displays times in various time zones.
447 See `display-time-world'."
449 (kill-all-local-variables)
451 major-mode
'display-time-world-mode
452 mode-name
"World clock")
453 (use-local-map display-time-world-mode-map
))
455 (defun display-time-world-display (alist)
456 "Replace current buffer text with times in various zones, based on ALIST."
457 (let ((inhibit-read-only t
)
458 (buffer-undo-list t
))
464 (let* ((label (cadr zone
))
465 (width (string-width label
)))
466 (set-time-zone-rule (car zone
))
471 (format-time-string display-time-world-time-format
))))
472 (when (> width max-width
)
473 (setq max-width width
))))
474 (set-time-zone-rule nil
))
477 (make-string (1+ (- max-width
(pop result
))) ?\s
)
479 (delete-backward-char 1)))
482 (defun display-time-world ()
483 "Enable updating display of times in various time zones.
484 `display-time-world-list' specifies the zones.
485 To turn off the world time display, go to that window and type `q'."
487 (when (and display-time-world-timer-enable
488 (not (get-buffer display-time-world-buffer-name
)))
489 (run-at-time t display-time-world-timer-second
'display-time-world-timer
))
490 (with-current-buffer (get-buffer-create display-time-world-buffer-name
)
491 (display-time-world-display display-time-world-list
))
492 (pop-to-buffer display-time-world-buffer-name
)
493 (fit-window-to-buffer)
494 (display-time-world-mode))
496 (defun display-time-world-timer ()
497 (if (get-buffer display-time-world-buffer-name
)
498 (with-current-buffer (get-buffer display-time-world-buffer-name
)
499 (display-time-world-display display-time-world-list
))
501 (let ((list timer-list
))
503 (let ((elt (pop list
)))
504 (when (equal (symbol-name (aref elt
5)) "display-time-world-timer")
505 (cancel-timer elt
)))))))
509 ;;; arch-tag: b9c1623f-b5cb-48e4-b650-482a4d23c5a6
510 ;;; time.el ends here