blob: 86b6ee17c417775eacd540e612153f812d82bc29 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossumec758ea1991-06-04 20:36:54 +00002
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
Guido van Rossum0cc19451992-08-31 10:54:06 +000013import regex
Guido van Rossumec758ea1991-06-04 20:36:54 +000014
15def main():
16 outfp = open('TAGS', 'w')
17 args = sys.argv[1:]
18 for file in args:
19 treat_file(file, outfp)
20
Guido van Rossum0cc19451992-08-31 10:54:06 +000021expr = '^[ \t]*\(def\|class\)[ \t]+\([a-zA-Z0-9_]+\)[ \t]*[:(]'
22matcher = regex.compile(expr)
Guido van Rossumec758ea1991-06-04 20:36:54 +000023
24def treat_file(file, outfp):
25 try:
26 fp = open(file, 'r')
27 except:
28 print 'Cannot open', file
29 return
30 charno = 0
31 lineno = 0
32 tags = []
33 size = 0
34 while 1:
35 line = fp.readline()
36 if not line: break
37 lineno = lineno + 1
Guido van Rossum0cc19451992-08-31 10:54:06 +000038 if matcher.search(line) >= 0:
39 (a, b), (a1, b1), (a2, b2) = matcher.regs[:3]
Guido van Rossumec758ea1991-06-04 20:36:54 +000040 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()