@@ -351,6 +351,69 @@ pub fn split_selection_on_newline(cx: &mut Context) {
351 doc.set_selection(selection);
351 doc.set_selection(selection);
354+// search: searches for the first occurence in file, provides a prompt
355+// search_next: reuses the last search regex and searches for the next match. The next match becomes the main selection.
356+// -> we always search from after the cursor.head
357+// TODO: be able to use selection as search query (*/alt *)
358+// I'd probably collect all the matches right now and store the current index. The cache needs
359+// wiping if input happens.
361+fn _search(doc: &mut Document, contents: &str, regex: &Regex) {
362+ let text = doc.text();
363+ let start = doc.selection().cursor();
365+ // TODO: use find_at to find the next match after the cursor, loop around the end
366+ if let Some(mat) = regex.find_at(&contents, start) {
367+ let start = text.byte_to_char(mat.start());
368+ let end = text.byte_to_char(mat.end());
369+ let selection = Selection::single(start, end - 1);
370+ // TODO: (first_match, regex) stuff in register?
371+ doc.set_selection(selection);
375+// TODO: use one function for search vs extend
376+pub fn search(cx: &mut Context) {
379+ // TODO: could probably share with select_on_matches?
381+ // HAXX: sadly we can't avoid allocating a single string for the whole buffer since we can't
382+ // feed chunks into the regex yet
383+ let contents = doc.text().slice(..).to_string();
385+ let prompt = ui::regex_prompt(cx, "search:".to_string(), move |doc, regex| {
386+ let text = doc.text();
387+ let start = doc.selection().cursor();
388+ _search(doc, &contents, ®ex);
390+ // TODO: only store on enter (accept), not update
391+ register::set('\\', vec![regex.as_str().to_string()]);
394+ cx.callback = Some(Box::new(
395+ move |compositor: &mut Compositor, editor: &mut Editor| {
396+ compositor.push(Box::new(prompt));
401+pub fn search_next(cx: &mut Context) {
402+ if let Some(query) = register::get('\\') {
403+ let query = query.first().unwrap();
405+ let contents = doc.text().slice(..).to_string();
406+ let regex = Regex::new(&query).unwrap();
407+ _search(doc, &contents, ®ex);
411+// TODO: N -> search_prev
412+// need to loop around buffer also and show a message
413+// same for no matches
354pub fn select_line(cx: &mut Context) {
417pub fn select_line(cx: &mut Context) {
355 // TODO: count
418 // TODO: count
356 let pos = cx.doc().selection().primary();
419 let pos = cx.doc().selection().primary();