Python programs to create tags/TAGS files
Guido van Rossum
guido at cwi.nl
Wed Feb 27 23:52:58 AEST 1991
Here are two Python scripts that both serve as simple Python
programming examples and enhance the programming environment for
Python.
Both scripts process a bunch of python modules looking for function
and class definitions. "ptags" creates a "tags" file, for use with
vi, while "eptags" creates a "TAGS" file, usable with GNU Emacs.
This version of "ptags" is much faster than the one distributed
with Python, which was written before module "regexp" existed.
Merging in existing tags/TAGS files is left as an exercise for the
reader.
Good luck,
--Guido van Rossum <guido at cwi.nl>
PS: I posted a "python.el" to gnu.emacs.sources.
#! /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: eptags.py ptags.py
# Wrapped by guido at voorn.cwi.nl on Wed Feb 27 13:46:23 1991
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'eptags.py' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'eptags.py'\"
else
echo shar: Extracting \"'eptags.py'\" \(1056 characters\)
sed "s/^X//" >'eptags.py' <<'END_OF_FILE'
X#! /ufs/guido/bin/sgi/python
X
X# eptags
X#
X# Create a TAGS file for Python programs, usable with GNU Emacs (version 18).
X# Tagged are:
X# - functions (even inside other defs or classes)
X# - classes
X# Warns about files it cannot open.
X# No warnings about duplicate tags.
X
Ximport sys
Ximport regexp
X
Xdef main():
X outfp = open('TAGS', 'w')
X args = sys.argv[1:]
X for file in args:
X treat_file(file, outfp)
X
Xmatcher = regexp.compile('^[ \t]*(def|class)[ \t]+([a-zA-Z0-9_]+)[ \t]*\(')
X
Xdef treat_file(file, outfp):
X try:
X fp = open(file, 'r')
X except:
X print 'Cannot open', file
X return
X charno = 0
X lineno = 0
X tags = []
X size = 0
X while 1:
X line = fp.readline()
X if not line: break
X lineno = lineno + 1
X res = matcher.exec(line)
X if res:
X (a, b), (a1, b1), (a2, b2) = res
X name = line[a2:b2]
X pat = line[a:b]
X tag = pat + '\177' + `lineno` + ',' + `charno` + '\n'
X tags.append(name, tag)
X size = size + len(tag)
X charno = charno + len(line)
X outfp.write('\f\n' + file + ',' + `size` + '\n')
X for name, tag in tags:
X outfp.write(tag)
X
Xmain()
END_OF_FILE
if test 1056 -ne `wc -c <'eptags.py'`; then
echo shar: \"'eptags.py'\" unpacked with wrong size!
fi
chmod +x 'eptags.py'
# end of 'eptags.py'
fi
if test -f 'ptags.py' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'ptags.py'\"
else
echo shar: Extracting \"'ptags.py'\" \(1013 characters\)
sed "s/^X//" >'ptags.py' <<'END_OF_FILE'
X#! /ufs/guido/bin/sgi/python
X
X# ptags
X#
X# Create a tags file for Python programs, usable with vi.
X# Tagged are:
X# - functions (even inside other defs or classes)
X# - classes
X# - filenames
X# Warns about files it cannot open.
X# No warnings about duplicate tags.
X
Ximport sys
Ximport regexp
Ximport path
X
Xtags = [] # Modified global variable!
X
Xdef main():
X args = sys.argv[1:]
X for file in args: treat_file(file)
X if tags:
X fp = open('tags', 'w')
X tags.sort()
X for s in tags: fp.write(s)
X
Xmatcher = regexp.compile('^[ \t]*(def|class)[ \t]+([a-zA-Z0-9_]+)[ \t]*\(')
X
Xdef treat_file(file):
X try:
X fp = open(file, 'r')
X except:
X print 'Cannot open', file
X return
X base = path.basename(file)
X if base[-3:] = '.py': base = base[:-3]
X s = base + '\t' + file + '\t' + '1\n'
X tags.append(s)
X while 1:
X line = fp.readline()
X if not line: break
X res = matcher.exec(line)
X if res:
X (a, b), (a1, b1), (a2, b2) = res
X name = line[a2:b2]
X s = name + '\t' + file + '\t/^' + line[a:b] + '/\n'
X tags.append(s)
X
Xmain()
END_OF_FILE
if test 1013 -ne `wc -c <'ptags.py'`; then
echo shar: \"'ptags.py'\" unpacked with wrong size!
fi
chmod +x 'ptags.py'
# end of 'ptags.py'
fi
echo shar: End of shell archive.
exit 0
More information about the Alt.sources
mailing list