0

I would like print some text in either Green Blue Yellow Amber or Red? How can I do it?

Have made the following function but calling

(mcolour "text" "red" "Mybuffer")

did not colour the text.

(defun mcolour (mesg colour buffer)
  "TODO."

  (let ( (buf (get-buffer-create buffer))
         (txt (propertize mesg
                          'face '(:foreground colour))) )
    (with-current-buffer buffer (insert mesg))
    (display-buffer bufferr)))
1
  • 1
    Paste the following snippet into an empty *scratch* buffer and type M-x eval-buffer, or go to the end of the snippet and type C-x C-e: (let ((buf (get-buffer-create "*FOO*")) (txt (propertize "hello-world" 'face '(:foreground "red")))) (with-current-buffer buf (insert txt)) (display-buffer buf)) See also the functions put-text-property and add-text-properties.
    – lawlist
    Nov 16, 2022 at 2:39

1 Answer 1

2

Your code did not work because it is littered with typos. Here is a working version:

    (defun mcolour (mesg colour buffer)
      "TODO."
      (let ((buf (get-buffer-create buffer))
            (txt (propertize mesg
                              'face `(:foreground ,colour))))
        (with-current-buffer buffer (insert txt))
        (display-buffer buffer)))

In detail: you need bufferr to be buffer in the last line; mesg to be txt in the insert form and you must use the back-quote/comma mechanism to evaluate colour.

2
  • It was not having a comma.
    – Dilna
    Nov 16, 2022 at 9:23
  • The comma works together with the backquote: you were missing both. All the other typos could have been avoided with some care (or by cutting and pasting and formatting @lawlist's comment), but this one you introduced by making colour a variable that needs to be evaluated inside a quoted expression: since quoting inhibits evaluation, you need the backquote/comma mechanism to inhibit some of the evaluation, but allowing the evaluation of colour.
    – NickD
    Nov 16, 2022 at 11:05

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.