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

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

Re: M-x call with optional arg; call from Elisp


From: Stefan Monnier
Subject: Re: M-x call with optional arg; call from Elisp
Date: Mon, 27 May 2013 14:59:57 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

> 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




reply via email to

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