[Top][All Lists]

[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: Mon, 12 Apr 2021 22:07:13 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

> (defun python-shell-multiline--apply-font-lock (limit)
>   (if-let ((process (get-buffer-process (current-buffer)))
>          (pmark (process-mark)))
>       (if (> limit pmark)
>         (let ((font-lock-keywords python-shell-multiline-font-lock-keywords)
>               (font-lock-syntactic-face-function
>                #'python-font-lock-syntactic-face-function)
>               (start (max pmark (point))))
>           (with-syntax-table python-mode-syntax-table
>             (font-lock-flush start limit)
>             (font-lock-ensure start limit))))))

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.

> It seems to me this general technique could be useful for lots of
> different “mixed fontification buffers”, simply rebinding
> font-lock-keywords/syntax table/etc. as needed for the region under
> consideration. It’s much simpler than the round-trip copy + full
> buffer re-fontify that many modes use, with no extra buffers to
> manage, post-command-hooks, etc.

Indeed that matches my intuition.
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.  ]


reply via email to

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