Skip to content
This repository
Browse code

normalize-tags.py: Enable normalizing several files at once

  • Loading branch information...
commit 8a186f47967ef1e6eb7dae272c6722ab1494f678 1 parent d2a2cb5
Peter Odding authored September 04, 2011
2  TODO.md
Source Rendered
@@ -14,6 +14,8 @@
14 14
 
15 15
 ## Possible bugs
16 16
 
  17
+ * 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.
  18
+
17 19
  * 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.
18 20
 
19 21
  * 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:
108  misc/easytags/normalize-tags.py
@@ -7,56 +7,74 @@
7 7
 This script makes a backup of the tags file in case something goes wrong.
8 8
 
9 9
 Author: Peter Odding <peter@peterodding.com>
10  
-Last Change: May 11, 2011
  10
+Last Change: September 4, 2011
11 11
 URL: https://github.com/xolox/vim-easytags/blob/master/normalize-tags.py
12 12
 '''
13 13
 
14 14
 import os, sys, time
15 15
 
16  
-tagsfile = os.path.expanduser(len(sys.argv) > 1 and sys.argv[1] or '~/.vimtags')
17  
-tempname = '%s-new-%d' % (tagsfile, time.time())
18  
-results, cache = {}, {}
19  
-infile = open(tagsfile)
20  
-outfile = open(tempname, 'w')
21  
-nprocessed = 0
22  
-fold_case = False
23  
-
24  
-for line in infile:
25  
-  nprocessed += 1
26  
-  line = line.rstrip()
27  
-  fields = line.split('\t')
28  
-  if line.startswith('!_TAG_'):
29  
-    results[line] = True
30  
-    if line.startswith('!_TAG_FILE_SORTED\t2'):
31  
-      fold_case = True
  16
+def main(arguments):
  17
+  for tagsfile in arguments or [os.path.expanduser('~/.vimtags')]:
  18
+    normalize(tagsfile)
  19
+  print "Done!"
  20
+
  21
+def normalize(tagsfile):
  22
+
  23
+  # Setup.
  24
+  tempname = '%s-new-%d' % (tagsfile, time.time())
  25
+  results, cache = {}, {}
  26
+  infile = open(tagsfile)
  27
+  outfile = open(tempname, 'w')
  28
+  nprocessed = 0
  29
+  fold_case = False
  30
+
  31
+  # Read tags file. 
  32
+  for line in infile:
  33
+    line = line.rstrip()
  34
+    fields = line.split('\t')
  35
+    if line.startswith('!_TAG_'):
  36
+      results[line] = True
  37
+      if line.startswith('!_TAG_FILE_SORTED\t2'):
  38
+        fold_case = True
  39
+    else:
  40
+      pathname = fields[1]
  41
+      if pathname not in cache:
  42
+        if os.path.exists(pathname):
  43
+          cache[pathname] = os.path.realpath(pathname)
  44
+        else:
  45
+          cache[pathname] = ''
  46
+      if cache[pathname]:
  47
+        fields[1] = cache[pathname]
  48
+        results['\t'.join(fields)] = True
  49
+    nprocessed += 1
  50
+  infile.close()
  51
+
  52
+  # Sort tags.
  53
+  lines = results.keys()
  54
+  if fold_case:
  55
+    lines.sort(key=str.lower)
32 56
   else:
33  
-    pathname = fields[1]
34  
-    if pathname not in cache:
35  
-      if not os.path.exists(pathname): continue
36  
-      cache[pathname] = os.path.realpath(pathname)
37  
-    fields[1] = cache[pathname]
38  
-    results['\t'.join(fields)] = True
39  
-
40  
-infile.close()
41  
-
42  
-lines = results.keys()
43  
-if fold_case:
44  
-  lines.sort(key=str.lower)
45  
-else:
46  
-  lines.sort()
47  
-
48  
-outfile.write('\n'.join(lines))
49  
-outfile.write('\n')
50  
-outfile.close()
51  
-
52  
-backup = '%s-backup-%d' % (tagsfile, time.time())
53  
-print "Making a backup of %s as %s" % (tagsfile, backup)
54  
-os.rename(tagsfile, backup)
55  
-
56  
-print "Replacing old", tagsfile, "with new one"
57  
-os.rename(tempname, tagsfile)
58  
-
59  
-nfiltered = nprocessed - len(lines)
60  
-print "Filtered %d out of %d entries" % (nfiltered, nprocessed)
  57
+    lines.sort()
  58
+
  59
+  # Write tags file.
  60
+  outfile.write('\n'.join(lines))
  61
+  outfile.write('\n')
  62
+  outfile.close()
  63
+
  64
+  # Backup old tags file.
  65
+  backup = '%s-backup-%d' % (tagsfile, time.time())
  66
+  print "Making a backup of %s as %s" % (tagsfile, backup)
  67
+  os.rename(tagsfile, backup)
  68
+
  69
+  # Replace tags file.
  70
+  print "Replacing old", tagsfile, "with new one"
  71
+  os.rename(tempname, tagsfile)
  72
+
  73
+  # Report results.
  74
+  nfiltered = nprocessed - len(lines)
  75
+  print "Filtered %d out of %d entries" % (nfiltered, nprocessed)
  76
+
  77
+if __name__ == '__main__':
  78
+  main(sys.argv[1:])
61 79
 
62 80
 # vim: ts=2 sw=2 et

0 notes on commit 8a186f4

Please sign in to comment.
Something went wrong with that request. Please try again.