Table Mode v1.0

This commit is contained in:
Dhruva Sagar
2013-03-17 03:56:43 +05:30
commit 368d32de45
4 changed files with 269 additions and 0 deletions

0
README.md Normal file
View File

130
doc/table-mode.txt Normal file
View File

@@ -0,0 +1,130 @@
*table-mode.txt* Table Mode for easy table formatting. v1.0
===============================================================================
Table Mode, THE AWESOME AUTOMATIC TABLE CREATOR & FORMATTER
VERSION 1.0
===============================================================================
CONTENTS *table-mode-contents*
1. Introduction .................... |table-mode-introduction|
2. Requirements .................... |table-mode-requirements|
3. Getting Started ................. |table-mode-getting-started|
4. Options ......................... |table-mode-options|
5. Mappings ........................ |table-mode-mappings|
6. Commands ........................ |table-mode-commands|
7. Contributing .................... |table-mode-contributing|
8. Report Issues ................... |table-mode-report-issues|
===============================================================================
INTRODUCTION *table-mode-introduction*
Table Mode is simple plugin that makes building tables in vim a breeze. It is
inspired from tpope's auto aligning script for creating tables in vim -
https://gist.github.com/tpope/287147, which in turn utilizes the Tabular
Plugin.
===============================================================================
REQUIREMENTS *table-mode-requirements*
This depends on the Tabular plugin - https://github.com/godlygeek/tabular,
make sure it is installed and loaded.
===============================================================================
GETTING STARTED *table-mode-getting-started*
Using Table Mode is dead simple. You simply start typing on a new line with
the table separator - |g:table-mode-separator|, and you just type away! The
plugin does the rest automatically for you as you type. With each additional
separator you add, it aligns the table properly, without having to do
anything else.
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
|g:table-mode-always-active| if you wish.
===============================================================================
OPTIONS *table-mode-options*
Overview:
|table-mode-options-loaded| .......... Disable the plugin
|table-mode-options-border| .......... Enable border
|table-mode-options-corner| .......... Set corner character
|table-mode-options-separator| ....... Set separator character
|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
g:table_mode_loaded *table-mode-options-loaded*
Use this option to disable the plugin: >
let g:table_mode_loaded = 1
<
g:table_mode_border *table-mode-options-border*
Use this option to enable / disable table border: >
let g:table_mode_border = 1
<
g:table_mode_corner *table-mode-options-corner*
Use this option to define the table corner character: >
let g:table_mode_corner = '+'
<
g:table_mode_separator *table-mode-options-separator*
Use this option to define the table column separator character: >
let g:table_mode_separator = '|'
<
This option also defines the trigger to be used to start creating a
table row in insert mode.
g:table_mode_fillchar *table-mode-options-fillchar*
Use this option to define the table border fill character: >
let g:table_mode_fillchar = '-'
<
g:table_mode_toggle_map *table-mode-options-toggle-map*
Use this option to define the mapping for toggling the table mode: >
let g:table_mode_toggle_map = '<Leader>tm'
<
Read |table-mode-mappings-toggle| for more info.
g:table_mode_always_active *table-mode-options-always-active*
Use this option to permanently enable the table mode: >
let g:table_mode_always_active = 0
<
This will trigger table creation once you type |table-mode-separator|
as long as it's the first character on the line, which can be
annoying. That's why I would recommend you to instead use
|table-mode-mappings-toggle| to enable / disable toggle mode when
needed.
===============================================================================
MAPPINGS *table-mode-mappings*
*table-mode-mappings-toggle*
<Leader>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-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.
===============================================================================
COMMANDS *table-mode-commands*
*table-mode-commands-toggle*
:TableModeToggle
Toggles the table mode. Same effect as |toggle-mode-mappings-toggle|
===============================================================================
CONTRIBUTING *table-mode-contributing*
If you want to take a stab at it, by all means, send me a pull request on
Github (http://github.com/dhruvasagar/table-mode.git) or get in touch with me
directly via e-mail at dhruva 'dot' sagar 'at' gmail.com.
===============================================================================
REPORT ISSUES *table-mode-report-issues*
If you discover any issues, please report them at
http://github.com/dhruvasagar/table-mode/issues.
vim:tw=78:ts=8:ft=help:norl:

20
doc/tags Normal file
View File

@@ -0,0 +1,20 @@
table-mode-commands table-mode.txt /*table-mode-commands*
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*
table-mode-getting-started table-mode.txt /*table-mode-getting-started*
table-mode-introduction table-mode.txt /*table-mode-introduction*
table-mode-mappings table-mode.txt /*table-mode-mappings*
table-mode-mappings-toggle table-mode.txt /*table-mode-mappings-toggle*
table-mode-mappings-trigger table-mode.txt /*table-mode-mappings-trigger*
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-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*
table-mode-options-toggle-map table-mode.txt /*table-mode-options-toggle-map*
table-mode-report-issues table-mode.txt /*table-mode-report-issues*
table-mode-requirements table-mode.txt /*table-mode-requirements*
table-mode.txt table-mode.txt /*table-mode.txt*

119
plugin/table-mode.vim Normal file
View File

@@ -0,0 +1,119 @@
" =============================================================================
" File: table-mode.vim
" Description: Table mode for vim for creating neat tables.
" Author: Dhruva Sagar <http://dhruvasagar.com/>
" License: MIT (http://www.opensource.org/licenses/MIT)
" Notes: This was inspired by Tim Pope's cucumbertables.vim
" (https://gist.github.com/tpope/287147)
" =============================================================================
"
if exists('g:table_mode_loaded')
finish
endif
let g:table_mode_loaded = 1
function! s:SetGlobalOptDefault(opt, val)
if !exists('g:' . a:opt)
let g:{a:opt} = a:val
endif
endfunction
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', '-')
call s:SetGlobalOptDefault('table_mode_toggle_map', '<Leader>tm')
call s:SetGlobalOptDefault('table_mode_always_active', 0)
function! s:SetBufferOptDefault(opt, val)
if !exists('b:' . a:opt)
let b:{a:opt} = a:val
endif
endfunction
if g:table_mode_separator ==# '|'
let s:table_mode_separator_map = '<Bar>'
else
let s:table_mode_separator_map = g:table_mode_separator
endif
function! s:error(str)
echohl ErrorMsg
echomsg a:sr
echohl None
let v:errmsg = a:str
endfunction
function! s:CountSeparator(line, separator)
return strlen(substitute(getline(a:line), '[^' . a:separator . ']', '', 'g'))
endfunction
function! s:UpdateTableBorder()
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 . "\<Esc>j"
endif
else
call append(line('.')-1, g:table_mode_corner)
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 . "\<Esc>k"
end
else
call append(line('.'), g:table_mode_corner)
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'
call cursor(current_line, current_col)
endfunction
function! s:ToggleTableMode()
if g:table_mode_always_active
return 1
endif
call s:SetBufferOptDefault('table_mode_active', 0)
let b:table_mode_active = !b:table_mode_active
endfunction
function! s:IsTableModeActive()
if g:table_mode_always_active
return 1
endif
call s:SetBufferOptDefault('table_mode_active', 0)
return b:table_mode_active
endfunction
function! s:Tableize()
let p = '^\s*' . g:table_mode_separator . '[^' . g:table_mode_separator . ']*' . g:table_mode_separator . '\s*$'
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()
endif
exec 'Tabularize/[' . g:table_mode_separator . g:table_mode_corner . ']/l1'
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
if !g:table_mode_always_active
exec "nnoremap <silent> " . g:table_mode_toggle_map .
\ " <Esc>:call <SID>ToggleTableMode()<CR>"
command! -nargs=0 TableModeToggle call s:ToggleTableMode()
endif
exec "inoremap <silent> " . s:table_mode_separator_map . ' ' .
\ s:table_mode_separator_map . "<Esc>:call <SID>Tableize()<CR>a"