Unable to import a file to your .emacs - emacs

How can you import a new chunk of code (an Emacs lisp library) into your .emacs file?
I do not want to put everything into one huge .emacs file.

Put the file google.el in a directory, say ~/lisp, and then in your .emacs:
(add-to-list 'load-path "~/lisp")
(require 'google)
If you want to add a directory and its sub-directories, you can check out the answers in this SO question.
And, as you add more and more 'require lines, you'll notice things slowing down on startup. At which point you'll want to find out how to make Emacs start up faster I of course like my answer best.

elisp-load-dir can help, if you need to load many files at once.
I use it to load per-topic setup files, which in turn only autoload the heavy stuff when actually needed:
.emacs
.emacs.d/
lisp/
elisp-load-dir.el
... other .el files that provide a feature
rc/
... many small .el file that set variables, defaults, etc for me
So my .emacs is really minimal, it just adds ~/.emacs.d/lisp to the load path, so that I can install third-party extensions there. Then it requires elisp-load-dir and uses it to load whatever config files I have in ~/.emacs.d/rc:
(add-to-list 'load-path "~/.emacs.d/lisp")
(require 'elisp-load-dir)
(elisp-load-dir "~/.emacs.d/rc")
;; then comes all the custom-set-faces stuff that emacs puts there
The rc/*.el files are basically what you'd put in your .emacs, except it's modularized. For instance, I have one for each mode I regularly use, one for the startup, disabling the splashscreen, the toolbar, etc…

You can also add an simple load statement in your .emacs file:
(load "/path/to/library.el")
Frankly, though, I like Trey's solution: it keeps all .el files in a single location.
Edit: removed the 'require' statement, as per Trey's statement.

Related

How to disable Emacs Lisp auto-reformatting?

I absolutely despise that Emacs automatically reformats my init.el file. Every time I use the GUI to change a setting, when I look back at my init.el file, Emacs has warped all of the formatting I've done on the file (e.g. changing the indentation, moving things onto their own lines, etc.).
How do I disable this functionality?
Your choice is to either avoid the customize GUI altogether (I am not
even sure that is possible because, e.g.,
packages
use them), or put something like this in your .emacs:
(custom-set-variables
`(custom-file ,(expand-file-name "custom.el" user-emacs-directory)))
(when (file-readable-p custom-file)
(message "ignoring and deleting `%s'!" custom-file)
(delete-file custom-file))
This will make Emacs write all its custom forms to a file that you
never load and delete whenever you start Emacs.
This also means that you will have to manually move all your
customizations from custom-file to your .emacs yourself.

emacs font setting does not load at start

I put my font setting at a separate font_settings.el file and load it with:
(load-library "font_settings")
at the end of init.el.
But every time I start Emacs, emacs seems not load the settings.
I have to eval-buffer to evaluate init.el to let it work.
So it seems the settings are OK but loading sequence or the way I load it is not right...
Any thoughts?
The code of font_settings.el is here (too long):
http://pastebin.com/ExpY1mTy
Is font_settings.el in your load-path? If not, put it there or add the directory that contains it:
(add-to-list 'load-path "/the/full/directory/name/my-directory/")
See the Emacs manual, node Lisp Libraries.
You can check whether the library was actually loaded by using C-h v load-history. (Alternatively, you can check whether something that the library defines actually gets defined.)

Does .emacs.d belong to load-path?

My .emacs.d/ contains the file load-directory.el, which, as its name suggests, provides the function load-directory (and, yes, it contains the statement (provide 'load-directory) too). I'd like load it at startup time, but the statement (require 'load-directory) returns this error:
File error: Cannot open load file, load-directory
What's wrong?
Thanks in advance.
Use a sub-directory for your custom Lisp extensions, e.g. ~/.emacs.d/lisp/, and add this directory to load-path:
(add-to-list 'load-path (locate-user-emacs-file "lisp/"))
Do not add ~/.emacs.d/ to your load-path. For details, see Disable warning about emacs.d in load path
No, .emacs.d does not belong into the load-path.
From the docstring:
Directory beneath which additional per-user Emacs-specific files are
placed. Various programs in Emacs store information in this directory.
So this is a directory to which files are automatically written. It should therefore not contain your own files. Which means, if you add it to your load-path, you are probably doing something wrong.
The warning is a bit misleading. AFAICT, the problem is not the load-path per se, but that you do not want your own lisp files in that directory, because they could be overwritten.
It looks like load-directory.el is not in your load path. If it's it your .emacs.d, add it via:
(add-to-list 'load-path "path/to/.emacs.d") ; note: no trailing /
If load-directory.el itself holds all your load-path customizations, you can use the second, optional parameter of require to tell it which file name to use:
(require 'load-directory "path/to/directory/load-directory") ; note: no file extension needed
(And just in case: it should be (provide 'load-directory) and not (provides ...) in that file.)

load-path and load a lisp file

after setting a path for my lisp files in emacs in the .emacs file, like this
(add-to-list 'load-path "~/elisp/")
logically I should also use a load command for a specific file I guess what is that command
I tried
(load-file-name "google-c-style") with the .el added also for the file, what should be the right way to do this
however no success.
It's just (load), not (load-file-name).
If the .el has a line like (provide 'google-c-style), then all you need in your .emacs is:
(require 'google-c-style)
load-file-name is a variable which holds the full name of the file loaded by 'load'
use C-h-v load-file-name to read the documentation
Now, to load a file use the function 'load' - This looks for elisp source or binaries in the loaded paths
Eg: (load "google-c-style.el")
Note: There are another functions 'load-file'and 'load-library which work slightly differently. Read more about these here: http://www.gnu.org/software/emacs/manual/html_node/emacs/Lisp-Libraries.html#Lisp-Libraries
Also, as mentioned in one of the answers one can also use the provide - require feature.
Read this post to learn more about the differences between these functions(load, load-file, require, autoload)
http://ergoemacs.org/emacs/elisp_library_system.html

Setup for emacs org-mode outside .emacs file

I would like to set up a project to be published as HTML using org-mode.
I don't want to litter my .emacs with project definitions, and I was wondering where I could put the (setq org-publish-project-alist) variable.
Can I somehow put it in the same dir?
Ryan McGeary describes what I think is a good way to organize emacs startup files.
Update:
The domain emacsblog.org expired :(
You can look at the cached copy of the originally linked page.
You could just add a new file in your .emacs.d (or whereever) and do a load-file in your .emacs file.
-- EDIT --
For example, you could have the following in your .emacs
(load (expand-file-name "~/.emacs.d/lisp/personal-org-mode-stuff.el"))
and then put all of your customization stuff in ~/.emacs.d/lisp/personal-org-mode-stuff.el and it will load that file and import all of your .emacs
Another poster also posted a link to a description of how to add your lisp files to the load path and require them.
If you don't set it manually at all, but rather use Emacs' customize mechanism to control the value of this variable, your .emacs file will not be cluttered if you add the following two lines to your .emacs:
(setq custom-file "~/.emacs-custom.el")
(load custom-file 'noerror)
Although some might claim that it's kind of ironic that you have to add two lines two your .emacs file to declutter it that way...