3

I have an init file on a USB drive. I use this USB drive at home and at work for my init file.

Until recently, my home and work computers were both Windows machines. I am now adding a Linux machine at home and am looking for a way for Emacs to play nicely with all of the machines: Windows and Linux.

Here is an example of the major problem I see:

(find-file "E:/emacs/org/gtd.org")

Linux doesn't use letter drives? What do I do?

Carl Roberts
  • 433
  • 2
  • 12
  • Instead of having different init files, I like to query `system-type` at various locations in one master `init.el` which sets things differently depending upon what OS I am using. – lawlist Nov 20 '16 at 04:13
  • @lawlist could you give me an example? – Carl Roberts Nov 21 '16 at 14:02
  • For the example given you would simply define a variable containing the system-dependent directory and create the file-names using `expand-filename`. – politza Nov 21 '16 at 18:19

2 Answers2

5

Make your configuration system or even machine dependant.

You can, for example, load system specific init files:

(load-file (expand-file-name
            (cond ((eq system-type 'windows-nt) "windows.el")
                  ((eq system-type 'gnu/linux) "linux.el")
                  (t "default-system.el"))
            user-emacs-directory))

To load machine depandent files you can use (system-name).

(defun load-with-warning (file)
  (when (file-exists-p file)
    (condition-case err
        (load-file file)
      (error (display-warning
              'init
              (format "loading file %s:\n\t%s" file (error-message-string err))
              :warning)))))

(load-with-warning (expand-file-name
            (system-name)
            user-emacs-directory))
theldoria
  • 1,825
  • 1
  • 13
  • 25
  • Does this mean that I would have one init file point to other init files, depending on the machine and/or system? – Carl Roberts Nov 21 '16 at 14:01
  • Yes, exactly. Your `init.el` would reference `windows.el`, `linux.el` or `default-system.el`, depending on system type. If you also use the second example, than files named after your machines would be needed as well. Note, that you should gard `load-file` for the case you have no machine specific config file. – theldoria Nov 22 '16 at 15:02
0

This is the solution I made for myself:

(add-to-list 'load-path
     (concat (getenv "HOME") "/.emacs.d/conf.d/" 
         (prin1-to-string emacs-major-version) ))

(add-to-list 'load-path
     (concat (getenv "HOME") "/.emacs.d/conf.d/" 
         (replace-regexp-in-string "/" "-" (prin1-to-string system-type) )))

(add-to-list 'load-path
     (concat (getenv "HOME") "/.emacs.d/conf.d/" ))

This way I can load conf files depending OS and major versions (from v.23 to v.24 many changes had been introduced). This method only changes load path, then I use both (provide 'mylib)/(require 'mylib) to load what I need when I need.

Daniele
  • 637
  • 1
  • 5
  • 14