author | Claus Gittinger <cg@exept.de> |
Thu, 09 Jun 2016 12:30:05 +0200 | |
changeset 3886 | b4fe47975cce |
parent 3833 | bd6ce7a78e48 |
child 4233 | 96704b0b6c5b |
permissions | -rw-r--r-- |
340 | 1 |
" |
2 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
1002
ff7c1f0de146
code now independent of actual zeroPosition
Claus Gittinger <cg@exept.de>
parents:
873
diff
changeset
|
12 |
"{ Package: 'stx:libbasic2' }" |
ff7c1f0de146
code now independent of actual zeroPosition
Claus Gittinger <cg@exept.de>
parents:
873
diff
changeset
|
13 |
|
3480 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1941
3f0b0b5df9ab
Inherit from CharacterWriteStream, in order to support Unicode
Stefan Vogel <sv@exept.de>
parents:
1899
diff
changeset
|
16 |
CharacterWriteStream subclass:#TextStream |
340 | 17 |
instanceVariableNames:'runs currentEmphasis' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Streams' |
|
21 |
! |
|
22 |
||
23 |
!TextStream class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
||
38 |
! |
|
39 |
||
40 |
documentation |
|
41 |
" |
|
562 | 42 |
a textStream is much like a regular writeStream; |
340 | 43 |
however, in addition to collecting characters, it keeps |
44 |
track of any change of the emphasis, and returns a Text instance |
|
45 |
as its contents (in contrast to a String instance). |
|
3480 | 46 |
Used to collect attributed text. |
340 | 47 |
|
48 |
[author:] |
|
49 |
Claus Gittinger |
|
50 |
||
51 |
[see also:] |
|
52 |
WriteStream |
|
53 |
Text String |
|
54 |
" |
|
55 |
! |
|
56 |
||
57 |
examples |
|
58 |
" |
|
59 |
[exBegin] |
|
60 |
|s| |
|
61 |
||
62 |
s := TextStream on:''. |
|
63 |
s emphasis:#italic; |
|
64 |
nextPutAll:'hello'; |
|
65 |
emphasis:nil; |
|
66 |
space; |
|
67 |
emphasis:#bold; |
|
1941
3f0b0b5df9ab
Inherit from CharacterWriteStream, in order to support Unicode
Stefan Vogel <sv@exept.de>
parents:
1899
diff
changeset
|
68 |
nextPutAll:'world '; |
3f0b0b5df9ab
Inherit from CharacterWriteStream, in order to support Unicode
Stefan Vogel <sv@exept.de>
parents:
1899
diff
changeset
|
69 |
nextPut:(Character codePoint:16r3C7). |
340 | 70 |
s contents inspect |
71 |
[exEnd] |
|
72 |
||
73 |
[exBegin] |
|
74 |
|s| |
|
75 |
||
76 |
s := TextStream on:''. |
|
77 |
s emphasis:#italic; |
|
78 |
nextPutAll:'hello'; |
|
79 |
emphasis:nil; |
|
80 |
space; |
|
81 |
emphasis:#bold; |
|
82 |
nextPutAll:'world'. |
|
83 |
||
562 | 84 |
Transcript nextPutAll:(s contents) |
85 |
[exEnd] |
|
86 |
||
87 |
[exBegin] |
|
88 |
|s| |
|
89 |
||
90 |
s := TextStream on:''. |
|
91 |
s emphasis:#italic; |
|
92 |
nextPutAll:'hello'; |
|
93 |
emphasis:nil; |
|
94 |
space; |
|
95 |
emphasis:#bold; |
|
96 |
nextPutAll:'world'. |
|
97 |
||
340 | 98 |
Dialog |
99 |
warn:(s contents) |
|
100 |
[exEnd] |
|
101 |
||
102 |
[exBegin] |
|
103 |
|s1 s2 flipFlop| |
|
104 |
||
105 |
s1 := PipeStream readingFrom:'ls -l'. |
|
106 |
s2 := TextStream on:''. |
|
107 |
||
108 |
flipFlop := true. |
|
109 |
[s1 atEnd] whileFalse:[ |
|
110 |
flipFlop ifTrue:[ |
|
111 |
s2 emphasis:(#color->Color red) |
|
112 |
] ifFalse:[ |
|
113 |
s2 emphasis:nil |
|
114 |
]. |
|
115 |
flipFlop := flipFlop not. |
|
116 |
s2 nextPutAll:(s1 nextLine). |
|
117 |
s2 cr. |
|
118 |
]. |
|
119 |
s1 close. |
|
120 |
||
121 |
(EditTextView new contents:s2 contents) open |
|
122 |
[exEnd] |
|
123 |
" |
|
124 |
! ! |
|
125 |
||
126 |
!TextStream methodsFor:'accessing'! |
|
127 |
||
128 |
contents |
|
129 |
"return the streams collected contents" |
|
130 |
||
2525 | 131 |
|stringContents| |
132 |
||
133 |
stringContents := super contents. |
|
134 |
||
1943
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
135 |
(currentEmphasis isNil and:[runs isEmptyOrNil]) ifTrue:[ |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
136 |
"no emphasis information: answer a string" |
2525 | 137 |
^ stringContents |
1943
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
138 |
]. |
340 | 139 |
self closeRun. |
2525 | 140 |
^ Text string:stringContents emphasisCollection:runs |
340 | 141 |
|
142 |
" |
|
1943
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
143 |
Answer a Text containing unicode characters: |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
144 |
(TextStream on:'') |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
145 |
emphasis:#italic; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
146 |
nextPutAll:'hello'; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
147 |
emphasis:nil; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
148 |
space; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
149 |
emphasis:#bold; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
150 |
nextPutAll:'world'; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
151 |
space; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
152 |
nextPut:Character euro; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
153 |
contents. |
340 | 154 |
|
1943
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
155 |
Answer a String: |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
156 |
(TextStream on:'') |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
157 |
nextPutAll:'hello'; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
158 |
contents. |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
159 |
|
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
160 |
Answer a Text: |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
161 |
(TextStream on:Text new) |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
162 |
nextPutAll:'hello'; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
163 |
contents. |
340 | 164 |
" |
2525 | 165 |
|
166 |
"Modified: / 28-01-2011 / 13:46:48 / cg" |
|
340 | 167 |
! |
168 |
||
1159 | 169 |
emphasis |
170 |
"return the current emphasis" |
|
171 |
||
172 |
^ currentEmphasis |
|
173 |
! |
|
174 |
||
663 | 175 |
emphasis:newEmphasis |
343 | 176 |
"change the emphasis; all followup elements are appended with |
177 |
that emphasis in effect" |
|
178 |
||
3001 | 179 |
position ~~ 0 ifTrue:[ |
1899 | 180 |
currentEmphasis ~= newEmphasis ifTrue:[ |
663 | 181 |
self closeRun. |
182 |
] |
|
340 | 183 |
]. |
663 | 184 |
currentEmphasis := newEmphasis |
340 | 185 |
|
186 |
" |
|
187 |
|s| |
|
188 |
||
189 |
s := TextStream on:String new. |
|
343 | 190 |
s emphasis:#italic; |
191 |
nextPutAll:'hello'; |
|
192 |
emphasis:nil; |
|
193 |
space; |
|
194 |
emphasis:#bold; |
|
195 |
nextPutAll:'world'. |
|
1899 | 196 |
s contents. |
2537
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
197 |
Transcript showCR:s contents. |
340 | 198 |
" |
663 | 199 |
|
2537
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
200 |
" |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
201 |
|s| |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
202 |
|
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
203 |
s := TextStream on:String new. |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
204 |
s emphasis:#italic; |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
205 |
nextPutAll:'hello'; |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
206 |
emphasis:nil; |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
207 |
space; |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
208 |
emphasis:{#bold. #color->Color red}; |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
209 |
nextPutAll:'world'. |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
210 |
s contents. |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
211 |
Transcript showCR:s contents. |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
212 |
" |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
213 |
|
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
214 |
"Modified: / 17-02-2011 / 14:13:07 / cg" |
873
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
215 |
! |
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
216 |
|
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
217 |
stringContents |
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
218 |
"return the streams collected string contents" |
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
219 |
|
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
220 |
^ super contents |
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
221 |
|
340 | 222 |
! ! |
223 |
||
224 |
!TextStream methodsFor:'private'! |
|
225 |
||
226 |
closeRun |
|
3001 | 227 |
position ~~ 0 ifTrue:[ |
340 | 228 |
runs isNil ifTrue:[ |
3001 | 229 |
runs := RunArray new:position withAll:currentEmphasis. |
340 | 230 |
] ifFalse:[ |
3001 | 231 |
runs add:currentEmphasis withOccurrences:(position-runs size) |
340 | 232 |
] |
233 |
] |
|
234 |
! ! |
|
235 |
||
2053 | 236 |
!TextStream methodsFor:'writing'! |
237 |
||
238 |
nextPutAllText:aText |
|
3833 | 239 |
"write some text to the stream and keep the emphasis |
240 |
(notice: |
|
241 |
nextPutAll: ignores the argument's emphasis, |
|
242 |
and instead writes with the current emphasis. |
|
243 |
In contrast, this ignores the current emphasis, and writes with |
|
244 |
the argument's emphasis." |
|
2053 | 245 |
|
246 |
|string pos oldEmphasis| |
|
247 |
||
248 |
string := aText string. |
|
249 |
pos := 1. |
|
250 |
||
251 |
oldEmphasis := currentEmphasis. |
|
252 |
aText emphasis runsDo:[:len :emphasis | |
|
253 |
|nextPos| |
|
254 |
||
255 |
nextPos := pos + len. |
|
256 |
||
257 |
self emphasis:emphasis. |
|
258 |
self nextPutAll:string startingAt:pos to:nextPos - 1. |
|
259 |
pos := nextPos |
|
260 |
]. |
|
261 |
self emphasis:oldEmphasis |
|
3833 | 262 |
|
263 |
" |
|
264 |
|t| |
|
265 |
||
266 |
t := TextStream new. |
|
267 |
t nextPutAll:'abc'. |
|
268 |
t nextPutAll:('def' allBold). |
|
269 |
t nextPutAllText:('ghi' allBold). |
|
270 |
t nextPutAllText:'jkl'. |
|
271 |
t contents. |
|
272 |
self assert:(t contents sameStringAndEmphasisAs:('abcdef','ghi' allBold,'jkl')). |
|
273 |
" |
|
2053 | 274 |
! ! |
275 |
||
340 | 276 |
!TextStream class methodsFor:'documentation'! |
277 |
||
278 |
version |
|
3833 | 279 |
^ '$Header$' |
2525 | 280 |
! |
281 |
||
282 |
version_CVS |
|
3833 | 283 |
^ '$Header$' |
340 | 284 |
! ! |
3001 | 285 |