3

I would like to add a suffix to a message reported by a function called in some source I don't maintain.

Is there a way to locally override the message function so I can manipulate it before it's displayed? (add a prefix or suffix for example).

I know I can read the message log and log new message, but I'd rather avoid flooding the log, or manipulate the message buffer.

  • You can advise the message. i advice in the Elisp manual. – Drew Dec 23 '19 at 1:29
  • Can this be done locally though (similar to how let can locally override variables) – ideasman42 Dec 23 '19 at 1:30
  • You can certainly turn advice on and off. And you can use cl-flet and cl-letf to redefine locally. – Drew Dec 23 '19 at 2:06
  • An issue with turning it on and off is there is some risk an error causes it to be left on. cl-letf looks closer to what I'm looking for. – ideasman42 Dec 23 '19 at 2:16
  • An issue with adding/removing advice - is there is some risk an error causes it to be left on. cl-letf seems to be what I'm looking for. – ideasman42 Dec 23 '19 at 22:31
3

This macro adds support for adding a suffix to messages.

Using advice allows this to be nested, so multiple functions can add their own suffixes which accumulate onto the end.

(defmacro with-temp-advice (fn-orig where fn-advice &rest body)
  "Execute BODY with advice temporarily enabled."
  `
  (let ((fn-advice-var ,fn-advice))
    (unwind-protect
      (progn
        (advice-add ,fn-orig ,where fn-advice-var)
        ,@body)
      (advice-remove ,fn-orig fn-advice-var))))

(defmacro with-message-suffix (suffix &rest body)
  "Add text after the message output.
Argument SUFFIX is the text to add at the start of the message.
Optional argument BODY runs with the message suffix."
  (declare (indent 1))
  `
  (with-temp-advice
    'message
    :around
    (lambda (fn-orig arg &rest args)
      (apply fn-orig (append (list (concat arg "%s")) args (list ,suffix))))
    ,@body))
| improve this answer | |

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.