]> gitweb.factorcode.org Git - factor.git/blob - basis/documents/documents.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[factor.git] / basis / documents / documents.factor
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 ;
7 IN: documents
8
9 : +col ( loc n -- newloc ) [ first2 ] dip + 2array ;
10
11 : +line ( loc n -- newloc ) [ first2 swap ] dip + swap 2array ;
12
13 : =col ( n loc -- newloc ) first swap 2array ;
14
15 : =line ( n loc -- newloc ) second 2array ;
16
17 : lines-equal? ( loc1 loc2 -- ? ) [ first ] bi@ number= ;
18
19 TUPLE: edit old-string new-string from old-to new-to ;
20
21 C: <edit> edit
22
23 TUPLE: document < model locs undos redos inside-undo? ;
24
25 : clear-undo ( document -- )
26     V{ } clone >>undos
27     V{ } clone >>redos
28     drop ;
29
30 : <document> ( -- document )
31     { "" } document new-model
32     V{ } clone >>locs
33     dup clear-undo ;
34
35 : add-loc ( loc document -- ) locs>> push ;
36
37 : remove-loc ( loc document -- ) locs>> delete ;
38
39 : update-locs ( loc document -- )
40     locs>> [ set-model ] with each ;
41
42 : doc-line ( n document -- string ) value>> nth ;
43
44 : line-end ( line# document -- loc )
45     [ drop ] [ doc-line length ] 2bi 2array ;
46
47 : doc-lines ( from to document -- slice )
48     [ 1 + ] [ value>> ] bi* <slice> ;
49
50 : start-on-line ( from line# document -- n1 )
51     drop over first =
52     [ second ] [ drop 0 ] if ;
53
54 :: end-on-line ( to line# document -- n2 )
55     to first line# =
56     [ to second ] [ line# document doc-line length ] if ;
57
58 : each-line ( from to quot -- )
59     2over = [ 3drop ] [
60         [ [ first ] bi@ [a,b] ] dip each
61     ] if ; inline
62
63 : map-lines ( from to quot -- results )
64     accumulator [ each-line ] dip ; inline
65
66 : start/end-on-line ( from to line# document -- n1 n2 )
67     [ start-on-line ] [ end-on-line ] bi-curry bi-curry bi* ;
68
69 : last-line# ( document -- line )
70     value>> length 1 - ;
71
72 CONSTANT: doc-start { 0 0 }
73
74 : doc-end ( document -- loc )
75     [ last-line# ] keep line-end ;
76
77 <PRIVATE
78
79 : (doc-range) ( from to line# document -- slice )
80     [ start/end-on-line ] 2keep doc-line <slice> ;
81
82 : text+loc ( lines loc -- loc )
83     over [
84         over length 1 = [
85             nip first2
86         ] [
87             first swap length 1 - + 0
88         ] if
89     ] dip last length + 2array ;
90
91 : prepend-first ( str seq -- )
92     0 swap [ append ] change-nth ;
93
94 : append-last ( str seq -- )
95     [ length 1 - ] keep [ prepend ] change-nth ;
96
97 : loc-col/str ( loc document -- str col )
98     [ first2 swap ] dip nth swap ;
99
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 ;
103
104 : (set-doc-range) ( doc-lines from to lines -- changed-lines )
105     [ prepare-insert ] 3keep
106     [ [ first ] bi@ 1 + ] dip
107     replace-slice ;
108
109 : entire-doc ( document -- start end document )
110     [ [ doc-start ] dip doc-end ] keep ;
111
112 : with-undo ( document quot: ( document -- ) -- )
113     [ t >>inside-undo? ] dip keep f >>inside-undo? drop ; inline
114
115 PRIVATE>
116
117 : doc-range ( from to document -- string )
118     [ 2dup ] dip
119     '[ [ 2dup ] dip _ (doc-range) ] map-lines
120     2nip "\n" join ;
121
122 : add-undo ( edit document -- )
123     dup inside-undo?>> [ 2drop ] [
124         [ undos>> push ] keep
125         redos>> delete-all
126     ] if ;
127
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
136     ] unless ;
137
138 : change-doc-range ( from to document quot -- )
139     '[ doc-range @ ] 3keep set-doc-range ; inline
140
141 : remove-doc-range ( from to document -- )
142     [ "" ] 3dip set-doc-range ;
143
144 : validate-line ( line document -- line )
145     last-line# min 0 max ;
146
147 : validate-col ( col line document -- col )
148     doc-line length min 0 max ;
149
150 : line-end? ( loc document -- ? )
151     [ first2 swap ] dip doc-line length = ;
152
153 : validate-loc ( loc document -- newloc )
154     2dup [ first ] [ value>> length ] bi* >= [
155         nip doc-end
156     ] [
157         over first 0 < [
158             2drop { 0 0 }
159         ] [
160             [ first2 over ] dip validate-col 2array
161         ] if
162     ] if ;
163
164 : doc-string ( document -- str )
165     entire-doc doc-range ;
166
167 : set-doc-string ( string document -- )
168     entire-doc set-doc-range ;
169
170 : clear-doc ( document -- )
171     [ "" ] dip set-doc-string ;
172
173 <PRIVATE
174
175 : undo/redo-edit ( edit document string-quot to-quot -- )
176     '[ [ _ [ from>> ] _ tri ] dip set-doc-range ] with-undo ; inline
177
178 : undo-edit ( edit document -- )
179     [ old-string>> ] [ new-to>> ] undo/redo-edit ;
180
181 : redo-edit ( edit document -- )
182     [ new-string>> ] [ old-to>> ] undo/redo-edit ;
183
184 : undo/redo ( document source-quot dest-quot do-quot -- )
185     [ dupd call [ drop ] ] 2dip
186     '[ pop swap [ @ push ] _ 2bi ] if-empty ; inline
187
188 PRIVATE>
189
190 : undo ( document -- )
191     [ undos>> ] [ redos>> ] [ undo-edit ] undo/redo ;
192
193 : redo ( document -- )
194     [ redos>> ] [ undos>> ] [ redo-edit ] undo/redo ;