| 1 |
import re |
| 2 |
|
| 3 |
from pygments.util import format_lines |
| 4 |
|
| 5 |
r_line = re.compile(r"^(syn keyword vimCommand contained|syn keyword vimOption " |
| 6 |
r"contained|syn keyword vimAutoEvent contained)\s+(.*)") |
| 7 |
r_item = re.compile(r"(\w+)(?:\[(\w+)\])?") |
| 8 |
|
| 9 |
HEADER = '''\ |
| 10 |
# -*- coding: utf-8 -*- |
| 11 |
""" |
| 12 |
pygments.lexers._vim_builtins |
| 13 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 14 |
|
| 15 |
This file is autogenerated by scripts/get_vimkw.py |
| 16 |
|
| 17 |
:copyright: Copyright 2006-present by the Pygments team, see AUTHORS. |
| 18 |
:license: BSD, see LICENSE for details. |
| 19 |
""" |
| 20 |
|
| 21 |
# Split up in multiple functions so it's importable by jython, which has a |
| 22 |
# per-method size limit. |
| 23 |
''' |
| 24 |
|
| 25 |
METHOD = '''\ |
| 26 |
def _get%(key)s(): |
| 27 |
%(body)s |
| 28 |
return var |
| 29 |
%(key)s = _get%(key)s() |
| 30 |
''' |
| 31 |
|
| 32 |
def getkw(input, output): |
| 33 |
out = open(output, 'w') |
| 34 |
|
| 35 |
# Copy template from an existing file. |
| 36 |
print(HEADER, file=out) |
| 37 |
|
| 38 |
output_info = {'command': [], 'option': [], 'auto': []} |
| 39 |
for line in open(input): |
| 40 |
m = r_line.match(line) |
| 41 |
if m: |
| 42 |
# Decide which output gets mapped to d |
| 43 |
if 'vimCommand' in m.group(1): |
| 44 |
d = output_info['command'] |
| 45 |
elif 'AutoEvent' in m.group(1): |
| 46 |
d = output_info['auto'] |
| 47 |
else: |
| 48 |
d = output_info['option'] |
| 49 |
|
| 50 |
# Extract all the shortened versions |
| 51 |
for i in r_item.finditer(m.group(2)): |
| 52 |
d.append('({!r},{!r})'.format(i.group(1), "{}{}".format(i.group(1), i.group(2) or ''))) |
| 53 |
|
| 54 |
output_info['option'].append("('nnoremap','nnoremap')") |
| 55 |
output_info['option'].append("('inoremap','inoremap')") |
| 56 |
output_info['option'].append("('vnoremap','vnoremap')") |
| 57 |
|
| 58 |
for key, keywordlist in output_info.items(): |
| 59 |
keywordlist.sort() |
| 60 |
body = format_lines('var', keywordlist, raw=True, indent_level=1) |
| 61 |
print(METHOD % locals(), file=out) |
| 62 |
|
| 63 |
def is_keyword(w, keywords): |
| 64 |
for i in range(len(w), 0, -1): |
| 65 |
if w[:i] in keywords: |
| 66 |
return keywords[w[:i]][:len(w)] == w |
| 67 |
return False |
| 68 |
|
| 69 |
if __name__ == "__main__": |
| 70 |
getkw("/usr/share/vim/vim74/syntax/vim.vim", |
| 71 |
"pygments/lexers/_vim_builtins.py") |