-- setup iron nvim local iron = require 'iron.core' local view = require 'iron.view' local common = require 'iron.fts.common' iron.setup { config = { -- Whether a repl should be discarded or not scratch_repl = true, -- Your repl definitions come here repl_definition = { sh = { -- Can be a table or a function that -- returns a table (see below) command = { 'zsh' }, }, python = { command = { 'python3' }, -- or { "ipython", "--no-autoindent" } format = common.bracketed_paste_python, block_dividers = { '# %%', '#%%' }, env = { PYTHON_BASIC_REPL = '1' }, --this is needed for python3.13 and up. }, }, -- set the file type of the newly created repl to ft -- bufnr is the buffer id of the REPL and ft is the filetype of the -- language being used for the REPL. repl_filetype = function(bufnr, ft) return ft -- or return a string name such as the following -- return "iron" end, -- Send selections to the DAP repl if an nvim-dap session is running. dap_integration = true, -- How the repl window will be displayed -- See below for more information repl_open_cmd = view.bottom(40), -- repl_open_cmd can also be an array-style table so that multiple -- repl_open_commands can be given. -- When repl_open_cmd is given as a table, the first command given will -- be the command that `IronRepl` initially toggles. -- Moreover, when repl_open_cmd is a table, each key will automatically -- be available as a keymap (see `keymaps` below) with the names -- toggle_repl_with_cmd_1, ..., toggle_repl_with_cmd_k -- For example, -- -- repl_open_cmd = { -- view.split.vertical.rightbelow("%40"), -- cmd_1: open a repl to the right -- view.split.rightbelow("%25") -- cmd_2: open a repl below -- } }, -- Iron doesn't set keymaps by default anymore. -- You can set them here or manually add keymaps to the functions in iron.core keymaps = { toggle_repl = 'rr', -- toggles the repl open and closed. -- If repl_open_command is a table as above, then the following keymaps are -- available -- toggle_repl_with_cmd_1 = "rv", -- toggle_repl_with_cmd_2 = "rh", restart_repl = 'jr', -- calls `IronRestart` to restart the repl send_motion = 'sc', visual_send = 'sc', send_file = 'ja', send_line = 'sl', send_paragraph = 'sp', send_until_cursor = 'su', send_mark = 'sm', send_code_block = 'sb', send_code_block_and_move = 'sn', mark_motion = 'mc', mark_visual = 'mc', remove_mark = 'md', cr = 's', interrupt = 's', exit = 'jq', clear = 'jl', }, -- If the highlight is on, you can change how it looks -- For the available options, check nvim_set_hl highlight = { italic = true, }, ignore_blank_lines = true, -- ignore blank lines when sending visual select lines } -- iron also has a list of commands, see :h iron-commands for all available commands vim.keymap.set('n', 'rf', 'IronFocus') vim.keymap.set('n', 'rh', 'IronHide') local function send_cell() local iron = require 'iron.core' local buf = vim.api.nvim_get_current_buf() local cursor = vim.api.nvim_win_get_cursor(0)[1] -- 1-based local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false) local start_line = 1 local end_line = #lines -- search upward for previous "# %%" for i = cursor, 1, -1 do if lines[i]:match '^# %%%%' then start_line = i + 1 break end end -- search downward for next "# %%" for i = cursor + 1, #lines do if lines[i]:match '^# %%%%' then end_line = i - 1 break end end if start_line > end_line then return end -- collect lines of the cell local cell_lines = vim.api.nvim_buf_get_lines(buf, start_line - 1, end_line, false) -- send to REPL iron.send(nil, cell_lines) end -- keymaps vim.keymap.set('n', 'jm', 'i# %%', { noremap = true, silent = false, desc = '[J]upyter Create [M]ark', }) vim.keymap.set('n', 'ji', ':vsplit:IronReplHere', { noremap = true, silent = false, desc = '[J]upyter [I]nit', }) vim.keymap.set('v', 'jv', ":lua require('iron.core').visual_send()", { noremap = true, silent = false, desc = '[J]upyter [V]isual Send', }) vim.keymap.set('n', 'jj', send_cell, { desc = 'Iron: send current # %% cell' }) return {}