emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master f7e6875: cperl-mode: Add new value "PBP" for 'cperl-set-style'


From: Lars Ingebrigtsen
Subject: master f7e6875: cperl-mode: Add new value "PBP" for 'cperl-set-style'
Date: Thu, 17 Sep 2020 11:35:18 -0400 (EDT)

branch: master
commit f7e68759d033e2a503f47cd7d97b760bd92e375f
Author: Harald Jörg <haj@posteo.de>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    cperl-mode: Add new value "PBP" for 'cperl-set-style'
    
    * lisp/progmodes/cperl-mode.el (cperl-style-alist)
    (cperl-set-style): Add indentation style recommended by Damian Conway's
    book "Perl Best Practices".
    
    * test/lisp/progmodes/cperl-mode-tests.el
    (cperl-mode-test-indent-styles): Add a test to verify indentation
    and unraveling of conditionals (bug#43457).
---
 etc/NEWS                                           |  8 ++++
 lisp/progmodes/cperl-mode.el                       | 41 ++++++++++++++++----
 .../cperl-mode-resources/cperl-indent-styles.pl    | 44 ++++++++++++++++++++++
 test/lisp/progmodes/cperl-mode-tests.el            | 31 +++++++++++++++
 4 files changed, 116 insertions(+), 8 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 5b69e2f..3a7180c 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1179,6 +1179,14 @@ non-nil, even if protected by 'dbus-ignore-errors' 
otherwise.
 ---
 *** D-Bus events keep the type information of their arguments.
 
+** CPerl Mode
+
+---
+*** The command 'cperl-set-style' offers the new value "PBP".
+This value customizes Emacs to use the style recommended in Damian
+Conway's book "Perl Best Practices" for indentation and formatting
+of conditionals.
+
 
 * New Modes and Packages in Emacs 28.1
 
diff --git a/lisp/progmodes/cperl-mode.el b/lisp/progmodes/cperl-mode.el
index af179e2..8804e83 100644
--- a/lisp/progmodes/cperl-mode.el
+++ b/lisp/progmodes/cperl-mode.el
@@ -1234,6 +1234,7 @@ versions of Emacs."
          ["Auto fill" auto-fill-mode t])
         ("Indent styles..."
          ["CPerl" (cperl-set-style "CPerl") t]
+         ["PBP" (cperl-set-style  "PBP") t]
          ["PerlStyle" (cperl-set-style "PerlStyle") t]
          ["GNU" (cperl-set-style "GNU") t]
          ["C++" (cperl-set-style "C++") t]
@@ -1553,12 +1554,12 @@ Variables controlling indentation style:
  `cperl-min-label-indent'
     Minimal indentation for line that is a label.
 
-Settings for classic indent-styles: K&R BSD=C++ GNU PerlStyle=Whitesmith
-  `cperl-indent-level'                5   4       2   4
-  `cperl-brace-offset'                0   0       0   0
-  `cperl-continued-brace-offset'     -5  -4       0   0
-  `cperl-label-offset'               -5  -4      -2  -4
-  `cperl-continued-statement-offset'  5   4       2   4
+Settings for classic indent-styles: K&R BSD=C++ GNU PBP PerlStyle=Whitesmith
+  `cperl-indent-level'                5   4       2   4   4
+  `cperl-brace-offset'                0   0       0   0   0
+  `cperl-continued-brace-offset'     -5  -4       0   0   0
+  `cperl-label-offset'               -5  -4      -2  -2  -4
+  `cperl-continued-statement-offset'  5   4       2   4   4
 
 CPerl knows several indentation styles, and may bulk set the
 corresponding variables.  Use \\[cperl-set-style] to do this.  Use
@@ -6046,7 +6047,19 @@ if (foo) {
   stop;
 }
 
-### PerlStyle  (=CPerl with 4 as indent)               4/0/0/-4/4/t/nil
+### PBP (=Perl Best Practices)                         4/0/0/-4/4/nil/nil
+if (foo) {
+    bar
+       baz;
+  label:
+    {
+       boon;
+    }
+}
+else {
+    stop;
+}
+### PerlStyle  (=CPerl with 4 as indent)               4/0/0/-2/4/t/nil
 if (foo) {
     bar
        baz;
@@ -6149,6 +6162,18 @@ else
      (cperl-extra-newline-before-brace-multiline .  nil)
      (cperl-merge-trailing-else               .  t))
 
+    ("PBP"  ;; Perl Best Practices by Damian Conway
+     (cperl-indent-level               .  4)
+     (cperl-brace-offset               .  0)
+     (cperl-continued-brace-offset     .  0)
+     (cperl-label-offset               . -2)
+     (cperl-continued-statement-offset .  4)
+     (cperl-extra-newline-before-brace .  nil)
+     (cperl-extra-newline-before-brace-multiline .  nil)
+     (cperl-merge-trailing-else        .  nil)
+     (cperl-indent-parens-as-block     .  t)
+     (cperl-tab-always-indent          .  t))
+
     ("PerlStyle"                       ; CPerl with 4 as indent
      (cperl-indent-level               .  4)
      (cperl-brace-offset               .  0)
@@ -6220,7 +6245,7 @@ See examples in `cperl-style-examples'.")
   "Set CPerl mode variables to use one of several different indentation styles.
 The arguments are a string representing the desired style.
 The list of styles is in `cperl-style-alist', available styles
-are CPerl, PerlStyle, GNU, K&R, BSD, C++ and Whitesmith.
+are CPerl, PBP, PerlStyle, GNU, K&R, BSD, C++ and Whitesmith.
 
 The current value of style is memorized (unless there is a memorized
 data already), may be restored by `cperl-set-style-back'.
diff --git a/test/lisp/progmodes/cperl-mode-resources/cperl-indent-styles.pl 
b/test/lisp/progmodes/cperl-mode-resources/cperl-indent-styles.pl
new file mode 100644
index 0000000..0832f86
--- /dev/null
+++ b/test/lisp/progmodes/cperl-mode-resources/cperl-indent-styles.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use 5.020;
+
+# This file contains test input and expected output for the tests in
+# cperl-mode-tests.el, cperl-mode-test-indent-exp.  The code is
+# syntactically valid, but doesn't make much sense.
+
+# -------- PBP indent: input --------
+for my $foo (@ARGV)
+{
+...;
+}
+# -------- PBP indent: expected output --------
+for my $foo (@ARGV) {
+    ...;
+}
+# -------- PBP indent: end --------
+
+# -------- PBP uncuddle else: input --------
+{
+if (1 < 2)
+{
+say "Seems ok";
+} elsif (1 == 2) {
+say "Strange things are happening";
+} else {
+die "This world is backwards";
+}
+}
+# -------- PBP uncuddle else: expected output --------
+{
+    if (1 < 2) {
+       say "Seems ok";
+    }
+    elsif (1 == 2) {
+       say "Strange things are happening";
+    }
+    else {
+       die "This world is backwards";
+    }
+}
+# -------- PBP uncuddle else: end --------
diff --git a/test/lisp/progmodes/cperl-mode-tests.el 
b/test/lisp/progmodes/cperl-mode-tests.el
index 2eaf633..f0ff8e9 100644
--- a/test/lisp/progmodes/cperl-mode-tests.el
+++ b/test/lisp/progmodes/cperl-mode-tests.el
@@ -172,4 +172,35 @@ end of the statement."
             (setq got (concat "test case " name ":\n" (buffer-string)))
             (should (equal got expected))))))))
 
+(ert-deftest cperl-mode-test-indent-styles ()
+  "Verify correct indentation by style \"PBP\".
+Perl Best Practices sets some indentation values different from
+  the defaults, and also wants an \"else\" or \"elsif\" keyword
+  to align with the \"if\"."
+  (let ((file (expand-file-name "cperl-indent-styles.pl"
+                                cperl-mode-tests-data-directory)))
+    (with-temp-buffer
+      (cperl-set-style "PBP")
+      (insert-file-contents file)
+      (goto-char (point-min))
+      (while (re-search-forward
+              (concat "^# ?-+ \\_<\\(?1:.+?\\)\\_>: input ?-+\n"
+                      "\\(?2:\\(?:.*\n\\)+?\\)"
+                      "# ?-+ \\1: expected output ?-+\n"
+                      "\\(?3:\\(?:.*\n\\)+?\\)"
+                      "# ?-+ \\1: end ?-+")
+              nil t)
+        (let ((name (match-string 1))
+              (code (match-string 2))
+              (expected (match-string 3))
+              got)
+          (with-temp-buffer
+            (insert code)
+           (cperl-mode)
+           (indent-region (point-min) (point-max)) ; here we go!
+            (setq expected (concat "test case " name ":\n" expected))
+            (setq got (concat "test case " name ":\n" (buffer-string)))
+            (should (equal got expected)))))
+      (cperl-set-style "CPerl"))))
+
 ;;; cperl-mode-tests.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]