Platform-independent path (in LaTex and Elisp)? - emacs

I run Emacs on Win7 and Ubuntu and try to share most of my configuration via dropbox.com. I still could not figure out how to write the path to load-files or images (for example) in a platform independent way.
I mirrored my directory structure on both machines so that inside the home directory the relative path should work out. Is there a way to achieve this with a single path without if-clauses (if system ... else ...) everywhere?
I tried ~/ and it worked sometimes, sometimes not.
'home' is set on Win7, but it must be addressed as %home% on Win7 and as $HOME on ubuntu (right?).
I need a solution for:
elisp code
latex files (adress images, listings)

In Windows, ~ resolves to whatever the HOME environment variable is set to. I found it most useful to set HOME to C:\Users\username (in Win 7). This means I can put my .emacs file there, and also that ~ will reference files in it when I'm opening files.
Once you do this, your elisp can refer to ~ as your home directory on either Windows or Linux.

I have a setting at the top of my .emacs the tests the OS, and sets a CONST pointing to the the root directory of the path accordingly. Then, within the configurations, I just reference the variable within my path statements. Just tested on OS X and Windows 7.
Here's some examples:
(defconst HOME_DIR
(if (eq system-type 'darwin)
(concat "/Users/" (getenv "USER"))
(concat "c:/cygwin/home/" (getenv "USER")))
"Home directory. I could rely on the HOME environment variable,
but I'm being retentive.")
(defconst EMACS_PKGS (concat HOME_DIR "/emacs-pkgs")
"Directory for the emacs pkgs and configuration files.
Default uses `HOME_DIR' as a prefix")
;; isolate customize settings
(setq custom-file (concat EMACS_PKGS "/emacs-custom.el"))

My experience with Emacs on Windows is that ~ is not at C:\Users\yourusername. For me, it's in C:\Users\rafe\AppData\Roaming. So, for the case of Emacs, if you drop files in, say, %HOME%\AppData\Roaming (or, in Windows Emacs, ~), you'll have access to those files. That's where my .emacs was by default, at least. The path might be different on your system, so see where Emacs takes you when you try to find a file and type ~/.
That setting might be configurable, but I've never cared enough to change it.
As for LaTeX, sadly I can't help.

For LaTeX, see: “How to add an extra searchable dir with personal style files to TeXLive, NOT under ~/Library/texmf?” on the TeX SE. It might help.

Related

My .emacs.el file is not loading, how can I get it to load? [duplicate]

I tried looking for the .emacs file for my Windows installation for Emacs, but I could not find it. Does it have the same filename under Windows as in Unix?
Do I have to create it myself? If so, under what specific directory does it go?
Copy and pasted from the Emacs FAQ, http://www.gnu.org/software/emacs/windows/:
Where do I put my init file?
On Windows, the .emacs file may be called _emacs for backward compatibility with DOS and FAT filesystems where filenames could not start with a dot. Some users prefer to continue using such a name, because Windows Explorer cannot create a file with a name starting with a dot, even though the filesystem and most other programs can handle it. In Emacs 22 and later, the init file may also be called .emacs.d/init.el. Many of the other files that are created by Lisp packages are now stored in the .emacs.d directory too, so this keeps all your Emacs related files in one place.
All the files mentioned above should go in your HOME directory. The HOME directory is determined by following the steps below:
If the environment variable HOME is set, use the directory it indicates.
If the registry entry HKCU\SOFTWARE\GNU\Emacs\HOME is set, use the directory it indicates.
If the registry entry HKLM\SOFTWARE\GNU\Emacs\HOME is set, use the directory it indicates. Not recommended, as it results in users sharing the same HOME directory.
If C:\.emacs exists, then use C:/. This is for backward compatibility, as previous versions defaulted to C:/ if HOME was not set.
Use the user's AppData directory, usually a directory called Application Data under the user's profile directory, the location of which varies according to Windows version and whether the computer is part of a domain.
Within Emacs, ~ at the beginning of a file name is expanded to your HOME directory, so you can always find your .emacs file with C-x C-f ~/.emacs.
There's further information at HOME and Startup Directories on MS-Windows.
It should be stored in the variable user-init-file. Use C-H v user-init-file RET to check. You can also open it directly by using M-x eval-expression RET (find-file user-init-file) RET
Open the file like this in Emacs for Windows:
C-x C-f ~/.emacs
More information in the Emacs Wiki
On my Vista box it's in C:\Users\<USER>\AppData\Roaming\
Note that it may NOT be enough to just type Ctrl-x Ctrl-f ~/.emacs and create the file.
It may be that your Emacs application uses a different place to store your init file, and if so, then creating the file ~/.emacs simply creates a useless file which your Emacs application ignores.
Also, you may want to do more than just access the .emacs init file, but you may want to know where it is, i.e., its pathname.
To get at this there are two methods:
Easy way: type Ctrl + H V user-init-file Return
Slightly trickier way:
You can find out where your system is storing its own .emacs file by:
Click options and scroll down to "Set Default Font..."
Change the font setting and click okay
On the options menu, go down to "Save Options"
When the options are saved, the system saves its .emacs file,
and you can read the file path in the minibuffer at the bottom of the Emacs screen
In Windows 7 put your init.el file in C:\Users\user-name\AppData\Roaming\.emacs.d\, where user-name is your user/login folder.
Take care so your init.el file won't be named init.el.txt. This is something Windows does if you create your file with some editor like Notepad.
On versions of Emacs on Windows above 22, it seems to have moved to
~/.emacs.d/init.el
, ~ being the value of your environment variable HOME (see Control Panel → System → Advanced → Environment variables).
The file itself might not exist. In that case just create it.
You must create an emacs initialization file. One is not automatically created.
I had a similar issue and this answer tracks down what I did.
My issue was my ~/.emacs.el file was not loading. Strange because this has always worked for me.
This question/answer helped me but I had to put my init file in the %USERPROFILE%\AppData\Roaming\.emacs.d\init.el because this is apparently the default behavior on Windows.
To troubleshoot this, I ran the following in the emacs *scratch* buffer.
user-emacs-directory
"~/.emacs.d/"
When I saw user-emacs-directory was ~/.emacs.d, I simply moved my .emacs.el file to %USERPROFILE%\.emacs.d\init.el. But this still didn't work.
I continued with expand-file-name as shown below:
(expand-file-name user-emacs-directory)
"c:/Users/pats/AppData/Roaming/.emacs.d/"
Got to love how Windows works. (not) So I moved my emacs.el file to the %USERPROFILE%\AppData\Roaming\.emacs.d\init.el and this worked. The file was now being read. But I got other errors because my initialization file loaded other (personal emacs) files (in ~/myenv/emacs/*.el.
Warning (initialization): An error occurred while loading ‘c:/Users/pats/AppData/Roaming/.emacs.d/init.el’:
Hum... Seems like all my files ~/myenv/emacs/*.el would need to be moved in order for this to work but I didn't want to do that. Then I realized that because the HOME environment variable was not set, emacs was performing its default behavior.
SOLUTION
Once I set my windows HOME environment variable to %USERPROFILE% everything began to work like it has for the past 25 years. :-)
To set the HOME environment variable, I typed WindowsKey+"edit environment variables for your account" to open the Environment Variables dialog box, and entered HOME=%USERPROFILE%.
Now my emacs initialization file .emacs.el is is back to its rightful place $HOME/.emacs.el and not in %USERPROFILE%\AppData\Roaming\.emacs.d\init.el
To be fair, if Windows had just one place to put files for user installed packages the solution of making HOME=%USERPROFILE\AppData\Roaming might be acceptable, but because some applications use %USERPROFILE%, some use %USERPROFILE\AppData\Roaming and others use %USERPROFILE\AppData\Local it just makes it difficult to know where to find your configuration files.
I prefer having everything in my %USERPROFILE% or $HOME directory.
Another similar question was here:
Emacs init.el file doesn't load
As kanja answered, the path to this file is stored in the user-init-file variable (or if no init file exists, the variable contains the default value for where to create it).
So regardless of which of the possible init file names you are using, and which directory it is in, you should be able to visit your init file with:
M-: (find-file user-init-file) RET
Or display its full path in the echo area with:
M-: (expand-file-name user-init-file) RET
On Emacs 23 and Windows 7 it only works if you set:
HKCU\SOFTWARE\GNU\Emacs\HOME
After Emacs 27.1, emacs has started respecting the $XDG_CONFIG_HOME. The init file or the init directory can now be found in $XDG_CONFIG_HOME/emacs/init.el.
In Windows $XDG_CONFIG_HOME could translate to %LOCALAPPDATA%.
In any case you can use the following emacs variables to find out the location of the your initialization file by M-x eval-expression
user-init-file
or the emacs configuration directory
user-emacs-directory
I've found that Emacs 22 will occasionally open either "C:\Documents and Settings\username\Application Data\.emacs", or just "C:\Documents and Settings\username\.emacs" on my XP machine. I haven't found an explanation for why it occasionally changes it's mind.
~ will always point to whatever the current instance of emacs thinks is HOME, but kanja's tip (C-h v user-init-file) will always tell you what ~/.emacs actually maps to.
On Windows 8.1, if Emacs is started from Windows Explorer, a shortcut or from cmd console it uses C:\Users\<USER>\AppData\Roaming.emacs init file. When I start Emacs from PowerShell, Emacs looks for its init file in C:\Users\<USER> folder. The fix to this issue was to set the HOME user environment variable (Control Panel\System and Security\System->Advanced system settings->Advanced->Environment variables) to C:\Users\<USER>. After this change, no matter how I start Emacs, it uses the same init file (see the accepted answer of this question)
On Windows XP it's:
C:\Documents and Settings\yourusernamehere\Application Data\
There is a list of directories based on your Windows version and extra information:
http://www.gnu.org/software/emacs/manual/html_node/emacs/Windows-HOME.html
For WIndows7& Emacs26.3:
if HOME environment is set, then the .emacs file should be in that folder.
otherwise, it should be in c:\.
In both cases, if .emacs is not there, _emacs should be used.
This is because we cannot create .emacs file according to the windows file naming rules.(but we can download or copy it from somewhere else).

Change .emacs directory in Ubuntu

Currently my .emacs file is found in /home
So is my .emacs.d folder and I suppose some other files like diary, notes etc will be created in this folder.
I find this really messy and I want all my emacs related files(including the .emacs) and folders in a single folder, say /home/EmacsHome
How do I do that in Ubuntu 12.04? In windows I did this by setting an environment variable HOME to the path of EmacsHome. But, in Ubuntu, the HOME variable will be used for several other programs as well, so I dont want to change that.
Emacs already supports a config directory for holding everything Emacs-related: ~/.emacs.d. Just use that.
Rename ~/.emacs to ~/.emacs.d/init.el, and you're virtually done.
If you have any other emacs-specific files outside of that directory, you might need to set a variable here or there to relocate them, or simply rename the file -- these days the defaults tend to automatically be within ~/.emacs.d, but an older filename in the home directory might still take precedence.
e.g.: the bookmarks file used to be ~/.emacs.bmk but if you haven't customized the variable then you can simply rename it to ~/.emacs.d/bookmarks. See C-h f locate-user-emacs-file RET and M-x find-variable RET bookmark-default-file RET for details.
If you're not sure how to proceed for any given file(s), you could just update the question with the details.
And as Alberto Zaccagni said, just create a symlink if you want an alternative name to access the directory. Although personally I would suggest retaining ~/.emacs.d as the real directory, and making the alternative name the link, like so:
ln -s ~/.emacs.d /home/EmacsHome
You could create a symbolic link to them, like so
ln -s /home/EmacsHome ~/.emacs.d
The first argument is the folder where you want the folders to be, the second one is where emacs usually looks for its configuration files, so with this approach you can put the folder wherever you like.
This works for OSX, so perhaps for you too . . . just change the paths to whatever suits your needs. The user-emacs-directory is where you would set the equivalent of .emacs.d. I have everything related to Emacs in my user-emacs-directory. If you use package manager, the elpa directory would be automatically crated there also.
(setq default-directory "~/.0.data/")
(setq user-emacs-directory "~/.0.data/.0.emacs/")
(setq diary-file "~/emacs/diary")
There's also another solution:
cd your-directory
HOME=$PWD emacs -L .
taken from https://github.com/capitaomorte/yasnippet#important-note-regarding-bug-reporting

How to declare the location of Emacs's init file as a variable

I use Emacs across a few computers (Linux and Windows boxes) with my .emacs.d synced via ownCloud: .emacs (in ~ or Appdata) just loads {ownCloud directory}/.emacs.d/init.el.
My question is, how do I get that path to the init.el as variable dependent on the OS/username.
For example, the README on the Zenburn theme requires your init includes
(add-to-list 'custom-theme-load-path "~/.emacs.d/themes/")
But as the path from my home directory and .emacs.d varies between machines, this won't work.
I was thinking of something like this to remedy it:
(add-to-list 'custom-theme-load-path "{THE DIRECTORY THAT CONTAINS THIS INIT FILE}/themes/")
Can this be done? I'm only a fortnight's into using Emacs, so don't bite too hard :P
The variable system-type determines between Linux and Windows machines. The variable user-login-name is self explanatory. I use these to load different files depending on which machine I'm in.
(if (eq system-type 'gnu/linux)
(when (string= user-login-name "home user name")
(load-file "/path/to/file"))
(when (string= user-login-name "work user name")
(load-file "/path/to/other-file")))
Honestly though, this is more appropriate for miscellaneous configurations. I wouldn't recommend using this to change your emacs directory location.
Instead, create symlinks on each machine in order to pretend your emacs directory is in the standard place. (I've been doing this on 4 different machines for a couple of years now, and it's going well).
Finally, to answer your last question. The init file already is a variable, and its name is user-init-file. To extract the directory it's located in just do
(file-name-directory user-init-file)
There is user-emacs-directory variable.

Independent emacs installations and locations of the .emacs.d directory and the .emacs file

I have multiple installations of emacs on my Windows 7 computer, each configured slightly differently. Let's say installation1 and installation2, where installation1 is the main emacs, and installation2 is subsidiary.
I would like to maintain two sets of .emacs files and .emacs.d. directories, such that installation1 looks for it in the default HOME or %appdata% directory (C-x C-f ~/.emacs RET), but that installation2 cannot find the .emacs file in these directories at all. That is, I would like installation2 to not look in the HOME or %appdata% locations for the .emacs.d directory or .emacs file. Ideally, this would be implemented by redefining the ~ expansion for installation2.
I guess I could have a (add-to-list 'load-path "C:/installation2-location/.emacs.d/lisp/") and save it to a .emacs file in the same directory as the installation2 emacs executable, but I am not sure that this is a robust solution.
Suggestions welcome.
Well you can use the system-type variable. From the Emacs help
system-type is a variable defined in `C source code'. Its value is
darwin
Documentation: The value is a symbol indicating the type of operating
system you are using. Special values: gnu' compiled for a
GNU Hurd system.gnu/linux' compiled for a GNU/Linux system.
gnu/kfreebsd' compiled for a GNU system with a FreeBSD kernel.
darwin' compiled for Darwin (GNU-Darwin, Mac OS X, ...).
ms-dos' compiled as an MS-DOS application.windows-nt'
compiled as a native W32 application. `cygwin' compiled using
the Cygwin library. Anything else (in Emacs 24.1, the possibilities
are: aix, berkeley-unix, hpux, irix, usg-unix-v) indicates some sort
of Unix system.
Or use system-name to determine discriminate between machines of the same of.
Finally you can make a function to load what you want in installation-1 and another to load what you want in installation-2. But I can't see any valid reason as to why you would want to maintain different emacs.d in the same machine.

How to install a Emacs plugin (many times it's a .el file) on Windows platform?

I'm new to Emacs. I found many emacs plugins are released as an .el file. I'm not sure how to install them. Can I just put them in my emacs installation directory?
After placing it, say myplugin.el to your ~/.emacs.d/ directory, add the following in your .emacs file:
(add-to-list 'load-path "~/.emacs.d/")
(load "myplugin.el")
Also, in many cases you would need the following instead of the second line:
(require 'myplugin)
In any case, you should consult the documentation of the package you are trying to install on which one you should use.
If you are unsure where your ~ directory is, you may see it by typing C-x d ~/ and pressing Enter.
As already stated, you'll need the location of the file to be in Emacs' load path.
Read the comments at the top of the file to see if it has any particular installation or usage instructions. Authors often provide this information, and there isn't one single correct way to do it, so it's sensible to look.
Failing that, if the file contains a (provide 'some-name) line (typically at the end of the file), then you would be expected to use (require 'some-name) to load it.
You may also wish to byte-compile the library for speed (but that's a different question).
Many times, an emacs plugin will consist of a directory of elisp files that need to be accessible from the load path. A simple way to ensure that all individual elisp files as well as subdirectories of elisp files are included in the load path and accessible is to do something similar to the following:
Create a directory called ~/.emacs.d/site-lisp.
Install any single elisp files in the ~/.emacs.d/site-lisp directory.
Install any packages that consist of multiple elisp files in a subdirectory under your ~/.emacs.d/site-lisp directory.
Add the following code to your ~/.emacs file to ensure that Emacs "sees" all the elisp files that you have installed:
(add-to-list 'load-path "~/.emacs.d/site-lisp")
(progn (cd "~/.emacs.d/site-lisp")
(normal-top-level-add-subdirs-to-load-path))
This will ensure that all elisp files that are located either in either the ~/.emacs.d/site-lisp directory or in a subdirectory under that directory are accessible.
Some supplementary information:
MATLAB.el comes from http://matlab-emacs.sourceforge.net/
On windows, use the load path that looks like this:
(add-to-list 'load-path' "C:\\Dropbox\\Portable\\emacs\\matlab-emacs")
If you want FULL MATLAB functionality you should use:
;;MATLAB Mode:
(add-to-list 'load-path' "C:\\Dropbox\\Portable\\emacs\\matlab-emacs")
(require 'matlab-load)
if you just want to edit text files:
;;MATLAB Mode:
(add-to-list 'load-path' "C:\\Dropbox\\Portable\\emacs\\matlab-emacs")
(autoload 'matlab-mode "matlab" "Enter MATLAB mode." t)
(setq auto-mode-alist (cons '("\\.m\\'" . matlab-mode) auto-mode-alist))
(autoload 'matlab-shell "matlab" "Interactive MATLAB mode." t)