" =============================================================================
" File: plugin/table-mode.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
" Note: This plugin was heavily inspired by the 'CucumberTables.vim'
" (https://gist.github.com/tpope/287147) plugin by Tim Pope.
"
" 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.
" =============================================================================
" Finish if already loaded {{{1
if exists('g:loaded_table_mode')
finish
endif
let g:loaded_table_mode = 1
" Avoiding side effects {{{1
let s:save_cpo = &cpo
set cpo&vim
function! s:SetGlobalOptDefault(opt, val) "{{{1
if !exists('g:' . a:opt)
let g:{a:opt} = a:val
endif
endfunction
" Set Global Defaults {{{1
call s:SetGlobalOptDefault('table_mode_corner', '+')
call s:SetGlobalOptDefault('table_mode_separator', '|')
call s:SetGlobalOptDefault('table_mode_fillchar', '-')
call s:SetGlobalOptDefault('table_mode_map_prefix', 't')
call s:SetGlobalOptDefault('table_mode_toggle_map', 'm')
call s:SetGlobalOptDefault('table_mode_always_active', 0)
call s:SetGlobalOptDefault('table_mode_delimiter', ',')
call s:SetGlobalOptDefault('table_mode_corner_corner', '|')
call s:SetGlobalOptDefault('table_mode_align_char', ':')
function! s:TableEchoCell() "{{{1
if tablemode#table#IsRow('.')
echomsg '$' . tablemode#spreadsheet#RowNr('.') . ',' . tablemode#spreadsheet#ColumnNr('.')
endif
endfunction
function! s:EnableTableSyntax() "{{{1
exec 'syntax match Table'
\ '/' . tablemode#table#StartExpr() . '\zs|.\+|\ze' . tablemode#table#EndExpr() . '/'
\ 'contains=TableBorder,TableSeparator,TableColumnAlign containedin=ALL'
syntax match TableSeparator /|/ contained
syntax match TableColumnAlign /:/ contained
syntax match TableBorder /[\-+]\+/ contained
endfunction
augroup TableMode
au!
autocmd Syntax * call EnableTableSyntax()
augroup END
hi! link TableBorder Delimiter
hi! link TableSeparator Delimiter
hi! link TableColumnAlign Type
" Define Commands & Mappings {{{1
if !g:table_mode_always_active "{{{2
exec "nnoremap " . g:table_mode_map_prefix . g:table_mode_toggle_map .
\ " :call tablemode#Toggle()"
command! -nargs=0 TableModeToggle call tablemode#Toggle()
command! -nargs=0 TableModeEnable call tablemode#Enable()
command! -nargs=0 TableModeDisable call tablemode#Disable()
else
let table_mode_separator_map = g:table_mode_separator
" '|' is a special character, we need to map instead
if g:table_mode_separator ==# '|' | let table_mode_separator_map = '' | endif
execute "inoremap " . table_mode_separator_map . ' ' .
\ table_mode_separator_map . ":call tablemode#TableizeInsertMode()a"
unlet table_mode_separator_map
endif
" }}}2
command! -nargs=? -range Tableize ,call tablemode#TableizeRange()
command! TableAddFormula call tablemode#spreadsheet#formula#Add()
command! TableModeRealign call tablemode#table#Realign('.')
command! TableEvalFormulaLine call tablemode#spreadsheet#formula#EvaluateFormulaLine()
nnoremap (table-mode-tableize) :Tableize
xnoremap (table-mode-tableize) :Tableize
xnoremap (table-mode-tableize-delimiter) :call tablemode#TableizeByDelimiter()
nnoremap (table-mode-realign) :call tablemode#table#Realign('.')
nnoremap (table-mode-motion-up) :call tablemode#spreadsheet#cell#Motion('k')
nnoremap (table-mode-motion-down) :call tablemode#spreadsheet#cell#Motion('j')
nnoremap (table-mode-motion-left) :call tablemode#spreadsheet#cell#Motion('h')
nnoremap (table-mode-motion-right) :call tablemode#spreadsheet#cell#Motion('l')
onoremap (table-mode-cell-text-object-a) :call tablemode#spreadsheet#cell#TextObject(0)
onoremap (table-mode-cell-text-object-i) :call tablemode#spreadsheet#cell#TextObject(1)
xnoremap (table-mode-cell-text-object-a) :call tablemode#spreadsheet#cell#TextObject(0)
xnoremap (table-mode-cell-text-object-i) :call tablemode#spreadsheet#cell#TextObject(1)
nnoremap (table-mode-delete-row) :call tablemode#spreadsheet#DeleteRow()
nnoremap (table-mode-delete-column) :call tablemode#spreadsheet#DeleteColumn()
nnoremap (table-mode-add-formula) :call tablemode#spreadsheet#formula#Add()
nnoremap (table-mode-eval-formula) :call tablemode#spreadsheet#formula#EvaluateFormulaLine()
nnoremap (table-mode-echo-cell) :call TableEchoCell()
if !hasmapto('(table-mode-tableize)')
nmap tt (table-mode-tableize)
xmap tt (table-mode-tableize)
endif
if !hasmapto('(table-mode-tableize-delimiter)')
xmap T (table-mode-tableize-delimiter)
endif
" Avoiding side effects {{{1
let &cpo = s:save_cpo
" ModeLine {{{
" vim: sw=2 sts=2 fdl=0 fdm=marker