help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to run shell command with stream input, to get string output


From: Jean Louis
Subject: Re: How to run shell command with stream input, to get string output
Date: Mon, 1 Jul 2019 10:17:16 +0200
User-agent: Mutt/1.10.1 (2018-07-13)

* Marcin Borkowski <address@hidden> [2019-07-01 09:22]:
> 
> On 2019-07-01, at 00:32, Jean Louis <bugs@gnu.support> wrote:
> 
> > Hello,
> >
> > I would like to know how to make in Emacs Lisp the
> > equivalent function to what I have here below in
> > Common Lisp.
> >
> > I wish to feed a string as stream to a command and
> > to get the string output.
> 
> How about `shell-command-to-string'?

That one runs only shell command, without the
input.

I am looking to something equivalent to shell

cat | markdown
Hello
=====

## Hello
CTRL-D here
<h1>Hello</h1>

<h2>Hello</h2>

I have to process thousands of files, I would not
like writing to hard disk the feed data for
processing to spare the hard disk.

As I cannot find solution yet in emacs lisp how to
feed some string as input to the shell command, I
am using now the virtual memory in /run/user/$UID
as this way the hard disk is spared of writing
files.

It would be ideal if I do not write anything to
file system.

(defun command-stream-in-out (command string &rest args)
  (let* ((uid (number-to-string (user-uid)))
         (xdg-runtime-dir (getenv "XDG_RUNTIME_DIR"))
         (runtime-dir (concat "/run/user/" uid))
         (runtime-dir (if xdg-runtime-dir xdg-runtime-dir runtime-dir))
         (infile (concat (slash-add runtime-dir) "markdown-input")))
    (string-to-file-force string infile)
    (with-temp-buffer
      (apply 'call-process command infile (current-buffer) nil args)
      (buffer-string))))

Then I can do something like this:

(defun markdown (string)
  (command-stream-in-out "markdown" string))

(defun pandoc-markdown (string)
  (command-stream-in-out "pandoc" string "-f" "markdown" "-t" "html"))

(markdown "Hello\n=============")

"<h1>Hello</h1>
"

(pandoc-markdown "Hello\n=============")

"<h1 id=\"hello\">Hello</h1>
"

I have solved my problem. But if somebody knows
how to feed the string to command as its input,
without writing to file system, let me know.

Jean



reply via email to

[Prev in Thread] Current Thread [Next in Thread]