Ralf
Ralf

Reputation: 1305

Tcl: tolerate line break in command execution

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!

Upvotes: 0

Views: 357

Answers (1)

mrcalvin
mrcalvin

Reputation: 3434

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.

Upvotes: 3

Related Questions