1 ! Copyright (C) 2006, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays combinators documents fry kernel math sequences
7 GENERIC: prev-elt ( loc document elt -- newloc )
8 GENERIC: next-elt ( loc document elt -- newloc )
10 : prev/next-elt ( loc document elt -- start end )
11 [ prev-elt ] [ next-elt ] 3bi ;
13 : elt-string ( loc document elt -- string )
14 [ prev/next-elt ] [ drop ] 2bi doc-range ;
16 : set-elt-string ( string loc document elt -- )
17 [ prev/next-elt ] [ drop ] 2bi set-doc-range ;
23 : (prev-char) ( loc document quot -- loc )
25 { [ pick { 0 0 } = ] [ 2drop ] }
26 { [ pick second zero? ] [ drop [ first 1- ] dip line-end ] }
30 : (next-char) ( loc document quot -- loc )
32 { [ 2over doc-end = ] [ 2drop ] }
33 { [ 2over line-end? ] [ 2drop first 1+ 0 2array ] }
40 drop [ drop -1 +col ] (prev-char) ;
43 drop [ drop 1 +col ] (next-char) ;
45 SINGLETON: one-char-elt
47 M: one-char-elt prev-elt 2drop ;
49 M: one-char-elt next-elt 2drop ;
53 : (word-elt) ( loc document quot -- loc )
55 [ [ first2 swap ] dip doc-line ] dip call
58 : ((word-elt)) ( n seq -- ? n seq )
59 [ ?nth blank? ] 2keep ;
61 : break-detector ( ? -- quot )
62 '[ blank? _ xor ] ; inline
64 : (prev-word) ( ? col str -- col )
65 rot break-detector find-last-from drop ?1+ ;
67 : (next-word) ( ? col str -- col )
68 [ rot break-detector find-from drop ] keep
69 over not [ nip length ] [ drop ] if ;
73 SINGLETON: one-word-elt
75 M: one-word-elt prev-elt
77 [ [ [ f ] dip 1- ] dip (prev-word) ] (word-elt) ;
79 M: one-word-elt next-elt
81 [ [ f ] 2dip (next-word) ] (word-elt) ;
87 [ [ [ 1- ] dip ((word-elt)) (prev-word) ] (word-elt) ]
92 [ [ ((word-elt)) (next-word) ] (word-elt) ]
95 SINGLETON: one-line-elt
97 M: one-line-elt prev-elt
98 2drop first 0 2array ;
100 M: one-line-elt next-elt
101 drop [ first dup ] dip doc-line length 2array ;
106 2drop dup first zero? [ drop { 0 0 } ] [ -1 +line ] if ;
109 drop over first over last-line# number=
110 [ nip doc-end ] [ drop 1 +line ] if ;
114 M: doc-elt prev-elt 3drop { 0 0 } ;
116 M: doc-elt next-elt drop nip doc-end ;