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 (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.")
37 (defvar display-time-day-and-date nil
"\
38 *Non-nil means \\[display-time] should display day and date as well as time.")
40 (defvar display-time-timer nil
)
42 (defvar display-time-interval
60
43 "*Seconds between updates of time in the mode line.")
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.")
49 (defvar display-time-string nil
)
51 (defvar display-time-hook nil
52 "* List of functions to be called when the time is updated on the mode line.")
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.")
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."
66 (display-time-mode 1))
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.
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."
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
'("")))
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.
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
))))
103 (defvar display-time-string-forms
104 '((if display-time-day-and-date
105 (format "%s %s %s " dayname monthname day
)
108 (if display-time-24hr-format
24-hours
12-hours
)
110 (if display-time-24hr-format
"" am-pm
))
112 (if mail
" Mail" ""))
113 "*A list of expressions governing display of the time in the mode line.
114 This expression is a list of expressions that can involve the keywords
115 `load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
116 `seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
117 and `time-zone' all alphabetic strings, and `mail' a true/nil value.
119 For example, the form
121 '((substring year -2) \"/\" month \"/\" day
122 \" \" 24-hours \":\" minutes \":\" seconds
123 (if time-zone \" (\") time-zone (if time-zone \")\")
124 (if mail \" Mail\" \"\"))
126 would give mode line times like `94/12/30 21:07:48 (UTC)'.")
128 (defun display-time-event-handler ()
129 (display-time-update)
130 ;; Do redisplay right now, if no input pending.
133 ;; Update the display-time info for the mode line
134 ;; but don't redisplay right now. This is used for
135 ;; things like Rmail `g' that want to force an update
136 ;; which can wait for the next redisplay.
137 (defun display-time-update ()
138 (let* ((now (current-time))
139 (time (current-time-string now
))
140 (load (condition-case ()
141 (if (zerop (car (load-average))) ""
142 (let ((str (format " %03d" (car (load-average)))))
143 (concat (substring str
0 -
2) "." (substring str -
2))))
145 (mail-spool-file (or display-time-mail-file
147 (concat rmail-spool-directory
149 (mail (and (stringp mail-spool-file
)
150 (or (null display-time-server-down-time
)
151 ;; If have been down for 20 min, try again.
152 (> (- (nth 1 (current-time))
153 display-time-server-down-time
)
155 (let ((start-time (current-time)))
157 (display-time-file-nonempty-p mail-spool-file
)
158 (if (> (- (nth 1 (current-time)) (nth 1 start-time
))
160 ;; Record that mail file is not accessible.
161 (setq display-time-server-down-time
162 (nth 1 (current-time)))
163 ;; Record that mail file is accessible.
164 (setq display-time-server-down-time nil
))))))
165 (24-hours (substring time
11 13))
166 (hour (string-to-int 24-hours
))
167 (12-hours (int-to-string (1+ (%
(+ hour
11) 12))))
168 (am-pm (if (>= hour
12) "pm" "am"))
169 (minutes (substring time
14 16))
170 (seconds (substring time
17 19))
171 (time-zone (car (cdr (current-time-zone now
))))
172 (day (substring time
8 10))
173 (year (substring time
20 24))
174 (monthname (substring time
4 7))
179 '(("Jan" .
"1") ("Feb" .
"2") ("Mar" .
"3") ("Apr" .
"4")
180 ("May" .
"5") ("Jun" .
"6") ("Jul" .
"7") ("Aug" .
"8")
181 ("Sep" .
"9") ("Oct" .
"10") ("Nov" .
"11") ("Dec" .
"12")))))
182 (dayname (substring time
0 3)))
183 (setq display-time-string
184 (mapconcat 'eval display-time-string-forms
""))
185 ;; This is inside the let binding, but we are not going to document
186 ;; what variables are available.
187 (run-hooks 'display-time-hook
))
188 (force-mode-line-update))
190 (defun display-time-file-nonempty-p (file)
191 (and (file-exists-p file
)
192 (< 0 (nth 7 (file-attributes (file-chase-links file
))))))
194 ;;; time.el ends here