1

I'm trying to create a .emacs file that works on multiple platforms. Therefore, I declare a variable with a path for each platform

(defvar macosx-p (string-match "darwin" (symbol-name system-type)))
(defvar gnulinux-p (string-match "linux" (symbol-name system-type)))
(defvar mswindows-p (string-match "windows" (symbol-name system-type)))

(defconst macosx-ps "/Users/xxx/")
(defconst gnulinux-ps "/home/xxx")
(defconst mswindows-ps "D:/xxx")

How can I pass one of the -ps variables to a function like this?

(if macosx-p
   (progn
    (setq org-capture-templates
     '(("t" "TODO" entry (file+headline (concat macosx-ps "file.org") "Inbox")
          "* TODO %?")))))

(if gnulinux-p
  (progn
   (setq org-agenda-files (quote
   ((concat gnulinux-ps "file.org")
    (concat gnulinux-ps "others/"))))))

I want macosx-ps to output my path string.

1
  • 2
    Why use string-match and symbol-name? You could simply test (eq 'darwin system-type).
    – JeanPierre
    Commented Sep 9, 2016 at 19:59

2 Answers 2

1

Instead of defining multiple versions and picking up the right one each time, it would make more sense to define a single variable according to the system that Emacs is running on.

(defconst somepath (cl-case system-type
                     ((gnu/linux gnu gnu/kfreebsd) "/home/xxx")
                     ((darwin) "/Users/xxx")
                     ((windows-nt cygwin) "D:/xxx")
                     (t (expand-file-name "~"))))

Note that if you want to refer to your home directory, you should call expand-file-name rather than hard-code values.

If somepath refers to a directory, call expand-file-name to build a path to a file in that directory. To evaluate part of an expression which is otherwise given in literal syntax, use a backquote rather than a quote, and a comma to indicate subexpressions to evaluate.

(setq org-capture-templates
     `(("t" "TODO" entry (file+headline ,(expand-file-name "file.org" somepath) "Inbox")
          "* TODO %?")))))
0

As xuchunyang pointed out, I need to add a backquote (´) and , to my list, like this:

(if gnulinux-p
  (progn
   `(setq org-agenda-files (quote
   (,(expand-file-name "file.org" gnulinux-ps)
    ,(expand-file-name "others/" gnulinux-ps))))))

Now everything works.

4
  • 1
    Isn't your backquote in the wrong place? It's quoting the setq so the whole expression won't do anything. Unless this is within a macro?
    – npostavs
    Commented Sep 10, 2016 at 22:15
  • Where would you place it?
    – Olli
    Commented Sep 22, 2016 at 8:21
  • 1
    (setq org-agenda-files `(,(expand-file-name "file.org" gnulinux-ps) ,(expand-file-name "others/" gnulinux-ps)))
    – npostavs
    Commented Sep 22, 2016 at 12:26
  • 4
    Or just (setq org-agenda-files (list (expand-file-name "file.org" gnulinux-ps) (expand-file-name "others/" gnulinux-ps))
    – npostavs
    Commented Sep 22, 2016 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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