-- language specific stuff
-- file ext detection
local fil = vis.win.file.name
if not fil then fil = '' end
local ext = fil:match('^.+(%..+)$')

-- body is the body of the structure
local printbody = ''
-- seq is the sequence of motions that has to be executed to end up at a
-- useful position
local printseq = ''
-- set determines wheter the sequences are set
local printset = false
local ifbody = ''
local ifseq = ''
local ifset = false
local forbody = ''
local forseq = ''
local forset = false
local methodbody = ''
local methodseq = ''
local methodset = false

-- respect gay tab enforcement
if fil == "Makefile" then
	vis:command('set et off')
end
if     (ext == '.c') or (ext == '.h') or (ext == '.cpp') or (ext == '.hpp') then
	printbody = 'printf(\"\\n\");'
	printseq = '<Escape>5hi'
	printset = true
	ifbody = 'if () {<Enter><C-i><++><Enter><C-d>}'
	ifseq = '<Escape>[{2h'
	ifset = true
	forbody = 'for (int i = 0; i ; i++) {<Enter><C-i><++><Enter><C-d>}'
	forseq = '<Escape>[{3b'
	forset = true
	methodbody = '(<++>)<Enter>{<Enter><C-i><++><Enter><C-d>}'
	methodseq = '<Escape>[{k0'
	methodset = true
elseif ext == '.java' then
	printbody = 'System.out.println("");'
	printseq = '<Escape>3hi'
	printset = true
	ifbody = 'if () {<Enter><C-i><++><Enter><C-d>}'
	ifseq = '<Escape>[{2h'
	ifset = true
	forbody = 'for (int i = 0; i ; i++) {<Enter><C-i><++><Enter><C-d>}'
	forseq = '<Escape>[{3b'
	forset = true
	methodbody = '(<++>) {<Enter><C-i><++><Enter><C-d>}'
	methodseq = '<Escape>[{0'
	methodset = true

	vis:command_register('psvm', function(argv)
		vis:command('set ai 1')
		vis:feedkeys('<Escape>ipublic static void main(String[] args) {<Escape>o}<Escape>O<C-i>')
		vis:command('set ai 0')
		end, 'prints PUBLIC STATIC VOID MAIN the java classic'
	)
elseif (ext == '.py') or (ext == '.lua') then
	printbody = 'print()'
	printseq = '<Left>'
	printset = true
elseif (ext == '.sh') then
	printbody = 'echo '
	printseq = 'i'
	printset = true
elseif (ext == '.scm') then
	forbody = '()'
	forseq = '<Left>'
	forset = true
elseif (ext == '.md') then
	-- markdown specialties
	require('plugins/ppdf')
	require('plugins/maker')
elseif (ext == '.html') then
	-- html intricacies
	require('plugins/html')
elseif (ext == '.ms') then
	-- groff intricacies
	require('plugins/groff')
end

if printset then
	vis:map(vis.modes.INSERT, '<C-p>', function()
		vis:feedkeys(printbody..printseq)
	end)
end

if ifset then
	vis:map(vis.modes.INSERT, '<C-a>', function()
		vis:command('set ai 1')
		vis:feedkeys('i'..ifbody..ifseq)
		vis:command('set ai 0')
		vis:feedkeys('i')
	end)
end

if forset then
	vis:map(vis.modes.INSERT, '<C-b>', function()
		vis:command('set ai 1')
		vis:feedkeys('i'..forbody..forseq)
		vis:command('set ai 0')
		vis:feedkeys('i')
	end)
end

if methodset then
	vis:map(vis.modes.INSERT, '<C-e>', function()
		vis:command('set ai 1')
		vis:feedkeys('i'..methodbody..methodseq)
		vis:command('set ai 0')
		vis:feedkeys('i')
	end)
end

