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