help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: font-lock and multiline comments


From: Stefan Monnier
Subject: Re: font-lock and multiline comments
Date: Fri, 09 Feb 2007 15:28:29 -0500
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.93 (gnu/linux)

> I am writing a new major mode for an in-house programming language.
> We'd like to have emacs do syntactic highlighting on the comments. The
> language supports two kinds of comments:

> // In-line C++ style comments

> Comments
>   Multi-line comments flagged by Comments/EndComments
>   Here is another line
> EndComments

> The in-line style I was able to easily add with a syntax table
> modification. The multi-line comments, however, I can't do because the
> syntax tables only support comment syntax with 1 or 2 characters.

> I then turned to font-lock mode. Adding the following font-lock
> keyword didn't work if there were more than maybe 7 or 8 lines between
> Comments and EndComments:

> '("^[ \t]*Comments\\(.\\|\n\\)*EndComments" . font-lock-comment-face)

> Apparently font-lock regions aren't designed to span multiple lines?

> Can someone tell me how to get font-lock to recognize and properly
> mark these multi-line comments?

Use `font-lock-syntactic-keywords' along the lines of

   (defvar foo-font-lock-syntactic-keywords
       '(("^Comments\\(\n\\)" (1 "< b"))
         ("EndComments$"
          (0 (unless (eq (match-beginning 0) (point-min))
               (put-text-property (1- (match-beginning 0)) (match-beginning 0)
                                  'syntax-table (eval-when-compile
                                                  (string-to-syntax "> b")))
               (put-text-property (1- (match-beginning 0)) (match-end 0)
                                  'font-lock-multiline t)
               nil)))))

and then set font-lock-defaults to

    (foo-font-lock-keywords ...blablabla...
     (font-lock-syntactic-keywords . foo-font-lock-syntactic-keywords))

See the docstring of font-lock-defaults to see how many entries to place
before the f-l-s-k one.

Note that it will not work correctly for

   Comments
   EndComments

:-(


        Stefan


reply via email to

[Prev in Thread] Current Thread [Next in Thread]