Subject: bug#48011: 27.2; Emacs freezes in modula 2 mode when entering a :

From: Gregory Heytings <[email-masked]>
Date: Sun, 25 Apr 2021 10:37:10 +0000

>
> I cannot reproduce this using the recipe in the Bugzilla report, neither 
> in Emacs 27.2 nor in the current master branch.  I see no hang.
>

The recipe isn't precise enough:

emacs -Q foo.mod
set SPC PROD SPC : SPC

Emacs hangs after the second SPC.  This bug is an old one, Emacs 24 has 
the same problem.  In fact an ever shorter recipe also works:

emacs -Q foo.mod
: SPC

>
> If C-g interrupts the hang, please set debug-on-signal non-nil, repeat 
> the recipe, press C-g, and post the Lisp backtrace here.
>

You probably meant "debug-on-quit".  Here is the backtrace:

smie-default-backward-token()
m2-smie-refine-colon()
m2-smie-backward-token()
smie-blink-matching-open()
self-insert-command(1 32)
funcall-interactively(self-insert-command 1 32)
call-interactively(self-insert-command nil nil)
command-execute(self-insert-command)

And here is the patch.

From 462ca0654d2aa2a2b6506f2e9a1a9ceb8e683cad Mon Sep 17 00:00:00 2001
From: Gregory Heytings <[email-masked]>
Date: Sun, 25 Apr 2021 10:33:48 +0000
Subject: [PATCH] Fix infloop in Modula-2 mode

* lisp/progmodes/modula2.el (m2-smie-refine-colon): Stop looping when
point does not move with forward-sexp (Bug#48011)
---
 lisp/progmodes/modula2.el | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/modula2.el b/lisp/progmodes/modula2.el
index 2a0374aa81..5756d91831 100644
--- a/lisp/progmodes/modula2.el
+++ b/lisp/progmodes/modula2.el
@@ -201,7 +201,10 @@ m2-smie-refine-colon
          ((zerop (length tok))
           (let ((forward-sexp-function nil))
             (condition-case nil
-                (forward-sexp -1)
+                (let ((p (point)))
+                  (forward-sexp -1)
+                  (if (= p (point))
+                      (setq res ":")))
               (scan-error (setq res ":")))))
          ((member tok '("|" "OF" "..")) (setq res ":-case"))
          ((member tok '(":" "END" ";" "BEGIN" "VAR" "RECORD" "PROCEDURE"))
-- 
2.30.2