0
votes

This tcl question is a bit special. I have text in braces which contain command executions in square brackets:

proc doit {txt} {
    return $txt
}

set txt {
    
    hallo [doit world]
    
}

puts [subst -novariables -nobackslashes $txt]

The output is hallo world which is what I want.

However, sometimes my editor formats the text such that the command execution is split over multiple lines:

set txt {
    
    hallo [doit
    world]
    
}

which leads to an error message wrong # args: should be "doit txt".

Is there an elegant way to avoid this problem? Introducing a backslash line extension doesn't work since my editor may put this in the wrong position after reformatting the text. And I want to keep the line breaks unchanged (otherwise I could just remove all line breaks from the txt before using subst). Parsing the text and removing line breaks from the command execution seems to be a bit complex.

Thanks for your help!

1
What's your editor? If it's vim, use :set textwidth=0 or :set tw=0 - glenn jackman
@glenn jackman: I'm using Gnu Emacs in the Tcl mode. Formatting of paragraphs (inside the braces) is done with ALT-Q. This breaks the paragraph at arbitrary spaces, tabs, or newlines. - Ralf
I've never had emacs mangle tcl like that... - Shawn
@Shawn: The rest of the Tcl formatting seems ok, maybe its a bit too special for emacs. The lines are of course longer than in my example. Would you expect emacs to avoid splitting the command execution? - Ralf
Well... yes? I don't recall it ever doing that for my code. - Shawn

1 Answers

2
votes

One trick is to wrap your script text to become subjected to subst by curly braces and have the Tcl parser unwrap it before command substitution using the {*} expand operator:

set txt {
    
    hallo [{*}{doit
       
         world}]
    
}

This will be robust against reformatting. However, I would rather teach your editor to behave gently.