Merge pull request #3719 from niten94/sbuf-switch-skipsave
Some checks failed
Build and Test / test (1.19.x, macos-latest) (push) Has been cancelled
Build and Test / test (1.19.x, ubuntu-latest) (push) Has been cancelled
Build and Test / test (1.19.x, windows-latest) (push) Has been cancelled
Build and Test / test (1.23.x, macos-latest) (push) Has been cancelled
Build and Test / test (1.23.x, ubuntu-latest) (push) Has been cancelled
Build and Test / test (1.23.x, windows-latest) (push) Has been cancelled

Skip save on `open` or `term` command if buffer is shared
This commit is contained in:
Jöran Karl 2025-04-24 13:36:16 +02:00 committed by GitHub
commit e5c3a3edc3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 33 deletions

View File

@ -1907,30 +1907,29 @@ func (h *BufPane) ForceQuit() bool {
return true 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 // Quit this will close the current tab or view that is open
func (h *BufPane) Quit() bool { func (h *BufPane) Quit() bool {
if h.Buf.Modified() { if h.Buf.Modified() && !h.Buf.Shared() {
for _, b := range buffer.OpenBuffers {
if b != h.Buf && b.SharedBuffer == h.Buf.SharedBuffer {
h.ForceQuit()
return true
}
}
if config.GlobalSettings["autosave"].(float64) > 0 && h.Buf.Path != "" { if config.GlobalSettings["autosave"].(float64) > 0 && h.Buf.Path != "" {
// autosave on means we automatically save when quitting // autosave on means we automatically save when quitting
h.SaveCB("Quit", func() { h.SaveCB("Quit", func() {
h.ForceQuit() h.ForceQuit()
}) })
} else { } else {
InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) { h.closePrompt("Quit", func() {
if !canceled && !yes { h.ForceQuit()
h.ForceQuit()
} else if !canceled && yes {
h.SaveCB("Quit", func() {
h.ForceQuit()
})
}
}) })
} }
} else { } else {

View File

@ -308,15 +308,8 @@ func (h *BufPane) OpenCmd(args []string) {
} }
h.OpenBuffer(b) h.OpenBuffer(b)
} }
if h.Buf.Modified() { if h.Buf.Modified() && !h.Buf.Shared() {
InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) { h.closePrompt("Save", open)
if !canceled && !yes {
open()
} else if !canceled && yes {
h.Save()
open()
}
})
} else { } else {
open() open()
} }
@ -1121,14 +1114,9 @@ func (h *BufPane) TermCmd(args []string) {
for i, p := range ps { for i, p := range ps {
if p.ID() == h.ID() { if p.ID() == h.ID() {
if h.Buf.Modified() { if h.Buf.Modified() && !h.Buf.Shared() {
InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) { h.closePrompt("Save", func() {
if !canceled && !yes { term(i, false)
term(i, false)
} else if !canceled && yes {
h.Save()
term(i, false)
}
}) })
} else { } else {
term(i, false) term(i, false)

View File

@ -620,6 +620,16 @@ func (b *Buffer) WordAt(loc Loc) []byte {
return b.Substr(start, end) 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 // Modified returns if this buffer has been modified since
// being opened // being opened
func (b *Buffer) Modified() bool { func (b *Buffer) Modified() bool {