Compare commits

..

27 Commits

Author SHA1 Message Date
Dhruva Sagar
4c91a4efa2 Updated tests 2014-05-30 12:20:46 +05:30
Dhruva Sagar
8d9a0082f5 Updated docs 2014-05-30 12:20:38 +05:30
Dhruva Sagar
0f1254e1fe Updated table syntax to get toggled with table mode 2014-05-30 12:05:31 +05:30
Dhruva Sagar
e7e806916f Refactored toggleMapping 2014-05-30 11:49:13 +05:30
Dhruva Sagar
9336261063 Added cell text object for visual mode 2014-05-15 11:02:21 +05:30
Dhruva Sagar
a5ae0253be Added modeline to CHANGELOG 2014-05-13 11:21:11 +05:30
Dhruva Sagar
80ec86e385 Version 4.4.2
* Updated mappings to be buffer local
* Updated mappings to toggle with Table Mode
2014-05-13 11:18:42 +05:30
Dhruva Sagar
7eff1e30f1 Fixed #30
In case there is nothing to align, it was failing with an error, this
ensures it exists gracefully in this situation.
2014-05-10 15:27:36 +05:30
Dhruva Sagar
7258a56d20 Updated Table syntax to incorporate commented tables 2014-05-04 10:13:40 +05:30
Dhruva Sagar
8d3c4912a2 Added syntax for matching tables 2014-05-03 02:19:19 +05:30
Dhruva Sagar
13e1a20002 Fixing tests 2014-05-01 21:29:31 +05:30
Dhruva Sagar
d73236f964 Fixing ruby version 2014-05-01 21:21:31 +05:30
Dhruva Sagar
c5efbe1ad7 Bumped ruby version for travis 2014-05-01 21:16:56 +05:30
Dhruva Sagar
8970d5ffbb Improved tests 2014-05-01 21:14:49 +05:30
Dhruva Sagar
859eb42856 Added more tests for Realigning unicode content 2014-05-01 21:07:53 +05:30
Dhruva Sagar
d7ad97f099 Fixing tests 2014-05-01 16:56:11 +05:30
Dhruva Sagar
dbd79f2c1e Minor refactor & improved test coverage 2014-05-01 16:35:25 +05:30
Dhruva Sagar
fa568fe91b Improved test for utils 2014-04-30 23:05:19 +05:30
Dhruva Sagar
53da3fb4c2 Fixed #28
Altered border generation code to incorporate true display width of
characters, hence generate more accurate borders.
2014-04-30 21:30:44 +05:30
Dhruva Sagar
6007953f0e Fixed typo in docs 2014-04-28 14:14:26 +05:30
Dhruva Sagar
766066394c Updated docs 2014-04-28 13:48:17 +05:30
Dhruva Sagar
f2f62d78e5 Added support for negative indices
When defining formulas you can now use negative indices for target /
cells to refer to them relative to the last. -1 being the last and so
on.
2014-04-28 13:41:21 +05:30
Dhruva Sagar
7810ce0ca0 Fixed #26
Not assuming a length for alignments, looping through all and adding 'l'
as default first then updating as per the column alignments.
2014-04-26 23:52:18 +05:30
Dhruva Sagar
b22d793135 Improved fix for #20
Instead of relying on a /g or /gg flag based on gdefault, I temporarily
disable gdefault and set it back to the old value aftwards.
2014-04-25 18:18:19 +05:30
Dhruva Sagar
de2ad0b71d Fixed header creation as first line of table 2014-04-25 18:10:12 +05:30
Dhruva Sagar
29c7ec1a23 Cosmetic change
changed variable name from i to jdx since for consistency, i is short
for insert which is a vim ex command so to make things unambiguous.
2014-04-25 15:59:08 +05:30
Dhruva Sagar
9ce981cc77 Updated README.md 2014-04-25 15:05:19 +05:30
29 changed files with 566 additions and 235 deletions

View File

@@ -1,4 +1,4 @@
language: ruby language: ruby
rvm: rvm:
- 1.9.3 - 2.1
script: rake ci script: rake ci

View File

@@ -1,4 +1,21 @@
# Change Log # Change Log
## Version 4.5.0
* Refactored toggled mappings
* Table Syntax now gets toggled with Table Mode
## Version 4.4.2
* Updated mappings to be buffer local.
* Updated mappings to toggle and function only when table mode is active.
## Version 4.4.1
* Added syntax for matching tables
## Version 4.4.0
* Minor bug fixes
* Added feature to allow using negative indices within formulas to access rows,
columns relative to the last, -1 being the last.
## Version 4.3.0 ## Version 4.3.0
* Refactored some more * Refactored some more
* Fixed issue #19, hiphens in the table broke alignment * Fixed issue #19, hiphens in the table broke alignment
@@ -98,3 +115,7 @@
## Version 1.0 : ## Version 1.0 :
* First stable release, create tables as you type. * First stable release, create tables as you type.
<!--
vim: ft=markdown
-->

View File

@@ -1,4 +1,4 @@
# VIM Table Mode v4.3.0 [![Build Status](https://travis-ci.org/dhruvasagar/vim-table-mode.png?branch=master)](https://travis-ci.org/dhruvasagar/vim-table-mode) # VIM Table Mode v4.5.0 [![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.
@@ -48,6 +48,10 @@ $ git submodule add git@github.com:dhruvasagar/vim-table-mode.git bundle/table-m
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
enabled explicitly. Please read `:h table-mode` for further information. enabled explicitly. Please read `:h table-mode` for further information.
You can also define in a table header border how it's content should be
aligned, whether right or left by using a `:` character defined by
`g:table_mode_align_char` option.
- **Format existing content into a table** : - **Format existing content into a table** :
Table Mode wouldn't justify it's name if it didn't allow formatting Table Mode wouldn't justify it's name if it didn't allow formatting
@@ -133,11 +137,13 @@ $ git submodule add git@github.com:dhruvasagar/vim-table-mode.git bundle/table-m
- `$n`: This matches the table column number `n`. So the `formula` would - `$n`: This matches the table column number `n`. So the `formula` would
be evaluated for each cell in that column and the result would be placed be evaluated for each cell in that column and the result would be placed
in it. in it. You can use negative indice to represent column relative to the
last, -1 being the last.
- `$n,m`: This matches the table cell n,m (row, column). So in this case - `$n,m`: This matches the table cell n,m (row, column). So in this case
the formula would be evaluated and the result will be placed in this the formula would be evaluated and the result will be placed in this
cell. cell. You can also use negative values to refer to cells relative to
the size, -1 being the last (row or column).
- The `formula` can be a simple mathematical expression involving cells - The `formula` can be a simple mathematical expression involving cells
which are also defined by the same format as that of the target cell. You which are also defined by the same format as that of the target cell. You
@@ -145,7 +151,7 @@ $ git submodule add git@github.com:dhruvasagar/vim-table-mode.git bundle/table-m
mode also provides 2 special functions `Sum` and `Average`. Both these mode also provides 2 special functions `Sum` and `Average`. Both these
functions take a range as input. A range can be of two forms : functions take a range as input. A range can be of two forms :
- `r1,r2`: This represents cells in the current column from row `r1` - `r1:r2`: This represents cells in the current column from row `r1`
through `r2`. If `r2` is negative it represents `r2` rows above the through `r2`. If `r2` is negative it represents `r2` rows above the
current row (of the target cell). current row (of the target cell).

View File

@@ -32,32 +32,101 @@ function! s:SetBufferOptDefault(opt, val) "{{{2
endif endif
endfunction endfunction
function! s:ToggleMapping() "{{{2 function! s:Map(map, to, mode)
if exists('b:table_mode_active') && b:table_mode_active if !hasmapto(a:map, a:mode)
call s:SetBufferOptDefault('table_mode_separator_map', g:table_mode_separator) for l:mode in split(a:mode, '.\zs')
" '|' is a special character, we need to map <Bar> instead execute l:mode . 'map <buffer>' a:to a:map
if g:table_mode_separator ==# '|' | let b:table_mode_separator_map = '<Bar>' | endif endfor
endif
endfunction
execute "inoremap <silent> <buffer> " . b:table_mode_separator_map . ' ' . function! s:UnMap(map, mode)
\ b:table_mode_separator_map . "<Esc>:call tablemode#TableizeInsertMode()<CR>a" if !empty(maparg(a:map, a:mode))
for mode in split(a:mode, '.\zs')
execute l:mode . 'unmap <buffer>' a:map
endfor
endif
endfunction
function! s:ToggleMapping() "{{{2
let separator_map = g:table_mode_separator
" '|' is a special character, we need to map <Bar> instead
if g:table_mode_separator ==# '|' | let separator_map = '<Bar>' | endif
if tablemode#IsActive()
call s:Map('<Plug>(table-mode-tableize)', separator_map, 'i')
call s:Map('<Plug>(table-mode-motion-up)', '{<Bar>', 'n')
call s:Map('<Plug>(table-mode-motion-down)', '}<Bar>', 'n')
call s:Map('<Plug>(table-mode-motion-left)', '[<Bar>', 'n')
call s:Map('<Plug>(table-mode-motion-right)', ']<Bar>', 'n')
call s:Map('<Plug>(table-mode-cell-text-object-a)', 'a<Bar>', 'ox')
call s:Map('<Plug>(table-mode-cell-text-object-i)', 'i<Bar>', 'ox')
call s:Map('<Plug>(table-mode-realign)', '<Leader>tr', 'n')
call s:Map('<Plug>(table-mode-delete-row)', '<Leader>tdd', 'n')
call s:Map('<Plug>(table-mode-delete-column)', '<Leader>tdc', 'n')
call s:Map('<Plug>(table-mode-add-formula)', '<Leader>tfa', 'n')
call s:Map('<Plug>(table-mode-eval-formula)', '<Leader>tfe', 'n')
call s:Map('<Plug>(table-mode-echo-cell)', '<Leader>t?', 'n')
else else
silent! execute "iunmap <silent> <buffer> " . b:table_mode_separator_map call s:UnMap(separator_map, 'i')
call s:UnMap('{<Bar>', 'n')
call s:UnMap('}<Bar>', 'n')
call s:UnMap('[<Bar>', 'n')
call s:UnMap(']<Bar>', 'n')
call s:UnMap('a<Bar>', 'o')
call s:UnMap('i<Bar>', 'o')
call s:UnMap('<Leader>tdd', 'n')
call s:UnMap('<Leader>tdc', 'n')
call s:UnMap('<Leader>tfa', 'n')
call s:UnMap('<Leader>tfe', 'n')
call s:UnMap('<Leader>t?', 'n')
endif
endfunction
function! tablemode#SyntaxEnable()
exec 'syntax match Table'
\ '/' . tablemode#table#StartExpr() . '\zs|.\+|\ze' . tablemode#table#EndExpr() . '/'
\ 'contains=TableBorder,TableSeparator,TableColumnAlign containedin=ALL'
syntax match TableSeparator /|/ contained
syntax match TableColumnAlign /:/ contained
syntax match TableBorder /[\-+]\+/ contained
hi! link TableBorder Delimiter
hi! link TableSeparator Delimiter
hi! link TableColumnAlign Type
endfunction
function! s:ToggleSyntax()
if tablemode#IsActive()
call tablemode#SyntaxEnable()
else
syntax clear Table
syntax clear TableBorder
syntax clear TableSeparator
syntax clear TableColumnAlign
hi! link TableBorder NONE
hi! link TableSeparator NONE
hi! link TableColumnAlign NONE
endif endif
endfunction endfunction
function! s:SetActive(bool) "{{{2 function! s:SetActive(bool) "{{{2
let b:table_mode_active = a:bool let b:table_mode_active = a:bool
call s:ToggleSyntax()
call s:ToggleMapping() call s:ToggleMapping()
endfunction endfunction
function! s:ConvertDelimiterToSeparator(line, ...) "{{{2 function! s:ConvertDelimiterToSeparator(line, ...) "{{{2
let gflag = 'g' let old_gdefault = &gdefault
if &gdefault | let gflag = 'gg' | endif set nogdefault
let delim = g:table_mode_delimiter let delim = g:table_mode_delimiter
if a:0 | let delim = a:1 | endif if a:0 | let delim = a:1 | endif
if delim ==# ',' if delim ==# ','
silent! execute a:line . 's/' . "[\'\"][^\'\"]*\\zs,\\ze[^\'\"]*[\'\"]/__COMMA__/" . gflag silent! execute a:line . 's/' . "[\'\"][^\'\"]*\\zs,\\ze[^\'\"]*[\'\"]/__COMMA__/g"
endif endif
let [cstart, cend] = [tablemode#table#GetCommentStart(), tablemode#table#GetCommentEnd()] let [cstart, cend] = [tablemode#table#GetCommentStart(), tablemode#table#GetCommentEnd()]
@@ -67,11 +136,13 @@ function! s:ConvertDelimiterToSeparator(line, ...) "{{{2
silent! execute a:line . 's/' . tablemode#table#StartExpr() . '\zs\ze' . match_char_start . silent! execute a:line . 's/' . tablemode#table#StartExpr() . '\zs\ze' . match_char_start .
\ '\|' . delim . '\|' . match_char_end . '\zs\ze' . tablemode#table#EndExpr() . '/' . \ '\|' . delim . '\|' . match_char_end . '\zs\ze' . tablemode#table#EndExpr() . '/' .
\ g:table_mode_separator . '/' . gflag \ g:table_mode_separator . '/g'
if delim ==# ',' if delim ==# ','
silent! execute a:line . 's/' . "[\'\"][^\'\"]*\\zs__COMMA__\\ze[^\'\"]*[\'\"]/,/" . gflag silent! execute a:line . 's/' . "[\'\"][^\'\"]*\\zs__COMMA__\\ze[^\'\"]*[\'\"]/,/g"
endif endif
let &gdefault=old_gdefault
endfunction endfunction
function! s:Tableizeline(line, ...) "{{{2 function! s:Tableizeline(line, ...) "{{{2
@@ -99,7 +170,7 @@ endfunction
function! tablemode#TableizeInsertMode() "{{{2 function! tablemode#TableizeInsertMode() "{{{2
if tablemode#IsActive() && getline('.') =~# (tablemode#table#StartExpr() . g:table_mode_separator . g:table_mode_separator) if tablemode#IsActive() && getline('.') =~# (tablemode#table#StartExpr() . g:table_mode_separator . g:table_mode_separator)
call tablemode#table#AddHeaderBorder('.') call tablemode#table#AddBorder('.')
normal! A normal! A
elseif tablemode#IsActive() && getline('.') =~# (tablemode#table#StartExpr() . g:table_mode_separator) elseif tablemode#IsActive() && getline('.') =~# (tablemode#table#StartExpr() . g:table_mode_separator)
let column = tablemode#utils#strlen(substitute(getline('.')[0:col('.')], '[^' . g:table_mode_separator . ']', '', 'g')) let column = tablemode#utils#strlen(substitute(getline('.')[0:col('.')], '[^' . g:table_mode_separator . ']', '', 'g'))

View File

@@ -19,32 +19,6 @@
" Borrowed from Tabular " Borrowed from Tabular
" Private Functions {{{1 " Private Functions {{{1
" Return the number of bytes in a string after expanding tabs to spaces. {{{2
" This expansion is done based on the current value of 'tabstop'
if exists('*strdisplaywidth')
" Needs vim 7.3
let s:Strlen = function("strdisplaywidth")
else
function! s:Strlen(string)
" Implement the tab handling part of strdisplaywidth for vim 7.2 and
" earlier - not much that can be done about handling doublewidth
" characters.
let rv = 0
let i = 0
for char in split(a:string, '\zs')
if char == "\t"
let rv += &ts - i
let i = 0
else
let rv += 1
let i = (i + 1) % &ts
endif
endfor
return rv
endfunction
endif
" function! s:StripTrailingSpaces(string) - Remove all trailing spaces {{{2 " function! s:StripTrailingSpaces(string) - Remove all trailing spaces {{{2
" from a string. " from a string.
function! s:StripTrailingSpaces(string) function! s:StripTrailingSpaces(string)
@@ -52,7 +26,7 @@ function! s:StripTrailingSpaces(string)
endfunction endfunction
function! s:Padding(string, length, where) "{{{3 function! s:Padding(string, length, where) "{{{3
let gap_length = a:length - s:Strlen(a:string) let gap_length = a:length - tablemode#utils#StrDisplayWidth(a:string)
if a:where =~# 'l' if a:where =~# 'l'
return a:string . repeat(" ", gap_length) return a:string . repeat(" ", gap_length)
elseif a:where =~# 'r' elseif a:where =~# 'r'
@@ -118,18 +92,21 @@ function! tablemode#align#Split(string, delim)
endfunction endfunction
function! tablemode#align#alignments(lnum, ncols) "{{{2 function! tablemode#align#alignments(lnum, ncols) "{{{2
let alignments = repeat(['l'], a:ncols) " For each column let alignments = []
if tablemode#table#IsHeader(a:lnum+1) if tablemode#table#IsBorder(a:lnum+1)
let hcols = tablemode#align#Split(getline(a:lnum+1), '[' . g:table_mode_corner . g:table_mode_corner_corner . ']') let hcols = tablemode#align#Split(getline(a:lnum+1), '[' . g:table_mode_corner . g:table_mode_corner_corner . ']')
for idx in range(len(hcols)) for idx in range(len(hcols))
" Right align if header " Right align if header
call add(alignments, 'l')
if hcols[idx] =~# g:table_mode_align_char . '$' | let alignments[idx] = 'r' | endif if hcols[idx] =~# g:table_mode_align_char . '$' | let alignments[idx] = 'r' | endif
if hcols[idx] !~# '[^0-9\.]' | let alignments[idx] = 'r' | endif
endfor endfor
end end
return alignments return alignments
endfunction endfunction
function! tablemode#align#Align(lines) "{{{2 function! tablemode#align#Align(lines) "{{{2
if empty(a:lines) | return [] | endif
let lines = map(a:lines, 'map(v:val, "v:key =~# \"text\" ? tablemode#align#Split(v:val, g:table_mode_separator) : v:val")') let lines = map(a:lines, 'map(v:val, "v:key =~# \"text\" ? tablemode#align#Split(v:val, g:table_mode_separator) : v:val")')
for line in lines for line in lines
@@ -152,9 +129,9 @@ function! tablemode#align#Align(lines) "{{{2
if len(stext) <= 1 | continue | endif if len(stext) <= 1 | continue | endif
for i in range(len(stext)) for i in range(len(stext))
if i == len(maxes) if i == len(maxes)
let maxes += [ s:Strlen(stext[i]) ] let maxes += [ tablemode#utils#StrDisplayWidth(stext[i]) ]
else else
let maxes[i] = max([ maxes[i], s:Strlen(stext[i]) ]) let maxes[i] = max([ maxes[i], tablemode#utils#StrDisplayWidth(stext[i]) ])
endif endif
endfor endfor
endfor endfor
@@ -166,9 +143,11 @@ function! tablemode#align#Align(lines) "{{{2
let tline = lines[idx].text let tline = lines[idx].text
if len(tline) <= 1 | continue | endif if len(tline) <= 1 | continue | endif
for i in range(len(tline)) for jdx in range(len(tline))
let field = s:Padding(tline[i], maxes[i], alignments[i]) " Dealing with the header being the first line
let tline[i] = field . (i == 0 || i == len(tline) ? '' : ' ') if jdx >= len(alignments) | call add(alignments, 'l') | endif
let field = s:Padding(tline[jdx], maxes[jdx], alignments[jdx])
let tline[jdx] = field . (jdx == 0 || jdx == len(tline) ? '' : ' ')
endfor endfor
let lines[idx].text = s:StripTrailingSpaces(join(tline, '')) let lines[idx].text = s:StripTrailingSpaces(join(tline, ''))

View File

@@ -50,10 +50,10 @@ function! tablemode#spreadsheet#GetFirstRow(line) "{{{2
if tablemode#table#IsRow(a:line) if tablemode#table#IsRow(a:line)
let line = tablemode#utils#line(a:line) let line = tablemode#utils#line(a:line)
while tablemode#table#IsRow(line - 1) || tablemode#table#IsHeader(line - 1) while tablemode#table#IsRow(line - 1) || tablemode#table#IsBorder(line - 1)
let line -= 1 let line -= 1
endwhile endwhile
if tablemode#table#IsHeader(line) | let line += 1 | endif if tablemode#table#IsBorder(line) | let line += 1 | endif
return line return line
endif endif
@@ -69,10 +69,10 @@ function! tablemode#spreadsheet#GetLastRow(line) "{{{2
if tablemode#table#IsRow(a:line) if tablemode#table#IsRow(a:line)
let line = tablemode#utils#line(a:line) let line = tablemode#utils#line(a:line)
while tablemode#table#IsRow(line + 1) || tablemode#table#IsHeader(line + 1) while tablemode#table#IsRow(line + 1) || tablemode#table#IsBorder(line + 1)
let line += 1 let line += 1
endwhile endwhile
if tablemode#table#IsHeader(line) | let line -= 1 | endif if tablemode#table#IsBorder(line) | let line -= 1 | endif
return line return line
endif endif
@@ -89,7 +89,7 @@ function! tablemode#spreadsheet#LineNr(row) "{{{2
let line = tablemode#spreadsheet#GetFirstRow('.') let line = tablemode#spreadsheet#GetFirstRow('.')
let row_nr = 0 let row_nr = 0
while tablemode#table#IsRow(line + 1) || tablemode#table#IsHeader(line + 1) while tablemode#table#IsRow(line + 1) || tablemode#table#IsBorder(line + 1)
if tablemode#table#IsRow(line) if tablemode#table#IsRow(line)
let row_nr += 1 let row_nr += 1
if row ==# row_nr | break | endif if row ==# row_nr | break | endif
@@ -105,7 +105,7 @@ function! tablemode#spreadsheet#RowNr(line) "{{{2
let line = tablemode#utils#line(a:line) let line = tablemode#utils#line(a:line)
let rowNr = 0 let rowNr = 0
while tablemode#table#IsRow(line) || tablemode#table#IsHeader(line) while tablemode#table#IsRow(line) || tablemode#table#IsBorder(line)
if tablemode#table#IsRow(line) | let rowNr += 1 | endif if tablemode#table#IsRow(line) | let rowNr += 1 | endif
let line -= 1 let line -= 1
endwhile endwhile
@@ -117,13 +117,13 @@ function! tablemode#spreadsheet#RowCount(line) "{{{2
let line = tablemode#utils#line(a:line) let line = tablemode#utils#line(a:line)
let [tline, totalRowCount] = [line, 0] let [tline, totalRowCount] = [line, 0]
while tablemode#table#IsRow(tline) || tablemode#table#IsHeader(tline) while tablemode#table#IsRow(tline) || tablemode#table#IsBorder(tline)
if tablemode#table#IsRow(tline) | let totalRowCount += 1 | endif if tablemode#table#IsRow(tline) | let totalRowCount += 1 | endif
let tline -= 1 let tline -= 1
endwhile endwhile
let tline = line + 1 let tline = line + 1
while tablemode#table#IsRow(tline) || tablemode#table#IsHeader(tline) while tablemode#table#IsRow(tline) || tablemode#table#IsBorder(tline)
if tablemode#table#IsRow(tline) | let totalRowCount += 1 | endif if tablemode#table#IsRow(tline) | let totalRowCount += 1 | endif
let tline += 1 let tline += 1
endwhile endwhile
@@ -166,27 +166,27 @@ function! tablemode#spreadsheet#MoveToStartOfCell() "{{{2
endif endif
endfunction endfunction
function! tablemode#spreadsheet#GetLastRow(line) "{{{2 function! tablemode#spreadsheet#GetFirstRow(line) "{{{2
if tablemode#table#IsRow(a:line) if tablemode#table#IsRow(a:line)
let line = tablemode#utils#line(a:line) let line = tablemode#utils#line(a:line)
while tablemode#table#IsRow(line + 1) || tablemode#table#IsHeader(line + 1) while tablemode#table#IsRow(line - 1) || tablemode#table#IsBorder(line - 1)
let line += 1 let line -= 1
endwhile endwhile
if tablemode#table#IsHeader(line) | let line -= 1 | endif if tablemode#table#IsBorder(line) | let line += 1 | endif
return line return line
endif endif
endfunction endfunction
function! tablemode#spreadsheet#GetFirstRow(line) "{{{2 function! tablemode#spreadsheet#GetLastRow(line) "{{{2
if tablemode#table#IsRow(a:line) if tablemode#table#IsRow(a:line)
let line = tablemode#utils#line(a:line) let line = tablemode#utils#line(a:line)
while tablemode#table#IsRow(line - 1) || tablemode#table#IsHeader(line - 1) while tablemode#table#IsRow(line + 1) || tablemode#table#IsBorder(line + 1)
let line -= 1 let line += 1
endwhile endwhile
if tablemode#table#IsHeader(line) | let line += 1 | endif if tablemode#table#IsBorder(line) | let line -= 1 | endif
return line return line
endif endif

View File

@@ -81,10 +81,11 @@ function! tablemode#spreadsheet#cell#GetCells(line, ...) abort
endif endif
let first_row = tablemode#spreadsheet#GetFirstRow(line) let first_row = tablemode#spreadsheet#GetFirstRow(line)
let last_row = tablemode#spreadsheet#GetLastRow(line)
if row == 0 if row == 0
let values = [] let values = []
let line = first_row let line = first_row
while tablemode#table#IsRow(line) || tablemode#table#IsHeader(line) while tablemode#table#IsRow(line) || tablemode#table#IsBorder(line)
if tablemode#table#IsRow(line) if tablemode#table#IsRow(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, tablemode#utils#strip(get(split(row_line, g:table_mode_separator), colm>0?colm-1:colm, ''))) call add(values, tablemode#utils#strip(get(split(row_line, g:table_mode_separator), colm>0?colm-1:colm, '')))
@@ -94,13 +95,14 @@ function! tablemode#spreadsheet#cell#GetCells(line, ...) abort
return values return values
else else
let row_nr = 0 let row_nr = 0
let line = first_row let row_diff = row > 0 ? 1 : -1
while tablemode#table#IsRow(line) || tablemode#table#IsHeader(line) let line = row > 0 ? first_row : last_row
while tablemode#table#IsRow(line) || tablemode#table#IsBorder(line)
if tablemode#table#IsRow(line) if tablemode#table#IsRow(line)
let row_nr += 1 let row_nr += row_diff
if row ==# row_nr | break | endif if row ==# row_nr | break | endif
endif endif
let line += 1 let line += row_diff
endwhile 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)]
@@ -125,10 +127,27 @@ function! tablemode#spreadsheet#cell#GetCell(...) "{{{2
endfunction endfunction
function! tablemode#spreadsheet#cell#GetRow(row, ...) abort "{{{2 function! tablemode#spreadsheet#cell#GetRow(row, ...) abort "{{{2
let line = a:0 < 1 ? '.' : a:1 let line = a:0 ? a:1 : '.'
return tablemode#spreadsheet#cell#GetCells(line, a:row) return tablemode#spreadsheet#cell#GetCells(line, a:row)
endfunction endfunction
function! tablemode#spreadsheet#cell#GetRowColumn(col, ...) abort "{{{2
let line = a:0 ? a:1 : '.'
let row = tablemode#spreadsheet#RowNr('.')
return tablemode#spreadsheet#cell#GetCells(line, row, a:col)
endfunction
function! tablemode#spreadsheet#cell#GetColumn(col, ...) abort "{{{2
let line = a:0 ? a:1 : '.'
return tablemode#spreadsheet#cell#GetCells(line, 0, a:col)
endfunction
function! tablemode#spreadsheet#cell#GetColumnRow(row, ...) abort "{{{2
let line = a:0 ? a:1 : '.'
let col = tablemode#spreadsheet#ColumnNr('.')
return tablemode#spreadsheet#cell#GetCells(line, a:row, col)
endfunction
function! tablemode#spreadsheet#cell#GetCellRange(range, ...) abort "{{{2 function! tablemode#spreadsheet#cell#GetCellRange(range, ...) abort "{{{2
if a:0 < 1 if a:0 < 1
let [line, colm] = ['.', tablemode#spreadsheet#ColumnNr('.')] let [line, colm] = ['.', tablemode#spreadsheet#ColumnNr('.')]
@@ -167,23 +186,6 @@ function! tablemode#spreadsheet#cell#GetCellRange(range, ...) abort "{{{2
return values return values
endfunction endfunction
function! tablemode#spreadsheet#cell#GetRowColumn(col, ...) abort "{{{2
let line = a:0 < 1 ? '.' : a:1
let row = tablemode#spreadsheet#RowNr('.')
return tablemode#spreadsheet#cell#GetCells(line, row, a:col)
endfunction
function! tablemode#spreadsheet#cell#GetColumn(col, ...) abort "{{{2
let line = a:0 < 1 ? '.' : a:1
return tablemode#spreadsheet#cell#GetCells(line, 0, a:col)
endfunction
function! tablemode#spreadsheet#cell#GetColumnRow(row, ...) abort "{{{2
let line = a:0 < 1 ? '.' : a:1
let col = tablemode#spreadsheet#ColumnNr('.')
return tablemode#spreadsheet#cell#GetCells(line, a:row, col)
endfunction
function! tablemode#spreadsheet#cell#SetCell(val, ...) "{{{2 function! tablemode#spreadsheet#cell#SetCell(val, ...) "{{{2
if a:0 == 0 if a:0 == 0
let [line, row, colm] = ['.', tablemode#spreadsheet#RowNr('.'), tablemode#spreadsheet#ColumnNr('.')] let [line, row, colm] = ['.', tablemode#spreadsheet#RowNr('.'), tablemode#spreadsheet#ColumnNr('.')]
@@ -193,6 +195,10 @@ function! tablemode#spreadsheet#cell#SetCell(val, ...) "{{{2
let [line, row, colm] = a:000 let [line, row, colm] = a:000
endif endif
" Account for negative values to reference from relatively from the last
if row < 0 | let row = tablemode#spreadsheet#RowCount(line) + row + 1 | endif
if colm < 0 | let colm = tablemode#spreadsheet#ColumnCount(line) + colm + 1 | endif
if tablemode#table#IsRow(line) if tablemode#table#IsRow(line)
let line = tablemode#utils#line(line) + (row - tablemode#spreadsheet#RowNr(line)) * 1 let line = tablemode#utils#line(line) + (row - tablemode#spreadsheet#RowNr(line)) * 1
let line_val = getline(line) let line_val = getline(line)
@@ -227,7 +233,7 @@ function! tablemode#spreadsheet#cell#Motion(direction, ...) "{{{2
for ii in range(l:count) for ii in range(l:count)
if a:direction ==# 'l' if a:direction ==# 'l'
if tablemode#spreadsheet#IsLastCell() if tablemode#spreadsheet#IsLastCell()
if !tablemode#table#IsRow(line('.') + 1) && (tablemode#table#IsHeader(line('.') + 1) && !tablemode#table#IsRow(line('.') + 2)) if !tablemode#table#IsRow(line('.') + 1) && (tablemode#table#IsBorder(line('.') + 1) && !tablemode#table#IsRow(line('.') + 2))
return return
endif endif
call tablemode#spreadsheet#cell#Motion('j', 1) call tablemode#spreadsheet#cell#Motion('j', 1)
@@ -242,7 +248,7 @@ function! tablemode#spreadsheet#cell#Motion(direction, ...) "{{{2
endif endif
elseif a:direction ==# 'h' elseif a:direction ==# 'h'
if tablemode#spreadsheet#IsFirstCell() if tablemode#spreadsheet#IsFirstCell()
if !tablemode#table#IsRow(line('.') - 1) && (tablemode#table#IsHeader(line('.') - 1) && !tablemode#table#IsRow(line('.') - 2)) if !tablemode#table#IsRow(line('.') - 1) && (tablemode#table#IsBorder(line('.') - 1) && !tablemode#table#IsRow(line('.') - 2))
return return
endif endif
call tablemode#spreadsheet#cell#Motion('k', 1) call tablemode#spreadsheet#cell#Motion('k', 1)
@@ -259,7 +265,7 @@ function! tablemode#spreadsheet#cell#Motion(direction, ...) "{{{2
if tablemode#table#IsRow(line('.') + 1) if tablemode#table#IsRow(line('.') + 1)
" execute 'normal! ' . 1 . 'j' " execute 'normal! ' . 1 . 'j'
normal! j normal! j
elseif tablemode#table#IsHeader(line('.') + 1) && tablemode#table#IsRow(line('.') + 2) elseif tablemode#table#IsBorder(line('.') + 1) && tablemode#table#IsRow(line('.') + 2)
" execute 'normal! ' . 2 . 'j' " execute 'normal! ' . 2 . 'j'
normal! 2j normal! 2j
endif endif
@@ -267,7 +273,7 @@ function! tablemode#spreadsheet#cell#Motion(direction, ...) "{{{2
if tablemode#table#IsRow(line('.') - 1) if tablemode#table#IsRow(line('.') - 1)
" execute 'normal! ' . 1 . 'k' " execute 'normal! ' . 1 . 'k'
normal! k normal! k
elseif tablemode#table#IsHeader(line('.') - 1) && tablemode#table#IsRow(line('.') - 2) elseif tablemode#table#IsBorder(line('.') - 1) && tablemode#table#IsRow(line('.') - 2)
" execute 'normal! ' . (1 + 1) . 'k' " execute 'normal! ' . (1 + 1) . 'k'
normal! 2k normal! 2k
endif endif

View File

@@ -39,7 +39,7 @@ function! tablemode#spreadsheet#formula#Add(...) "{{{2
if fr !=# '' if fr !=# ''
let fr = '$' . row . ',' . colm . '=' . fr let fr = '$' . row . ',' . colm . '=' . fr
let fline = tablemode#spreadsheet#GetLastRow('.') + 1 let fline = tablemode#spreadsheet#GetLastRow('.') + 1
if tablemode#table#IsHeader(fline) | let fline += 1 | endif if tablemode#table#IsBorder(fline) | let fline += 1 | 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
@@ -129,13 +129,13 @@ function! tablemode#spreadsheet#formula#EvaluateFormulaLine() abort "{{{2
if tablemode#table#IsRow('.') " We're inside the table if tablemode#table#IsRow('.') " We're inside the table
let line = tablemode#spreadsheet#GetLastRow('.') let line = tablemode#spreadsheet#GetLastRow('.')
let fline = line + 1 let fline = line + 1
if tablemode#table#IsHeader(fline) | let fline += 1 | endif if tablemode#table#IsBorder(fline) | let fline += 1 | endif
if getline(fline) =~# 'tmf: ' if getline(fline) =~# 'tmf: '
let exprs = split(matchstr(getline(fline), matchexpr), ';') 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('.') - 1 let line = line('.') - 1
if tablemode#table#IsHeader(line) | let line -= 1 | endif if tablemode#table#IsBorder(line) | let line -= 1 | endif
if tablemode#table#IsRow(line) if tablemode#table#IsRow(line)
let exprs = split(matchstr(getline('.'), matchexpr), ';') let exprs = split(matchstr(getline('.'), matchexpr), ';')
endif endif

View File

@@ -47,7 +47,9 @@ function! s:GenerateHeaderBorder(line) "{{{2
if tablemode#utils#strlen(line_val) <= 1 | return s:DefaultHeaderBorder() | endif if tablemode#utils#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') " To accurately deal with unicode double width characters
let fill_columns = map(split(border, g:table_mode_corner), 'repeat(g:table_mode_fillchar, tablemode#utils#StrDisplayWidth(v:val))')
let border = g:table_mode_corner . join(fill_columns, g:table_mode_corner) . g:table_mode_corner
let border = substitute(border, '^' . g:table_mode_corner . '\(.*\)' . g:table_mode_corner . '$', g:table_mode_corner_corner . '\1' . g:table_mode_corner_corner, '') let border = substitute(border, '^' . g:table_mode_corner . '\(.*\)' . g:table_mode_corner . '$', g:table_mode_corner_corner . '\1' . g:table_mode_corner_corner, '')
" Incorporate header alignment chars " Incorporate header alignment chars
@@ -94,24 +96,6 @@ function! tablemode#table#scope() "{{{2
return s: return s:
endfunction endfunction
function! tablemode#table#StartCommentExpr() "{{{2
let cstartexpr = tablemode#table#GetCommentStart()
if tablemode#utils#strlen(cstartexpr) > 0
return '^\s*' . cstartexpr . '\s*'
else
return ''
endif
endfunction
function! tablemode#table#EndCommentExpr() "{{{2
let cendexpr = tablemode#table#GetCommentEnd()
if tablemode#utils#strlen(cendexpr) > 0
return '.*\zs\s\+' . cendexpr . '\s*$'
else
return ''
endif
endfunction
function! tablemode#table#GetCommentStart() "{{{2 function! tablemode#table#GetCommentStart() "{{{2
let cstring = &commentstring let cstring = &commentstring
if tablemode#utils#strlen(cstring) > 0 if tablemode#utils#strlen(cstring) > 0
@@ -121,6 +105,15 @@ function! tablemode#table#GetCommentStart() "{{{2
endif endif
endfunction endfunction
function! tablemode#table#StartCommentExpr() "{{{2
let cstartexpr = tablemode#table#GetCommentStart()
if tablemode#utils#strlen(cstartexpr) > 0
return '^\s*' . cstartexpr . '\s*'
else
return ''
endif
endfunction
function! tablemode#table#GetCommentEnd() "{{{2 function! tablemode#table#GetCommentEnd() "{{{2
let cstring = &commentstring let cstring = &commentstring
if tablemode#utils#strlen(cstring) > 0 if tablemode#utils#strlen(cstring) > 0
@@ -135,6 +128,15 @@ function! tablemode#table#GetCommentEnd() "{{{2
endif endif
endfunction endfunction
function! tablemode#table#EndCommentExpr() "{{{2
let cendexpr = tablemode#table#GetCommentEnd()
if tablemode#utils#strlen(cendexpr) > 0
return '.*\zs\s\+' . cendexpr . '\s*$'
else
return ''
endif
endfunction
function! tablemode#table#StartExpr() "{{{2 function! tablemode#table#StartExpr() "{{{2
let cstart = tablemode#table#GetCommentStart() let cstart = tablemode#table#GetCommentStart()
if tablemode#utils#strlen(cstart) > 0 if tablemode#utils#strlen(cstart) > 0
@@ -153,15 +155,20 @@ function! tablemode#table#EndExpr() "{{{2
endif endif
endfunction endfunction
function! tablemode#table#IsHeader(line) "{{{2 function! tablemode#table#IsBorder(line) "{{{2
return getline(a:line) =~# s:HeaderBorderExpr() return getline(a:line) =~# s:HeaderBorderExpr()
endfunction endfunction
function! tablemode#table#IsRow(line) "{{{2 function! tablemode#table#IsHeader(line) "{{{2
return !tablemode#table#IsHeader(a:line) && getline(a:line) =~# (tablemode#table#StartExpr() . g:table_mode_separator) let line = tablemode#utils#line(a:line)
return tablemode#table#IsBorder(line+1) && !tablemode#table#IsRow(line-1) && !tablemode#table#IsRow(line-2)
endfunction endfunction
function! tablemode#table#AddHeaderBorder(line) "{{{2 function! tablemode#table#IsRow(line) "{{{2
return !tablemode#table#IsBorder(a:line) && getline(a:line) =~# (tablemode#table#StartExpr() . g:table_mode_separator)
endfunction
function! tablemode#table#AddBorder(line) "{{{2
call setline(a:line, s:GenerateHeaderBorder(a:line)) call setline(a:line, s:GenerateHeaderBorder(a:line))
endfunction endfunction
@@ -170,8 +177,8 @@ function! tablemode#table#Realign(line) "{{{2
let lines = [] let lines = []
let [lnum, blines] = [line, []] let [lnum, blines] = [line, []]
while tablemode#table#IsRow(lnum) || tablemode#table#IsHeader(lnum) while tablemode#table#IsRow(lnum) || tablemode#table#IsBorder(lnum)
if tablemode#table#IsHeader(lnum) if tablemode#table#IsBorder(lnum)
call insert(blines, lnum) call insert(blines, lnum)
let lnum -= 1 let lnum -= 1
continue continue
@@ -181,8 +188,8 @@ function! tablemode#table#Realign(line) "{{{2
endwhile endwhile
let lnum = line + 1 let lnum = line + 1
while tablemode#table#IsRow(lnum) || tablemode#table#IsHeader(lnum) while tablemode#table#IsRow(lnum) || tablemode#table#IsBorder(lnum)
if tablemode#table#IsHeader(lnum) if tablemode#table#IsBorder(lnum)
call add(blines, lnum) call add(blines, lnum)
let lnum += 1 let lnum += 1
continue continue
@@ -198,6 +205,6 @@ function! tablemode#table#Realign(line) "{{{2
endfor endfor
for bline in blines for bline in blines
call tablemode#table#AddHeaderBorder(bline) call tablemode#table#AddBorder(bline)
endfor endfor
endfunction endfunction

View File

@@ -51,3 +51,27 @@ endfunction
function! tablemode#utils#strlen(text) function! tablemode#utils#strlen(text)
return strlen(substitute(a:text, '.', 'x', 'g')) return strlen(substitute(a:text, '.', 'x', 'g'))
endfunction endfunction
function! tablemode#utils#StrDisplayWidth(string) "{{{2
if exists('*strdisplaywidth')
return strdisplaywidth(a:string)
else
" Implement the tab handling part of strdisplaywidth for vim 7.2 and
" earlier - not much that can be done about handling doublewidth
" characters.
let rv = 0
let i = 0
for char in split(a:string, '\zs')
if char == "\t"
let rv += &ts - i
let i = 0
else
let rv += 1
let i = (i + 1) % &ts
endif
endfor
return rv
endif
endfunction

View File

@@ -1,7 +1,7 @@
*table-mode.txt* Table Mode for easy table formatting. v4.3.0 *table-mode.txt* Table Mode for easy table formatting. v4.5.0
=============================================================================== ===============================================================================
Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER
VERSION 4.3.0 VERSION 4.5.0
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/>
@@ -104,11 +104,14 @@ Formula Expressions :
'$n': This matches the table column number 'n'. So the formula '$n': This matches the table column number 'n'. So the formula
would be evaluated for each cell in that column and the result would be evaluated for each cell in that column and the result
would be placed in it. would be placed in it. You can use negative indice to
represent column relative to the last, -1 being the last.
'$n,m': This matches the table cell n,m (row, column). So in '$n,m': This matches the table cell n,m (row, column). So in
this case the formula would be evaluated and the result will this case the formula would be evaluated and the result will
be placed in this cell. be placed in this cell. You can also use negative values to
refer to cells relative to the size, -1 being the last (row or
column).
The formula can be a simple mathematical expression involving cells The formula can be a simple mathematical expression involving cells
which are also defined by the same format as that of the target cell. which are also defined by the same format as that of the target cell.
@@ -116,7 +119,7 @@ Formula Expressions :
special functions 'Sum' and 'Average'. Both these functions take a special functions 'Sum' and 'Average'. Both these functions take a
range as input. A range can be of two forms : range as input. A range can be of two forms :
'n,m': This represents cells in the current column from row 'n:m': This represents cells in the current column from row
'n' through 'm'. If 'm' is negative it represents 'm' row 'n' through 'm'. If 'm' is negative it represents 'm' row
above the current row (of the target cell). above the current row (of the target cell).

View File

@@ -50,6 +50,12 @@ function! s:TableEchoCell() "{{{1
endif endif
endfunction endfunction
augroup TableMode
au!
autocmd Syntax * if tablemode#IsActive() | call tablemode#SyntaxEnable() | endif
augroup END
" 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 .
@@ -73,6 +79,10 @@ command! TableAddFormula call tablemode#spreadsheet#formula#Add()
command! TableModeRealign call tablemode#table#Realign('.') command! TableModeRealign call tablemode#table#Realign('.')
command! TableEvalFormulaLine call tablemode#spreadsheet#formula#EvaluateFormulaLine() command! TableEvalFormulaLine call tablemode#spreadsheet#formula#EvaluateFormulaLine()
" '|' is a special character, we need to map <Bar> instead
if g:table_mode_separator ==# '|' | let separator_map = '<Bar>' | endif
execute 'inoremap <silent> <Plug>(table-mode-tableize)' separator_map . '<Esc>:call tablemode#TableizeInsertMode()<CR>a'
nnoremap <silent> <Plug>(table-mode-tableize) :Tableize<CR> nnoremap <silent> <Plug>(table-mode-tableize) :Tableize<CR>
xnoremap <silent> <Plug>(table-mode-tableize) :Tableize<CR> xnoremap <silent> <Plug>(table-mode-tableize) :Tableize<CR>
xnoremap <silent> <Plug>(table-mode-tableize-delimiter) :<C-U>call tablemode#TableizeByDelimiter()<CR> xnoremap <silent> <Plug>(table-mode-tableize-delimiter) :<C-U>call tablemode#TableizeByDelimiter()<CR>
@@ -86,6 +96,8 @@ nnoremap <silent> <Plug>(table-mode-motion-right) :<C-U>call tablemode#spreadshe
onoremap <silent> <Plug>(table-mode-cell-text-object-a) :<C-U>call tablemode#spreadsheet#cell#TextObject(0)<CR> onoremap <silent> <Plug>(table-mode-cell-text-object-a) :<C-U>call tablemode#spreadsheet#cell#TextObject(0)<CR>
onoremap <silent> <Plug>(table-mode-cell-text-object-i) :<C-U>call tablemode#spreadsheet#cell#TextObject(1)<CR> onoremap <silent> <Plug>(table-mode-cell-text-object-i) :<C-U>call tablemode#spreadsheet#cell#TextObject(1)<CR>
xnoremap <silent> <Plug>(table-mode-cell-text-object-a) :<C-U>call tablemode#spreadsheet#cell#TextObject(0)<CR>
xnoremap <silent> <Plug>(table-mode-cell-text-object-i) :<C-U>call tablemode#spreadsheet#cell#TextObject(1)<CR>
nnoremap <silent> <Plug>(table-mode-delete-row) :call tablemode#spreadsheet#DeleteRow()<CR> nnoremap <silent> <Plug>(table-mode-delete-row) :call tablemode#spreadsheet#DeleteRow()<CR>
nnoremap <silent> <Plug>(table-mode-delete-column) :call tablemode#spreadsheet#DeleteColumn()<CR> nnoremap <silent> <Plug>(table-mode-delete-column) :call tablemode#spreadsheet#DeleteColumn()<CR>
@@ -104,48 +116,6 @@ if !hasmapto('<Plug>(table-mode-tableize-delimiter)')
xmap <Leader>T <Plug>(table-mode-tableize-delimiter) xmap <Leader>T <Plug>(table-mode-tableize-delimiter)
endif endif
if !hasmapto('<Plug>(table-mode-realign)')
nmap <Leader>tr <Plug>(table-mode-realign)
endif
if !hasmapto('<Plug>(table-mode-motion-up)')
nmap {<Bar> <Plug>(table-mode-motion-up)
endif
if !hasmapto('<Plug>(table-mode-motion-down)')
nmap }<Bar> <Plug>(table-mode-motion-down)
endif
if !hasmapto('<Plug>(table-mode-motion-left)')
nmap [<Bar> <Plug>(table-mode-motion-left)
endif
if !hasmapto('<Plug>(table-mode-motion-right)')
nmap ]<Bar> <Plug>(table-mode-motion-right)
endif
if !hasmapto('<Plug>(table-mode-cell-text-object-a)')
omap a<Bar> <Plug>(table-mode-cell-text-object-a)
endif
if !hasmapto('<Plug>(table-mode-cell-text-object-i)')
omap i<Bar> <Plug>(table-mode-cell-text-object-i)
endif
if !hasmapto('<Plug>(table-mode-delete-row)')
nmap <Leader>tdd <Plug>(table-mode-delete-row)
endif
if !hasmapto('<Plug>(table-mode-delete-column)')
nmap <Leader>tdc <Plug>(table-mode-delete-column)
endif
if !hasmapto('<Plug>(table-mode-add-formula)')
nmap <Leader>tfa <Plug>(table-mode-add-formula)
endif
if !hasmapto('<Plug>(table-mode-eval-formula)')
nmap <Leader>tfe <Plug>(table-mode-eval-formula)
endif
if !hasmapto('<Plug>(table-mode-echo-cell)')
nmap <Leader>t? <Plug>(table-mode-echo-cell)
endif
" Avoiding side effects {{{1 " Avoiding side effects {{{1
let &cpo = s:save_cpo let &cpo = s:save_cpo

View File

@@ -10,7 +10,7 @@ describe 'cell'
read t/fixtures/sample.txt read t/fixtures/sample.txt
end end
it 'should return the cells' it 'should return the cells with GetCells'
Expect tablemode#spreadsheet#cell#GetCells(2, 1, 1) ==# 'test11' Expect tablemode#spreadsheet#cell#GetCells(2, 1, 1) ==# 'test11'
" Get Rows " Get Rows
Expect tablemode#spreadsheet#cell#GetCells(2, 1) == ['test11', 'test12'] Expect tablemode#spreadsheet#cell#GetCells(2, 1) == ['test11', 'test12']
@@ -20,7 +20,17 @@ describe 'cell'
Expect tablemode#spreadsheet#cell#GetCells(2, 0, 2) == ['test12', 'test22'] Expect tablemode#spreadsheet#cell#GetCells(2, 0, 2) == ['test12', 'test22']
end end
it 'should return the cells in a range' it 'should return the row with GetRow'
Expect tablemode#spreadsheet#cell#GetRow(1, 2) == ['test11', 'test12']
Expect tablemode#spreadsheet#cell#GetRow(2, 2) == ['test21', 'test22']
end
it 'should return the column with GetColumn'
Expect tablemode#spreadsheet#cell#GetColumn(1, 2) == ['test11', 'test21']
Expect tablemode#spreadsheet#cell#GetColumn(2, 2) == ['test12', 'test22']
end
it 'should return the cells in a range with GetCellRange'
" Entire table as range " Entire table as range
Expect tablemode#spreadsheet#cell#GetCellRange('1,1:2,2', 2, 1) == [['test11', 'test21'], ['test12', 'test22']] Expect tablemode#spreadsheet#cell#GetCellRange('1,1:2,2', 2, 1) == [['test11', 'test21'], ['test12', 'test22']]
@@ -54,20 +64,19 @@ describe 'cell'
before before
new new
normal! ggdG normal! ggdG
call tablemode#Enable() read t/fixtures/sample.txt
normal i|test11|test12| call cursor(2, 3)
|test21|test22|
end end
it 'should move left when not on first column' it 'should move left when not on first column'
it 'should move left when not on first column' call cursor(2, 12)
Expect tablemode#spreadsheet#ColumnNr('.') == 2 Expect tablemode#spreadsheet#ColumnNr('.') == 2
call tablemode#spreadsheet#cell#Motion('h') call tablemode#spreadsheet#cell#Motion('h')
Expect tablemode#spreadsheet#ColumnNr('.') == 1 Expect tablemode#spreadsheet#ColumnNr('.') == 1
end end
it 'should move to the previous row last column if it exists when on first column' it 'should move to the previous row last column if it exists when on first column'
it 'should move to the previous row last column if it exists when on first column' call cursor(3, 3)
Expect tablemode#spreadsheet#RowNr('.') == 2 Expect tablemode#spreadsheet#RowNr('.') == 2
Expect tablemode#spreadsheet#ColumnNr('.') == 1 Expect tablemode#spreadsheet#ColumnNr('.') == 1
call tablemode#spreadsheet#cell#Motion('h') call tablemode#spreadsheet#cell#Motion('h')
@@ -82,7 +91,7 @@ describe 'cell'
end end
it 'should move to the next row first column if it exists when on last column' it 'should move to the next row first column if it exists when on last column'
it 'should move to the next row first column if it exists when on last column' call cursor(2, 12)
Expect tablemode#spreadsheet#RowNr('.') == 1 Expect tablemode#spreadsheet#RowNr('.') == 1
Expect tablemode#spreadsheet#ColumnNr('.') == 2 Expect tablemode#spreadsheet#ColumnNr('.') == 2
call tablemode#spreadsheet#cell#Motion('l') call tablemode#spreadsheet#cell#Motion('l')
@@ -95,13 +104,12 @@ describe 'cell'
before before
new new
normal! ggdG normal! ggdG
normal! ggdG read t/fixtures/sample.txt
call tablemode#Enable() call cursor(2, 3)
normal i|test11|test12|
end end
it 'should move a row up unless on first row' it 'should move a row up unless on first row'
call cursor(3, 3)
Expect tablemode#spreadsheet#RowNr('.') == 2 Expect tablemode#spreadsheet#RowNr('.') == 2
call tablemode#spreadsheet#cell#Motion('k') call tablemode#spreadsheet#cell#Motion('k')
Expect tablemode#spreadsheet#RowNr('.') == 1 Expect tablemode#spreadsheet#RowNr('.') == 1

View File

@@ -0,0 +1,2 @@
| 1 | 2 |
| 3 | 4 |

View File

@@ -0,0 +1,4 @@
| test11 | test12 |
| test21 | test22 |

View File

@@ -0,0 +1,3 @@
| test11 | test12 |
| test21 | test22 |

View File

@@ -0,0 +1,9 @@
| abc | 测试长度 | 长测试 |
| 长 | 测试测试测试测试 | 测试测试 |
| 测试测试 | 测试 | 测试测测试 |
| 测试测试测试 | 测试测试 | 测试 |

View File

@@ -0,0 +1,4 @@
| Title | Message |
|------:+--------:|
| t1 | msg1 |
| t2 | msg2 |

View File

@@ -0,0 +1,4 @@
| Title | Message |
|------:+--------:|
| t1 | msg1 |
| t2 | msg2 |

View File

@@ -0,0 +1,8 @@
|--------------+------------------+------------|
| 测试测试 | 测试长度 | 长测试 |
|--------------+------------------+-----------:|
| abc | 测试长度 | 长测试 |
| 长 | 测试测试测试测试 | 测试测试 |
| 测试测试 | 测试 | 测试测测试 |
| 测试测试测试 | 测试测试 | 测试 |
|--------------+------------------+------------|

View File

@@ -0,0 +1,8 @@
|--------+--------+------|
|测试测试|测试长度|长测试|
|--------+--------+-----:|
|abc|测试长度|长测试|
|长|测试测试测试测试|测试测试|
|测试测试|测试|测试测测试|
|测试测试测试|测试测试|测试|
|------------+--------+----|

View File

@@ -0,0 +1,2 @@
| test11 | test12 |
| test21 | test22 |

View File

@@ -0,0 +1,2 @@
|test11|test12|
|test21|test22|

View File

@@ -0,0 +1,4 @@
| abc | 测试长度 | 长测试 |
| 长 | 测试测试测试测试 | 测试测试 |
| 测试测试 | 测试 | 测试测测试 |
| 测试测试测试 | 测试测试 | 测试 |

View File

@@ -0,0 +1,4 @@
|abc|测试长度|长测试|
|长|测试测试测试测试|测试测试|
|测试测试|测试|测试测测试|
|测试测试测试|测试测试|测试|

View File

@@ -1,4 +1,4 @@
|--------+---------|
| Title | Message | | Title | Message |
|--------+---------| |--------+---------|
| test11 | test12 | | test11 | test12 |

View File

@@ -25,6 +25,27 @@ describe 'spreadsheet'
Expect tablemode#spreadsheet#ColumnCount(3) == 2 Expect tablemode#spreadsheet#ColumnCount(3) == 2
end end
it 'should return the column number'
call cursor(2,3)
Expect tablemode#spreadsheet#ColumnNr('.') == 1
call cursor(2,12)
Expect tablemode#spreadsheet#ColumnNr('.') == 2
end
it 'should return true when in the first cell'
call cursor(2,3)
Expect tablemode#spreadsheet#IsFirstCell() to_be_true
call cursor(2,12)
Expect tablemode#spreadsheet#IsFirstCell() to_be_false
end
it 'should return true when in the last cell'
call cursor(2,3)
Expect tablemode#spreadsheet#IsLastCell() to_be_false
call cursor(2,12)
Expect tablemode#spreadsheet#IsLastCell() to_be_true
end
it 'should return the line number of the first row' it 'should return the line number of the first row'
Expect tablemode#spreadsheet#GetFirstRow(2) == 2 Expect tablemode#spreadsheet#GetFirstRow(2) == 2
Expect tablemode#spreadsheet#GetFirstRow(3) == 2 Expect tablemode#spreadsheet#GetFirstRow(3) == 2
@@ -34,13 +55,41 @@ describe 'spreadsheet'
Expect tablemode#spreadsheet#GetLastRow(2) == 3 Expect tablemode#spreadsheet#GetLastRow(2) == 3
Expect tablemode#spreadsheet#GetLastRow(3) == 3 Expect tablemode#spreadsheet#GetLastRow(3) == 3
end end
describe 'Math'
before
new
read t/fixtures/cell/sample.txt
end
it 'should return the sum of cell range'
call cursor(1,3)
Expect tablemode#spreadsheet#Sum('1:2') == 4.0
Expect tablemode#spreadsheet#Sum('1,1:1,2') == 3.0
Expect tablemode#spreadsheet#Sum('1,1:2,2') == 10.0
call cursor(2,7)
Expect tablemode#spreadsheet#Sum('1:2') == 6.0
Expect tablemode#spreadsheet#Sum('2,1:2,2') == 7.0
end
it 'should return the average of cell range'
call cursor(1,3)
Expect tablemode#spreadsheet#Average('1:2') == 2.0
Expect tablemode#spreadsheet#Average('1,1:1,2') == 1.5
Expect tablemode#spreadsheet#Average('1,1:2,2') == 5.0
call cursor(2,7)
Expect tablemode#spreadsheet#Average('1:2') == 3.0
Expect tablemode#spreadsheet#Average('2,1:2,2') == 3.5
end
end
end end
describe 'Manipulations' describe 'Manipulations'
before before
new new
normal i|test11|test12| normal! ggdG
|test21|test22| read t/fixtures/sample.txt
call cursor(2, 3)
end end
it 'should delete a row successfully' it 'should delete a row successfully'

View File

@@ -4,40 +4,159 @@ source t/config/options.vim
call vspec#hint({'scope': 'tablemode#table#scope()', 'sid': 'tablemode#table#sid()'}) call vspec#hint({'scope': 'tablemode#table#scope()', 'sid': 'tablemode#table#sid()'})
describe 'table' describe 'table'
describe 'API'
describe 'IsRow' describe 'IsRow'
before before
new new
read t/fixtures/sample.txt normal! ggdG
read t/fixtures/table/sample.txt
end end
it 'should return true when inside a table' it 'should be true when on a table row'
Expect tablemode#table#IsRow(2) to_be_true Expect tablemode#table#IsRow(2) to_be_true
Expect tablemode#table#IsRow(3) to_be_true Expect tablemode#table#IsRow(3) to_be_true
end end
it 'should return false when outside a table' it 'should be false when not on a table row'
Expect tablemode#table#IsRow(1) to_be_false Expect tablemode#table#IsRow(1) to_be_false
Expect tablemode#table#IsRow(4) to_be_false Expect tablemode#table#IsRow(4) to_be_false
end end
end end
describe 'IsBorder'
before
new
normal! ggdG
read t/fixtures/table/sample_with_header.txt
end
it 'should be true on a table border'
Expect tablemode#table#IsBorder(1) to_be_true
Expect tablemode#table#IsBorder(3) to_be_true
Expect tablemode#table#IsBorder(6) to_be_true
end
it 'should be false when not on a table border'
Expect tablemode#table#IsBorder(2) to_be_false
Expect tablemode#table#IsBorder(4) to_be_false
Expect tablemode#table#IsBorder(5) to_be_false
end
end
describe 'IsHeader' describe 'IsHeader'
before before
new new
read t/fixtures/sample_with_header.txt normal! ggdG
read t/fixtures/table/sample_with_header.txt
end end
it 'should return true when on a table header' it 'should be true on the table header'
Expect tablemode#table#IsHeader(3) to_be_true Expect tablemode#table#IsHeader(2) to_be_true
Expect tablemode#table#IsHeader(6) to_be_true
end end
it 'should return false when not on a table header' it 'should be false anywhere else'
Expect tablemode#table#IsHeader(1) to_be_false Expect tablemode#table#IsHeader(1) to_be_false
Expect tablemode#table#IsHeader(2) to_be_false
Expect tablemode#table#IsHeader(4) to_be_false Expect tablemode#table#IsHeader(4) to_be_false
Expect tablemode#table#IsHeader(5) to_be_false Expect tablemode#table#IsHeader(5) to_be_false
Expect tablemode#table#IsHeader(6) to_be_false
Expect tablemode#table#IsHeader(7) to_be_false
end
end
describe 'AddBorder'
before
new
normal! ggdG
read t/fixtures/table/sample_for_header.txt
end
it 'should add border to line'
call tablemode#table#AddBorder(2)
Expect tablemode#table#IsHeader(1) to_be_true
Expect tablemode#table#IsBorder(2) to_be_true
end
describe 'for unicode'
before
new
normal! ggdG
read t/fixtures/table/sample_for_header_unicode.txt
end
it 'should add border to line'
call tablemode#table#AddBorder(1)
call tablemode#table#AddBorder(3)
call tablemode#table#AddBorder(5)
call tablemode#table#AddBorder(7)
call tablemode#table#AddBorder(9)
Expect tablemode#table#IsBorder(1) to_be_true
Expect tablemode#utils#StrDisplayWidth(getline(1)) == tablemode#utils#StrDisplayWidth(getline(2))
Expect tablemode#table#IsBorder(3) to_be_true
Expect tablemode#utils#StrDisplayWidth(getline(3)) == tablemode#utils#StrDisplayWidth(getline(4))
Expect tablemode#table#IsBorder(5) to_be_true
Expect tablemode#utils#StrDisplayWidth(getline(5)) == tablemode#utils#StrDisplayWidth(getline(6))
Expect tablemode#table#IsBorder(7) to_be_true
Expect tablemode#utils#StrDisplayWidth(getline(7)) == tablemode#utils#StrDisplayWidth(getline(8))
Expect tablemode#table#IsBorder(9) to_be_true
Expect tablemode#utils#StrDisplayWidth(getline(9)) == tablemode#utils#StrDisplayWidth(getline(8))
end
end
end
describe 'Realign'
describe 'without header alignments'
describe 'for simple'
before
new
normal! ggdG
read t/fixtures/table/sample_realign_before.txt
end
it 'should be aligned properly'
call tablemode#table#Realign(1)
Expect getline(1,'$') == readfile('t/fixtures/table/sample_realign_after.txt')
end
end
describe 'for unicode'
before
new
normal! ggdG
read t/fixtures/table/sample_realign_unicode_before.txt
end
it 'should be aligned properly'
call tablemode#table#Realign(1)
Expect getline(1,'$') == readfile('t/fixtures/table/sample_realign_unicode_after.txt')
end
end
end
describe 'with header alignments'
describe 'for simple'
before
new
normal! ggdG
read t/fixtures/table/sample_header_realign_before.txt
end
it 'should be aligned properly'
call tablemode#table#Realign(1)
Expect getline(1,'$') == readfile('t/fixtures/table/sample_header_realign_after.txt')
end
end
describe 'for unicode'
before
new
normal! ggdG
read t/fixtures/table/sample_header_realign_unicode_before.txt
end
it 'should be aligned properly'
call tablemode#table#Realign(1)
Expect getline(1,'$') == readfile('t/fixtures/table/sample_header_realign_unicode_after.txt')
end
end end
end end
end end

View File

@@ -3,6 +3,7 @@ source t/config/options.vim
call vspec#hint({'scope': 'tablemode#utils#scope()', 'sid': 'tablemode#utils#sid()'}) call vspec#hint({'scope': 'tablemode#utils#scope()', 'sid': 'tablemode#utils#sid()'})
describe 'utils'
describe 'line' describe 'line'
it 'should return the current line number' it 'should return the current line number'
Expect tablemode#utils#line('.') == line('.') Expect tablemode#utils#line('.') == line('.')
@@ -31,3 +32,16 @@ describe 'strlen'
Expect tablemode#utils#strlen(string) == 11 Expect tablemode#utils#strlen(string) == 11
end end
end end
describe 'strdisplaywidth'
it 'should return the display width of a string correctly'
let string = 'this is a test'
Expect tablemode#utils#StrDisplayWidth(string) == 14
end
it 'should return the display width of a unicode string correctly'
let string = '測試 is good.'
Expect tablemode#utils#StrDisplayWidth(string) == 13
end
end
end