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

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

Re: Best way to detect font-lock mode is on?


From: Ilya Zakharevich
Subject: Re: Best way to detect font-lock mode is on?
Date: Wed, 12 Jan 2011 03:46:30 +0000 (UTC)
User-agent: slrn/0.9.8.1pl1 (Linux)

On 2011-01-03, Tim X <address@hidden> wrote:
>
> Just wanting to know what people would suggest as the best way to detect
> if font-lock mode is enabled. 
>
> I have two functions I use in a mode to determine if the point is
> currently within a comment or a string. If font-lock mode is enabled,
> this is quite a fast operation as I can just test the text property.
> However, if font-lock is not enabled, I need to do some parsing to try
> and determine whether point is on a comment or string. I have defined
> two functions i.e. 
>
> (defun pde-in-comment-p ()
>   "Returns t if point is within a comment, nil otherwise."
>   (if font-lock-defaults
>       (eq (get-text-property (point) 'face) 'font-lock-comment-face)
>     (nth 4 (parse-partial-sexp (point-min) (point)))))
>
> (defun pde-in-string-p ()
>   "Return t if point within a string, nil otherwise."
>   (if font-lock-defaults
>       (eq (get-text-property (point) 'face) 'font-lock-string-face)
>     (nth 3 (parse-partial-sexp (point-min) (point)))))
>
> As you can see, I'm using font-lock-defaults to test whether font-lock
> is enabled.

No matter what is the test, your logic is not enough.  In cperl-mode,
I pepper all such functions with
   (cperl-update-fontification pos)
(sp?) so that lazy-locking is not playing time-sensitive tricks with
your logic.

Hope this helps,
Ilya


reply via email to

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