From 8a186f47967ef1e6eb7dae272c6722ab1494f678 Mon Sep 17 00:00:00 2001 From: Peter Odding Date: Sun, 4 Sep 2011 13:12:38 +0200 Subject: [PATCH] normalize-tags.py: Enable normalizing several files at once --- TODO.md | 2 + misc/easytags/normalize-tags.py | 108 +++++++++++++++++++------------- 2 files changed, 65 insertions(+), 45 deletions(-) diff --git a/TODO.md b/TODO.md index 3dc1f31..b67ffba 100644 --- a/TODO.md +++ b/TODO.md @@ -14,6 +14,8 @@ ## Possible bugs + * Right now easytags is a messy combination of Vim script code and Python scripts. I plan to port the Python code back to Vim script. + * On Microsoft Windows (tested on XP) GVim loses focus while `ctags` is running because Vim opens a command prompt window. Also the CursorHold event seems to fire repeatedly, contradicting my understanding of the automatic command and its behavior on UNIX?! This behavior doesn't occur when I use the integration with my `shell.vim` plug-in. * I might have found a bug in Vim: The tag `easytags#highlight_cmd` was correctly being highlighted by my plug-in (and was indeed included in my tags file) even though I couldn't jump to it using `Ctrl-]`, which caused: diff --git a/misc/easytags/normalize-tags.py b/misc/easytags/normalize-tags.py index 6701c53..a96ed3f 100755 --- a/misc/easytags/normalize-tags.py +++ b/misc/easytags/normalize-tags.py @@ -7,56 +7,74 @@ This script makes a backup of the tags file in case something goes wrong. Author: Peter Odding -Last Change: May 11, 2011 +Last Change: September 4, 2011 URL: https://github.com/xolox/vim-easytags/blob/master/normalize-tags.py ''' import os, sys, time -tagsfile = os.path.expanduser(len(sys.argv) > 1 and sys.argv[1] or '~/.vimtags') -tempname = '%s-new-%d' % (tagsfile, time.time()) -results, cache = {}, {} -infile = open(tagsfile) -outfile = open(tempname, 'w') -nprocessed = 0 -fold_case = False - -for line in infile: - nprocessed += 1 - line = line.rstrip() - fields = line.split('\t') - if line.startswith('!_TAG_'): - results[line] = True - if line.startswith('!_TAG_FILE_SORTED\t2'): - fold_case = True +def main(arguments): + for tagsfile in arguments or [os.path.expanduser('~/.vimtags')]: + normalize(tagsfile) + print "Done!" + +def normalize(tagsfile): + + # Setup. + tempname = '%s-new-%d' % (tagsfile, time.time()) + results, cache = {}, {} + infile = open(tagsfile) + outfile = open(tempname, 'w') + nprocessed = 0 + fold_case = False + + # Read tags file. + for line in infile: + line = line.rstrip() + fields = line.split('\t') + if line.startswith('!_TAG_'): + results[line] = True + if line.startswith('!_TAG_FILE_SORTED\t2'): + fold_case = True + else: + pathname = fields[1] + if pathname not in cache: + if os.path.exists(pathname): + cache[pathname] = os.path.realpath(pathname) + else: + cache[pathname] = '' + if cache[pathname]: + fields[1] = cache[pathname] + results['\t'.join(fields)] = True + nprocessed += 1 + infile.close() + + # Sort tags. + lines = results.keys() + if fold_case: + lines.sort(key=str.lower) else: - pathname = fields[1] - if pathname not in cache: - if not os.path.exists(pathname): continue - cache[pathname] = os.path.realpath(pathname) - fields[1] = cache[pathname] - results['\t'.join(fields)] = True - -infile.close() - -lines = results.keys() -if fold_case: - lines.sort(key=str.lower) -else: - lines.sort() - -outfile.write('\n'.join(lines)) -outfile.write('\n') -outfile.close() - -backup = '%s-backup-%d' % (tagsfile, time.time()) -print "Making a backup of %s as %s" % (tagsfile, backup) -os.rename(tagsfile, backup) - -print "Replacing old", tagsfile, "with new one" -os.rename(tempname, tagsfile) - -nfiltered = nprocessed - len(lines) -print "Filtered %d out of %d entries" % (nfiltered, nprocessed) + lines.sort() + + # Write tags file. + outfile.write('\n'.join(lines)) + outfile.write('\n') + outfile.close() + + # Backup old tags file. + backup = '%s-backup-%d' % (tagsfile, time.time()) + print "Making a backup of %s as %s" % (tagsfile, backup) + os.rename(tagsfile, backup) + + # Replace tags file. + print "Replacing old", tagsfile, "with new one" + os.rename(tempname, tagsfile) + + # Report results. + nfiltered = nprocessed - len(lines) + print "Filtered %d out of %d entries" % (nfiltered, nprocessed) + +if __name__ == '__main__': + main(sys.argv[1:]) # vim: ts=2 sw=2 et