| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  | ! Copyright (C) 2006, 2009 Slava Pestov | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-09-10 21:07:00 -04:00
										 |  |  | USING: accessors arrays io kernel math models namespaces make | 
					
						
							| 
									
										
										
										
											2008-06-08 17:47:20 -04:00
										 |  |  | sequences strings splitting combinators unicode.categories | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  | math.order math.ranges fry locals ;
 | 
					
						
							| 
									
										
										
										
											2009-05-14 17:54:16 -04:00
										 |  |  | FROM: models => change-model ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: documents | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-22 01:30:39 -05:00
										 |  |  | : +col ( loc n -- newloc ) [ first2 ] dip + 2array ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-22 01:30:39 -05:00
										 |  |  | : +line ( loc n -- newloc ) [ first2 swap ] dip + swap 2array ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : =col ( n loc -- newloc ) first swap 2array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : =line ( n loc -- newloc ) second 2array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-29 21:36:58 -04:00
										 |  |  | : lines-equal? ( loc1 loc2 -- ? ) [ first ] bi@ number= ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  | TUPLE: edit old-string new-string from old-to new-to ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C: <edit> edit | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: document < model locs undos redos inside-undo? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : clear-undo ( document -- )
 | 
					
						
							|  |  |  |     V{ } clone >>undos | 
					
						
							|  |  |  |     V{ } clone >>redos | 
					
						
							|  |  |  |     drop ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : <document> ( -- document )
 | 
					
						
							| 
									
										
										
										
											2009-01-29 04:04:23 -05:00
										 |  |  |     { "" } document new-model | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  |     V{ } clone >>locs | 
					
						
							|  |  |  |     dup clear-undo ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-08 16:32:55 -04:00
										 |  |  | : add-loc ( loc document -- ) locs>> push ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-28 00:25:35 -04:00
										 |  |  | : remove-loc ( loc document -- ) locs>> remove! drop ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : update-locs ( loc document -- )
 | 
					
						
							| 
									
										
										
										
											2008-08-29 03:13:08 -04:00
										 |  |  |     locs>> [ set-model ] with each ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-31 03:51:01 -04:00
										 |  |  | : doc-line ( n document -- string ) value>> nth ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  | : line-end ( line# document -- loc )
 | 
					
						
							|  |  |  |     [ drop ] [ doc-line length ] 2bi 2array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : doc-lines ( from to document -- slice )
 | 
					
						
							| 
									
										
										
										
											2009-08-13 20:21:44 -04:00
										 |  |  |     [ 1 + ] [ value>> ] bi* <slice> ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-07 19:09:50 -05:00
										 |  |  | : start-on-line ( from line# document -- n1 )
 | 
					
						
							|  |  |  |     drop over first =
 | 
					
						
							|  |  |  |     [ second ] [ drop 0 ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: end-on-line ( to line# document -- n2 )
 | 
					
						
							|  |  |  |     to first line# =
 | 
					
						
							|  |  |  |     [ to second ] [ line# document doc-line length ] if ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : each-line ( from to quot -- )
 | 
					
						
							| 
									
										
										
										
											2009-02-07 19:09:50 -05:00
										 |  |  |     2over = [ 3drop ] [ | 
					
						
							| 
									
										
										
										
											2008-12-02 21:46:09 -05:00
										 |  |  |         [ [ first ] bi@ [a,b] ] dip each
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-07 19:09:50 -05:00
										 |  |  | : map-lines ( from to quot -- results )
 | 
					
						
							|  |  |  |     accumulator [ each-line ] dip ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : start/end-on-line ( from to line# document -- n1 n2 )
 | 
					
						
							|  |  |  |     [ start-on-line ] [ end-on-line ] bi-curry bi-curry bi* ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  | : last-line# ( document -- line )
 | 
					
						
							| 
									
										
										
										
											2009-08-13 20:21:44 -04:00
										 |  |  |     value>> length 1 - ;
 | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | CONSTANT: doc-start { 0 0 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : doc-end ( document -- loc )
 | 
					
						
							|  |  |  |     [ last-line# ] keep line-end ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-07 19:09:50 -05:00
										 |  |  | : (doc-range) ( from to line# document -- slice )
 | 
					
						
							|  |  |  |     [ start/end-on-line ] 2keep doc-line <slice> ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : text+loc ( lines loc -- loc )
 | 
					
						
							| 
									
										
										
										
											2008-11-22 01:30:39 -05:00
										 |  |  |     over [ | 
					
						
							|  |  |  |         over length 1 = [ | 
					
						
							|  |  |  |             nip first2
 | 
					
						
							|  |  |  |         ] [ | 
					
						
							| 
									
										
										
										
											2009-08-13 20:21:44 -04:00
										 |  |  |             first swap length 1 - + 0
 | 
					
						
							| 
									
										
										
										
											2008-11-22 01:30:39 -05:00
										 |  |  |         ] if
 | 
					
						
							| 
									
										
										
										
											2009-05-25 17:38:33 -04:00
										 |  |  |     ] dip last length + 2array ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : prepend-first ( str seq -- )
 | 
					
						
							|  |  |  |     0 swap [ append ] change-nth ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : append-last ( str seq -- )
 | 
					
						
							| 
									
										
										
										
											2009-08-13 20:21:44 -04:00
										 |  |  |     [ length 1 - ] keep [ prepend ] change-nth ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : loc-col/str ( loc document -- str col )
 | 
					
						
							| 
									
										
										
										
											2008-11-22 01:30:39 -05:00
										 |  |  |     [ first2 swap ] dip nth swap ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  | : prepare-insert ( new-lines from to lines -- new-lines )
 | 
					
						
							| 
									
										
										
										
											2009-02-02 14:43:54 -05:00
										 |  |  |     [ loc-col/str head-slice ] [ loc-col/str tail-slice ] bi-curry bi*
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     pick append-last over prepend-first ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-29 04:04:23 -05:00
										 |  |  | : (set-doc-range) ( doc-lines from to lines -- changed-lines )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     [ prepare-insert ] 3keep
 | 
					
						
							| 
									
										
										
										
											2009-08-13 20:21:44 -04:00
										 |  |  |     [ [ first ] bi@ 1 + ] dip
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     replace-slice ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  | : entire-doc ( document -- start end document )
 | 
					
						
							|  |  |  |     [ [ doc-start ] dip doc-end ] keep ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : with-undo ( document quot: ( document -- ) -- )
 | 
					
						
							|  |  |  |     [ t >>inside-undo? ] dip keep f >>inside-undo? drop ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : doc-range ( from to document -- string )
 | 
					
						
							| 
									
										
										
										
											2009-02-07 19:09:50 -05:00
										 |  |  |     [ 2dup ] dip
 | 
					
						
							|  |  |  |     '[ [ 2dup ] dip _ (doc-range) ] map-lines | 
					
						
							|  |  |  |     2nip "\n" join ;
 | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : add-undo ( edit document -- )
 | 
					
						
							|  |  |  |     dup inside-undo?>> [ 2drop ] [ | 
					
						
							|  |  |  |         [ undos>> push ] keep
 | 
					
						
							|  |  |  |         redos>> delete-all
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: set-doc-range ( string from to document -- )
 | 
					
						
							| 
									
										
										
										
											2009-01-28 13:11:45 -05:00
										 |  |  |     from to = string empty? and [ | 
					
						
							|  |  |  |         string string-lines :> new-lines | 
					
						
							|  |  |  |         new-lines from text+loc :> new-to | 
					
						
							|  |  |  |         from to document doc-range :> old-string | 
					
						
							|  |  |  |         old-string string from to new-to <edit> document add-undo | 
					
						
							| 
									
										
										
										
											2009-01-29 04:04:23 -05:00
										 |  |  |         new-lines from to document [ (set-doc-range) ] change-model | 
					
						
							| 
									
										
										
										
											2009-01-28 13:11:45 -05:00
										 |  |  |         new-to document update-locs | 
					
						
							|  |  |  |     ] unless ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-26 00:03:13 -05:00
										 |  |  | : change-doc-range ( from to document quot -- )
 | 
					
						
							|  |  |  |     '[ doc-range @ ] 3keep set-doc-range ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : remove-doc-range ( from to document -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-22 01:30:39 -05:00
										 |  |  |     [ "" ] 3dip set-doc-range ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : validate-line ( line document -- line )
 | 
					
						
							|  |  |  |     last-line# min 0 max ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : validate-col ( col line document -- col )
 | 
					
						
							|  |  |  |     doc-line length min 0 max ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : line-end? ( loc document -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-11-22 01:30:39 -05:00
										 |  |  |     [ first2 swap ] dip doc-line length = ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : validate-loc ( loc document -- newloc )
 | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  |     2dup [ first ] [ value>> length ] bi* >= [ | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |         nip doc-end | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         over first 0 < [ | 
					
						
							|  |  |  |             2drop { 0 0 } | 
					
						
							|  |  |  |         ] [ | 
					
						
							| 
									
										
										
										
											2009-02-02 14:43:54 -05:00
										 |  |  |             [ first2 over ] dip validate-col 2array
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |         ] if
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : doc-string ( document -- str )
 | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  |     entire-doc doc-range ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : set-doc-string ( string document -- )
 | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  |     entire-doc set-doc-range ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : clear-doc ( document -- )
 | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  |     [ "" ] dip set-doc-string ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  | : undo/redo-edit ( edit document string-quot to-quot -- )
 | 
					
						
							|  |  |  |     '[ [ _ [ from>> ] _ tri ] dip set-doc-range ] with-undo ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  | : undo-edit ( edit document -- )
 | 
					
						
							|  |  |  |     [ old-string>> ] [ new-to>> ] undo/redo-edit ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  | : redo-edit ( edit document -- )
 | 
					
						
							|  |  |  |     [ new-string>> ] [ old-to>> ] undo/redo-edit ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  | : undo/redo ( document source-quot dest-quot do-quot -- )
 | 
					
						
							|  |  |  |     [ dupd call [ drop ] ] 2dip
 | 
					
						
							|  |  |  |     '[ pop swap [ @ push ] _ 2bi ] if-empty ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  | : undo ( document -- )
 | 
					
						
							|  |  |  |     [ undos>> ] [ redos>> ] [ undo-edit ] undo/redo ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 01:30:30 -05:00
										 |  |  | : redo ( document -- )
 | 
					
						
							| 
									
										
										
										
											2009-05-25 17:38:33 -04:00
										 |  |  |     [ redos>> ] [ undos>> ] [ redo-edit ] undo/redo ;
 |