@@ -574,12 +574,11 @@ pub fn select_all(cx: &mut Context) {
574574}
575575
576576pub fn select_regex(cx: &mut Context) {
577− let view_id = cx.view_id;
578− let prompt = ui::regex_prompt(cx, "select:".to_string(), move |doc, regex| {
577+ let prompt = ui::regex_prompt(cx, "select:".to_string(), move |view, doc, regex| {
579578 let text = doc.text().slice(..);
580− if let Some(selection) = selection::select_on_matches(text, doc.selection(view_id), ®ex)
579+ if let Some(selection) = selection::select_on_matches(text, doc.selection(view.id), ®ex)
581580 {
582− doc.set_selection(view_id, selection);
581+ doc.set_selection(view.id, selection);
583582 }
584583 });
585584
@@ -587,11 +586,10 @@ pub fn select_regex(cx: &mut Context) {
587586}
588587
589588pub fn split_selection(cx: &mut Context) {
590− let view_id = cx.view_id;
591− let prompt = ui::regex_prompt(cx, "split:".to_string(), move |doc, regex| {
589+ let prompt = ui::regex_prompt(cx, "split:".to_string(), move |view, doc, regex| {
592590 let text = doc.text().slice(..);
593− let selection = selection::split_on_matches(text, doc.selection(view_id), ®ex);
594− doc.set_selection(view_id, selection);
591+ let selection = selection::split_on_matches(text, doc.selection(view.id), ®ex);
592+ doc.set_selection(view.id, selection);
595593 });
596594
597595 cx.push_layer(Box::new(prompt));
@@ -614,9 +612,9 @@ pub fn split_selection_on_newline(cx: &mut Context) {
614612// I'd probably collect all the matches right now and store the current index. The cache needs
615613// wiping if input happens.
616614
617−fn _search(doc: &mut Document, view_id: ViewId, contents: &str, regex: &Regex, extend: bool) {
615+fn _search(doc: &mut Document, view: &mut View, contents: &str, regex: &Regex, extend: bool) {
618616 let text = doc.text();
619− let selection = doc.selection(view_id);
617+ let selection = doc.selection(view.id);
620618 let start = selection.cursor();
621619
622620 // use find_at to find the next match after the cursor, loop around the end
@@ -627,14 +625,19 @@ fn _search(doc: &mut Document, view_id: ViewId, contents: &str, regex: &Regex, e
627625 let start = text.byte_to_char(mat.start());
628626 let end = text.byte_to_char(mat.end());
629627
628+ let head = end - 1;
629+
630630 let selection = if extend {
631− selection.clone().push(Range::new(start, end - 1))
631+ selection.clone().push(Range::new(start, head))
632632 } else {
633− Selection::single(start, end - 1)
633+ Selection::single(start, head)
634634 };
635635
636636 // TODO: (first_match, regex) stuff in register?
637− doc.set_selection(view_id, selection);
637+ doc.set_selection(view.id, selection);
638+ // TODO: extract this centering into a function to share with _goto?
639+ let line = doc.text().char_to_line(head);
640+ view.first_line = line.saturating_sub(view.area.height as usize / 2);
638641 };
639642}
640643
@@ -649,10 +652,10 @@ pub fn search(cx: &mut Context) {
649652 let contents = doc.text().slice(..).to_string();
650653
651654 let view_id = cx.view_id;
652− let prompt = ui::regex_prompt(cx, "search:".to_string(), move |doc, regex| {
655+ let prompt = ui::regex_prompt(cx, "search:".to_string(), move |view, doc, regex| {
653656 let text = doc.text();
654− let start = doc.selection(view_id).cursor();
655− _search(doc, view_id, &contents, ®ex, false);
657+ let start = doc.selection(view.id).cursor();
658+ _search(doc, view, &contents, ®ex, false);
656659
657660 // TODO: only store on enter (accept), not update
658661 register::set('\\', vec![regex.as_str().to_string()]);
@@ -664,11 +667,10 @@ pub fn search(cx: &mut Context) {
664667pub fn _search_next(cx: &mut Context, extend: bool) {
665668 if let Some(query) = register::get('\\') {
666669 let query = query.first().unwrap();
667− let view_id = cx.view_id;
668− let doc = cx.doc();
670+ let (view, doc) = cx.current();
669671 let contents = doc.text().slice(..).to_string();
670672 let regex = Regex::new(&query).unwrap();
671− _search(doc, view_id, &contents, ®ex, extend);
673+ _search(doc, view, &contents, ®ex, extend);
672674 }
673675}
674676
@@ -1652,12 +1654,11 @@ pub fn join_selections(cx: &mut Context) {
16521654
16531655pub fn keep_selections(cx: &mut Context) {
16541656 // keep selections matching regex
1655− let view_id = cx.view_id;
1656− let prompt = ui::regex_prompt(cx, "keep:".to_string(), move |doc, regex| {
1657+ let prompt = ui::regex_prompt(cx, "keep:".to_string(), move |view, doc, regex| {
16571658 let text = doc.text().slice(..);
16581659
1659− if let Some(selection) = selection::keep_matches(text, doc.selection(view_id), ®ex) {
1660− doc.set_selection(view_id, selection);
1660+ if let Some(selection) = selection::keep_matches(text, doc.selection(view.id), ®ex) {
1661+ doc.set_selection(view.id, selection);
16611662 }
16621663 });
16631664