]> code.delx.au - dotfiles/blob - .vim/abbrs2vim.py
ea77fb73b67ffd50f342ebe78c16ed8ce7572c56
[dotfiles] / .vim / abbrs2vim.py
1 #!/usr/bin/env python
2
3 import time, os, os.path, sys
4
5 # To override the defaults, set these variables
6 EMAIL = ""
7
8 macros = {}
9 macros["YDATE"] = lambda: time.strftime("%Y-%m-%d")
10 macros["YEAR"] = lambda: time.strftime("%Y")
11 macros["EMAIL"] = lambda: EMAIL or os.environ.get("EMAIL", "~/.vim/abbrs2vim.py")
12
13
14 vimdir = os.path.join(os.environ.get("HOME"), ".vim")
15 try:
16 outfilename = os.path.join(vimdir, "abbrsout.vim")
17 outfile = open(outfilename, "w")
18 except IOError:
19 print >> sys.stderr, "Couldn't open output file for writing: %s" % outfilename
20 sys.exit(1)
21
22 abbrsdir = os.path.join(vimdir, "abbrs")
23 if not os.path.isdir(abbrsdir):
24 print >> sys.stderr, "Could not find directory with abbreviations: %s" % abbrsdir
25 sys.exit(1)
26
27 for abbrfile in os.listdir(abbrsdir):
28 abbr = open(os.path.join(abbrsdir, abbrfile), "r").read()
29 for macro in macros.keys():
30 if abbr.find(macro) >= 0:
31 abbr = abbr.replace(macro, macros[macro]())
32 abbr = abbr.replace("\n", "\r")
33 # Put into paste mode and take out of paste mode
34 abbr = "iab %s \x1b:set paste\ri%s\x1b:set nopaste\r" % (abbrfile.rsplit('.', 1)[0], abbr)
35 if abbr.find("___") >= 0:
36 # Then search for ___ to place the cursor there
37 abbr += "gg\r/___\x1b:nohlsearch\n"
38 else:
39 # Leave the cursor in insert mode
40 abbr += "a\n"
41 outfile.write(abbr)
42
43