Skip to content

Commit e5c3a3e

Browse files
authored
Merge pull request zyedidia#3719 from niten94/sbuf-switch-skipsave
Skip save on `open` or `term` command if buffer is shared
2 parents 79fe4ae + c457ae4 commit e5c3a3e

File tree

3 files changed

+30
-33
lines changed

3 files changed

+30
-33
lines changed

internal/action/actions.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,30 +1907,29 @@ func (h *BufPane) ForceQuit() bool {
19071907
return true
19081908
}
19091909

1910-
// Quit this will close the current tab or view that is open
1911-
func (h *BufPane) Quit() bool {
1912-
if h.Buf.Modified() {
1913-
for _, b := range buffer.OpenBuffers {
1914-
if b != h.Buf && b.SharedBuffer == h.Buf.SharedBuffer {
1915-
h.ForceQuit()
1916-
return true
1917-
}
1910+
// closePrompt displays a prompt to save the buffer before closing it to proceed
1911+
// with a different action or command
1912+
func (h *BufPane) closePrompt(action string, callback func()) {
1913+
InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) {
1914+
if !canceled && !yes {
1915+
callback()
1916+
} else if !canceled && yes {
1917+
h.SaveCB(action, callback)
19181918
}
1919+
})
1920+
}
19191921

1922+
// Quit this will close the current tab or view that is open
1923+
func (h *BufPane) Quit() bool {
1924+
if h.Buf.Modified() && !h.Buf.Shared() {
19201925
if config.GlobalSettings["autosave"].(float64) > 0 && h.Buf.Path != "" {
19211926
// autosave on means we automatically save when quitting
19221927
h.SaveCB("Quit", func() {
19231928
h.ForceQuit()
19241929
})
19251930
} else {
1926-
InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) {
1927-
if !canceled && !yes {
1928-
h.ForceQuit()
1929-
} else if !canceled && yes {
1930-
h.SaveCB("Quit", func() {
1931-
h.ForceQuit()
1932-
})
1933-
}
1931+
h.closePrompt("Quit", func() {
1932+
h.ForceQuit()
19341933
})
19351934
}
19361935
} else {

internal/action/command.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,8 @@ func (h *BufPane) OpenCmd(args []string) {
308308
}
309309
h.OpenBuffer(b)
310310
}
311-
if h.Buf.Modified() {
312-
InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) {
313-
if !canceled && !yes {
314-
open()
315-
} else if !canceled && yes {
316-
h.Save()
317-
open()
318-
}
319-
})
311+
if h.Buf.Modified() && !h.Buf.Shared() {
312+
h.closePrompt("Save", open)
320313
} else {
321314
open()
322315
}
@@ -1121,14 +1114,9 @@ func (h *BufPane) TermCmd(args []string) {
11211114

11221115
for i, p := range ps {
11231116
if p.ID() == h.ID() {
1124-
if h.Buf.Modified() {
1125-
InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) {
1126-
if !canceled && !yes {
1127-
term(i, false)
1128-
} else if !canceled && yes {
1129-
h.Save()
1130-
term(i, false)
1131-
}
1117+
if h.Buf.Modified() && !h.Buf.Shared() {
1118+
h.closePrompt("Save", func() {
1119+
term(i, false)
11321120
})
11331121
} else {
11341122
term(i, false)

internal/buffer/buffer.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,16 @@ func (b *Buffer) WordAt(loc Loc) []byte {
620620
return b.Substr(start, end)
621621
}
622622

623+
// Shared returns if there are other buffers with the same file as this buffer
624+
func (b *Buffer) Shared() bool {
625+
for _, buf := range OpenBuffers {
626+
if buf != b && buf.SharedBuffer == b.SharedBuffer {
627+
return true
628+
}
629+
}
630+
return false
631+
}
632+
623633
// Modified returns if this buffer has been modified since
624634
// being opened
625635
func (b *Buffer) Modified() bool {

0 commit comments

Comments
 (0)