jwiegley / emacs-async

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

How can I use the current buffer in the finish function of async-start? #127

Closed NightMachinery closed 2 years ago

NightMachinery commented 3 years ago

I have a function that gets a link from the clipboard, gets its metadata, and outputs richly formatted markdown. I want to insert this rich link asynchronously: The user calls the insertion function, and then is free to do their own editing, and when the rich link is ready, it'll be inserted at the point the user initiated the call. I have tried the following, but I can't pass current-buffer to async lambdas at all:

(defun night/unt ()
  (interactive)
  (let* (
         (link (current-kill 0))
         (my-buffer (current-buffer))
         )
    (async-start (lambda () (let* (
                                   (cmd (concat "brishzr.dash " (shell-quote-argument (concat "ec " (shell-quote-argument link) " | inargsf unt"))))
                                   (text (shell-command-to-string cmd))
                                   )
                              text
                              ))
                 (lambda (text)
                   (message "name: %s my-buffer: %s string: %s" (buffer-name) my-buffer (buffer-string))
                   (with-current-buffer my-buffer (insert text))
                   (save-buffer)
                   ))))

fails with

error in process sentinel: async-handle-result: Invalid read syntax: "#"
error in process sentinel: Invalid read syntax: "#"
f4nyc commented 3 years ago

Hi, I got the same problem, have u solve this or find another way out?

thierryvolpiatto commented 3 years ago

Use buffer-name instead of a buffer object with buffer syntax i.e #<buffer *scratch*>, same for your strings with properties, strip out the properties when passing strings to closures.

f4nyc commented 3 years ago

Use buffer-name instead of a buffer object with buffer syntax i.e #<buffer *scratch*>, same for your strings with properties, strip out the properties when passing strings to closures. Thanks for your nice helping! And I got another question : how could I pass buffer to child process of async-start i.e inside START-FUNC ? The only way I can figure out is saving the buffer to a temporary file and accessing it.

thierryvolpiatto commented 3 years ago

f4nyc notifications@github.com writes:

And I got another question : how could I pass buffer to child
process of async-start i.e inside START-FUNC ?

You can't, emacs sessions can't share buffers.

The only way I can figure out is saving the buffer to a temporary
file and accessing it.

Yes, this would work, it will be long though if you have a long list of buffers to treat.

-- Thierry