1 ! Copyright (C) 2006, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs help help.topics io.pathnames io.styles
4 kernel models models.delay models.filter namespaces prettyprint
5 quotations sequences sorting source-files definitions strings
6 tools.completion tools.crossref classes.tuple vocabs words
7 vocabs.loader tools.vocabs unicode.case calendar locals
8 ui.tools.interactor ui.tools.listener ui.tools.workspace
9 ui.commands ui.gadgets ui.gadgets.editors ui.gadgets.lists
10 ui.gadgets.scrollers ui.gadgets.tracks ui.gadgets.borders
11 ui.gestures ui.operations ui ;
14 TUPLE: live-search < track field list ;
16 : search-value ( live-search -- value )
19 : search-gesture ( gesture live-search -- operation/f )
20 search-value object-operations
21 [ operation-gesture = ] with find nip ;
23 M: live-search handle-gesture ( gesture live-search -- ? )
24 tuck search-gesture dup [
25 over find-workspace hide-popup
26 [ search-value ] dip invoke-command f
31 : find-live-search ( gadget -- search )
32 [ live-search? ] find-parent ;
34 : find-search-list ( gadget -- list )
35 find-live-search list>> ;
37 TUPLE: search-field < editor ;
39 : <search-field> ( -- gadget )
40 search-field new-editor ;
43 { T{ key-down f f "UP" } [ find-search-list select-previous ] }
44 { T{ key-down f f "DOWN" } [ find-search-list select-next ] }
45 { T{ key-down f f "PAGE_UP" } [ find-search-list list-page-up ] }
46 { T{ key-down f f "PAGE_DOWN" } [ find-search-list list-page-down ] }
47 { T{ key-down f f "RET" } [ find-search-list invoke-value-action ] }
50 : <search-model> ( live-search producer -- filter )
53 ui-running? [ 1/5 seconds <delay> ] when
54 ] dip [ "\n" join ] prepend <filter> ;
56 : init-search-model ( live-search seq limited? -- live-search )
58 [ [ limited-completions ] [ completions ] ? curry <search-model> ] 3bi
61 : <search-list> ( presenter live-search -- list )
62 [ [ find-workspace hide-popup ] ] [ ] [ model>> ] tri* <list> ;
64 :: <live-search> ( string seq limited? presenter -- gadget )
65 { 0 1 } live-search new-track
66 <search-field> >>field
67 seq limited? init-search-model
68 presenter over <search-list> >>list
69 dup field>> 1 <border> { 1 1 } >>fill f track-add
70 dup list>> <scroller> 1 track-add
71 string over field>> set-editor-string
72 dup field>> end-of-document ;
74 M: live-search focusable-child* field>> ;
76 M: live-search pref-dim* drop { 400 200 } ;
78 : current-word ( workspace -- string )
79 listener>> input>> selected-word ;
81 : definition-candidates ( words -- candidates )
82 [ dup synopsis >lower ] { } map>assoc sort-values ;
84 : <definition-search> ( string words limited? -- gadget )
85 [ definition-candidates ] dip [ synopsis ] <live-search> ;
87 : word-candidates ( words -- candidates )
88 [ dup name>> >lower ] { } map>assoc ;
90 : <word-search> ( string words limited? -- gadget )
91 [ word-candidates ] dip [ synopsis ] <live-search> ;
93 : com-words ( workspace -- )
94 dup current-word all-words t <word-search>
95 "Word search" show-titled-popup ;
97 : show-vocab-words ( workspace vocab -- )
98 [ "" swap words natural-sort f <word-search> ]
99 [ "Words in " swap vocab-name append ]
100 bi show-titled-popup ;
102 : show-word-usage ( workspace word -- )
103 [ "" swap smart-usage f <definition-search> ]
104 [ "Words and methods using " swap name>> append ]
105 bi show-titled-popup ;
107 : help-candidates ( seq -- candidates )
108 [ dup >link swap article-title >lower ] { } map>assoc
111 : <help-search> ( string -- gadget )
112 all-articles help-candidates
113 f [ article-title ] <live-search> ;
115 : com-search ( workspace -- )
116 "" <help-search> "Help search" show-titled-popup ;
118 : source-file-candidates ( seq -- candidates )
119 [ dup <pathname> swap >lower ] { } map>assoc ;
121 : <source-file-search> ( string files -- gadget )
122 source-file-candidates
123 f [ string>> ] <live-search> ;
125 : all-source-files ( -- seq )
126 source-files get keys natural-sort ;
128 : com-sources ( workspace -- )
129 "" all-source-files <source-file-search>
130 "Source file search" show-titled-popup ;
132 : show-vocab-files ( workspace vocab -- )
133 [ "" swap vocab-files <source-file-search> ]
134 [ "Source files in " swap vocab-name append ]
135 bi show-titled-popup ;
137 : vocab-candidates ( -- candidates )
138 all-vocabs-seq [ dup vocab-name >lower ] { } map>assoc ;
140 : <vocab-search> ( string -- gadget )
141 vocab-candidates f [ vocab-name ] <live-search> ;
143 : com-vocabs ( workspace -- )
144 dup current-word <vocab-search>
145 "Vocabulary search" show-titled-popup ;
147 : history-candidates ( seq -- candidates )
148 [ dup <input> swap >lower ] { } map>assoc ;
150 : <history-search> ( string seq -- gadget )
152 f [ string>> ] <live-search> ;
154 : listener-history ( listener -- seq )
155 input>> history>> <reversed> ;
157 : com-history ( workspace -- )
158 "" over listener>> listener-history <history-search>
159 "History search" show-titled-popup ;
161 workspace "toolbar" f {
162 { T{ key-down f { C+ } "p" } com-history }
163 { T{ key-down f f "TAB" } com-words }
164 { T{ key-down f { C+ } "u" } com-vocabs }
165 { T{ key-down f { C+ } "e" } com-sources }
166 { T{ key-down f { C+ } "h" } com-search }