On Wed, Sep 21, 2011 at 01:05:07AM +0200, Jan Kanis wrote: > On Mon, Sep 19, 2011 at 17:06, Robert Shinka <[1]k...@unknownlayer.net> > wrote: > <elided> > I'm only proposing changing the semantics of "$var", while $var would be > unchanged, so the common case of (echo $var) would format as usual, > separated with spaces, but (echo "$var") would print the original > string, including any embedded newlines. > > eg., > > set var (echo 1\n2\n3) > > echo $var # echo '1' '2' '3' > 1 2 3 > > echo "$var" # echo '1\n2\n3' > 1 > 2 > 3 > > Is there a compelling reason not to make this change? > > Yes. > First of all, the internal representation of variables would have to be > changed, because they don't store the whitespace that separated the array > elements/arguments. Fish splits the result of command substitutions on \n, > but > in other cases it uses other whitespace:
If, by "other cases", you mean the 'set' command, then yes. > > set var a b c d (echo e\ \ f\ng) > # a<space>b<three spaces>c<tab>d<space>e<two > space>f<newline>g > > echo $var # 'a' 'b' 'c' 'd' 'e f' 'g' > a b c d e f g > > echo $var[4] > d > > echo $var[5] > e f In both fish-1.23.1 and the latest git, it does not work that way. > set var a b c d (echo e\ \ f\ng) > set | grep \^var var 'a' 'b ' 'c' 'd' 'e f' 'g' > echo $var a b c d e f g The mechanics of 'set' are both simple and hideous: fish first splits the output of any subcommands by newlines, so the command becomes... set var a b c d 'e f' 'g' The whitespace is then tokenized in an odd manner, where a single whitespace character is stripped delimiting each token, leaving us... set var 'a' 'b ' 'c' 'd' 'e f' 'g' Note that two of the spaces following 'b' were kept. Why? I don't know. This is then passed to builtin_set, which parses any options and stores the elements verbatim if it's an ordinary assignment (which this is). > If 'echo "$var"' as you propose is to work on this example, the internal > representation of 'var' will need to remember what whitespace was used where. > But this is merely a programming problem. > > The real problem is that the 'set' command itself doesn't have access to that > whitespace. 'set' is just a regular command, it is not special syntax such as > 'for' etc. It just receives a list of arguments in it's $argv (or the builtin > command's equivalent) and works with those, just like any other > builtin/function/external command. If 'set' is to remember the whitespace, it > will need to become a special case (and the only one at that) for fish's > generic argument-parsing code. It would also be a very non-obvious and non > composable special case: a function like "function myset; set $argv; end" > wouldn't work. > > There's also no benefit to doing this: fish already has a way to store > whitespace in variables, but you need to indicate what you want when > assigning > the variable instead of when using the variable like in your proposal. Just > do > > set var "a b c d" > The problem is that there's no way to handle command substitutions in this > form. > <elided> It's not necessary to keep track of that whitespace, and I would rather fish didn't (including in the example I gave above, which is just odd). What I expect 'set' to do is store each element passed to it as a separate element in an array: the same way it does now, except for that oddity in argument parsing (which should probably be fixed, but I'm uncertain of the reason for it). Then, when expanding that array, offer two expansion forms: $var, which passes each element as a separate argument the way it does now, and "$var", which formats the contents into a single string, with each element separated from the next with a single \n character. In the case of output from a command, this could also be optimized to store the string directly, if desired, for a significant performance boost. Your 'myset' function would also work as expected given this behavior. ------------------------------------------------------------------------------ All the data continuously generated in your IT infrastructure contains a definitive record of customers, application performance, security threats, fraudulent activity and more. Splunk takes this data and makes sense of it. Business sense. IT sense. Common sense. http://p.sf.net/sfu/splunk-d2dcopy1 _______________________________________________ Fish-users mailing list Fish-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/fish-users