1

I want to print in the echo area a coloured label with a description, but cannot get the colour to render properly.

(defun echos (label text)
  "Display label with text in the Echo Area."
  (let* ((label-colour "red")
         (clabel (propertize label 'face `(:foreground ,label-colour))))
    (message "%s %s" clabel text)))
6
  • 1
    What do you mean by "cannot get the colour to render properly"?
    – shynur
    Commented Jul 2, 2023 at 13:24
  • 1
    BTW, I recommend you post well-formatted code next time. And don't simply cut a snippet from your buffer with those variables whose names' meanings are unknown; choose some meaningful name, and try to let people know what you want.
    – shynur
    Commented Jul 2, 2023 at 13:28
  • clabel is not printed with red foreground.
    – Dilna
    Commented Jul 2, 2023 at 13:36
  • 1
    1) You changed those names, good. 2) what did you see in echo area? Something like this #("clabel text" 0 6 (face (:foreground "red")))?
    – shynur
    Commented Jul 2, 2023 at 13:36
  • 2
    I'm pretty sure this is a duplicate. The confusion between printing a returned value and printing as a side effect is a common gotcha. Maybe Someone(TM) has the time to search for the dup(s) - likely with M-:, C-x C-e, and similar.
    – Drew
    Commented Jul 2, 2023 at 14:26

1 Answer 1

1

C-x C-e will evaluate that expression first and then display the returned value from that expression.

So after you C-x C-e (echos "clabel" "text"),

  1. The rendered text is displayed in echo area, lasting for several milliseconds/nanoseconds.
  2. Then the returned value from (echos "clabel" "text"), which is #("clabel text" 0 6 (face (:foreground "red"))), is be displayed in echo area.

So, to observe the rendered text better, evaluate this:

(prog1 (echos "clabel" "text")
  (sleep-for 1))

Or use C-j if you are using lisp-interaction-mode.

6
  • Why does display in colour, then change to #("clabel text" 0 6 (face (:foreground "red"))) ?
    – Dilna
    Commented Jul 2, 2023 at 14:06
  • If I call (echos "clabel" "text") from an interactive function, label gets printed in red as expected. No reed for sleeping at all.
    – Dilna
    Commented Jul 2, 2023 at 14:10
  • 1
    @Dilna: So you should read my answer carefully
    – shynur
    Commented Jul 2, 2023 at 14:11
  • 1
    @Dilna: Because that is C-x C-e's feature. It just does that way.
    – shynur
    Commented Jul 2, 2023 at 14:26
  • 2
    FWIW: OP doesn't read carefully, in general: Questions that OP posts (versus what OP might think OP asked); answers to those questions; or comments on those questions and answers. Nor does OP invest in reading Emacs *Help* or manuals. Kind of a waste of time for well-meaning interlocutors here. By now, one would think that the message might have been received, and that OP would start helping OP.
    – Drew
    Commented Jul 2, 2023 at 14:32

Your Answer

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

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