unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* font-lock and multiline comments
@ 2007-02-09 20:14 bmaron2
  2007-02-09 20:28 ` Stefan Monnier
  0 siblings, 1 reply; 3+ messages in thread
From: bmaron2 @ 2007-02-09 20:14 UTC (permalink / raw)
  To: help-gnu-emacs

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?

Many thanks!!

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: font-lock and multiline comments
  2007-02-09 20:14 font-lock and multiline comments bmaron2
@ 2007-02-09 20:28 ` Stefan Monnier
  2007-02-09 20:45   ` bmaron2
  0 siblings, 1 reply; 3+ messages in thread
From: Stefan Monnier @ 2007-02-09 20:28 UTC (permalink / raw)
  To: help-gnu-emacs

> 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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: font-lock and multiline comments
  2007-02-09 20:28 ` Stefan Monnier
@ 2007-02-09 20:45   ` bmaron2
  0 siblings, 0 replies; 3+ messages in thread
From: bmaron2 @ 2007-02-09 20:45 UTC (permalink / raw)
  To: help-gnu-emacs

On Feb 9, 3:28 pm, Stefan Monnier <monn...@iro.umontreal.ca> wrote:
> > 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

Dear Stefan,

That worked perfectly. Thanks for the expert (and prompt) advice!

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-02-09 20:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-02-09 20:14 font-lock and multiline comments bmaron2
2007-02-09 20:28 ` Stefan Monnier
2007-02-09 20:45   ` bmaron2

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).