1 ! Copyright (C) 2006, 2009 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 fry locals ;
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: edit old-string new-string from old-to new-to ;
22 TUPLE: document < model locs undos redos inside-undo? ;
24 : clear-undo ( document -- )
29 : <document> ( -- document )
30 { "" } document new-model
34 : add-loc ( loc document -- ) locs>> push ;
36 : remove-loc ( loc document -- ) locs>> delete ;
38 : update-locs ( loc document -- )
39 locs>> [ set-model ] with each ;
41 : doc-line ( n document -- string ) value>> nth ;
43 : line-end ( line# document -- loc )
44 [ drop ] [ doc-line length ] 2bi 2array ;
46 : doc-lines ( from to document -- slice )
47 [ 1+ ] [ value>> ] bi* <slice> ;
49 : start-on-line ( from line# document -- n1 )
51 [ second ] [ drop 0 ] if ;
53 :: end-on-line ( to line# document -- n2 )
55 [ to second ] [ line# document doc-line length ] if ;
57 : each-line ( from to quot -- )
59 [ [ first ] bi@ [a,b] ] dip each
62 : map-lines ( from to quot -- results )
63 accumulator [ each-line ] dip ; inline
65 : start/end-on-line ( from to line# document -- n1 n2 )
66 [ start-on-line ] [ end-on-line ] bi-curry bi-curry bi* ;
68 : last-line# ( document -- line )
71 CONSTANT: doc-start { 0 0 }
73 : doc-end ( document -- loc )
74 [ last-line# ] keep line-end ;
78 : (doc-range) ( from to line# document -- slice )
79 [ start/end-on-line ] 2keep doc-line <slice> ;
81 : text+loc ( lines loc -- loc )
86 first swap length 1- + 0
88 ] dip peek length + 2array ;
90 : prepend-first ( str seq -- )
91 0 swap [ append ] change-nth ;
93 : append-last ( str seq -- )
94 [ length 1- ] keep [ prepend ] change-nth ;
96 : loc-col/str ( loc document -- str col )
97 [ first2 swap ] dip nth swap ;
99 : prepare-insert ( new-lines from to lines -- new-lines )
100 [ loc-col/str head-slice ] [ loc-col/str tail-slice ] bi-curry bi*
101 pick append-last over prepend-first ;
103 : (set-doc-range) ( doc-lines from to lines -- changed-lines )
104 [ prepare-insert ] 3keep
105 [ [ first ] bi@ 1+ ] dip
108 : entire-doc ( document -- start end document )
109 [ [ doc-start ] dip doc-end ] keep ;
111 : with-undo ( document quot: ( document -- ) -- )
112 [ t >>inside-undo? ] dip keep f >>inside-undo? drop ; inline
116 : doc-range ( from to document -- string )
118 '[ [ 2dup ] dip _ (doc-range) ] map-lines
121 : add-undo ( edit document -- )
122 dup inside-undo?>> [ 2drop ] [
123 [ undos>> push ] keep
127 :: set-doc-range ( string from to document -- )
128 from to = string empty? and [
129 string string-lines :> new-lines
130 new-lines from text+loc :> new-to
131 from to document doc-range :> old-string
132 old-string string from to new-to <edit> document add-undo
133 new-lines from to document [ (set-doc-range) ] change-model
134 new-to document update-locs
137 : change-doc-range ( from to document quot -- )
138 '[ doc-range @ ] 3keep set-doc-range ; inline
140 : remove-doc-range ( from to document -- )
141 [ "" ] 3dip set-doc-range ;
143 : validate-line ( line document -- line )
144 last-line# min 0 max ;
146 : validate-col ( col line document -- col )
147 doc-line length min 0 max ;
149 : line-end? ( loc document -- ? )
150 [ first2 swap ] dip doc-line length = ;
152 : validate-loc ( loc document -- newloc )
153 2dup [ first ] [ value>> length ] bi* >= [
159 [ first2 over ] dip validate-col 2array
163 : doc-string ( document -- str )
164 entire-doc doc-range ;
166 : set-doc-string ( string document -- )
167 entire-doc set-doc-range ;
169 : clear-doc ( document -- )
170 [ "" ] dip set-doc-string ;
174 : undo/redo-edit ( edit document string-quot to-quot -- )
175 '[ [ _ [ from>> ] _ tri ] dip set-doc-range ] with-undo ; inline
177 : undo-edit ( edit document -- )
178 [ old-string>> ] [ new-to>> ] undo/redo-edit ;
180 : redo-edit ( edit document -- )
181 [ new-string>> ] [ old-to>> ] undo/redo-edit ;
183 : undo/redo ( document source-quot dest-quot do-quot -- )
184 [ dupd call [ drop ] ] 2dip
185 '[ pop swap [ @ push ] _ 2bi ] if-empty ; inline
189 : undo ( document -- )
190 [ undos>> ] [ redos>> ] [ undo-edit ] undo/redo ;
192 : redo ( document -- )
193 [ redos>> ] [ undos>> ] [ redo-edit ] undo/redo ;