5

How can I print a message for a period of time to the mode line and restore the default format afterwards?

Drew
  • 77,472
  • 10
  • 114
  • 243
clemera
  • 3,451
  • 14
  • 40

2 Answers2

3

You can save the current value of mode-line-format (elisp manual) before changing it, then use a timer (manual) to set it back to the saved value.

(defun temporary-mode-line (text time)
  "Display TEXT in mode line for TIME seconds."
  (let ((old mode-line-format)
    (buf (current-buffer)))
    (setq mode-line-format text)
    (run-at-time time nil
            (lambda (v b)
              (with-current-buffer b
                (setq mode-line-format v)
                (force-mode-line-update)))
            old buf)))
JeanPierre
  • 7,465
  • 1
  • 20
  • 39
  • I accepted the answer from Drew because he was first although yours is a good answer as well, thanks. – clemera Nov 29 '15 at 15:12
2

Temporarily change mode-line-format or any of its constituent variables.

See the Elisp manual, node Mode Line Format and its subnodes.

For example (taken from icicle-show-in-mode-line):

(defun my-show-in-mode-line (text &optional buffer delay)
  "Display TEXT in BUFFER's mode line.
The text is shown for DELAY seconds (default 2), or until a user event.
So call this last in a sequence of user-visible actions."
  (message nil) ; Remove any current msg
  (with-current-buffer (or buffer  (current-buffer))
    (make-local-variable 'mode-line-format) ; Needed for Emacs 21+.
    (let ((mode-line-format  text))
      (force-mode-line-update) (sit-for (or delay  2)))
    (force-mode-line-update)))
Drew
  • 77,472
  • 10
  • 114
  • 243