Compare commits

...

32 Commits

Author SHA1 Message Date
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
Dhruva Sagar
0c9cd8fb50 Releasing v4.3.0 2014-04-25 15:02:27 +05:30
Dhruva Sagar
da39088fd8 Merge branch 'feature/gfm_column_alignment' 2014-04-25 14:55:22 +05:30
Dhruva Sagar
a0fcb89f43 Fixed #19
Updated tablemode#table#IsRow() to allow hiphens within the row.
2014-04-25 14:54:43 +05:30
Dhruva Sagar
5007e5f728 Added support for column alignment #23
Now we can define gfm-style aligment by adding them to the header column
2014-04-25 14:42:02 +05:30
Dhruva Sagar
71051e2c22 Merge branch 'master' into feature/gfm_column_alignment
Conflicts:
	autoload/tablemode/table.vim
2014-04-25 14:31:33 +05:30
Dhruva Sagar
43d0c0ff6f Cosmetic refactoring
* Changed the API calls to be more sensible & simple and free of
  redundancy.
2014-04-25 11:09:14 +05:30
Dhruva Sagar
00c54e8e02 Refactoring alignment 2014-04-24 15:48:57 +05:30
Dhruva Sagar
cfbac6979d Fixed #25
Minor refactoring & corrected variable names
2014-04-23 11:27:25 +05:30
Dhruva Sagar
7e00cea7f5 Improved table.vim tests. Fixed #24. 2014-04-22 00:11:40 +05:30
Dhruva Sagar
79166b3d4e Fixing #24
Exposed a public api for IsTableModeActive and using it instead.
2014-04-21 14:58:02 +05:30
Dhruva Sagar
6862a8b7e4 Update CHANGELOG.md 2014-04-09 14:40:31 +05:30
Dhruva Sagar
d889c342d3 Fixed the tests with new changes 2014-04-09 11:45:02 +05:30
Dhruva Sagar
8a54ef28ab Removed CellTextObject() from spreadsheet.vim 2014-04-09 11:34:54 +05:30
Dhruva Sagar
a2492fbf08 Removed old TableMotion from autoload/tablemode/table.vim 2014-04-09 11:32:03 +05:30
32 changed files with 760 additions and 439 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,17 @@
# Change Log # Change Log
## 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
* Refactored some more
* Fixed issue #19, hiphens in the table broke alignment
* Added feature #26, you can now define column alignments in the table header
## Version 4.2.0 ## Version 4.2.0
* Refactored cells logic out to autoload/tablemode/spreadsheet/cell.vim * Refactored cells logic out to autoload/tablemode/spreadsheet/cell.vim
* Refactored formula logic out to autoload/tablemode/spreadsheet/formula.vim * Refactored formula logic out to autoload/tablemode/spreadsheet/formula.vim
@@ -11,7 +24,7 @@
* Major refactoring of the codebase. * Major refactoring of the codebase.
* Improved modular tests. * Improved modular tests.
* Fixed long standing unicode character alignment issue. * Fixed long standing unicode character alignment issue.
* Moved to providing <Plug> mappings rather than configuration based mappings * Moved to providing \<Plug\> mappings rather than configuration based mappings
which can be more easily overriden by end user. which can be more easily overriden by end user.
## Version 3.3.2 ## Version 3.3.2

View File

@@ -1,4 +1,4 @@
# VIM Table Mode v4.2.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.4.1 [![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,13 +32,6 @@ function! s:SetBufferOptDefault(opt, val) "{{{2
endif endif
endfunction endfunction
function! s:IsTableModeActive() "{{{2
if g:table_mode_always_active | return 1 | endif
call s:SetBufferOptDefault('table_mode_active', 0)
return b:table_mode_active
endfunction
function! s:ToggleMapping() "{{{2 function! s:ToggleMapping() "{{{2
if exists('b:table_mode_active') && b:table_mode_active if exists('b:table_mode_active') && b:table_mode_active
call s:SetBufferOptDefault('table_mode_separator_map', g:table_mode_separator) call s:SetBufferOptDefault('table_mode_separator_map', g:table_mode_separator)
@@ -58,13 +51,13 @@ function! s:SetActive(bool) "{{{2
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()]
@@ -74,11 +67,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
@@ -97,28 +92,35 @@ function! tablemode#scope() "{{{2
return s: return s:
endfunction endfunction
function! tablemode#IsActive() "{{{2
if g:table_mode_always_active | return 1 | endif
call s:SetBufferOptDefault('table_mode_active', 0)
return b:table_mode_active
endfunction
function! tablemode#TableizeInsertMode() "{{{2 function! tablemode#TableizeInsertMode() "{{{2
if s:IsTableModeActive() && 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 s:IsTableModeActive() && 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'))
let position = tablemode#utils#strlen(matchstr(getline('.')[0:col('.')], '.*' . g:table_mode_separator . '\s*\zs.*')) let position = tablemode#utils#strlen(matchstr(getline('.')[0:col('.')], '.*' . g:table_mode_separator . '\s*\zs.*'))
call tablemode#table#TableRealign('.') call tablemode#table#Realign('.')
normal! 0 normal! 0
call search(repeat('[^' . g:table_mode_separator . ']*' . g:table_mode_separator, column) . '\s\{-\}' . repeat('.', position), 'ce', line('.')) call search(repeat('[^' . g:table_mode_separator . ']*' . g:table_mode_separator, column) . '\s\{-\}' . repeat('.', position), 'ce', line('.'))
endif endif
endfunction endfunction
function! tablemode#TableModeEnable() "{{{2 function! tablemode#Enable() "{{{2
call s:SetActive(1) call s:SetActive(1)
endfunction endfunction
function! tablemode#TableModeDisable() "{{{2 function! tablemode#Disable() "{{{2
call s:SetActive(0) call s:SetActive(0)
endfunction endfunction
function! tablemode#TableModeToggle() "{{{2 function! tablemode#Toggle() "{{{2
if g:table_mode_always_active if g:table_mode_always_active
return 1 return 1
endif endif
@@ -135,7 +137,7 @@ function! tablemode#TableizeRange(...) range "{{{2
let lnum += 1 let lnum += 1
endwhile endwhile
call tablemode#table#TableRealign(lnum - 1) call tablemode#table#Realign(lnum - 1)
endfunction endfunction
function! tablemode#TableizeByDelimiter() "{{{2 function! tablemode#TableizeByDelimiter() "{{{2

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'
@@ -64,11 +38,21 @@ function! s:Padding(string, length, where) "{{{3
endif endif
endfunction endfunction
" function! s:Split() - Split a string into fields and delimiters {{{2 " Public Functions {{{1
function! tablemode#align#sid() "{{{2
return maparg('<sid>', 'n')
endfunction
nnoremap <sid> <sid>
function! tablemode#align#scope() "{{{2
return s:
endfunction
" function! tablemode#align#Split() - Split a string into fields and delimiters {{{2
" Like split(), but include the delimiters as elements " Like split(), but include the delimiters as elements
" All odd numbered elements are delimiters " All odd numbered elements are delimiters
" All even numbered elements are non-delimiters (including zero) " All even numbered elements are non-delimiters (including zero)
function! s:Split(string, delim) function! tablemode#align#Split(string, delim)
let rv = [] let rv = []
let beg = 0 let beg = 0
@@ -107,59 +91,65 @@ function! s:Split(string, delim)
return rv return rv
endfunction endfunction
" Public Functions {{{1 function! tablemode#align#alignments(lnum, ncols) "{{{2
function! tablemode#align#sid() "{{{2 let alignments = []
return maparg('<sid>', 'n') if tablemode#table#IsBorder(a:lnum+1)
endfunction let hcols = tablemode#align#Split(getline(a:lnum+1), '[' . g:table_mode_corner . g:table_mode_corner_corner . ']')
nnoremap <sid> <sid> for idx in range(len(hcols))
" Right align if header
function! tablemode#align#scope() "{{{2 call add(alignments, 'l')
return s: if hcols[idx] =~# g:table_mode_align_char . '$' | let alignments[idx] = 'r' | endif
if hcols[idx] !~# '[^0-9\.]' | let alignments[idx] = 'r' | endif
endfor
end
return alignments
endfunction endfunction
function! tablemode#align#Align(lines) "{{{2 function! tablemode#align#Align(lines) "{{{2
let lines = map(a:lines, 's:Split(v:val, g:table_mode_separator)') 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
if len(line) <= 1 | continue | endif let stext = line.text
if len(stext) <= 1 | continue | endif
if line[0] !~ tablemode#table#StartExpr() if stext[0] !~ tablemode#table#StartExpr()
let line[0] = s:StripTrailingSpaces(line[0]) let stext[0] = s:StripTrailingSpaces(stext[0])
endif endif
if len(line) >= 2 if len(stext) >= 2
for i in range(1, len(line)-1) for i in range(1, len(stext)-1)
let line[i] = tablemode#utils#strip(line[i]) let stext[i] = tablemode#utils#strip(stext[i])
endfor endfor
endif endif
endfor endfor
let maxes = [] let maxes = []
for line in lines for line in lines
if len(line) <= 1 | continue | endif let stext = line.text
for i in range(len(line)) if len(stext) <= 1 | continue | endif
for i in range(len(stext))
if i == len(maxes) if i == len(maxes)
let maxes += [ s:Strlen(line[i]) ] let maxes += [ tablemode#utils#StrDisplayWidth(stext[i]) ]
else else
let maxes[i] = max([ maxes[i], s:Strlen(line[i]) ]) let maxes[i] = max([ maxes[i], tablemode#utils#StrDisplayWidth(stext[i]) ])
endif endif
endfor endfor
endfor endfor
let alignments = tablemode#align#alignments(lines[0].lnum, len(lines[0].text))
for idx in range(len(lines)) for idx in range(len(lines))
let line = lines[idx] let tlnum = lines[idx].lnum
let tline = lines[idx].text
if len(line) <= 1 | continue | endif if len(tline) <= 1 | continue | endif
for i in range(len(line)) for jdx in range(len(tline))
if line[i] !~# '[^0-9\.]' " Dealing with the header being the first line
let field = s:Padding(line[i], maxes[i], 'r') if jdx >= len(alignments) | call add(alignments, 'l') | endif
else let field = s:Padding(tline[jdx], maxes[jdx], alignments[jdx])
let field = s:Padding(line[i], maxes[i], 'l') let tline[jdx] = field . (jdx == 0 || jdx == len(tline) ? '' : ' ')
endif
let line[i] = field . (i == 0 || i == len(line) ? '' : ' ')
endfor endfor
let lines[idx] = s:StripTrailingSpaces(join(line, '')) let lines[idx].text = s:StripTrailingSpaces(join(tline, ''))
endfor endfor
return lines return lines

View File

@@ -47,50 +47,50 @@ function! tablemode#spreadsheet#scope() "{{{2
endfunction endfunction
function! tablemode#spreadsheet#GetFirstRow(line) "{{{2 function! tablemode#spreadsheet#GetFirstRow(line) "{{{2
if tablemode#table#IsATableRow(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#IsATableRow(line - 1) || tablemode#table#IsATableHeader(line - 1) while tablemode#table#IsRow(line - 1) || tablemode#table#IsBorder(line - 1)
let line -= 1 let line -= 1
endwhile endwhile
if tablemode#table#IsATableHeader(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#MoveToFirstRow() "{{{2 function! tablemode#spreadsheet#MoveToFirstRow() "{{{2
if tablemode#table#IsATableRow('.') if tablemode#table#IsRow('.')
call cursor(tablemode#spreadsheet#GetFirstRow('.'), col('.')) call cursor(tablemode#spreadsheet#GetFirstRow('.'), col('.'))
endif endif
endfunction endfunction
function! tablemode#spreadsheet#GetLastRow(line) "{{{2 function! tablemode#spreadsheet#GetLastRow(line) "{{{2
if tablemode#table#IsATableRow(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#IsATableRow(line + 1) || tablemode#table#IsATableHeader(line + 1) while tablemode#table#IsRow(line + 1) || tablemode#table#IsBorder(line + 1)
let line += 1 let line += 1
endwhile endwhile
if tablemode#table#IsATableHeader(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#MoveToLastRow() "{{{2 function! tablemode#spreadsheet#MoveToLastRow() "{{{2
if tablemode#table#IsATableRow('.') if tablemode#table#IsRow('.')
call cursor(tablemode#spreadsheet#GetLastRow('.'), col('.')) call cursor(tablemode#spreadsheet#GetLastRow('.'), col('.'))
endif endif
endfunction endfunction
function! tablemode#spreadsheet#LineNr(row) "{{{2 function! tablemode#spreadsheet#LineNr(row) "{{{2
if tablemode#table#IsATableRow('.') if tablemode#table#IsRow('.')
let line = tablemode#spreadsheet#GetFirstRow('.') let line = tablemode#spreadsheet#GetFirstRow('.')
let row_nr = 0 let row_nr = 0
while tablemode#table#IsATableRow(line + 1) || tablemode#table#IsATableHeader(line + 1) while tablemode#table#IsRow(line + 1) || tablemode#table#IsBorder(line + 1)
if tablemode#table#IsATableRow(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
endif endif
@@ -105,8 +105,8 @@ 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#IsATableRow(line) || tablemode#table#IsATableHeader(line) while tablemode#table#IsRow(line) || tablemode#table#IsBorder(line)
if tablemode#table#IsATableRow(line) | let rowNr += 1 | endif if tablemode#table#IsRow(line) | let rowNr += 1 | endif
let line -= 1 let line -= 1
endwhile endwhile
@@ -117,14 +117,14 @@ 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#IsATableRow(tline) || tablemode#table#IsATableHeader(tline) while tablemode#table#IsRow(tline) || tablemode#table#IsBorder(tline)
if tablemode#table#IsATableRow(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#IsATableRow(tline) || tablemode#table#IsATableHeader(tline) while tablemode#table#IsRow(tline) || tablemode#table#IsBorder(tline)
if tablemode#table#IsATableRow(tline) | let totalRowCount += 1 | endif if tablemode#table#IsRow(tline) | let totalRowCount += 1 | endif
let tline += 1 let tline += 1
endwhile endwhile
@@ -166,34 +166,34 @@ function! tablemode#spreadsheet#MoveToStartOfCell() "{{{2
endif endif
endfunction endfunction
function! tablemode#spreadsheet#GetLastRow(line) "{{{2 function! tablemode#spreadsheet#GetFirstRow(line) "{{{2
if tablemode#table#IsATableRow(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#IsATableRow(line + 1) || tablemode#table#IsATableHeader(line + 1) while tablemode#table#IsRow(line - 1) || tablemode#table#IsBorder(line - 1)
let line += 1 let line -= 1
endwhile endwhile
if tablemode#table#IsATableHeader(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#IsATableRow(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#IsATableRow(line - 1) || tablemode#table#IsATableHeader(line - 1) while tablemode#table#IsRow(line + 1) || tablemode#table#IsBorder(line + 1)
let line -= 1 let line += 1
endwhile endwhile
if tablemode#table#IsATableHeader(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#DeleteColumn() "{{{2 function! tablemode#spreadsheet#DeleteColumn() "{{{2
if tablemode#table#IsATableRow('.') if tablemode#table#IsRow('.')
for i in range(v:count1) for i in range(v:count1)
call tablemode#spreadsheet#MoveToStartOfCell() call tablemode#spreadsheet#MoveToStartOfCell()
call tablemode#spreadsheet#MoveToFirstRow() call tablemode#spreadsheet#MoveToFirstRow()
@@ -202,23 +202,23 @@ function! tablemode#spreadsheet#DeleteColumn() "{{{2
normal! d normal! d
endfor endfor
call tablemode#table#TableRealign('.') call tablemode#table#Realign('.')
endif endif
endfunction endfunction
function! tablemode#spreadsheet#DeleteRow() "{{{2 function! tablemode#spreadsheet#DeleteRow() "{{{2
if tablemode#table#IsATableRow('.') if tablemode#table#IsRow('.')
for i in range(v:count1) for i in range(v:count1)
if tablemode#table#IsATableRow('.') if tablemode#table#IsRow('.')
normal! dd normal! dd
endif endif
if !tablemode#table#IsATableRow('.') if !tablemode#table#IsRow('.')
normal! k normal! k
endif endif
endfor endfor
call tablemode#table#TableRealign('.') call tablemode#table#Realign('.')
endif endif
endfunction endfunction
@@ -234,14 +234,3 @@ function! tablemode#spreadsheet#Average(range, ...) abort "{{{2
return s:Average(call('tablemode#spreadsheet#cell#GetCellRange', args)) return s:Average(call('tablemode#spreadsheet#cell#GetCellRange', args))
endfunction endfunction
function! tablemode#spreadsheet#CellTextObject(inner) "{{{2
if tablemode#table#IsATableRow('.')
call tablemode#spreadsheet#MoveToStartOfCell()
if a:inner
normal! v
call search('[^' . g:table_mode_separator . ']\ze\s*' . g:table_mode_separator)
else
execute 'normal! vf' . g:table_mode_separator . 'l'
endif
endif
endfunction

View File

@@ -71,7 +71,7 @@ endfunction
function! tablemode#spreadsheet#cell#GetCells(line, ...) abort function! tablemode#spreadsheet#cell#GetCells(line, ...) abort
let line = tablemode#utils#line(a:line) let line = tablemode#utils#line(a:line)
if tablemode#table#IsATableRow(line) if tablemode#table#IsRow(line)
if a:0 < 1 if a:0 < 1
let [row, colm] = [line, 0] let [row, colm] = [line, 0]
elseif a:0 < 2 elseif a:0 < 2
@@ -81,11 +81,12 @@ 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#IsATableRow(line) || tablemode#table#IsATableHeader(line) while tablemode#table#IsRow(line) || tablemode#table#IsBorder(line)
if tablemode#table#IsATableRow(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, '')))
endif endif
@@ -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#IsATableRow(line) || tablemode#table#IsATableHeader(line) let line = row > 0 ? first_row : last_row
if tablemode#table#IsATableRow(line) while tablemode#table#IsRow(line) || tablemode#table#IsBorder(line)
let row_nr += 1 if tablemode#table#IsRow(line)
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('.')]
@@ -142,7 +161,7 @@ function! tablemode#spreadsheet#cell#GetCellRange(range, ...) abort "{{{2
let values = [] let values = []
if tablemode#table#IsATableRow(line) if tablemode#table#IsRow(line)
let [row1, col1, row2, col2] = s:ParseRange(a:range, colm) let [row1, col1, row2, col2] = s:ParseRange(a:range, colm)
if row1 == row2 if row1 == row2
@@ -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,7 +195,11 @@ function! tablemode#spreadsheet#cell#SetCell(val, ...) "{{{2
let [line, row, colm] = a:000 let [line, row, colm] = a:000
endif endif
if tablemode#table#IsATableRow(line) " 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)
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)
let cstartexpr = tablemode#table#StartCommentExpr() let cstartexpr = tablemode#table#StartCommentExpr()
@@ -207,11 +213,11 @@ function! tablemode#spreadsheet#cell#SetCell(val, ...) "{{{2
let line_value = sce . line_value . ece let line_value = sce . line_value . ece
endif endif
call setline(line, line_value) call setline(line, line_value)
call tablemode#table#TableRealign(line) call tablemode#table#Realign(line)
endif endif
endfunction endfunction
function! tablemode#spreadsheet#cell#TextObject(inner) "{{{2 function! tablemode#spreadsheet#cell#TextObject(inner) "{{{2
if tablemode#table#IsATableRow('.') if tablemode#table#IsRow('.')
call tablemode#spreadsheet#MoveToStartOfCell() call tablemode#spreadsheet#MoveToStartOfCell()
if a:inner if a:inner
normal! v normal! v
@@ -223,11 +229,11 @@ function! tablemode#spreadsheet#cell#TextObject(inner) "{{{2
endfunction endfunction
function! tablemode#spreadsheet#cell#Motion(direction, ...) "{{{2 function! tablemode#spreadsheet#cell#Motion(direction, ...) "{{{2
let l:count = a:0 ? a:1 : v:count1 let l:count = a:0 ? a:1 : v:count1
if tablemode#table#IsATableRow('.') if tablemode#table#IsRow('.')
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#IsATableRow(line('.') + 1) && (tablemode#table#IsATableHeader(line('.') + 1) && !tablemode#table#IsATableRow(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#IsATableRow(line('.') - 1) && (tablemode#table#IsATableHeader(line('.') - 1) && !tablemode#table#IsATableRow(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)
@@ -256,18 +262,18 @@ function! tablemode#spreadsheet#cell#Motion(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#table#IsATableRow(line('.') + 1) if tablemode#table#IsRow(line('.') + 1)
" execute 'normal! ' . 1 . 'j' " execute 'normal! ' . 1 . 'j'
normal! j normal! j
elseif tablemode#table#IsATableHeader(line('.') + 1) && tablemode#table#IsATableRow(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
elseif a:direction ==# 'k' elseif a:direction ==# 'k'
if tablemode#table#IsATableRow(line('.') - 1) if tablemode#table#IsRow(line('.') - 1)
" execute 'normal! ' . 1 . 'k' " execute 'normal! ' . 1 . 'k'
normal! k normal! k
elseif tablemode#table#IsATableHeader(line('.') - 1) && tablemode#table#IsATableRow(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#IsATableHeader(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
@@ -98,7 +98,7 @@ function! tablemode#spreadsheet#formula#EvaluateExpr(expr, line) abort "{{{2
call tablemode#spreadsheet#cell#SetCell(eval(expr), line, row, colm) call tablemode#spreadsheet#cell#SetCell(eval(expr), line, row, colm)
else else
let [row, line] = [1, tablemode#spreadsheet#GetFirstRow(line)] let [row, line] = [1, tablemode#spreadsheet#GetFirstRow(line)]
while tablemode#table#IsATableRow(line) while tablemode#table#IsRow(line)
let texpr = expr let texpr = expr
if expr =~# '\$' if expr =~# '\$'
let texpr = substitute(texpr, '\$\(\d\+\)', let texpr = substitute(texpr, '\$\(\d\+\)',
@@ -126,17 +126,17 @@ function! tablemode#spreadsheet#formula#EvaluateFormulaLine() abort "{{{2
else else
let matchexpr = '^\s* tmf: \zs.*$' let matchexpr = '^\s* tmf: \zs.*$'
endif endif
if tablemode#table#IsATableRow('.') " 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#IsATableHeader(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#IsATableHeader(line) | let line -= 1 | endif if tablemode#table#IsBorder(line) | let line -= 1 | endif
if tablemode#table#IsATableRow(line) if tablemode#table#IsRow(line)
let exprs = split(matchstr(getline('.'), matchexpr), ';') let exprs = split(matchstr(getline('.'), matchexpr), ';')
endif endif
endif endif

View File

@@ -21,13 +21,13 @@
function! s:HeaderBorderExpr() "{{{2 function! s:HeaderBorderExpr() "{{{2
return tablemode#table#StartExpr() . return tablemode#table#StartExpr() .
\ '[' . g:table_mode_corner . g:table_mode_corner_corner . ']' . \ '[' . g:table_mode_corner . g:table_mode_corner_corner . ']' .
\ '[' . g:table_mode_fillchar . g:table_mode_corner . ']*' . \ '[' . g:table_mode_fillchar . g:table_mode_corner . g:table_mode_align_char . ']*' .
\ '[' . g:table_mode_corner . g:table_mode_corner_corner . ']' . \ '[' . g:table_mode_corner . g:table_mode_corner_corner . ']' .
\ tablemode#table#EndExpr() \ tablemode#table#EndExpr()
endfunction endfunction
function! s:DefaultHeaderBorder() "{{{2 function! s:DefaultHeaderBorder() "{{{2
if s:IsTableModeActive() if tablemode#IsActive()
return g:table_mode_corner_corner . g:table_mode_fillchar . g:table_mode_corner . g:table_mode_fillchar . g:table_mode_corner_corner return g:table_mode_corner_corner . g:table_mode_fillchar . g:table_mode_corner . g:table_mode_fillchar . g:table_mode_corner_corner
else else
return '' return ''
@@ -36,19 +36,40 @@ endfunction
function! s:GenerateHeaderBorder(line) "{{{2 function! s:GenerateHeaderBorder(line) "{{{2
let line = tablemode#utils#line(a:line) let line = tablemode#utils#line(a:line)
if tablemode#table#IsATableRow(line - 1) || tablemode#table#IsATableRow(line + 1) if tablemode#table#IsRow(line - 1) || tablemode#table#IsRow(line + 1)
let line_val = '' let line_val = ''
if tablemode#table#IsATableRow(line + 1) if tablemode#table#IsRow(line + 1)
let line_val = getline(line + 1) let line_val = getline(line + 1)
endif endif
if tablemode#table#IsATableRow(line - 1) && tablemode#utils#strlen(line_val) < tablemode#utils#strlen(getline(line - 1)) if tablemode#table#IsRow(line - 1) && tablemode#utils#strlen(line_val) < tablemode#utils#strlen(getline(line - 1))
let line_val = getline(line - 1) let line_val = getline(line - 1)
endif endif
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
if getline(line) =~# g:table_mode_align_char
let pat = '[' . g:table_mode_corner_corner . g:table_mode_corner . ']'
let hcols = tablemode#align#Split(getline(line), pat)
let gcols = tablemode#align#Split(border, pat)
for idx in range(len(hcols))
if hcols[idx] =~# g:table_mode_align_char
if hcols[idx] =~# g:table_mode_align_char . '$'
let gcols[idx] = gcols[idx][:-2] . g:table_mode_align_char
else
let gcols[idx] = g:table_mode_align_char . gcols[idx][1:]
endif
endif
endfor
let border = join(gcols, '')
endif
let cstartexpr = tablemode#table#StartCommentExpr() let cstartexpr = tablemode#table#StartCommentExpr()
if tablemode#utils#strlen(cstartexpr) > 0 && getline(line) =~# cstartexpr if tablemode#utils#strlen(cstartexpr) > 0 && getline(line) =~# cstartexpr
let sce = matchstr(line_val, tablemode#table#StartCommentExpr()) let sce = matchstr(line_val, tablemode#table#StartCommentExpr())
@@ -75,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
@@ -102,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
@@ -116,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
@@ -134,109 +155,56 @@ function! tablemode#table#EndExpr() "{{{2
endif endif
endfunction endfunction
function! tablemode#table#IsATableRow(line) "{{{2 function! tablemode#table#IsBorder(line) "{{{2
return getline(a:line) =~# (tablemode#table#StartExpr() . g:table_mode_separator . '[^' .
\ g:table_mode_fillchar . ']*[^' . g:table_mode_corner . ']*$')
endfunction
function! tablemode#table#IsATableHeader(line) "{{{2
return getline(a:line) =~# s:HeaderBorderExpr() return getline(a:line) =~# s:HeaderBorderExpr()
endfunction endfunction
function! tablemode#table#AddHeaderBorder(line) "{{{2 function! tablemode#table#IsHeader(line) "{{{2
let line = tablemode#utils#line(a:line)
return tablemode#table#IsBorder(line+1) && !tablemode#table#IsRow(line-1) && !tablemode#table#IsRow(line-2)
endfunction
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
function! tablemode#table#TableRealign(line) "{{{2 function! tablemode#table#Realign(line) "{{{2
let line = tablemode#utils#line(a:line) let line = tablemode#utils#line(a:line)
let [lnums, lines] = [[], []] let lines = []
let [tline, blines] = [line, []] let [lnum, blines] = [line, []]
while tablemode#table#IsATableRow(tline) || tablemode#table#IsATableHeader(tline) while tablemode#table#IsRow(lnum) || tablemode#table#IsBorder(lnum)
if tablemode#table#IsATableHeader(tline) if tablemode#table#IsBorder(lnum)
call insert(blines, tline) call insert(blines, lnum)
let tline -= 1 let lnum -= 1
continue continue
endif endif
call insert(lnums, tline) call insert(lines, {'lnum': lnum, 'text': getline(lnum)})
call insert(lines, getline(tline)) let lnum -= 1
let tline -= 1
endwhile endwhile
let tline = line + 1 let lnum = line + 1
while tablemode#table#IsRow(lnum) || tablemode#table#IsBorder(lnum)
while tablemode#table#IsATableRow(tline) || tablemode#table#IsATableHeader(tline) if tablemode#table#IsBorder(lnum)
if tablemode#table#IsATableHeader(tline) call add(blines, lnum)
call insert(blines, tline) let lnum += 1
let tline += 1
continue continue
endif endif
call add(lnums, tline) call add(lines, {'lnum': lnum, 'text': getline(lnum)})
call add(lines, getline(tline)) let lnum += 1
let tline += 1
endwhile endwhile
let lines = tablemode#align#Align(lines) let lines = tablemode#align#Align(lines)
for lnum in lnums for aline in lines
let index = index(lnums, lnum) call setline(aline.lnum, aline.text)
call setline(lnum, lines[index])
endfor endfor
for bline in blines for bline in blines
call tablemode#table#AddHeaderBorder(bline) call tablemode#table#AddBorder(bline)
endfor endfor
endfunction endfunction
function! tablemode#table#TableMotion(direction, ...) "{{{2
let l:count = a:0 ? a:1 : v:count1
if tablemode#table#IsATableRow('.')
for ii in range(l:count)
if a:direction ==# 'l'
if tablemode#spreadsheet#IsLastCell()
if !tablemode#table#IsATableRow(line('.') + 1) && (tablemode#table#IsATableHeader(line('.') + 1) && !tablemode#table#IsATableRow(line('.') + 2 * 1))
return
endif
call tablemode#table#TableMotion('j', 1)
normal! 0
endif
" If line starts with g:table_mode_separator
if getline('.')[col('.')-1] ==# g:table_mode_separator
normal! 2l
else
execute 'normal! f' . g:table_mode_separator . '2l'
endif
elseif a:direction ==# 'h'
if tablemode#spreadsheet#IsFirstCell()
if !tablemode#table#IsATableRow(line('.') - 1) && (tablemode#table#IsATableHeader(line('.') - 1) && !tablemode#table#IsATableRow(line('.') - 2 * 1))
return
endif
call tablemode#table#TableMotion('k', 1)
normal! $
endif
" If line ends with g:table_mode_separator
if getline('.')[col('.')-1] ==# g:table_mode_separator
execute 'normal! F' . g:table_mode_separator . '2l'
else
execute 'normal! 2F' . g:table_mode_separator . '2l'
endif
elseif a:direction ==# 'j'
if tablemode#table#IsATableRow(line('.') + 1)
execute 'normal! ' . 1 . 'j'
elseif tablemode#table#IsATableHeader(line('.') + 1) && tablemode#table#IsATableRow(line('.') + 2)
execute 'normal! ' . (1 + 1) . 'j'
endif
elseif a:direction ==# 'k'
if tablemode#table#IsATableRow(line('.') - 1)
execute 'normal! ' . 1 . 'k'
elseif tablemode#table#IsATableHeader(line('.') - 1) && tablemode#table#IsATableRow(line('.') - 2)
execute 'normal! ' . (1 + 1) . 'k'
endif
endif
endfor
endif
endfunction
" vim: sw=2 sts=2 fdl=0 fdm=marker

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.2.0 *table-mode.txt* Table Mode for easy table formatting. v4.4.1
=============================================================================== ===============================================================================
Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER
VERSION 4.2.0 VERSION 4.4.1
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/>
@@ -34,6 +34,11 @@ Create Table on the fly:
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.
Table Mode now supports gfm style tables and allows to define column
alignments with the aid of placing ':' (configured using
|g:table_mode_align_char|) on the table header border appropriately to
align content left or right.
The table mode is disabled by default and you can enter table mode The table mode is disabled by default and you can enter table mode
using |table-mode-toggle-map| or you can also enable it permanently using |table-mode-toggle-map| or you can also enable it permanently
using |table-mode-always-active| if you wish though not recommended. using |table-mode-always-active| if you wish though not recommended.
@@ -99,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.
@@ -111,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).
@@ -139,6 +147,10 @@ Overview:
|table-mode-delimiter| .......... Set the delimiter for Tableize. |table-mode-delimiter| .......... Set the delimiter for Tableize.
|table-mode-corner-corner| ...... Set the character to be used for |table-mode-corner-corner| ...... Set the character to be used for
extreme corners of the table border. extreme corners of the table border.
|table-mode-align-char|.......... Set the alignment character which
can be added to the table header
border to control alignment of that
column.
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: >
@@ -196,6 +208,11 @@ g:table_mode_corner_corner *table-mode-corner-corner*
corners of the table border. > corners of the table border. >
let g:table_mode_corner_corner = '|' let g:table_mode_corner_corner = '|'
< <
g:table_mode_align_char *table-mode-align-char*
Use this option to define the character to be used for defining
alignments for columns in the table header border. >
let g:table_mode_align_char = ':'
<
=============================================================================== ===============================================================================
MAPPINGS *table-mode-mappings* MAPPINGS *table-mode-mappings*

View File

@@ -42,20 +42,38 @@ call s:SetGlobalOptDefault('table_mode_toggle_map', 'm')
call s:SetGlobalOptDefault('table_mode_always_active', 0) call s:SetGlobalOptDefault('table_mode_always_active', 0)
call s:SetGlobalOptDefault('table_mode_delimiter', ',') call s:SetGlobalOptDefault('table_mode_delimiter', ',')
call s:SetGlobalOptDefault('table_mode_corner_corner', '|') call s:SetGlobalOptDefault('table_mode_corner_corner', '|')
call s:SetGlobalOptDefault('table_mode_align_char', ':')
function! s:TableEchoCell() "{{{1 function! s:TableEchoCell() "{{{1
if tablemode#table#IsATableRow('.') if tablemode#table#IsRow('.')
echomsg '$' . tablemode#spreadsheet#RowNr('.') . ',' . tablemode#spreadsheet#ColumnNr('.') echomsg '$' . tablemode#spreadsheet#RowNr('.') . ',' . tablemode#spreadsheet#ColumnNr('.')
endif endif
endfunction endfunction
function! s:EnableTableSyntax() "{{{1
syntax match Table /^\s*|.\+|\s*$/ contains=TableBorder,TableSeparator,TableColumnAlign containedin=ALL
syntax match TableSeparator /|/ contained
syntax match TableColumnAlign /:/ contained
syntax match TableBorder /[\-+]\+/ contained
endfunction
augroup TableMode
au!
autocmd Syntax * call <SID>EnableTableSyntax()
augroup END
hi! link TableBorder Delimiter
hi! link TableSeparator Delimiter
hi! link TableColumnAlign Type
" 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 .
\ " <Esc>:call tablemode#TableModeToggle()<CR>" \ " <Esc>:call tablemode#Toggle()<CR>"
command! -nargs=0 TableModeToggle call tablemode#TableModeToggle() command! -nargs=0 TableModeToggle call tablemode#Toggle()
command! -nargs=0 TableModeEnable call tablemode#TableModeEnable() command! -nargs=0 TableModeEnable call tablemode#Enable()
command! -nargs=0 TableModeDisable call tablemode#TableModeDisable() command! -nargs=0 TableModeDisable call tablemode#Disable()
else else
let table_mode_separator_map = g:table_mode_separator let table_mode_separator_map = g:table_mode_separator
" '|' is a special character, we need to map <Bar> instead " '|' is a special character, we need to map <Bar> instead
@@ -67,16 +85,16 @@ else
endif endif
" }}}2 " }}}2
command! -nargs=? -range Tableize <line1>,<line2>call tablemode#table#TableizeRange(<q-args>) command! -nargs=? -range Tableize <line1>,<line2>call tablemode#TableizeRange(<q-args>)
command! TableAddFormula call tablemode#spreadsheet#formula#Add() command! TableAddFormula call tablemode#spreadsheet#formula#Add()
command! TableModeRealign call tablemode#spreadsheet#TableRealign('.') command! TableModeRealign call tablemode#table#Realign('.')
command! TableEvalFormulaLine call tablemode#spreadsheet#formula#EvaluateFormulaLine() command! TableEvalFormulaLine call tablemode#spreadsheet#formula#EvaluateFormulaLine()
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>
nnoremap <silent> <Plug>(table-mode-realign) :call tablemode#table#TableRealign('.')<CR> nnoremap <silent> <Plug>(table-mode-realign) :call tablemode#table#Realign('.')<CR>
nnoremap <silent> <Plug>(table-mode-motion-up) :<C-U>call tablemode#spreadsheet#cell#Motion('k')<CR> nnoremap <silent> <Plug>(table-mode-motion-up) :<C-U>call tablemode#spreadsheet#cell#Motion('k')<CR>
nnoremap <silent> <Plug>(table-mode-motion-down) :<C-U>call tablemode#spreadsheet#cell#Motion('j')<CR> nnoremap <silent> <Plug>(table-mode-motion-down) :<C-U>call tablemode#spreadsheet#cell#Motion('j')<CR>

View File

@@ -3,12 +3,20 @@ source t/config/options.vim
call vspec#hint({'scope': 'tablemode#align#scope()', 'sid': 'tablemode#align#sid()'}) call vspec#hint({'scope': 'tablemode#align#scope()', 'sid': 'tablemode#align#sid()'})
function! ConvertLines2Dict(lines)
let lines = []
for idx in range(len(a:lines))
call insert(lines, {"lnum": idx+1, "text": a:lines[idx]})
endfor
return lines
endfunction
describe 'Align' describe 'Align'
it 'should align table content correctly' it 'should align table content correctly'
Expect tablemode#align#Align(readfile('t/fixtures/align/simple_before.txt')) == readfile('t/fixtures/align/simple_after.txt') Expect tablemode#align#Align(ConvertLines2Dict(readfile('t/fixtures/align/simple_before.txt'))) == ConvertLines2Dict(readfile('t/fixtures/align/simple_after.txt'))
end end
it 'should align table content with unicode characters correctly' it 'should align table content with unicode characters correctly'
Expect tablemode#align#Align(readfile('t/fixtures/align/unicode_before.txt')) == readfile('t/fixtures/align/unicode_after.txt') Expect tablemode#align#Align(ConvertLines2Dict(readfile('t/fixtures/align/unicode_before.txt'))) == ConvertLines2Dict(readfile('t/fixtures/align/unicode_after.txt'))
end end
end end

View File

@@ -3,13 +3,14 @@ source t/config/options.vim
call vspec#hint({'scope': 'tablemode#spreadsheet#cell#scope()', 'sid': 'tablemode#spreadsheet#cell#sid()'}) call vspec#hint({'scope': 'tablemode#spreadsheet#cell#scope()', 'sid': 'tablemode#spreadsheet#cell#sid()'})
describe 'cell API' describe 'cell'
describe 'API'
before before
new new
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']
@@ -19,7 +20,17 @@ describe 'cell API'
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']]
@@ -46,4 +57,83 @@ describe 'cell API'
Expect tablemode#spreadsheet#cell#GetCellRange('1:-1', 2, 2) == ['test12'] Expect tablemode#spreadsheet#cell#GetCellRange('1:-1', 2, 2) == ['test12']
Expect tablemode#spreadsheet#cell#GetCellRange('1:-1', 3, 2) == ['test12'] Expect tablemode#spreadsheet#cell#GetCellRange('1:-1', 3, 2) == ['test12']
end end
end
describe 'Motions'
describe 'left or right'
before
new
normal! ggdG
call tablemode#Enable()
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#spreadsheet#ColumnNr('.') == 2
call tablemode#spreadsheet#cell#Motion('h')
Expect tablemode#spreadsheet#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#spreadsheet#RowNr('.') == 2
Expect tablemode#spreadsheet#ColumnNr('.') == 1
call tablemode#spreadsheet#cell#Motion('h')
Expect tablemode#spreadsheet#RowNr('.') == 1
Expect tablemode#spreadsheet#ColumnNr('.') == 2
end
it 'should move right when not on last column'
Expect tablemode#spreadsheet#ColumnNr('.') == 1
call tablemode#spreadsheet#cell#Motion('l')
Expect tablemode#spreadsheet#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#spreadsheet#RowNr('.') == 1
Expect tablemode#spreadsheet#ColumnNr('.') == 2
call tablemode#spreadsheet#cell#Motion('l')
Expect tablemode#spreadsheet#RowNr('.') == 2
Expect tablemode#spreadsheet#ColumnNr('.') == 1
end
end
describe 'up or down'
before
new
normal! ggdG
call tablemode#Enable()
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#spreadsheet#RowNr('.') == 2
call tablemode#spreadsheet#cell#Motion('k')
Expect tablemode#spreadsheet#RowNr('.') == 1
end
it 'should remain on first row when trying to move up'
Expect tablemode#spreadsheet#RowNr('.') == 1
call tablemode#spreadsheet#cell#Motion('k')
Expect tablemode#spreadsheet#RowNr('.') == 1
end
it 'should move a row down unless on last row'
Expect tablemode#spreadsheet#RowNr('.') == 1
call tablemode#spreadsheet#cell#Motion('j')
Expect tablemode#spreadsheet#RowNr('.') == 2
end
it 'should remain on last row when trying to move down'
Expect tablemode#spreadsheet#RowNr('.') == 1
call tablemode#spreadsheet#cell#Motion('k')
Expect tablemode#spreadsheet#RowNr('.') == 1
end
end end

View File

@@ -6,3 +6,4 @@ let g:table_mode_toggle_map = 'm'
let g:table_mode_always_active = 0 let g:table_mode_always_active = 0
let g:table_mode_delimiter = ',' let g:table_mode_delimiter = ','
let g:table_mode_corner_corner = '|' let g:table_mode_corner_corner = '|'
let g:table_mode_align_char = ':'

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

@@ -0,0 +1,7 @@
|--------+---------|
| Title | Message |
|--------+---------|
| test11 | test12 |
| test21 | test22 |
|--------+---------|

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,11 +55,40 @@ 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! ggdG
call tablemode#Enable()
normal i|test11|test12| normal i|test11|test12|
|test21|test22| |test21|test22|
call cursor(1, 3) call cursor(1, 3)

View File

@@ -4,116 +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'
before
new
read t/fixtures/sample.txt
end
it 'should return true when inside a table'
Expect tablemode#table#IsATableRow(2) to_be_true
end
it 'should return false when outside a table'
Expect tablemode#table#IsATableRow(4) to_be_false
end
end
describe 'Tableize'
before
new
read t/fixtures/tableize.txt
end
it 'should tableize with default delimiter'
:2,3call tablemode#TableizeRange('')
Expect tablemode#table#IsATableRow(2) to_be_true
Expect tablemode#spreadsheet#RowCount(2) == 2
Expect tablemode#spreadsheet#ColumnCount(2) == 3
end
it 'should tableize with given delimiter'
:2,3call tablemode#TableizeRange('/;')
Expect tablemode#table#IsATableRow(2) to_be_true
Expect tablemode#spreadsheet#RowCount(2) == 2
Expect tablemode#spreadsheet#ColumnCount(2) == 2
end
end
describe 'Motions'
describe 'left or right'
before before
new new
normal! ggdG normal! ggdG
call tablemode#TableModeEnable() read t/fixtures/table/sample.txt
normal i|test11|test12|
|test21|test22|
end end
it 'should be true when on a table row'
it 'should move left when not on first column' Expect tablemode#table#IsRow(2) to_be_true
call cursor(1, 12) Expect tablemode#table#IsRow(3) to_be_true
Expect tablemode#spreadsheet#ColumnNr('.') == 2
call tablemode#table#TableMotion('h')
end end
it 'should be false when not on a table row'
it 'should move to the previous row last column if it exists when on first column' Expect tablemode#table#IsRow(1) to_be_false
call cursor(2, 3) Expect tablemode#table#IsRow(4) to_be_false
Expect tablemode#spreadsheet#RowNr('.') == 2
Expect tablemode#spreadsheet#ColumnNr('.') == 1
call tablemode#table#TableMotion('h')
Expect tablemode#spreadsheet#RowNr('.') == 1
Expect tablemode#spreadsheet#ColumnNr('.') == 2
end
it 'should move right when not on last column'
Expect tablemode#spreadsheet#ColumnNr('.') == 1
call tablemode#table#TableMotion('l')
Expect tablemode#spreadsheet#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#spreadsheet#RowNr('.') == 1
Expect tablemode#spreadsheet#ColumnNr('.') == 2
call tablemode#table#TableMotion('l')
Expect tablemode#spreadsheet#RowNr('.') == 2
end end
end end
describe 'IsBorder'
before before
new new
normal! ggdG normal! ggdG
normal! ggdG read t/fixtures/table/sample_with_header.txt
call tablemode#TableModeEnable()
normal i|test11|test12|
end end
end it 'should be true on a table border'
Expect tablemode#table#IsBorder(1) to_be_true
it 'should move a row up unless on first row' Expect tablemode#table#IsBorder(3) to_be_true
call cursor(2, 3) Expect tablemode#table#IsBorder(6) to_be_true
Expect tablemode#spreadsheet#RowNr('.') == 2
end end
end it 'should be false when not on a table border'
Expect tablemode#table#IsBorder(2) to_be_false
it 'should remain on first row when trying to move up' Expect tablemode#table#IsBorder(4) to_be_false
Expect tablemode#spreadsheet#RowNr('.') == 1 Expect tablemode#table#IsBorder(5) to_be_false
end
end end
end describe 'IsHeader'
before
it 'should move a row down unless on last row' new
Expect tablemode#spreadsheet#RowNr('.') == 1 normal! ggdG
read t/fixtures/table/sample_with_header.txt
end end
end it 'should be true on the table header'
Expect tablemode#table#IsHeader(2) to_be_true
it 'should remain on last row when trying to move down' end
Expect tablemode#spreadsheet#RowNr('.') == 1
it 'should be false anywhere else'
Expect tablemode#table#IsHeader(1) to_be_false
Expect tablemode#table#IsHeader(4) 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

@@ -5,9 +5,9 @@ call vspec#hint({'scope': 'tablemode#scope()', 'sid': 'tablemode#sid()'})
describe 'tablemode' describe 'tablemode'
describe 'Activation' describe 'Activation'
describe 'tablemode#TableModeEnable()' describe 'tablemode#Enable()'
before before
call tablemode#TableModeEnable() call tablemode#Enable()
end end
it 'should enable table mode' it 'should enable table mode'
@@ -15,9 +15,9 @@ describe 'tablemode'
end end
end end
describe 'tablemode#TableModeDisable()' describe 'tablemode#Disable()'
before before
call tablemode#TableModeDisable() call tablemode#Disable()
end end
it 'should disable table mode' it 'should disable table mode'
@@ -25,13 +25,34 @@ describe 'tablemode'
end end
end end
describe 'tablemode#TableModeToggle()' describe 'tablemode#Toggle()'
it 'should toggle table mode' it 'should toggle table mode'
call tablemode#TableModeToggle() call tablemode#Toggle()
Expect b:table_mode_active to_be_true Expect b:table_mode_active to_be_true
call tablemode#TableModeToggle() call tablemode#Toggle()
Expect b:table_mode_active to_be_false Expect b:table_mode_active to_be_false
end end
end end
end end
describe 'Tableize'
before
new
read t/fixtures/tableize.txt
end
it 'should tableize with default delimiter'
:2,3call tablemode#TableizeRange('')
Expect tablemode#table#IsRow(2) to_be_true
Expect tablemode#spreadsheet#RowCount(2) == 2
Expect tablemode#spreadsheet#ColumnCount(2) == 3
end
it 'should tableize with given delimiter'
:2,3call tablemode#TableizeRange('/;')
Expect tablemode#table#IsRow(2) to_be_true
Expect tablemode#spreadsheet#RowCount(2) == 2
Expect tablemode#spreadsheet#ColumnCount(2) == 2
end
end
end end

View File

@@ -3,7 +3,8 @@ 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 'line' describe 'utils'
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('.')
end end
@@ -11,16 +12,16 @@ describe 'line'
it 'should return the line number itself if it is a number' it 'should return the line number itself if it is a number'
Expect tablemode#utils#line(1) == 1 Expect tablemode#utils#line(1) == 1
end end
end end
describe 'strip' describe 'strip'
it 'should strip all initial or trailing whitespace from a string' it 'should strip all initial or trailing whitespace from a string'
let string = ' This is awesome ' let string = ' This is awesome '
Expect tablemode#utils#strip(string) == 'This is awesome' Expect tablemode#utils#strip(string) == 'This is awesome'
end end
end end
describe 'strlen' describe 'strlen'
it 'should return the length of a string correctly' it 'should return the length of a string correctly'
let string = 'this is a test' let string = 'this is a test'
Expect tablemode#utils#strlen(string) == 14 Expect tablemode#utils#strlen(string) == 14
@@ -30,4 +31,17 @@ describe 'strlen'
let string = '測試 is good.' let string = '測試 is good.'
Expect tablemode#utils#strlen(string) == 11 Expect tablemode#utils#strlen(string) == 11
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 end