|
|
1.1 ! root 1: ;; Display time and load in mode line of Emacs. ! 2: ;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc. ! 3: ! 4: ;; This file is part of GNU Emacs. ! 5: ! 6: ;; GNU Emacs is distributed in the hope that it will be useful, ! 7: ;; but WITHOUT ANY WARRANTY. No author or distributor ! 8: ;; accepts responsibility to anyone for the consequences of using it ! 9: ;; or for whether it serves any particular purpose or works at all, ! 10: ;; unless he says so in writing. Refer to the GNU Emacs General Public ! 11: ;; License for full details. ! 12: ! 13: ;; Everyone is granted permission to copy, modify and redistribute ! 14: ;; GNU Emacs, but only under the conditions described in the ! 15: ;; GNU Emacs General Public License. A copy of this license is ! 16: ;; supposed to have been given to you along with GNU Emacs so you ! 17: ;; can know your rights and responsibilities. It should be in a ! 18: ;; file named COPYING. Among other things, the copyright notice ! 19: ;; and this notice must be preserved on all copies. ! 20: ! 21: ! 22: (defvar display-time-process nil) ! 23: ! 24: (defvar display-time-interval 60 ! 25: "*Seconds between updates of time in the mode line.") ! 26: ! 27: (defvar display-time-string nil) ! 28: ! 29: (defun display-time () ! 30: "Display current time and load level in mode line of each buffer. ! 31: Updates automatically every minute. ! 32: If display-time-day-and-date is non-nil, the current day and date ! 33: are displayed as well." ! 34: (interactive) ! 35: (let ((live (and display-time-process ! 36: (eq (process-status display-time-process) 'run)))) ! 37: (if (not live) ! 38: (save-excursion ! 39: (if display-time-process ! 40: (delete-process display-time-process)) ! 41: (or global-mode-string (setq global-mode-string '(""))) ! 42: (or (memq 'display-time-string global-mode-string) ! 43: (setq global-mode-string ! 44: (append global-mode-string '(display-time-string)))) ! 45: (setq display-time-string "time and load") ! 46: (setq display-time-process ! 47: (start-process "display-time" nil ! 48: "loadst" ! 49: "-n" (int-to-string display-time-interval))) ! 50: (process-kill-without-query display-time-process) ! 51: (set-process-sentinel display-time-process 'display-time-sentinel) ! 52: (set-process-filter display-time-process 'display-time-filter))))) ! 53: ! 54: (defun display-time-sentinel (proc reason) ! 55: (or (eq (process-status proc) 'run) ! 56: (setq display-time-string "")) ! 57: ;; Force mode-line updates ! 58: (save-excursion (set-buffer (other-buffer))) ! 59: (set-buffer-modified-p (buffer-modified-p)) ! 60: (sit-for 0)) ! 61: ! 62: (defun display-time-filter (proc string) ! 63: ;; Desired data can't need more than the last 30 chars, ! 64: ;; so save time by flushing the rest. ! 65: ;; This way, if we have many different times all collected at once, ! 66: ;; we can discard all but the last few very fast. ! 67: (if (> (length string) 30) ! 68: (setq string (substring string -30))) ! 69: ;; Now discard all but the very last one. ! 70: (while (and (> (length string) 4) ! 71: (string-match "[0-9]+:[0-9][0-9].." string 4)) ! 72: (setq string (substring string (match-beginning 0)))) ! 73: (if (string-match "[^0-9][0-9]+:" string) ! 74: (setq string (substring string 0 (1+ (match-beginning 0))))) ! 75: ;; Append the date if desired. ! 76: (if display-time-day-and-date ! 77: (setq string (concat (substring (current-time-string) 0 11) string))) ! 78: ;; Install the new time for display. ! 79: (setq display-time-string string) ! 80: ;; Force redisplay of all buffers' mode lines to be considered. ! 81: (save-excursion (set-buffer (other-buffer))) ! 82: (set-buffer-modified-p (buffer-modified-p)) ! 83: ;; Do redisplay right now, if no input pending. ! 84: (sit-for 0))
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.