From 9d15bc79f499b156584b0f970370dafa38191dbc Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Thu, 6 Feb 2025 12:49:10 +0100 Subject: [PATCH] nvim: Clean up markdown/quarto key maps --- nvim/.config/nvim/after/ftplugin/markdown.lua | 14 +++++----- nvim/.config/nvim/after/ftplugin/quarto.lua | 17 +++--------- .../nvim/lua/plugins/data_analysis.lua | 26 +++++++++---------- 3 files changed, 21 insertions(+), 36 deletions(-) diff --git a/nvim/.config/nvim/after/ftplugin/markdown.lua b/nvim/.config/nvim/after/ftplugin/markdown.lua index 303dc82..50d5511 100644 --- a/nvim/.config/nvim/after/ftplugin/markdown.lua +++ b/nvim/.config/nvim/after/ftplugin/markdown.lua @@ -11,22 +11,20 @@ if require("core.util").is_available("zk") and require("zk.util").notebook_root( map("n", "", "lua vim.lsp.buf.definition()", { silent = true }) end --- edit code cells with full lsp access -map("n", "ce", "FeMaco", { silent = true, desc = "edit code block" }) -- execute code cells -if not vim.fn.mapcheck("cc") then - map("n", "cc", "MdEval", { silent = true, desc = "evaluate code block" }) +if vim.fn.mapcheck("cc") == "" then + map("n", "cc", require("mdeval").eval_code_block, { silent = true, desc = "evaluate code block" }) end -if not vim.fn.mapcheck("cx") then - map("n", "cx", "MdEvalClean", { silent = true, desc = "clear code results" }) +if vim.fn.mapcheck("cx") == "" then + map("n", "cx", require("mdeval").eval_clean_results, { silent = true, desc = "clear code results" }) end -- jump to beginning of previous/ next cell code map("n", "]c", "/^```}:nohl", { desc = "next code cell" }) map("n", "[c", "?^```n}:nohl", { desc = "previous code cell" }) -- insert cell header above/below -map("n", "co", "o```python```k", { desc = "Insert quarto cell below" }) -map("n", "cO", "O```python```k", { desc = "Insert quarto cell above" }) +map("n", "co", "o```python```k", { desc = "Insert code cell below" }) +map("n", "cO", "O```python```k", { desc = "Insert code cell above" }) if require("core.util").is_available("which-key") then require("which-key").add({ "p", group = "prose" }) diff --git a/nvim/.config/nvim/after/ftplugin/quarto.lua b/nvim/.config/nvim/after/ftplugin/quarto.lua index 3251bbc..a38d353 100644 --- a/nvim/.config/nvim/after/ftplugin/quarto.lua +++ b/nvim/.config/nvim/after/ftplugin/quarto.lua @@ -2,17 +2,6 @@ if require("core.util").is_available("quarto") then vim.keymap.set("n", "po", require("quarto").quartoPreview, { desc = "show quarto preview" }) end -local default_buffer_session = function() - local buffer_path = vim.api.nvim_buf_get_name(0) or vim.fn.tempname() - local temp_path = vim.fn.stdpath("run") .. "/molten-sessions" .. buffer_path .. ".json" - - local dir = vim.fn.fnamemodify(temp_path, ":p:h") - if vim.fn.getftype(dir) ~= "dir" then - vim.fn.mkdir(dir, "p") - end - return temp_path -end - -- TODO: Puths kernel into local file dir currently, -- could take an argument to put it into temporary dir instead. local kernel_filename = function() @@ -29,6 +18,7 @@ end local startsession = function(opts) local args = opts.fargs local kernel_filen = args[1] or kernel_filename() + vim.b["sessionfile"] = kernel_filen local path = require("core.util").get_python_venv_bin() if not path then @@ -75,15 +65,14 @@ end vim.api.nvim_create_user_command("JupyterStart", function(opts) startsession(opts) end, { nargs = "?" }) +vim.keymap.set("n", "cS", ":JupyterStart", { desc = "start code session", silent = true }) if vim.g.quarto_auto_init_molten_session then vim.api.nvim_create_autocmd({ "BufEnter" }, { callback = function() if vim.b["sessionfile"] == nil then - local path = kernel_filename() - vim.b["sessionfile"] = path vim.schedule(function() - startsession({ fargs = { path } }) + startsession() end) end end, diff --git a/nvim/.config/nvim/lua/plugins/data_analysis.lua b/nvim/.config/nvim/lua/plugins/data_analysis.lua index 2b40065..0b125b5 100644 --- a/nvim/.config/nvim/lua/plugins/data_analysis.lua +++ b/nvim/.config/nvim/lua/plugins/data_analysis.lua @@ -40,10 +40,11 @@ return { map("n", "ca", require("quarto.runner").run_above, { desc = "run cells above" }) map("n", "cb", require("quarto.runner").run_below, { desc = "run cells below" }) map("n", "cA", require("quarto.runner").run_all, { desc = "run all similar cells" }) + -- TODO: overwritten by other moves, i.e. comment? map("n", "]c", "/^```{}:nohl", { desc = "Codecell forward" }) map("n", "[c", "?^```n}:nohl", { desc = "Codecell last" }) - map("n", "co", "o```{python}```k", { desc = "Insert quarto cell below" }) - map("n", "cO", "O```{python}```k", { desc = "Insert quarto cell above" }) + map("n", "co", "o```{python}```k", { desc = "Insert code cell below" }) + map("n", "cO", "O```{python}```k", { desc = "Insert code cell above" }) if require("core.util").is_available("which-key") then require("which-key").add({ "c", group = "codecells" }) @@ -191,9 +192,9 @@ return { ft = { "norg", "quarto", "python" }, keys = { { "vn", ":MoltenInfo" }, - { "cJ", ":JupyterStart", desc = "start jupyter", silent = true }, }, cmd = { + "JupyterStart", "MoltenInfo", "MoltenInit", "MoltenDeinit", @@ -224,9 +225,7 @@ return { -- Edit code blocks in md/quarto using whatever language is { "AckslD/nvim-FeMaco.lua", - cmd = { - "FeMaco", - }, + cmd = { "FeMaco" }, ft = { "markdown", "rmd", "quarto" }, opts = { ensure_newline = function(base_ft) @@ -236,20 +235,19 @@ return { return false end, }, + config = function(_, opts) + vim.keymap.set("n", "ce", ":FeMaco", { desc = "edit codecell" }) + require("femaco").setup(opts) + end, dependencies = { "nvim-treesitter/nvim-treesitter", }, - keys = { - { "ce", ":FeMaco", desc = "edit codecell" }, - }, }, -- MARKDOWN ONLY -- Evaluate markdown code blocks - { + { -- TODO: Have results appear as virtual text instead of real text? "jubnzv/mdeval.nvim", - cmd = { - "MdEval", - }, + cmd = { "MdEval" }, ft = { "markdown" }, opts = { require_confirmation = false, @@ -277,7 +275,7 @@ return { }, }, }, - cond = vim.fn.executable("jupytext") == 1, -- only runs if imagemagick installed + cond = vim.fn.executable("jupytext") == 1, -- only runs if jupytext installed lazy = false, -- does not work in lazy mode }, }