aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2023-10-04 06:31:25 -0700
committerGitHub <noreply@github.com>2023-10-04 06:31:25 -0700
commit29fe883aa9166bdbcae3f935523c75a8aa56fe45 (patch)
tree40c1179d6d854acec5d044a94cc6111dc54883c7 /runtime/lua/vim
parent1e7e9ee91f73c62b8c5ba9dbdabba3a3b6dc0130 (diff)
downloadrneovim-29fe883aa9166bdbcae3f935523c75a8aa56fe45.tar.gz
rneovim-29fe883aa9166bdbcae3f935523c75a8aa56fe45.tar.bz2
rneovim-29fe883aa9166bdbcae3f935523c75a8aa56fe45.zip
feat: ignore swapfile for running Nvim processes #25336
Problem: The swapfile "E325: ATTENTION" dialog is displayed when editing a file already open in another (running) Nvim. Usually this behavior is annoying and irrelevant: - "Recover" and the other options ("Open readonly", "Quit", "Abort") are almost never wanted. - swapfiles are less relevant for "multi-Nvim" since 'autoread' is enabled by default. - Even less relevant if user enables 'autowrite'. Solution: Define a default SwapExists handler which does the following: 1. If the swapfile is owned by a running Nvim process, automatically chooses "(E)dit anyway" (caveat: this creates a new, extra swapfile, which is mostly harmless and ignored except by `:recover` or `nvim -r`. 2. Shows a 1-line "ignoring swapfile..." message. 3. Users can disable the default SwapExists handler via `autocmd! nvim_swapfile`.
Diffstat (limited to 'runtime/lua/vim')
-rw-r--r--runtime/lua/vim/_editor.lua17
-rw-r--r--runtime/lua/vim/_meta/vimfn.lua4
2 files changed, 19 insertions, 2 deletions
diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua
index 7f09fc8038..1ba7d6163d 100644
--- a/runtime/lua/vim/_editor.lua
+++ b/runtime/lua/vim/_editor.lua
@@ -1147,11 +1147,28 @@ function vim._init_default_autocmds()
end
end,
})
+
vim.api.nvim_create_autocmd({ 'CmdwinEnter' }, {
pattern = '[:>]',
group = vim.api.nvim_create_augroup('nvim_cmdwin', {}),
command = 'syntax sync minlines=1 maxlines=1',
})
+
+ vim.api.nvim_create_autocmd({ 'SwapExists' }, {
+ pattern = '*',
+ group = vim.api.nvim_create_augroup('nvim_swapfile', {}),
+ callback = function()
+ local info = vim.fn.swapinfo(vim.v.swapname)
+ local user = vim.uv.os_get_passwd().username
+ local iswin = 1 == vim.fn.has('win32')
+ if info.error or info.pid <= 0 or (not iswin and info.user ~= user) then
+ vim.v.swapchoice = '' -- Show the prompt.
+ return
+ end
+ vim.v.swapchoice = 'e' -- Choose "(E)dit".
+ vim.notify(('W325: Ignoring swapfile from Nvim process %d'):format(info.pid))
+ end,
+ })
end
function vim._init_defaults()
diff --git a/runtime/lua/vim/_meta/vimfn.lua b/runtime/lua/vim/_meta/vimfn.lua
index 55c324d739..5a3a5f3890 100644
--- a/runtime/lua/vim/_meta/vimfn.lua
+++ b/runtime/lua/vim/_meta/vimfn.lua
@@ -9312,8 +9312,8 @@ function vim.fn.swapfilelist() end
--- user user name
--- host host name
--- fname original file name
---- pid PID of the Vim process that created the swap
---- file
+--- pid PID of the Nvim process that created the swap
+--- file, or zero if not running.
--- mtime last modification time in seconds
--- inode Optional: INODE number of the file
--- dirty 1 if file was modified, 0 if not