unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* shell-command-on-region but with command line arguments
@ 2022-11-07 15:51 Luca Ferrari
  2022-11-07 17:22 ` Emanuel Berg
  2022-11-07 19:21 ` Bruno Barbier
  0 siblings, 2 replies; 8+ messages in thread
From: Luca Ferrari @ 2022-11-07 15:51 UTC (permalink / raw)
  To: help-gnu-emacs

Hi all,
I'm trying to configure my Perl environment to be able to run the
buffer I'm editing, so I created a function to run
shell-command-on-region and bound to C-c r:

(defun fluca1978/run-perl-on-region ()
"A function to invoke Perl on the current region or buffer.
If a region is active, the perl interpreter will be executed on such a region;
otherwise if no region is active, the interpreter will be executed against the
whole buffer."
    (interactive)
    (let ((b (if mark-active (min (point) (mark)) (point-min)))
      (e (if mark-active (max (point) (mark)) (point-max)))
      (perl "perl" ))
      (shell-command-on-region b e perl)))

  ;; bind the function to run when
  ;; C-c r
  ;; in cperl-mode is hit
  (eval-after-load "cperl-mode"
    '(progn
       (define-key cperl-mode-map   (kbd "C-c r")
'fluca1978/run-perl-on-region)))


So far, so good, it works for me.
I was wondering if there's a way to prompt me for optional command
line arguments, so that the command should run against the
buffer/region followed by a list of arguments.
Any idea?

Thanks,
Luca



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shell-command-on-region but with command line arguments
  2022-11-07 15:51 shell-command-on-region but with command line arguments Luca Ferrari
@ 2022-11-07 17:22 ` Emanuel Berg
  2022-11-07 19:21 ` Bruno Barbier
  1 sibling, 0 replies; 8+ messages in thread
From: Emanuel Berg @ 2022-11-07 17:22 UTC (permalink / raw)
  To: help-gnu-emacs

Luca Ferrari wrote:

> (defun fluca1978/run-perl-on-region () [...]

No real need for any -region functions IMO, check this out.

(This, in turn should be integrated in `interactive' and/or
`defun', see the other thread ... `cl-defun' with it's default
values is interesting, in principle we could even decouple
argument docstrings and have all that fully automated except
for the reading and writing parts ...)

(defun use-region ()
  (when (use-region-p)
    (list (region-beginning) (region-end)) ))

(defun test-dwim (&optional beg end)
  (interactive (use-region))
  (or beg (setq beg (point-min)))
  (or end (setq end (point-max)))
  (message "%d %d" beg end) )

https://dataswamp.org/~incal/emacs-init/dwim.el

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shell-command-on-region but with command line arguments
  2022-11-07 15:51 shell-command-on-region but with command line arguments Luca Ferrari
  2022-11-07 17:22 ` Emanuel Berg
@ 2022-11-07 19:21 ` Bruno Barbier
  2022-11-08  0:13   ` Emanuel Berg
  2022-11-09  7:21   ` Luca Ferrari
  1 sibling, 2 replies; 8+ messages in thread
From: Bruno Barbier @ 2022-11-07 19:21 UTC (permalink / raw)
  To: Luca Ferrari, help-gnu-emacs


Luca Ferrari <fluca1978@gmail.com> writes:

> I was wondering if there's a way to prompt me for optional command
> line arguments, so that the command should run against the
> buffer/region followed by a list of arguments.
> Any idea?

The section "Defining Commands" of the elisp
manual 

  (info "(elisp)Defining Commands")

should allow you to prompt for additional arguments.


Bruno



> Thanks,
> Luca



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shell-command-on-region but with command line arguments
  2022-11-07 19:21 ` Bruno Barbier
@ 2022-11-08  0:13   ` Emanuel Berg
  2022-11-09  7:21   ` Luca Ferrari
  1 sibling, 0 replies; 8+ messages in thread
From: Emanuel Berg @ 2022-11-08  0:13 UTC (permalink / raw)
  To: help-gnu-emacs

Bruno Barbier wrote:

>> I was wondering if there's a way to prompt me for optional
>> command line arguments, so that the command should run
>> against the buffer/region followed by a list of arguments.
>> Any idea?
>
> The section "Defining Commands" of the elisp
> manual 
>
>   (info "(elisp)Defining Commands")
>
> should allow you to prompt for additional arguments.

Ah, that was the question?

If so, there are several ways to do that depending on what you
want. For interactive use you can do this:

(let ((args "-h -l -X"))
  (read-string
   (format-prompt "args" args)
   nil nil args) )

Eval and try: RET, M-n, type whatever ...

(Shouldn't `read-string' with DEFAULT-VALUE do the
`format-prompt' part for us BTW?)

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shell-command-on-region but with command line arguments
  2022-11-07 19:21 ` Bruno Barbier
  2022-11-08  0:13   ` Emanuel Berg
@ 2022-11-09  7:21   ` Luca Ferrari
  2022-11-09  7:53     ` Bruno Barbier
  1 sibling, 1 reply; 8+ messages in thread
From: Luca Ferrari @ 2022-11-09  7:21 UTC (permalink / raw)
  To: Bruno Barbier; +Cc: help-gnu-emacs

On Mon, Nov 7, 2022 at 8:15 PM Bruno Barbier <brubar.cs@gmail.com> wrote:
>
>
> Luca Ferrari <fluca1978@gmail.com> writes:
>
> > I was wondering if there's a way to prompt me for optional command
> > line arguments, so that the command should run against the
> > buffer/region followed by a list of arguments.
> > Any idea?
>
> The section "Defining Commands" of the elisp
> manual
>
>   (info "(elisp)Defining Commands")

Thanks, now I know that I need to specify a format in (interactive) to
get my arguments.
The problem is that I'm using (shell-command-on-region) that does not
allow me to insert arguments to the command, and I don't know how the
command is effectively invoked (e.g., piping the region to standard
input, using a temporary file) so I'm not able to "append" my
arguments.
I need a little more help.

Thanks,
Luca



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shell-command-on-region but with command line arguments
  2022-11-09  7:21   ` Luca Ferrari
@ 2022-11-09  7:53     ` Bruno Barbier
  2022-11-09 10:37       ` Emanuel Berg
  2022-11-09 11:40       ` Luca Ferrari
  0 siblings, 2 replies; 8+ messages in thread
From: Bruno Barbier @ 2022-11-09  7:53 UTC (permalink / raw)
  To: Luca Ferrari; +Cc: help-gnu-emacs

Luca Ferrari <fluca1978@gmail.com> writes:

> The problem is that I'm using (shell-command-on-region) that does not
> allow me to insert arguments to the command, and I don't know how the
> command is effectively invoked (e.g., piping the region to standard
> input, using a temporary file) so I'm not able to "append" my
> arguments.

The command will receive the region as its standard input (see the help
of the command shell-command-on-region):
   "Execute string COMMAND in inferior shell with region as input."


Couldn't you just customize your own shell command string ?

Something like:

    (defun my-on-region (where how)
      (interactive "sWhere: \nsHow: \n")
      (let ((b (if mark-active (min (point) (mark)) (point-min)))
            (e (if mark-active (max (point) (mark)) (point-max)))
            ;; Build a command that uses the arguments WHERE and
            ;; HOW, and also display the standard input.
            (perl (format "echo \"BEGIN Where=%s, How=%s\"; cat; echo \"\nEND\"" 
                          (shell-quote-argument where) 
                          (shell-quote-argument how))))
        (shell-command-on-region b e perl)))

Bruno




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shell-command-on-region but with command line arguments
  2022-11-09  7:53     ` Bruno Barbier
@ 2022-11-09 10:37       ` Emanuel Berg
  2022-11-09 11:40       ` Luca Ferrari
  1 sibling, 0 replies; 8+ messages in thread
From: Emanuel Berg @ 2022-11-09 10:37 UTC (permalink / raw)
  To: help-gnu-emacs

Bruno Barbier wrote:

> Something like:
>
>   (defun my-on-region (where how) ... )

(defun command (cmd &optional args beg end) ...)

where args is a list of arguments, &optional arguments
including args LOL default to nil which in this case is fine
as that is an empty list, i.e., args holds no arguments,
however "beg" and "end" has to be set explicitly, and not just
for interactive use.

Close to this example ...

(defun use-region ()
  (when (use-region-p)
    (list (region-beginning) (region-end)) ))

(defun test-dwim-2 (re &optional beg end)
  (interactive `(,(read-regexp "re: ")
                 ,@(use-region) ))
  (or beg (setq beg (point-min)))
  (or end (setq end (point-max)))
  (message "%d %d %s" beg end re) )

[ That interactive spec couldn't have been fully integrated
  with the "R" code letter (that doesn't exist) anyway since
  there seems to be no code letter for regexps either? ]

Or maybe even better:

  (defun command (cmd &optional beg end &rest args) ... )

That will mean a lot of ugly

  (command "havoc" nil nil "--nuclear")

in Lisp tho, yet another option would be

  (defun command (cmd &optional args beg end) ... )

which is the same as the first suggestion only here args can
be either non-nil that isn't a list, say "-n", in what case
it's the one and only argument, otherwise it'd be a list of
arguments as earlier.

But then one cannot send nil as a single argument, since that
means no arguments? Just do ("nil") then ...

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shell-command-on-region but with command line arguments
  2022-11-09  7:53     ` Bruno Barbier
  2022-11-09 10:37       ` Emanuel Berg
@ 2022-11-09 11:40       ` Luca Ferrari
  1 sibling, 0 replies; 8+ messages in thread
From: Luca Ferrari @ 2022-11-09 11:40 UTC (permalink / raw)
  To: Bruno Barbier; +Cc: help-gnu-emacs

On Wed, Nov 9, 2022 at 8:47 AM Bruno Barbier <brubar.cs@gmail.com> wrote:
>
> Luca Ferrari <fluca1978@gmail.com> writes:
>
> > The problem is that I'm using (shell-command-on-region) that does not
> > allow me to insert arguments to the command, and I don't know how the
> > command is effectively invoked (e.g., piping the region to standard
> > input, using a temporary file) so I'm not able to "append" my
> > arguments.
>
> The command will receive the region as its standard input (see the help
> of the command shell-command-on-region):
>    "Execute string COMMAND in inferior shell with region as input."
>
>
> Couldn't you just customize your own shell command string ?

Yes, and it works, thanks. I simply thought there was an embedded
function to invoke the shell command with arguments.
However, I've used this:

          (perl (format "perl - %s"
                argv)))
      (shell-command-on-region b e perl)))

without the quoting around argv because otherwise multiple arguments
(all into argv) would have been seen as a single one on the Perl side.

Thanks.
Luca



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-11-09 11:40 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-07 15:51 shell-command-on-region but with command line arguments Luca Ferrari
2022-11-07 17:22 ` Emanuel Berg
2022-11-07 19:21 ` Bruno Barbier
2022-11-08  0:13   ` Emanuel Berg
2022-11-09  7:21   ` Luca Ferrari
2022-11-09  7:53     ` Bruno Barbier
2022-11-09 10:37       ` Emanuel Berg
2022-11-09 11:40       ` Luca Ferrari

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).