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 ;
6 FROM: models => change-model ;
9 : +col ( loc n -- newloc ) [ first2 ] dip + 2array ;
11 : +line ( loc n -- newloc ) [ first2 swap ] dip + swap 2array ;
13 : =col ( n loc -- newloc ) first swap 2array ;
15 : =line ( n loc -- newloc ) second 2array ;
17 : lines-equal? ( loc1 loc2 -- ? ) [ first ] bi@ number= ;
19 TUPLE: edit old-string new-string from old-to new-to ;
23 TUPLE: document < model locs undos redos inside-undo? ;
25 : clear-undo ( document -- )
30 : <document> ( -- document )
31 { "" } document new-model
35 : add-loc ( loc document -- ) locs>> push ;
37 : remove-loc ( loc document -- ) locs>> delete ;
39 : update-locs ( loc document -- )
40 locs>> [ set-model ] with each ;
42 : doc-line ( n document -- string ) value>> nth ;
44 : line-end ( line# document -- loc )
45 [ drop ] [ doc-line length ] 2bi 2array ;
47 : doc-lines ( from to document -- slice )
48 [ 1+ ] [ value>> ] bi* <slice> ;
50 : start-on-line ( from line# document -- n1 )
52 [ second ] [ drop 0 ] if ;
54 :: end-on-line ( to line# document -- n2 )
56 [ to second ] [ line# document doc-line length ] if ;
58 : each-line ( from to quot -- )
60 [ [ first ] bi@ [a,b] ] dip each
63 : map-lines ( from to quot -- results )
64 accumulator [ each-line ] dip ; inline
66 : start/end-on-line ( from to line# document -- n1 n2 )
67 [ start-on-line ] [ end-on-line ] bi-curry bi-curry bi* ;
69 : last-line# ( document -- line )
72 CONSTANT: doc-start { 0 0 }
74 : doc-end ( document -- loc )
75 [ last-line# ] keep line-end ;
79 : (doc-range) ( from to line# document -- slice )
80 [ start/end-on-line ] 2keep doc-line <slice> ;
82 : text+loc ( lines loc -- loc )
87 first swap length 1- + 0
89 ] dip last length + 2array ;
91 : prepend-first ( str seq -- )
92 0 swap [ append ] change-nth ;
94 : append-last ( str seq -- )
95 [ length 1- ] keep [ prepend ] change-nth ;
97 : loc-col/str ( loc document -- str col )
98 [ first2 swap ] dip nth swap ;
100 : prepare-insert ( new-lines from to lines -- new-lines )
101 [ loc-col/str head-slice ] [ loc-col/str tail-slice ] bi-curry bi*
102 pick append-last over prepend-first ;
104 : (set-doc-range) ( doc-lines from to lines -- changed-lines )
105 [ prepare-insert ] 3keep
106 [ [ first ] bi@ 1+ ] dip
109 : entire-doc ( document -- start end document )
110 [ [ doc-start ] dip doc-end ] keep ;
112 : with-undo ( document quot: ( document -- ) -- )
113 [ t >>inside-undo? ] dip keep f >>inside-undo? drop ; inline
117 : doc-range ( from to document -- string )
119 '[ [ 2dup ] dip _ (doc-range) ] map-lines
122 : add-undo ( edit document -- )
123 dup inside-undo?>> [ 2drop ] [
124 [ undos>> push ] keep
128 :: set-doc-range ( string from to document -- )
129 from to = string empty? and [
130 string string-lines :> new-lines
131 new-lines from text+loc :> new-to
132 from to document doc-range :> old-string
133 old-string string from to new-to <edit> document add-undo
134 new-lines from to document [ (set-doc-range) ] change-model
135 new-to document update-locs
138 : change-doc-range ( from to document quot -- )
139 '[ doc-range @ ] 3keep set-doc-range ; inline
141 : remove-doc-range ( from to document -- )
142 [ "" ] 3dip set-doc-range ;
144 : validate-line ( line document -- line )
145 last-line# min 0 max ;
147 : validate-col ( col line document -- col )
148 doc-line length min 0 max ;
150 : line-end? ( loc document -- ? )
151 [ first2 swap ] dip doc-line length = ;
153 : validate-loc ( loc document -- newloc )
154 2dup [ first ] [ value>> length ] bi* >= [
160 [ first2 over ] dip validate-col 2array
164 : doc-string ( document -- str )
165 entire-doc doc-range ;
167 : set-doc-string ( string document -- )
168 entire-doc set-doc-range ;
170 : clear-doc ( document -- )
171 [ "" ] dip set-doc-string ;
175 : undo/redo-edit ( edit document string-quot to-quot -- )
176 '[ [ _ [ from>> ] _ tri ] dip set-doc-range ] with-undo ; inline
178 : undo-edit ( edit document -- )
179 [ old-string>> ] [ new-to>> ] undo/redo-edit ;
181 : redo-edit ( edit document -- )
182 [ new-string>> ] [ old-to>> ] undo/redo-edit ;
184 : undo/redo ( document source-quot dest-quot do-quot -- )
185 [ dupd call [ drop ] ] 2dip
186 '[ pop swap [ @ push ] _ 2bi ] if-empty ; inline
190 : undo ( document -- )
191 [ undos>> ] [ redos>> ] [ undo-edit ] undo/redo ;
193 : redo ( document -- )
194 [ redos>> ] [ undos>> ] [ redo-edit ] undo/redo ;