0

I have this function:

(defun foo ()
  (interactive)

  (call-interactively 'bar)
  (call-interactively 'baz)
  (call-interactively 'qux)
  (call-interactively 'quux)
  (call-interactively 'fred))

When I call foo, I would like to see in the echo area a message like X is running as long as the X function is running, where X is bar, baz and so on, but without hiding any messages that emacs might show when foo itself is running. I mean something like this:

bar is running
Query replacing regexp foo with bar: (? for help)

Just I have no idea how to do such a thing.


Clarification

db84x pointed out to me that my question is unclear, so I'm going to clarify it.

Suppose bar contains this line:

(query-replace-regexp "bar" "qwerty" nil (point-min) (point-max))

When emacs finds this line, the following will appear in your echo area:

--- Echo area ---
Query replacing regexp bar with qwerty: (? for help)
--- Echo area ---

When I call bar, I first want a message to appear in echo area:

--- Echo area ---
bar is running
--- Echo area ---

and this message must not disapper as long as bar is running. So, when emacs finds the query-replace-regexpabove, the echo area should appear this way:

--- Echo area ---
bar is running
Query replacing regexp bar with qwerty: (? for help)
--- Echo area ---

I'm sorry if my question was unclear. Hope now it is.


Attempt 1

I picked up Drew's suggestion about message and I found with-temp-message:

Macro: with-temp-message message &rest body

This construct displays a message in the echo area temporarily, during the execution of body. It displays message, executes body, then returns the value of the last body form while restoring the previous echo area contents.

It seems this macro is just what I am looking for, so I defined this function:

(defun fred ()
  (interactive)
  (with-temp-message
      (message "Time's up! Three bucks off!")
    (query-replace-regexp "foo" "bar" nil (point-min) (point-max))))

When I call fred, my message is shown only at the end of its execution and not during. Where am I wrong?

1
  • 3
    Add calls to message in your command, or advise those commands you invoke, to have them add calls to message. (But this hints at an X-Y problem.)
    – Drew
    Oct 22, 2021 at 18:30

1 Answer 1

1

Ok, now that we’ve discovered that you actually want to display two messages, your own message plus whatever the code you are running wants to display, we may be able to answer the question.

I think you could potentially do what you want by changing the set-message-function variable temporarily. I don’t have time to verify that this will work, but something like this might work:

(defun foo ()
  (interactive)
  (let* ((prefix "")
         (set-message-function
          (lambda (message)
            (concat prefix "\n" message))))
    (setq prefix "bar is running")
    (call-interactively 'bar)
    (setq prefix "baz is running")
    (call-interactively 'baz)
    (setq prefix "qux is running")
    (call-interactively 'qux)
    (setq prefix "quux is running")
    (call-interactively 'quux)
    (setq prefix "fred is running")
    (call-interactively 'fred)))

Check out 39.4.1 Displaying Messages in the Echo Area in the Emacs Lisp manual for more information. You can also open the Emacs manuals inside of Emacs with C-h i.

6
  • @db84x I've already tried as you say, but it doesn't work: without the macro message no message is displayed in the echo area. Instead, using the message macro, my message is displayed in the echo area after fred has been run and the message does not disappear as I think it should. I guess I'm not understanding the meaning of with-temp-message, aren't I? Oct 24, 2021 at 13:20
  • Works for me. Test it with something that doesn’t itself replace the message, like this: (with-temp-message "Time's up! Three bucks off!" (sleep-for 1))
    – db48x
    Oct 24, 2021 at 15:03
  • @db84x Thanks, this last code works for me too, just the message does not remain in the echo area: it appears for a while and gives way to "Query replacing ...", while I want it to always remain visible as long as fred is running. This way you should see in the echo area "Time's up! ..." and under it "Query replacing ..." or whatever message emacs might show. Oct 24, 2021 at 15:36
  • Oh, you want to somehow display multiple messages? You never said anything about that in your question.
    – db48x
    Oct 24, 2021 at 15:58
  • @db84x I said "as long as the X function is running". I thought it was clear what I meant. I'm sorry it wasn't. Oct 24, 2021 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.