aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVG <vg@devys.org>2015-07-24 17:34:41 +0200
committerVG <vg@devys.org>2015-07-24 17:34:41 +0200
commitab212a360ed1c0d10a1f6bf02beee28979a30922 (patch)
tree41153ff1f43ca2d0ddddc6dace5b5105c715935e
parentc5d527d906cc0836c29d3733bde518929fda69eb (diff)
downloadvim-rst-tables-ab212a360ed1c0d10a1f6bf02beee28979a30922.tar.gz
vim-rst-tables-ab212a360ed1c0d10a1f6bf02beee28979a30922.tar.bz2
vim-rst-tables-ab212a360ed1c0d10a1f6bf02beee28979a30922.zip
Revert "remove dependency to vim_bridge"
This reverts commit c5d527d906cc0836c29d3733bde518929fda69eb. I revert since I modified files in src and run build.py, but previous modifications of the original author were made to rst_tables.vim directly so my modifications removed unicode character count bug correction.
-rw-r--r--ftplugin/rst_tables.vim42
-rw-r--r--src/base.vim8
-rw-r--r--src/rst_tables.py3
3 files changed, 38 insertions, 15 deletions
diff --git a/ftplugin/rst_tables.vim b/ftplugin/rst_tables.vim
index ed2bd9f..6c994ce 100644
--- a/ftplugin/rst_tables.vim
+++ b/ftplugin/rst_tables.vim
@@ -20,13 +20,16 @@ python << endpython
import vim
import re
import textwrap
+import unicodedata
+import codecs
+from vim_bridge import bridged
def get_table_bounds():
row, col = vim.current.window.cursor
upper = lower = row
try:
- while vim.current.buffer[upper - 1].strip():
+ while vim.current.buffer[upper - 1].strip() and upper > 0:
upper -= 1
except IndexError:
pass
@@ -161,8 +164,17 @@ def table_line(widths, header=False):
def get_field_width(field_text):
- return max(map(lambda s: len(s), field_text.split('\n')))
-
+ return max(map(get_string_width, field_text.split('\n')))
+
+def get_string_width(string):
+ width = 0
+ for char in list(string):
+ eaw = unicodedata.east_asian_width(char)
+ if eaw == 'Na' or eaw == 'H':
+ width += 1
+ else:
+ width += 2
+ return width
def split_row_into_lines(row):
row = map(lambda field: field.split('\n'), row)
@@ -273,31 +285,39 @@ def draw_table(indent, table, manual_widths=None):
return output
+@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])
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
" Add mappings, unless the user didn't want this.
" The default mapping is registered, unless the user remapped it already.
if !exists("no_plugin_maps") && !exists("no_rst_table_maps")
- if !hasmapto(' reformat_table(')
- noremap <silent> <leader><leader>c :python reformat_table()<CR>
+ if !hasmapto('ReformatTable(')
+ noremap <silent> <leader><leader>c :call ReformatTable()<CR>
endif
- if !hasmapto(' reflow_table(')
- noremap <silent> <leader><leader>f :python reflow_table()<CR>
+ if !hasmapto('ReflowTable(')
+ noremap <silent> <leader><leader>f :call ReflowTable()<CR>
endif
endif
diff --git a/src/base.vim b/src/base.vim
index 282f4e0..037a9dd 100644
--- a/src/base.vim
+++ b/src/base.vim
@@ -23,10 +23,10 @@ endpython
" Add mappings, unless the user didn't want this.
" The default mapping is registered, unless the user remapped it already.
if !exists("no_plugin_maps") && !exists("no_rst_table_maps")
- if !hasmapto(' reformat_table(')
- noremap <silent> <leader><leader>c :python reformat_table()<CR>
+ if !hasmapto('ReformatTable(')
+ noremap <silent> <leader><leader>c :call ReformatTable()<CR>
endif
- if !hasmapto(' reflow_table(')
- noremap <silent> <leader><leader>f :python reflow_table()<CR>
+ if !hasmapto('ReflowTable(')
+ noremap <silent> <leader><leader>f :call ReflowTable()<CR>
endif
endif
diff --git a/src/rst_tables.py b/src/rst_tables.py
index a2cfc43..50f5b3b 100644
--- a/src/rst_tables.py
+++ b/src/rst_tables.py
@@ -1,6 +1,7 @@
import vim
import re
import textwrap
+from vim_bridge import bridged
def get_table_bounds():
@@ -254,6 +255,7 @@ def draw_table(indent, table, manual_widths=None):
return output
+@bridged
def reformat_table():
upper, lower, indent = get_table_bounds()
slice = vim.current.buffer[upper - 1:lower]
@@ -262,6 +264,7 @@ def reformat_table():
vim.current.buffer[upper - 1:lower] = slice
+@bridged
def reflow_table():
upper, lower, indent = get_table_bounds()
slice = vim.current.buffer[upper - 1:lower]