I used code like this:
(defvar my-defvar "test")
(completing-read "input: " '("1" "2" my-defvar))
Then M-x eval-region
.
I got "1", "2", my-defvar
in minibuffer.
My question is how to convert my-defvar
to string in a list.
In Lisp, the ´
-symbol will quote the rest of the expression. This means that the value will be the expression exactly as it is written, function calls are not evaluated, variables are not replaced with it's value etc.
The most straight-forward way is to use the function list
that creates a list of elements, after evaluating it's arguments, for example:
(completing-read "input: " (list "1" "2" my-defvar))
Of course, you could also use the backquote syntax, as suggested in another answer. This allows you to quote a complex expression but unquote (i.e. evaluate) parts of it. However, in this simple case I don't think it's the right tool for the job.
my-defvar
isn't being evaluated as a variable, it's being interpreted as a symbol.
See Emacs Lisp: evaluate variable in alist.
So:
(defvar my-defvar "test")
(completing-read "input: " `("1" "2" ,my-defvar))
should work.
Update: A more appropriate solution is given in @Lindydancer's answer, but I leave this here for reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With