Subject: Re: bug#997: perl-mode: Incorrect syntax highlighting for regex at top-level

From: Mauro Aranda <[email-masked]>
Date: Mon, 19 Sep 2022 13:03:08 -0300

tags 997 patch
quit

[email-masked] (Harald Jörg) writes:

 > Correctly detecting "nothing" in a regex needs some care.  So I'd like
 > to treat this as a separate bug which remains open for now.

I attach a patch to fix this bug, by detecting that we moved to the
beginning of buffer when searching for something before the "/".

From e339176f338d8603c8671d27b3f116ca1b988364 Mon Sep 17 00:00:00 2001
From: Mauro Aranda <[email-masked]>
Date: Mon, 19 Sep 2022 12:58:44 -0300
Subject: [PATCH] perl-mode: / is a regexp match if there's nothing before it

* lisp/progmodes/perl-mode.el (perl-syntax-propertize-function):
A "/" that starts the first statement is a regexp match.  (Bug#997)
* test/lisp/progmodes/cperl-mode-tests.el (cperl-test-bug-997): New
test.
---
 lisp/progmodes/perl-mode.el             |  1 +
 test/lisp/progmodes/cperl-mode-tests.el | 12 ++++++++++++
 2 files changed, 13 insertions(+)

diff --git a/lisp/progmodes/perl-mode.el b/lisp/progmodes/perl-mode.el
index 70cb460568..bd8f4ecd1c 100644
--- a/lisp/progmodes/perl-mode.el
+++ b/lisp/progmodes/perl-mode.el
@@ -280,6 +280,7 @@ perl-syntax-propertize-function
                                       (backward-sexp 1)
                                       (member (buffer-substring (point) end)
                                               perl--syntax-exp-intro-keywords)))
+                               (bobp)
                                (memq (char-before)
                                      '(?? ?: ?. ?, ?\; ?= ?! ?~ ?\( ?\[))))))
                nil ;; A division sign instead of a regexp-match.
diff --git a/test/lisp/progmodes/cperl-mode-tests.el b/test/lisp/progmodes/cperl-mode-tests.el
index db3feec93a..66039d6fc7 100644
--- a/test/lisp/progmodes/cperl-mode-tests.el
+++ b/test/lisp/progmodes/cperl-mode-tests.el
@@ -723,6 +723,18 @@ cperl-test-imenu-index
 
 ;;; Tests for issues reported in the Bug Tracker
 
+(ert-deftest cperl-test-bug-997 ()
+  "Test that we distinguish a regexp match when there's nothing before it."
+  (let ((code "# some comment\n\n/fontify me/;\n"))
+    (with-temp-buffer
+      (funcall cperl-test-mode)
+      (insert code)
+      (font-lock-ensure)
+      (goto-char (point-min))
+      (search-forward "/f")
+      (should (equal (get-text-property (point) 'face)
+                     'font-lock-string-face)))))
+
 (defun cperl-test--run-bug-10483 ()
   "Runs a short program, intended to be under timer scrutiny.
 This function is intended to be used by an Emacs subprocess in
-- 
2.34.1