[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Selective font-locking?
From: |
Stefan Monnier |
Subject: |
Re: Selective font-locking? |
Date: |
Tue, 13 Apr 2021 00:04:28 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
>> On Apr 12, 2021, at 10:07 PM, Stefan Monnier <monnier@iro.umontreal.ca>
>> wrote:
>>
>> Calling `font-lock-flush` or `font-lock-ensure` from font-lock-keywords
>> is quite odd. I'd call something like `font-lock-fontify-region` instead.
>
> Using `font-lock-fontify-region` instead causes Emacs to become mostly
> unresponsive. Sending a USR2 reveals:
>
> Debugger entered--entering a function:
> * #f(compiled-function () #<bytecode 0x1fed77d1f329>)()
> font-lock-default-fontify-region(188 189 nil)
> font-lock-fontify-region(188 189)
> #f(compiled-function (fun) #<bytecode
> 0x1fed77d1f2f9>)(font-lock-fontify-region)
> run-hook-wrapped(#f(compiled-function (fun) #<bytecode 0x1fed77d1f2f9>)
> font-lock-fontify-region)
> jit-lock--run-functions(188 189)
> jit-lock-fontify-now(188 688)
> jit-lock-function(188)
> redisplay_internal\ \(C\ function\)()
That's not giving enough info to figure out what's going on, sorry.
My crystal ball suggests that maybe your matching function simply
forgets to return nil, so font-lock calls it again and again thinking
we're keeping matching "more stuff" without ever reaching limit.
But it does remind me that rather than mess with `font-lock-keywords`,
you could do a similar dance with `font-lock-fontify-region-function`
(and internally call `font-lock-default-fontify-region`).
It might prove simpler and more reliable.
>> There's one thing with which you might want to be careful, tho, which is
>> the `syntax-ppss` state. You might want to `narrow-to-region` around
>> the call to `font-lock-fontify-region` (maybe narrow to
>> pmark...(point-max)?).
>>
>> This is because in a shell buffer, some of the past interactions may
>> have been truncated (e.g. by `comint-truncate-buffer`), so you may end
>> up with (point-min) being in the middle of a string or something.
>> [ Similar problems can occur if the prompt itself contains funny characters
>> like unmatched quotes. or if past interactions include output which
>> is not lexically valid Python code. ]
>
> Well hmm, this is a bummer. l tested for this issue by inserting an entirely
> unmatched quote:
>
> In [12]: print(chr(39))
> '
>
> and this does affect the syntax (everything is a string). But unfortunately
> narrowing as follows doesn’t seem to fix this:
>
> (save-restriction
> (narrow-to-region pmark (point-max))
> (with-syntax-table python-mode-syntax-table
> (font-lock-flush start limit)
> (font-lock-ensure start limit)))))))
>
> I’m not sure it’s the same thing, but I found a related issue with
> `indent-for-tab-command'. In attempting to ignore the prompt for computing
> indentation, I narrowed to a region which excluded it, but indent.el calls
> `indent--funcall-widened’, which undoes my narrowing!
>
> Is there any way to specify "narrow to this region and don’t let anybody
> widen it(!)"?
It's called `font-lock-dont-widen`.
Stefan