time.el 3.4 KB

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