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