1 ;;; time.el --- display time and load in mode line of Emacs.
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 1996 Free Software Foundation, Inc.
7 ;; This file is part of GNU Emacs.
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)
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.
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.
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'.
31 (defgroup display-time nil
32 "Display time and load in mode line of Emacs."
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
"Default" nil
)
46 (defcustom display-time-day-and-date nil
"\
47 *Non-nil means \\[display-time] should display day and date as well as time."
51 (defvar display-time-timer nil
)
53 (defcustom display-time-interval
60
54 "*Seconds between updates of time in the mode line."
58 (defcustom display-time-24hr-format nil
59 "*Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23.
60 Nil means 1 <= hh <= 12, and an AM/PM suffix is used."
64 (defvar display-time-string nil
)
66 (defcustom display-time-hook nil
67 "*List of functions to be called when the time is updated on the mode line."
71 (defvar display-time-server-down-time nil
72 "Time when mail file's file system was recorded to be down.
73 If that file system seems to be up, the value is nil.")
76 (defun display-time ()
77 "Enable display of time, load level, and mail flag in mode lines.
78 This display updates automatically every minute.
79 If `display-time-day-and-date' is non-nil, the current day and date
80 are displayed as well.
81 This runs the normal hook `display-time-hook' after each update."
83 (display-time-mode 1))
86 (defun display-time-mode (arg)
87 "Toggle display of time, load level, and mail flag in mode lines.
88 With a numeric arg, enable this display if arg is positive.
90 When this display is enabled, it updates automatically every minute.
91 If `display-time-day-and-date' is non-nil, the current day and date
92 are displayed as well.
93 This runs the normal hook `display-time-hook' after each update."
95 (let ((on (if (null arg
)
96 (not display-time-timer
)
97 (> (prefix-numeric-value arg
) 0))))
98 (and display-time-timer
(cancel-timer display-time-timer
))
99 (setq display-time-timer nil
)
100 (setq display-time-string
"")
101 (or global-mode-string
(setq global-mode-string
'("")))
104 (or (memq 'display-time-string global-mode-string
)
105 (setq global-mode-string
106 (append global-mode-string
'(display-time-string))))
107 ;; Set up the time timer.
108 (setq display-time-timer
109 (run-at-time t display-time-interval
110 'display-time-event-handler
))
111 ;; Make the time appear right away.
112 (display-time-update)
113 ;; When you get new mail, clear "Mail" from the mode line.
114 (add-hook 'rmail-after-get-new-mail-hook
115 'display-time-event-handler
))
116 (remove-hook 'rmail-after-get-new-mail-hook
117 'display-time-event-handler
))))
120 (defcustom display-time-format nil
121 "*A string specifying the format for displaying the time in the mode line.
122 See the function `format-time-string' for an explanation of
123 how to write this string. If this is nil, the defaults
124 depend on `display-time-day-and-date' and `display-time-24hr-format'."
125 :type
'(choice (const :tag
"Default" nil
)
127 :group
'display-time
)
129 (defcustom display-time-string-forms
130 '((if (and (not display-time-format
) display-time-day-and-date
)
131 (format-time-string "%a %b %e " now
)
133 (format-time-string (or display-time-format
134 (if display-time-24hr-format
"%H:%M" "%-I:%M%p"))
137 (if mail
" Mail" ""))
138 "*A list of expressions governing display of the time in the mode line.
139 For most purposes, you can control the time format using `display-time-format'
140 which is a more standard interface.
142 This expression is a list of expressions that can involve the keywords
143 `load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
144 `seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
145 and `time-zone' all alphabetic strings, and `mail' a true/nil value.
147 For example, the form
149 '((substring year -2) \"/\" month \"/\" day
150 \" \" 24-hours \":\" minutes \":\" seconds
151 (if time-zone \" (\") time-zone (if time-zone \")\")
152 (if mail \" Mail\" \"\"))
154 would give mode line times like `94/12/30 21:07:48 (UTC)'."
156 :group
'display-time
)
158 (defun display-time-event-handler ()
159 (display-time-update)
160 ;; Do redisplay right now, if no input pending.
162 (let* ((current (current-time))
163 (timer display-time-timer
)
164 ;; Compute the time when this timer will run again, next.
165 (next-time (timer-relative-time
166 (list (aref timer
1) (aref timer
2) (aref timer
3))
167 (* 5 (aref timer
4)) 0)))
168 ;; If the activation time is far in the past,
169 ;; skip executions until we reach a time in the future.
170 ;; This avoids a long pause if Emacs has been suspended for hours.
171 (or (> (nth 0 next-time
) (nth 0 current
))
172 (and (= (nth 0 next-time
) (nth 0 current
))
173 (> (nth 1 next-time
) (nth 1 current
)))
174 (and (= (nth 0 next-time
) (nth 0 current
))
175 (= (nth 1 next-time
) (nth 1 current
))
176 (> (nth 2 next-time
) (nth 2 current
)))
178 (timer-set-time timer
(timer-next-integral-multiple-of-time
179 current display-time-interval
)
180 display-time-interval
)
181 (timer-activate timer
)))))
183 ;; Update the display-time info for the mode line
184 ;; but don't redisplay right now. This is used for
185 ;; things like Rmail `g' that want to force an update
186 ;; which can wait for the next redisplay.
187 (defun display-time-update ()
188 (let* ((now (current-time))
189 (time (current-time-string now
))
190 (load (condition-case ()
191 (if (zerop (car (load-average))) ""
192 (let ((str (format " %03d" (car (load-average)))))
193 (concat (substring str
0 -
2) "." (substring str -
2))))
195 (mail-spool-file (or display-time-mail-file
197 (concat rmail-spool-directory
199 (mail (and (stringp mail-spool-file
)
200 (or (null display-time-server-down-time
)
201 ;; If have been down for 20 min, try again.
202 (> (- (nth 1 (current-time))
203 display-time-server-down-time
)
205 (let ((start-time (current-time)))
207 (display-time-file-nonempty-p mail-spool-file
)
208 (if (> (- (nth 1 (current-time)) (nth 1 start-time
))
210 ;; Record that mail file is not accessible.
211 (setq display-time-server-down-time
212 (nth 1 (current-time)))
213 ;; Record that mail file is accessible.
214 (setq display-time-server-down-time nil
))))))
215 (24-hours (substring time
11 13))
216 (hour (string-to-int 24-hours
))
217 (12-hours (int-to-string (1+ (%
(+ hour
11) 12))))
218 (am-pm (if (>= hour
12) "pm" "am"))
219 (minutes (substring time
14 16))
220 (seconds (substring time
17 19))
221 (time-zone (car (cdr (current-time-zone now
))))
222 (day (substring time
8 10))
223 (year (substring time
20 24))
224 (monthname (substring time
4 7))
229 '(("Jan" .
"1") ("Feb" .
"2") ("Mar" .
"3") ("Apr" .
"4")
230 ("May" .
"5") ("Jun" .
"6") ("Jul" .
"7") ("Aug" .
"8")
231 ("Sep" .
"9") ("Oct" .
"10") ("Nov" .
"11") ("Dec" .
"12")))))
232 (dayname (substring time
0 3)))
233 (setq display-time-string
234 (mapconcat 'eval display-time-string-forms
""))
235 ;; This is inside the let binding, but we are not going to document
236 ;; what variables are available.
237 (run-hooks 'display-time-hook
))
238 (force-mode-line-update))
240 (defun display-time-file-nonempty-p (file)
241 (and (file-exists-p file
)
242 (< 0 (nth 7 (file-attributes (file-chase-links file
))))))
246 ;;; time.el ends here