diff --git a/texttools.tcl b/texttools.tcl index cf9e71f..a1e7e33 100755 --- a/texttools.tcl +++ b/texttools.tcl @@ -1,9 +1,42 @@ #!/usr/bin/env wish -package require textutil - set active_transform 0 +# Remove empty items at the beginning and the end of a list. +proc ltrim {list} { + set first [lsearch -not -exact $list {}] + set last [lsearch -not -exact [lreverse $list] {}] + return [ + if {$first == -1} { + list + } else { + lrange $list $first end-$last + } + ] +} + +# Trim indentation in multiline quoted text. +proc undent {msg {whitespaceChars " "}} { + set msgLines [split $msg "\n"] + set maxLength [string length $msg] + + set regExp [subst -nocommands {([$whitespaceChars]*)[^$whitespaceChars]}] + + set indent [ + tcl::mathfunc::min {*}[ + lmap x $msgLines { + if {[regexp $regExp $x match whitespace]} { + string length $whitespace + } else { + lindex $maxLength + } + } + ] + ] + + join [ltrim [lmap x $msgLines {string range $x $indent end}]] "\n" +} + proc oneline {text} { return [regsub -all \n+ $text " "] } @@ -20,7 +53,7 @@ proc wordcount {text} { } proc dedent {text} { - return [textutil::undent $text] + return [undent $text] } proc markdown_quote {text} {