]> code.delx.au - gnu-emacs/blob - lisp/time.el
Merge from emacs--rel--22
[gnu-emacs] / lisp / time.el
1 ;;; time.el --- display time, load and mail indicator in mode line of Emacs -*-coding: utf-8 -*-
2
3 ;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1996, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
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 3, 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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;; Facilities to display current time/date and a new-mail indicator
28 ;; in the Emacs mode line. The entry point is `display-time'.
29
30 ;; Display time world in a buffer, the entry point is
31 ;; `display-time-world'.
32
33 ;;; Code:
34
35 (defgroup display-time nil
36 "Display time and load in mode line of Emacs."
37 :group 'mode-line
38 :group 'mail)
39
40
41 (defcustom display-time-mail-file nil
42 "*File name of mail inbox file, for indicating existence of new mail.
43 Non-nil and not a string means don't check for mail; nil means use
44 default, which is system-dependent, and is the same as used by Rmail."
45 :type '(choice (const :tag "None" none)
46 (const :tag "Default" nil)
47 (file :format "%v"))
48 :group 'display-time)
49
50 (defcustom display-time-mail-directory nil
51 "*Name of mail inbox directory, for indicating existence of new mail.
52 Any nonempty regular file in the directory is regarded as newly arrived mail.
53 If nil, do not check a directory for arriving mail."
54 :type '(choice (const :tag "None" nil)
55 (directory :format "%v"))
56 :group 'display-time)
57
58 (defcustom display-time-mail-function nil
59 "*Function to call, for indicating existence of new mail.
60 If nil, that means use the default method: check that the file
61 specified by `display-time-mail-file' is nonempty or that the
62 directory `display-time-mail-directory' contains nonempty files."
63 :type '(choice (const :tag "Default" nil)
64 (function))
65 :group 'display-time)
66
67 (defcustom display-time-default-load-average 0
68 "*Which load average value will be shown in the mode line.
69 Almost every system can provide values of load for past 1 minute, past 5 or
70 past 15 minutes. The default is to display 1 minute load average."
71 :type '(choice (const :tag "1 minute load" 0)
72 (const :tag "5 minutes load" 1)
73 (const :tag "15 minutes load" 2)
74 (const :tag "None" nil))
75 :group 'display-time)
76
77 (defvar display-time-load-average nil
78 "Load average currently being shown in mode line.")
79
80 (defcustom display-time-load-average-threshold 0.1
81 "*Load-average values below this value won't be shown in the mode line."
82 :type 'number
83 :group 'display-time)
84
85 ;;;###autoload
86 (defcustom display-time-day-and-date nil "\
87 *Non-nil means \\[display-time] should display day and date as well as time."
88 :type 'boolean
89 :group 'display-time)
90
91 (defvar display-time-timer nil)
92
93 (defcustom display-time-interval 60
94 "*Seconds between updates of time in the mode line."
95 :type 'integer
96 :group 'display-time)
97
98 (defcustom display-time-24hr-format nil
99 "*Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23.
100 A value of nil means 1 <= hh <= 12, and an AM/PM suffix is used."
101 :type 'boolean
102 :group 'display-time)
103
104 (defvar display-time-string nil)
105
106 (defcustom display-time-hook nil
107 "*List of functions to be called when the time is updated on the mode line."
108 :type 'hook
109 :group 'display-time)
110
111 (defvar display-time-server-down-time nil
112 "Time when mail file's file system was recorded to be down.
113 If that file system seems to be up, the value is nil.")
114
115 (defcustom display-time-world-list
116 '(("America/Los_Angeles" "Seattle")
117 ("America/New_York" "New York")
118 ("Europe/London" "London")
119 ("Europe/Paris" "Paris")
120 ("Asia/Calcutta" "Bangalore")
121 ("Asia/Tokyo" "Tokyo"))
122 "Alist specifying time zones and places for `display-time-world'.
123 Each element has the form (TIMEZONE LABEL).
124 TIMEZONE should be a valid argument for `set-time-zone-rule'.
125 LABEL is a string to display to label that zone's time."
126 :group 'display-time
127 :type '(repeat (list string string))
128 :version "23.1")
129
130 (defcustom display-time-world-time-format "%A %m %B %R %Z"
131 "Format of the time displayed, see `format-time-string'."
132 :group 'display-time
133 :type 'string
134 :version "23.1")
135
136 (defcustom display-time-world-buffer-name "*wclock*"
137 "Name of the wclock buffer."
138 :group 'display-time
139 :type 'string
140 :version "23.1")
141
142 (defcustom display-time-world-timer-enable t
143 "If non-nil, a timer will update the world clock."
144 :group 'display-time
145 :type 'boolean
146 :version "23.1")
147
148 (defcustom display-time-world-timer-second 60
149 "Interval in seconds for updating the world clock."
150 :group 'display-time
151 :type 'integer
152 :version "23.1")
153
154 (defvar display-time-world-mode-map
155 (let ((map (make-sparse-keymap)))
156 (define-key map "q" 'kill-this-buffer)
157 map)
158 "Keymap of Display Time World mode")
159
160 ;;;###autoload
161 (defun display-time ()
162 "Enable display of time, load level, and mail flag in mode lines.
163 This display updates automatically every minute.
164 If `display-time-day-and-date' is non-nil, the current day and date
165 are displayed as well.
166 This runs the normal hook `display-time-hook' after each update."
167 (interactive)
168 (display-time-mode 1))
169
170 ;; This business used to be simpler when all mode lines had the same
171 ;; face and the image could just be pbm. Now we try to rely on an xpm
172 ;; image with a transparent background. Otherwise, set the background
173 ;; for pbm.
174
175 (defcustom display-time-mail-face nil
176 "Face to use for `display-time-mail-string'.
177 If `display-time-use-mail-icon' is non-nil, the image's
178 background color is the background of this face. Set this to
179 make the mail indicator stand out on a color display."
180 :group 'mode-line-faces
181 :group 'display-time
182 :version "22.1"
183 :type '(choice (const :tag "None" nil) face))
184
185 (defvar display-time-mail-icon
186 (find-image '((:type xpm :file "letter.xpm" :ascent center)
187 (:type pbm :file "letter.pbm" :ascent center)))
188 "Image specification to offer as the mail indicator on a graphic display.
189 See `display-time-use-mail-icon' and `display-time-mail-face'.")
190
191 ;; Fixme: Default to icon on graphical display?
192 (defcustom display-time-use-mail-icon nil
193 "Non-nil means use an icon as mail indicator on a graphic display.
194 Otherwise use `display-time-mail-string'. The icon may consume less
195 of the mode line. It is specified by `display-time-mail-icon'."
196 :group 'display-time
197 :type 'boolean)
198
199 ;; Fixme: maybe default to the character if we can display Unicode.
200 (defcustom display-time-mail-string "Mail"
201 "String to use as the mail indicator in `display-time-string-forms'.
202 This can use the Unicode letter character if you can display it."
203 :group 'display-time
204 :version "22.1"
205 :type '(choice (const "Mail")
206 ;; Use :tag here because the Lucid menu won't display
207 ;; multibyte text.
208 (const :tag "Unicode letter character" "✉")
209 string))
210
211 (defcustom display-time-format nil
212 "*String specifying format for displaying the time in the mode line.
213 See the function `format-time-string' for an explanation of
214 how to write this string. If this is nil, the defaults
215 depend on `display-time-day-and-date' and `display-time-24hr-format'."
216 :type '(choice (const :tag "Default" nil)
217 string)
218 :group 'display-time)
219
220 (defcustom display-time-string-forms
221 '((if (and (not display-time-format) display-time-day-and-date)
222 (format-time-string "%a %b %e " now)
223 "")
224 (propertize
225 (format-time-string (or display-time-format
226 (if display-time-24hr-format "%H:%M" "%-I:%M%p"))
227 now)
228 'help-echo (format-time-string "%a %b %e, %Y" now))
229 load
230 (if mail
231 ;; Build the string every time to act on customization.
232 ;; :set-after doesn't help for `customize-option'. I think it
233 ;; should.
234 (concat
235 " "
236 (propertize
237 display-time-mail-string
238 'display `(when (and display-time-use-mail-icon
239 (display-graphic-p))
240 ,@display-time-mail-icon
241 ,@(if (and display-time-mail-face
242 (memq (plist-get (cdr display-time-mail-icon)
243 :type)
244 '(pbm xbm)))
245 (let ((bg (face-attribute display-time-mail-face
246 :background)))
247 (if (stringp bg)
248 (list :background bg)))))
249 'face display-time-mail-face
250 'help-echo "You have new mail; mouse-2: Read mail"
251 'mouse-face 'mode-line-highlight
252 'local-map (make-mode-line-mouse-map 'mouse-2
253 read-mail-command)))
254 ""))
255 "*List of expressions governing display of the time in the mode line.
256 For most purposes, you can control the time format using `display-time-format'
257 which is a more standard interface.
258
259 This expression is a list of expressions that can involve the keywords
260 `load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
261 `seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
262 and `time-zone' all alphabetic strings, and `mail' a true/nil value.
263
264 For example, the form
265
266 '((substring year -2) \"/\" month \"/\" day
267 \" \" 24-hours \":\" minutes \":\" seconds
268 (if time-zone \" (\") time-zone (if time-zone \")\")
269 (if mail \" Mail\" \"\"))
270
271 would give mode line times like `94/12/30 21:07:48 (UTC)'."
272 :type 'sexp
273 :group 'display-time)
274
275 (defun display-time-event-handler ()
276 (display-time-update)
277 ;; Do redisplay right now, if no input pending.
278 (sit-for 0)
279 (let* ((current (current-time))
280 (timer display-time-timer)
281 ;; Compute the time when this timer will run again, next.
282 (next-time (timer-relative-time
283 (list (aref timer 1) (aref timer 2) (aref timer 3))
284 (* 5 (aref timer 4)) 0)))
285 ;; If the activation time is far in the past,
286 ;; skip executions until we reach a time in the future.
287 ;; This avoids a long pause if Emacs has been suspended for hours.
288 (or (> (nth 0 next-time) (nth 0 current))
289 (and (= (nth 0 next-time) (nth 0 current))
290 (> (nth 1 next-time) (nth 1 current)))
291 (and (= (nth 0 next-time) (nth 0 current))
292 (= (nth 1 next-time) (nth 1 current))
293 (> (nth 2 next-time) (nth 2 current)))
294 (progn
295 (timer-set-time timer (timer-next-integral-multiple-of-time
296 current display-time-interval)
297 display-time-interval)
298 (timer-activate timer)))))
299
300 (defun display-time-next-load-average ()
301 (interactive)
302 (if (= 3 (setq display-time-load-average (1+ display-time-load-average)))
303 (setq display-time-load-average 0))
304 (display-time-update)
305 (sit-for 0))
306
307 (defun display-time-mail-check-directory ()
308 (let ((mail-files (directory-files display-time-mail-directory t))
309 (size 0))
310 (while (and mail-files (= size 0))
311 ;; Count size of regular files only.
312 (setq size (+ size (or (and (file-regular-p (car mail-files))
313 (nth 7 (file-attributes (car mail-files))))
314 0)))
315 (setq mail-files (cdr mail-files)))
316 (if (> size 0)
317 size
318 nil)))
319
320 (defun display-time-update ()
321 "Update the display-time info for the mode line.
322 However, don't redisplay right now.
323
324 This is used for things like Rmail `g' that want to force an
325 update which can wait for the next redisplay."
326 (let* ((now (current-time))
327 (time (current-time-string now))
328 (load (if (null display-time-load-average)
329 ""
330 (condition-case ()
331 ;; Do not show values less than
332 ;; `display-time-load-average-threshold'.
333 (if (> (* display-time-load-average-threshold 100)
334 (nth display-time-load-average (load-average)))
335 ""
336 ;; The load average number is mysterious, so
337 ;; provide some help.
338 (let ((str (format " %03d"
339 (nth display-time-load-average
340 (load-average)))))
341 (propertize
342 (concat (substring str 0 -2) "." (substring str -2))
343 'local-map (make-mode-line-mouse-map
344 'mouse-2 'display-time-next-load-average)
345 'mouse-face 'mode-line-highlight
346 'help-echo (concat
347 "System load average for past "
348 (if (= 0 display-time-load-average)
349 "1 minute"
350 (if (= 1 display-time-load-average)
351 "5 minutes"
352 "15 minutes"))
353 "; mouse-2: next"))))
354 (error ""))))
355 (mail-spool-file (or display-time-mail-file
356 (getenv "MAIL")
357 (concat rmail-spool-directory
358 (user-login-name))))
359 (mail (or (and display-time-mail-function
360 (funcall display-time-mail-function))
361 (and display-time-mail-directory
362 (display-time-mail-check-directory))
363 (and (stringp mail-spool-file)
364 (or (null display-time-server-down-time)
365 ;; If have been down for 20 min, try again.
366 (> (- (nth 1 now) display-time-server-down-time)
367 1200)
368 (and (< (nth 1 now) display-time-server-down-time)
369 (> (- (nth 1 now)
370 display-time-server-down-time)
371 -64336)))
372 (let ((start-time (current-time)))
373 (prog1
374 (display-time-file-nonempty-p mail-spool-file)
375 (if (> (- (nth 1 (current-time))
376 (nth 1 start-time))
377 20)
378 ;; Record that mail file is not accessible.
379 (setq display-time-server-down-time
380 (nth 1 (current-time)))
381 ;; Record that mail file is accessible.
382 (setq display-time-server-down-time nil)))))))
383 (24-hours (substring time 11 13))
384 (hour (string-to-number 24-hours))
385 (12-hours (int-to-string (1+ (% (+ hour 11) 12))))
386 (am-pm (if (>= hour 12) "pm" "am"))
387 (minutes (substring time 14 16))
388 (seconds (substring time 17 19))
389 (time-zone (car (cdr (current-time-zone now))))
390 (day (substring time 8 10))
391 (year (substring time 20 24))
392 (monthname (substring time 4 7))
393 (month
394 (cdr
395 (assoc
396 monthname
397 '(("Jan" . "1") ("Feb" . "2") ("Mar" . "3") ("Apr" . "4")
398 ("May" . "5") ("Jun" . "6") ("Jul" . "7") ("Aug" . "8")
399 ("Sep" . "9") ("Oct" . "10") ("Nov" . "11") ("Dec" . "12")))))
400 (dayname (substring time 0 3)))
401 (setq display-time-string
402 (mapconcat 'eval display-time-string-forms ""))
403 ;; This is inside the let binding, but we are not going to document
404 ;; what variables are available.
405 (run-hooks 'display-time-hook))
406 (force-mode-line-update))
407
408 (defun display-time-file-nonempty-p (file)
409 (and (file-exists-p file)
410 (< 0 (nth 7 (file-attributes (file-chase-links file))))))
411
412 ;;;###autoload
413 (define-minor-mode display-time-mode
414 "Toggle display of time, load level, and mail flag in mode lines.
415 With a numeric arg, enable this display if arg is positive.
416
417 When this display is enabled, it updates automatically every minute.
418 If `display-time-day-and-date' is non-nil, the current day and date
419 are displayed as well.
420 This runs the normal hook `display-time-hook' after each update."
421 :global t :group 'display-time
422 (and display-time-timer (cancel-timer display-time-timer))
423 (setq display-time-timer nil)
424 (setq display-time-string "")
425 (or global-mode-string (setq global-mode-string '("")))
426 (setq display-time-load-average display-time-default-load-average)
427 (if display-time-mode
428 (progn
429 (or (memq 'display-time-string global-mode-string)
430 (setq global-mode-string
431 (append global-mode-string '(display-time-string))))
432 ;; Set up the time timer.
433 (setq display-time-timer
434 (run-at-time t display-time-interval
435 'display-time-event-handler))
436 ;; Make the time appear right away.
437 (display-time-update)
438 ;; When you get new mail, clear "Mail" from the mode line.
439 (add-hook 'rmail-after-get-new-mail-hook
440 'display-time-event-handler))
441 (remove-hook 'rmail-after-get-new-mail-hook
442 'display-time-event-handler)))
443
444
445 (defun display-time-world-mode ()
446 "Major mode for buffer that displays times in various time zones.
447 See `display-time-world'."
448 (interactive)
449 (kill-all-local-variables)
450 (setq
451 major-mode 'display-time-world-mode
452 mode-name "World clock")
453 (use-local-map display-time-world-mode-map))
454
455 (defun display-time-world-display (alist)
456 "Replace current buffer text with times in various zones, based on ALIST."
457 (let ((inhibit-read-only t)
458 (buffer-undo-list t))
459 (erase-buffer)
460 (let ((max-width 0)
461 (result ()))
462 (unwind-protect
463 (dolist (zone alist)
464 (let* ((label (cadr zone))
465 (width (string-width label)))
466 (set-time-zone-rule (car zone))
467 (setq result
468 (append result
469 (list
470 label width
471 (format-time-string display-time-world-time-format))))
472 (when (> width max-width)
473 (setq max-width width))))
474 (set-time-zone-rule nil))
475 (while result
476 (insert (pop result)
477 (make-string (1+ (- max-width (pop result))) ?\s)
478 (pop result) "\n")))
479 (delete-backward-char 1)))
480
481 ;;;###autoload
482 (defun display-time-world ()
483 "Enable updating display of times in various time zones.
484 `display-time-world-list' specifies the zones.
485 To turn off the world time display, go to that window and type `q'."
486 (interactive)
487 (when (and display-time-world-timer-enable
488 (not (get-buffer display-time-world-buffer-name)))
489 (run-at-time t display-time-world-timer-second 'display-time-world-timer))
490 (with-current-buffer (get-buffer-create display-time-world-buffer-name)
491 (display-time-world-display display-time-world-list))
492 (pop-to-buffer display-time-world-buffer-name)
493 (fit-window-to-buffer)
494 (display-time-world-mode))
495
496 (defun display-time-world-timer ()
497 (if (get-buffer display-time-world-buffer-name)
498 (with-current-buffer (get-buffer display-time-world-buffer-name)
499 (display-time-world-display display-time-world-list))
500 ;; cancel timer
501 (let ((list timer-list))
502 (while list
503 (let ((elt (pop list)))
504 (when (equal (symbol-name (aref elt 5)) "display-time-world-timer")
505 (cancel-timer elt)))))))
506
507 (provide 'time)
508
509 ;;; arch-tag: b9c1623f-b5cb-48e4-b650-482a4d23c5a6
510 ;;; time.el ends here