1 ;;; time.el --- display time and load in mode line of Emacs.
3 ;; Copyright (C) 1985, 1986, 1987, 1993 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 Default is system-dependent, and is the same as used by Rmail.")
35 (defvar display-time-day-and-date nil
"\
36 *Non-nil means \\[display-time] should display day and date as well as time.")
38 (defvar display-time-process nil
)
40 (defvar display-time-interval
60
41 "*Seconds between updates of time in the mode line.")
43 (defvar display-time-24hr-format nil
44 "*Non-nill indicates time should be displayed as hh:mm, 0 <= hh <= 23.
45 Nil means 1 <= hh <= 12, and an AM/PM suffix is used.")
47 (defvar display-time-string nil
)
49 (defvar display-time-hook nil
50 "* List of functions to be called when the time is updated on the mode line.")
52 (defvar display-time-server-down-time nil
53 "Time when mail file's file system was recorded to be down.
54 If that file system seems to be up, the value is nil.")
57 (defun display-time ()
58 "Display current time, load level, and mail flag in mode line of each buffer.
59 Updates automatically every minute.
60 If `display-time-day-and-date' is non-nil, the current day and date
61 are displayed as well.
62 After each update, `display-time-hook' is run with `run-hooks'."
64 (let ((live (and display-time-process
65 (eq (process-status display-time-process
) 'run
))))
68 (if display-time-process
69 (delete-process display-time-process
))
70 (or global-mode-string
(setq global-mode-string
'("")))
71 (or (memq 'display-time-string global-mode-string
)
72 (setq global-mode-string
73 (append global-mode-string
'(display-time-string))))
74 (setq display-time-string
"")
75 (setq display-time-process
76 (start-process "display-time" nil
77 (expand-file-name "wakeup" exec-directory
)
78 (int-to-string display-time-interval
)))
79 (process-kill-without-query display-time-process
)
80 (set-process-sentinel display-time-process
'display-time-sentinel
)
81 (set-process-filter display-time-process
'display-time-filter
)))))
83 (defun display-time-sentinel (proc reason
)
84 (or (eq (process-status proc
) 'run
)
85 (setq display-time-string
""))
86 ;; Force mode-line updates
87 (save-excursion (set-buffer (other-buffer)))
88 (set-buffer-modified-p (buffer-modified-p))
91 (defun display-time-filter (proc string
)
92 (let ((time (current-time-string))
93 (load (condition-case ()
94 (if (zerop (car (load-average))) ""
95 (let ((str (format " %03d" (car (load-average)))))
96 (concat (substring str
0 -
2) "." (substring str -
2))))
98 (mail-spool-file (or display-time-mail-file
100 (concat rmail-spool-directory
102 hour am-pm-flag mail-flag
)
103 (setq hour
(read (substring time
11 13)))
104 (if (not display-time-24hr-format
)
106 (setq am-pm-flag
(if (>= hour
12) "pm" "am"))
108 (setq hour
(- hour
12))
111 (setq am-pm-flag
""))
113 (if (and (or (null display-time-server-down-time
)
114 ;; If have been down for 20 min, try again.
115 (> (- (nth 1 (current-time))
116 display-time-server-down-time
)
118 (let ((start-time (current-time)))
120 (display-time-file-nonempty-p mail-spool-file
)
121 (if (> (- (nth 1 (current-time)) (nth 1 start-time
))
123 ;; Record that mail file is not accessible.
124 (setq display-time-server-down-time
125 (nth 1 (current-time)))
126 ;; Record that mail file is accessible.
127 (setq display-time-server-down-time nil
))
131 (setq display-time-string
132 (concat (format "%d" hour
) (substring time
13 16)
136 ;; Append the date if desired.
137 (if display-time-day-and-date
138 (setq display-time-string
139 (concat (substring time
0 11) display-time-string
))))
140 (run-hooks 'display-time-hook
)
141 ;; Force redisplay of all buffers' mode lines to be considered.
142 (save-excursion (set-buffer (other-buffer)))
143 (set-buffer-modified-p (buffer-modified-p))
144 ;; Do redisplay right now, if no input pending.
147 (defun display-time-file-nonempty-p (file)
148 (and (file-exists-p file
)
149 (< 0 (nth 7 (file-attributes (file-chase-links file
))))))
151 ;;; time.el ends here