1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs colors.constants combinators
4 combinators.short-circuit fry io.directories io.files
5 io.files.info io.pathnames kernel locals make math math.order
6 sequences sequences.private sorting splitting
7 splitting.monotonic unicode unicode.data vectors vocabs
13 : smart-index-from ( obj i seq -- n/f )
14 rot [ ch>lower ] [ ch>upper ] bi
15 '[ dup _ eq? [ drop t ] [ _ eq? ] if ] find-from drop ;
17 :: (fuzzy) ( accum i full ch -- accum i ? )
18 ch i full smart-index-from [
20 [ accum swap 1 + t ] bi
27 : fuzzy ( full short -- indices )
28 dup [ length <vector> 0 ] curry 2dip
29 [ (fuzzy) ] with all? 2drop ;
31 : runs ( seq -- newseq )
32 [ 1 - = ] monotonic-split-slice ;
36 : score-1 ( i full -- n )
38 { [ over zero? ] [ 2drop 10 ] }
39 { [ 2dup length 1 - number= ] [ 2drop 4 ] }
40 { [ 2dup [ 1 - ] dip nth-unsafe Letter? not ] [ 2drop 10 ] }
41 { [ 2dup [ 1 + ] dip nth-unsafe Letter? not ] [ 2drop 4 ] }
47 : score ( full fuzzy -- n )
49 [ [ length ] bi@ - 15 swap [-] 3 /f ] 2keep
51 [ 0 [ pick score-1 max ] reduce nip ] keep
58 : rank-completions ( results -- newresults )
59 [ 0 [ first max ] reduce 4 /f ] keep
61 [ [ first-unsafe < ] with filter ] [ nip ] if
62 sort-keys <reversed> values ;
64 : complete ( full short -- score )
65 2dup [ <reversed> ] bi@ [ dupd fuzzy score ] 2bi@ max ;
67 : completion ( short candidate -- result )
68 [ second swap complete ] keep 2array ; inline
70 : completion, ( short candidate -- )
71 completion dup first-unsafe 0 > [ , ] [ drop ] if ;
73 : completions ( short candidates -- seq )
75 [ [ completion, ] with each ] { } make
79 : named ( seq -- seq' )
80 [ dup name>> ] { } map>assoc ;
82 : vocabs-matching ( str -- seq )
83 all-disk-vocabs-recursive filter-vocabs named completions ;
85 : vocab-words-matching ( str vocab -- seq )
86 vocab-words named completions ;
88 : qualified-named ( str -- seq/f )
90 drop vocabs-matching keys [
91 [ vocab-words ] [ vocab-name ] bi ":" append
92 [ over name>> append ] curry { } map>assoc
96 : words-matching ( str -- seq )
98 [ qualified-named [ append ] unless-empty ] bi
101 : chars-matching ( str -- seq )
102 name-map keys dup zip completions ;
104 : colors-matching ( str -- seq )
105 named-colors dup zip completions ;
107 : strings-matching ( str seq -- seq' )
108 dup zip completions keys ;
112 : directory-paths ( directory -- alist )
115 [ name>> dup _ prepend-path ]
116 [ directory? [ path-separator append ] when ]
119 ] with-directory-entries ;
123 : paths-matching ( str -- seq )
124 dup last-path-separator [ 1 + cut ] [ drop "" ] if swap
125 dup { [ file-exists? ] [ file-info directory? ] } 1&&
126 [ directory-paths completions ] [ 2drop { } ] if ;
130 : (complete-single-vocab?) ( str -- ? )
132 "IN:" "USE:" "UNUSE:" "QUALIFIED:"
133 "QUALIFIED-WITH:" "FROM:" "EXCLUDE:"
136 : complete-single-vocab? ( tokens -- ? )
138 harvest ?last (complete-single-vocab?)
140 harvest dup length 1 >
141 [ 2 tail* ?first (complete-single-vocab?) ] [ drop f ] if
144 : chop-; ( seq -- seq' )
145 { ";" } split1-last [ ] [ ] ?if ;
147 : complete-vocab-list? ( tokens -- ? )
148 chop-; 1 short head* "USING:" swap member? ;
152 : complete-vocab? ( tokens -- ? )
153 { [ complete-single-vocab? ] [ complete-vocab-list? ] } 1|| ;
155 : complete-vocab-words? ( tokens -- ? )
158 [ first { "FROM:" "EXCLUDE:" } member? ]
164 : complete-token? ( tokens token -- ? )
166 [ harvest ?last ] [ = ] bi*
168 swap harvest dup length 1 >
169 [ 2 tail* ?first = ] [ 2drop f ] if
174 : complete-char? ( tokens -- ? ) "CHAR:" complete-token? ;
176 : complete-color? ( tokens -- ? ) "COLOR:" complete-token? ;
178 : complete-pathname? ( tokens -- ? ) "P\"" complete-token? ;