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