1 ;;; time.el --- display time and load in mode line of Emacs.
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 1996 Free Software Foundation, Inc.
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; Facilities to display current time/date and a new-mail indicator
27 ;; in the Emacs mode line. The single entry point is `display-time'.
31 (defgroup display-time nil
32 "Display time and load in mode line of Emacs."
37 (defcustom display-time-mode nil
38 "Toggle display of time, load level, and mail flag in mode lines.
39 You must modify via \\[customize] for this variable to have an effect."
40 :set
(lambda (symbol value
)
41 (display-time-mode (or value
0)))
42 :initialize
'custom-initialize-default
49 (defcustom display-time-mail-file nil
50 "*File name of mail inbox file, for indicating existence of new mail.
51 Non-nil and not a string means don't check for mail. nil means use
52 default, which is system-dependent, and is the same as used by Rmail."
53 :type
'(choice (const :tag
"Default" nil
)
58 (defcustom display-time-day-and-date nil
"\
59 *Non-nil means \\[display-time] should display day and date as well as time."
63 (defvar display-time-timer nil
)
65 (defcustom display-time-interval
60
66 "*Seconds between updates of time in the mode line."
70 (defcustom display-time-24hr-format nil
71 "*Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23.
72 Nil means 1 <= hh <= 12, and an AM/PM suffix is used."
76 (defvar display-time-string nil
)
78 (defcustom display-time-hook nil
79 "*List of functions to be called when the time is updated on the mode line."
83 (defvar display-time-server-down-time nil
84 "Time when mail file's file system was recorded to be down.
85 If that file system seems to be up, the value is nil.")
88 (defun display-time ()
89 "Enable display of time, load level, and mail flag in mode lines.
90 This display updates automatically every minute.
91 If `display-time-day-and-date' is non-nil, the current day and date
92 are displayed as well.
93 This runs the normal hook `display-time-hook' after each update."
95 (display-time-mode 1))
98 (defun display-time-mode (arg)
99 "Toggle display of time, load level, and mail flag in mode lines.
100 With a numeric arg, enable this display if arg is positive.
102 When this display is enabled, it updates automatically every minute.
103 If `display-time-day-and-date' is non-nil, the current day and date
104 are displayed as well.
105 This runs the normal hook `display-time-hook' after each update."
107 (let ((on (if (null arg
)
108 (not display-time-timer
)
109 (> (prefix-numeric-value arg
) 0))))
110 (setq display-time-mode on
)
111 (and display-time-timer
(cancel-timer display-time-timer
))
112 (setq display-time-timer nil
)
113 (setq display-time-string
"")
114 (or global-mode-string
(setq global-mode-string
'("")))
117 (or (memq 'display-time-string global-mode-string
)
118 (setq global-mode-string
119 (append global-mode-string
'(display-time-string))))
120 ;; Set up the time timer.
121 (setq display-time-timer
122 (run-at-time t display-time-interval
123 'display-time-event-handler
))
124 ;; Make the time appear right away.
125 (display-time-update)
126 ;; When you get new mail, clear "Mail" from the mode line.
127 (add-hook 'rmail-after-get-new-mail-hook
128 'display-time-event-handler
))
129 (remove-hook 'rmail-after-get-new-mail-hook
130 'display-time-event-handler
))))
133 (defcustom display-time-format nil
134 "*A string specifying the format for displaying the time in the mode line.
135 See the function `format-time-string' for an explanation of
136 how to write this string. If this is nil, the defaults
137 depend on `display-time-day-and-date' and `display-time-24hr-format'."
138 :type
'(choice (const :tag
"Default" nil
)
140 :group
'display-time
)
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
)
146 (format-time-string (or display-time-format
147 (if display-time-24hr-format
"%H:%M" "%-I:%M%p"))
150 (if mail
" Mail" ""))
151 "*A list of expressions governing display of the time in the mode line.
152 For most purposes, you can control the time format using `display-time-format'
153 which is a more standard interface.
155 This expression is a list of expressions that can involve the keywords
156 `load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
157 `seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
158 and `time-zone' all alphabetic strings, and `mail' a true/nil value.
160 For example, the form
162 '((substring year -2) \"/\" month \"/\" day
163 \" \" 24-hours \":\" minutes \":\" seconds
164 (if time-zone \" (\") time-zone (if time-zone \")\")
165 (if mail \" Mail\" \"\"))
167 would give mode line times like `94/12/30 21:07:48 (UTC)'."
169 :group
'display-time
)
171 (defun display-time-event-handler ()
172 (display-time-update)
173 ;; Do redisplay right now, if no input pending.
175 (let* ((current (current-time))
176 (timer display-time-timer
)
177 ;; Compute the time when this timer will run again, next.
178 (next-time (timer-relative-time
179 (list (aref timer
1) (aref timer
2) (aref timer
3))
180 (* 5 (aref timer
4)) 0)))
181 ;; If the activation time is far in the past,
182 ;; skip executions until we reach a time in the future.
183 ;; This avoids a long pause if Emacs has been suspended for hours.
184 (or (> (nth 0 next-time
) (nth 0 current
))
185 (and (= (nth 0 next-time
) (nth 0 current
))
186 (> (nth 1 next-time
) (nth 1 current
)))
187 (and (= (nth 0 next-time
) (nth 0 current
))
188 (= (nth 1 next-time
) (nth 1 current
))
189 (> (nth 2 next-time
) (nth 2 current
)))
191 (timer-set-time timer
(timer-next-integral-multiple-of-time
192 current display-time-interval
)
193 display-time-interval
)
194 (timer-activate timer
)))))
196 ;; Update the display-time info for the mode line
197 ;; but don't redisplay right now. This is used for
198 ;; things like Rmail `g' that want to force an update
199 ;; which can wait for the next redisplay.
200 (defun display-time-update ()
201 (let* ((now (current-time))
202 (time (current-time-string now
))
203 (load (condition-case ()
204 (if (zerop (car (load-average))) ""
205 (let ((str (format " %03d" (car (load-average)))))
206 (concat (substring str
0 -
2) "." (substring str -
2))))
208 (mail-spool-file (or display-time-mail-file
210 (concat rmail-spool-directory
212 (mail (and (stringp mail-spool-file
)
213 (or (null display-time-server-down-time
)
214 ;; If have been down for 20 min, try again.
215 (> (- (nth 1 (current-time))
216 display-time-server-down-time
)
218 (let ((start-time (current-time)))
220 (display-time-file-nonempty-p mail-spool-file
)
221 (if (> (- (nth 1 (current-time)) (nth 1 start-time
))
223 ;; Record that mail file is not accessible.
224 (setq display-time-server-down-time
225 (nth 1 (current-time)))
226 ;; Record that mail file is accessible.
227 (setq display-time-server-down-time nil
))))))
228 (24-hours (substring time
11 13))
229 (hour (string-to-int 24-hours
))
230 (12-hours (int-to-string (1+ (%
(+ hour
11) 12))))
231 (am-pm (if (>= hour
12) "pm" "am"))
232 (minutes (substring time
14 16))
233 (seconds (substring time
17 19))
234 (time-zone (car (cdr (current-time-zone now
))))
235 (day (substring time
8 10))
236 (year (substring time
20 24))
237 (monthname (substring time
4 7))
242 '(("Jan" .
"1") ("Feb" .
"2") ("Mar" .
"3") ("Apr" .
"4")
243 ("May" .
"5") ("Jun" .
"6") ("Jul" .
"7") ("Aug" .
"8")
244 ("Sep" .
"9") ("Oct" .
"10") ("Nov" .
"11") ("Dec" .
"12")))))
245 (dayname (substring time
0 3)))
246 (setq display-time-string
247 (mapconcat 'eval display-time-string-forms
""))
248 ;; This is inside the let binding, but we are not going to document
249 ;; what variables are available.
250 (run-hooks 'display-time-hook
))
251 (force-mode-line-update))
253 (defun display-time-file-nonempty-p (file)
254 (and (file-exists-p file
)
255 (< 0 (nth 7 (file-attributes (file-chase-links file
))))))
257 (if display-time-mode
258 (display-time-mode t
))
262 ;;; time.el ends here