1 ;;; time.el --- display time and load in mode line of Emacs.
3 ;; Copyright (C) 1985, 1986, 1987, 1993, 1994 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
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 ;;; Facilities to display current time/date and a new-mail indicator
26 ;;; in the Emacs mode line. The single entry point is `display-time'.
30 (defvar display-time-mail-file nil
31 "*File name of mail inbox file, for indicating existence of new mail.
32 Non-nil and not a string means don't check for mail. nil means use
33 default, which is system-dependent, and is the same as used by Rmail.")
36 (defvar display-time-day-and-date nil
"\
37 *Non-nil means \\[display-time] should display day and date as well as time.")
39 (defvar display-time-process nil
)
41 (defvar display-time-interval
60
42 "*Seconds between updates of time in the mode line.")
44 (defvar display-time-24hr-format nil
45 "*Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23.
46 Nil means 1 <= hh <= 12, and an AM/PM suffix is used.")
48 (defvar display-time-string nil
)
50 (defvar display-time-hook nil
51 "* List of functions to be called when the time is updated on the mode line.")
53 (defvar display-time-server-down-time nil
54 "Time when mail file's file system was recorded to be down.
55 If that file system seems to be up, the value is nil.")
58 (defun display-time ()
59 "Display current time, load level, and mail flag in mode line of each buffer.
60 Updates automatically every minute.
61 If `display-time-day-and-date' is non-nil, the current day and date
62 are displayed as well.
63 After each update, `display-time-hook' is run with `run-hooks'."
65 (let ((live (and display-time-process
66 (eq (process-status display-time-process
) 'run
))))
69 (if display-time-process
70 (delete-process display-time-process
))
71 (or global-mode-string
(setq global-mode-string
'("")))
72 (or (memq 'display-time-string global-mode-string
)
73 (setq global-mode-string
74 (append global-mode-string
'(display-time-string))))
75 (setq display-time-string
"")
76 ;; Using a pty is wasteful, and the separate session causes
77 ;; annoyance sometimes (some systems kill idle sessions).
78 (let ((process-connection-type nil
))
79 (setq display-time-process
80 (start-process "display-time" nil
81 (expand-file-name "wakeup" exec-directory
)
82 (int-to-string display-time-interval
))))
83 (process-kill-without-query display-time-process
)
84 (set-process-sentinel display-time-process
'display-time-sentinel
)
85 (set-process-filter display-time-process
'display-time-filter
)))))
87 (defun display-time-sentinel (proc reason
)
88 (or (eq (process-status proc
) 'run
)
89 (setq display-time-string
""))
90 ;; Force mode-line updates
91 (save-excursion (set-buffer (other-buffer)))
92 (set-buffer-modified-p (buffer-modified-p))
95 (defvar display-time-string-forms
96 '((if display-time-day-and-date
97 (format "%s %s %s " dayname monthname day
)
100 (if display-time-24hr-format
24-hours
12-hours
)
102 (if display-time-24hr-format
"" am-pm
))
104 (if mail
" Mail" ""))
105 "*A list of expressions governing display of the time in the mode line.
106 This expression is a list of expressions that can involve the keywords
107 `load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
108 `seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
109 and `time-zone' all alphabetic strings, and `mail' a true/nil value.
111 For example, the form
113 '((substring year -2) \"/\" month \"/\" day
114 \" \" 24-hours \":\" minutes \":\" seconds
115 (if time-zone \" (\") time-zone (if time-zone \")\")
116 (if mail \" Mail\" \"\"))
118 would give mode line times like `94/12/30 21:07:48 (UTC)'.")
120 (defun display-time-filter (proc string
)
121 (let* ((time (current-time-string))
122 (load (condition-case ()
123 (if (zerop (car (load-average))) ""
124 (let ((str (format " %03d" (car (load-average)))))
125 (concat (substring str
0 -
2) "." (substring str -
2))))
127 (mail-spool-file (or display-time-mail-file
129 (concat rmail-spool-directory
131 (mail (and (stringp mail-spool-file
)
132 (or (null display-time-server-down-time
)
133 ;; If have been down for 20 min, try again.
134 (> (- (nth 1 (current-time))
135 display-time-server-down-time
)
137 (let ((start-time (current-time)))
139 (display-time-file-nonempty-p mail-spool-file
)
140 (if (> (- (nth 1 (current-time)) (nth 1 start-time
))
142 ;; Record that mail file is not accessible.
143 (setq display-time-server-down-time
144 (nth 1 (current-time)))
145 ;; Record that mail file is accessible.
146 (setq display-time-server-down-time nil
))))))
147 (24-hours (substring time
11 13))
148 (hour (string-to-int 24-hours
))
149 (12-hours (int-to-string (1+ (%
(+ hour
11) 12))))
150 (am-pm (if (>= hour
12) "pm" "am"))
151 (minutes (substring time
14 16))
152 (seconds (substring time
17 19))
153 (time-zone (car (cdr (current-time-zone))))
154 (day (substring time
8 10))
155 (year (substring time
20 24))
156 (monthname (substring time
4 7))
161 '(("Jan" .
"1") ("Feb" .
"2") ("Mar" .
"3") ("Apr" .
"4")
162 ("May" .
"5") ("Jun" .
"6") ("Jul" .
"7") ("Aug" .
"8")
163 ("Sep" .
"9") ("Oct" .
"10") ("Nov" .
"11") ("Dec" .
"12")))))
164 (dayname (substring time
0 3)))
165 (setq display-time-string
166 (mapconcat 'eval display-time-string-forms
"")))
167 (run-hooks 'display-time-hook
)
168 ;; Force redisplay of all buffers' mode lines to be considered.
169 (save-excursion (set-buffer (other-buffer)))
170 (set-buffer-modified-p (buffer-modified-p))
171 ;; Do redisplay right now, if no input pending.
174 (defun display-time-file-nonempty-p (file)
175 (and (file-exists-p file
)
176 (< 0 (nth 7 (file-attributes (file-chase-links file
))))))
178 ;;; time.el ends here