Skip to content
2 changes: 1 addition & 1 deletion ftplugin/r_rnvim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ require("r").show_R_out = function()
routfile = vim.fn.expand("%:r") .. ".Rout"
if vim.fn.bufloaded(routfile) == 1 then
vim.api.nvim_command("bunload " .. routfile)
vim.fn.delete(routfile)
vim.fs.rm(routfile, { force = true })
end

-- If not silent, the user will have to type <Enter>
Expand Down
8 changes: 6 additions & 2 deletions lua/r/browser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,13 @@ function M.update_OB(what)
-- Read the data from temporary files created by R
local lines
if wht == "GlobalEnv" then
lines = vim.fn.readfile(config.tmpdir .. "/globenv_" .. vim.env.RNVIM_ID)
lines = vim.fn.readfile(
vim.fs.joinpath(config.tmpdir, "globenv_" .. vim.env.RNVIM_ID)
)
else
lines = vim.fn.readfile(config.tmpdir .. "/liblist_" .. vim.env.RNVIM_ID)
lines = vim.fn.readfile(
vim.fs.joinpath(config.tmpdir, "liblist_" .. vim.env.RNVIM_ID)
)
end
if not lines then lines = { "Error loading data" } end

Expand Down
96 changes: 48 additions & 48 deletions lua/r/config.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local utils = require("r.utils")
local uv = vim.uv
local fs = vim.fs
local hooks = require("r.hooks")

---@class RLSConfigOpts
Expand Down Expand Up @@ -839,14 +840,15 @@ local set_directories = function()
-- config.rnvim_home should be the directory where the plugin files are.
local rndir = debug.getinfo(1, "S").source
rndir = rndir:match("^@(.*)/lua/r/.*")
config.rnvim_home = rndir
config.rnvim_home = fs.normalize(rndir)

-- config.uservimfiles must be a writable directory. It will be config.rnvim_home
-- unless it's not writable. Then it wil be ~/.vim or ~/vimfiles.
if vim.fn.filewritable(config.rnvim_home) == 2 then
config.uservimfiles = config.rnvim_home
else
config.uservimfiles = vim.split(vim.fn.expand("&runtimepath"), ",")[1]
config.uservimfiles =
fs.normalize(vim.split(vim.fn.expand("&runtimepath"), ",")[1])
end

-- Windows logins can include domain, e.g: 'DOMAIN\Username', need to remove
Expand Down Expand Up @@ -881,64 +883,61 @@ local set_directories = function()
swarn("Could not determine user name.")
end

if config.is_windows then
config.rnvim_home = utils.normalize_windows_path(config.rnvim_home)
config.uservimfiles = utils.normalize_windows_path(config.uservimfiles)
end
vim.env.RNVIM_HOME = rndir

if config.compldir ~= "" then
config.compldir = vim.fn.expand(config.compldir)
config.compldir = fs.normalize(config.compldir)
elseif config.is_windows and vim.env.APPDATA then
config.compldir = vim.fn.expand(vim.env.APPDATA) .. "\\R.nvim"
config.compldir = fs.joinpath(vim.env.APPDATA, "R.nvim")
elseif vim.env.XDG_CACHE_HOME then
config.compldir = vim.fn.expand(vim.env.XDG_CACHE_HOME) .. "/R.nvim"
elseif vim.fn.isdirectory(vim.fn.expand("~/.cache")) ~= 0 then
config.compldir = vim.fn.expand("~/.cache/R.nvim")
elseif vim.fn.isdirectory(vim.fn.expand("~/Library/Caches")) ~= 0 then
config.compldir = vim.fn.expand("~/Library/Caches/R.nvim")
config.compldir = fs.joinpath(fs.normalize(vim.env.XDG_CACHE_HOME), "R.nvim")
elseif vim.fn.isdirectory(fs.normalize("~/.cache")) ~= 0 then
config.compldir = fs.normalize("~/.cache/R.nvim")
elseif vim.fn.isdirectory(fs.normalize("~/Library/Caches")) ~= 0 then
config.compldir = fs.normalize("~/Library/Caches/R.nvim")
else
config.compldir = config.uservimfiles .. "/R_cache/"
config.compldir = fs.joinpath(config.uservimfiles, "R_cache")
end

utils.ensure_directory_exists(config.compldir)

-- Check if the 'config' table has the key 'tmpdir'
if not config.tmpdir ~= "" then
local suffix = "R.nvim-" .. config.user_login
local base
-- Set temporary directory based on the platform
if config.is_windows then
if vim.env.TMP and vim.fn.isdirectory(vim.env.TMP) ~= 0 then
config.tmpdir = vim.env.TMP .. "/R.nvim-" .. config.user_login
base = fs.normalize(vim.env.TMP)
elseif vim.env.TEMP and vim.fn.isdirectory(vim.env.TEMP) ~= 0 then
config.tmpdir = vim.env.TEMP .. "/R.nvim-" .. config.user_login
base = fs.normalize(vim.env.TEMP)
else
config.tmpdir = config.uservimfiles .. "/R_tmp"
base = config.uservimfiles
suffix = "R_tmp"
end
config.tmpdir = utils.normalize_windows_path(config.tmpdir)
else
if vim.env.TMPDIR and vim.fn.isdirectory(vim.env.TMPDIR) ~= 0 then
if string.find(vim.env.TMPDIR, "/$") then
config.tmpdir = vim.env.TMPDIR .. "R.nvim-" .. config.user_login
else
config.tmpdir = vim.env.TMPDIR .. "/R.nvim-" .. config.user_login
end
base = fs.normalize(vim.env.TMPDIR)
elseif vim.fn.isdirectory("/dev/shm") ~= 0 then
config.tmpdir = "/dev/shm/R.nvim-" .. config.user_login
base = "/dev/shm"
elseif vim.fn.isdirectory("/tmp") ~= 0 then
config.tmpdir = "/tmp/R.nvim-" .. config.user_login
base = "/tmp"
else
config.tmpdir = config.uservimfiles .. "/R_tmp"
base = config.uservimfiles
suffix = "R_tmp"
end
end

config.tmpdir = fs.joinpath(base, suffix)
end

-- Adjust options when accessing R remotely
if config.remote_R_host ~= "" then
config.tmpdir = config.compldir .. "/remote/tmp"
config.tmpdir = fs.joinpath(config.compldir, "remote/tmp")
if config.remote_compl_dir == "" then
config.remote_compl_dir = config.compldir
end
config.remote_tmpdir = config.remote_compl_dir .. "/tmp"
config.remote_tmpdir = fs.joinpath(config.remote_compl_dir, "tmp")
end

utils.ensure_directory_exists(config.tmpdir)
Expand All @@ -947,7 +946,7 @@ local set_directories = function()
vim.env.RNVIM_COMPLDIR = config.compldir

-- Make the file name of files to be sourced
config.source_file = config.tmpdir .. "/Rsource-" .. vim.fn.getpid()
config.source_file = fs.joinpath(config.tmpdir, "Rsource-" .. vim.fn.getpid())
end

local check_readme = function()
Expand All @@ -966,7 +965,7 @@ local check_readme = function()
local l = vim.fn.split(vim.fn.glob(config.compldir .. "/*"), "\n")
if #l > 0 then
for _, f in ipairs(l) do
vim.fn.delete(f)
vim.fs.rm(f, { force = true })
end
end

Expand Down Expand Up @@ -1016,13 +1015,15 @@ local check_readme = function()
" - Line breaks are indicated by \\x14.",
}

vim.fn.writefile(readme, config.compldir .. "/README")
vim.fn.writefile(readme, fs.joinpath(config.compldir, "README"))
end
end

local do_common_global = function()
config.uname = uv.os_uname().sysname
config.is_windows = config.uname:find("Windows", 1, true) ~= nil
config.is_windows = not not (
config.uname:lower():find("windows") or config.uname:lower():find("mingw")
) -- in line with `vim.fs`
if config.r_ls.doc_width == 0 then
local dw = vim.o.columns / 2 - 4
if dw < 30 then dw = 30 end
Expand Down Expand Up @@ -1114,29 +1115,31 @@ local do_common_global = function()
end

local mtime = function(fname)
local fd = vim.uv.fs_open(fname, "r", tonumber("644", 8))
local fd = uv.fs_open(fname, "r", tonumber("644", 8))
local mt
if fd then
mt = vim.uv.fs_fstat(fd).mtime.sec
vim.uv.fs_close(fd)
mt = uv.fs_fstat(fd).mtime.sec
uv.fs_close(fd)
end
return mt
end

--- Install the "rout" parser, required to properly highlight R output in
--- hover and resolve windows from the language server
local check_rout_parser = function()
local routp = config.rnvim_home .. "/parser/rout.so"
local mt1 = mtime(config.rnvim_home .. "/resources/tree-sitter-rout/grammar.js")
local routp = fs.joinpath(config.rnvim_home, "parser/rout.so")
local mt1 =
mtime(fs.joinpath(config.rnvim_home, "resources/tree-sitter-rout/grammar.js"))
local mt2 = mtime(routp)
if mt1 and mt2 and mt2 > mt1 then return end
if vim.fn.executable("tree-sitter") == 0 then return end

local _, err = vim.uv.fs_mkdir(config.rnvim_home .. "/parser", tonumber("755", 8))
local _, err =
uv.fs_mkdir(fs.joinpath(config.rnvim_home, "parser"), tonumber("755", 8))
if err and not err:find("EEXIST") then return end

local cwdir = vim.uv.cwd()
vim.uv.chdir(config.rnvim_home .. "/resources/tree-sitter-rout")
local cwdir = uv.cwd()
uv.chdir(fs.joinpath(config.rnvim_home, "resources/tree-sitter-rout"))
-- from nvim-treesitter
local obj = vim.system({ "tree-sitter", "generate" }, { text = true }):wait(3000)
if obj.code ~= 0 then
Expand All @@ -1148,7 +1151,7 @@ local check_rout_parser = function()
swarn("Error building tree-sitter parser for `rout`: " .. obj.stderr)
return
end
if cwdir then vim.uv.chdir(cwdir) end
if cwdir then uv.chdir(cwdir) end
end

local global_setup = function()
Expand Down Expand Up @@ -1295,8 +1298,8 @@ M.real_setup = function()

local bufname = vim.api.nvim_buf_get_name(0)
if bufname ~= "" then
local rnc = vim.fs.joinpath(vim.fs.dirname(bufname), "rnvim_config.lua")
if vim.uv.fs_access(rnc, "R") then
local rnc = fs.joinpath(fs.dirname(bufname), "rnvim_config.lua")
if uv.fs_access(rnc, "R") then
local opts = dofile(rnc)
apply_user_opts(opts)
end
Expand Down Expand Up @@ -1356,17 +1359,14 @@ M.check_health = function()

-- Check if treesitter is available
local function has_parser(parser_name, parsers)
local path = "parser" .. (config.is_windows and "\\" or "/") .. parser_name .. "."
local path = "parser/" .. parser_name .. "."
for _, v in pairs(parsers) do
if v:find(path, 1, true) then return true end
end
return false
end

local parsers = vim.api.nvim_get_runtime_file(
"parser" .. (config.is_windows and "\\" or "/") .. "*.*",
true
)
local parsers = vim.api.nvim_get_runtime_file("parser/*", true) -- uses and returns forward-slashed paths despite the platform

local needed = { "r" }
if vim.tbl_contains({ "rmd", "quarto", "rnoweb", "typst" }, vim.bo.filetype) then
Expand Down
2 changes: 1 addition & 1 deletion lua/r/doc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ end
M.load_html = function(fullpath, browser)
if config.open_html == "no" then return end

local fname = fullpath:gsub(".*/", "")
local fname = vim.fs.basename(fullpath)
if job.is_running(fullpath) then
if config.open_html:find("focus") then
utils.focus_window(fname, job.get_pid(fullpath))
Expand Down
21 changes: 14 additions & 7 deletions lua/r/edit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ M.vim_leave = function()
end

for _, fn in pairs(del_list) do
vim.fn.delete(fn)
vim.fs.rm(fn, { force = true })
end

if config.remote_R_host == "" then
Expand Down Expand Up @@ -448,7 +448,7 @@ M.view_df = function(oname, txt)
if config.view_df.save_fun and config.view_df.save_fun ~= "" then
tsvnm = txt
else
tsvnm = config.tmpdir .. "/" .. oname .. ".tsv"
tsvnm = vim.fs.joinpath(config.tmpdir, oname .. ".tsv")
vim.fn.writefile(csv_lines, tsvnm)
end
M.add_for_deletion(tsvnm)
Expand Down Expand Up @@ -480,28 +480,35 @@ end
M.open_example = function()
local bl = vim.api.nvim_list_bufs()
for _, v in pairs(bl) do
if vim.api.nvim_buf_get_name(v) == config.tmpdir .. "/example.R" then
if
vim.fs.normalize(vim.api.nvim_buf_get_name(v))
== vim.fs.joinpath(config.tmpdir, "example.R")
then
vim.cmd("bunload! " .. tostring(v))
break
end
end

if config.nvimpager == "tabnew" or config.nvimpager == "tab" then
vim.cmd("tabnew " .. config.tmpdir:gsub(" ", "\\ ") .. "/example.R")
vim.cmd("tabnew " .. vim.fs.joinpath(config.tmpdir:gsub(" ", "\\ "), "example.R"))
else
if config.nvimpager == "split_v" then
vim.cmd(
"belowright vsplit " .. config.tmpdir:gsub(" ", "\\ ") .. "/example.R"
"belowright vsplit "
.. vim.fs.joinpath(config.tmpdir:gsub(" ", "\\ "), "example.R")
)
else
vim.cmd("belowright split " .. config.tmpdir:gsub(" ", "\\ ") .. "/example.R")
vim.cmd(
"belowright split "
.. vim.fs.joinpath(config.tmpdir:gsub(" ", "\\ "), "example.R")
)
end
end
vim.api.nvim_buf_set_keymap(0, "n", "q", ":q<CR>", { noremap = true, silent = true })
vim.api.nvim_set_option_value("bufhidden", "wipe", { scope = "local" })
vim.api.nvim_set_option_value("swapfile", false, { scope = "local" })
vim.api.nvim_set_option_value("buftype", "nofile", { scope = "local" })
vim.fn.delete(config.tmpdir .. "/example.R")
vim.fs.rm(vim.fs.joinpath(config.tmpdir, "example.R"), { force = true })
end

return M
5 changes: 3 additions & 2 deletions lua/r/pdf/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ M.open = function(fullpath)

if fullpath == "Get Master" then
local fpath = require("r.rnw").SyncTeX_get_master() .. ".pdf"
fpath = vim.b.rplugin_pdfdir .. "/" .. fpath:gsub(".*/", "")
fpath = vim.fs.joinpath(vim.b.rplugin_pdfdir, vim.fs.basename(fpath))
M.open(fpath)
return
end

local fname = fullpath:gsub(".*/", "")
local fname = vim.fs.basename(fullpath)

if job.is_running(fullpath) then
if config.open_pdf:find("focus") then
utils.focus_window(fname, job.get_pid(fullpath))
Expand Down
22 changes: 11 additions & 11 deletions lua/r/pdf/sumatra.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ local M = {}
M.open = function(fullpath)
if not SumatraInPath() then return end

local pdir = fullpath:gsub("(.*)/.*", "%1")
local olddir = vim.fn.getcwd():gsub("\\", "/"):gsub(" ", "\\ ")
local pdir = vim.fs.dirname(fullpath)
local olddir = vim.fs.normalize(vim.fn.getcwd()):gsub(" ", "\\ ")
vim.cmd("cd " .. pdir)
vim.fn.writefile({
'start SumatraPDF.exe -reuse-instance -inverse-search "rnvimserver.exe %%f %%l" "'
.. fullpath
.. '"',
}, config.tmpdir .. "/run_cmd.bat")
vim.system({ config.tmpdir .. "/run_cmd.bat" }, { detach = true })
}, vim.fs.joinpath(config.tmpdir, "run_cmd.bat"))
vim.system({ vim.fs.joinpath(config.tmpdir, "run_cmd.bat") }, { detach = true })
vim.cmd("cd " .. olddir)
require("r.edit").add_for_deletion(config.tmpdir .. "/run_cmd.bat")
require("r.edit").add_for_deletion(vim.fs.joinpath(config.tmpdir, "run_cmd.bat"))
end

---Send the SyncTeX forward command to Sumatra
Expand All @@ -55,10 +55,10 @@ M.SyncTeX_forward = function(tpath, ppath, texln)
if not SumatraInPath() then return end

-- Empty spaces must be removed from the rnoweb file name to get SyncTeX support with SumatraPDF.
local tname = tpath:gsub(".*/(.*)", "%1")
local tdir = tpath:gsub("(.*)/.*", "%1")
local tname = vim.fs.basename(tpath)
local tdir = vim.fs.dirname(tpath)
local pname = ppath:gsub(tdir .. "/", "")
local olddir = vim.fn.getcwd():gsub("\\", "/"):gsub(" ", "\\ ")
local olddir = vim.fs.normalize(vim.fn.getcwd()):gsub(" ", "\\ ")
vim.cmd("cd " .. tdir:gsub(" ", "\\ "))
vim.fn.writefile({
'start SumatraPDF.exe -reuse-instance -forward-search "'
Expand All @@ -68,10 +68,10 @@ M.SyncTeX_forward = function(tpath, ppath, texln)
.. ' -inverse-search "rnvimserver.exe %%f %%l" "'
.. pname
.. '"',
}, config.tmpdir .. "/run_cmd.bat")
vim.system({ config.tmpdir .. "/run_cmd.bat" }, { detach = true })
}, vim.fs.joinpath(config.tmpdir, "run_cmd.bat"))
vim.system({ vim.fs.joinpath(config.tmpdir, "run_cmd.bat") }, { detach = true })
vim.cmd("cd " .. olddir)
require("r.edit").add_for_deletion(config.tmpdir .. "/run_cmd.bat")
require("r.edit").add_for_deletion(vim.fs.joinpath(config.tmpdir, "run_cmd.bat"))
end

return M
Loading
Loading