Subject: Re: bug#42168: 26.1; cperl-mode: Bad interpretation of $a++ / $b

From: Harald Jörg <[email-masked]>
Date: Thu, 13 Aug 2020 22:40:37 +0200

Hello Stefan,

Thank you for your thorough review.  I'm rather new to Emacs Lisp, and
also to the Emacs development workflows, so this is highly
appreciated.

Here I've attached a new version of the patch.
 - Copyright notice is now as it should be (now that the paperwork
   permits it)
 - All the stuff you suggested to be removed has been removed
 - The test has been renamed as suggested

In one place I've changed your suggestion:

> For now, I think you should simply place the file directly at:
>
> lisp/progmodes/cperl-mode-tests.el

I guess this was meant to read test/lisp/progmodes/cperl-mode-tests.el.

Also, I've added a ChangeLog-style attachment, as suggested by Lars
Ingebrigtsen in his comment to Bug#42355.
--
Cheers,
haj
diff --git a/lisp/progmodes/cperl-mode.el b/lisp/progmodes/cperl-mode.el
index 6122caf518..2d2713a36a 100644
--- a/lisp/progmodes/cperl-mode.el
+++ b/lisp/progmodes/cperl-mode.el
@@ -3979,6 +3979,9 @@ cperl-find-pods-heres
 				    (and (eq (preceding-char) ?.)
 					 (eq (char-after (- (point) 2)) ?.))
 				    (bobp))
+				;; { $a++ / $b } doesn't start a regex, nor does $a--
+				(not (and (memq (preceding-char) '(?+ ?-))
+					  (eq (preceding-char) (char-before (1- (point))))))
 				;;  m|blah| ? foo : bar;
 				(not
 				 (and (eq c ?\?)
diff --git a/test/lisp/progmodes/cperl-mode-tests.el b/test/lisp/progmodes/cperl-mode-tests.el
new file mode 100644
index 0000000000..030bdbe3db
--- /dev/null
+++ b/test/lisp/progmodes/cperl-mode-tests.el
@@ -0,0 +1,48 @@
+;;; cperl-mode-tests --- Test for cperl-mode -*- lexical-binding: t -*-
+
+;; Copyright (C) 2020 Free Software Foundation, Inc.
+
+;; Author: Harald Jörg <[email-masked]>
+;; Maintainer: Harald Jörg
+;; Keywords:       internal
+;; Homepage: https://github.com/HaraldJoerg/cperl-mode
+
+;;; Commentary:
+
+;; This is a collection of tests for the fontification of CPerl-mode.
+
+;; Run these tests interactively:
+;; (ert-run-tests-interactively '(tag :fontification))
+
+;;; Code:
+
+(defun cperl-test-face (text regexp)
+  "Returns the face of the first character matched by REGEXP in TEXT."
+  (interactive)
+  (with-temp-buffer
+      (insert text)
+      (cperl-mode)
+      (font-lock-fontify-buffer)
+      (goto-char (point-min))
+      (re-search-forward regexp)
+      (get-text-property (match-beginning 0) 'face)))
+
+(ert-deftest cperl-mode-test-bug-42168 ()
+  "Verify that '/' is a division after ++ or --, not a regexp.
+Reported in https://github.com/jrockway/cperl-mode/issues/45.
+If seen as regular expression, then the slash is displayed using
+font-lock-constant-face.  If seen as a division, then it doesn't
+have a face property."
+  :tags '(:fontification)
+  ;; The next two Perl expressions have divisions.  Perl "punctuation"
+  ;; operators don't get a face.
+  (let ((code "{ $a++ / $b }"))
+    (should (equal (cperl-test-face code "/" ) nil)))
+  (let ((code "{ $a-- / $b }"))
+    (should (equal (cperl-test-face code "/" ) nil)))
+  ;; The next two Perl expressions have regular expressions.  The
+  ;; delimiter of a RE is fontified with font-lock-constant-face.
+  (let ((code "{ $a+ / $b } # /"))
+    (should (equal (cperl-test-face code "/" ) font-lock-constant-face)))
+  (let ((code "{ $a- / $b } # /"))
+    (should (equal (cperl-test-face code "/" ) font-lock-constant-face))))