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