1 ! Copyright (C) 2006, 2009 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays fry kernel locals math math.order
4 math.ranges models sequences splitting ;
7 : +col ( loc n -- newloc ) [ first2 ] dip + 2array ;
9 : +line ( loc n -- newloc ) [ first2 swap ] dip + swap 2array ;
11 : =col ( n loc -- newloc ) first swap 2array ;
13 : =line ( n loc -- newloc ) second 2array ;
15 : lines-equal? ( loc1 loc2 -- ? ) [ first ] bi@ number= ;
17 TUPLE: edit old-string new-string from old-to new-to ;
21 TUPLE: document < model locs undos redos inside-undo? ;
23 : clear-undo ( document -- )
28 : <document> ( -- document )
29 { "" } document new-model
33 : add-loc ( loc document -- ) locs>> push ;
35 : remove-loc ( loc document -- ) locs>> remove! drop ;
37 : update-locs ( loc document -- )
38 locs>> [ set-model ] with each ;
40 : doc-line ( n document -- string ) value>> nth ;
42 : line-end ( line# document -- loc )
43 [ drop ] [ doc-line length ] 2bi 2array ;
45 : doc-lines ( from to document -- slice )
46 [ 1 + ] [ value>> ] bi* <slice> ;
48 : start-on-line ( from line# document -- n1 )
50 [ second ] [ drop 0 ] if ;
52 :: end-on-line ( to line# document -- n2 )
54 [ to second ] [ line# document doc-line length ] if ;
56 : each-line ( ... from to quot: ( ... line -- ... ) -- ... )
58 [ [ first ] bi@ [a,b] ] dip each
61 : map-lines ( ... from to quot: ( ... line -- ... result ) -- ... results )
62 collector [ each-line ] dip ; inline
64 : start/end-on-line ( from to line# document -- n1 n2 )
65 [ start-on-line ] [ end-on-line ] bi-curry bi-curry bi* ;
67 : last-line# ( document -- line )
70 CONSTANT: doc-start { 0 0 }
72 : doc-end ( document -- loc )
73 [ last-line# ] keep line-end ;
77 : (doc-range) ( from to line# document -- slice )
78 [ start/end-on-line ] 2keep doc-line <slice> ;
80 : text+loc ( lines loc -- loc )
85 first swap length 1 - + 0
87 ] dip last length + 2array ;
89 : prepend-first ( str seq -- )
90 0 swap [ append ] change-nth ;
92 : append-last ( str seq -- )
93 [ length 1 - ] keep [ prepend ] change-nth ;
95 : loc-col/str ( loc document -- str col )
96 [ first2 swap ] dip nth swap ;
98 : prepare-insert ( new-lines from to lines -- new-lines )
99 [ loc-col/str head-slice ] [ loc-col/str tail-slice ] bi-curry bi*
100 pick append-last over prepend-first ;
102 : (set-doc-range) ( doc-lines from to lines -- changed-lines )
103 [ prepare-insert ] 3keep
104 [ [ first ] bi@ 1 + ] dip
107 : entire-doc ( document -- start end document )
108 [ [ doc-start ] dip doc-end ] keep ;
110 : with-undo ( ..a document quot: ( ..a document -- ..b ) -- ..b )
111 [ t >>inside-undo? ] dip keep f >>inside-undo? drop ; inline
113 ! XXX: This is the old string-lines behavior, it would be nice
114 ! if we could update documents to work with the new string-lines
116 : split-lines ( str -- seq )
117 dup [ "\r\n" member? ] any? [
121 "\r" ?tail drop "\r" split
124 [ length 1 - ] keep [ "\r" split ] change-nth
134 :: doc-range ( from to document -- string )
135 from to [ [ from to ] dip document (doc-range) ] map-lines
138 : add-undo ( edit document -- )
139 dup inside-undo?>> [ 2drop ] [
140 [ undos>> push ] keep
144 :: set-doc-range ( string from to document -- )
145 from to = string empty? and [
146 string split-lines :> new-lines
147 new-lines from text+loc :> new-to
148 from to document doc-range :> old-string
149 old-string string from to new-to <edit> document add-undo
150 new-lines from to document [ (set-doc-range) ] models:change-model
151 new-to document update-locs
154 : change-doc-range ( from to document quot -- )
155 '[ doc-range @ ] 3keep set-doc-range ; inline
157 : remove-doc-range ( from to document -- )
158 [ "" ] 3dip set-doc-range ;
160 : validate-line ( line document -- line )
161 last-line# min 0 max ;
163 : validate-col ( col line document -- col )
164 doc-line length min 0 max ;
166 : line-end? ( loc document -- ? )
167 [ first2 swap ] dip doc-line length = ;
169 : validate-loc ( loc document -- newloc )
170 2dup [ first ] [ value>> length ] bi* >= [
176 [ first2 over ] dip validate-col 2array
180 : doc-string ( document -- str )
181 entire-doc doc-range ;
183 : set-doc-string ( string document -- )
184 entire-doc set-doc-range ;
186 : clear-doc ( document -- )
187 [ "" ] dip set-doc-string ;
191 : undo/redo-edit ( edit document string-quot to-quot -- )
192 '[ [ _ [ from>> ] _ tri ] dip set-doc-range ] with-undo ; inline
194 : undo-edit ( edit document -- )
195 [ old-string>> ] [ new-to>> ] undo/redo-edit ;
197 : redo-edit ( edit document -- )
198 [ new-string>> ] [ old-to>> ] undo/redo-edit ;
200 : undo/redo ( document source-quot dest-quot do-quot -- )
201 [ dupd call [ drop ] ] 2dip
202 '[ pop swap [ @ push ] _ 2bi ] if-empty ; inline
206 : undo ( document -- )
207 [ undos>> ] [ redos>> ] [ undo-edit ] undo/redo ;
209 : redo ( document -- )
210 [ redos>> ] [ undos>> ] [ redo-edit ] undo/redo ;