fn find(&mut self) {
        let mut direction = Direction::Forward;
        let mut last_match: Option<LastMatch> = None;
        let mut on_keypress = |editor: &mut Editor, partial_input: &str, kp: Keypress| {
            last_match
                .as_mut()
                .map(|lm| { editor.rows[lm.row_index].highlights = lm.take_saved(); });

            match kp {
                Keypress::Enter | Keypress::Esc => return,
                Keypress::Ar(dir) => direction = Direction::from(dir),
                _ => {
                    direction = Direction::Forward;
                    last_match = None;
                }
            }
            Some(partial_input)
                .iter()
                .find(|q| !q.is_empty())
                .and_then(|q| {
                    // there's a non-empty search query

                    // always start forward
                    if last_match.is_none() {
                        direction = Direction::Forward
                    };

                    // start with the line after the current match
                    // relative to the beginning if forward, end if back
                    let current = last_match
                        .as_ref()
                        .map(|lm| match direction {
                                 Direction::Forward => lm.row_index + 1,
                                 Direction::Backward => {
                                     (((editor.rows.len() - 1) as i32) -
                                      (lm.row_index as i32 - 1)) as
                                     usize
                                 }
                             })
                        .unwrap_or(0);

                    // make a pass through the file starting at the current match offset,
                    // looping back around through the file when reaching either end
                    direction
                        .traverse(editor.rows.iter().enumerate())
                        .into_iter()
                        .cycle()
                        .skip(current)
                        .take(editor.rows.len())
                        .flat_map(|(i, row)| row.rendered.find(q).map(|f| (i, f)).into_iter())
                        .next()
                })
                .map(|(rowidx, colidx)| {
                    // Found a match, update cursor position, highlight
                    last_match = Some(LastMatch {
                                          row_index: rowidx,
                                          saved_hl: editor.rows[rowidx].highlights.clone(),
                                      });
                    editor.position.rowoff = editor.rows.len();
                    editor.position.cy = rowidx;
                    let brow = &mut editor.rows[rowidx];
                    editor.position.cx = brow.rx_to_cx(colidx);
                    brow.highlight_range(Highlight::Match, colidx, colidx + partial_input.len());
                });
        };
        // Save the position from before the start of the search
        let initial = self.position;

        let last = self.prompt("Seach: {} (Use ESC/Arrows/Enter)", &mut on_keypress);
        last.ok()
            .iter()
            .find(|s| !s.is_empty())
            .map(|_| ())
            // If the prompt returned empty, the user cancelled the operation,
            // set the cursor back to the original position
            .unwrap_or_else(|| self.position = initial);
    }
/// Open a prompt and search for user's input.
Open a prompt and search for user's input.
Open a prompt and search for user ' s input .
fn find(&mut self) {
        let mut direction = Direction::Forward;
        let mut last_match: Option<LastMatch> = None;
        let mut on_keypress = |editor: &mut Editor, partial_input: &str, kp: Keypress| {
            last_match
                .as_mut()
                .map(|lm| { editor.rows[lm.row_index].highlights = lm.take_saved(); });
            match kp {
                Keypress::Enter | Keypress::Esc => return,
                Keypress::Ar(dir) => direction = Direction::from(dir),
                _ => {
                    direction = Direction::Forward;
                    last_match = None;
                }
            }
            Some(partial_input)
                .iter()
                .find(|q| !q.is_empty())
                .and_then(|q| {
                    // there's a non-empty search query
                    // always start forward
                    if last_match.is_none() {
                        direction = Direction::Forward
                    };
                    // start with the line after the current match
                    // relative to the beginning if forward, end if back
                    let current = last_match
                        .as_ref()
                        .map(|lm| match direction {
                                 Direction::Forward => lm.row_index + 1,
                                 Direction::Backward => {
                                     (((editor.rows.len() - 1) as i32) -
                                      (lm.row_index as i32 - 1)) as
                                     usize
                                 }
                             })
                        .unwrap_or(0);
                    // make a pass through the file starting at the current match offset,
                    // looping back around through the file when reaching either end
                    direction
                        .traverse(editor.rows.iter().enumerate())
                        .into_iter()
                        .cycle()
                        .skip(current)
                        .take(editor.rows.len())
                        .flat_map(|(i, row)| row.rendered.find(q).map(|f| (i, f)).into_iter())
                        .next()
                })
                .map(|(rowidx, colidx)| {
                    // Found a match, update cursor position, highlight
                    last_match = Some(LastMatch {
                                          row_index: rowidx,
                                          saved_hl: editor.rows[rowidx].highlights.clone(),
                                      });
                    editor.position.rowoff = editor.rows.len();
                    editor.position.cy = rowidx;
                    let brow = &mut editor.rows[rowidx];
                    editor.position.cx = brow.rx_to_cx(colidx);
                    brow.highlight_range(Highlight::Match, colidx, colidx + partial_input.len());
                });
        };
        // Save the position from before the start of the search
        let initial = self.position;
        let last = self.prompt("Seach: {} (Use ESC/Arrows/Enter)", &mut on_keypress);
        last.ok()
            .iter()
            .find(|s| !s.is_empty())
            .map(|_| ())
            // If the prompt returned empty, the user cancelled the operation,
            // set the cursor back to the original position
            .unwrap_or_else(|| self.position = initial);
    }
fn find ( & mut self ) { let mut direction = Direction :: Forward ; let mut last_match : Option < LastMatch > = None ; let mut on_keypress = | editor : & mut Editor , partial_input : & str , kp : Keypress | { last_match . as_mut ( ) . map ( | lm | { editor . rows [ lm . row_index ] . highlights = lm . take_saved ( ) ; } ) ; match kp { Keypress :: Enter | Keypress :: Esc => return , Keypress :: Ar ( dir ) => direction = Direction :: from ( dir ) , _ => { direction = Direction :: Forward ; last_match = None ; } } Some ( partial_input ) . iter ( ) . find ( | q | ! q . is_empty ( ) ) . and_then ( | q | { // there's a non-empty search query // always start forward if last_match . is_none ( ) { direction = Direction :: Forward } ; // start with the line after the current match // relative to the beginning if forward, end if back let current = last_match . as_ref ( ) . map ( | lm | match direction { Direction :: Forward => lm . row_index + 1 , Direction :: Backward => { ( ( ( editor . rows . len ( ) - 1 ) as i32 ) - ( lm . row_index as i32 - 1 ) ) as usize } } ) . unwrap_or ( 0 ) ; // make a pass through the file starting at the current match offset, // looping back around through the file when reaching either end direction . traverse ( editor . rows . iter ( ) . enumerate ( ) ) . into_iter ( ) . cycle ( ) . skip ( current ) . take ( editor . rows . len ( ) ) . flat_map ( | ( i , row ) | row . rendered . find ( q ) . map ( | f | ( i , f ) ) . into_iter ( ) ) . next ( ) } ) . map ( | ( rowidx , colidx ) | { // Found a match, update cursor position, highlight last_match = Some ( LastMatch { row_index : rowidx , saved_hl : editor . rows [ rowidx ] . highlights . clone ( ) , } ) ; editor . position . rowoff = editor . rows . len ( ) ; editor . position . cy = rowidx ; let brow = & mut editor . rows [ rowidx ] ; editor . position . cx = brow . rx_to_cx ( colidx ) ; brow . highlight_range ( Highlight :: Match , colidx , colidx + partial_input . len ( ) ) ; } ) ; } ; // Save the position from before the start of the search let initial = self . position ; let last = self . prompt ( " " , & mut on_keypress ) ; last . ok ( ) . iter ( ) . find ( | s | ! s . is_empty ( ) ) . map ( | _ | ( ) ) // If the prompt returned empty, the user cancelled the operation, // set the cursor back to the original position . unwrap_or_else ( | | self . position = initial ) ; }
Open a prompt and search for user's input.
Open a prompt and search for user ' s input .
&mut self