factor/basis/ui/tools/search/search.factor

168 lines
5.3 KiB
Factor
Raw Normal View History

2008-02-21 21:57:41 -05:00
! Copyright (C) 2006, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs help help.topics io.pathnames io.styles
2008-11-20 22:58:30 -05:00
kernel models models.delay models.filter namespaces prettyprint
2008-07-04 18:58:37 -04:00
quotations sequences sorting source-files definitions strings
2008-11-20 22:58:30 -05:00
tools.completion tools.crossref classes.tuple vocabs words
vocabs.loader tools.vocabs unicode.case calendar locals
ui.tools.interactor ui.tools.listener ui.tools.workspace
ui.commands ui.gadgets ui.gadgets.editors ui.gadgets.lists
ui.gadgets.scrollers ui.gadgets.tracks ui.gadgets.borders
ui.gestures ui.operations ui ;
2007-09-20 18:09:08 -04:00
IN: ui.tools.search
TUPLE: live-search < track field list ;
2007-09-20 18:09:08 -04:00
: search-value ( live-search -- value )
2008-09-01 04:40:31 -04:00
list>> list-value ;
2007-09-20 18:09:08 -04:00
: search-gesture ( gesture live-search -- operation/f )
search-value object-operations
2008-01-09 17:36:30 -05:00
[ operation-gesture = ] with find nip ;
2007-09-20 18:09:08 -04:00
2008-08-23 00:27:25 -04:00
M: live-search handle-gesture ( gesture live-search -- ? )
tuck search-gesture dup [
2007-09-20 18:09:08 -04:00
over find-workspace hide-popup
2008-11-20 22:58:30 -05:00
[ search-value ] dip invoke-command f
2007-09-20 18:09:08 -04:00
] [
2drop t
] if ;
2008-06-08 16:32:55 -04:00
: find-live-search ( gadget -- search )
2008-08-23 00:20:49 -04:00
[ live-search? ] find-parent ;
2007-09-20 18:09:08 -04:00
2008-06-08 16:32:55 -04:00
: find-search-list ( gadget -- list )
2008-09-01 04:40:31 -04:00
find-live-search list>> ;
2007-09-20 18:09:08 -04:00
TUPLE: search-field < editor ;
2007-09-20 18:09:08 -04:00
2007-12-14 01:16:47 -05:00
: <search-field> ( -- gadget )
search-field new-editor ;
2007-09-20 18:09:08 -04:00
search-field H{
{ T{ key-down f f "UP" } [ find-search-list select-previous ] }
{ T{ key-down f f "DOWN" } [ find-search-list select-next ] }
{ T{ key-down f f "PAGE_UP" } [ find-search-list list-page-up ] }
{ T{ key-down f f "PAGE_DOWN" } [ find-search-list list-page-down ] }
2007-09-20 18:09:08 -04:00
{ T{ key-down f f "RET" } [ find-search-list invoke-value-action ] }
} set-gestures
2008-11-20 22:58:30 -05:00
: <search-model> ( live-search producer -- filter )
[
field>> model>>
ui-running? [ 1/5 seconds <delay> ] when
] dip [ "\n" join ] prepend <filter> ;
2007-09-20 18:09:08 -04:00
2008-11-20 22:58:30 -05:00
: init-search-model ( live-search seq limited? -- live-search )
[ 2drop ]
[ [ limited-completions ] [ completions ] ? curry <search-model> ] 3bi
>>model ; inline
2007-09-20 18:09:08 -04:00
2008-11-20 22:58:30 -05:00
: <search-list> ( presenter live-search -- list )
[ [ find-workspace hide-popup ] ] [ ] [ model>> ] tri* <list> ;
:: <live-search> ( string seq limited? presenter -- gadget )
{ 0 1 } live-search new-track
<search-field> >>field
2008-11-20 22:58:30 -05:00
seq limited? init-search-model
presenter over <search-list> >>list
2008-11-20 23:13:24 -05:00
dup field>> 1 <border> { 1 1 } >>fill f track-add
dup list>> <scroller> 1 track-add
2008-11-20 22:58:30 -05:00
string over field>> set-editor-string
dup field>> end-of-document ;
2007-09-20 18:09:08 -04:00
2008-09-01 04:40:31 -04:00
M: live-search focusable-child* field>> ;
2007-09-20 18:09:08 -04:00
M: live-search pref-dim* drop { 400 200 } ;
: current-word ( workspace -- string )
2008-09-01 04:40:31 -04:00
listener>> input>> selected-word ;
2007-09-20 18:09:08 -04:00
: definition-candidates ( words -- candidates )
[ dup synopsis >lower ] { } map>assoc sort-values ;
: <definition-search> ( string words limited? -- gadget )
2008-11-20 22:58:30 -05:00
[ definition-candidates ] dip [ synopsis ] <live-search> ;
2007-09-20 18:09:08 -04:00
: word-candidates ( words -- candidates )
[ dup name>> >lower ] { } map>assoc ;
2007-09-20 18:09:08 -04:00
: <word-search> ( string words limited? -- gadget )
2008-11-20 22:58:30 -05:00
[ word-candidates ] dip [ synopsis ] <live-search> ;
2007-09-20 18:09:08 -04:00
: com-words ( workspace -- )
dup current-word all-words t <word-search>
"Word search" show-titled-popup ;
: show-vocab-words ( workspace vocab -- )
2008-11-20 22:58:30 -05:00
[ "" swap words natural-sort f <word-search> ]
[ "Words in " swap vocab-name append ]
bi show-titled-popup ;
2007-09-20 18:09:08 -04:00
: show-word-usage ( workspace word -- )
2008-11-20 22:58:30 -05:00
[ "" swap smart-usage f <definition-search> ]
[ "Words and methods using " swap name>> append ]
bi show-titled-popup ;
2007-09-20 18:09:08 -04:00
: help-candidates ( seq -- candidates )
[ dup >link swap article-title >lower ] { } map>assoc
sort-values ;
: <help-search> ( string -- gadget )
all-articles help-candidates
f [ article-title ] <live-search> ;
: com-search ( workspace -- )
"" <help-search> "Help search" show-titled-popup ;
: source-file-candidates ( seq -- candidates )
[ dup <pathname> swap >lower ] { } map>assoc ;
: <source-file-search> ( string files -- gadget )
source-file-candidates
2008-08-31 17:17:46 -04:00
f [ string>> ] <live-search> ;
2007-09-20 18:09:08 -04:00
: all-source-files ( -- seq )
source-files get keys natural-sort ;
: com-sources ( workspace -- )
"" all-source-files <source-file-search>
"Source file search" show-titled-popup ;
: show-vocab-files ( workspace vocab -- )
2008-11-20 22:58:30 -05:00
[ "" swap vocab-files <source-file-search> ]
[ "Source files in " swap vocab-name append ]
bi show-titled-popup ;
2007-09-20 18:09:08 -04:00
: vocab-candidates ( -- candidates )
all-vocabs-seq [ dup vocab-name >lower ] { } map>assoc ;
: <vocab-search> ( string -- gadget )
vocab-candidates f [ vocab-name ] <live-search> ;
: com-vocabs ( workspace -- )
dup current-word <vocab-search>
"Vocabulary search" show-titled-popup ;
: history-candidates ( seq -- candidates )
[ dup <input> swap >lower ] { } map>assoc ;
: <history-search> ( string seq -- gadget )
history-candidates
2008-08-31 17:17:46 -04:00
f [ string>> ] <live-search> ;
2007-09-20 18:09:08 -04:00
: listener-history ( listener -- seq )
2008-09-01 04:40:31 -04:00
input>> history>> <reversed> ;
2007-09-20 18:09:08 -04:00
: com-history ( workspace -- )
2008-09-01 04:40:31 -04:00
"" over listener>> listener-history <history-search>
2007-09-20 18:09:08 -04:00
"History search" show-titled-popup ;
workspace "toolbar" f {
{ T{ key-down f { C+ } "p" } com-history }
{ T{ key-down f f "TAB" } com-words }
{ T{ key-down f { C+ } "u" } com-vocabs }
{ T{ key-down f { C+ } "e" } com-sources }
{ T{ key-down f { C+ } "h" } com-search }
} define-command-map