Eisen
Eisen

Reputation: 480

In Emacs, how can I load a certain file when (require 'x) is called?

I need CEDET for eassist (eassist-list-methods is quite handy). In eassist.el there's the line

(require 'semantic)

which fails if CEDET isn't loaded. The thing is that I don't need CEDET all the time and it takes a long time to load so I want to defer loading it until I call eassist-list-methods.

Is there a way to run

(load "cedet")

when semantic (or something else that is provided by CEDET) is required?

I'm looking for a simple solution that doesn't change eassist.el.

Upvotes: 0

Views: 616

Answers (3)

Drew
Drew

Reputation: 30699

I might be misunderstanding you, but if not the answer is autoload: you want to load eassist.el only when you invoke one of its commands. When it loads it will load semantic or CEDET or whatever it needs -- that's not your problem (it should be taken care of by the design of library eassist.el).

(autoload 'eassist-list-methods "eassist" nil t)

Upvotes: 0

cjm
cjm

Reputation: 62109

Genehack is probably right; I'm being too literal in answering the question. The best way to handle something like this is to figure out which function(s) are required by external code, and add autoloads for them.

But if autoload won't work in your case, the normal way to do something when a file is loaded is to do

(eval-after-load "semantic" '(load "cedet"))

But I just noticed that you say that semantic.el fails to load if CEDET hasn't been loaded first. As implied by the name, eval-after-load runs the code after the specified file is loaded.

You can try finding a different file to trigger loading, instead of using semantic.el. (Perhaps some other file that semantic.el requires.)

If necessary, you could hook into require:

(defadvice require (before CEDET-require activate)
  (if (eq 'semantic (ad-get-arg 0))
      (load "cedet")))

Although (load "cedet") should probably be (require 'cedet), or you'll wind up reloading it every time. (I'm not sure if CEDET has a (provide 'cedet), so I didn't do it that way in my example.)

Note that putting advice on require will not do anything if semantic has already been loaded, so you may need to check (featurep 'semantic) first and load cedet.el immediately if necessary.

Upvotes: 4

genehack
genehack

Reputation: 140758

Assuming you have all the CEDET stuff in your load-path something like:

(autoload 'eassist-list-methods "cedet" nil t)

in your .emacs.d/init.el (or other init file) should do the trick.

Upvotes: 1

Related Questions