* Can I use Emacs as pipe-thru formatter?
@ 2003-08-05 14:18 Irving Kimura
2003-08-05 15:00 ` Barry Margolin
2003-08-05 15:32 ` Kevin Rodgers
0 siblings, 2 replies; 4+ messages in thread
From: Irving Kimura @ 2003-08-05 14:18 UTC (permalink / raw)
I'm writing a Perl script that, among other things, needs to pretty
print JavaScript code. The formatting produced by the Emacs module
javascript-mode.el would be adequate for my purposes, but I don't
know how to apply it from within a Perl program.
(I'm sure that this is trivial to do from within an Emacs Lisp
script, but I am not very handy in this language, and besides, the
task is part of a much larger Perl program, so I would prefer to
do this from within Perl.)
Since, under Unix at least, it's not too difficult for a Perl script
to pipe a string through an external program, one possible way to
do what I want to do would be to write an "Emacs pipe" that somehow
uses javascript-mode.el to pretty print its stdin and spits the
results to stdout:
% cat ugly.js | emacs_js_pretty_printer > pretty.js
But I have no idea about how to write such a pipe. I would greatly
appreciate any help or advice on how to do this, or any other ideas
on how to tackle this problem.
Many thanks!
-Irv
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Can I use Emacs as pipe-thru formatter?
2003-08-05 14:18 Can I use Emacs as pipe-thru formatter? Irving Kimura
@ 2003-08-05 15:00 ` Barry Margolin
2003-08-05 16:21 ` Oliver Scholz
2003-08-05 15:32 ` Kevin Rodgers
1 sibling, 1 reply; 4+ messages in thread
From: Barry Margolin @ 2003-08-05 15:00 UTC (permalink / raw)
In article <bgoebt$gf0$1@reader1.panix.com>,
Irving Kimura <irving_kimura@lycos.com> wrote:
>Since, under Unix at least, it's not too difficult for a Perl script
>to pipe a string through an external program, one possible way to
>do what I want to do would be to write an "Emacs pipe" that somehow
>uses javascript-mode.el to pretty print its stdin and spits the
>results to stdout:
>
> % cat ugly.js | emacs_js_pretty_printer > pretty.js
>
>But I have no idea about how to write such a pipe. I would greatly
>appreciate any help or advice on how to do this, or any other ideas
>on how to tackle this problem.
Someone asked almost the same question a week or two ago.
Emacs doesn't read and write stdin/stdout, so it can't be used as a filter
in a pipeline. But you can use -batch and -eval to make it run a function
and then exit, so you could have it read ugly.js into a buffer, format it,
and then write the result out to pretty.js.
--
Barry Margolin, barry.margolin@level3.com
Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Can I use Emacs as pipe-thru formatter?
2003-08-05 14:18 Can I use Emacs as pipe-thru formatter? Irving Kimura
2003-08-05 15:00 ` Barry Margolin
@ 2003-08-05 15:32 ` Kevin Rodgers
1 sibling, 0 replies; 4+ messages in thread
From: Kevin Rodgers @ 2003-08-05 15:32 UTC (permalink / raw)
Irving Kimura wrote:
> Since, under Unix at least, it's not too difficult for a Perl script
> to pipe a string through an external program, one possible way to
> do what I want to do would be to write an "Emacs pipe" that somehow
> uses javascript-mode.el to pretty print its stdin and spits the
> results to stdout:
>
> % cat ugly.js | emacs_js_pretty_printer > pretty.js
Try this:
emacs --batch \
ugly.js \
-f javascript-mode \
--eval '(indent-region (point-min) (point-max) nil)' \
--eval '(write-file "pretty.js")'
--
Kevin Rodgers
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Can I use Emacs as pipe-thru formatter?
2003-08-05 15:00 ` Barry Margolin
@ 2003-08-05 16:21 ` Oliver Scholz
0 siblings, 0 replies; 4+ messages in thread
From: Oliver Scholz @ 2003-08-05 16:21 UTC (permalink / raw)
Barry Margolin <barry.margolin@level3.com> writes:
> Emacs doesn't read and write stdin/stdout, so it can't be used as a filter
> in a pipeline.
Well, indeed you can not get a buffer from stdin (AFAICS). But you do
have some limited options. `print' for example prints to stdout in
batch-mode and `read' reads from stdin.
emacs -q --batch --eval '(print "hello")'
emacs -q --batch --eval '(princ (progn (insert initial-scratch-message) (buffer-substring (point-min) (point-max))))'
echo "\"lirum larum\"" | emacs -q --batch --eval '(message (read))'
Of course also:
echo '(print "Hello")' | emacs -q --batch --eval '(eval (read))'
There is even some interactivity possible:
emacs -q --batch --eval '(progn (find-file "~/.emacs") (if (y-or-n-p "Shall I print your .emacs? ") (print (buffer-substring (point-min) (point-max))) (print "O.k. Bye.")))'
Oliver
--
18 Thermidor an 211 de la Révolution
Liberté, Egalité, Fraternité!
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2003-08-05 16:21 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-08-05 14:18 Can I use Emacs as pipe-thru formatter? Irving Kimura
2003-08-05 15:00 ` Barry Margolin
2003-08-05 16:21 ` Oliver Scholz
2003-08-05 15:32 ` Kevin Rodgers
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).