Backslashitis

10/10/2015

Regular expressions are of utmost importance in Emacs, simply because two of the basic operations inside a buffer are searching for one (re-search-forward) and deciding whether point precedes a certain regular expression (looking-at). This allows one to do basic parsing, syntax highlighting, traversing buffers and much more.

A common annoyance with regular expressions is that if they’re given in string form, both special constructs and backslashes themselves need to be escaped, depending on the context they’re used in even multiple times. The amount of backslashes can be reduced by either introducing a special string or regular expression type, usually known as “raw string”. A number of languages do have this feature, including C++11, Ruby, Python, Go, Perl, C# and many more, but Emacs Lisp doesn’t. There has been a few proposals on the emacs-devel mailing list, but so far each one has been dropped with the reasoning that the code scanning over strings for movement in buffers (like, M-f and M-b) would need to be adjusted as well.

Makes one wonder how the most severe case of backslashitis looks like…

"...
- If a string, use as regexp to search *backward* from the label.  Context
  is then the text following the end of the match.  E.g. setting this to
  \"\\\\\\\\caption[[{]\" will use the caption in a figure or table
  environment.
  \"\\\\\\\\begin{eqnarray}\\\\|\\\\\\\\\\\\\\\\\" works for eqnarrays.
..."

Seventeen backslashes in a row! One is for the quote, the remaining sixteen get turned into eight when viewing this docstring in a help buffer, the Emacs Lisp reader makes four of them and search operates on two, simply because one uses two backslashes inside an eqnarray environment for a linebreak.