comparison lisp/packages/time.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children ac2d302a0011
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
1 ;;; time.el --- display time and load in mode line of Emacs.
2
3 ;; Copyright (C) 1985, 1986, 1987, 1992-1994 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: extensions
7
8 ;; This file is part of XEmacs.
9
10 ;; XEmacs is free software; you can redistribute it and/or modify it
11 ;; under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; XEmacs is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
22 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Synched up with: FSF 19.30.
25
26 ;;; Commentary:
27
28 ;;; Facilities to display current time/date and a new-mail indicator
29 ;;; in the Emacs mode line. The single entry point is `display-time'.
30
31 ;;; See also reportmail.el.
32 ;;; This uses the XEmacs timeout-event mechanism, via a version
33 ;;; of Kyle Jones' itimer package.
34
35 ;;; Code:
36
37 (require 'itimer)
38
39 (defvar display-time-mail-file nil
40 "*File name of mail inbox file, for indicating existence of new mail.
41 Non-nil and not a string means don't check for mail. nil means use
42 default, which is system-dependent, and is the same as used by Rmail.")
43
44 ;;;###autoload
45 (defvar display-time-day-and-date nil "\
46 *Non-nil means \\[display-time] should display day and date as well as time.")
47
48 (defvar display-time-interval 60
49 "*Seconds between updates of time in the mode line.")
50
51 (defvar display-time-24hr-format nil
52 "*Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23.
53 Nil means 1 <= hh <= 12, and an AM/PM suffix is used.")
54
55 (defvar display-time-echo-area nil
56 "*If non-nil, display-time will use the echo area instead of the mode line.")
57
58 (defvar display-time-string nil)
59
60 (defvar display-time-hook nil
61 "*List of functions to be called when the time is updated on the mode line.")
62
63 (defvar display-time-server-down-time nil
64 "Time when mail file's file system was recorded to be down.
65 If that file system seems to be up, the value is nil.")
66
67 ;;;###autoload
68 (defun display-time ()
69 "Display current time, load level, and mail flag in mode line of each buffer.
70 Updates automatically every minute.
71 If `display-time-day-and-date' is non-nil, the current day and date
72 are displayed as well.
73 After each update, `display-time-hook' is run with `run-hooks'.
74 If `display-time-echo-area' is non-nil, the time is displayed in the
75 echo area instead of in the mode-line."
76 (interactive)
77 ;; if the "display-time" itimer already exists, nuke it first.
78 (let ((old (get-itimer "display-time")))
79 (if old (delete-itimer old)))
80 ;; If we're not displaying the time in the echo area
81 ;; and the global mode string does not have a non-nil value
82 ;; then initialize the global mode string's value.
83 (or display-time-echo-area
84 global-mode-string
85 (setq global-mode-string '("")))
86 ;; If we're not displaying the time in the echo area
87 ;; and our display variable is not part of the global-mode-string list
88 ;; the we add our variable to the list. This will make the time
89 ;; appear on the modeline.
90 (or display-time-echo-area
91 (memq 'display-time-string global-mode-string)
92 (setq global-mode-string
93 (append global-mode-string '(display-time-string))))
94 ;; Display the time initially...
95 (display-time-function)
96 ;; ... and start an itimer to do it automatically thereafter.
97 ;;
98 ;; If we wanted to be really clever about this, we could have the itimer
99 ;; not be automatically restarted, but have it re-add itself each time.
100 ;; Then we could look at (current-time) and arrange for the itimer to
101 ;; wake up exactly at the minute boundary. But that's just a little
102 ;; more work than it's worth...
103 (start-itimer "display-time" 'display-time-function
104 display-time-interval display-time-interval))
105
106 (defvar display-time-string-forms
107 '((if display-time-day-and-date
108 (format "%s %s %s " dayname monthname day)
109 "")
110 (format "%s:%s%s"
111 (if display-time-24hr-format 24-hours 12-hours)
112 minutes
113 (if display-time-24hr-format "" am-pm))
114 load
115 (if mail " Mail" ""))
116 "*A list of expressions governing display of the time in the mode line.
117 This expression is a list of expressions that can involve the keywords
118 `load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
119 `seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
120 and `time-zone' all alphabetic strings, and `mail' a true/nil value.
121
122 For example, the form
123
124 '((substring year -2) \"/\" month \"/\" day
125 \" \" 24-hours \":\" minutes \":\" seconds
126 (if time-zone \" (\") time-zone (if time-zone \")\")
127 (if mail \" Mail\" \"\"))
128
129 would give mode line times like `94/12/30 21:07:48 (UTC)'.")
130
131 (defun display-time-function ()
132 (let* ((now (current-time))
133 (time (current-time-string now))
134 (load (condition-case ()
135 (if (zerop (car (load-average))) ""
136 (let ((str (format " %03d" (car (load-average)))))
137 (concat (substring str 0 -2) "." (substring str -2))))
138 (error "")))
139 (mail-spool-file (or display-time-mail-file
140 (getenv "MAIL")
141 (concat rmail-spool-directory
142 (user-login-name))))
143 (mail (and (stringp mail-spool-file)
144 (or (null display-time-server-down-time)
145 ;; If have been down for 20 min, try again.
146 (> (- (nth 1 (current-time))
147 display-time-server-down-time)
148 1200))
149 (let ((start-time (current-time)))
150 (prog1
151 (display-time-file-nonempty-p mail-spool-file)
152 (if (> (- (nth 1 (current-time)) (nth 1 start-time))
153 20)
154 ;; Record that mail file is not accessible.
155 (setq display-time-server-down-time
156 (nth 1 (current-time)))
157 ;; Record that mail file is accessible.
158 (setq display-time-server-down-time nil))))))
159 (24-hours (substring time 11 13))
160 (hour (string-to-int 24-hours))
161 (12-hours (int-to-string (1+ (% (+ hour 11) 12))))
162 (am-pm (if (>= hour 12) "pm" "am"))
163 (minutes (substring time 14 16))
164 (seconds (substring time 17 19))
165 (time-zone (car (cdr (current-time-zone now))))
166 (day (substring time 8 10))
167 (year (substring time 20 24))
168 (monthname (substring time 4 7))
169 (month
170 (cdr
171 (assoc
172 monthname
173 '(("Jan" . "1") ("Feb" . "2") ("Mar" . "3") ("Apr" . "4")
174 ("May" . "5") ("Jun" . "6") ("Jul" . "7") ("Aug" . "8")
175 ("Sep" . "9") ("Oct" . "10") ("Nov" . "11") ("Dec" . "12")))))
176 (dayname (substring time 0 3)))
177 (setq display-time-string
178 (mapconcat 'eval display-time-string-forms ""))
179 ;; This is inside the let binding, but we are not going to document
180 ;; what variables are available.
181 (run-hooks 'display-time-hook))
182 (if display-time-echo-area
183 (or (> (minibuffer-depth) 0)
184 ;; don't stomp echo-area-buffer if reading from minibuffer now.
185 (save-excursion
186 (save-window-excursion
187 (select-window (minibuffer-window))
188 (erase-buffer)
189 (indent-to (- (screen-width) (length display-time-string) 1))
190 (insert display-time-string)
191 (message (buffer-string)))))
192 (force-mode-line-update)
193 ;; Do redisplay right now, if no input pending.
194 (sit-for 0)))
195
196 (defun display-time-file-nonempty-p (file)
197 (and (file-exists-p file)
198 (< 0 (nth 7 (file-attributes (file-chase-links file))))))
199
200 (provide 'time)
201
202 ;;; time.el ends here