jwiegley/emacs-async

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

NightMachinery opened this issue · 4 comments

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

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

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

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.