1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
# text_write.tcl --
#
# Commands for the generation of TEXT
#
# Copyright (c) 2009 Andreas Kupries <andreas_kupries@sourceforge.net>
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: text_write.tcl,v 1.1 2010/03/26 05:07:24 andreas_kupries Exp $
# ### ### ### ######### ######### #########
## Requisites
package require Tcl 8.5 9
package require textutil::adjust
namespace eval ::text::write {
namespace export \
reset clear field fieldl fieldr /line prefix indent \
store recall undef undo get getl maxlen fieldsep \
push pop pop-append copy move clear-block exists
namespace ensemble create
}
# ### ### ### ######### ######### #########
## API.
proc ::text::write::reset {} {
# Reset state, fully (clear line and block buffers, , stack, set
# the default field separator, and flush the named blocks)
variable currentline {}
variable currentblock {}
variable stack {}
variable fieldseparator { }
variable blocks
array unset blocks *
return
}
proc ::text::write::clear {} {
# Reset state (clear line and block buffers, stack, and set the
# default field separator)
variable currentline {}
variable currentblock {}
variable stack {}
variable fieldseparator { }
return
}
proc ::text::write::field {args} {
# Extend line buffer, at end.
variable currentline
lappend currentline {*}$args
return
}
proc ::text::write::fieldl {fieldlength text} {
# As field, but a text left-aligned in a field of given length.
field [format %-${fieldlength}s $text]
return
}
proc ::text::write::fieldr {fieldlength text} {
# As field, but a text right-aligned in a field of given length.
field [format %${fieldlength}s $text]
return
}
proc ::text::write::fieldsep {char} {
# Set field separator for '/line'
variable fieldseparator $char
return
}
proc ::text::write::get {} {
# Return text of current block.
variable currentblock
set res $currentblock
reset
return [join $res \n]
}
proc ::text::write::getl {} {
# As get, but retrieve the raw list of lines.
variable currentblock
set res $currentblock
reset
return $res
}
proc ::text::write::/line {} {
# Commit current line to current block (added at end)
variable currentline
variable currentblock
variable fieldseparator
lappend currentblock [string trimright [join $currentline $fieldseparator]]
set currentline {}
return
}
proc ::text::write::undo {} {
# Remove last line from current block.
variable currentblock
set currentblock [lreplace $currentblock end end]
return
}
proc ::text::write::prefix {prefix {n 0}} {
# Indent current block using the prefix text, skipping the first n lines
variable currentblock
set currentblock \
[split \
[textutil::adjust::indent \
[join $currentblock \n] \
$prefix $n] \
\n]
return
}
proc ::text::write::indent {k {n 0}} {
# Indent current block by k spaces, skipping the first n lines
variable currentblock
set currentblock \
[split \
[textutil::adjust::indent \
[join $currentblock \n] \
[string repeat { } $k] $n] \
\n]
return
}
proc ::text::write::store {name} {
# Save current block and under a name. /store
variable currentblock
variable blocks
set blocks($name) $currentblock
return
}
proc ::text::write::recall {name} {
# Append named block to current block. /recall
variable currentblock
variable blocks
lappend currentblock {*}$blocks($name)
return
}
proc ::text::write::undef {name} {
# Remove the specified block from memory
variable blocks
unset blocks($name)
return
}
proc ::text::write::exists {name} {
# Remove the specified block from memory
variable blocks
return [info exists blocks($name)]
}
proc ::text::write::copy {src dst} {
# Copy named block to other named block, overwriting it.
variable blocks
set blocks($dst) $blocks($src)
return
}
proc ::text::write::clear-block {name} {
# Clear the named block.
variable blocks
set blocks($name) ""
return
}
proc ::text::write::move {src dst} {
# Move named block to other named block, overwriting it.
variable blocks
set blocks($dst) $blocks($src)
unset blocks($src)
return
}
proc ::text::write::push {} {
# Suspend current block.
variable currentblock
variable stack
lappend stack $currentblock
return
}
proc ::text::write::pop {} {
# Recall the last suspended block, replace current block.
variable currentblock
variable stack
set currentblock [lindex $stack end]
set stack [lrange $stack 0 end-1]
return
}
proc ::text::write::pop-append {} {
# Recall the last suspended block, add to the current block.
variable currentblock
variable stack
lappend currentblock {*}[lindex $stack end]
set stack [lrange $stack 0 end-1]
return
}
proc ::text::write::maxlen {list} {
# Find the max length of the strings in the list.
set lengths 0 ; # This will be the max if the list is empty, and
# prevents the mathfunc from throwing errors for
# that case.
foreach str $list {
lappend lengths [::string length $str]
}
return [tcl::mathfunc::max {*}$lengths]
}
# ### ### ### ######### ######### #########
## Internals.
# ### ### ### ######### ######### #########
namespace eval ::text::write {
# State of the writer.
variable currentline {} ; # List of text fragments which make
# up the current line.
variable currentblock {} ; # List of lines which make up the
# current block.
variable blocks ; # Set of named blocks.
array set blocks {} ; #
variable fieldseparator { } ; # Current field separator.
variable stack {} ; # Stack of suspended blocks.
}
# ### ### ### ######### ######### #########
## Ready
package provide text::write 1.1
return
|