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