sub pp_new { my( $nextBit, $width, $EOS ) = @_; $string .= $nextBit; while( length( $string ) > ($EOS ? 0 : $width) ) { for (substr($string, 0, $width)) { my $pos; my $leftmargin = $indent x $depth; if (my ($bracket) = /^[^,]*?([\Q[{\E])/) { $bracket =~ tr/[{/]}/; if (my $closepos = indexBal($_, $bracket, $width)) { $pos = $closepos + 2; } else { $pos = $+[0]; ++$depth } } else { $pos = 1 + (/,/ ? $+[0] : $width); $leftmargin = $indent x --$depth if substr($_, 0, $pos-1) =~ /([\Q]}\E])/; } print $leftmargin, substr( $_, 0, $pos, '' ); } $string =~ s/^\s+//; } }