nvim: turn my functions into proper module

This commit is contained in:
tastytea 2022-08-13 19:27:37 +02:00
parent c052b052dc
commit 673af86eaa
Signed by: tastytea
SSH Key Fingerprint: SHA256:FBkvrOlhq5use1XEttyUGT4bUTDVA1ar9SgIc9P03cM
3 changed files with 25 additions and 18 deletions

View File

@ -1,4 +1,6 @@
function MY_remove_trailing_whitespace()
local M = {}
function M.remove_trailing_whitespace()
local curpos = vim.api.nvim_win_get_cursor(0)
vim.cmd([[keeppatterns %s/\s\+$//e]])
vim.api.nvim_win_set_cursor(0, curpos)
@ -12,11 +14,11 @@ vim.api.nvim_create_autocmd(
'*.lua', '*.cpp',
'*.hpp', '*.conf', '*.cfg', '*.ini'
},
callback = MY_remove_trailing_whitespace
callback = M.remove_trailing_whitespace
}
)
function MY_insert_modeline()
function M.insert_modeline()
local comment_string = vim.o.commentstring
local space_maybe = ''
if string.match(comment_string, '%%s(.*)') ~= '' then
@ -53,10 +55,10 @@ function MY_insert_modeline()
print('modeline inserted')
end
end
vim.cmd([[command! ModelineInsert lua MY_insert_modeline()]])
vim.cmd([[command! ModelineInsert lua M.insert_modeline()]])
-- set colorcolumn to textwidth after buffer is displayed or option is changed
function MY_set_colorcolumn()
function M.set_colorcolumn()
if vim.o.textwidth > 0 then
vim.opt_local.colorcolumn = { vim.o.textwidth }
else
@ -68,7 +70,7 @@ vim.api.nvim_create_autocmd(
{ 'BufEnter' },
{
group = 'config_settings',
callback = MY_set_colorcolumn
callback = M.set_colorcolumn
}
)
vim.api.nvim_create_autocmd(
@ -76,11 +78,11 @@ vim.api.nvim_create_autocmd(
{
group = 'config_settings',
pattern = { 'textwidth' },
callback = MY_set_colorcolumn
callback = M.set_colorcolumn
}
)
function MY_get_project_root()
function M.get_project_root()
local path = vim.api.nvim_buf_get_name(0)
local root_markers = {
'.git', '.hg', '.svn', '.bzr', '_darcs',
@ -99,10 +101,12 @@ function MY_get_project_root()
return nil
end
function MY_shell_capture(command)
function M.shell_capture(command)
local handle = io.popen(command)
if not handle then return nil end
local result = handle:read() or nil
handle:close()
return result
end
return M

View File

@ -39,7 +39,11 @@ packer.use {
}
map('n', '<Leader>tb', require('telescope.builtin').buffers)
map('n', '<Leader>tf', function() require('telescope.builtin').find_files({ cwd = MY_get_project_root() }) end)
map('n', '<Leader>tf', function()
require('telescope.builtin').find_files({
cwd = require('my.functions').get_project_root()
})
end)
map('n', '<Leader>tr', require('telescope.builtin').oldfiles)
map('n', '<Leader>tg', require('telescope.builtin').live_grep)
map('n', '<Leader>tm', require('telescope.builtin').man_pages)

View File

@ -1,5 +1,3 @@
require('my/functions')
-- <https://github.com/L3MON4D3/LuaSnip/blob/69cb81cf7490666890545fef905d31a414edc15b/lua/luasnip/config.lua#L82-L104>
local s = require("luasnip.nodes.snippet").S
local f = require("luasnip.nodes.functionNode").F
@ -43,6 +41,7 @@ local bsd0 =
{cursor}]]
local my = require('my.functions')
return {
s(
{
@ -52,7 +51,7 @@ return {
},
fmt(agpl, {
project = f(function()
local root = MY_get_project_root()
local root = my.get_project_root()
if root then return root:gsub('.*/', '') end
return 'INSERT_PROJECT'
end),
@ -60,11 +59,11 @@ return {
return os.date('%Y')
end),
name = f(function()
return MY_shell_capture('git config user.name')
return my.shell_capture('git config user.name')
or 'INSERT_NAME'
end),
email = f(function()
return MY_shell_capture('git config user.email')
return my.shell_capture('git config user.email')
or 'INSERT_EMAIL'
end),
cursor = i()
@ -78,7 +77,7 @@ return {
},
fmt(bsd0, {
project = f(function()
local root = MY_get_project_root()
local root = my.get_project_root()
if root then return root:gsub('.*/', '') end
return 'INSERT_PROJECT'
end),
@ -86,11 +85,11 @@ return {
return os.date('%Y')
end),
name = f(function()
return MY_shell_capture('git config user.name')
return my.shell_capture('git config user.name')
or 'INSERT_NAME'
end),
email = f(function()
return MY_shell_capture('git config user.email')
return my.shell_capture('git config user.email')
or 'INSERT_EMAIL'
end),
cursor = i()