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