unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: ndame <laszlomail@protonmail.com>
To: "help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>
Subject: Re: backward-sexp throws scan-error "Unbalanced parentheses", but forward-sexp works
Date: Wed, 19 Jan 2022 21:30:41 +0000	[thread overview]
Message-ID: <txPbqa4U_7y4Wk3nMOua212ZsgDysLlBM5j_BPlbQ1vaKo_gyWlr9yURfSL96equVaf5p7WKbkIzNrd-gcHSbtDcVAtacxSrBFIHHcVJ5lc=@protonmail.com> (raw)
In-Reply-To: <nY2Zo90KD4pUvLR39E4aDs_yM1kp46FfdqEXG4aCeh1mrBtzGPd57kj2jOUMqtcRQwQAH--ub-Iiik1XOVtq40dj6_MI6cY5MYcRCezF3k8=@protonmail.com>

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

> I haven't looked in detail, but I did notice that it let-binds
> `after-change-functions` which can interfere with other code adding
> functions to this hook.
>
> Does the quick&dirty patch below help?

I started emacs -Q and though I have the mode installed via
package.el, I did not want to setup package stuff, so I just
copied the code from here:

https://raw.githubusercontent.com/Malabarba/aggressive-indent-mode/master/aggressive-indent.el

Turns out with this latest version the problem is not reproducible.

I made a diff compared to my installed version and apparently the
new, working version adds the `after-change-functions` which you wanted
to remove among other things . (diff attached)

So the problem went away, because I upgraded to the latest version,
which is good, but I'm still wondering how the syntax parser got
confused because of indentation stuff.


[-- Attachment #2: diff --]
[-- Type: application/octet-stream, Size: 5962 bytes --]

-;;; aggressive-indent.el --- Minor mode to aggressively keep your code always indented
+;;; aggressive-indent.el --- Minor mode to aggressively keep your code always indented  -*- lexical-binding:t -*-
 
-;; Copyright (C) 2014 Free Software Foundation, Inc.
+;; Copyright (C) 2014-2021 Free Software Foundation, Inc
 
 ;; Author: Artur Malabarba <emacs@endlessparentheses.com>
 ;; URL: https://github.com/Malabarba/aggressive-indent-mode
-;; Package-Version: 20190218.2331
-;; Version: 1.8.4
-;; Package-Requires: ((emacs "24.1") (cl-lib "0.5"))
+;; Version: 1.10.0
+;; Package-Requires: ((emacs "24.3"))
 ;; Keywords: indent lisp maint tools
 ;; Prefix: aggressive-indent
 ;; Separator: -
@@ -71,7 +70,7 @@
 ;;
 ;; This program is free software; you can redistribute it and/or
 ;; modify it under the terms of the GNU General Public License
-;; as published by the Free Software Foundation; either version 2
+;; as published by the Free Software Foundation; either version 3
 ;; of the License, or (at your option) any later version.
 ;;
 ;; This program is distributed in the hope that it will be useful,
@@ -355,8 +354,7 @@
 Call `aggressive-indent-region-function' between L and R, and
 then keep indenting until nothing more happens."
   (interactive "r")
-  (let ((p (point-marker))
-        was-begining-of-line)
+  (let ((p (point-marker)))
     (set-marker-insertion-type p t)
     (unwind-protect
         (progn
@@ -391,11 +389,11 @@
   "List of (left right) limit of regions changed in the last command loop.")
 (make-variable-buffer-local 'aggressive-indent--changed-list)
 
-(defun aggressive-indent--proccess-changed-list-and-indent ()
+(defun aggressive-indent--process-changed-list-and-indent ()
   "Indent the regions in `aggressive-indent--changed-list'."
   (unless (or (run-hook-wrapped 'aggressive-indent--internal-dont-indent-if #'eval)
               (aggressive-indent--run-user-hooks))
-    (let ((inhibit-modification-hooks t)
+    (let ((after-change-functions (remove 'aggressive-indent--keep-track-of-changes after-change-functions))
           (inhibit-point-motion-hooks t)
           (indent-function
            (if (cl-member-if #'derived-mode-p aggressive-indent-modes-to-prefer-defun)
@@ -460,26 +458,31 @@
              nil)
             (t val)))))))
 
-(defun aggressive-indent--indent-if-changed ()
-  "Indent any region that changed in the last command loop."
-  (if (not (buffer-live-p (current-buffer)))
-      (cancel-timer aggressive-indent--idle-timer)
-    (when (and aggressive-indent-mode aggressive-indent--changed-list)
-      (save-excursion
-        (save-selected-window
-          (aggressive-indent--while-no-input
-            (aggressive-indent--proccess-changed-list-and-indent))))
-      (when (timerp aggressive-indent--idle-timer)
-        (cancel-timer aggressive-indent--idle-timer)))))
+(defun aggressive-indent--maybe-cancel-timer ()
+  "Cancel and remove the timer if it is set."
+  (when (timerp aggressive-indent--idle-timer)
+    (cancel-timer aggressive-indent--idle-timer)
+    (setq aggressive-indent--idle-timer nil)))
+
+(defun aggressive-indent--indent-if-changed (buffer)
+  "Indent any region that changed in BUFFER in the last command loop."
+  (if (not (buffer-live-p buffer))
+      (aggressive-indent--maybe-cancel-timer)
+    (with-current-buffer buffer
+      (when (and aggressive-indent-mode aggressive-indent--changed-list)
+        (save-excursion
+          (save-selected-window
+            (aggressive-indent--while-no-input
+              (aggressive-indent--process-changed-list-and-indent))))
+        (aggressive-indent--maybe-cancel-timer)))))
 
 (defun aggressive-indent--keep-track-of-changes (l r &rest _)
   "Store the limits (L and R) of each change in the buffer."
   (when aggressive-indent-mode
     (push (list l r) aggressive-indent--changed-list)
-    (when (timerp aggressive-indent--idle-timer)
-      (cancel-timer aggressive-indent--idle-timer))
+    (aggressive-indent--maybe-cancel-timer)
     (setq aggressive-indent--idle-timer
-          (run-with-idle-timer aggressive-indent-sit-for-time t #'aggressive-indent--indent-if-changed))))
+          (run-with-idle-timer aggressive-indent-sit-for-time t #'aggressive-indent--indent-if-changed (current-buffer)))))
 
 ;;; Minor modes
 ;;;###autoload
@@ -510,14 +513,15 @@
           (aggressive-indent--local-electric t))
         (add-hook 'after-change-functions #'aggressive-indent--keep-track-of-changes nil 'local)
         (add-hook 'after-revert-hook #'aggressive-indent--clear-change-list nil 'local)
-        (add-hook 'before-save-hook #'aggressive-indent--proccess-changed-list-and-indent nil 'local))
+        (add-hook 'before-save-hook #'aggressive-indent--process-changed-list-and-indent nil 'local)
+        (add-hook 'kill-buffer-hook #'aggressive-indent--maybe-cancel-timer nil 'local))
     ;; Clean the hooks
-    (when (timerp aggressive-indent--idle-timer)
-      (cancel-timer aggressive-indent--idle-timer))
+    (aggressive-indent--maybe-cancel-timer)
     (remove-hook 'after-change-functions #'aggressive-indent--keep-track-of-changes 'local)
     (remove-hook 'after-revert-hook #'aggressive-indent--clear-change-list 'local)
-    (remove-hook 'before-save-hook #'aggressive-indent--proccess-changed-list-and-indent 'local)
-    (remove-hook 'post-command-hook #'aggressive-indent--softly-indent-defun 'local)))
+    (remove-hook 'before-save-hook #'aggressive-indent--process-changed-list-and-indent 'local)
+    (remove-hook 'post-command-hook #'aggressive-indent--softly-indent-defun 'local)
+    (remove-hook 'kill-buffer-hook #'aggressive-indent--maybe-cancel-timer 'local)))
 
 (defun aggressive-indent--local-electric (on)
   "Turn variable `electric-indent-mode' on or off locally, as per boolean ON."

Diff finished.  Wed Jan 19 22:15:23 2022

  parent reply	other threads:[~2022-01-19 21:30 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-12 16:49 backward-sexp throws scan-error "Unbalanced parentheses", but forward-sexp works ndame via Users list for the GNU Emacs text editor
2022-01-12 17:33 ` ndame via Users list for the GNU Emacs text editor
2022-01-12 21:06   ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-01-13  5:40 ` ndame via Users list for the GNU Emacs text editor
2022-01-13  5:54   ` ndame via Users list for the GNU Emacs text editor
2022-01-13  6:23     ` Emanuel Berg via Users list for the GNU Emacs text editor
2022-01-13  9:04     ` ndame via Users list for the GNU Emacs text editor
2022-01-19 20:47       ` ndame
2022-01-19 20:52         ` Emanuel Berg via Users list for the GNU Emacs text editor
2022-01-19 20:57         ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-01-19 21:30         ` ndame [this message]
2022-01-19 23:44           ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-01-20  4:47           ` ndame
2022-01-20 17:20             ` Stefan Monnier via Users list for the GNU Emacs text editor

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='txPbqa4U_7y4Wk3nMOua212ZsgDysLlBM5j_BPlbQ1vaKo_gyWlr9yURfSL96equVaf5p7WKbkIzNrd-gcHSbtDcVAtacxSrBFIHHcVJ5lc=@protonmail.com' \
    --to=laszlomail@protonmail.com \
    --cc=help-gnu-emacs@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.
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).