From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: Syntax-ppss flush should happen in after-change hook Date: Sat, 16 Mar 2019 11:53:53 -0400 Message-ID: References: <87sgvm50dk.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="170966"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Mar 16 17:04:51 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.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h5Bnm-000iJE-NP for ged-emacs-devel@m.gmane.org; Sat, 16 Mar 2019 17:04:50 +0100 Original-Received: from localhost ([127.0.0.1]:43567 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h5Bnl-000651-Mq for ged-emacs-devel@m.gmane.org; Sat, 16 Mar 2019 12:04:49 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:33868) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h5BmM-00062x-QC for emacs-devel@gnu.org; Sat, 16 Mar 2019 12:03:23 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h5BdM-00042l-PQ for emacs-devel@gnu.org; Sat, 16 Mar 2019 11:54:05 -0400 Original-Received: from [195.159.176.226] (port=55642 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h5BdM-000427-D1 for emacs-devel@gnu.org; Sat, 16 Mar 2019 11:54:04 -0400 Original-Received: from list by blaine.gmane.org with local (Exim 4.89) (envelope-from ) id 1h5BdI-000Wf4-Gz for emacs-devel@gnu.org; Sat, 16 Mar 2019 16:54:00 +0100 X-Injected-Via-Gmane: http://gmane.org/ Cancel-Lock: sha1:j/49207j21U32AiY9LHTEKfQggE= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 195.159.176.226 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 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:234225 Archived-At: > Currently ppss flush happens in before-change hook. This leads to a real > possibility that syntax-propertize is called before the actual change and not > refreshed after the change. This could occur in at least two scenarios. > > First, when a hook placed after syntax-ppss-flush-cache in > before-change-functions invokes `syntax-propertize` (for example with a > search). If syntax-ppss-flush-cache is on after-change-functions then the risk is when a hook function is placed before it. Given the fact that add-hook defaults to placing functions at the head and that after-change-functions are much more commonly used than before-change-functions, the risks associated with placing it on after-change-functions are at least as high. FWIW, syntax-ppss-flush-cache was originally placed on after-change-functions and later changed to before-change-functions because of exactly that problem. > Second is when the actual function responsible for a change invokes > `syntax-propertize`. > > Here is a real case of uncomment-region in markdown-mode: > > markdown-syntax-propertize(63 823) > syntax-propertize(823) > internal--syntax-propertize(64) > parse-partial-sexp(63 # nil nil nil t) > comment-search-forward(# t) > uncomment-region-default-1(63 77 nil) > #f(compiled-function () #)() > combine-change-calls-1(63 77 #f(compiled-function () #)) > uncomment-region-default(63 77 nil) > uncomment-region(63 77 nil) I do see that combine-change-calls is involved and indeed combine-change-calls seems just fundamentally problematic in this respect (the problem would appear as well if we moved syntax-ppss-flush-cache on after-change-functions since it would mean that the syntax-ppss cache is left inconsistent during the execution of its body). The interaction between the syntax-ppss cache (and the syntax-propertize properties as well) and the changes that can take place during combine-change-calls can't be done right with a coarse "run something at the beginning and something else at the end". A crude solution could be the ad-hoc hack below. We could make it less ad-hoc by adding a way for any hook function (on b-c-f and a-c-f) to indicate whether it should stay active within combine-change-calls, e.g. with a symbol property `do-not-combine-change-function`. Stefan diff --git a/lisp/subr.el b/lisp/subr.el index 3a901b3174..abdc116e5f 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -3834,7 +3834,10 @@ combine-change-calls-1 (if (eq buffer-undo-list t) (setq result (funcall body)) (let (;; (inhibit-modification-hooks t) - before-change-functions after-change-functions) + (before-change-functions + (if (memq #'syntax-ppss-flush-cache before-change-functions) + '(syntax-ppss-flush-cache))) + after-change-functions) (setq result (funcall body))) (let ((ap-elt (list 'apply