3

Suppose I call a function which is named as foo.

Sometimes that function foo will give a message such as "Example of error message". If that is the case, then something else should be executed.

This means that I need a way that to read the output of the messages buffer. In particular, I want to define a function bar that calls foo and executes something when foo gives a error:

(defun bar () 
   (foo)
   (if (string-match `"Example of error message"` (minibuffer-output after calling foo))
           then do this)
      else do this thingy). 

When I read the Emacs Documentation, I couldn't find anything that gives the functionality of reading the latest message from the minibuffer.

Any suggestion would be appreciated.

SITUATION FROM THE REAL WORLD:

It seems my post is not clear enough. I will detail it further from the actual situation here. I have the following:

(defun foo () 
   (interactive)
   (evil-goto-mark ?8)
)

Calling the function foo produces the error Marker 8' is not set in this buffer.

If I get that message after calling (evil-goto-mark ?8), I want to call (evil-set-marker ?8) instead. The answer below didn't work in my case, unfortunately.

  • Please rewrite your question to make it clear what you're asking. The reference to abo-abo's answer in the very first line is particularly jarring. – jch May 30 '15 at 23:00
  • 2
    abo-abo's answer should work for you. Try this function: (defun foo () (interactive) (condition-case nil (evil-goto-mark ?8) (error (evil-set-mark ?8)))) – Malabarba May 31 '15 at 0:39
5

The error in question is thrown with user-error, not simply echoed with message.

In your particular case testing the return value of evil-get-marker should work also:

(if (evil-get-marker ?8)
  (evil-goto-mark ?8)
 (evil-set-marker ?8))
  • Very simple and elegant. I really appreciate it, thanks for that! I will try it soon. I didn't knew it was possible to get a false of true output from a function. How did you discover that? – ReneFroger May 31 '15 at 8:57
  • I read the evil source; evil-get-marker is documented to return nil if there is no such marker, and nil is false. – Tom Regner May 31 '15 at 9:05
  • Clever.. I marked your answer as the right one, because it solved my issue. Thanks for that! – ReneFroger May 31 '15 at 9:21
  • 2
    It solves your problem, but does not answer the question :-) so strictly speaking it is not the correct answer... – Tom Regner May 31 '15 at 9:33
  • 1
    Yes, it does not answer the question as posed. Rene: Please consider editing your question to reflect what you really wanted to know, so the answer fits the question better. That helps others looking for similar help. – Drew May 31 '15 at 18:44
9

No need to read *Messages*. Here's how error handling works in Elisp:

(defun bar ()
  (error "asdf"))

(defun foo ()
  (condition-case e
      (bar)
    (error
     (message "Caught an error: %S" e))))
  • I don't get it. Propably you switched foo and bar here? I would like to execute something in foo. And call foo from bar. and in bar something is executed when foo returns an error message. – ReneFroger May 30 '15 at 21:38
  • Yes, I guess I switched it then. – abo-abo May 30 '15 at 21:46
  • Thanks for your reply, I modified it, but it's not working in my case. The problem is that foo will call another function, but I cannot modify it that it will return an error. I modified my opening post more, to be clear why. – ReneFroger May 30 '15 at 22:16
  • 2
    Rene: You need to read the Elisp manual about handling errors - that's node Handling Errors. I found that by: C-h i, choose Elisp manual, i error S-SPC handl (using Icicles). But even with vanilla Emacs it's not hard to find that node. – Drew May 31 '15 at 1:07
3

If the function your are calling really echoes a message, you can use (current-message).

Return the string currently displayed in the echo area, or nil if none.

Of course, it would be better to issue an error, see other answer for more details on this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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