Definitely worth trying, thanks. I came up with: (defun python-shell-multiline--apply-font-lock (limit) (let ((end (cdr-safe comint-last-prompt))) (if (and end (> limit end)) (let ((font-lock-keywords python-font-lock-keywords) (font-lock-syntactic-face-function #'python-font-lock-syntactic-face-function) (start (max end (point)))) (font-lock-flush start limit) (font-lock-ensure start limit))))) (set (make-local-variable 'font-lock-keywords) '(python-shell-multiline--apply-font-lock))) I can verify that font-lock-ensure is being called on an appropriate region (lots of times). With either font-lock-flush or font-lock-ensure, no actual fontification occurs. With both of these together (as above), this error is signaled: Error during redisplay: (jit-lock-function 179) signaled (void-function python-font-lock-keywords-level-1) Note that python-font-lock-keywords is a *list* beginning with this symbol: Python-font-lock-keywords is a variable defined in ‘python.el’. Its value is (python-font-lock-keywords-level-1 python-font-lock-keywords-level-1 python-font-lock-keywords-level-2 python-font-lock-keywords-maximum-decoration) Not sure why jit-lock-function would be evaluating it like an sexp. > On Apr 11, 2021, at 12:31 PM, Stefan Monnier wrote: > >> But then, why bother round-tripping text out to a special-use buffer anyway, >> vs. just letting font-lock operate in-situ in the shell buffer itself using >> python-mode’s fairly simple font-lock-defaults. The only thing needed to >> make this work is asking font-lock to ignore all the text with ‘field of >> ‘output? > > Maybe you can try something like the following? > > (defvar python--font-lock-keywords ...) > (defvar python-font-lock-keywords > '(python--apply-font-lock)) > (defun python--apply-font-lock (limit) > (while (< (point) limit) > (let ((next-boundary (find-next-boundary limit))) > (if (we-should-skip-this-block) > (goto-char next-boundary) > (let ((font-lock-keywords python--font-lock-keywords)) > (font-lock-ensure (point) limit)))))) > > > -- Stefan >