" ============================================================================= " 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: 2.2.2 " 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. " " Copyright Notice: " Permission is hereby granted to use and distribute this code, " with or without modifications, provided that this copyright " notice is copied with it. Like anything else that's free, " table-mode.vim is provided *as is* and comes with no warranty " 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 function! s:SetBufferOptDefault(opt, val) "{{{2 if !exists('b:' . a:opt) let b:{a:opt} = a:val endif endfunction " }}}2 " s:Strlen(text) For counting 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:CountSeparator(line, separator) "{{{2 return s:Strlen(substitute(getline(a:line), '[^' . a:separator . ']', '', 'g')) endfunction " }}}2 function! s:GetCommentStart() "{{{2 let cstring = &commentstring if s:Strlen(cstring) > 0 return substitute(split(substitute(cstring, '%s', ' ', 'g'))[0], '.', '\\\0', 'g') else return '' endif endfunction " }}}2 function! s:StartExpr() "{{{2 let cstart = s:GetCommentStart() if s:Strlen(cstart) > 0 return '^\s*\(' . cstart . '\)\?\s*' else return '^\s*' endif endfunction " }}}2 function! s:StartCommentExpr() "{{{2 let cstartexpr = s:GetCommentStart() if s:Strlen(cstartexpr) > 0 return '^\s*' . cstartexpr . '\s*' else return '' endif endfunction " }}}2 function! s:IsTableModeActive() "{{{2 if g:table_mode_always_active | return 1 | endif call s:SetBufferOptDefault('table_mode_active', 0) return b:table_mode_active 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) " '|' is a special character, we need to map instead if g:table_mode_separator ==# '|' | let b:table_mode_separator_map = '' | endif execute "inoremap " . b:table_mode_separator_map . ' ' . \ b:table_mode_separator_map . ":call tablemode#TableizeInsertMode()a" else execute "iunmap " . b:table_mode_separator_map endif endfunction " }}}2 function! s:SetActive(bool) "{{{2 let b:table_mode_active = a:bool call s:ToggleMapping() endfunction " }}}2 function! s:UpdateLineBorder(line) "{{{2 let cline = a:line let hf = s:StartExpr() . g:table_mode_corner . '[' . g:table_mode_corner . ' ' . \ g:table_mode_fillchar . ']*' . g:table_mode_corner . '\?\s*$' let curr_line_count = s:CountSeparator(cline, g:table_mode_separator) if getline(cline-1) =~# hf let prev_line_count = s:CountSeparator(cline-1, g:table_mode_corner) if curr_line_count > prev_line_count silent! execute 'normal! kA' . repeat(g:table_mode_corner, curr_line_count - prev_line_count) . "\j" endif else let cstartexpr = s:StartCommentExpr() if s:Strlen(cstartexpr) > 0 && getline(cline) =~# cstartexpr let indent = matchstr(getline(cline), s:StartCommentExpr()) call append(cline-1, indent . repeat(g:table_mode_corner, curr_line_count)) else call append(cline-1, repeat(g:table_mode_corner, curr_line_count)) endif let cline = a:line + 1 " because of the append, the current line moved down endif if getline(cline+1) =~# hf let next_line_count = s:CountSeparator(cline+1, g:table_mode_corner) if curr_line_count > next_line_count silent! execute 'normal! jA' . repeat(g:table_mode_corner, curr_line_count - next_line_count) . "\k" end else let cstartexpr = s:StartCommentExpr() if s:Strlen(cstartexpr) > 0 && getline(cline) =~# cstartexpr let indent = matchstr(getline(cline), s:StartCommentExpr()) call append(cline, indent . repeat(g:table_mode_corner, curr_line_count)) else call append(cline, repeat(g:table_mode_corner, curr_line_count)) endif endif endfunction " }}}2 function! s:FillTableBorder() "{{{2 let [ current_col, current_line ] = [ col('.'), line('.') ] if g:table_mode_no_border_padding silent! execute '%s/' . g:table_mode_corner . '\zs\([' . \ g:table_mode_fillchar . ' ]*\)\ze' . g:table_mode_corner . \ '/\=repeat("' . g:table_mode_fillchar . '", s:Strlen(submatch(0)))/g' else silent! execute '%s/' . g:table_mode_corner . ' \zs\([' . \ g:table_mode_fillchar . ' ]*\)\ze ' . g:table_mode_corner . \ '/\=repeat("' . g:table_mode_fillchar . '", s:Strlen(submatch(0)))/g' endif call cursor(current_line, current_col) endfunction " }}}2 function! s:ConvertDelimiterToSeparator(line, ...) "{{{2 let delim = g:table_mode_delimiter if a:0 | let delim = a:1 | endif if delim ==# ',' silent! execute a:line . 's/' . "[\'\"][^\'\"]*\\zs,\\ze[^\'\"]*[\'\"]/__COMMA__/g" endif silent! execute a:line . 's/' . s:StartExpr() . '\zs\ze.\|' . delim . '\|$/' . \ g:table_mode_separator . '/g' if delim ==# ',' silent! execute a:line . 's/' . "[\'\"][^\'\"]*\\zs__COMMA__\\ze[^\'\"]*[\'\"]/,/g" endif endfunction " }}}2 function! s:Tableizeline(line, ...) "{{{2 let delim = g:table_mode_delimiter if a:0 && type(a:1) == type('') && !empty(a:1) | let delim = a:1[1:-1] | endif call s:ConvertDelimiterToSeparator(a:line, delim) if g:table_mode_border | call s:UpdateLineBorder(a:line) | endif call tablemode#TableRealign() endfunction " }}}2 function! s:ColumnCount(line) "{{{2 return s:Strlen(substitute(getline(a:line), '[^' . g:table_mode_separator . ']', '', 'g'))-1 endfunction " }}}2 function! s:IsFirstCell() "{{{2 return tablemode#TableColumnNr('.') ==# 1 endfunction " }}}2 function! s:IsLastCell() "{{{2 return tablemode#TableColumnNr('.') ==# s:ColumnCount('.') endfunction " }}}2 " }}}1 " Public API {{{1 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')) let position = s:Strlen(matchstr(getline('.')[0:col('.')], '.*' . g:table_mode_separator . '\s*\zs.*')) if g:table_mode_border | call s:UpdateLineBorder(line('.')) | endif if g:table_mode_no_border_padding && g:table_mode_align !=# 'c0' | let g:table_mode_align = 'c0' | endif call tablemode#TableRealign() if g:table_mode_border | call s:FillTableBorder() | endif normal! 0 call search(repeat('[^' . g:table_mode_separator . ']*' . g:table_mode_separator, column) . '\s\{-\}' . repeat('.', position), 'ce', line('.')) endif endfunction " }}}2 function! tablemode#TableModeEnable() "{{{2 call s:SetActive(1) endfunction " }}}2 function! tablemode#TableModeDisable() "{{{2 call s:SetActive(0) endfunction " }}}2 function! tablemode#TableModeToggle() "{{{2 if g:table_mode_always_active return 1 endif call s:SetBufferOptDefault('table_mode_active', 0) call s:SetActive(!b:table_mode_active) endfunction " }}}2 function! tablemode#TableizeRange(...) range "{{{2 let shift = 1 if g:table_mode_border | let shift = 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) call s:Tableizeline(lnum, a:1) undojoin let lnum = lnum + shift endwhile if g:table_mode_border | call s:FillTableBorder() | endif endfunction " }}}2 function! tablemode#TableizeByDelimiter() "{{{2 let delim = input('/') if delim =~# "\" || delim =~# "\" | return | endif let vm = visualmode() if vm ==? 'line' || vm ==? 'V' exec line("'<") . ',' . line("'>") . "call tablemode#TableizeRange('/' . delim)" endif endfunction " }}}2 function! tablemode#TableRealign() "{{{2 execute 'Tabularize/[' . g:table_mode_separator . g:table_mode_corner . ']/' . g:table_mode_align endfunction " }}}2 function! tablemode#IsATableRow(line) "{{{2 return getline(a:line) =~# (s:StartExpr() . g:table_mode_separator) endfunction " }}}2 function! tablemode#TableRowNr(pos) "{{{2 let line = 0 if type(a:pos) == type('') let line = line(a:pos) elseif type(a:pos) == type(1) let line = a:pos endif let rowNr = 0 while line > 0 if tablemode#IsATableRow(line) let rowNr = rowNr + 1 else break endif let line = line - 2 endwhile return rowNr endfunction " }}}2 function! tablemode#TableColumnNr(pos) "{{{2 let pos = [] if type(a:pos) == type('') let pos = [line(a:pos), col(a:pos)] elseif type(a:pos) == type([]) let pos = a:pos else return 0 endif return s:Strlen(substitute(getline(pos[0])[0:pos[1]-2], '[^' . g:table_mode_separator . ']', '', 'g')) endfunction " }}}2 function! tablemode#TableMotion(direction) "{{{2 if tablemode#IsATableRow('.') let rowCount = 1 if g:table_mode_border | let rowCount = 2 | endif if a:direction ==# 'l' if s:IsLastCell() if !tablemode#IsATableRow(line('.') + rowCount) | return | endif call tablemode#TableMotion('j') normal! 0 endif " If line starts with g:table_mode_separator if getline('.')[col('.')-1] ==# g:table_mode_separator execute 'normal! 2l' else execute 'normal! f' . g:table_mode_separator . '2l' endif elseif a:direction ==# 'h' if s:IsFirstCell() if !tablemode#IsATableRow(line('.') - rowCount) | return | endif call tablemode#TableMotion('k') normal! $ endif " If line ends with g:table_mode_separator if getline('.')[col('.')-1] ==# g:table_mode_separator execute 'normal! F' . g:table_mode_separator . '2l' else execute 'normal! 2F' . g:table_mode_separator . '2l' endif elseif a:direction ==# 'j' if tablemode#IsATableRow(line('.') + rowCount) | execute 'normal ' . rowCount . 'j' | endif elseif a:direction ==# 'k' if tablemode#IsATableRow(line('.') - rowCount) | execute 'normal ' . rowCount . 'k' | endif endif endif endfunction " }}}2 " }}}1