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