home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / packages / timer.shar / ntime.el next >
Encoding:
Text File  |  1991-01-14  |  2.3 KB  |  61 lines

  1. ;; Simple implementation of mode-line/echo-area clock, using timers.
  2.  
  3. (require 'timer)
  4.  
  5. (defvar display-time-interval 60
  6.   "*Number of secods between update of the clock display.")
  7.  
  8. (defvar display-time-echo-area nil
  9.   "*Non-nil value means the clock should be displayed in the message echo area,
  10. instead of the mode line.")
  11.  
  12. (defvar display-time-day-and-date nil
  13.   "*Non-nil value means the day and date should be displayed along with the
  14. usual time of day.")
  15.  
  16. (defun display-time ()
  17.   "Display time of day in the mode line or echo area."
  18.   (interactive)
  19.   ;; if the "display-time" timer already exists, do nothing.
  20.   (if (get-timer "display-time")
  21.       ()
  22.     ;; If we're not displaying the time in the echo area
  23.     ;; and the global mode string does not have a non-nil value
  24.     ;; then initialize the global mode string's value.
  25.     (or display-time-echo-area
  26.     global-mode-string
  27.     (setq global-mode-string '("")))
  28.     ;; If we're not displaying the time in the echo area
  29.     ;; and our display variable is not part of the global-mode-string list
  30.     ;; the we add our variable to the list.  This will make the time
  31.     ;; appear on the modeline.
  32.     (or display-time-echo-area
  33.     (memq 'display-time-string global-mode-string)
  34.     (setq global-mode-string
  35.           (append global-mode-string '(display-time-string))))
  36.     ;; Display the time initially...
  37.     (display-time-function)
  38.     ;; ... and start a timer to do it automatically thereafter.
  39.     (start-timer "display-time" 'display-time-function
  40.          display-time-interval display-time-interval)))
  41.  
  42. (defun display-time-function ()
  43.   (let (string)
  44.     ;; display the day and date if the user requests it.
  45.     (setq string (substring (current-time-string)
  46.                 (if display-time-day-and-date 0 11) -8))
  47.     ;; stuff the time in the echo area if specified,
  48.     ;; otherwise put it in the modeline, via display-time-string
  49.     ;; and global-mode-string.
  50.     (if display-time-echo-area
  51.     (if (zerop (minibuffer-depth))
  52.         (save-excursion
  53.           (set-buffer (window-buffer (minibuffer-window)))
  54.           (erase-buffer)
  55.           (indent-to (- (screen-width) (length string) 1))
  56.           (insert string)))
  57.       (setq display-time-string string)
  58.       ;; voodoo to fake Emacs into recalculating the mode line displays.
  59.       (save-excursion (set-buffer (other-buffer)))
  60.       (set-buffer-modified-p (buffer-modified-p)))))
  61.