diff --git a/README.md b/README.md index 1068b09..95f0f59 100644 --- a/README.md +++ b/README.md @@ -158,8 +158,9 @@ Any options that are not specified when calling `setup` will take on their defau }, -- The repl plugin with which to interface -- Current options: "iron" for iron.nvim, "toggleterm" for toggleterm.nvim, - -- "molten" for molten-nvim or "auto" which checks which of the above are + -- "molten" for molten-nvim or "auto" which checks which of the above are -- installed + -- (start_line, end_line, repl_args, cell_marker) -> boolean (success) repl_provider = "auto", -- Syntax based highlighting. If you don't want to install mini.hipattners or -- enjoy a more minimalistic look @@ -210,6 +211,8 @@ keymaps on the plugin configuration or even map them on they Hydra mode as long as you take heed of the [advice given above](#current-limitations). - `move_cell(dir)`: Move up or done a cell in the `u`p or `d`own direction. + - `D` to move to the first non-empty line of the next cell, likewise `U` + - `e` to move to the last non-empty line of the next cell, `E` for previous cell - `run_cell(repl_args)`: Run the current cell. You may optionally pass a table of `repl_args` that will be forwarded to the REPL. For the details of what is forwarded exactly and how it is used check `repls.lua` and look for your REPL diff --git a/afile.py b/afile.py new file mode 100644 index 0000000..4886f86 --- /dev/null +++ b/afile.py @@ -0,0 +1,5 @@ +# %% +import numpy as np + +# %% +print("allo") diff --git a/doc/NotebookNavigator.txt b/doc/NotebookNavigator.txt index be37209..043a2de 100644 --- a/doc/NotebookNavigator.txt +++ b/doc/NotebookNavigator.txt @@ -184,6 +184,8 @@ Default values: -- Current options: "iron" for iron.nvim, "toggleterm" for toggleterm.nvim, -- "molten" for molten-nvim or "auto" which checks which of the above are -- installed + -- or provide a function with signature + -- (start_line, end_line, repl_args, cell_marker) -> boolean (success) repl_provider = "auto", -- Syntax based highlighting. If you don't want to install mini.hipattners or -- enjoy a more minimalistic look diff --git a/lua/notebook-navigator/core.lua b/lua/notebook-navigator/core.lua index c563c35..65e9925 100644 --- a/lua/notebook-navigator/core.lua +++ b/lua/notebook-navigator/core.lua @@ -7,17 +7,26 @@ local M = {} M.move_cell = function(dir, cell_marker) local search_res local result + local mod = dir:sub(2, 2) - if dir == "d" then + if dir == "d" or dir == "D" or dir == "e" then search_res = vim.fn.search("^" .. cell_marker, "W") if search_res == 0 then result = "last" + elseif dir == "D" then + vim.fn.search("^\\w", "W") + elseif dir == "e" then + vim.fn.search("^\\w", "bW") end else search_res = vim.fn.search("^" .. cell_marker, "bW") if search_res == 0 then result = "first" vim.api.nvim_win_set_cursor(0, { 1, 0 }) + elseif dir == "U" then + vim.fn.search("^\\w", "W") + elseif dir == "E" then + vim.fn.search("^\\w", "bW") end end @@ -125,7 +134,7 @@ M.run_all_cells = function(repl_provider, repl_args) local buf_length = vim.api.nvim_buf_line_count(0) local repl = get_repl(repl_provider) - repl(1, buf_length, repl_args) + return repl(1, buf_length, repl_args) end M.run_cells_below = function(cell_marker, repl_provider, repl_args) @@ -133,7 +142,7 @@ M.run_cells_below = function(cell_marker, repl_provider, repl_args) local cell_object = miniai_spec("i", cell_marker) local repl = get_repl(repl_provider) - repl(cell_object.from.line, buf_length, repl_args) + return repl(cell_object.from.line, buf_length, repl_args) end M.merge_cell = function(dir, cell_marker) diff --git a/lua/notebook-navigator/init.lua b/lua/notebook-navigator/init.lua index c1c02de..1f8637e 100644 --- a/lua/notebook-navigator/init.lua +++ b/lua/notebook-navigator/init.lua @@ -221,13 +221,13 @@ local function activate_hydra(config) local hydra_config = { name = "NotebookNavigator", mode = { "n" }, - config = { + config = vim.tbl_extend("force", { invoke_on_body = true, color = "pink", hint = { float_opts = { border = "rounded" } }, - }, - body = config.activate_hydra_keys, + }, config.hydra_config or {}), heads = active_hydra_heads, + body = config.activate_hydra_keys, } if config.show_hydra_hint then hydra_config.hint = hydra_hint @@ -251,6 +251,8 @@ M.config = { -- If not `nil` the keymap defined in the string will activate the hydra head activate_hydra_keys = nil, + -- Optional `config` table for the Hydra head + hydra_config = nil, -- If `true` a hint panel will be shown when the hydra head is active show_hydra_hint = true, -- Mappings while the hydra head is active. @@ -267,6 +269,8 @@ M.config = { -- The repl plugin with which to interface -- Current options: "iron" for iron.nvim, "toggleterm" for toggleterm.nvim, -- or "auto" which checks which of the above are installed + -- installed + -- (start_line, end_line, repl_args, cell_marker) -> boolean (success) repl_provider = "auto", -- Syntax based highlighting. If you don't want to install mini.hipattners or -- enjoy a more minimalistic look @@ -288,21 +292,22 @@ M.setup = function(config) vim.validate("config", config, "table", true) M.config = vim.tbl_deep_extend("force", M.config, config or {}) - vim.validate("cell_markers", M.config.cell_markers, "table") - vim.validate("activate_hydra_keys", M.config.activate_hydra_keys, "string", true) - vim.validate("show_hydra_hint", M.config.show_hydra_hint, "boolean") - vim.validate("hydra_keys", M.config.hydra_keys, "table") + vim.validate("cell_markers", M.config.cell_markers, "table") + vim.validate("activate_hydra_keys", M.config.activate_hydra_keys, "string", true) + vim.validate("hydra_config", M.config.hydra_config, { "table" }, true) + vim.validate("show_hydra_hint", M.config.show_hydra_hint, "boolean") + vim.validate("hydra_keys", M.config.hydra_keys, "table") vim.validate("config.hydra_keys.comment", M.config.hydra_keys.comment, "string") vim.validate("config.hydra_keys.run", M.config.hydra_keys.run, "string") vim.validate("config.hydra_keys.run_and_move", M.config.hydra_keys.run_and_move, "string") vim.validate("config.hydra_keys.move_up", M.config.hydra_keys.move_up, "string") - vim.validate("config.hydra_keys.move_down", M.config.hydra_keys.move_down, "string") - vim.validate("config.hydra_keys.add_cell_before", M.config.hydra_keys.add_cell_before, "string") - vim.validate("config.hydra_keys.add_cell_after", M.config.hydra_keys.add_cell_after, "string") + vim.validate("config.hydra_keys.move_down", M.config.hydra_keys.move_down, "string") + vim.validate("config.hydra_keys.add_cell_before", M.config.hydra_keys.add_cell_before, "string") + vim.validate("config.hydra_keys.add_cell_after", M.config.hydra_keys.add_cell_after, "string") for ft, marker in pairs(M.config.cell_markers) do - vim.validate("config.cell_markers." .. ft, marker, "string") + vim.validate("config.cell_markers." .. ft, marker, "string") end if (not got_hydra) and (M.config.activate_hydra_keys ~= nil) then @@ -310,7 +315,9 @@ M.setup = function(config) end if - M.config.repl_provider ~= "auto" and not utils.has_value(utils.available_repls, M.config.repl_provider) + M.config.repl_provider ~= "auto" + and type(M.config.repl_provider) == "string" + and not utils.has_value(utils.available_repls, M.config.repl_provider) then vim.notify("[NotebookNavigator] The requested repl (" .. M.config.repl_provider .. ") is not available.") end diff --git a/lua/notebook-navigator/repls.lua b/lua/notebook-navigator/repls.lua index ed9d52d..4001c75 100644 --- a/lua/notebook-navigator/repls.lua +++ b/lua/notebook-navigator/repls.lua @@ -78,21 +78,24 @@ repls.no_repl = function(_) end local get_repl = function(repl_provider) local available_repls = utils.available_repls - if #available_repls == 0 then + if type(repl_provider) ~= "function" and #available_repls == 0 then vim.notify("[NotebookNavigator] No supported REPLs available.\nMost functionality will error out.") return nil end + local chosen_repl = nil if repl_provider == "auto" then for _, r in ipairs(available_repls) do chosen_repl = repls[r] break end - else + elseif type(repl_provider) == "string" then chosen_repl = repls[repl_provider] + elseif type(repl_provider) == "function" then + chosen_repl = repl_provider end - -- Check if we actuall got out a supported repl + -- Check if we actually got out a supported repl if chosen_repl == nil then vim.notify("[NotebookNavigator] The provided repl, " .. repl_provider .. ", is not supported.") chosen_repl = repls["no_repl"]