TransWikia.com

use-package: autoload function outside the main package file

Emacs Asked on November 15, 2021

I am trying to install simplenote2 to read my org files on my phone.
The official setup method is:

(require 'simplenote2)
(setq simplenote2-email "[email protected]")
(setq simplenote2-password "yourpassword")
(simplenote2-setup)

and the app starts with

M-x simplenote2-list

In practice credentials are not necessary in clear and (require...) can be omitted because simplenote2-setup is autoloaded.

I would like to use a use-package declaration, therefore I am using:

(use-package simplenote2
  :config
  (setq simplenote2-email "[email protected]")
  (setq simplenote2-password "yourpassword")
  (setq simplenote2-notes-mode (quote org-mode))
  (simplenote2-setup))

(The extra line is for org-mode integration).

When I start the library with M-x simplenote2-list, I get an error due to a void variable simplenote2-notes-info-version.
This variable is defined through a defvar in simplenote2.el. So, it seems that the use-package declaration is not requiring simplenote2.el.

The likely cause is that the autoloaded function simplenote2-list is defined in the separate file simplenote2-list.el. Therefore simplenote2-list requires only simplenote2-list.el, but not simplenote2.el.

If this is the cause, how can I instruct use-package to require also simplenote2.el the first time M-x simplenote2-list is run?
Or alternatively: before running M-x simplenote2-list, how can I run the autoloaded (simplenote2-setup), which automatically requires simplenote2.el?

Note that I have the global option use-package-always-defer set true.

One Answer

You cannot easily run simplenote2-setup when lazy loading the package.

However, there are several options, to make lazy loading happening:


1. use of :after and defining a use-package declaration for simplenote2-list i.e.:

(use-package simplenote2-list
  :after (simplenote2))

2. require simplenote2 from simplenote2-list use-package declaration

(use-package simplenote2-list
  :config (require 'simplenote2))

3. you could define a keybinding for simplenote2-list from simplenote2 use-package declaration:

(use-package simplenote2
  :bind ("C-c s" . simplenote2-list)
  :config
  ...)

This works for the same reason like the next option.


4. a somewhat hacky one, declaring simplenote2-list as command of simplenote2. This works because simplenote2-list is required by simplenote2.

(use-package simplenote2
  :commands (simplenote2-list)
  :config
  ...)

5. if simplenotes2 is able to be started with hooks or mode-interpreters, you could emulate the simplenote2-setup with keywords :mode or :hook. Please read the use-package manual.

Answered by jue on November 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP