Skip to content

Commit 23c5536

Browse files
committed
feat: automatically open and close reticulate python repl in R terminal when R
terminal was opened
1 parent 1769baa commit 23c5536

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

lua/config/keymap.lua

+31-5
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,37 @@ imap(";", ";<c-g>u")
4949

5050
nmap("Q", "<Nop>")
5151

52+
local M = {}
53+
M.reticulate_running = false
54+
local function send_cell()
55+
if vim.b["quarto_is_" .. "r" .. "_mode"] == nil then
56+
vim.cmd [[call slime#send_cell()]]
57+
return
58+
end
59+
if vim.b["quarto_is_" .. "r" .. "_mode"] == true then
60+
vim.g.slime_python_ipython = 0
61+
local is_python = require("otter.tools.functions").is_otter_language_context("python")
62+
if is_python and not M.reticulate_running then
63+
vim.cmd[[call slime#send("reticulate::repl_python()" . "\r")]]
64+
M.reticulate_running = true
65+
end
66+
if not is_python and M.reticulate_running then
67+
vim.cmd[[call slime#send("exit" . "\r")]]
68+
M.reticulate_running = false
69+
end
70+
vim.cmd [[call slime#send_cell()]]
71+
end
72+
end
73+
5274
-- send code with ctrl+Enter
5375
-- just like in e.g. RStudio
5476
-- needs kitty (or other terminal) config:
5577
-- map shift+enter send_text all \x1b[13;2u
5678
-- map ctrl+enter send_text all \x1b[13;5u
57-
nmap("<c-cr>", "<Plug>SlimeSendCell")
58-
nmap("<s-cr>", "<Plug>SlimeSendCell")
59-
imap("<c-cr>", "<esc><Plug>SlimeSendCell<cr>i")
60-
imap("<s-cr>", "<esc><Plug>SlimeSendCell<cr>i")
79+
nmap("<c-cr>", send_cell)
80+
nmap("<s-cr>", send_cell)
81+
imap("<c-cr>", send_cell)
82+
imap("<s-cr>", send_cell)
6183

6284
-- send code with Enter and leader Enter
6385
vmap("<cr>", "<Plug>SlimeRegionSend")
@@ -102,6 +124,7 @@ local function toggle_light_dark_theme()
102124
end
103125
end
104126

127+
vim.b["quarto_is_" .. "r" .. "_mode"] = nil
105128
--show kepbindings with whichkey
106129
--add your own here if you want them to
107130
--show up in the popup as well
@@ -110,7 +133,10 @@ wk.register({
110133
name = "code",
111134
c = { ":SlimeConfig<cr>", "slime config" },
112135
n = { ":vsplit term://$SHELL<cr>", "new terminal" },
113-
r = { ":vsplit term://R<cr>", "new R terminal" },
136+
r = { function()
137+
vim.b["quarto_is_" .. "r" .. "_mode"] = true
138+
vim.cmd "vsplit term://R"
139+
end, "new R terminal" },
114140
p = { ":vsplit term://python<cr>", "new python terminal" },
115141
i = { ":vsplit term://ipython<cr>", "new ipython terminal" },
116142
j = { ":vsplit term://julia<cr>", "new julia terminal" },

lua/plugins/quarto.lua

+5-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ return {
5454
"latex",
5555
"html",
5656
"css",
57-
"dot"
57+
"dot",
58+
"javascript",
59+
"mermaid",
60+
"norg",
5861
},
5962
highlight = {
6063
enable = true,
@@ -644,7 +647,7 @@ return {
644647
let g:slime_dispatch_ipython_pause = 100
645648
function SlimeOverride_EscapeText_quarto(text)
646649
call v:lua.Quarto_is_in_python_chunk()
647-
if exists('g:slime_python_ipython') && len(split(a:text,"\n")) > 1 && b:quarto_is_python_chunk
650+
if exists('g:slime_python_ipython') && len(split(a:text,"\n")) > 1 && b:quarto_is_python_chunk && !b:quarto_is_r_mode
648651
return ["%cpaste -q\n", g:slime_dispatch_ipython_pause, a:text, "--", "\n"]
649652
else
650653
return a:text

0 commit comments

Comments
 (0)