unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Mauro Aranda <maurooaranda@gmail.com>
To: 35925@debbugs.gnu.org
Cc: "積丹尼 Dan Jacobson" <jidanni@jidanni.org>
Subject: bug#35925: perl-mode wrecks formats
Date: Sat, 16 Sep 2023 20:03:14 -0300	[thread overview]
Message-ID: <08ea891e-55fc-145d-17fd-efd7c779f1d8@gmail.com> (raw)
In-Reply-To: <87lfysujo6.8.fsf@jidanni.org>

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

tags 35925 patch
quit


積丹尼 Dan Jacobson <jidanni@jidanni.org> writes:

 > In perl-mode,
 >
 > format FH =
 > @<< @# @<< @### @<<<<< @# @<<<< @#
 > (split)[0..5, 7..8]
 > .
 > write FH;
 >
 > becomes
 >
 > format FH =
 >     @<< @# @<< @### @<<<<< @# @<<<< @#
 >     (split)[0..5, 7..8]
 >     .
 >     write FH;
 >
 > wrecking output indentation and even introducing a syntax error on 
the "." line.
 >
 > (See man perlform.)
 >
 > Seen with C-x h TAB, emacs-version "26.1".

AFAICS, perl-mode doesn't recognize a format declaration when indenting.
I attach a patch that adds a check, plus some tests.

[-- Attachment #2: 0001-Fix-indentation-in-perl-mode-Bug-35925.patch --]
[-- Type: text/x-patch, Size: 6362 bytes --]

From 2a6416a26c3b7e11340a2b296dbbd9c1d8a51ca7 Mon Sep 17 00:00:00 2001
From: Mauro Aranda <maurooaranda@gmail.com>
Date: Sat, 16 Sep 2023 18:15:40 -0300
Subject: [PATCH] Fix indentation in perl-mode (Bug#35925)

* lisp/progmodes/perl-mode.el (perl--format-regexp): New defconst.
(perl--end-of-format-p): New function.
(perl-continuation-line-p): Use it.
(perl-calculate-indent): Use it.  Make the lines of the formlist stay
at column 0.

* test/lisp/progmodes/cperl-mode-resources/cperl-bug-35925.pl: New
test file.

* test/lisp/progmodes/cperl-mode-tests.el (cperl-test-bug-35925): New
test.
---
 lisp/progmodes/perl-mode.el                   | 30 ++++++++++++----
 .../cperl-mode-resources/cperl-bug-35925.pl   | 36 +++++++++++++++++++
 test/lisp/progmodes/cperl-mode-tests.el       | 14 ++++++++
 3 files changed, 74 insertions(+), 6 deletions(-)
 create mode 100644 test/lisp/progmodes/cperl-mode-resources/cperl-bug-35925.pl

diff --git a/lisp/progmodes/perl-mode.el b/lisp/progmodes/perl-mode.el
index 040ef187e97..b8d811baf0d 100644
--- a/lisp/progmodes/perl-mode.el
+++ b/lisp/progmodes/perl-mode.el
@@ -223,7 +223,10 @@ perl-quote-like-pairs
             "\\|=>"
             "\\|[?:.,;|&*=!~({[]"
             "\\|[^-+][-+]"    ;Bug#42168: `+' is intro but `++' isn't!
-            "\\|\\(^\\)\\)[ \t\n]*")))
+            "\\|\\(^\\)\\)[ \t\n]*"))
+
+  (defconst perl--format-regexp "^[ \t]*format.*=[ \t]*\\(\n\\)"
+  "Regexp to match the start of a format declaration."))
 
 (defun perl-syntax-propertize-function (start end)
   (let ((case-fold-search nil))
@@ -252,7 +255,7 @@ perl-syntax-propertize-function
       ;; Handle funny names like $DB'stop.
       ("\\$ ?{?\\^?[_[:alpha:]][_[:alnum:]]*\\('\\)[_[:alpha:]]" (1 "_"))
       ;; format statements
-      ("^[ \t]*format.*=[ \t]*\\(\n\\)"
+      (perl--format-regexp
        (1 (prog1 "\"" (perl-syntax-propertize-special-constructs end))))
       ;; Propertize perl prototype chars `$%&*;+@\[]' as punctuation
       ;; in `sub' arg-specs like `sub myfun ($)' and `sub ($)'.  But
@@ -946,6 +949,17 @@ perl-indent-line
 	(goto-char (- (point-max) pos)))
     shift-amt))
 
+(defun perl--end-of-format-p ()
+  "Non-nil if point is at the end of a format declaration, skipping whitespace."
+  (save-excursion
+    (skip-chars-backward " \t\n")
+    (beginning-of-line)
+    (when-let ((comm (and (looking-at "^\\.$")
+                          (nth 8 (syntax-ppss)))))
+      (goto-char comm)
+      (beginning-of-line)
+      (looking-at perl--format-regexp))))
+
 (defun perl-continuation-line-p ()
   "Move to end of previous line and return non-nil if continued."
   ;; Statement level.  Is it a continuation or a new statement?
@@ -959,7 +973,8 @@ perl-continuation-line-p
     (beginning-of-line)
     (perl-backward-to-noncomment))
   ;; Now we get the answer.
-  (unless (memq (preceding-char) '(?\; ?\} ?\{))
+  (unless (or (memq (preceding-char) '(?\; ?\} ?\{))
+              (perl--end-of-format-p))
     (preceding-char)))
 
 (defun perl-hanging-paren-p ()
@@ -999,7 +1014,9 @@ perl-calculate-indent
 	   (state (syntax-ppss))
 	   (containing-sexp (nth 1 state))
 	   ;; Don't auto-indent in a quoted string or a here-document.
-	   (unindentable (or (nth 3 state) (eq 2 (nth 7 state)))))
+           (unindentable (or (nth 3 state) (eq 2 (nth 7 state))))
+           (format (and (nth 3 state)
+                        (char-equal (nth 3 state) ?\n))))
       (when (and (eq t (nth 3 state))
                  (save-excursion
                    (goto-char (nth 8 state))
@@ -1009,7 +1026,7 @@ perl-calculate-indent
         (setq unindentable nil)
         (setq containing-sexp (nth 8 state)))
       (cond
-       (unindentable 'noindent)
+       (unindentable (if format 0 'noindent))
        ((null containing-sexp)          ; Line is at top level.
         (skip-chars-forward " \t\f")
         (if (memq (following-char)
@@ -1018,7 +1035,8 @@ perl-calculate-indent
           ;; indent a little if this is a continuation line
           (perl-backward-to-noncomment)
           (if (or (bobp)
-                  (memq (preceding-char) '(?\; ?\})))
+                  (memq (preceding-char) '(?\; ?\}))
+                  (perl--end-of-format-p))
               0 perl-continued-statement-offset)))
        ((/= (char-after containing-sexp) ?{)
         ;; line is expression, not statement:
diff --git a/test/lisp/progmodes/cperl-mode-resources/cperl-bug-35925.pl b/test/lisp/progmodes/cperl-mode-resources/cperl-bug-35925.pl
new file mode 100644
index 00000000000..e3f96241ab7
--- /dev/null
+++ b/test/lisp/progmodes/cperl-mode-resources/cperl-bug-35925.pl
@@ -0,0 +1,36 @@
+# This resource file can be run with cperl--run-testcases from
+# cperl-tests.el and works with both perl-mode and cperl-mode.
+
+# -------- Bug#35925: input -------
+format FH =
+@### @.### @###
+42, 3.1415, 0
+.
+write FH;
+
+# -------- Bug#35925: expected output -------
+format FH =
+@### @.### @###
+42, 3.1415, 0
+.
+write FH;
+
+# -------- Bug#35925: end -------
+
+# -------- format not as top-level: input -------
+foo: {
+    format STDOUT =
+^<<<<
+$foo
+.
+write;
+}
+# -------- format not as top-level: expected output -------
+foo: {
+    format STDOUT =
+^<<<<
+$foo
+.
+    write;
+}
+# -------- format not as top-level: end -------
diff --git a/test/lisp/progmodes/cperl-mode-tests.el b/test/lisp/progmodes/cperl-mode-tests.el
index 87d8ffa2d8d..172adfc79dd 100644
--- a/test/lisp/progmodes/cperl-mode-tests.el
+++ b/test/lisp/progmodes/cperl-mode-tests.el
@@ -1139,6 +1139,20 @@ cperl-test-bug-30393
      (cperl-indent-command)
      (forward-line 1))))
 
+(ert-deftest cperl-test-bug-35925 ()
+  "Check that indentation is correct after a terminating format declaration."
+  (cperl-set-style "PBP") ; Make cperl-mode use the same settings as perl-mode.
+  (cperl--run-test-cases
+   (ert-resource-file "cperl-bug-35925.pl")
+   (let ((tab-function
+          (if (equal cperl-test-mode 'perl-mode)
+              #'indent-for-tab-command
+            #'cperl-indent-command)))
+     (goto-char (point-max))
+     (forward-line -2)
+     (funcall tab-function)))
+  (cperl-set-style-back))
+
 (ert-deftest cperl-test-bug-37127 ()
   "Verify that closing a paren in a regex goes without a message.
 Also check that the message is issued if the regex terminator is
-- 
2.34.1


  parent reply	other threads:[~2023-09-16 23:03 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-27 13:11 bug#35925: perl-mode wrecks formats 積丹尼 Dan Jacobson
2019-07-09  2:11 ` Lars Ingebrigtsen
2019-07-09  9:06   ` 積丹尼 Dan Jacobson
2019-07-09 12:08     ` Lars Ingebrigtsen
2019-07-09 12:18       ` 積丹尼 Dan Jacobson
2019-07-09 15:11       ` Eli Zaretskii
2023-09-16 23:03 ` Mauro Aranda [this message]
2023-10-01  2:17   ` 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=08ea891e-55fc-145d-17fd-efd7c779f1d8@gmail.com \
    --to=maurooaranda@gmail.com \
    --cc=35925@debbugs.gnu.org \
    --cc=jidanni@jidanni.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).