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