@@ -954,17 +954,22 @@ mod cmd {
954954 };
955955 }
956956
957− fn write(editor: &mut Editor, args: &[&str], event: PromptEvent) {
958− let (view, doc) = editor.current();
959− if let Some(path) = args.get(0) {
960− if let Err(err) = doc.set_path(Path::new(path)) {
961− editor.set_error(format!("invalid filepath: {}", err));
962− return;
957+ fn _write<P: AsRef<Path>>(
958+ view: &View,
959+ doc: &mut Document,
960+ path: Option<P>,
961+ ) -> Result<(), anyhow::Error> {
962+ use anyhow::anyhow;
963+
964+ if let Some(path) = path {
965+ if let Err(err) = doc.set_path(path.as_ref()) {
966+ return Err(anyhow!("invalid filepath: {}", err));
963967 };
964968 }
965969 if doc.path().is_none() {
966− editor.set_error("cannot write a buffer without a filename".to_string());
967− return;
970+ return Err(anyhow!(
971+ "cannot write a buffer without a filename".to_string()
972+ ));
968973 }
969974 let autofmt = doc
970975 .language_config()
@@ -974,6 +979,14 @@ mod cmd {
974979 doc.format(view.id); // TODO: merge into save
975980 }
976981 tokio::spawn(doc.save());
982+ Ok(())
983+ }
984+
985+ fn write(editor: &mut Editor, args: &[&str], event: PromptEvent) {
986+ let (view, doc) = editor.current();
987+ if let Err(e) = _write(view, doc, args.first()) {
988+ editor.set_error(e.to_string());
989+ };
977990 }
978991
979992 fn new_file(editor: &mut Editor, args: &[&str], event: PromptEvent) {
@@ -1010,6 +1023,52 @@ mod cmd {
10101023 doc.later(view.id, uk)
10111024 }
10121025
1026+ fn write_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1027+ write(editor, args, event.clone());
1028+ quit(editor, &[], event);
1029+ }
1030+
1031+ fn force_write_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1032+ write(editor, args, event.clone());
1033+ force_quit(editor, &[], event);
1034+ }
1035+
1036+ fn _write_all(editor: &mut Editor, args: &[&str], event: PromptEvent, quit: bool, force: bool) {
1037+ let ids = editor
1038+ .tree
1039+ .views()
1040+ .map(|(view, _)| (view.id, view.doc))
1041+ .collect::<Vec<_>>();
1042+
1043+ for (view_id, doc_id) in ids {
1044+ if let Some(doc) = editor.documents.get_mut(doc_id) {
1045+ let view = editor.tree.get(view_id);
1046+ if let Err(e) = _write(view, doc, None::<&str>) {
1047+ editor.set_error(e.to_string());
1048+ } else if quit {
1049+ editor.close(view_id, false);
1050+ continue;
1051+ }
1052+
1053+ if force {
1054+ editor.close(view_id, false);
1055+ }
1056+ }
1057+ }
1058+ }
1059+
1060+ fn write_all(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1061+ _write_all(editor, args, event, false, false)
1062+ }
1063+
1064+ fn write_all_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1065+ _write_all(editor, args, event, true, false)
1066+ }
1067+
1068+ fn force_write_all_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1069+ _write_all(editor, args, event, true, true)
1070+ }
1071+
10131072 pub const COMMAND_LIST: &[Command] = &[
10141073 Command {
10151074 name: "quit",
@@ -1067,6 +1126,41 @@ mod cmd {
10671126 fun: later,
10681127 completer: None,
10691128 },
1129+ Command {
1130+ name: "write-quit",
1131+ alias: Some("wq"),
1132+ doc: "Writes changes to disk and closes the current view. Accepts an optional path (:wq some/path.txt)",
1133+ fun: write_quit,
1134+ completer: Some(completers::filename),
1135+ },
1136+ Command {
1137+ name: "write-quit!",
1138+ alias: Some("wq!"),
1139+ doc: "Writes changes to disk and closes the current view forcefully. Accepts an optional path (:wq! some/path.txt)",
1140+ fun: force_write_quit,
1141+ completer: Some(completers::filename),
1142+ },
1143+ Command {
1144+ name: "write-all",
1145+ alias: Some("wa"),
1146+ doc: "Writes changes from all views to disk.",
1147+ fun: write_all,
1148+ completer: None,
1149+ },
1150+ Command {
1151+ name: "write-all-quit",
1152+ alias: Some("waq"),
1153+ doc: "Writes changes from all views to disk and close all views.",
1154+ fun: write_all_quit,
1155+ completer: None,
1156+ },
1157+ Command {
1158+ name: "write-all-quit!",
1159+ alias: Some("waq!"),
1160+ doc: "Writes changes from all views to disk and close all views forcefully (ignoring errors).",
1161+ fun: force_write_all_quit,
1162+ completer: None,
1163+ },
10701164 ];
10711165
10721166 pub static COMMANDS: Lazy<HashMap<&'static str, &'static Command>> = Lazy::new(|| {