]> code.delx.au - gnu-emacs/blob - lisp/time.el
(sendmail-send-it): Explicitly pass interactive delivery options to
[gnu-emacs] / lisp / time.el
1 ;;; time.el --- display time and load in mode line of Emacs.
2
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 1996 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6
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
11 ;; the Free Software Foundation; either version 2, or (at your option)
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
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.
23
24 ;;; Commentary:
25
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'.
28
29 ;;; Code:
30
31 (defvar display-time-mail-file nil
32 "*File name of mail inbox file, for indicating existence of new mail.
33 Non-nil and not a string means don't check for mail. nil means use
34 default, which is system-dependent, and is the same as used by Rmail.")
35
36 ;;;###autoload
37 (defvar display-time-day-and-date nil "\
38 *Non-nil means \\[display-time] should display day and date as well as time.")
39
40 (defvar display-time-timer nil)
41
42 (defvar display-time-interval 60
43 "*Seconds between updates of time in the mode line.")
44
45 (defvar display-time-24hr-format nil
46 "*Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23.
47 Nil means 1 <= hh <= 12, and an AM/PM suffix is used.")
48
49 (defvar display-time-string nil)
50
51 (defvar display-time-hook nil
52 "* List of functions to be called when the time is updated on the mode line.")
53
54 (defvar display-time-server-down-time nil
55 "Time when mail file's file system was recorded to be down.
56 If that file system seems to be up, the value is nil.")
57
58 ;;;###autoload
59 (defun display-time ()
60 "Enable display of time, load level, and mail flag in mode lines.
61 This display updates automatically every minute.
62 If `display-time-day-and-date' is non-nil, the current day and date
63 are displayed as well.
64 This runs the normal hook `display-time-hook' after each update."
65 (interactive)
66 (display-time-mode 1))
67
68 ;;;###autoload
69 (defun display-time-mode (arg)
70 "Toggle display of time, load level, and mail flag in mode lines.
71 With a numeric arg, enable this display if arg is positive.
72
73 When this display is enabled, it updates automatically every minute.
74 If `display-time-day-and-date' is non-nil, the current day and date
75 are displayed as well.
76 This runs the normal hook `display-time-hook' after each update."
77 (interactive "P")
78 (let ((on (if (null arg)
79 (not display-time-timer)
80 (> (prefix-numeric-value arg) 0))))
81 (and display-time-timer (cancel-timer display-time-timer))
82 (setq display-time-timer nil)
83 (setq display-time-string "")
84 (or global-mode-string (setq global-mode-string '("")))
85 (if on
86 (progn
87 (or (memq 'display-time-string global-mode-string)
88 (setq global-mode-string
89 (append global-mode-string '(display-time-string))))
90 ;; Set up the time timer.
91 (setq display-time-timer
92 (run-at-time t display-time-interval
93 'display-time-event-handler))
94 ;; Make the time appear right away.
95 (display-time-update)
96 ;; When you get new mail, clear "Mail" from the mode line.
97 (add-hook 'rmail-after-get-new-mail-hook
98 'display-time-event-handler))
99 (remove-hook 'rmail-after-get-new-mail-hook
100 'display-time-event-handler))))
101
102
103 (defvar display-time-format
104 (concat
105 (if display-time-day-and-date
106 "%a %b %e" "")
107 (if display-time-24hr-format "%H:%m" "%-I:%M%p"))
108 "*A string specifying the format for displaying the time in the mode line.
109 See the function `format-time-string' for an explanation of
110 how to write this string.")
111
112 (defvar display-time-string-forms
113 '((format-time-string display-time-format now)
114 load
115 (if mail " Mail" ""))
116 "*A list of expressions governing display of the time in the mode line.
117 For most purposes, you can control the time format using `display-time-format'
118 which is a more standard interface.
119
120 This expression is a list of expressions that can involve the keywords
121 `load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
122 `seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
123 and `time-zone' all alphabetic strings, and `mail' a true/nil value.
124
125 For example, the form
126
127 '((substring year -2) \"/\" month \"/\" day
128 \" \" 24-hours \":\" minutes \":\" seconds
129 (if time-zone \" (\") time-zone (if time-zone \")\")
130 (if mail \" Mail\" \"\"))
131
132 would give mode line times like `94/12/30 21:07:48 (UTC)'.")
133
134 (defun display-time-event-handler ()
135 (display-time-update)
136 ;; Do redisplay right now, if no input pending.
137 (sit-for 0)
138 (let ((current (current-time))
139 (timer display-time-timer)
140 ;; Compute the time when this timer will run again, next.
141 (next-time (timer-relative-time
142 (list (aref timer 1) (aref timer 2) (aref timer 3))
143 (* 5 (aref timer 4)) 0)))
144 ;; If the activation time is far in the past,
145 ;; skip executions until we reach a time in the future.
146 ;; This avoids a long pause if Emacs has been suspended for hours.
147 (or (> (nth 0 next-time) (nth 0 current))
148 (and (= (nth 0 next-time) (nth 0 current))
149 (> (nth 1 next-time) (nth 1 current)))
150 (and (= (nth 0 next-time) (nth 0 current))
151 (= (nth 1 next-time) (nth 1 current))
152 (> (nth 2 next-time) (nth 2 current)))
153 (progn
154 (timer-set-time timer (timer-next-integral-multiple-of-time
155 current display-time-interval)
156 display-time-interval)
157 (timer-activate timer)))))
158
159 ;; Update the display-time info for the mode line
160 ;; but don't redisplay right now. This is used for
161 ;; things like Rmail `g' that want to force an update
162 ;; which can wait for the next redisplay.
163 (defun display-time-update ()
164 (let* ((now (current-time))
165 (time (current-time-string now))
166 (load (condition-case ()
167 (if (zerop (car (load-average))) ""
168 (let ((str (format " %03d" (car (load-average)))))
169 (concat (substring str 0 -2) "." (substring str -2))))
170 (error "")))
171 (mail-spool-file (or display-time-mail-file
172 (getenv "MAIL")
173 (concat rmail-spool-directory
174 (user-login-name))))
175 (mail (and (stringp mail-spool-file)
176 (or (null display-time-server-down-time)
177 ;; If have been down for 20 min, try again.
178 (> (- (nth 1 (current-time))
179 display-time-server-down-time)
180 1200))
181 (let ((start-time (current-time)))
182 (prog1
183 (display-time-file-nonempty-p mail-spool-file)
184 (if (> (- (nth 1 (current-time)) (nth 1 start-time))
185 20)
186 ;; Record that mail file is not accessible.
187 (setq display-time-server-down-time
188 (nth 1 (current-time)))
189 ;; Record that mail file is accessible.
190 (setq display-time-server-down-time nil))))))
191 (24-hours (substring time 11 13))
192 (hour (string-to-int 24-hours))
193 (12-hours (int-to-string (1+ (% (+ hour 11) 12))))
194 (am-pm (if (>= hour 12) "pm" "am"))
195 (minutes (substring time 14 16))
196 (seconds (substring time 17 19))
197 (time-zone (car (cdr (current-time-zone now))))
198 (day (substring time 8 10))
199 (year (substring time 20 24))
200 (monthname (substring time 4 7))
201 (month
202 (cdr
203 (assoc
204 monthname
205 '(("Jan" . "1") ("Feb" . "2") ("Mar" . "3") ("Apr" . "4")
206 ("May" . "5") ("Jun" . "6") ("Jul" . "7") ("Aug" . "8")
207 ("Sep" . "9") ("Oct" . "10") ("Nov" . "11") ("Dec" . "12")))))
208 (dayname (substring time 0 3)))
209 (setq display-time-string
210 (mapconcat 'eval display-time-string-forms ""))
211 ;; This is inside the let binding, but we are not going to document
212 ;; what variables are available.
213 (run-hooks 'display-time-hook))
214 (force-mode-line-update))
215
216 (defun display-time-file-nonempty-p (file)
217 (and (file-exists-p file)
218 (< 0 (nth 7 (file-attributes (file-chase-links file))))))
219
220 ;;; time.el ends here