emacs-devel
[Top][All Lists]
Advanced

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

Re: highlighting large regions (comments) with font-lock keywords


From: Eric Schulte
Subject: Re: highlighting large regions (comments) with font-lock keywords
Date: Wed, 28 Sep 2011 07:08:58 -0600
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

Stefan Monnier <address@hidden> writes:

>> I'm working on a major mode for a new language [1] which comments
>> regions between \* ... *\.  I've properly (I believe) instantiated
>> font-lock keywords, and I have added a function to the
>> `font-lock-extend-region-functions' list to ensure that both ends of a
>> comment are always considered at the same time but unfortunately comment
>> highlighting often does not work.
>
>> Specifically, when I first enter a buffer and right after calling
>> `shen-mode' all comments are properly highlighted, however as I edit and
>> navigate in the buffer larger comments often lose fontification.
>
> Let's see:
>
>    (defun shen-font-lock-extend-region-comment ()
>      "Move fontification boundaries to contain whole comments."
>      (let ((changed nil))
>        (goto-char font-lock-beg)
>        (when (and (re-search-forward "\\\\\\*" font-lock-end t)
>                   (< (match-beginning 0) font-lock-beg))
>          (setq font-lock-beg (match-beginning 0)
>                changed t)
>          (when (and (re-search-forward "\\*\\\\" nil t)
>                     (> (match-end 0) font-lock-end))
>            (setq font-lock-end (match-end 0)
>                  changed t)))
>        changed))
>
> We have an obvious problem here: after (goto-char font-lock-beg) we're
> at font-lock-beg, and after (re-search-forward "\\\\\\*" font-lock-end
> t), we can only be further, so (match-beginning 0) will never be
> < font-lock-beg.
>
> BTW, why not simply do:
>
>    (defvar shen-mode-syntax-table
>      (let ((table (make-syntax-table)))
>        (modify-syntax-entry ?- "w" table)
>        (modify-syntax-entry ?\\ ". 14" table)
>        (modify-syntax-entry ?* ". 23" table)
>        (modify-syntax-entry ?? "w" table)
>        (modify-syntax-entry ?< "w" table)
>        (modify-syntax-entry ?> "w" table)
>        table)
>      "Syntax table to use in shen-mode.")
>

Hi Stefan,

Yes, the syntax-table method is definitely preferable.  Simpler and
(more importantly) works reliably.

>
> Oh, and I haven't looked any other part of the code, but just happened
> to see:
>
>   (add-to-list 'font-lock-extend-region-functions
>                'shen-font-lock-extend-region-comment t))
>
> Where the `local' arg is nil whereas it should be t (maybe the t was
> meant for `local' rather than for `append'?).
>

You're right again, I should have made font-lock-extend-region-functions
local before appending to it.  Luckily this font-lock-fanciness can be
discarded now thanks to the simple syntax table fix.

Thanks for the help, much appreciated -- Eric

>
>
>         Stefan

-- 
Eric Schulte
http://cs.unm.edu/~eschulte/



reply via email to

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