简体   繁体   中英

Emacs using path relative to init.el file

I am storing my Emacs init.el file in Dropbox, along with dependent files like color themes. Say I have a directory ~/Dropbox/emacs whose content is something like:

.
├── init.el
└── themes
    └── zenburn-theme.el

Then on my Linux machine I simply have a symlink from ~/emacs.d/init.el to ~/Dropbox/emacs/init.el , which works just fine. In my init.el though, I'd like to be able to load the theme using a path relative to the init.el file itself. ie I'd like to do something like:

(add-to-list 'custom-theme-load-path "./themes/")
(load-theme 'zenburn t)

My use of "./themes/" as a path relative to init.el seems to not work as anticipated though. I can change this to:

(add-to-list 'custom-theme-load-path "~/Dropbox/emacs/themes/")
(load-theme 'zenburn t)

and it will work. However, I'd prefer a notation that is relative to the init.el file. This way, if I for example decide at a later date to have all my init.el file and other related files somewhere other than Dropbox, I can simply copy the entire directory and rewire the single init.el symlink without having to change all the references to ~/Dropbox .

So,

  1. Is there a way to reference directories relative to the init.el file?
  2. As someone who is not particularly well versed in Lisp syntax, what would this look like in the specific example above (adding a custom theme path)?

What you are looking for is load-file-name , it contains the full path of the current file being loaded, for your example it would be something similar to "/home/username/emacs.d/init.el" .

You can then use file-name-directory to strip the file name itself and only keep the directory path, and then prepend it with expand-file-name .

(add-to-list 'load-path (expand-file-name "themes/" (file-name-directory load-file-name)))

Note that it will not follow symlinks: you will get the path that was originally opened, not the path of what the symlink is pointing to. For your example you would need to make sure that you also have a symlink to ~/Dropbox/emacs/themes present in the same folder as your symlink to ~/Dropbox/emacs/init.el .

I mentioned in a comment that you can avoid the problem if you instead symlink the directories rather than just symlink the init.el file.

However, I suddenly thought it would probably be a good idea to warn you about some of the pitfalls with this approach. I use to do it this way, but then discovered it can create some problems.

If you end up with multiple emacs sessions on different machines, weird conflicts can occur. For example, I originally did this because I use emacs from home and at work and I wanted just one config setup to maintain. However, I sometimes noticed 'odd' behaviour because of conflicts with some of emacs' housekeeping files, auto save files, session recovery files etc. I also had some issues when I had a system which had not been on-line for a while and then when it did get on-line, had problems working out how to sync files with dropbox etc.

I now use a git repo for my emacs config hosted on github. It doesn't cost anything and all I have to do to have my config is just git co into .emacs.d. Works much much better and no conflicts.

Dropbox is great for sharing files, but not as good if the fiiles represent app data which gets modified frequently.

With respect to your original question, if you don't want to symlink the whole directory tree, the other option would be to just add the other directories ie such as wherre your themes are to the load path in your init.el file and then not worry about having to do any default path stuff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM