" =============================================================================
" 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', '|')
function! s:TableEchoCell() "{{{1
if tablemode#table#IsATableRow('.')
echomsg '$' . tablemode#spreadsheet#RowNr('.') . ',' . tablemode#spreadsheet#ColumnNr('.')
endif
endfunction
" 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#TableModeToggle()"
command! -nargs=0 TableModeToggle call tablemode#TableModeToggle()
command! -nargs=0 TableModeEnable call tablemode#TableModeEnable()
command! -nargs=0 TableModeDisable call tablemode#TableModeDisable()
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)
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
if !hasmapto('(table-mode-realign)')
nmap tr (table-mode-realign)
endif
if !hasmapto('(table-mode-motion-up)')
nmap { (table-mode-motion-up)
endif
if !hasmapto('(table-mode-motion-down)')
nmap } (table-mode-motion-down)
endif
if !hasmapto('(table-mode-motion-left)')
nmap [ (table-mode-motion-left)
endif
if !hasmapto('(table-mode-motion-right)')
nmap ] (table-mode-motion-right)
endif
if !hasmapto('(table-mode-cell-text-object-a)')
omap a (table-mode-cell-text-object-a)
endif
if !hasmapto('(table-mode-cell-text-object-i)')
omap i (table-mode-cell-text-object-i)
endif
if !hasmapto('(table-mode-delete-row)')
nmap tdd (table-mode-delete-row)
endif
if !hasmapto('(table-mode-delete-column)')
nmap tdc (table-mode-delete-column)
endif
if !hasmapto('(table-mode-add-formula)')
nmap tfa (table-mode-add-formula)
endif
if !hasmapto('(table-mode-eval-formula)')
nmap tfe (table-mode-eval-formula)
endif
if !hasmapto('(table-mode-echo-cell)')
nmap t? (table-mode-echo-cell)
endif
" Avoiding side effects {{{1
let &cpo = s:save_cpo
" ModeLine {{{
" vim: sw=2 sts=2 fdl=0 fdm=marker