Skip to main content
1 of 5
Gabriele
  • 1.7k
  • 11
  • 22

Change foreground color of a region (temporarily)

I need to change the "OBJECT" string foreground color (let's say "red") in all the occurrences of the string "\command{OBJECT}" in my buffer and I need to restore the original foreground color at the end of the job.

I figured out to do something like (it's a simplified MWE):

(defun propertize-command-arg ()
  (interactive)
  (save-excursion

    (goto-char (point-min))
    (while (search-forward-regexp "\\\\command{\\([a-z]+\\)}" nil t)
      (save-excursion
    (let ((b (match-beginning 1))
          (e (match-end 1))
          )
        (put-text-property b e SOMETHING)
      )))
    )
  )

The "SOMETHING" stuff is what I've not be able to to understand/find. I tried also the add-text-properties and add-face-text-property.

Please, keep in mind I need to do it temporarily. I'll restore the original appeareance of the text at the end of my job (script).

Gabriele
  • 1.7k
  • 11
  • 22