diff --git a/README.md b/README.md index c198c8b..4a5762c 100644 --- a/README.md +++ b/README.md @@ -297,7 +297,7 @@ Note that at the moment the ssh adapter does not support Windows machines, and i - [open(dir)](doc/api.md#opendir) - [close()](doc/api.md#close) - [select(opts, callback)](doc/api.md#selectopts-callback) -- [save(opts)](doc/api.md#saveopts) +- [save(opts, cb)](doc/api.md#saveopts-cb) - [setup(opts)](doc/api.md#setupopts) diff --git a/doc/api.md b/doc/api.md index 3f49456..38d042d 100644 --- a/doc/api.md +++ b/doc/api.md @@ -15,7 +15,7 @@ - [open(dir)](#opendir) - [close()](#close) - [select(opts, callback)](#selectopts-callback) -- [save(opts)](#saveopts) +- [save(opts, cb)](#saveopts-cb) - [setup(opts)](#setupopts) @@ -132,15 +132,21 @@ Select the entry under the cursor | | close | `boolean` | Close the original oil buffer once selection is made | | callback | `nil\|fun(err: nil\|string)` | Called once all entries have been opened | | -## save(opts) +## save(opts, cb) -`save(opts)` \ +`save(opts, cb)` \ Save all changes -| Param | Type | Desc | | -| ----- | ------------ | -------------- | ------------------------------------------------------------------------------------------- | -| opts | `nil\|table` | | | -| | confirm | `nil\|boolean` | Show confirmation when true, never when false, respect skip_confirm_for_simple_edits if nil | +| Param | Type | Desc | | +| ----- | ---------------------------- | ------------------------------- | ------------------------------------------------------------------------------------------- | +| opts | `nil\|table` | | | +| | confirm | `nil\|boolean` | Show confirmation when true, never when false, respect skip_confirm_for_simple_edits if nil | +| cb | `nil\|fun(err: nil\|string)` | Called when mutations complete. | | + +**Note:** +
+If you provide your own callback function, there will be no notification for errors.
+
## setup(opts) diff --git a/doc/oil.txt b/doc/oil.txt index 2da5bb2..e8c3dbe 100644 --- a/doc/oil.txt +++ b/doc/oil.txt @@ -238,13 +238,17 @@ select({opts}, {callback}) *oil.selec {callback} `nil|fun(err: nil|string)` Called once all entries have been opened -save({opts}) *oil.save* +save({opts}, {cb}) *oil.save* Save all changes Parameters: {opts} `nil|table` {confirm} `nil|boolean` Show confirmation when true, never when false, respect skip_confirm_for_simple_edits if nil + {cb} `nil|fun(err: nil|string)` Called when mutations complete. + + Note: + If you provide your own callback function, there will be no notification for errors. setup({opts}) *oil.setup* Initialize oil diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 82d8e53..3181191 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -749,10 +749,20 @@ end ---Save all changes ---@param opts nil|table --- confirm nil|boolean Show confirmation when true, never when false, respect skip_confirm_for_simple_edits if nil -M.save = function(opts) +---@param cb? fun(err: nil|string) Called when mutations complete. +---@note +--- If you provide your own callback function, there will be no notification for errors. +M.save = function(opts, cb) opts = opts or {} + if not cb then + cb = function(err) + if err and err ~= "Canceled" then + vim.notify(err, vim.log.levels.ERROR) + end + end + end local mutator = require("oil.mutator") - mutator.try_write_changes(opts.confirm) + mutator.try_write_changes(opts.confirm, cb) end local function restore_alt_buf() @@ -944,10 +954,28 @@ M.setup = function(opts) pattern = scheme_pattern, nested = true, callback = function(params) + local winid = vim.api.nvim_get_current_win() + local last_cmd = vim.fn.histget("cmd", -1) + local last_expr = vim.fn.histget("expr", -1) + -- If the user issued a :wq or similar, we should quit after saving + local quit_after_save = last_cmd == "wq" or last_cmd == "x" or last_expr == "ZZ" + local quit_all = last_cmd:match("^wqal*$") local bufname = vim.api.nvim_buf_get_name(params.buf) if vim.endswith(bufname, "/") then vim.cmd.doautocmd({ args = { "BufWritePre", params.file }, mods = { silent = true } }) - M.save() + M.save(nil, function(err) + if err then + if err ~= "Canceled" then + vim.notify(err, vim.log.levels.ERROR) + end + elseif winid == vim.api.nvim_get_current_win() then + if quit_after_save then + vim.cmd.quit() + elseif quit_all then + vim.cmd.quitall() + end + end + end) vim.cmd.doautocmd({ args = { "BufWritePost", params.file }, mods = { silent = true } }) else local adapter = config.get_adapter_by_scheme(bufname) diff --git a/lua/oil/mutator/init.lua b/lua/oil/mutator/init.lua index 29d04a6..eb9aeef 100644 --- a/lua/oil/mutator/init.lua +++ b/lua/oil/mutator/init.lua @@ -456,9 +456,13 @@ end local mutation_in_progress = false ---@param confirm nil|boolean -M.try_write_changes = function(confirm) +---@param cb? fun(err: nil|string) +M.try_write_changes = function(confirm, cb) + if not cb then + cb = function(_err) end + end if mutation_in_progress then - error("Cannot perform mutation when already in progress") + cb("Cannot perform mutation when already in progress") return end local current_buf = vim.api.nvim_get_current_buf() @@ -495,7 +499,6 @@ M.try_write_changes = function(confirm) local ns = vim.api.nvim_create_namespace("Oil") vim.diagnostic.reset(ns) if not vim.tbl_isempty(all_errors) then - vim.notify("Error parsing oil buffers", vim.log.levels.ERROR) for bufnr, errors in pairs(all_errors) do vim.diagnostic.set(ns, bufnr, errors) end @@ -514,13 +517,17 @@ M.try_write_changes = function(confirm) vim.api.nvim_win_set_buf(0, bufnr) pcall(vim.api.nvim_win_set_cursor, 0, { errs[1].lnum + 1, errs[1].col }) end - return unlock() + unlock() + cb("Error parsing oil buffers") + return end local actions = M.create_actions_from_diffs(all_diffs) preview.show(actions, confirm, function(proceed) if not proceed then - return unlock() + unlock() + cb("Canceled") + return end M.process_actions( @@ -528,7 +535,7 @@ M.try_write_changes = function(confirm) vim.schedule_wrap(function(err) view.unlock_buffers() if err then - vim.notify(string.format("[oil] Error applying actions: %s", err), vim.log.levels.ERROR) + err = string.format("[oil] Error applying actions: %s", err) view.rerender_all_oil_buffers() else local current_entry = oil.get_cursor_entry() @@ -543,6 +550,7 @@ M.try_write_changes = function(confirm) vim.api.nvim_exec_autocmds("User", { pattern = "OilMutationComplete", modeline = false }) end mutation_in_progress = false + cb(err) end) ) end) diff --git a/lua/oil/view.lua b/lua/oil/view.lua index 4a2a7d0..8fd423e 100644 --- a/lua/oil/view.lua +++ b/lua/oil/view.lua @@ -684,6 +684,7 @@ M.render_buffer_async = function(bufnr, opts, callback) local seek_after_render_found = false local first = true vim.bo[bufnr].modifiable = false + vim.bo[bufnr].modified = false loading.set_loading(bufnr, true) local finish = vim.schedule_wrap(function()