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