diff --git a/lua/custom/functions.lua b/lua/custom/functions.lua index 3e5df69..8442554 100644 --- a/lua/custom/functions.lua +++ b/lua/custom/functions.lua @@ -1,4 +1,3 @@ --- Alternatively, using a Lua function local function lines_with_preview() local current_file = vim.fn.fnameescape(vim.fn.expand('%')) local rg_command = string.format("rg --with-filename --column --line-number --no-heading --color=always --smart-case . %s", current_file) @@ -6,4 +5,44 @@ local function lines_with_preview() vim.cmd(fzf_command) end +function DevtoolsPath() + local devPath = vim.fn.trim(vim.fn.system('ls ' .. vim.fn.expand("%:p:h") .. ' | grep DESCRIPTION')) + + if devPath == "" then + devPath = vim.fn.trim(vim.fn.system('realpath ' .. vim.fn.expand("%:p:h") .. '/../')) + return devPath + else + devPath = vim.fn.expand('%:p:h') + return devPath + end +end + vim.api.nvim_create_user_command('LinesWithPreview', lines_with_preview, {}) +vim.api.nvim_create_autocmd({"BufNewFile", "BufRead"}, { + pattern = "DESCRIPTION", + command = "set filetype=r" +}) + +-- Map wd to send devtools::document command in R +vim.api.nvim_create_autocmd("FileType", { + pattern = "r", + callback = function() + vim.api.nvim_set_keymap('n', 'wd', ':RSend devtools::document("' .. DevtoolsPath() .. '")', { noremap = true, silent = true }) + end +}) + +-- Map wi to send devtools::install command in R +vim.api.nvim_create_autocmd("FileType", { + pattern = "r", + callback = function() + vim.api.nvim_set_keymap('n', 'wi', ':RSend devtools::install("' .. DevtoolsPath() .. '", quick = TRUE, keepSource=TRUE, upgrade = "never")', { noremap = true, silent = true }) + end +}) + +vim.api.nvim_create_autocmd("FileType", { + pattern = "r", + callback = function() + vim.api.nvim_set_keymap('n', 'i', ':RSend install.packages("")', { noremap = true, silent = true }) + end +}) +