Initial revision
[emacs.git] / lisp / time.el
blob8a1f37f8e18d0cfc06c606c60deca03cf42a74f7
1 ;; Display time and load in mode line of Emacs.
2 ;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 1, or (at your option)
9 ;; any later version.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 (defvar display-time-mail-file nil
22 "*File name of mail inbox file, for indicating existence of new mail.
23 Default is system-dependent, and is the same as used by Rmail.")
25 (defvar display-time-process nil)
27 (defvar display-time-interval 60
28 "*Seconds between updates of time in the mode line.")
30 (defvar display-time-string nil)
32 (defvar display-time-hook nil
33 "* List of functions to be called when the time is updated on the mode line.")
35 (defun display-time ()
36 "Display current time and load level in mode line of each buffer.
37 Updates automatically every minute.
38 If `display-time-day-and-date' is non-nil, the current day and date
39 are displayed as well.
40 After each update, `display-time-hook' is run with `run-hooks'."
41 (interactive)
42 (let ((live (and display-time-process
43 (eq (process-status display-time-process) 'run))))
44 (if (not live)
45 (progn
46 (if display-time-process
47 (delete-process display-time-process))
48 (or global-mode-string (setq global-mode-string '("")))
49 (or (memq 'display-time-string global-mode-string)
50 (setq global-mode-string
51 (append global-mode-string '(display-time-string))))
52 (setq display-time-string "")
53 (setq display-time-process
54 (start-process "display-time" nil
55 "wakeup"
56 (int-to-string display-time-interval)))
57 (process-kill-without-query display-time-process)
58 (set-process-sentinel display-time-process 'display-time-sentinel)
59 (set-process-filter display-time-process 'display-time-filter)))))
61 (defun display-time-sentinel (proc reason)
62 (or (eq (process-status proc) 'run)
63 (setq display-time-string ""))
64 ;; Force mode-line updates
65 (save-excursion (set-buffer (other-buffer)))
66 (set-buffer-modified-p (buffer-modified-p))
67 (sit-for 0))
69 (defun display-time-filter (proc string)
70 (let ((time (current-time-string))
71 (load (format "%03d" (car (load-average))))
72 (mail-spool-file (or display-time-mail-file
73 (getenv "MAIL")
74 (concat rmail-spool-directory
75 (or (getenv "LOGNAME")
76 (getenv "USER")
77 (user-login-name)))))
78 hour pm)
79 (setq hour (read (substring time 11 13)))
80 (setq pm (>= hour 12))
81 (if (> hour 12)
82 (setq hour (- hour 12))
83 (if (= hour 0)
84 (setq hour 12)))
85 (setq display-time-string
86 (concat (format "%d" hour) (substring time 13 16)
87 (if pm "pm " "am ")
88 (substring load 0 -2) "." (substring load -2)
89 (if (and (file-exists-p mail-spool-file)
90 ;; file not empty?
91 (> (nth 7 (file-attributes mail-spool-file)) 0))
92 " Mail"
93 "")))
94 ;; Append the date if desired.
95 (if display-time-day-and-date
96 (setq display-time-string
97 (concat (substring time 0 11) display-time-string))))
98 (run-hooks 'display-time-hook)
99 ;; Force redisplay of all buffers' mode lines to be considered.
100 (save-excursion (set-buffer (other-buffer)))
101 (set-buffer-modified-p (buffer-modified-p))
102 ;; Do redisplay right now, if no input pending.
103 (sit-for 0))