unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Question: loading code when two packages are load?
@ 2021-09-08 16:06 Qiantan Hong
  2021-09-08 20:25 ` Stefan Monnier
  2021-09-08 21:44 ` Karl Fogel
  0 siblings, 2 replies; 6+ messages in thread
From: Qiantan Hong @ 2021-09-08 16:06 UTC (permalink / raw)
  To: emacs-devel@gnu.org

I’ve been working on the integration of crdt.el with a few other packages (xscheme.el, comint.el).
Those code only make sense when both crdt.el and the “client” package are loaded.

Is there a way to lazy load those code?

Currently I (require *client-package*) in crdt.el but that causes all client package to be loaded
when crdt.el is loaded.
Or I could remove the require but then that leaves bunch of free variable warnings
(because client package haven’t been loaded) and bunch of command that is
invokable but not usable.

Best,
Qiantan


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Question: loading code when two packages are load?
  2021-09-08 16:06 Question: loading code when two packages are load? Qiantan Hong
@ 2021-09-08 20:25 ` Stefan Monnier
  2021-09-09  2:05   ` Qiantan Hong
  2021-09-08 21:44 ` Karl Fogel
  1 sibling, 1 reply; 6+ messages in thread
From: Stefan Monnier @ 2021-09-08 20:25 UTC (permalink / raw)
  To: Qiantan Hong; +Cc: emacs-devel@gnu.org

> I’ve been working on the integration of crdt.el with a few other packages (xscheme.el, comint.el).
> Those code only make sense when both crdt.el and the “client” package are loaded.
> Is there a way to lazy load those code?

Some ways to address these kinds of problem:

- Move the relevant code to `crdt-<foo>.el` and then arrange for
  `crdt-<foo>.el` to be loaded when need for example using an autoloaded
  `crdt-<foo>-setup` function and in `crdt.el` you do
  `(add-hook '<foo>-mode-hook 'crdt-<foo>-setup).

- Put the code inside an `with-eval-after-load`.

- Just add enough `defvar`s and `declare-function` to silence the
  warnings.

The first solution above is arguably the cleanest, but can be a too
heavy if the code of `crdt-<foo>.el` is trivial.


        Stefan




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Question: loading code when two packages are load?
  2021-09-08 16:06 Question: loading code when two packages are load? Qiantan Hong
  2021-09-08 20:25 ` Stefan Monnier
@ 2021-09-08 21:44 ` Karl Fogel
  1 sibling, 0 replies; 6+ messages in thread
From: Karl Fogel @ 2021-09-08 21:44 UTC (permalink / raw)
  To: Qiantan Hong; +Cc: emacs-devel@gnu.org

On 08 Sep 2021, Qiantan Hong wrote:
>I’ve been working on the integration of crdt.el with a few other 
>packages (xscheme.el, comint.el).
>Those code only make sense when both crdt.el and the “client” 
>package are loaded.
>
>Is there a way to lazy load those code?
>
>Currently I (require *client-package*) in crdt.el but that causes 
>all client package to be loaded
>when crdt.el is loaded.
>Or I could remove the require but then that leaves bunch of free 
>variable warnings
>(because client package haven’t been loaded) and bunch of command 
>that is
>invokable but not usable.

Would the "autoload" facility solve this problem?

See the section "Autoload" in the Elisp manual:

https://www.gnu.org/software/emacs/manual/html_node/elisp/Autoload.html#Autoload

and this introduction:

https://www.gnu.org/software/emacs/manual/html_node/eintr/Autoload.html

Best regards,
-Karl



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Question: loading code when two packages are load?
  2021-09-08 20:25 ` Stefan Monnier
@ 2021-09-09  2:05   ` Qiantan Hong
  2021-09-09  3:31     ` Stefan Monnier
  2021-09-09  4:25     ` Karl Fogel
  0 siblings, 2 replies; 6+ messages in thread
From: Qiantan Hong @ 2021-09-09  2:05 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Karl Fogel, emacs-devel@gnu.org

> - Put the code inside an `with-eval-after-load`.

Thanks! This looks like exactly what I need.
I don’t understand why the manual says
"well-designed Lisp programs should not use with-eval-after-load”
and advocate for using require though. 
Isn’t with-eval-after-load exactly the lazy counterpart of require?

> Would the "autoload" facility solve this problem?

To my understanding autoload triggers file loading when function invoked,
while in my case I need to invoke function on file loading 
(to augment the behavior of that file).
It sounds like after-load-functions (although eval-after-load works as well
because we just need to target some specific packages),
but somehow manual says it’s an “abnormal hook” which I don’t really understand.

The Hooks for Loading info section seems to try very hard to
talk people out of using itself.


Best,
Qiantan


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Question: loading code when two packages are load?
  2021-09-09  2:05   ` Qiantan Hong
@ 2021-09-09  3:31     ` Stefan Monnier
  2021-09-09  4:25     ` Karl Fogel
  1 sibling, 0 replies; 6+ messages in thread
From: Stefan Monnier @ 2021-09-09  3:31 UTC (permalink / raw)
  To: Qiantan Hong; +Cc: emacs-devel@gnu.org, Karl Fogel

> Thanks! This looks like exactly what I need.
> I don’t understand why the manual says
> "well-designed Lisp programs should not use with-eval-after-load”
> and advocate for using require though.
> Isn’t with-eval-after-load exactly the lazy counterpart of require?

Experience shows that `with-eval-after-load` (and its brother
`after-load-functions`) can make debugging more difficult (typically
when you aren't aware that such and such package you're using uses
`with-eval-after-load` on the file you're trying to debug), which is why
we recommend to use alternatives when possible.

For example often you can replace use of `with-eval-after-load` by using
the hook of the (major or minor) mode defined in that file.

> It sounds like after-load-functions (although eval-after-load works as well
> because we just need to target some specific packages),
> but somehow manual says it’s an “abnormal hook” which I don’t really understand.

Normal hooks have a specific calling convention (no arguments, no
return value).  Any hook which uses a different calling convention is
hence an abnormal hook (and this is reflected in its name ending in
`functions` rather than `-hook`).  "Abnormal" does not mean "bad".

> The Hooks for Loading info section seems to try very hard to
> talk people out of using itself.

Emacs is full of tools to shoot oneself (and others) in the foot,
because we feel it's important to make sure users can do that when they
feel the need.  But yes we do try and discourage them from using those
tools ;-)


        Stefan




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Question: loading code when two packages are load?
  2021-09-09  2:05   ` Qiantan Hong
  2021-09-09  3:31     ` Stefan Monnier
@ 2021-09-09  4:25     ` Karl Fogel
  1 sibling, 0 replies; 6+ messages in thread
From: Karl Fogel @ 2021-09-09  4:25 UTC (permalink / raw)
  To: Qiantan Hong; +Cc: Stefan Monnier, emacs-devel@gnu.org

On 09 Sep 2021, Qiantan Hong wrote:
>> - Put the code inside an `with-eval-after-load`.
>
>Thanks! This looks like exactly what I need.
>I don’t understand why the manual says
>"well-designed Lisp programs should not use with-eval-after-load”
>and advocate for using require though. 
>Isn’t with-eval-after-load exactly the lazy counterpart of 
>require?

TIL ("Today I Learned") about `with-eval-after-load'!  It's a good 
day :-).

>> Would the "autoload" facility solve this problem?
>
>To my understanding autoload triggers file loading when function 
>invoked,
>while in my case I need to invoke function on file loading 
>(to augment the behavior of that file).

Ah -- I should have read more slowly and thoroughly to grok this, 
sorry.

>It sounds like after-load-functions (although eval-after-load 
>works as well
>because we just need to target some specific packages),
>but somehow manual says it’s an “abnormal hook” which I don’t 
>really understand.
>
>The Hooks for Loading info section seems to try very hard to
>talk people out of using itself.

Heh.  When using one of those facilities that Emacs has yet 
discourages the use of, I usually put a comment explaining why 
it's really the only solution for that particular situation and 
how therefore this is one of those rare *appropriate* uses.

Best regards,
-Karl



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-09-09  4:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-08 16:06 Question: loading code when two packages are load? Qiantan Hong
2021-09-08 20:25 ` Stefan Monnier
2021-09-09  2:05   ` Qiantan Hong
2021-09-09  3:31     ` Stefan Monnier
2021-09-09  4:25     ` Karl Fogel
2021-09-08 21:44 ` Karl Fogel

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).