diff --git a/README.md b/README.md index 7a868a7..075b4c3 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,9 @@ An awesome automatic table creator & formatter allowing one to create neat tables as you type. +Change Log for v1.1 + Added Tableize command and mapping to convert existing content into a table. + # Installation There are 2 ways to do this diff --git a/doc/table-mode.txt b/doc/table-mode.txt index 3bdce81..6890f81 100644 --- a/doc/table-mode.txt +++ b/doc/table-mode.txt @@ -1,7 +1,7 @@ *table-mode.txt* Table Mode for easy table formatting. v1.0 =============================================================================== Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER - VERSION 1.0 + VERSION 1.1 Author: Dhruva Sagar License: MIT @@ -55,6 +55,7 @@ Overview: |table-mode-options-fillchar| ........ Set table fillchar character |table-mode-options-toggle-map| ...... Set table mode toggle mapping |table-mode-options-always-active| ... Set table mode to always enabled + |table-mode-options-delimiter| ....... Set the delimiter for Tableize g:table_mode_loaded *table-mode-options-loaded* Use this option to disable the plugin: > @@ -99,32 +100,55 @@ g:table_mode_always_active *table-mode-options-always-active* the table mode or |table-mode-commands-enable| to enable and |table-mode-commands-disable| to disable mode when needed. +g:table_mode_delimiter *table-mode-options-delimiter* + Use this option to define the delimiter which used by + |table-mode-commands-tableize| + =============================================================================== MAPPINGS *table-mode-mappings* *table-mode-mappings-toggle* -tm Enable table mode for the current buffer. You can change this - using the |toggle-mode-options-toggle-map| option. This is - applicable only if |table-mode-options-always-active| is not set. +tm Toggle table mode for the current buffer. You can change this + using the |toggle-mode-options-toggle-map| option. + + NOTE This is applicable only if + |table-mode-options-always-active| is not set. *table-mode-mappings-trigger* | Trigger table creation in table mode. You can change this using the |toggle-mode-options-separator| option. +T Triggers |table-mode-commands-tableize| on the visually + selected content. + =============================================================================== COMMANDS *table-mode-commands* *table-mode-commands-toggle* :TableModeToggle - Toggles the table mode. Same effect as |toggle-mode-mappings-toggle| + Toggles the table mode. Same effect as |toggle-mode-mappings-toggle|. + + NOTE this is applicable only if |table-mode-options-always-active| is + not set. *table-mode-commands-enable* :TableModeEnable Enables Table Mode. + NOTE this is applicable only if |table-mode-options-always-active| is + not set. + *table-mode-commands-disable* :TableModeDisable - Disables Table Mode. + Disables Table Mode. + + NOTE this is applicable only if |table-mode-options-always-active| is + not set. + +:Tableize *table-mode-commands-tableize* + This converts the current line into a table if it consists of + |table-mode-options-delimiter|. This accepts a range, without which it + applies on the current line. =============================================================================== CONTRIBUTING *table-mode-contributing* diff --git a/doc/tags b/doc/tags index e7fb4f7..10443e0 100644 --- a/doc/tags +++ b/doc/tags @@ -1,6 +1,7 @@ table-mode-commands table-mode.txt /*table-mode-commands* table-mode-commands-disable table-mode.txt /*table-mode-commands-disable* table-mode-commands-enable table-mode.txt /*table-mode-commands-enable* +table-mode-commands-tableize table-mode.txt /*table-mode-commands-tableize* table-mode-commands-toggle table-mode.txt /*table-mode-commands-toggle* table-mode-contents table-mode.txt /*table-mode-contents* table-mode-contributing table-mode.txt /*table-mode-contributing* @@ -13,6 +14,7 @@ table-mode-options table-mode.txt /*table-mode-options* table-mode-options-always-active table-mode.txt /*table-mode-options-always-active* table-mode-options-border table-mode.txt /*table-mode-options-border* table-mode-options-corner table-mode.txt /*table-mode-options-corner* +table-mode-options-delimiter table-mode.txt /*table-mode-options-delimiter* table-mode-options-fillchar table-mode.txt /*table-mode-options-fillchar* table-mode-options-loaded table-mode.txt /*table-mode-options-loaded* table-mode-options-separator table-mode.txt /*table-mode-options-separator* diff --git a/plugin/table-mode.vim b/plugin/table-mode.vim index 1d97965..0d9a56e 100644 --- a/plugin/table-mode.vim +++ b/plugin/table-mode.vim @@ -24,6 +24,8 @@ call s:SetGlobalOptDefault('table_mode_separator', '|') call s:SetGlobalOptDefault('table_mode_fillchar', '-') call s:SetGlobalOptDefault('table_mode_toggle_map', 'tm') call s:SetGlobalOptDefault('table_mode_always_active', 0) +call s:SetGlobalOptDefault('table_mode_delimiter', ',') +call s:SetGlobalOptDefault('table_mode_tableize_map', 'T') function! s:SetBufferOptDefault(opt, val) if !exists('b:' . a:opt) @@ -48,30 +50,35 @@ function! s:CountSeparator(line, separator) return strlen(substitute(getline(a:line), '[^' . a:separator . ']', '', 'g')) endfunction -function! s:UpdateTableBorder() +function! s:UpdateLineBorder(...) + let cline = a:0 ? a:1 : line('.') let hf = '^\s*' . g:table_mode_corner . '[' . g:table_mode_corner . ' ' . g:table_mode_fillchar . ']*' . g:table_mode_corner . '\?\s*$' - - if getline(line('.')-1) =~# hf - if s:CountSeparator(line('.')-1, g:table_mode_corner) < s:CountSeparator(line('.'), g:table_mode_separator) - exec 'normal! kA' . g:table_mode_corner . "\j" + 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 prev_line_count < curr_line_count + exec 'normal! kA' . repeat(g:table_mode_corner, curr_line_count - prev_line_count) . "\j" endif else - call append(line('.')-1, g:table_mode_corner) + call append(cline-1, repeat(g:table_mode_corner, curr_line_count)) + let cline = a:0 ? (a:1+1) : line('.') endif - if getline(line('.')+1) =~# hf - if s:CountSeparator(line('.')+1, g:table_mode_corner) < s:CountSeparator(line('.'), g:table_mode_separator) - exec 'normal! jA' . g:table_mode_corner . "\k" + if getline(cline+1) =~# hf + let next_line_count = s:CountSeparator(cline+1, g:table_mode_corner) + if next_line_count < curr_line_count + exec 'normal! jA' . repeat(g:table_mode_corner, curr_line_count - next_line_count) . "\k" end else - call append(line('.'), g:table_mode_corner) + call append(cline, repeat(g:table_mode_corner, curr_line_count)) endif endfunction function! s:FillTableBorder() let current_col = col('.') let current_line = line('.') - execute '%s/' . g:table_mode_corner . ' \zs\([\' . g:table_mode_fillchar . ' ]*\)\ze ' . g:table_mode_corner . '/\=repeat("' . g:table_mode_fillchar . '", strlen(submatch(0)))/ge' + execute 'silent! %s/' . g:table_mode_corner . ' \zs\([\' . g:table_mode_fillchar . ' ]*\)\ze ' . g:table_mode_corner . '/\=repeat("' . g:table_mode_fillchar . '", strlen(submatch(0)))/ge' call cursor(current_line, current_col) endfunction @@ -101,12 +108,18 @@ function! s:IsTableModeActive() return b:table_mode_active endfunction +function! s:ConvertDelimiterToSeparator(line) + if getline(a:line) =~# g:table_mode_delimiter + execute 'silent! ' . a:line . 's/^\|' . g:table_mode_delimiter . '\|$/' . g:table_mode_separator . '/ge' + endif +endfunction + function! s:Tableize() if s:IsTableModeActive() && exists(':Tabularize') && getline('.') =~# ('^\s*' . g:table_mode_separator) let column = strlen(substitute(getline('.')[0:col('.')], '[^' . g:table_mode_separator . ']', '', 'g')) let position = strlen(matchstr(getline('.')[0:col('.')], '.*' . g:table_mode_separator . '\s*\zs.*')) if g:table_mode_border - call s:UpdateTableBorder() + call s:UpdateLineBorder() endif exec 'Tabularize/[' . g:table_mode_separator . g:table_mode_corner . ']/l1' if g:table_mode_border @@ -117,6 +130,24 @@ function! s:Tableize() endif endfunction +function! s:Tableizeline(line) + call s:ConvertDelimiterToSeparator(a:line) + call s:UpdateLineBorder(a:line) + exec 'Tabularize/[' . g:table_mode_separator . g:table_mode_corner . ']/l1' +endfunction + +function! s:TableizeRange() range + call s:Tableizeline(a:firstline) + undojoin + let lnum = a:firstline+3 + while lnum <= (a:firstline + (a:lastline - a:firstline+1)*2) + call s:Tableizeline(lnum) + undojoin + let lnum = lnum + 2 + endwhile + call s:FillTableBorder() +endfunction + if !g:table_mode_always_active exec "nnoremap " . g:table_mode_toggle_map . \ " :call TableModeToggle()" @@ -125,4 +156,7 @@ if !g:table_mode_always_active command! -nargs=0 TableModeDisable call s:TableModeDisable() endif exec "inoremap " . s:table_mode_separator_map . ' ' . - \ s:table_mode_separator_map . ":call Tableize()a" + \ s:table_mode_separator_map . ":call Tableize()a" + +command! -nargs=0 -range Tableize ,call s:TableizeRange() +exec "xnoremap " . g:table_mode_tableize_map . " :Tableize"