views:

128

answers:

2

In Emacs is there a way to syntax-highlight the parentheses of quoted and backquoted sexps differently than the parens of other sexps so they stand out? e.g. I want these parens to be a different color than other parens:

(foo `(bar (baz)) quux)
      ^         ^
+1  A: 

Have a look at mic-paren, a minor mode built as an extension to the packages paren.el and stig-paren.el for Emacs. It features recognition of "escaped" sexps.

Now for special highlighting : if you look at the code, the behaviour of quoted sexp matching is governed by the variable paren-match-quoted-paren. When finding a couple of match sexps, the typeface change is made using statements such as:

(mic-overlay-put mic-paren-backw-overlay 'face paren-mismatch-face)

(with similar alternatives for matched, unmatched). It shouldn't be too hard to define an alternative font (similarly to what is done with paren-mismatch-face), and replace those typeface-changing statements by functions that use your alternative font if paren-match-quoted-paren is true.

Note: updated links to refer to latest version

huitseeker
The code you linked to is completely impenetrable to me. Thanks though.
Brian Carper
+1  A: 

You can apply the following patch to mic-paren (follow link for latest version, 3.8) to get what you want. Customize the newly created face paren-face-quoted-match which is glaringly set up to have a green foreground and orange background for testing purposes.

Now when you're next to a matched set of parenthesis preceded by a single open quote `, you'll get the quoted face. This example uses an orange background and green foreground - most likely colors you'll want to change.

Here's a picture of it in action: alt text

--- orig-mic-paren.el   2009-11-11 17:02:42.000000000 -0800
+++ mic-paren.el    2009-11-11 17:05:35.306263000 -0800
@@ -561,4 +561,16 @@
   :group 'mic-paren-matching)

+(defface paren-face-quoted-match
+  '((((class color)) (:foreground "green" :background "orange"))
+    (t (:reverse-video t)))
+  ""
+  :group 'faces
+  :group 'mic-paren-matching)
+
+(defcustom paren-quoted-match-face 'paren-face-quoted-match
+  "Mic-paren face used for a quoted paren"
+  :type 'face
+  :group 'mic-paren-matching)
+
 ;;; End of User Options
 ;;; ======================================================================
@@ -1052,5 +1064,9 @@
                   face (if mismatch
                            paren-mismatch-face
-                         paren-match-face)
+                         (save-excursion
+                          (if (progn (goto-char (- (min (point) opos) 1))
+                                     (looking-at "`"))
+                              paren-quoted-match-face
+                            paren-match-face)))
                   visible (when (pos-visible-in-window-p opos)
                             (save-excursion

To apply the patch, cut/paste the patch chunk to a file named mic.patch, and run the following:

patch mic-paren.el mic.patch
Trey Jackson