File: EditStream.st

package info (click to toggle)
gnu-smalltalk 3.2.4-2.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,688 kB
  • ctags: 14,104
  • sloc: ansic: 87,424; sh: 22,729; asm: 8,465; perl: 4,513; cpp: 3,548; xml: 1,669; awk: 1,581; yacc: 1,357; makefile: 1,237; lisp: 855; lex: 843; sed: 258; objc: 124
file content (222 lines) | stat: -rw-r--r-- 5,410 bytes parent folder | download | duplicates (6)
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
"======================================================================
|
|   `Splitting' stream, useful for editors and the like.
|
|
 ======================================================================"


"======================================================================
|
| Written by Ulf Dambacher.
|
| This file is part of GNU Smalltalk.
|
| GNU Smalltalk is free software; you can redistribute it and/or modify it
| under the terms of the GNU General Public License as published by the Free
| Software Foundation; either version 2, or (at your option) any later version.
| 
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
| FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
| details.
| 
| You should have received a copy of the GNU General Public License along with
| GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
| Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  
|
 ======================================================================"


Stream subclass: #EditorStream
       instanceVariableNames: 'head headEnd tail tailStart overwrite'
       classVariableNames: ''
       poolDictionaries: ''
       category: 'Examples-Useful tools'
!

" headEnd points behind the last valid member of head.
  tailStart points to the first valid member of tail!
"

!EditorStream class methodsFor: 'all'!
on: aString
   ^self new initOn: aString
! !

!EditorStream methodsFor: 'initializing'!

initOn: aColl
    tail _ aColl.
    head _ aColl species new: 10.
    headEnd _ tailStart _ 1.
    overwrite _ false.
!
insert
    overwrite _ false.
!
overwrite
    overwrite _ true.
! !

!EditorStream methodsFor: 'positioning'!

position
    ^headEnd
!

position: anInt
    anInt < headEnd
	ifTrue: [ self copyHeadToTail: (headEnd - anInt) ]
	ifFalse:[ self copyTailToHead: (anInt - headEnd)]
!

skip: anInt
    anInt < 0
	ifTrue: [ self copyHeadToTail: anInt negated ]
	ifFalse:[ self copyTailToHead: anInt ]
!

toEnd
    self copyTailToHead: tail size - tailStart + 1.
!

toPos1
    self copyHeadToTail: headEnd.
! !

!EditorStream methodsFor: 'access'!
previous
    self skip: -1.
    ^ self peek
!
next
    ^ (self next:1) at: 1.
!

next: anInt
    | old |
    self atEnd ifTrue:  [ self error: 'no object to next!'].
    old _ headEnd.
    self skip: anInt.
    ^ head copyFrom: old to: headEnd - 1
!

nextPut: anObject
    headEnd > head size ifTrue: [head grow].
    head at: headEnd put: anObject.
    headEnd _ headEnd + 1.
    overwrite ifTrue: [ tailStart  _ (tailStart min: tail size) + 1].
!

remove: anInt
    anInt < 0
	ifTrue: [ headEnd _ headEnd + anInt max: 1 ]
	ifFalse: [ tailStart _ tailStart + anInt min: tail size +1 ].
!

peek
    self atEnd ifTrue: [self error: 'no object to peek!'].
    ^tail at: tailStart
! !

!EditorStream methodsFor: 'testing'!

atEnd
    ^ tailStart > tail size
! !

!EditorStream methodsFor: 'contents access'!

head
    ^ head copyFrom: 1 to: headEnd - 1
!

tail
    ^ tail copyFrom: tailStart to: tail size
!

contents
    ^ self head , self tail
!

size
"    ^self contents size"
    ^ headEnd "- 1 " + tail size - tailStart " + 1 ".
! !

!EditorStream methodsFor: 'collection like access'!

at: anInt
    ^self at: anInt ifAbsent: [ ^self error: 'index out of range'].
!

at: anInt ifAbsent: aBlock
    (anInt between: 1 and: self size) ifFalse: [^aBlock value].
    anInt < headEnd
	ifTrue: [ ^head at: anInt ]
	ifFalse: [ ^ tail at: (anInt - headEnd + tailStart)]
!

inject: aValue into: aBlock
    1 to: self size do: [ :i | aValue _ aBlock value: aValue
				    value: (self at: i)].
    ^aValue
! !

!EditorStream methodsFor: 'private'!
" 
oldcopyHeadToTail: anInt
    [ (tailStart > 1 and: [headEnd > 1]) and: [anInt > 0]]
	whileTrue: [ tailStart _ tailStart - 1.
		     headEnd _ headEnd - 1.
		     tail at: tailStart put: (head at: headEnd).
		     anInt _ anInt - 1].
    anInt > 0 ifTrue:
	[ tail _ (head copyFrom: (headEnd - anInt max: 1) 
		       to: headEnd - 1),tail.
	  headEnd _ headEnd - anInt max: 1]
!

oldcopyTailToHead: anInt
    [ tailStart <= tail size and: [anInt > 0]] 
	whileTrue:
	    [ headEnd > head size ifTrue: [head grow].
	      head at: headEnd put: (tail at: tailStart).
	      tailStart _ tailStart +1.
	      headEnd _ headEnd + 1.
	      anInt _ anInt - 1 ].
!
"

copyHeadToTail: anInt
    | coll i j |
    anInt _ anInt min: headEnd - 1.
    tailStart <= anInt
	ifTrue: [ i _ anInt + 10. " 10 more spaces... "
		  j _ tail size - tailStart + 1.
		  coll _ tail species new: i + j.
		  coll replaceFrom: i + 1 to: i + j
		       with: tail startingAt: tailStart.
		  tail _ coll.
		  tailStart _ i + 1].
    tail replaceFrom: tailStart - anInt  to: tailStart - 1
	 with: head startingAt: headEnd - anInt.
    headEnd _ headEnd - anInt.
    tailStart _ tailStart - anInt.
!    

copyTailToHead: anInt    
    | coll i |
    anInt _ anInt min: tail size - tailStart + 1.
    headEnd + anInt > head size
	ifTrue: [ i _ anInt + 10.
		  coll _ head species new: i + headEnd - 1.
		  coll replaceFrom: 1 to: headEnd - 1
		       with: head startingAt: 1.
		  head _ coll ].
    head replaceFrom: headEnd to: headEnd - 1 + anInt
	 with: tail startingAt: tailStart.
    tailStart _ tailStart + anInt.
    headEnd _ headEnd + anInt.
! !