0

This question already has an answer here:

I wish to define a variable to replace the hardcode string below:

(setq org-publish-project-alist
      '(
        ("blog" :components ("org-blog-docs" "org-blog-static"))

        ("org-blog-docs"
         :base-directory "~/myblog/_org"
         :base-extension "org"
         :publishing-directory "~/myblog/_posts"
         :recursive t
         :publishing-function blog-html-publish-to-html
         :headline-levels 4
         :html-extension "html"
         :body-only t
         )

        ("org-blog-static"
         :base-directory "~/myblog/_org"
         :base-extension "css\\|js\\|png\\|jpg\\|gif\\|pdf\\|mp3\\|ogg\\|swf"
         :publishing-directory "~/myblog/assets"
         :recursive t
         :publishing-function org-publish-attachment
         )))

For example:

(setq base-dir "~/myblog/_org")
...
:base-directory base-dir
...

But such a replace doesn't work.

  1. What's this data struture? is it a association or alist?
  2. How can I replace the string with a variable?

marked as duplicate by Drew, Stefan, Dan Mar 17 '17 at 17:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5

1) It's not an association list, strictly speaking, even though the name seems to say it is. An alist is a list of (key . value) dotted pairs. org-publish-project-alist is just a list of lists, where each of the sublists contains a tag (the first element) and a property list (the rest of it). But property lists consist of key-value pairs (pairs in a more general sense, not lisp dotted pairs). See the emacs lisp manual for more details.

EDIT: As @phils points out, this is wrong: it is an alist. Each sublist is a pair, whose car is the tag and whose cdr is the property list. Apologies for my mistake.

2) The quote at the beginning inhibits evaluation of everything inside, so any variables don't get evaluated - try it:

(setq x 3)
(setq l '(1 2 x))

The value of l is the list (1 2 x), not (1 2 3).

To allow partial evaluation of expressions inside a mostly quoted structure, you need to use the backquote mechanism:

(setq x 3)
(setq l `(1 2 ,x))

Now the value of l is (1 2 3). Note the backquote instead of the quote, and the comma in front of the variable. You can find more info here.

Applying this to your case becomes

(setq base-dir "~/myblog/_org")
(setq org-publish-project-alist
  `(
     ...
     :base-directory ,base-dir
     ...
  ))
  • 1
    Sure it's an alist. E.g. The first key is "blog" with the value (:components ("org-blog-docs" "org-blog-static")). i.e. The property list is the value. (foo bar) is just shorthand for (foo . (bar . ())). – phils Mar 16 '17 at 6:27
  • @phils: you are right. I edited the answer to reflect your comment - thanks for the correction! – NickD Mar 16 '17 at 7:16
  • @phis Great, it works as expected. – lucky1928 Mar 16 '17 at 13:32

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