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."
38 (defcustom display-time-mode nil
39 "Toggle display of time, load level, and mail flag in mode lines.
40 Setting this variable directly does not take effect;
41 use either \\[customize] or the function `display-time-mode'."
42 :set
(lambda (symbol value
)
43 (display-time-mode (or value
0)))
44 :initialize
'custom-initialize-default
51 (defcustom display-time-mail-file nil
52 "*File name of mail inbox file, for indicating existence of new mail.
53 Non-nil and not a string means don't check for mail. nil means use
54 default, which is system-dependent, and is the same as used by Rmail."
55 :type
'(choice (const :tag
"Default" nil
)
60 (defcustom display-time-day-and-date nil
"\
61 *Non-nil means \\[display-time] should display day and date as well as time."
65 (defvar display-time-timer nil
)
67 (defcustom display-time-interval
60
68 "*Seconds between updates of time in the mode line."
72 (defcustom display-time-24hr-format nil
73 "*Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23.
74 Nil means 1 <= hh <= 12, and an AM/PM suffix is used."
78 (defvar display-time-string nil
)
80 (defcustom display-time-hook nil
81 "*List of functions to be called when the time is updated on the mode line."
85 (defvar display-time-server-down-time nil
86 "Time when mail file's file system was recorded to be down.
87 If that file system seems to be up, the value is nil.")
90 (defun display-time ()
91 "Enable display of time, load level, and mail flag in mode lines.
92 This display updates automatically every minute.
93 If `display-time-day-and-date' is non-nil, the current day and date
94 are displayed as well.
95 This runs the normal hook `display-time-hook' after each update."
97 (display-time-mode 1))
100 (defun display-time-mode (arg)
101 "Toggle display of time, load level, and mail flag in mode lines.
102 With a numeric arg, enable this display if arg is positive.
104 When this display is enabled, it updates automatically every minute.
105 If `display-time-day-and-date' is non-nil, the current day and date
106 are displayed as well.
107 This runs the normal hook `display-time-hook' after each update."
109 (let ((on (if (null arg
)
110 (not display-time-timer
)
111 (> (prefix-numeric-value arg
) 0))))
112 (setq display-time-mode on
)
113 (and display-time-timer
(cancel-timer display-time-timer
))
114 (setq display-time-timer nil
)
115 (setq display-time-string
"")
116 (or global-mode-string
(setq global-mode-string
'("")))
119 (or (memq 'display-time-string global-mode-string
)
120 (setq global-mode-string
121 (append global-mode-string
'(display-time-string))))
122 ;; Set up the time timer.
123 (setq display-time-timer
124 (run-at-time t display-time-interval
125 'display-time-event-handler
))
126 ;; Make the time appear right away.
127 (display-time-update)
128 ;; When you get new mail, clear "Mail" from the mode line.
129 (add-hook 'rmail-after-get-new-mail-hook
130 'display-time-event-handler
))
131 (remove-hook 'rmail-after-get-new-mail-hook
132 'display-time-event-handler
))))
135 (defcustom display-time-format nil
136 "*A string specifying the format for displaying the time in the mode line.
137 See the function `format-time-string' for an explanation of
138 how to write this string. If this is nil, the defaults
139 depend on `display-time-day-and-date' and `display-time-24hr-format'."
140 :type
'(choice (const :tag
"Default" nil
)
142 :group
'display-time
)
144 (defcustom display-time-string-forms
145 '((if (and (not display-time-format
) display-time-day-and-date
)
146 (format-time-string "%a %b %e " now
)
148 (format-time-string (or display-time-format
149 (if display-time-24hr-format
"%H:%M" "%-I:%M%p"))
152 (if mail
" Mail" ""))
153 "*A list of expressions governing display of the time in the mode line.
154 For most purposes, you can control the time format using `display-time-format'
155 which is a more standard interface.
157 This expression is a list of expressions that can involve the keywords
158 `load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
159 `seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
160 and `time-zone' all alphabetic strings, and `mail' a true/nil value.
162 For example, the form
164 '((substring year -2) \"/\" month \"/\" day
165 \" \" 24-hours \":\" minutes \":\" seconds
166 (if time-zone \" (\") time-zone (if time-zone \")\")
167 (if mail \" Mail\" \"\"))
169 would give mode line times like `94/12/30 21:07:48 (UTC)'."
171 :group
'display-time
)
173 (defun display-time-event-handler ()
174 (display-time-update)
175 ;; Do redisplay right now, if no input pending.
177 (let* ((current (current-time))
178 (timer display-time-timer
)
179 ;; Compute the time when this timer will run again, next.
180 (next-time (timer-relative-time
181 (list (aref timer
1) (aref timer
2) (aref timer
3))
182 (* 5 (aref timer
4)) 0)))
183 ;; If the activation time is far in the past,
184 ;; skip executions until we reach a time in the future.
185 ;; This avoids a long pause if Emacs has been suspended for hours.
186 (or (> (nth 0 next-time
) (nth 0 current
))
187 (and (= (nth 0 next-time
) (nth 0 current
))
188 (> (nth 1 next-time
) (nth 1 current
)))
189 (and (= (nth 0 next-time
) (nth 0 current
))
190 (= (nth 1 next-time
) (nth 1 current
))
191 (> (nth 2 next-time
) (nth 2 current
)))
193 (timer-set-time timer
(timer-next-integral-multiple-of-time
194 current display-time-interval
)
195 display-time-interval
)
196 (timer-activate timer
)))))
198 ;; Update the display-time info for the mode line
199 ;; but don't redisplay right now. This is used for
200 ;; things like Rmail `g' that want to force an update
201 ;; which can wait for the next redisplay.
202 (defun display-time-update ()
203 (let* ((now (current-time))
204 (time (current-time-string now
))
205 (load (condition-case ()
206 (if (zerop (car (load-average))) ""
207 (let ((str (format " %03d" (car (load-average)))))
208 (concat (substring str
0 -
2) "." (substring str -
2))))
210 (mail-spool-file (or display-time-mail-file
212 (concat rmail-spool-directory
214 (mail (and (stringp mail-spool-file
)
215 (or (null display-time-server-down-time
)
216 ;; If have been down for 20 min, try again.
217 (> (- (nth 1 now
) display-time-server-down-time
)
219 (and (< (nth 1 now
) display-time-server-down-time
)
220 (> (- (nth 1 now
) display-time-server-down-time
)
222 (let ((start-time (current-time)))
224 (display-time-file-nonempty-p mail-spool-file
)
225 (if (> (- (nth 1 (current-time)) (nth 1 start-time
))
227 ;; Record that mail file is not accessible.
228 (setq display-time-server-down-time
229 (nth 1 (current-time)))
230 ;; Record that mail file is accessible.
231 (setq display-time-server-down-time nil
))))))
232 (24-hours (substring time
11 13))
233 (hour (string-to-int 24-hours
))
234 (12-hours (int-to-string (1+ (%
(+ hour
11) 12))))
235 (am-pm (if (>= hour
12) "pm" "am"))
236 (minutes (substring time
14 16))
237 (seconds (substring time
17 19))
238 (time-zone (car (cdr (current-time-zone now
))))
239 (day (substring time
8 10))
240 (year (substring time
20 24))
241 (monthname (substring time
4 7))
246 '(("Jan" .
"1") ("Feb" .
"2") ("Mar" .
"3") ("Apr" .
"4")
247 ("May" .
"5") ("Jun" .
"6") ("Jul" .
"7") ("Aug" .
"8")
248 ("Sep" .
"9") ("Oct" .
"10") ("Nov" .
"11") ("Dec" .
"12")))))
249 (dayname (substring time
0 3)))
250 (setq display-time-string
251 (mapconcat 'eval display-time-string-forms
""))
252 ;; This is inside the let binding, but we are not going to document
253 ;; what variables are available.
254 (run-hooks 'display-time-hook
))
255 (force-mode-line-update))
257 (defun display-time-file-nonempty-p (file)
258 (and (file-exists-p file
)
259 (< 0 (nth 7 (file-attributes (file-chase-links file
))))))
261 (if display-time-mode
262 (display-time-mode t
))
266 ;;; time.el ends here