summaryrefslogtreecommitdiffstats
path: root/tcllib/modules/doctools2base/text.tcl
blob: 2d3af9b504224b087640b16d4550623ffc7ccc4a (plain)
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
# -*- tcl -*-
# Copyright (c) 2009 Andreas Kupries <andreas_kupries@sourceforge.net>

# Support package. Basic text generation commands.

# # ## ### ##### ######## ############# #####################
## Requirements

package require Tcl 8.4 ; # Required Core

namespace eval ::doctools::text {}

# # ## ### ##### ######## ############# #####################

proc ::doctools::text::begin {} {
    variable state
    array unset state *
    array set   state {
	stack     {}
	buffer    {}
	prefix    {}
	pstack    {}
	underl    {}
	break     0
	newlines  1
	indenting 1
    }
    return
}

proc ::doctools::text::done {} {
    variable state
    return $state(buffer)
}

proc ::doctools::text::save {} {
    variable state
    set current [array get state]
    begin
    set state(stack) $current
    return
}

proc ::doctools::text::restore {} {
    variable state
    set text [done]
    array set state $state(stack)
    return $text
}

proc ::doctools::text::collect {script} {
    save
    uplevel 1 $script
    return [restore]
}

# # ## ### ##### ######## ############# #####################

proc ::doctools::text::+ {text} {
    variable state
    if {$state(break)} {
	+++ [string repeat \n $state(break)]
	+++ $state(prefix)
	set state(break) 0
    }
    +++ $text
    set state(underl) [string length $text]
    return
}

proc ::doctools::text::underline {char} {
    variable state
    newline
    + [string repeat [string index $char 0] $state(underl)]
    newline
    return
}

proc ::doctools::text::+++ {text} {
    variable state
    append   state(buffer) $text
    return
}

# # ## ### ##### ######## ############# #####################

proc ::doctools::text::newline {{increment 1}} {
    variable state
    if {!$state(newlines)} { return 0 }
    incr state(break) $increment
    return 1
}

proc ::doctools::text::newline? {} {
    variable state
    if {!$state(newlines)} { return 0 }
    if {$state(break)} { return 1 }
    if {![string length $state(buffer)]} { return 1 }
    if {[string index   $state(buffer) end] eq "\n"} { return 1 }
    incr state(break)
    return 1
}

# # ## ### ##### ######## ############# #####################

proc ::doctools::text::prefix {text} {
    variable state
    if {!$state(indenting)} return
    set state(prefix) $text
    return
}

proc ::doctools::text::indent {{increment 2}} {
    variable state
    if {!$state(indenting)} return
    lappend state(pstack) $state(prefix)
    set     state(prefix) [string repeat { } $increment]$state(prefix)
    return
}

proc ::doctools::text::dedent {} {
    variable state
    if {!$state(indenting)} return
    set state(prefix) [lindex   $state(pstack) end]
    set state(pstack) [lreplace $state(pstack) end end]
    return
}

proc ::doctools::text::indented {increment script} {
    indent $increment
    uplevel 1 $script
    dedent
    return
}

# # ## ### ##### ######## ############# #####################

proc ::doctools::text::indenting {enable} {
    variable state
    set state(indenting) $enable
    return
}

proc ::doctools::text::newlines {enable} {
    variable state
    set state(newlines) $enable
    return
}

# # ## ### ##### ######## ############# #####################

proc ::doctools::text::field {wvar elements {index {}}} {
    upvar 1 $wvar width
    set width 0
    #puts @!$width
    if {$index ne {}} {
	foreach e $elements {
	    #puts stdout @/$e
	    set e [lindex $e $index]
	    #puts stdout @^$e
	    set l [string length $e]
	    if {$l <= $width} continue
	    set width $l
	}
    } else {
	foreach e $elements {
	    #puts stdout @/$e
	    set l [string length $e]
	    if {$l <= $width} continue
	    set width $l
	}
    }
    #puts stdout @=$width
    return
}

proc ::doctools::text::right {wvar str} {
    upvar $wvar width
    return [format %${width}s $str]
}

proc ::doctools::text::left {wvar str} {
    upvar $wvar width
    return [format %-${width}s $str]
}

# # ## ### ##### ######## ############# #####################

proc ::doctools::text::import {{namespace {}}} {
    uplevel 1 [list namespace eval ${namespace}::text {
	namespace import ::doctools::text::*
    }]
    return
}

proc ::doctools::text::importhere {{namespace ::}} {
    uplevel 1 [list namespace eval ${namespace} {
	namespace import ::doctools::text::*
    }]
    return
}

# # ## ### ##### ######## ############# #####################

namespace eval ::doctools::text {
    variable  state
    array set state {}

    namespace export begin done save restore collect + underline +++ \
	prefix indent dedent indented indenting newline newlines \
	field right left newline?
}

# # ## ### ##### ######## ############# #####################
package provide doctools::text 0.1
return