load-path and load a lisp file - emacs

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

Related

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.)

Importing files in Emacs Lisp/Emacs configuration file in the same directory

I have a couple of emacs configuration files. I was going to consolidate them down into a common file and then a couple other files that import the common file. Then have their own functions. I was just going to have them all as .emacsCommon in my home folder but when I write:
(require '.emacsCommon)
it doesn't load the function. What is the right way to do this??
Cheers
Use 'load-file' to load a EmacsLisp file
(load-file "./.emacsCommon")
If you want to use require, you should add (provide 'foo) at the end of a file named foo.el. If that file is on the load-path, you can then use (require 'foo) to load this file, and add the feature (foo) to the feature list. (The printname of 'foo, the feature name, is used as a filename here.)
Since your filename has a leading dot, and doesn't end in .el, you should give the filename as an argument to require though:
(require 'foo ".foo")
Note, that you could also just use load or load-file.

Emacs c-mode autoloading failed

I want to load my file named "my-c-setup.el" when the c-mode is loading. So, I'm using the function "autoload".
With my python setup, it works well :
lang.el
(autoload 'python-mode "my-python-setup" "" t)
my-python-setup.el
(require 'python)
; ...
I'm trying to do the same with the c-mode, but i does not work :
lang.el
(autoload 'c-mode "my-c-setup" "" t)
my-c-setup.el
(setq c-basic-offset 4)
; ...
When I try to open a file in c-mode (test.c for example), I have the following error :
File mode specification error: (error "Autoloading failed to define function c-mode")
Autoload is not what you're looking for. What it does is simply load some code the first time it is needed, which is a handy way to extend Emacs' functionality while still keeping the start-up time low.
To solve your problem, we gotta think about what you really want to do: do you simply want some of your code to be loaded at some point, or do you want buffer-local customizations for ever buffer that is in c-mode?
If you simply want Emacs to load your code at start-up, either put your code directly into your .emacs file or use load-file or require instead of autoload:
load-file simply takes a file name, loads the lisp code in that file and evaluates it. So if your code is in a file named "/path/to/my-c-setup.el", you could put the following line in your .emacs, and the code will be loaded on every start-up:
(load-file "/path/to/my-c-setup.el")
Perhaps you don't want to give the absolute path name for every file you load. In that case, you could use the function load-library instead which is similar to load-file but tries to find the given filename in any of the directories stored in the variable load-path:
(add-to-list 'load-path "/path/to")
(load-library "my-c-setup.el")
The advantage is that you have to do the add-to-list part only once, and all subsequent calls to load-library will be able to find code in that directory.
An alternative way is the provide/require mechanism: you can make your .el-file "provide" some feature by putting a (provide 'feature) call in it, e.g.
(provide 'my-c-mode-customizations)
Then put an according (require 'feature) in your .emacs file, and your code will be loaded as well:
(require 'my-c-mode-customizations)
However, if you want your code only be loaded when c-mode is activated on a buffer, the way to achieve that is through Emacs' Hook mechanism:
A hook is a variable where you can
store a function or functions to be
called on a particular occasion by an
existing program.
Most major modes provide a customizable hook variable to which you can add functions that will be called whenever the major mode is invoked. For instance, c-mode provides c-mode-hook. In order for your own customizations to be called whenever c-mode is turned on for a buffer, put them in a function, say, my-c-mode-customizations and add the following line to your .emacs file:
(add-hook 'c-mode-hook 'my-c-mode-customizations)
Of course, you still need autoload for Emacs to actually find the definition of that function.
Lisp's autoload does not call a function when a file is loaded but tells lisp that the function is available and that the given file provides it. Whenever someone calls the (not yet defined) function, the file is loaded.
I think that c-mode is already defined and thus fails to re-register.
Autoload doesn't do what you think it does.
http://www.gnu.org/software/emacs/elisp/html_node/Autoload.html
What you probably want are mode-hooks or eval-after-load.
See eval-after-load vs. mode hook for the difference between the two.

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...

Unable to import a file to your .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.