Added Tableize to wrap content into a table.

- Added :Tableize which accepts a range. It checks if the content has a
  delimiter defined by g:table_mode_delimiter and if so, it adds a table
  around it.
- Added <Leader>T mapping, which is defined by g:table_mode_tableize_map
  that applies :Tableize on the visually selected range.
- Added options for tableize.
- Updated vim doc.
This commit is contained in:
Dhruva Sagar
2013-03-18 12:02:48 +05:30
parent 71135d6b60
commit b2ba3caf24
4 changed files with 82 additions and 19 deletions

View File

@@ -3,6 +3,9 @@
An awesome automatic table creator & formatter allowing one to create neat An awesome automatic table creator & formatter allowing one to create neat
tables as you type. tables as you type.
Change Log for v1.1
Added Tableize command and mapping to convert existing content into a table.
# Installation # Installation
There are 2 ways to do this There are 2 ways to do this

View File

@@ -1,7 +1,7 @@
*table-mode.txt* Table Mode for easy table formatting. v1.0 *table-mode.txt* Table Mode for easy table formatting. v1.0
=============================================================================== ===============================================================================
Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER
VERSION 1.0 VERSION 1.1
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/>
@@ -55,6 +55,7 @@ Overview:
|table-mode-options-fillchar| ........ Set table fillchar character |table-mode-options-fillchar| ........ Set table fillchar character
|table-mode-options-toggle-map| ...... Set table mode toggle mapping |table-mode-options-toggle-map| ...... Set table mode toggle mapping
|table-mode-options-always-active| ... Set table mode to always enabled |table-mode-options-always-active| ... Set table mode to always enabled
|table-mode-options-delimiter| ....... Set the delimiter for Tableize
g:table_mode_loaded *table-mode-options-loaded* g:table_mode_loaded *table-mode-options-loaded*
Use this option to disable the plugin: > Use this option to disable the plugin: >
@@ -99,32 +100,55 @@ g:table_mode_always_active *table-mode-options-always-active*
the table mode or |table-mode-commands-enable| to enable and the table mode or |table-mode-commands-enable| to enable and
|table-mode-commands-disable| to disable mode when needed. |table-mode-commands-disable| to disable mode when needed.
g:table_mode_delimiter *table-mode-options-delimiter*
Use this option to define the delimiter which used by
|table-mode-commands-tableize|
=============================================================================== ===============================================================================
MAPPINGS *table-mode-mappings* MAPPINGS *table-mode-mappings*
*table-mode-mappings-toggle* *table-mode-mappings-toggle*
<Leader>tm Enable table mode for the current buffer. You can change this <Leader>tm Toggle table mode for the current buffer. You can change this
using the |toggle-mode-options-toggle-map| option. This is using the |toggle-mode-options-toggle-map| option.
applicable only if |table-mode-options-always-active| is not set.
NOTE This is applicable only if
|table-mode-options-always-active| is not set.
*table-mode-mappings-trigger* *table-mode-mappings-trigger*
| Trigger table creation in table mode. You can change this | Trigger table creation in table mode. You can change this
using the |toggle-mode-options-separator| option. using the |toggle-mode-options-separator| option.
<Leader>T Triggers |table-mode-commands-tableize| on the visually
selected content.
=============================================================================== ===============================================================================
COMMANDS *table-mode-commands* COMMANDS *table-mode-commands*
*table-mode-commands-toggle* *table-mode-commands-toggle*
:TableModeToggle :TableModeToggle
Toggles the table mode. Same effect as |toggle-mode-mappings-toggle| Toggles the table mode. Same effect as |toggle-mode-mappings-toggle|.
NOTE this is applicable only if |table-mode-options-always-active| is
not set.
*table-mode-commands-enable* *table-mode-commands-enable*
:TableModeEnable :TableModeEnable
Enables Table Mode. Enables Table Mode.
NOTE this is applicable only if |table-mode-options-always-active| is
not set.
*table-mode-commands-disable* *table-mode-commands-disable*
:TableModeDisable :TableModeDisable
Disables Table Mode. Disables Table Mode.
NOTE this is applicable only if |table-mode-options-always-active| is
not set.
:Tableize *table-mode-commands-tableize*
This converts the current line into a table if it consists of
|table-mode-options-delimiter|. This accepts a range, without which it
applies on the current line.
=============================================================================== ===============================================================================
CONTRIBUTING *table-mode-contributing* CONTRIBUTING *table-mode-contributing*

View File

@@ -1,6 +1,7 @@
table-mode-commands table-mode.txt /*table-mode-commands* table-mode-commands table-mode.txt /*table-mode-commands*
table-mode-commands-disable table-mode.txt /*table-mode-commands-disable* table-mode-commands-disable table-mode.txt /*table-mode-commands-disable*
table-mode-commands-enable table-mode.txt /*table-mode-commands-enable* table-mode-commands-enable table-mode.txt /*table-mode-commands-enable*
table-mode-commands-tableize table-mode.txt /*table-mode-commands-tableize*
table-mode-commands-toggle table-mode.txt /*table-mode-commands-toggle* table-mode-commands-toggle table-mode.txt /*table-mode-commands-toggle*
table-mode-contents table-mode.txt /*table-mode-contents* table-mode-contents table-mode.txt /*table-mode-contents*
table-mode-contributing table-mode.txt /*table-mode-contributing* table-mode-contributing table-mode.txt /*table-mode-contributing*
@@ -13,6 +14,7 @@ table-mode-options table-mode.txt /*table-mode-options*
table-mode-options-always-active table-mode.txt /*table-mode-options-always-active* table-mode-options-always-active table-mode.txt /*table-mode-options-always-active*
table-mode-options-border table-mode.txt /*table-mode-options-border* table-mode-options-border table-mode.txt /*table-mode-options-border*
table-mode-options-corner table-mode.txt /*table-mode-options-corner* table-mode-options-corner table-mode.txt /*table-mode-options-corner*
table-mode-options-delimiter table-mode.txt /*table-mode-options-delimiter*
table-mode-options-fillchar table-mode.txt /*table-mode-options-fillchar* table-mode-options-fillchar table-mode.txt /*table-mode-options-fillchar*
table-mode-options-loaded table-mode.txt /*table-mode-options-loaded* table-mode-options-loaded table-mode.txt /*table-mode-options-loaded*
table-mode-options-separator table-mode.txt /*table-mode-options-separator* table-mode-options-separator table-mode.txt /*table-mode-options-separator*

View File

@@ -24,6 +24,8 @@ call s:SetGlobalOptDefault('table_mode_separator', '|')
call s:SetGlobalOptDefault('table_mode_fillchar', '-') call s:SetGlobalOptDefault('table_mode_fillchar', '-')
call s:SetGlobalOptDefault('table_mode_toggle_map', '<Leader>tm') call s:SetGlobalOptDefault('table_mode_toggle_map', '<Leader>tm')
call s:SetGlobalOptDefault('table_mode_always_active', 0) call s:SetGlobalOptDefault('table_mode_always_active', 0)
call s:SetGlobalOptDefault('table_mode_delimiter', ',')
call s:SetGlobalOptDefault('table_mode_tableize_map', '<Leader>T')
function! s:SetBufferOptDefault(opt, val) function! s:SetBufferOptDefault(opt, val)
if !exists('b:' . a:opt) if !exists('b:' . a:opt)
@@ -48,30 +50,35 @@ function! s:CountSeparator(line, separator)
return strlen(substitute(getline(a:line), '[^' . a:separator . ']', '', 'g')) return strlen(substitute(getline(a:line), '[^' . a:separator . ']', '', 'g'))
endfunction endfunction
function! s:UpdateTableBorder() function! s:UpdateLineBorder(...)
let cline = a:0 ? a:1 : line('.')
let hf = '^\s*' . g:table_mode_corner . '[' . g:table_mode_corner . ' ' . g:table_mode_fillchar . ']*' . g:table_mode_corner . '\?\s*$' let hf = '^\s*' . g:table_mode_corner . '[' . g:table_mode_corner . ' ' . g:table_mode_fillchar . ']*' . g:table_mode_corner . '\?\s*$'
let curr_line_count = s:CountSeparator(cline, g:table_mode_separator)
if getline(line('.')-1) =~# hf if getline(cline-1) =~# hf
if s:CountSeparator(line('.')-1, g:table_mode_corner) < s:CountSeparator(line('.'), g:table_mode_separator) let prev_line_count = s:CountSeparator(cline-1, g:table_mode_corner)
exec 'normal! kA' . g:table_mode_corner . "\<Esc>j" if prev_line_count < curr_line_count
exec 'normal! kA' . repeat(g:table_mode_corner, curr_line_count - prev_line_count) . "\<Esc>j"
endif endif
else else
call append(line('.')-1, g:table_mode_corner) call append(cline-1, repeat(g:table_mode_corner, curr_line_count))
let cline = a:0 ? (a:1+1) : line('.')
endif endif
if getline(line('.')+1) =~# hf if getline(cline+1) =~# hf
if s:CountSeparator(line('.')+1, g:table_mode_corner) < s:CountSeparator(line('.'), g:table_mode_separator) let next_line_count = s:CountSeparator(cline+1, g:table_mode_corner)
exec 'normal! jA' . g:table_mode_corner . "\<Esc>k" if next_line_count < curr_line_count
exec 'normal! jA' . repeat(g:table_mode_corner, curr_line_count - next_line_count) . "\<Esc>k"
end end
else else
call append(line('.'), g:table_mode_corner) call append(cline, repeat(g:table_mode_corner, curr_line_count))
endif endif
endfunction endfunction
function! s:FillTableBorder() function! s:FillTableBorder()
let current_col = col('.') let current_col = col('.')
let current_line = line('.') let current_line = line('.')
execute '%s/' . g:table_mode_corner . ' \zs\([\' . g:table_mode_fillchar . ' ]*\)\ze ' . g:table_mode_corner . '/\=repeat("' . g:table_mode_fillchar . '", strlen(submatch(0)))/ge' execute 'silent! %s/' . g:table_mode_corner . ' \zs\([\' . g:table_mode_fillchar . ' ]*\)\ze ' . g:table_mode_corner . '/\=repeat("' . g:table_mode_fillchar . '", strlen(submatch(0)))/ge'
call cursor(current_line, current_col) call cursor(current_line, current_col)
endfunction endfunction
@@ -101,12 +108,18 @@ function! s:IsTableModeActive()
return b:table_mode_active return b:table_mode_active
endfunction endfunction
function! s:ConvertDelimiterToSeparator(line)
if getline(a:line) =~# g:table_mode_delimiter
execute 'silent! ' . a:line . 's/^\|' . g:table_mode_delimiter . '\|$/' . g:table_mode_separator . '/ge'
endif
endfunction
function! s:Tableize() function! s:Tableize()
if s:IsTableModeActive() && exists(':Tabularize') && getline('.') =~# ('^\s*' . g:table_mode_separator) if s:IsTableModeActive() && exists(':Tabularize') && getline('.') =~# ('^\s*' . g:table_mode_separator)
let column = strlen(substitute(getline('.')[0:col('.')], '[^' . g:table_mode_separator . ']', '', 'g')) let column = strlen(substitute(getline('.')[0:col('.')], '[^' . g:table_mode_separator . ']', '', 'g'))
let position = strlen(matchstr(getline('.')[0:col('.')], '.*' . g:table_mode_separator . '\s*\zs.*')) let position = strlen(matchstr(getline('.')[0:col('.')], '.*' . g:table_mode_separator . '\s*\zs.*'))
if g:table_mode_border if g:table_mode_border
call s:UpdateTableBorder() call s:UpdateLineBorder()
endif endif
exec 'Tabularize/[' . g:table_mode_separator . g:table_mode_corner . ']/l1' exec 'Tabularize/[' . g:table_mode_separator . g:table_mode_corner . ']/l1'
if g:table_mode_border if g:table_mode_border
@@ -117,6 +130,24 @@ function! s:Tableize()
endif endif
endfunction endfunction
function! s:Tableizeline(line)
call s:ConvertDelimiterToSeparator(a:line)
call s:UpdateLineBorder(a:line)
exec 'Tabularize/[' . g:table_mode_separator . g:table_mode_corner . ']/l1'
endfunction
function! s:TableizeRange() range
call s:Tableizeline(a:firstline)
undojoin
let lnum = a:firstline+3
while lnum <= (a:firstline + (a:lastline - a:firstline+1)*2)
call s:Tableizeline(lnum)
undojoin
let lnum = lnum + 2
endwhile
call s:FillTableBorder()
endfunction
if !g:table_mode_always_active if !g:table_mode_always_active
exec "nnoremap <silent> " . g:table_mode_toggle_map . exec "nnoremap <silent> " . g:table_mode_toggle_map .
\ " <Esc>:call <SID>TableModeToggle()<CR>" \ " <Esc>:call <SID>TableModeToggle()<CR>"
@@ -125,4 +156,7 @@ if !g:table_mode_always_active
command! -nargs=0 TableModeDisable call s:TableModeDisable() command! -nargs=0 TableModeDisable call s:TableModeDisable()
endif endif
exec "inoremap <silent> " . s:table_mode_separator_map . ' ' . exec "inoremap <silent> " . s:table_mode_separator_map . ' ' .
\ s:table_mode_separator_map . "<Esc>:call <SID>Tableize()<CR>a" \ s:table_mode_separator_map . "<Esc>:call <SID>Tableize()<CR>a"
command! -nargs=0 -range Tableize <line1>,<line2>call s:TableizeRange()
exec "xnoremap <silent> " . g:table_mode_tableize_map . " :Tableize<CR>"