The debug-on-message variable tells Emacs to start the debugger when a matching message is displayed. How can I get Emacs to invoke my own function when a matching message is displayed?

My motivation in this case is to build a contingency engine for common errors. Not all of these are well-handled locally through advice or unwind-protect, so I would like to use the echo area (or equivalently the *Messages* buffer) as my input.

Example usage: (do-on-message "Foo error" 'handle-foo)

  • Perhaps you could set the debugger variable to a custom function. Not sure if that would work together with debug-on-message. – legoscia May 21 '15 at 22:31
  • What is a contingency engine for error?! – user227 May 22 '15 at 16:09

Well, you could advise message.

But do you really need/want this to take effect for calls to message, i.e., including for calls that your code does not make?

If what you are really trying to do is "build a contingency engine for common errors" then it sounds like what it would be helpful to define your own error symbols. To do that, you use define-error - see the Elisp manual, node Error Symbols.

And see these nodes:

Note: In versions of Emacs prior to Emacs 24.4, there was no define-error. You accomplished the same thing using symbol property error-conditions (and you can still do this). Here are the Emacs 24.4 and Emacs 24.3 definitions of error condition bookmark-error-no-filename, for instance:

Emacs 24.4 (+):

(define-error 'bookmark-errors nil)
(define-error 'bookmark-error-no-filename
  "Bookmark has no associated file (or directory)" 'bookmark-errors)

Emacs 24.3:

(put 'bookmark-error-no-filename
     '(error bookmark-errors bookmark-error-no-filename))
(put 'bookmark-error-no-filename
     "Bookmark has no associated file (or directory)")

P.S. If my guess is right about what your question is really about, please consider editing the question (at least the title) to make it clear.

  • I also would not advise message. I tried this once, and it resulted in some serious stability issues that were quite difficult to track down. – Qudit May 22 '15 at 6:03

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.