2

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)))
9

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.