diff --git a/lua/hacker-helper.lua b/lua/hacker-helper.lua index 249e0a5..2e6a790 100644 --- a/lua/hacker-helper.lua +++ b/lua/hacker-helper.lua @@ -24,4 +24,10 @@ M.hello = function() return module.my_first_function(M.config.opt) end +-- Key mappings for executing in terminal +vim.api.nvim_set_keymap('n', 'e', ':lua require("hacker-helper.module").exec_line_or_selection_in_term()', + { noremap = true, silent = true }) +vim.api.nvim_set_keymap('v', 'e', ':lua require("hacker-helper.module").exec_line_or_selection_in_term()', + { noremap = true, silent = true }) + return M diff --git a/lua/hacker-helper/module.lua b/lua/hacker-helper/module.lua index 78a9962..f29c881 100644 --- a/lua/hacker-helper/module.lua +++ b/lua/hacker-helper/module.lua @@ -5,5 +5,73 @@ local M = {} M.my_first_function = function(greeting) return greeting end +-- Initialize the toggleterm plugin if not already done +-- require('toggleterm').setup { +-- direction = 'vertical', +-- size = 50, +-- } + +-- Function to execute the current line or visual selection in an existing terminal +function M.exec_line_or_selection_in_term() + -- Initialize the toggleterm plugin if not already done + local ok, toggleterm = pcall(require, 'toggleterm') + if ok then + toggleterm.setup { + direction = 'vertical', + size = 50, + } + else + print("toggleterm is not installed or cannot be loaded.") + return + end + + local mode = vim.api.nvim_get_mode().mode + local lines = {} + + if mode == 'v' then + -- Get the visually selected lines + local start_pos = vim.fn.getpos("'<") + local end_pos = vim.fn.getpos("'>") + lines = vim.fn.getline(start_pos[2], end_pos[2]) + else + -- Get the current line + table.insert(lines, vim.fn.getline('.')) + end + + -- Find the existing terminal buffer + local term_bufnr = nil + local buffers = vim.api.nvim_list_bufs() + + for _, buf in ipairs(buffers) do + if vim.api.nvim_buf_get_option(buf, 'buftype') == 'terminal' then + term_bufnr = buf + break + end + end + + if term_bufnr then + -- Switch to the terminal buffer + vim.api.nvim_set_current_buf(term_bufnr) + + -- Safely check if the terminal job ID exists + local term_job_id = vim.b.terminal_job_id + if not term_job_id then + print("No terminal job ID found.") + return + end + + -- Send the command to the terminal + for _, line in ipairs(lines) do + vim.api.nvim_chan_send(term_job_id, line .. "\n") + end + + -- Switch back to the previous buffer + vim.cmd("b#") + else + print("No terminal buffer found.") + end +end + +-- End of Function return M diff --git a/plugin/hacker-helper.lua b/plugin/hacker-helper.lua index 2e3cd2e..0fd747e 100644 --- a/plugin/hacker-helper.lua +++ b/plugin/hacker-helper.lua @@ -1 +1,6 @@ vim.api.nvim_create_user_command("HackerHelper", require("hacker-helper").hello, {}) +-- Key mappings for executing in terminal +vim.api.nvim_set_keymap('n', 'e', ':lua require("hacker-helper.module").exec_line_or_selection_in_term()', + { noremap = true, silent = true }) +vim.api.nvim_set_keymap('v', 'e', ':lua require("hacker-helper.module").exec_line_or_selection_in_term()', + { noremap = true, silent = true })