unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Johan Lindström <johanl@DarSerMan.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Async shell-command-to-string?
Date: Tue, 03 Jun 2008 01:47:01 +0100	[thread overview]
Message-ID: <E1K3KfK-0006Go-Du@lists.gnu.org> (raw)
In-Reply-To: <E1K2wQC-0003ay-DH@lists.gnu.org>

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





      parent reply	other threads:[~2008-06-03  0:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1K3KfK-0006Go-Du@lists.gnu.org \
    --to=johanl@darserman.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).