Commit | Line | Data |
---|---|---|
b7eb6e8a | 1 | ;;; time.el --- display time, load and mail indicator in mode line of Emacs -*-coding: utf-8 -*- |
c2acf685 | 2 | |
acaf905b | 3 | ;; Copyright (C) 1985-1987, 1993-1994, 1996, 2000-2012 |
95df8112 | 4 | ;; Free Software Foundation, Inc. |
c2acf685 SM |
5 | |
6 | ;; Maintainer: FSF | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
eb3fa2cf | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
c2acf685 | 11 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
c2acf685 SM |
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 | |
eb3fa2cf | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
c2acf685 SM |
22 | |
23 | ;;; Commentary: | |
24 | ||
25 | ;; Facilities to display current time/date and a new-mail indicator | |
aff2ba04 RS |
26 | ;; in the Emacs mode line. The entry point is `display-time'. |
27 | ||
28 | ;; Display time world in a buffer, the entry point is | |
29 | ;; `display-time-world'. | |
c2acf685 SM |
30 | |
31 | ;;; Code: | |
32 | ||
33 | (defgroup display-time nil | |
34 | "Display time and load in mode line of Emacs." | |
97904e51 | 35 | :group 'mode-line |
c2acf685 SM |
36 | :group 'mail) |
37 | ||
38 | ||
c2acf685 | 39 | (defcustom display-time-mail-file nil |
9201cc28 | 40 | "File name of mail inbox file, for indicating existence of new mail. |
cbb7527f | 41 | Non-nil and not a string means don't check for mail; nil means use |
c2acf685 | 42 | default, which is system-dependent, and is the same as used by Rmail." |
fe6456af | 43 | :type '(choice (const :tag "None" none) |
c2acf685 SM |
44 | (const :tag "Default" nil) |
45 | (file :format "%v")) | |
46 | :group 'display-time) | |
47 | ||
cc351ed4 | 48 | (defcustom display-time-mail-directory nil |
9201cc28 | 49 | "Name of mail inbox directory, for indicating existence of new mail. |
ad68bb68 RS |
50 | Any nonempty regular file in the directory is regarded as newly arrived mail. |
51 | If nil, do not check a directory for arriving mail." | |
cc351ed4 PJ |
52 | :type '(choice (const :tag "None" nil) |
53 | (directory :format "%v")) | |
54 | :group 'display-time) | |
55 | ||
c2acf685 | 56 | (defcustom display-time-mail-function nil |
9201cc28 | 57 | "Function to call, for indicating existence of new mail. |
ad68bb68 RS |
58 | If nil, that means use the default method: check that the file |
59 | specified by `display-time-mail-file' is nonempty or that the | |
60 | directory `display-time-mail-directory' contains nonempty files." | |
c2acf685 SM |
61 | :type '(choice (const :tag "Default" nil) |
62 | (function)) | |
63 | :group 'display-time) | |
64 | ||
4b05e68d | 65 | (defcustom display-time-default-load-average 0 |
9201cc28 | 66 | "Which load average value will be shown in the mode line. |
4afee9d5 EZ |
67 | Almost every system can provide values of load for the past 1 minute, |
68 | past 5 or past 15 minutes. The default is to display 1-minute load average. | |
3deeb46b GM |
69 | The value can be one of: |
70 | ||
71 | 0 => 1 minute load | |
72 | 1 => 5 minutes load | |
4afee9d5 EZ |
73 | 2 => 15 minutes load |
74 | nil => None (do not display the load average)" | |
4b05e68d PJ |
75 | :type '(choice (const :tag "1 minute load" 0) |
76 | (const :tag "5 minutes load" 1) | |
fe6456af RS |
77 | (const :tag "15 minutes load" 2) |
78 | (const :tag "None" nil)) | |
4b05e68d PJ |
79 | :group 'display-time) |
80 | ||
fe6456af | 81 | (defvar display-time-load-average nil |
4afee9d5 EZ |
82 | "Value of the system's load average currently shown on the mode line. |
83 | See `display-time-default-load-average'. | |
84 | ||
85 | This is an internal variable; setting it has no effect.") | |
4b05e68d PJ |
86 | |
87 | (defcustom display-time-load-average-threshold 0.1 | |
9201cc28 | 88 | "Load-average values below this value won't be shown in the mode line." |
b7b4e4d4 PJ |
89 | :type 'number |
90 | :group 'display-time) | |
4b05e68d | 91 | |
c2acf685 SM |
92 | ;;;###autoload |
93 | (defcustom display-time-day-and-date nil "\ | |
7c7c04c0 | 94 | Non-nil means \\[display-time] should display day and date as well as time." |
c2acf685 SM |
95 | :type 'boolean |
96 | :group 'display-time) | |
97 | ||
98 | (defvar display-time-timer nil) | |
99 | ||
100 | (defcustom display-time-interval 60 | |
9201cc28 | 101 | "Seconds between updates of time in the mode line." |
c2acf685 SM |
102 | :type 'integer |
103 | :group 'display-time) | |
104 | ||
105 | (defcustom display-time-24hr-format nil | |
9201cc28 | 106 | "Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23. |
7d326782 | 107 | A value of nil means 1 <= hh <= 12, and an AM/PM suffix is used." |
c2acf685 SM |
108 | :type 'boolean |
109 | :group 'display-time) | |
110 | ||
111 | (defvar display-time-string nil) | |
6dc3311d | 112 | ;;;###autoload(put 'display-time-string 'risky-local-variable t) |
c2acf685 SM |
113 | |
114 | (defcustom display-time-hook nil | |
9201cc28 | 115 | "List of functions to be called when the time is updated on the mode line." |
c2acf685 SM |
116 | :type 'hook |
117 | :group 'display-time) | |
118 | ||
119 | (defvar display-time-server-down-time nil | |
120 | "Time when mail file's file system was recorded to be down. | |
121 | If that file system seems to be up, the value is nil.") | |
122 | ||
dca2168c | 123 | (defcustom zoneinfo-style-world-list |
aff2ba04 RS |
124 | '(("America/Los_Angeles" "Seattle") |
125 | ("America/New_York" "New York") | |
126 | ("Europe/London" "London") | |
127 | ("Europe/Paris" "Paris") | |
128 | ("Asia/Calcutta" "Bangalore") | |
129 | ("Asia/Tokyo" "Tokyo")) | |
dca2168c EZ |
130 | "Alist of zoneinfo-style time zones and places for `display-time-world'. |
131 | Each element has the form (TIMEZONE LABEL). | |
132 | TIMEZONE should be a string of the form AREA/LOCATION, where AREA is | |
133 | the name of a region -- a continent or ocean, and LOCATION is the name | |
134 | of a specific location, e.g., a city, within that region. | |
135 | LABEL is a string to display as the label of that TIMEZONE's time." | |
136 | :group 'display-time | |
137 | :type '(repeat (list string string)) | |
138 | :version "23.1") | |
139 | ||
140 | (defcustom legacy-style-world-list | |
141 | '(("PST8PDT" "Seattle") | |
142 | ("EST5EDT" "New York") | |
9db0c29b | 143 | ("GMT0BST" "London") |
dca2168c | 144 | ("CET-1CDT" "Paris") |
9db0c29b EZ |
145 | ("IST-5:30" "Bangalore") |
146 | ("JST-9" "Tokyo")) | |
dca2168c EZ |
147 | "Alist of traditional-style time zones and places for `display-time-world'. |
148 | Each element has the form (TIMEZONE LABEL). | |
149 | TIMEZONE should be a string of the form: | |
150 | ||
151 | std[+|-]offset[dst[offset][,date[/time],date[/time]]] | |
152 | ||
153 | See the documentation of the TZ environment variable on your system, | |
154 | for more details about the format of TIMEZONE. | |
155 | LABEL is a string to display as the label of that TIMEZONE's time." | |
156 | :group 'display-time | |
157 | :type '(repeat (list string string)) | |
158 | :version "23.1") | |
159 | ||
160 | (defcustom display-time-world-list | |
36d4b145 JR |
161 | ;; Determine if zoneinfo style timezones are supported by testing that |
162 | ;; America/New York and Europe/London return different timezones. | |
37e11a63 CY |
163 | (let ((old-tz (getenv "TZ")) |
164 | gmt nyt) | |
165 | (unwind-protect | |
166 | (progn | |
167 | (setenv "TZ" "America/New_York") | |
168 | (setq nyt (format-time-string "%z")) | |
169 | (setenv "TZ" "Europe/London") | |
170 | (setq gmt (format-time-string "%z"))) | |
171 | (setenv "TZ" old-tz)) | |
36d4b145 JR |
172 | (if (string-equal nyt gmt) |
173 | legacy-style-world-list | |
174 | zoneinfo-style-world-list)) | |
dca2168c | 175 | "Alist of time zones and places for `display-time-world' to display. |
aff2ba04 | 176 | Each element has the form (TIMEZONE LABEL). |
37e11a63 CY |
177 | TIMEZONE should be in a format supported by your system. See the |
178 | documentation of `zoneinfo-style-world-list' and | |
179 | \`legacy-style-world-list' for two widely used formats. LABEL is | |
180 | a string to display as the label of that TIMEZONE's time." | |
aff2ba04 RS |
181 | :group 'display-time |
182 | :type '(repeat (list string string)) | |
183 | :version "23.1") | |
184 | ||
658dcd47 | 185 | (defcustom display-time-world-time-format "%A %d %B %R %Z" |
aff2ba04 RS |
186 | "Format of the time displayed, see `format-time-string'." |
187 | :group 'display-time | |
188 | :type 'string | |
189 | :version "23.1") | |
190 | ||
191 | (defcustom display-time-world-buffer-name "*wclock*" | |
7c7c04c0 | 192 | "Name of the world clock buffer." |
aff2ba04 RS |
193 | :group 'display-time |
194 | :type 'string | |
195 | :version "23.1") | |
196 | ||
197 | (defcustom display-time-world-timer-enable t | |
198 | "If non-nil, a timer will update the world clock." | |
199 | :group 'display-time | |
200 | :type 'boolean | |
201 | :version "23.1") | |
202 | ||
203 | (defcustom display-time-world-timer-second 60 | |
204 | "Interval in seconds for updating the world clock." | |
205 | :group 'display-time | |
206 | :type 'integer | |
207 | :version "23.1") | |
208 | ||
209 | (defvar display-time-world-mode-map | |
210 | (let ((map (make-sparse-keymap))) | |
211 | (define-key map "q" 'kill-this-buffer) | |
212 | map) | |
7c7c04c0 | 213 | "Keymap of Display Time World mode.") |
aff2ba04 | 214 | |
c2acf685 SM |
215 | ;;;###autoload |
216 | (defun display-time () | |
217 | "Enable display of time, load level, and mail flag in mode lines. | |
218 | This display updates automatically every minute. | |
219 | If `display-time-day-and-date' is non-nil, the current day and date | |
220 | are displayed as well. | |
221 | This runs the normal hook `display-time-hook' after each update." | |
222 | (interactive) | |
223 | (display-time-mode 1)) | |
224 | ||
b7eb6e8a DL |
225 | ;; This business used to be simpler when all mode lines had the same |
226 | ;; face and the image could just be pbm. Now we try to rely on an xpm | |
227 | ;; image with a transparent background. Otherwise, set the background | |
228 | ;; for pbm. | |
229 | ||
230 | (defcustom display-time-mail-face nil | |
c2acf685 | 231 | "Face to use for `display-time-mail-string'. |
b7eb6e8a | 232 | If `display-time-use-mail-icon' is non-nil, the image's |
6bd47b39 JB |
233 | background color is the background of this face. Set this to |
234 | make the mail indicator stand out on a color display." | |
83033a3a | 235 | :group 'mode-line-faces |
c2acf685 | 236 | :group 'display-time |
bf247b6e | 237 | :version "22.1" |
b7eb6e8a | 238 | :type '(choice (const :tag "None" nil) face)) |
c2acf685 SM |
239 | |
240 | (defvar display-time-mail-icon | |
cc351ed4 | 241 | (find-image '((:type xpm :file "letter.xpm" :ascent center) |
b7eb6e8a DL |
242 | (:type pbm :file "letter.pbm" :ascent center))) |
243 | "Image specification to offer as the mail indicator on a graphic display. | |
244 | See `display-time-use-mail-icon' and `display-time-mail-face'.") | |
c2acf685 | 245 | |
b7eb6e8a | 246 | ;; Fixme: Default to icon on graphical display? |
c2acf685 | 247 | (defcustom display-time-use-mail-icon nil |
d7baab9c | 248 | "Non-nil means use an icon as mail indicator on a graphic display. |
b7eb6e8a DL |
249 | Otherwise use `display-time-mail-string'. The icon may consume less |
250 | of the mode line. It is specified by `display-time-mail-icon'." | |
c2acf685 SM |
251 | :group 'display-time |
252 | :type 'boolean) | |
253 | ||
b7eb6e8a DL |
254 | ;; Fixme: maybe default to the character if we can display Unicode. |
255 | (defcustom display-time-mail-string "Mail" | |
256 | "String to use as the mail indicator in `display-time-string-forms'. | |
257 | This can use the Unicode letter character if you can display it." | |
258 | :group 'display-time | |
bf247b6e | 259 | :version "22.1" |
b7eb6e8a DL |
260 | :type '(choice (const "Mail") |
261 | ;; Use :tag here because the Lucid menu won't display | |
262 | ;; multibyte text. | |
263 | (const :tag "Unicode letter character" "✉") | |
264 | string)) | |
265 | ||
c2acf685 | 266 | (defcustom display-time-format nil |
9201cc28 | 267 | "String specifying format for displaying the time in the mode line. |
c2acf685 SM |
268 | See the function `format-time-string' for an explanation of |
269 | how to write this string. If this is nil, the defaults | |
270 | depend on `display-time-day-and-date' and `display-time-24hr-format'." | |
271 | :type '(choice (const :tag "Default" nil) | |
272 | string) | |
273 | :group 'display-time) | |
274 | ||
275 | (defcustom display-time-string-forms | |
276 | '((if (and (not display-time-format) display-time-day-and-date) | |
277 | (format-time-string "%a %b %e " now) | |
278 | "") | |
e93b2f39 SM |
279 | (propertize |
280 | (format-time-string (or display-time-format | |
281 | (if display-time-24hr-format "%H:%M" "%-I:%M%p")) | |
282 | now) | |
ef3e9175 | 283 | 'help-echo (format-time-string "%a %b %e, %Y" now)) |
c2acf685 SM |
284 | load |
285 | (if mail | |
286 | ;; Build the string every time to act on customization. | |
b7eb6e8a DL |
287 | ;; :set-after doesn't help for `customize-option'. I think it |
288 | ;; should. | |
289 | (concat | |
290 | " " | |
291 | (propertize | |
292 | display-time-mail-string | |
293 | 'display `(when (and display-time-use-mail-icon | |
294 | (display-graphic-p)) | |
295 | ,@display-time-mail-icon | |
296 | ,@(if (and display-time-mail-face | |
297 | (memq (plist-get (cdr display-time-mail-icon) | |
298 | :type) | |
299 | '(pbm xbm))) | |
300 | (let ((bg (face-attribute display-time-mail-face | |
301 | :background))) | |
302 | (if (stringp bg) | |
303 | (list :background bg))))) | |
304 | 'face display-time-mail-face | |
305 | 'help-echo "You have new mail; mouse-2: Read mail" | |
3a02b63e | 306 | 'mouse-face 'mode-line-highlight |
b7eb6e8a DL |
307 | 'local-map (make-mode-line-mouse-map 'mouse-2 |
308 | read-mail-command))) | |
c2acf685 | 309 | "")) |
9201cc28 | 310 | "List of expressions governing display of the time in the mode line. |
c2acf685 SM |
311 | For most purposes, you can control the time format using `display-time-format' |
312 | which is a more standard interface. | |
313 | ||
314 | This expression is a list of expressions that can involve the keywords | |
315 | `load', `day', `month', and `year', `12-hours', `24-hours', `minutes', | |
316 | `seconds', all numbers in string form, and `monthname', `dayname', `am-pm', | |
317 | and `time-zone' all alphabetic strings, and `mail' a true/nil value. | |
318 | ||
319 | For example, the form | |
320 | ||
321 | '((substring year -2) \"/\" month \"/\" day | |
322 | \" \" 24-hours \":\" minutes \":\" seconds | |
323 | (if time-zone \" (\") time-zone (if time-zone \")\") | |
324 | (if mail \" Mail\" \"\")) | |
325 | ||
326 | would give mode line times like `94/12/30 21:07:48 (UTC)'." | |
327 | :type 'sexp | |
328 | :group 'display-time) | |
329 | ||
330 | (defun display-time-event-handler () | |
331 | (display-time-update) | |
332 | ;; Do redisplay right now, if no input pending. | |
333 | (sit-for 0) | |
334 | (let* ((current (current-time)) | |
335 | (timer display-time-timer) | |
336 | ;; Compute the time when this timer will run again, next. | |
337 | (next-time (timer-relative-time | |
338 | (list (aref timer 1) (aref timer 2) (aref timer 3)) | |
339 | (* 5 (aref timer 4)) 0))) | |
340 | ;; If the activation time is far in the past, | |
341 | ;; skip executions until we reach a time in the future. | |
342 | ;; This avoids a long pause if Emacs has been suspended for hours. | |
343 | (or (> (nth 0 next-time) (nth 0 current)) | |
344 | (and (= (nth 0 next-time) (nth 0 current)) | |
345 | (> (nth 1 next-time) (nth 1 current))) | |
346 | (and (= (nth 0 next-time) (nth 0 current)) | |
347 | (= (nth 1 next-time) (nth 1 current)) | |
348 | (> (nth 2 next-time) (nth 2 current))) | |
349 | (progn | |
350 | (timer-set-time timer (timer-next-integral-multiple-of-time | |
351 | current display-time-interval) | |
352 | display-time-interval) | |
353 | (timer-activate timer))))) | |
354 | ||
4b05e68d | 355 | (defun display-time-next-load-average () |
2bb4227e GM |
356 | "Switch between different load averages in the mode line. |
357 | Switches from the 1 to 5 to 15 minute load average, and then back to 1." | |
4b05e68d PJ |
358 | (interactive) |
359 | (if (= 3 (setq display-time-load-average (1+ display-time-load-average))) | |
360 | (setq display-time-load-average 0)) | |
361 | (display-time-update) | |
362 | (sit-for 0)) | |
363 | ||
cc351ed4 PJ |
364 | (defun display-time-mail-check-directory () |
365 | (let ((mail-files (directory-files display-time-mail-directory t)) | |
366 | (size 0)) | |
367 | (while (and mail-files (= size 0)) | |
368 | ;; Count size of regular files only. | |
369 | (setq size (+ size (or (and (file-regular-p (car mail-files)) | |
370 | (nth 7 (file-attributes (car mail-files)))) | |
371 | 0))) | |
372 | (setq mail-files (cdr mail-files))) | |
373 | (if (> size 0) | |
374 | size | |
375 | nil))) | |
376 | ||
06b60517 | 377 | (with-no-warnings |
c80e3b4a | 378 | ;; Warnings are suppressed to avoid "global/dynamic var `X' lacks a prefix". |
06b60517 JB |
379 | (defvar now) |
380 | (defvar time) | |
381 | (defvar load) | |
382 | (defvar mail) | |
383 | (defvar 24-hours) | |
384 | (defvar hour) | |
385 | (defvar 12-hours) | |
386 | (defvar am-pm) | |
387 | (defvar minutes) | |
388 | (defvar seconds) | |
389 | (defvar time-zone) | |
390 | (defvar day) | |
391 | (defvar year) | |
392 | (defvar monthname) | |
393 | (defvar month) | |
394 | (defvar dayname)) | |
395 | ||
c2acf685 | 396 | (defun display-time-update () |
b7eb6e8a DL |
397 | "Update the display-time info for the mode line. |
398 | However, don't redisplay right now. | |
399 | ||
400 | This is used for things like Rmail `g' that want to force an | |
401 | update which can wait for the next redisplay." | |
c2acf685 SM |
402 | (let* ((now (current-time)) |
403 | (time (current-time-string now)) | |
fe6456af RS |
404 | (load (if (null display-time-load-average) |
405 | "" | |
406 | (condition-case () | |
407 | ;; Do not show values less than | |
408 | ;; `display-time-load-average-threshold'. | |
409 | (if (> (* display-time-load-average-threshold 100) | |
410 | (nth display-time-load-average (load-average))) | |
411 | "" | |
412 | ;; The load average number is mysterious, so | |
413 | ;; provide some help. | |
b7eb6e8a DL |
414 | (let ((str (format " %03d" |
415 | (nth display-time-load-average | |
416 | (load-average))))) | |
fe6456af RS |
417 | (propertize |
418 | (concat (substring str 0 -2) "." (substring str -2)) | |
b7eb6e8a DL |
419 | 'local-map (make-mode-line-mouse-map |
420 | 'mouse-2 'display-time-next-load-average) | |
3a02b63e | 421 | 'mouse-face 'mode-line-highlight |
b7eb6e8a DL |
422 | 'help-echo (concat |
423 | "System load average for past " | |
424 | (if (= 0 display-time-load-average) | |
425 | "1 minute" | |
426 | (if (= 1 display-time-load-average) | |
427 | "5 minutes" | |
428 | "15 minutes")) | |
429 | "; mouse-2: next")))) | |
fe6456af | 430 | (error "")))) |
c2acf685 SM |
431 | (mail-spool-file (or display-time-mail-file |
432 | (getenv "MAIL") | |
433 | (concat rmail-spool-directory | |
434 | (user-login-name)))) | |
ab283561 LMI |
435 | (mail (cond |
436 | (display-time-mail-function | |
437 | (funcall display-time-mail-function)) | |
438 | (display-time-mail-directory | |
439 | (display-time-mail-check-directory)) | |
440 | ((and (stringp mail-spool-file) | |
441 | (or (null display-time-server-down-time) | |
442 | ;; If have been down for 20 min, try again. | |
443 | (> (- (nth 1 now) display-time-server-down-time) | |
444 | 1200) | |
445 | (and (< (nth 1 now) display-time-server-down-time) | |
446 | (> (- (nth 1 now) | |
447 | display-time-server-down-time) | |
448 | -64336)))) | |
449 | (let ((start-time (current-time))) | |
450 | (prog1 | |
451 | (display-time-file-nonempty-p mail-spool-file) | |
452 | (if (> (- (nth 1 (current-time)) | |
453 | (nth 1 start-time)) | |
454 | 20) | |
455 | ;; Record that mail file is not accessible. | |
456 | (setq display-time-server-down-time | |
457 | (nth 1 (current-time))) | |
458 | ;; Record that mail file is accessible. | |
459 | (setq display-time-server-down-time nil))))))) | |
c2acf685 | 460 | (24-hours (substring time 11 13)) |
027a4b6b | 461 | (hour (string-to-number 24-hours)) |
c2acf685 SM |
462 | (12-hours (int-to-string (1+ (% (+ hour 11) 12)))) |
463 | (am-pm (if (>= hour 12) "pm" "am")) | |
464 | (minutes (substring time 14 16)) | |
465 | (seconds (substring time 17 19)) | |
466 | (time-zone (car (cdr (current-time-zone now)))) | |
467 | (day (substring time 8 10)) | |
468 | (year (substring time 20 24)) | |
469 | (monthname (substring time 4 7)) | |
470 | (month | |
471 | (cdr | |
472 | (assoc | |
473 | monthname | |
474 | '(("Jan" . "1") ("Feb" . "2") ("Mar" . "3") ("Apr" . "4") | |
475 | ("May" . "5") ("Jun" . "6") ("Jul" . "7") ("Aug" . "8") | |
476 | ("Sep" . "9") ("Oct" . "10") ("Nov" . "11") ("Dec" . "12"))))) | |
477 | (dayname (substring time 0 3))) | |
478 | (setq display-time-string | |
479 | (mapconcat 'eval display-time-string-forms "")) | |
480 | ;; This is inside the let binding, but we are not going to document | |
481 | ;; what variables are available. | |
482 | (run-hooks 'display-time-hook)) | |
483 | (force-mode-line-update)) | |
484 | ||
485 | (defun display-time-file-nonempty-p (file) | |
4bc3c53d MA |
486 | (let ((remote-file-name-inhibit-cache (- display-time-interval 5))) |
487 | (and (file-exists-p file) | |
488 | (< 0 (nth 7 (file-attributes (file-chase-links file))))))) | |
c2acf685 | 489 | |
57ce63c4 SM |
490 | ;;;###autoload |
491 | (define-minor-mode display-time-mode | |
492 | "Toggle display of time, load level, and mail flag in mode lines. | |
06e21633 CY |
493 | With a prefix argument ARG, enable Display Time mode if ARG is |
494 | positive, and disable it otherwise. If called from Lisp, enable | |
495 | it if ARG is omitted or nil. | |
496 | ||
497 | When Display Time mode is enabled, it updates every minute (you | |
498 | can control the number of seconds between updates by customizing | |
499 | `display-time-interval'). If `display-time-day-and-date' is | |
500 | non-nil, the current day and date are displayed as well. This | |
501 | runs the normal hook `display-time-hook' after each update." | |
bb7a71c5 | 502 | :global t :group 'display-time |
fe6456af RS |
503 | (and display-time-timer (cancel-timer display-time-timer)) |
504 | (setq display-time-timer nil) | |
505 | (setq display-time-string "") | |
506 | (or global-mode-string (setq global-mode-string '(""))) | |
507 | (setq display-time-load-average display-time-default-load-average) | |
508 | (if display-time-mode | |
509 | (progn | |
510 | (or (memq 'display-time-string global-mode-string) | |
511 | (setq global-mode-string | |
512 | (append global-mode-string '(display-time-string)))) | |
513 | ;; Set up the time timer. | |
514 | (setq display-time-timer | |
515 | (run-at-time t display-time-interval | |
516 | 'display-time-event-handler)) | |
517 | ;; Make the time appear right away. | |
518 | (display-time-update) | |
519 | ;; When you get new mail, clear "Mail" from the mode line. | |
520 | (add-hook 'rmail-after-get-new-mail-hook | |
521 | 'display-time-event-handler)) | |
522 | (remove-hook 'rmail-after-get-new-mail-hook | |
523 | 'display-time-event-handler))) | |
c2acf685 | 524 | |
aff2ba04 | 525 | |
16f3ade5 | 526 | (define-derived-mode display-time-world-mode nil "World clock" |
aff2ba04 RS |
527 | "Major mode for buffer that displays times in various time zones. |
528 | See `display-time-world'." | |
16f3ade5 | 529 | (setq show-trailing-whitespace nil)) |
aff2ba04 RS |
530 | |
531 | (defun display-time-world-display (alist) | |
532 | "Replace current buffer text with times in various zones, based on ALIST." | |
533 | (let ((inhibit-read-only t) | |
37e11a63 CY |
534 | (buffer-undo-list t) |
535 | (old-tz (getenv "TZ")) | |
536 | (max-width 0) | |
537 | result fmt) | |
aff2ba04 | 538 | (erase-buffer) |
37e11a63 CY |
539 | (unwind-protect |
540 | (dolist (zone alist) | |
541 | (let* ((label (cadr zone)) | |
542 | (width (string-width label))) | |
543 | (setenv "TZ" (car zone)) | |
544 | (push (cons label | |
545 | (format-time-string display-time-world-time-format)) | |
546 | result) | |
547 | (when (> width max-width) | |
548 | (setq max-width width)))) | |
549 | (setenv "TZ" old-tz)) | |
550 | (setq fmt (concat "%-" (int-to-string max-width) "s %s\n")) | |
551 | (dolist (timedata (nreverse result)) | |
552 | (insert (format fmt (car timedata) (cdr timedata))))) | |
553 | (delete-char -1)) | |
aff2ba04 RS |
554 | |
555 | ;;;###autoload | |
556 | (defun display-time-world () | |
557 | "Enable updating display of times in various time zones. | |
558 | `display-time-world-list' specifies the zones. | |
559 | To turn off the world time display, go to that window and type `q'." | |
560 | (interactive) | |
561 | (when (and display-time-world-timer-enable | |
562 | (not (get-buffer display-time-world-buffer-name))) | |
563 | (run-at-time t display-time-world-timer-second 'display-time-world-timer)) | |
564 | (with-current-buffer (get-buffer-create display-time-world-buffer-name) | |
565 | (display-time-world-display display-time-world-list)) | |
566 | (pop-to-buffer display-time-world-buffer-name) | |
567 | (fit-window-to-buffer) | |
568 | (display-time-world-mode)) | |
569 | ||
570 | (defun display-time-world-timer () | |
571 | (if (get-buffer display-time-world-buffer-name) | |
572 | (with-current-buffer (get-buffer display-time-world-buffer-name) | |
573 | (display-time-world-display display-time-world-list)) | |
574 | ;; cancel timer | |
575 | (let ((list timer-list)) | |
576 | (while list | |
577 | (let ((elt (pop list))) | |
578 | (when (equal (symbol-name (aref elt 5)) "display-time-world-timer") | |
579 | (cancel-timer elt))))))) | |
580 | ||
2736785a | 581 | ;;;###autoload |
04e56139 GM |
582 | (defun emacs-uptime (&optional format) |
583 | "Return a string giving the uptime of this instance of Emacs. | |
584 | FORMAT is a string to format the result, using `format-seconds'. | |
585 | For example, the Unix uptime command format is \"%D, %z%2h:%.2m\"." | |
2736785a GM |
586 | (interactive) |
587 | (let ((str | |
04e56139 | 588 | (format-seconds (or format "%Y, %D, %H, %M, %z%S") |
0d00c61c | 589 | (float-time |
e279593d | 590 | (time-subtract (current-time) before-init-time))))) |
32226619 | 591 | (if (called-interactively-p 'interactive) |
2736785a GM |
592 | (message "%s" str) |
593 | str))) | |
594 | ||
24d6c292 JL |
595 | ;;;###autoload |
596 | (defun emacs-init-time () | |
597 | "Return a string giving the duration of the Emacs initialization." | |
598 | (interactive) | |
599 | (let ((str | |
ac1efd51 | 600 | (format "%.1f seconds" |
0d00c61c | 601 | (float-time |
ac1efd51 | 602 | (time-subtract after-init-time before-init-time))))) |
32226619 | 603 | (if (called-interactively-p 'interactive) |
24d6c292 JL |
604 | (message "%s" str) |
605 | str))) | |
606 | ||
c2acf685 SM |
607 | (provide 'time) |
608 | ||
609 | ;;; time.el ends here |