From 69128806978841deceb23ba2f906cf570a8023d1 Mon Sep 17 00:00:00 2001 From: ibbem Date: Tue, 31 Mar 2020 18:21:45 +0200 Subject: [PATCH 1/5] Include the header when deleting columns --- autoload/tablemode/spreadsheet.vim | 21 ++++++++++++++++++++- t/fixtures/complex_header.txt | 9 +++++++++ t/spreadsheet.vim | 26 +++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 t/fixtures/complex_header.txt diff --git a/autoload/tablemode/spreadsheet.vim b/autoload/tablemode/spreadsheet.vim index cb772ed..4bebec3 100644 --- a/autoload/tablemode/spreadsheet.vim +++ b/autoload/tablemode/spreadsheet.vim @@ -31,12 +31,31 @@ function! tablemode#spreadsheet#GetFirstRow(line) "{{{2 endif endfunction +function! tablemode#spreadsheet#GetFirstRowOrHeader(line) "{{{2 + if tablemode#table#IsRow(a:line) + let line = tablemode#utils#line(a:line) + + while tablemode#table#IsTable(line - 1) + let line -= 1 + endwhile + if tablemode#table#IsBorder(line) | let line += 1 | endif + + return line + endif +endfunction + function! tablemode#spreadsheet#MoveToFirstRow() "{{{2 if tablemode#table#IsRow('.') call cursor(tablemode#spreadsheet#GetFirstRow('.'), col('.')) endif endfunction +function! tablemode#spreadsheet#MoveToFirstRowOrHeader() "{{{2 + if tablemode#table#IsRow('.') + call cursor(tablemode#spreadsheet#GetFirstRowOrHeader('.'), col('.')) + endif +endfunction + function! tablemode#spreadsheet#GetLastRow(line) "{{{2 if tablemode#table#IsRow(a:line) let line = tablemode#utils#line(a:line) @@ -142,7 +161,7 @@ function! tablemode#spreadsheet#DeleteColumn() "{{{2 if tablemode#table#IsRow('.') for i in range(v:count1) call tablemode#spreadsheet#MoveToStartOfCell() - call tablemode#spreadsheet#MoveToFirstRow() + call tablemode#spreadsheet#MoveToFirstRowOrHeader() silent! execute "normal! h\f" . g:table_mode_separator call tablemode#spreadsheet#MoveToLastRow() normal! d diff --git a/t/fixtures/complex_header.txt b/t/fixtures/complex_header.txt new file mode 100644 index 0000000..7ed440f --- /dev/null +++ b/t/fixtures/complex_header.txt @@ -0,0 +1,9 @@ +|----------+----------+----------+----------| +| counting | backward | alphabet | backward | +|:========:+:=========+=========:+==========| +| 1 | 9 | a | z | +| 2 | 8 | b | y | +| 3 | 7 | c | x | +| 4 | 6 | d | w | +| 5 | 5 | e | v | +|----------+----------+----------+----------| diff --git a/t/spreadsheet.vim b/t/spreadsheet.vim index f83194f..fbcd31f 100644 --- a/t/spreadsheet.vim +++ b/t/spreadsheet.vim @@ -102,7 +102,31 @@ describe 'spreadsheet' Expect tablemode#spreadsheet#ColumnCount('.') == 1 end end - + + describe 'Manipulation of tables with headers' + before + new + normal! ggdG + let g:table_mode_header_fillchar = '=' + read t/fixtures/complex_header.txt + call cursor(4, 7) + end + + it 'should successfully delete a row ' + Expect tablemode#spreadsheet#RowCount('.') == 5 + call tablemode#spreadsheet#DeleteRow() + Expect tablemode#spreadsheet#RowCount('.') == 4 + Expect getline(4) == '| 2 | 8 | b | y |' + end + + it 'should successfully delete a column' + Expect tablemode#spreadsheet#ColumnCount('.') == 4 + call tablemode#spreadsheet#DeleteColumn() + Expect tablemode#spreadsheet#ColumnCount('.') == 3 + Expect getline(4) == '| 9 | a | z |' + end + end + describe 'Repeated Manipulations' before new From 0b58003b68b11410eaa109df2df37af3a6127228 Mon Sep 17 00:00:00 2001 From: ibbem Date: Tue, 31 Mar 2020 19:56:31 +0200 Subject: [PATCH 2/5] Add support for inserting columns #130 Two mappings are introduced to enable usage of this feature like vim's pasting with p and P. --- README.md | 10 ++++++ autoload/tablemode.vim | 4 +++ autoload/tablemode/spreadsheet.vim | 51 ++++++++++++++++++++++++++++++ doc/table-mode.txt | 30 ++++++++++++++++++ plugin/table-mode.vim | 4 +++ t/config/options.vim | 2 ++ t/spreadsheet.vim | 46 +++++++++++++++++++++++++++ 7 files changed, 147 insertions(+) diff --git a/README.md b/README.md index 806f46d..15a881c 100644 --- a/README.md +++ b/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 [count] to delete multiple columns. + - **Insert Column** : + + You can use the \tic 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 + \tiC 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 ### Table Formulas diff --git a/autoload/tablemode.vim b/autoload/tablemode.vim index 5354db3..e00c0d8 100644 --- a/autoload/tablemode.vim +++ b/autoload/tablemode.vim @@ -40,6 +40,8 @@ function! s:ToggleMapping() "{{{2 call s:Map('(table-mode-realign)', g:table_mode_realign_map, 'n') call s:Map('(table-mode-delete-row)', g:table_mode_delete_row_map, 'n') call s:Map('(table-mode-delete-column)', g:table_mode_delete_column_map, 'n') + call s:Map('(table-mode-insert-column-before)', g:table_mode_insert_column_before_map, 'n') + call s:Map('(table-mode-insert-column-after)', g:table_mode_insert_column_after_map, 'n') call s:Map('(table-mode-add-formula)', g:table_mode_add_formula_map, 'n') call s:Map('(table-mode-eval-formula)', g:table_mode_eval_formula_map, 'n') call s:Map('(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_delete_row_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_eval_formula_map, 'n') call s:UnMap(g:table_mode_echo_cell_map, 'n') diff --git a/autoload/tablemode/spreadsheet.vim b/autoload/tablemode/spreadsheet.vim index 4bebec3..5d7d87d 100644 --- a/autoload/tablemode/spreadsheet.vim +++ b/autoload/tablemode/spreadsheet.vim @@ -187,6 +187,57 @@ function! tablemode#spreadsheet#DeleteRow() "{{{2 endif 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\" + 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 let args = copy(a:000) call insert(args, a:range) diff --git a/doc/table-mode.txt b/doc/table-mode.txt index 5afd671..ae9a407 100644 --- a/doc/table-mode.txt +++ b/doc/table-mode.txt @@ -75,6 +75,10 @@ Manipulation of tables: 3. Delete Row : Delete an entire table row using |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 Mode now has support for formulas like a spreadsheet. There are 2 ways of defining formulas : @@ -165,6 +169,12 @@ Overview: |table-mode-realign-map| ........ Set the realign mapping |table-mode-delete-row-map| ..... Set the delete row 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-eval-formula-map| ... Set the eval formula 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. > let g:table_mode_delete_column_map = '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 = '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 = 'tic' +> g:table_mode_add_formula_map *table-mode-add-formula-map* Set this to configure the mapping for adding a formula for a table cell. > @@ -379,6 +399,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-insert-column-before* +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* +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* tfa Add a fomula for the current table cell. This invokes |TableAddFormula| command. diff --git a/plugin/table-mode.vim b/plugin/table-mode.vim index f266f4a..099d316 100644 --- a/plugin/table-mode.vim +++ b/plugin/table-mode.vim @@ -40,6 +40,8 @@ call s:SetGlobalOptDefault('table_mode_cell_text_object_i_map', 'i') 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_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_eval_formula_map', g:table_mode_map_prefix.'fe') call s:SetGlobalOptDefault('table_mode_echo_cell_map', g:table_mode_map_prefix.'?') @@ -102,6 +104,8 @@ xnoremap (table-mode-cell-text-object-i) :call tablemode#spr nnoremap (table-mode-delete-row) :call tablemode#spreadsheet#DeleteRow() nnoremap (table-mode-delete-column) :call tablemode#spreadsheet#DeleteColumn() +nnoremap (table-mode-insert-column-before) :call tablemode#spreadsheet#InsertColumn(0) +nnoremap (table-mode-insert-column-after) :call tablemode#spreadsheet#InsertColumn(1) nnoremap (table-mode-add-formula) :call tablemode#spreadsheet#formula#Add() nnoremap (table-mode-eval-formula) :call tablemode#spreadsheet#formula#EvaluateFormulaLine() diff --git a/t/config/options.vim b/t/config/options.vim index fa3fbdd..a330d7e 100644 --- a/t/config/options.vim +++ b/t/config/options.vim @@ -22,6 +22,8 @@ let g:table_mode_cell_text_object_i_map = 'i' let g:table_mode_realign_map = 'tr' let g:table_mode_delete_row_map = 'tdd' let g:table_mode_delete_column_map = 'tdc' +let g:table_mode_insert_column_before_map = 'tiC' +let g:table_mode_insert_column_after_map = 'tic' let g:table_mode_add_formula_map = 'tfa' let g:table_mode_eval_formula_map = 'tfe' let g:table_mode_echo_cell_map = 't?' diff --git a/t/spreadsheet.vim b/t/spreadsheet.vim index fbcd31f..c60073b 100644 --- a/t/spreadsheet.vim +++ b/t/spreadsheet.vim @@ -101,6 +101,21 @@ describe 'spreadsheet' call tablemode#spreadsheet#DeleteColumn() Expect tablemode#spreadsheet#ColumnCount('.') == 1 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 describe 'Manipulation of tables with headers' @@ -125,6 +140,21 @@ describe 'spreadsheet' Expect tablemode#spreadsheet#ColumnCount('.') == 3 Expect getline(4) == '| 9 | a | z |' 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 describe 'Repeated Manipulations' @@ -146,5 +176,21 @@ describe 'spreadsheet' .,.+1 call tablemode#spreadsheet#DeleteColumn() Expect tablemode#spreadsheet#ColumnCount('.') == 2 end + + it 'should insert multiple columns before the cursor correctly' + call cursor(2, 7) + Expect tablemode#spreadsheet#ColumnCount('.') == 4 + execute "normal! 2:\call tablemode#spreadsheet#InsertColumn(0)\" + 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:\call tablemode#spreadsheet#InsertColumn(1)\" + Expect tablemode#spreadsheet#ColumnCount('.') == 6 + Expect getline('.') == '| 1 | 9 | | | a | z |' + end end end From 67129cd1e047ffd504524ef1b0d4f9eeb5636795 Mon Sep 17 00:00:00 2001 From: ibbem Date: Fri, 3 Apr 2020 20:23:23 +0200 Subject: [PATCH 3/5] Fix column modifications if Unicode is involved --- autoload/tablemode/spreadsheet.vim | 6 +++--- autoload/tablemode/utils.vim | 9 ++++++++ t/spreadsheet.vim | 33 ++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/autoload/tablemode/spreadsheet.vim b/autoload/tablemode/spreadsheet.vim index 5d7d87d..dfda45a 100644 --- a/autoload/tablemode/spreadsheet.vim +++ b/autoload/tablemode/spreadsheet.vim @@ -46,13 +46,13 @@ endfunction function! tablemode#spreadsheet#MoveToFirstRow() "{{{2 if tablemode#table#IsRow('.') - call cursor(tablemode#spreadsheet#GetFirstRow('.'), col('.')) + call tablemode#utils#MoveToLine(tablemode#spreadsheet#GetFirstRow('.')) endif endfunction function! tablemode#spreadsheet#MoveToFirstRowOrHeader() "{{{2 if tablemode#table#IsRow('.') - call cursor(tablemode#spreadsheet#GetFirstRowOrHeader('.'), col('.')) + call tablemode#utils#MoveToLine(tablemode#spreadsheet#GetFirstRowOrHeader('.')) endif endfunction @@ -71,7 +71,7 @@ endfunction function! tablemode#spreadsheet#MoveToLastRow() "{{{2 if tablemode#table#IsRow('.') - call cursor(tablemode#spreadsheet#GetLastRow('.'), col('.')) + call tablemode#utils#MoveToLine(tablemode#spreadsheet#GetLastRow('.')) endif endfunction diff --git a/autoload/tablemode/utils.vim b/autoload/tablemode/utils.vim index aa95a92..9dc32f2 100644 --- a/autoload/tablemode/utils.vim +++ b/autoload/tablemode/utils.vim @@ -51,3 +51,12 @@ endfunction function! tablemode#utils#get_buffer_or_global_option(table_option) "{{{2 return get(b:, a:table_option, get(g:, a:table_option)) endf + +function tablemode#utils#MoveToLine(line) "{{{2 + let offset = tablemode#utils#line(a:line) - line('.') + if offset > 0 + execute "normal! ".offset."j" + elseif offset < 0 + execute "normal! ".(-offset)."k" + endif +endfunction diff --git a/t/spreadsheet.vim b/t/spreadsheet.vim index c60073b..0350b00 100644 --- a/t/spreadsheet.vim +++ b/t/spreadsheet.vim @@ -193,4 +193,37 @@ describe 'spreadsheet' Expect getline('.') == '| 1 | 9 | | | a | z |' end end + + describe 'Unicode table separators' + before + new + normal! ggdG + read t/fixtures/table/sample_realign_unicode_after.txt + call cursor(2, 19) + end + + it 'should not prevent the deletion of rows' + Expect tablemode#spreadsheet#RowCount('.') == 4 + call tablemode#spreadsheet#DeleteRow() + Expect tablemode#spreadsheet#RowCount('.') == 3 + end + + it 'should not prevent the deletion of columns' + Expect tablemode#spreadsheet#ColumnCount('.') == 3 + call tablemode#spreadsheet#DeleteColumn() + Expect tablemode#spreadsheet#ColumnCount('.') == 2 + end + + it 'should not prevent the insertion of columns before the cursor' + Expect tablemode#spreadsheet#ColumnCount('.') == 3 + call tablemode#spreadsheet#InsertColumn(1) + Expect tablemode#spreadsheet#ColumnCount('.') == 4 + end + + it 'should not prevent the insertion of columns after the cursor' + Expect tablemode#spreadsheet#ColumnCount('.') == 3 + call tablemode#spreadsheet#InsertColumn(1) + Expect tablemode#spreadsheet#ColumnCount('.') == 4 + end + end end From 85a5e3d4e17110748f676a4b59d64202bcab096c Mon Sep 17 00:00:00 2001 From: ibbem Date: Fri, 3 Apr 2020 23:41:56 +0200 Subject: [PATCH 4/5] Fix the column modifications on escaped separators If a g:table_mode_separator is escaped by a backslash, the table is aligned ignoring this instance of the g:table_mode_separator, but the column deletion and insertion functions did not consider this behaviour. The g:table_mode_escaped_separator regex is simplified and optimized a little bit. Also more care is taken to prevent unexpected behaviour if special characters are used as g:table_mode_separator. --- autoload/tablemode/spreadsheet.vim | 25 +++++++++++++++---------- autoload/tablemode/utils.vim | 4 ++++ plugin/table-mode.vim | 2 +- t/config/options.vim | 2 +- t/fixtures/escaped_seperator.txt | 9 +++++++++ t/spreadsheet.vim | 30 ++++++++++++++++++++++++++++++ 6 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 t/fixtures/escaped_seperator.txt diff --git a/autoload/tablemode/spreadsheet.vim b/autoload/tablemode/spreadsheet.vim index dfda45a..357efe7 100644 --- a/autoload/tablemode/spreadsheet.vim +++ b/autoload/tablemode/spreadsheet.vim @@ -132,13 +132,11 @@ function! tablemode#spreadsheet#ColumnNr(pos) "{{{2 return 0 endif let row_start = stridx(getline(pos[0]), g:table_mode_separator) - return tablemode#utils#strlen(substitute(getline(pos[0])[(row_start):pos[1]-2], '[^' . g:table_mode_separator . ']', '', 'g')) + return tablemode#utils#SeparatorCount(getline(pos[0])[row_start:pos[1]-2]) endfunction function! tablemode#spreadsheet#ColumnCount(line) "{{{2 - let line = tablemode#utils#line(a:line) - - return tablemode#utils#strlen(substitute(getline(line), '[^' . g:table_mode_separator . ']', '', 'g'))-1 + return tablemode#utils#SeparatorCount(getline(tablemode#utils#line(a:line))) - 1 endfunction function! tablemode#spreadsheet#IsFirstCell() "{{{2 @@ -150,11 +148,15 @@ function! tablemode#spreadsheet#IsLastCell() "{{{2 endfunction function! tablemode#spreadsheet#MoveToStartOfCell() "{{{2 - if getline('.')[col('.')-1] ==# g:table_mode_separator && !tablemode#spreadsheet#IsLastCell() - normal! 2l - else - execute 'normal! F' . g:table_mode_separator . '2l' + if getline('.')[col('.')-1] !=# g:table_mode_separator || tablemode#spreadsheet#IsLastCell() + call search(g:table_mode_escaped_separator_regex, 'b', line('.')) endif + normal! 2l +endfunction + +function! tablemode#spreadsheet#MoveToEndOfCell() "{{{2 + call search(g:table_mode_escaped_separator_regex, 'z', line('.')) + normal! 2h endfunction function! tablemode#spreadsheet#DeleteColumn() "{{{2 @@ -162,7 +164,9 @@ function! tablemode#spreadsheet#DeleteColumn() "{{{2 for i in range(v:count1) call tablemode#spreadsheet#MoveToStartOfCell() call tablemode#spreadsheet#MoveToFirstRowOrHeader() - silent! execute "normal! h\f" . g:table_mode_separator + silent! execute "normal! h\" + call tablemode#spreadsheet#MoveToEndOfCell() + normal! 2l call tablemode#spreadsheet#MoveToLastRow() normal! d endfor @@ -194,7 +198,8 @@ function! tablemode#spreadsheet#InsertColumn(after) "{{{2 call tablemode#spreadsheet#MoveToFirstRowOrHeader() call tablemode#spreadsheet#MoveToStartOfCell() if a:after - execute "normal! f".g:table_mode_separator + call tablemode#spreadsheet#MoveToEndOfCell() + normal! 3l endif execute "normal! h\" call tablemode#spreadsheet#MoveToLastRow() diff --git a/autoload/tablemode/utils.vim b/autoload/tablemode/utils.vim index 9dc32f2..1a3dadb 100644 --- a/autoload/tablemode/utils.vim +++ b/autoload/tablemode/utils.vim @@ -60,3 +60,7 @@ function tablemode#utils#MoveToLine(line) "{{{2 execute "normal! ".(-offset)."k" endif endfunction + +function! tablemode#utils#SeparatorCount(str) + return tablemode#utils#strlen(substitute(a:str, '\V\C\(\\' . escape(g:table_mode_separator, '\') . '\|\[^' . escape(g:table_mode_separator, ']^-\') . ']\)', '', 'g')) +endfunction diff --git a/plugin/table-mode.vim b/plugin/table-mode.vim index 099d316..576d4c4 100644 --- a/plugin/table-mode.vim +++ b/plugin/table-mode.vim @@ -18,7 +18,7 @@ endfunction call s:SetGlobalOptDefault('table_mode_corner', '+') call s:SetGlobalOptDefault('table_mode_verbose', 1) call s:SetGlobalOptDefault('table_mode_separator', '|') -call s:SetGlobalOptDefault('table_mode_escaped_separator_regex', '\(\\\)\@t') diff --git a/t/config/options.vim b/t/config/options.vim index a330d7e..d386f96 100644 --- a/t/config/options.vim +++ b/t/config/options.vim @@ -1,6 +1,6 @@ let g:table_mode_corner = '+' let g:table_mode_separator = '|' -let g:table_mode_escaped_separator_regex = '\(\\\)\@t' diff --git a/t/fixtures/escaped_seperator.txt b/t/fixtures/escaped_seperator.txt new file mode 100644 index 0000000..f561539 --- /dev/null +++ b/t/fixtures/escaped_seperator.txt @@ -0,0 +1,9 @@ +|-------------------+---------------------------| +| The \| works as | It can be escaped by a \. | +| a separator. | | +| | | +| Escaping \ with | This feature would | +| a \ doesn't work. | be unnecessary, because | +| | a separator must be | +| | preceded by a space. | +|-------------------+---------------------------| diff --git a/t/spreadsheet.vim b/t/spreadsheet.vim index 0350b00..f119080 100644 --- a/t/spreadsheet.vim +++ b/t/spreadsheet.vim @@ -226,4 +226,34 @@ describe 'spreadsheet' Expect tablemode#spreadsheet#ColumnCount('.') == 4 end end + + describe 'Escaped table separators' + before + new + normal! ggdG + read t/fixtures/escaped_seperator.txt + call cursor(2, 3) + end + + it 'should not prevent the deletion of rows' + Expect tablemode#spreadsheet#RowCount('.') == 7 + call tablemode#spreadsheet#DeleteRow() + Expect tablemode#spreadsheet#RowCount('.') == 6 + Expect getline('.') == '| a separator. | |' + end + + it 'should not prevent the deletion of columns' + Expect tablemode#spreadsheet#ColumnCount('.') == 2 + call tablemode#spreadsheet#DeleteColumn() + Expect tablemode#spreadsheet#ColumnCount('.') == 1 + Expect getline('.') == '| It can be escaped by a \. |' + end + + it 'should not prevent the insertion of columns' + Expect tablemode#spreadsheet#ColumnCount('.') == 2 + call tablemode#spreadsheet#InsertColumn(1) + Expect tablemode#spreadsheet#ColumnCount('.') == 3 + Expect getline('.') == '| The \| works as | | It can be escaped by a \. |' + end + end end From 46e4493b306be7c8749f522666c53f3ffe90ebb1 Mon Sep 17 00:00:00 2001 From: ibbem Date: Mon, 13 Apr 2020 22:34:44 +0200 Subject: [PATCH 5/5] Start insert mode after adding a column --- autoload/tablemode/spreadsheet.vim | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/autoload/tablemode/spreadsheet.vim b/autoload/tablemode/spreadsheet.vim index 357efe7..f617809 100644 --- a/autoload/tablemode/spreadsheet.vim +++ b/autoload/tablemode/spreadsheet.vim @@ -234,12 +234,13 @@ function! tablemode#spreadsheet#InsertColumn(after) "{{{2 \ 'b') if a:after - execute "normal! ".quantity."p2l" + execute "normal! ".quantity."pl" else - execute "normal! ".quantity."Pl" + execute "normal! ".quantity."P" endif call tablemode#table#Realign('.') + startinsert endif endfunction