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