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