In latex-mode many macros comes with syntax highlighting. For example:

enter image description here

where the argument of the command/macro \bibitem (Fisher:99) has font-lock-constant-face.

I need to do something similar with my own macros. E.g., if I have the string


I need to fontify the string "argument" so the function (face-at-point) will give me the output my-custom-face. Plus, I need to do it "temporarily". Indeed I need to do it during the running of my scripts. After that I need to restore the original face.

I'm not interested in the visual effect of the face. I only need the function (face-at-point) to return non nil when the pointer is on the argument of my macro.


(highlight-regexp)/(unhighlight-regexp) solved my problem. Here's, as an example, a piece of code that shows what I want to achieve.

      (highlight-regexp "\\\\mymacro{[^}]+}" 'font-lock-keyword-face)
      (when (string-match "TARGET_REGEXP" (buffer-string))
    (goto-char (point-min))
    (while (search-forward-regexp "TARGET_REGEXP" nil t)
      (redisplay t)
      (unless (string= (face-at-point) "font-lock-keyword-face")

          (let ((b (copy-marker (match-beginning 0)))
            (e (copy-marker (match-end 0))))

        (perform-replace (match-string 0)
                 t nil nil 1 nil b e )))))))

      (unhighlight-regexp "\\\\mymacro{[^}]+}"))

So I'll be asked for a replacement for all istances of "TARGET_REGEXP" (that may be a regexp) but "\mymacro{TARGET_REGEXP}".

| 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.