unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Async shell-command-to-string?
@ 2008-06-01 22:53 Johan Lindström
  2008-06-01 23:11 ` Lennart Borgman (gmail)
  2008-06-03  0:47 ` Johan Lindström
  0 siblings, 2 replies; 3+ messages in thread
From: Johan Lindström @ 2008-06-01 22:53 UTC (permalink / raw)
  To: help-gnu-emacs

Hi all!

I'd like to run something similar to shell-command-to-string, but 
that runs "asynchronously" in a "subprocess" in the "background". 
I've searched for all those things without finding anything suitable.

async-shell-command-to-string or whatever it would be called would 
take a callback for the string output when the subprocess is done.

Is there anything like that already?

If not, any hints on how to implement it? The async part of 
shell-command looks promising to nick.


/J





^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Async shell-command-to-string?
  2008-06-01 22:53 Async shell-command-to-string? Johan Lindström
@ 2008-06-01 23:11 ` Lennart Borgman (gmail)
  2008-06-03  0:47 ` Johan Lindström
  1 sibling, 0 replies; 3+ messages in thread
From: Lennart Borgman (gmail) @ 2008-06-01 23:11 UTC (permalink / raw)
  To: Johan Lindström; +Cc: help-gnu-emacs

Johan Lindström wrote:
> Hi all!
> 
> I'd like to run something similar to shell-command-to-string, but that 
> runs "asynchronously" in a "subprocess" in the "background". I've 
> searched for all those things without finding anything suitable.
> 
> async-shell-command-to-string or whatever it would be called would take 
> a callback for the string output when the subprocess is done.
> 
> Is there anything like that already?
> 
> If not, any hints on how to implement it? The async part of 
> shell-command looks promising to nick.


Look in the Emacs manual:

   (info "(elisp) Processes")




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Async shell-command-to-string?
  2008-06-01 22:53 Async shell-command-to-string? Johan Lindström
  2008-06-01 23:11 ` Lennart Borgman (gmail)
@ 2008-06-03  0:47 ` Johan Lindström
  1 sibling, 0 replies; 3+ messages in thread
From: Johan Lindström @ 2008-06-03  0:47 UTC (permalink / raw)
  To: help-gnu-emacs

At 23:53 2008-06-01, Johan Lindström wrote:
>Hi all!
>
>I'd like to run something similar to shell-command-to-string, but 
>that runs "asynchronously" in a "subprocess" in the "background". 
>I've searched for all those things without finding anything suitable.
>
>async-shell-command-to-string or whatever it would be called would 
>take a callback for the string output when the subprocess is done.

This is the result of a couple of hours of (rather fun) hacking.

I haven't plugged it into the app yet so I haven't seen it work in 
real life, but manual tests look good (including concurrent calls).


(require 'cl)

(defun async-shell-command-to-string (command callback)
   "Execute shell command COMMAND asynchronously in the
   background.

Return the temporary output buffer which command is writing to
during execution.

When the command is finished, call CALLBACK with the resulting
output as a string."
   (lexical-let
       ((output-buffer (generate-new-buffer " *temp*"))
        (callback-fun callback))
     (set-process-sentinel
      (start-process "Shell" output-buffer shell-file-name 
shell-command-switch command)
      (lambda (process signal)
        (when (memq (process-status process) '(exit signal))
          (with-current-buffer output-buffer
            (let ((output-string
                   (buffer-substring-no-properties
                    (point-min)
                    (point-max))))
              (funcall callback-fun output-string)))
          (kill-buffer output-buffer))))
     output-buffer))

(provide 'async-shell-command-to-string)


Code review anyone? I'm an elisp newbie so I'm certain there are 
things to improve. Rip it apart.


/J





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-06-03  0:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-06-01 22:53 Async shell-command-to-string? Johan Lindström
2008-06-01 23:11 ` Lennart Borgman (gmail)
2008-06-03  0:47 ` Johan Lindström

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).