reddit / emacs

How to use `use-package` correctly to bind to a custom function?

Tohiko · 2022-06-17 15:22
I have a function `my/tex-command` that relies on the package `tex` and which I want to bind to some key. I handle this with `use-package` as follows: ``` (use-package tex :bind ("C-c C-o C-o" . my/tex-command) :config (defun my/tex-command () (interactive) ;; Stuff )) ``` This works since `tex` is automatically auto-loaded when `my/tex-command` is called. Moreover, the function is defined correctly when `tex` is loaded. However, if I run `(symbol-file 'my/tex-command)` I get back the file `tex.el` instead of the file where I actually defined the function. This is because `load-history` has several entries for `my/tex-command` including ones that associate it to `tex.el` and ones that associate it to the correct files. The result is that my helpful buffers show the wrong file when I look up `my/tex-command`. How do I correctly deal with this situation? I know of two solutions that I want to avoid: - Define a new module which includes `my/tex-command` and which requires `tex` when loaded. This is fine, but it does mean that I have to create many custom modules and I want to have a simpler configuration. - Include `(require tex)` in `my/tex-command` and define it outside the `use-package` statement. I want to avoid having to call `require` commands in functions.
cradlemann · 2022-06-17 18:40
You could use preface for your custom functions. Like [here](https://github.com/Crandel/home/blob/master/.config/emacs/recipes/consult-rcp.el#L56).
[deleted] · 2022-06-18 09:37
[deleted]
Tohiko · 2022-06-18 15:00
Update: It seems that using preface does not autoload the package when my custom command is called. Is that right? How do make sure that the package is autoloaded?
cradlemann · 2022-06-18 15:14
Than you need to use [requires](https://github.com/jwiegley/use-package#prevent-loading-if-dependencies-are-missing)
Tohiko · 2022-06-18 16:56
Thanks @cradlemann. I am not sure how that would help me. What I want is to define my custom command inside the `use-package` of `tex`, so that when it is called the package `tex` is automatically loaded.
cabbagegandalf · 2022-06-21 11:40
You can explicitly tell eMacs to autoload the package [with autoload](https://www.gnu.org/software/emacs/manual/html_node/eintr/Autoload.html) but maybe you can achieve the same by specifying the functions on “:commands”