Subject: bug#21871: Emacs Lisp Mode (at least): spurious parens in column 0 don't get bold red highlighting.

From: Noam Postavsky <[email-masked]>
Date: Sat, 11 Apr 2020 11:00:35 -0400

Alan Mackenzie <[email-masked]> writes:

> In the Emacs manual page "Left Margin Paren", it says that:
>
>    To help you catch violations of this convention, Font Lock mode
>    highlights confusing opening delimiters (those that ought to be quoted)
>    in bold red.
>
> , where "this convention" is the convention of not putting opening parens
> in column 0 when they aren't at the beginning of defuns.
>
> In Emacs Lisp Mode, this highlighting isn't done.  It isn't in CC Mode,
> either.

The recent thread in emacs-devel[1] reminded me I wrote a (still
half-baked) patch for this.

[1]: https://lists.gnu.org/r/emacs-devel/2020-04/msg00402.html

>From b3794a2a87c6aafc2174162f72f716a5801668a4 Mon Sep 17 00:00:00 2001
From: Noam Postavsky <[email-masked]>
Date: Sat, 5 Aug 2017 12:31:02 -0400
Subject: [PATCH] [WIP] Restore highlighting of open parens in column-0
 (Bug#21871)

---
 lisp/emacs-lisp/lisp-mode.el |  1 +
 lisp/font-lock.el            | 38 ++++++++++++++++++++++++--------------
 2 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/lisp/emacs-lisp/lisp-mode.el b/lisp/emacs-lisp/lisp-mode.el
index 3b0f5493ee..50707fd10a 100644
--- a/lisp/emacs-lisp/lisp-mode.el
+++ b/lisp/emacs-lisp/lisp-mode.el
@@ -642,6 +642,7 @@ lisp-mode-variables
   (setq-local imenu-generic-expression lisp-imenu-generic-expression)
   (setq-local multibyte-syntax-as-symbol t)
   ;; (setq-local syntax-begin-function 'beginning-of-defun)  ;;Bug#16247.
+  (setq-local font-lock-syntax-paren-check t) ;; Bug#21871.
   (setq font-lock-defaults
 	`(,(if elisp '(lisp-el-font-lock-keywords
                        lisp-el-font-lock-keywords-1
diff --git a/lisp/font-lock.el b/lisp/font-lock.el
index e0955b74ab..565f4b22b4 100644
--- a/lisp/font-lock.el
+++ b/lisp/font-lock.el
@@ -1779,6 +1779,11 @@ font-lock-fontify-keywords-region
 
 ;; Various functions.
 
+(defvar-local font-lock-syntax-paren-check nil
+  "Major modes should set this to non-nil if they rely on the
+`open-paren-in-column-0-is-defun-start' convention for movement
+or fontification.")
+
 (defun font-lock-compile-keywords (keywords &optional syntactic-keywords)
   "Compile KEYWORDS into the form (t KEYWORDS COMPILED...)
 Here each COMPILED is of the form (MATCHER HIGHLIGHT ...) as shown in the
@@ -1798,24 +1803,29 @@ font-lock-compile-keywords
 	  (cons t (cons keywords
 			(mapcar #'font-lock-compile-keyword keywords))))
     (if (and (not syntactic-keywords)
-	     (let ((beg-function (with-no-warnings syntax-begin-function)))
-	       (or (eq beg-function #'beginning-of-defun)
-                   (if (symbolp beg-function)
-                       (get beg-function 'font-lock-syntax-paren-check))))
-	     (not beginning-of-defun-function))
+	     (or (and (let ((beg-function (with-no-warnings syntax-begin-function)))
+                        (or (eq beg-function #'beginning-of-defun)
+                            (if (symbolp beg-function)
+                                (get beg-function 'font-lock-syntax-paren-check))))
+                      (not beginning-of-defun-function))
+                 font-lock-syntax-paren-check))
 	;; Try to detect when a string or comment contains something that
 	;; looks like a defun and would thus confuse font-lock.
 	(nconc keywords
-	       `((,(if defun-prompt-regexp
-		       (concat "^\\(?:" defun-prompt-regexp "\\)?\\s(")
-		     "^\\s(")
-		  (0
-		   (if (memq (get-text-property (match-beginning 0) 'face)
-			     '(font-lock-string-face font-lock-doc-face
-			       font-lock-comment-face))
-		       (list 'face font-lock-warning-face
+               `((,(let ((regexp
+                          (if defun-prompt-regexp
+                              (concat "^\\(?:" defun-prompt-regexp "\\)?\\s(")
+                            "^\\s(")))
+                     (lambda (bound)
+                       (if open-paren-in-column-0-is-defun-start
+                           (re-search-forward regexp bound t))))
+                  (0
+                   (if (memq (get-text-property (match-beginning 0) 'face)
+                             '(font-lock-string-face font-lock-doc-face
+                                                     font-lock-comment-face))
+                       (list 'face font-lock-warning-face
                              'help-echo "Looks like a toplevel defun: escape the parenthesis"))
-		   prepend)))))
+                   prepend)))))
     keywords))
 
 (defun font-lock-compile-keyword (keyword)
-- 
2.11.0