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: Emanuel Berg
Subject: Re: M-x call with optional arg; call from Elisp
Date: Tue, 28 May 2013 21:52:04 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)

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


reply via email to

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