title: How many backslashes are required to escape regexps in emacs' "Customize" mode?
body: <p>I'm trying to use emacs' <code>customize-group</code> packages to tweak some parts of my setup, and I'm stymied. I see things like this in my .emacs file after I make changes with customize:</p>
  
  <pre class="lang-el prettyprint-override"><code>'(tramp-backup-directory-alist (quote (("\\\\`.*\\\\'" . "~/.emacs.d/autobackups"))))
  </code></pre>
  
  <p>This was the result of putting the following into the customize text field: </p>
  
  <pre><code>Regexp matching filename: \\`.*\\'
  </code></pre>
  
  <p>This is a representative sample: I'm actually trying to change several things that want a regexp, and they all show this same problem. How many layers of quoting are there, really? I can't seem to find the magic number of backslashes to get the gosh-dang thing to do what I'm asking it to, even for the simplest regular expressions like <code>.*</code>. Right now, the given customization produces - nothing. It makes no change from emacs' default behavior. </p>
  
  <p>Better yet, where on earth is this documented? It's a little difficult to Google for, but I've been trying quite a few things there as well as in the official documentation and the Emacs wiki. Where is an authoritative source for how many dang backslashes one needs to make a regular expression in customize-mode actually work - or at the very least, to fail with some kind of warning instead of failing silently?</p>
  
  <hr>
  
  <p><strong>EDIT:</strong> As so often happens with questions asked in anger, I was asking the wrong question. Fortunately the answers below, led me to the answer to the question that I needed, which was about quoting rules. I'm going to try to write down what I learned here, because I find the documentation and Googleable resources to be maddeningly obscure about this. So here are the quoting rules I found by trial and error, and I hope that they help someone else, inspire correction, or both.</p>
  
  <p>When an emacs customize-mode buffer asks you for a "Regexp matching filename", it is being, as emacs often is, both terse and idiosyncratic (how often <a href="http://en.wikipedia.org/wiki/Richard_Stallman" rel="nofollow noreferrer">the creator's personality</a> is imparted to the creation!).  It means, for one thing, a regexp that will be compared to the <em>whole path</em> of the file in search of a match, not just to the name of the file itself as you might assume from the term "filename". This is the same sense of "filename" used in emacs' <code>buffer-file-name</code> function, for example. </p>
  
  <p>Further, although if you put <code>foo</code> in the field, you'll see <code>"foo"</code> (with double-quotes) written to the actual file, that's not enough quoting and not the right quoting. You need to quote your regexp with the quoting style that, as far as I can tell, only emacs uses: the ``backtick-foo-single-quote'`scheme. And then you need to <em>escape that,</em> making it <code>\&#x60;backslash-backtick-foo-backslash-single-quote\'</code> (and if you think that's a headache to input in Markdown, it's more so in emacs). </p>
  
  <p>On top of this, emacs appears to have a rule that the <code>.</code> regexp special character does not match a <code>/</code> at the beginning of filenames, so, as was happening to me above, the classic <code>.*</code> pattern will appear to match nothing: to match "all files", you actually need the regexp <code>/.*</code>, which then you stuff into the quote format of customize-mode to produce <code>\&#x60;/.*\'</code>, after which customize paints <em>another</em> layer of escaping onto it and writes it to the customization file. </p>
  
  <p>The final result for one of my efforts - a setting such that #autosave# files don't gunk up the directory you're working in, but instead all live in one place:</p>
  
  <pre class="lang-el prettyprint-override"><code>(custom-set variables
    '(auto-save-file-name-transforms (quote (
      ("\\`/[^/]*:\\([^/]*/\\)*\\([^/]*\\)\\'" "~/.emacs.d/autobackups/\\2" t)
      ("\\`/.*/\\(.*?\\)\\'" "~/.emacs.d/autobackups/\\1" t)
  ))))
  </code></pre>
  
  <p>Backslashes in elisp are a far greater threat to your sanity than parentheses. </p>
  
  <hr>
  
  <p><strong>EDIT 2</strong>: Time for me to be wrong again. I finally found the relevant documentation (through reading <a href="https://stackoverflow.com/questions/7572411/beginning-and-end-of-the-string-in-emacs-regexps">another Stack Overflow question</a>, of course!): <a href="http://www.gnu.org/s/emacs/manual/html_node/elisp/Regexp-Backslash.html#Regexp-Backslash" rel="nofollow noreferrer">Regexp Backslash Constructs</a>. The crucial point of confusion for me: the backtick and single quote are <em>not</em> quoting in this context: they're the equivalent of perl's <code>^</code> and <code>$</code> special characters. The backslash-backtick construct matches an empty string anchored at the beginning of the string being checked for a match, and the backslash-single-quote construct matches the empty string at the end of the string-under-consideration. And by "string under consideration," I mean "buffer, which just happens to contain only a file path in this case, but you need to match the whole dang thing if you want a match at all, since this is elisp's global regexp behavior."</p>
  
  <p>Swear to god, it's like dealing with an alien civilization. </p>
  
  <hr>
  
  <p><strong>EDIT 3</strong>: In order to avoid confusing future readers - </p>
  
  <ul>
  <li><code>\`</code> is the emacs regex for "the beginning of the buffer." (cf <a href="http://perldoc.perl.org/perlre.html#Assertions" rel="nofollow noreferrer">Perl's <code>\A</code></a>)</li>
  <li><code>\'</code> is the emacs regex for "the end of the buffer." (cf Perl's <code>\Z</code>)</li>
  <li><code>^</code> is the common-idiom regex for "the beginning of the line." It can be used in emacs.</li>
  <li><code>$</code> is the common-idiom regex for "the end of the line." It can be used in emacs.</li>
  </ul>
  
  <p>Because regex searches across multi-line bodies of text are more common in emacs than elsewhere (e.g. <code>M-x occur</code>), the backtick and single-quote special characters are used in emacs, and as best as I can tell, they're used in the context of customize-mode because if you are considering generic unknown input to a customize-mode field, it could contain newlines, and therefore you want to use the beginning-of-buffer and end-of-buffer special characters because the beginning and end of the <em>input</em> are not guaranteed to be the beginning and end of a <em>line.</em></p>
  
  <p>I am not sure whether to regret hijacking my own Stack Overflow question and essentially turning it into a blog post.</p>