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