@@ -586,7 +586,7 @@ fn goto_line_end(cx: &mut Context) {
586586 goto_line_end_impl(
587587 view,
588588 doc,
589− if doc.mode == Mode::Select {
589+ if cx.editor.mode == Mode::Select {
590590 Movement::Extend
591591 } else {
592592 Movement::Move
@@ -616,7 +616,7 @@ fn goto_line_end_newline(cx: &mut Context) {
616616 goto_line_end_newline_impl(
617617 view,
618618 doc,
619− if doc.mode == Mode::Select {
619+ if cx.editor.mode == Mode::Select {
620620 Movement::Extend
621621 } else {
622622 Movement::Move
@@ -647,7 +647,7 @@ fn goto_line_start(cx: &mut Context) {
647647 goto_line_start_impl(
648648 view,
649649 doc,
650− if doc.mode == Mode::Select {
650+ if cx.editor.mode == Mode::Select {
651651 Movement::Extend
652652 } else {
653653 Movement::Move
@@ -752,7 +752,7 @@ fn goto_first_nonwhitespace(cx: &mut Context) {
752752
753753 if let Some(pos) = find_first_non_whitespace_char(text.line(line)) {
754754 let pos = pos + text.line_to_char(line);
755− range.put_cursor(text, pos, doc.mode == Mode::Select)
755+ range.put_cursor(text, pos, cx.editor.mode == Mode::Select)
756756 } else {
757757 range
758758 }
@@ -952,7 +952,7 @@ where
952952 let motion = move |editor: &mut Editor| {
953953 let (view, doc) = current!(editor);
954954 let text = doc.text().slice(..);
955− let behavior = if doc.mode == Mode::Select {
955+ let behavior = if editor.mode == Mode::Select {
956956 Movement::Extend
957957 } else {
958958 Movement::Move
@@ -985,7 +985,7 @@ fn goto_file_start(cx: &mut Context) {
985985 let selection = doc
986986 .selection(view.id)
987987 .clone()
988− .transform(|range| range.put_cursor(text, 0, doc.mode == Mode::Select));
988+ .transform(|range| range.put_cursor(text, 0, cx.editor.mode == Mode::Select));
989989 push_jump(view, doc);
990990 doc.set_selection(view.id, selection);
991991 }
@@ -998,7 +998,7 @@ fn goto_file_end(cx: &mut Context) {
998998 let selection = doc
999999 .selection(view.id)
10001000 .clone()
1001− .transform(|range| range.put_cursor(text, pos, doc.mode == Mode::Select));
1001+ .transform(|range| range.put_cursor(text, pos, cx.editor.mode == Mode::Select));
10021002 push_jump(view, doc);
10031003 doc.set_selection(view.id, selection);
10041004}
@@ -1375,7 +1375,7 @@ pub fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
13751375 if line != cursor.row {
13761376 let head = pos_at_coords(text, Position::new(line, cursor.col), true); // this func will properly truncate to line end
13771377
1378− let anchor = if doc.mode == Mode::Select {
1378+ let anchor = if cx.editor.mode == Mode::Select {
13791379 range.anchor
13801380 } else {
13811381 head
@@ -2098,7 +2098,7 @@ fn delete_selection_impl(cx: &mut Context, op: Operation) {
20982098 exit_select_mode(cx);
20992099 }
21002100 Operation::Change => {
2101− enter_insert_mode(doc);
2101+ enter_insert_mode(cx);
21022102 }
21032103 }
21042104}
@@ -2167,14 +2167,14 @@ fn ensure_selections_forward(cx: &mut Context) {
21672167 doc.set_selection(view.id, selection);
21682168}
21692169
2170−fn enter_insert_mode(doc: &mut Document) {
2171− doc.mode = Mode::Insert;
2170+fn enter_insert_mode(cx: &mut Context) {
2171+ cx.editor.mode = Mode::Insert;
21722172}
21732173
21742174// inserts at the start of each selection
21752175fn insert_mode(cx: &mut Context) {
2176+ enter_insert_mode(cx);
21762177 let (view, doc) = current!(cx.editor);
2177− enter_insert_mode(doc);
21782178
21792179 log::trace!(
21802180 "entering insert mode with sel: {:?}, text: {:?}",
@@ -2192,8 +2192,8 @@ fn insert_mode(cx: &mut Context) {
21922192
21932193// inserts at the end of each selection
21942194fn append_mode(cx: &mut Context) {
2195+ enter_insert_mode(cx);
21952196 let (view, doc) = current!(cx.editor);
2196− enter_insert_mode(doc);
21972197 doc.restore_cursor = true;
21982198 let text = doc.text().slice(..);
21992199
@@ -2421,9 +2421,9 @@ impl ui::menu::Item for MappableCommand {
24212421pub fn command_palette(cx: &mut Context) {
24222422 cx.callback = Some(Box::new(
24232423 move |compositor: &mut Compositor, cx: &mut compositor::Context| {
2424− let doc = doc_mut!(cx.editor);
2425− let keymap =
2426− compositor.find::<ui::EditorView>().unwrap().keymaps.map()[&doc.mode].reverse_map();
2424+ let keymap = compositor.find::<ui::EditorView>().unwrap().keymaps.map()
2425+ [&cx.editor.mode]
2426+ .reverse_map();
24272427
24282428 let mut commands: Vec<MappableCommand> = MappableCommand::STATIC_COMMAND_LIST.into();
24292429 commands.extend(typed::TYPABLE_COMMAND_LIST.iter().map(|cmd| {
@@ -2464,14 +2464,13 @@ fn last_picker(cx: &mut Context) {
24642464// I inserts at the first nonwhitespace character of each line with a selection
24652465fn prepend_to_line(cx: &mut Context) {
24662466 goto_first_nonwhitespace(cx);
2467− let doc = doc_mut!(cx.editor);
2468− enter_insert_mode(doc);
2467+ enter_insert_mode(cx);
24692468}
24702469
24712470// A inserts at the end of each line with a selection
24722471fn append_to_line(cx: &mut Context) {
2472+ enter_insert_mode(cx);
24732473 let (view, doc) = current!(cx.editor);
2474− enter_insert_mode(doc);
24752474
24762475 let selection = doc.selection(view.id).clone().transform(|range| {
24772476 let text = doc.text().slice(..);
@@ -2527,8 +2526,8 @@ pub enum Open {
25272526
25282527fn open(cx: &mut Context, open: Open) {
25292528 let count = cx.count();
2529+ enter_insert_mode(cx);
25302530 let (view, doc) = current!(cx.editor);
2531− enter_insert_mode(doc);
25322531
25332532 let text = doc.text().slice(..);
25342533 let contents = doc.text();
@@ -2606,13 +2605,12 @@ fn open_above(cx: &mut Context) {
26062605}
26072606
26082607fn normal_mode(cx: &mut Context) {
2609− let (view, doc) = current!(cx.editor);
2610−
2611− if doc.mode == Mode::Normal {
2608+ if cx.editor.mode == Mode::Normal {
26122609 return;
26132610 }
26142611
2615− doc.mode = Mode::Normal;
2612+ cx.editor.mode = Mode::Normal;
2613+ let (view, doc) = current!(cx.editor);
26162614
26172615 try_restore_indent(doc, view.id);
26182616
@@ -2690,7 +2688,7 @@ fn goto_line_impl(editor: &mut Editor, count: Option<NonZeroUsize>) {
26902688 let selection = doc
26912689 .selection(view.id)
26922690 .clone()
2693− .transform(|range| range.put_cursor(text, pos, doc.mode == Mode::Select));
2691+ .transform(|range| range.put_cursor(text, pos, editor.mode == Mode::Select));
26942692
26952693 push_jump(view, doc);
26962694 doc.set_selection(view.id, selection);
@@ -2710,7 +2708,7 @@ fn goto_last_line(cx: &mut Context) {
27102708 let selection = doc
27112709 .selection(view.id)
27122710 .clone()
2713− .transform(|range| range.put_cursor(text, pos, doc.mode == Mode::Select));
2711+ .transform(|range| range.put_cursor(text, pos, cx.editor.mode == Mode::Select));
27142712
27152713 push_jump(view, doc);
27162714 doc.set_selection(view.id, selection);
@@ -2733,7 +2731,7 @@ fn goto_last_modification(cx: &mut Context) {
27332731 let selection = doc
27342732 .selection(view.id)
27352733 .clone()
2736− .transform(|range| range.put_cursor(text, pos, doc.mode == Mode::Select));
2734+ .transform(|range| range.put_cursor(text, pos, cx.editor.mode == Mode::Select));
27372735 doc.set_selection(view.id, selection);
27382736 }
27392737}
@@ -2770,13 +2768,12 @@ fn select_mode(cx: &mut Context) {
27702768 });
27712769 doc.set_selection(view.id, selection);
27722770
2773− doc_mut!(cx.editor).mode = Mode::Select;
2771+ cx.editor.mode = Mode::Select;
27742772}
27752773
27762774fn exit_select_mode(cx: &mut Context) {
2777− let doc = doc_mut!(cx.editor);
2778− if doc.mode == Mode::Select {
2779− doc.mode = Mode::Normal;
2775+ if cx.editor.mode == Mode::Select {
2776+ cx.editor.mode = Mode::Normal;
27802777 }
27812778}
27822779
@@ -3439,11 +3436,11 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &View, action: Paste,
34393436
34403437pub(crate) fn paste_bracketed_value(cx: &mut Context, contents: String) {
34413438 let count = cx.count();
3442− let (view, doc) = current!(cx.editor);
3443− let paste = match doc.mode {
3439+ let paste = match cx.editor.mode {
34443440 Mode::Insert | Mode::Select => Paste::Cursor,
34453441 Mode::Normal => Paste::Before,
34463442 };
3443+ let (view, doc) = current!(cx.editor);
34473444 paste_impl(&[contents], doc, view, paste, count);
34483445}
34493446
@@ -3838,8 +3835,7 @@ pub fn completion(cx: &mut Context) {
38383835 cx.callback(
38393836 future,
38403837 move |editor, compositor, response: Option<lsp::CompletionResponse>| {
3841− let doc = doc!(editor);
3842− if doc.mode() != Mode::Insert {
3838+ if editor.mode != Mode::Insert {
38433839 // we're not in insert mode anymore
38443840 return;
38453841 }
@@ -4046,7 +4042,7 @@ fn match_brackets(cx: &mut Context) {
40464042 if let Some(pos) =
40474043 match_brackets::find_matching_bracket_fuzzy(syntax, doc.text(), range.cursor(text))
40484044 {
4049− range.put_cursor(text, pos, doc.mode == Mode::Select)
4045+ range.put_cursor(text, pos, cx.editor.mode == Mode::Select)
40504046 } else {
40514047 range
40524048 }