Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame^] | 1 | #! /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 | |
| 12 | import sys |
| 13 | import regexp |
| 14 | |
| 15 | def main(): |
| 16 | outfp = open('TAGS', 'w') |
| 17 | args = sys.argv[1:] |
| 18 | for file in args: |
| 19 | treat_file(file, outfp) |
| 20 | |
| 21 | matcher = regexp.compile('^[ \t]*(def|class)[ \t]+([a-zA-Z0-9_]+)[ \t]*\(') |
| 22 | |
| 23 | def 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 | |
| 50 | main() |