all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* M-x call with optional arg; call from Elisp
@ 2013-05-27 16:03 Emanuel Berg
  2013-05-27 16:13 ` Emanuel Berg
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Emanuel Berg @ 2013-05-27 16:03 UTC (permalink / raw
  To: help-gnu-emacs

Take a look at the below code, and you'll understand immediately what
I ask.

1. How can I call a function with M-x and provide an optional string
arg?

2. Why do I get 0 when I call from Elisp? (Is it the return code - $?
- and not the output?) I tried with eval and funcall as well:
same. How do I call from Elisp?

;;; word count
(defun words (&optional opts)
  "Count buffer words with wc OPTS, or wc -w if not OPTS is given."
  (interactive)
  (defvar count-cmd)
  (setq count-cmd (format "wc %s" (if (stringp opts) opts "-w")))
  (if mark-active
    (shell-command-on-region (region-beginning) (region-end) count-cmd)
    (shell-command-on-region (point-min) (point-max) count-cmd) ))
(words "-l") ; doesn't work (0) - can I call this with M-x and an arg?
(words)      ; same, but `M-x words' works

(defun count-buffer-wc-option (option)
  (interactive "s wc options: ")
  "Count buffer according to wc OPTION."
  (shell-command-on-region (point-min) (point-max)
                           (format "wc %s" option)) )
(count-buffer-wc-option "-l") ; same, but
                              ; `M-x count-... RET -l' works

-- 
Emanuel Berg - programmer (hire me! CV below)
computer projects: http://user.it.uu.se/~embe8573
internet activity: http://home.student.uu.se/embe8573


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

* Re: M-x call with optional arg; call from Elisp
  2013-05-27 16:03 M-x call with optional arg; call from Elisp Emanuel Berg
@ 2013-05-27 16:13 ` Emanuel Berg
  2013-05-28 19:24   ` Emanuel Berg
  2013-05-27 18:59 ` Stefan Monnier
       [not found] ` <mailman.476.1369681518.22516.help-gnu-emacs@gnu.org>
  2 siblings, 1 reply; 6+ messages in thread
From: Emanuel Berg @ 2013-05-27 16:13 UTC (permalink / raw
  To: help-gnu-emacs

Emanuel Berg <embe8573@student.uu.se> writes:

> (defun count-buffer-wc-option (option)
>   (interactive "s wc options: ")
>   "Count buffer according to wc OPTION."
>   (shell-command-on-region (point-min) (point-max)
>                            (format "wc %s" option)) )

Oh, I should say not to cause confusion, the above function - I only
wrote it for test purposes - if I get the other function to work,
that should cover both.

-- 
Emanuel Berg - programmer (hire me! CV below)
computer projects: http://user.it.uu.se/~embe8573
internet activity: http://home.student.uu.se/embe8573


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

* Re: M-x call with optional arg; call from Elisp
  2013-05-27 16:03 M-x call with optional arg; call from Elisp Emanuel Berg
  2013-05-27 16:13 ` Emanuel Berg
@ 2013-05-27 18:59 ` Stefan Monnier
       [not found] ` <mailman.476.1369681518.22516.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 6+ messages in thread
From: Stefan Monnier @ 2013-05-27 18:59 UTC (permalink / raw
  To: help-gnu-emacs

> 1. How can I call a function with M-x and provide an optional string
> arg?

You have to put in the `interactive' spec a description of what value
to pass.

> 2. Why do I get 0 when I call from Elisp?

Because that's the value of the last expression.

> (Is it the return code - $? - and not the output?)

Yes.

>   (defvar count-cmd)
>   (setq count-cmd (format "wc %s" (if (stringp opts) opts "-w")))

`defvar does not define the variable, nor declares it as local.
Quite the opposite, it basically declares it as global (or rather as
dynamically-scoped, which ends up being fairly close).
You want to use

  (let ((count-cmd (format "wc %s" (if (stringp opts) opts "-w"))))

instead.

>   (if mark-active
>     (shell-command-on-region (region-beginning) (region-end) count-cmd)
>     (shell-command-on-region (point-min) (point-max) count-cmd) ))

Don't use shell-command-on-region from Elisp.  Use call-process-region instead.


        Stefan




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

* Re: M-x call with optional arg; call from Elisp
  2013-05-27 16:13 ` Emanuel Berg
@ 2013-05-28 19:24   ` Emanuel Berg
  0 siblings, 0 replies; 6+ messages in thread
From: Emanuel Berg @ 2013-05-28 19:24 UTC (permalink / raw
  To: help-gnu-emacs

First, I got this reply as a mail, but I don't see it on the list.

Quoting "B. T. Raven":

> As in your "count-buffer-wc-option" function below. Of course the
> whole exercise can be avoided with M-| or C-x h M-|

`C-x h' would be mark-whole-buffer, and M-| (pipe)
shell-command-on-region. Tough that is certainly an alternative, I
think `M-x words' is a lot better (on the region if there is one;
otherwise, the entire buffer). So this is not an "exercise", I set it
up because I intend to use it.

> Since a null string is still a string you need something like
> (length opts).

My thought was rather - it is not even a null string if no optional
arg is passed. I don't know if that is the case; but that would be
easy to find out once I'm able to call the functions as intended,
with or without the optional arg.

-- 
Emanuel Berg - programmer (hire me! CV below)
computer projects: http://user.it.uu.se/~embe8573
internet activity: http://home.student.uu.se/embe8573


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

* Re: M-x call with optional arg; call from Elisp
       [not found] ` <mailman.476.1369681518.22516.help-gnu-emacs@gnu.org>
@ 2013-05-28 19:52   ` Emanuel Berg
  2013-05-30  5:44     ` Kevin Rodgers
  0 siblings, 1 reply; 6+ messages in thread
From: Emanuel Berg @ 2013-05-28 19:52 UTC (permalink / raw
  To: help-gnu-emacs

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> 1. How can I call a function with M-x and provide an optional
>> string arg?
>
> You have to put in the `interactive' spec a description of what
> value to pass.

Yes, that did it: this works from M-x:

(defun words (&optional opts)
  "Count buffer words with wc OPTS, or wc -w if not OPTS is given."
  (interactive "s (RET for -w) options to wc: ")
  (let*((options (if (not (string= "" opts)) opts "-w"))
        (count-cmd (format "wc %s" options)) )
   (if mark-active
    (shell-command-on-region (region-beginning) (region-end) count-cmd)
    (shell-command-on-region (point-min) (point-max) count-cmd) )))

> defvar does not define the variable, nor declares it as local.
> Quite the opposite, it basically declares it as global (or rather
> as dynamically-scoped, which ends up being fairly close). You want
> to use let.

Aha, when I started with Elisp, I always used let, but then I found
defvar and started using it, because the syntax is much easier to
use. I thought defvar gave a local variable if in a defun.

> Don't use shell-command-on-region from Elisp. Use
> call-process-region instead.

The above function seems to work. shell-command-on-region and
call-process-region are not syntactically interchangeable, but I'll
do it if you care to tell me the advantage.
-- 
Emanuel Berg - programmer (hire me! CV below)
computer projects: http://user.it.uu.se/~embe8573
internet activity: http://home.student.uu.se/embe8573


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

* Re: M-x call with optional arg; call from Elisp
  2013-05-28 19:52   ` Emanuel Berg
@ 2013-05-30  5:44     ` Kevin Rodgers
  0 siblings, 0 replies; 6+ messages in thread
From: Kevin Rodgers @ 2013-05-30  5:44 UTC (permalink / raw
  To: help-gnu-emacs

On 5/28/13 1:52 PM, Emanuel Berg wrote:
> Stefan Monnier<monnier@iro.umontreal.ca>  writes:
>> Don't use shell-command-on-region from Elisp. Use
>> call-process-region instead.
>
> The above function seems to work. shell-command-on-region and
> call-process-region are not syntactically interchangeable, but I'll
> do it if you care to tell me the advantage.

If you are not using shell features such as redirection, pipes, globbing (file
name expansion), variable expansion, conditional and iterative control flow
etc. then there is no need to invoke an additional/intermediary shell process
between emacs and the command you want to run.

-- 
Kevin Rodgers
Denver, Colorado, USA




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

end of thread, other threads:[~2013-05-30  5:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-05-27 16:03 M-x call with optional arg; call from Elisp Emanuel Berg
2013-05-27 16:13 ` Emanuel Berg
2013-05-28 19:24   ` Emanuel Berg
2013-05-27 18:59 ` Stefan Monnier
     [not found] ` <mailman.476.1369681518.22516.help-gnu-emacs@gnu.org>
2013-05-28 19:52   ` Emanuel Berg
2013-05-30  5:44     ` Kevin Rodgers

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.