From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: =?UTF-8?B?Sm/Do28gVMOhdm9yYQ==?= Newsgroups: gmane.emacs.devel Subject: Re: contextual refontification (was: [PATCH] Re: Algorithm in electric-pair--unbalanced-strings-p unsuitable for CC Mode) Date: Wed, 10 Jul 2019 10:32:28 +0100 Message-ID: References: <20190708100539.GD4529@ACM> <20190708164501.GB5244@ACM> <20190708180551.GD5244@ACM> <20190709095222.GA5230@ACM> <81716eae-14f1-a427-b6e0-46e861adc93a@yandex.ru> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000c24786058d505bdd" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="105870"; mail-complaints-to="usenet@blaine.gmane.org" Cc: Alan Mackenzie , =?UTF-8?Q?Cl=C3=A9ment_Pit=2DClaudel?= , emacs-devel , Dmitry Gutov To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Jul 10 11:34:13 2019 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1hl8zL-000RPs-An for ged-emacs-devel@m.gmane.org; Wed, 10 Jul 2019 11:34:11 +0200 Original-Received: from localhost ([::1]:59358 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hl8zJ-0005kX-Uc for ged-emacs-devel@m.gmane.org; Wed, 10 Jul 2019 05:34:10 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:50713) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hl8yG-0005fH-F2 for emacs-devel@gnu.org; Wed, 10 Jul 2019 05:33:07 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hl8yE-0003Qb-JC for emacs-devel@gnu.org; Wed, 10 Jul 2019 05:33:04 -0400 Original-Received: from mail-io1-xd35.google.com ([2607:f8b0:4864:20::d35]:46045) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hl8y3-0003Dd-Sp for emacs-devel@gnu.org; Wed, 10 Jul 2019 05:32:57 -0400 Original-Received: by mail-io1-xd35.google.com with SMTP id g20so3205324ioc.12 for ; Wed, 10 Jul 2019 02:32:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=X52Z1OxbAEfUM1h8aDgbmE1zslA+VOQnUv2l3YQ0UE4=; b=B4we/iAVGSaoE2X/+08FGNpKwQUtWKwAX6NWGNsNau1WEsg46L7EK/USgkNFg/amee tKGuTset07/JivVBZ9ZLGNlVb6EMVtuX7KQjZgk/OA6XyNLUMnFg6KSE3W+CDIby+oij vvhoT++1ju0g0ACKm4jlVZojORkygj3ffC8epVNAv1fbJGZxHDBiFoXzTF/5DPctj+RU FCnukpm8SrSIkoXCTfDOt7rg3TEXwDj0uQQ6A9EwHCglo+OwXpScmYw4m/4fQ0C+SIrI kdmwlmKMmojMZb3RoIbOLfCuj4hyi/Hzl2miDL+b8D/vV2YoalJCKO9U3AIhRQ9N36AD J9kA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=X52Z1OxbAEfUM1h8aDgbmE1zslA+VOQnUv2l3YQ0UE4=; b=qiMWCsa1D5VIstqKu5T+7U7IXUycyuQdtD4jLnMw+xIsgqIdhPXpQuPeCprNY68f97 b9EdDbgEHXoAY4OqsMcOvq7lkUmEMcqMJevYN7uIVZY/fuQAcySnM+MA5jKH0DDCjf5q up4q6XnmBKyRYBlltTjikLVYeJpq1K+vjxdMP5+UqThjy5z0sSlBBLXCHQlBekB2/k1y AJUykHvYBiMr/3dm4zi/nYkvoVYNgT5pcZLy+nr7wCQGXWX6tkl4sZ+gFY5JqUGUgWA7 y9YETWQ7a7rR/ovm2fNbh13cIuivNZRQh0eYjP3UxaDNYHUm1jIw42uvTc4ywdm4uwo6 ziqg== X-Gm-Message-State: APjAAAWYRqsx41ACsiMi3mJ+SCiq16j03E4JXSJF+zN1ik72Iwm/1xCO TuQt+OnLAmbDl6D9YUwEzcNTSy30L1c/XCfNKfc= X-Google-Smtp-Source: APXvYqzX2LbRZAbY30K13VxjD3D77XkrGLX0THobtOwuorcSaN3ZdQ18LffG/w8YnHgdk9TPQN3984gXnfU+nd3hRX4= X-Received: by 2002:a02:69d1:: with SMTP id e200mr34408803jac.138.1562751159910; Wed, 10 Jul 2019 02:32:39 -0700 (PDT) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::d35 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:238493 Archived-At: --000000000000c24786058d505bdd Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Jul 9, 2019 at 4:40 PM Stefan Monnier wrote: > IOW make it so that font-lock: > - delays refontification after the current line if the end of line is > now within a string/comment (and wasn't before). > - "un-delays" this fontification if the end of line is now outside of any > string/comment (but was within it before). > - of course, with some kind of timeout, but one longer than 0.5s. > > Or something like that. Here's my take on that. Cl=C3=A9ment/Dmitry if you find the time test this.= .. It should, in principle, reduce "blinking" in all modes (for people who aren't using electric-pair-mode or something like that). If it doesn't, I've made some kind of false assumption. Btw to restart jit-lock-mode in a running emacs, you need to do sth like this: (progn (jit-lock-mode -1) (cancel-timer jit-lock-context-timer) (setq jit-lock-context-timer nil) (jit-lock-mode 1)) Jo=C3=A3o diff --git a/lisp/jit-lock.el b/lisp/jit-lock.el index 48998a81fe..0d7c434f3f 100644 --- a/lisp/jit-lock.el +++ b/lisp/jit-lock.el @@ -123,6 +123,15 @@ jit-lock-context-time :type '(number :tag "seconds") :group 'jit-lock) +(defcustom jit-lock-multiline-string-grace 2 + "Like `jit-lock-context-time' but for unterminated multiline strings. +If the user has just opened an unterminated string at EOL, give +him/her some grace time before deciding it is a multi-line string +and fontifying accordingly, do so only if the user stares idle at +that string for more than this many seconds." + :type '(number :tag "seconds") + :group 'jit-lock) + (defcustom jit-lock-defer-time nil ;; 0.25 "Idle time after which deferred fontification should take place. If nil, fontification is not deferred. @@ -232,7 +241,7 @@ jit-lock-mode (unless jit-lock-context-timer (setq jit-lock-context-timer (run-with-idle-timer jit-lock-context-time t - 'jit-lock-context-fontify))) + (jit--lock-context-timer-function)))) (setq jit-lock-context-unfontify-pos (or jit-lock-context-unfontify-pos (point-max)))) @@ -306,6 +315,44 @@ jit-lock--debug-fontify pos 'fontified))))))))) (setq jit-lock-defer-buffers nil)))) +(defun jit--lock-context-timer-function () + (let (last ; point marker the last time context timer was run + in-s-or-c-p ; t if in string or comment that time around + grace-timer ; idle timer for fontifying unterminated s-or-c, or nil + ) + (lambda () + (let ((point (point-marker)) + (new-in-s-or-c-p + (nth 8 (save-excursion (syntax-ppss (line-end-position)))))) + (if (and jit-lock-multiline-string-grace + last + (eq (marker-buffer last) (current-buffer)) + (eq (line-number-at-pos last) (line-number-at-pos))) + (cond ((and (null in-s-or-c-p) new-in-s-or-c-p (null grace-timer)) + (setq grace-timer + (run-with-idle-timer jit-lock-multiline-string-grace nil + (lambda () + (jit-lock-context-fontify) + (setq grace-timer nil))))) + ((and in-s-or-c-p + (null new-in-s-or-c-p) + grace-timer) + (cancel-timer grace-timer) + (setq grace-timer nil)) + (t + ;; no state change, leave everything as it was + )) + ;; left the line somehow or customized feature away: cancel + ;; everything, resume normal operation. + (when grace-timer + (cancel-timer grace-timer) + (setq grace-timer nil))) + ;; proceed as usual, unless grace-timer is counting + (unless grace-timer + (jit-lock-context-fontify)) + (setq last point in-s-or-c-p new-in-s-or-c-p))))) + + (defun jit-lock-register (fun &optional contextual) "Register FUN as a fontification function to be called in this buffer. FUN will be called with two arguments START and END indicating the region --000000000000c24786058d505bdd Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Tue, Jul 9, 2019 at 4:40 PM Stefan Mon= nier <monn= ier@iro.umontreal.ca> wrote:
IOW make it so that font-loc= k:
- delays refontification after the current line if the end of line is
=C2=A0 now within a string/comment (and wasn't before).
- "un-delays" this fontification if the end of line is now outsid= e of any
=C2=A0 string/comment (but was within it before).
- of course, with some kind of timeout, but one longer than 0.5s.

Or something like that.

Here's my take on th= at. Cl=C3=A9ment/Dmitry if you find the time test this. ..
It should, in principle, reduce "blinking" in= all modes (for people who
aren't = using electric-pair-mode or something like that). If it doesn't,
I've made some kind of false assumption.=

Btw t= o restart jit-lock-mode in a running emacs, you need to
do sth like this:

(progn
= =C2=A0 (jit-lock-mode -1)
=C2=A0 (canc= el-timer jit-lock-context-timer)
=C2= =A0 (setq jit-lock-context-timer nil)
= =C2=A0 (jit-lock-mode 1))

Jo=C3=A3o

diff --git a/lisp/jit-lock.el b/lisp/jit-lock.e= l
index 48998a81fe..0d7c434f3f 100644
--- a/lisp/jit-lock.el
+++ b= /lisp/jit-lock.el
@@ -123,6 +123,15 @@ jit-lock-context-time
=C2=A0 = =C2=A0:type '(number :tag "seconds")
=C2=A0 =C2=A0:group &= #39;jit-lock)
=C2=A0
+(defcustom jit-lock-multiline-string-grace 2+ =C2=A0"Like `jit-lock-context-time' but for unterminated multil= ine strings.
+If the user has just opened an unterminated string at EOL,= give
+him/her some grace time before deciding it is a multi-line string=
+and fontifying accordingly, do so only if the user stares idle at
+= that string for more than this many seconds."
+ =C2=A0:type '(n= umber :tag "seconds")
+ =C2=A0:group 'jit-lock)
+
= =C2=A0(defcustom jit-lock-defer-time nil ;; 0.25
=C2=A0 =C2=A0"Idle= time after which deferred fontification should take place.
=C2=A0If nil= , fontification is not deferred.
@@ -232,7 +241,7 @@ jit-lock-mode
= =C2=A0 =C2=A0 =C2=A0 =C2=A0(unless jit-lock-context-timer
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0(setq jit-lock-context-timer
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(run-with-idle-timer jit-lock-context-tim= e t
- =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 'jit-lock-context-= fontify)))
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (jit--lock-cont= ext-timer-function))))
=C2=A0 =C2=A0 =C2=A0 =C2=A0(setq jit-lock-context= -unfontify-pos
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(or jit-l= ock-context-unfontify-pos (point-max))))
=C2=A0
@@ -306,6 +315,44 @@ = jit-lock--debug-fontify
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 pos 'fontified)))))))))
=C2=A0 =C2=A0 =C2=A0 =C2=A0(setq jit-loc= k-defer-buffers nil))))
=C2=A0
+(defun jit--lock-context-timer-functi= on ()
+ =C2=A0(let (last =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0; poin= t marker the last time context timer was run
+ =C2=A0 =C2=A0 =C2=A0 =C2= =A0in-s-or-c-p =C2=A0 =C2=A0 ; t if in string or comment that time around+ =C2=A0 =C2=A0 =C2=A0 =C2=A0grace-timer =C2=A0; idle timer for fontifyin= g unterminated s-or-c, or nil
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0)
+ =C2=A0= =C2=A0(lambda ()
+ =C2=A0 =C2=A0 =C2=A0(let ((point (point-marker))
= + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(new-in-s-or-c-p
+ =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (nth 8 (save-excursion (syntax-ppss (line-e= nd-position))))))
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0(if (and jit-lock-multili= ne-string-grace
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 last
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (eq (= marker-buffer last) (current-buffer))
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 (eq (line-number-at-pos last) (line-number-at-pos)= ))
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(cond ((and (null in-s-or-= c-p) new-in-s-or-c-p (null grace-timer))
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq grace-timer
+ =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (run-wit= h-idle-timer jit-lock-multiline-string-grace nil
+ =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(lambda (= )
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0(jit-lock-context-fontify)
+ =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0(setq grace-timer nil)))))
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0((and in-s-or-c-p
+ =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(null new-in-s-o= r-c-p)
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0grace-timer)
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 (cancel-timer grace-timer)
+ =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq grace-timer nil))+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(t
+ = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ;; no state = change, leave everything as it was
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 ))
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0;; le= ft the line somehow or customized feature away: cancel
+ =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0;; everything, resume normal operation.
+ =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0(when grace-timer
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0(cancel-timer grace-timer)
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0(setq grace-timer nil)))
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0;; pr= oceed as usual, unless grace-timer is counting
+ =C2=A0 =C2=A0 =C2=A0 = =C2=A0(unless grace-timer
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(jit-lock-= context-fontify))
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0(setq last point in-s-or-= c-p new-in-s-or-c-p)))))
+
+
=C2=A0(defun jit-lock-register (fun &= amp;optional contextual)
=C2=A0 =C2=A0"Register FUN as a fontificat= ion function to be called in this buffer.
=C2=A0FUN will be called with = two arguments START and END indicating the region
=C2=A0
=
--000000000000c24786058d505bdd--