Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame^] | 1 | #! /usr/bin/env python |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 0cc1945 | 1992-08-31 10:54:06 +0000 | [diff] [blame] | 13 | import regex |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 0cc1945 | 1992-08-31 10:54:06 +0000 | [diff] [blame] | 21 | expr = '^[ \t]*\(def\|class\)[ \t]+\([a-zA-Z0-9_]+\)[ \t]*[:(]' |
| 22 | matcher = regex.compile(expr) |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 23 | |
| 24 | def 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 Rossum | 0cc1945 | 1992-08-31 10:54:06 +0000 | [diff] [blame] | 38 | if matcher.search(line) >= 0: |
| 39 | (a, b), (a1, b1), (a2, b2) = matcher.regs[:3] |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 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() |