Compare commits

...

29 Commits
v3.0 ... v3.3.3

Author SHA1 Message Date
Dhruva Sagar
10ab0425f6 Fixes #17
Added a new configuration option g:table_mode_corner_corner to
correspond to the extreme corners of the table border, by default it is
set to '|' to maintain backward compatibility but it can be overriden to
anything else.
2014-01-29 11:32:30 +05:30
Dhruva Sagar
e10a2ab4da Updated README 2013-12-31 12:53:41 +05:30
Dhruva Sagar
496872f342 trying with <kbd> 2013-12-31 12:51:35 +05:30
Dhruva Sagar
af432f6ed5 Updated CHANGELOG 2013-12-31 12:34:42 +05:30
Dhruva Sagar
5c42d58d4b Updated table mode
- Bumped version
- Added new mapping \t? to echo current cells representation for
  defining formulas.
- Updated docs
2013-12-31 12:30:52 +05:30
Dhruva Sagar
1302782b55 Updated vim-flavor & tests 2013-12-31 12:22:22 +05:30
Dhruva Sagar
9935e57def Fixed motions 2013-12-11 16:15:07 +05:30
Dhruva Sagar
8993e2a454 Fixed tablemode#DeleteColumn 2013-12-11 15:50:32 +05:30
Dhruva Sagar
df41b7deff Fixed Travis Url in README 2013-12-11 15:22:52 +05:30
Dhruva Sagar
c88a913397 Fixed tests 2013-12-11 15:21:20 +05:30
Dhruva Sagar
8609ad4778 Releasing version 3.3.1
* Borders are now disregarded, so add as many as you'd like.
* Improved test coverage.
2013-12-11 12:48:25 +05:30
Dhruva Sagar
535dd9601a Fixed doc/table-mode.txt mixed indent 2013-11-13 08:49:40 +05:30
Dhruva Sagar
e94bbcc0b4 Switched to using || mapping for table border generation, more intuitive & create a minimal border on a new line without a table 2013-11-13 08:45:15 +05:30
Dhruva Sagar
a0618db019 Fixed #14, You can now have a top border (before header) and a bottom border as well (after last line) 2013-11-12 19:49:10 +05:30
Dhruva Sagar
176999f347 Allowing border creation above table row 2013-10-24 16:47:15 +05:30
Dhruva Sagar
5a6a9b3473 Fixed motions to wrap around edges incorporating table header 2013-10-07 15:07:55 +05:30
Dhruva Sagar
aa8817621c Fixed doc #11 2013-09-25 09:29:37 +05:30
Dhruva Sagar
54187d0be1 Updated Header Border to be like org-mode table header separator for consistency. 2013-09-19 16:53:28 +05:30
Dhruva Sagar
3e2eaeee5c Fixed delete column api with header changes 2013-09-19 15:03:13 +05:30
Dhruva Sagar
6dfc508af5 Fixed headerExpr to incorporate comments & spaces around header border 2013-09-19 13:55:07 +05:30
Dhruva Sagar
ed441f19a1 Added travis status to README 2013-09-19 13:26:42 +05:30
Dhruva Sagar
e4525c465a Fixed travis.yml file, issue with tabs 2013-09-19 13:24:20 +05:30
Dhruva Sagar
19bfa092a3 Updating CHANGELOG 2013-09-19 13:23:19 +05:30
Dhruva Sagar
f91ec2458f Updated README & CHANGELOG 2013-09-19 12:33:28 +05:30
Dhruva Sagar
0799523ffb Releasing v3.2
* Added tests to test various use cases
* Added .travis.yml for travis integration
2013-09-19 12:28:47 +05:30
Dhruva Sagar
9875a117a7 Fixed iabbrev 2013-09-19 07:04:12 +05:30
Dhruva Sagar
cca55e72ce Releasing Table Mode v3.1
* Removed table border and related options. You can now optionally have
  a header instead, simply add a table header and add a border to it
  triggered by the vim iabbrev '+-' on the line immidiately after the
  header and it will expand to the correct border. It will auto update
  as the table realigns with changes. Similar approach as followed by
  tables in org-mode.
* Fixed bug of incorrect indentation of a newly added formula line.
* Fixed bug in ConvertDelimiterToSeparator causing incorrect
  tableization of commented blocks of delimited content.
2013-09-19 02:10:49 +05:30
Dhruva Sagar
eed25aee91 Removed unnecessary closing markers for folds 2013-06-10 09:56:54 +05:30
Dhruva Sagar
0683cec58c Added more error checks in GetCells 2013-05-22 07:36:53 +05:30
12 changed files with 571 additions and 221 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
tags
.vim-flavor/

4
.travis.yml Normal file
View File

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

View File

@@ -1,4 +1,31 @@
# Change Log
## Version 3.3.2
* Added new mapping \t? to echo a cells representation for use while defining
formulas.
## Version 3.3.1
* Improved logic to ignore table borders (add as many as you'd like), the
first row is not treated special, it is row # 1. Keep that in mind while
defining Formulas
* Improved test coverage
## Version 3.3
* Dropped +- mapping to create table border instead now using ||
* You can now have a top table border (before header) as well as a bottom
table border.
## Version 3.2
* Added tests to test various use cases using <a
href='https://github.com/kana/vim-vspec'>Vspec</a>..
* Added travis integration for automated tests.
## Version 3.1
* Removed borders. You can now optionally create a table header by simply
adding a header border immidiately after the header line by using the
iabbrev trigger '+-'. Just type that on the line immidiately after the
header and press space / \<CR\> to complete the header border.
* Some Bug Fixes
## Version 3.0
* Removed dependence on Tabular and added code borrowed from Tabular for
aligning the table rows.

4
Gemfile Normal file
View File

@@ -0,0 +1,4 @@
source 'https://rubygems.org'
gem 'rake'
gem 'vim-flavor', '~> 1.1'

18
Gemfile.lock Normal file
View File

@@ -0,0 +1,18 @@
GEM
remote: https://rubygems.org/
specs:
blankslate (2.1.2.4)
parslet (1.5.0)
blankslate (~> 2.0)
rake (10.1.0)
thor (0.18.1)
vim-flavor (1.1.3)
parslet (~> 1.0)
thor (~> 0.14)
PLATFORMS
ruby
DEPENDENCIES
rake
vim-flavor (~> 1.1)

View File

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

13
Rakefile Normal file
View File

@@ -0,0 +1,13 @@
#!/usr/bin/env rake
task :ci => [:dump, :test]
task :dump do
sh 'vim --version'
end
task :test do
sh 'bundle exec vim-flavor test'
end
task :default => :test

1
VimFlavor.lock Normal file
View File

@@ -0,0 +1 @@
kana/vim-vspec (1.1.2)

View File

@@ -1,13 +1,12 @@
" ============================== Header ==================================={{{
" ============================== Header ======================================
" File: autoload/tablemode.vim
" Description: Table mode for vim for creating neat tables.
" Author: Dhruva Sagar <http://dhruvasagar.com/>
" License: MIT (http://www.opensource.org/licenses/MIT)
" Website: http://github.com/dhruvasagar/vim-table-mode
" Version: 3.0
" Version: 3.3.3
" Note: This plugin was heavily inspired by the 'CucumberTables.vim'
" (https://gist.github.com/tpope/287147) plugin by Tim Pope and
" uses a small amount of code from it.
" (https://gist.github.com/tpope/287147) plugin by Tim Pope.
"
" Copyright Notice:
" Permission is hereby granted to use and distribute this code,
@@ -17,7 +16,7 @@
" of any kind, either expressed or implied. In no event will
" the copyright holder be liable for any damamges resulting
" from the use of this software.
" ==========================================================================}}}
" =============================================================================
" Private Functions {{{1
@@ -25,30 +24,25 @@ if exists('g:autoloaded_table_mode') "{{{2
finish
endif
let g:autoloaded_table_mode = 1
" }}}2
function! s:throw(string) abort "{{{2
let v:errmsg = 'table-mode: ' . a:string
throw v:errmsg
endfunction
" }}}2
function! s:sub(str,pat,rep) abort "{{{2
return substitute(a:str,'\v\C'.a:pat,a:rep,'')
endfunction
" }}}2
function! s:gsub(str,pat,rep) abort "{{{2
return substitute(a:str,'\v\C'.a:pat,a:rep,'g')
endfunction
" }}}2
function! s:SetBufferOptDefault(opt, val) "{{{2
if !exists('b:' . a:opt)
let b:{a:opt} = a:val
endif
endfunction
" }}}2
function! s:Line(line) "{{{2
if type(a:line) == type('')
@@ -57,39 +51,34 @@ function! s:Line(line) "{{{2
return a:line
endif
endfunction
" }}}2
" function! s:Strlen(text) - Count multibyte characters accurately {{{2
" See :h strlen() for more details
function! s:Strlen(text)
return strlen(substitute(a:text, '.', 'x', 'g'))
endfunction
" }}}2
function! s:Strip(string) "{{{2
return matchstr(a:string, '^\s*\zs.\{-}\ze\s*$')
endfunction
" }}}2
function! s:Sum(list) "{{{2
let result = 0.0
for item in a:list
if type(item) == type(1) || type(item) == type(1.0)
let result = result + item
let result += item
elseif type(item) == type('')
let result = result + str2float(item)
let result += str2float(item)
elseif type(item) == type([])
let result = result + s:Sum(item)
let result += s:Sum(item)
endif
endfor
return result
endfunction
" }}}2
function! s:Average(list) "{{{2
return s:Sum(a:list)/len(a:list)
endfunction
" }}}2
function! s:GetCommentStart() "{{{2
let cstring = &commentstring
@@ -99,7 +88,6 @@ function! s:GetCommentStart() "{{{2
return ''
endif
endfunction
" }}}2
function! s:GetCommentEnd() "{{{2
let cstring = &commentstring
@@ -114,7 +102,6 @@ function! s:GetCommentEnd() "{{{2
return ''
endif
endfunction
" }}}2
function! s:StartExpr() "{{{2
let cstart = s:GetCommentStart()
@@ -124,7 +111,7 @@ function! s:StartExpr() "{{{2
return '^\s*'
endif
endfunction
" }}}2
function! s:EndExpr() "{{{2
let cend = s:GetCommentEnd()
@@ -134,7 +121,14 @@ function! s:EndExpr() "{{{2
return '\s*$'
endif
endfunction
" }}}2
function! s:HeaderBorderExpr() "{{{2
return s:StartExpr() .
\ '[' . g:table_mode_corner . g:table_mode_corner_corner . ']' .
\ '[' . g:table_mode_fillchar . g:table_mode_corner . ']*' .
\ '[' . g:table_mode_corner . g:table_mode_corner_corner . ']' .
\ s:EndExpr()
endfunction
function! s:StartCommentExpr() "{{{2
let cstartexpr = s:GetCommentStart()
@@ -144,7 +138,6 @@ function! s:StartCommentExpr() "{{{2
return ''
endif
endfunction
" }}}2
function! s:EndCommentExpr() "{{{2
let cendexpr = s:GetCommentEnd()
@@ -154,7 +147,6 @@ function! s:EndCommentExpr() "{{{2
return ''
endif
endfunction
" }}}2
function! s:IsTableModeActive() "{{{2
if g:table_mode_always_active | return 1 | endif
@@ -162,12 +154,11 @@ function! s:IsTableModeActive() "{{{2
call s:SetBufferOptDefault('table_mode_active', 0)
return b:table_mode_active
endfunction
" }}}2
function! s:RowGap() "{{{2
return g:table_mode_border ? 2 : 1
" Getting rid of borders, so the row gap is now just 1
return 1
endfunction
" }}}2
function! s:ToggleMapping() "{{{2
if exists('b:table_mode_active') && b:table_mode_active
@@ -181,60 +172,50 @@ function! s:ToggleMapping() "{{{2
execute "iunmap <silent> <buffer> " . b:table_mode_separator_map
endif
endfunction
" }}}2
function! s:SetActive(bool) "{{{2
let b:table_mode_active = a:bool
call s:ToggleMapping()
endfunction
" }}}2
function! s:GenerateBorder(line) "{{{2
let line_val = getline(a:line)
let border = substitute(line_val[stridx(line_val, g:table_mode_separator):strridx(line_val, g:table_mode_separator)], g:table_mode_separator, g:table_mode_corner, 'g')
let border = substitute(border, '[^' . g:table_mode_corner . ']', g:table_mode_fillchar, 'g')
let cstartexpr = s:StartCommentExpr()
if s:Strlen(cstartexpr) > 0 && getline(a:line) =~# cstartexpr
let sce = matchstr(line_val, s:StartCommentExpr())
let ece = matchstr(line_val, s:EndCommentExpr())
return sce . border . ece
elseif getline(a:line) =~# s:StartExpr()
let indent = matchstr(line_val, s:StartExpr())
return indent . border
function! s:DefaultHeaderBorder() "{{{2
if s:IsTableModeActive()
return g:table_mode_corner_corner . g:table_mode_fillchar . g:table_mode_corner . g:table_mode_fillchar . g:table_mode_corner_corner
else
return border
return ''
endif
endfunction
" }}}2
function! s:UpdateLineBorder(line) "{{{2
function! s:GenerateHeaderBorder(line) "{{{2
let line = s:Line(a:line)
if tablemode#IsATableRow(line - s:RowGap()) || tablemode#IsATableRow(line + s:RowGap())
let line_val = ''
if tablemode#IsATableRow(line + s:RowGap())
let line_val = getline(line + s:RowGap())
endif
if tablemode#IsATableRow(line - s:RowGap()) && s:Strlen(line_val) < s:Strlen(getline(line - s:RowGap()))
let line_val = getline(line - s:RowGap())
endif
if s:Strlen(line_val) <= 1 | return s:DefaultHeaderBorder() | endif
let border = substitute(line_val[stridx(line_val, g:table_mode_separator):strridx(line_val, g:table_mode_separator)], g:table_mode_separator, g:table_mode_corner, 'g')
let border = substitute(border, '[^' . g:table_mode_corner . ']', g:table_mode_fillchar, 'g')
let border = substitute(border, '^' . g:table_mode_corner . '\(.*\)' . g:table_mode_corner . '$', g:table_mode_corner_corner . '\1' . g:table_mode_corner_corner, '')
let hf = s:StartExpr() . g:table_mode_corner . '[' . g:table_mode_corner .
\ g:table_mode_fillchar . ']*' . g:table_mode_corner . '\?' . s:EndExpr()
let rowgap = s:RowGap()
let border = s:GenerateBorder(line)
let [prev_line, next_line] = [getline(line-1), getline(line+1)]
if next_line =~# hf
if s:Strlen(border) > s:Strlen(s:GenerateBorder(line + rowgap)) || !tablemode#IsATableRow(line + rowgap)
call setline(line+1, border)
let cstartexpr = s:StartCommentExpr()
if s:Strlen(cstartexpr) > 0 && getline(line) =~# cstartexpr
let sce = matchstr(line_val, s:StartCommentExpr())
let ece = matchstr(line_val, s:EndCommentExpr())
return sce . border . ece
elseif getline(line) =~# s:StartExpr()
let indent = matchstr(line_val, s:StartExpr())
return indent . border
else
return border
endif
else
call append(line, border)
endif
if prev_line =~# hf
if s:Strlen(border) > s:Strlen(s:GenerateBorder(line - rowgap)) || !tablemode#IsATableRow(line - rowgap)
call setline(line-1, border)
endif
else
call append(line-1, border)
return s:DefaultHeaderBorder()
endif
endfunction
" }}}2
function! s:ConvertDelimiterToSeparator(line, ...) "{{{2
let delim = g:table_mode_delimiter
@@ -242,71 +223,89 @@ function! s:ConvertDelimiterToSeparator(line, ...) "{{{2
if delim ==# ','
silent! execute a:line . 's/' . "[\'\"][^\'\"]*\\zs,\\ze[^\'\"]*[\'\"]/__COMMA__/g"
endif
silent! execute a:line . 's/' . s:StartExpr() . '\zs\ze.\|' . delim . '\|.\zs\ze' . s:EndExpr() . '/' .
let [cstart, cend] = [s:GetCommentStart(), s:GetCommentEnd()]
let [match_char_start, match_char_end] = ['.', '.']
if s:Strlen(cend) > 0 | let match_char_end = '[^' . cend . ']' | endif
if s:Strlen(cstart) > 0 | let match_char_start = '[^' . cstart . ']' | endif
silent! execute a:line . 's/' . s:StartExpr() . '\zs\ze' . match_char_start .
\ '\|' . delim . '\|' . match_char_end . '\zs\ze' . s:EndExpr() . '/' .
\ g:table_mode_separator . '/g'
if delim ==# ','
silent! execute a:line . 's/' . "[\'\"][^\'\"]*\\zs__COMMA__\\ze[^\'\"]*[\'\"]/,/g"
endif
endfunction
" }}}2
function! s:Tableizeline(line, ...) "{{{2
let delim = g:table_mode_delimiter
if a:0 && type(a:1) == type('') && !empty(a:1) | let delim = a:1[1:-1] | endif
call s:ConvertDelimiterToSeparator(a:line, delim)
if g:table_mode_border | call s:UpdateLineBorder(a:line) | endif
endfunction
" }}}2
function! s:IsFirstCell() "{{{2
return tablemode#ColumnNr('.') ==# 1
endfunction
" }}}2
function! s:IsLastCell() "{{{2
return tablemode#ColumnNr('.') ==# tablemode#ColumnCount('.')
endfunction
" }}}2
function! s:GetFirstRow(line) "{{{2
if tablemode#IsATableRow(a:line)
let line = s:Line(a:line)
while tablemode#IsATableRow(line - s:RowGap())
let line = line - s:RowGap()
while tablemode#IsATableRow(line - s:RowGap()) || tablemode#IsATableHeader(line - s:RowGap())
let line -= s:RowGap()
endwhile
if tablemode#IsATableHeader(line) | let line += s:RowGap() | endif
return line
endif
endfunction
" }}}2
function! s:MoveToFirstRow() "{{{2
if tablemode#IsATableRow('.')
call cursor(s:GetFirstRow('.'), col('.'))
endif
endfunction
" }}}2
function! s:GetLastRow(line) "{{{2
if tablemode#IsATableRow(a:line)
let line = s:Line(a:line)
while tablemode#IsATableRow(line+ s:RowGap())
let line = line + s:RowGap()
while tablemode#IsATableRow(line + s:RowGap()) || tablemode#IsATableHeader(line + s:RowGap())
let line += s:RowGap()
endwhile
if tablemode#IsATableHeader(line) | let line -= s:RowGap() | endif
return line
endif
endfunction
" }}}2
function! s:MoveToLastRow() "{{{2
if tablemode#IsATableRow('.')
call cursor(s:GetLastRow('.'), col('.'))
endif
endfunction
" }}}2
function! s:LineNr(row) "{{{2
if tablemode#IsATableRow('.')
let line = s:GetFirstRow('.')
let row_nr = 0
while tablemode#IsATableRow(line + s:RowGap()) || tablemode#IsATableHeader(line + s:RowGap())
if tablemode#IsATableRow(line)
let row_nr += 1
if row ==# row_nr | break | endif
endif
let line += s:RowGap()
endwhile
return line
endif
endfunction
function! s:MoveToStartOfCell() "{{{2
if getline('.')[col('.')-1] ==# g:table_mode_separator && !s:IsLastCell()
@@ -315,7 +314,6 @@ function! s:MoveToStartOfCell() "{{{2
execute 'normal! F' . g:table_mode_separator . '2l'
endif
endfunction
" }}}2
" function! s:GetCells() - Function to get values of cells in a table {{{2
" s:GetCells(row) - Get values of all cells in a row as a List.
@@ -333,32 +331,39 @@ function! s:GetCells(line, ...) abort
let [row, colm] = a:000
endif
let first_row = s:GetFirstRow(line)
if row == 0
let values = []
let line = s:GetFirstRow(line)
while tablemode#IsATableRow(line)
let row_line = getline(line)[stridx(getline(line), g:table_mode_separator):strridx(getline(line), g:table_mode_separator)]
call add(values, s:Strip(split(row_line, g:table_mode_separator)[colm>0?colm-1:colm]))
let line = line + s:RowGap()
let line = first_row
while tablemode#IsATableRow(line) || tablemode#IsATableHeader(line)
if tablemode#IsATableRow(line)
let row_line = getline(line)[stridx(getline(line), g:table_mode_separator):strridx(getline(line), g:table_mode_separator)]
call add(values, s:Strip(get(split(row_line, g:table_mode_separator), colm>0?colm-1:colm, '')))
endif
let line += s:RowGap()
endwhile
return values
else
if row > 0
let line = line + (row - tablemode#RowNr(line)) * s:RowGap()
else
let line = line + row * s:RowGap()
endif
let row_nr = 0
let line = first_row
while tablemode#IsATableRow(line) || tablemode#IsATableHeader(line)
if tablemode#IsATableRow(line)
let row_nr += 1
if row ==# row_nr | break | endif
endif
let line += s:RowGap()
endwhile
let row_line = getline(line)[stridx(getline(line), g:table_mode_separator):strridx(getline(line), g:table_mode_separator)]
if colm == 0
return map(split(row_line, g:table_mode_separator), 's:Strip(v:val)')
else
return s:Strip(split(row_line, g:table_mode_separator)[colm>0?colm-1:colm])
let split_line = split(row_line, g:table_mode_separator)
return s:Strip(get(split(row_line, g:table_mode_separator), colm>0?colm-1:colm, ''))
endif
endif
endif
endfunction
" }}}2
function! s:GetCell(...) "{{{2
if a:0 == 0
@@ -369,7 +374,6 @@ function! s:GetCell(...) "{{{2
return s:GetCells('.', row, col)
endfunction
" }}}2
function! s:SetCell(val, ...) abort "{{{2
if a:0 == 0
@@ -385,6 +389,7 @@ function! s:SetCell(val, ...) abort "{{{2
let line_val = getline(line)
let cstartexpr = s:StartCommentExpr()
let values = split(getline(line)[stridx(line_val, g:table_mode_separator):strridx(line_val, g:table_mode_separator)], g:table_mode_separator)
if len(values) < colm | return | endif
let values[colm-1] = a:val
let line_value = g:table_mode_separator . join(values, g:table_mode_separator) . g:table_mode_separator
if s:Strlen(cstartexpr) > 0 && line_val =~# cstartexpr
@@ -396,33 +401,28 @@ function! s:SetCell(val, ...) abort "{{{2
call tablemode#TableRealign(line)
endif
endfunction
" }}}2
function! s:GetRow(row, ...) abort "{{{2
let line = a:0 < 1 ? '.' : a:1
return s:GetCells(line, a:row)
endfunction
" }}}2
function! s:GetRowColumn(col, ...) abort "{{{2
let line = a:0 < 1 ? '.' : a:1
let row = tablemode#RowNr('.')
return s:GetCells(line, row, a:col)
endfunction
" }}}2
function! s:GetColumn(col, ...) abort "{{{2
let line = a:0 < 1 ? '.' : a:1
return s:GetCells(line, 0, a:col)
endfunction
" }}}2
function! s:GetColumnRow(row, ...) abort "{{{2
let line = a:0 < 1 ? '.' : a:1
let col = tablemode#ColumnNr('.')
return s:GetCells(line, a:row, col)
endfunction
" }}}2
function! s:ParseRange(range, ...) "{{{2
if a:0 < 1
@@ -451,10 +451,9 @@ function! s:ParseRange(range, ...) "{{{2
else
let [row2, col2] = [rcs2[0], default_col]
endif
return [row1, col1, row2, col2]
endfunction
" }}}2
" function! s:GetCellRange(range, ...) {{{2
" range: A string representing range of cells.
@@ -498,7 +497,6 @@ function! s:GetCellRange(range, ...) abort
return values
endfunction
" }}}2
" Borrowed from Tabular : {{{2
@@ -507,7 +505,6 @@ endfunction
function! s:StripTrailingSpaces(string)
return matchstr(a:string, '^.\{-}\ze\s*$')
endfunction
" }}}3
function! s:Padding(string, length, where) "{{{3
let gap_length = a:length - s:Strlen(a:string)
@@ -521,7 +518,6 @@ function! s:Padding(string, length, where) "{{{3
return repeat(" ", left) . a:string . repeat(" ", right)
endif
endfunction
" }}}3
" function! s:Split() - Split a string into fields and delimiters {{{3
" Like split(), but include the delimiters as elements
@@ -565,7 +561,6 @@ function! s:Split(string, delim)
return rv
endfunction
" }}}3
function! s:Align(lines) "{{{3
let lines = map(a:lines, 's:Split(v:val, g:table_mode_separator)')
@@ -614,26 +609,31 @@ function! s:Align(lines) "{{{3
return lines
endfunction
" }}}3
" }}}2
" }}}1
" Public API {{{1
function! tablemode#sid() "{{{2
return maparg('<SID>', 'n')
endfunction
nnoremap <SID> <SID>
function! tablemode#scope() "{{{2
return s:
endfunction
function! tablemode#GetLastRow(line) "{{{2
return s:GetLastRow(a:line)
endfunction
" }}}2
function! tablemode#GetFirstRow(line) "{{{2
return s:GetFirstRow(a:line)
endfunction
" }}}2
function! tablemode#TableizeInsertMode() "{{{2
if s:IsTableModeActive() && getline('.') =~# (s:StartExpr() . g:table_mode_separator)
if s:IsTableModeActive() && getline('.') =~# (s:StartExpr() . g:table_mode_separator . g:table_mode_separator)
call tablemode#AddHeaderBorder('.')
normal! A
elseif s:IsTableModeActive() && getline('.') =~# (s:StartExpr() . g:table_mode_separator)
let column = s:Strlen(substitute(getline('.')[0:col('.')], '[^' . g:table_mode_separator . ']', '', 'g'))
let position = s:Strlen(matchstr(getline('.')[0:col('.')], '.*' . g:table_mode_separator . '\s*\zs.*'))
call tablemode#TableRealign('.')
@@ -641,17 +641,14 @@ function! tablemode#TableizeInsertMode() "{{{2
call search(repeat('[^' . g:table_mode_separator . ']*' . g:table_mode_separator, column) . '\s\{-\}' . repeat('.', position), 'ce', line('.'))
endif
endfunction
" }}}2
function! tablemode#TableModeEnable() "{{{2
call s:SetActive(1)
endfunction
" }}}2
function! tablemode#TableModeDisable() "{{{2
call s:SetActive(0)
endfunction
" }}}2
function! tablemode#TableModeToggle() "{{{2
if g:table_mode_always_active
@@ -661,25 +658,17 @@ function! tablemode#TableModeToggle() "{{{2
call s:SetBufferOptDefault('table_mode_active', 0)
call s:SetActive(!b:table_mode_active)
endfunction
" }}}2
function! tablemode#TableizeRange(...) range "{{{2
let shift = 1
if g:table_mode_border | let shift += 1 | endif
call s:Tableizeline(a:firstline, a:1)
undojoin
" The first one causes 2 extra lines for top & bottom border while the
" following lines cause only 1 for the bottom border.
let lnum = a:firstline + shift + (g:table_mode_border > 0)
while lnum < (a:firstline + (a:lastline - a:firstline + 1)*shift)
let lnum = a:firstline
while lnum < (a:firstline + (a:lastline - a:firstline + 1)*s:RowGap())
call s:Tableizeline(lnum, a:1)
undojoin
let lnum = lnum + shift
let lnum += s:RowGap()
endwhile
if g:table_mode_border | call tablemode#TableRealign(lnum - shift) | endif
call tablemode#TableRealign(lnum - s:RowGap())
endfunction
" }}}2
function! tablemode#TableizeByDelimiter() "{{{2
let delim = input('/')
@@ -689,24 +678,38 @@ function! tablemode#TableizeByDelimiter() "{{{2
exec line("'<") . ',' . line("'>") . "call tablemode#TableizeRange('/' . delim)"
endif
endfunction
" }}}2
function! tablemode#AddHeaderBorder(line) "{{{2
call setline(a:line, s:GenerateHeaderBorder(a:line))
endfunction
function! tablemode#TableRealign(line) "{{{2
let line = s:Line(a:line)
let [lnums, lines] = [[], []]
let tline = line
while tablemode#IsATableRow(tline)
let [tline, blines] = [line, []]
while tablemode#IsATableRow(tline) || tablemode#IsATableHeader(tline)
if tablemode#IsATableHeader(tline)
call insert(blines, tline)
let tline -= s:RowGap()
continue
endif
call insert(lnums, tline)
call insert(lines, getline(tline))
let tline = tline - s:RowGap()
let tline -= s:RowGap()
endwhile
let tline = line + s:RowGap()
while tablemode#IsATableRow(tline)
while tablemode#IsATableRow(tline) || tablemode#IsATableHeader(tline)
if tablemode#IsATableHeader(tline)
call insert(blines, tline)
let tline += s:RowGap()
continue
endif
call add(lnums, tline)
call add(lines, getline(tline))
let tline = tline + s:RowGap()
let tline += s:RowGap()
endwhile
let lines = s:Align(lines)
@@ -714,55 +717,61 @@ function! tablemode#TableRealign(line) "{{{2
for lnum in lnums
let index = index(lnums, lnum)
call setline(lnum, lines[index])
undojoin
call s:UpdateLineBorder(lnum)
endfor
for bline in blines
call tablemode#AddHeaderBorder(bline)
endfor
endfunction
" }}}2
function! tablemode#IsATableRow(line) "{{{2
return getline(a:line) =~# (s:StartExpr() . g:table_mode_separator)
return getline(a:line) =~# (s:StartExpr() . g:table_mode_separator . '[^' .
\ g:table_mode_fillchar . ']*[^' . g:table_mode_corner . ']*$')
endfunction
function! tablemode#IsATableHeader(line) "{{{2
return getline(a:line) =~# s:HeaderBorderExpr()
endfunction
function! tablemode#LineNr(row) "{{{2
return s:LineNr(row)
endfunction
" }}}2
function! tablemode#RowCount(line) "{{{2
let line = s:Line(a:line)
let [tline, totalRowCount] = [line, 0]
while tablemode#IsATableRow(tline)
let totalRowCount += 1
let tline = tline - s:RowGap()
while tablemode#IsATableRow(tline) || tablemode#IsATableHeader(tline)
if tablemode#IsATableRow(tline) | let totalRowCount += 1 | endif
let tline -= s:RowGap()
endwhile
let tline = line + s:RowGap()
while tablemode#IsATableRow(tline)
let totalRowCount += 1
let tline = tline + s:RowGap()
while tablemode#IsATableRow(tline) || tablemode#IsATableHeader(tline)
if tablemode#IsATableRow(tline) | let totalRowCount += 1 | endif
let tline += s:RowGap()
endwhile
return totalRowCount
endfunction
" }}}2
function! tablemode#RowNr(line) "{{{2
let line = s:Line(a:line)
let rowNr = 0
while tablemode#IsATableRow(line)
let rowNr += 1
let line = line - s:RowGap()
while tablemode#IsATableRow(line) || tablemode#IsATableHeader(line)
if tablemode#IsATableRow(line) | let rowNr += 1 | endif
let line -= s:RowGap()
endwhile
return rowNr
endfunction
" }}}2
function! tablemode#ColumnCount(line) "{{{2
let line = s:Line(a:line)
return s:Strlen(substitute(getline(line), '[^' . g:table_mode_separator . ']', '', 'g'))-1
endfunction
" }}}2
function! tablemode#ColumnNr(pos) "{{{2
let pos = []
@@ -776,13 +785,14 @@ function! tablemode#ColumnNr(pos) "{{{2
let row_start = stridx(getline(pos[0]), g:table_mode_separator)
return s:Strlen(substitute(getline(pos[0])[(row_start):pos[1]-2], '[^' . g:table_mode_separator . ']', '', 'g'))
endfunction
" }}}2
function! tablemode#TableMotion(direction) "{{{2
if tablemode#IsATableRow('.')
if a:direction ==# 'l'
if s:IsLastCell()
if !tablemode#IsATableRow(line('.') + s:RowGap()) | return | endif
if !tablemode#IsATableRow(line('.') + s:RowGap()) || (tablemode#IsATableHeader(line('.') + s:RowGap()) && tablemode#IsATableRow(line('.') + 2 * s:RowGap()))
return
endif
call tablemode#TableMotion('j')
normal! 0
endif
@@ -795,7 +805,9 @@ function! tablemode#TableMotion(direction) "{{{2
endif
elseif a:direction ==# 'h'
if s:IsFirstCell()
if !tablemode#IsATableRow(line('.') - s:RowGap()) | return | endif
if !tablemode#IsATableRow(line('.') - s:RowGap()) || (tablemode#IsATableHeader(line('.') - s:RowGap()) && tablemode#IsATableRow(line('.') - 2 * s:RowGap()))
return
endif
call tablemode#TableMotion('k')
normal! $
endif
@@ -807,13 +819,20 @@ function! tablemode#TableMotion(direction) "{{{2
execute 'normal! 2F' . g:table_mode_separator . '2l'
endif
elseif a:direction ==# 'j'
if tablemode#IsATableRow(line('.') + s:RowGap()) | execute 'normal ' . s:RowGap() . 'j' | endif
if tablemode#IsATableRow(line('.') + s:RowGap())
execute 'normal! ' . s:RowGap() . 'j'
elseif tablemode#IsATableHeader(line('.') + s:RowGap()) && tablemode#IsATableRow(line('.') + 2 * s:RowGap())
execute 'normal! ' . (s:RowGap() + 1) . 'j'
endif
elseif a:direction ==# 'k'
if tablemode#IsATableRow(line('.') - s:RowGap()) | execute 'normal ' . s:RowGap() . 'k' | endif
if tablemode#IsATableRow(line('.') - s:RowGap())
execute 'normal! ' . s:RowGap() . 'k'
elseif tablemode#IsATableHeader(line('.') - s:RowGap()) && tablemode#IsATableRow(line('.') - 2 * s:RowGap())
execute 'normal! ' . (s:RowGap() + 1) . 'k'
endif
endif
endif
endfunction
" }}}2
function! tablemode#CellTextObject() "{{{2
if tablemode#IsATableRow('.')
@@ -827,13 +846,12 @@ function! tablemode#CellTextObject() "{{{2
endif
endif
endfunction
" }}}2
function! tablemode#DeleteColumn() "{{{2
if tablemode#IsATableRow('.')
for i in range(v:count1)
call s:MoveToFirstRow()
call s:MoveToStartOfCell()
call s:MoveToFirstRow()
silent! execute "normal! h\<C-V>f" . g:table_mode_separator
call s:MoveToLastRow()
normal! d
@@ -842,20 +860,15 @@ function! tablemode#DeleteColumn() "{{{2
call tablemode#TableRealign('.')
endif
endfunction
" }}}2
function! tablemode#DeleteRow() "{{{2
if tablemode#IsATableRow('.')
for i in range(v:count1)
if tablemode#RowCount('.') ==# 1
normal! kVjjd
else
normal! kVjd
if tablemode#IsATableRow('.')
normal! dd
endif
if tablemode#IsATableRow(line('.')+1)
normal! j
else
if !tablemode#IsATableRow('.')
normal! k
endif
endfor
@@ -863,51 +876,48 @@ function! tablemode#DeleteRow() "{{{2
call tablemode#TableRealign('.')
endif
endfunction
" }}}2
function! tablemode#GetCells(...) abort "{{{2
let args = copy(a:000)
call insert(args, '.')
return call('s:GetCells', args)
endfunction
" }}}2
function! tablemode#SetCell(val, ...) "{{{2
let args = copy(a:000)
call insert(args, a:val)
call call('s:SetCell', args)
endfunction
" }}}2
function! tablemode#GetCellRange(range, ...) abort "{{{2
let args = copy(a:000)
call insert(args, a:range)
return call('s:GetCellRange', args)
endfunction
" }}}2
function! tablemode#Sum(range, ...) abort "{{{2
let args = copy(a:000)
call insert(args, a:range)
return s:Sum(call('s:GetCellRange', args))
endfunction
" }}}2
function! tablemode#Average(range, ...) abort "{{{2
let args = copy(a:000)
call insert(args, a:range)
return s:Average(call('s:GetCellRange', args))
endfunction
" }}}2
function! tablemode#AddFormula() "{{{2
let fr = input('f=')
let row = tablemode#RowNr('.')
let colm = tablemode#ColumnNr('.')
let indent = indent('.')
let indent_str = repeat(' ', indent)
if fr !=# ''
let fr = '$' . row . ',' . colm . '=' . fr
let fline = tablemode#GetLastRow('.') + s:RowGap()
if tablemode#IsATableHeader(fline) | let fline += s:RowGap() | endif
let cursor_pos = [line('.'), col('.')]
if getline(fline) =~# 'tmf: '
" Comment line correctly
@@ -927,14 +937,13 @@ function! tablemode#AddFormula() "{{{2
let [cmss, cmse] = [cms[0], '']
endif
endif
let fr = cmss . ' tmf: ' . fr . ' ' . cmse
let fr = indent_str . cmss . ' tmf: ' . fr . ' ' . cmse
call append(fline-1, fr)
call cursor(cursor_pos)
endif
call tablemode#EvaluateFormulaLine()
endif
endfunction
" }}}2
function! tablemode#EvaluateExpr(expr, line) abort "{{{2
let line = s:Line(a:line)
@@ -980,9 +989,8 @@ function! tablemode#EvaluateExpr(expr, line) abort "{{{2
endwhile
endif
endfunction
" }}}2
function! tablemode#EvaluateFormulaLine() "{{{2
function! tablemode#EvaluateFormulaLine() abort "{{{2
let exprs = []
let cstring = &commentstring
let matchexpr = ''
@@ -998,11 +1006,14 @@ function! tablemode#EvaluateFormulaLine() "{{{2
endif
if tablemode#IsATableRow('.') " We're inside the table
let line = s:GetLastRow('.')
if getline(line + s:RowGap()) =~# 'tmf: '
let exprs = split(matchstr(getline(line + s:RowGap()), matchexpr), ';')
let fline = line + s:RowGap()
if tablemode#IsATableHeader(fline) | let fline += s:RowGap() | endif
if getline(fline) =~# 'tmf: '
let exprs = split(matchstr(getline(fline), matchexpr), ';')
endif
elseif getline('.') =~# 'tmf: ' " We're on the formula line
let line = line('.') - s:RowGap()
if tablemode#IsATableHeader(line) | let line -= s:RowGap() | endif
if tablemode#IsATableRow(line)
let exprs = split(matchstr(getline('.'), matchexpr), ';')
endif
@@ -1012,8 +1023,5 @@ function! tablemode#EvaluateFormulaLine() "{{{2
call tablemode#EvaluateExpr(expr, line)
endfor
endfunction
" }}}2
" }}}1
" vim: sw=2 sts=2 fdl=0 fdm=marker

View File

@@ -1,7 +1,7 @@
*table-mode.txt* Table Mode for easy table formatting. v3.0
*table-mode.txt* Table Mode for easy table formatting. v3.3.3
===============================================================================
Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER
VERSION 3.0
VERSION 3.3.3
Author: Dhruva Sagar <http://dhruvasagar.com/>
License: MIT <http://opensource.org/licenses/MIT/>
@@ -29,14 +29,14 @@ GETTING STARTED *table-mode-getting-started*
Create Table on the fly:
Using Table Mode is dead simple. You simply start typing on a new line
with the table separator - |g:table-mode-separator|, and you just type
with the table separator - |table-mode-separator|, and you just type
away! The plugin does the rest automatically for you as you type. With
each additional separator you add, it aligns the table properly,
without having to do anything else.
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-always-active| if you wish.
using |table-mode-always-active| if you wish though not recommended.
Table Mode allows for creation of tables within comments, it looks at
the 'commentstring' setting to identify whether the current line is
@@ -130,7 +130,6 @@ OPTIONS *table-mode-options*
Overview:
|table-mode-loaded| ............. Disable the plugin.
|table-mode-border| ............. Enable border.
|table-mode-corner| ............. Set corner character.
|table-mode-separator| .......... Set separator character.
|table-mode-fillchar| ........... Set table fillchar character.
@@ -149,17 +148,13 @@ Overview:
current cell.
|table-mode-eval-expr-map| ...... Evaluate formula line and update
table.
|table-mode-corner-corner| ...... Set the character to be used for
extreme corners of the table border.
g:loaded_table_mode *table-mode-loaded*
Use this option to disable the plugin: >
let g:loaded_table_mode = 1
<
g:table_mode_border *table-mode-border*
Use this option to enable / disable table border: >
let g:table_mode_border = 1
<
g:table_mode_corner *table-mode-corner*
Use this option to define the table corner character: >
let g:table_mode_corner = '+'
@@ -172,7 +167,7 @@ g:table_mode_separator *table-mode-separator*
table row in insert mode.
g:table_mode_fillchar *table-mode-fillchar*
Use this option to define the table border fill character: >
Use this option to define the table header border fill character: >
let g:table_mode_fillchar = '-'
<
@@ -279,6 +274,17 @@ g:table_mode_eval_expr_map *table-mode-eval-expr-map*
NOTE you will need to use the |table-mode-map-prefix| before this to
get the desired effect.
g:table_mode_echo_cell_map *table-mode-echo-cell-map*
Use this option to define the mapping for echoing current cells
representation for defining formulas. >
let g:table_mode_echo_cell_map = '?'
<
g:table_mode_corner_corner *table-mode-corner-corner*
Use this option to define the character to be used for the extreme
corners of the table border. >
let g:table_mode_corner_corner = '|'
<
===============================================================================
MAPPINGS *table-mode-mappings*
@@ -307,6 +313,9 @@ MAPPINGS *table-mode-mappings*
*table-mode-mappings-realign*
<Leader>tr Realigns table columns
<Leader>t? Echo the current table cells representation for defining
formulas.
*table-mode-mappings-motions*
<Leader>t[hjkl] Move to previous | below | above | right cell in the table.
@@ -330,6 +339,11 @@ MAPPINGS *table-mode-mappings*
would evaluate the formula line and update the table
accordingly. This invokes the |TableEvalFormulaLine| command.
|| Expands to a header border. You can change this by changing
|table-mode-separator| option. You can change the character to
be used for te extreme corners of the border by changing
|table-mode-corner-corner| option.
===============================================================================
COMMANDS *table-mode-commands*
@@ -400,4 +414,4 @@ REPORT ISSUES *table-mode-report-issues*
If you discover any issues, please report them at
http://github.com/dhruvasagar/table-mode/issues.
vim:tw=78:ts=8:ft=help:norl:ai
vim:tw=78:ts=8:ft=help:norl:ai:et

View File

@@ -4,10 +4,9 @@
" Author: Dhruva Sagar <http://dhruvasagar.com/>
" License: MIT (http://www.opensource.org/licenses/MIT)
" Website: http://github.com/dhruvasagar/vim-table-mode
" Version: 2.4.0
" Version: 3.3.3
" Note: This plugin was heavily inspired by the 'CucumberTables.vim'
" (https://gist.github.com/tpope/287147) plugin by Tim Pope and
" uses a small amount of code from it.
" (https://gist.github.com/tpope/287147) plugin by Tim Pope.
"
" Copyright Notice:
" Permission is hereby granted to use and distribute this code,
@@ -24,22 +23,18 @@ if exists('g:loaded_table_mode')
finish
endif
let g:loaded_table_mode = 1
"}}}1
" Avoiding side effects {{{1
let s:save_cpo = &cpo
set cpo&vim
" }}}1
function! s:SetGlobalOptDefault(opt, val) "{{{1
if !exists('g:' . a:opt)
let g:{a:opt} = a:val
endif
endfunction
" }}}1
" Set Global Defaults {{{1
call s:SetGlobalOptDefault('table_mode_border', 1)
call s:SetGlobalOptDefault('table_mode_corner', '+')
call s:SetGlobalOptDefault('table_mode_separator', '|')
call s:SetGlobalOptDefault('table_mode_fillchar', '-')
@@ -55,7 +50,8 @@ call s:SetGlobalOptDefault('table_mode_delete_row_map', 'dd')
call s:SetGlobalOptDefault('table_mode_delete_column_map', 'dc')
call s:SetGlobalOptDefault('table_mode_add_formula_map', 'fa')
call s:SetGlobalOptDefault('table_mode_eval_expr_map', 'fe')
"}}}1
call s:SetGlobalOptDefault('table_mode_echo_cell_map', '?')
call s:SetGlobalOptDefault('table_mode_corner_corner', '|')
function! s:TableMotion() "{{{1
let direction = nr2char(getchar())
@@ -63,7 +59,12 @@ function! s:TableMotion() "{{{1
call tablemode#TableMotion(direction)
endfor
endfunction
" }}}1
function! s:TableEchoCell() "{{{1
if tablemode#IsATableRow('.')
echomsg '$' . tablemode#RowNr('.') . ',' . tablemode#ColumnNr('.')
endif
endfunction
" Define Commands & Mappings {{{1
if !g:table_mode_always_active "{{{2
@@ -108,11 +109,11 @@ execute "nnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_add_formul
\ " :TableAddFormula<CR>"
execute "nnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_eval_expr_map .
\ " :TableEvalFormulaLine<CR>"
"}}}1
execute "nnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_echo_cell_map .
\ " :call <SID>TableEchoCell()<CR>"
" Avoiding side effects {{{1
let &cpo = s:save_cpo
" }}}1
" ModeLine {{{
" vim: sw=2 sts=2 fdl=0 fdm=marker

253
t/tablemode.vim Normal file
View File

@@ -0,0 +1,253 @@
" vim: fdm=indent
let g:table_mode_corner = '+'
let g:table_mode_separator = '|'
let g:table_mode_fillchar = '-'
let g:table_mode_map_prefix = '<Leader>t'
let g:table_mode_toggle_map = 'm'
let g:table_mode_always_active = 0
let g:table_mode_delimiter = ','
let g:table_mode_tableize_map = 't'
let g:table_mode_tableize_op_map = '<Leader>T'
let g:table_mode_realign_map = 'r'
let g:table_mode_cell_text_object = 'tc'
let g:table_mode_delete_row_map = 'dd'
let g:table_mode_delete_column_map = 'dc'
let g:table_mode_add_formula_map = 'fa'
let g:table_mode_eval_expr_map = 'fe'
let g:table_mode_corner_corner = '|'
call vspec#hint({'scope': 'tablemode#scope()', 'sid': 'tablemode#sid()'})
describe 'tablemode'
describe 'Activation'
describe 'tablemode#TableModeEnable()'
before
call tablemode#TableModeEnable()
end
it 'should enable table mode'
Expect b:table_mode_active to_be_true
end
end
describe 'tablemode#TableModeDisable()'
before
call tablemode#TableModeDisable()
end
it 'should disable table mode'
Expect b:table_mode_active to_be_false
end
end
describe 'tablemode#TableModeToggle()'
it 'should toggle table mode'
call tablemode#TableModeToggle()
Expect b:table_mode_active to_be_true
call tablemode#TableModeToggle()
Expect b:table_mode_active to_be_false
end
end
end
describe 'API'
before
new
normal! ggdG
call tablemode#TableModeEnable()
normal i
|test11|test12|
|test21|test22|
end
it 'should return true when inside a table'
Expect tablemode#IsATableRow(2) to_be_true
end
it 'should return false when outside a table'
Expect tablemode#IsATableRow(4) to_be_false
end
it 'should return the row count'
Expect tablemode#RowCount(2) == 2
Expect tablemode#RowCount(3) == 2
end
it 'should return the row number'
Expect tablemode#RowNr(2) == 1
Expect tablemode#RowNr(3) == 2
end
it 'should return the column count'
Expect tablemode#ColumnCount(2) == 2
Expect tablemode#ColumnCount(3) == 2
end
it 'should return the line number of the first row'
Expect tablemode#GetFirstRow(2) == 2
Expect tablemode#GetFirstRow(3) == 2
end
it 'should return the line nuber of the last row'
Expect tablemode#GetLastRow(2) == 3
Expect tablemode#GetLastRow(3) == 3
end
it 'should return the cells'
Expect Call('s:GetCells', 2, 1, 1) ==# 'test11'
" Get Rows
Expect Call('s:GetCells', 2, 1) == ['test11', 'test12']
Expect Call('s:GetCells', 2, 2) == ['test21', 'test22']
" Get Columns
Expect Call('s:GetCells', 2, 0, 1) == ['test11', 'test21']
Expect Call('s:GetCells', 2, 0, 2) == ['test12', 'test22']
end
it 'should return the cells in a range'
" Entire table as range
Expect Call('s:GetCellRange', '1,1:2,2', 2, 1) == [['test11', 'test21'], ['test12', 'test22']]
" Get Rows given different seed lines and columns
Expect Call('s:GetCellRange', '1,1:1,2', 2, 1) == ['test11', 'test12']
Expect Call('s:GetCellRange', '1,1:1,2', 2, 2) == ['test11', 'test12']
Expect Call('s:GetCellRange', '1,1:1,2', 3, 1) == ['test11', 'test12']
Expect Call('s:GetCellRange', '1,1:1,2', 3, 2) == ['test11', 'test12']
Expect Call('s:GetCellRange', '2,1:2,2', 2, 1) == ['test21', 'test22']
Expect Call('s:GetCellRange', '2,1:2,2', 2, 2) == ['test21', 'test22']
Expect Call('s:GetCellRange', '2,1:2,2', 3, 1) == ['test21', 'test22']
Expect Call('s:GetCellRange', '2,1:2,2', 3, 2) == ['test21', 'test22']
" Get Columns given different seed lines and column
Expect Call('s:GetCellRange', '1:2', 2, 1) == ['test11', 'test21']
Expect Call('s:GetCellRange', '1:2', 2, 2) == ['test12', 'test22']
Expect Call('s:GetCellRange', '1:2', 3, 1) == ['test11', 'test21']
Expect Call('s:GetCellRange', '1:2', 3, 2) == ['test12', 'test22']
" Get Column given negative values in range for representing rows from
" the end, -1 being the second last row.
Expect Call('s:GetCellRange', '1:-1', 2, 1) == ['test11']
Expect Call('s:GetCellRange', '1:-1', 3, 1) == ['test11']
Expect Call('s:GetCellRange', '1:-1', 2, 2) == ['test12']
Expect Call('s:GetCellRange', '1:-1', 3, 2) == ['test12']
end
end
describe 'Tableize'
before
new
normal! ggdG
normal i
asd,asd;asd,asd
asd,asd;asd,asd
end
it 'should tableize with default delimiter'
:2,3call tablemode#TableizeRange('')
Expect tablemode#IsATableRow(2) to_be_true
Expect tablemode#RowCount(2) == 2
Expect tablemode#ColumnCount(2) == 3
end
it 'should tableize with given delimiter'
:2,3call tablemode#TableizeRange('/;')
Expect tablemode#IsATableRow(2) to_be_true
Expect tablemode#RowCount(2) == 2
Expect tablemode#ColumnCount(2) == 2
end
end
describe 'Motions'
describe 'left or right'
before
new
normal! ggdG
call tablemode#TableModeEnable()
normal i|test11|test12|
|test21|test22|
call cursor(1, 3)
end
it 'should move left when not on first column'
call cursor(1, 12)
Expect tablemode#ColumnNr('.') == 2
call tablemode#TableMotion('h')
Expect tablemode#ColumnNr('.') == 1
end
it 'should move to the previous row last column if it exists when on first column'
call cursor(2, 3)
Expect tablemode#RowNr('.') == 2
Expect tablemode#ColumnNr('.') == 1
call tablemode#TableMotion('h')
Expect tablemode#RowNr('.') == 1
Expect tablemode#ColumnNr('.') == 2
end
it 'should move right when not on last column'
Expect tablemode#ColumnNr('.') == 1
call tablemode#TableMotion('l')
Expect tablemode#ColumnNr('.') == 2
end
it 'should move to the next row first column if it exists when on last column'
call cursor(1, 12)
Expect tablemode#RowNr('.') == 1
Expect tablemode#ColumnNr('.') == 2
call tablemode#TableMotion('l')
Expect tablemode#RowNr('.') == 2
Expect tablemode#ColumnNr('.') == 1
end
end
describe 'up or down'
before
new
normal! ggdG
call tablemode#TableModeEnable()
normal i|test11|test12|
|test21|test22|
call cursor(1, 3)
end
it 'should move a row up unless on first row'
call cursor(2, 3)
Expect tablemode#RowNr('.') == 2
call tablemode#TableMotion('k')
Expect tablemode#RowNr('.') == 1
end
it 'should remain on first row when trying to move up'
Expect tablemode#RowNr('.') == 1
call tablemode#TableMotion('k')
Expect tablemode#RowNr('.') == 1
end
it 'should move a row down unless on last row'
Expect tablemode#RowNr('.') == 1
call tablemode#TableMotion('j')
Expect tablemode#RowNr('.') == 2
end
it 'should remain on last row when trying to move down'
Expect tablemode#RowNr('.') == 1
call tablemode#TableMotion('k')
Expect tablemode#RowNr('.') == 1
end
end
end
describe 'Manipulations'
before
new
normal! ggdG
normal i|test11|test12|
|test21|test22|
call cursor(1, 3)
end
it 'should delete a row successfully'
Expect tablemode#RowCount('.') == 2
call tablemode#DeleteRow()
Expect tablemode#RowCount('.') == 1
end