factor/core/ui/tools/search.factor

185 lines
5.0 KiB
Factor
Raw Normal View History

! Copyright (C) 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
IN: gadgets-search
2006-10-22 18:46:02 -04:00
USING: arrays gadgets gadgets-labels gadgets-panes
gadgets-scrolling gadgets-text gadgets-theme
generic help tools kernel models sequences words
2006-11-17 18:11:35 -05:00
gadgets-borders gadgets-lists gadgets-workspace gadgets-listener
namespaces parser hashtables io completion styles strings
2006-11-18 03:51:34 -05:00
modules prettyprint ;
TUPLE: live-search field list ;
2006-10-05 02:10:49 -04:00
2006-11-27 00:30:57 -05:00
: search-gesture ( gesture live-search -- command/f )
live-search-list list-value object-operations
[ command-gesture = ] find-with nip ;
M: live-search handle-gesture* ( gadget gesture delegate -- ? )
drop over search-gesture dup [
over find-workspace hide-popup
>r live-search-list list-value r> invoke-command f
] [
2drop t
] if ;
2006-11-17 04:37:28 -05:00
: find-live-search [ [ live-search? ] is? ] find-parent ;
2006-10-05 02:10:49 -04:00
: find-search-list find-live-search live-search-list ;
TUPLE: search-field ;
C: search-field ( -- gadget )
2006-10-05 02:10:49 -04:00
<editor> over set-gadget-delegate
dup dup set-control-self
2006-10-07 14:36:32 -04:00
[ editor-doc-end ] keep ;
2006-10-05 02:10:49 -04:00
search-field H{
{ T{ key-down f f "UP" } [ find-search-list select-prev ] }
{ T{ key-down f f "DOWN" } [ find-search-list select-next ] }
2006-11-17 04:37:28 -05:00
{ T{ key-down f f "RETURN" } [ find-search-list list-action ] }
2006-10-05 02:10:49 -04:00
} set-gestures
: <search-model> ( producer -- model )
gadget get live-search-field control-model 200 <delay>
[ "\n" join ] <filter>
swap <filter> ;
2006-11-17 18:11:35 -05:00
: <search-list> ( seq producer presenter -- gadget )
-rot curry <search-model>
[ find-workspace hide-popup ] -rot
2006-11-17 18:11:35 -05:00
<list> ;
2006-11-17 18:11:35 -05:00
C: live-search ( string seq producer presenter -- gadget )
2006-10-05 02:10:49 -04:00
{
{
[ <search-field> ]
set-live-search-field
f
@top
}
{
[ <search-list> ]
set-live-search-list
[ <scroller> ]
@center
}
} make-frame*
2006-11-17 18:11:35 -05:00
[ live-search-field set-editor-text ] keep
2006-11-28 21:57:29 -05:00
[ live-search-field editor-doc-end ] keep ;
2006-10-05 02:10:49 -04:00
M: live-search focusable-child* live-search-field ;
2006-11-27 00:30:57 -05:00
: <word-search> ( string words -- gadget )
[ word-completions ]
2006-11-18 03:51:34 -05:00
[ summary ]
2006-11-27 00:30:57 -05:00
<live-search> ;
2006-10-05 17:15:41 -04:00
: help-completions ( str pairs -- seq )
>r >lower r>
[ second >lower ] swap completions
[ first <link> ] map ;
2006-11-27 00:30:57 -05:00
: <help-search> ( string -- gadget )
all-articles [ dup article-title 2array ] map sort-values
[ help-completions ]
[ article-title ]
2006-11-27 00:30:57 -05:00
<live-search> ;
2006-11-27 00:30:57 -05:00
: <source-file-search> ( string files -- gadget )
[ string-completions [ <pathname> ] map ]
[ pathname-string ]
2006-11-27 00:30:57 -05:00
<live-search> ;
: module-completions ( str modules -- seq )
[ module-name ] swap completions ;
2006-11-27 00:30:57 -05:00
: <module-search> ( string -- gadget )
available-modules [ module-completions ]
2006-11-18 03:51:34 -05:00
[ module-string ]
2006-11-27 00:30:57 -05:00
<live-search> ;
2006-10-06 17:07:13 -04:00
2006-11-27 00:30:57 -05:00
: <vocab-search> ( string -- gadget )
vocabs [ string-completions [ <vocab-link> ] map ]
[ vocab-link-name ]
2006-11-27 00:30:57 -05:00
<live-search> ;
2006-11-12 23:38:44 -05:00
2006-11-27 00:30:57 -05:00
: <history-search> ( string seq -- gadget )
[ string-completions [ <input> ] map ]
[ input-string ]
2006-11-27 00:30:57 -05:00
<live-search> ;
2006-11-17 18:11:35 -05:00
: workspace-listener ( workspace -- listener )
listener-gadget swap find-tool tool-gadget nip ;
: current-word ( workspace -- string )
workspace-listener listener-gadget-input selected-word ;
: show-word-search ( workspace words -- )
>r dup current-word r> <word-search>
"Word search" show-titled-popup ;
: show-vocab-words ( workspace vocab -- )
"" over words natural-sort <word-search>
2006-11-17 18:11:35 -05:00
"Words in " rot append show-titled-popup ;
: show-help-search ( workspace -- )
"" <help-search> "Help search" show-titled-popup ;
: all-source-files ( -- seq )
source-files get hash-keys natural-sort ;
: show-source-file-search ( workspace -- )
"" all-source-files <source-file-search>
"Source file search" show-titled-popup ;
: show-module-files ( workspace module -- )
2006-11-18 03:51:34 -05:00
"" over module-files* <source-file-search>
2006-11-17 18:11:35 -05:00
"Source files in " rot module-name append show-titled-popup ;
: show-vocab-search ( workspace -- )
dup current-word <vocab-search>
"Vocabulary search" show-titled-popup ;
: show-module-search ( workspace -- )
"" <module-search> "Module search" show-titled-popup ;
: listener-history ( listener -- seq )
listener-gadget-input interactor-history <reversed> ;
: show-history ( workspace -- )
2006-11-21 04:05:59 -05:00
"" over workspace-listener listener-history <history-search>
2006-11-17 18:11:35 -05:00
"History search" show-titled-popup ;
workspace "toolbar" {
{
"History"
T{ key-down f { C+ } "p" }
[ show-history ]
}
{
"Words"
T{ key-down f f "TAB" }
[ all-words show-word-search ]
}
{
"Vocabularies"
T{ key-down f { C+ } "u" }
[ show-vocab-search ]
}
{
"Modules"
T{ key-down f { C+ } "m" }
[ show-module-search ]
}
{
"Sources"
T{ key-down f { C+ } "e" }
[ show-source-file-search ]
}
{
"Search help"
T{ key-down f { C+ } "h" }
[ show-help-search ]
}
} define-commands