Pod/Search.pm untested

filter: Statementstmt 0.0% 0 / 180 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 288 CC 23 SCAR 34.5
CC
23
Cov
0%
_search
90.0
Populate
42.0
split_path
30.0
CRAP 31.5
1 package Tk::Pod::Search;
2  
3 use strict;
4 use vars qw(@ISA $VERSION);
5  
6 $VERSION = '5.16';
7  
8 use Carp;
9 use Config qw(%Config);
10 use File::Spec;
11 use Tk::Frame;
12  
13 Construct Tk::Widget 'PodSearch';
14 @ISA = 'Tk::Frame';
15  
16 my $searchfull_history;
17  
18 sub Populate {
19 my ($cw, $args) = @_;
20  
21 my $Entry;
22 eval {
23 require Tk::HistEntry;
24 $Entry = "HistEntry";
25 };
26 if ($@) {
27 require Tk::BrowseEntry;
28 $Entry = "BrowseEntry";
29 }
30  
31 my $l = $cw->Scrolled('Listbox',-width=>40,-scrollbars=>$Tk::platform eq 'MSWin32'?'e':'w');
32 require Tk::Pod::Styles;
33 my $fontsize = Tk::Pod::Styles::standard_font_size($l);
34 $l->configure(-font => "courier $fontsize");
35 #xxx BrowseEntry V1.3 does not honour -label at creation time :-(
36 #my $e = $cw->BrowseEntry(-labelPack=>[-side=>'left'],-label=>'foo',
37 #-listcmd=> ['_logit', 'list'],
38 #-browsecmd=> ['_logit', 'browse'],
39 #);
40 my $f = $cw->Frame;
41 my $e = $f->$Entry();
42 if ($e->can('history') && $searchfull_history) {
43 $e->history($searchfull_history);
44 }
45 my $s = $f->Label();
46 my $b = $f->Button(-text=>'OK',-command=>[\&_search,$e,$cw,$l]);
47  
48 $l->pack(-fill=>'both', -side=>'top', -expand=>1);
49 $f->pack(-fill => "x", -side => "top");
50 $s->pack(-anchor => 'e', -side=>'left');
51 $e->pack(-fill=>'x', -side=>'left', -expand=>1);
52 $b->pack(-side => 'left');
53  
54 my $current_path = delete $args->{-currentpath};
55 $cw->{RestrictPod} = undef;
56 my $cb;
57 if (defined $current_path && $current_path ne "") {
58 $cb = $cw->Checkbutton(-variable => \$cw->{RestrictPod},
59 -text => "Restrict to $current_path",
60 -anchor => "w",
61 -onvalue => $current_path,
62 -offvalue => undef,
63 )->pack(-fill => "x",
64 -side => "top",
65 );
66 }
67  
68 $cw->Advertise( 'entry' => $e->Subwidget('entry') );
69 $cw->Advertise( 'listbox' => $l->Subwidget('listbox') );
70 $cw->Advertise( 'browse' => $e);
71 $cw->Advertise( 'restrict' => $cb) if $cb;
72  
73 $cw->Delegates(
74 'focus' => $cw->Subwidget('entry'),
75 );
76  
77 $cw->ConfigSpecs(
78 -label => [{-text=>$s}, 'label', 'Label', 'Search:'],
79 -indexdir => ['PASSIVE', 'indexDir', 'IndexDir', undef],
80 -command => ['CALLBACK', undef, undef, undef],
81 -search => ['METHOD', 'search', 'Search', ""],
82 'DEFAULT' => [ $cw ],
83 );
84  
85 foreach (qw/Return space 1/) {
86 $cw->Subwidget('listbox')->bind("<$_>", [\&_load_pod, $cw]);
87 }
88 $cw->Subwidget('entry')->bind('<Return>',[$b,'invoke']);
89  
90 undef;
91 }
92  
93 sub addHistory {
94 my ($w, $obj) = @_;
95  
96 my $entry_or_browse = $w->Subwidget('browse');
97 if ($entry_or_browse->can('historyAdd')) {
98 $entry_or_browse->historyAdd($obj);
99 $searchfull_history = [ $entry_or_browse->history ];
100 } else {
101 $entry_or_browse->insert(0,$obj);
102 }
103 }
104  
105 sub _logit { print "logit=|", join('|',@_),"|\n"; }
106  
107 sub search {
108 my $cw = shift;
109 my $e = $cw->Subwidget('entry');
110 if (@_) {
111 my $search = shift;
112 $search = join(' ', @$search) if ref($search) eq 'ARRAY';
113 $e->delete(0,'end');
114 $e->insert(0,$search);
115 return undef;
116 } else {
117 return $e->get;
118 }
119 }
120  
121 sub search_as_regexp {
122 my $cw = shift;
123 my $search = $cw->search;
124 my @search = split ' ', $search;
125 if (@search) {
126 require Text::English;
127 my $rx = join("|", map { quotemeta } Text::English::stem(@search));
128 if (@search > 1) {
129 $rx = '(' . $rx . ')';
130 }
131 $rx;
132 } else {
133 '';
134 }
135 }
136  
137 sub _load_pod {
138 my $l = shift;
139 my $cw = shift;
140  
141 my $pod = pretty2path( $l->get(($l->curselection)[0]));
142  
143 $cw->Callback('-command', $pod, -searchterm => $cw->search_as_regexp());
144 }
145  
146  
147 sub _search {
148 my $e = shift;
149 my $w = shift;
150 my $l = shift;
151  
152 my $find = ref $e eq 'Tk::BrowseEntry' ? $e->Subwidget("entry")->get : $e->get;
153 $w->addHistory($find) if $find ne '';
154  
155 my %args;
156 if ($w->{RestrictPod}) {
157 $args{-restrictpod} = $w->{RestrictPod};
158 }
159  
160 #xxx: always open/close DBM files???
161 my $idx;
162 eval {
163 require Tk::Pod::Search_db;
164 $idx = Tk::Pod::Search_db->new($w->{Configure}{-indexdir});
165 };
166 if ($@) {
167 my $err = $@;
168 $e->messageBox(-icon => 'error',
169 -title => 'perlindex error',
170 -message => <<EOF);
171 Can't create Tk::Pod::Search_db object:
172 Is perlindex (aka Text::English) installed
173 and did you run 'perlindex -index'?
174 EOF
175 die $err;
176 }
177 my @raw_hits = $idx->searchWords($find, %args);
178 if (@raw_hits) {
179 $l->delete(0,'end');
180 my @hits;
181 my $max_length;
182 for my $raw_hit (@raw_hits) {
183 my($module, $path) = split_path($raw_hit->{path});
184 push @hits, [$raw_hit->{termhits}, $raw_hit->{score}, $module, $path];
185 $max_length = length $module if !defined $max_length || length $module > $max_length;
186 }
187 my $need_termhits = $hits[0]->[0] > 1;
188 for my $hit (@hits) {
189 my($termhits, $quality, $module, $path) = @$hit;
190 $l->insert('end', sprintf(($need_termhits ? "%d " : "") . "%6.3f %-${max_length}s (%s)",
191 ($need_termhits ? $termhits : ()), $quality, $module, $path)
192 );
193 }
194 $l->see(0);
195 $l->activate(0);
196 $l->selectionSet(0);
197 $l->focus;
198 } else {
199 my $msg = "No Pod documentation in Library matches: '$find'";
200 $e->messageBox(-icon => "error",
201 -title => "No match",
202 -message => $msg);
203 die $msg;
204 }
205 }
206  
207 # Converts /where/ever/it/it/Mod/Sub/Name.pm
208 # to ("Mod/Sub/Name.pm", "/where/ever/it/is")
209 # . Assumes that module subdirectories
210 # start with an upper case char. (xxx: Better solution
211 # when perlindex gives more infos.
212  
213 # XXX Note that split_path is also used in Search_db.pm
214 sub split_path {
215 my($path, $max_length) = @_;
216  
217 my @inc = sort { length($b) <=> length($a) } (@INC, $Config{scriptdir});
218 for my $inc (@inc) {
219 # XXX Nicer solution without hardcoded directory separators needed!
220 if (index($path, "$inc/") >= 0) {
221 return (substr($path, length($inc)+1), $inc);
222 }
223 if ($^O eq 'MSWin32' && index($path, "$inc\\") >= 0) {
224 return (substr($path, length($inc)+1), $inc);
225 }
226 }
227  
228 # Rarely this fallback should be used:
229  
230 my($volume, $directories, $file) = File::Spec->splitpath($path);
231 my @path = (File::Spec->splitdir($directories), $file);
232  
233 # Guess the separator point between path and module/script name
234 my $path_i;
235 for($path_i = $#path; $path_i >= 0; $path_i--) {
236 if ($path[$path_i] ne '' && $path[$path_i] !~ /^[A-Z]/) {
237 last;
238 }
239 }
240  
241 # Scripts are usually lowercase, so the above logic does not work.
242 # Fix it:
243 if ($path_i == $#path) {
244 $path_i--;
245 }
246  
247 # Remove empty directories from the end (a relict from
248 # splitpath/splitdir)
249 my @dirs = @path[0 .. $path_i];
250 while(@dirs && $dirs[-1] eq '') { pop @dirs }
251  
252 # Remove empty directories from the beginning (also a relict from
253 # splitpath/splitdir)
254 my @moddirs = @path[$path_i+1 .. $#path];
255 while(@moddirs && $moddirs[0] eq '') { shift @moddirs }
256  
257 my($dirpart,$modpart) = (File::Spec->catpath($volume, File::Spec->catfile(@dirs), ''),
258 File::Spec->catfile(@moddirs));
259 return ($modpart, $dirpart);
260 }
261  
262 sub pretty2path {
263 local($_) = shift;
264 /([^\s]+) \s+\( (.*) \)/x;
265 File::Spec->catfile($2, $1);
266 }
267  
268 #$path = '/where/ever/it/is/Tk/Pod.pm'; print "orig|",$path, "|\n";
269 #$nice = path2pretty $path; print "nice|",$nice, "|\n";
270 #$path = pretty2path $nice; print "path|",$path, "|\n";
271  
272  
273 1;
274 __END__