From 97bfcd068536fde09b206f58e6569b9df50b0087 Mon Sep 17 00:00:00 2001 From: Jannik Buhr Date: Mon, 12 Feb 2024 11:33:22 +0100 Subject: [PATCH] feat: automatically open and close reticulate python repl in R terminal when R terminal was opened --- lua/config/keymap.lua | 36 +++++++++++++++++++++++++++++++----- lua/plugins/quarto.lua | 6 ++++-- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/lua/config/keymap.lua b/lua/config/keymap.lua index 05c32f4..acd660e 100644 --- a/lua/config/keymap.lua +++ b/lua/config/keymap.lua @@ -49,15 +49,37 @@ imap(";", ";u") nmap("Q", "") +local M = {} +M.reticulate_running = false +local function send_cell() + if vim.b["quarto_is_" .. "r" .. "_mode"] == nil then + vim.cmd [[call slime#send_cell()]] + return + end + if vim.b["quarto_is_" .. "r" .. "_mode"] == true then + vim.g.slime_python_ipython = 0 + local is_python = require("otter.tools.functions").is_otter_language_context("python") + if is_python and not M.reticulate_running then + vim.cmd[[call slime#send("reticulate::repl_python()" . "\r")]] + M.reticulate_running = true + end + if not is_python and M.reticulate_running then + vim.cmd[[call slime#send("exit" . "\r")]] + M.reticulate_running = false + end + vim.cmd [[call slime#send_cell()]] + end +end + -- send code with ctrl+Enter -- just like in e.g. RStudio -- needs kitty (or other terminal) config: -- map shift+enter send_text all \x1b[13;2u -- map ctrl+enter send_text all \x1b[13;5u -nmap("", "SlimeSendCell") -nmap("", "SlimeSendCell") -imap("", "SlimeSendCelli") -imap("", "SlimeSendCelli") +nmap("", send_cell) +nmap("", send_cell) +imap("", send_cell) +imap("", send_cell) -- send code with Enter and leader Enter vmap("", "SlimeRegionSend") @@ -102,6 +124,7 @@ local function toggle_light_dark_theme() end end +vim.b["quarto_is_" .. "r" .. "_mode"] = nil --show kepbindings with whichkey --add your own here if you want them to --show up in the popup as well @@ -113,7 +136,10 @@ wk.register({ name = "code", c = { ":SlimeConfig", "slime config" }, n = { ":vsplit term://$SHELL", "new terminal" }, - r = { ":vsplit term://R", "new R terminal" }, + r = { function() + vim.b["quarto_is_" .. "r" .. "_mode"] = true + vim.cmd "vsplit term://R" + end, "new R terminal" }, p = { ":vsplit term://python", "new python terminal" }, i = { ":vsplit term://ipython", "new ipython terminal" }, j = { ":vsplit term://julia", "new julia terminal" }, diff --git a/lua/plugins/quarto.lua b/lua/plugins/quarto.lua index 85e5ec2..ef9dadc 100644 --- a/lua/plugins/quarto.lua +++ b/lua/plugins/quarto.lua @@ -69,7 +69,9 @@ return { "html", "css", "dot", - "javascript" + "javascript", + "mermaid", + "norg", }, highlight = { enable = true, @@ -682,7 +684,7 @@ return { let g:slime_dispatch_ipython_pause = 100 function SlimeOverride_EscapeText_quarto(text) call v:lua.Quarto_is_in_python_chunk() - if exists('g:slime_python_ipython') && len(split(a:text,"\n")) > 1 && b:quarto_is_python_chunk + if exists('g:slime_python_ipython') && len(split(a:text,"\n")) > 1 && b:quarto_is_python_chunk && !b:quarto_is_r_mode return ["%cpaste -q\n", g:slime_dispatch_ipython_pause, a:text, "--", "\n"] else return a:text