-- groff/troff specific stuff

-- generalized groff macros

vis:command_register('bold', function(argv)
	vis:insert('.B ')
	end, 'Prints groff bold (pp) token'
)

vis:command_register('pp', function(argv)
	vis:insert('.PP\n')
	end, 'Prints groff paragraph (pp) token'
)

vis:command_register('tl', function(argv)
	vis:insert('.TL\n.AU\n.SH\n')
	vis:feedkeys('3k')
	end, 'Prints groff three-part title (tl, au, sh) tokens consisting of title, author and soft-hyphen'
)

vis:command_register('ab', function(argv)
	vis:insert('{}')
	end, 'Prints angular brackets'
)

vis:command_register('sb', function(argv)
	vis:insert('[]')
	end, 'Prints square brackets'
)

vis:command_register('eq', function(argv)
	vis:insert('.EQ\n\n.EN')
	end, 'Prints eqn tokens'
)

vis:command_register('comp', function(argv, force, win, selection, range)
	local filepathpdf = win.file.path:gsub('.ms', '.pdf')
	local res, out = vis:pipe(
		win.file,
		{ start = 0, finish = 0 },
		'groff -m www -e  '..win.file.path..' -ms -Tpdf > '..filepathpdf
	)
    vis:info('compiled '..filepathpdf)
	end, 'Converts currently viewed groff file into a pdf'
)

-- INSERT mode command macros

vis:map(vis.modes.INSERT, '<C-b>', function()
	vis:command('bold')
    vis:feedkeys('i')
end)

vis:map(vis.modes.INSERT, '<C-p>', function()
	vis:command('pp')
    vis:feedkeys('i')
end)

vis:map(vis.modes.INSERT, '<C-a>', function()
	vis:command('ab')
    vis:feedkeys('hi')
end)

vis:map(vis.modes.INSERT, '<C-e>', function()
	vis:command('eq')
    vis:feedkeys('ki')
end)

-- NORMAL mode command macros

vis:map(vis.modes.NORMAL, '<C-c>', function()
	vis:command('comp')
end)
