Skip to content

Commit

Permalink
Fix starting R with custom arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jalvesaq committed Feb 21, 2024
1 parent dae066c commit dcac55f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
16 changes: 10 additions & 6 deletions lua/r/external_term.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ end

local M = {}

M.start_extern_term = function(Rcmd)
M.start_extern_term = function()
local rcmd = config.R_app .. " " .. require("r.run").get_r_args()

local tmuxcnf = " "
if config.config_tmux then
tmuxcnf = '-f "' .. config.tmpdir .. "/tmux.conf" .. '"'
Expand Down Expand Up @@ -149,7 +151,7 @@ M.start_extern_term = function(Rcmd)
.. " R_DEFAULT_PACKAGES="
.. vim.env.R_DEFAULT_PACKAGES
.. " "
.. Rcmd
.. rcmd

if config.is_darwin then
open_cmd = string.format(
Expand Down Expand Up @@ -229,10 +231,12 @@ M.send_cmd_to_external_term = function(command)
if str:find("^-") then str = " " .. str end

if not base_pane_index then
local obj = utils.system(
{ "tmux", "-L", "Rnvim show-options", "-gv", "pane-base-index" },
{ text = true }
):wait()
local obj = utils
.system(
{ "tmux", "-L", "Rnvim show-options", "-gv", "pane-base-index" },
{ text = true }
)
:wait()
base_pane_index = obj.stdout:gsub("\n+$", "")
end
local scmd
Expand Down
2 changes: 1 addition & 1 deletion lua/r/osx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ M.start_Rapp = function()

local rcmd = r64app and "/Applications/R64.app" or "/Applications/R.app"

local args_str = table.concat(config.R_args, " ")
local args_str = require("r.run").get_r_args()
if args_str ~= " " and args_str ~= "" then
-- https://github.com/jcfaria/Vim-R-plugin/issues/63
-- https://stat.ethz.ch/pipermail/r-sig-mac/2013-February/009978.html
Expand Down
24 changes: 14 additions & 10 deletions lua/r/run.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ start_R2 = function()
return
end

r_args = table.concat(config.R_args, " ")
if what_R:find("custom") then
r_args = vim.fn.split(vim.fn.input("Enter parameters for R: "))
else
r_args = config.R_args
vim.ui.input({ prompt = "Enter parameters for R: " }, function(input)
if input then r_args = input end
end)
end

vim.fn.writefile({}, config.localtmpdir .. "/globenv_" .. vim.env.RNVIM_ID)
Expand Down Expand Up @@ -148,12 +149,14 @@ start_R2 = function()
return
end

local args_str = table.concat(r_args, " ")
local rcmd = config.R_app .. " " .. args_str

require("r.external_term").start_extern_term(rcmd)
require("r.external_term").start_extern_term()
end

--- Return arguments to start R defined as config.R_args or during custom R
--- start.
---@return string
M.get_r_args = function() return r_args end

M.auto_start_R = function()
if vim.g.R_Nvim_status > 3 then return end
if vim.v.vim_did_enter == 0 or vim.g.R_Nvim_status < 3 then
Expand Down Expand Up @@ -210,7 +213,9 @@ end

-- Send SIGINT to R
M.signal_to_R = function(signal)
if R_pid ~= 0 then utils.system({ "kill", "-s", tostring(signal), tostring(R_pid) }) end
if R_pid ~= 0 then
utils.system({ "kill", "-s", tostring(signal), tostring(R_pid) })
end
end

M.check_nvimcom_running = function()
Expand All @@ -227,8 +232,7 @@ M.check_nvimcom_running = function()
end

M.wait_nvimcom_start = function()
local args_str = table.concat(r_args, " ")
if string.find(args_str, "vanilla") then return 0 end
if string.find(r_args, "vanilla") then return 0 end

if config.wait < 2 then config.wait = 2 end

Expand Down
2 changes: 1 addition & 1 deletion lua/r/term.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ M.start_term = function()
split_window()

if config.is_windows then require("r.windows").set_R_home() end
require("r.job").R_term_open(config.R_app .. " " .. table.concat(config.R_args, " "))
require("r.job").R_term_open(config.R_app .. " " .. require("r.run").get_r_args())
if config.is_windows then
-- vim.cmd("redraw") -- superfluous?
require("r.windows").unset_R_home()
Expand Down
18 changes: 10 additions & 8 deletions lua/r/windows.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ M.set_R_home = function()
-- R and Vim use different values for the $HOME variable.
if config.set_home_env then
saved_home = vim.env.HOME
local obj = utils.system({
"reg.exe",
"QUERY",
"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
"/v",
"Personal",
}, { text = true }):wait()
local obj = utils
.system({
"reg.exe",
"QUERY",
"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
"/v",
"Personal",
}, { text = true })
:wait()
local prs = obj.stdout
if prs and #prs > 0 then
prs = prs:gsub(".*REG_SZ%s*", "")
Expand Down Expand Up @@ -42,7 +44,7 @@ M.start_Rgui = function()
end

M.set_R_home()
vim.fn.system("start " .. config.R_app .. " " .. table.concat(config.R_args, " "))
vim.fn.system("start " .. config.R_app .. " " .. require("r.run").get_r_args())
M.unset_R_home()

require("r.run").wait_nvimcom_start()
Expand Down

0 comments on commit dcac55f

Please sign in to comment.