5

I was referring the source code of switch-to-buffer in elisp source code, window.el.gz. I am seeing some backward single quotes like, `

I know single quote is used to refer to symbols but what does backward single quote do?

Source code is here. When to use backward quote in elisp?

Reproducing the same here,

       ((pcase switch-to-buffer-in-dedicated-window
          (`nil (user-error
                 "Cannot switch buffers in a dedicated window"))
          (`prompt
           (if (y-or-n-p
                (format "Window is dedicated to %s; undedicate it"
                        (window-buffer)))
               (progn
                 (set-window-dedicated-p nil nil)
                 'force-same-window)
             (user-error
              "Cannot switch buffers in a dedicated window")))
          (`pop nil)
          (_ (set-window-dedicated-p nil nil) 'force-same-window)))
JeanPierre
  • 7,323
  • 1
  • 18
  • 37
Madhavan
  • 1,957
  • 12
  • 28
  • 1
    See also [What is the point of quote with single argument and comma?](http://emacs.stackexchange.com/questions/7602/what-is-the-point-of-quote-with-single-argument-and-comma-quote-arg) and [How to evaluate the variables before adding them to a list?](http://emacs.stackexchange.com/questions/7481/how-to-evaluate-the-variables-before-adding-them-to-a-list) – Dan Sep 11 '16 at 18:42
  • @Dan First link very interesting, thanks! In this precise question I see no reason to not "simply" use a quote instead of a backquote. Is this some stylistic convention? – JeanPierre Sep 11 '16 at 18:47
  • @JeanPierre: it looks like the backquote is needed in the context of `pcase`. – Dan Sep 11 '16 at 20:54
  • 1
    @Dan: Indeed I should have read https://www.gnu.org/software/emacs/manual/html_node/elisp/Pattern-matching-case-statement.html#Pattern-matching-case-statement first. Trying it, I found the backquote can actually be omitted, but not replaced with a straight quote. – JeanPierre Sep 16 '16 at 19:48

1 Answers1

12

Ask Emacs.

  1. C-h i, choose the Elisp manual.

  2. i backquote

You'll get to node Backquote, where you'll find a complete explanation of how backquotes are used in Emacs Lisp.

The first paragraph provides a summary description:

"Backquote constructs" allow you to quote a list, but selectively evaluate elements of that list. In the simplest case, it is identical to the special form ‘quote’ (described in the previous section; *note Quoting::).

You see there several examples, as well. Here is one:

`(1 2 (3 ,(+ 4 5)))
⇒ (1 2 (3 9))

That's equivalent to each of these, for example:

(cons 1 (cons 2 (list 3 (+ 4 5))))

(list 1 2 (list 3 (+ 4 5)))

Another possible summary: backquoting gives you a simple way to see, pictorially, what list structure you are creating. In other words, it is essentially syntactic sugar that most users typically find easier to read than the equivalent sexps that use lots of cons, list, or append calls.

A typical use case is thus for constructing Lisp code, especially complex code. And the most typical of such use cases is the code defining a Lisp macro, e.g., in the body of a defmacro expression.

All of that said, the particular case that you cite is a case of using pcase. If you choose to use backquoting in a typical pcase expression (which is what most people do) then the overall appearance is similar to that of a cond or a cl-case (Common Lisp case) expression. If you do not use backquoting then your pcase expression (like a defmacro body) is likely to be more difficult to read -- the pcase patterns are not so obvious.

Drew
  • 75,699
  • 9
  • 109
  • 225