Initial import of my home directory
[home/.git] / .vim / ftplugin / latex-suite / pytools.py
1 import string, vim, re, os, glob
2 # catFile: assigns a local variable retval to the contents of a file {{{
3 def catFile(filename):
4         try:
5                 file = open(filename)
6                 lines = ''.join(file.readlines())
7                 file.close()
8         except:
9                 lines = ''
10
11         # escape double quotes and backslashes before quoting the string so
12         # everything passes throught.
13         vim.command("""let retval = "%s" """ % re.sub(r'"|\\', r'\\\g<0>', lines))
14         return lines
15
16 # }}}
17 # isPresentInFile: check if regexp is present in the file {{{
18 def isPresentInFile(regexp, filename):
19         try:
20                 fp = open(filename)
21                 fcontents = string.join(fp.readlines(), '')
22                 fp.close()
23                 if re.search(regexp, fcontents):
24                         vim.command('let retval = 1')
25                         return 1
26                 else:
27                         vim.command('let retval = 0')
28                         return None
29         except:
30                 vim.command('let retval = 0')
31                 return None
32
33 # }}}
34 # deleteFile: deletes a file if present {{{
35 #       If the file does not exist, check if its a filepattern rather than a
36 #       filename. If its a pattern, then deletes all files matching the
37 #       pattern.
38 def deleteFile(filepattern):
39         if os.path.exists(filepattern):
40                 try:
41                         os.remove(filepattern)
42                 except:
43                         vim.command('let retval = -1')
44         else:
45                 if glob.glob(filepattern):
46                         for filename in glob.glob(filepattern):
47                                 os.remove(filename)
48                 else:
49                         vim.command('let retval = -1')
50
51 # }}}
52 # vim:fdm=marker:ff=unix:noet:ts=4:sw=4:nowrap