Fix maintainer address.
[emacs.git] / lisp / time.el
blob9e80c636af1bec9f4e4df60ff7f06ffc846eada7
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 (defgroup display-time nil
32 "Display time and load in mode line of Emacs."
33 :group 'modeline
34 :group 'mail)
37 (defcustom display-time-mode nil
38 "Toggle display of time, load level, and mail flag in mode lines.
39 Setting this variable directly does not take effect;
40 use either \\[customize] or the function `display-time-mode'."
41 :set (lambda (symbol value)
42 (display-time-mode (or value 0)))
43 :initialize 'custom-initialize-default
44 :type 'boolean
45 :group 'display-time
46 :require 'time
47 :version "20.3")
50 (defcustom display-time-mail-file nil
51 "*File name of mail inbox file, for indicating existence of new mail.
52 Non-nil and not a string means don't check for mail. nil means use
53 default, which is system-dependent, and is the same as used by Rmail."
54 :type '(choice (const :tag "Default" nil)
55 (file :format "%v"))
56 :group 'display-time)
58 ;;;###autoload
59 (defcustom display-time-day-and-date nil "\
60 *Non-nil means \\[display-time] should display day and date as well as time."
61 :type 'boolean
62 :group 'display-time)
64 (defvar display-time-timer nil)
66 (defcustom display-time-interval 60
67 "*Seconds between updates of time in the mode line."
68 :type 'integer
69 :group 'display-time)
71 (defcustom display-time-24hr-format nil
72 "*Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23.
73 Nil means 1 <= hh <= 12, and an AM/PM suffix is used."
74 :type 'boolean
75 :group 'display-time)
77 (defvar display-time-string nil)
79 (defcustom display-time-hook nil
80 "*List of functions to be called when the time is updated on the mode line."
81 :type 'hook
82 :group 'display-time)
84 (defvar display-time-server-down-time nil
85 "Time when mail file's file system was recorded to be down.
86 If that file system seems to be up, the value is nil.")
88 ;;;###autoload
89 (defun display-time ()
90 "Enable display of time, load level, and mail flag in mode lines.
91 This display updates automatically every minute.
92 If `display-time-day-and-date' is non-nil, the current day and date
93 are displayed as well.
94 This runs the normal hook `display-time-hook' after each update."
95 (interactive)
96 (display-time-mode 1))
98 ;;;###autoload
99 (defun display-time-mode (arg)
100 "Toggle display of time, load level, and mail flag in mode lines.
101 With a numeric arg, enable this display if arg is positive.
103 When this display is enabled, it updates automatically every minute.
104 If `display-time-day-and-date' is non-nil, the current day and date
105 are displayed as well.
106 This runs the normal hook `display-time-hook' after each update."
107 (interactive "P")
108 (let ((on (if (null arg)
109 (not display-time-timer)
110 (> (prefix-numeric-value arg) 0))))
111 (setq display-time-mode on)
112 (and display-time-timer (cancel-timer display-time-timer))
113 (setq display-time-timer nil)
114 (setq display-time-string "")
115 (or global-mode-string (setq global-mode-string '("")))
116 (if on
117 (progn
118 (or (memq 'display-time-string global-mode-string)
119 (setq global-mode-string
120 (append global-mode-string '(display-time-string))))
121 ;; Set up the time timer.
122 (setq display-time-timer
123 (run-at-time t display-time-interval
124 'display-time-event-handler))
125 ;; Make the time appear right away.
126 (display-time-update)
127 ;; When you get new mail, clear "Mail" from the mode line.
128 (add-hook 'rmail-after-get-new-mail-hook
129 'display-time-event-handler))
130 (remove-hook 'rmail-after-get-new-mail-hook
131 'display-time-event-handler))))
134 (defcustom display-time-format nil
135 "*A string specifying the format for displaying the time in the mode line.
136 See the function `format-time-string' for an explanation of
137 how to write this string. If this is nil, the defaults
138 depend on `display-time-day-and-date' and `display-time-24hr-format'."
139 :type '(choice (const :tag "Default" nil)
140 string)
141 :group 'display-time)
143 (defcustom display-time-string-forms
144 '((if (and (not display-time-format) display-time-day-and-date)
145 (format-time-string "%a %b %e " now)
147 (format-time-string (or display-time-format
148 (if display-time-24hr-format "%H:%M" "%-I:%M%p"))
149 now)
150 load
151 (if mail " Mail" ""))
152 "*A list of expressions governing display of the time in the mode line.
153 For most purposes, you can control the time format using `display-time-format'
154 which is a more standard interface.
156 This expression is a list of expressions that can involve the keywords
157 `load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
158 `seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
159 and `time-zone' all alphabetic strings, and `mail' a true/nil value.
161 For example, the form
163 '((substring year -2) \"/\" month \"/\" day
164 \" \" 24-hours \":\" minutes \":\" seconds
165 (if time-zone \" (\") time-zone (if time-zone \")\")
166 (if mail \" Mail\" \"\"))
168 would give mode line times like `94/12/30 21:07:48 (UTC)'."
169 :type 'sexp
170 :group 'display-time)
172 (defun display-time-event-handler ()
173 (display-time-update)
174 ;; Do redisplay right now, if no input pending.
175 (sit-for 0)
176 (let* ((current (current-time))
177 (timer display-time-timer)
178 ;; Compute the time when this timer will run again, next.
179 (next-time (timer-relative-time
180 (list (aref timer 1) (aref timer 2) (aref timer 3))
181 (* 5 (aref timer 4)) 0)))
182 ;; If the activation time is far in the past,
183 ;; skip executions until we reach a time in the future.
184 ;; This avoids a long pause if Emacs has been suspended for hours.
185 (or (> (nth 0 next-time) (nth 0 current))
186 (and (= (nth 0 next-time) (nth 0 current))
187 (> (nth 1 next-time) (nth 1 current)))
188 (and (= (nth 0 next-time) (nth 0 current))
189 (= (nth 1 next-time) (nth 1 current))
190 (> (nth 2 next-time) (nth 2 current)))
191 (progn
192 (timer-set-time timer (timer-next-integral-multiple-of-time
193 current display-time-interval)
194 display-time-interval)
195 (timer-activate timer)))))
197 ;; Update the display-time info for the mode line
198 ;; but don't redisplay right now. This is used for
199 ;; things like Rmail `g' that want to force an update
200 ;; which can wait for the next redisplay.
201 (defun display-time-update ()
202 (let* ((now (current-time))
203 (time (current-time-string now))
204 (load (condition-case ()
205 (if (zerop (car (load-average))) ""
206 (let ((str (format " %03d" (car (load-average)))))
207 (concat (substring str 0 -2) "." (substring str -2))))
208 (error "")))
209 (mail-spool-file (or display-time-mail-file
210 (getenv "MAIL")
211 (concat rmail-spool-directory
212 (user-login-name))))
213 (mail (and (stringp mail-spool-file)
214 (or (null display-time-server-down-time)
215 ;; If have been down for 20 min, try again.
216 (> (- (nth 1 now) display-time-server-down-time)
217 1200)
218 (and (< (nth 1 now) display-time-server-down-time)
219 (> (- (nth 1 now) display-time-server-down-time)
220 -64336)))
221 (let ((start-time (current-time)))
222 (prog1
223 (display-time-file-nonempty-p mail-spool-file)
224 (if (> (- (nth 1 (current-time)) (nth 1 start-time))
226 ;; Record that mail file is not accessible.
227 (setq display-time-server-down-time
228 (nth 1 (current-time)))
229 ;; Record that mail file is accessible.
230 (setq display-time-server-down-time nil))))))
231 (24-hours (substring time 11 13))
232 (hour (string-to-int 24-hours))
233 (12-hours (int-to-string (1+ (% (+ hour 11) 12))))
234 (am-pm (if (>= hour 12) "pm" "am"))
235 (minutes (substring time 14 16))
236 (seconds (substring time 17 19))
237 (time-zone (car (cdr (current-time-zone now))))
238 (day (substring time 8 10))
239 (year (substring time 20 24))
240 (monthname (substring time 4 7))
241 (month
242 (cdr
243 (assoc
244 monthname
245 '(("Jan" . "1") ("Feb" . "2") ("Mar" . "3") ("Apr" . "4")
246 ("May" . "5") ("Jun" . "6") ("Jul" . "7") ("Aug" . "8")
247 ("Sep" . "9") ("Oct" . "10") ("Nov" . "11") ("Dec" . "12")))))
248 (dayname (substring time 0 3)))
249 (setq display-time-string
250 (mapconcat 'eval display-time-string-forms ""))
251 ;; This is inside the let binding, but we are not going to document
252 ;; what variables are available.
253 (run-hooks 'display-time-hook))
254 (force-mode-line-update))
256 (defun display-time-file-nonempty-p (file)
257 (and (file-exists-p file)
258 (< 0 (nth 7 (file-attributes (file-chase-links file))))))
260 (if display-time-mode
261 (display-time-mode t))
263 (provide 'time)
265 ;;; time.el ends here