mirror of
https://github.com/dhruvasagar/vim-table-mode.git
synced 2025-11-11 12:33:47 -05:00
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:
@@ -3,6 +3,9 @@
|
||||
An awesome automatic table creator & formatter allowing one to create neat
|
||||
tables as you type.
|
||||
|
||||
Change Log for v1.1
|
||||
Added Tableize command and mapping to convert existing content into a table.
|
||||
|
||||
# Installation
|
||||
|
||||
There are 2 ways to do this
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
*table-mode.txt* Table Mode for easy table formatting. v1.0
|
||||
===============================================================================
|
||||
Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER
|
||||
VERSION 1.0
|
||||
VERSION 1.1
|
||||
|
||||
Author: Dhruva Sagar <http://dhruvasagar.com/>
|
||||
License: MIT <http://opensource.org/licenses/MIT/>
|
||||
@@ -55,6 +55,7 @@ Overview:
|
||||
|table-mode-options-fillchar| ........ Set table fillchar character
|
||||
|table-mode-options-toggle-map| ...... Set table mode toggle mapping
|
||||
|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*
|
||||
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
|
||||
|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*
|
||||
|
||||
*table-mode-mappings-toggle*
|
||||
<Leader>tm Enable table mode for the current buffer. You can change this
|
||||
using the |toggle-mode-options-toggle-map| option. This is
|
||||
applicable only if |table-mode-options-always-active| is not set.
|
||||
<Leader>tm Toggle table mode for the current buffer. You can change this
|
||||
using the |toggle-mode-options-toggle-map| option.
|
||||
|
||||
NOTE This is applicable only if
|
||||
|table-mode-options-always-active| is not set.
|
||||
|
||||
*table-mode-mappings-trigger*
|
||||
| Trigger table creation in table mode. You can change this
|
||||
using the |toggle-mode-options-separator| option.
|
||||
|
||||
<Leader>T Triggers |table-mode-commands-tableize| on the visually
|
||||
selected content.
|
||||
|
||||
===============================================================================
|
||||
COMMANDS *table-mode-commands*
|
||||
|
||||
*table-mode-commands-toggle*
|
||||
: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*
|
||||
:TableModeEnable
|
||||
Enables Table Mode.
|
||||
|
||||
NOTE this is applicable only if |table-mode-options-always-active| is
|
||||
not set.
|
||||
|
||||
*table-mode-commands-disable*
|
||||
:TableModeDisable
|
||||
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*
|
||||
|
||||
|
||||
2
doc/tags
2
doc/tags
@@ -1,6 +1,7 @@
|
||||
table-mode-commands table-mode.txt /*table-mode-commands*
|
||||
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-tableize table-mode.txt /*table-mode-commands-tableize*
|
||||
table-mode-commands-toggle table-mode.txt /*table-mode-commands-toggle*
|
||||
table-mode-contents table-mode.txt /*table-mode-contents*
|
||||
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-border table-mode.txt /*table-mode-options-border*
|
||||
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-loaded table-mode.txt /*table-mode-options-loaded*
|
||||
table-mode-options-separator table-mode.txt /*table-mode-options-separator*
|
||||
|
||||
@@ -24,6 +24,8 @@ call s:SetGlobalOptDefault('table_mode_separator', '|')
|
||||
call s:SetGlobalOptDefault('table_mode_fillchar', '-')
|
||||
call s:SetGlobalOptDefault('table_mode_toggle_map', '<Leader>tm')
|
||||
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)
|
||||
if !exists('b:' . a:opt)
|
||||
@@ -48,30 +50,35 @@ function! s:CountSeparator(line, separator)
|
||||
return strlen(substitute(getline(a:line), '[^' . a:separator . ']', '', 'g'))
|
||||
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 curr_line_count = s:CountSeparator(cline, g:table_mode_separator)
|
||||
|
||||
if getline(line('.')-1) =~# hf
|
||||
if s:CountSeparator(line('.')-1, g:table_mode_corner) < s:CountSeparator(line('.'), g:table_mode_separator)
|
||||
exec 'normal! kA' . g:table_mode_corner . "\<Esc>j"
|
||||
if getline(cline-1) =~# hf
|
||||
let prev_line_count = s:CountSeparator(cline-1, g:table_mode_corner)
|
||||
if prev_line_count < curr_line_count
|
||||
exec 'normal! kA' . repeat(g:table_mode_corner, curr_line_count - prev_line_count) . "\<Esc>j"
|
||||
endif
|
||||
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
|
||||
|
||||
if getline(line('.')+1) =~# hf
|
||||
if s:CountSeparator(line('.')+1, g:table_mode_corner) < s:CountSeparator(line('.'), g:table_mode_separator)
|
||||
exec 'normal! jA' . g:table_mode_corner . "\<Esc>k"
|
||||
if getline(cline+1) =~# hf
|
||||
let next_line_count = s:CountSeparator(cline+1, g:table_mode_corner)
|
||||
if next_line_count < curr_line_count
|
||||
exec 'normal! jA' . repeat(g:table_mode_corner, curr_line_count - next_line_count) . "\<Esc>k"
|
||||
end
|
||||
else
|
||||
call append(line('.'), g:table_mode_corner)
|
||||
call append(cline, repeat(g:table_mode_corner, curr_line_count))
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:FillTableBorder()
|
||||
let current_col = col('.')
|
||||
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)
|
||||
endfunction
|
||||
|
||||
@@ -101,12 +108,18 @@ function! s:IsTableModeActive()
|
||||
return b:table_mode_active
|
||||
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()
|
||||
if s:IsTableModeActive() && exists(':Tabularize') && getline('.') =~# ('^\s*' . g:table_mode_separator)
|
||||
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.*'))
|
||||
if g:table_mode_border
|
||||
call s:UpdateTableBorder()
|
||||
call s:UpdateLineBorder()
|
||||
endif
|
||||
exec 'Tabularize/[' . g:table_mode_separator . g:table_mode_corner . ']/l1'
|
||||
if g:table_mode_border
|
||||
@@ -117,6 +130,24 @@ function! s:Tableize()
|
||||
endif
|
||||
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
|
||||
exec "nnoremap <silent> " . g:table_mode_toggle_map .
|
||||
\ " <Esc>:call <SID>TableModeToggle()<CR>"
|
||||
@@ -125,4 +156,7 @@ if !g:table_mode_always_active
|
||||
command! -nargs=0 TableModeDisable call s:TableModeDisable()
|
||||
endif
|
||||
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>"
|
||||
|
||||
Reference in New Issue
Block a user