blob: 1c682d31be4290a5e75855f2b116e6dd87755d69 [file] [log] [blame]
Guido van Rossumec758ea1991-06-04 20:36:54 +00001#! /usr/local/python
2
3# eptags
4#
5# Create a TAGS file for Python programs, usable with GNU Emacs (version 18).
6# Tagged are:
7# - functions (even inside other defs or classes)
8# - classes
9# Warns about files it cannot open.
10# No warnings about duplicate tags.
11
12import sys
13import regexp
14
15def main():
16 outfp = open('TAGS', 'w')
17 args = sys.argv[1:]
18 for file in args:
19 treat_file(file, outfp)
20
21matcher = regexp.compile('^[ \t]*(def|class)[ \t]+([a-zA-Z0-9_]+)[ \t]*\(')
22
23def treat_file(file, outfp):
24 try:
25 fp = open(file, 'r')
26 except:
27 print 'Cannot open', file
28 return
29 charno = 0
30 lineno = 0
31 tags = []
32 size = 0
33 while 1:
34 line = fp.readline()
35 if not line: break
36 lineno = lineno + 1
37 res = matcher.exec(line)
38 if res:
39 (a, b), (a1, b1), (a2, b2) = res
40 name = line[a2:b2]
41 pat = line[a:b]
42 tag = pat + '\177' + `lineno` + ',' + `charno` + '\n'
43 tags.append(name, tag)
44 size = size + len(tag)
45 charno = charno + len(line)
46 outfp.write('\f\n' + file + ',' + `size` + '\n')
47 for name, tag in tags:
48 outfp.write(tag)
49
50main()