Hi,

 A few years ago I wrote the short script ltags that is included in vim-latex.
 I attach an improved version, in case anybody wants to use it.
 Changes:
 * now it can read multiply-nested \input'd files
 * fixed some strange things that people brought to my attention,
   eg \include can now have whitespace in front, 
   commented-out lines are ignored,
   it doesn't unnecessarily read the source file more than once (as the 
original did)
 * small changes in the regular expression capture more things, eg arguments
   in \cite commands that spread over more than 1 line.

 Also, I attach a very brief script I use, let's call it bibgrep,
 which can search the bibliography database file using keywords, like this:
     bibgrep keyword1 keyword2 ... 
 will print the full bibitems that matched these keywords.
 I invoke this script from inside vim, typing ",b" and then the keywords,
 and the results are shown in a preview vim window - I use these in my .vimrc:

     map ,b  :Bibgrep
     command -nargs=* Bibgrep call SearchBib(<f-args>)
     function SearchBib(first, ...)
       let keywords = a:first
       for morekeywords in a:000
          let keywords = a:first . ' ' . morekeywords
       endfor
       cexpr system('/usr/local/bin/bibgrep' . ' ' . keywords)
       copen
     endfunction

-- 
 Dimitri Antoniou
 Department of Biophysics
 Albert Einstein College of Medicine      
 email  : [EMAIL PROTECTED]                 
#!/usr/bin/perl -w

# Author:  [EMAIL PROTECTED]
# usage:   ltags filename.tex
#     looks for \label, citation commands, \include|\input'd files
#     can search in multiply-nested \input'd files
#     ignores commented out lines
#     \cite can have multiple arguments, allowed to
#           spread over more than one line

# keywords that signify references
 $citewords = "cite|citep|citet|onlinecite|citeonline";

# suffixes of files that we don't want to search if \input'd
 $noinclsuffix = "toc|pic";

# get main LaTeX source file from command line:
 $mainfile = shift;

# open it, search it, create tags; create array of included files
 &search_file($mainfile);
 @tags = @unsortedtag;
 @includedfiles = @inputfiles;

# repeat for files that were \input'd or \include'd 
 while (@includedfiles) {
     @newincludedfiles = ();
     @inputfiles = ();
     # open \input'd files, create tags, look for nested \input'd files
     foreach $file (@includedfiles ){
         if     ( $file =~ m/\.$noinclsuffix/ ) {next}
         unless ( $file =~ m/\.tex/ ) { $file = $file . "\.tex" }
         # uncomment, if you want the filenames printed on the screen
         # print "$file\n";
         &search_file($file);
         push @newincludedfiles, @inputfiles;
         push @tags, @unsortedtag;
     }
     @includedfiles = @newincludedfiles;
 }

#  sort tags and eliminate duplicates
 @sortedtag = sort @tags;
 %seen = ();
 @uniqtag = grep { !$seen{$_}++ } @sortedtag;
 
 open(TAGS, "> tags");
 print TAGS @uniqtag;

######################################################################
 sub search_file {

     $srcfile = $_[0];
     @unsortedtag = ();
     @labelList = ();
     @citeList = ();

     open SRC, $srcfile or die "$!";
     $/='';   # allows to match across newlines
     # store contents of source file in array @texfile
     @texfile = <SRC>;
     close SRC;

     foreach $line (@texfile) {

         # skip comment lines
         if ( $line =~ m/\A\s*%.*?\Z/ ) {next}

         # save input'd files in an array
         while ( $line =~ /\\(?:input|include){(.*?)}/g ) {
             push @inputfiles, $1;
         }

         # see if we use an external database
         while ( $line =~ /\\bibliography{(.*?)}/g ) {
            ($dbase) = $1;
         }

         # store keywords of \label 
         while ( $line =~ /\\label{(.*?)}/g ) {
             push @labelList, $1;
         }

         # store keywords of references
         # because of $/='', the flag /s searches for keyword in next line too
         while ( $line =~ /\\(?:$citewords){(.*?)}/gs ) {
             push @citeList, $1  
         }
     } # finished with processing the latex source file

     # if we use blbliography database
     if ($dbase) {
        $bibfile = $dbase . "\.bib";
     }

     # create array of keywords of \label
     foreach (@labelList) {
          push @unsortedtag, "$_\t$srcfile\t/label{$_}/\n";
     }

     # create array of keywords of various \cite commands
     @mrefs = ();
     @refs = ();
     @multirefs = ();
     foreach $refs (@citeList) {

         # if \cite has more than one argument, split them:
         if ( $refs =~ /,/ ) {
             @mrefs = split /,/, $refs;

             # remove possible whitespace from the keywords
             foreach ( 0 .. $#mrefs ) {
                $mrefs[$_] =~ s/^\s+//;
                $mrefs[$_] =~ s/\s+$//;
             }
             push ( @multirefs, @mrefs );
         }
         else {
             push ( @multirefs, ($refs) );
         }

         # in BibTeX, format is @ARTICLE{Name, }; in source file, \bibitem{Name}
         for $ref (@multirefs) {
             if ($dbase) {
                 push @unsortedtag, "$ref\t$bibfile\t/{$ref,/\n";
             }
             else {
                 push @unsortedtag, "$ref\t$srcfile\t/bibitem{$ref}/\n";
             }
         }
     }
 }
#!/usr/bin/perl -w
# usage:   bibgrep keyword1 keyword2 ...

$/ = '';   # "paragraph slurp" mode; we want to print matched bibitems, not 
lines

$bibfile = "Full_path_to_your_Bibliography_file"; 
open BFILE, $bibfile or die "cannot open $bibfile";

BIBITEM: foreach $bibitem (<BFILE>) {
   foreach $word (@ARGV) {
       next BIBITEM unless ( $bibitem =~ m/$word/i );
   }
   print $bibitem;
}

Reply via email to