Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; time.el --- display time, load and mail indicator in mode line of Emacs |
c2acf685 | 2 | |
cc351ed4 | 3 | ;; Copyright (C) 1985, 86, 87, 93, 94, 96, 2000, 2001, 2002 |
a7850723 | 4 | ;; Free Software Foundation, Inc. |
c2acf685 SM |
5 | |
6 | ;; Maintainer: FSF | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
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 2, or (at your option) | |
13 | ;; any later version. | |
14 | ||
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. | |
19 | ||
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., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
24 | ||
25 | ;;; Commentary: | |
26 | ||
27 | ;; Facilities to display current time/date and a new-mail indicator | |
28 | ;; in the Emacs mode line. The single entry point is `display-time'. | |
29 | ||
30 | ;;; Code: | |
31 | ||
32 | (defgroup display-time nil | |
33 | "Display time and load in mode line of Emacs." | |
34 | :group 'modeline | |
35 | :group 'mail) | |
36 | ||
37 | ||
c2acf685 SM |
38 | (defcustom display-time-mail-file nil |
39 | "*File name of mail inbox file, for indicating existence of new mail. | |
40 | Non-nil and not a string means don't check for mail. nil means use | |
41 | default, which is system-dependent, and is the same as used by Rmail." | |
fe6456af | 42 | :type '(choice (const :tag "None" none) |
c2acf685 SM |
43 | (const :tag "Default" nil) |
44 | (file :format "%v")) | |
45 | :group 'display-time) | |
46 | ||
cc351ed4 PJ |
47 | (defcustom display-time-mail-directory nil |
48 | "*Name of mail inbox directory, for indicating existence of new mail. | |
ad68bb68 RS |
49 | Any nonempty regular file in the directory is regarded as newly arrived mail. |
50 | If nil, do not check a directory for arriving mail." | |
cc351ed4 PJ |
51 | :type '(choice (const :tag "None" nil) |
52 | (directory :format "%v")) | |
53 | :group 'display-time) | |
54 | ||
c2acf685 SM |
55 | (defcustom display-time-mail-function nil |
56 | "*Function to call, for indicating existence of new mail. | |
ad68bb68 RS |
57 | If nil, that means use the default method: check that the file |
58 | specified by `display-time-mail-file' is nonempty or that the | |
59 | directory `display-time-mail-directory' contains nonempty files." | |
c2acf685 SM |
60 | :type '(choice (const :tag "Default" nil) |
61 | (function)) | |
62 | :group 'display-time) | |
63 | ||
4b05e68d PJ |
64 | (defcustom display-time-default-load-average 0 |
65 | "*Which load-average value will be shown in the mode line. | |
66 | Almost every system can provide values of load for past 1 minute, past 5 or | |
67 | past 15 minutes. The default is to display 1 minute load average." | |
68 | :type '(choice (const :tag "1 minute load" 0) | |
69 | (const :tag "5 minutes load" 1) | |
fe6456af RS |
70 | (const :tag "15 minutes load" 2) |
71 | (const :tag "None" nil)) | |
4b05e68d PJ |
72 | :group 'display-time) |
73 | ||
fe6456af RS |
74 | (defvar display-time-load-average nil |
75 | "Load average currently being shown in mode line") | |
4b05e68d PJ |
76 | |
77 | (defcustom display-time-load-average-threshold 0.1 | |
b7b4e4d4 PJ |
78 | "*Load-average values below this value won't be shown in the mode line." |
79 | :type 'number | |
80 | :group 'display-time) | |
4b05e68d | 81 | |
c2acf685 SM |
82 | ;;;###autoload |
83 | (defcustom display-time-day-and-date nil "\ | |
84 | *Non-nil means \\[display-time] should display day and date as well as time." | |
85 | :type 'boolean | |
86 | :group 'display-time) | |
87 | ||
88 | (defvar display-time-timer nil) | |
89 | ||
90 | (defcustom display-time-interval 60 | |
91 | "*Seconds between updates of time in the mode line." | |
92 | :type 'integer | |
93 | :group 'display-time) | |
94 | ||
95 | (defcustom display-time-24hr-format nil | |
96 | "*Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23. | |
4b05e68d | 97 | nil means 1 <= hh <= 12, and an AM/PM suffix is used." |
c2acf685 SM |
98 | :type 'boolean |
99 | :group 'display-time) | |
100 | ||
101 | (defvar display-time-string nil) | |
102 | ||
103 | (defcustom display-time-hook nil | |
104 | "*List of functions to be called when the time is updated on the mode line." | |
105 | :type 'hook | |
106 | :group 'display-time) | |
107 | ||
108 | (defvar display-time-server-down-time nil | |
109 | "Time when mail file's file system was recorded to be down. | |
110 | If that file system seems to be up, the value is nil.") | |
111 | ||
112 | ;;;###autoload | |
113 | (defun display-time () | |
114 | "Enable display of time, load level, and mail flag in mode lines. | |
115 | This display updates automatically every minute. | |
116 | If `display-time-day-and-date' is non-nil, the current day and date | |
117 | are displayed as well. | |
118 | This runs the normal hook `display-time-hook' after each update." | |
119 | (interactive) | |
120 | (display-time-mode 1)) | |
121 | ||
c2acf685 SM |
122 | (defcustom display-time-mail-face 'mode-line |
123 | "Face to use for `display-time-mail-string'. | |
124 | If `display-time-use-mail-icon' is non-nil, the image's background | |
125 | colour is the background of this face. Set this to a face other than | |
126 | `mode-line' to make the mail indicator stand out on a suitable | |
127 | display." | |
128 | :group 'faces | |
129 | :group 'display-time | |
130 | :type 'face) | |
131 | ||
132 | (defvar display-time-mail-icon | |
cc351ed4 PJ |
133 | (find-image '((:type xpm :file "letter.xpm" :ascent center) |
134 | (:type xbm :file "letter.xbm" :ascent center))) | |
c2acf685 SM |
135 | "Image specification to offer as the mail indicator on a graphic |
136 | display. See `display-time-use-mail-icon' and | |
137 | `display-time-mail-face'.") | |
138 | ||
139 | (defcustom display-time-use-mail-icon nil | |
140 | "Non-nil means use an icon as the mail indicator on a graphic display. | |
141 | Otherwise use the string \"Mail\". The icon may consume less of the | |
142 | mode line. It is specified by `display-time-mail-icon'." | |
143 | :group 'display-time | |
144 | :type 'boolean) | |
145 | ||
146 | (defcustom display-time-format nil | |
147 | "*A string specifying the format for displaying the time in the mode line. | |
148 | See the function `format-time-string' for an explanation of | |
149 | how to write this string. If this is nil, the defaults | |
150 | depend on `display-time-day-and-date' and `display-time-24hr-format'." | |
151 | :type '(choice (const :tag "Default" nil) | |
152 | string) | |
153 | :group 'display-time) | |
154 | ||
155 | (defcustom display-time-string-forms | |
156 | '((if (and (not display-time-format) display-time-day-and-date) | |
157 | (format-time-string "%a %b %e " now) | |
158 | "") | |
159 | (format-time-string (or display-time-format | |
160 | (if display-time-24hr-format "%H:%M" "%-I:%M%p")) | |
161 | now) | |
162 | load | |
163 | (if mail | |
164 | ;; Build the string every time to act on customization. | |
165 | (concat " " | |
166 | (propertize | |
167 | "Mail" | |
168 | 'display `(when (and display-time-use-mail-icon | |
169 | (display-graphic-p)) | |
170 | ,@display-time-mail-icon | |
171 | ,@(list :background (face-attribute | |
172 | display-time-mail-face | |
173 | :background))) | |
cc351ed4 | 174 | 'help-echo "You have new mail; mouse-2: Read mail" |
b03e7e2f | 175 | 'local-map (make-mode-line-mouse-map 'mouse-2 |
39fce518 | 176 | read-mail-command))) |
c2acf685 SM |
177 | "")) |
178 | "*A list of expressions governing display of the time in the mode line. | |
179 | For most purposes, you can control the time format using `display-time-format' | |
180 | which is a more standard interface. | |
181 | ||
182 | This expression is a list of expressions that can involve the keywords | |
183 | `load', `day', `month', and `year', `12-hours', `24-hours', `minutes', | |
184 | `seconds', all numbers in string form, and `monthname', `dayname', `am-pm', | |
185 | and `time-zone' all alphabetic strings, and `mail' a true/nil value. | |
186 | ||
187 | For example, the form | |
188 | ||
189 | '((substring year -2) \"/\" month \"/\" day | |
190 | \" \" 24-hours \":\" minutes \":\" seconds | |
191 | (if time-zone \" (\") time-zone (if time-zone \")\") | |
192 | (if mail \" Mail\" \"\")) | |
193 | ||
194 | would give mode line times like `94/12/30 21:07:48 (UTC)'." | |
195 | :type 'sexp | |
196 | :group 'display-time) | |
197 | ||
198 | (defun display-time-event-handler () | |
199 | (display-time-update) | |
200 | ;; Do redisplay right now, if no input pending. | |
201 | (sit-for 0) | |
202 | (let* ((current (current-time)) | |
203 | (timer display-time-timer) | |
204 | ;; Compute the time when this timer will run again, next. | |
205 | (next-time (timer-relative-time | |
206 | (list (aref timer 1) (aref timer 2) (aref timer 3)) | |
207 | (* 5 (aref timer 4)) 0))) | |
208 | ;; If the activation time is far in the past, | |
209 | ;; skip executions until we reach a time in the future. | |
210 | ;; This avoids a long pause if Emacs has been suspended for hours. | |
211 | (or (> (nth 0 next-time) (nth 0 current)) | |
212 | (and (= (nth 0 next-time) (nth 0 current)) | |
213 | (> (nth 1 next-time) (nth 1 current))) | |
214 | (and (= (nth 0 next-time) (nth 0 current)) | |
215 | (= (nth 1 next-time) (nth 1 current)) | |
216 | (> (nth 2 next-time) (nth 2 current))) | |
217 | (progn | |
218 | (timer-set-time timer (timer-next-integral-multiple-of-time | |
219 | current display-time-interval) | |
220 | display-time-interval) | |
221 | (timer-activate timer))))) | |
222 | ||
4b05e68d PJ |
223 | (defun display-time-next-load-average () |
224 | (interactive) | |
225 | (if (= 3 (setq display-time-load-average (1+ display-time-load-average))) | |
226 | (setq display-time-load-average 0)) | |
227 | (display-time-update) | |
228 | (sit-for 0)) | |
229 | ||
cc351ed4 PJ |
230 | (defun display-time-mail-check-directory () |
231 | (let ((mail-files (directory-files display-time-mail-directory t)) | |
232 | (size 0)) | |
233 | (while (and mail-files (= size 0)) | |
234 | ;; Count size of regular files only. | |
235 | (setq size (+ size (or (and (file-regular-p (car mail-files)) | |
236 | (nth 7 (file-attributes (car mail-files)))) | |
237 | 0))) | |
238 | (setq mail-files (cdr mail-files))) | |
239 | (if (> size 0) | |
240 | size | |
241 | nil))) | |
242 | ||
c2acf685 SM |
243 | ;; Update the display-time info for the mode line |
244 | ;; but don't redisplay right now. This is used for | |
245 | ;; things like Rmail `g' that want to force an update | |
246 | ;; which can wait for the next redisplay. | |
247 | (defun display-time-update () | |
248 | (let* ((now (current-time)) | |
249 | (time (current-time-string now)) | |
fe6456af RS |
250 | (load (if (null display-time-load-average) |
251 | "" | |
252 | (condition-case () | |
253 | ;; Do not show values less than | |
254 | ;; `display-time-load-average-threshold'. | |
255 | (if (> (* display-time-load-average-threshold 100) | |
256 | (nth display-time-load-average (load-average))) | |
257 | "" | |
258 | ;; The load average number is mysterious, so | |
259 | ;; provide some help. | |
260 | (let ((str (format " %03d" (nth display-time-load-average (load-average))))) | |
261 | (propertize | |
262 | (concat (substring str 0 -2) "." (substring str -2)) | |
263 | 'local-map (make-mode-line-mouse-map 'mouse-2 | |
264 | 'display-time-next-load-average) | |
265 | 'help-echo (concat "System load average for past " | |
266 | (if (= 0 display-time-load-average) | |
267 | "1 minute" | |
268 | (if (= 1 display-time-load-average) | |
269 | "5 minutes" | |
270 | "15 minutes")) "; mouse-2: next" )))) | |
271 | (error "")))) | |
c2acf685 SM |
272 | (mail-spool-file (or display-time-mail-file |
273 | (getenv "MAIL") | |
274 | (concat rmail-spool-directory | |
275 | (user-login-name)))) | |
276 | (mail (or (and display-time-mail-function | |
277 | (funcall display-time-mail-function)) | |
cc351ed4 PJ |
278 | (and display-time-mail-directory |
279 | (display-time-mail-check-directory)) | |
c2acf685 SM |
280 | (and (stringp mail-spool-file) |
281 | (or (null display-time-server-down-time) | |
282 | ;; If have been down for 20 min, try again. | |
283 | (> (- (nth 1 now) display-time-server-down-time) | |
284 | 1200) | |
285 | (and (< (nth 1 now) display-time-server-down-time) | |
286 | (> (- (nth 1 now) display-time-server-down-time) | |
287 | -64336))) | |
288 | (let ((start-time (current-time))) | |
289 | (prog1 | |
290 | (display-time-file-nonempty-p mail-spool-file) | |
291 | (if (> (- (nth 1 (current-time)) (nth 1 start-time)) | |
292 | 20) | |
293 | ;; Record that mail file is not accessible. | |
294 | (setq display-time-server-down-time | |
295 | (nth 1 (current-time))) | |
296 | ;; Record that mail file is accessible. | |
297 | (setq display-time-server-down-time nil))))))) | |
298 | (24-hours (substring time 11 13)) | |
299 | (hour (string-to-int 24-hours)) | |
300 | (12-hours (int-to-string (1+ (% (+ hour 11) 12)))) | |
301 | (am-pm (if (>= hour 12) "pm" "am")) | |
302 | (minutes (substring time 14 16)) | |
303 | (seconds (substring time 17 19)) | |
304 | (time-zone (car (cdr (current-time-zone now)))) | |
305 | (day (substring time 8 10)) | |
306 | (year (substring time 20 24)) | |
307 | (monthname (substring time 4 7)) | |
308 | (month | |
309 | (cdr | |
310 | (assoc | |
311 | monthname | |
312 | '(("Jan" . "1") ("Feb" . "2") ("Mar" . "3") ("Apr" . "4") | |
313 | ("May" . "5") ("Jun" . "6") ("Jul" . "7") ("Aug" . "8") | |
314 | ("Sep" . "9") ("Oct" . "10") ("Nov" . "11") ("Dec" . "12"))))) | |
315 | (dayname (substring time 0 3))) | |
316 | (setq display-time-string | |
317 | (mapconcat 'eval display-time-string-forms "")) | |
318 | ;; This is inside the let binding, but we are not going to document | |
319 | ;; what variables are available. | |
320 | (run-hooks 'display-time-hook)) | |
321 | (force-mode-line-update)) | |
322 | ||
323 | (defun display-time-file-nonempty-p (file) | |
324 | (and (file-exists-p file) | |
325 | (< 0 (nth 7 (file-attributes (file-chase-links file)))))) | |
326 | ||
57ce63c4 SM |
327 | ;;;###autoload |
328 | (define-minor-mode display-time-mode | |
329 | "Toggle display of time, load level, and mail flag in mode lines. | |
330 | With a numeric arg, enable this display if arg is positive. | |
331 | ||
332 | When this display is enabled, it updates automatically every minute. | |
333 | If `display-time-day-and-date' is non-nil, the current day and date | |
334 | are displayed as well. | |
335 | This runs the normal hook `display-time-hook' after each update." | |
bb7a71c5 | 336 | :global t :group 'display-time |
fe6456af RS |
337 | (and display-time-timer (cancel-timer display-time-timer)) |
338 | (setq display-time-timer nil) | |
339 | (setq display-time-string "") | |
340 | (or global-mode-string (setq global-mode-string '(""))) | |
341 | (setq display-time-load-average display-time-default-load-average) | |
342 | (if display-time-mode | |
343 | (progn | |
344 | (or (memq 'display-time-string global-mode-string) | |
345 | (setq global-mode-string | |
346 | (append global-mode-string '(display-time-string)))) | |
347 | ;; Set up the time timer. | |
348 | (setq display-time-timer | |
349 | (run-at-time t display-time-interval | |
350 | 'display-time-event-handler)) | |
351 | ;; Make the time appear right away. | |
352 | (display-time-update) | |
353 | ;; When you get new mail, clear "Mail" from the mode line. | |
354 | (add-hook 'rmail-after-get-new-mail-hook | |
355 | 'display-time-event-handler)) | |
356 | (remove-hook 'rmail-after-get-new-mail-hook | |
357 | 'display-time-event-handler))) | |
c2acf685 SM |
358 | |
359 | (provide 'time) | |
360 | ||
361 | ;;; time.el ends here |