Commit | Line | Data |
---|---|---|
d501f516 ER |
1 | ;;; time.el --- display time and load in mode line of Emacs. |
2 | ||
9596811a | 3 | ;; Copyright (C) 1985, 86, 87, 93, 94, 1996 Free Software Foundation, Inc. |
9673300a | 4 | |
58142744 ER |
5 | ;; Maintainer: FSF |
6 | ||
9673300a RS |
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 | |
e5167999 | 11 | ;; the Free Software Foundation; either version 2, or (at your option) |
9673300a RS |
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 | |
b578f267 EN |
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. | |
9673300a | 23 | |
c91c4e6d ER |
24 | ;;; Commentary: |
25 | ||
b578f267 EN |
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'. | |
c91c4e6d | 28 | |
e5167999 | 29 | ;;; Code: |
9673300a | 30 | |
a2185576 RS |
31 | (defgroup display-time nil |
32 | "Display time and load in mode line of Emacs." | |
33 | :group 'modeline | |
34 | :group 'mail) | |
35 | ||
36 | ||
37 | (defcustom display-time-mail-file nil | |
29975121 | 38 | "*File name of mail inbox file, for indicating existence of new mail. |
7ff97448 | 39 | Non-nil and not a string means don't check for mail. nil means use |
a2185576 RS |
40 | default, which is system-dependent, and is the same as used by Rmail." |
41 | :type '(choice (const :tag "Default" nil) | |
42 | (file :format "%v")) | |
43 | :group 'display-time) | |
29975121 | 44 | |
73fa8346 | 45 | ;;;###autoload |
a2185576 RS |
46 | (defcustom display-time-day-and-date nil "\ |
47 | *Non-nil means \\[display-time] should display day and date as well as time." | |
48 | :type 'boolean | |
49 | :group 'display-time) | |
7229064d | 50 | |
04ec3e39 | 51 | (defvar display-time-timer nil) |
9673300a | 52 | |
a2185576 RS |
53 | (defcustom display-time-interval 60 |
54 | "*Seconds between updates of time in the mode line." | |
55 | :type 'integer | |
56 | :group 'display-time) | |
9673300a | 57 | |
a2185576 | 58 | (defcustom display-time-24hr-format nil |
151ca170 | 59 | "*Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23. |
a2185576 RS |
60 | Nil means 1 <= hh <= 12, and an AM/PM suffix is used." |
61 | :type 'boolean | |
62 | :group 'display-time) | |
c2be0af4 | 63 | |
9673300a RS |
64 | (defvar display-time-string nil) |
65 | ||
a2185576 | 66 | (defcustom display-time-hook nil |
f8533e9e | 67 | "*List of functions to be called when the time is updated on the mode line." |
a2185576 RS |
68 | :type 'hook |
69 | :group 'display-time) | |
9673300a | 70 | |
c44565c8 RS |
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.") | |
74 | ||
7229064d | 75 | ;;;###autoload |
9673300a | 76 | (defun display-time () |
7fd5b62d RS |
77 | "Enable display of time, load level, and mail flag in mode lines. |
78 | This display updates automatically every minute. | |
9673300a RS |
79 | If `display-time-day-and-date' is non-nil, the current day and date |
80 | are displayed as well. | |
7fd5b62d | 81 | This runs the normal hook `display-time-hook' after each update." |
9673300a | 82 | (interactive) |
7fd5b62d RS |
83 | (display-time-mode 1)) |
84 | ||
85 | ;;;###autoload | |
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. | |
89 | ||
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." | |
94 | (interactive "P") | |
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 '(""))) | |
102 | (if on | |
103 | (progn | |
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 | |
d694e9df PE |
109 | (run-at-time t display-time-interval |
110 | 'display-time-event-handler)) | |
7fd5b62d RS |
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)))) | |
118 | ||
9673300a | 119 | |
a2185576 | 120 | (defcustom display-time-format nil |
3bfbd249 RS |
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 | |
121211d4 | 123 | how to write this string. If this is nil, the defaults |
a2185576 RS |
124 | depend on `display-time-day-and-date' and `display-time-24hr-format'." |
125 | :type '(choice (const :tag "Default" nil) | |
126 | string) | |
127 | :group 'display-time) | |
3bfbd249 | 128 | |
a2185576 | 129 | (defcustom display-time-string-forms |
121211d4 RS |
130 | '((if (and (not display-time-format) display-time-day-and-date) |
131 | (format-time-string "%a %b %e " now) | |
132 | "") | |
133 | (format-time-string (or display-time-format | |
057cf39e | 134 | (if display-time-24hr-format "%H:%M" "%-I:%M%p")) |
121211d4 | 135 | now) |
823d86f3 RS |
136 | load |
137 | (if mail " Mail" "")) | |
138 | "*A list of expressions governing display of the time in the mode line. | |
3bfbd249 RS |
139 | For most purposes, you can control the time format using `display-time-format' |
140 | which is a more standard interface. | |
141 | ||
823d86f3 RS |
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. | |
146 | ||
147 | For example, the form | |
148 | ||
149 | '((substring year -2) \"/\" month \"/\" day | |
13b53275 | 150 | \" \" 24-hours \":\" minutes \":\" seconds |
823d86f3 RS |
151 | (if time-zone \" (\") time-zone (if time-zone \")\") |
152 | (if mail \" Mail\" \"\")) | |
153 | ||
a2185576 RS |
154 | would give mode line times like `94/12/30 21:07:48 (UTC)'." |
155 | :type 'sexp | |
156 | :group 'display-time) | |
823d86f3 | 157 | |
04ec3e39 | 158 | (defun display-time-event-handler () |
b36bca9a KH |
159 | (display-time-update) |
160 | ;; Do redisplay right now, if no input pending. | |
a39a6e40 | 161 | (sit-for 0) |
85673d2b RS |
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))) | |
d2b7637f | 168 | ;; If the activation time is far in the past, |
a39a6e40 RS |
169 | ;; skip executions until we reach a time in the future. |
170 | ;; This avoids a long pause if Emacs has been suspended for hours. | |
d2b7637f RS |
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))) | |
a39a6e40 | 177 | (progn |
a39a6e40 | 178 | (timer-set-time timer (timer-next-integral-multiple-of-time |
d2b7637f RS |
179 | current display-time-interval) |
180 | display-time-interval) | |
a39a6e40 | 181 | (timer-activate timer))))) |
b36bca9a KH |
182 | |
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 () | |
e974f599 KH |
188 | (let* ((now (current-time)) |
189 | (time (current-time-string now)) | |
823d86f3 RS |
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)))) | |
194 | (error ""))) | |
195 | (mail-spool-file (or display-time-mail-file | |
196 | (getenv "MAIL") | |
197 | (concat rmail-spool-directory | |
151ca170 | 198 | (user-login-name)))) |
7ff97448 RS |
199 | (mail (and (stringp mail-spool-file) |
200 | (or (null display-time-server-down-time) | |
151ca170 KH |
201 | ;; If have been down for 20 min, try again. |
202 | (> (- (nth 1 (current-time)) | |
203 | display-time-server-down-time) | |
204 | 1200)) | |
205 | (let ((start-time (current-time))) | |
206 | (prog1 | |
207 | (display-time-file-nonempty-p mail-spool-file) | |
208 | (if (> (- (nth 1 (current-time)) (nth 1 start-time)) | |
209 | 20) | |
210 | ;; Record that mail file is not accessible. | |
f8533e9e | 211 | (setq display-time-server-down-time |
151ca170 KH |
212 | (nth 1 (current-time))) |
213 | ;; Record that mail file is accessible. | |
214 | (setq display-time-server-down-time nil)))))) | |
823d86f3 RS |
215 | (24-hours (substring time 11 13)) |
216 | (hour (string-to-int 24-hours)) | |
151ca170 | 217 | (12-hours (int-to-string (1+ (% (+ hour 11) 12)))) |
5b0841fd | 218 | (am-pm (if (>= hour 12) "pm" "am")) |
823d86f3 RS |
219 | (minutes (substring time 14 16)) |
220 | (seconds (substring time 17 19)) | |
e974f599 | 221 | (time-zone (car (cdr (current-time-zone now)))) |
823d86f3 RS |
222 | (day (substring time 8 10)) |
223 | (year (substring time 20 24)) | |
224 | (monthname (substring time 4 7)) | |
225 | (month | |
226 | (cdr | |
227 | (assoc | |
228 | monthname | |
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))) | |
9673300a | 233 | (setq display-time-string |
83656a19 RS |
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)) | |
b36bca9a | 238 | (force-mode-line-update)) |
7229064d RM |
239 | |
240 | (defun display-time-file-nonempty-p (file) | |
c44565c8 RS |
241 | (and (file-exists-p file) |
242 | (< 0 (nth 7 (file-attributes (file-chase-links file)))))) | |
d501f516 | 243 | |
896546cd RS |
244 | (provide 'time) |
245 | ||
d501f516 | 246 | ;;; time.el ends here |