texfind.pl / last modification: 2020-01-30 14:16
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' && eval 9;exec perl -S $0 $argv:q'
        if 0;

#D \module
#D   [       file=texfind.pl,
#D        version=1998.05.10,
#D          title=\TEXFIND, 
#D       subtitle=searching files, 
#D         author=Hans Hagen,
#D           date=\currentdate,
#D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
#C
#C This module is part of the \CONTEXT\ macro||package and is
#C therefore copyrighted by \PRAGMA. See licen-en.pdf for 
#C details. 

# test with "doif(un|)defined"

use strict ; 
use Getopt::Long ;
use File::Find ;
use Cwd ;
use Tk ; 
use Tk::widgets  ; 
use Tk::ROText ;

use FindBin ;
use lib $FindBin::Bin ;
use path_tre ; 

my $FileSuffix    = 'tex' ; 
my $SearchString  = '' ; 
my $Recurse       = 0 ; 
my $NumberOfHits  = 0 ;
my $QuitSearch    = 0 ; 
my $Location      = '' ;
my $currentpath   = '.' ;

my @FileList ; 

my ($dw, $mw, $log, $sea, $fil, $num, $but, $dir, $loc) ; 

$mw = MainWindow -> new () ; 
$dw = MainWindow -> new () ; 

$mw -> protocol( 'WM_DELETE_WINDOW' => sub { exit } ) ;
$dw -> protocol( 'WM_DELETE_WINDOW' => sub { exit } ) ;

$log = $mw -> Scrolled  ( 'ROText' ,
                          -scrollbars => 'se'           ,
                                -font => 'courier'      ,
                                -wrap => 'none'         , 
                               -width => 65             ,
                              -height => 22             )
           -> pack      (       -side => 'bottom'       ,
                                -padx => 2              , 
                                -pady => 2              ,
                              -expand => 1              ,
                                -fill => 'both'         ) ;

$sea = $mw -> Entry  (  -textvariable => \$SearchString , 
                                -font => 'courier'      ,
                               -width => 20             )
           -> pack   (          -side => 'left'         , 
                                -padx => 2              , 
                                -pady => 2              ) ;

$fil = $mw -> Entry  (  -textvariable => \$FileSuffix   ,
                                -font => 'courier'      ,
                               -width => 5              ) 
           -> pack   (          -side => 'left'         , 
                                -padx => 2              , 
                                -pady => 2              ) ;

$but = $mw -> Checkbutton ( -variable => \$Recurse      ,
                                -text => 'recurse'      )  
           -> pack   (          -side => 'left'         ) ; 

$num = $mw -> Entry  (  -textvariable => \$NumberOfHits , 
                                -font => 'courier'      ,
                             -justify => 'right'        ,
                               -width => 5              )
           -> pack   (          -side => 'right'        , 
                                -padx => 2              , 
                                -pady => 2              ) ;

$loc = $mw -> Entry  (  -textvariable => \$Location     , 
                                -font => 'courier'      ,
                               -width => 8              )
           -> pack   (          -side => 'right'        , 
                                -padx => 2              , 
                                -pady => 2              ) ;

sub BuildDir 
  {  if (Exists($dir)) { $dir -> destroy } ;
     $dir = $dw -> Scrolled ( 'PathTree' ,
                               -scrollbars => 'se'         ) 
                -> pack     (      -expand => 1            , 
                                     -fill => 'both'       ,
                                     -padx => 2            , 
                                     -pady => 2            ) ;
     $dir -> configure      (        -font => 'courier'    ,
                                   -height => 24           , 
                                    -width => 65           , 
                         -selectbackground => 'blue3'      ,
                                -browsecmd => \&ChangePath ) ;
     $dir -> bind (&#39;<Return>'   , \&ShowFile  ) ; 
     $dir -> bind (&#39;<Double-1>' , \&ShowFile  ) }

BuildDir ;

sub ShowFile { $mw -> raise ; $sea -> focusForce } 
sub ShowPath { $dw -> raise ; $dir -> focusForce } 

$log -> tagConfigure ( &#39;found', -foreground => 'green3' ) ;
$log -> tagConfigure ( &#39;title', -foreground => 'blue3' ) ;

$sea -> bind (&#39;<Return>'   , \&LocateStrings  ) ;
$fil -> bind (&#39;<Return>'   , \&LocateStrings  ) ;
$loc -> bind (&#39;<Return>'   , \&ChangeLocation ) ; 
$log -> bind (&#39;<Return>'   , \&ShowPath       ) ; 

$sea -> bind (&#39;<KeyPress>' , \&QuitSearch     ) ;
$fil -> bind (&#39;<KeyPress>' , \&QuitSearch     ) ;
$loc -> bind (&#39;<KeyPress>' , \&QuitSearch     ) ; 

$sea -> bind (&#39;<Escape>'   , \&QuitSearch     ) ;
$fil -> bind (&#39;<Escape>'   , \&QuitSearch     ) ;
$loc -> bind (&#39;<Escape>'   , \&QuitSearch     ) ;
$log -> bind (&#39;<Escape>'   , \&QuitSearch     ) ;

$sea -> bind (&#39;<Double-1>' , \&LocateStrings  ) ; 
$fil -> bind (&#39;<Double-1>' , \&LocateStrings  ) ; 
$loc -> bind (&#39;<Double-1>' , \&ChangeLocation ) ; 
$log -> bind (&#39;<Double-1>' , \&ShowPath       ) ; 

sub ChangePath 
  { my $currentpath = shift ; 
chdir($currentpath) ; 
    $QuitSearch = 1 ; 
    $log -> delete (&#39;1.0', 'end') ;
    $log -> insert (&#39;end', "$currentpath\n\n", 'title') }

sub ChangeLocation
  { $QuitSearch = 1 ;
    $log -> delete (&#39;1.0', 'end') ;
    $Location =~ s/^\s*//o ;
    $Location =~ s/\s*$//o ;    
    $Location =~ s/(\\|\/\/)/\//go ;    
    unless (-d $Location) 
      { unless ($Location =~ /\//) { $Location .= &#39;/' } }
    if (-d $Location) 
      { $log -> insert (&#39;end', "changed to location '$Location'\n\n", 'title') ;
        $currentpath = $Location ;
        chdir ($currentpath) ;
        $dir -> destroy ; 
        BuildDir ;  
        $dw -> raise ; 
        $dw -> focusForce } 
    else
      { $log -> insert (&#39;end', "unknown location '$Location'\n\n", 'title') ;
        $Location = &#39;' } }

sub QuitSearch 
  { $QuitSearch = 1 } 

sub SearchFile 
  { my ($FileName, $SearchString) = @_ ; 
    my $Ok = 0 ; my $len ; 
    open (TEX, $FileName) ; 
    my $LineNumber = 0 ; 
    while (<TEX>) 
      { ++$LineNumber ; 
        if ($QuitSearch) 
          { if ($Ok) { $log -> see (&#39;end') }
            last } 
        if (/$SearchString/i)
          { ++$NumberOfHits ; $num -> update ; 
            unless ($Ok) 
              { $Ok = 1 ; 
                $log -> insert (&#39;end', "$FileName\n\n",'title') }
            $log -> insert (&#39;end', sprintf("%5i : ",$LineNumber), 'title') ;
            s/^\s*//o ;
#
            $len = 0 ; 
            while (/(.*?)($SearchString)/gi)
              { $len += length($1) + length($2) ;  
                $log -> insert (&#39;end', "$1") ; 
                $log -> insert (&#39;end', "$2", 'found' ) }
            $_ = substr($_,$len) ;  
            $log -> insert (&#39;end', "$_") ;
#
            $log -> update ;
            $log -> see (&#39;end') } } 
    if ($Ok) { $log -> insert (&#39;end', "\n") }
    close (TEX) } 

sub DoLocateFiles 
  { @FileList = () ; 
    $NumberOfHits = 0 ; 
    if ($FileSuffix ne "") 
      { $log -> delete (&#39;1.0', 'end') ;
        if ($Recurse)
          { $log -> insert (&#39;end', "recursively identifying files\n", 'title') ;
            $log -> see (&#39;end') ;
            find (\&wanted, $currentpath) ;
            sub wanted 
              { if ($QuitSearch) { last ; return } 
                if (/.*\.$FileSuffix/i) 
                  { ++$NumberOfHits ; $num -> update ; 
                    push @FileList, $File::Find::name } } } 
        else  
          { $log -> insert (&#39;end', "identifying files\n", 'title') ;
            $log -> see (&#39;end') ;
            opendir(DIR, $currentpath) ; my @TEMPLIST = readdir(DIR) ; closedir(DIR) ;
            foreach my $FileName (@TEMPLIST) 
              { if ($FileName =~ /.*\.$FileSuffix/i) 
                  { ++$NumberOfHits ; $num -> update ; 
                    if ($QuitSearch) 
                      { last } 
                    push @FileList, $FileName } } } 
        @FileList = sort @FileList } }

sub DoLocateStrings
  { $log -> delete (&#39;1.0', 'end') ; 
    $log -> update ; 
    $log -> see (&#39;end') ; 
    $NumberOfHits = 0 ; 
    if ($SearchString ne "") 
      { foreach my $FileName (@FileList) 
          { if ($QuitSearch) 
              { $log -> insert (&#39;end', "search aborted\n", 'title') ;
                $log -> see (&#39;end') ; 
                last } 
            SearchFile($FileName,$SearchString) } } 
    unless ($QuitSearch) 
      { $log -> insert (&#39;end', "done\n", 'title') ;
        $log -> see (&#39;end') } }

sub LocateStrings
  { $QuitSearch = 0 ; 
    DoLocateFiles() ; 
    DoLocateStrings() } 

$log -> insert (&#39;end', 

  "data fields\n\n" , &#39;' ,  


  "string   :", &#39;title', " regular expression to search for\n"   , '' ,
  "suffix   :", &#39;title', " type of file to search in\n"          , '' ,
  "recurse  :", &#39;title', " enable searching subpaths\n"          , '' ,
  "location :", &#39;title', " drive of root path\n"                 , '' ,
  "counter  :", &#39;title', " file/hit counter\n\n"                 , '' ,

  "key bindings\n\n" , &#39;' ,  

  "double 1 :", &#39;title', " directory window <-> search window\n" , '' ,
  "enter    :", &#39;title', " start searching\n"                    , '' ,
  "escape   :", &#39;title', " quit searching\n\n"                   , '' ,
  
  "current path\n\n" , &#39;' , 
 
  cwd(), &#39;title', "\n\n" , 'title' ) ; 

$log -> update ; 

ShowPath ; 

MainLoop() ;