(command_loop_1): Cancel echoing etc.
[emacs.git] / lisp / time.el
blob59dc075cfd89692410ae970fb25713ba1c92fc5b
1 ;;; time.el --- display time and load in mode line of Emacs.
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 1996 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
24 ;;; Commentary:
26 ;; Facilities to display current time/date and a new-mail indicator
27 ;; in the Emacs mode line. The single entry point is `display-time'.
29 ;;; Code:
31 (defvar display-time-mail-file nil
32 "*File name of mail inbox file, for indicating existence of new mail.
33 Non-nil and not a string means don't check for mail. nil means use
34 default, which is system-dependent, and is the same as used by Rmail.")
36 ;;;###autoload
37 (defvar display-time-day-and-date nil "\
38 *Non-nil means \\[display-time] should display day and date as well as time.")
40 (defvar display-time-timer nil)
42 (defvar display-time-interval 60
43 "*Seconds between updates of time in the mode line.")
45 (defvar display-time-24hr-format nil
46 "*Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23.
47 Nil means 1 <= hh <= 12, and an AM/PM suffix is used.")
49 (defvar display-time-string nil)
51 (defvar display-time-hook nil
52 "* List of functions to be called when the time is updated on the mode line.")
54 (defvar display-time-server-down-time nil
55 "Time when mail file's file system was recorded to be down.
56 If that file system seems to be up, the value is nil.")
58 ;;;###autoload
59 (defun display-time ()
60 "Display current time, load level, and mail flag in mode line of each buffer.
61 Updates automatically every minute.
62 If `display-time-day-and-date' is non-nil, the current day and date
63 are displayed as well.
64 After each update, `display-time-hook' is run with `run-hooks'."
65 (interactive)
66 (setq display-time-string "")
67 (or global-mode-string (setq global-mode-string '("")))
68 (or (memq 'display-time-string global-mode-string)
69 (setq global-mode-string
70 (append global-mode-string '(display-time-string))))
71 ;; Setup the time timer.
72 (and display-time-timer (cancel-timer display-time-timer))
73 (setq display-time-timer
74 ;; Start timer at the beginning of the next minute.
75 (run-at-time (apply 'encode-time 60 (cdr (decode-time)))
76 display-time-interval 'display-time-event-handler))
77 ;; Make the time appear right away.
78 (display-time-update)
79 ;; When you get new mail, clear "Mail" from the mode line.
80 (add-hook 'rmail-after-get-new-mail-hook 'display-time-event-handler))
82 (defvar display-time-string-forms
83 '((if display-time-day-and-date
84 (format "%s %s %s " dayname monthname day)
85 "")
86 (format "%s:%s%s"
87 (if display-time-24hr-format 24-hours 12-hours)
88 minutes
89 (if display-time-24hr-format "" am-pm))
90 load
91 (if mail " Mail" ""))
92 "*A list of expressions governing display of the time in the mode line.
93 This expression is a list of expressions that can involve the keywords
94 `load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
95 `seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
96 and `time-zone' all alphabetic strings, and `mail' a true/nil value.
98 For example, the form
100 '((substring year -2) \"/\" month \"/\" day
101 \" \" 24-hours \":\" minutes \":\" seconds
102 (if time-zone \" (\") time-zone (if time-zone \")\")
103 (if mail \" Mail\" \"\"))
105 would give mode line times like `94/12/30 21:07:48 (UTC)'.")
107 (defun display-time-event-handler ()
108 (display-time-update)
109 ;; Do redisplay right now, if no input pending.
110 (sit-for 0))
112 ;; Update the display-time info for the mode line
113 ;; but don't redisplay right now. This is used for
114 ;; things like Rmail `g' that want to force an update
115 ;; which can wait for the next redisplay.
116 (defun display-time-update ()
117 (let* ((now (current-time))
118 (time (current-time-string now))
119 (load (condition-case ()
120 (if (zerop (car (load-average))) ""
121 (let ((str (format " %03d" (car (load-average)))))
122 (concat (substring str 0 -2) "." (substring str -2))))
123 (error "")))
124 (mail-spool-file (or display-time-mail-file
125 (getenv "MAIL")
126 (concat rmail-spool-directory
127 (user-login-name))))
128 (mail (and (stringp mail-spool-file)
129 (or (null display-time-server-down-time)
130 ;; If have been down for 20 min, try again.
131 (> (- (nth 1 (current-time))
132 display-time-server-down-time)
133 1200))
134 (let ((start-time (current-time)))
135 (prog1
136 (display-time-file-nonempty-p mail-spool-file)
137 (if (> (- (nth 1 (current-time)) (nth 1 start-time))
139 ;; Record that mail file is not accessible.
140 (setq display-time-server-down-time
141 (nth 1 (current-time)))
142 ;; Record that mail file is accessible.
143 (setq display-time-server-down-time nil))))))
144 (24-hours (substring time 11 13))
145 (hour (string-to-int 24-hours))
146 (12-hours (int-to-string (1+ (% (+ hour 11) 12))))
147 (am-pm (if (>= hour 12) "pm" "am"))
148 (minutes (substring time 14 16))
149 (seconds (substring time 17 19))
150 (time-zone (car (cdr (current-time-zone now))))
151 (day (substring time 8 10))
152 (year (substring time 20 24))
153 (monthname (substring time 4 7))
154 (month
155 (cdr
156 (assoc
157 monthname
158 '(("Jan" . "1") ("Feb" . "2") ("Mar" . "3") ("Apr" . "4")
159 ("May" . "5") ("Jun" . "6") ("Jul" . "7") ("Aug" . "8")
160 ("Sep" . "9") ("Oct" . "10") ("Nov" . "11") ("Dec" . "12")))))
161 (dayname (substring time 0 3)))
162 (setq display-time-string
163 (mapconcat 'eval display-time-string-forms ""))
164 ;; This is inside the let binding, but we are not going to document
165 ;; what variables are available.
166 (run-hooks 'display-time-hook))
167 (force-mode-line-update))
169 (defun display-time-file-nonempty-p (file)
170 (and (file-exists-p file)
171 (< 0 (nth 7 (file-attributes (file-chase-links file))))))
173 ;;; time.el ends here