Compare commits

..

2 Commits

Author SHA1 Message Date
Dhruva Sagar
241d0ccba0 Finished #6
- Updated doc.
- Bumped version.
- Added a mapping for realigning table columns.
- Added mapping for moving to next / prev row / column.
2013-05-01 01:57:47 +05:30
Dhruva Sagar
ef3e5323d0 Fixed bugs
- Covered case when commentstring is not set (default).
- Escaping commentstring symbols.
2013-04-09 10:53:57 +05:30
3 changed files with 165 additions and 14 deletions

View File

@@ -4,7 +4,7 @@
" Author: Dhruva Sagar <http://dhruvasagar.com/>
" License: MIT (http://www.opensource.org/licenses/MIT)
" Website: http://github.com/dhruvasagar/vim-table-mode
" Version: 2.2.1
" Version: 2.2.2
" Note: This plugin was heavily inspired by the 'CucumberTables.vim'
" (https://gist.github.com/tpope/287147) plugin by Tim Pope and
" uses a small amount of code from it.
@@ -57,8 +57,18 @@ call s:SetGlobalOptDefault('table_mode_tableize_map', '<Leader>tt')
call s:SetGlobalOptDefault('table_mode_tableize_op_map', '<Leader>T')
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_motion_prefix', '<Leader>t')
"}}}1
function! s:TableMotion() "{{{1
let direction = nr2char(getchar())
for i in range(v:count1)
call tablemode#TableMotion(direction)
endfor
endfunction
" }}}1
" Define Commands & Mappings {{{1
if !g:table_mode_always_active
exec "nnoremap <silent> " . g:table_mode_toggle_map .
@@ -80,6 +90,9 @@ command! -nargs=? -range Tableize <line1>,<line2>call tablemode#TableizeRange(<q
execute "xnoremap <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 "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>"
"}}}1
" Avoiding side effects {{{1

View File

@@ -4,7 +4,7 @@
" Author: Dhruva Sagar <http://dhruvasagar.com/>
" License: MIT (http://www.opensource.org/licenses/MIT)
" Website: http://github.com/dhruvasagar/vim-table-mode
" Version: 2.2.1
" Version: 2.2.2
" Note: This plugin was heavily inspired by the 'CucumberTables.vim'
" (https://gist.github.com/tpope/287147) plugin by Tim Pope and
" uses a small amount of code from it.
@@ -41,17 +41,32 @@ endfunction
" }}}2
function! s:GetCommentStart() "{{{2
return split(substitute(&commentstring, '%s', ' ', 'g'))[0]
let cstring = &commentstring
if s:Strlen(cstring) > 0
return substitute(split(substitute(cstring, '%s', ' ', 'g'))[0], '.', '\\\0', 'g')
else
return ''
endif
endfunction
" }}}2
function! s:StartExpr() "{{{2
return '^\s*\(' . s:GetCommentStart() . '\)\?\s*'
let cstart = s:GetCommentStart()
if s:Strlen(cstart) > 0
return '^\s*\(' . cstart . '\)\?\s*'
else
return '^\s*'
endif
endfunction
" }}}2
function! s:StartCommentExpr() "{{{2
return '^\s*' . s:GetCommentStart() . '\s*'
let cstartexpr = s:GetCommentStart()
if s:Strlen(cstartexpr) > 0
return '^\s*' . cstartexpr . '\s*'
else
return ''
endif
endfunction
" }}}2
@@ -95,7 +110,8 @@ function! s:UpdateLineBorder(line) "{{{2
silent! execute 'normal! kA' . repeat(g:table_mode_corner, curr_line_count - prev_line_count) . "\<Esc>j"
endif
else
if getline(cline) =~# s:StartCommentExpr()
let cstartexpr = s:StartCommentExpr()
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
@@ -110,7 +126,8 @@ function! s:UpdateLineBorder(line) "{{{2
silent! execute 'normal! jA' . repeat(g:table_mode_corner, curr_line_count - next_line_count) . "\<Esc>k"
end
else
if getline(cline) =~# s:StartCommentExpr()
let cstartexpr = s:StartCommentExpr()
if s:Strlen(cstartexpr) > 0 && getline(cline) =~# cstartexpr
let indent = matchstr(getline(cline), s:StartCommentExpr())
call append(cline, indent . repeat(g:table_mode_corner, curr_line_count))
else
@@ -138,8 +155,14 @@ endfunction
function! s:ConvertDelimiterToSeparator(line, ...) "{{{2
let delim = g:table_mode_delimiter
if a:0 | let delim = a:1 | endif
if delim ==# ','
silent! execute a:line . 's/' . "[\'\"][^\'\"]*\\zs,\\ze[^\'\"]*[\'\"]/__COMMA__/g"
endif
silent! execute a:line . 's/' . s:StartExpr() . '\zs\ze.\|' . delim . '\|$/' .
\ g:table_mode_separator . '/g'
if delim ==# ','
silent! execute a:line . 's/' . "[\'\"][^\'\"]*\\zs__COMMA__\\ze[^\'\"]*[\'\"]/,/g"
endif
endfunction
" }}}2
@@ -148,7 +171,22 @@ function! s:Tableizeline(line, ...) "{{{2
if a:0 && type(a:1) == type('') && !empty(a:1) | let delim = a:1[1:-1] | endif
call s:ConvertDelimiterToSeparator(a:line, delim)
if g:table_mode_border | call s:UpdateLineBorder(a:line) | endif
execute 'Tabularize/[' . g:table_mode_separator . g:table_mode_corner . ']/' . g:table_mode_align
call tablemode#TableRealign()
endfunction
" }}}2
function! s:ColumnCount(line) "{{{2
return s:Strlen(substitute(getline(a:line), '[^' . g:table_mode_separator . ']', '', 'g'))-1
endfunction
" }}}2
function! s:IsFirstCell() "{{{2
return tablemode#TableColumnNr('.') ==# 1
endfunction
" }}}2
function! s:IsLastCell() "{{{2
return tablemode#TableColumnNr('.') ==# s:ColumnCount('.')
endfunction
" }}}2
@@ -162,7 +200,7 @@ function! tablemode#TableizeInsertMode() "{{{2
let position = s:Strlen(matchstr(getline('.')[0:col('.')], '.*' . g:table_mode_separator . '\s*\zs.*'))
if g:table_mode_border | call s:UpdateLineBorder(line('.')) | endif
if g:table_mode_no_border_padding && g:table_mode_align !=# 'c0' | let g:table_mode_align = 'c0' | endif
execute 'Tabularize/[' . g:table_mode_separator . g:table_mode_corner . ']/' . g:table_mode_align
call tablemode#TableRealign()
if g:table_mode_border | call s:FillTableBorder() | endif
normal! 0
call search(repeat('[^' . g:table_mode_separator . ']*' . g:table_mode_separator, column) . '\s\{-\}' . repeat('.', position), 'ce', line('.'))
@@ -217,7 +255,88 @@ function! tablemode#TableizeByDelimiter() "{{{2
endfunction
" }}}2
" }}}1
function! tablemode#TableRealign() "{{{2
execute 'Tabularize/[' . g:table_mode_separator . g:table_mode_corner . ']/' . g:table_mode_align
endfunction
" }}}2
" ModeLine {{{
" vim:fdm=marker
function! tablemode#IsATableRow(line) "{{{2
return getline(a:line) =~# (s:StartExpr() . g:table_mode_separator)
endfunction
" }}}2
function! tablemode#TableRowNr(pos) "{{{2
let line = 0
if type(a:pos) == type('')
let line = line(a:pos)
elseif type(a:pos) == type(1)
let line = a:pos
endif
let rowNr = 0
while line > 0
if tablemode#IsATableRow(line)
let rowNr = rowNr + 1
else
break
endif
let line = line - 2
endwhile
return rowNr
endfunction
" }}}2
function! tablemode#TableColumnNr(pos) "{{{2
let pos = []
if type(a:pos) == type('')
let pos = [line(a:pos), col(a:pos)]
elseif type(a:pos) == type([])
let pos = a:pos
else
return 0
endif
return s:Strlen(substitute(getline(pos[0])[0:pos[1]-2], '[^' . g:table_mode_separator . ']', '', 'g'))
endfunction
" }}}2
function! tablemode#TableMotion(direction) "{{{2
if tablemode#IsATableRow('.')
let rowCount = 1
if g:table_mode_border | let rowCount = 2 | endif
if a:direction ==# 'l'
if s:IsLastCell()
if !tablemode#IsATableRow(line('.') + rowCount) | return | endif
call tablemode#TableMotion('j')
normal! 0
endif
" If line starts with g:table_mode_separator
if getline('.')[col('.')-1] ==# g:table_mode_separator
execute 'normal! 2l'
else
execute 'normal! f' . g:table_mode_separator . '2l'
endif
elseif a:direction ==# 'h'
if s:IsFirstCell()
if !tablemode#IsATableRow(line('.') - rowCount) | return | endif
call tablemode#TableMotion('k')
normal! $
endif
" If line ends with g:table_mode_separator
if getline('.')[col('.')-1] ==# g:table_mode_separator
execute 'normal! F' . g:table_mode_separator . '2l'
else
execute 'normal! 2F' . g:table_mode_separator . '2l'
endif
elseif a:direction ==# 'j'
if tablemode#IsATableRow(line('.') + rowCount) | execute 'normal ' . rowCount . 'j' | endif
elseif a:direction ==# 'k'
if tablemode#IsATableRow(line('.') - rowCount) | execute 'normal ' . rowCount . 'k' | endif
endif
endif
endfunction
" }}}2
" }}}1

View File

@@ -1,7 +1,7 @@
*table-mode.txt* Table Mode for easy table formatting. v2.2.1
*table-mode.txt* Table Mode for easy table formatting. v2.2.2
===============================================================================
Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER
VERSION 2.2.1
VERSION 2.2.2
Author: Dhruva Sagar <http://dhruvasagar.com/>
License: MIT <http://opensource.org/licenses/MIT/>
@@ -61,6 +61,8 @@ Overview:
|table-mode-align| .............. Set the text alignment for
Tableize.
|table-mode-no-border-padding| .. Set for no border padding.
|table-mode-realign-map| ........ Set mapping for table realigning.
|table-mode-motion-prefix| ...... Set prefix for table mode motions.
g:table_mode_loaded *table-mode-loaded*
Use this option to disable the plugin: >
@@ -138,6 +140,21 @@ g:table_mode_no_border_padding *table-mode-no-border-padding*
<
NOTE this option changes |table-mode-align| to 'c0', so that
there is no extra padding around the contents.
g:table_mode_realign_map *table-mode-realign-map*
Use this option to define the mapping for realigning table columns.
This is useful in case you make edits to an existing table. >
let g:table_mode_realign_map = '<Leader>tr'
<
g:table_mode_motion_prefix *table-mode-motion-prefix*
Use this option to define the prefix for table mode motion commands. >
let g:table_mode_motion_prefix = '<Leader>t'
<
You can move to the next / previous row / column using the motion
commands. The motions 'hjkl' follow the prefix are in accordance to
standard vim character motions to make them easier to remember. They
may also be preceeded with a [count].
===============================================================================
MAPPINGS *table-mode-mappings*
@@ -159,6 +176,8 @@ MAPPINGS *table-mode-mappings*
<Leader>T Triggers |table-mode-commands-tableize| on the visually
selected asking for user to input the delimiter.
*table-mode-mappings-realign*
<Leader>tr Realigns table columns
===============================================================================
COMMANDS *table-mode-commands*