Does .emacs.d belong to load-path? - emacs

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

Related

Multiple Emacs Lisp config files lead to "free variable" and "function not known to be defined" warnings

I've been looking for emacs config files for a while, so that I learn how to structure my Emacs config in the most optimal way. Now i'm looking at purcell's config files which look very tidy and simple to read.
However, for every config file I've found there's always the same problem: "free variable"
and "function not known to be defined" warnings on all the included files. It is quite frustrating to see all the red underlines from flymake everytime, and it also makes me question if this really is the best way to write the config file.
Here's a file from the repository mentioned above:
;;; init-python.el --- Python editing -*- lexical-binding: t -*-
(setq auto-mode-alist
(append '(("SConstruct\\'" . python-mode)
("SConscript\\'" . python-mode))
auto-mode-alist))
;;WARNING: assignment to free variable ‘python-shell-interpreter’
(setq python-shell-interpreter "python3")
(require-package 'pip-requirements)
(when (maybe-require-package 'toml-mode)
(add-to-list 'auto-mode-alist '("poetry\\.lock\\'" . toml-mode)))
;;WARNING: reference to free variable ‘black’
(when (maybe-require-package 'reformatter)
(reformatter-define black :program "black" :args '("-")))
(provide 'init-python)
;;WARNING: the following functions are not known to be defined: require-package, maybe-require-package, reformatter-define
;;; init-python.el ends here
The python-shell-interpreter is a variable defined from the python-mode package.
The require-package and maybe-require-package are defined in a custom local file called init-elpa.el which is requires'd by the init.el file and thus they're not directly recognized by this file.
The black variable warning is the mostr "intricate": I guess it's caused by the fact that the reformatter package isn't included because maybe-require-package isn't included as well.
This gives me a headache. If I see it correctly, the root problem is the fact that the included files (init-python.el et al.) depend on stuff from the including file (init.el) without it being explicitly stated.
Is there a better way to structure an Emacs Lisp config among multiple files?
The python-shell-interpreter is a variable defined from the python-mode package.
Add (require 'python-mode) to the top of the file (or simply above the code which depends on that).
The require-package and maybe-require-package are defined in a custom local file called init-elpa.el
Add (require 'init-elpa) to the file (I'm assuming it provides the init-elpa feature).
which is require'd by the init.el file
This is fine -- require does nothing if the library in question has already been loaded. Even when there's an overarching init file loading other files, it's sensible for each file to require the things it needs (unless there are circular dependencies).
The black variable warning is the most "intricate": I guess it's caused by the fact that the reformatter package isn't included because maybe-require-package isn't included as well.
I'm guessing that reformatter-define is a macro and that the symbol black is not actually used as a variable. When only macros are required, one would typically (eval-when-compile (require 'reformatter)) but judging by the name maybe-require-package it's intended that reformatter might not be loadable at all. If that's all accurate, you can use this wrapper to cope with the false-positive:
(with-suppressed-warnings ((free-vars black))
...)
See C-hig (elisp)Compiler Errors for further information on handling byte-compilation warnings and errors.

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.

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

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.