1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs fry help.markup help.topics io
4 kernel make math math.parser namespaces sequences sorting
5 summary tools.completion vocabs.hierarchy help.vocabs
6 vocabs words unicode.case help unicode.categories
10 : $completions ( seq -- )
11 dup [ word? ] all? [ words-table ] [
12 dup [ vocab-spec? ] all? [
15 [ <$pretty-link> 1array ] map $table
19 SYMBOLS: word-result vocabulary-result article-result ;
21 : category>title ( category -- name )
23 { word-result [ "Words" ] }
24 { vocabulary-result [ "Vocabularies" ] }
25 { article-result [ "Help articles" ] }
28 : category>name ( category -- name )
30 { word-result [ "word" ] }
31 { vocabulary-result [ "vocabulary" ] }
32 { article-result [ "help article" ] }
35 TUPLE: more-completions seq search category ;
37 CONSTANT: max-completions 5
39 M: more-completions valid-article? drop t ;
41 M: more-completions article-title
44 [ seq>> length # " " % ]
45 [ category>> category>name % ]
46 [ " results for “" % search>> % "”" % ] tri
49 M: more-completions article-content
50 seq>> [ second >lower ] sort-with keys \ $completions prefix ;
52 :: (apropos) ( search completions category -- element )
56 [ max-completions short head keys \ $completions prefix , ]
58 length max-completions >
59 [ { $link T{ more-completions f completions search category } } , ] when
64 : articles-matching ( str -- seq )
66 [ [ >link ] [ title>> ] bi* ] { } assoc-map-as
71 [ dup words-matching word-result (apropos) ]
72 [ dup vocabs-matching vocabulary-result (apropos) ]
73 [ dup articles-matching article-result (apropos) ]
74 tri 3array print-element ;
76 TUPLE: apropos-search text ;
78 C: <apropos-search> apropos-search
80 M: apropos-search valid-article? drop t ;
82 M: apropos-search article-title
83 text>> "Search results for “" "”" surround ;
85 M: apropos-search article-content
86 text>> 1array \ $apropos prefix ;
88 M: apropos-search >link ;
90 INSTANCE: apropos-search topic
93 [ blank? ] trim <apropos-search> print-topic ;