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

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

Surprising behaviour of 'append' with strings


From: R. Diez
Subject: Surprising behaviour of 'append' with strings
Date: Sun, 6 Nov 2022 12:05:07 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2

Hi all:

I want to build a list of command-line arguments to run a program. Each element 
of the list must be a string.

The commands are complex shell invocations with nested shell commands. I have 
been shell-quoting and concatenating plain strings for a long time, but it is 
error prone (in my experience).

So now I am testing a different approach: I am building a list of strings, joining them 
with (string-join my-cmd-args " "), and passing the result to 'compile'.

For nested commands, I use instead (shell-quote-argument (string-join my-sub-cmd-args 
" ")), and then append the resulting string to the parent command's list of 
arguments. And so on.

When building a single command, there are optional, complex lists of arguments 
that get built somewhere else. So the code is building and concatenating many 
lists. This is the idea (very simplified):

(setq cmd-args (list "arg 1" "arg 2"))

(setq whole-cmd (append "cmd-exe" cmd-args))

However, I got into trouble with 'append'. Its documentation states:

"Each argument may be a list, vector or string."

I tested it like this:

(append "1")      -> "1"
(append "1" "2")  -> (49 . "2")

That behaviour is surprising and it does not fit the bill.

I need a routine like this:

(my-append
  "1"
  (list "2" "3")
  "4"
)

The result should be:
 ("1" "2" "3" "4")

As a bonus, 'my-append' should check that all elements are strings, or list of 
strings (no nested lists, and no other data types).

The trouble is, my Lisp skills are rather limited. Can somebody help?

Thanks in advance,
  rdiez


reply via email to

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