Subject: bug#47902: cperl-mode: unwanted expansion of '$continue' [PATCH]

From: [email-masked] (Harald Jörg)
Date: Mon, 19 Apr 2021 22:30:04 +0000

The symptom: When typing "$continue", abbrev expansion kicks in and
converts this to "$continue { }" - which immediately catches the eye as
suddenly it is formatted as a hash access.

How to reproduce from Emacs -Q:

    C-x b demo.pl <RET>
    M-: (setq cperl-electric-keywords t)
    cperl-mode <RET>
    $continue = 1;

It is quite usual to activate 'cperl-electric-keywords', either directly
or with the catch-all customization value 'cperl-hairy'.

Root cause: The expansion routine in 'cperl-electric-else' attempts to
verify that the keyword starts a statement, by jumping back over the
keyword with (backward-sexp 1).  For a scalar variable "$else" or
"$continue", this expression also skips back over the dollar (which has
syntax type "escape" in CPerl mode), and "$continue" does start a
statement, so unwanted expansion happens.

The patch replaces (backward-sexp 1) by (skip-chars-backward "[:alpha:]")
and avoids skipping over anything which doesn't belong to the keyword.

-- 
Cheers,
haj
>From 753c185393b399059d348a6277b46c2203c47886 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Harald=20J=C3=B6rg?= <[email-masked]>
Date: Tue, 20 Apr 2021 00:25:39 +0200
Subject: [PATCH] ; cperl-mode: Avoid abbrev expansion in variable names

* lisp/progmodes/cperl-mode.el (cperl-electric-else): don't expand
keywords which are scalar variables like '$continue'.
* test/lisp/progmodes/cperl-mode-tests.el
(cperl-test-hyperactive-electric-else): Verify that keywords are
expanded but variable names aren't.
---
 lisp/progmodes/cperl-mode.el            |  2 +-
 test/lisp/progmodes/cperl-mode-tests.el | 28 +++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/cperl-mode.el b/lisp/progmodes/cperl-mode.el
index 7878e91096..bff3e60e90 100644
--- a/lisp/progmodes/cperl-mode.el
+++ b/lisp/progmodes/cperl-mode.el
@@ -2224,7 +2224,7 @@ cperl-electric-else
 to nil."
   (let ((beg (point-at-bol)))
     (and (save-excursion
-	   (backward-sexp 1)
+           (skip-chars-backward "[:alpha:]")
 	   (cperl-after-expr-p nil "{;:"))
 	 (save-excursion
 	   (not
diff --git a/test/lisp/progmodes/cperl-mode-tests.el b/test/lisp/progmodes/cperl-mode-tests.el
index 14bc48b92f..1a13aec36a 100644
--- a/test/lisp/progmodes/cperl-mode-tests.el
+++ b/test/lisp/progmodes/cperl-mode-tests.el
@@ -495,4 +495,32 @@ cperl-test-bug-47112
                          'font-lock-constant-face
                        font-lock-string-face))))))
 
+(ert-deftest cperl-test-hyperactive-electric-else ()
+  "Demonstrate cperl-electric-else behavior.
+If `cperl-electric-keywords' is true, keywords like \"else\" and
+\"continue\" are expanded by a following empty block, with the
+cursor in the appropriate position to write that block.  This,
+however, must not happen when the keyword occurs in a variable
+\"$else\" or \"$continue\"."
+  (skip-unless (eq cperl-test-mode #'cperl-mode))
+  ;; `self-insert-command' takes a second argument only since Emacs 27
+  (skip-unless (not (< emacs-major-version 27)))
+  (with-temp-buffer
+    (setq cperl-electric-keywords t)
+    (cperl-mode)
+    (insert "continue")
+    (self-insert-command 1 ?\ )
+    (indent-region (point-min) (point-max))
+    (goto-char (point-min))
+    ;; cperl-mode creates a block here
+    (should (search-forward-regexp "continue {\n[[:blank:]]+\n}")))
+  (with-temp-buffer
+    (setq cperl-electric-keywords t)
+    (cperl-mode)
+    (insert "$continue")
+    (self-insert-command 1 ?\ )
+    (indent-region (point-min) (point-max))
+    (goto-char (point-min))
+    ;; No block should have been created here
+    (should-not (search-forward-regexp "{" nil t))))
 ;;; cperl-mode-tests.el ends here
-- 
2.20.1