mirror of
https://github.com/dhruvasagar/vim-table-mode.git
synced 2025-11-08 11:03:47 -05:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
60ab92cd35 | ||
|
|
503271fca5 | ||
|
|
b789e2c86f | ||
|
|
7b89b6b50d | ||
|
|
28dc1626ab | ||
|
|
f8610cebdb | ||
|
|
da871cac51 | ||
|
|
f23737d5fa | ||
|
|
3cf75f665e | ||
|
|
e6e49e8907 |
25
README.md
25
README.md
@@ -4,13 +4,26 @@ An awesome automatic table creator & formatter allowing one to create neat
|
||||
tables as you type.
|
||||
|
||||
## Change Log
|
||||
### Version 2.1.2 :
|
||||
* Bug Fixes #2, #3 & #4
|
||||
|
||||
### Version 2.1.1 :
|
||||
* Added option g:table_mode_align to allow setting Tabular format option for
|
||||
more control on how Tabular aligns text.
|
||||
|
||||
### Version 2.1 :
|
||||
* VIM loads plugins in alphabetical order and so table-mode would be loaded
|
||||
before Tabularize which it depends on. Hence Moved plugin into an after
|
||||
plugin. Checking if Tabularize is available and finish immidiately if it's
|
||||
not.
|
||||
|
||||
### Version 2.0 :
|
||||
* Moved bulk of code to autoload for vimscript optimisation.
|
||||
|
||||
### version 1.1 :
|
||||
### Version 1.1 :
|
||||
* Added Tableize command and mapping to convert existing content into a table.
|
||||
|
||||
### version 1.0 :
|
||||
### Version 1.0 :
|
||||
* First stable release, create tables as you type.
|
||||
|
||||
## Getting Started
|
||||
@@ -26,8 +39,10 @@ There are 2 ways to do this
|
||||
$ cd ~/.vim
|
||||
$ git submodule add git@github.com:dhruvasagar/vim-table-mode.git bundle/table-mode
|
||||
```
|
||||
2. Copy table-mode.vim into ~/.vim/plugin/ (Unix) or vimfiles/plugin/ (Windows)
|
||||
as with other plugins.
|
||||
2. Copy autoload/todomode.vim, plugin/todo-mode.vim, doc/todo-mode.txt to
|
||||
respective ~/.vim/autoload/, ~/.vim/plugin and ~/.vim/doc under UNIX or
|
||||
vimfiles/autoload/, vimfiles/plugin/ and vimfiles/doc under WINDOWS and
|
||||
restart VIM
|
||||
|
||||
### Requirements
|
||||
|
||||
@@ -62,7 +77,7 @@ sure Tabular is installed and loaded into your runtime to ensure this works.
|
||||
the mapping `<Leader>T` with a `[count]` to apply it to the next `[count]`
|
||||
lines in usual vim style.
|
||||
|
||||
### Demo:
|
||||
### Demo
|
||||
|
||||
<a href="http://www.youtube.com/watch?v=sK2IH1hiDkw"><img
|
||||
src="https://raw.github.com/dhruvasagar/vim-table-mode/master/youtube.png"/></a>
|
||||
|
||||
@@ -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.0
|
||||
" Version: 2.1.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.
|
||||
@@ -13,10 +13,10 @@
|
||||
" Permission is hereby granted to use and distribute this code,
|
||||
" with or without modifications, provided that this copyright
|
||||
" notice is copied with it. Like anything else that's free,
|
||||
" taglist.vim is provided *as is* and comes with no warranty of
|
||||
" any kind, either expressed or implied. In no event will the
|
||||
" copyright holder be liable for any damamges resulting from the
|
||||
" use of this software.
|
||||
" table-mode.vim is provided *as is* and comes with no warranty
|
||||
" of any kind, either expressed or implied. In no event will
|
||||
" the copyright holder be liable for any damamges resulting
|
||||
" from the use of this software.
|
||||
" =============================================================================
|
||||
|
||||
" Finish if already loaded {{{1
|
||||
@@ -25,6 +25,13 @@ if exists('g:table_mode_loaded')
|
||||
endif
|
||||
let g:table_mode_loaded = 1
|
||||
"}}}1
|
||||
"
|
||||
" Finish if Tabularize plugin is not available {{{1
|
||||
if !exists(':Tabularize')
|
||||
echoerr 'Table Mode depends on Tabularize, ensure that is installed first.'
|
||||
finish
|
||||
endif
|
||||
" }}}1
|
||||
|
||||
function! s:SetGlobalOptDefault(opt, val) "{{{1
|
||||
if !exists('g:' . a:opt)
|
||||
@@ -38,10 +45,11 @@ call s:SetGlobalOptDefault('table_mode_border', 1)
|
||||
call s:SetGlobalOptDefault('table_mode_corner', '+')
|
||||
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_toggle_map', '<LocalLeader>tm')
|
||||
call s:SetGlobalOptDefault('table_mode_always_active', 0)
|
||||
call s:SetGlobalOptDefault('table_mode_delimiter', ',')
|
||||
call s:SetGlobalOptDefault('table_mode_tableize_map', '<Leader>T')
|
||||
call s:SetGlobalOptDefault('table_mode_align', 'l1')
|
||||
"}}}1
|
||||
|
||||
" Define Commands & Mappings {{{1
|
||||
@@ -51,12 +59,20 @@ if !g:table_mode_always_active
|
||||
command! -nargs=0 TableModeToggle call tablemode#TableModeToggle()
|
||||
command! -nargs=0 TableModeEnable call tablemode#TableModeEnable()
|
||||
command! -nargs=0 TableModeDisable call tablemode#TableModeDisable()
|
||||
else
|
||||
let table_mode_separator_map = g:table_mode_separator
|
||||
" '|' is a special character, we need to map <Bar> instead
|
||||
if g:table_mode_separator ==# '|' | let table_mode_separator_map = '<Bar>' | endif
|
||||
|
||||
execute "inoremap <silent> " . table_mode_separator_map . ' ' .
|
||||
\ table_mode_separator_map . "<Esc>:call tablemode#TableizeInsertMode()<CR>a"
|
||||
unlet table_mode_separator_map
|
||||
endif
|
||||
|
||||
command! -nargs=0 -range Tableize <line1>,<line2>call tablemode#TableizeRange()
|
||||
exec "xnoremap <silent> " . g:table_mode_tableize_map . " :Tableize<CR>"
|
||||
exec "nnoremap <silent> " . g:table_mode_tableize_map . " :Tableize<CR>"
|
||||
execute "xnoremap <silent> " . g:table_mode_tableize_map . " :Tableize<CR>"
|
||||
execute "nnoremap <silent> " . g:table_mode_tableize_map . " :Tableize<CR>"
|
||||
"}}}1
|
||||
|
||||
" ModeLine {{{
|
||||
" vim:fdm=marker
|
||||
" vim:fdl=0 fdm=marker
|
||||
@@ -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.0
|
||||
" Version: 2.1.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.
|
||||
@@ -13,10 +13,10 @@
|
||||
" Permission is hereby granted to use and distribute this code,
|
||||
" with or without modifications, provided that this copyright
|
||||
" notice is copied with it. Like anything else that's free,
|
||||
" taglist.vim is provided *as is* and comes with no warranty of
|
||||
" any kind, either expressed or implied. In no event will the
|
||||
" copyright holder be liable for any damamges resulting from the
|
||||
" use of this software.
|
||||
" table-mode.vim is provided *as is* and comes with no warranty
|
||||
" of any kind, either expressed or implied. In no event will
|
||||
" the copyright holder be liable for any damamges resulting
|
||||
" from the use of this software.
|
||||
" =============================================================================
|
||||
|
||||
" Private Functions {{{1
|
||||
@@ -40,54 +40,6 @@ function! s:CountSeparator(line, separator) "{{{2
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:UpdateLineBorder(line) "{{{2
|
||||
let cline = a: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(cline-1) =~# hf
|
||||
let prev_line_count = s:CountSeparator(cline-1, g:table_mode_corner)
|
||||
if curr_line_count > prev_line_count
|
||||
exec 'normal! kA' . repeat(g:table_mode_corner, curr_line_count - prev_line_count) . "\<Esc>j"
|
||||
endif
|
||||
else
|
||||
call append(cline-1, repeat(g:table_mode_corner, curr_line_count))
|
||||
let cline = a:line + 1
|
||||
endif
|
||||
|
||||
if getline(cline+1) =~# hf
|
||||
let next_line_count = s:CountSeparator(cline+1, g:table_mode_corner)
|
||||
if curr_line_count > next_line_count
|
||||
exec 'normal! jA' . repeat(g:table_mode_corner, curr_line_count - next_line_count) . "\<Esc>k"
|
||||
end
|
||||
else
|
||||
call append(cline, repeat(g:table_mode_corner, curr_line_count))
|
||||
endif
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:FillTableBorder() "{{{2
|
||||
let current_col = col('.')
|
||||
let current_line = line('.')
|
||||
execute 'silent! %s/' . g:table_mode_corner . ' \zs\([' . g:table_mode_fillchar . ' ]*\)\ze ' . g:table_mode_corner . '/\=repeat("' . g:table_mode_fillchar . '", s:Strlen(submatch(0)))/g'
|
||||
call cursor(current_line, current_col)
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:Tableizeline(line) "{{{2
|
||||
if exists(':Tabularize')
|
||||
call s:ConvertDelimiterToSeparator(a:line)
|
||||
call s:UpdateLineBorder(a:line)
|
||||
exec 'Tabularize/[' . g:table_mode_separator . g:table_mode_corner . ']/l1'
|
||||
endif
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:ConvertDelimiterToSeparator(line) "{{{2
|
||||
execute 'silent! ' . a:line . 's/^\s*\zs\ze.\|' . g:table_mode_delimiter . '\|$/' . g:table_mode_separator . '/g'
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:IsTableModeActive() "{{{2
|
||||
if g:table_mode_always_active | return 1 | endif
|
||||
|
||||
@@ -96,35 +48,16 @@ function! s:IsTableModeActive() "{{{2
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:Tableize() "{{{2
|
||||
if s:IsTableModeActive() && exists(':Tabularize') && getline('.') =~# ('^\s*' . g:table_mode_separator)
|
||||
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.*'))
|
||||
if g:table_mode_border | call s:UpdateLineBorder(line('.')) | endif
|
||||
exec 'Tabularize/[' . g:table_mode_separator . g:table_mode_corner . ']/l1'
|
||||
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('.'))
|
||||
endif
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:TableModeSeparatorMap() "{{{2
|
||||
if g:table_mode_separator ==# '|'
|
||||
let table_mode_separator_map = '<Bar>'
|
||||
else
|
||||
let table_mode_separator_map = g:table_mode_separator
|
||||
endif
|
||||
return table_mode_separator_map
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:ToggleMapping() "{{{2
|
||||
if exists('b:table_mode_active') && b:table_mode_active
|
||||
exec "inoremap <silent> " . s:TableModeSeparatorMap() . ' ' .
|
||||
\ s:TableModeSeparatorMap() . "<Esc>:call <SID>Tableize()<CR>a"
|
||||
call s:SetBufferOptDefault('table_mode_separator_map', g:table_mode_separator)
|
||||
" '|' is a special character, we need to map <Bar> instead
|
||||
if g:table_mode_separator ==# '|' | let b:table_mode_separator_map = '<Bar>' | endif
|
||||
|
||||
execute "inoremap <silent> <buffer> " . b:table_mode_separator_map . ' ' .
|
||||
\ b:table_mode_separator_map . "<Esc>:call tablemode#TableizeInsertMode()<CR>a"
|
||||
else
|
||||
exec "iunmap <silent> " . s:TableModeSeparatorMap()
|
||||
execute "iunmap <silent> <buffer> " . b:table_mode_separator_map
|
||||
endif
|
||||
endfunction
|
||||
" }}}2
|
||||
@@ -135,10 +68,73 @@ function! s:SetActive(bool) "{{{2
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:UpdateLineBorder(line) "{{{2
|
||||
let cline = a: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(cline-1) =~# hf
|
||||
let prev_line_count = s:CountSeparator(cline-1, g:table_mode_corner)
|
||||
if curr_line_count > prev_line_count
|
||||
execute 'normal! kA' . repeat(g:table_mode_corner, curr_line_count - prev_line_count) . "\<Esc>j"
|
||||
endif
|
||||
else
|
||||
call append(cline-1, repeat(g:table_mode_corner, curr_line_count))
|
||||
let cline = a:line + 1 " because of the append, the current line moved down
|
||||
endif
|
||||
|
||||
if getline(cline+1) =~# hf
|
||||
let next_line_count = s:CountSeparator(cline+1, g:table_mode_corner)
|
||||
if curr_line_count > next_line_count
|
||||
execute 'normal! jA' . repeat(g:table_mode_corner, curr_line_count - next_line_count) . "\<Esc>k"
|
||||
end
|
||||
else
|
||||
call append(cline, repeat(g:table_mode_corner, curr_line_count))
|
||||
endif
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:FillTableBorder() "{{{2
|
||||
let current_col = col('.')
|
||||
let current_line = line('.')
|
||||
execute 'silent! %s/' . g:table_mode_corner . ' \zs\([' .
|
||||
\ g:table_mode_fillchar . ' ]*\)\ze ' . g:table_mode_corner . '/\=repeat("' .
|
||||
\ g:table_mode_fillchar . '", s:Strlen(submatch(0)))/g'
|
||||
call cursor(current_line, current_col)
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:ConvertDelimiterToSeparator(line) "{{{2
|
||||
execute 'silent! ' . a:line . 's/^\s*\zs\ze.\|' . g:table_mode_delimiter .
|
||||
\ '\|$/' . g:table_mode_separator . '/g'
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:Tableizeline(line) "{{{2
|
||||
call s:ConvertDelimiterToSeparator(a:line)
|
||||
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
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
" }}}1
|
||||
|
||||
" Public API {{{1
|
||||
|
||||
function! tablemode#TableizeInsertMode() "{{{2
|
||||
if s:IsTableModeActive() && getline('.') =~# ('^\s*' . g:table_mode_separator)
|
||||
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.*'))
|
||||
if g:table_mode_border | call s:UpdateLineBorder(line('.')) | endif
|
||||
execute 'Tabularize/[' . g:table_mode_separator . g:table_mode_corner . ']/' . g:table_mode_align
|
||||
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('.'))
|
||||
endif
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! tablemode#TableModeEnable() "{{{2
|
||||
call s:SetActive(1)
|
||||
endfunction
|
||||
@@ -160,17 +156,19 @@ endfunction
|
||||
" }}}2
|
||||
|
||||
function! tablemode#TableizeRange() range "{{{2
|
||||
let shift = 1
|
||||
if g:table_mode_border | let shift = shift + 1 | endif
|
||||
call s:Tableizeline(a:firstline)
|
||||
undojoin
|
||||
" The first one causes 2 extra lines for top & bottom border while the
|
||||
" following lines cause only 1 for the bottom border.
|
||||
let lnum = a:firstline+3
|
||||
while lnum <= (a:firstline + (a:lastline - a:firstline+1)*2)
|
||||
let lnum = a:firstline + shift + (g:table_mode_border > 0)
|
||||
while lnum < (a:firstline + (a:lastline - a:firstline + 1)*shift)
|
||||
call s:Tableizeline(lnum)
|
||||
undojoin
|
||||
let lnum = lnum + 2
|
||||
let lnum = lnum + shift
|
||||
endwhile
|
||||
call s:FillTableBorder()
|
||||
if g:table_mode_border | call s:FillTableBorder() | endif
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
*table-mode.txt* Table Mode for easy table formatting. v2.0
|
||||
*table-mode.txt* Table Mode for easy table formatting. v2.1.2
|
||||
===============================================================================
|
||||
Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER
|
||||
VERSION 2.0
|
||||
VERSION 2.1.2
|
||||
|
||||
Author: Dhruva Sagar <http://dhruvasagar.com/>
|
||||
License: MIT <http://opensource.org/licenses/MIT/>
|
||||
@@ -56,6 +56,7 @@ Overview:
|
||||
|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
|
||||
|table-mode-options-align| ........... Set the text alignment for Tableize
|
||||
|
||||
g:table_mode_loaded *table-mode-options-loaded*
|
||||
Use this option to disable the plugin: >
|
||||
@@ -104,6 +105,11 @@ g:table_mode_delimiter *table-mode-options-delimiter*
|
||||
Use this option to define the delimiter which used by
|
||||
|table-mode-commands-tableize|
|
||||
|
||||
g:table_mode_align *table-mode-options-align*
|
||||
Use this option to define the format for text alignment to be used for
|
||||
the tables. Go through |tabular-walkthrough| for details on how to set
|
||||
the format options for alignment. >
|
||||
let g:table_mode_align = 'l1'
|
||||
===============================================================================
|
||||
MAPPINGS *table-mode-mappings*
|
||||
|
||||
|
||||
1
doc/tags
1
doc/tags
@@ -15,6 +15,7 @@ table-mode-mappings table-mode.txt /*table-mode-mappings*
|
||||
table-mode-mappings-toggle table-mode.txt /*table-mode-mappings-toggle*
|
||||
table-mode-mappings-trigger table-mode.txt /*table-mode-mappings-trigger*
|
||||
table-mode-options table-mode.txt /*table-mode-options*
|
||||
table-mode-options-align table-mode.txt /*table-mode-options-align*
|
||||
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*
|
||||
|
||||
Reference in New Issue
Block a user