From bac5f39ece4d2eb8630e6091438a020b80ede934 Mon Sep 17 00:00:00 2001 From: srdusr Date: Thu, 2 Nov 2023 23:18:22 +0200 Subject: Run executable file --- .config/nvim/lua/user/mods.lua | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to '.config/nvim') diff --git a/.config/nvim/lua/user/mods.lua b/.config/nvim/lua/user/mods.lua index 7c2db16..883c1c7 100644 --- a/.config/nvim/lua/user/mods.lua +++ b/.config/nvim/lua/user/mods.lua @@ -970,5 +970,53 @@ end -------------------------------------------------- +-- Run executable file +local interpreters = { + python = 'python', + lua = 'lua', + bash = 'bash', + zsh = 'zsh', + perl = 'perl', + ruby = 'ruby', + node = 'node', + rust = 'rust', + php = 'php', +} + +function M.RunCurrentFile() + local file_path = vim.fn.expand('%:p') + local file = io.open(file_path, 'r') + + if not file then + print('Error: Unable to open the file') + return + end + + local shebang = file:read() + file:close() + + local interpreter = shebang:match('#!%s*(.-)$') + if not interpreter then + print('Error: No shebang line found in the file') + return + end + + -- Remove leading spaces and any arguments, extracting the interpreter name + interpreter = interpreter:gsub('^%s*([^%s]+).*', '%1') + + local cmd = interpreters[interpreter] + + if not cmd then + cmd = interpreter -- Set the command to the interpreter directly + end + + -- Run the file using the determined interpreter + vim.fn.jobstart(cmd .. ' ' .. file_path, { + cwd = vim.fn.expand('%:p:h'), + }) +end + +-------------------------------------------------- + -- ... return M -- cgit v1.2.3