! Copyright (C) 2006, 2008 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: accessors assocs help help.topics io.files io.styles kernel models models.delay models.filter namespaces prettyprint quotations sequences sorting source-files definitions strings 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 ; IN: ui.tools.search TUPLE: live-search < track field list ; : search-value ( live-search -- value ) list>> list-value ; : search-gesture ( gesture live-search -- operation/f ) search-value object-operations [ operation-gesture = ] with find nip ; M: live-search handle-gesture ( gesture live-search -- ? ) tuck search-gesture dup [ over find-workspace hide-popup [ search-value ] dip invoke-command f ] [ 2drop t ] if ; : find-live-search ( gadget -- search ) [ live-search? ] find-parent ; : find-search-list ( gadget -- list ) find-live-search list>> ; TUPLE: search-field < editor ; : ( -- gadget ) search-field new-editor ; 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 ] } { T{ key-down f f "RET" } [ find-search-list invoke-value-action ] } } set-gestures : ( live-search producer -- filter ) [ field>> model>> ui-running? [ 1/5 seconds ] when ] dip [ "\n" join ] prepend ; : init-search-model ( live-search seq limited? -- live-search ) [ 2drop ] [ [ limited-completions ] [ completions ] ? curry ] 3bi >>model ; inline : ( presenter live-search -- list ) [ [ find-workspace hide-popup ] ] [ ] [ model>> ] tri* ; :: ( string seq limited? presenter -- gadget ) { 0 1 } live-search new-track >>field seq limited? init-search-model presenter over >>list dup field>> 1 { 1 1 } >>fill f track-add dup list>> 1 track-add string over field>> set-editor-string dup field>> end-of-document ; M: live-search focusable-child* field>> ; M: live-search pref-dim* drop { 400 200 } ; : current-word ( workspace -- string ) listener>> input>> selected-word ; : definition-candidates ( words -- candidates ) [ dup synopsis >lower ] { } map>assoc sort-values ; : ( string words limited? -- gadget ) [ definition-candidates ] dip [ synopsis ] ; : word-candidates ( words -- candidates ) [ dup name>> >lower ] { } map>assoc ; : ( string words limited? -- gadget ) [ word-candidates ] dip [ synopsis ] ; : com-words ( workspace -- ) dup current-word all-words t "Word search" show-titled-popup ; : show-vocab-words ( workspace vocab -- ) [ "" swap words natural-sort f ] [ "Words in " swap vocab-name append ] bi show-titled-popup ; : show-word-usage ( workspace word -- ) [ "" swap smart-usage f ] [ "Words and methods using " swap name>> append ] bi show-titled-popup ; : help-candidates ( seq -- candidates ) [ dup >link swap article-title >lower ] { } map>assoc sort-values ; : ( string -- gadget ) all-articles help-candidates f [ article-title ] ; : com-search ( workspace -- ) "" "Help search" show-titled-popup ; : source-file-candidates ( seq -- candidates ) [ dup swap >lower ] { } map>assoc ; : ( string files -- gadget ) source-file-candidates f [ string>> ] ; : all-source-files ( -- seq ) source-files get keys natural-sort ; : com-sources ( workspace -- ) "" all-source-files "Source file search" show-titled-popup ; : show-vocab-files ( workspace vocab -- ) [ "" swap vocab-files ] [ "Source files in " swap vocab-name append ] bi show-titled-popup ; : vocab-candidates ( -- candidates ) all-vocabs-seq [ dup vocab-name >lower ] { } map>assoc ; : ( string -- gadget ) vocab-candidates f [ vocab-name ] ; : com-vocabs ( workspace -- ) dup current-word "Vocabulary search" show-titled-popup ; : history-candidates ( seq -- candidates ) [ dup swap >lower ] { } map>assoc ; : ( string seq -- gadget ) history-candidates f [ string>> ] ; : listener-history ( listener -- seq ) input>> history>> ; : com-history ( workspace -- ) "" over listener>> listener-history "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