Hot answers tagged

28

autoload is not a substitute for require. Typically require is used to make sure that a certain file is loaded. autoload on the other hand gives Emacs a hint in which file to find a given function without loading the file right away. Only when the autoloaded function is called is the corresponding file loaded. Basically with autoload you can delay the ...


12

If you load a library repeatedly, that file will be read and its code evaluated repeatedly (each and every time you load it). If you require a library repeatedly, it will load the library once (at most -- and not at all if the library had already been loaded). require provides this efficiency even if require wasn't used to load the library initially, ...


7

(require <foo>) is treated specially when it's at top-level (it causes the compiler to require the file also, rather than only do the require at run-time). If the require is not performed at compilation time, then your code can't be compiled properly since the define-clojure-indent macro won't be known during compilation, leading to mis-compilation, ...


7

You just test with featurep: (require 'projectile nil t) (when (featurep 'projectile) (do-some-stuff-that-needs-projectile)) or you use that the require returns nil if it is not possible to load: (when (require 'projectile nil t) (do-some-stuff-that-needs-projectile)


5

Like John Kitchin pointed out, load-file does what you want. For example: (setq my-utils-file "utils.el") (load-file (expand-file-name my-utils-file (file-name-directory (buffer-file-name))))


5

(require (intern user-login-name)) require needs a symbol as its argument, the value of user-log-name is a string, intern takes a string and returns a symbol.


4

I don't know of any standard way of doing this. However, you can fairly simply advice require so that it keeps trace of who calls it. (defvar my/require-tree nil) (defun require--advice (orig-fun feature &rest args) (setq my/require-tree (append my/require-tree (list (let ((my/require-tree (list feature))) (apply orig-fun ...


4

In terms of Emacs initialization, don't make either choice. GitHub user jwiegley has the excellent use-package declaration macro, that will require or autoload a package as determined by need. Best used to defer the loading of packages that can be loaded on-demand.


4

IMHO, the right way is to ignore it. ;-) But the standard Emacs answer is to use declare-function - see (elisp) Declaring Functions. All you need to do is add a declare-function statement before the first use of the function in question: (declare-function gud-find-c-expr "gud.el" nil) This says that gud-find-c-expr is defined in gud.el (the .el ...


4

What you are looking for is (add-to-list 'load-path "~/Projects/modules-test") See docs: Lisp Libraries Library Search The best thing you can do is C-h v load-path RET and follow links in the *Help* buffer.


4

@Stefan explained why you see what you see: Your require statement is not at top level of the file. A solution to the byte-compiler not evaluating your (non top-level) require statement is to force its evaluation, using (eval-and-compile (require 'clojure-mode)). The fact that require is evaluated by the byte-compiler when it is at top level is pointed out ...


3

You should be able to use require, yes, but only after running package-initialize (in Emacs≥27, this is done for you before the beginning of the .emacs file). Note that for 99% of the packages require should not be needed (the autoloads should cause the package to be loaded as soon as you try to make use of it). E.g. if you have use-package installed as an ...


3

It's an internal autoload of the cl-lib package, not a global autoload. cl- has no global autoloads. To use cl- functions, require cl-lib. To use cl- macros, eval-when-compile require cl-lib.


3

You discovered most of the answer yourself. When you install clojure-mode, you also get a file clojure-mode-autoloads.el. That file besides adding the entries you mentioned to auto-mode-alist, also defines autoloads for the functions clojure-mode, clojurec-mode, etc. Once the autoload file has been loaded (which normally package-initialize does on start up),...


3

If you want a certain feature to be loaded and available directly when your Emacs has started, use require, otherwise use autoload. I personally value startup time much (I actually restart Emacs a couple of times per week, sometimes several times per day), so I accept that it will take a little bit of time when Emacs autoloads a certain function when I need ...


2

Emacs initializes the package system after loading .emacs, not before. (See Startup Summary and Packaging Basisc.) I think the reason is to allow you to configure (or even disable) the package loading process from .emacs. You can add flx-ido-mode to after-init-hook. Or you can follow the officious advice which is to call package-initialize from your init ...


2

Here's how I'd do it: (require 'markdown-mode nil 'noerror) ... ... ... (if (fboundp 'markdown-mode) ..use markdown-mode..)


2

If the package has its dependencies properly declared,cask install should do all the setup that is necessary. Cask then builds the load-path automatically for you so that (require ...) statements resolve when you invoke cask exec. Interactively evaluation / development of a package with cask-installed dependencies seems to be a shortcoming of Cask. Four ...


2

Noe that the warnings you see are real: your code happens to work because you're lucky enough that it doesn't get byte-compiled, but if for some reason it gets byte-compiled then it won't work any more (unless flycheck was loaded already when the code got byte-compiled). So to fix your code "right", you have two options: add a (eval-when-compile (require '...


1

The point of require is not lazy-loading (that would be autoload instead). Instead, the purpose is to avoid loading the same package multiple times. In the case of config files, which of load or require is preferable is unclear and will depend on your particular use case (but I think in most cases the difference will be negligible).


1

Just put the directory where dash.el[c] resides in your load-path value, before you require that library: (add-to-list 'load-path "/YOUR/DIRECTORY/CONTAINING/DASH/") (require 'dash)


1

I have one answer with Nix, but I am curious as to how it is possible to do so without Nix #! /usr/bin/env nix-shell #! nix-shell -i "emacs --script" -p "pkgs.emacsWithPackages(epkgs: (with epkgs.melpaPackages; [ dash ]))" #! nix-shell -I nixpkgs=channel:nixos-18.03 (package-initialize) (require 'dash) (message "Hi")


1

"function definition is void" most probably means the file where the function is defined has not be loaded yet. In this case you need to first (require 'calc-ext) Now, how can you find this is calc-ext? Just use calc, this triggers loading of necessary files, then ask for the docstring of calc-precision: C-h f calc-precision calc-precision is an ...


1

(provide 'wordpress-mode) provides a feature named wordpress-mode and your use-package config clearly wants to (require 'wordpress) and is, naturally, failing to do so. Check the use-package documentation -- I'm sure there's a simple way to specify that the feature symbol name and the library name are not the same. Without use-package, this would be: (...


1

require takes following arguments: (require FEATURE &optional FILENAME NOERROR) The NOERROR argument causes the function to return nil when file with the feature isn't found. Without that argument set to t, you'd get an error.


1

Experiments revealed that byte-compilation was a red herring - it did not affect the behavior. It turned out that I must have (package-initialize) before requireing any packages.


1

The gnu source comes pre-configured by default. It includes everything in the GNU ELPA. The configuration you've listed is for MELPA, a different ELPA (Emacs Lisp Package Archive).


1

As ocoh2 points out, cl-lib is not autoloaded, the only way to get it is to require it or have it indirectly autoloaded (some autoloaded package requires it). It must be the Debugger that loads cl-lib. With emacs -Q --eval '(cl-evenp 2)', there is an error but no debugger and cl-lib is not loaded. cl-lib only gets loaded after the Debugger pops up once ...


1

Do you need the macros at runtime? If you need them only at compile time, do this: (eval-when-compile (require 'cl)) If you need them also at runtime (with byte-compiled code) then do this: (load-library "cl-macs")


Only top voted, non community-wiki answers of a minimum length are eligible