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