Question

I am a bit of an emacs noob, but not quite sure how to even find this. I use solarized light color theme, which is a low contrast theme with intelligent accents to keep things readable. One thing they do in the vim version is highlight the string delimiters (meaning double quote and single quote in some languages) differently then the rest of the string to make them stand out more.

The emacs port of the theme does not have this, however I have seen some pretty crazy stuff happen with the font locking mechanism (like this http://www.emacswiki.org/emacs/HexColour), so I figured it was possible.

So is it possible to highlight string delimeters? if so, where should I look for more info on how to do it?

EDIT:

trying to get Jon O.'s answer working. First I tried

(defface my-string-delimiter-face
  '((t (:foreground "red" :weight bold)))
   "My custom face for string delimiters")

(add-hook 'after-change-major-mode-hook 
          (lambda () 
            (font-lock-add-keywords nil '(("\\s\"\\|\\s|" 0 solarized-string-delimiter-face t)))))

in an attempt to add it to every mode (didn't work)

So then I tried replacing the hook expression with

(font-lock-add-keywords 'emacs-lisp '(("\\s\"\\|\\s|" 0 solarized-string-delimiter-face t)))

In an attempt to just get things working, same thing (didn't work)

Was it helpful?

Solution

You can use font-lock-add-keywords to highlight string delimiters by doing something like the following:

(font-lock-add-keywords 'foo-mode '(("\\s\"\\|\\s|" 0 'my-string-delimiter-face t)))

The regexp "\\s\"\\|\\s|" matches a single character, which must have syntax class "string quote" (the \\s\" part) or "generic string delimiter" (the \\s| part). \s matches various different character syntax-classes, which you can read about in the Elisp manual under (elisp)Syntax Tables and (elisp)Regexp Backslash.

It's a little easier to read if you see it without string escaping: \s"\|\s|

The 0 means to apply the face to the entire matched string, and the t at the end makes this face override any faces that are already present on the string (since many modes will highlight the entire string including the delimiters with font-lock-string-face or similar)

'foo-mode could be the quoted name of any mode (e.g. 'emacs-lisp-mode, 'php-mode), or nil to make this change buffer-local in the current buffer only. (In that case you should probably put this in the mode-hooks of the modes you want to apply it to)

my-string-delimiter-face could be any existing face, or you could define one by doing something like the following:

(defface my-string-delimiter-face
  '((t (:foreground "red" :weight bold)))
   "My custom face for string delimiters")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top