jwiegley / emacs-async

Simple library for asynchronous processing in Emacs
GNU General Public License v3.0
838 stars 68 forks source link

FR: An easy way to specify required modules #39

Open stsquad opened 9 years ago

stsquad commented 9 years ago

I've been experimenting with using this as a wrapper for some org-mode babel lisp. However I'm adding a bunch of boiler plate to require libraries for the child emacs. I notice you already jump some hoops to make "async" itself visible to the child. Would it be possible to add something like the inject-variables mechanism to make some libraries available to the child?

alphapapa commented 6 years ago

This seems very important to me. I'm trying to use this library to make a package that uses emacsql and org, and it doesn't seem practical to manually initialize the package system in the START-FUNC. It's not even correct, because it's dependent on each user's config. Some users won't even use package.el.

How do I give the child Emacs process access to the same libraries that are loaded in the parent process? What is the correct way to do this?

thierryvolpiatto commented 6 years ago

alphapapa notifications@github.com writes:

This seems very important to me. I'm trying to use this library to make a package that uses emacsql and org, and it doesn't seem practical to manually initialize the package system in the START-FUNC. It's not even correct, because it's dependent on each user's config. Some users won't even use package.el.

How do I give the child Emacs process access to the same libraries that are loaded in the parent process? What is the correct way to do this?

Install your libraries in ..emacs/site-lisp and use async-quiet-switch == "-q", like this all your libraries will be in load-path.

-- Thierry

alphapapa commented 6 years ago

Thanks, Thierry. I'm not sure exactly what you mean, because that would still be dependent on the user's config. I was able to get it working by doing (setq load-path ,load-path) in the START-FUNC to pass the parent process's already-initialized load-path.

Maybe this should be documented in the readme, because now that I know about it, it seems like a straightforward solution. Or maybe there should even be an argument to async-start to do this automatically, because it seems like a commonly needed solution.

stsquad commented 6 years ago

So I ended up doing this which is good enough for me:

(defun my-add-first-elfeed-enclosure-to-transmission ()
  "Queue the first enclosure (if it is a torrent)."
  (interactive)
  (let ((enclosures (elfeed-entry-enclosures elfeed-show-entry)))
    (when (and enclosures
               (string-match-p
                "application/x-bittorrent"
                (nth 1 (-first-item enclosures))))
      (transmission-add (-first-item (-first-item enclosures))))))

(use-package elfeed
  :ensure t
  :bind (:map elfeed-show-mode-map
              ("C-c C-c" . my-add-first-elfeed-enclosure-to-transmission))
  :config (setq elfeed-enclosure-default-dir "~/torrent/"
                elfeed-log-level 'debug
                elfeed-use-curl 't))

Is this worth adding to a FAQ? I'm not sure how hidden this will be once you close the issue.

alphapapa commented 6 years ago

@stsquad Did you post that on the wrong issue?

stsquad commented 6 years ago

@alphapapa I totally did, sorry - that was meant for https://github.com/skeeto/elfeed/issues/274

Fuco1 commented 1 year ago

The best way to do this with somewhat modern Emacs is to use lexical binding:

(let ((load--path load-path))
  (lambda ()
    (let ((load-path load--path))
      your code here
)))

This will use the parent load path for the child process. Similar construct can be used for any value which does not depend on the parent environment (like buffers).