2022-08-13 19:27:37 +02:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
function M.remove_trailing_whitespace()
|
2022-08-09 11:26:00 +02:00
|
|
|
local curpos = vim.api.nvim_win_get_cursor(0)
|
|
|
|
vim.cmd([[keeppatterns %s/\s\+$//e]])
|
|
|
|
vim.api.nvim_win_set_cursor(0, curpos)
|
|
|
|
end
|
2022-08-10 17:52:25 +02:00
|
|
|
vim.api.nvim_create_augroup('config_functions', { clear = true })
|
2022-08-09 11:26:00 +02:00
|
|
|
vim.api.nvim_create_autocmd(
|
|
|
|
{ 'BufWritePre' },
|
|
|
|
{
|
2022-08-10 17:52:25 +02:00
|
|
|
group = 'config_functions',
|
2022-08-09 11:26:00 +02:00
|
|
|
pattern = {
|
2022-08-12 14:09:38 +02:00
|
|
|
'*.lua', '*.cpp',
|
|
|
|
'*.hpp', '*.conf', '*.cfg', '*.ini'
|
2022-08-09 11:26:00 +02:00
|
|
|
},
|
2022-08-13 19:27:37 +02:00
|
|
|
callback = M.remove_trailing_whitespace
|
2022-08-09 11:26:00 +02:00
|
|
|
}
|
|
|
|
)
|
2022-08-09 13:28:46 +02:00
|
|
|
|
2022-08-13 19:27:37 +02:00
|
|
|
function M.insert_modeline()
|
2022-08-09 13:28:46 +02:00
|
|
|
local comment_string = vim.o.commentstring
|
|
|
|
local space_maybe = ''
|
2022-08-12 14:05:53 +02:00
|
|
|
if string.match(comment_string, '%%s(.*)') ~= '' then
|
2022-08-09 13:28:46 +02:00
|
|
|
space_maybe = ' '
|
|
|
|
end
|
|
|
|
|
|
|
|
local fenc = vim.o.fileencoding
|
2022-08-12 14:05:53 +02:00
|
|
|
if fenc == '' then
|
2022-08-09 13:28:46 +02:00
|
|
|
fenc = 'utf-8'
|
|
|
|
end
|
|
|
|
|
|
|
|
local modeline_elements = {
|
|
|
|
' vim: set',
|
|
|
|
' fenc=' .. fenc,
|
|
|
|
' ts=' .. vim.o.tabstop,
|
|
|
|
' sw=' .. vim.o.shiftwidth,
|
|
|
|
(vim.o.expandtab and ' et' or ' noet'),
|
|
|
|
' tw=' .. vim.o.textwidth,
|
|
|
|
':',
|
|
|
|
space_maybe
|
|
|
|
}
|
|
|
|
local modeline = comment_string:gsub('%%s', table.concat(modeline_elements))
|
|
|
|
modeline = modeline:gsub(' ', ' ')
|
|
|
|
|
|
|
|
local buffer = vim.api.nvim_win_get_buf(0)
|
|
|
|
local current = vim.api.nvim_buf_get_lines(buffer, -2, -1, true)[1]
|
2022-08-12 14:05:53 +02:00
|
|
|
if current == modeline then
|
2022-08-09 13:28:46 +02:00
|
|
|
print('modeline already exists')
|
2022-08-12 14:05:53 +02:00
|
|
|
elseif string.match(current, 'vim:') then
|
2022-08-09 13:28:46 +02:00
|
|
|
vim.api.nvim_buf_set_lines(buffer, -2, -1, true, { modeline })
|
|
|
|
print('modeline updated')
|
|
|
|
else
|
|
|
|
vim.api.nvim_buf_set_lines(buffer, -1, -1, true, { '', modeline })
|
|
|
|
print('modeline inserted')
|
|
|
|
end
|
|
|
|
end
|
2022-08-13 19:27:37 +02:00
|
|
|
vim.cmd([[command! ModelineInsert lua M.insert_modeline()]])
|
2022-08-11 01:58:06 +02:00
|
|
|
|
|
|
|
-- set colorcolumn to textwidth after buffer is displayed or option is changed
|
2022-08-13 19:27:37 +02:00
|
|
|
function M.set_colorcolumn()
|
2022-08-11 01:58:06 +02:00
|
|
|
if vim.o.textwidth > 0 then
|
|
|
|
vim.opt_local.colorcolumn = { vim.o.textwidth }
|
|
|
|
else
|
|
|
|
vim.opt_local.colorcolumn = ''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
vim.api.nvim_create_augroup('config_settings', { clear = true })
|
|
|
|
vim.api.nvim_create_autocmd(
|
|
|
|
{ 'BufEnter' },
|
|
|
|
{
|
|
|
|
group = 'config_settings',
|
2022-08-13 19:27:37 +02:00
|
|
|
callback = M.set_colorcolumn
|
2022-08-11 01:58:06 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
vim.api.nvim_create_autocmd(
|
|
|
|
{ 'OptionSet' },
|
|
|
|
{
|
|
|
|
group = 'config_settings',
|
|
|
|
pattern = { 'textwidth' },
|
2022-08-13 19:27:37 +02:00
|
|
|
callback = M.set_colorcolumn
|
2022-08-11 01:58:06 +02:00
|
|
|
}
|
|
|
|
)
|
2022-08-13 00:15:32 +02:00
|
|
|
|
2022-08-13 19:27:37 +02:00
|
|
|
function M.get_project_root()
|
2022-08-13 00:15:32 +02:00
|
|
|
local path = vim.api.nvim_buf_get_name(0)
|
|
|
|
local root_markers = {
|
|
|
|
'.git', '.hg', '.svn', '.bzr', '_darcs',
|
|
|
|
'.projectile', '.clang-format', '.luarc.json'
|
|
|
|
}
|
|
|
|
local sep = '/'
|
|
|
|
|
|
|
|
repeat
|
2022-08-13 01:06:08 +02:00
|
|
|
path = path:gsub(string.format('%s[^%s]*$', sep, sep), '')
|
2022-08-13 00:15:32 +02:00
|
|
|
for _, marker in ipairs(root_markers) do
|
|
|
|
if io.open(path .. sep .. marker) then
|
|
|
|
return path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
until path == ''
|
2022-08-13 01:19:25 +02:00
|
|
|
return nil
|
2022-08-13 00:15:32 +02:00
|
|
|
end
|
|
|
|
|
2022-08-13 19:27:37 +02:00
|
|
|
function M.shell_capture(command)
|
2022-08-13 00:15:32 +02:00
|
|
|
local handle = io.popen(command)
|
2022-08-13 01:25:43 +02:00
|
|
|
if not handle then return nil end
|
|
|
|
local result = handle:read() or nil
|
2022-08-13 00:15:32 +02:00
|
|
|
handle:close()
|
|
|
|
return result
|
|
|
|
end
|
2022-08-13 19:27:37 +02:00
|
|
|
|
|
|
|
return M
|