| 1 | |||
| 2 | require 5; | ||
| 3 | package Tk::Pod::Text; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | |||
| 7 | BEGIN { # Make a DEBUG constant very first thing... | ||
| 8 | if(defined &DEBUG) { | ||
| 9 | } elsif(($ENV{'TKPODDEBUG'} || '') =~ m/^(\d+)/) { # untaint | ||
| 10 | my $debug = $1; | ||
| 11 | *DEBUG = sub () { $debug }; | ||
| 12 | } else { | ||
| 13 | *DEBUG = sub () {0}; | ||
| 14 | } | ||
| 15 | } | ||
| 16 | |||
| 17 | use Carp; | ||
| 18 | use Config; | ||
| 19 | use Tk qw(catch); | ||
| 20 | use Tk::Frame; | ||
| 21 | use Tk::Pod; | ||
| 22 | use Tk::Pod::SimpleBridge; | ||
| 23 | use Tk::Pod::Cache; | ||
| 24 | use Tk::Pod::Util qw(is_in_path is_interactive detect_window_manager start_browser); | ||
| 25 | |||
| 26 | use vars qw($VERSION @ISA @POD $IDX | ||
| 27 | @tempfiles @gv_pids $terminal_fallback_warn_shown); | ||
| 28 | |||
| 29 | $VERSION = '5.34'; | ||
| 30 | |||
| 31 | @ISA = qw(Tk::Frame Tk::Pod::SimpleBridge Tk::Pod::Cache); | ||
| 32 | |||
| 33 | BEGIN { DEBUG and warn "Running ", __PACKAGE__, "\n" } | ||
| 34 | |||
| 35 | Construct Tk::Widget 'PodText'; | ||
| 36 | |||
| 37 | BEGIN { | ||
| 38 | unshift @POD, ( | ||
| 39 | @INC, | ||
| 40 | $ENV{'PATH'} ? | ||
| 41 | grep(-d, split($Config{path_sep}, $ENV{'PATH'})) | ||
| 42 | : () | ||
| 43 | ); | ||
| 44 | $IDX = undef; | ||
| 45 | DEBUG and warn "POD: @POD\n"; | ||
| 46 | }; | ||
| 47 | |||
| 48 | { | ||
| 49 | package # hide from CPAN indexer | ||
| 50 | Tk::Pod::Text::_HistoryEntry; | ||
| 51 | |||
| 52 | use File::Basename qw(basename); | ||
| 53 | |||
| 54 | for my $member (qw(file text index pod_title)) { | ||
| 55 | my $sub = sub { | ||
| 56 | my $self = shift; | ||
| 57 | if (@_) { | ||
| 58 | $self->{$member} = $_[0]; | ||
| 59 | } | ||
| 60 | $self->{$member}; | ||
| 61 | }; | ||
| 62 | no strict 'refs'; | ||
| 63 | *{$member} = $sub; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub create { | ||
| 67 | my($class,$what,$index) = @_; | ||
| 68 | my $o = bless {}, $class; | ||
| 69 | if (ref $what eq 'HASH') { | ||
| 70 | $o->file($what->{file}); | ||
| 71 | $o->text($what->{text}); | ||
| 72 | } else { | ||
| 73 | $o->file($what); | ||
| 74 | } | ||
| 75 | $o->index($index); | ||
| 76 | $o; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub get_label { | ||
| 80 | my $self = shift; | ||
| 81 | my $pod_title = $self->pod_title; | ||
| 82 | return $pod_title if defined $pod_title; | ||
| 83 | my $file = $self->file; | ||
| 84 | return basename $file if defined $file; | ||
| 85 | return "<Untitled document>"; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | use constant HISTORY_DIALOG_ARGS => [-icon => 'info', | ||
| 90 | -title => 'History Error', | ||
| 91 | -type => 'OK']; | ||
| 92 | sub Dir | ||
| 93 | { | ||
| 94 | my $class = shift; | ||
| 95 | unshift(@POD,@_); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub Find | ||
| 99 | { | ||
| 100 | my ($file) = @_; | ||
| 101 | return $file if (-f $file); | ||
| 102 | my $dir; | ||
| 103 | foreach $dir ("",@POD) | ||
| 104 | { | ||
| 105 | my $prefix; | ||
| 106 | foreach $prefix ("","pod/","pods/") | ||
| 107 | { | ||
| 108 | my $suffix; | ||
| 109 | foreach $suffix (".pod",".pm",".pl","") | ||
| 110 | { | ||
| 111 | my $path = "$dir/" . $prefix . $file . $suffix; | ||
| 112 | return $path if (-r $path && -T $path); | ||
| 113 | $path =~ s,::,/,g; | ||
| 114 | return $path if (-r $path && -T $path); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | } | ||
| 118 | return undef; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub findpod { | ||
| 122 | my ($w,$name,%opts) = @_; | ||
| 123 | my $quiet = delete $opts{-quiet}; | ||
| 124 | warn "Unhandled extra options: ". join " ", %opts | ||
| 125 | if %opts; | ||
| 126 | unless (defined $name and length $name) { | ||
| 127 | return if $quiet; | ||
| 128 | $w->_die_dialog("Empty Pod file/name"); | ||
| 129 | } | ||
| 130 | |||
| 131 | my $absname; | ||
| 132 | if (-f $name) { | ||
| 133 | $absname = $name; | ||
| 134 | } else { | ||
| 135 | if ($name !~ /^[-_+:.\/A-Za-z0-9]+$/) { | ||
| 136 | return if $quiet; | ||
| 137 | $w->_die_dialog("Invalid path/file/module name '$name'\n"); | ||
| 138 | } | ||
| 139 | $absname = Find($name); | ||
| 140 | } | ||
| 141 | if (!defined $absname) { | ||
| 142 | return if $quiet; | ||
| 143 | $w->_error_dialog("Can't find Pod '$name'\n"); | ||
| 144 | die "Can't find Pod '$name' in @POD\n"; | ||
| 145 | } | ||
| 146 | if (eval { require File::Spec; File::Spec->can("rel2abs") }) { | ||
| 147 | DEBUG and warn "Turn $absname into an absolute file name"; | ||
| 148 | $absname = File::Spec->rel2abs($absname); | ||
| 149 | } | ||
| 150 | $absname; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub _remember_old { | ||
| 154 | my $w = shift; | ||
| 155 | for (qw(File Text)) { | ||
| 156 | $w->{"Old$_"} = $w->{$_}; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | sub _restore_old { | ||
| 161 | my $w = shift; | ||
| 162 | for (qw(File Text)) { | ||
| 163 | $w->{$_} = $w->{"Old$_"}; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | sub file { # main entry point | ||
| 168 | my $w = shift; | ||
| 169 | if (@_) | ||
| 170 | { | ||
| 171 | my $file = shift; | ||
| 172 | $w->_remember_old; | ||
| 173 | eval { | ||
| 174 | my $calling_from_history = $w->privateData()->{'from_history'}; | ||
| 175 | $w->{'File'} = $file; | ||
| 176 | $w->{'Text'} = undef; | ||
| 177 | my $path = $w->findpod($file); | ||
| 178 | if (!$calling_from_history) { | ||
| 179 | $w->history_modify_entry; | ||
| 180 | $w->history_add({file => $path}, "1.0"); | ||
| 181 | } | ||
| 182 | $w->configure('-path' => $path); | ||
| 183 | $w->delete('1.0' => 'end'); | ||
| 184 | my $tree_sw = $w->parent->Subwidget("tree"); | ||
| 185 | if ($tree_sw) { | ||
| 186 | $tree_sw->SeePath("file:$path"); | ||
| 187 | } | ||
| 188 | my $t; | ||
| 189 | if (DEBUG) { | ||
| 190 | require Benchmark; | ||
| 191 | $t = Benchmark->new; | ||
| 192 | } | ||
| 193 | if (!$w->get_from_cache) { | ||
| 194 | $w->process($path); | ||
| 195 | $w->add_to_cache; # XXX pass time for processing? | ||
| 196 | if (!$calling_from_history) { | ||
| 197 | $w->history_modify_current_title; # now the pod_title is known | ||
| 198 | } | ||
| 199 | } | ||
| 200 | if (defined $t) { | ||
| 201 | print Benchmark::timediff(Benchmark->new, $t)->timestr,"\n"; | ||
| 202 | } | ||
| 203 | $w->focus; | ||
| 204 | }; | ||
| 205 | if ($@) { | ||
| 206 | $w->_restore_old; | ||
| 207 | die $@; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | $w->{'File'}; | ||
| 211 | } | ||
| 212 | |||
| 213 | sub text { | ||
| 214 | my $w = shift; | ||
| 215 | if (@_) | ||
| 216 | { | ||
| 217 | my $text = shift; | ||
| 218 | $w->_remember_old; | ||
| 219 | eval { | ||
| 220 | my $calling_from_history = $w->privateData()->{'from_history'}; | ||
| 221 | $w->{'Text'} = $text; | ||
| 222 | $w->{'File'} = undef; | ||
| 223 | if (!$calling_from_history) { | ||
| 224 | $w->history_modify_entry; | ||
| 225 | $w->history_add({text => $text}, "1.0"); | ||
| 226 | } | ||
| 227 | $w->configure('-path' => undef); | ||
| 228 | $w->delete('1.0' => 'end'); | ||
| 229 | ## XXX Implementation unclear, maybe should be done in showcommand call... | ||
| 230 | # my $tree_sw = $w->parent->Subwidget("tree"); | ||
| 231 | # if ($tree_sw) { | ||
| 232 | # $tree_sw->SeeFunc("file:$path"); | ||
| 233 | # } | ||
| 234 | my $t; | ||
| 235 | if (DEBUG) { | ||
| 236 | require Benchmark; | ||
| 237 | $t = Benchmark->new; | ||
| 238 | } | ||
| 239 | # No caching here | ||
| 240 | # XXX title: the 2nd part of the hack | ||
| 241 | my $title = $w->cget(-title); | ||
| 242 | $w->process(\$text, $title); | ||
| 243 | if (!$calling_from_history) { | ||
| 244 | $w->history_modify_current_title; # now the pod_title is known | ||
| 245 | } | ||
| 246 | if (defined $t) { | ||
| 247 | print Benchmark::timediff(Benchmark->new, $t)->timestr,"\n"; | ||
| 248 | } | ||
| 249 | $w->focus; | ||
| 250 | }; | ||
| 251 | if ($@) { | ||
| 252 | $w->_restore_old; | ||
| 253 | die $@; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | $w->{'Text'}; | ||
| 257 | } | ||
| 258 | |||
| 259 | sub reload | ||
| 260 | { | ||
| 261 | my ($w) = @_; | ||
| 262 | # remember old y position | ||
| 263 | my ($currpos) = $w->yview; | ||
| 264 | $w->delete('0.0','end'); | ||
| 265 | $w->delete_from_cache; | ||
| 266 | $w->process($w->cget('-path')); | ||
| 267 | # restore old y position | ||
| 268 | $w->yview(moveto => $currpos); | ||
| 269 | # set (invisible) insertion cursor into the visible text area | ||
| 270 | $w->markSet(insert => '@0,0'); | ||
| 271 | } | ||
| 272 | |||
| 273 | # Works also for viewing source code | ||
| 274 | sub _get_editable_path | ||
| 275 | { | ||
| 276 | my ($w) = @_; | ||
| 277 | my $path = $w->cget('-path'); | ||
| 278 | if (!defined $path) | ||
| 279 | { | ||
| 280 | my $text = $w->cget("-text"); | ||
| 281 | $w->_need_File_Temp; | ||
| 282 | my($fh,$fname) = File::Temp::tempfile(UNLINK => 1, | ||
| 283 | SUFFIX => "_tkpod.pod"); | ||
| 284 | print $fh $text; | ||
| 285 | close $fh; | ||
| 286 | $path = $fname; | ||
| 287 | } | ||
| 288 | $path; | ||
| 289 | } | ||
| 290 | |||
| 291 | sub edit | ||
| 292 | { | ||
| 293 | my ($w,$edit,$linenumber) = @_; | ||
| 294 | my $path = $w->_get_editable_path; | ||
| 295 | if (!defined $edit) | ||
| 296 | { | ||
| 297 | $edit = $ENV{TKPODEDITOR}; | ||
| 298 | } | ||
| 299 | if ($^O eq 'MSWin32') | ||
| 300 | { | ||
| 301 | if (defined $edit && $edit ne "") | ||
| 302 | { | ||
| 303 | system(1, $edit, $path); | ||
| 304 | } | ||
| 305 | else | ||
| 306 | { | ||
| 307 | system(1, "ptked", $path); | ||
| 308 | } | ||
| 309 | } | ||
| 310 | else | ||
| 311 | { | ||
| 312 | if (!defined $edit || $edit eq "") | ||
| 313 | { | ||
| 314 | # VISUAL and EDITOR are supposed to have a terminal, but tkpod can | ||
| 315 | # be started without a terminal. | ||
| 316 | my $isatty = is_interactive(); | ||
| 317 | if (!$isatty) | ||
| 318 | { | ||
| 319 | if (!defined $edit || $edit eq "") | ||
| 320 | { | ||
| 321 | $edit = $ENV{XEDITOR}; | ||
| 322 | } | ||
| 323 | if (!defined $edit || $edit eq "") | ||
| 324 | { | ||
| 325 | if (!$terminal_fallback_warn_shown) | ||
| 326 | { | ||
| 327 | $w->_warn_dialog("No terminal and neither TKPODEDITOR nor XEDITOR environment variables set. Fallback to ptked."); | ||
| 328 | $terminal_fallback_warn_shown = 1; | ||
| 329 | } | ||
| 330 | $edit = 'ptked'; | ||
| 331 | } | ||
| 332 | } | ||
| 333 | else | ||
| 334 | { | ||
| 335 | $edit = $ENV{VISUAL} || $ENV{'EDITOR'} || '/usr/bin/vi'; | ||
| 336 | } | ||
| 337 | } | ||
| 338 | |||
| 339 | if (defined $edit) | ||
| 340 | { | ||
| 341 | if (fork) | ||
| 342 | { | ||
| 343 | wait; # parent | ||
| 344 | } | ||
| 345 | else | ||
| 346 | { | ||
| 347 | #child | ||
| 348 | if (fork) | ||
| 349 | { | ||
| 350 | # still child | ||
| 351 | exec("true"); | ||
| 352 | } | ||
| 353 | else | ||
| 354 | { | ||
| 355 | # grandchild | ||
| 356 | if (defined $linenumber && $edit =~ m{\bemacsclient\b}) # XXX an experiment, maybe support more editors? | ||
| 357 | { | ||
| 358 | exec("$edit +$linenumber $path"); | ||
| 359 | } | ||
| 360 | else | ||
| 361 | { | ||
| 362 | exec("$edit $path"); | ||
| 363 | } | ||
| 364 | } | ||
| 365 | } | ||
| 366 | } | ||
| 367 | } | ||
| 368 | } | ||
| 369 | |||
| 370 | sub edit_get_linenumber | ||
| 371 | { | ||
| 372 | my($w) = @_; | ||
| 373 | my $linenumber = $w->get_linenumber; | ||
| 374 | $w->edit(undef, $linenumber); | ||
| 375 | } | ||
| 376 | |||
| 377 | sub get_linenumber | ||
| 378 | { | ||
| 379 | my($w) = @_; | ||
| 380 | my $coord; | ||
| 381 | if (defined $w->{MenuX}) # called from a popup menu | ||
| 382 | { | ||
| 383 | $coord = '@' . ($w->{MenuX} - $w->rootx) . ',' . ($w->{MenuY} - $w->rooty); | ||
| 384 | } | ||
| 385 | else # called from a keyboard shortcut | ||
| 386 | { | ||
| 387 | $coord = '@0,0'; | ||
| 388 | } | ||
| 389 | for my $try_line (0 .. 3) # top line resp. line under cursor might be empty, without tags, so try also the following ones | ||
| 390 | { | ||
| 391 | for my $tag ($w->tagNames($coord . ($try_line ? " + $try_line lines" : ''))) | ||
| 392 | { | ||
| 393 | if ($tag =~ m{start_line_(\d+)}) | ||
| 394 | { | ||
| 395 | return $1; | ||
| 396 | } | ||
| 397 | } | ||
| 398 | } | ||
| 399 | undef; | ||
| 400 | } | ||
| 401 | |||
| 402 | sub view_source | ||
| 403 | { | ||
| 404 | my($w) = @_; | ||
| 405 | # XXX why is -title empty here? | ||
| 406 | my $title = $w->cget(-title) || $w->cget('-file'); | ||
| 407 | my $t = $w->Toplevel(-title => "Source of $title - Tkpod"); | ||
| 408 | my $font_size = $w->base_font_size; | ||
| 409 | my $more = $t->Scrolled('More', | ||
| 410 | -font => "Courier $font_size", | ||
| 411 | -scrollbars => $Tk::platform eq 'MSWin32' ? 'e' : 'w', | ||
| 412 | )->pack(-fill => "both", -expand => 1); | ||
| 413 | $more->Load($w->_get_editable_path); | ||
| 414 | my $linenumber = $w->get_linenumber; | ||
| 415 | if (defined $linenumber) | ||
| 416 | { | ||
| 417 | $more->see($linenumber.'.'.0); | ||
| 418 | } | ||
| 419 | $more->AddQuitBindings; | ||
| 420 | $more->focus; | ||
| 421 | } | ||
| 422 | |||
| 423 | sub copy_pod_location | ||
| 424 | { | ||
| 425 | my($w) = @_; | ||
| 426 | my $file = $w->_get_editable_path; | ||
| 427 | if (!defined $file) | ||
| 428 | { | ||
| 429 | $w->_error_dialog("Cannot copy location: this Pod is not associated with a file"); | ||
| 430 | return; | ||
| 431 | } | ||
| 432 | $w->SelectionOwn; | ||
| 433 | $w->SelectionHandle(sub { | ||
| 434 | my($offset,$maxbytes) = @_; | ||
| 435 | # XXX It's not exactly clear why I have to | ||
| 436 | # call _get_editable_path again here and not | ||
| 437 | # reuse $file. | ||
| 438 | my $file = $w->_get_editable_path; | ||
| 439 | return undef if $offset > length($file); | ||
| 440 | substr($file, $offset, $maxbytes); | ||
| 441 | }); | ||
| 442 | } | ||
| 443 | |||
| 444 | sub _sgn { $_[0] cmp 0 } | ||
| 445 | |||
| 446 | sub zoom_normal { | ||
| 447 | my $w = shift; | ||
| 448 | $w->adjust_font_size($w->standard_font_size); | ||
| 449 | $w->clear_cache; | ||
| 450 | } | ||
| 451 | |||
| 452 | # XXX should use different increments for different styles | ||
| 453 | sub zoom_out { | ||
| 454 | my $w = shift; | ||
| 455 | $w->adjust_font_size($w->base_font_size - 1 * _sgn($w->base_font_size)); | ||
| 456 | $w->clear_cache; | ||
| 457 | } | ||
| 458 | |||
| 459 | sub zoom_in { | ||
| 460 | my $w = shift; | ||
| 461 | $w->adjust_font_size($w->base_font_size + 1 * _sgn($w->base_font_size)); | ||
| 462 | $w->clear_cache; | ||
| 463 | } | ||
| 464 | |||
| 465 | sub More_Widget { "More" } | ||
| 466 | sub More_Module { "Tk::More" } | ||
| 467 | |||
| 468 | sub Populate | ||
| 469 | { | ||
| 470 | my ($w,$args) = @_; | ||
| 471 | |||
| 472 | if ($w->More_Module) { | ||
| 473 | eval q{ require } . $w->More_Module; | ||
| 474 | die $@ if $@; | ||
| 475 | } | ||
| 476 | |||
| 477 | $w->SUPER::Populate($args); | ||
| 478 | |||
| 479 | $w->privateData()->{history} = []; | ||
| 480 | $w->privateData()->{history_index} = -1; | ||
| 481 | |||
| 482 | my $p = $w->Scrolled($w->More_Widget, | ||
| 483 | -helpcommand => sub { | ||
| 484 | $w->parent->help if $w->parent->can('help'); | ||
| 485 | }, | ||
| 486 | -scrollbars => $Tk::platform eq 'MSWin32' ? 'e' : 'w'); | ||
| 487 | my $p_scr = $p->Subwidget('scrolled'); | ||
| 488 | $w->Advertise('more' => $p_scr); | ||
| 489 | $p->pack(-expand => 1, -fill => 'both'); | ||
| 490 | |||
| 491 | # XXX Subwidget stuff needed because Scrolled does not | ||
| 492 | # delegate bind, bindtag to the scrolled widget. Tk402.* (and before?) | ||
| 493 | # (patch posted and included in Tk402.004) | ||
| 494 | $p_scr->bindtags([$p_scr, $p_scr->bindtags]); | ||
| 495 | $p_scr->bind('<Double-1>', sub { $w->DoubleClick($_[0]) }); | ||
| 496 | $p_scr->bind('<Shift-Double-1>', sub { $w->ShiftDoubleClick($_[0]) }); | ||
| 497 | $p_scr->bind('<Double-2>', sub { $w->ShiftDoubleClick($_[0]) }); | ||
| 498 | $p_scr->bind('<3>', sub { $w->PostPopupMenu($p_scr, $w->pointerxy) }); | ||
| 499 | $p_scr->bind('<ButtonRelease-2>', [sub { | ||
| 500 | # A hack solution to prevent from firing this | ||
| 501 | # event over pod links. See http://wiki.tcl.tk/6101 | ||
| 502 | my($ro,$x,$y) = @_; | ||
| 503 | if (grep { $_ eq 'pod_link' } $ro->tagNames("\@$x,$y")) { | ||
| 504 | Tk->break; | ||
| 505 | } else { | ||
| 506 | $w->OpenPodBySelection; | ||
| 507 | } | ||
| 508 | }, Tk::Ev("x"), Tk::Ev("y")]); | ||
| 509 | |||
| 510 | $p->configure(-font => $w->Font(family => 'courier')); | ||
| 511 | |||
| 512 | $p->tag('configure','text', -font => $w->Font(family => 'times')); | ||
| 513 | |||
| 514 | $p->insert('0.0',"\n"); | ||
| 515 | |||
| 516 | $w->{List} = []; # stack of =over | ||
| 517 | $w->{Item} = undef; | ||
| 518 | $w->{'indent'} = 0; | ||
| 519 | $w->{Length} = 64; | ||
| 520 | $w->{Indent} = {}; # tags for various indents | ||
| 521 | |||
| 522 | # Seems like a perl bug: ->can() does not work before actually calling | ||
| 523 | # the subroutines (perl5.6.0 isa bug?) | ||
| 524 | eval { | ||
| 525 | $p->EditMenuItems; | ||
| 526 | $p->SearchMenuItems; | ||
| 527 | $p->ViewMenuItems; | ||
| 528 | }; | ||
| 529 | |||
| 530 | my $m = $p->Menu | ||
| 531 | (-title => "Tkpod", | ||
| 532 | -tearoff => $Tk::platform ne 'MSWin32', | ||
| 533 | -menuitems => | ||
| 534 | [ | ||
| 535 | [Button => 'Back', -command => [$w, 'history_move', -1]], | ||
| 536 | [Button => 'Forward', -command => [$w, 'history_move', +1]], | ||
| 537 | [Button => 'Reload', -command => sub{$w->reload} ], | ||
| 538 | [Button => 'Edit Pod', -command => sub{ $w->edit_get_linenumber } ], | ||
| 539 | [Button => 'View source', -command => sub{ $w->view_source } ], | ||
| 540 | [Button => 'Copy Pod location', -command => sub { $w->copy_pod_location } ], | ||
| 541 | [Button => 'Search full text',-command => ['SearchFullText', $w]], | ||
| 542 | [Separator => ""], | ||
| 543 | [Cascade => 'Edit', | ||
| 544 | ($Tk::VERSION > 800.015 && $p->can('EditMenuItems') ? (-menuitems => $p->EditMenuItems) : ()), | ||
| 545 | ], | ||
| 546 | [Cascade => 'Search', | ||
| 547 | ($Tk::VERSION > 800.015 && $p->can('SearchMenuItems') ? (-menuitems => $p->SearchMenuItems) : ()), | ||
| 548 | ], | ||
| 549 | [Cascade => 'View', | ||
| 550 | ($Tk::VERSION > 800.015 && $p->can('ViewMenuItems') ? (-menuitems => $p->ViewMenuItems) : ()), | ||
| 551 | ] | ||
| 552 | ]); | ||
| 553 | eval { $p->menu($m) }; warn $@ if $@; | ||
| 554 | |||
| 555 | $w->Delegates(DEFAULT => $p, | ||
| 556 | 'SearchFullText' => 'SELF', | ||
| 557 | ); | ||
| 558 | |||
| 559 | $w->ConfigSpecs( | ||
| 560 | '-file' => ['METHOD' ], | ||
| 561 | '-text' => ['METHOD' ], | ||
| 562 | '-path' => ['PASSIVE' ], | ||
| 563 | '-poddone' => ['CALLBACK'], | ||
| 564 | '-title' => ['PASSIVE' ], # XXX unclear | ||
| 565 | |||
| 566 | '-wrap' => [ $p, qw(wrap Wrap word) ], | ||
| 567 | # -font ignored because it does not change the other fonts | ||
| 568 | #'-font' => [ 'PASSIVE', undef, undef, undef], | ||
| 569 | '-scrollbars' => [ $p, qw(scrollbars Scrollbars), $Tk::platform eq 'MSWin32' ? 'e' : 'w' ], | ||
| 570 | '-basefontsize' => ['METHOD'], # XXX may change | ||
| 571 | |||
| 572 | 'DEFAULT' => [ $p ], | ||
| 573 | ); | ||
| 574 | |||
| 575 | $args->{-width} = $w->{Length}; | ||
| 576 | } | ||
| 577 | |||
| 578 | sub basefontsize | ||
| 579 | { | ||
| 580 | my($w, $val) = @_; | ||
| 581 | if ($val) | ||
| 582 | { | ||
| 583 | $w->set_base_font_size($val); | ||
| 584 | } | ||
| 585 | else | ||
| 586 | { | ||
| 587 | $w->base_font_size; | ||
| 588 | } | ||
| 589 | } | ||
| 590 | |||
| 591 | sub Font | ||
| 592 | { | ||
| 593 | my ($w,%args) = @_; | ||
| 594 | $args{'family'} = 'times' unless (exists $args{'family'}); | ||
| 595 | $args{'weight'} = 'medium' unless (exists $args{'weight'}); | ||
| 596 | $args{'slant'} = 'r' unless (exists $args{'slant'}); | ||
| 597 | $args{'size'} = 140 unless (exists $args{'size'}); | ||
| 598 | $args{'spacing'} = '*' unless (exists $args{'spacing'}); | ||
| 599 | $args{'slant'} = substr($args{'slant'},0,1); | ||
| 600 | my $name = "-*-$args{'family'}-$args{'weight'}-$args{'slant'}-*-*-*-$args{'size'}-*-*-$args{'spacing'}-*-iso8859-1"; | ||
| 601 | return $name; | ||
| 602 | } | ||
| 603 | |||
| 604 | sub ShiftDoubleClick { | ||
| 605 | shift->DoubleClick(shift, 'new'); | ||
| 606 | } | ||
| 607 | |||
| 608 | sub DoubleClick | ||
| 609 | { | ||
| 610 | my ($w,$ww,$how) = @_; | ||
| 611 | my $Ev = $ww->XEvent; | ||
| 612 | $w->SelectToModule($Ev->xy); | ||
| 613 | my $sel = catch { $w->SelectionGet }; | ||
| 614 | if (defined $sel) | ||
| 615 | { | ||
| 616 | my $file; | ||
| 617 | if ($file = $w->findpod($sel)) { | ||
| 618 | if (defined $how && $how eq 'new') | ||
| 619 | { | ||
| 620 | my $tree = eval { $w->parent->cget(-tree) }; | ||
| 621 | my $exitbutton = eval { $w->parent->cget(-exitbutton) }; | ||
| 622 | $w->MainWindow->Pod('-file' => $sel, | ||
| 623 | '-tree' => $tree, | ||
| 624 | -exitbutton => $exitbutton); | ||
| 625 | } | ||
| 626 | else | ||
| 627 | { | ||
| 628 | $w->configure('-file'=>$file); | ||
| 629 | } | ||
| 630 | } else { | ||
| 631 | $w->_die_dialog("No Pod documentation found for '$sel'\n"); | ||
| 632 | } | ||
| 633 | } | ||
| 634 | Tk->break; | ||
| 635 | } | ||
| 636 | |||
| 637 | sub Link | ||
| 638 | { | ||
| 639 | my ($w,$how,$index,$man,$sec) = @_; | ||
| 640 | |||
| 641 | # If clicking on a Link, the <Leave> binding is never called, so it | ||
| 642 | # have to be done here: | ||
| 643 | $w->LeaveLink; | ||
| 644 | |||
| 645 | $man = '' unless defined $man; | ||
| 646 | $sec = '' unless defined $sec; | ||
| 647 | |||
| 648 | if ($how eq 'reuse' && $man ne '') | ||
| 649 | { | ||
| 650 | my $file = $w->cget('-file'); | ||
| 651 | $w->configure('-file' => $man) | ||
| 652 | unless ( defined $file and ($file =~ /\Q$man\E\.\w+$/ or $file eq $man) ); | ||
| 653 | } | ||
| 654 | |||
| 655 | if ($how eq 'new') | ||
| 656 | { | ||
| 657 | $man = $w->cget('-file') if ($man eq ""); | ||
| 658 | my $tree = eval { $w->parent->cget(-tree) }; | ||
| 659 | my $exitbutton = eval { $w->parent->cget(-exitbutton) }; | ||
| 660 | my $old_w = $w; | ||
| 661 | my $new_pod = $w->MainWindow->Pod('-tree' => $tree, | ||
| 662 | -exitbutton => $exitbutton, | ||
| 663 | ); | ||
| 664 | $new_pod->configure('-file' => $man); # see tkpod for the same problem | ||
| 665 | |||
| 666 | $w = $new_pod->Subwidget('pod'); | ||
| 667 | # set search term for new window | ||
| 668 | my $search_term_ref = $old_w->Subwidget('more')->Subwidget('searchentry')->cget(-textvariable); | ||
| 669 | if (defined $$search_term_ref && $$search_term_ref ne "") { | ||
| 670 | $ {$w->Subwidget('more')->Subwidget('searchentry')->cget(-textvariable) } = $$search_term_ref; | ||
| 671 | } | ||
| 672 | } | ||
| 673 | # XXX big docs like Tk::Text take too long until they return | ||
| 674 | |||
| 675 | if ($sec ne '' && $man eq '') # XXX reuse vs. new | ||
| 676 | { | ||
| 677 | $w->history_modify_entry; | ||
| 678 | } | ||
| 679 | |||
| 680 | if ($sec ne '') | ||
| 681 | { | ||
| 682 | |||
| 683 | my $highlight_match = sub | ||
| 684 | { | ||
| 685 | my $start = shift; | ||
| 686 | my($line) = split(/\./, $start); | ||
| 687 | $w->tag('remove', '_section_mark', qw/0.0 end/); | ||
| 688 | $w->tag('add', '_section_mark', | ||
| 689 | $line . ".0", | ||
| 690 | $line . ".0 lineend"); | ||
| 691 | $w->yview("_section_mark.first"); | ||
| 692 | $w->after(500, [$w, qw/tag remove _section_mark 0.0 end/]); | ||
| 693 | }; | ||
| 694 | |||
| 695 | DEBUG and warn "Looking for section \"$sec\" across Sections entries...\n"; | ||
| 696 | |||
| 697 | foreach my $s ( @{$w->{'sections'} || []} ) | ||
| 698 | { | ||
| 699 | if($s->[1] eq $sec) | ||
| 700 | { | ||
| 701 | DEBUG and warn " $sec is $$s[1] (at $$s[2])\n"; | ||
| 702 | my $start = $s->[2]; | ||
| 703 | my($line) = split(/\./, $start); | ||
| 704 | $line--; # off by one, why? | ||
| 705 | $highlight_match->("$line.0"); | ||
| 706 | return; | ||
| 707 | } | ||
| 708 | else | ||
| 709 | { | ||
| 710 | DEBUG > 2 and warn " Nope, it's not $$s[1] (at $$s[2])\n"; | ||
| 711 | } | ||
| 712 | } | ||
| 713 | |||
| 714 | my $start = ($w->tag('nextrange',$sec, '1.0'))[0]; | ||
| 715 | |||
| 716 | if (defined $start) | ||
| 717 | { | ||
| 718 | DEBUG and warn " Found at $start\n"; | ||
| 719 | $highlight_match->($start); | ||
| 720 | return; | ||
| 721 | } | ||
| 722 | else | ||
| 723 | { | ||
| 724 | DEBUG and warn " Not found so far. Using a quoted nextrange search...\n"; | ||
| 725 | my $link = ($man || '') . $sec; | ||
| 726 | $start = ($w->tag('nextrange',"\"$link\"",'1.0'))[0]; | ||
| 727 | } | ||
| 728 | |||
| 729 | if (defined $start) | ||
| 730 | { | ||
| 731 | DEBUG and warn " Found at $start\n"; | ||
| 732 | $highlight_match->($start); | ||
| 733 | return; | ||
| 734 | } | ||
| 735 | else | ||
| 736 | { | ||
| 737 | DEBUG and warn " Again not found. Using an exact search at line beginnings...\n"; | ||
| 738 | $start = $w->search(qw/-regexp -nocase --/, qr{^\s*\Q$sec}, '1.0'); | ||
| 739 | } | ||
| 740 | |||
| 741 | if (defined $start) | ||
| 742 | { | ||
| 743 | DEBUG and warn " Found at $start\n"; | ||
| 744 | $highlight_match->($start); | ||
| 745 | return; | ||
| 746 | } | ||
| 747 | else | ||
| 748 | { | ||
| 749 | DEBUG and warn " Again not found. Using an exact search...\n"; | ||
| 750 | $start = $w->search(qw/-exact -nocase --/, $sec, '1.0'); | ||
| 751 | } | ||
| 752 | |||
| 753 | if (defined $start) | ||
| 754 | { | ||
| 755 | DEBUG and warn " Found at $start\n"; | ||
| 756 | $highlight_match->($start); | ||
| 757 | return; | ||
| 758 | } | ||
| 759 | else | ||
| 760 | { | ||
| 761 | DEBUG and warn " Not found! (\"sec\")\n"; | ||
| 762 | $w->_die_dialog("Section '$sec' not found\n"); | ||
| 763 | } | ||
| 764 | DEBUG and warn "link-zapping to $start linestart\n"; | ||
| 765 | $w->yview("$start linestart"); | ||
| 766 | } | ||
| 767 | |||
| 768 | if ($sec ne '' && $man eq '') # XXX reuse vs. new | ||
| 769 | { | ||
| 770 | $w->history_add({file => $w->cget(-path)}, $w->index('@0,0')); | ||
| 771 | } | ||
| 772 | |||
| 773 | } | ||
| 774 | |||
| 775 | sub Link_url { | ||
| 776 | my ($w,$how,$index,$man,$sec) = @_; | ||
| 777 | if (my($lat,$lon) = $man =~ m{^geo:([^,]+),([^,]+)}) { | ||
| 778 | DEBUG and warn "Translate geo URI $man\n"; | ||
| 779 | # XXX currently hardcoded to OSM, maybe make configurable | ||
| 780 | $man = "http://www.openstreetmap.org/?mlat=$lat&mlon=$lon"; | ||
| 781 | } | ||
| 782 | DEBUG and warn "Start browser with $man\n"; | ||
| 783 | start_browser($man); | ||
| 784 | } | ||
| 785 | |||
| 786 | sub Link_man { | ||
| 787 | my ($w,$how,$index,$man,$sec) = @_; | ||
| 788 | my $mansec; | ||
| 789 | if ($man =~ s/\s*\((.*)\)\s*$//) { | ||
| 790 | $mansec = $1; | ||
| 791 | } | ||
| 792 | my @manbrowser; | ||
| 793 | if (exists $ENV{TKPODMANVIEWER} && $ENV{TKPODMANVIEWER} eq "internal") { | ||
| 794 | DEBUG and warn "Use internal man viewer\n"; | ||
| 795 | } else { | ||
| 796 | my $manurl = "man:$man($mansec)"; | ||
| 797 | if (defined $sec && $sec ne "") { | ||
| 798 | $manurl .= "#$sec"; | ||
| 799 | } | ||
| 800 | DEBUG and warn "Try to start any man browser for $manurl\n"; | ||
| 801 | @manbrowser = ('gnome-help-browser', 'khelpcenter'); | ||
| 802 | my $wm = detect_window_manager($w); | ||
| 803 | DEBUG and warn "Window manager system is $wm\n"; | ||
| 804 | if ($wm eq 'kde') { | ||
| 805 | unshift @manbrowser, 'khelpcenter'; | ||
| 806 | } | ||
| 807 | if (defined $ENV{TKPODMANVIEWER}) { | ||
| 808 | unshift @manbrowser, $ENV{TKPODMANVIEWER}; | ||
| 809 | } | ||
| 810 | for my $manbrowser (@manbrowser) { | ||
| 811 | DEBUG and warn "Try $manbrowser...\n"; | ||
| 812 | if (is_in_path($manbrowser)) { | ||
| 813 | if (fork == 0) { | ||
| 814 | DEBUG and warn "Use $manbrowser...\n"; | ||
| 815 | exec($manbrowser, $manurl); | ||
| 816 | die $!; | ||
| 817 | } | ||
| 818 | return; | ||
| 819 | } | ||
| 820 | } | ||
| 821 | } | ||
| 822 | if (!$w->InternalManViewer($mansec, $man)) { | ||
| 823 | $w->_die_dialog("No useable man browser found. Tried @manbrowser and internal man viewer via `man'"); | ||
| 824 | } | ||
| 825 | } | ||
| 826 | |||
| 827 | sub InternalManViewer { | ||
| 828 | my($w, $mansec, $man) = @_; | ||
| 829 | my $man_exe = "man"; | ||
| 830 | if (!is_in_path($man_exe)) { | ||
| 831 | if ($^O eq 'MSWin32') { | ||
| 832 | $man_exe = "c:/cygwin/bin/man.exe"; | ||
| 833 | if (!-e $man_exe) { | ||
| 834 | return 0; | ||
| 835 | } | ||
| 836 | } else { | ||
| 837 | return 0; | ||
| 838 | } | ||
| 839 | } | ||
| 840 | my $t = $w->Toplevel(-title => "Manpage $man($mansec)"); | ||
| 841 | my $font_size = $w->base_font_size; | ||
| 842 | my $more = $t->Scrolled("More", | ||
| 843 | -font => "Courier $font_size", | ||
| 844 | -scrollbars => $Tk::platform eq 'MSWin32' ? 'e' : 'w', | ||
| 845 | )->pack(-fill => "both", -expand => 1); | ||
| 846 | $more->tagConfigure("bold", -font => "Courier $font_size bold"); | ||
| 847 | my $menu = $more->menu; | ||
| 848 | $t->configure(-menu => $menu); | ||
| 849 | local $SIG{PIPE} = "IGNORE"; | ||
| 850 | my $can_langinfo = $] >= 5.008 && eval { require I18N::Langinfo; 1 }; | ||
| 851 | local $ENV{LANG} = $ENV{LANG}; | ||
| 852 | if (!$can_langinfo) { | ||
| 853 | $ENV{LANG} = "C"; | ||
| 854 | } | ||
| 855 | open(MAN, $man_exe . (defined $mansec ? " $mansec" : "") . " $man |") | ||
| 856 | or die $!; | ||
| 857 | if ($can_langinfo) { | ||
| 858 | my $codeset = I18N::Langinfo::langinfo(I18N::Langinfo::CODESET()); | ||
| 859 | eval qq{ binmode MAN, q{:encoding($codeset)} }; | ||
| 860 | warn $@ if $@; | ||
| 861 | } | ||
| 862 | if (eof MAN) { | ||
| 863 | $more->insert("end", "No entry for for $man" . (defined $mansec ? " in section $mansec of" : "") . " the manual"); | ||
| 864 | } else { | ||
| 865 | while(<MAN>) { | ||
| 866 | chomp; | ||
| 867 | (my $line = $_) =~ s/.\cH//g; | ||
| 868 | my @bold; | ||
| 869 | while (/(.*?)((?:(.)(\cH\3)+)+)/g) { | ||
| 870 | my($pre, $bm) = ($1, $2); | ||
| 871 | $pre =~ s/.\cH//g; | ||
| 872 | $bm =~ s/.\cH//g; | ||
| 873 | push @bold, length $pre, length $bm; | ||
| 874 | } | ||
| 875 | if (@bold) { | ||
| 876 | my $is_bold = 0; | ||
| 877 | foreach my $length (@bold) { | ||
| 878 | if ($length > 0) { | ||
| 879 | (my($s), $line) = $line =~ /^(.{$length})(.*)/; | ||
| 880 | $more->insert("end", $s, $is_bold ? "bold" : ()); | ||
| 881 | } | ||
| 882 | $is_bold = 1 - $is_bold; | ||
| 883 | } | ||
| 884 | $more->insert("end", "$line\n"); | ||
| 885 | } else { | ||
| 886 | $more->insert("end", "$line\n"); | ||
| 887 | } | ||
| 888 | } | ||
| 889 | } | ||
| 890 | close MAN; | ||
| 891 | 1; | ||
| 892 | } | ||
| 893 | |||
| 894 | sub EnterLink { | ||
| 895 | my $w = shift; | ||
| 896 | $w->configure(-cursor=>'hand2'); | ||
| 897 | } | ||
| 898 | |||
| 899 | sub LeaveLink { | ||
| 900 | my $w = shift; | ||
| 901 | $w->configure(-cursor=>undef); | ||
| 902 | } | ||
| 903 | |||
| 904 | sub SearchFullText { | ||
| 905 | my $w = shift; | ||
| 906 | unless (defined $IDX && $IDX->IsWidget) { | ||
| 907 | require Tk::Pod::Search; # | ||
| 908 | $IDX = $w->Toplevel(-title=>'Perl Library Full Text Search'); | ||
| 909 | $IDX->transient($w); | ||
| 910 | |||
| 911 | my $current_path; | ||
| 912 | my $tree_sw = $w->parent->Subwidget("tree"); | ||
| 913 | if ($tree_sw) { | ||
| 914 | $current_path = $tree_sw->GetCurrentPodPath; | ||
| 915 | } | ||
| 916 | |||
| 917 | $IDX->PodSearch( | ||
| 918 | -command => | ||
| 919 | sub { | ||
| 920 | my($pod, %args) = @_; | ||
| 921 | $w->configure('-file' => $pod); | ||
| 922 | $w->focus; | ||
| 923 | my $more = $w->Subwidget('more'); | ||
| 924 | $more->SearchText | ||
| 925 | (-direction => 'Next', | ||
| 926 | -quiet => 1, | ||
| 927 | -searchterm => $args{-searchterm}, | ||
| 928 | -onlymatch => 1, | ||
| 929 | ); | ||
| 930 | }, | ||
| 931 | -currentpath => $current_path, | ||
| 932 | )->pack(-fill=>'both',-expand=>'both'); | ||
| 933 | # XXX A very rough solution: | ||
| 934 | $IDX->Button(-text => "Rebuild search index", | ||
| 935 | -command => sub { | ||
| 936 | my $installscriptdir = $Config{'installscript'}; | ||
| 937 | my $perlindex = 'perlindex'; | ||
| 938 | if (-d $installscriptdir) | ||
| 939 | { | ||
| 940 | $perlindex = "$installscriptdir/perlindex"; | ||
| 941 | if (!-f $perlindex) | ||
| 942 | { | ||
| 943 | $w->_error_dialog("perlindex was expected in the path '$perlindex', but not found. Cannot build search index."); | ||
| 944 | return; | ||
| 945 | } | ||
| 946 | } | ||
| 947 | my $pw_bg_msg = "The next dialog will ask for the root password. The search index building will happen in background."; | ||
| 948 | if (!is_in_path("gksu")) | ||
| 949 | { | ||
| 950 | if (!is_in_path("xsu")) | ||
| 951 | { | ||
| 952 | $w->_error_dialog("gksu or xsu needed to start perlindex"); | ||
| 953 | return; | ||
| 954 | } | ||
| 955 | $w->_warn_dialog($pw_bg_msg); | ||
| 956 | if (fork == 0) | ||
| 957 | { | ||
| 958 | system('xsu', | ||
| 959 | '--command', "$perlindex -index", | ||
| 960 | '--username', 'root', | ||
| 961 | '--title' => 'Rebuild search index', | ||
| 962 | '--set-display' => $w->screen, | ||
| 963 | ); | ||
| 964 | CORE::exit(0); | ||
| 965 | } | ||
| 966 | } | ||
| 967 | else | ||
| 968 | { | ||
| 969 | $w->_warn_dialog($pw_bg_msg); | ||
| 970 | if (fork == 0) | ||
| 971 | { | ||
| 972 | system('gksu', | ||
| 973 | '--user', 'root', | ||
| 974 | #'--description', 'Rebuild search index', | ||
| 975 | "perlindex -index", | ||
| 976 | ); | ||
| 977 | CORE::exit(0); | ||
| 978 | } | ||
| 979 | } | ||
| 980 | } | ||
| 981 | )->pack(-fill => 'x'); | ||
| 982 | $IDX->Button(-text => "Close", | ||
| 983 | -command => sub { $IDX->destroy }, | ||
| 984 | )->pack(-fill => 'x'); | ||
| 985 | } | ||
| 986 | $IDX->deiconify; | ||
| 987 | $IDX->raise; | ||
| 988 | $IDX->bind('<Escape>' => [$IDX, 'destroy']); | ||
| 989 | (($IDX->children)[0])->focus; | ||
| 990 | } | ||
| 991 | |||
| 992 | sub _need_File_Temp { | ||
| 993 | my $w = shift; | ||
| 994 | if (!eval { require File::Temp; 1 }) { | ||
| 995 | $w->_die_dialog("The perl module 'File::Temp' is missing"); | ||
| 996 | } | ||
| 997 | } | ||
| 998 | |||
| 999 | sub Print { | ||
| 1000 | my $w = shift; | ||
| 1001 | |||
| 1002 | my($text, $path); | ||
| 1003 | $path = $w->cget(-path); | ||
| 1004 | if (defined $path) { | ||
| 1005 | if (!-r $path) { | ||
| 1006 | $w->_die_dialog("Cannot find file `$path`"); | ||
| 1007 | } | ||
| 1008 | } else { | ||
| 1009 | $text = $w->cget("-text"); | ||
| 1010 | $w->_need_File_Temp; | ||
| 1011 | my($fh,$fname) = File::Temp::tempfile(UNLINK => 1, | ||
| 1012 | SUFFIX => "_tkpod.pod"); | ||
| 1013 | print $fh $text; | ||
| 1014 | close $fh; | ||
| 1015 | $path = $fname; | ||
| 1016 | } | ||
| 1017 | |||
| 1018 | if ($ENV{'TKPODPRINT'}) { | ||
| 1019 | my @cmd = _substitute_cmd($ENV{'TKPODPRINT'}, $path); | ||
| 1020 | DEBUG and warn "Running @cmd\n"; | ||
| 1021 | system @cmd; | ||
| 1022 | return; | ||
| 1023 | } elsif ($^O =~ m/Win32/) { | ||
| 1024 | return $w->Print_MSWin($path); | ||
| 1025 | } | ||
| 1026 | # otherwise fall thru... | ||
| 1027 | |||
| 1028 | my $success = $w->_print_pod_unix($path); | ||
| 1029 | |||
| 1030 | if (!$success) { | ||
| 1031 | $w->_error_dialog("Can't print on your system.\nEither pod2man, groff,\ngv or ghostview are missing."); | ||
| 1032 | } | ||
| 1033 | } | ||
| 1034 | |||
| 1035 | sub _print_pod_unix { | ||
| 1036 | my($w, $path) = @_; | ||
| 1037 | if (is_in_path("pod2man") && is_in_path("groff")) { | ||
| 1038 | my $pod2ps_pipe = "pod2man $path | groff -man -Tps"; | ||
| 1039 | |||
| 1040 | if ($^O eq 'darwin') { | ||
| 1041 | my $cmd = "$pod2ps_pipe | /usr/bin/open -a Preview -f"; | ||
| 1042 | system($cmd) == 0 | ||
| 1043 | or $w->_die_dialog("Error while executing <$cmd>. Status code is $?"); | ||
| 1044 | return 1; | ||
| 1045 | } | ||
| 1046 | |||
| 1047 | # XXX maybe determine user's environment (GNOME vs. KDE vs. plain X11)? | ||
| 1048 | my $gv = is_in_path("gv") | ||
| 1049 | || is_in_path("ghostview") | ||
| 1050 | || is_in_path("ggv") # newer versions seem to work | ||
| 1051 | || is_in_path("kghostview"); | ||
| 1052 | if ($gv) { | ||
| 1053 | $w->_need_File_Temp; | ||
| 1054 | |||
| 1055 | my($fh,$fname) = File::Temp::tempfile(SUFFIX => "_tkpod.ps"); | ||
| 1056 | system("$pod2ps_pipe > $fname"); | ||
| 1057 | push @tempfiles, $fname; | ||
| 1058 | my $pid = fork; | ||
| 1059 | if (!defined $pid) { | ||
| 1060 | die "Can't fork: $!"; | ||
| 1061 | } | ||
| 1062 | if ($pid == 0) { | ||
| 1063 | exec($gv, $fname); | ||
| 1064 | warn "Exec of $gv $fname failed: $!"; | ||
| 1065 | CORE::exit(1); | ||
| 1066 | } | ||
| 1067 | push @gv_pids, $pid; | ||
| 1068 | return 1; | ||
| 1069 | } | ||
| 1070 | } | ||
| 1071 | return 0; | ||
| 1072 | } | ||
| 1073 | |||
| 1074 | |||
| 1075 | sub _substitute_cmd { | ||
| 1076 | my($cmd, $path) = @_; | ||
| 1077 | my @cmd; | ||
| 1078 | if ($cmd =~ /%s/) { | ||
| 1079 | ($cmd[0] = $cmd) =~ s/%s/$path/g; | ||
| 1080 | } else { | ||
| 1081 | @cmd = ($cmd, $path); | ||
| 1082 | } | ||
| 1083 | @cmd; | ||
| 1084 | } | ||
| 1085 | |||
| 1086 | sub Print_MSWin { | ||
| 1087 | my($self, $path) = @_; | ||
| 1088 | my $is_old; | ||
| 1089 | $is_old = 1 if | ||
| 1090 | defined(&Win32::GetOSVersion) and | ||
| 1091 | eval {require Win32; 1} and | ||
| 1092 | defined(&Win32::GetOSName) and | ||
| 1093 | (Win32::GetOSName() eq 'Win32s' or Win32::GetOSName() eq 'Win95'); | ||
| 1094 | require POSIX; # XXX should be probably replaced by File::Temp, but I have no Win machine to test... | ||
| 1095 | |||
| 1096 | my $temp = POSIX::tmpnam(); # XXX it never gets deleted | ||
| 1097 | $temp =~ tr{/}{\\}; | ||
| 1098 | $temp =~ s/\.$//; | ||
| 1099 | DEBUG and warn "Using $temp as the temp file for hardcopying\n"; | ||
| 1100 | # XXX cleanup of temp file? | ||
| 1101 | |||
| 1102 | if($is_old) { # so we can't assume that write.exe can handle RTF | ||
| 1103 | require Pod::Simple::Text; | ||
| 1104 | require Text::Wrap; | ||
| 1105 | local $Text::Wrap::columns = 65; # reasonable number, I think. | ||
| 1106 | $temp .= '.txt'; | ||
| 1107 | Pod::Simple::Text->parse_from_file($path, $temp); | ||
| 1108 | system("notepad.exe", "/p", $temp); | ||
| 1109 | |||
| 1110 | } else { # Assume that our write.exe should understand RTF | ||
| 1111 | require Pod::Simple::RTF; | ||
| 1112 | $temp .= '.rtf'; | ||
| 1113 | Pod::Simple::RTF->parse_from_file($path, $temp); | ||
| 1114 | system("write.exe", "/p", "\"$temp\""); | ||
| 1115 | } | ||
| 1116 | |||
| 1117 | return; | ||
| 1118 | } | ||
| 1119 | |||
| 1120 | sub PrintHasDialog { $^O ne 'MSWin32' } | ||
| 1121 | |||
| 1122 | # Return $first and $last indices of the word under $index | ||
| 1123 | sub _word_under_index { | ||
| 1124 | my($w, $index)= @_; | ||
| 1125 | my ($first,$last); | ||
| 1126 | $first = $w->search(qw/-backwards -regexp --/, '[^\w:]', $index, "$index linestart"); | ||
| 1127 | $first = $w->index("$first + 1c") if $first; | ||
| 1128 | $first = $w->index("$index linestart") unless $first; | ||
| 1129 | $last = $w->search(qw/-regexp --/, '[^\w:]', $index, "$index lineend"); | ||
| 1130 | $last = $w->index("$index lineend") unless $last; | ||
| 1131 | ($first, $last); | ||
| 1132 | } | ||
| 1133 | |||
| 1134 | sub SelectToModule { | ||
| 1135 | my($w, $index)= @_; | ||
| 1136 | my ($first,$last) = $w->_word_under_index($index); | ||
| 1137 | if ($first && $last) { | ||
| 1138 | $w->tagRemove('sel','1.0',$first); | ||
| 1139 | $w->tagAdd('sel',$first,$last); | ||
| 1140 | $w->tagRemove('sel',$last,'end'); | ||
| 1141 | $w->idletasks; | ||
| 1142 | } | ||
| 1143 | } | ||
| 1144 | |||
| 1145 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 1146 | |||
| 1147 | # Add the file $file (with optional text index position $index) to the | ||
| 1148 | # history. | ||
| 1149 | sub history_add { | ||
| 1150 | my ($w,$what,$index) = @_; | ||
| 1151 | my($file, $text); | ||
| 1152 | if (ref $what eq 'HASH') { | ||
| 1153 | $file = $what->{file}; | ||
| 1154 | $text = $what->{text}; | ||
| 1155 | } else { | ||
| 1156 | $file = $what; | ||
| 1157 | $what = {file => $file}; | ||
| 1158 | } | ||
| 1159 | if (defined $file) { | ||
| 1160 | unless (-f $file) { | ||
| 1161 | $w->messageBox(-message => "Not a file '$file'. Can't add to history\n", | ||
| 1162 | @{&HISTORY_DIALOG_ARGS}); | ||
| 1163 | return; | ||
| 1164 | } | ||
| 1165 | } | ||
| 1166 | my $hist = $w->privateData()->{history}; | ||
| 1167 | my $hist_entry = Tk::Pod::Text::_HistoryEntry->create($what, $index, $w->{pod_title}); | ||
| 1168 | $hist->[++$w->privateData()->{history_index}] = $hist_entry; | ||
| 1169 | splice @$hist, $w->privateData()->{history_index}+1; | ||
| 1170 | $w->history_view_update; | ||
| 1171 | $w->history_view_select; | ||
| 1172 | $w->_history_navigation_update; | ||
| 1173 | undef; | ||
| 1174 | } | ||
| 1175 | |||
| 1176 | # Perform a "history back" operation, if possible. The current page is | ||
| 1177 | # updated in the history. | ||
| 1178 | sub history_back { | ||
| 1179 | my ($w) = @_; | ||
| 1180 | my $hist = $w->privateData()->{history}; | ||
| 1181 | if (!@$hist) { | ||
| 1182 | $w->messageBox(-message => "History is empty", | ||
| 1183 | @{&HISTORY_DIALOG_ARGS}); | ||
| 1184 | return; | ||
| 1185 | } | ||
| 1186 | if ($w->privateData()->{history_index} <= 0) { | ||
| 1187 | $w->messageBox(-message => "Can't go back in history", | ||
| 1188 | @{&HISTORY_DIALOG_ARGS}); | ||
| 1189 | return; | ||
| 1190 | } | ||
| 1191 | |||
| 1192 | $w->history_modify_entry; | ||
| 1193 | |||
| 1194 | $hist->[--$w->privateData()->{history_index}]; | ||
| 1195 | } | ||
| 1196 | |||
| 1197 | # Perform a "history forward" operation, if possible. The current page is | ||
| 1198 | # updated in the history. | ||
| 1199 | sub history_forward { | ||
| 1200 | my ($w) = @_; | ||
| 1201 | my $hist = $w->privateData()->{history}; | ||
| 1202 | if (!@$hist) { | ||
| 1203 | $w->messageBox(-message => "History is empty", | ||
| 1204 | @{&HISTORY_DIALOG_ARGS}); | ||
| 1205 | return; | ||
| 1206 | } | ||
| 1207 | if ($w->privateData()->{history_index} >= $#$hist) { | ||
| 1208 | $w->messageBox(-message => "Can't go forward in history", | ||
| 1209 | @{&HISTORY_DIALOG_ARGS}); | ||
| 1210 | return; | ||
| 1211 | } | ||
| 1212 | |||
| 1213 | $w->history_modify_entry; | ||
| 1214 | |||
| 1215 | $hist->[++$w->privateData()->{history_index}]; | ||
| 1216 | } | ||
| 1217 | |||
| 1218 | # Private method: update the pod view if called from a history back/forward | ||
| 1219 | # operation. This method will set the specified _HistoryEntry object. | ||
| 1220 | sub _history_update { | ||
| 1221 | my($w, $hist_entry) = @_; | ||
| 1222 | if ($hist_entry) { | ||
| 1223 | if (defined $hist_entry->file) { | ||
| 1224 | if ($w->cget('-path') ne $hist_entry->file) { | ||
| 1225 | $w->privateData()->{'from_history'} = 1; | ||
| 1226 | $w->configure('-file' => $hist_entry->file); | ||
| 1227 | $w->privateData()->{'from_history'} = 0; | ||
| 1228 | } | ||
| 1229 | } elsif (defined $hist_entry->text) { | ||
| 1230 | $w->privateData()->{'from_history'} = 1; | ||
| 1231 | $w->configure('-text' => $hist_entry->text); | ||
| 1232 | $w->privateData()->{'from_history'} = 0; | ||
| 1233 | } | ||
| 1234 | $w->_history_navigation_update; | ||
| 1235 | $w->afterIdle(sub { $w->see($hist_entry->index) }) | ||
| 1236 | if $hist_entry->index; | ||
| 1237 | } | ||
| 1238 | } | ||
| 1239 | |||
| 1240 | sub _history_navigation_update { | ||
| 1241 | my $w = shift; | ||
| 1242 | # XXX Be careful with the search pattern | ||
| 1243 | # if I decide to I18N Tk::Pod one day... | ||
| 1244 | my $m_history; | ||
| 1245 | if ($w->parent and $m_history = $w->parent->Subwidget("menubar")) { | ||
| 1246 | $m_history = $m_history->entrycget("History", "-menu"); | ||
| 1247 | my $inx = $w->privateData()->{history_index}; | ||
| 1248 | if ($inx == 0) { | ||
| 1249 | $m_history->entryconfigure("Back", -state => "disabled"); | ||
| 1250 | } else { | ||
| 1251 | $m_history->entryconfigure("Back", -state => "normal"); | ||
| 1252 | } | ||
| 1253 | if ($inx == $#{$w->privateData()->{history}}) { | ||
| 1254 | $m_history->entryconfigure("Forward", -state => "disabled"); | ||
| 1255 | } else { | ||
| 1256 | $m_history->entryconfigure("Forward", -state => "normal"); | ||
| 1257 | } | ||
| 1258 | } | ||
| 1259 | } | ||
| 1260 | |||
| 1261 | # Move the history backward ($inc == -1) or forward ($inc == +1) | ||
| 1262 | sub history_move { | ||
| 1263 | my($w, $inc) = @_; | ||
| 1264 | my $hist_entry = ($inc == -1 ? $w->history_back : $w->history_forward); | ||
| 1265 | $w->_history_update($hist_entry); | ||
| 1266 | $w->history_view_select; | ||
| 1267 | } | ||
| 1268 | |||
| 1269 | # Set the history to the given index $inx. | ||
| 1270 | sub history_set { | ||
| 1271 | my($w, $inx) = @_; | ||
| 1272 | if ($inx >= 0 && $inx <= $#{$w->privateData()->{history}}) { | ||
| 1273 | $w->history_modify_entry; | ||
| 1274 | $w->privateData()->{history_index} = $inx; | ||
| 1275 | $w->_history_update($w->privateData()->{history}->[$inx]); | ||
| 1276 | } | ||
| 1277 | } | ||
| 1278 | |||
| 1279 | # Modify the index (position) information of the current history entry. | ||
| 1280 | sub history_modify_entry { | ||
| 1281 | my $w = shift; | ||
| 1282 | if ($w->privateData()->{'history_index'} >= 0) { | ||
| 1283 | my $entry = $w->privateData()->{'history'}->[$w->privateData()->{'history_index'}]; | ||
| 1284 | $entry->index($w->index('@0,0')); | ||
| 1285 | } | ||
| 1286 | } | ||
| 1287 | |||
| 1288 | # Modify the pod title of the current history entry. | ||
| 1289 | sub history_modify_current_title { | ||
| 1290 | my $w = shift; | ||
| 1291 | my $pod_title = $w->{pod_title}; | ||
| 1292 | if (defined $pod_title) { | ||
| 1293 | my $history_index = $w->privateData()->{'history_index'}; | ||
| 1294 | if ($history_index >= 0) { | ||
| 1295 | my $entry = $w->privateData()->{'history'}->[$history_index]; | ||
| 1296 | $entry->pod_title($pod_title); | ||
| 1297 | $w->history_view_update; | ||
| 1298 | $w->history_view_select; | ||
| 1299 | } | ||
| 1300 | } | ||
| 1301 | } | ||
| 1302 | |||
| 1303 | # Create a new history view toplevel or reuse an old one. | ||
| 1304 | sub history_view { | ||
| 1305 | my $w = shift; | ||
| 1306 | my $t = $w->privateData()->{'history_view_toplevel'}; | ||
| 1307 | if (!$t || !Tk::Exists($t)) { | ||
| 1308 | $t = $w->Toplevel(-title => 'History'); | ||
| 1309 | $t->transient($w); | ||
| 1310 | $w->privateData()->{'history_view_toplevel'} = $t; | ||
| 1311 | my $lb = $t->Scrolled("Listbox", -scrollbars => 'oso'.($Tk::platform eq 'MSWin32'?'e':'w'))->pack(-fill => "both", '-expand' => 1); | ||
| 1312 | $t->Advertise(Lb => $lb); | ||
| 1313 | $lb->bind("<1>" => sub { | ||
| 1314 | my $lb = shift; | ||
| 1315 | my $y = $lb->XEvent->y; | ||
| 1316 | $w->history_set($lb->nearest($y)); | ||
| 1317 | }); | ||
| 1318 | $lb->bind("<Return>" => sub { | ||
| 1319 | my $lb = shift; | ||
| 1320 | my $sel = $lb->curselection; | ||
| 1321 | return if !defined $sel; | ||
| 1322 | $w->history_set($sel); | ||
| 1323 | }); | ||
| 1324 | $t->Button(-text => "Close", | ||
| 1325 | -command => sub { $t->destroy }, | ||
| 1326 | )->pack(-fill => 'x'); | ||
| 1327 | } | ||
| 1328 | $t->deiconify; | ||
| 1329 | $t->raise; | ||
| 1330 | $w->history_view_update; | ||
| 1331 | $w->history_view_select; | ||
| 1332 | } | ||
| 1333 | |||
| 1334 | # Re-fill the history view with the current history array. | ||
| 1335 | sub history_view_update { | ||
| 1336 | my $w = shift; | ||
| 1337 | my $t = $w->privateData()->{'history_view_toplevel'}; | ||
| 1338 | if ($t && Tk::Exists($t)) { | ||
| 1339 | my $lb = $t->Subwidget('Lb'); | ||
| 1340 | $lb->delete(0, "end"); | ||
| 1341 | foreach my $histentry (@{$w->privateData()->{'history'}}) { | ||
| 1342 | $lb->insert("end", $histentry->get_label); | ||
| 1343 | } | ||
| 1344 | } | ||
| 1345 | } | ||
| 1346 | |||
| 1347 | # Move the history view selection to the current selected history entry. | ||
| 1348 | sub history_view_select { | ||
| 1349 | my $w = shift; | ||
| 1350 | my $t = $w->privateData()->{'history_view_toplevel'}; | ||
| 1351 | if ($t && Tk::Exists($t)) { | ||
| 1352 | my $lb = $t->Subwidget('Lb'); | ||
| 1353 | $lb->selectionClear(0, "end"); | ||
| 1354 | $lb->selectionSet($w->privateData()->{history_index}); | ||
| 1355 | } | ||
| 1356 | } | ||
| 1357 | |||
| 1358 | sub PostPopupMenu { | ||
| 1359 | my($w, $p_scr, $X, $Y) = @_; | ||
| 1360 | $w->{MenuX} = $X; | ||
| 1361 | $w->{MenuY} = $Y; | ||
| 1362 | $p_scr->PostPopupMenu($X, $Y); | ||
| 1363 | } | ||
| 1364 | |||
| 1365 | sub OpenPodBySelection { | ||
| 1366 | my($w) = @_; | ||
| 1367 | my $sel; | ||
| 1368 | Tk::catch { | ||
| 1369 | $sel = $w->SelectionGet('-selection' => ($Tk::platform eq 'MSWin32' | ||
| 1370 | ? "CLIPBOARD" | ||
| 1371 | : "PRIMARY")); | ||
| 1372 | }; | ||
| 1373 | $sel =~ s{\s}{}g; # no whitespace in Pod names possible | ||
| 1374 | $w->configure(-file => $sel); | ||
| 1375 | } | ||
| 1376 | |||
| 1377 | sub _die_dialog { | ||
| 1378 | shift->_error_dialog(@_); | ||
| 1379 | die; | ||
| 1380 | } | ||
| 1381 | |||
| 1382 | sub _error_dialog { | ||
| 1383 | my($w, $message) = @_; | ||
| 1384 | $w->messageBox( | ||
| 1385 | -title => "Tk::Pod Error", | ||
| 1386 | -message => $message, | ||
| 1387 | -icon => 'error', | ||
| 1388 | ); | ||
| 1389 | } | ||
| 1390 | |||
| 1391 | sub _warn_dialog { | ||
| 1392 | my($w, $message) = @_; | ||
| 1393 | $w->messageBox( | ||
| 1394 | -title => "Tk::Pod Warning", | ||
| 1395 | -message => $message, | ||
| 1396 | -icon => 'warning', | ||
| 1397 | ); | ||
| 1398 | } | ||
| 1399 | |||
| 1400 | sub cleanup_tempfiles { | ||
| 1401 | if (@tempfiles) { | ||
| 1402 | # first get rid of all possible zombies | ||
| 1403 | # before we can check with kill 0 => ... | ||
| 1404 | require POSIX; | ||
| 1405 | if (defined &POSIX::WNOHANG) { # defined everywhere? | ||
| 1406 | while (waitpid(-1, &POSIX::WNOHANG) > 0) { } | ||
| 1407 | } | ||
| 1408 | |||
| 1409 | my $gv_running; | ||
| 1410 | for my $pid (@gv_pids) { | ||
| 1411 | if (kill 0 => $pid) { | ||
| 1412 | $gv_running = 1; | ||
| 1413 | last; | ||
| 1414 | } | ||
| 1415 | } | ||
| 1416 | |||
| 1417 | if ($gv_running) { | ||
| 1418 | warn "A ghostscript (or equivalent) process is still running, won't delete temporary files: @tempfiles\n"; | ||
| 1419 | } else { | ||
| 1420 | for my $temp (@tempfiles) { | ||
| 1421 | unlink $temp; | ||
| 1422 | } | ||
| 1423 | @tempfiles = (); | ||
| 1424 | } | ||
| 1425 | } | ||
| 1426 | } | ||
| 1427 | |||
| 1428 | END { | ||
| 1429 | cleanup_tempfiles(); | ||
| 1430 | } | ||
| 1431 | |||
| 1432 | 1; | ||
| 1433 | |||
| 1434 | __END__ |