#!/usr/bin/perl

=head1 NAME
  pttags - generate perl and tcl tags file

=head1 SYNOPSIS
  pttags file... >tags

=head1 DESCRIPTION
  This scans the files for perl functions  and  tcl  procedures,  and
  produces a vi(1) "tags" file on standard output.  This will make it
  possible for a vi user to use the -t option  and  the  :ta  command
  with perl and tcl code.

  This program's output may be merged with other tags files, such  as
  those  created by ctags(1).  Note that the tags file must be sorted
  alphabetically to work properly.  So if you want to run both  ctags
  and  pttags,  and  merge the results, you will need to do something
  like:

=begin text
  ctags -f tags_c *.[ch]
  pttags  > tags_pt *.p[lm] *.tcl *.w *.exp
  cat tags_* | sort > tags
=end text

=head1 OPTIONS
  None, so far.

=head1 FILES
  This program can't act as a "filter" and read from  stdin,  because
  it  must  know the names of the files that it's scanning.  The file
  names must go into the output, after all, so we  must  insist  that
  you list them on the command line.  Sorry about that.

=head1 BUGS
  This program only recognizes "sub foo" if the two tokens are at the
  start of a line.

  We  should  probably add code to recognize a few things in addition
  to functions.  Stay tuned ...

=head1 SEE ALSO
  ctags(1)

=head1 AUTHOR
  John Chambers <jc@trillian.mit.edu>
=cut

for $f (@ARGV) {
	if (!open(F,$f)) {
		print STDERR "$0: Can't read \"$f\" [$!]\n";
		next;
	}
	for $l (<F>) {
		if ($l =~ /^\s*sub\s*(\w+)\b/) {
			$tag{$1} = "$1\t$f\t/$&/";
		} elsif ($l =~ /^\s*proc\s*(\w+)\b/) {
			$tag{$1} = "$1\t$f\t/$&/";
		}
	}
}

for $k (sort keys %tag) {
	print $tag{$k} . "\n";
}
