From 8c904b9db10cf3f6d3bf53da8818d2fe1a90f23d Mon Sep 17 00:00:00 2001
From: Shynur <one.last.kiss@outlook.com>
Date: Wed, 26 Apr 2023 21:16:20 +0800
Subject: [PATCH] Display offscreen matched openparen

Propertize the matched openparen displayed in the echo area in order to make
it prominent; use light font for non-context characters (i.e., Matches').
* lisp/simple.el (blink-matching-open): Light font for 'Matches'.
* lisp/simple.el (blink-paren-open-paren-line-string): Add a face to the
matched openparen.
---
 lisp/simple.el | 78 +++++++++++++++++++++++++++++++++++---------------
 1 file changed, 55 insertions(+), 23 deletions(-)

diff --git a/lisp/simple.el b/lisp/simple.el
index b621e1603bd..bb977517ab2 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -9312,47 +9312,79 @@ blink-matching-open
                  (delete-overlay blink-matching--overlay)))))
        ((not show-paren-context-when-offscreen)
         (minibuffer-message
-         "Matches %s"
-         (substring-no-properties
-          (blink-paren-open-paren-line-string blinkpos))))))))
+         #("Matches %s"
+           ;; Make the following text (i.e., %s) prominent.
+           0 7 (face (:weight light)))
+         (blink-paren-open-paren-line-string blinkpos)))))))
 
 (defun blink-paren-open-paren-line-string (pos)
-  "Return the line string that contains the openparen at POS."
+  "Return the line string that contains the openparen at POS.
+Remove the line string's properties; give the openparen a face."
   (save-excursion
     (goto-char pos)
     ;; Capture the regions in terms of (beg . end) conses whose
     ;; buffer-substrings we want to show as a context string.  Ensure
     ;; they are font-locked (bug#59527).
-    (let (regions)
-      ;; Show what precedes the open in its line, if anything.
+    (let (regions
+          openparen-idx)
       (cond
-       ((save-excursion (skip-chars-backward " \t") (not (bolp)))
-        (setq regions (list (cons (line-beginning-position)
-                                  (1+ pos)))))
+       ;; Show what precedes the open in its line, if anything.
+       ((save-excursion
+          (skip-chars-backward " \t")
+          (not (bolp)))
+        (let ((bol (line-beginning-position)))
+          (setq regions `((,bol . ,(1+ pos)))
+                openparen-idx (- pos bol))))
        ;; Show what follows the open in its line, if anything.
        ((save-excursion
           (forward-char 1)
           (skip-chars-forward " \t")
           (not (eolp)))
-        (setq regions (list (cons pos (line-end-position)))))
+        (setq regions `((,pos . ,(line-end-position)))
+              openparen-idx 0))
        ;; Otherwise show the previous nonblank line,
        ;; if there is one.
-       ((save-excursion (skip-chars-backward "\n \t") (not (bobp)))
-        (setq regions (list (cons (progn
-                                    (skip-chars-backward "\n \t")
-                                    (line-beginning-position))
-                                  (progn (end-of-line)
-                                         (skip-chars-backward " \t")
-                                         (point)))
-                            (cons pos (1+ pos)))))
+       ((save-excursion
+          (skip-chars-backward "\n \t")
+          (not (bobp)))
+        (setq regions `((,(let (bol)
+                            (skip-chars-backward "\n \t")
+                            (setq bol (line-beginning-position)
+                                  openparen-idx (- bol))
+                            bol)
+                         . ,(let (eol)
+                              (end-of-line)
+                              (skip-chars-backward " \t")
+                              (setq eol (point)
+                                    openparen-idx (+ openparen-idx
+                                                     eol
+                                                     ;; (length "...")
+                                                     3))
+                              eol))
+                        (,pos . ,(1+ pos)))))
        ;; There is nothing to show except the char itself.
-       (t (setq regions (list (cons pos (1+ pos))))))
+       (t
+        (setq regions `((,pos . ,(1+ pos)))
+              openparen-idx 0)))
       ;; Ensure we've font-locked the context region.
       (font-lock-ensure (caar regions) (cdar (last regions)))
-      (mapconcat (lambda (region)
-                   (buffer-substring (car region) (cdr region)))
-                 regions
-                 "..."))))
+      (let ((line-string
+             (mapconcat
+              (lambda (region)
+                (buffer-substring (car region) (cdr region)))
+              regions
+              "..."))
+            (1+openparen-idx (1+ openparen-idx)))
+        (setq line-string (substring-no-properties line-string))
+        (concat
+         (substring line-string
+                    0 openparen-idx)
+         (propertize (substring line-string
+                                openparen-idx 1+openparen-idx)
+                     ;; Maybe its face should be customizable.
+                     'face '(:foreground "green"))
+         (substring line-string
+                    1+openparen-idx))))))
 
 (defvar blink-paren-function 'blink-matching-open
   "Function called, if non-nil, whenever a close parenthesis is inserted.
-- 
2.34.1

