2 USING: arrays kernel math sequences strings models ;
4 GENERIC: prev-elt ( loc document elt -- newloc )
5 GENERIC: next-elt ( loc document elt -- newloc )
9 : (prev-char) ( loc document quot -- loc )
11 { [ over { 0 0 } = ] [ drop ] }
12 { [ over second zero? ] [ >r first 1- r> line-end ] }
13 { [ t ] [ pick call ] }
16 : (next-char) ( loc document quot -- loc )
18 { [ 2dup doc-end = ] [ drop ] }
19 { [ 2dup line-end? ] [ drop first 1+ 0 2array ] }
20 { [ t ] [ pick call ] }
24 drop [ drop -1 +col ] (prev-char) ;
27 drop [ drop 1 +col ] (next-char) ;
29 : (word-elt) ( loc document quot -- loc )
31 >r >r first2 swap r> doc-line r> call
34 : ((word-elt)) [ ?nth blank? ] 2keep ;
36 : (prev-word) ( ? col str -- col )
37 [ blank? xor ] find-last-with* drop 1+ ;
39 : (next-word) ( ? col str -- col )
40 [ [ blank? xor ] find-with* drop ] keep
41 over -1 = [ nip length ] [ drop ] if ;
45 M: one-word-elt prev-elt
47 [ [ f -rot >r 1- r> (prev-word) ] (word-elt) ] (prev-char) ;
49 M: one-word-elt next-elt
51 [ [ f -rot (next-word) ] (word-elt) ] (next-char) ;
57 [ [ >r 1- r> ((word-elt)) (prev-word) ] (word-elt) ]
62 [ [ ((word-elt)) (next-word) ] (word-elt) ]
67 M: one-line-elt prev-elt
68 2drop first 0 2array ;
70 M: one-line-elt next-elt
71 drop >r first dup r> doc-line length 2array ;
75 M: line-elt prev-elt 2drop -1 +line ;
77 M: line-elt next-elt 2drop 1 +line ;
81 M: doc-elt prev-elt 3drop { 0 0 } ;
83 M: doc-elt next-elt drop nip doc-end ;