Releasing v2.3.0

- Refactored realignment logic.
- Generating borders by hand rather than relying on
  tabular formatting since that puts restrictions on the borders
  (padding). With the new scheme, I am able to generate good looking
  tables with neat borders like they should be, without padding.
- Updated doc/table-mode.txt
This commit is contained in:
Dhruva Sagar
2013-05-04 03:23:20 +05:30
parent 7c4039e5e1
commit e9973bb530
3 changed files with 97 additions and 62 deletions

View File

@@ -4,7 +4,7 @@
" 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)
" Website: http://github.com/dhruvasagar/vim-table-mode " Website: http://github.com/dhruvasagar/vim-table-mode
" Version: 2.2.2 " Version: 2.3.0
" Note: This plugin was heavily inspired by the 'CucumberTables.vim' " Note: This plugin was heavily inspired by the 'CucumberTables.vim'
" (https://gist.github.com/tpope/287147) plugin by Tim Pope and " (https://gist.github.com/tpope/287147) plugin by Tim Pope and
" uses a small amount of code from it. " uses a small amount of code from it.
@@ -56,7 +56,6 @@ call s:SetGlobalOptDefault('table_mode_delimiter', ',')
call s:SetGlobalOptDefault('table_mode_tableize_map', '<Leader>tt') call s:SetGlobalOptDefault('table_mode_tableize_map', '<Leader>tt')
call s:SetGlobalOptDefault('table_mode_tableize_op_map', '<Leader>T') call s:SetGlobalOptDefault('table_mode_tableize_op_map', '<Leader>T')
call s:SetGlobalOptDefault('table_mode_align', 'l1') call s:SetGlobalOptDefault('table_mode_align', 'l1')
call s:SetGlobalOptDefault('table_mode_no_border_padding', '0')
call s:SetGlobalOptDefault('table_mode_realign_map', '<Leader>tr') call s:SetGlobalOptDefault('table_mode_realign_map', '<Leader>tr')
call s:SetGlobalOptDefault('table_mode_motion_prefix', '<Leader>t') call s:SetGlobalOptDefault('table_mode_motion_prefix', '<Leader>t')
"}}}1 "}}}1
@@ -91,7 +90,7 @@ execute "xnoremap <silent> " . g:table_mode_tableize_map . " :Tableize<CR>"
execute "nnoremap <silent> " . g:table_mode_tableize_map . " :Tableize<CR>" execute "nnoremap <silent> " . g:table_mode_tableize_map . " :Tableize<CR>"
execute "xnoremap <silent> " . g:table_mode_tableize_op_map . " :<C-U>call tablemode#TableizeByDelimiter()<CR>" execute "xnoremap <silent> " . g:table_mode_tableize_op_map . " :<C-U>call tablemode#TableizeByDelimiter()<CR>"
execute "nnoremap <silent> " . g:table_mode_realign_map . " :<C-U>call tablemode#TableRealign()<CR>" execute "nnoremap <silent> " . g:table_mode_realign_map . " :<C-U>call tablemode#TableRealign('.')<CR>"
execute "nnoremap <silent> " . g:table_mode_motion_prefix . " :<C-U>call <SID>TableMotion()<CR>" execute "nnoremap <silent> " . g:table_mode_motion_prefix . " :<C-U>call <SID>TableMotion()<CR>"
"}}}1 "}}}1

View File

@@ -4,7 +4,7 @@
" 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)
" Website: http://github.com/dhruvasagar/vim-table-mode " Website: http://github.com/dhruvasagar/vim-table-mode
" Version: 2.2.2 " Version: 2.3.0
" Note: This plugin was heavily inspired by the 'CucumberTables.vim' " Note: This plugin was heavily inspired by the 'CucumberTables.vim'
" (https://gist.github.com/tpope/287147) plugin by Tim Pope and " (https://gist.github.com/tpope/287147) plugin by Tim Pope and
" uses a small amount of code from it. " uses a small amount of code from it.
@@ -35,11 +35,6 @@ function! s:Strlen(text)
endfunction endfunction
" }}}2 " }}}2
function! s:CountSeparator(line, separator) "{{{2
return s:Strlen(substitute(getline(a:line), '[^' . a:separator . ']', '', 'g'))
endfunction
" }}}2
function! s:GetCommentStart() "{{{2 function! s:GetCommentStart() "{{{2
let cstring = &commentstring let cstring = &commentstring
if s:Strlen(cstring) > 0 if s:Strlen(cstring) > 0
@@ -98,57 +93,53 @@ function! s:SetActive(bool) "{{{2
endfunction endfunction
" }}}2 " }}}2
function! s:UpdateLineBorder(line) "{{{2 function! s:GenerateBorder(line) "{{{2
let cline = a:line let line = 0
let hf = s:StartExpr() . g:table_mode_corner . '[' . g:table_mode_corner . ' ' . if type(a:line) == type('')
\ g:table_mode_fillchar . ']*' . g:table_mode_corner . '\?\s*$' let line = line(a:line)
let curr_line_count = s:CountSeparator(cline, g:table_mode_separator)
if getline(cline-1) =~# hf
let prev_line_count = s:CountSeparator(cline-1, g:table_mode_corner)
if curr_line_count > prev_line_count
silent! execute 'normal! kA' . repeat(g:table_mode_corner, curr_line_count - prev_line_count) . "\<Esc>j"
endif
else else
let cstartexpr = s:StartCommentExpr() let line = a:line
if s:Strlen(cstartexpr) > 0 && getline(cline) =~# cstartexpr
let indent = matchstr(getline(cline), s:StartCommentExpr())
call append(cline-1, indent . repeat(g:table_mode_corner, curr_line_count))
else
call append(cline-1, repeat(g:table_mode_corner, curr_line_count))
endif
let cline = a:line + 1 " because of the append, the current line moved down
endif endif
if getline(cline+1) =~# hf let border = substitute(getline(line)[stridx(getline(line), g:table_mode_separator):-1], g:table_mode_separator, g:table_mode_corner, 'g')
let next_line_count = s:CountSeparator(cline+1, g:table_mode_corner) let border = substitute(border, '[^' . g:table_mode_corner . ']', g:table_mode_fillchar, 'g')
if curr_line_count > next_line_count
silent! execute 'normal! jA' . repeat(g:table_mode_corner, curr_line_count - next_line_count) . "\<Esc>k"
end
else
let cstartexpr = s:StartCommentExpr() let cstartexpr = s:StartCommentExpr()
if s:Strlen(cstartexpr) > 0 && getline(cline) =~# cstartexpr if s:Strlen(cstartexpr) > 0 && getline(line) =~# cstartexpr
let indent = matchstr(getline(cline), s:StartCommentExpr()) let indent = matchstr(getline(line), s:StartCommentExpr())
call append(cline, indent . repeat(g:table_mode_corner, curr_line_count)) return indent . border
elseif getline(line) =~# s:StartExpr()
let indent = matchstr(getline(line), s:StartExpr())
return indent . border
else else
call append(cline, repeat(g:table_mode_corner, curr_line_count)) return border
endif
endif endif
endfunction endfunction
" }}}2 " }}}2
function! s:FillTableBorder() "{{{2 function! s:UpdateLineBorder(line) "{{{2
let [ current_col, current_line ] = [ col('.'), line('.') ] let cline = a:line
if g:table_mode_no_border_padding let hf = s:StartExpr() . g:table_mode_corner . '[' . g:table_mode_corner .
silent! execute '%s/' . g:table_mode_corner . '\zs\([' . \ g:table_mode_fillchar . ']*' . g:table_mode_corner . '\?\s*$'
\ g:table_mode_fillchar . ' ]*\)\ze' . g:table_mode_corner .
\ '/\=repeat("' . g:table_mode_fillchar . '", s:Strlen(submatch(0)))/g' let border = s:GenerateBorder(cline)
else
silent! execute '%s/' . g:table_mode_corner . ' \zs\([' . let [prev_line, next_line] = [getline(cline-1), getline(cline+1)]
\ g:table_mode_fillchar . ' ]*\)\ze ' . g:table_mode_corner . if next_line =~# hf
\ '/\=repeat("' . g:table_mode_fillchar . '", s:Strlen(submatch(0)))/g' if next_line !=# border
call setline(cline+1, border)
endif
else
call append(cline, border)
endif
if prev_line =~# hf
if prev_line !=# border
call setline(cline-1, border)
endif
else
call append(cline-1, border)
endif endif
call cursor(current_line, current_col)
endfunction endfunction
" }}}2 " }}}2
@@ -171,7 +162,6 @@ function! s:Tableizeline(line, ...) "{{{2
if a:0 && type(a:1) == type('') && !empty(a:1) | let delim = a:1[1:-1] | endif if a:0 && type(a:1) == type('') && !empty(a:1) | let delim = a:1[1:-1] | endif
call s:ConvertDelimiterToSeparator(a:line, delim) call s:ConvertDelimiterToSeparator(a:line, delim)
if g:table_mode_border | call s:UpdateLineBorder(a:line) | endif if g:table_mode_border | call s:UpdateLineBorder(a:line) | endif
call tablemode#TableRealign()
endfunction endfunction
" }}}2 " }}}2
@@ -193,8 +183,7 @@ function! tablemode#TableizeInsertMode() "{{{2
if s:IsTableModeActive() && getline('.') =~# (s:StartExpr() . g:table_mode_separator) if s:IsTableModeActive() && getline('.') =~# (s:StartExpr() . g:table_mode_separator)
let column = s:Strlen(substitute(getline('.')[0:col('.')], '[^' . g:table_mode_separator . ']', '', 'g')) let column = s:Strlen(substitute(getline('.')[0:col('.')], '[^' . g:table_mode_separator . ']', '', 'g'))
let position = s:Strlen(matchstr(getline('.')[0:col('.')], '.*' . g:table_mode_separator . '\s*\zs.*')) let position = s:Strlen(matchstr(getline('.')[0:col('.')], '.*' . g:table_mode_separator . '\s*\zs.*'))
if g:table_mode_border | call s:UpdateLineBorder(line('.')) | endif call tablemode#TableRealign('.')
call tablemode#TableRealign()
normal! 0 normal! 0
call search(repeat('[^' . g:table_mode_separator . ']*' . g:table_mode_separator, column) . '\s\{-\}' . repeat('.', position), 'ce', line('.')) call search(repeat('[^' . g:table_mode_separator . ']*' . g:table_mode_separator, column) . '\s\{-\}' . repeat('.', position), 'ce', line('.'))
endif endif
@@ -234,7 +223,8 @@ function! tablemode#TableizeRange(...) range "{{{2
undojoin undojoin
let lnum = lnum + shift let lnum = lnum + shift
endwhile endwhile
if g:table_mode_border | call s:FillTableBorder() | endif
if g:table_mode_border | call tablemode#TableRealign(lnum - shift) | endif
endfunction endfunction
" }}}2 " }}}2
@@ -248,10 +238,47 @@ function! tablemode#TableizeByDelimiter() "{{{2
endfunction endfunction
" }}}2 " }}}2
function! tablemode#TableRealign() "{{{2 function! tablemode#TableRealign(line) "{{{2
if g:table_mode_no_border_padding && g:table_mode_align !=# 'c0' | let g:table_mode_align = 'c0' | endif let line = 0
execute 'Tabularize/[' . g:table_mode_separator . g:table_mode_corner . ']/' . g:table_mode_align if type(a:line) == type('')
if g:table_mode_border | call s:FillTableBorder() | endif let line = line(a:line)
else
let line = a:line
endif
let rowCount = 1
if g:table_mode_border | let rowCount = 2 | endif
let [lnums, lines] = [[], []]
let tline = line
while tline > 0
if tablemode#IsATableRow(tline)
call insert(lnums, tline)
call insert(lines, getline(tline))
else
break
endif
let tline = tline - rowCount
endwhile
let tline = line + rowCount
while tline <= line('$')
if tablemode#IsATableRow(tline)
call add(lnums, tline)
call add(lines, getline(tline))
else
break
endif
let tline = tline + rowCount
endwhile
call tabular#TabularizeStrings(lines, g:table_mode_separator)
for lnum in lnums
let index = index(lnums, lnum)
call setline(lnum, lines[index])
call s:UpdateLineBorder(lnum)
endfor
endfunction endfunction
" }}}2 " }}}2
@@ -262,7 +289,7 @@ endfunction
function! tablemode#RowCount(line) "{{{2 function! tablemode#RowCount(line) "{{{2
let line = 0 let line = 0
if type(line) == type('') if type(a:line) == type('')
let line = line(a:line) let line = line(a:line)
else else
let line = a:line let line = a:line
@@ -322,7 +349,7 @@ endfunction
function! tablemode#ColumnCount(line) "{{{2 function! tablemode#ColumnCount(line) "{{{2
let line = 0 let line = 0
if type(line) == type('') if type(a:line) == type('')
let line = line(a:line) let line = line(a:line)
else else
let line = a:line let line = a:line

View File

@@ -1,7 +1,7 @@
*table-mode.txt* Table Mode for easy table formatting. v2.2.2 *table-mode.txt* Table Mode for easy table formatting. v2.3.0
=============================================================================== ===============================================================================
Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER
VERSION 2.2.2 VERSION 2.3.0
Author: Dhruva Sagar <http://dhruvasagar.com/> Author: Dhruva Sagar <http://dhruvasagar.com/>
License: MIT <http://opensource.org/licenses/MIT/> License: MIT <http://opensource.org/licenses/MIT/>
@@ -44,6 +44,12 @@ The table mode is disabled by default and you can enter table mode using
|table-mode-toggle-map| or you can also enable it permanently using |table-mode-toggle-map| or you can also enable it permanently using
|g:table-mode-always-active| if you wish. |g:table-mode-always-active| if you wish.
Table Mode allows for creation of tables within comments, it looks at the
'commentstring' setting to identify whether the current line is commented.
Table Mode enables conversion of delimited text into tables. Again like table
creation, this is also applicable within comments.
=============================================================================== ===============================================================================
OPTIONS *table-mode-options* OPTIONS *table-mode-options*
@@ -180,6 +186,9 @@ MAPPINGS *table-mode-mappings*
*table-mode-mappings-realign* *table-mode-mappings-realign*
<Leader>tr Realigns table columns <Leader>tr Realigns table columns
*table-mode-mappings-motions*
<Leader>t[hjkl] Move to previous | below | above | right cell in the table.
=============================================================================== ===============================================================================
COMMANDS *table-mode-commands* COMMANDS *table-mode-commands*