[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Async shell-command-to-string?
From: |
Johan Lindström |
Subject: |
Re: Async shell-command-to-string? |
Date: |
Tue, 03 Jun 2008 01:47:01 +0100 |
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