0
|
1 ;;; time.el --- display time and load in mode line of Emacs.
|
|
2
|
2
|
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 1996 Free Software Foundation, Inc.
|
0
|
4
|
70
|
5 ;; Maintainer: FSF
|
0
|
6
|
|
7 ;; This file is part of XEmacs.
|
|
8
|
|
9 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
10 ;; 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 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
17 ;; General Public License for more details.
|
|
18
|
|
19 ;; You should have received a copy of the GNU General Public License
|
|
20 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
2
|
21 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
22 ;; 02111-1307, USA.
|
0
|
23
|
2
|
24 ;;; Synched up with: Not synched with FSF.
|
0
|
25
|
|
26 ;;; Commentary:
|
|
27
|
2
|
28 ;; Facilities to display current time/date and a new-mail indicator
|
|
29 ;; in the Emacs mode line. The single entry point is `display-time'.
|
0
|
30
|
2
|
31 ;; See also reportmail.el.
|
|
32 ;; This uses the XEmacs timeout-event mechanism, via a version
|
|
33 ;; of Kyle Jones' itimer package.
|
0
|
34
|
|
35 ;;; Code:
|
|
36
|
|
37 (require 'itimer)
|
|
38
|
70
|
39 (defvar display-time-mail-file nil
|
0
|
40 "*File name of mail inbox file, for indicating existence of new mail.
|
|
41 Non-nil and not a string means don't check for mail. nil means use
|
70
|
42 default, which is system-dependent, and is the same as used by Rmail.")
|
0
|
43
|
|
44 ;;;###autoload
|
70
|
45 (defvar display-time-day-and-date nil "\
|
|
46 *Non-nil means \\[display-time] should display day and date as well as time.")
|
0
|
47
|
70
|
48 (defvar display-time-interval 60
|
|
49 "*Seconds between updates of time in the mode line.")
|
0
|
50
|
70
|
51 (defvar display-time-24hr-format nil
|
0
|
52 "*Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23.
|
70
|
53 Nil means 1 <= hh <= 12, and an AM/PM suffix is used.")
|
0
|
54
|
70
|
55 (defvar display-time-echo-area nil
|
|
56 "*If non-nil, display-time will use the echo area instead of the mode line.")
|
0
|
57
|
|
58 (defvar display-time-string nil)
|
|
59
|
70
|
60 (defvar display-time-hook nil
|
|
61 "*List of functions to be called when the time is updated on the mode line.")
|
0
|
62
|
|
63 (defvar display-time-server-down-time nil
|
|
64 "Time when mail file's file system was recorded to be down.
|
|
65 If that file system seems to be up, the value is nil.")
|
|
66
|
|
67 ;;;###autoload
|
|
68 (defun display-time ()
|
|
69 "Display current time, load level, and mail flag in mode line of each buffer.
|
|
70 Updates automatically every minute.
|
|
71 If `display-time-day-and-date' is non-nil, the current day and date
|
|
72 are displayed as well.
|
|
73 After each update, `display-time-hook' is run with `run-hooks'.
|
|
74 If `display-time-echo-area' is non-nil, the time is displayed in the
|
|
75 echo area instead of in the mode-line."
|
|
76 (interactive)
|
|
77 ;; if the "display-time" itimer already exists, nuke it first.
|
|
78 (let ((old (get-itimer "display-time")))
|
|
79 (if old (delete-itimer old)))
|
|
80 ;; If we're not displaying the time in the echo area
|
|
81 ;; and the global mode string does not have a non-nil value
|
|
82 ;; then initialize the global mode string's value.
|
|
83 (or display-time-echo-area
|
|
84 global-mode-string
|
|
85 (setq global-mode-string '("")))
|
|
86 ;; If we're not displaying the time in the echo area
|
70
|
87 ;; and our display variable is not part of the global-mode-string list
|
|
88 ;; the we add our variable to the list. This will make the time
|
0
|
89 ;; appear on the modeline.
|
|
90 (or display-time-echo-area
|
70
|
91 (memq 'display-time-string global-mode-string)
|
0
|
92 (setq global-mode-string
|
70
|
93 (append global-mode-string '(display-time-string))))
|
0
|
94 ;; Display the time initially...
|
|
95 (display-time-function)
|
|
96 ;; ... and start an itimer to do it automatically thereafter.
|
|
97 ;;
|
|
98 ;; If we wanted to be really clever about this, we could have the itimer
|
|
99 ;; not be automatically restarted, but have it re-add itself each time.
|
|
100 ;; Then we could look at (current-time) and arrange for the itimer to
|
|
101 ;; wake up exactly at the minute boundary. But that's just a little
|
|
102 ;; more work than it's worth...
|
|
103 (start-itimer "display-time" 'display-time-function
|
|
104 display-time-interval display-time-interval))
|
|
105
|
104
|
106 (defvar display-time-show-load t)
|
|
107
|
|
108 (defvar display-time-show-icons-maybe t
|
|
109 "Use icons to indicate the mail status if we're running under X and
|
|
110 XEmacs was compiled with xpm support")
|
|
111
|
|
112 (defun display-time-get-icons-dir ()
|
|
113 (let ((path load-path)
|
|
114 dir elem)
|
|
115 (while (setq elem (pop path))
|
|
116 (setq dir (concat (directory-file-name elem) "/../etc/time/"))
|
|
117 (if (file-directory-p dir) (setq path nil)
|
|
118 nil))
|
|
119 dir))
|
|
120
|
|
121 (defvar display-time-icons-dir (display-time-get-icons-dir))
|
|
122
|
|
123 (defvar display-time-mail-sign
|
|
124 (progn
|
|
125 (let* ((file (concat display-time-icons-dir "letter.xpm"))
|
|
126 (glyph (if (featurep 'xpm) (make-glyph file) nil))
|
|
127 (display-time-mail-ext (detach-extent (make-extent 1 1))))
|
|
128 (if (and (featurep 'x) glyph
|
|
129 (file-exists-p file))
|
|
130 (cons display-time-mail-ext glyph)
|
|
131 " Mail")))
|
|
132 "A variable holding a string or a cons cell (ext . glyph) which gives
|
|
133 an indicator for unread mail. The default displays a xpm-file (a yellow letter)
|
|
134 if (feturep 'xpm) and (featurep 'x) are both t, a string \" Mail\" otherwise")
|
|
135
|
|
136 (defvar display-time-no-mail-sign
|
|
137 (progn
|
|
138 (let* ((file (concat display-time-icons-dir "no-letter.xpm"))
|
|
139 (glyph (if (featurep 'xpm) (make-glyph file) nil))
|
|
140 (display-time-mail-ext (detach-extent (make-extent 1 1))))
|
|
141 (if (and (featurep 'x) glyph
|
|
142 (file-exists-p file))
|
|
143 (cons display-time-mail-ext glyph)
|
|
144 "")))
|
|
145 "A variable holding a string or a cons cell (ext . glyph) which gives
|
|
146 an indicator for `no mail'. The default displays a xpm-file
|
|
147 if (feturep 'xpm) and (featurep 'x) are both t, and nothing otherwise")
|
|
148
|
|
149
|
0
|
150 (defvar display-time-string-forms
|
|
151 '((if display-time-day-and-date
|
|
152 (format "%s %s %s " dayname monthname day)
|
|
153 "")
|
|
154 (format "%s:%s%s"
|
|
155 (if display-time-24hr-format 24-hours 12-hours)
|
|
156 minutes
|
|
157 (if display-time-24hr-format "" am-pm))
|
104
|
158 (if display-time-show-load load)
|
|
159 (if (and (not display-time-show-icons-maybe) mail) " Mail" ""))
|
|
160 "*A list of expressions governing display of the time in the mode line.
|
0
|
161 This expression is a list of expressions that can involve the keywords
|
|
162 `load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
|
|
163 `seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
|
104
|
164 and `time-zone' all alphabetic strings and `mail' a true/nil string value.
|
|
165 Beware: if display-time-show-icons-maybe is non-nil, the `mail' spec is also
|
|
166 evaluated after this form and depending on it's result display-time-mail-sign
|
|
167 or display-time-no-mail-sign is appended to the modeline string.
|
|
168 This was made so you can also use xpm-files as mail indicator.
|
0
|
169
|
|
170 For example, the form
|
|
171
|
|
172 '((substring year -2) \"/\" month \"/\" day
|
|
173 \" \" 24-hours \":\" minutes \":\" seconds
|
104
|
174 (if time-zone \" (\") time-zone (if time-zone \")\"))
|
0
|
175
|
|
176 would give mode line times like `94/12/30 21:07:48 (UTC)'.")
|
|
177
|
|
178 (defun display-time-function ()
|
|
179 (let* ((now (current-time))
|
|
180 (time (current-time-string now))
|
|
181 (load (condition-case ()
|
|
182 (if (zerop (car (load-average))) ""
|
|
183 (let ((str (format " %03d" (car (load-average)))))
|
|
184 (concat (substring str 0 -2) "." (substring str -2))))
|
|
185 (error "")))
|
|
186 (mail-spool-file (or display-time-mail-file
|
|
187 (getenv "MAIL")
|
|
188 (concat rmail-spool-directory
|
|
189 (user-login-name))))
|
|
190 (mail (and (stringp mail-spool-file)
|
|
191 (or (null display-time-server-down-time)
|
|
192 ;; If have been down for 20 min, try again.
|
|
193 (> (- (nth 1 (current-time))
|
|
194 display-time-server-down-time)
|
|
195 1200))
|
|
196 (let ((start-time (current-time)))
|
|
197 (prog1
|
|
198 (display-time-file-nonempty-p mail-spool-file)
|
|
199 (if (> (- (nth 1 (current-time)) (nth 1 start-time))
|
|
200 20)
|
|
201 ;; Record that mail file is not accessible.
|
|
202 (setq display-time-server-down-time
|
|
203 (nth 1 (current-time)))
|
|
204 ;; Record that mail file is accessible.
|
|
205 (setq display-time-server-down-time nil))))))
|
|
206 (24-hours (substring time 11 13))
|
|
207 (hour (string-to-int 24-hours))
|
|
208 (12-hours (int-to-string (1+ (% (+ hour 11) 12))))
|
|
209 (am-pm (if (>= hour 12) "pm" "am"))
|
|
210 (minutes (substring time 14 16))
|
|
211 (seconds (substring time 17 19))
|
|
212 (time-zone (car (cdr (current-time-zone now))))
|
|
213 (day (substring time 8 10))
|
|
214 (year (substring time 20 24))
|
|
215 (monthname (substring time 4 7))
|
|
216 (month
|
|
217 (cdr
|
|
218 (assoc
|
|
219 monthname
|
|
220 '(("Jan" . "1") ("Feb" . "2") ("Mar" . "3") ("Apr" . "4")
|
|
221 ("May" . "5") ("Jun" . "6") ("Jul" . "7") ("Aug" . "8")
|
|
222 ("Sep" . "9") ("Oct" . "10") ("Nov" . "11") ("Dec" . "12")))))
|
|
223 (dayname (substring time 0 3)))
|
|
224 (setq display-time-string
|
70
|
225 (mapconcat 'eval display-time-string-forms ""))
|
104
|
226 (if (and mail display-time-show-icons-maybe)
|
|
227 (setq display-time-string
|
|
228 (list display-time-string display-time-mail-sign))
|
|
229 (if display-time-show-icons-maybe
|
|
230 (setq display-time-string
|
|
231 (list display-time-string display-time-no-mail-sign))))
|
0
|
232 ;; This is inside the let binding, but we are not going to document
|
|
233 ;; what variables are available.
|
|
234 (run-hooks 'display-time-hook))
|
|
235 (if display-time-echo-area
|
|
236 (or (> (minibuffer-depth) 0)
|
|
237 ;; don't stomp echo-area-buffer if reading from minibuffer now.
|
|
238 (save-excursion
|
|
239 (save-window-excursion
|
|
240 (select-window (minibuffer-window))
|
|
241 (erase-buffer)
|
70
|
242 (indent-to (- (screen-width) (length display-time-string) 1))
|
0
|
243 (insert display-time-string)
|
|
244 (message (buffer-string)))))
|
|
245 (force-mode-line-update)
|
|
246 ;; Do redisplay right now, if no input pending.
|
|
247 (sit-for 0)))
|
|
248
|
|
249 (defun display-time-file-nonempty-p (file)
|
70
|
250 (and (file-exists-p file)
|
|
251 (< 0 (nth 7 (file-attributes (file-chase-links file))))))
|
0
|
252
|
|
253 (provide 'time)
|
|
254
|
|
255 ;;; time.el ends here
|