*** empty log message ***
[emacs.git] / lisp / time.el
blob11ab1cd524aad52e13dc96be33f04e2ae0ba02f9
1 ;;; time.el --- display time and load in mode line of Emacs.
3 ;; Maintainer: FSF
4 ;; Last-Modified: 09 Oct 1990
6 ;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it 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.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;;; Code:
26 (defvar display-time-mail-file nil
27 "*File name of mail inbox file, for indicating existence of new mail.
28 Default is system-dependent, and is the same as used by Rmail.")
30 ;;;###autoload
31 (defconst display-time-day-and-date nil "\
32 *Non-nil means \\[display-time] should display day and date as well as time.")
34 (defvar display-time-process nil)
36 (defvar display-time-interval 60
37 "*Seconds between updates of time in the mode line.")
39 (defvar display-time-24hr-format nil
40 "Non-nill indicates time should be displayed as hh:mm, 0 <= hh <= 23.
41 Nil means 1 <= hh <= 12, and an AM/PM suffix is used.")
43 (defvar display-time-string nil)
45 (defvar display-time-hook nil
46 "* List of functions to be called when the time is updated on the mode line.")
48 ;;;###autoload
49 (defun display-time ()
50 "Display current time and load level in mode line of each buffer.
51 Updates automatically every minute.
52 If `display-time-day-and-date' is non-nil, the current day and date
53 are displayed as well.
54 After each update, `display-time-hook' is run with `run-hooks'."
55 (interactive)
56 (let ((live (and display-time-process
57 (eq (process-status display-time-process) 'run))))
58 (if (not live)
59 (progn
60 (if display-time-process
61 (delete-process display-time-process))
62 (or global-mode-string (setq global-mode-string '("")))
63 (or (memq 'display-time-string global-mode-string)
64 (setq global-mode-string
65 (append global-mode-string '(display-time-string))))
66 (setq display-time-string "")
67 (setq display-time-process
68 (start-process "display-time" nil
69 (concat exec-directory "wakeup")
70 (int-to-string display-time-interval)))
71 (process-kill-without-query display-time-process)
72 (set-process-sentinel display-time-process 'display-time-sentinel)
73 (set-process-filter display-time-process 'display-time-filter)))))
75 (defun display-time-sentinel (proc reason)
76 (or (eq (process-status proc) 'run)
77 (setq display-time-string ""))
78 ;; Force mode-line updates
79 (save-excursion (set-buffer (other-buffer)))
80 (set-buffer-modified-p (buffer-modified-p))
81 (sit-for 0))
83 (defun display-time-filter (proc string)
84 (let ((time (current-time-string))
85 (load (condition-case ()
86 (if (zerop (car (load-average))) ""
87 (let ((str (format " %03d" (car (load-average)))))
88 (concat (substring str 0 -2) "." (substring str -2))))
89 (error "")))
90 (mail-spool-file (or display-time-mail-file
91 (getenv "MAIL")
92 (concat rmail-spool-directory
93 (or (getenv "LOGNAME")
94 (getenv "USER")
95 (user-login-name)))))
96 hour am-pm-flag)
97 (setq hour (read (substring time 11 13)))
98 (if (not display-time-24hr-format)
99 (progn
100 (setq am-pm-flag (if (>= hour 12) "pm" "am"))
101 (if (> hour 12)
102 (setq hour (- hour 12))
103 (if (= hour 0)
104 (setq hour 12))))
105 (setq am-pm-flag ""))
106 (setq display-time-string
107 (concat (format "%d" hour) (substring time 13 16)
108 am-pm-flag
109 load
110 (if (and (file-exists-p mail-spool-file)
111 ;; file not empty?
112 (display-time-file-nonempty-p mail-spool-file))
113 " Mail"
114 "")))
115 ;; Append the date if desired.
116 (if display-time-day-and-date
117 (setq display-time-string
118 (concat (substring time 0 11) display-time-string))))
119 (run-hooks 'display-time-hook)
120 ;; Force redisplay of all buffers' mode lines to be considered.
121 (save-excursion (set-buffer (other-buffer)))
122 (set-buffer-modified-p (buffer-modified-p))
123 ;; Do redisplay right now, if no input pending.
124 (sit-for 0))
126 (defun display-time-file-nonempty-p (file)
127 (while (file-symlink-p file)
128 (setq file (file-symlink-p file)))
129 (> (nth 7 (file-attributes file)) 0))
131 ;;; time.el ends here