unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: JD Smith <jdtsmith@gmail.com>
To: emacs-devel@gnu.org
Subject: Selective font-locking?
Date: Sun, 11 Apr 2021 11:27:39 -0400	[thread overview]
Message-ID: <7A948673-E156-42BA-BA50-E91986908BB5@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2358 bytes --]

What is the current state of applying font-lock to only portions of a buffer? I’ve seen font-lock+ which allows adding a ‘font-lock-ignore’ property, but it redefines font-lock functions and so may not be reliable long term.  

To make this concrete, here’s a usage case.  I’m currently extending python mode to include support for multiline input.  Here is how python mode currently fontifies the text being input at a shell prompt:
In a post-command-hook, after every change, copies the entire input after the prompt, sans properties, to a hidden “font-lock” buffer with python-mode enabled.
Calls font-lock-ensure, which refontifies this entire buffer.
Copies all the newly updated text properties back into the shell input.
So: super inefficient.  For single lines of input, this is "fast enough".  Once you have multi-line input with hundreds of lines or more, this incurs 50-100ms latency for each and every insertion, deletion, etc.   For a good approximation of how typing with this amount of latency feels, eval the following in *scratch*:

	(add-hook 'post-self-insert-hook (lambda () (sleep-for 0.1) ) nil t)

#1 is readily fixed by using an after-change-function which only updates the relevant text from the shell to the hidden buffer.  But #2 is the real killer, taking 70ms or more to completely re-fontify a decent sized block of input.  Adding region beg/end to font-lock-ensure doesn’t work; how do you know if a change occurred in a long string, for example?

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?  

It seems what would be ideal is tying font-lock-defaults to specific ‘field properties, so that only text with a given ‘field (or not matching a given ‘field) is fontified according to the matching set of font-lock rules (with no field specifier matching all text). This would make mixed multi-mode buffer fontification fairly straightforward. 

I’m sure this is simple-minded given the complexities font-lock has to solve, but there has to be a better solution than re-fontifying everything after each character is typed!

[-- Attachment #2: Type: text/html, Size: 3220 bytes --]

             reply	other threads:[~2021-04-11 15:27 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-11 15:27 JD Smith [this message]
2021-04-11 16:31 ` Selective font-locking? 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
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=7A948673-E156-42BA-BA50-E91986908BB5@gmail.com \
    --to=jdtsmith@gmail.com \
    --cc=emacs-devel@gnu.org \
    /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).