From 54189d10dd18584a8cf87c8109811c96b4f47386 Mon Sep 17 00:00:00 2001 From: Jan-Erik Rediger Date: Tue, 19 Nov 2024 12:00:06 +0100 Subject: [PATCH] replace textutils with inlined function --- texttools.tcl | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) 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} {