How to create a list with defvar in emacs

I used the following code:

(defvar my-defvar "test") (completing-read "input: " '("1" "2" my-defvar)) 

Then Mx eval-region . I got "1", "2", my-defvar in the minibuffer.

My question is how to convert my-defvar to a string in a list.

+6
source share
2 answers

In Lisp, the ยด character will quote the rest of the expression. This means that the value will be an expression exactly as it is written, function calls are not evaluated, variables are not replaced with a value, etc.

The easiest way is to use the list function, which creates a list of elements after evaluating its arguments, for example:

 (completing-read "input: " (list "1" "2" my-defvar)) 

Of course, you can also use the backquote syntax, as suggested in another answer. This allows you to quote a complex expression, but not specify (i.e. evaluate) parts of it. However, in this simple case, I do not think this is the right tool for the job.

+12
source

my-defvar not evaluated as a variable, it is interpreted as a character.

See Emacs Lisp: evaluate a variable in alist .

So:

 (defvar my-defvar "test") (completing-read "input: " `("1" "2" ,my-defvar)) 

must work.

Update: A more suitable solution is given in @Lindydancer's answer , but I leave this here for reference.

+6
source

Source: https://habr.com/ru/post/910684/


All Articles