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

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

Re: Split string into shell words?


From: Kevin Rodgers
Subject: Re: Split string into shell words?
Date: Tue, 20 Jan 2004 16:19:48 -0700
User-agent: Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:0.9.4.1) Gecko/20020406 Netscape6/6.2.2

Kai Grossjohann wrote:

Given a string as in the following line:

        foo "a b" 'c d' e\ f bar

I'd like a function that returns the following list:

        ("foo" "a b" "c d" "e f" "bar")

That is, the string should be split into words like a shell would
split it into words.


As Stefan points out, this is hard because of things like command substitution.


I thought there must be a function in comint*.el or shell*.el
somewhere, but couldn't find it.  Probably I'm blind.

Shell Mode's completion mechanism seems to just ignore quoting.  E.g.

if you've typed

        cat /dev/nul""

on the command line, TAB doesn't complete it to

        cat /dev/nul""l


and it reports "No completions of /dev/nul\" for both

        cat /dev/nul\
and
        cat /dev/nul\\

Here's what I tried in the *scratch* buffer for your example, to see if the
Emacs Lisp reader could do most of the work:

(setq string "foo \"a b\" 'c d' e\\ f bar")
"foo \"a b\" 'c d' e\\ f bar"

;; Everything looks good:
(read-from-string string)
(foo . 3)

(read-from-string string 3)
("a b" . 9)

;; Until this:
(read-from-string string 9)
((quote c) . 12)

(read-from-string string 12)
(d . 14)

;; And now things are OK again:
(read-from-string string 14)
((quote e\ f) . 20)

(read-from-string string 20)
(bar . 24)

It might be nice if the 'form -> (quote form) implementation in
src/lread.c:read1() could be customized (e.g. by temporarily making the
single quote behave like a double quote), but Emacs Lisp doesn't have
the notion of reader macros and its semantics are hard-coded:

    case '\'':
      {
        return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
      }

--
Kevin Rodgers



reply via email to

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