[gnu.emacs] Time in minibuffer

merlyn@iwarp.intel.com (Randal Schwartz) (11/21/89)

In article <sZNq4mK00W0I83eXtY@andrew.cmu.edu>, jb3o+@andrew (Jon Allen Boone) writes:
| Can someone send me a copy of the code used to cause GNU to display the
| current time on the mode line.  I've seen this done in GosEmacs, and I'm
| next to certain that Gnu can do it too.  Am I right?  Or is it just a
| pipe dream?

Although as many people have replied, executing (display-time) shows
the time and date in the mode line, I prefer not cluttering up all the
mode lines with the same info.  I have an alternate function based on
Kyle Jones' "timer" package that puts the date, load average, and mail
status into the minibuffer line whenever nothing else is happening
there.

Here's the code for my timer routine... you'll need Kyle's timer
package as well.  Signature follows code.  Enjoy.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; SNIP SNIP
;;; Simple (?!) implementation of echo-area clock, using timers.
;;; inspired by ntime.el, hacked on considerably by merlyn
;;; LastEditDate="Wed Jun 21 11:09:05 1989" by merlyn

(require 'timer)

(defvar display-time-string nil
  "String displayed in mode-line.")
  
(defun time-and-date (interval)
  "Start a time-and-date display in the minibuffer every INTERVAL seconds."
  (interactive "NSeconds: ")
  (or global-mode-string (setq global-mode-string '("")))
  (or (memq 'display-time-string global-mode-string)
      (setq global-mode-string
	    (append global-mode-string '(display-time-string))))
  (if (get-timer "time-and-date")
      (delete-timer "time-and-date"))
  (start-timer "time-and-date" 'time-and-date-function 0 interval))

(defun time-and-date-function ()
  "Internal function used by time-and-date."
  ;; (set-you-have-mail-banner (you-have-mail-p))
  (let* ((ctime (current-time-string))
	 (loadavnum (car (load-average)))
	 (loadav (format "%d.%d%d"
			 (/ loadavnum 100)
			 (% (/ loadavnum 10) 10)
			 (% loadavnum 10)))
	 (string (concat (if (you-have-mail-p) "///MAIL/// " "")
			 ctime " " loadav))
	 (msg (concat
	       (make-string (- (screen-width) (length string) 1) 32)
	       string)))
    (save-excursion
      (set-buffer (window-buffer (minibuffer-window)))
      (cond ((zerop (minibuffer-depth))
	     (erase-buffer)
	     (insert msg))))))

(defun you-have-mail-p ()
  "Internal function used by time-and-date-function."
  (< 0 (or (nth 7 (file-attributes (concat "/usr/spool/mail/"
					   (user-login-name))))
	   0)))

;;;(defun set-you-have-mail-banner (&optional arg)
;;;  "If ARG, put up a `you have mail' banner, else remove.
;;;Interactively, ARG is the presence of a prefix."
;;;  (interactive "P")
;;;  (let* ((mb (get-buffer-create " YOU HAVE MAIL"))
;;;	 (mw (get-buffer-window mb))
;;;	 (nn (screen-height))
;;;	 (sw (selected-window)))
;;;    (if arg
;;;	(if mw nil			; do nothing if window is already there
;;;	  (select-window (get-largest-window))
;;;	  (split-window-horizontally (- (window-width (selected-window)) 14))
;;;	  (other-window 1)
;;;	  (switch-to-buffer mb t)
;;;	  (setq mode-line-format "YOU HAVE MAIL %-")
;;;	  (erase-buffer)
;;;	  (while (> (setq nn (1- nn)) 0)
;;;	    (insert "YOU HAVE MAIL\n"))
;;;	  (goto-char (point-min))
;;;	  (select-window sw))
;;;      (if mb (kill-buffer mb))
;;;      (if mw (delete-window mw)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; SNIP SNIP

Just another Elisp hacker,
-- 
/== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\
| on contract to Intel's iWarp project, Hillsboro, Oregon, USA, Sol III  |
| merlyn@iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/