mirror of
https://github.com/dhruvasagar/vim-table-mode.git
synced 2025-11-08 19:13:46 -05:00
Compare commits
27 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
27544e168a | ||
|
|
e4af7200ff | ||
|
|
3cea0bbaba | ||
|
|
8931891845 | ||
|
|
0810512c54 | ||
|
|
4606d9c637 | ||
|
|
9ff1b37210 | ||
|
|
287f45cfa2 | ||
|
|
1ce3ec4e7f | ||
|
|
34e271b24a | ||
|
|
d0d9d1d4f6 | ||
|
|
41fa837437 | ||
|
|
fb63484e47 | ||
|
|
51c108188c | ||
|
|
974d506484 | ||
|
|
dd38fcfeeb | ||
|
|
94a76dc0f7 | ||
|
|
eba2481798 | ||
|
|
edfb221b3e | ||
|
|
e9d9030cbb | ||
|
|
2bde444575 | ||
|
|
def9a08623 | ||
|
|
57a49efe7e | ||
|
|
af55d03e14 | ||
|
|
cb21c1626c | ||
|
|
4e8e87256b | ||
|
|
88edc35ea6 |
@@ -1,4 +1,9 @@
|
||||
# Change Log
|
||||
## Version 3.0
|
||||
* Removed dependence on Tabular and added code borrowed from Tabular for
|
||||
aligning the table rows.
|
||||
* Added feature to be able to define & evaluate formulas.
|
||||
|
||||
## Version 2.4.0
|
||||
* Added Table Cell text object.
|
||||
* Added api to delete entire table row.
|
||||
|
||||
159
README.md
159
README.md
@@ -26,42 +26,146 @@ $ git submodule add git@github.com:dhruvasagar/vim-table-mode.git bundle/table-m
|
||||
vimfiles/autoload/, vimfiles/plugin/ and vimfiles/doc under WINDOWS and
|
||||
restart VIM
|
||||
|
||||
### Requirements
|
||||
|
||||
Depends on <a href="https://github.com/godlygeek/tabular">Tabular</a>. Make
|
||||
sure Tabular is installed and loaded into your runtime to ensure this works.
|
||||
|
||||
### Usage
|
||||
|
||||
- On the fly table creation :
|
||||
- **On the fly table creation** :
|
||||
|
||||
By default the table column separator is '|' (it can be changed). 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 automatically on the fly.
|
||||
By default the table column separator is `|` 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
|
||||
automatically on the fly.
|
||||
|
||||
Since this could lead to unwanted behavior I have disabled table mode by
|
||||
default. You would have to use `:TableModeToggle` command or the table mode
|
||||
toggle mapping, which is `<Leader>tm` by default 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 enabled explicitly. Please read `:h
|
||||
table-mode` for further information.
|
||||
- Format existing content into a table :
|
||||
default. You have to use `:TableModeToggle` command or the table mode
|
||||
toggle mapping, which is `<Leader>tm` 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
|
||||
enabled explicitly. Please read `:h table-mode` for further information.
|
||||
|
||||
- **Format existing content into a table** :
|
||||
|
||||
Table Mode wouldn't justify it's name if it didn't allow formatting
|
||||
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>T` (this is configurable). `:Tableize`
|
||||
accepts a range and so you can also call it by giving lines manually like
|
||||
`:line1,line2Tableize`, but this is not that intuitive. You can also use
|
||||
the mapping `<Leader>T` with a `[count]` to apply it to the next `[count]`
|
||||
lines in usual vim style.
|
||||
- Move between cells :
|
||||
simple. You can visually select multiple lines and call `:Tableize` on it,
|
||||
or alternatively use the mapping `<Leader>tt` 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.
|
||||
|
||||
Now you can move between cells using table mode motions. Check `:h
|
||||
table-mode-motion-prefix` for more details.
|
||||
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`
|
||||
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
|
||||
next `[count]` lines in standard vim style.
|
||||
|
||||
- **Move between cells** :
|
||||
|
||||
Now you can move between cells using table mode motions `<Leader>t[hjkl]`
|
||||
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
|
||||
the next | previous row after the last | first cell in the current row if
|
||||
one exists.
|
||||
|
||||
- **Manipulating Table** :
|
||||
|
||||
- **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
|
||||
`g:table_mode_cell_text_object` option.
|
||||
|
||||
This is intelligent in the sense that if you use it to delete the
|
||||
contents it will delete everything till the `g:table_mode_separator` so
|
||||
that the cell is removed and you can enter new stuff if you wish to (had
|
||||
you used 'c' operator), having to input the `g:table_mode_separator`
|
||||
again to form the cell & realign it (since it can't be done from the
|
||||
text object itself). However if you were to 'y' or yank the text, it
|
||||
would copy the right text, i.e. the contents of the cell and nothing
|
||||
else.
|
||||
|
||||
- **Delete Row** :
|
||||
|
||||
You can use the `<Leader>tdd` 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
|
||||
`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.
|
||||
|
||||
- **Table Formulas** :
|
||||
|
||||
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`
|
||||
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
|
||||
one will be created with the input formula taking the current cell as the
|
||||
target cell. The formula line is evaluated immidiately to reflect the
|
||||
results.
|
||||
|
||||
- You can directly add / manipulate formula expressions in the formula line.
|
||||
The formula line is a commented line right after the table, beginning with
|
||||
'tmf:' (table mode formula). eg) `# tmf: $3=$2*$1`. You can add multiple
|
||||
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`
|
||||
from anywhere inside the table or while on the formula line.
|
||||
|
||||
- **Formula Expressions** :
|
||||
|
||||
Expressions are of the format `$target = formula`.
|
||||
|
||||
- The `target` can be of 2 forms :
|
||||
|
||||
- `$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
|
||||
in it.
|
||||
|
||||
- `$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
|
||||
cell.
|
||||
|
||||
- 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
|
||||
can use all native vim functions within the formula. Apart from that table
|
||||
mode also provides 2 special functions `Sum` and `Average`. Both these
|
||||
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`
|
||||
through `r2`. If `r2` is negative it represents `r2` rows above the
|
||||
current row (of the target cell).
|
||||
|
||||
- `r1,c1:r2,c2`: This represents cells in the table from cell r1,c1
|
||||
through cell r2,c2 (row, column).
|
||||
|
||||
- Examples :
|
||||
- `$2 = $1 * $1`
|
||||
- `$2 = pow($1, 5)` NOTE: Remember to put space between the $1, and 5
|
||||
here otherwise it will be treated like a table cell.
|
||||
- `$2 = $1 / $1,3`
|
||||
- `$1,2 = $1,1 * $1,1`
|
||||
- `$5,1 = Sum(1:-1)`
|
||||
- `$5,1 = float2nr(Sum(1:-1))`
|
||||
- `$5,3 = Sum(1,2:5,2)`
|
||||
- `$5,3 = Sum(1,2:5,2)/$5,1`
|
||||
- `$5,3 = Average(1,2:5,2)/$5,1`
|
||||
|
||||
### Demo
|
||||
|
||||
@@ -85,5 +189,4 @@ I must thank Tim Pope for inspiration. The initial concept was created by him
|
||||
named <a href="https://gist.github.com/tpope/287147">cucumbertables.vim</a>.
|
||||
|
||||
Also a shout out to godlygeek who developed the incredible <a
|
||||
href="http://github.com/godlygeek/tabular">Tabular</a> plugin which does most
|
||||
of the grunt work behind the scenes.
|
||||
href="http://github.com/godlygeek/tabular">Tabular</a> plugin.
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
" =============================================================================
|
||||
" ============================== 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: 2.4.0
|
||||
" Version: 3.0
|
||||
" Note: This plugin was heavily inspired by the 'CucumberTables.vim'
|
||||
" (https://gist.github.com/tpope/287147) plugin by Tim Pope and
|
||||
" uses a small amount of code from it.
|
||||
@@ -17,10 +17,32 @@
|
||||
" 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
|
||||
|
||||
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
|
||||
@@ -28,17 +50,66 @@ function! s:SetBufferOptDefault(opt, val) "{{{2
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
" s:Strlen(text) For counting multibyte characters accurately {{{2
|
||||
function! s:Line(line) "{{{2
|
||||
if type(a:line) == type('')
|
||||
return line(a:line)
|
||||
else
|
||||
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
|
||||
elseif type(item) == type('')
|
||||
let result = result + str2float(item)
|
||||
elseif type(item) == type([])
|
||||
let result = 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
|
||||
if s:Strlen(cstring) > 0
|
||||
return substitute(split(substitute(cstring, '%s', ' ', 'g'))[0], '.', '\\\0', 'g')
|
||||
return substitute(split(cstring, '%s')[0], '.', '\\\0', 'g')
|
||||
else
|
||||
return ''
|
||||
endif
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:GetCommentEnd() "{{{2
|
||||
let cstring = &commentstring
|
||||
if s:Strlen(cstring) > 0
|
||||
let cst = split(cstring, '%s')
|
||||
if len(cst) == 2
|
||||
return substitute(cst[1], '.', '\\\0', 'g')
|
||||
else
|
||||
return ''
|
||||
endif
|
||||
else
|
||||
return ''
|
||||
endif
|
||||
@@ -55,6 +126,16 @@ function! s:StartExpr() "{{{2
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:EndExpr() "{{{2
|
||||
let cend = s:GetCommentEnd()
|
||||
if s:Strlen(cend) > 0
|
||||
return '\s*\(\s\+' . cend . '\)\?\s*$'
|
||||
else
|
||||
return '\s*$'
|
||||
endif
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:StartCommentExpr() "{{{2
|
||||
let cstartexpr = s:GetCommentStart()
|
||||
if s:Strlen(cstartexpr) > 0
|
||||
@@ -65,6 +146,16 @@ function! s:StartCommentExpr() "{{{2
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:EndCommentExpr() "{{{2
|
||||
let cendexpr = s:GetCommentEnd()
|
||||
if s:Strlen(cendexpr) > 0
|
||||
return '.*\zs\s\+' . cendexpr . '\s*$'
|
||||
else
|
||||
return ''
|
||||
endif
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:IsTableModeActive() "{{{2
|
||||
if g:table_mode_always_active | return 1 | endif
|
||||
|
||||
@@ -73,6 +164,11 @@ function! s:IsTableModeActive() "{{{2
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:RowGap() "{{{2
|
||||
return g:table_mode_border ? 2 : 1
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:ToggleMapping() "{{{2
|
||||
if exists('b:table_mode_active') && b:table_mode_active
|
||||
call s:SetBufferOptDefault('table_mode_separator_map', g:table_mode_separator)
|
||||
@@ -93,27 +189,18 @@ function! s:SetActive(bool) "{{{2
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:Line(line) "{{{2
|
||||
if type(a:line) == type('')
|
||||
return line(a:line)
|
||||
else
|
||||
return a:line
|
||||
endif
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:GenerateBorder(line) "{{{2
|
||||
let line = s:Line(a:line)
|
||||
|
||||
let border = substitute(getline(line)[stridx(getline(line), g:table_mode_separator):-1], g:table_mode_separator, g:table_mode_corner, 'g')
|
||||
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(line) =~# cstartexpr
|
||||
let indent = matchstr(getline(line), s:StartCommentExpr())
|
||||
return indent . border
|
||||
elseif getline(line) =~# s:StartExpr()
|
||||
let indent = matchstr(getline(line), s:StartExpr())
|
||||
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
|
||||
else
|
||||
return border
|
||||
@@ -125,13 +212,14 @@ function! s:UpdateLineBorder(line) "{{{2
|
||||
let line = s:Line(a:line)
|
||||
|
||||
let hf = s:StartExpr() . g:table_mode_corner . '[' . g:table_mode_corner .
|
||||
\ g:table_mode_fillchar . ']*' . g:table_mode_corner . '\?\s*$'
|
||||
\ 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 + s:RowGap())) || !tablemode#IsATableRow(line + s:RowGap())
|
||||
if s:Strlen(border) > s:Strlen(s:GenerateBorder(line + rowgap)) || !tablemode#IsATableRow(line + rowgap)
|
||||
call setline(line+1, border)
|
||||
endif
|
||||
else
|
||||
@@ -139,7 +227,7 @@ function! s:UpdateLineBorder(line) "{{{2
|
||||
endif
|
||||
|
||||
if prev_line =~# hf
|
||||
if s:Strlen(border) > s:Strlen(s:GenerateBorder(line - s:RowGap())) || !tablemode#IsATableRow(line - s:RowGap())
|
||||
if s:Strlen(border) > s:Strlen(s:GenerateBorder(line - rowgap)) || !tablemode#IsATableRow(line - rowgap)
|
||||
call setline(line-1, border)
|
||||
endif
|
||||
else
|
||||
@@ -154,7 +242,7 @@ 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 . '\|$/' .
|
||||
silent! execute a:line . 's/' . s:StartExpr() . '\zs\ze.\|' . delim . '\|.\zs\ze' . s:EndExpr() . '/' .
|
||||
\ g:table_mode_separator . '/g'
|
||||
if delim ==# ','
|
||||
silent! execute a:line . 's/' . "[\'\"][^\'\"]*\\zs__COMMA__\\ze[^\'\"]*[\'\"]/,/g"
|
||||
@@ -180,30 +268,42 @@ function! s:IsLastCell() "{{{2
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:MoveToFirstRow() "{{{2
|
||||
if tablemode#IsATableRow('.')
|
||||
let line = s:Line('.')
|
||||
while line > 0
|
||||
if !tablemode#IsATableRow(line)
|
||||
break
|
||||
endif
|
||||
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()
|
||||
endwhile
|
||||
call cursor(line + s:RowGap(), col('.'))
|
||||
|
||||
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()
|
||||
endwhile
|
||||
|
||||
return line
|
||||
endif
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:MoveToLastRow() "{{{2
|
||||
if tablemode#IsATableRow('.')
|
||||
let line = s:Line('.')
|
||||
while line <= line('$')
|
||||
if !tablemode#IsATableRow(line)
|
||||
break
|
||||
endif
|
||||
let line = line + s:RowGap()
|
||||
endwhile
|
||||
call cursor(line - s:RowGap(), col('.'))
|
||||
call cursor(s:GetLastRow('.'), col('.'))
|
||||
endif
|
||||
endfunction
|
||||
" }}}2
|
||||
@@ -217,19 +317,321 @@ function! s:MoveToStartOfCell() "{{{2
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:RowGap() "{{{2
|
||||
if g:table_mode_border
|
||||
return 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.
|
||||
" s:GetCells(0, col) - Get values of all cells in a column as a List.
|
||||
" s:GetCells(row, col) - Get the value of table cell by given row, col.
|
||||
function! s:GetCells(line, ...) abort
|
||||
let line = s:Line(a:line)
|
||||
|
||||
if tablemode#IsATableRow(line)
|
||||
if a:0 < 1
|
||||
let [row, colm] = [line, 0]
|
||||
elseif a:0 < 2
|
||||
let [row, colm] = [a:1, 0]
|
||||
elseif a:0 < 3
|
||||
let [row, colm] = a:000
|
||||
endif
|
||||
|
||||
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()
|
||||
endwhile
|
||||
return values
|
||||
else
|
||||
return 1
|
||||
if row > 0
|
||||
let line = line + (row - tablemode#RowNr(line)) * s:RowGap()
|
||||
else
|
||||
let line = line + row * s:RowGap()
|
||||
endif
|
||||
|
||||
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])
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:GetCell(...) "{{{2
|
||||
if a:0 == 0
|
||||
let [row, colm] = [tablemode#RowNr('.'), tablemode#ColumnNr('.')]
|
||||
elseif a:0 == 2
|
||||
let [row, colm] = [a:1, a:2]
|
||||
endif
|
||||
|
||||
return s:GetCells('.', row, col)
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! s:SetCell(val, ...) abort "{{{2
|
||||
if a:0 == 0
|
||||
let [line, row, colm] = ['.', tablemode#RowNr('.'), tablemode#ColumnNr('.')]
|
||||
elseif a:0 == 2
|
||||
let [line, row, colm] = ['.', a:1, a:2]
|
||||
elseif a:0 == 3
|
||||
let [line, row, colm] = a:000
|
||||
endif
|
||||
|
||||
if tablemode#IsATableRow(line)
|
||||
let line = s:Line(line) + (row - tablemode#RowNr(line)) * s:RowGap()
|
||||
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)
|
||||
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
|
||||
let sce = matchstr(line_val, s:StartCommentExpr())
|
||||
let ece = matchstr(line_val, s:EndCommentExpr())
|
||||
let line_value = sce . line_value . ece
|
||||
endif
|
||||
call setline(line, line_value)
|
||||
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
|
||||
let default_col = tablemode#ColumnNr('.')
|
||||
elseif a:0 < 2
|
||||
let default_col = a:1
|
||||
endif
|
||||
|
||||
if type(a:range) != type('')
|
||||
let range = string(a:range)
|
||||
else
|
||||
let range = a:range
|
||||
endif
|
||||
|
||||
let [rowcol1, rowcol2] = split(range, ':')
|
||||
let [rcs1, rcs2] = [map(split(rowcol1, ','), 'str2nr(v:val)'), map(split(rowcol2, ','), 'str2nr(v:val)')]
|
||||
|
||||
if len(rcs1) == 2
|
||||
let [row1, col1] = rcs1
|
||||
else
|
||||
let [row1, col1] = [rcs1[0], default_col]
|
||||
endif
|
||||
|
||||
if len(rcs2) == 2
|
||||
let [row2, col2] = rcs2
|
||||
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.
|
||||
" - Can be row1:row2 for values in the current columns in those rows.
|
||||
" - Can be row1,col1:row2,col2 for range between row1,col1 till
|
||||
" row2,col2.
|
||||
function! s:GetCellRange(range, ...) abort
|
||||
if a:0 < 1
|
||||
let [line, colm] = ['.', tablemode#ColumnNr('.')]
|
||||
elseif a:0 < 2
|
||||
let [line, colm] = [a:1, tablemode#ColumnNr('.')]
|
||||
elseif a:0 < 3
|
||||
let [line, colm] = [a:1, a:2]
|
||||
else
|
||||
call s:throw('Invalid Range')
|
||||
endif
|
||||
|
||||
let values = []
|
||||
|
||||
if tablemode#IsATableRow(line)
|
||||
let [row1, col1, row2, col2] = s:ParseRange(a:range, colm)
|
||||
|
||||
if row1 == row2
|
||||
if col1 == col2
|
||||
call add(values, s:GetCells(line, row1, col1))
|
||||
else
|
||||
let values = s:GetRow(row1, line)[(col1-1):(col2-1)]
|
||||
endif
|
||||
else
|
||||
if col1 == col2
|
||||
let values = s:GetColumn(col1, line)[(row1-1):(row2-1)]
|
||||
else
|
||||
let tcol = col1
|
||||
while tcol <= col2
|
||||
call add(values, s:GetColumn(tcol, line)[(row1-1):(row2-1)])
|
||||
let tcol += 1
|
||||
endwhile
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
return values
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
" Borrowed from Tabular : {{{2
|
||||
|
||||
" function! s:StripTrailingSpaces(string) - Remove all trailing spaces {{{3
|
||||
" from a string.
|
||||
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)
|
||||
if a:where =~# 'l'
|
||||
return a:string . repeat(" ", gap_length)
|
||||
elseif a:where =~# 'r'
|
||||
return repeat(" ", gap_length) . a:string
|
||||
elseif a:where =~# 'c'
|
||||
let right = spaces / 2
|
||||
let left = right + (right * 2 != gap_length)
|
||||
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
|
||||
" All odd numbered elements are delimiters
|
||||
" All even numbered elements are non-delimiters (including zero)
|
||||
function! s:Split(string, delim)
|
||||
let rv = []
|
||||
let beg = 0
|
||||
|
||||
let len = len(a:string)
|
||||
let searchoff = 0
|
||||
|
||||
while 1
|
||||
let mid = match(a:string, a:delim, beg + searchoff, 1)
|
||||
if mid == -1 || mid == len
|
||||
break
|
||||
endif
|
||||
|
||||
let matchstr = matchstr(a:string, a:delim, beg + searchoff, 1)
|
||||
let length = strlen(matchstr)
|
||||
|
||||
if length == 0 && beg == mid
|
||||
" Zero-length match for a zero-length delimiter - advance past it
|
||||
let searchoff += 1
|
||||
continue
|
||||
endif
|
||||
|
||||
if beg == mid
|
||||
let rv += [ "" ]
|
||||
else
|
||||
let rv += [ a:string[beg : mid-1] ]
|
||||
endif
|
||||
|
||||
let rv += [ matchstr ]
|
||||
|
||||
let beg = mid + length
|
||||
let searchoff = 0
|
||||
endwhile
|
||||
|
||||
let rv += [ strpart(a:string, beg) ]
|
||||
|
||||
return rv
|
||||
endfunction
|
||||
" }}}3
|
||||
|
||||
function! s:Align(lines) "{{{3
|
||||
let lines = map(a:lines, 's:Split(v:val, g:table_mode_separator)')
|
||||
|
||||
for line in lines
|
||||
if len(line) <= 1 | continue | endif
|
||||
|
||||
if line[0] !~ s:StartExpr()
|
||||
let line[0] = s:StripTrailingSpaces(line[0])
|
||||
endif
|
||||
if len(line) >= 2
|
||||
for i in range(1, len(line)-1)
|
||||
let line[i] = s:Strip(line[i])
|
||||
endfor
|
||||
endif
|
||||
endfor
|
||||
|
||||
let maxes = []
|
||||
for line in lines
|
||||
if len(line) <= 1 | continue | endif
|
||||
for i in range(len(line))
|
||||
if i == len(maxes)
|
||||
let maxes += [ s:Strlen(line[i]) ]
|
||||
else
|
||||
let maxes[i] = max([ maxes[i], s:Strlen(line[i]) ])
|
||||
endif
|
||||
endfor
|
||||
endfor
|
||||
|
||||
for idx in range(len(lines))
|
||||
let line = lines[idx]
|
||||
|
||||
if len(line) <= 1 | continue | endif
|
||||
for i in range(len(line))
|
||||
if line[i] !~# '[^0-9\.]'
|
||||
let field = s:Padding(line[i], maxes[i], 'r')
|
||||
else
|
||||
let field = s:Padding(line[i], maxes[i], 'l')
|
||||
endif
|
||||
|
||||
let line[i] = field . (i == 0 || i == len(line) ? '' : ' ')
|
||||
endfor
|
||||
|
||||
let lines[idx] = s:StripTrailingSpaces(join(line, ''))
|
||||
endfor
|
||||
|
||||
return lines
|
||||
endfunction
|
||||
" }}}3
|
||||
|
||||
" }}}2
|
||||
|
||||
" }}}1
|
||||
|
||||
" Public API {{{1
|
||||
|
||||
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)
|
||||
let column = s:Strlen(substitute(getline('.')[0:col('.')], '[^' . g:table_mode_separator . ']', '', 'g'))
|
||||
@@ -263,7 +665,7 @@ endfunction
|
||||
|
||||
function! tablemode#TableizeRange(...) range "{{{2
|
||||
let shift = 1
|
||||
if g:table_mode_border | let shift = shift + 1 | endif
|
||||
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
|
||||
@@ -290,37 +692,24 @@ endfunction
|
||||
" }}}2
|
||||
|
||||
function! tablemode#TableRealign(line) "{{{2
|
||||
let line = 0
|
||||
if type(a:line) == type('')
|
||||
let line = line(a:line)
|
||||
else
|
||||
let line = a:line
|
||||
endif
|
||||
let line = s:Line(a:line)
|
||||
|
||||
let [lnums, lines] = [[], []]
|
||||
let tline = line
|
||||
while tline > 0
|
||||
if tablemode#IsATableRow(tline)
|
||||
while tablemode#IsATableRow(tline)
|
||||
call insert(lnums, tline)
|
||||
call insert(lines, getline(tline))
|
||||
else
|
||||
break
|
||||
endif
|
||||
let tline = tline - s:RowGap()
|
||||
endwhile
|
||||
|
||||
let tline = line + s:RowGap()
|
||||
while tline <= line('$')
|
||||
if tablemode#IsATableRow(tline)
|
||||
while tablemode#IsATableRow(tline)
|
||||
call add(lnums, tline)
|
||||
call add(lines, getline(tline))
|
||||
else
|
||||
break
|
||||
endif
|
||||
let tline = tline + s:RowGap()
|
||||
endwhile
|
||||
|
||||
call tabular#TabularizeStrings(lines, g:table_mode_separator)
|
||||
let lines = s:Align(lines)
|
||||
|
||||
for lnum in lnums
|
||||
let index = index(lnums, lnum)
|
||||
@@ -340,22 +729,14 @@ function! tablemode#RowCount(line) "{{{2
|
||||
let line = s:Line(a:line)
|
||||
|
||||
let [tline, totalRowCount] = [line, 0]
|
||||
while tline > 0
|
||||
if tablemode#IsATableRow(tline)
|
||||
let totalRowCount = totalRowCount + 1
|
||||
else
|
||||
break
|
||||
endif
|
||||
while tablemode#IsATableRow(tline)
|
||||
let totalRowCount += 1
|
||||
let tline = tline - s:RowGap()
|
||||
endwhile
|
||||
|
||||
let tline = line + s:RowGap()
|
||||
while tline <= line('$')
|
||||
if tablemode#IsATableRow(tline)
|
||||
let totalRowCount = totalRowCount + 1
|
||||
else
|
||||
break
|
||||
endif
|
||||
while tablemode#IsATableRow(tline)
|
||||
let totalRowCount += 1
|
||||
let tline = tline + s:RowGap()
|
||||
endwhile
|
||||
|
||||
@@ -367,12 +748,8 @@ function! tablemode#RowNr(line) "{{{2
|
||||
let line = s:Line(a:line)
|
||||
|
||||
let rowNr = 0
|
||||
while line > 0
|
||||
if tablemode#IsATableRow(line)
|
||||
let rowNr = rowNr + 1
|
||||
else
|
||||
break
|
||||
endif
|
||||
while tablemode#IsATableRow(line)
|
||||
let rowNr += 1
|
||||
let line = line - s:RowGap()
|
||||
endwhile
|
||||
|
||||
@@ -396,8 +773,8 @@ function! tablemode#ColumnNr(pos) "{{{2
|
||||
else
|
||||
return 0
|
||||
endif
|
||||
|
||||
return s:Strlen(substitute(getline(pos[0])[0:pos[1]-2], '[^' . g:table_mode_separator . ']', '', 'g'))
|
||||
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
|
||||
|
||||
@@ -488,4 +865,155 @@ function! tablemode#DeleteRow() "{{{2
|
||||
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('.')
|
||||
|
||||
if fr !=# ''
|
||||
let fr = '$' . row . ',' . colm . '=' . fr
|
||||
let fline = tablemode#GetLastRow('.') + s:RowGap()
|
||||
let cursor_pos = [line('.'), col('.')]
|
||||
if getline(fline) =~# 'tmf: '
|
||||
" Comment line correctly
|
||||
let line_val = getline(fline)
|
||||
let line_expr = line_val[match(line_val, s:StartCommentExpr()):match(line_val, s:EndCommentExpr())]
|
||||
let sce = matchstr(line_val, s:StartCommentExpr() . '\zs')
|
||||
let ece = matchstr(line_val, s:EndCommentExpr())
|
||||
call setline(fline, sce . line_expr . '; ' . fr . ece)
|
||||
else
|
||||
let cstring = &commentstring
|
||||
let [cmss, cmse] = ['', '']
|
||||
if len(cstring) > 0
|
||||
let cms = split(cstring, '%s')
|
||||
if len(cms) == 2
|
||||
let [cmss, cmse] = cms
|
||||
else
|
||||
let [cmss, cmse] = [cms[0], '']
|
||||
endif
|
||||
endif
|
||||
let fr = 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)
|
||||
let [target, expr] = map(split(a:expr, '='), 's:Strip(v:val)')
|
||||
let cell = substitute(target, '\$', '', '')
|
||||
if cell =~# ','
|
||||
let [row, colm] = map(split(cell, ','), 'str2nr(v:val)')
|
||||
else
|
||||
let [row, colm] = [0, str2nr(cell)]
|
||||
endif
|
||||
|
||||
if expr =~# 'Sum(.*)'
|
||||
let expr = substitute(expr, 'Sum(\([^)]*\))', 'tablemode#Sum("\1",'.line.','.colm.')', 'g')
|
||||
endif
|
||||
|
||||
if expr =~# 'Average(.*)'
|
||||
let expr = substitute(expr, 'Average(\([^)]*\))', 'tablemode#Average("\1",'.line.','.colm.')', 'g')
|
||||
endif
|
||||
|
||||
if expr =~# '\$\d\+,\d\+'
|
||||
let expr = substitute(expr, '\$\(\d\+\),\(\d\+\)',
|
||||
\ '\=str2float(s:GetCells(line, submatch(1), submatch(2)))', 'g')
|
||||
endif
|
||||
|
||||
if cell =~# ','
|
||||
if expr =~# '\$'
|
||||
let expr = substitute(expr, '\$\(\d\+\)',
|
||||
\ '\=str2float(s:GetCells(line, row, submatch(1)))', 'g')
|
||||
endif
|
||||
call s:SetCell(eval(expr), line, row, colm)
|
||||
else
|
||||
let [row, line] = [1, s:GetFirstRow(line)]
|
||||
while tablemode#IsATableRow(line)
|
||||
let texpr = expr
|
||||
if expr =~# '\$'
|
||||
let texpr = substitute(texpr, '\$\(\d\+\)',
|
||||
\ '\=str2float(s:GetCells(line, row, submatch(1)))', 'g')
|
||||
endif
|
||||
|
||||
call s:SetCell(eval(texpr), line, row, colm)
|
||||
let row += 1
|
||||
let line += s:RowGap()
|
||||
endwhile
|
||||
endif
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
function! tablemode#EvaluateFormulaLine() "{{{2
|
||||
let exprs = []
|
||||
let cstring = &commentstring
|
||||
let matchexpr = ''
|
||||
if len(cstring) > 0
|
||||
let cms = split(cstring, '%s')
|
||||
if len(cms) == 2
|
||||
let matchexpr = '^\s*' . escape(cms[0], '/*') . '\s*tmf: \zs.*\ze' . escape(cms[1], '/*') . '\s*$'
|
||||
else
|
||||
let matchexpr = '^\s*' . escape(cms[0], '/*') . '\s*tmf: \zs.*$'
|
||||
endif
|
||||
else
|
||||
let matchexpr = '^\s* tmf: \zs.*$'
|
||||
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), ';')
|
||||
endif
|
||||
elseif getline('.') =~# 'tmf: ' " We're on the formula line
|
||||
let line = line('.') - s:RowGap()
|
||||
if tablemode#IsATableRow(line)
|
||||
let exprs = split(matchstr(getline('.'), matchexpr), ';')
|
||||
endif
|
||||
endif
|
||||
|
||||
for expr in exprs
|
||||
call tablemode#EvaluateExpr(expr, line)
|
||||
endfor
|
||||
endfunction
|
||||
" }}}2
|
||||
|
||||
" }}}1
|
||||
|
||||
" vim: sw=2 sts=2 fdl=0 fdm=marker
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
*table-mode.txt* Table Mode for easy table formatting. v2.4.0
|
||||
*table-mode.txt* Table Mode for easy table formatting. v3.0
|
||||
===============================================================================
|
||||
Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER
|
||||
VERSION 2.4.0
|
||||
VERSION 3.0
|
||||
|
||||
Author: Dhruva Sagar <http://dhruvasagar.com/>
|
||||
License: MIT <http://opensource.org/licenses/MIT/>
|
||||
@@ -9,13 +9,12 @@
|
||||
CONTENTS *table-mode-contents*
|
||||
|
||||
1. Introduction .................... |table-mode-introduction|
|
||||
2. Requirements .................... |table-mode-requirements|
|
||||
3. Getting Started ................. |table-mode-getting-started|
|
||||
4. Options ......................... |table-mode-options|
|
||||
5. Mappings ........................ |table-mode-mappings|
|
||||
6. Commands ........................ |table-mode-commands|
|
||||
7. Contributing .................... |table-mode-contributing|
|
||||
8. Report Issues ................... |table-mode-report-issues|
|
||||
2. Getting Started ................. |table-mode-getting-started|
|
||||
3. Options ......................... |table-mode-options|
|
||||
4. Mappings ........................ |table-mode-mappings|
|
||||
5. Commands ........................ |table-mode-commands|
|
||||
6. Contributing .................... |table-mode-contributing|
|
||||
7. Report Issues ................... |table-mode-report-issues|
|
||||
|
||||
===============================================================================
|
||||
INTRODUCTION *table-mode-introduction*
|
||||
@@ -25,12 +24,6 @@ inspired from tpope's auto aligning script for creating tables in vim -
|
||||
https://gist.github.com/tpope/287147, which in turn utilizes the Tabular
|
||||
Plugin.
|
||||
|
||||
===============================================================================
|
||||
REQUIREMENTS *table-mode-requirements*
|
||||
|
||||
This depends on the Tabular plugin - https://github.com/godlygeek/tabular,
|
||||
make sure it is installed and loaded.
|
||||
|
||||
===============================================================================
|
||||
GETTING STARTED *table-mode-getting-started*
|
||||
|
||||
@@ -43,7 +36,7 @@ Create Table on the fly:
|
||||
|
||||
The table mode is disabled by default and you can enter table mode
|
||||
using |table-mode-toggle-map| or you can also enable it permanently
|
||||
using |g:table-mode-always-active| if you wish.
|
||||
using |table-mode-always-active| if you wish.
|
||||
|
||||
Table Mode allows for creation of tables within comments, it looks at
|
||||
the 'commentstring' setting to identify whether the current line is
|
||||
@@ -53,6 +46,14 @@ Tableize content:
|
||||
Table Mode enables conversion of delimited text into tables. Again
|
||||
like table creation, this is also applicable within comments.
|
||||
|
||||
Move between cells :
|
||||
Now you can move between cells using table mode motions
|
||||
'<Leader>t[hjkl]' to move left | down | up | right cells respectively.
|
||||
You can use |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 the next | previous row after the last | first
|
||||
cell in the current row if one exists.
|
||||
|
||||
Manipulation of tables:
|
||||
Tableize provides 3 easy ways to quickly manipulate tables.
|
||||
|
||||
@@ -69,6 +70,61 @@ Manipulation of tables:
|
||||
3. Delete Row : Delete an entire table row using
|
||||
|table-mode-delete-row-map|
|
||||
|
||||
Table Formulas:
|
||||
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| defined by the option
|
||||
|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 one will be created with the input formula
|
||||
taking the current cell as the target cell. The formula line
|
||||
is evaluated immidiately to reflect the results.
|
||||
|
||||
You can directly also add / manipulate formula expressions in
|
||||
the formula line. The formula line is a commented line right
|
||||
after the table, beginning with 'tmf:' (table mode formula).
|
||||
eg) '# tmf: $3=$2*$1'. You can add multiple 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 |table-mode-expr-calc-map|
|
||||
|
||||
Formula Expressions :
|
||||
Expressions are of the format '$target = formula'.
|
||||
|
||||
The target can be of 2 forms :
|
||||
|
||||
'$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 in it.
|
||||
|
||||
'$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 cell.
|
||||
|
||||
The formula can be a simple mathematical expression involving cells
|
||||
which are also defined by the same format as that of the target cell.
|
||||
Apart from basic mathematical expressions, table mode also provides
|
||||
special functions 'Sum' and 'Average'. Both these functions take a
|
||||
range as input. A range can be of two forms :
|
||||
|
||||
'n,m': This represents cells in the current column from row
|
||||
'n' through 'm'. If 'm' is negative it represents 'm' row
|
||||
above the current row (of the target cell).
|
||||
|
||||
'r1,c1:r2,c2': This represents cells in the table from cell
|
||||
r1,c1 through cell r2,c2 (row, column).
|
||||
|
||||
Examples :
|
||||
$2 = $1 * $1
|
||||
$2 = $1 / $1,3
|
||||
$1,2 = $1,1 * $1,1
|
||||
$5,1 = Sum(1:-1)
|
||||
$5,3 = Sum(1,2:5,2)
|
||||
|
||||
===============================================================================
|
||||
OPTIONS *table-mode-options*
|
||||
|
||||
@@ -78,24 +134,25 @@ Overview:
|
||||
|table-mode-corner| ............. Set corner character.
|
||||
|table-mode-separator| .......... Set separator character.
|
||||
|table-mode-fillchar| ........... Set table fillchar character.
|
||||
|table-mode-map-prefix| ......... Set prefix for table mode commands.
|
||||
|table-mode-toggle-map| ......... Set table mode toggle mapping.
|
||||
|table-mode-always-active| ...... Set table mode to always enabled.
|
||||
|table-mode-delimiter| .......... Set the delimiter for Tableize.
|
||||
|table-mode-tableize-map| ....... Set mapping for Tableize.
|
||||
|table-mode-tableize-op-map| .... Set mapping for Tableize with input.
|
||||
|table-mode-align| .............. Set the text alignment for
|
||||
Tableize.
|
||||
|table-mode-no-border-padding| .. Set for no border padding.
|
||||
|table-mode-realign-map| ........ Set mapping for table realigning.
|
||||
|table-mode-motion-prefix| ...... Set prefix for table mode motions.
|
||||
|table-mode-cell-text-object| ... Set mapping for table cell object.
|
||||
|table-mode-delete-row-map| ..... Set mapping for deleting table row.
|
||||
|table-mode-delete-column-map| .. Set mapping for deleting table
|
||||
column.
|
||||
|table-mode-add-formula-map| .... Define a new table formula for the
|
||||
current cell.
|
||||
|table-mode-eval-expr-map| ...... Evaluate formula line and update
|
||||
table.
|
||||
|
||||
g:table_mode_loaded *table-mode-loaded*
|
||||
g:loaded_table_mode *table-mode-loaded*
|
||||
Use this option to disable the plugin: >
|
||||
let g:table_mode_loaded = 1
|
||||
let g:loaded_table_mode = 1
|
||||
<
|
||||
|
||||
g:table_mode_border *table-mode-border*
|
||||
@@ -119,12 +176,21 @@ g:table_mode_fillchar *table-mode-fillchar*
|
||||
let g:table_mode_fillchar = '-'
|
||||
<
|
||||
|
||||
g:table_mode_map_prefix *table-mode-map-prefix*
|
||||
Use this option to define the table mode mapping prefix that will be
|
||||
prefixed for all other table mode mappings. >
|
||||
let g:table_mode_map_prefix = '<Leader>t'
|
||||
<
|
||||
|
||||
g:table_mode_toggle_map *table-mode-toggle-map*
|
||||
Use this option to define the mapping for toggling the table mode: >
|
||||
let g:table_mode_toggle_map = '<Leader>tm'
|
||||
let g:table_mode_toggle_map = 'm'
|
||||
<
|
||||
Read |table-mode-mappings-toggle| for more info.
|
||||
|
||||
NOTE you will need to use the |table-mode-map-prefix| before this to
|
||||
get the desired effect.
|
||||
|
||||
g:table_mode_always_active *table-mode-always-active*
|
||||
Use this option to permanently enable the table mode: >
|
||||
let g:table_mode_always_active = 0
|
||||
@@ -145,7 +211,7 @@ g:table_mode_delimiter *table-mode-delimiter*
|
||||
g:table_mode_tableize_map *table-mode-tableize-map*
|
||||
Use this option to define the mapping to invoke |:Tableize| with
|
||||
default delimiter, i.e. |:Tableize| without input. >
|
||||
let g:table_mode_tableize_map = '<Leader>tt'
|
||||
let g:table_mode_tableize_map = 't'
|
||||
<
|
||||
|
||||
g:table_mode_tableize_op_map *table-mode-tableize-op-map*
|
||||
@@ -155,37 +221,13 @@ g:table_mode_tableize_op_map *table-mode-tableize-op-map*
|
||||
let g:table_mode_tableize_op_map = '<Leader>T'
|
||||
<
|
||||
|
||||
g:table_mode_align *table-mode-align*
|
||||
Use this option to define the format for text alignment to be used for
|
||||
the tables. Go through |tabular-walkthrough| for details on how to set
|
||||
the format options for alignment. >
|
||||
let g:table_mode_align = 'l1'
|
||||
<
|
||||
|
||||
g:table_mode_no_border_padding *table-mode-no-border-padding*
|
||||
Use this option to remove the border padding (extra spaces around the
|
||||
|table-mode-fillchar|). >
|
||||
let g:table_mode_no_border_padding = 0
|
||||
<
|
||||
NOTE this option changes |table-mode-align| to 'c0', so that
|
||||
there is no extra padding around the contents.
|
||||
|
||||
g:table_mode_realign_map *table-mode-realign-map*
|
||||
Use this option to define the mapping for realigning table columns.
|
||||
This is useful in case you make edits to an existing table. >
|
||||
let g:table_mode_realign_map = '<Leader>tr'
|
||||
let g:table_mode_realign_map = 'r'
|
||||
<
|
||||
|
||||
g:table_mode_motion_prefix *table-mode-motion-prefix*
|
||||
Use this option to define the prefix for table mode motion commands. >
|
||||
let g:table_mode_motion_prefix = '<Leader>t'
|
||||
<
|
||||
You can move to the next / previous row / column using the motion
|
||||
commands. The motions 'hjkl' follow the prefix are in accordance to
|
||||
standard vim character motions to make them easier to remember. They
|
||||
may also be preceeded with a [count]. The 'h', 'l' (left and right
|
||||
motions) wrap around the table row and move to the previous rows last
|
||||
column, next rows first column respectively if one exists.
|
||||
NOTE you will need to use the |table-mode-map-prefix| before this to
|
||||
get the desired effect.
|
||||
|
||||
g:table_mode_cell_text_object *table-mode-cell-text-object*
|
||||
Use this option to define the table mode cell text object. >
|
||||
@@ -207,18 +249,43 @@ g:table_mode_cell_text_object *table-mode-cell-text-object*
|
||||
g:table_mode_delete_row_map *table-mode-delete-row-map*
|
||||
Use this option to define the mapping for deletion of the entire table
|
||||
row. You can delete multiple rows by preceeding this with a [count]. >
|
||||
let g:table_mode_delete_column_map = '<Leader>tdd'
|
||||
let g:table_mode_delete_column_map = 'dd'
|
||||
<
|
||||
NOTE you will need to use the |table-mode-map-prefix| before this to
|
||||
get the desired effect.
|
||||
|
||||
g:table_mode_delete_column_map *table-mode-delete-column-map*
|
||||
USe this option to define the mapping for deletion of the entire table
|
||||
Use this option to define the mapping for deletion of the entire table
|
||||
column. You can delete multiple columns to the right by preceeding
|
||||
this with a [count] >
|
||||
let g:table_mode_delete_column_map = '<Leader>tdc'
|
||||
let g:table_mode_delete_column_map = 'dc'
|
||||
<
|
||||
NOTE you will need to use the |table-mode-map-prefix| before this to
|
||||
get the desired effect.
|
||||
|
||||
g:table_mode_add_formula_map *table-mode-add-formula-map*
|
||||
Use this option to define the mapping for invoking |TableAddFormula|
|
||||
command. >
|
||||
let g:table_mode_add_formula_map = 'fa'
|
||||
<
|
||||
NOTE you will need to use the |table-mode-map-prefix| before this to
|
||||
get the desired effect.
|
||||
|
||||
g:table_mode_eval_expr_map *table-mode-eval-expr-map*
|
||||
Use this option to define the mapping for invoking
|
||||
|TableEvalFormulaLine| command. >
|
||||
let g:table_mode_eval_expr_map = 'fe'
|
||||
<
|
||||
NOTE you will need to use the |table-mode-map-prefix| before this to
|
||||
get the desired effect.
|
||||
|
||||
===============================================================================
|
||||
MAPPINGS *table-mode-mappings*
|
||||
|
||||
*table-mode-mappings-prefix*
|
||||
<Leader>t This is a prefix defined by the option |table-mode-map-prefix|
|
||||
used before all other table mode commands.
|
||||
|
||||
*table-mode-mappings-toggle*
|
||||
<Leader>tm Toggle table mode for the current buffer. You can change this
|
||||
using the |toggle-mode-options-toggle-map| option.
|
||||
@@ -253,6 +320,16 @@ MAPPINGS *table-mode-mappings*
|
||||
with a [count] to delete multiple columns to the right. You
|
||||
can change this using |table-mode-delete-column-map| option.
|
||||
|
||||
*table-mode-mappings-add-formula*
|
||||
<Leader>tfa Add a fomula for the current table cell. This invokes
|
||||
|TableAddFormula| command.
|
||||
|
||||
*table-mode-mappings-evaluate-formula-line*
|
||||
<Leader>tfe Evaluate the formula line which is a commented line right
|
||||
after the table beginning with 'tmf:'. If one exists this
|
||||
would evaluate the formula line and update the table
|
||||
accordingly. This invokes the |TableEvalFormulaLine| command.
|
||||
|
||||
===============================================================================
|
||||
COMMANDS *table-mode-commands*
|
||||
|
||||
@@ -294,7 +371,22 @@ COMMANDS *table-mode-commands*
|
||||
The above command will Tableize using ';' as the delimiter.
|
||||
|
||||
NOTE this is optional, by default without the expression it will
|
||||
tableize the content using |g:table-mode-delimiter| as the delimiter.
|
||||
tableize the content using |table-mode-delimiter| as the delimiter.
|
||||
|
||||
*:TableAddFormula*
|
||||
*table-mode-:TableAddFormula*
|
||||
:TableAddFormula
|
||||
This command is for defining a formula for the current table cell. It
|
||||
takes input on the cmd-line with a 'f=' prompt and appends it to the
|
||||
formula line if it exists or adds a new formula line with the
|
||||
expression using the current cell as the target and the input formula.
|
||||
|
||||
*:TableEvalFormulaLine*
|
||||
*table-mode-:TableEvalFormulaLine*
|
||||
:TableEvalFormulaLine
|
||||
This command when invoked from anywhere within the table or directly
|
||||
on the formula line evaluates it and updates the table accordingly.
|
||||
|
||||
===============================================================================
|
||||
CONTRIBUTING *table-mode-contributing*
|
||||
|
||||
|
||||
@@ -20,18 +20,11 @@
|
||||
" =============================================================================
|
||||
|
||||
" Finish if already loaded {{{1
|
||||
if exists('g:table_mode_loaded')
|
||||
if exists('g:loaded_table_mode')
|
||||
finish
|
||||
endif
|
||||
let g:table_mode_loaded = 1
|
||||
let g:loaded_table_mode = 1
|
||||
"}}}1
|
||||
"
|
||||
" Finish if Tabularize plugin is not available {{{1
|
||||
if !exists(':Tabularize')
|
||||
echoerr 'Table Mode depends on Tabularize, ensure that is installed first.'
|
||||
finish
|
||||
endif
|
||||
" }}}1
|
||||
|
||||
" Avoiding side effects {{{1
|
||||
let s:save_cpo = &cpo
|
||||
@@ -50,17 +43,18 @@ 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', '-')
|
||||
call s:SetGlobalOptDefault('table_mode_toggle_map', '<LocalLeader>tm')
|
||||
call s:SetGlobalOptDefault('table_mode_map_prefix', '<Leader>t')
|
||||
call s:SetGlobalOptDefault('table_mode_toggle_map', 'm')
|
||||
call s:SetGlobalOptDefault('table_mode_always_active', 0)
|
||||
call s:SetGlobalOptDefault('table_mode_delimiter', ',')
|
||||
call s:SetGlobalOptDefault('table_mode_tableize_map', '<Leader>tt')
|
||||
call s:SetGlobalOptDefault('table_mode_tableize_map', 't')
|
||||
call s:SetGlobalOptDefault('table_mode_tableize_op_map', '<Leader>T')
|
||||
call s:SetGlobalOptDefault('table_mode_align', 'l1')
|
||||
call s:SetGlobalOptDefault('table_mode_realign_map', '<Leader>tr')
|
||||
call s:SetGlobalOptDefault('table_mode_motion_prefix', '<Leader>t')
|
||||
call s:SetGlobalOptDefault('table_mode_realign_map', 'r')
|
||||
call s:SetGlobalOptDefault('table_mode_cell_text_object', 'tc')
|
||||
call s:SetGlobalOptDefault('table_mode_delete_row_map', '<Leader>tdd')
|
||||
call s:SetGlobalOptDefault('table_mode_delete_column_map', '<Leader>tdc')
|
||||
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
|
||||
|
||||
function! s:TableMotion() "{{{1
|
||||
@@ -73,7 +67,7 @@ endfunction
|
||||
|
||||
" Define Commands & Mappings {{{1
|
||||
if !g:table_mode_always_active "{{{2
|
||||
exec "nnoremap <silent> " . g:table_mode_toggle_map .
|
||||
exec "nnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_toggle_map .
|
||||
\ " <Esc>:call tablemode#TableModeToggle()<CR>"
|
||||
command! -nargs=0 TableModeToggle call tablemode#TableModeToggle()
|
||||
command! -nargs=0 TableModeEnable call tablemode#TableModeEnable()
|
||||
@@ -91,17 +85,29 @@ endif
|
||||
|
||||
command! -nargs=? -range Tableize <line1>,<line2>call tablemode#TableizeRange(<q-args>)
|
||||
|
||||
execute "xnoremap <silent> " . g:table_mode_tableize_map . " :Tableize<CR>"
|
||||
execute "nnoremap <silent> " . g:table_mode_tableize_map . " :Tableize<CR>"
|
||||
execute "xnoremap <silent> " . g:table_mode_tableize_op_map . " :<C-U>call tablemode#TableizeByDelimiter()<CR>"
|
||||
command! TableAddFormula call tablemode#AddFormula()
|
||||
command! TableEvalFormulaLine call tablemode#EvaluateFormulaLine()
|
||||
|
||||
execute "nnoremap <silent> " . g:table_mode_realign_map . " :<C-U>call tablemode#TableRealign('.')<CR>"
|
||||
execute "nnoremap <silent> " . g:table_mode_motion_prefix . " :<C-U>call <SID>TableMotion()<CR>"
|
||||
|
||||
execute "onoremap <silent> " . g:table_mode_cell_text_object . " :<C-U>call tablemode#CellTextObject()<CR>"
|
||||
|
||||
execute "nnoremap <silent> " . g:table_mode_delete_row_map . " :<C-U>call tablemode#DeleteRow()<CR>"
|
||||
execute "nnoremap <silent> " . g:table_mode_delete_column_map . " :<C-U>call tablemode#DeleteColumn()<CR>"
|
||||
execute "xnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_tableize_map .
|
||||
\ " :Tableize<CR>"
|
||||
execute "nnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_tableize_map .
|
||||
\ " :Tableize<CR>"
|
||||
execute "xnoremap <silent> " . g:table_mode_tableize_op_map .
|
||||
\ " :<C-U>call tablemode#TableizeByDelimiter()<CR>"
|
||||
execute "nnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_realign_map .
|
||||
\ " :call tablemode#TableRealign('.')<CR>"
|
||||
execute "nnoremap <silent> " . g:table_mode_map_prefix .
|
||||
\ " :call <SID>TableMotion()<CR>"
|
||||
execute "onoremap <silent> " . g:table_mode_cell_text_object .
|
||||
\ " :<C-U>call tablemode#CellTextObject()<CR>"
|
||||
execute "nnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_delete_row_map .
|
||||
\ " :call tablemode#DeleteRow()<CR>"
|
||||
execute "nnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_delete_column_map .
|
||||
\ " :call tablemode#DeleteColumn()<CR>"
|
||||
execute "nnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_add_formula_map .
|
||||
\ " :TableAddFormula<CR>"
|
||||
execute "nnoremap <silent> " . g:table_mode_map_prefix . g:table_mode_eval_expr_map .
|
||||
\ " :TableEvalFormulaLine<CR>"
|
||||
"}}}1
|
||||
|
||||
" Avoiding side effects {{{1
|
||||
@@ -109,4 +115,4 @@ let &cpo = s:save_cpo
|
||||
" }}}1
|
||||
|
||||
" ModeLine {{{
|
||||
" vim:fdl=0 fdm=marker
|
||||
" vim: sw=2 sts=2 fdl=0 fdm=marker
|
||||
Reference in New Issue
Block a user