Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

elisp - Emacs Lisp: evaluate variable in alist

This is probably silly but I don't have enough Elisp knowledge to understand what is going on with respect to quoting and evaluation.

Suppose I have this Elisp code:

(add-to-list 'default-frame-alist '(width . 100))
(add-to-list 'default-frame-alist '(height . 50))

It will result in the expected default-frame-alist value:

((height 50)
 (width 100))

But now if I have this:

(setq my-frame-width 100)
(setq my-frame-height 50)
(add-to-list 'default-frame-alist '(width . my-frame-width))
(add-to-list 'default-frame-alist '(height . my-frame-height))

It will result in -

((height my-frame-height)
 (width my-frame-width))

and, judging from the frame geometry, never evaluates those variables. How do I make the actual values of my-frame-width and height appear in this alist? Do I have too many quotes? But I cannot remove any from the add-to-list evaluations...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this:

(setq my-frame-width 100)
(setq my-frame-height 50)
(add-to-list 'default-frame-alist `(width . ,my-frame-width))
(add-to-list 'default-frame-alist `(height . ,my-frame-height))

Using backquote instead of quote allows you to use , to force the evaluation of a parameter.

See the Elisp reference manual. Type C-x info, search for the elisp reference manual, then search for backquote within that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...