@@ -2,7 +2,7 @@ use std::ops::Deref;
3use super::*;
3use super::*;
5−use helix_view::editor::{Action, ConfigEvent};
5+use helix_view::editor::{Action, CloseError, ConfigEvent};
6use ui::completers::{self, Completer};
6use ui::completers::{self, Completer};
8#[derive(Clone)]
8#[derive(Clone)]
@@ -71,8 +71,29 @@ fn buffer_close_by_ids_impl(
71 doc_ids: &[DocumentId],
71 doc_ids: &[DocumentId],
72 force: bool,
72 force: bool,
73) -> anyhow::Result<()> {
73) -> anyhow::Result<()> {
74− for &doc_id in doc_ids {
74+ let (modified_ids, modified_names): (Vec<_>, Vec<_>) = doc_ids
76+ .filter_map(|&doc_id| {
75− editor.close_document(doc_id, force)?;
77+ if let Err(CloseError::BufferModified(name)) = editor.close_document(doc_id, force) {
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);
93+ "{} unsaved buffer(s) remaining: {:?}",
94+ modified_names.len(),
@@ -513,23 +534,26 @@ fn force_write_quit(
513 force_quit(cx, &[], event)
534 force_quit(cx, &[], event)
516−/// Results an error if there are modified buffers remaining and sets editor error,
537+/// Results in an error if there are modified buffers remaining and sets editor
517−/// otherwise returns `Ok(())`
538+/// error, otherwise returns `Ok(())`. If the current document is unmodified,
539+/// and there are modified documents, switches focus to one of them.
518pub(super) fn buffers_remaining_impl(editor: &mut Editor) -> anyhow::Result<()> {
540pub(super) fn buffers_remaining_impl(editor: &mut Editor) -> anyhow::Result<()> {
519− let modified: Vec<_> = editor
541+ let (modified_ids, modified_names): (Vec<_>, Vec<_>) = editor
520 .documents()
542 .documents()
521 .filter(|doc| doc.is_modified())
543 .filter(|doc| doc.is_modified())
522− .map(|doc| {
544+ .map(|doc| (doc.id(), doc.display_name()))
523− doc.relative_path()
545+ .unzip();
524− .map(|path| path.to_string_lossy().to_string())
546+ if let Some(first) = modified_ids.first() {
525− .unwrap_or_else(|| SCRATCH_BUFFER_NAME.into())
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);
528− if !modified.is_empty() {
530 "{} unsaved buffer(s) remaining: {:?}",
554 "{} unsaved buffer(s) remaining: {:?}",
531− modified.len(),
555+ modified_names.len(),
532− modified
556+ modified_names