unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: emacs-devel@gnu.org
Subject: Re: Syntax-ppss flush should happen in after-change hook
Date: Sat, 16 Mar 2019 11:53:53 -0400	[thread overview]
Message-ID: <jwvpnqq7sd3.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: 87sgvm50dk.fsf@gmail.com

> 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 #<marker at 77 in test.tmd> nil nil nil t)
>   comment-search-forward(#<marker at 77 in test.tmd> t)
>   uncomment-region-default-1(63 77 nil)
>   #f(compiled-function () #<bytecode 0x280781d>)()
>   combine-change-calls-1(63 77 #f(compiled-function () #<bytecode 0x280781d>))
>   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




  reply	other threads:[~2019-03-16 15:53 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-16 15:01 Syntax-ppss flush should happen in after-change hook Vitalie Spinu
2019-03-16 15:53 ` Stefan Monnier [this message]
2019-03-16 17:35   ` Vitalie Spinu
2019-03-16 18:09     ` Stefan Monnier
2019-03-16 19:56       ` Vitalie Spinu
2019-03-16 22:41         ` 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=jwvpnqq7sd3.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --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).