how to specify a property value of a variable in e

2019-05-11 09:24发布

问题:

I use the following code in .emacs file to set default publish behavior. I put the org base directory in difference locations for difference computers:

;; define machine specific directories storing my org files
(cond ((system-name-is-home) (setq org-dir "/data/org"))
      ((system-name-is-work) (setq org-dir "~/org")))

Thus I'd like to use a variable to specify :base-directory to org-dir instead of hard-coding it as "~/org". How can I do that?

(require 'org-publish)
(setq org-publish-project-alist
      '(
        ("org-notes"
         :base-directory "~/org"
         :base-extension "org"
         :publishing-directory "~/tmp/"
         :recursive t
         :publishing-function org-publish-org-to-html
         :headline-levels 4
         :auto-preamble t

         :auto-sitemap t          ; Generate sitemap.org automagically ...
         :sitemap-filename "sitemap.org" ; ... call it sitemap.org (the default) ...
         :sitemap-title "Sitemap" ; ... with title 'Sitemap'.
         )
        ("org-static"
         :base-directory "~/org"
         :base-extension "css\\|js\\|png\\|jpg\\|gif\\|pdf\\|mp3\\|ogg\\|swf"
         :publishing-directory "~/tmp/"
         :recursive t
         :publishing-function org-publish-attachment
         )
        ("org" :components ("org-notes" "org-static"))
        ))

回答1:

One way to do it will be using ` (backquote) and , (comma). From GNU Emacs Lisp Reference Manual,

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. (...) The special marker ',' inside of the argument to backquote indicates a value that isn't constant. The Emacs Lisp evaluator evaluates the argument of ',', and puts the value in the list structure.

So you can write your program as follows:

(require 'org-publish)
(setq org-publish-project-alist
      `(                              ; XXX: backquote
        ("org-notes"
         :base-directory ,org-dir     ; XXX: comma
         :base-extension "org"
         :publishing-directory "~/tmp/"
         :recursive t
         :publishing-function org-publish-org-to-html
         :headline-levels 4
         :auto-preamble t

         :auto-sitemap t          
         :sitemap-filename "sitemap.org" 
         :sitemap-title "Sitemap"
         )
        ("org-static"
         :base-directory ,org-dir     ; XXX: comma
         :base-extension "css\\|js\\|png\\|jpg\\|gif\\|pdf\\|mp3\\|ogg\\|swf"
         :publishing-directory "~/tmp/"
         :recursive t
         :publishing-function org-publish-attachment
         )
        ("org" :components ("org-notes" "org-static"))
        ))