]> gitweb.factorcode.org Git - factor.git/blob - basis/documents/elements/elements.factor
Fix conflict in images vocab
[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 unicode.categories accessors ;
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-char) ( loc document quot -- 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-char) ( loc document quot -- loc )
31     {
32         { [ 2over doc-end = ] [ 2drop ] }
33         { [ 2over line-end? ] [ 2drop first 1+ 0 2array ] }
34         [ call ]
35     } cond ; inline
36
37 PRIVATE>
38
39 M: char-elt prev-elt
40     drop [ drop -1 +col ] (prev-char) ;
41
42 M: char-elt next-elt
43     drop [ drop 1 +col ] (next-char) ;
44
45 SINGLETON: one-char-elt
46
47 M: one-char-elt prev-elt 2drop ;
48
49 M: one-char-elt next-elt 2drop ;
50
51 <PRIVATE
52
53 : (word-elt) ( loc document quot -- loc )
54     pick [
55         [ [ first2 swap ] dip doc-line ] dip call
56     ] dip =col ; inline
57
58 : ((word-elt)) ( 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) ] (word-elt) ;
77
78 M: one-word-elt next-elt
79     drop
80     [ f (next-word) ] (word-elt) ;
81
82 SINGLETON: word-elt
83
84 M: word-elt prev-elt
85     drop
86     [ [ [ 1- ] dip ((word-elt)) (prev-word) ] (word-elt) ]
87     (prev-char) ;
88
89 M: word-elt next-elt
90     drop
91     [ [ ((word-elt)) (next-word) ] (word-elt) ]
92     (next-char) ;
93
94 SINGLETON: one-line-elt
95
96 M: one-line-elt prev-elt
97     2drop first 0 2array ;
98
99 M: one-line-elt next-elt
100     drop [ first dup ] dip doc-line length 2array ;
101
102 TUPLE: page-elt { lines read-only } ;
103
104 C: <page-elt> page-elt
105
106 M: page-elt prev-elt
107     nip
108     2dup [ first ] [ lines>> ] bi* <
109     [ 2drop { 0 0 } ] [ lines>> neg +line ] if ;
110
111 M: page-elt next-elt
112     3dup [ first ] [ last-line# ] [ lines>> ] tri* - >
113     [ drop nip doc-end ] [ nip lines>> +line ] if ;
114
115 CONSTANT: line-elt T{ page-elt f 1 }
116
117 SINGLETON: doc-elt
118
119 M: doc-elt prev-elt 3drop { 0 0 } ;
120
121 M: doc-elt next-elt drop nip doc-end ;