@@ -574,12 +574,11 @@ pub fn select_all(cx: &mut Context) {
576pub fn select_regex(cx: &mut Context) {
576pub 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| {
579 let text = doc.text().slice(..);
578 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)
582− doc.set_selection(view_id, selection);
581+ doc.set_selection(view.id, selection);
@@ -587,11 +586,10 @@ pub fn select_regex(cx: &mut Context) {
589pub fn split_selection(cx: &mut Context) {
588pub 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| {
592 let text = doc.text().slice(..);
590 let text = doc.text().slice(..);
593− let selection = selection::split_on_matches(text, doc.selection(view_id), ®ex);
591+ let selection = selection::split_on_matches(text, doc.selection(view.id), ®ex);
594− doc.set_selection(view_id, selection);
592+ doc.set_selection(view.id, selection);
597 cx.push_layer(Box::new(prompt));
595 cx.push_layer(Box::new(prompt));
@@ -614,9 +612,9 @@ pub fn split_selection_on_newline(cx: &mut Context) {
614// I'd probably collect all the matches right now and store the current index. The cache needs
612// I'd probably collect all the matches right now and store the current index. The cache needs
615// wiping if input happens.
613// wiping if input happens.
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) {
618 let text = doc.text();
616 let text = doc.text();
619− let selection = doc.selection(view_id);
617+ let selection = doc.selection(view.id);
620 let start = selection.cursor();
618 let start = selection.cursor();
622 // use find_at to find the next match after the cursor, loop around the end
620 // 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
627 let start = text.byte_to_char(mat.start());
625 let start = text.byte_to_char(mat.start());
628 let end = text.byte_to_char(mat.end());
626 let end = text.byte_to_char(mat.end());
630 let selection = if extend {
630 let selection = if extend {
631− selection.clone().push(Range::new(start, end - 1))
631+ selection.clone().push(Range::new(start, head))
633− Selection::single(start, end - 1)
633+ Selection::single(start, head)
636 // TODO: (first_match, regex) stuff in register?
636 // 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);
@@ -649,10 +652,10 @@ pub fn search(cx: &mut Context) {
649 let contents = doc.text().slice(..).to_string();
652 let contents = doc.text().slice(..).to_string();
651 let view_id = cx.view_id;
654 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| {
653 let text = doc.text();
656 let text = doc.text();
654− let start = doc.selection(view_id).cursor();
657+ let start = doc.selection(view.id).cursor();
655− _search(doc, view_id, &contents, ®ex, false);
658+ _search(doc, view, &contents, ®ex, false);
657 // TODO: only store on enter (accept), not update
660 // TODO: only store on enter (accept), not update
658 register::set('\\', vec![regex.as_str().to_string()]);
661 register::set('\\', vec![regex.as_str().to_string()]);
@@ -664,11 +667,10 @@ pub fn search(cx: &mut Context) {
664pub fn _search_next(cx: &mut Context, extend: bool) {
667pub fn _search_next(cx: &mut Context, extend: bool) {
665 if let Some(query) = register::get('\\') {
668 if let Some(query) = register::get('\\') {
666 let query = query.first().unwrap();
669 let query = query.first().unwrap();
667− let view_id = cx.view_id;
670+ let (view, doc) = cx.current();
669 let contents = doc.text().slice(..).to_string();
671 let contents = doc.text().slice(..).to_string();
670 let regex = Regex::new(&query).unwrap();
672 let regex = Regex::new(&query).unwrap();
671− _search(doc, view_id, &contents, ®ex, extend);
673+ _search(doc, view, &contents, ®ex, extend);
@@ -1652,12 +1654,11 @@ pub fn join_selections(cx: &mut Context) {
1653pub fn keep_selections(cx: &mut Context) {
1655pub fn keep_selections(cx: &mut Context) {
1654 // keep selections matching regex
1656 // 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| {
1657 let text = doc.text().slice(..);
1658 let text = doc.text().slice(..);
1659− if let Some(selection) = selection::keep_matches(text, doc.selection(view_id), ®ex) {
1660+ if let Some(selection) = selection::keep_matches(text, doc.selection(view.id), ®ex) {
1660− doc.set_selection(view_id, selection);
1661+ doc.set_selection(view.id, selection);