Compare commits

...

19 Commits

Author SHA1 Message Date
Dhruva Sagar
10ab0425f6 Fixes #17
Added a new configuration option g:table_mode_corner_corner to
correspond to the extreme corners of the table border, by default it is
set to '|' to maintain backward compatibility but it can be overriden to
anything else.
2014-01-29 11:32:30 +05:30
Dhruva Sagar
e10a2ab4da Updated README 2013-12-31 12:53:41 +05:30
Dhruva Sagar
496872f342 trying with <kbd> 2013-12-31 12:51:35 +05:30
Dhruva Sagar
af432f6ed5 Updated CHANGELOG 2013-12-31 12:34:42 +05:30
Dhruva Sagar
5c42d58d4b Updated table mode
- Bumped version
- Added new mapping \t? to echo current cells representation for
  defining formulas.
- Updated docs
2013-12-31 12:30:52 +05:30
Dhruva Sagar
1302782b55 Updated vim-flavor & tests 2013-12-31 12:22:22 +05:30
Dhruva Sagar
9935e57def Fixed motions 2013-12-11 16:15:07 +05:30
Dhruva Sagar
8993e2a454 Fixed tablemode#DeleteColumn 2013-12-11 15:50:32 +05:30
Dhruva Sagar
df41b7deff Fixed Travis Url in README 2013-12-11 15:22:52 +05:30
Dhruva Sagar
c88a913397 Fixed tests 2013-12-11 15:21:20 +05:30
Dhruva Sagar
8609ad4778 Releasing version 3.3.1
* Borders are now disregarded, so add as many as you'd like.
* Improved test coverage.
2013-12-11 12:48:25 +05:30
Dhruva Sagar
535dd9601a Fixed doc/table-mode.txt mixed indent 2013-11-13 08:49:40 +05:30
Dhruva Sagar
e94bbcc0b4 Switched to using || mapping for table border generation, more intuitive & create a minimal border on a new line without a table 2013-11-13 08:45:15 +05:30
Dhruva Sagar
a0618db019 Fixed #14, You can now have a top border (before header) and a bottom border as well (after last line) 2013-11-12 19:49:10 +05:30
Dhruva Sagar
176999f347 Allowing border creation above table row 2013-10-24 16:47:15 +05:30
Dhruva Sagar
5a6a9b3473 Fixed motions to wrap around edges incorporating table header 2013-10-07 15:07:55 +05:30
Dhruva Sagar
aa8817621c Fixed doc #11 2013-09-25 09:29:37 +05:30
Dhruva Sagar
54187d0be1 Updated Header Border to be like org-mode table header separator for consistency. 2013-09-19 16:53:28 +05:30
Dhruva Sagar
3e2eaeee5c Fixed delete column api with header changes 2013-09-19 15:03:13 +05:30
7 changed files with 297 additions and 118 deletions

View File

@@ -1,4 +1,19 @@
# Change Log # Change Log
## Version 3.3.2
* Added new mapping \t? to echo a cells representation for use while defining
formulas.
## Version 3.3.1
* Improved logic to ignore table borders (add as many as you'd like), the
first row is not treated special, it is row # 1. Keep that in mind while
defining Formulas
* Improved test coverage
## Version 3.3
* Dropped +- mapping to create table border instead now using ||
* You can now have a top table border (before header) as well as a bottom
table border.
## Version 3.2 ## Version 3.2
* Added tests to test various use cases using <a * Added tests to test various use cases using <a
href='https://github.com/kana/vim-vspec'>Vspec</a>.. href='https://github.com/kana/vim-vspec'>Vspec</a>..

View File

@@ -6,7 +6,7 @@ GEM
blankslate (~> 2.0) blankslate (~> 2.0)
rake (10.1.0) rake (10.1.0)
thor (0.18.1) thor (0.18.1)
vim-flavor (1.1.2) vim-flavor (1.1.3)
parslet (~> 1.0) parslet (~> 1.0)
thor (~> 0.14) thor (~> 0.14)

View File

@@ -1,4 +1,4 @@
# VIM Table Mode [![Build Status](https://travis-ci.org/dhruvasagar/soulmate_rails.png?branch=master)](https://travis-ci.org/dhruvasagar/soulmate_rails) # VIM Table Mode [![Build Status](https://travis-ci.org/dhruvasagar/vim-table-mode.png?branch=master)](https://travis-ci.org/dhruvasagar/vim-table-mode)
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.
@@ -34,7 +34,7 @@ $ git submodule add git@github.com:dhruvasagar/vim-table-mode.git bundle/table-m
- **On the fly table creation** : - **On the fly table creation** :
By default the table column separator is `|` defined by the By default the table column separator is <kbd>|</kbd> defined by the
`g:table_mode_separator` option. As soon as you type it on a new line (ignores `g:table_mode_separator` option. As soon as you type it on a new line (ignores
indentation) the script gets to work on creating a table around it. As you indentation) the script gets to work on creating a table around it. As you
type and define more columns, the table is completed, formatted and aligned type and define more columns, the table is completed, formatted and aligned
@@ -42,7 +42,7 @@ $ git submodule add git@github.com:dhruvasagar/vim-table-mode.git bundle/table-m
Since this could lead to unwanted behavior I have disabled table mode by Since this could lead to unwanted behavior I have disabled table mode by
default. You have to use `:TableModeToggle` command or the table mode default. You have to use `:TableModeToggle` command or the table mode
toggle mapping, which is `<Leader>tm` defined by `g:table_mode_toggle_map` toggle mapping, which is <kbd>\<Leader\>tm</kbd> defined by `g:table_mode_toggle_map`
option to toggle the table mode or you can directly use `:TableModeEnable` option to toggle the table mode or you can directly use `:TableModeEnable`
and `:TableModeDisable` to enable or disable the table mode. This is on a and `:TableModeDisable` to enable or disable the table mode. This is on a
per buffer basis and so it does not cause any unusual behavior unless it is per buffer basis and so it does not cause any unusual behavior unless it is
@@ -54,25 +54,25 @@ $ git submodule add git@github.com:dhruvasagar/vim-table-mode.git bundle/table-m
existing content into a table. And it does as promised. Like table creation existing content into a table. And it does as promised. Like table creation
on the fly as you type, formatting existing content into a table is equally on the fly as you type, formatting existing content into a table is equally
simple. You can visually select multiple lines and call `:Tableize` on it, simple. You can visually select multiple lines and call `:Tableize` on it,
or alternatively use the mapping `<Leader>tt` defined by the or alternatively use the mapping <kbd>\<Leader\>tt</kbd> defined by the
`g:table_mode_tableize_map` option which would convert CSV (Comma Separated `g:table_mode_tableize_map` option which would convert CSV (Comma Separated
Value) data into a table and use `,` defined by `g:table_mode_delimiter` Value) data into a table and use `,` defined by `g:table_mode_delimiter`
option as the delimiter. option as the delimiter.
If however you wish to use a different delimiter, you can use the command If however you wish to use a different delimiter, you can use the command
`:Tableize/{pattern}` in a similar fashion as you use tabular (eg. `:Tableize/{pattern}` in a similar fashion as you use tabular (eg.
`:Tableize/;` uses ';' as the delimiter) or use the mapping `<Leader>T` `:Tableize/;` uses ';' as the delimiter) or use the mapping <kbd>\<Leader\>T</kbd>
defined by `g:table_mode_tableize_op_map` option which takes input in the defined by `g:table_mode_tableize_op_map` option which takes input in the
cmd-line and uses the `{pattern}` input as the delimiter. cmd-line and uses the `{pattern}` input as the delimiter.
`:Tableize` also accepts a range and so you can also call it by giving `:Tableize` also accepts a range and so you can also call it by giving
lines manually like `:line1,line2Tableize`, but this is not very intuitive. lines manually like `:line1,line2Tableize`, but this is not very intuitive.
You can use the mapping `<Leader>T` with a `[count]` to apply it to the You can use the mapping <kbd>\<Leader\>T</kbd> with a `[count]` to apply it to the
next `[count]` lines in standard vim style. next `[count]` lines in standard vim style.
- **Move between cells** : - **Move between cells** :
Now you can move between cells using table mode motions `<Leader>t[hjkl]` Now you can move between cells using table mode motions <kbd>\<Leader\>t[hjkl]</kbd>
to move left | down | up | right cells respectively. You can use to move left | down | up | right cells respectively. You can use
`g:table_mode_map_prefix` option to define the prefix mapping to be used `g:table_mode_map_prefix` option to define the prefix mapping to be used
before 'hjkl'. The left | right motions wrap around the table and move to before 'hjkl'. The left | right motions wrap around the table and move to
@@ -84,7 +84,7 @@ $ git submodule add git@github.com:dhruvasagar/vim-table-mode.git bundle/table-m
- **Cell Text Object** : - **Cell Text Object** :
Tableize provides a text object for manipulating table cells. By default Tableize provides a text object for manipulating table cells. By default
it's `tc`, but that can be changed if you wish to by setting the it's <kbd>tc</kbd>, but that can be changed if you wish to by setting the
`g:table_mode_cell_text_object` option. `g:table_mode_cell_text_object` option.
This is intelligent in the sense that if you use it to delete the This is intelligent in the sense that if you use it to delete the
@@ -98,14 +98,14 @@ $ git submodule add git@github.com:dhruvasagar/vim-table-mode.git bundle/table-m
- **Delete Row** : - **Delete Row** :
You can use the `<Leader>tdd` mapping defined by the option You can use the <kbd>\<Leader\>tdd</kbd> mapping defined by the option
`g:table_mode_delete_row_map` to delete the current table row (provided `g:table_mode_delete_row_map` to delete the current table row (provided
you are within a table row), this can be preceeded with a [count] to you are within a table row), this can be preceeded with a [count] to
delete multiple rows just like you would with 'dd'. delete multiple rows just like you would with 'dd'.
- **Delete Column** : - **Delete Column** :
You can use the `<Leader>tdc` mapping defined by the option You can use the <kbd>\<Leader\>tdc</kbd> mapping defined by the option
`g:table_mode_delete_column_map` to delete the entire current column `g:table_mode_delete_column_map` to delete the entire current column
(provided you are within a table row), this can also be preceeded with a (provided you are within a table row), this can also be preceeded with a
[count] to delete multiple columns. [count] to delete multiple columns.
@@ -115,7 +115,7 @@ $ git submodule add git@github.com:dhruvasagar/vim-table-mode.git bundle/table-m
Table Mode now has support for formulas like a spreadsheet. There are 2 ways Table Mode now has support for formulas like a spreadsheet. There are 2 ways
of defining formulas : of defining formulas :
- You can add formulas using `:TableAddFormula` or the mapping `<Leader>tfa` - You can add formulas using `:TableAddFormula` or the mapping <kbd>\<Leader\>tfa</kbd>
defined by the option `g:table_mode_add_formula_map` from within a table defined by the option `g:table_mode_add_formula_map` from within a table
cell, which will ask for input on the cmd-line with a `f=` prompt. The cell, which will ask for input on the cmd-line with a `f=` prompt. The
input formula will be appended to the formula line if one exists or a new input formula will be appended to the formula line if one exists or a new
@@ -129,9 +129,11 @@ $ git submodule add git@github.com:dhruvasagar/vim-table-mode.git bundle/table-m
formulas on the line separated with a ';' eg) `# tmf: $3=$2*$1;$4=$3/3.14` formulas on the line separated with a ';' eg) `# tmf: $3=$2*$1;$4=$3/3.14`
You can evaluate the formula line using `:TableEvalFormulaLine` or the You can evaluate the formula line using `:TableEvalFormulaLine` or the
mapping `<Leader>tfe` defined by the option `g:table_mode_eval_expr_map` mapping <kbd>\<Leader\>tfe</kbd> defined by the option `g:table_mode_eval_expr_map`
from anywhere inside the table or while on the formula line. from anywhere inside the table or while on the formula line.
NOTE: You can now use the mapping <kbd>\<Leader\>t?</kbd>
- **Formula Expressions** : - **Formula Expressions** :
Expressions are of the format `$target = formula`. Expressions are of the format `$target = formula`.

View File

@@ -4,10 +4,9 @@
" Author: Dhruva Sagar <http://dhruvasagar.com/> " Author: Dhruva Sagar <http://dhruvasagar.com/>
" License: MIT (http://www.opensource.org/licenses/MIT) " License: MIT (http://www.opensource.org/licenses/MIT)
" Website: http://github.com/dhruvasagar/vim-table-mode " Website: http://github.com/dhruvasagar/vim-table-mode
" Version: 3.2 " Version: 3.3.3
" Note: This plugin was heavily inspired by the 'CucumberTables.vim' " Note: This plugin was heavily inspired by the 'CucumberTables.vim'
" (https://gist.github.com/tpope/287147) plugin by Tim Pope and " (https://gist.github.com/tpope/287147) plugin by Tim Pope.
" uses a small amount of code from it.
" "
" Copyright Notice: " Copyright Notice:
" Permission is hereby granted to use and distribute this code, " Permission is hereby granted to use and distribute this code,
@@ -67,11 +66,11 @@ function! s:Sum(list) "{{{2
let result = 0.0 let result = 0.0
for item in a:list for item in a:list
if type(item) == type(1) || type(item) == type(1.0) if type(item) == type(1) || type(item) == type(1.0)
let result = result + item let result += item
elseif type(item) == type('') elseif type(item) == type('')
let result = result + str2float(item) let result += str2float(item)
elseif type(item) == type([]) elseif type(item) == type([])
let result = result + s:Sum(item) let result += s:Sum(item)
endif endif
endfor endfor
return result return result
@@ -124,7 +123,11 @@ function! s:EndExpr() "{{{2
endfunction endfunction
function! s:HeaderBorderExpr() "{{{2 function! s:HeaderBorderExpr() "{{{2
return s:StartExpr() . g:table_mode_corner . '[' . g:table_mode_fillchar . g:table_mode_corner . ']*' . g:table_mode_corner . s:EndExpr() return s:StartExpr() .
\ '[' . g:table_mode_corner . g:table_mode_corner_corner . ']' .
\ '[' . g:table_mode_fillchar . g:table_mode_corner . ']*' .
\ '[' . g:table_mode_corner . g:table_mode_corner_corner . ']' .
\ s:EndExpr()
endfunction endfunction
function! s:StartCommentExpr() "{{{2 function! s:StartCommentExpr() "{{{2
@@ -165,13 +168,8 @@ function! s:ToggleMapping() "{{{2
execute "inoremap <silent> <buffer> " . b:table_mode_separator_map . ' ' . execute "inoremap <silent> <buffer> " . b:table_mode_separator_map . ' ' .
\ b:table_mode_separator_map . "<Esc>:call tablemode#TableizeInsertMode()<CR>a" \ b:table_mode_separator_map . "<Esc>:call tablemode#TableizeInsertMode()<CR>a"
execute "inoreabbrev <silent> <buffer> " . g:table_mode_corner .
\ g:table_mode_fillchar . " <Esc>:call tablemode#AddHeaderBorder('.')<CR>A"
else else
execute "iunmap <silent> <buffer> " . b:table_mode_separator_map execute "iunmap <silent> <buffer> " . b:table_mode_separator_map
execute "iunabbrev <silent> <buffer> " . g:table_mode_corner . g:table_mode_fillchar
endif endif
endfunction endfunction
@@ -180,12 +178,28 @@ function! s:SetActive(bool) "{{{2
call s:ToggleMapping() call s:ToggleMapping()
endfunction endfunction
function! s:DefaultHeaderBorder() "{{{2
if s:IsTableModeActive()
return g:table_mode_corner_corner . g:table_mode_fillchar . g:table_mode_corner . g:table_mode_fillchar . g:table_mode_corner_corner
else
return ''
endif
endfunction
function! s:GenerateHeaderBorder(line) "{{{2 function! s:GenerateHeaderBorder(line) "{{{2
let line = s:Line(a:line) let line = s:Line(a:line)
if tablemode#IsATableRow(line - s:RowGap()) if tablemode#IsATableRow(line - s:RowGap()) || tablemode#IsATableRow(line + s:RowGap())
let line_val = ''
if tablemode#IsATableRow(line + s:RowGap())
let line_val = getline(line + s:RowGap())
endif
if tablemode#IsATableRow(line - s:RowGap()) && s:Strlen(line_val) < s:Strlen(getline(line - s:RowGap()))
let line_val = getline(line - s:RowGap()) let line_val = getline(line - s:RowGap())
endif
if s:Strlen(line_val) <= 1 | return s:DefaultHeaderBorder() | endif
let border = substitute(line_val[stridx(line_val, g:table_mode_separator):strridx(line_val, g:table_mode_separator)], g:table_mode_separator, g:table_mode_corner, 'g') let border = substitute(line_val[stridx(line_val, g:table_mode_separator):strridx(line_val, g:table_mode_separator)], g:table_mode_separator, g:table_mode_corner, 'g')
let border = substitute(border, '[^' . g:table_mode_corner . ']', g:table_mode_fillchar, 'g') let border = substitute(border, '[^' . g:table_mode_corner . ']', g:table_mode_fillchar, 'g')
let border = substitute(border, '^' . g:table_mode_corner . '\(.*\)' . g:table_mode_corner . '$', g:table_mode_corner_corner . '\1' . g:table_mode_corner_corner, '')
let cstartexpr = s:StartCommentExpr() let cstartexpr = s:StartCommentExpr()
if s:Strlen(cstartexpr) > 0 && getline(line) =~# cstartexpr if s:Strlen(cstartexpr) > 0 && getline(line) =~# cstartexpr
@@ -198,6 +212,8 @@ function! s:GenerateHeaderBorder(line) "{{{2
else else
return border return border
endif endif
else
return s:DefaultHeaderBorder()
endif endif
endfunction endfunction
@@ -240,9 +256,10 @@ function! s:GetFirstRow(line) "{{{2
if tablemode#IsATableRow(a:line) if tablemode#IsATableRow(a:line)
let line = s:Line(a:line) let line = s:Line(a:line)
while tablemode#IsATableRow(line - s:RowGap()) while tablemode#IsATableRow(line - s:RowGap()) || tablemode#IsATableHeader(line - s:RowGap())
let line = line - s:RowGap() let line -= s:RowGap()
endwhile endwhile
if tablemode#IsATableHeader(line) | let line += s:RowGap() | endif
return line return line
endif endif
@@ -258,9 +275,10 @@ function! s:GetLastRow(line) "{{{2
if tablemode#IsATableRow(a:line) if tablemode#IsATableRow(a:line)
let line = s:Line(a:line) let line = s:Line(a:line)
while tablemode#IsATableRow(line+ s:RowGap()) while tablemode#IsATableRow(line + s:RowGap()) || tablemode#IsATableHeader(line + s:RowGap())
let line = line + s:RowGap() let line += s:RowGap()
endwhile endwhile
if tablemode#IsATableHeader(line) | let line -= s:RowGap() | endif
return line return line
endif endif
@@ -272,6 +290,23 @@ function! s:MoveToLastRow() "{{{2
endif endif
endfunction endfunction
function! s:LineNr(row) "{{{2
if tablemode#IsATableRow('.')
let line = s:GetFirstRow('.')
let row_nr = 0
while tablemode#IsATableRow(line + s:RowGap()) || tablemode#IsATableHeader(line + s:RowGap())
if tablemode#IsATableRow(line)
let row_nr += 1
if row ==# row_nr | break | endif
endif
let line += s:RowGap()
endwhile
return line
endif
endfunction
function! s:MoveToStartOfCell() "{{{2 function! s:MoveToStartOfCell() "{{{2
if getline('.')[col('.')-1] ==# g:table_mode_separator && !s:IsLastCell() if getline('.')[col('.')-1] ==# g:table_mode_separator && !s:IsLastCell()
normal! 2l normal! 2l
@@ -296,21 +331,28 @@ function! s:GetCells(line, ...) abort
let [row, colm] = a:000 let [row, colm] = a:000
endif endif
let first_row = s:GetFirstRow(line)
if row == 0 if row == 0
let values = [] let values = []
let line = s:GetFirstRow(line) let line = first_row
while tablemode#IsATableRow(line) while tablemode#IsATableRow(line) || tablemode#IsATableHeader(line)
if tablemode#IsATableRow(line)
let row_line = getline(line)[stridx(getline(line), g:table_mode_separator):strridx(getline(line), g:table_mode_separator)] let row_line = getline(line)[stridx(getline(line), g:table_mode_separator):strridx(getline(line), g:table_mode_separator)]
call add(values, s:Strip(get(split(row_line, g:table_mode_separator), colm>0?colm-1:colm, ''))) call add(values, s:Strip(get(split(row_line, g:table_mode_separator), colm>0?colm-1:colm, '')))
let line = line + s:RowGap() endif
let line += s:RowGap()
endwhile endwhile
return values return values
else else
if row > 0 let row_nr = 0
let line = line + (row - tablemode#RowNr(line)) * s:RowGap() let line = first_row
else while tablemode#IsATableRow(line) || tablemode#IsATableHeader(line)
let line = line + row * s:RowGap() if tablemode#IsATableRow(line)
let row_nr += 1
if row ==# row_nr | break | endif
endif endif
let line += s:RowGap()
endwhile
let row_line = getline(line)[stridx(getline(line), g:table_mode_separator):strridx(getline(line), g:table_mode_separator)] let row_line = getline(line)[stridx(getline(line), g:table_mode_separator):strridx(getline(line), g:table_mode_separator)]
if colm == 0 if colm == 0
@@ -568,8 +610,6 @@ function! s:Align(lines) "{{{3
return lines return lines
endfunction endfunction
" Public API {{{1 " Public API {{{1
function! tablemode#sid() "{{{2 function! tablemode#sid() "{{{2
@@ -590,7 +630,10 @@ function! tablemode#GetFirstRow(line) "{{{2
endfunction endfunction
function! tablemode#TableizeInsertMode() "{{{2 function! tablemode#TableizeInsertMode() "{{{2
if s:IsTableModeActive() && getline('.') =~# (s:StartExpr() . g:table_mode_separator) if s:IsTableModeActive() && getline('.') =~# (s:StartExpr() . g:table_mode_separator . g:table_mode_separator)
call tablemode#AddHeaderBorder('.')
normal! A
elseif s:IsTableModeActive() && getline('.') =~# (s:StartExpr() . g:table_mode_separator)
let column = s:Strlen(substitute(getline('.')[0:col('.')], '[^' . g:table_mode_separator . ']', '', 'g')) 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.*')) let position = s:Strlen(matchstr(getline('.')[0:col('.')], '.*' . g:table_mode_separator . '\s*\zs.*'))
call tablemode#TableRealign('.') call tablemode#TableRealign('.')
@@ -621,7 +664,7 @@ function! tablemode#TableizeRange(...) range "{{{2
while lnum < (a:firstline + (a:lastline - a:firstline + 1)*s:RowGap()) while lnum < (a:firstline + (a:lastline - a:firstline + 1)*s:RowGap())
call s:Tableizeline(lnum, a:1) call s:Tableizeline(lnum, a:1)
undojoin undojoin
let lnum = lnum + s:RowGap() let lnum += s:RowGap()
endwhile endwhile
call tablemode#TableRealign(lnum - s:RowGap()) call tablemode#TableRealign(lnum - s:RowGap())
@@ -644,34 +687,29 @@ function! tablemode#TableRealign(line) "{{{2
let line = s:Line(a:line) let line = s:Line(a:line)
let [lnums, lines] = [[], []] let [lnums, lines] = [[], []]
let [bline, tline] = [0, line] let [tline, blines] = [line, []]
while tablemode#IsATableRow(tline) while tablemode#IsATableRow(tline) || tablemode#IsATableHeader(tline)
call insert(lnums, tline) if tablemode#IsATableHeader(tline)
call insert(lines, getline(tline)) call insert(blines, tline)
let tline = tline - s:RowGap() let tline -= s:RowGap()
endwhile continue
" If we reached header walking upwards
if getline(tline) =~# s:HeaderBorderExpr() && tablemode#IsATableRow(tline - s:RowGap())
let bline = tline
let tline = tline - s:RowGap()
" Insert the header line
call insert(lnums, tline)
call insert(lines, getline(tline))
endif endif
call insert(lnums, tline)
call insert(lines, getline(tline))
let tline -= s:RowGap()
endwhile
let tline = line + s:RowGap() let tline = line + s:RowGap()
" If we start at header while tablemode#IsATableRow(tline) || tablemode#IsATableHeader(tline)
if !bline && getline(tline) =~# s:HeaderBorderExpr() if tablemode#IsATableHeader(tline)
let bline = tline call insert(blines, tline)
let tline = tline + s:RowGap() let tline += s:RowGap()
continue
endif endif
while tablemode#IsATableRow(tline)
call add(lnums, tline) call add(lnums, tline)
call add(lines, getline(tline)) call add(lines, getline(tline))
let tline = tline + s:RowGap() let tline += s:RowGap()
endwhile endwhile
let lines = s:Align(lines) let lines = s:Align(lines)
@@ -681,40 +719,37 @@ function! tablemode#TableRealign(line) "{{{2
call setline(lnum, lines[index]) call setline(lnum, lines[index])
endfor endfor
if bline for bline in blines
call tablemode#AddHeaderBorder(bline) call tablemode#AddHeaderBorder(bline)
endif endfor
endfunction endfunction
function! tablemode#IsATableRow(line) "{{{2 function! tablemode#IsATableRow(line) "{{{2
return getline(a:line) =~# (s:StartExpr() . g:table_mode_separator) return getline(a:line) =~# (s:StartExpr() . g:table_mode_separator . '[^' .
\ g:table_mode_fillchar . ']*[^' . g:table_mode_corner . ']*$')
endfunction endfunction
function! tablemode#IsATableHeader(line) "{{{2
return getline(a:line) =~# s:HeaderBorderExpr()
endfunction
function! tablemode#LineNr(row) "{{{2 function! tablemode#LineNr(row) "{{{2
if tablemode#IsATableRow('.') return s:LineNr(row)
let line = s:Line('.')
let row = tablemode#RowNr('.')
if a:row != row
let line += a:row - row
endif
return line
endif
endfunction endfunction
function! tablemode#RowCount(line) "{{{2 function! tablemode#RowCount(line) "{{{2
let line = s:Line(a:line) let line = s:Line(a:line)
let [tline, totalRowCount] = [line, 0] let [tline, totalRowCount] = [line, 0]
while tablemode#IsATableRow(tline) while tablemode#IsATableRow(tline) || tablemode#IsATableHeader(tline)
let totalRowCount += 1 if tablemode#IsATableRow(tline) | let totalRowCount += 1 | endif
let tline = tline - s:RowGap() let tline -= s:RowGap()
endwhile endwhile
let tline = line + s:RowGap() let tline = line + s:RowGap()
while tablemode#IsATableRow(tline) while tablemode#IsATableRow(tline) || tablemode#IsATableHeader(tline)
let totalRowCount += 1 if tablemode#IsATableRow(tline) | let totalRowCount += 1 | endif
let tline = tline + s:RowGap() let tline += s:RowGap()
endwhile endwhile
return totalRowCount return totalRowCount
@@ -724,9 +759,9 @@ function! tablemode#RowNr(line) "{{{2
let line = s:Line(a:line) let line = s:Line(a:line)
let rowNr = 0 let rowNr = 0
while tablemode#IsATableRow(line) while tablemode#IsATableRow(line) || tablemode#IsATableHeader(line)
let rowNr += 1 if tablemode#IsATableRow(line) | let rowNr += 1 | endif
let line = line - s:RowGap() let line -= s:RowGap()
endwhile endwhile
return rowNr return rowNr
@@ -755,7 +790,9 @@ function! tablemode#TableMotion(direction) "{{{2
if tablemode#IsATableRow('.') if tablemode#IsATableRow('.')
if a:direction ==# 'l' if a:direction ==# 'l'
if s:IsLastCell() if s:IsLastCell()
if !tablemode#IsATableRow(line('.') + s:RowGap()) | return | endif if !tablemode#IsATableRow(line('.') + s:RowGap()) || (tablemode#IsATableHeader(line('.') + s:RowGap()) && tablemode#IsATableRow(line('.') + 2 * s:RowGap()))
return
endif
call tablemode#TableMotion('j') call tablemode#TableMotion('j')
normal! 0 normal! 0
endif endif
@@ -768,7 +805,9 @@ function! tablemode#TableMotion(direction) "{{{2
endif endif
elseif a:direction ==# 'h' elseif a:direction ==# 'h'
if s:IsFirstCell() if s:IsFirstCell()
if !tablemode#IsATableRow(line('.') - s:RowGap()) | return | endif if !tablemode#IsATableRow(line('.') - s:RowGap()) || (tablemode#IsATableHeader(line('.') - s:RowGap()) && tablemode#IsATableRow(line('.') - 2 * s:RowGap()))
return
endif
call tablemode#TableMotion('k') call tablemode#TableMotion('k')
normal! $ normal! $
endif endif
@@ -780,9 +819,17 @@ function! tablemode#TableMotion(direction) "{{{2
execute 'normal! 2F' . g:table_mode_separator . '2l' execute 'normal! 2F' . g:table_mode_separator . '2l'
endif endif
elseif a:direction ==# 'j' elseif a:direction ==# 'j'
if tablemode#IsATableRow(line('.') + s:RowGap()) | execute 'normal ' . s:RowGap() . 'j' | endif if tablemode#IsATableRow(line('.') + s:RowGap())
execute 'normal! ' . s:RowGap() . 'j'
elseif tablemode#IsATableHeader(line('.') + s:RowGap()) && tablemode#IsATableRow(line('.') + 2 * s:RowGap())
execute 'normal! ' . (s:RowGap() + 1) . 'j'
endif
elseif a:direction ==# 'k' elseif a:direction ==# 'k'
if tablemode#IsATableRow(line('.') - s:RowGap()) | execute 'normal ' . s:RowGap() . 'k' | endif if tablemode#IsATableRow(line('.') - s:RowGap())
execute 'normal! ' . s:RowGap() . 'k'
elseif tablemode#IsATableHeader(line('.') - s:RowGap()) && tablemode#IsATableRow(line('.') - 2 * s:RowGap())
execute 'normal! ' . (s:RowGap() + 1) . 'k'
endif
endif endif
endif endif
endfunction endfunction
@@ -803,8 +850,8 @@ endfunction
function! tablemode#DeleteColumn() "{{{2 function! tablemode#DeleteColumn() "{{{2
if tablemode#IsATableRow('.') if tablemode#IsATableRow('.')
for i in range(v:count1) for i in range(v:count1)
call s:MoveToFirstRow()
call s:MoveToStartOfCell() call s:MoveToStartOfCell()
call s:MoveToFirstRow()
silent! execute "normal! h\<C-V>f" . g:table_mode_separator silent! execute "normal! h\<C-V>f" . g:table_mode_separator
call s:MoveToLastRow() call s:MoveToLastRow()
normal! d normal! d
@@ -817,15 +864,11 @@ endfunction
function! tablemode#DeleteRow() "{{{2 function! tablemode#DeleteRow() "{{{2
if tablemode#IsATableRow('.') if tablemode#IsATableRow('.')
for i in range(v:count1) for i in range(v:count1)
if tablemode#RowCount('.') ==# 1 if tablemode#IsATableRow('.')
normal! kVjjd normal! dd
else
normal! kVjd
endif endif
if tablemode#IsATableRow(line('.')+1) if !tablemode#IsATableRow('.')
normal! j
else
normal! k normal! k
endif endif
endfor endfor
@@ -874,6 +917,7 @@ function! tablemode#AddFormula() "{{{2
if fr !=# '' if fr !=# ''
let fr = '$' . row . ',' . colm . '=' . fr let fr = '$' . row . ',' . colm . '=' . fr
let fline = tablemode#GetLastRow('.') + s:RowGap() let fline = tablemode#GetLastRow('.') + s:RowGap()
if tablemode#IsATableHeader(fline) | let fline += s:RowGap() | endif
let cursor_pos = [line('.'), col('.')] let cursor_pos = [line('.'), col('.')]
if getline(fline) =~# 'tmf: ' if getline(fline) =~# 'tmf: '
" Comment line correctly " Comment line correctly
@@ -962,11 +1006,14 @@ function! tablemode#EvaluateFormulaLine() abort "{{{2
endif endif
if tablemode#IsATableRow('.') " We're inside the table if tablemode#IsATableRow('.') " We're inside the table
let line = s:GetLastRow('.') let line = s:GetLastRow('.')
if getline(line + s:RowGap()) =~# 'tmf: ' let fline = line + s:RowGap()
let exprs = split(matchstr(getline(line + s:RowGap()), matchexpr), ';') if tablemode#IsATableHeader(fline) | let fline += s:RowGap() | endif
if getline(fline) =~# 'tmf: '
let exprs = split(matchstr(getline(fline), matchexpr), ';')
endif endif
elseif getline('.') =~# 'tmf: ' " We're on the formula line elseif getline('.') =~# 'tmf: ' " We're on the formula line
let line = line('.') - s:RowGap() let line = line('.') - s:RowGap()
if tablemode#IsATableHeader(line) | let line -= s:RowGap() | endif
if tablemode#IsATableRow(line) if tablemode#IsATableRow(line)
let exprs = split(matchstr(getline('.'), matchexpr), ';') let exprs = split(matchstr(getline('.'), matchexpr), ';')
endif endif

View File

@@ -1,7 +1,7 @@
*table-mode.txt* Table Mode for easy table formatting. v3.2 *table-mode.txt* Table Mode for easy table formatting. v3.3.3
=============================================================================== ===============================================================================
Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER
VERSION 3.2 VERSION 3.3.3
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/>
@@ -29,7 +29,7 @@ GETTING STARTED *table-mode-getting-started*
Create Table on the fly: Create Table on the fly:
Using Table Mode is dead simple. You simply start typing on a new line Using Table Mode is dead simple. You simply start typing on a new line
with the table separator - |g:table-mode-separator|, and you just type with the table separator - |table-mode-separator|, and you just type
away! The plugin does the rest automatically for you as you type. With away! The plugin does the rest automatically for you as you type. With
each additional separator you add, it aligns the table properly, each additional separator you add, it aligns the table properly,
without having to do anything else. without having to do anything else.
@@ -148,6 +148,8 @@ Overview:
current cell. current cell.
|table-mode-eval-expr-map| ...... Evaluate formula line and update |table-mode-eval-expr-map| ...... Evaluate formula line and update
table. table.
|table-mode-corner-corner| ...... Set the character to be used for
extreme corners of the table border.
g:loaded_table_mode *table-mode-loaded* g:loaded_table_mode *table-mode-loaded*
Use this option to disable the plugin: > Use this option to disable the plugin: >
@@ -272,6 +274,17 @@ g:table_mode_eval_expr_map *table-mode-eval-expr-map*
NOTE you will need to use the |table-mode-map-prefix| before this to NOTE you will need to use the |table-mode-map-prefix| before this to
get the desired effect. get the desired effect.
g:table_mode_echo_cell_map *table-mode-echo-cell-map*
Use this option to define the mapping for echoing current cells
representation for defining formulas. >
let g:table_mode_echo_cell_map = '?'
<
g:table_mode_corner_corner *table-mode-corner-corner*
Use this option to define the character to be used for the extreme
corners of the table border. >
let g:table_mode_corner_corner = '|'
<
=============================================================================== ===============================================================================
MAPPINGS *table-mode-mappings* MAPPINGS *table-mode-mappings*
@@ -300,6 +313,9 @@ MAPPINGS *table-mode-mappings*
*table-mode-mappings-realign* *table-mode-mappings-realign*
<Leader>tr Realigns table columns <Leader>tr Realigns table columns
<Leader>t? Echo the current table cells representation for defining
formulas.
*table-mode-mappings-motions* *table-mode-mappings-motions*
<Leader>t[hjkl] Move to previous | below | above | right cell in the table. <Leader>t[hjkl] Move to previous | below | above | right cell in the table.
@@ -323,11 +339,10 @@ MAPPINGS *table-mode-mappings*
would evaluate the formula line and update the table would evaluate the formula line and update the table
accordingly. This invokes the |TableEvalFormulaLine| command. accordingly. This invokes the |TableEvalFormulaLine| command.
+- Expands to a header border, the line immidiately above this || Expands to a header border. You can change this by changing
will be considered as the table header. You can change this by |table-mode-separator| option. You can change the character to
changing |table-mode-corner| and |table-mode-fillchar| be used for te extreme corners of the border by changing
options. This is an iabbrev so is triggered when you press |table-mode-corner-corner| option.
space or <CR> after typing the mapping.
=============================================================================== ===============================================================================
COMMANDS *table-mode-commands* COMMANDS *table-mode-commands*
@@ -399,4 +414,4 @@ REPORT ISSUES *table-mode-report-issues*
If you discover any issues, please report them at If you discover any issues, please report them at
http://github.com/dhruvasagar/table-mode/issues. http://github.com/dhruvasagar/table-mode/issues.
vim:tw=78:ts=8:ft=help:norl:ai vim:tw=78:ts=8:ft=help:norl:ai:et

View File

@@ -4,10 +4,9 @@
" Author: Dhruva Sagar <http://dhruvasagar.com/> " Author: Dhruva Sagar <http://dhruvasagar.com/>
" License: MIT (http://www.opensource.org/licenses/MIT) " License: MIT (http://www.opensource.org/licenses/MIT)
" Website: http://github.com/dhruvasagar/vim-table-mode " Website: http://github.com/dhruvasagar/vim-table-mode
" Version: 3.2 " Version: 3.3.3
" Note: This plugin was heavily inspired by the 'CucumberTables.vim' " Note: This plugin was heavily inspired by the 'CucumberTables.vim'
" (https://gist.github.com/tpope/287147) plugin by Tim Pope and " (https://gist.github.com/tpope/287147) plugin by Tim Pope.
" uses a small amount of code from it.
" "
" Copyright Notice: " Copyright Notice:
" Permission is hereby granted to use and distribute this code, " Permission is hereby granted to use and distribute this code,
@@ -51,6 +50,8 @@ call s:SetGlobalOptDefault('table_mode_delete_row_map', 'dd')
call s:SetGlobalOptDefault('table_mode_delete_column_map', 'dc') call s:SetGlobalOptDefault('table_mode_delete_column_map', 'dc')
call s:SetGlobalOptDefault('table_mode_add_formula_map', 'fa') call s:SetGlobalOptDefault('table_mode_add_formula_map', 'fa')
call s:SetGlobalOptDefault('table_mode_eval_expr_map', 'fe') call s:SetGlobalOptDefault('table_mode_eval_expr_map', 'fe')
call s:SetGlobalOptDefault('table_mode_echo_cell_map', '?')
call s:SetGlobalOptDefault('table_mode_corner_corner', '|')
function! s:TableMotion() "{{{1 function! s:TableMotion() "{{{1
let direction = nr2char(getchar()) let direction = nr2char(getchar())
@@ -59,6 +60,12 @@ function! s:TableMotion() "{{{1
endfor endfor
endfunction endfunction
function! s:TableEchoCell() "{{{1
if tablemode#IsATableRow('.')
echomsg '$' . tablemode#RowNr('.') . ',' . tablemode#ColumnNr('.')
endif
endfunction
" Define Commands & Mappings {{{1 " Define Commands & Mappings {{{1
if !g:table_mode_always_active "{{{2 if !g:table_mode_always_active "{{{2
exec "nnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_toggle_map . exec "nnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_toggle_map .
@@ -102,6 +109,8 @@ execute "nnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_add_formul
\ " :TableAddFormula<CR>" \ " :TableAddFormula<CR>"
execute "nnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_eval_expr_map . execute "nnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_eval_expr_map .
\ " :TableEvalFormulaLine<CR>" \ " :TableEvalFormulaLine<CR>"
execute "nnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_echo_cell_map .
\ " :call <SID>TableEchoCell()<CR>"
" Avoiding side effects {{{1 " Avoiding side effects {{{1
let &cpo = s:save_cpo let &cpo = s:save_cpo

View File

@@ -1,3 +1,4 @@
" vim: fdm=indent
let g:table_mode_corner = '+' let g:table_mode_corner = '+'
let g:table_mode_separator = '|' let g:table_mode_separator = '|'
let g:table_mode_fillchar = '-' let g:table_mode_fillchar = '-'
@@ -13,6 +14,7 @@ let g:table_mode_delete_row_map = 'dd'
let g:table_mode_delete_column_map = 'dc' let g:table_mode_delete_column_map = 'dc'
let g:table_mode_add_formula_map = 'fa' let g:table_mode_add_formula_map = 'fa'
let g:table_mode_eval_expr_map = 'fe' let g:table_mode_eval_expr_map = 'fe'
let g:table_mode_corner_corner = '|'
call vspec#hint({'scope': 'tablemode#scope()', 'sid': 'tablemode#sid()'}) call vspec#hint({'scope': 'tablemode#scope()', 'sid': 'tablemode#sid()'})
@@ -50,9 +52,10 @@ describe 'tablemode'
describe 'API' describe 'API'
before before
new
normal! ggdG normal! ggdG
call tablemode#TableModeEnable() call tablemode#TableModeEnable()
normal! i normal i
|test11|test12| |test11|test12|
|test21|test22| |test21|test22|
end end
@@ -129,8 +132,9 @@ describe 'tablemode'
end end
end end
describe 'Tableize'
before before
before new
normal! ggdG normal! ggdG
normal i normal i
asd,asd;asd,asd asd,asd;asd,asd
@@ -149,14 +153,101 @@ describe 'tablemode'
Expect tablemode#IsATableRow(2) to_be_true Expect tablemode#IsATableRow(2) to_be_true
Expect tablemode#RowCount(2) == 2 Expect tablemode#RowCount(2) == 2
Expect tablemode#ColumnCount(2) == 2 Expect tablemode#ColumnCount(2) == 2
end end
end end
describe 'Motions'
describe 'left or right'
before
new
normal! ggdG
call tablemode#TableModeEnable()
normal i|test11|test12|
|test21|test22|
call cursor(1, 3)
end
it 'should move left when not on first column'
call cursor(1, 12)
Expect tablemode#ColumnNr('.') == 2
call tablemode#TableMotion('h')
Expect tablemode#ColumnNr('.') == 1
end
it 'should move to the previous row last column if it exists when on first column'
call cursor(2, 3)
Expect tablemode#RowNr('.') == 2
Expect tablemode#ColumnNr('.') == 1
call tablemode#TableMotion('h')
Expect tablemode#RowNr('.') == 1
Expect tablemode#ColumnNr('.') == 2
end
it 'should move right when not on last column'
Expect tablemode#ColumnNr('.') == 1
call tablemode#TableMotion('l')
Expect tablemode#ColumnNr('.') == 2
end
it 'should move to the next row first column if it exists when on last column'
call cursor(1, 12)
Expect tablemode#RowNr('.') == 1
Expect tablemode#ColumnNr('.') == 2
call tablemode#TableMotion('l')
Expect tablemode#RowNr('.') == 2
Expect tablemode#ColumnNr('.') == 1
end
end
describe 'up or down'
before
new
normal! ggdG
call tablemode#TableModeEnable()
normal i|test11|test12|
|test21|test22|
call cursor(1, 3)
end
it 'should move a row up unless on first row'
call cursor(2, 3)
Expect tablemode#RowNr('.') == 2
call tablemode#TableMotion('k')
Expect tablemode#RowNr('.') == 1
end
it 'should remain on first row when trying to move up'
Expect tablemode#RowNr('.') == 1
call tablemode#TableMotion('k')
Expect tablemode#RowNr('.') == 1
end
it 'should move a row down unless on last row'
Expect tablemode#RowNr('.') == 1
call tablemode#TableMotion('j')
Expect tablemode#RowNr('.') == 2
end
it 'should remain on last row when trying to move down' it 'should remain on last row when trying to move down'
Expect tablemode#RowNr('.') == 1 Expect tablemode#RowNr('.') == 1
call tablemode#TableMotion('k') call tablemode#TableMotion('k')
end Expect tablemode#RowNr('.') == 1
end end
end
end
describe 'Manipulations'
before
new
normal! ggdG
normal i|test11|test12|
|test21|test22|
call cursor(1, 3)
end
it 'should delete a row successfully'
Expect tablemode#RowCount('.') == 2
call tablemode#DeleteRow()
Expect tablemode#RowCount('.') == 1 Expect tablemode#RowCount('.') == 1
end end