(mule-version): Change version name to SAKAKI. AOI has already been
[bpt/emacs.git] / lisp / time.el
CommitLineData
4f37b78a 1;;; time.el --- display time, load and mail indicator in mode line of Emacs.
d501f516 2
f6e556e9 3;; Copyright (C) 1985, 86, 87, 93, 94, 96, 2000 Free Software Foundation, Inc.
9673300a 4
58142744
ER
5;; Maintainer: FSF
6
9673300a
RS
7;; This file is part of GNU Emacs.
8
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
e5167999 11;; the Free Software Foundation; either version 2, or (at your option)
9673300a
RS
12;; any later version.
13
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.
18
19;; You should have received a copy of the GNU General Public License
b578f267
EN
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.
9673300a 23
c91c4e6d
ER
24;;; Commentary:
25
b578f267
EN
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'.
c91c4e6d 28
e5167999 29;;; Code:
9673300a 30
a2185576
RS
31(defgroup display-time nil
32 "Display time and load in mode line of Emacs."
33 :group 'modeline
34 :group 'mail)
35
36
76d4fa66 37;;;###autoload
e4191987
RS
38(defcustom display-time-mode nil
39 "Toggle display of time, load level, and mail flag in mode lines.
e5638335
DL
40Setting this variable directly does not take effect;
41use either \\[customize] or the function `display-time-mode'."
e4191987
RS
42 :set (lambda (symbol value)
43 (display-time-mode (or value 0)))
44 :initialize 'custom-initialize-default
45 :type 'boolean
46 :group 'display-time
f9e9ac1d
DN
47 :require 'time
48 :version "20.3")
e4191987
RS
49
50
a2185576 51(defcustom display-time-mail-file nil
29975121 52 "*File name of mail inbox file, for indicating existence of new mail.
7ff97448 53Non-nil and not a string means don't check for mail. nil means use
a2185576 54default, which is system-dependent, and is the same as used by Rmail."
ba611a5a
GM
55 :type '(choice (const :tag "(None)" none)
56 (const :tag "Default" nil)
a2185576
RS
57 (file :format "%v"))
58 :group 'display-time)
29975121 59
73fa8346 60;;;###autoload
a2185576
RS
61(defcustom display-time-day-and-date nil "\
62*Non-nil means \\[display-time] should display day and date as well as time."
63 :type 'boolean
64 :group 'display-time)
7229064d 65
04ec3e39 66(defvar display-time-timer nil)
9673300a 67
a2185576
RS
68(defcustom display-time-interval 60
69 "*Seconds between updates of time in the mode line."
70 :type 'integer
71 :group 'display-time)
9673300a 72
a2185576 73(defcustom display-time-24hr-format nil
151ca170 74 "*Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23.
a2185576
RS
75Nil means 1 <= hh <= 12, and an AM/PM suffix is used."
76 :type 'boolean
77 :group 'display-time)
c2be0af4 78
9673300a
RS
79(defvar display-time-string nil)
80
a2185576 81(defcustom display-time-hook nil
f8533e9e 82 "*List of functions to be called when the time is updated on the mode line."
a2185576
RS
83 :type 'hook
84 :group 'display-time)
9673300a 85
c44565c8
RS
86(defvar display-time-server-down-time nil
87 "Time when mail file's file system was recorded to be down.
88If that file system seems to be up, the value is nil.")
89
7229064d 90;;;###autoload
9673300a 91(defun display-time ()
7fd5b62d
RS
92 "Enable display of time, load level, and mail flag in mode lines.
93This display updates automatically every minute.
9673300a
RS
94If `display-time-day-and-date' is non-nil, the current day and date
95are displayed as well.
7fd5b62d 96This runs the normal hook `display-time-hook' after each update."
9673300a 97 (interactive)
7fd5b62d
RS
98 (display-time-mode 1))
99
100;;;###autoload
101(defun display-time-mode (arg)
102 "Toggle display of time, load level, and mail flag in mode lines.
103With a numeric arg, enable this display if arg is positive.
104
105When this display is enabled, it updates automatically every minute.
106If `display-time-day-and-date' is non-nil, the current day and date
107are displayed as well.
108This runs the normal hook `display-time-hook' after each update."
109 (interactive "P")
110 (let ((on (if (null arg)
111 (not display-time-timer)
112 (> (prefix-numeric-value arg) 0))))
e4191987 113 (setq display-time-mode on)
7fd5b62d
RS
114 (and display-time-timer (cancel-timer display-time-timer))
115 (setq display-time-timer nil)
116 (setq display-time-string "")
117 (or global-mode-string (setq global-mode-string '("")))
118 (if on
119 (progn
120 (or (memq 'display-time-string global-mode-string)
121 (setq global-mode-string
122 (append global-mode-string '(display-time-string))))
123 ;; Set up the time timer.
124 (setq display-time-timer
d694e9df
PE
125 (run-at-time t display-time-interval
126 'display-time-event-handler))
7fd5b62d
RS
127 ;; Make the time appear right away.
128 (display-time-update)
129 ;; When you get new mail, clear "Mail" from the mode line.
130 (add-hook 'rmail-after-get-new-mail-hook
131 'display-time-event-handler))
132 (remove-hook 'rmail-after-get-new-mail-hook
133 'display-time-event-handler))))
134
5941622a
DL
135(defcustom display-time-mail-face 'mode-line
136 "Face to use for `display-time-mail-string'.
137If `display-time-use-mail-icon' is non-nil, the image's background
138colour is the background of this face. Set this to a face other than
139`mode-line' to make the mail indicator stand out on a suitable
140display."
141 :group 'faces
142 :group 'display-time
143 :type 'face)
144
145(defvar display-time-mail-icon
be0505fe 146 (find-image '((:type xbm :file "letter.xbm" :ascent center)))
4f37b78a 147 "Image specification to offer as the mail indicator on a graphic
5941622a
DL
148display. See `display-time-use-mail-icon' and
149`display-time-mail-face'.")
150
151(defcustom display-time-use-mail-icon nil
4f37b78a 152 "Non-nil means use an icon as the mail indicator on a graphic display.
5941622a
DL
153Otherwise use the string \"Mail\". The icon may consume less of the
154mode line. It is specified by `display-time-mail-icon'."
155 :group 'display-time
156 :type 'boolean)
9673300a 157
a2185576 158(defcustom display-time-format nil
3bfbd249
RS
159 "*A string specifying the format for displaying the time in the mode line.
160See the function `format-time-string' for an explanation of
121211d4 161how to write this string. If this is nil, the defaults
a2185576
RS
162depend on `display-time-day-and-date' and `display-time-24hr-format'."
163 :type '(choice (const :tag "Default" nil)
164 string)
165 :group 'display-time)
3bfbd249 166
a2185576 167(defcustom display-time-string-forms
5941622a 168 '((if (and (not display-time-format) display-time-day-and-date)
121211d4
RS
169 (format-time-string "%a %b %e " now)
170 "")
171 (format-time-string (or display-time-format
057cf39e 172 (if display-time-24hr-format "%H:%M" "%-I:%M%p"))
121211d4 173 now)
823d86f3 174 load
5941622a
DL
175 (if mail
176 ;; Build the string every time to act on customization.
177 (concat " "
178 (propertize
179 "Mail"
180 'display `(when (and display-time-use-mail-icon
181 (display-graphic-p))
182 ,@display-time-mail-icon
183 ,@(list :background (face-attribute
184 display-time-mail-face
185 :background)))
186 'help-echo "mouse-2: Read mail"
187 'local-map (make-mode-line-mouse2-map read-mail-command)))
f6e556e9 188 ""))
823d86f3 189 "*A list of expressions governing display of the time in the mode line.
3bfbd249
RS
190For most purposes, you can control the time format using `display-time-format'
191which is a more standard interface.
192
823d86f3
RS
193This expression is a list of expressions that can involve the keywords
194`load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
195`seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
196and `time-zone' all alphabetic strings, and `mail' a true/nil value.
197
198For example, the form
199
200 '((substring year -2) \"/\" month \"/\" day
13b53275 201 \" \" 24-hours \":\" minutes \":\" seconds
823d86f3
RS
202 (if time-zone \" (\") time-zone (if time-zone \")\")
203 (if mail \" Mail\" \"\"))
204
a2185576
RS
205would give mode line times like `94/12/30 21:07:48 (UTC)'."
206 :type 'sexp
207 :group 'display-time)
823d86f3 208
04ec3e39 209(defun display-time-event-handler ()
b36bca9a
KH
210 (display-time-update)
211 ;; Do redisplay right now, if no input pending.
a39a6e40 212 (sit-for 0)
85673d2b
RS
213 (let* ((current (current-time))
214 (timer display-time-timer)
215 ;; Compute the time when this timer will run again, next.
216 (next-time (timer-relative-time
217 (list (aref timer 1) (aref timer 2) (aref timer 3))
218 (* 5 (aref timer 4)) 0)))
d2b7637f 219 ;; If the activation time is far in the past,
a39a6e40
RS
220 ;; skip executions until we reach a time in the future.
221 ;; This avoids a long pause if Emacs has been suspended for hours.
d2b7637f
RS
222 (or (> (nth 0 next-time) (nth 0 current))
223 (and (= (nth 0 next-time) (nth 0 current))
224 (> (nth 1 next-time) (nth 1 current)))
225 (and (= (nth 0 next-time) (nth 0 current))
226 (= (nth 1 next-time) (nth 1 current))
227 (> (nth 2 next-time) (nth 2 current)))
a39a6e40 228 (progn
a39a6e40 229 (timer-set-time timer (timer-next-integral-multiple-of-time
d2b7637f
RS
230 current display-time-interval)
231 display-time-interval)
a39a6e40 232 (timer-activate timer)))))
b36bca9a
KH
233
234;; Update the display-time info for the mode line
235;; but don't redisplay right now. This is used for
236;; things like Rmail `g' that want to force an update
237;; which can wait for the next redisplay.
238(defun display-time-update ()
e974f599
KH
239 (let* ((now (current-time))
240 (time (current-time-string now))
823d86f3
RS
241 (load (condition-case ()
242 (if (zerop (car (load-average))) ""
4f37b78a 243 ;; The load average number is mysterious, so
f6e556e9 244 ;; propvide some help.
823d86f3 245 (let ((str (format " %03d" (car (load-average)))))
f6e556e9
DL
246 (propertize
247 (concat (substring str 0 -2) "." (substring str -2))
fc09f4ba 248 'help-echo "System load average")))
823d86f3
RS
249 (error "")))
250 (mail-spool-file (or display-time-mail-file
251 (getenv "MAIL")
252 (concat rmail-spool-directory
151ca170 253 (user-login-name))))
7ff97448
RS
254 (mail (and (stringp mail-spool-file)
255 (or (null display-time-server-down-time)
151ca170 256 ;; If have been down for 20 min, try again.
5d18c0b3
AI
257 (> (- (nth 1 now) display-time-server-down-time)
258 1200)
259 (and (< (nth 1 now) display-time-server-down-time)
260 (> (- (nth 1 now) display-time-server-down-time)
261 -64336)))
151ca170
KH
262 (let ((start-time (current-time)))
263 (prog1
264 (display-time-file-nonempty-p mail-spool-file)
265 (if (> (- (nth 1 (current-time)) (nth 1 start-time))
266 20)
267 ;; Record that mail file is not accessible.
f8533e9e 268 (setq display-time-server-down-time
151ca170
KH
269 (nth 1 (current-time)))
270 ;; Record that mail file is accessible.
271 (setq display-time-server-down-time nil))))))
823d86f3
RS
272 (24-hours (substring time 11 13))
273 (hour (string-to-int 24-hours))
151ca170 274 (12-hours (int-to-string (1+ (% (+ hour 11) 12))))
5b0841fd 275 (am-pm (if (>= hour 12) "pm" "am"))
823d86f3
RS
276 (minutes (substring time 14 16))
277 (seconds (substring time 17 19))
e974f599 278 (time-zone (car (cdr (current-time-zone now))))
823d86f3
RS
279 (day (substring time 8 10))
280 (year (substring time 20 24))
281 (monthname (substring time 4 7))
282 (month
283 (cdr
284 (assoc
285 monthname
286 '(("Jan" . "1") ("Feb" . "2") ("Mar" . "3") ("Apr" . "4")
287 ("May" . "5") ("Jun" . "6") ("Jul" . "7") ("Aug" . "8")
288 ("Sep" . "9") ("Oct" . "10") ("Nov" . "11") ("Dec" . "12")))))
289 (dayname (substring time 0 3)))
9673300a 290 (setq display-time-string
83656a19
RS
291 (mapconcat 'eval display-time-string-forms ""))
292 ;; This is inside the let binding, but we are not going to document
293 ;; what variables are available.
294 (run-hooks 'display-time-hook))
b36bca9a 295 (force-mode-line-update))
7229064d
RM
296
297(defun display-time-file-nonempty-p (file)
c44565c8
RS
298 (and (file-exists-p file)
299 (< 0 (nth 7 (file-attributes (file-chase-links file))))))
d501f516 300
e4191987
RS
301(if display-time-mode
302 (display-time-mode t))
303
896546cd
RS
304(provide 'time)
305
d501f516 306;;; time.el ends here