Compare commits

...

9 Commits
v2.2 ... v2.3.0

Author SHA1 Message Date
Dhruva Sagar
272bef4ce0 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
2013-05-04 03:23:20 +05:30
Dhruva Sagar
7c4039e5e1 Updated Table Mode Public API
- Added tablemode#RowCount(line). Returns the total number of rows in a
  table given a line (row).
- Added tablemode#ColumnCount(line). Returns the total number of columns
  in a line (row).
- Added tablemode#RowNr(line). Returns the row number on given line.
- Added tablemode#ColumnNr(line). Returns the column on given line.
2013-05-03 16:22:20 +05:30
Dhruva Sagar
28b13a32ab Fixed #6
- Realign now updates table borders as well.
2013-05-01 12:08:39 +05:30
Dhruva Sagar
599a39c28d Updated documentation 2013-05-01 02:30:24 +05:30
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
Dhruva Sagar
8489ca0c3b Refactored for table within inline comments 2013-04-08 21:40:02 +05:30
Dhruva Sagar
a5e2584347 Releasing v2.2.1
- Fixes #5. We now have the ability to invoke table mode within inline
  comments. Works for both instant table creation and table conversion
  for csv data. Uses 'commentstring' option of vim to identify comments,
  so it should work for most filetypes as long as 'commentstring' option
  has been set. This is usually done appropriately in filetype plugins.
2013-04-08 19:52:37 +05:30
Dhruva Sagar
c6609fd67d Moved Change Log to CHANGELOG.md 2013-03-28 18:02:14 +05:30
5 changed files with 354 additions and 76 deletions

40
CHANGELOG.md Normal file
View File

@@ -0,0 +1,40 @@
# Change Log
## Version 2.2.2
* Added mapping for realigning table columns.
* Added table motions to move around in the table.
## Version 2.2.1
* Added feature to allow Table-Mode to work within comments. Uses
'commentstring' option of vim to identify comments, so it should work for
most filetypes as long as 'commentstring' option has been set. This is
usually done appropriately in filetype plugins.
## Version 2.2
* Improved :Tableize to now accept a {pattern} just like :Tabular to match the
delimiter.
## Version 2.1.3 :
* Bug Fix #1, added new option `g:table_mode_no_border_padding` which removes
padding from the border.
## 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 :
* Added Tableize command and mapping to convert existing content into a table.
## Version 1.0 :
* First stable release, create tables as you type.

View File

@@ -4,35 +4,9 @@ An awesome automatic table creator & formatter allowing one to create neat
tables as you type.
## Change Log
### Version 2.2
* Improved :Tableize to now accept a {pattern} just like :Tabular to match the
delimiter.
### Version 2.1.3 :
* Bug Fix #1, added new option `g:table_mode_no_border_padding` which removes
padding from the border.
### 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 :
* Added Tableize command and mapping to convert existing content into a table.
### Version 1.0 :
* First stable release, create tables as you type.
See <a
href="https://github.com/dhruvasagar/vim-table-mode/blob/master/CHANGELOG.md">
CHANGELOG.md </a>
## Getting Started
### Installation
@@ -84,6 +58,10 @@ sure Tabular is installed and loaded into your runtime to ensure this works.
`:line1,line2Tableize`, but this is not that intuitive. You can also use
the mapping `<Leader>T` with a `[count]` to apply it to the next `[count]`
lines in usual vim style.
- Move between cells :
Now you can move between cells using table mode motions. Check `:h
table-mode-motion-prefix` for more details.
### Demo

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
" Version: 2.3.0
" 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.
@@ -56,9 +56,18 @@ call s:SetGlobalOptDefault('table_mode_delimiter', ',')
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 +89,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
" Version: 2.3.0
" 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.
@@ -35,8 +35,33 @@ function! s:Strlen(text)
endfunction
" }}}2
function! s:CountSeparator(line, separator) "{{{2
return s:Strlen(substitute(getline(a:line), '[^' . a:separator . ']', '', 'g'))
function! s:GetCommentStart() "{{{2
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
let cstart = s:GetCommentStart()
if s:Strlen(cstart) > 0
return '^\s*\(' . cstart . '\)\?\s*'
else
return '^\s*'
endif
endfunction
" }}}2
function! s:StartCommentExpr() "{{{2
let cstartexpr = s:GetCommentStart()
if s:Strlen(cstartexpr) > 0
return '^\s*' . cstartexpr . '\s*'
else
return ''
endif
endfunction
" }}}2
@@ -68,53 +93,67 @@ 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
silent! execute 'normal! kA' . repeat(g:table_mode_corner, curr_line_count - prev_line_count) . "\<Esc>j"
endif
function! s:GenerateBorder(line) "{{{2
let line = 0
if type(a:line) == type('')
let line = line(a:line)
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
let line = a:line
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
silent! execute 'normal! jA' . repeat(g:table_mode_corner, curr_line_count - next_line_count) . "\<Esc>k"
end
let border = substitute(getline(line)[stridx(getline(line), g:table_mode_separator):-1], g:table_mode_separator, g:table_mode_corner, 'g')
let border = substitute(border, '[^' . g:table_mode_corner . ']', g:table_mode_fillchar, 'g')
let cstartexpr = s:StartCommentExpr()
if s:Strlen(cstartexpr) > 0 && getline(line) =~# cstartexpr
let indent = matchstr(getline(line), s:StartCommentExpr())
return indent . border
elseif getline(line) =~# s:StartExpr()
let indent = matchstr(getline(line), s:StartExpr())
return indent . border
else
call append(cline, repeat(g:table_mode_corner, curr_line_count))
return border
endif
endfunction
" }}}2
function! s:FillTableBorder() "{{{2
let [ current_col, current_line ] = [ col('.'), line('.') ]
if g:table_mode_no_border_padding
silent! execute '%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'
function! s:UpdateLineBorder(line) "{{{2
let cline = a:line
let hf = s:StartExpr() . g:table_mode_corner . '[' . g:table_mode_corner .
\ g:table_mode_fillchar . ']*' . g:table_mode_corner . '\?\s*$'
let border = s:GenerateBorder(cline)
let [prev_line, next_line] = [getline(cline-1), getline(cline+1)]
if next_line =~# hf
if next_line !=# border
call setline(cline+1, border)
endif
else
silent! execute '%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 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
call cursor(current_line, current_col)
endfunction
" }}}2
function! s:ConvertDelimiterToSeparator(line, ...) "{{{2
let delim = g:table_mode_delimiter
if a:0 | let delim = a:1 | endif
silent! execute a:line . 's/^\s*\zs\ze.\|' . delim . '\|$/' .
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
@@ -123,7 +162,16 @@ 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
endfunction
" }}}2
function! s:IsFirstCell() "{{{2
return tablemode#TableColumnNr('.') ==# 1
endfunction
" }}}2
function! s:IsLastCell() "{{{2
return tablemode#TableColumnNr('.') ==# tablemode#ColumnCount('.')
endfunction
" }}}2
@@ -132,13 +180,10 @@ endfunction
" Public API {{{1
function! tablemode#TableizeInsertMode() "{{{2
if s:IsTableModeActive() && getline('.') =~# ('^\s*' . 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 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
if g:table_mode_border | call s:FillTableBorder() | endif
call tablemode#TableRealign('.')
normal! 0
call search(repeat('[^' . g:table_mode_separator . ']*' . g:table_mode_separator, column) . '\s\{-\}' . repeat('.', position), 'ce', line('.'))
endif
@@ -178,7 +223,8 @@ function! tablemode#TableizeRange(...) range "{{{2
undojoin
let lnum = lnum + shift
endwhile
if g:table_mode_border | call s:FillTableBorder() | endif
if g:table_mode_border | call tablemode#TableRealign(lnum - shift) | endif
endfunction
" }}}2
@@ -192,7 +238,179 @@ function! tablemode#TableizeByDelimiter() "{{{2
endfunction
" }}}2
" }}}1
function! tablemode#TableRealign(line) "{{{2
let line = 0
if type(a:line) == type('')
let line = line(a:line)
else
let line = a:line
endif
" ModeLine {{{
" vim:fdm=marker
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
" }}}2
function! tablemode#IsATableRow(line) "{{{2
return getline(a:line) =~# (s:StartExpr() . g:table_mode_separator)
endfunction
" }}}2
function! tablemode#RowCount(line) "{{{2
let line = 0
if type(a:line) == type('')
let line = line(a:line)
else
let line = a:line
endif
let rowCount = 1
if g:table_mode_border | let rowCount = 2 | endif
let [tline, totalRowCount] = [line, 0]
while tline > 0
if tablemode#IsATableRow(tline)
let totalRowCount = totalRowCount + 1
else
break
endif
let tline = tline - rowCount
endwhile
let tline = line + rowCount
while tline <= line('$')
if tablemode#IsATableRow(tline)
let totalRowCount = totalRowCount + 1
else
break
endif
let tline = tline + rowCount
endwhile
return totalRowCount
endfunction
" }}}2
function! tablemode#RowNr(line) "{{{2
let line = 0
if type(a:line) == type('')
let line = line(a:line)
else
let line = a:line
endif
let rowCount = 1
if g:table_mode_border | let rowCount = 2 | endif
let rowNr = 0
while line > 0
if tablemode#IsATableRow(line)
let rowNr = rowNr + 1
else
break
endif
let line = line - rowCount
endwhile
return rowNr
endfunction
" }}}2
function! tablemode#ColumnCount(line) "{{{2
let line = 0
if type(a:line) == type('')
let line = line(a:line)
else
let line = a:line
endif
return s:Strlen(substitute(getline(line), '[^' . g:table_mode_separator . ']', '', 'g'))-1
endfunction
" }}}2
function! tablemode#ColumnNr(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
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
*table-mode.txt* Table Mode for easy table formatting. v2.3.0
===============================================================================
Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER
VERSION 2.2
VERSION 2.3.0
Author: Dhruva Sagar <http://dhruvasagar.com/>
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
|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*
@@ -61,6 +67,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 +146,23 @@ 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]. The 'h', 'l' (left and right
motions) wrap around the table row and move to the previous rows last
column, next rows first column respectively if one exists.
===============================================================================
MAPPINGS *table-mode-mappings*
@@ -159,6 +184,11 @@ 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
*table-mode-mappings-motions*
<Leader>t[hjkl] Move to previous | below | above | right cell in the table.
===============================================================================
COMMANDS *table-mode-commands*