1

How might I match a string for Font Lock fontification while ignoring comments within that string? e.g.

(setq str-regexp "^foobar$"
      comment-regexp "/\\*[^*]*\\*/")

Now how might I return match data to fontify the string "foobar" in the following buffer text?

foo/*comment*/bar
fo/*comment*/obar
fooba/*comment*/r

I can easily strip the comments from a buffer substring and then match a regular expression within that substring, but I don't know how this could then translate back to buffer markers/points suitable for Font Lock. Any ideas?

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
rnkn
  • 495
  • 3
  • 13
  • The program `int main() { int var; ++v/*comment*/ar; return 0; }` does not compile with `gcc`. So, at least in the C-language `var` and `v/*comment*/ar` are **not** the same. On the other hand `int main() { int var; ++/*comment*/var; return 0; }` compiles fine. Such things should be taken into account in a context sensitive search. But, I do not know whether your question is related to any programming language. So, may be this comment is irrelevant. – Tobias Apr 06 '14 at 09:16
  • Thanks for the tip, but I'm not writing for C-lang, I'm writing for Fountain! – rnkn Apr 07 '14 at 03:27

3 Answers3

1

Try using your match code inside macro with-comments-hidden from library hide-comnts.el.

EmacsWiki page Hide Or Ignore Comments describes the macro and the library briefly.

Drew
  • 29,895
  • 7
  • 74
  • 104
  • This seems to be the ticket, however I've run into a new problem: I'm working with two values each for `comment-start` and `comment-end` (which mirror the C++ style) and `hide-comnt.el` doesn't seem to like it. I figure I can run the macro inside itself, but with `//` comments, where `comment-end` is "", it wants to chomp the newline as well. Any ideas? – rnkn Apr 07 '14 at 04:02
  • p.s. this is with `(let ((hide-whitespace-before-comment-flag nil)) ...)` – rnkn Apr 07 '14 at 04:12
  • @mkn: Please follow up with a bug report by mail. I will need more details. (See the file header for mail info.) – Drew Apr 07 '14 at 14:07
0

Maybe you want to remove all the comments from the text:

Use the following regex to match the comments:

((?:\/\*)(.*?)(?:\*\/))

and then replace it with '' to result in only the non-commented text.

Resulting text:

foobar
foobar
foobar

Demo

http://regex101.com/r/lC9vY1

sshashank124
  • 31,495
  • 9
  • 67
  • 76
  • I need to leave the buffer contents unmodified, it's purely for fontification. This would be useful if it could be executed in a similar fashion to `(save-excursion ...)` but instead have the buffer contents restored after running BODY, but I'm not sure if that works. Ideas? – rnkn Apr 07 '14 at 04:48
0

Font-lock supports an alternative way to match words. Instead of using a regexp, it allows you to call a function to do the work for it.

In this function, you could match the word letter for letter, skipping comments as you go along. The only requirement of the function is to behave like re-search-forward, that is returning non-nil when a match is found and setting match-data accordingly.

As the matched word will contain comments, you will have to write your font-lock rule so that it will color the word, but leave the comments unchanged. You can do this by using setting the OVERRIDE flag in your rule to keep or append (depending on what you want to do).

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
  • This is mostly what I'm already doing, except for the hardest part (in my mind), which is writing the function and/or regexp that will match the regexp while skipping the comments. I don't suppose you'd be able to help me out with a short example that would match the `foobar` example as above...? – rnkn Apr 07 '14 at 04:33