From c487da69a864cbdfe92472ded4107472f5a36c1e Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Tue, 5 Dec 2023 21:24:33 +0100 Subject: [PATCH] nvim: Add molten and image.nvim plugins Image nvim works mostly well (slow on wezterm but that will always be the case with kitty protocol for now as far as I know). Would love to be able to toggle images on/off dynamically but I don't see a way to accomplish that now. (or really, get to any option of the plugin). Molten itself also works well - the output is displayed more nicely than for the Magma plugins and everything continues working mostly well (or rather, just as wonky as I had it set up on my older magma install :) For now, the molten - image.nvim integration seems to not work at all - it simply errors out when it would produce an image as output. No clue why and it also complains about the wrong image provider (which I have taken from the molten readme). No time to bugfix now but maybe at some point. To do - find a much better way of installing the image.nvim required luarock magick - done manually with hardcoded path in setup now Also extended the old `py` alias to a full-blown script which will in addition to detecting the python repl also find any running molten session for the current directory (i.e. any running jupy kernel) and let the user choose the right one if there is multiple. Will then default to starting a kernel-aware repl environment (euporia or jupyter-console). Added a very simple `-c` option which lets you choose python command to run manually. --- nvim/.config/nvim/after/ftplugin/quarto.lua | 32 +++++---- nvim/.config/nvim/lazy-lock.json | 3 +- .../nvim/lua/plugins/data_analysis.lua | 67 ++++++++++++++----- scripts/.local/bin/py | 43 ++++++++++++ sh/.config/sh/alias | 9 --- 5 files changed, 114 insertions(+), 40 deletions(-) create mode 100755 scripts/.local/bin/py diff --git a/nvim/.config/nvim/after/ftplugin/quarto.lua b/nvim/.config/nvim/after/ftplugin/quarto.lua index f6cca8e..699abc6 100644 --- a/nvim/.config/nvim/after/ftplugin/quarto.lua +++ b/nvim/.config/nvim/after/ftplugin/quarto.lua @@ -11,10 +11,14 @@ local startsession = function(file, args) if args then file = args[0] end + local once = false vim.fn.jobstart({ "jupyter", "console", "-f", file }, { on_stdout = function(_) - vim.cmd("MagmaInit " .. file) - vim.cmd("JupyterAttach " .. file) + if not once then + vim.cmd("MoltenInit " .. file) + -- vim.cmd("JupyterAttach " .. file) + end + once = true end, on_exit = function(_) vim.notify(string.format("jupyter kernel stopped: %s", file), vim.log.levels.INFO) @@ -30,28 +34,28 @@ local map = vim.keymap.set -- filetype mappings -- PLUGIN: magma-nvim -- Operate jupyter notebooks from within vim -map("n", "cc", ":MagmaEvaluateLine", { silent = true }) +map("n", "cc", ":MoltenEvaluateLine", { silent = true }) map( "n", "C", - "?^```{jV/```k:MagmaEvaluateVisual", + "?^```{jV/```k:MoltenEvaluateVisual", { silent = true, desc = "Evaluate current code cell" } ) -map("x", "c", ":MagmaEvaluateVisual", { silent = true }) +map("x", "c", ":MoltenEvaluateVisual", { silent = true }) map( "n", "c", - "nvim_exec('MagmaEvaluateOperator', v:true)", + "nvim_exec('MoltenEvaluateOperator', v:true)", { expr = true, silent = true, desc = "+code-evaluation" } ) -map("n", "cr", ":MagmaReevaluateCell", { silent = true }) -map("n", "cu", ":MagmaShowOutput", { silent = true }) -map("n", "cU", ":noautocmd :MagmaEnterOutput", { silent = true, desc = "MagmaEnterOutput" }) -map("n", "cd", ":MagmaDelete", { silent = true }) -map("n", "cs", ":MagmaInterrupt") -map("n", "ci", ":MagmaInit ") -map("n", "cD", ":MagmaDeinit") -map("n", "cR", ":MagmaRestart") +map("n", "cr", ":MoltenReevaluateCell", { silent = true }) +map("n", "cu", ":MoltenShowOutput", { silent = true }) +map("n", "cU", ":noautocmd :MoltenEnterOutput", { silent = true, desc = "Molten enter output" }) +map("n", "cd", ":MoltenDelete", { silent = true }) +map("n", "cs", ":MoltenInterrupt") +map("n", "ci", ":MoltenInit ") +map("n", "cD", ":MoltenDeinit") +map("n", "cR", ":MoltenRestart") -- jump to beginning of previous/ next cell code map("n", "]c", "/^```{}:nohl", { desc = "Next quarto cell" }) diff --git a/nvim/.config/nvim/lazy-lock.json b/nvim/.config/nvim/lazy-lock.json index c57c953..c735918 100644 --- a/nvim/.config/nvim/lazy-lock.json +++ b/nvim/.config/nvim/lazy-lock.json @@ -31,12 +31,12 @@ "git-conflict.nvim": { "branch": "main", "commit": "4e0191c9a0ae05d7fbdcdc7f15cd358f56d23bfb" }, "gitsigns.nvim": { "branch": "main", "commit": "6ef8c54fb526bf3a0bc4efb0b2fe8e6d9a7daed2" }, "headlines.nvim": { "branch": "master", "commit": "e3d7bfdf40e41a020d966d35f8b48d75b90367d2" }, + "image.nvim": { "branch": "master", "commit": "c40215d7d7d1d8c823ee9a77be1a894d5c8df41b" }, "jupyter-kernel.nvim": { "branch": "main", "commit": "5772fa8932f2c73736a777082656f1bfe0287076" }, "lazy.nvim": { "branch": "main", "commit": "96584866b9c5e998cbae300594d0ccfd0c464627" }, "lightspeed.nvim": { "branch": "main", "commit": "fcc72d8a4d5f4ebba62d8a3a0660f88f1b5c3b05" }, "lsp-setup.nvim": { "branch": "main", "commit": "7276e54faf4af909b6ea171975fd4179ebe65cec" }, "lualine.nvim": { "branch": "master", "commit": "2248ef254d0a1488a72041cfb45ca9caada6d994" }, - "magma-nvim-goose": { "branch": "main", "commit": "9a626aab63361d027541d023707f82e28d7f872c" }, "markdown-preview.nvim": { "branch": "master", "commit": "9becceee5740b7db6914da87358a183ad11b2049" }, "markmap.nvim": { "branch": "main", "commit": "3befc2a54c2448a16c30c1c7762aab263f22946a" }, "mason-lspconfig.nvim": { "branch": "main", "commit": "4eb8e15e3c0757303d4c6dea64d2981fc679e990" }, @@ -44,6 +44,7 @@ "mason.nvim": { "branch": "main", "commit": "41e75af1f578e55ba050c863587cffde3556ffa6" }, "mdeval.nvim": { "branch": "master", "commit": "2654caf8ecaad702b50199d18e39cff23d81e0ba" }, "mini.nvim": { "branch": "main", "commit": "b5645ac6eefce8e7af9d7dd4e5e296a81cba8a10" }, + "molten-nvim": { "branch": "main", "commit": "93f2f168e77cbd82a881e07030cdbd45a48ed070" }, "neotest": { "branch": "master", "commit": "009328955066ae6c170d24bb0de5f168d8760ff8" }, "neotest-python": { "branch": "master", "commit": "81d2265efac717bb567bc15cc652ae10801286b3" }, "null-ls.nvim": { "branch": "main", "commit": "0010ea927ab7c09ef0ce9bf28c2b573fc302f5a7" }, diff --git a/nvim/.config/nvim/lua/plugins/data_analysis.lua b/nvim/.config/nvim/lua/plugins/data_analysis.lua index 0aac996..40816b7 100644 --- a/nvim/.config/nvim/lua/plugins/data_analysis.lua +++ b/nvim/.config/nvim/lua/plugins/data_analysis.lua @@ -39,24 +39,59 @@ return { -- REPL work { - "WhiteBlackGoose/magma-nvim-goose", + "3rd/image.nvim", + config = function() + -- Example for configuring Neovim to load user-installed installed Lua rocks: + package.path = package.path .. ";" .. vim.fn.expand("$HOME") .. "/.luarocks/share/lua/5.1/?/init.lua;" + package.path = package.path .. ";" .. vim.fn.expand("$HOME") .. "/.luarocks/share/lua/5.1/?.lua;" + require("image").setup({ + backend = "kitty", + integrations = { + markdown = { + enabled = true, + download_remote_images = true, + only_render_image_at_cursor = false, + filetypes = { "markdown", "vimwiki", "quarto" }, + }, + }, + }) + end, + lazy = false, + }, + { + "benlubas/molten-nvim", build = ":UpdateRemotePlugins", config = function() - vim.g.magma_image_provider = "kitty" - vim.g.magma_automatically_open_output = false + vim.g.molten_image_provider = "image_nvim" + vim.g.molten_auto_open_output = false end, cmd = { - "MagmaInit", - "MagmaEvaluateOperator", - "MagmaEvaluateLine", - "MagmaEvaluateVisual", - "MagmaRestart", + "MoltenInfo", + "MoltenInit", + "MoltenDeinit", + "MoltenGoto", + "MoltenNext", + "MoltenPrev", + "MoltenEvaluateLine", + "MoltenEvaluateVisual", + "MoltenEvaluateOperator", + "MoltenEvaluateArgument", + "MoltenReevaluateCell", + "MoltenDelete", + "MoltenShowOutput", + "MoltenHideOutput", + "MoltenEnterOutput", + "MoltenInterrupt", + "MoltenRestart", + "MoltenSave", + "MoltenLoad", + "MoltenExportOutput", }, ft = { "quarto", "python" }, lazy = false, }, - -- MARKDOWN ONLY + -- MARKDOWN ONLY -- Evaluate markdown code blocks { "jubnzv/mdeval.nvim", @@ -71,11 +106,11 @@ return { lazy = false, }, { - "AckslD/nvim-FeMaco.lua", - cmd = { - "FeMaco" - }, - ft = { "markdown" }, - config = true - } + "AckslD/nvim-FeMaco.lua", + cmd = { + "FeMaco", + }, + ft = { "markdown" }, + config = true, + }, } diff --git a/scripts/.local/bin/py b/scripts/.local/bin/py new file mode 100755 index 0000000..5512fdb --- /dev/null +++ b/scripts/.local/bin/py @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +molten_session_dir="/run/user/$UID/molten-sessions" +cur_dir=$(pwd) +search_dir="${molten_session_dir}${cur_dir}" + +if [ "$1" == "-c" ]; then + cmd=$(echo -e "euporie-console\njupyter-console\nptipython\nipython\npython" | fzf) +fi + +# set normal command first +if [ -z "$cmd" ]; then + if exist ptipython; then + cmd=ptipython + elif exist ipython; then + cmd=ipython + elif exist python; then + cmd=python + else + echo "No python found on system, please install." 1>&2 + exit 1 + fi +fi + +# search for molten session for current dir +if [ -n "$(ls -A "${search_dir}" 2>/dev/null)" ]; then + res=$(find "$search_dir" -maxdepth 1 -type f -name "*.json") + if [ "$(echo "$res" | wc -l)" -gt 1 ]; then + session=$(echo "$res" | fzf --prompt "Molten session> ") + fi +fi + +# attach to kernel +if [ -n "${session}" ]; then + if exist euporie-console || [ "$cmd" == "euporie-console" ]; then + euporie-console --edit-mode vi --connection-file "$session" + elif exist jupyter-console || [ "$cmd" == "jupyter-console" ]; then + jupyter-console --existing "$session" + fi +# or just run normal python +else + $cmd +fi diff --git a/sh/.config/sh/alias b/sh/.config/sh/alias index bc94a82..5b628a5 100644 --- a/sh/.config/sh/alias +++ b/sh/.config/sh/alias @@ -106,15 +106,6 @@ else done fi -# python -if exist ptipython; then - alias py=ptipython -elif exist ipython; then - alias py=ipython -elif exist python; then - alias py=python -fi - # distrobox if exist distrobox; then alias db=distrobox