Fine tuning & formatting

This commit is contained in:
Dhruva Sagar
2013-05-16 14:49:03 +05:30
parent edfb221b3e
commit eba2481798

View File

@@ -1,4 +1,4 @@
" ============================================================================= " ============================== Header ==================================={{{
" Description: Table mode for vim for creating neat tables. " Description: Table mode for vim for creating neat tables.
" Author: Dhruva Sagar <http://dhruvasagar.com/> " Author: Dhruva Sagar <http://dhruvasagar.com/>
" License: MIT (http://www.opensource.org/licenses/MIT) " License: MIT (http://www.opensource.org/licenses/MIT)
@@ -16,7 +16,7 @@
" of any kind, either expressed or implied. In no event will " of any kind, either expressed or implied. In no event will
" the copyright holder be liable for any damamges resulting " the copyright holder be liable for any damamges resulting
" from the use of this software. " from the use of this software.
" ============================================================================= " ==========================================================================}}}
" Private Functions {{{1 " Private Functions {{{1
@@ -54,15 +54,15 @@ function! s:Line(line) "{{{3
endfunction endfunction
" }}}3 " }}}3
" s:Strlen(text) For counting multibyte characters accurately {{{3 " function! s:Strlen(text) - Count multibyte characters accurately {{{3
" See :h strlen() for more details " See :h strlen() for more details
function! s:Strlen(text) function! s:Strlen(text)
return strlen(substitute(a:text, '.', 'x', 'g')) return strlen(substitute(a:text, '.', 'x', 'g'))
endfunction endfunction
" }}}3 " }}}3
function! s:Strip(input_string) "{{{3 function! s:Strip(string) "{{{3
return substitute(a:input_string, '^\s*\(.\{-}\)\s*$', '\1', '') return matchstr(a:string, '^\s*\zs.\{-}\ze\s*$')
endfunction endfunction
" }}}3 " }}}3
@@ -283,7 +283,7 @@ function! s:MoveToStartOfCell() "{{{2
endfunction endfunction
" }}}2 " }}}2
" s:GetCells() - Function to get values of cells in a table {{{2 " function! s:GetCells() - Function to get values of cells in a table {{{2
" s:GetCells(row) - Get values of all cells in a row as a List. " s:GetCells(row) - Get values of all cells in a row as a List.
" s:GetCells(0, col) - Get values of all cells in a column as a List. " s:GetCells(0, col) - Get values of all cells in a column as a List.
" s:GetCells(row, col) - Get the value of table cell by given row, col. " s:GetCells(row, col) - Get the value of table cell by given row, col.
@@ -376,9 +376,7 @@ function! s:GetColumn(col, ...) "{{{2
endfunction endfunction
" }}}2 " }}}2
" Borrowed from Tabular : {{{2 function! s:ParseRange(range, ...) "{{{2
function! s:ParseRange(range, ...) "{{{3
if a:0 < 1 if a:0 < 1
let default_col = tablemode#ColumnNr('.') let default_col = tablemode#ColumnNr('.')
elseif a:0 < 2 elseif a:0 < 2
@@ -408,9 +406,9 @@ function! s:ParseRange(range, ...) "{{{3
return [row1, col1, row2, col2] return [row1, col1, row2, col2]
endfunction endfunction
" }}}3 " }}}2
" function! s:GetCellRange(range, ...) {{{3 " function! s:GetCellRange(range, ...) {{{2
" range: A string representing range of cells. " range: A string representing range of cells.
" - Can be row1:row2 for values in the current columns in those rows. " - Can be row1:row2 for values in the current columns in those rows.
" - Can be row1,col1:row2,col2 for range between row1,col1 till " - Can be row1,col1:row2,col2 for range between row1,col1 till
@@ -452,6 +450,15 @@ function! s:GetCellRange(range, ...) abort
return values return values
endfunction endfunction
" }}}2
" Borrowed from Tabular : {{{2
" function! s:StripTrailingSpaces(string) - Remove all trailing spaces {{{3
" from a string.
function! s:StripTrailingSpaces(string)
return matchstr(a:string, '^.\{-}\ze\s*$')
endfunction
" }}}3 " }}}3
function! s:Padding(string, length, where) "{{{3 function! s:Padding(string, length, where) "{{{3
@@ -468,7 +475,7 @@ function! s:Padding(string, length, where) "{{{3
endfunction endfunction
" }}}3 " }}}3
" Split a string into fields and delimiters {{{3 " function! s:Split() - Split a string into fields and delimiters {{{3
" Like split(), but include the delimiters as elements " Like split(), but include the delimiters as elements
" All odd numbered elements are delimiters " All odd numbered elements are delimiters
" All even numbered elements are non-delimiters (including zero) " All even numbered elements are non-delimiters (including zero)
@@ -508,8 +515,6 @@ function! s:SplitDelim(string, delim)
let rv += [ strpart(a:string, beg) ] let rv += [ strpart(a:string, beg) ]
call filter(rv, 'len(v:val) > 0')
return rv return rv
endfunction endfunction
" }}}3 " }}}3
@@ -519,9 +524,15 @@ function! s:Align(lines) "{{{3
for line in lines for line in lines
if len(line) <= 1 | continue | endif if len(line) <= 1 | continue | endif
for i in range(len(line))
if line[0] !~ '^\s*$'
let line[0] = s:StripTrailingSpaces(line[0])
endif
if len(line) >= 2
for i in range(1, len(line)-1)
let line[i] = s:Strip(line[i]) let line[i] = s:Strip(line[i])
endfor endfor
endif
endfor endfor
let maxes = [] let maxes = []
@@ -542,13 +553,15 @@ function! s:Align(lines) "{{{3
if len(line) <= 1 | continue | endif if len(line) <= 1 | continue | endif
for i in range(len(line)) for i in range(len(line))
if line[i] !~# '[^0-9]' if line[i] !~# '[^0-9]'
let line[i] = s:Padding(line[i], maxes[i], 'r') let field = s:Padding(line[i], maxes[i], 'r')
else else
let line[i] = s:Padding(line[i], maxes[i], 'l') let field = s:Padding(line[i], maxes[i], 'l')
endif endif
let line[i] = field . (i == 0 || i == len(line) ? '' : ' ')
endfor endfor
let lines[idx] = join(line) let lines[idx] = s:StripTrailingSpaces(join(line, ''))
endfor endfor
return lines return lines