mirror of
https://github.com/dhruvasagar/vim-table-mode.git
synced 2025-11-08 11:03:47 -05:00
Add support for inserting columns #130
Two mappings are introduced to enable usage of this feature like vim's pasting with p and P.
This commit is contained in:
10
README.md
10
README.md
@@ -177,6 +177,16 @@ it using `:TableModeRealign` or using the default mapping
|
|||||||
(provided you are within a table row), this can also be preceeded with a
|
(provided you are within a table row), this can also be preceeded with a
|
||||||
[count] to delete multiple columns.
|
[count] to delete multiple columns.
|
||||||
|
|
||||||
|
- **Insert Column** :
|
||||||
|
|
||||||
|
You can use the <kbd>\<Leader\>tic</kbd> mapping defined by the option
|
||||||
|
`g:table_mode_insert_column_after_map` to insert a column after the
|
||||||
|
cursor (provided you are within a table row). Of course you can use the
|
||||||
|
<kbd>\<Leader\>tiC</kbd> mapping defined by
|
||||||
|
`g:table_mode_insert_column_before_map` to insert a column before the
|
||||||
|
cursor. Both can also be preceeded with a [count] to insert multiple
|
||||||
|
columns.
|
||||||
|
|
||||||
## Advanced Usage: Spreadsheet Capabilities
|
## Advanced Usage: Spreadsheet Capabilities
|
||||||
|
|
||||||
### Table Formulas
|
### Table Formulas
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ function! s:ToggleMapping() "{{{2
|
|||||||
call s:Map('<Plug>(table-mode-realign)', g:table_mode_realign_map, 'n')
|
call s:Map('<Plug>(table-mode-realign)', g:table_mode_realign_map, 'n')
|
||||||
call s:Map('<Plug>(table-mode-delete-row)', g:table_mode_delete_row_map, 'n')
|
call s:Map('<Plug>(table-mode-delete-row)', g:table_mode_delete_row_map, 'n')
|
||||||
call s:Map('<Plug>(table-mode-delete-column)', g:table_mode_delete_column_map, 'n')
|
call s:Map('<Plug>(table-mode-delete-column)', g:table_mode_delete_column_map, 'n')
|
||||||
|
call s:Map('<Plug>(table-mode-insert-column-before)', g:table_mode_insert_column_before_map, 'n')
|
||||||
|
call s:Map('<Plug>(table-mode-insert-column-after)', g:table_mode_insert_column_after_map, 'n')
|
||||||
call s:Map('<Plug>(table-mode-add-formula)', g:table_mode_add_formula_map, 'n')
|
call s:Map('<Plug>(table-mode-add-formula)', g:table_mode_add_formula_map, 'n')
|
||||||
call s:Map('<Plug>(table-mode-eval-formula)', g:table_mode_eval_formula_map, 'n')
|
call s:Map('<Plug>(table-mode-eval-formula)', g:table_mode_eval_formula_map, 'n')
|
||||||
call s:Map('<Plug>(table-mode-echo-cell)', g:table_mode_echo_cell_map, 'n')
|
call s:Map('<Plug>(table-mode-echo-cell)', g:table_mode_echo_cell_map, 'n')
|
||||||
@@ -57,6 +59,8 @@ function! s:ToggleMapping() "{{{2
|
|||||||
call s:UnMap(g:table_mode_realign_map, 'n')
|
call s:UnMap(g:table_mode_realign_map, 'n')
|
||||||
call s:UnMap(g:table_mode_delete_row_map, 'n')
|
call s:UnMap(g:table_mode_delete_row_map, 'n')
|
||||||
call s:UnMap(g:table_mode_delete_column_map, 'n')
|
call s:UnMap(g:table_mode_delete_column_map, 'n')
|
||||||
|
call s:UnMap(g:table_mode_insert_column_before_map, 'n')
|
||||||
|
call s:UnMap(g:table_mode_insert_column_after_map, 'n')
|
||||||
call s:UnMap(g:table_mode_add_formula_map, 'n')
|
call s:UnMap(g:table_mode_add_formula_map, 'n')
|
||||||
call s:UnMap(g:table_mode_eval_formula_map, 'n')
|
call s:UnMap(g:table_mode_eval_formula_map, 'n')
|
||||||
call s:UnMap(g:table_mode_echo_cell_map, 'n')
|
call s:UnMap(g:table_mode_echo_cell_map, 'n')
|
||||||
|
|||||||
@@ -187,6 +187,57 @@ function! tablemode#spreadsheet#DeleteRow() "{{{2
|
|||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function! tablemode#spreadsheet#InsertColumn(after) "{{{2
|
||||||
|
if tablemode#table#IsRow('.')
|
||||||
|
let quantity = v:count1
|
||||||
|
|
||||||
|
call tablemode#spreadsheet#MoveToFirstRowOrHeader()
|
||||||
|
call tablemode#spreadsheet#MoveToStartOfCell()
|
||||||
|
if a:after
|
||||||
|
execute "normal! f".g:table_mode_separator
|
||||||
|
endif
|
||||||
|
execute "normal! h\<C-V>"
|
||||||
|
call tablemode#spreadsheet#MoveToLastRow()
|
||||||
|
normal! y
|
||||||
|
|
||||||
|
let corner = tablemode#utils#get_buffer_or_global_option('table_mode_corner')
|
||||||
|
if a:after
|
||||||
|
let cell_line = g:table_mode_separator.' '
|
||||||
|
let header_line = corner.g:table_mode_fillchar.g:table_mode_fillchar
|
||||||
|
else
|
||||||
|
let cell_line = ' '.g:table_mode_separator
|
||||||
|
let header_line = g:table_mode_fillchar.g:table_mode_fillchar.corner
|
||||||
|
endif
|
||||||
|
let cell_line = escape(cell_line, '\&')
|
||||||
|
let header_line = escape(header_line, '\&')
|
||||||
|
|
||||||
|
" This transforms the character column before or after the column separator
|
||||||
|
" into a new column with separator.
|
||||||
|
" This requires, that
|
||||||
|
" g:table_mode_separator != g:table_mode_fillchar
|
||||||
|
" && g:table_mode_separator != g:table_mode_header_fillchar
|
||||||
|
" && g:table_mode_separator != g:table_mode_align_char
|
||||||
|
call setreg(
|
||||||
|
\ '"',
|
||||||
|
\ substitute(
|
||||||
|
\ substitute(@", ' ', cell_line, 'g'),
|
||||||
|
\ '\V\C'.escape(g:table_mode_fillchar, '\')
|
||||||
|
\ .'\|'.escape(g:table_mode_header_fillchar, '\')
|
||||||
|
\ .'\|'.escape(g:table_mode_align_char, '\'),
|
||||||
|
\ header_line,
|
||||||
|
\ 'g'),
|
||||||
|
\ 'b')
|
||||||
|
|
||||||
|
if a:after
|
||||||
|
execute "normal! ".quantity."p2l"
|
||||||
|
else
|
||||||
|
execute "normal! ".quantity."Pl"
|
||||||
|
endif
|
||||||
|
|
||||||
|
call tablemode#table#Realign('.')
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
function! tablemode#spreadsheet#Sum(range, ...) abort "{{{2
|
function! tablemode#spreadsheet#Sum(range, ...) abort "{{{2
|
||||||
let args = copy(a:000)
|
let args = copy(a:000)
|
||||||
call insert(args, a:range)
|
call insert(args, a:range)
|
||||||
|
|||||||
@@ -75,6 +75,10 @@ Manipulation of tables:
|
|||||||
3. Delete Row : Delete an entire table row using
|
3. Delete Row : Delete an entire table row using
|
||||||
|table-mode-delete-row-map|
|
|table-mode-delete-row-map|
|
||||||
|
|
||||||
|
4. Insert Column : Insert a table column either before the cursor
|
||||||
|
using |table-mode-insert-column-before-map| or after the cusor using
|
||||||
|
|table-mode-insert-column-after-map|.
|
||||||
|
|
||||||
Table Formulas:
|
Table Formulas:
|
||||||
Table Mode now has support for formulas like a spreadsheet. There
|
Table Mode now has support for formulas like a spreadsheet. There
|
||||||
are 2 ways of defining formulas :
|
are 2 ways of defining formulas :
|
||||||
@@ -165,6 +169,12 @@ Overview:
|
|||||||
|table-mode-realign-map| ........ Set the realign mapping
|
|table-mode-realign-map| ........ Set the realign mapping
|
||||||
|table-mode-delete-row-map| ..... Set the delete row mapping
|
|table-mode-delete-row-map| ..... Set the delete row mapping
|
||||||
|table-mode-delete-column-map| .. Set the delete column mapping
|
|table-mode-delete-column-map| .. Set the delete column mapping
|
||||||
|
|table-mode-insert-column-before-map|
|
||||||
|
Set the insert column before the
|
||||||
|
cursor mapping
|
||||||
|
|table-mode-insert-column-after-map|
|
||||||
|
Set the insert column after the
|
||||||
|
cursor mapping
|
||||||
|table-mode-add-formula-map| .... Set the add formula mapping
|
|table-mode-add-formula-map| .... Set the add formula mapping
|
||||||
|table-mode-eval-formula-map| ... Set the eval formula mapping
|
|table-mode-eval-formula-map| ... Set the eval formula mapping
|
||||||
|table-mode-echo-cell-map| ...... Set the echo cell mapping
|
|table-mode-echo-cell-map| ...... Set the echo cell mapping
|
||||||
@@ -286,6 +296,16 @@ g:table_mode_delete_column_map *table-mode-delete-column-map*
|
|||||||
Set this to configure the mapping for deleting a table column. >
|
Set this to configure the mapping for deleting a table column. >
|
||||||
let g:table_mode_delete_column_map = '<Leader>tdc'
|
let g:table_mode_delete_column_map = '<Leader>tdc'
|
||||||
>
|
>
|
||||||
|
g:table_mode_insert_column_before_map *table-mode-insert-column-before-map*
|
||||||
|
Set this to configure the mapping for inserting a table column before
|
||||||
|
the cursor. >
|
||||||
|
let g:table_mode_insert_column_before_map = '<Leader>tiC'
|
||||||
|
>
|
||||||
|
g:table_mode_insert_column_after_map *table-mode-insert-column-after-map*
|
||||||
|
Set this to configure the mapping for inserting a table column after
|
||||||
|
the cursor. >
|
||||||
|
let g:table_mode_insert_column_after_map = '<Leader>tic'
|
||||||
|
>
|
||||||
g:table_mode_add_formula_map *table-mode-add-formula-map*
|
g:table_mode_add_formula_map *table-mode-add-formula-map*
|
||||||
Set this to configure the mapping for adding a formula for a table
|
Set this to configure the mapping for adding a formula for a table
|
||||||
cell. >
|
cell. >
|
||||||
@@ -379,6 +399,16 @@ MAPPINGS *table-mode-mappings*
|
|||||||
with a [count] to delete multiple columns to the right. You
|
with a [count] to delete multiple columns to the right. You
|
||||||
can change this using |table-mode-delete-column-map| option.
|
can change this using |table-mode-delete-column-map| option.
|
||||||
|
|
||||||
|
*table-mode-mappings-insert-column-before*
|
||||||
|
<Leader>tiC Insert a table column before the column you are within. You can
|
||||||
|
preceed it with a [count] to insert multiple columns. You can
|
||||||
|
change this using |table-mode-insert-column-before-map| option.
|
||||||
|
|
||||||
|
*table-mode-mappings-insert-column-after*
|
||||||
|
<Leader>tic Insert a table column after the column you are within. You can
|
||||||
|
preceed it with a [count] to insert multiple columns. You can
|
||||||
|
change this using |table-mode-insert-column-after-map| option.
|
||||||
|
|
||||||
*table-mode-mappings-add-formula*
|
*table-mode-mappings-add-formula*
|
||||||
<Leader>tfa Add a fomula for the current table cell. This invokes
|
<Leader>tfa Add a fomula for the current table cell. This invokes
|
||||||
|TableAddFormula| command.
|
|TableAddFormula| command.
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ call s:SetGlobalOptDefault('table_mode_cell_text_object_i_map', 'i<Bar>')
|
|||||||
call s:SetGlobalOptDefault('table_mode_realign_map', g:table_mode_map_prefix.'r')
|
call s:SetGlobalOptDefault('table_mode_realign_map', g:table_mode_map_prefix.'r')
|
||||||
call s:SetGlobalOptDefault('table_mode_delete_row_map', g:table_mode_map_prefix.'dd')
|
call s:SetGlobalOptDefault('table_mode_delete_row_map', g:table_mode_map_prefix.'dd')
|
||||||
call s:SetGlobalOptDefault('table_mode_delete_column_map', g:table_mode_map_prefix.'dc')
|
call s:SetGlobalOptDefault('table_mode_delete_column_map', g:table_mode_map_prefix.'dc')
|
||||||
|
call s:SetGlobalOptDefault('table_mode_insert_column_before_map', g:table_mode_map_prefix.'iC')
|
||||||
|
call s:SetGlobalOptDefault('table_mode_insert_column_after_map', g:table_mode_map_prefix.'ic')
|
||||||
call s:SetGlobalOptDefault('table_mode_add_formula_map', g:table_mode_map_prefix.'fa')
|
call s:SetGlobalOptDefault('table_mode_add_formula_map', g:table_mode_map_prefix.'fa')
|
||||||
call s:SetGlobalOptDefault('table_mode_eval_formula_map', g:table_mode_map_prefix.'fe')
|
call s:SetGlobalOptDefault('table_mode_eval_formula_map', g:table_mode_map_prefix.'fe')
|
||||||
call s:SetGlobalOptDefault('table_mode_echo_cell_map', g:table_mode_map_prefix.'?')
|
call s:SetGlobalOptDefault('table_mode_echo_cell_map', g:table_mode_map_prefix.'?')
|
||||||
@@ -102,6 +104,8 @@ xnoremap <silent> <Plug>(table-mode-cell-text-object-i) :<C-U>call tablemode#spr
|
|||||||
|
|
||||||
nnoremap <silent> <Plug>(table-mode-delete-row) :<C-U>call tablemode#spreadsheet#DeleteRow()<CR>
|
nnoremap <silent> <Plug>(table-mode-delete-row) :<C-U>call tablemode#spreadsheet#DeleteRow()<CR>
|
||||||
nnoremap <silent> <Plug>(table-mode-delete-column) :<C-U>call tablemode#spreadsheet#DeleteColumn()<CR>
|
nnoremap <silent> <Plug>(table-mode-delete-column) :<C-U>call tablemode#spreadsheet#DeleteColumn()<CR>
|
||||||
|
nnoremap <silent> <Plug>(table-mode-insert-column-before) :<C-U>call tablemode#spreadsheet#InsertColumn(0)<CR>
|
||||||
|
nnoremap <silent> <Plug>(table-mode-insert-column-after) :<C-U>call tablemode#spreadsheet#InsertColumn(1)<CR>
|
||||||
|
|
||||||
nnoremap <silent> <Plug>(table-mode-add-formula) :call tablemode#spreadsheet#formula#Add()<CR>
|
nnoremap <silent> <Plug>(table-mode-add-formula) :call tablemode#spreadsheet#formula#Add()<CR>
|
||||||
nnoremap <silent> <Plug>(table-mode-eval-formula) :call tablemode#spreadsheet#formula#EvaluateFormulaLine()<CR>
|
nnoremap <silent> <Plug>(table-mode-eval-formula) :call tablemode#spreadsheet#formula#EvaluateFormulaLine()<CR>
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ let g:table_mode_cell_text_object_i_map = 'i<Bar>'
|
|||||||
let g:table_mode_realign_map = '<Leader>tr'
|
let g:table_mode_realign_map = '<Leader>tr'
|
||||||
let g:table_mode_delete_row_map = '<Leader>tdd'
|
let g:table_mode_delete_row_map = '<Leader>tdd'
|
||||||
let g:table_mode_delete_column_map = '<Leader>tdc'
|
let g:table_mode_delete_column_map = '<Leader>tdc'
|
||||||
|
let g:table_mode_insert_column_before_map = '<Leader>tiC'
|
||||||
|
let g:table_mode_insert_column_after_map = '<Leader>tic'
|
||||||
let g:table_mode_add_formula_map = '<Leader>tfa'
|
let g:table_mode_add_formula_map = '<Leader>tfa'
|
||||||
let g:table_mode_eval_formula_map = '<Leader>tfe'
|
let g:table_mode_eval_formula_map = '<Leader>tfe'
|
||||||
let g:table_mode_echo_cell_map = '<Leader>t?'
|
let g:table_mode_echo_cell_map = '<Leader>t?'
|
||||||
|
|||||||
@@ -101,6 +101,21 @@ describe 'spreadsheet'
|
|||||||
call tablemode#spreadsheet#DeleteColumn()
|
call tablemode#spreadsheet#DeleteColumn()
|
||||||
Expect tablemode#spreadsheet#ColumnCount('.') == 1
|
Expect tablemode#spreadsheet#ColumnCount('.') == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should successfully insert a column before the cursor'
|
||||||
|
Expect tablemode#spreadsheet#ColumnCount('.') == 2
|
||||||
|
call tablemode#spreadsheet#InsertColumn(0)
|
||||||
|
Expect tablemode#spreadsheet#ColumnCount('.') == 3
|
||||||
|
Expect getline('.') == '| | test11 | test12 |'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should successfully insert a column after the cursor'
|
||||||
|
normal! $
|
||||||
|
Expect tablemode#spreadsheet#ColumnCount('.') == 2
|
||||||
|
call tablemode#spreadsheet#InsertColumn(1)
|
||||||
|
Expect tablemode#spreadsheet#ColumnCount('.') == 3
|
||||||
|
Expect getline('.') == '| test11 | test12 | |'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'Manipulation of tables with headers'
|
describe 'Manipulation of tables with headers'
|
||||||
@@ -125,6 +140,21 @@ describe 'spreadsheet'
|
|||||||
Expect tablemode#spreadsheet#ColumnCount('.') == 3
|
Expect tablemode#spreadsheet#ColumnCount('.') == 3
|
||||||
Expect getline(4) == '| 9 | a | z |'
|
Expect getline(4) == '| 9 | a | z |'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should successfully insert a column before the cursor'
|
||||||
|
Expect tablemode#spreadsheet#ColumnCount('.') == 4
|
||||||
|
call tablemode#spreadsheet#InsertColumn(0)
|
||||||
|
Expect tablemode#spreadsheet#ColumnCount('.') == 5
|
||||||
|
Expect getline(4) == '| | 1 | 9 | a | z |'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should successfully insert a column after the cursor'
|
||||||
|
normal! $
|
||||||
|
Expect tablemode#spreadsheet#ColumnCount('.') == 4
|
||||||
|
call tablemode#spreadsheet#InsertColumn(1)
|
||||||
|
Expect tablemode#spreadsheet#ColumnCount('.') == 5
|
||||||
|
Expect getline(4) == '| 1 | 9 | a | z | |'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'Repeated Manipulations'
|
describe 'Repeated Manipulations'
|
||||||
@@ -146,5 +176,21 @@ describe 'spreadsheet'
|
|||||||
.,.+1 call tablemode#spreadsheet#DeleteColumn()
|
.,.+1 call tablemode#spreadsheet#DeleteColumn()
|
||||||
Expect tablemode#spreadsheet#ColumnCount('.') == 2
|
Expect tablemode#spreadsheet#ColumnCount('.') == 2
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should insert multiple columns before the cursor correctly'
|
||||||
|
call cursor(2, 7)
|
||||||
|
Expect tablemode#spreadsheet#ColumnCount('.') == 4
|
||||||
|
execute "normal! 2:\<C-u>call tablemode#spreadsheet#InsertColumn(0)\<CR>"
|
||||||
|
Expect tablemode#spreadsheet#ColumnCount('.') == 6
|
||||||
|
Expect getline('.') == '| 1 | | | 9 | a | z |'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should insert multiple columns after the cursor correctly'
|
||||||
|
call cursor(2, 7)
|
||||||
|
Expect tablemode#spreadsheet#ColumnCount('.') == 4
|
||||||
|
execute "normal! 2:\<C-u>call tablemode#spreadsheet#InsertColumn(1)\<CR>"
|
||||||
|
Expect tablemode#spreadsheet#ColumnCount('.') == 6
|
||||||
|
Expect getline('.') == '| 1 | 9 | | | a | z |'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user