| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! Copyright (C) 2006, 2007 Slava Pestov | 
					
						
							|  |  |  | ! 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 | 
					
						
							|  |  |  | math.order ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: documents | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : +col ( loc n -- newloc ) >r first2 r> + 2array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : +line ( loc n -- newloc ) >r first2 swap r> + swap 2array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : =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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-09 00:08:12 -04:00
										 |  |  | TUPLE: document < model locs ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : <document> ( -- document )
 | 
					
						
							| 
									
										
										
										
											2008-07-09 00:08:12 -04:00
										 |  |  |     V{ "" } clone document new-model | 
					
						
							|  |  |  |     V{ } clone >>locs ;
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-08 16:32:55 -04:00
										 |  |  | : remove-loc ( loc document -- ) locs>> delete ;
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | : doc-lines ( from to document -- slice )
 | 
					
						
							| 
									
										
										
										
											2008-08-31 03:51:01 -04:00
										 |  |  |     >r 1+ r> value>> <slice> ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : start-on-line ( document from line# -- n1 )
 | 
					
						
							|  |  |  |     >r dup first r> = [ nip second ] [ 2drop 0 ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : end-on-line ( document to line# -- n2 )
 | 
					
						
							|  |  |  |     over first over = [ | 
					
						
							|  |  |  |         drop second nip
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         nip swap doc-line length
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : each-line ( from to quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-02-16 19:50:26 -05:00
										 |  |  |     2over = [ | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |         3drop
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2008-03-29 21:36:58 -04:00
										 |  |  |         >r [ first ] bi@ 1+ dup <slice> r> each
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : start/end-on-line ( from to line# -- n1 n2 )
 | 
					
						
							|  |  |  |     tuck >r >r document get -rot start-on-line r> r> | 
					
						
							|  |  |  |     document get -rot end-on-line ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (doc-range) ( from to line# -- )
 | 
					
						
							|  |  |  |     [ start/end-on-line ] keep document get doc-line <slice> , ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : doc-range ( from to document -- string )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         document set 2dup [ | 
					
						
							|  |  |  |             >r 2dup r> (doc-range) | 
					
						
							|  |  |  |         ] each-line 2drop
 | 
					
						
							|  |  |  |     ] { } make "\n" join ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : text+loc ( lines loc -- loc )
 | 
					
						
							|  |  |  |     over >r over length 1 = [ | 
					
						
							|  |  |  |         nip first2
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         first swap length 1- + 0
 | 
					
						
							|  |  |  |     ] if r> peek length + 2array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : prepend-first ( str seq -- )
 | 
					
						
							|  |  |  |     0 swap [ append ] change-nth ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : append-last ( str seq -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-19 20:15:32 -04:00
										 |  |  |     [ length 1- ] keep [ prepend ] change-nth ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : loc-col/str ( loc document -- str col )
 | 
					
						
							|  |  |  |     >r first2 swap r> nth swap ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : prepare-insert ( newinput from to lines -- newinput )
 | 
					
						
							|  |  |  |     tuck loc-col/str tail-slice >r loc-col/str head-slice r> | 
					
						
							|  |  |  |     pick append-last over prepend-first ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (set-doc-range) ( newlines from to lines -- )
 | 
					
						
							|  |  |  |     [ prepare-insert ] 3keep
 | 
					
						
							| 
									
										
										
										
											2008-03-29 21:36:58 -04:00
										 |  |  |     >r [ first ] bi@ 1+ r> | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     replace-slice ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : set-doc-range ( string from to document -- )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         >r >r >r string-lines r> [ text+loc ] 2keep r> r> | 
					
						
							|  |  |  |         [ [ (set-doc-range) ] keep ] change-model | 
					
						
							|  |  |  |     ] keep update-locs ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : remove-doc-range ( from to document -- )
 | 
					
						
							|  |  |  |     >r >r >r "" r> r> r> set-doc-range ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : last-line# ( document -- line )
 | 
					
						
							| 
									
										
										
										
											2008-08-31 03:51:01 -04:00
										 |  |  |     value>> length 1- ;
 | 
					
						
							| 
									
										
										
										
											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 ( line# document -- loc )
 | 
					
						
							|  |  |  |     dupd doc-line length 2array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : line-end? ( loc document -- ? )
 | 
					
						
							|  |  |  |     >r first2 swap r> doc-line length = ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : doc-end ( document -- loc )
 | 
					
						
							|  |  |  |     [ last-line# ] keep line-end ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : validate-loc ( loc document -- newloc )
 | 
					
						
							| 
									
										
										
										
											2008-08-31 03:51:01 -04:00
										 |  |  |     over first over value>> length >= [ | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |         nip doc-end | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         over first 0 < [ | 
					
						
							|  |  |  |             2drop { 0 0 } | 
					
						
							|  |  |  |         ] [ | 
					
						
							|  |  |  |             >r first2 swap tuck r> validate-col 2array
 | 
					
						
							|  |  |  |         ] if
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : doc-string ( document -- str )
 | 
					
						
							| 
									
										
										
										
											2008-08-31 03:51:01 -04:00
										 |  |  |     value>> "\n" join ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : set-doc-string ( string document -- )
 | 
					
						
							|  |  |  |     >r string-lines V{ } like r> [ set-model ] keep
 | 
					
						
							| 
									
										
										
										
											2008-08-29 03:13:08 -04:00
										 |  |  |     [ doc-end ] [ update-locs ] bi ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : clear-doc ( document -- )
 | 
					
						
							|  |  |  |     "" swap set-doc-string ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: prev-elt ( loc document elt -- newloc )
 | 
					
						
							|  |  |  | GENERIC: next-elt ( loc document elt -- newloc )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : prev/next-elt ( loc document elt -- start end )
 | 
					
						
							|  |  |  |     3dup next-elt >r prev-elt r> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : elt-string ( loc document elt -- string )
 | 
					
						
							|  |  |  |     over >r prev/next-elt r> doc-range ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: char-elt ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (prev-char) ( loc document quot -- loc )
 | 
					
						
							|  |  |  |     -rot { | 
					
						
							|  |  |  |         { [ over { 0 0 } = ] [ drop ] } | 
					
						
							|  |  |  |         { [ over second zero? ] [ >r first 1- r> line-end ] } | 
					
						
							| 
									
										
										
										
											2008-04-11 13:55:57 -04:00
										 |  |  |         [ pick call ] | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     } cond nip ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (next-char) ( loc document quot -- loc )
 | 
					
						
							|  |  |  |     -rot { | 
					
						
							|  |  |  |         { [ 2dup doc-end = ] [ drop ] } | 
					
						
							|  |  |  |         { [ 2dup line-end? ] [ drop first 1+ 0 2array ] } | 
					
						
							| 
									
										
										
										
											2008-04-11 13:55:57 -04:00
										 |  |  |         [ pick call ] | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     } cond nip ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: char-elt prev-elt | 
					
						
							|  |  |  |     drop [ drop -1 +col ] (prev-char) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: char-elt next-elt | 
					
						
							|  |  |  |     drop [ drop 1 +col ] (next-char) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-22 21:00:56 -05:00
										 |  |  | TUPLE: one-char-elt ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: one-char-elt prev-elt 2drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: one-char-elt next-elt 2drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : (word-elt) ( loc document quot -- loc )
 | 
					
						
							|  |  |  |     pick >r | 
					
						
							|  |  |  |     >r >r first2 swap r> doc-line r> call
 | 
					
						
							|  |  |  |     r> =col ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-08 16:32:55 -04:00
										 |  |  | : ((word-elt)) ( n seq -- ? n seq ) [ ?nth blank? ] 2keep ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : break-detector ( ? -- quot )
 | 
					
						
							|  |  |  |     [ >r blank? r> xor ] curry ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (prev-word) ( ? col str -- col )
 | 
					
						
							| 
									
										
										
										
											2008-04-26 00:17:08 -04:00
										 |  |  |     rot break-detector find-last-from drop ?1+ ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (next-word) ( ? col str -- col )
 | 
					
						
							| 
									
										
										
										
											2008-04-26 00:17:08 -04:00
										 |  |  |     [ rot break-detector find-from drop ] keep
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     over not [ nip length ] [ drop ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: one-word-elt ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: one-word-elt prev-elt | 
					
						
							|  |  |  |     drop
 | 
					
						
							| 
									
										
										
										
											2007-12-13 16:34:36 -05:00
										 |  |  |     [ f -rot >r 1- r> (prev-word) ] (word-elt) ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: one-word-elt next-elt | 
					
						
							|  |  |  |     drop
 | 
					
						
							| 
									
										
										
										
											2007-12-13 16:34:36 -05:00
										 |  |  |     [ f -rot (next-word) ] (word-elt) ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | TUPLE: word-elt ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: word-elt prev-elt | 
					
						
							|  |  |  |     drop
 | 
					
						
							|  |  |  |     [ [ >r 1- r> ((word-elt)) (prev-word) ] (word-elt) ] | 
					
						
							|  |  |  |     (prev-char) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: word-elt next-elt | 
					
						
							|  |  |  |     drop
 | 
					
						
							|  |  |  |     [ [ ((word-elt)) (next-word) ] (word-elt) ] | 
					
						
							|  |  |  |     (next-char) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: one-line-elt ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: one-line-elt prev-elt | 
					
						
							|  |  |  |     2drop first 0 2array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: one-line-elt next-elt | 
					
						
							|  |  |  |     drop >r first dup r> doc-line length 2array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: line-elt ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: line-elt prev-elt | 
					
						
							|  |  |  |     2drop dup first zero? [ drop { 0 0 } ] [ -1 +line ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: line-elt next-elt | 
					
						
							|  |  |  |     drop over first over last-line# number=
 | 
					
						
							|  |  |  |     [ nip doc-end ] [ drop 1 +line ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: doc-elt ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: doc-elt prev-elt 3drop { 0 0 } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: doc-elt next-elt drop nip doc-end ;
 |