Subject: bug#14481: 24.3.50; Highlighting escape sequences

From: Dmitry Gutov <[email-masked]>
Date: Mon, 27 May 2013 09:55:00 +0400

Most of the other text editors highlight stuff like \\, \t, \123 inside
string and regexp literals. For example, Vim and Sublime Text do.

In Emacs, we only have that in emacs-lisp-mode for grouping expressions,
I'm guessing because of their uncommon syntax.

Do we want it in other language modes? Here's some initial
implementation for ruby-mode and js-mode, using the face
font-lock-regexp-grouping-backslash, because it's the closest we have.

(defconst escape-sequence-re
  "\\(\\\\\\(\\(?:[0-9]\\|x\\)\\(?:[0-9]\\(?:[0-9]\\)?\\)?\\|.\\)\\)"
  "Regexp to match an escape sequence.
Currently handles octals (\\123), hexadecimals (\\x12) and
backslash followed by anything else.")

(font-lock-add-keywords
 'ruby-mode
 `((,escape-sequence-re
    (1 (let ((term (nth 3 (syntax-ppss))))
         (when (or (and (eq term ?')
                        (member (match-string 2) '("\\" "'")))
                   (memq term '(?\" ?/ ?\n t)))
           'font-lock-regexp-grouping-backslash))
       prepend)))
 'append)

(font-lock-add-keywords
 'js-mode
 `((,escape-sequence-re
    (1 (when (nth 3 (syntax-ppss))
         'font-lock-regexp-grouping-backslash)
       prepend)))
 'append)

If yes, where should this code live? The regexp itself should be either
the same or quite similar for many modern languages, so I would prefer
to have it in one place.