all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#47902: cperl-mode: unwanted expansion of '$continue' [PATCH]
@ 2021-04-19 22:30 Harald Jörg
  2021-04-20 20:56 ` Stefan Kangas
  0 siblings, 1 reply; 2+ messages in thread
From: Harald Jörg @ 2021-04-19 22:30 UTC (permalink / raw)
  To: 47902

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

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

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Avoid expansion of "$continue" --]
[-- Type: text/x-diff, Size: 2705 bytes --]

From 753c185393b399059d348a6277b46c2203c47886 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Harald=20J=C3=B6rg?= <haj@posteo.de>
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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-04-20 20:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-04-19 22:30 bug#47902: cperl-mode: unwanted expansion of '$continue' [PATCH] Harald Jörg
2021-04-20 20:56 ` Stefan Kangas

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.