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