1textutil(n) Text and string utilities, macro processing textutil(n)
2
3
4
5______________________________________________________________________________
6
8 textutil - Procedures to manipulate texts and strings.
9
11 package require Tcl 8.2
12
13 package require textutil ?0.8?
14
15 ::textutil::adjust string args
16
17 ::textutil::adjust::readPatterns filename
18
19 ::textutil::adjust::listPredefined
20
21 ::textutil::adjust::getPredefined filename
22
23 ::textutil::indent string prefix ?skip?
24
25 ::textutil::undent string
26
27 ::textutil::splitn string ?len?
28
29 ::textutil::splitx string ?regexp?
30
31 ::textutil::tabify string ?num?
32
33 ::textutil::tabify2 string ?num?
34
35 ::textutil::trim string ?regexp?
36
37 ::textutil::trimleft string ?regexp?
38
39 ::textutil::trimright string ?regexp?
40
41 ::textutil::trimPrefix string prefix
42
43 ::textutil::trimEmptyHeading string
44
45 ::textutil::untabify string ?num?
46
47 ::textutil::untabify2 string ?num?
48
49 ::textutil::strRepeat text num
50
51 ::textutil::blank num
52
53 ::textutil::chop string
54
55 ::textutil::tail string
56
57 ::textutil::cap string
58
59 ::textutil::uncap string
60
61 ::textutil::longestCommonPrefixList list
62
63 ::textutil::longestCommonPrefix ?string...?
64
65______________________________________________________________________________
66
68 The package textutil provides commands that manipulate strings or texts
69 (a.k.a. long strings or string with embedded newlines or paragraphs).
70 It is actually a bundle providing the commands of the six packages
71
72 textutil::adjust
73
74 textutil::repeat
75
76 textutil::split
77
78 textutil::string
79
80 textutil::tabify
81
82 textutil::trim
83
84 in the namespace textutil.
85
86 The bundle is deprecated, and it will be removed in a future release of
87 Tcllib, after the next release. It is recommended to use the relevant
88 sub packages instead for whatever functionality is needed by the using
89 package or application.
90
91 The complete set of procedures is described below.
92
93 ::textutil::adjust string args
94 Do a justification on the string according to args. The string
95 is taken as one big paragraph, ignoring any newlines. Then the
96 line is formatted according to the options used, and the command
97 return a new string with enough lines to contain all the print‐
98 able chars in the input string. A line is a set of chars between
99 the beginning of the string and a newline, or between 2 new‐
100 lines, or between a newline and the end of the string. If the
101 input string is small enough, the returned string won't contain
102 any newlines.
103
104 Together with ::textutil::indent it is possible to create prop‐
105 erly wrapped paragraphs with arbitrary indentations.
106
107 By default, any occurrence of spaces characters or tabulation
108 are replaced by a single space so each word in a line is sepa‐
109 rated from the next one by exactly one space char, and this
110 forms a real line. Each real line is placed in a logical line,
111 which have exactly a given length (see -length option below).
112 The real line may have a lesser length. Again by default, any
113 trailing spaces are ignored before returning the string (see
114 -full option below). The following options may be used after the
115 string parameter, and change the way the command place a real
116 line in a logical line.
117
118 -full boolean
119 If set to false, any trailing space chars are deleted
120 before returning the string. If set to true, any trailing
121 space chars are left in the string. Default to false.
122
123 -hyphenate boolean
124 if set to false, no hyphenation will be done. If set to
125 true, the last word of a line is tried to be hyphenated.
126 Defaults to false. Note: hyphenation patterns must be
127 loaded prior, using the command ::textutil::adjust::read‐
128 Patterns.
129
130 -justify center|left|plain|right
131 Set the justification of the returned string to center,
132 left, plain or right. By default, it is set to left. The
133 justification means that any line in the returned string
134 but the last one is build according to the value. If the
135 justification is set to plain and the number of printable
136 chars in the last line is less than 90% of the length of
137 a line (see -length), then this line is justified with
138 the left value, avoiding the expansion of this line when
139 it is too small. The meaning of each value is:
140
141 center The real line is centered in the logical line. If
142 needed, a set of space characters are added at the
143 beginning (half of the needed set) and at the end
144 (half of the needed set) of the line if required
145 (see the option -full).
146
147 left The real line is set on the left of the logical
148 line. It means that there are no space chars at
149 the beginning of this line. If required, all
150 needed space chars are added at the end of the
151 line (see the option -full).
152
153 plain The real line is exactly set in the logical line.
154 It means that there are no leading or trailing
155 space chars. All the needed space chars are added
156 in the real line, between 2 (or more) words.
157
158 right The real line is set on the right of the logical
159 line. It means that there are no space chars at
160 the end of this line, and there may be some space
161 chars at the beginning, despite of the -full
162 option.
163
164 -length integer
165 Set the length of the logical line in the string to inte‐
166 ger. integer must be a positive integer value. Defaults
167 to 72.
168
169 -strictlength boolean
170 If set to false, a line can exceed the specified -length
171 if a single word is longer than -length. If set to true,
172 words that are longer than -length are split so that no
173 line exceeds the specified -length. Defaults to false.
174
175 ::textutil::adjust::readPatterns filename
176 Loads the internal storage for hyphenation patterns with the
177 contents of the file filename. This has to be done prior to
178 calling command ::textutil::adjust with "-hyphenate true", or
179 the hyphenation process will not work correctly.
180
181 The package comes with a number of predefined pattern files, and
182 the command ::textutil::adjust::listPredefined can be used to
183 find out their names.
184
185 ::textutil::adjust::listPredefined
186 This command returns a list containing the names of the hyphen‐
187 ation files coming with this package.
188
189 ::textutil::adjust::getPredefined filename
190 Use this command to query the package for the full path name of
191 the hyphenation file filename coming with the package. Only the
192 filenames found in the list returned by ::textu‐
193 til::adjust::listPredefined are legal arguments for this com‐
194 mand.
195
196 ::textutil::indent string prefix ?skip?
197 Each line in the string indented by adding the string prefix at
198 its beginning. The modified string is returned as the result of
199 the command.
200
201 If skip is specified the first skip lines are left untouched.
202 The default for skip is 0, causing the modification of all
203 lines. Negative values for skip are treated like 0. In other
204 words, skip > 0 creates a hanging indentation.
205
206 Together with ::textutil::adjust it is possible to create prop‐
207 erly wrapped paragraphs with arbitrary indentations.
208
209 ::textutil::undent string
210 The command computes the common prefix for all lines in string
211 consisting solely out of whitespace, removes this from each line
212 and returns the modified string.
213
214 Lines containing only whitespace are always reduced to com‐
215 pletely empty lines. They and empty lines are also ignored when
216 computing the prefix to remove.
217
218 Together with ::textutil::adjust it is possible to create prop‐
219 erly wrapped paragraphs with arbitrary indentations.
220
221 ::textutil::splitn string ?len?
222 This command splits the given string into chunks of len charac‐
223 ters and returns a list containing these chunks. The argument
224 len defaults to 1 if none is specified. A negative length is not
225 allowed and will cause the command to throw an error. Providing
226 an empty string as input is allowed, the command will then
227 return an empty list. If the length of the string is not an
228 entire multiple of the chunk length, then the last chunk in the
229 generated list will be shorter than len.
230
231 ::textutil::splitx string ?regexp?
232 Split the string and return a list. The string is split accord‐
233 ing to the regular expression regexp instead of a simple list of
234 chars. Note that if you add parenthesis into the regexp, the
235 parentheses part of separator would be added into list as addi‐
236 tional element. If the string is empty the result is the empty
237 list, like for split. If regexp is empty the string is split at
238 every character, like split does. The regular expression regexp
239 defaults to "[\\t \\r\\n]+".
240
241 ::textutil::tabify string ?num?
242 Tabify the string by replacing any substring of num space chars
243 by a tabulation and return the result as a new string. num
244 defaults to 8.
245
246 ::textutil::tabify2 string ?num?
247 Similar to ::textutil::tabify this command tabifies the string
248 and returns the result as a new string. A different algorithm is
249 used however. Instead of replacing any substring of num spaces
250 this command works more like an editor. num defaults to 8.
251
252 Each line of the text in string is treated as if there are tab‐
253 stops every num columns. Only sequences of space characters con‐
254 taining more than one space character and found immediately
255 before a tabstop are replaced with tabs.
256
257 ::textutil::trim string ?regexp?
258 Remove in string any leading and trailing substring according to
259 the regular expression regexp and return the result as a new
260 string. This apply on any line in the string, that is any sub‐
261 string between 2 newline chars, or between the beginning of the
262 string and a newline, or between a newline and the end of the
263 string, or, if the string contain no newline, between the begin‐
264 ning and the end of the string. The regular expression regexp
265 defaults to "[ \\t]+".
266
267 ::textutil::trimleft string ?regexp?
268 Remove in string any leading substring according to the regular
269 expression regexp and return the result as a new string. This
270 apply on any line in the string, that is any substring between 2
271 newline chars, or between the beginning of the string and a new‐
272 line, or between a newline and the end of the string, or, if the
273 string contain no newline, between the beginning and the end of
274 the string. The regular expression regexp defaults to "[
275 \\t]+".
276
277 ::textutil::trimright string ?regexp?
278 Remove in string any trailing substring according to the regular
279 expression regexp and return the result as a new string. This
280 apply on any line in the string, that is any substring between 2
281 newline chars, or between the beginning of the string and a new‐
282 line, or between a newline and the end of the string, or, if the
283 string contain no newline, between the beginning and the end of
284 the string. The regular expression regexp defaults to "[
285 \\t]+".
286
287 ::textutil::trimPrefix string prefix
288 Removes the prefix from the beginning of string and returns the
289 result. The string is left unchanged if it doesn't have prefix
290 at its beginning.
291
292 ::textutil::trimEmptyHeading string
293 Looks for empty lines (including lines consisting of only white‐
294 space) at the beginning of the string and removes it. The modi‐
295 fied string is returned as the result of the command.
296
297 ::textutil::untabify string ?num?
298 Untabify the string by replacing any tabulation char by a sub‐
299 string of num space chars and return the result as a new string.
300 num defaults to 8.
301
302 ::textutil::untabify2 string ?num?
303 Untabify the string by replacing any tabulation char by a sub‐
304 string of at most num space chars and return the result as a new
305 string. Unlike textutil::untabify each tab is not replaced by a
306 fixed number of space characters. The command overlays each
307 line in the string with tabstops every num columns instead and
308 replaces tabs with just enough space characters to reach the
309 next tabstop. This is the complement of the actions taken by
310 ::textutil::tabify2. num defaults to 8.
311
312 There is one asymmetry though: A tab can be replaced with a sin‐
313 gle space, but not the other way around.
314
315 ::textutil::strRepeat text num
316 The implementation depends on the core executing the package.
317 Used string repeat if it is present, or a fast tcl implementa‐
318 tion if it is not. Returns a string containing the text repeated
319 num times. The repetitions are joined without characters between
320 them. A value of num <= 0 causes the command to return an empty
321 string.
322
323 ::textutil::blank num
324 A convenience command. Returns a string of num spaces.
325
326 ::textutil::chop string
327 A convenience command. Removes the last character of string and
328 returns the shortened string.
329
330 ::textutil::tail string
331 A convenience command. Removes the first character of string and
332 returns the shortened string.
333
334 ::textutil::cap string
335 Capitalizes the first character of string and returns the modi‐
336 fied string.
337
338 ::textutil::uncap string
339 The complementary operation to ::textutil::cap. Forces the first
340 character of string to lower case and returns the modified
341 string.
342
343 ::textutil::longestCommonPrefixList list
344
345 ::textutil::longestCommonPrefix ?string...?
346 Computes the longest common prefix for either the strings given
347 to the command, or the strings specified in the single list, and
348 returns it as the result of the command.
349
350 If no strings were specified the result is the empty string. If
351 only one string was specified, the string itself is returned, as
352 it is its own longest common prefix.
353
355 This document, and the package it describes, will undoubtedly contain
356 bugs and other problems. Please report such in the category textutil
357 of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. Please
358 also report any ideas for enhancements you may have for either package
359 and/or documentation.
360
361 When proposing code changes, please provide unified diffs, i.e the out‐
362 put of diff -u.
363
364 Note further that attachments are strongly preferred over inlined
365 patches. Attachments can be made by going to the Edit form of the
366 ticket immediately after its creation, and then using the left-most
367 button in the secondary navigation bar.
368
370 regexp(n), split(n), string(n)
371
373 TeX, formatting, hyphenation, indenting, paragraph, regular expression,
374 string, trimming
375
377 Text processing
378
379
380
381tcllib 0.8 textutil(n)