@@ -16,8 +16,36 @@ if exists("g:loaded_rst_tables_ftplugin")
16
16
endif
17
17
let loaded_rst_tables_ftplugin = 1
18
18
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
+
20
44
import vim
45
+
46
+ import sys
47
+ PY2 = sys.version_info[0 ] < 2
48
+
21
49
import re
22
50
import textwrap
23
51
import unicodedata
@@ -44,7 +72,7 @@ def get_table_bounds():
44
72
else :
45
73
lower -= 1
46
74
47
- match = re .match (' ^(\s*).*$' , vim .current.buffer [upper- 1 ])
75
+ match = re .match (r ' ^(\s*).*$' , vim .current.buffer [upper- 1 ])
48
76
49
77
return (upper, lower, match .group (1 ))
50
78
@@ -65,11 +93,11 @@ def join_rows(rows, sep='\n'):
65
93
field_text = field.strip ()
66
94
if field_text:
67
95
output[i ].append (field_text)
68
- return map (lambda lines : sep.join (lines ), output)
96
+ return [ sep.join (lines ) for lines in output]
69
97
70
98
71
99
def line_is_separator (line ):
72
- return re .match (' ^[\t +=-]+$' , line )
100
+ return re .match (r ' ^[\t +=-]+$' , line )
73
101
74
102
75
103
def has_line_seps (raw_lines):
@@ -85,20 +113,19 @@ def partition_raw_lines(raw_lines):
85
113
86
114
" ""
87
115
if not has_line_seps (raw_lines):
88
- return map (lambda x : [ x ], raw_lines)
116
+ return [[ x ] for x in raw_lines]
89
117
90
118
curr_part = []
91
119
parts = [curr_part]
92
120
for line in raw_lines:
93
- line = line .encode (' utf8' )
94
121
if line_is_separator (line ):
95
122
curr_part = []
96
123
parts.append (curr_part)
97
124
else :
98
125
curr_part.append (line )
99
126
100
127
# 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 != []]
102
129
103
130
104
131
def unify_table (table):
@@ -107,7 +134,8 @@ def unify_table(table):
107
134
empty (i .e . all rows have that field empty ), the column is removed.
108
135
109
136
" ""
110
- max_fields = max (map (lambda row: len (row), table))
137
+ max_fields = max ([len (row) for row in table])
138
+
111
139
empty_cols = [True] * max_fields
112
140
output = []
113
141
for row in table:
@@ -145,8 +173,8 @@ def split_table_row(row_string):
145
173
146
174
def parse_table (raw_lines):
147
175
row_partition = partition_raw_lines (raw_lines)
148
- lines = map (lambda row_strin g: 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]
150
178
return unify_table (lines )
151
179
152
180
@@ -165,7 +193,8 @@ def table_line(widths, header=False):
165
193
166
194
167
195
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
+
169
198
170
199
def get_string_width (string ):
171
200
width = 0
@@ -178,8 +207,8 @@ def get_string_width(string):
178
207
return width
179
208
180
209
def split_row_into_lines (row):
181
- row = map (lambda field: field .split (' \n' ), row)
182
- height = max (map (lambda field_line s: len (field_lines), row) )
210
+ row = [ field.split (' \n' ) for field in row]
211
+ height = max ([ len (field_lines) for field_lines in row] )
183
212
turn_table = []
184
213
for i in range (height):
185
214
fields = []
@@ -221,23 +250,21 @@ def get_column_widths_from_border_spec(slice):
221
250
left = 1
222
251
if border[-1 ] == ' +' :
223
252
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 (' +' )]
225
255
226
256
227
257
def pad_fields (row, widths):
228
258
" ""Pads fields of the given row, so each field lines up nicely with the
229
259
others.
230
260
231
261
" ""
232
- widths = map (lambda w: ' %-' + str (w ) + ' s ' , widths)
262
+ widths = [ ' %-' + str (w ) + ' s ' for w in widths]
233
263
234
264
# Pad all fields using the calculated widths
235
265
new_row = []
236
266
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 ()
241
268
new_row.append (col )
242
269
return new_row
243
270
@@ -260,7 +287,7 @@ def draw_table(indent, table, manual_widths=None):
260
287
col_widths = manual_widths
261
288
262
289
# 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]
264
291
header_line = table_line (sep_col_widths, header= True)
265
292
normal_line = table_line (sep_col_widths, header= False)
266
293
@@ -288,29 +315,38 @@ def draw_table(indent, table, manual_widths=None):
288
315
return output
289
316
290
317
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
+
291
335
@b ridged
292
336
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)
301
344
302
345
303
346
@b ridged
304
347
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
+
314
350
315
351
endpython
316
352
0 commit comments