Search code examples
emacsfont-lock

How to use emacs/elisp to highlight parts of font-locked source code


I have some log files that contain the directory paths and file names (and line numbers) for C++, C, Java and C# source code files. I have written a regex to search for these file names and line numbers and open the source code file and position the insertion point at the specified line number (kinda like the next-error function when used with the compile command).

Given a file name that appears in the log file multiple times I want to add highlighting (and selectively remove highlighting) to the source code file display window/buffer.

I can do this with functions like add-text-properties, remove-text-properties and add-face-text-property (where is remove-face-text-property?) if there is no font-lock (keyword color coding). These functions don't work if font-lock is turned on!

How do I do this if the font-lock is turned on? I see that the incremental search feature does it so it is possible to add and remove highlighting with out messing up the font-lock coding.

Thanks Siegfried


Solution

  • Use overlays instead of text-properties. E.g. to highlight with face bold the text between BEG and END, do something like:

    (let ((ol (make-overlay BEG END)))
      (overlay-put ol 'face 'bold))