Single dot emacs file and per-computer configuration
Once you have found the perfect emacs configuration stuff for you, you don’t usually want to change it. But it can happen that you want to use the same dot emacs configuration in different computers or even different systems.
I use emacs at home (Mac OS X and Ubuntu) and also at work (Kubuntu). It happens that when I write ChangeLog entries (C-x 4 a) I usually want to have different email addresses in the entries (my work email in the computers I use at work, and my GNU email in the computers I use at home). In the same way, I have a specific font type configuration for my GNU systems, but I keep the default one in my Mac O X emacs.
So basically you just need to detect different systems and computers, and apply specific configurations.
2. Get system type and system name
To detect different systems, you can use the ‘system-type‘ variable, and to detect different computers (with different hostnames, of course), you can use the ‘system-name‘ variable.
The following defuns show how you can know wich system type and name you have in each machine:
;; Get current system's name
"Get current system's name"
(insert (format "%s" system-name))
;; Get current system type
"Get current system type"
(insert (format "%s" system-type))
Those previous commands are interactive, so you can just type ‘M-x insert-system-name‘ or ‘M-x insert-system-type‘ to test them.
3. Configuration depending on the system type
In order to include system-type based configurations, you can define functions to check if the current system is the one you are looking for. Check these two functions to check if system is Mac OS X (darwin) or GNU (gnu/linux).
;; Check if system is Darwin/Mac OS X
(defun system-type-is-darwin ()
"Return true if system is darwin-based (Mac OS X)"
(string-equal system-type "darwin")
;; Check if system is GNU/Linux
(defun system-type-is-gnu ()
"Return true if system is GNU/Linux-based"
(string-equal system-type "gnu/linux")
Once you have these functions, you can put the system-dependent configuration:
;; disable closing emacs in Mac OS X
Note that ‘darwin‘ is the default system name for Mac OS X systems, and ‘gnu/linux‘ is the default system name for GNU/Linux systems.
3. Configuration depending on the system name
Now, you also want to put machine-dependent configuration, so you can define functions like this to detect in which machine you are currently located:
;; Check if the system is my Kubuntu GNU/Linux at work
(defun system-is-my-workpc ()
"Return true if the system we are running on is my PC at work"
(string-equal system-name "dev002.workplace.com")
And then you just need to use the previous function in the specific configurations (note that I’m using the hostname of my PC at work. Use your own hostname for you functions, dude).
;; For my machine in my worplace, setup my work email address
(setq user-mail-address "email@example.com")