Skip to content
This repository was archived by the owner on Jun 23, 2022. It is now read-only.

Commit 39b14d6

Browse files
authored
Merge pull request #30 from matthew-brett/proposed-refactor
MRG: refactor for Python 3, testing rst_tables.vim
2 parents 7a66513 + 66586c7 commit 39b14d6

File tree

5 files changed

+154
-405
lines changed

5 files changed

+154
-405
lines changed

README.rst

-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ Installation
1919
git clone git://github.com/nvie/vim-rst-tables.git
2020
cd vim-rst-tables
2121

22-
3. Make the project::
23-
24-
python build.py
25-
2622
4. Copy the file ``ftplugin/rst_tables.vim`` to your ``~/.vim/ftplugin``
2723
directory. If your vim is not already configured to source scripts
2824
in this directory, make sure to add the appropriate command to your

ftplugin/rst_tables.vim

+73-37
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,36 @@ if exists("g:loaded_rst_tables_ftplugin")
1616
endif
1717
let loaded_rst_tables_ftplugin = 1
1818

19-
python << endpython
19+
" Default to Python 2
20+
let py_cmd_ver = 'python'
21+
let py_cmd_ver_other = 'python3'
22+
" Allow user to select Python 3
23+
if exists('g:rst_prefer_python_version') &&
24+
\ g:rst_prefer_python_version == 3
25+
let py_cmd_ver = 'python3'
26+
let py_cmd_ver_other = 'python'
27+
endif
28+
if !has(py_cmd_ver)
29+
let py_cmd_ver = py_cmd_ver_other
30+
if !has(py_cmd_ver)
31+
echoerr "Error: Requires Vim compiled with +python or +python3"
32+
finish
33+
endif
34+
endif
35+
36+
if py_cmd_ver == 'python'
37+
command! -nargs=1 Python python <args>
38+
else
39+
command! -nargs=1 Python python3 <args>
40+
endif
41+
42+
Python << endpython
43+
2044
import vim
45+
46+
import sys
47+
PY2 = sys.version_info[0] < 2
48+
2149
import re
2250
import textwrap
2351
import unicodedata
@@ -44,7 +72,7 @@ def get_table_bounds():
4472
else:
4573
lower -= 1
4674

47-
match = re.match('^(\s*).*$', vim.current.buffer[upper-1])
75+
match = re.match(r'^(\s*).*$', vim.current.buffer[upper-1])
4876

4977
return (upper, lower, match.group(1))
5078

@@ -65,11 +93,11 @@ def join_rows(rows, sep='\n'):
6593
field_text = field.strip()
6694
if field_text:
6795
output[i].append(field_text)
68-
return map(lambda lines: sep.join(lines), output)
96+
return [sep.join(lines) for lines in output]
6997

7098

7199
def line_is_separator(line):
72-
return re.match('^[\t +=-]+$', line)
100+
return re.match(r'^[\t +=-]+$', line)
73101

74102

75103
def has_line_seps(raw_lines):
@@ -85,20 +113,19 @@ def partition_raw_lines(raw_lines):
85113

86114
"""
87115
if not has_line_seps(raw_lines):
88-
return map(lambda x: [x], raw_lines)
116+
return [[x] for x in raw_lines]
89117

90118
curr_part = []
91119
parts = [curr_part]
92120
for line in raw_lines:
93-
line = line.encode('utf8')
94121
if line_is_separator(line):
95122
curr_part = []
96123
parts.append(curr_part)
97124
else:
98125
curr_part.append(line)
99126

100127
# remove any empty partitions (typically the first and last ones)
101-
return filter(lambda x: x != [], parts)
128+
return [x for x in parts if x!= []]
102129

103130

104131
def unify_table(table):
@@ -107,7 +134,8 @@ def unify_table(table):
107134
empty (i.e. all rows have that field empty), the column is removed.
108135

109136
"""
110-
max_fields = max(map(lambda row: len(row), table))
137+
max_fields = max([len(row) for row in table])
138+
111139
empty_cols = [True] * max_fields
112140
output = []
113141
for row in table:
@@ -145,8 +173,8 @@ def split_table_row(row_string):
145173

146174
def parse_table(raw_lines):
147175
row_partition = partition_raw_lines(raw_lines)
148-
lines = map(lambda row_string: join_rows(map(split_table_row, row_string)),
149-
row_partition)
176+
lines = [join_rows([split_table_row(row) for row in row_string])
177+
for row_string in row_partition]
150178
return unify_table(lines)
151179

152180

@@ -165,7 +193,8 @@ def table_line(widths, header=False):
165193

166194

167195
def get_field_width(field_text):
168-
return max(map(get_string_width, field_text.split('\n')))
196+
return max([len(s) for s in field_text.split('\n')])
197+
169198

170199
def get_string_width(string):
171200
width = 0
@@ -178,8 +207,8 @@ def get_string_width(string):
178207
return width
179208

180209
def split_row_into_lines(row):
181-
row = map(lambda field: field.split('\n'), row)
182-
height = max(map(lambda field_lines: len(field_lines), row))
210+
row = [field.split('\n') for field in row]
211+
height = max([len(field_lines) for field_lines in row])
183212
turn_table = []
184213
for i in range(height):
185214
fields = []
@@ -221,23 +250,21 @@ def get_column_widths_from_border_spec(slice):
221250
left = 1
222251
if border[-1] == '+':
223252
right = -1
224-
return map(lambda drawing: max(0, len(drawing) - 2), border[left:right].split('+'))
253+
# This will return one width if there are no + characters
254+
return [max(0, len(drawing) - 2) for drawing in border[left:right].split('+')]
225255

226256

227257
def pad_fields(row, widths):
228258
"""Pads fields of the given row, so each field lines up nicely with the
229259
others.
230260

231261
"""
232-
widths = map(lambda w: ' %-' + str(w) + 's ', widths)
262+
widths = [' %-' + str(w) + 's ' for w in widths]
233263

234264
# Pad all fields using the calculated widths
235265
new_row = []
236266
for i in range(len(row)):
237-
col = row[i]
238-
col = col.decode('utf8')
239-
col = widths[i] % col.strip()
240-
col = col.encode('utf8')
267+
col = widths[i] % row[i].strip()
241268
new_row.append(col)
242269
return new_row
243270

@@ -260,7 +287,7 @@ def draw_table(indent, table, manual_widths=None):
260287
col_widths = manual_widths
261288

262289
# Reserve room for the spaces
263-
sep_col_widths = map(lambda x: x + 2, col_widths)
290+
sep_col_widths = [x + 2 for x in col_widths]
264291
header_line = table_line(sep_col_widths, header=True)
265292
normal_line = table_line(sep_col_widths, header=False)
266293

@@ -288,29 +315,38 @@ def draw_table(indent, table, manual_widths=None):
288315
return output
289316

290317

318+
def proc_table(func):
319+
upper, lower, indent = get_table_bounds()
320+
table_txt = vim.current.buffer[upper - 1:lower]
321+
if PY2:
322+
encoding = vim.eval("&encoding")
323+
table_txt = [codecs.decode(x, encoding) for x in table_txt]
324+
table_txt = func(indent, table_txt)
325+
if PY2:
326+
table_txt = [codecs.encode(x, encoding) for x in table_txt]
327+
vim.current.buffer[upper - 1:lower] = table_txt
328+
329+
330+
def _reformat(indent, table_txt):
331+
table = parse_table(table_txt)
332+
return draw_table(indent, table)
333+
334+
291335
@bridged
292336
def reformat_table():
293-
upper, lower, indent = get_table_bounds()
294-
encoding = vim.eval("&encoding")
295-
slice = map(lambda x: codecs.decode(x, encoding), \
296-
vim.current.buffer[upper - 1:lower])
297-
table = parse_table(slice)
298-
slice = draw_table(indent, table)
299-
vim.current.buffer[upper - 1:lower] = map(lambda x: \
300-
codecs.encode(x, encoding), slice)
337+
proc_table(_reformat)
338+
339+
340+
def _reflow(indent, table_txt):
341+
widths = get_column_widths_from_border_spec(table_txt)
342+
table = parse_table(table_txt)
343+
return draw_table(indent, table, widths)
301344

302345

303346
@bridged
304347
def reflow_table():
305-
upper, lower, indent = get_table_bounds()
306-
encoding = vim.eval("&encoding")
307-
slice = map(lambda x: codecs.decode(x, encoding), \
308-
vim.current.buffer[upper - 1:lower])
309-
widths = get_column_widths_from_border_spec(slice)
310-
table = parse_table(slice)
311-
slice = draw_table(indent, table, widths)
312-
vim.current.buffer[upper - 1:lower] = map(lambda x: \
313-
codecs.encode(x, encoding), slice)
348+
proc_table(_reflow)
349+
314350

315351
endpython
316352

src/base.vim

-32
This file was deleted.

0 commit comments

Comments
 (0)