From 2e909c06689df60e3ed28db61f7cb6de23182336 Mon Sep 17 00:00:00 2001 From: Dhruva Sagar Date: Thu, 19 Sep 2013 02:06:22 +0530 Subject: [PATCH] Releasing Table Mode v3.1 * Removed table border and related options. You can now optionally have a header instead, simply add a table header and add a border to it triggered by the vim iabbrev '+-' on the line immidiately after the header and it will expand to the correct border. It will auto update as the table realigns with changes. Similar approach as followed by tables in org-mode. * Fixed bug of incorrect indentation of a newly added formula line. * Fixed bug in ConvertDelimiterToSeparator causing incorrect tableization of commented blocks of delimited content. --- autoload/tablemode.vim | 127 ++++++++++++++++++++++------------------- doc/table-mode.txt | 21 ++++--- plugin/table-mode.vim | 10 +--- 3 files changed, 80 insertions(+), 78 deletions(-) diff --git a/autoload/tablemode.vim b/autoload/tablemode.vim index 62e30e2..5b9af0c 100644 --- a/autoload/tablemode.vim +++ b/autoload/tablemode.vim @@ -1,10 +1,10 @@ -" ============================== Header ==================================={{{ +" ============================== Header ====================================== " File: autoload/tablemode.vim " Description: Table mode for vim for creating neat tables. " Author: Dhruva Sagar " License: MIT (http://www.opensource.org/licenses/MIT) " Website: http://github.com/dhruvasagar/vim-table-mode -" Version: 3.0 +" Version: 3.1 " 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. @@ -113,6 +113,10 @@ function! s:StartExpr() "{{{2 endif endfunction +function! s:HeaderBorderExpr() "{{{2 + return '^\s*' . g:table_mode_corner . '[' . g:table_mode_fillchar . g:table_mode_corner . ']*' . g:table_mode_corner . '$' +endfunction + function! s:EndExpr() "{{{2 let cend = s:GetCommentEnd() if s:Strlen(cend) > 0 @@ -148,7 +152,8 @@ function! s:IsTableModeActive() "{{{2 endfunction function! s:RowGap() "{{{2 - return g:table_mode_border ? 2 : 1 + " Getting rid of borders, so the row gap is now just 1 + return 1 endfunction function! s:ToggleMapping() "{{{2 @@ -159,8 +164,13 @@ function! s:ToggleMapping() "{{{2 execute "inoremap " . b:table_mode_separator_map . ' ' . \ b:table_mode_separator_map . ":call tablemode#TableizeInsertMode()a" + + execute "inoreabbrev " . g:table_mode_corner . + \ g:table_mode_fillchar . ":call tablemode#AddHeaderBorder('.')A" else execute "iunmap " . b:table_mode_separator_map + + execute "iunabbrev " . g:table_mode_corner . g:table_mode_fillchar endif endfunction @@ -169,48 +179,24 @@ function! s:SetActive(bool) "{{{2 call s:ToggleMapping() endfunction -function! s:GenerateBorder(line) "{{{2 - let line_val = getline(a:line) - let border = substitute(line_val[stridx(line_val, g:table_mode_separator):strridx(line_val, g:table_mode_separator)], g:table_mode_separator, g:table_mode_corner, 'g') - let border = substitute(border, '[^' . g:table_mode_corner . ']', g:table_mode_fillchar, 'g') - - let cstartexpr = s:StartCommentExpr() - if s:Strlen(cstartexpr) > 0 && getline(a:line) =~# cstartexpr - let sce = matchstr(line_val, s:StartCommentExpr()) - let ece = matchstr(line_val, s:EndCommentExpr()) - return sce . border . ece - elseif getline(a:line) =~# s:StartExpr() - let indent = matchstr(line_val, s:StartExpr()) - return indent . border - else - return border - endif -endfunction - -function! s:UpdateLineBorder(line) "{{{2 +function! s:GenerateHeaderBorder(line) "{{{2 let line = s:Line(a:line) + if tablemode#IsATableRow(line - s:RowGap()) + let line_val = getline(line - s:RowGap()) + 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 hf = s:StartExpr() . g:table_mode_corner . '[' . g:table_mode_corner . - \ g:table_mode_fillchar . ']*' . g:table_mode_corner . '\?' . s:EndExpr() - - let rowgap = s:RowGap() - let border = s:GenerateBorder(line) - - let [prev_line, next_line] = [getline(line-1), getline(line+1)] - if next_line =~# hf - if s:Strlen(border) > s:Strlen(s:GenerateBorder(line + rowgap)) || !tablemode#IsATableRow(line + rowgap) - call setline(line+1, border) + let cstartexpr = s:StartCommentExpr() + if s:Strlen(cstartexpr) > 0 && getline(line) =~# cstartexpr + let sce = matchstr(line_val, s:StartCommentExpr()) + let ece = matchstr(line_val, s:EndCommentExpr()) + return sce . border . ece + elseif getline(line) =~# s:StartExpr() + let indent = matchstr(line_val, s:StartExpr()) + return indent . border + else + return border endif - else - call append(line, border) - endif - - if prev_line =~# hf - if s:Strlen(border) > s:Strlen(s:GenerateBorder(line - rowgap)) || !tablemode#IsATableRow(line - rowgap) - call setline(line-1, border) - endif - else - call append(line-1, border) endif endfunction @@ -220,8 +206,16 @@ function! s:ConvertDelimiterToSeparator(line, ...) "{{{2 if delim ==# ',' silent! execute a:line . 's/' . "[\'\"][^\'\"]*\\zs,\\ze[^\'\"]*[\'\"]/__COMMA__/g" endif - silent! execute a:line . 's/' . s:StartExpr() . '\zs\ze.\|' . delim . '\|.\zs\ze' . s:EndExpr() . '/' . + + let [cstart, cend] = [s:GetCommentStart(), s:GetCommentEnd()] + let [match_char_start, match_char_end] = ['.', '.'] + if s:Strlen(cend) > 0 | let match_char_end = '[^' . cend . ']' | endif + if s:Strlen(cstart) > 0 | let match_char_start = '[^' . cstart . ']' | endif + + silent! execute a:line . 's/' . s:StartExpr() . '\zs\ze' . match_char_start . + \ '\|' . delim . '\|' . match_char_end . '\zs\ze' . s:EndExpr() . '/' . \ g:table_mode_separator . '/g' + if delim ==# ',' silent! execute a:line . 's/' . "[\'\"][^\'\"]*\\zs__COMMA__\\ze[^\'\"]*[\'\"]/,/g" endif @@ -231,7 +225,6 @@ function! s:Tableizeline(line, ...) "{{{2 let delim = g:table_mode_delimiter if a:0 && type(a:1) == type('') && !empty(a:1) | let delim = a:1[1:-1] | endif call s:ConvertDelimiterToSeparator(a:line, delim) - if g:table_mode_border | call s:UpdateLineBorder(a:line) | endif endfunction function! s:IsFirstCell() "{{{2 @@ -614,20 +607,14 @@ function! tablemode#TableModeToggle() "{{{2 endfunction function! tablemode#TableizeRange(...) range "{{{2 - let shift = 1 - if g:table_mode_border | let shift += 1 | endif - call s:Tableizeline(a:firstline, a:1) - undojoin - " The first one causes 2 extra lines for top & bottom border while the - " following lines cause only 1 for the bottom border. - let lnum = a:firstline + shift + (g:table_mode_border > 0) - while lnum < (a:firstline + (a:lastline - a:firstline + 1)*shift) + let lnum = a:firstline + while lnum < (a:firstline + (a:lastline - a:firstline + 1)*s:RowGap()) call s:Tableizeline(lnum, a:1) undojoin - let lnum = lnum + shift + let lnum = lnum + s:RowGap() endwhile - if g:table_mode_border | call tablemode#TableRealign(lnum - shift) | endif + call tablemode#TableRealign(lnum - s:RowGap()) endfunction function! tablemode#TableizeByDelimiter() "{{{2 @@ -639,18 +626,38 @@ function! tablemode#TableizeByDelimiter() "{{{2 endif endfunction +function! tablemode#AddHeaderBorder(line) "{{{2 + call setline(a:line, s:GenerateHeaderBorder(a:line)) +endfunction + function! tablemode#TableRealign(line) "{{{2 let line = s:Line(a:line) let [lnums, lines] = [[], []] - let tline = line + let [bline, tline] = [0, line] while tablemode#IsATableRow(tline) call insert(lnums, tline) call insert(lines, getline(tline)) let tline = tline - s:RowGap() endwhile + " If we reached header walking upwards + if getline(tline) =~# s:HeaderBorderExpr() && tablemode#IsATableRow(tline - s:RowGap()) + let bline = tline + let tline = tline - s:RowGap() + " Insert the header line + call insert(lnums, tline) + call insert(lines, getline(tline)) + endif + let tline = line + s:RowGap() + + " If we start at header + if !bline && getline(tline) =~# s:HeaderBorderExpr() + let bline = tline + let tline = tline + s:RowGap() + endif + while tablemode#IsATableRow(tline) call add(lnums, tline) call add(lines, getline(tline)) @@ -662,9 +669,11 @@ function! tablemode#TableRealign(line) "{{{2 for lnum in lnums let index = index(lnums, lnum) call setline(lnum, lines[index]) - undojoin - call s:UpdateLineBorder(lnum) endfor + + if bline + call tablemode#AddHeaderBorder(bline) + endif endfunction function! tablemode#IsATableRow(line) "{{{2 @@ -837,6 +846,8 @@ function! tablemode#AddFormula() "{{{2 let fr = input('f=') let row = tablemode#RowNr('.') let colm = tablemode#ColumnNr('.') + let indent = indent('.') + let indent_str = repeat(' ', indent) if fr !=# '' let fr = '$' . row . ',' . colm . '=' . fr @@ -860,7 +871,7 @@ function! tablemode#AddFormula() "{{{2 let [cmss, cmse] = [cms[0], ''] endif endif - let fr = cmss . ' tmf: ' . fr . ' ' . cmse + let fr = indent_str . cmss . ' tmf: ' . fr . ' ' . cmse call append(fline-1, fr) call cursor(cursor_pos) endif diff --git a/doc/table-mode.txt b/doc/table-mode.txt index baf41bf..ea1e41a 100644 --- a/doc/table-mode.txt +++ b/doc/table-mode.txt @@ -1,7 +1,7 @@ -*table-mode.txt* Table Mode for easy table formatting. v3.0 +*table-mode.txt* Table Mode for easy table formatting. v3.1 =============================================================================== Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER - VERSION 3.0 + VERSION 3.1 Author: Dhruva Sagar License: MIT @@ -36,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 |table-mode-always-active| if you wish. + using |table-mode-always-active| if you wish though not recommended. Table Mode allows for creation of tables within comments, it looks at the 'commentstring' setting to identify whether the current line is @@ -130,7 +130,6 @@ OPTIONS *table-mode-options* Overview: |table-mode-loaded| ............. Disable the plugin. - |table-mode-border| ............. Enable border. |table-mode-corner| ............. Set corner character. |table-mode-separator| .......... Set separator character. |table-mode-fillchar| ........... Set table fillchar character. @@ -154,12 +153,6 @@ g:loaded_table_mode *table-mode-loaded* Use this option to disable the plugin: > let g:loaded_table_mode = 1 < - -g:table_mode_border *table-mode-border* - Use this option to enable / disable table border: > - let g:table_mode_border = 1 -< - g:table_mode_corner *table-mode-corner* Use this option to define the table corner character: > let g:table_mode_corner = '+' @@ -172,7 +165,7 @@ g:table_mode_separator *table-mode-separator* table row in insert mode. g:table_mode_fillchar *table-mode-fillchar* - Use this option to define the table border fill character: > + Use this option to define the table header border fill character: > let g:table_mode_fillchar = '-' < @@ -330,6 +323,12 @@ MAPPINGS *table-mode-mappings* would evaluate the formula line and update the table accordingly. This invokes the |TableEvalFormulaLine| command. ++- Expands to a header border, the line immidiately above this + will be considered as the table header. You can change this by + changing |table-mode-corner| and |table-mode-fillchar| + options. This is an iabbrev so is triggered when you press + space or after typing the mapping. + =============================================================================== COMMANDS *table-mode-commands* diff --git a/plugin/table-mode.vim b/plugin/table-mode.vim index 9cdd22d..8f4369b 100644 --- a/plugin/table-mode.vim +++ b/plugin/table-mode.vim @@ -4,7 +4,7 @@ " Author: Dhruva Sagar " License: MIT (http://www.opensource.org/licenses/MIT) " Website: http://github.com/dhruvasagar/vim-table-mode -" Version: 2.4.0 +" Version: 3.1 " 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. @@ -24,22 +24,18 @@ if exists('g:loaded_table_mode') finish endif let g:loaded_table_mode = 1 -"}}}1 " Avoiding side effects {{{1 let s:save_cpo = &cpo set cpo&vim -" }}}1 function! s:SetGlobalOptDefault(opt, val) "{{{1 if !exists('g:' . a:opt) let g:{a:opt} = a:val endif endfunction -" }}}1 " Set Global Defaults {{{1 -call s:SetGlobalOptDefault('table_mode_border', 1) call s:SetGlobalOptDefault('table_mode_corner', '+') call s:SetGlobalOptDefault('table_mode_separator', '|') call s:SetGlobalOptDefault('table_mode_fillchar', '-') @@ -55,7 +51,6 @@ 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 let direction = nr2char(getchar()) @@ -63,7 +58,6 @@ function! s:TableMotion() "{{{1 call tablemode#TableMotion(direction) endfor endfunction -" }}}1 " Define Commands & Mappings {{{1 if !g:table_mode_always_active "{{{2 @@ -108,11 +102,9 @@ execute "nnoremap " . g:table_mode_map_prefix . g:table_mode_add_formul \ " :TableAddFormula" execute "nnoremap " . g:table_mode_map_prefix . g:table_mode_eval_expr_map . \ " :TableEvalFormulaLine" -"}}}1 " Avoiding side effects {{{1 let &cpo = s:save_cpo -" }}}1 " ModeLine {{{ " vim: sw=2 sts=2 fdl=0 fdm=marker