2008-12-02 21:46:09 -05:00
|
|
|
! Copyright (C) 2006, 2008 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
|
2008-12-24 23:38:02 -05:00
|
|
|
math.order math.ranges fry ;
|
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
|
|
|
|
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-11-22 01:30:39 -05:00
|
|
|
[ 1+ ] dip value>> <slice> ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: start-on-line ( document from line# -- n1 )
|
2008-11-22 01:30:39 -05:00
|
|
|
[ dup first ] dip = [ nip second ] [ 2drop 0 ] if ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: 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-12-02 21:46:09 -05:00
|
|
|
[ [ first ] bi@ [a,b] ] dip each
|
2007-09-20 18:09:08 -04:00
|
|
|
] if ; inline
|
|
|
|
|
|
|
|
: start/end-on-line ( from to line# -- n1 n2 )
|
2008-11-22 01:30:39 -05:00
|
|
|
tuck
|
|
|
|
[ [ document get ] 2dip start-on-line ]
|
|
|
|
[ [ document get ] 2dip end-on-line ]
|
|
|
|
2bi* ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: (doc-range) ( from to line# -- )
|
|
|
|
[ start/end-on-line ] keep document get doc-line <slice> , ;
|
|
|
|
|
|
|
|
: doc-range ( from to document -- string )
|
|
|
|
[
|
|
|
|
document set 2dup [
|
2008-11-22 01:30:39 -05:00
|
|
|
[ 2dup ] dip (doc-range)
|
2007-09-20 18:09:08 -04:00
|
|
|
] each-line 2drop
|
|
|
|
] { } make "\n" join ;
|
|
|
|
|
|
|
|
: text+loc ( lines loc -- loc )
|
2008-11-22 01:30:39 -05:00
|
|
|
over [
|
|
|
|
over length 1 = [
|
|
|
|
nip first2
|
|
|
|
] [
|
|
|
|
first swap length 1- + 0
|
|
|
|
] if
|
|
|
|
] dip peek length + 2array ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: 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 )
|
2008-11-22 01:30:39 -05:00
|
|
|
[ first2 swap ] dip nth swap ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: prepare-insert ( newinput from to lines -- newinput )
|
2008-11-22 01:30:39 -05:00
|
|
|
tuck [ loc-col/str head-slice ] [ loc-col/str tail-slice ] 2bi*
|
2007-09-20 18:09:08 -04:00
|
|
|
pick append-last over prepend-first ;
|
|
|
|
|
|
|
|
: (set-doc-range) ( newlines from to lines -- )
|
|
|
|
[ prepare-insert ] 3keep
|
2008-11-22 01:30:39 -05:00
|
|
|
[ [ first ] bi@ 1+ ] dip
|
2007-09-20 18:09:08 -04:00
|
|
|
replace-slice ;
|
|
|
|
|
|
|
|
: set-doc-range ( string from to document -- )
|
|
|
|
[
|
2008-11-22 01:30:39 -05:00
|
|
|
[ [ string-lines ] dip [ text+loc ] 2keep ] 2dip
|
2007-09-20 18:09:08 -04:00
|
|
|
[ [ (set-doc-range) ] keep ] change-model
|
|
|
|
] keep update-locs ;
|
|
|
|
|
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
|
|
|
|
|
|
|
: 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 -- ? )
|
2008-11-22 01:30:39 -05:00
|
|
|
[ first2 swap ] dip doc-line length = ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: 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 }
|
|
|
|
] [
|
2008-11-22 01:30:39 -05:00
|
|
|
[ first2 swap tuck ] dip validate-col 2array
|
2007-09-20 18:09:08 -04:00
|
|
|
] 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 -- )
|
2008-11-22 01:30:39 -05:00
|
|
|
[ string-lines V{ } like ] dip [ 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 )
|
2008-11-22 01:30:39 -05:00
|
|
|
[ prev-elt ] [ next-elt ] 3bi ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: elt-string ( loc document elt -- string )
|
2008-11-22 01:30:39 -05:00
|
|
|
[ prev/next-elt ] [ drop ] 2bi doc-range ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-01-12 00:51:41 -05:00
|
|
|
: set-elt-string ( string loc document elt -- )
|
|
|
|
[ prev/next-elt ] [ drop ] 2bi set-doc-range ;
|
|
|
|
|
2008-12-24 23:38:02 -05:00
|
|
|
SINGLETON: char-elt
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: (prev-char) ( loc document quot -- loc )
|
2008-12-22 06:50:43 -05:00
|
|
|
{
|
|
|
|
{ [ pick { 0 0 } = ] [ 2drop ] }
|
|
|
|
{ [ pick second zero? ] [ drop [ first 1- ] dip line-end ] }
|
|
|
|
[ call ]
|
|
|
|
} cond ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: (next-char) ( loc document quot -- loc )
|
2008-12-22 06:50:43 -05:00
|
|
|
{
|
|
|
|
{ [ 2over doc-end = ] [ 2drop ] }
|
|
|
|
{ [ 2over line-end? ] [ 2drop first 1+ 0 2array ] }
|
|
|
|
[ call ]
|
|
|
|
} cond ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
M: char-elt prev-elt
|
|
|
|
drop [ drop -1 +col ] (prev-char) ;
|
|
|
|
|
|
|
|
M: char-elt next-elt
|
|
|
|
drop [ drop 1 +col ] (next-char) ;
|
|
|
|
|
2008-12-24 23:38:02 -05:00
|
|
|
SINGLETON: one-char-elt
|
2007-11-22 21:00:56 -05:00
|
|
|
|
|
|
|
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 )
|
2008-11-22 01:30:39 -05:00
|
|
|
pick [
|
|
|
|
[ [ first2 swap ] dip doc-line ] dip call
|
|
|
|
] dip =col ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
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 )
|
2008-12-24 23:38:02 -05:00
|
|
|
'[ blank? _ xor ] ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: (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 ;
|
|
|
|
|
2008-12-24 23:38:02 -05:00
|
|
|
SINGLETON: one-word-elt
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
M: one-word-elt prev-elt
|
|
|
|
drop
|
2008-11-22 01:30:39 -05:00
|
|
|
[ [ [ f ] dip 1- ] dip (prev-word) ] (word-elt) ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
M: one-word-elt next-elt
|
|
|
|
drop
|
2008-11-22 01:30:39 -05:00
|
|
|
[ [ f ] 2dip (next-word) ] (word-elt) ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-12-24 23:38:02 -05:00
|
|
|
SINGLETON: word-elt
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
M: word-elt prev-elt
|
|
|
|
drop
|
2008-11-22 01:30:39 -05:00
|
|
|
[ [ [ 1- ] dip ((word-elt)) (prev-word) ] (word-elt) ]
|
2007-09-20 18:09:08 -04:00
|
|
|
(prev-char) ;
|
|
|
|
|
|
|
|
M: word-elt next-elt
|
|
|
|
drop
|
|
|
|
[ [ ((word-elt)) (next-word) ] (word-elt) ]
|
|
|
|
(next-char) ;
|
|
|
|
|
2008-12-24 23:38:02 -05:00
|
|
|
SINGLETON: one-line-elt
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
M: one-line-elt prev-elt
|
|
|
|
2drop first 0 2array ;
|
|
|
|
|
|
|
|
M: one-line-elt next-elt
|
2008-11-22 01:30:39 -05:00
|
|
|
drop [ first dup ] dip doc-line length 2array ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-12-24 23:38:02 -05:00
|
|
|
SINGLETON: line-elt
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
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 ;
|
|
|
|
|
2008-12-24 23:38:02 -05:00
|
|
|
SINGLETON: doc-elt
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
M: doc-elt prev-elt 3drop { 0 0 } ;
|
|
|
|
|
|
|
|
M: doc-elt next-elt drop nip doc-end ;
|