@@ -2,7 +2,7 @@ use std::ops::Deref;
22
33use super::*;
44
5−use helix_view::editor::{Action, ConfigEvent};
5+use helix_view::editor::{Action, CloseError, ConfigEvent};
66use ui::completers::{self, Completer};
77
88#[derive(Clone)]
@@ -71,8 +71,29 @@ fn buffer_close_by_ids_impl(
7171 doc_ids: &[DocumentId],
7272 force: bool,
7373) -> anyhow::Result<()> {
74− for &doc_id in doc_ids {
75− editor.close_document(doc_id, force)?;
74+ let (modified_ids, modified_names): (Vec<_>, Vec<_>) = doc_ids
75+ .iter()
76+ .filter_map(|&doc_id| {
77+ if let Err(CloseError::BufferModified(name)) = editor.close_document(doc_id, force) {
78+ Some((doc_id, name))
79+ } else {
80+ None
81+ }
82+ })
83+ .unzip();
84+
85+ if let Some(first) = modified_ids.first() {
86+ let current = doc!(editor);
87+ // If the current document is unmodified, and there are modified
88+ // documents, switch focus to the first modified doc.
89+ if !modified_ids.contains(¤t.id()) {
90+ editor.switch(*first, Action::Replace);
91+ }
92+ bail!(
93+ "{} unsaved buffer(s) remaining: {:?}",
94+ modified_names.len(),
95+ modified_names
96+ );
7697 }
7798
7899 Ok(())
@@ -513,23 +534,26 @@ fn force_write_quit(
513534 force_quit(cx, &[], event)
514535}
515536
516−/// Results an error if there are modified buffers remaining and sets editor error,
517−/// otherwise returns `Ok(())`
537+/// Results in an error if there are modified buffers remaining and sets editor
538+/// error, otherwise returns `Ok(())`. If the current document is unmodified,
539+/// and there are modified documents, switches focus to one of them.
518540pub(super) fn buffers_remaining_impl(editor: &mut Editor) -> anyhow::Result<()> {
519− let modified: Vec<_> = editor
541+ let (modified_ids, modified_names): (Vec<_>, Vec<_>) = editor
520542 .documents()
521543 .filter(|doc| doc.is_modified())
522− .map(|doc| {
523− doc.relative_path()
524− .map(|path| path.to_string_lossy().to_string())
525− .unwrap_or_else(|| SCRATCH_BUFFER_NAME.into())
526− })
527− .collect();
528− if !modified.is_empty() {
544+ .map(|doc| (doc.id(), doc.display_name()))
545+ .unzip();
546+ if let Some(first) = modified_ids.first() {
547+ let current = doc!(editor);
548+ // If the current document is unmodified, and there are modified
549+ // documents, switch focus to the first modified doc.
550+ if !modified_ids.contains(¤t.id()) {
551+ editor.switch(*first, Action::Replace);
552+ }
529553 bail!(
530554 "{} unsaved buffer(s) remaining: {:?}",
531− modified.len(),
532− modified
555+ modified_names.len(),
556+ modified_names
533557 );
534558 }
535559 Ok(())