> On Apr 12, 2021, at 10:07 PM, Stefan Monnier 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 () #)() font-lock-default-fontify-region(188 189 nil) font-lock-fontify-region(188 189) #f(compiled-function (fun) #)(font-lock-fontify-region) run-hook-wrapped(#f(compiled-function (fun) #) font-lock-fontify-region) jit-lock--run-functions(188 189) jit-lock-fontify-now(188 688) jit-lock-function(188) redisplay_internal\ \(C\ function\)() > 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(!)"?