From f2e1028dc65c9ff020486a24b79201e8b6236adb Mon Sep 17 00:00:00 2001 From: "Guilherme D. Garcia" Date: Sun, 8 Feb 2026 19:09:02 -0500 Subject: [PATCH] feat: add RStartHorizontal and RStartVertical mappings Add RStartHorizontal (\rfh) and RStartVertical (\rfv) mappings that let users start R with an explicit 50/50 split direction. The direction is passed cleanly as a function parameter through start_R -> start_R2 -> builtin.start -> split_window, with no config mutation. The existing \rf mapping and all config-based split logic remain unchanged. --- lua/r/maps.lua | 8 ++++++-- lua/r/run.lua | 9 ++++++--- lua/r/term/builtin.lua | 21 ++++++++++++++++----- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/lua/r/maps.lua b/lua/r/maps.lua index 97c9c88f..0d76c569 100644 --- a/lua/r/maps.lua +++ b/lua/r/maps.lua @@ -9,6 +9,8 @@ local map_desc = { RSaveClose = { m = "", k = "", c = "Start", d = "Quit R, saving the workspace" }, RClose = { m = "", k = "", c = "Start", d = "Send to R: quit(save = 'no')" }, RStart = { m = "", k = "", c = "Start", d = "Start R with default configuration or reopen terminal window" }, + RStartHorizontal = { m = "", k = "", c = "Start", d = "Start R in a horizontal split (50/50)" }, + RStartVertical = { m = "", k = "", c = "Start", d = "Start R in a vertical split (50/50)" }, RInsertAssign = { m = "", k = "", c = "Edit", d = "Insert ` <- `" }, RInsertPipe = { m = "", k = "", c = "Edit", d = "Insert ` |>` (or ` %>%`)" }, ROpenPDF = { m = "", k = "", c = "Edit", d = "Open the PDF generated from the current document" }, @@ -229,8 +231,10 @@ end local start = function() -- Start - create_maps("nvi", "RStart", "rf", "lua require('r.run').start_R('R')") - create_maps("nvi", "RCustomStart", "rc", "lua require('r.run').start_R('custom')") + create_maps("nvi", "RStart", "rf", "lua require('r.run').start_R('R')") + create_maps("nvi", "RCustomStart", "rc", "lua require('r.run').start_R('custom')") + create_maps("nvi", "RStartHorizontal", "rfh", "lua require('r.run').start_R('R', 'horizontal')") + create_maps("nvi", "RStartVertical", "rfv", "lua require('r.run').start_R('R', 'vertical')") -- Close create_maps("nvi", "RClose", "rq", "lua require('r.run').quit_R('nosave')") diff --git a/lua/r/run.lua b/lua/r/run.lua index 03cd3580..69ed9afa 100644 --- a/lua/r/run.lua +++ b/lua/r/run.lua @@ -7,6 +7,7 @@ local send = require("r.send") local cursor = require("r.cursor") local hooks = require("r.hooks") local what_R = "R" +local split_override = nil local R_pid = 0 local r_args local nseconds @@ -134,7 +135,8 @@ start_R2 = function() end if config.external_term == "" then - require("r.term.builtin").start() + require("r.term.builtin").start(split_override) + split_override = nil return end @@ -180,10 +182,10 @@ M.set_rns_port = function(p) vim.env.RNVIM_PORT = p end -M.start_R = function(whatr) +M.start_R = function(whatr, split) -- R started and nvimcom loaded if vim.g.R_Nvim_status == 7 then - if config.external_term == "" then require("r.term.builtin").reopen_win() end + if config.external_term == "" then require("r.term.builtin").reopen_win(split) end return end @@ -218,6 +220,7 @@ M.start_R = function(whatr) require("r.send").set_send_cmd_fun() require("r.lsp").send_msg({ code = "1" }) what_R = whatr + split_override = split vim.fn.timer_start(30, start_R2) return end diff --git a/lua/r/term/builtin.lua b/lua/r/term/builtin.lua index 3dc48eb2..63214fe2 100644 --- a/lua/r/term/builtin.lua +++ b/lua/r/term/builtin.lua @@ -68,7 +68,18 @@ M.close_term = function() r_bufnr = nil end -local split_window = function() +local split_window = function(direction) + if direction == "horizontal" then + local height = math.floor(vim.o.lines / 2) + vim.cmd("silent exe 'belowright " .. height .. "new'") + return + elseif direction == "vertical" then + local width = math.floor(vim.o.columns / 2) + vim.cmd("silent exe 'belowright " .. width .. "vnew'") + return + end + + -- Original logic when no direction override local nw = vim.o.number and vim.o.numberwidth or 0 local sw = config.rconsole_width + config.min_editor_width + 1 + nw if config.rconsole_width > 0 and vim.fn.winwidth(0) > sw then @@ -92,7 +103,7 @@ local split_window = function() end end -M.reopen_win = function() +M.reopen_win = function(direction) if not r_bufnr then return end local wlist = vim.api.nvim_list_wins() for _, wnr in ipairs(wlist) do @@ -102,18 +113,18 @@ M.reopen_win = function() end end local edbuf = vim.api.nvim_get_current_buf() - split_window() + split_window(direction) vim.api.nvim_win_set_buf(0, r_bufnr) vim.cmd.sb(edbuf) end -M.start = function() +M.start = function(direction) vim.g.R_Nvim_status = 6 local edbuf = vim.api.nvim_get_current_buf() vim.o.switchbuf = "useopen" - split_window() + split_window(direction) if config.is_windows then require("r.windows").set_R_home() end require("r.job").R_term_open(config.R_app .. " " .. require("r.run").get_r_args())