reddit / emacs

How to make a global function without `require`?

Babygau · 2022-12-13 23:02
In .emacs.d I have the following ``` core/lib.el modules/abc/xyz.el init.el early-init.el ``` I have `foo` and others defined in `lib.el` and want these functions to be available in all `modules` without using `(require 'lib)`
ieure · 2022-12-13 23:58
You need to [autoload them](https://www.gnu.org/software/emacs/manual/html_node/elisp/Autoload.html). But really, it's poor form to depend on anything without declaring it. Not just in elisp.
Babygau · 2022-12-14 00:22
Thank you for your suggestion. I'm moving away from Doom Emacs. It has some built-in functions and macros such as very useful `map!`, `after!` macros... which just worked without declaring them. I want to do the same thing with my config
codemuncher · 2022-12-14 05:41
You need to load that file lib.el before all other modules.. the mechanism for doing that is.... (require) The function itself lives in a global namespace. So the require is just about telling emacs which file to load in a way that doesn't require you to hard code path names all over. It's line #include <stdlib.h> in C - where exactly is stdlib.h located? Well another mechanism tells the compiler -- in elisp, that's the variable 'load-path'. &#x200B; You could always just be sure to do (load-file "<path to lib.el>") early in your init.el and then you'd have all those functions available, but it is fairly brittle - if anything changes in init.el then all of a sudden your 'foo' function doesn't exist anymore and then ??? confusing! Good elisp modules shouldn't magically fail because a global fn that is loaded for them stopped being loaded for them. &#x200B; In short: why not require?
Babygau · 2022-12-14 07:41
Many thanks for your clarification. I didn't know function is global by default. That's interesting. In short, I need `load-path` to make sure `lib` and `modules` files are available, then make sure that using `(require 'lib)` before `(require 'xyz)`. By doing so, all functions in `lib.el` are available in `xyz.el`, am I correct?
Babygau · 2022-12-14 07:43
@ieure your solution is great but it's not how I want to achieve. @codemuncher gave me a better explanation.