Skip to content

Commit

Permalink
fix: restore normal mode and cursor position when exiting input
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Dec 4, 2021
1 parent 7d0e85f commit 7705013
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion autoload/dressing.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ func! dressing#prompt_confirm(text) abort
endfunc

func! dressing#prompt_cancel() abort
lua dressing_prompt_cancel()
lua dressing_prompt_confirm()
endfunc

function! dressing#fzf_run(labels, options, window) abort
Expand Down
23 changes: 13 additions & 10 deletions lua/dressing/input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ local util = require("dressing.util")

local function clear_callbacks()
_G.dressing_prompt_confirm = function() end
_G.dressing_prompt_cancel = function() end
_G.dressing_prompt_hl = function() end
end

Expand All @@ -20,6 +19,7 @@ return function(opts, on_confirm)
end
local config = global_config.get_mod_config("input", opts)

local start_in_insert = vim.api.nvim_get_mode().mode == "i"
local bufnr = vim.api.nvim_create_buf(false, true)
local prompt = opts.prompt or config.default_prompt
local width = util.calculate_width(config.prefer_width + vim.api.nvim_strwidth(prompt), config)
Expand All @@ -39,16 +39,22 @@ return function(opts, on_confirm)
vim.api.nvim_buf_set_option(bufnr, "swapfile", false)
vim.api.nvim_buf_set_option(bufnr, "bufhidden", "wipe")
vim.fn.prompt_setprompt(bufnr, prompt)
-- Would prefer to use v:lua directly here, but it doesn't work :(
vim.fn.prompt_setcallback(bufnr, "dressing#prompt_confirm")
vim.fn.prompt_setinterrupt(bufnr, "dressing#prompt_cancel")
_G.dressing_prompt_confirm = function(text)
clear_callbacks()
vim.api.nvim_win_close(winnr, true)
if not start_in_insert then
vim.cmd("stopinsert")
-- stopinsert will move the cursor back 1. We need to move it forward 1 to
-- put it in the place you were when you opened the modal.
local cursor = vim.api.nvim_win_get_cursor(0)
cursor[2] = cursor[2] + 1
vim.api.nvim_win_set_cursor(0, cursor)
end
on_confirm(text)
end
_G.dressing_prompt_cancel = function()
clear_callbacks()
vim.api.nvim_win_close(winnr, true)
on_confirm(nil)
end
if opts.highlight then
_G.dressing_prompt_hl = function()
local text = vim.api.nvim_buf_get_lines(bufnr, 0, 1, true)[1]
Expand All @@ -72,11 +78,8 @@ return function(opts, on_confirm)
autocmd TextChangedI <buffer> lua dressing_prompt_hl()
]])
end
-- Would prefer to use v:lua directly here, but it doesn't work :(
vim.fn.prompt_setcallback(bufnr, "dressing#prompt_confirm")
vim.fn.prompt_setinterrupt(bufnr, "dressing#prompt_cancel")
vim.cmd([[
autocmd BufLeave <buffer> call dressing#prompt_cancel()
autocmd BufLeave <buffer> lua dressing_prompt_confirm()
]])
vim.cmd("startinsert!")
if opts.default then
Expand Down

0 comments on commit 7705013

Please sign in to comment.