home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sources.misc
- Path: sparky!kent
- From: dmeggins@acadvm1.uottawa.ca (David Megginson)
- Subject: v31i055: petags - generate emacs TAGS for arbitrary file types, Part01/01
- Message-ID: <1992Jul27.022913.1152@sparky.imd.sterling.com>
- Followup-To: comp.sources.d
- X-Md4-Signature: 1d9086f77a068e8dbf6fbc7ded3ac972
- Sender: kent@sparky.imd.sterling.com (Kent Landfield)
- Organization: University of Toronto - EPAS
- Date: Mon, 27 Jul 1992 02:29:13 GMT
- Approved: kent@sparky.imd.sterling.com
- Lines: 380
-
- Submitted-by: dmeggins@acadvm1.uottawa.ca (David Megginson)
- Posting-number: Volume 31, Issue 55
- Archive-name: petags/part01
- Environment: perl
-
- Required to run: Gnu Emacs, Perl
-
- I developed this program in an earlier version so that I could use the
- Gnu Emacs TAGS features with my 300-page thesis on Old English
- spelling, written in SGML using a modified version of the QWERTZ DTD.
- I decided to make the program more general and post it.
-
- This is by no means the final word on TAGS -- it does not actually
- parse files, it only looks for regular-expression matches -- but it is
- effective for a surprisingly large range of files. I have included a
- detailed README.
-
- If no one else writes anything better, I would be happy to see this
- script appear in the etc/ directory for the next Gnu Emacs distribution
- (I have released it under the Gnu license).
-
- Enjoy!
-
- David Megginson
- dmeggins@acadvm1.uottawa.ca
-
- ==============================CUT HERE==============================
- #! /bin/sh
- # This is a shell archive. Remove anything before this line, then unpack
- # it by saving it into a file and typing "sh file". To overwrite existing
- # files, type "sh file -c". You can also feed this as standard input via
- # unshar, or by typing "sh <file", e.g.. If this archive is complete, you
- # will see the following message at the end:
- # "End of shell archive."
- # Contents: README petags
- # Wrapped by meggin@epas on Fri Jul 24 10:36:50 1992
- PATH=/bin:/usr/bin:/usr/ucb ; export PATH
- if test -f 'README' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'README'\"
- else
- echo shar: Extracting \"'README'\" \(5279 characters\)
- sed "s/^X//" >'README' <<'END_OF_FILE'
- X -*-Text-*-
- X
- X PETAGS Version 0.1:
- X
- X A perl script to generate emacs TAGS for arbitrary file types.
- X
- X
- X (c) Copyright 1992 by David Megginson (dmeggins@acadvm1.uottawa.ca)
- X
- X Petags is free software; you can redistribute it and/or modify it
- X under the terms of the GNU General Public License as published by the
- X Free Software Foundation; either version 1, or (at your option) any
- X later version.
- X
- X Petags is distributed in the hope that it will be useful, but WITHOUT
- X ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- X FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- X for more details.
- X
- X The program Perl is required to run this program, and Gnu Emacs is
- X required to use the TAGS file which it creates -- you should have
- X received a copy of the GNU General Public License along with both of
- X these programs; see the file COPYING. If not, write to the Free
- X Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
- X
- X
- XINTRODUCTION
- X
- XThis is my first attempt at generating emacs TAGS for arbitrary file
- Xtypes. Petags's usage is similar to that of tags or etags, except
- Xthat you must supply the program with a list of (perl-style)
- Xregular-expression patterns for the lines which you want tagged. The
- Xusage is as follows:
- X
- X petags [-e <pat> -f <file> -o <output> -d <dir>] FILES...
- X
- X -e <pat> Add all lines containing <pat> to the TAGS file.
- X -f <file> Like -e, but read patterns from <file>, one on each line.
- X -o <output> Use <output> rather than TAGS for the tag file name.
- X -d <dir> With the -f option, look for pattern files in <dir>
- X if they are not in the current directory.
- X -- End options.
- X
- X
- XPATTERN FILES
- X
- XThere must be at least one -e or -f option on the command line, or the
- Xprogram will fail. For general usage, it is probably best to make
- Xseveral pattern files and keep them in a central place, like
- X/usr/local/lib/perltags. You may specify this directory on the
- Xcommand line using the -d option, or in the environment variable
- XPETAGSDIR -- in either case, you MUST omit the final slash. The -d
- Xoption overrides the environment variable. Using this method, you can
- Xcreate a file of regular-expression patterns for the programming
- Xlanguage /usr/local/lib/perltags/foo, set PETAGSDIR to
- X"/usr/local/lib/perltags", then simply use the command
- X
- X petags -d foo *.pl
- X
- Xto create TAGS for every .pl file in the current directory.
- X
- X
- XUSING THE PROGRAM
- X
- XA _real_ tags program would actually parse its input files -- this
- Xprogram merely matches regular-expression patterns, so it is easy to
- Xconfuse. Unfortunately, the Gnu emacs TAGS file format does not seem
- Xto allow multi-line tags, so this program is confined to regular
- Xexpressions which appear entirely on a single line. Even with these
- Xlimitation (and depending on your formatting preferences), petags
- Xworks for a surprising number of file types. For example, in an SGML
- Xfile using the QWERTZ DTD, which might contain text like this:
- X
- X <chapt> Introduction
- X <label id="ChIntro">
- X
- X <sect> Background
- X
- X blah blah blah
- X
- X <sect1> History of the Background
- X
- X blah blah blah
- X
- Xyou could create a pattern file with these three regular expressions
- X(one on each line)
- X
- X <chapt
- X <sect
- X <label
- X
- Xto tag every chapter, section (and subsection) and label in the text.
- XOf course, if you have a label over a line break, like
- X
- X ...<label
- X id="LookHere">...
- X
- Xthe system falls apart, but if you format carefully, you should be
- Xable to use this program (I used an earlier version for my 300-page
- Xthesis on Old English spelling, and it worked perfectly). For Prolog,
- Xfor example, the pattern ":-" should guarantee that every regular
- Xpredicate appears in the TAGS file, and "-->" will also include
- Xgrammar rules. For Perl, the pattern "\bsub\b" should include all
- Xsubroutine definitions for Perl libraries, as long as the subroutine
- Xname appears on the same line as "sub".
- X
- X
- XTROUBLESHOOTING
- X
- X1) Make sure that you edit the script to set the correct path to Perl
- X (both in the #! line and in the "eval..." line). If you are using
- X MeSsyDOS, you must also change the value of the $dirsep variable
- X from "/" to "\\".
- X
- X2) This is a Perl script, and Perl uses a slightly different
- X regular-expression syntax than Gnu Emacs. In particular, "("
- X begins a group in Perl, while "\(" begins a group in Gnu Emacs.
- X
- X3) If you are using a Unix system, and your shell or kernel does not
- X understand #! notation, this script will still run directly if you
- X make it executable, but use of the -e command-line option is not
- X recommended (the characters will confuse the shell). In this case,
- X always put your patterns in a file and use the -f option.
- X
- X4) If you have set PETAGSDIR and it does not work, make sure that the
- X path does _not_ contain a trailing slash -- petags will add that
- X automatically.
- X
- X
- XI will be losing my access to Usenet news in a few days, when I move
- Xto Ottawa to begin teaching with the English department. I will,
- Xhowever, have an EMail account at dmeggins@acadvm1.uottawa.ca, and
- Xwould love to hear feedback. I will also be subscribing to several
- Xmailing lists which echo newsgroups, including the comp.text.sgml
- Xlist.
- X
- X
- XEnjoy!
- X
- XDavid Megginson
- X24 July, 1992
- X
- END_OF_FILE
- if test 5279 -ne `wc -c <'README'`; then
- echo shar: \"'README'\" unpacked with wrong size!
- fi
- # end of 'README'
- fi
- if test -f 'petags' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'petags'\"
- else
- echo shar: Extracting \"'petags'\" \(4339 characters\)
- sed "s/^X//" >'petags' <<'END_OF_FILE'
- X#!/usr/gnu/bin/perl
- X
- X# PETAGS Version 0.1
- X# A perl script to generate emacs TAGS for arbitrary file types.
- X
- X# (c) 1992 by David Megginson (dmeggins@acadvm1.uottawa.ca)
- X
- X# Petags is free software; you can redistribute it and/or modify it
- X# under the terms of the GNU General Public License as published by
- X# the Free Software Foundation; either version 1, or (at your option)
- X# any later version.
- X
- X# Petags is distributed in the hope that it will be useful, but
- X# WITHOUT ANY WARRANTY; without even the implied warranty of
- X# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- X# General Public License for more details.
- X
- X# The program Perl is required to run this program, and Gnu Emacs is
- X# required to use the TAGS file which it creates -- you should have
- X# received a copy of the GNU General Public License along with both of
- X# these programs; see the file COPYING. If not, write to the Free
- X# Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
- X
- Xeval "exec /usr/gnu/bin/perl -S $0 $*"
- X unless $running_under_some_shell;
- X
- X
- X#
- X# Configuration variables.
- X#
- X$dirsep = "/"; # Change for MeSsyDOS.
- X$pattern_dir = $ENV{"PETAGSDIR"} || '.';
- X # May be modified by &get_options.
- X # Note that environment variable overrides.
- X$tags_file = "TAGS"; # May be modified by &get_options.
- X
- X
- X#
- X# Startup stuff.
- X#
- X&get_options; # Do that command-line stuff.
- X
- X&usage("no patterns specified (use '-e' or '-f')") if ($#patterns < 0);
- X&usage("$0: no files specified") if ($#ARGV < 0);
- X
- Xopen(TAGS,">$tags_file") || die "$0: cannot write to $tags_file.\n";
- X
- X
- X#
- X# Main program loop: open each file, store matching lines (and count
- X# characters), then print out the whole thing in etags format.
- X#
- Xforeach $file (@ARGV) {
- X open(INPUT,$file) || die "$0: cannot open $file.\n";
- X while (<INPUT>) {
- X chop;
- X ploop: foreach $pattern (@patterns) {
- X if(/$pattern/) {
- X &add_line("$_\177$.,$position\n");
- X last ploop;
- X }
- X }
- X $position += length($_) + 1;
- X }
- X close(INPUT);
- X &flush_lines($file);
- X}
- X
- Xclose(TAGS);
- X
- X
- X#################################################################
- X# Subroutines to add information to the tags file.
- X#################################################################
- X
- X#
- X# Add a new line to the tags file.
- X#
- Xsub add_line {
- X local ($line) = @_;
- X
- X $total_characters += length($line);
- X push(lines,$line);
- X}
- X
- X#
- X# Flush lines to the tags file, with the file name and total characters.
- X#
- Xsub flush_lines {
- X local ($file) = @_;
- X
- X print TAGS "\f\n$file,$total_characters\n";
- X foreach $line (@lines) {
- X print TAGS $line;
- X }
- X @lines = ();
- X $total_characters = 0;
- X}
- X
- X
- X#################################################################
- X# Subroutines to initialise the program (arguments, options).
- X#################################################################
- X
- X#
- X# Get options from @ARGV array.
- X#
- Xsub get_options {
- X opts: while ($ARGV[0] =~ /^-/) {
- X $_ = shift(ARGV);
- X {
- X if (/^-e$/) { push(@patterns,shift(ARGV)); next opts; }
- X if (/^-f$/) { &read_patterns_from_file(shift(ARGV)); next opts; }
- X if (/^-o$/) { $tags_file = shift(ARGV); next opts; }
- X if (/^-d$/) { $pattern_dir = shift(ARGV); next opts; }
- X if (/^--$/) { last opts; }
- X &usage("illegal option '$_'");
- X }
- X }
- X}
- X
- X#
- X# Read a list of patterns from a file: called by get_options().
- X#
- Xsub read_patterns_from_file {
- X local ($file) = @_;
- X
- X $file = "$pattern_dir$dirsep$file" unless -r $file;
- X
- X open(FILE,$file) || die "$0: cannot open $file to read patterns.\n";
- X
- X while (<FILE>) {
- X chop;
- X push(@patterns, $_);
- X }
- X close(FILE);
- X}
- X
- X#
- X# Print the program's usage and quit.
- X#
- Xsub usage {
- X local ($errmsg) = @_;
- X
- X print STDERR <<END_OF_USAGE;
- XUsage:
- X
- X petags [-e <pat> -f <file> -o <output> -d <dir>] FILES...
- X
- X -e <pat> Add all lines containing <pat> to the TAGS file.
- X -f <file> Like -e, but read patterns from <file>, one on each line.
- X File may be in the current directory or in $PETAGSDIR/.
- X -o <output> Use <output> rather than TAGS for the tag file name.
- X -d <dir> With the -f option, look for pattern files in <dir>
- X if they are not in the current directory (see also
- X the environment variable PETAGSDIR.
- X -- End options.
- X
- XEND_OF_USAGE
- X
- X die "$0: $errmsg\n";
- X}
- END_OF_FILE
- if test 4339 -ne `wc -c <'petags'`; then
- echo shar: \"'petags'\" unpacked with wrong size!
- fi
- # end of 'petags'
- fi
- echo shar: End of shell archive.
- exit 0
-
- --
- o--------------- ---------------o
- |... |meggin@vm.epas.|
- |. .avid meggin|utoronto.ca on |
- |. .son.centre |the internet. |
- |... for mediev | |
- |al studies.univ| |
- |ersity of toron| .explicit. |
- |to | |
- | ..meggin| |
- o--------------- ---------------o
-
-
- exit 0 # Just in case...
-