1 ! Copyright (C) 2006, 2008 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays io kernel math models namespaces make
4 sequences strings splitting combinators unicode.categories
5 math.order math.ranges ;
8 : +col ( loc n -- newloc ) [ first2 ] dip + 2array ;
10 : +line ( loc n -- newloc ) [ first2 swap ] dip + swap 2array ;
12 : =col ( n loc -- newloc ) first swap 2array ;
14 : =line ( n loc -- newloc ) second 2array ;
16 : lines-equal? ( loc1 loc2 -- ? ) [ first ] bi@ number= ;
18 TUPLE: document < model locs ;
20 : <document> ( -- document )
21 V{ "" } clone document new-model
24 : add-loc ( loc document -- ) locs>> push ;
26 : remove-loc ( loc document -- ) locs>> delete ;
28 : update-locs ( loc document -- )
29 locs>> [ set-model ] with each ;
31 : doc-line ( n document -- string ) value>> nth ;
33 : doc-lines ( from to document -- slice )
34 [ 1+ ] dip value>> <slice> ;
36 : start-on-line ( document from line# -- n1 )
37 [ dup first ] dip = [ nip second ] [ 2drop 0 ] if ;
39 : end-on-line ( document to line# -- n2 )
43 nip swap doc-line length
46 : each-line ( from to quot -- )
50 [ [ first ] bi@ [a,b] ] dip each
53 : start/end-on-line ( from to line# -- n1 n2 )
55 [ [ document get ] 2dip start-on-line ]
56 [ [ document get ] 2dip end-on-line ]
59 : (doc-range) ( from to line# -- )
60 [ start/end-on-line ] keep document get doc-line <slice> , ;
62 : doc-range ( from to document -- string )
65 [ 2dup ] dip (doc-range)
67 ] { } make "\n" join ;
69 : text+loc ( lines loc -- loc )
74 first swap length 1- + 0
76 ] dip peek length + 2array ;
78 : prepend-first ( str seq -- )
79 0 swap [ append ] change-nth ;
81 : append-last ( str seq -- )
82 [ length 1- ] keep [ prepend ] change-nth ;
84 : loc-col/str ( loc document -- str col )
85 [ first2 swap ] dip nth swap ;
87 : prepare-insert ( newinput from to lines -- newinput )
88 tuck [ loc-col/str head-slice ] [ loc-col/str tail-slice ] 2bi*
89 pick append-last over prepend-first ;
91 : (set-doc-range) ( newlines from to lines -- )
92 [ prepare-insert ] 3keep
93 [ [ first ] bi@ 1+ ] dip
96 : set-doc-range ( string from to document -- )
98 [ [ string-lines ] dip [ text+loc ] 2keep ] 2dip
99 [ [ (set-doc-range) ] keep ] change-model
102 : remove-doc-range ( from to document -- )
103 [ "" ] 3dip set-doc-range ;
105 : last-line# ( document -- line )
108 : validate-line ( line document -- line )
109 last-line# min 0 max ;
111 : validate-col ( col line document -- col )
112 doc-line length min 0 max ;
114 : line-end ( line# document -- loc )
115 dupd doc-line length 2array ;
117 : line-end? ( loc document -- ? )
118 [ first2 swap ] dip doc-line length = ;
120 : doc-end ( document -- loc )
121 [ last-line# ] keep line-end ;
123 : validate-loc ( loc document -- newloc )
124 over first over value>> length >= [
130 [ first2 swap tuck ] dip validate-col 2array
134 : doc-string ( document -- str )
137 : set-doc-string ( string document -- )
138 [ string-lines V{ } like ] dip [ set-model ] keep
139 [ doc-end ] [ update-locs ] bi ;
141 : clear-doc ( document -- )
142 "" swap set-doc-string ;
144 GENERIC: prev-elt ( loc document elt -- newloc )
145 GENERIC: next-elt ( loc document elt -- newloc )
147 : prev/next-elt ( loc document elt -- start end )
148 [ prev-elt ] [ next-elt ] 3bi ;
150 : elt-string ( loc document elt -- string )
151 [ prev/next-elt ] [ drop ] 2bi doc-range ;
155 : (prev-char) ( loc document quot -- loc )
157 { [ pick { 0 0 } = ] [ 2drop ] }
158 { [ pick second zero? ] [ drop [ first 1- ] dip line-end ] }
162 : (next-char) ( loc document quot -- loc )
164 { [ 2over doc-end = ] [ 2drop ] }
165 { [ 2over line-end? ] [ 2drop first 1+ 0 2array ] }
170 drop [ drop -1 +col ] (prev-char) ;
173 drop [ drop 1 +col ] (next-char) ;
175 TUPLE: one-char-elt ;
177 M: one-char-elt prev-elt 2drop ;
179 M: one-char-elt next-elt 2drop ;
181 : (word-elt) ( loc document quot -- loc )
183 [ [ first2 swap ] dip doc-line ] dip call
186 : ((word-elt)) ( n seq -- ? n seq ) [ ?nth blank? ] 2keep ;
188 : break-detector ( ? -- quot )
189 [ [ blank? ] dip xor ] curry ; inline
191 : (prev-word) ( ? col str -- col )
192 rot break-detector find-last-from drop ?1+ ;
194 : (next-word) ( ? col str -- col )
195 [ rot break-detector find-from drop ] keep
196 over not [ nip length ] [ drop ] if ;
198 TUPLE: one-word-elt ;
200 M: one-word-elt prev-elt
202 [ [ [ f ] dip 1- ] dip (prev-word) ] (word-elt) ;
204 M: one-word-elt next-elt
206 [ [ f ] 2dip (next-word) ] (word-elt) ;
212 [ [ [ 1- ] dip ((word-elt)) (prev-word) ] (word-elt) ]
217 [ [ ((word-elt)) (next-word) ] (word-elt) ]
220 TUPLE: one-line-elt ;
222 M: one-line-elt prev-elt
223 2drop first 0 2array ;
225 M: one-line-elt next-elt
226 drop [ first dup ] dip doc-line length 2array ;
231 2drop dup first zero? [ drop { 0 0 } ] [ -1 +line ] if ;
234 drop over first over last-line# number=
235 [ nip doc-end ] [ drop 1 +line ] if ;
239 M: doc-elt prev-elt 3drop { 0 0 } ;
241 M: doc-elt next-elt drop nip doc-end ;