unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: haj@posteo.de (Harald Jörg)
To: 47902@debbugs.gnu.org
Subject: bug#47902: cperl-mode: unwanted expansion of '$continue' [PATCH]
Date: Mon, 19 Apr 2021 22:30:04 +0000	[thread overview]
Message-ID: <87a6ptgb6b.fsf@hajtower> (raw)

[-- 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


             reply	other threads:[~2021-04-19 22:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-19 22:30 Harald Jörg [this message]
2021-04-20 20:56 ` bug#47902: cperl-mode: unwanted expansion of '$continue' [PATCH] Stefan Kangas

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=87a6ptgb6b.fsf@hajtower \
    --to=haj@posteo.de \
    --cc=47902@debbugs.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).