From 7d8699bfb0c4e1a7df0f7c5e4e9d3f62b9721f91 Mon Sep 17 00:00:00 2001 From: Jorge Scandaliaris Date: Fri, 24 Apr 2015 18:56:03 -0300 Subject: Fix wrong table width when non-ascii characters are present --- ftplugin/rst_tables.vim | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/ftplugin/rst_tables.vim b/ftplugin/rst_tables.vim index 4b4ebb7..88ef42c 100644 --- a/ftplugin/rst_tables.vim +++ b/ftplugin/rst_tables.vim @@ -21,6 +21,7 @@ import vim import re import textwrap import unicodedata +import codecs from vim_bridge import bridged @@ -167,7 +168,7 @@ def get_field_width(field_text): def get_string_width(string): width = 0 - for char in list(string.decode('utf-8')): + for char in list(string): eaw = unicodedata.east_asian_width(char) if eaw == 'Na' or eaw == 'H': width += 1 @@ -287,20 +288,27 @@ def draw_table(indent, table, manual_widths=None): @bridged def reformat_table(): upper, lower, indent = get_table_bounds() - slice = vim.current.buffer[upper - 1:lower] + encoding = vim.eval("&encoding") + slice = map(lambda x: codecs.decode(x, encoding), \ + vim.current.buffer[upper - 1:lower]) + print 'encoding: ', encoding table = parse_table(slice) slice = draw_table(indent, table) - vim.current.buffer[upper - 1:lower] = slice + vim.current.buffer[upper - 1:lower] = map(lambda x: \ + codecs.encode(x, encoding), slice) @bridged def reflow_table(): upper, lower, indent = get_table_bounds() - slice = vim.current.buffer[upper - 1:lower] + encoding = vim.eval("&encoding") + slice = map(lambda x: codecs.decode(x, encoding), \ + vim.current.buffer[upper - 1:lower]) widths = get_column_widths_from_border_spec(slice) table = parse_table(slice) slice = draw_table(indent, table, widths) - vim.current.buffer[upper - 1:lower] = slice + vim.current.buffer[upper - 1:lower] = map(lambda x: \ + codecs.encode(x, encoding), slice) endpython -- cgit v1.2.3 From b0defcc4ea77717ec255003621bae55769e4c75c Mon Sep 17 00:00:00 2001 From: Jorge Scandaliaris Date: Fri, 24 Apr 2015 20:13:28 -0300 Subject: Remove print statement used for debugging --- ftplugin/rst_tables.vim | 1 - 1 file changed, 1 deletion(-) diff --git a/ftplugin/rst_tables.vim b/ftplugin/rst_tables.vim index 88ef42c..6c994ce 100644 --- a/ftplugin/rst_tables.vim +++ b/ftplugin/rst_tables.vim @@ -291,7 +291,6 @@ def reformat_table(): encoding = vim.eval("&encoding") slice = map(lambda x: codecs.decode(x, encoding), \ vim.current.buffer[upper - 1:lower]) - print 'encoding: ', encoding table = parse_table(slice) slice = draw_table(indent, table) vim.current.buffer[upper - 1:lower] = map(lambda x: \ -- cgit v1.2.3