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