From f0011b9eb1c71f9eeeecf4fbef93aeb600e29bf0 Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Thu, 1 Aug 2024 22:45:04 +0200 Subject: [PATCH] nvim: execute current buffer as lua script by :write and :luafile --- config/nvim/lua/fschauen/util/lua.lua | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/config/nvim/lua/fschauen/util/lua.lua b/config/nvim/lua/fschauen/util/lua.lua index 7edfcc2..6c3e192 100644 --- a/config/nvim/lua/fschauen/util/lua.lua +++ b/config/nvim/lua/fschauen/util/lua.lua @@ -40,12 +40,15 @@ M.execute_selection = function() M.execute_lines(unpack(selection)) end +---Execute lua script in a file. +---@param path string|nil Path to the file to execute. If nil, write and source +--- current buffer. M.execute_file = function(path) - if path then - vim.cmd.luafile(path) - else - M.execute_lines(1, vim.fn.line("$")) + if not path then + path = vim.fn.expand("%") + vim.cmd.write() end + vim.cmd.luafile(path) end ---Edit a lua module.