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