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

View File

@ -39,7 +39,11 @@ packer.use {
} }
map('n', '<Leader>tb', require('telescope.builtin').buffers) 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>tr', require('telescope.builtin').oldfiles)
map('n', '<Leader>tg', require('telescope.builtin').live_grep) map('n', '<Leader>tg', require('telescope.builtin').live_grep)
map('n', '<Leader>tm', require('telescope.builtin').man_pages) 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> -- <https://github.com/L3MON4D3/LuaSnip/blob/69cb81cf7490666890545fef905d31a414edc15b/lua/luasnip/config.lua#L82-L104>
local s = require("luasnip.nodes.snippet").S local s = require("luasnip.nodes.snippet").S
local f = require("luasnip.nodes.functionNode").F local f = require("luasnip.nodes.functionNode").F
@ -43,6 +41,7 @@ local bsd0 =
{cursor}]] {cursor}]]
local my = require('my.functions')
return { return {
s( s(
{ {
@ -52,7 +51,7 @@ return {
}, },
fmt(agpl, { fmt(agpl, {
project = f(function() project = f(function()
local root = MY_get_project_root() local root = my.get_project_root()
if root then return root:gsub('.*/', '') end if root then return root:gsub('.*/', '') end
return 'INSERT_PROJECT' return 'INSERT_PROJECT'
end), end),
@ -60,11 +59,11 @@ return {
return os.date('%Y') return os.date('%Y')
end), end),
name = f(function() name = f(function()
return MY_shell_capture('git config user.name') return my.shell_capture('git config user.name')
or 'INSERT_NAME' or 'INSERT_NAME'
end), end),
email = f(function() email = f(function()
return MY_shell_capture('git config user.email') return my.shell_capture('git config user.email')
or 'INSERT_EMAIL' or 'INSERT_EMAIL'
end), end),
cursor = i() cursor = i()
@ -78,7 +77,7 @@ return {
}, },
fmt(bsd0, { fmt(bsd0, {
project = f(function() project = f(function()
local root = MY_get_project_root() local root = my.get_project_root()
if root then return root:gsub('.*/', '') end if root then return root:gsub('.*/', '') end
return 'INSERT_PROJECT' return 'INSERT_PROJECT'
end), end),
@ -86,11 +85,11 @@ return {
return os.date('%Y') return os.date('%Y')
end), end),
name = f(function() name = f(function()
return MY_shell_capture('git config user.name') return my.shell_capture('git config user.name')
or 'INSERT_NAME' or 'INSERT_NAME'
end), end),
email = f(function() email = f(function()
return MY_shell_capture('git config user.email') return my.shell_capture('git config user.email')
or 'INSERT_EMAIL' or 'INSERT_EMAIL'
end), end),
cursor = i() cursor = i()