]> gitweb.factorcode.org Git - factor.git/blob - basis/documents/elements/elements.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[factor.git] / basis / documents / elements / elements.factor
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
4 accessors unicode.categories unicode.breaks combinators.short-circuit ;
5 IN: documents.elements
6
7 GENERIC: prev-elt ( loc document elt -- newloc )
8 GENERIC: next-elt ( loc document elt -- newloc )
9
10 : prev/next-elt ( loc document elt -- start end )
11     [ prev-elt ] [ next-elt ] 3bi ;
12
13 : elt-string ( loc document elt -- string )
14     [ prev/next-elt ] [ drop ] 2bi doc-range ;
15
16 : set-elt-string ( string loc document elt -- )
17     [ prev/next-elt ] [ drop ] 2bi set-doc-range ;
18
19 SINGLETON: char-elt
20
21 <PRIVATE
22
23 : prev ( loc document quot: ( loc document -- loc ) -- loc )
24     {
25         { [ pick { 0 0 } = ] [ 2drop ] }
26         { [ pick second zero? ] [ drop [ first 1 - ] dip line-end ] }
27         [ call ]
28     } cond ; inline
29
30 : next ( loc document quot: ( loc document -- loc ) -- loc )
31     {
32         { [ 2over doc-end = ] [ 2drop ] }
33         { [ 2over line-end? ] [ 2drop first 1 + 0 2array ] }
34         [ call ]
35     } cond ; inline
36
37 : modify-col ( loc document quot: ( col str -- col' ) -- loc )
38     pick [
39         [ [ first2 swap ] dip doc-line ] dip call
40     ] dip =col ; inline
41
42 PRIVATE>
43
44 M: char-elt prev-elt
45     drop [ [ last-grapheme-from ] modify-col ] prev ;
46
47 M: char-elt next-elt
48     drop [ [ first-grapheme-from ] modify-col ] next ;
49
50 SINGLETON: one-char-elt
51
52 M: one-char-elt prev-elt 2drop ;
53
54 M: one-char-elt next-elt 2drop ;
55
56 <PRIVATE
57
58 : blank-at? ( n seq -- n seq ? )
59     2dup ?nth blank? ;
60
61 : break-detector ( ? -- quot )
62     '[ blank? _ xor ] ; inline
63
64 : prev-word ( col str ? -- col )
65     break-detector find-last-from drop ?1+ ;
66
67 : next-word ( col str ? -- col )
68     [ break-detector find-from drop ] [ drop length ] 2bi or ;
69
70 PRIVATE>
71
72 SINGLETON: one-word-elt
73
74 M: one-word-elt prev-elt
75     drop
76     [ [ 1 - ] dip f prev-word ] modify-col ;
77
78 M: one-word-elt next-elt
79     drop
80     [ f next-word ] modify-col ;
81
82 SINGLETON: word-start-elt
83
84 M: word-start-elt prev-elt
85     drop one-word-elt prev-elt ;
86
87 M: word-start-elt next-elt 2drop ;
88
89 SINGLETON: word-elt
90
91 M: word-elt prev-elt
92     drop
93     [ [ [ 1 - ] dip blank-at? prev-word ] modify-col ]
94     prev ;
95
96 M: word-elt next-elt
97     drop
98     [ [ blank-at? next-word ] modify-col ]
99     next ;
100
101 SINGLETON: one-line-elt
102
103 M: one-line-elt prev-elt
104     2drop first 0 2array ;
105
106 M: one-line-elt next-elt
107     drop [ first dup ] dip doc-line length 2array ;
108
109 TUPLE: page-elt { lines read-only } ;
110
111 C: <page-elt> page-elt
112
113 M: page-elt prev-elt
114     nip
115     2dup [ first ] [ lines>> ] bi* <
116     [ 2drop { 0 0 } ] [ lines>> neg +line ] if ;
117
118 M: page-elt next-elt
119     3dup [ first ] [ last-line# ] [ lines>> ] tri* - >
120     [ drop nip doc-end ] [ nip lines>> +line ] if ;
121
122 CONSTANT: line-elt T{ page-elt f 1 }
123
124 SINGLETON: doc-elt
125
126 M: doc-elt prev-elt 3drop { 0 0 } ;
127
128 M: doc-elt next-elt drop nip doc-end ;