Siegfried
Siegfried

Reputation: 705

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

Upvotes: 3

Views: 427

Answers (2)

Drew
Drew

Reputation: 30701

Any highlighting that uses text property face is overruled by font-lock highlighting -- font-lock wants to win. In many cases you can still highlight text, but sooner or later font-lock erases that highlighting when it refontifies the buffer.

This does not apply to highlighting that uses overlays –- font-lock has no effect on overlays. So one answer is to just use overlays. However, if that does not work for your use case (there are some downsides to using overlays) there is still hope.

To prevent the interference of font-lock with other highlighting, the typical Emacs approach is to fool font-lock into thinking that it is font-lock highlighting, even when it does not involve font-lock-keywords.

But this has the effect that such highlighting is turned off when font-lock-mode is turned off. Whether this is a good thing or bad depends on your use case.

In vanilla Emacs you have no choice about this. Either the highlighting is not recognized by font-lock, which overrules it, or it is recognized as “one of its own”, in which case it is turned off when font-lock highlighting is turned off.

If you don't need your special highlighting when font-lock-mode is turned off, then you can just use text property font-lock-face instead of property face.

If you use library highlight.el to implement your highlighting then you can do that just by leaving option hlt-face-prop at its default value of font-lock-face. (Value font-lock-face means that the highlighting is controlled by font-lock. Value face means that font-lock does not recognize the highlighting.)

For the case where the option value is face, if you also use library font-lock+.el then there is no interference by font-lock –- the highlighting is independent of font-lock. Library font-lock+.el is loaded automatically by highlight.el, if it is in your load-path. It prevents font-locking from removing any highlighting face properties that you apply using the commands defined here.

See Highlight Library for more information.

Upvotes: 1

Stefan
Stefan

Reputation: 28531

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

Upvotes: 2

Related Questions