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