diff --git a/internal/action/actions.go b/internal/action/actions.go index a27de68a..0ed94558 100644 --- a/internal/action/actions.go +++ b/internal/action/actions.go @@ -1907,30 +1907,29 @@ func (h *BufPane) ForceQuit() bool { return true } +// closePrompt displays a prompt to save the buffer before closing it to proceed +// with a different action or command +func (h *BufPane) closePrompt(action string, callback func()) { + InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) { + if !canceled && !yes { + callback() + } else if !canceled && yes { + h.SaveCB(action, callback) + } + }) +} + // Quit this will close the current tab or view that is open func (h *BufPane) Quit() bool { - if h.Buf.Modified() { - for _, b := range buffer.OpenBuffers { - if b != h.Buf && b.SharedBuffer == h.Buf.SharedBuffer { - h.ForceQuit() - return true - } - } - + if h.Buf.Modified() && !h.Buf.Shared() { if config.GlobalSettings["autosave"].(float64) > 0 && h.Buf.Path != "" { // autosave on means we automatically save when quitting h.SaveCB("Quit", func() { h.ForceQuit() }) } else { - InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) { - if !canceled && !yes { - h.ForceQuit() - } else if !canceled && yes { - h.SaveCB("Quit", func() { - h.ForceQuit() - }) - } + h.closePrompt("Quit", func() { + h.ForceQuit() }) } } else { diff --git a/internal/action/command.go b/internal/action/command.go index 2df2ced6..3b590559 100644 --- a/internal/action/command.go +++ b/internal/action/command.go @@ -308,15 +308,8 @@ func (h *BufPane) OpenCmd(args []string) { } h.OpenBuffer(b) } - if h.Buf.Modified() { - InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) { - if !canceled && !yes { - open() - } else if !canceled && yes { - h.Save() - open() - } - }) + if h.Buf.Modified() && !h.Buf.Shared() { + h.closePrompt("Save", open) } else { open() } @@ -1121,14 +1114,9 @@ func (h *BufPane) TermCmd(args []string) { for i, p := range ps { if p.ID() == h.ID() { - if h.Buf.Modified() { - InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) { - if !canceled && !yes { - term(i, false) - } else if !canceled && yes { - h.Save() - term(i, false) - } + if h.Buf.Modified() && !h.Buf.Shared() { + h.closePrompt("Save", func() { + term(i, false) }) } else { term(i, false) diff --git a/internal/buffer/buffer.go b/internal/buffer/buffer.go index 4226d972..7a96b09f 100644 --- a/internal/buffer/buffer.go +++ b/internal/buffer/buffer.go @@ -620,6 +620,16 @@ func (b *Buffer) WordAt(loc Loc) []byte { return b.Substr(start, end) } +// Shared returns if there are other buffers with the same file as this buffer +func (b *Buffer) Shared() bool { + for _, buf := range OpenBuffers { + if buf != b && buf.SharedBuffer == b.SharedBuffer { + return true + } + } + return false +} + // Modified returns if this buffer has been modified since // being opened func (b *Buffer) Modified() bool {