home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Search.pm < prev    next >
Encoding:
Perl POD Document  |  2003-09-16  |  5.3 KB  |  243 lines

  1. package Tk::Pod::Search;
  2.  
  3. use strict;
  4. use vars qw(@ISA $VERSION);
  5.  
  6. $VERSION = substr q$Revision: 1.2 $, 10 . "";
  7.  
  8. use Carp;
  9. use Tk::Frame;
  10.  
  11. Construct Tk::Widget 'PodSearch';
  12. @ISA = 'Tk::Frame';
  13.  
  14.  
  15. #sub ClassInit {
  16. #    my ($class,$mw) = @_;
  17. #
  18. #}
  19.  
  20. sub Populate {
  21.     my ($cw, $args) = @_;
  22.  
  23.     my $Entry;
  24.     eval {
  25.     require Tk::HistEntry;
  26.     $Entry = "HistEntry";
  27.     };
  28.     if ($@) {
  29.     require Tk::BrowseEntry;
  30.     $Entry = "BrowseEntry";
  31.     }
  32.  
  33.     my $l = $cw->Scrolled('Listbox',-scrollbars=>$Tk::platform eq 'MSWin32'?'e':'w');
  34.     #xxx BrowseEntry V1.3 does not honour -label at creation time :-(
  35.     #my $e = $cw->BrowseEntry(-labelPack=>[-side=>'left'],-label=>'foo',
  36.     #-listcmd=> ['_logit', 'list'],
  37.     #-browsecmd=> ['_logit', 'browse'],
  38.     #);
  39.     my $e = $cw->$Entry();
  40.     my $s = $cw->Label();
  41.  
  42.     $l->pack(-fill=>'both', -side=>'top',  -expand=>1);
  43.     $s->pack(-anchor => 'e', -side=>'left');
  44.     $e->pack(-fill=>'x', -side=>'left', -expand=>1);
  45.  
  46.     $cw->Advertise( 'entry'    => $e->Subwidget('entry')   );
  47.     $cw->Advertise( 'listbox'    => $l->Subwidget('listbox') );
  48.     $cw->Advertise( 'browse'    => $e);
  49.  
  50.     $cw->Delegates(
  51.         'focus' => $cw->Subwidget('entry'),
  52.         );
  53.  
  54.     $cw->ConfigSpecs(
  55.         -label =>    [{-text=>$s}, 'label',    'Label',    'Search:'],
  56.         -indexdir =>    ['PASSIVE',   'indexDir', 'IndexDir', undef],
  57.         -command =>    ['CALLBACK',  undef,      undef,      undef],
  58.         -search =>    ['METHOD',    'search',   'Search',   ""],
  59.         'DEFAULT' =>    [ $cw ],
  60.         );
  61.  
  62.     foreach (qw/Return space 1/) {
  63.     $cw->Subwidget('listbox')->bind("<$_>", [\&_load_pod, $cw]);
  64.     }
  65.     $cw->Subwidget('entry')->bind('<Return>',[\&_search,$cw,$l]);
  66.  
  67.     undef;
  68. }
  69.  
  70. sub addHistory {
  71.     my ($w, $obj) = @_;
  72.  
  73.     my $entry_or_browse = $w->Subwidget('browse');
  74.     if ($entry_or_browse->can('historyAdd')) {
  75.     $entry_or_browse->historyAdd($obj);
  76.     } else {
  77.     $entry_or_browse->insert(0,$obj);
  78.     }
  79. }
  80.  
  81. sub _logit { print "logit=|", join('|',@_),"|\n"; }
  82.  
  83. sub search {
  84.     my $cw = shift;
  85.     my $e = $cw->Subwidget('entry');
  86.     if (@_) {
  87.     my $search = shift;
  88.     $search = join(' ', @$search) if ref($search) eq 'ARRAY';
  89.         $e->delete(0,'end');
  90.         $e->insert(0,$search);
  91.         return undef;
  92.     } else {
  93.     return $e->get;
  94.     }
  95. }
  96.  
  97. sub _load_pod {
  98.     my $l = shift;
  99.     my $cw = shift;
  100.  
  101.     my $pod = pretty2path( $l->get(($l->curselection)[0]));
  102.  
  103.     $cw->Callback('-command', $pod, -searchterm => $cw->search());
  104. }
  105.  
  106.  
  107. sub _search {
  108.     my $e = shift;
  109.     my $w = shift;
  110.     my $l = shift;
  111.  
  112.     my $find = $e->get;
  113.     $w->addHistory($find) if $find ne '';
  114.  
  115.     require Tk::Pod::Search_db;
  116.  
  117.     #xxx: always open/close DBM files???
  118.     my $idx = Tk::Pod::Search_db->new($w->{Configure}{-indexdir});
  119.     my @hits = $idx->searchWords($find);
  120.     if (@hits) {
  121.     $l->delete(0,'end');
  122.         while (@hits) {
  123.         $l->insert('end', sprintf("%6.3f  %s", shift @hits,
  124.              path2pretty($idx->prefix . '/'. shift(@hits)) )
  125.             );
  126.         }
  127.     $l->see(0);
  128.     $l->activate(0);
  129.     } else {
  130.     my $msg = "No Pod documentation in Library matches: '$find'";
  131.     $e->messageBox(-icon => "error",
  132.                -title => "No match",
  133.                -message => $msg);
  134.     die $msg;
  135.     }
  136. }
  137.  
  138. # Converts  /where/ever/it/it/Mod/Sub/Name.pm
  139. # to        Mod/Sub/Name.pm   (/where/ever/it/is)
  140. # and vice versa.  Assumes that module subdirectories
  141. # start with an upper case char. (xxx: Better solution
  142. # when perlindex gives more infos.
  143.  
  144. sub path2pretty {
  145.     my @path = split '/', shift, -1;
  146. #    shift @path if $path[0] eq "";    # due to leading /
  147.     my $pretty = pop(@path);
  148.     while (@path) {
  149.         last if $path[-1] !~ /^[A-Z]/;
  150.     $pretty = pop(@path) . '/' . $pretty;
  151.     }
  152.     #xxx is there a min 40c_or_more format directive?
  153.     sprintf "%-40s (%s)", $pretty, join('/',@path);
  154. }
  155.  
  156. sub pretty2path {
  157.     local($_) = shift;
  158.     /([^\s]+) \s+\( (.*) \)/x;
  159.     $2 . '/' . $1;
  160. }
  161.  
  162. #$path = '/where/ever/it/is/Tk/Pod.pm';    print "orig|",$path, "|\n";
  163. #$nice = path2pretty $path;        print "nice|",$nice, "|\n";
  164. #$path =  pretty2path $nice;        print "path|",$path, "|\n";
  165.  
  166.  
  167. 1;
  168. __END__
  169.  
  170. =head1 NAME
  171.  
  172. Tk::Pod::Search - Widget to access perlindex Pod full text index
  173.  
  174. =for section General Purpose Widget
  175.  
  176. =head1 SYNOPSIS
  177.  
  178.     use Tk::Pod::Search;
  179.     ...
  180.     $widget = $parent->PodSearch( ... );
  181.     ...
  182.     $widget->configure( -search => WORDS_TO_SEARCH );
  183.  
  184.  
  185. =head1 DESCRIPTION
  186.  
  187. GUI interface to the full Pod text indexer B<perlindex>.
  188.  
  189. =head1 OPTIONS
  190.  
  191. =over 4
  192.  
  193. =item B<Class:> Search
  194.  
  195. =item B<Member:> search
  196.  
  197. =item B<Option:> -search
  198.  
  199. Expects a list of words (or a whitespace seperated list).
  200.  
  201. =item B<Class:> undef
  202.  
  203. =item B<Member:> undef
  204.  
  205. =item B<Option:> -command
  206.  
  207. Defines a call back that is called when the use selects
  208. a Pod file. It gets the full path name of the Pod file
  209. as argument.
  210.  
  211. =back
  212.  
  213.  
  214. =head1 METHODS
  215.  
  216. =over 4
  217.  
  218. =item I<$widget>->B<method1>I<(...,?...?)>
  219.  
  220. =back
  221.  
  222.  
  223. =head1 SEE ALSO
  224.  
  225. Tk::Pod::Text, tkpod, perlindex, Tk::Pod, Tk::Pod::Search_db
  226.  
  227. =head1 KEYWORDS
  228.  
  229. widget, tk, pod, search, full text
  230.  
  231. =head1 AUTHOR
  232.  
  233. Achim Bohnet <F<ach@mpe.mpg.de>>
  234.  
  235. Current maintainer is Slaven Rezic <F<slaven@rezic.de>>.
  236.  
  237. Copyright (c) 1997-1998 Achim Bohnet. All rights reserved.  This program
  238. is free software; you can redistribute it and/or modify it under the same
  239. terms as Perl itself.
  240.  
  241. =cut
  242.  
  243.