Re: sending multiple arguments to shell command with defun andinteractive

"John Bullock" <[email protected]> Wed, 19 Oct 2005 19:31:37 -0700
Newsgroups gmane.emacs.xemacs.general
Message-ID <[email protected]>
> (shell-command-on-region (point) (mark)
>  (format "perl c:/perl/misc/perlhello.pl \"%s\" \"%s\"" old new) nil t)

Thank you.  This definitely moves me in the right direction.

The only problem that remains for me is that I want the
region -- the block of text I've highlighted in XEmacs --
to be passed to the Perl script as another argument,
joining "old" and "new."  This code more or less does
what I have in mind:

(defun hello (string_to_replace old new)
  (interactive "sString to replace: \nsOld: \nsNew: ")
  (shell-command-on-region (point) (mark)
      (format "perl c:/perl/misc/perlhello.pl \"%s\" \"%s\" \"%s\""
string_to_replace old new) nil t))

But it's inefficient.  I want the "string_to_replace" argument to be nothing
more than the region I've highlighted.  But with this code, I need to manually
re-type that code when prompted for "String to replace: ".  How can I
automatically pass the highlighted region to my Perl script?

Thank you again,
--John


"Glynn Clements" <[email protected]> wrote in message
news:[email protected]...
>
> John Bullock wrote:
>
>>         I often want to process a block of text with a Perl script, and
>> then
>> have the original block replaced by the output from the Perl script.  I
>> think
>> this should be very easy to do in XEmacs.  Indeed, I've almost done it.
>> But
>> not quite.
>>         This is the not-quite-working function I have in custom.el:
>>
>>                 (defun perlhello (old new)
>>                   (interactive)
>>                   (shell-command-on-region (point)
>>                       (mark) "perl c:/perl/misc/perlhello.pl" nil t))
>>
>> The problem is simple: I don't know how to pass arguments "old" and "new"
>> to
>> perlhello.pl.
>
> Something like:
>
> (shell-command-on-region (point) (mark)
>  (format "perl c:/perl/misc/perlhello.pl \"%s\" \"%s\"" old new) nil t)
>
> This might fall down if either old or new contain characters which are
> meaningful to the shell when inside quotes (e.g. quotes). In that
> case, you'd have to use call-process directly.
>
>> I am sure that the fix has something to do with
>> (interactive) -- but more than that, I don't know.
>
> That's a separate issue.
>
>> I'd like to execute this command just by highlighting a block of text and
>> typing "M-x perlhello old new", where "old" and "new" are just strings that
>> I
>> will pass to the script.
>
> You can't include the arguments along with the command (apart from
> anything else, Space is bound to minibuffer-complete-word for
> minibuffer input). You can use e.g.:
>
> (interactive "sOld: \nsNew: ")
>
> You would type "M-x perlhello RET", and it will prompt for Old: and
> New: separately.
>
> Also, you can get the region via (interactive "r"), e.g.
>
> (defun perlhello (start end old new)
>   (interactive "rsOld: \nsNew: ")
>
> This has the advantage (over using (point) and (mark) directly) that
> the arguments will always be passed smallest-first.
>
> -- 
> Glynn Clements <[email protected]>
>
>