unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: JD Smith <jdtsmith@gmail.com>
Cc: emacs-devel@gnu.org
Subject: Re: Selective font-locking?
Date: Mon, 12 Apr 2021 22:07:13 -0400	[thread overview]
Message-ID: <jwveeff6j1r.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <0A6735A5-ABC0-48B0-B7C0-EACF5D85A32B@gmail.com> (JD Smith's message of "Mon, 12 Apr 2021 21:51:28 -0400")

> (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.  ]


        Stefan




  reply	other threads:[~2021-04-13  2:07 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-11 15:27 Selective font-locking? JD Smith
2021-04-11 16:31 ` Stefan Monnier
2021-04-11 20:54   ` JD Smith
2021-04-11 21:10     ` Stefan Monnier
2021-04-13  1:51       ` JD Smith
2021-04-13  2:07         ` Stefan Monnier [this message]
2021-04-13  3:33           ` JD Smith
2021-04-13  4:04             ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwveeff6j1r.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=emacs-devel@gnu.org \
    --cc=jdtsmith@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).