I'm writing a major mode for a language that uses (* ... *)
as comments.
I'm trying to match multi-line comments. e.g.
(*
foo
bar
*)
I was initially doing "(\\*.*\\*)"
, however, .
doesn't match newline.
I then tried what the wiki on multi-line regex said: "(\\*[\0-\377[:nonascii:]]*\\*)"
. However, this leads to matching that I wasn't expecting. For instance, if I have the following:
(* comment 1*)
code block 1
(* comment 2*)
code block 2
(* comment 3*)
code block 3
It highlights everything from comment 1
to comment 3
as a big comment, including code block 1
and code block 2
.
How can I get it to match (*
to the first closing *)
rather than the last one?
.
matches any character but a newline, you have to to use(.|\n)
. This however is inefficient when there are several newlines. Better match multiple lines with(.*\n?)*
instead. The newline is optional so that the expression can end in the middle of a line.[\0-\377[:nonascii:]]
is better than(.*\n?)*
, which is so slow it crashes emacs on my computer. However, my problem isn't matching newline, but matching(*
with the first*)
. As I explain in the question, it matches the last*)
, which is not good. I have tried matching everything but(*
in the blocks, but it doesn't seem to match things character by character, rather, it matches the whole thing.syntax-ppss
to check if you are in a comment,forward-comment
to move over comments etc.