From b91eb4f2d0ebae7592045d7393dea7226a8007cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Diego=20Aur=C3=A9lio=20Mesquita?= Date: Sun, 29 Oct 2017 00:17:23 -0200 Subject: [PATCH] Proof of concept for incremental search. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marco Diego Aurélio Mesquita --- src/nano.c | 2 ++ src/prompt.c | 10 ++++++++++ src/proto.h | 4 +++- src/search.c | 45 +++++++++++++++++++++++++++++++++++++++------ src/text.c | 2 +- 5 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/nano.c b/src/nano.c index b3888d9d..34cf0d65 100644 --- a/src/nano.c +++ b/src/nano.c @@ -1413,6 +1413,8 @@ void do_toggle(int flag) if (flag == NO_HELP || flag == NO_WRAP || flag == NO_COLOR_SYNTAX) enabled = !enabled; + spotlight(TRUE, 0, 7); + statusline(HUSH, "%s %s", _(flagtostr(flag)), enabled ? _("enabled") : _("disabled")); } diff --git a/src/prompt.c b/src/prompt.c index 30e21c38..eeb3de0a 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -437,6 +437,13 @@ void update_the_statusbar(void) wnoutrefresh(bottomwin); } +void (*prompt_func)(char*) = NULL; + +void set_prompt_cb(void (*prompt_cb)(char*)) +{ + prompt_func = prompt_cb; +} + /* Get a string of input at the statusbar prompt. */ functionptrtype acquire_an_answer(int *actual, bool allow_tabs, bool allow_files, bool *listed, @@ -575,6 +582,9 @@ functionptrtype acquire_an_answer(int *actual, bool allow_tabs, if (finished) break; + if (prompt_func) { + prompt_func (answer); + } update_the_statusbar(); #if !defined(DISABLE_HISTORIES) && defined(ENABLE_TABCOMP) diff --git a/src/proto.h b/src/proto.h index 7dfcccc3..22ff0748 100644 --- a/src/proto.h +++ b/src/proto.h @@ -476,6 +476,7 @@ size_t statusbar_xplustabs(void); size_t get_statusbar_page_start(size_t start_col, size_t column); void reinit_statusbar_x(void); void update_the_statusbar(void); +void set_prompt_cb(void (*prompt_cb)(char*)); int do_prompt(bool allow_tabs, bool allow_files, int menu, const char *curranswer, #ifndef DISABLE_HISTORIES @@ -498,7 +499,8 @@ void do_rcfiles(void); void not_found_msg(const char *str); void search_replace_abort(void); int findnextstr(const char *needle, bool whole_word_only, int modus, - size_t *match_len, bool skipone, const filestruct *begin, size_t begin_x); + size_t *match_len, bool skipone, const filestruct *begin, + size_t begin_x, bool verbose); void do_search(void); void do_search_forward(void); void do_search_backward(void); diff --git a/src/search.c b/src/search.c index f2f6331d..57023a57 100644 --- a/src/search.c +++ b/src/search.c @@ -89,6 +89,34 @@ void search_replace_abort(void) regexp_cleanup(); } +void inc_search_cb(char *answer) +{ + //fprintf(stderr, "Answer: %s\n", answer); + filestruct *was_current = openfile->current; + size_t was_current_x = openfile->current_x; + size_t len; + bool was_full_circle = came_full_circle; + int didfind; + + if (answer[0] == '\0') + return; + + len = strlen(answer); + + came_full_circle = FALSE; + + didfind = findnextstr(answer, FALSE, JUSTFIND, NULL, FALSE, + openfile->current, openfile->current_x, TRUE); + edit_refresh(); + spotlight(didfind == 1 && len >= 2 ? TRUE : FALSE, 0, len); + + // Reposition cursor + openfile->current= was_current; + openfile->current_x = was_current_x; + openfile->placewewant = xplustabs(); + came_full_circle = was_full_circle; +} + /* Set up the system variables for a search or replace. If use_answer * is TRUE, only set backupstring to answer. Return -2 to run the * opposite program (search -> replace, replace -> search), return -1 if @@ -128,6 +156,7 @@ int search_init(bool replacing, bool use_answer) } else buf = mallocstrcpy(NULL, ""); + set_prompt_cb(inc_search_cb); /* This is now one simple call. It just does a lot. */ i = do_prompt(FALSE, FALSE, inhelp ? MFINDINHELP : (replacing ? MREPLACE : MWHEREIS), @@ -149,6 +178,7 @@ int search_init(bool replacing, bool use_answer) /* Release buf now that we don't need it anymore. */ free(buf); + set_prompt_cb(NULL); free(backupstring); backupstring = NULL; @@ -206,7 +236,8 @@ int search_init(bool replacing, bool use_answer) * found something, 0 when nothing, and -2 on cancel. When match_len is * not NULL, set it to the length of the found string, if any. */ int findnextstr(const char *needle, bool whole_word_only, int modus, - size_t *match_len, bool skipone, const filestruct *begin, size_t begin_x) + size_t *match_len, bool skipone, const filestruct *begin, + size_t begin_x, bool verbose) { size_t found_len = strlen(needle); /* The length of a match -- will be recomputed for a regex. */ @@ -240,14 +271,15 @@ int findnextstr(const char *needle, bool whole_word_only, int modus, /* Consume all waiting keystrokes until a Cancel. */ while (input) { if (func_from_key(&input) == do_cancel) { - statusbar(_("Cancelled")); + if (verbose) + statusbar(_("Cancelled")); enable_waiting(); return -2; } input = parse_kbinput(NULL); } - if (++feedback > 0) + if (++feedback > 0 && verbose) /* TRANSLATORS: This is shown when searching takes * more than half a second. */ statusbar(_("Searching...")); @@ -315,7 +347,8 @@ int findnextstr(const char *needle, bool whole_word_only, int modus, line = openfile->fileage; if (modus == JUSTFIND) { - statusbar(_("Search Wrapped")); + if (verbose) + statusbar(_("Search Wrapped")); /* Delay the "Searching..." message for at least two seconds. */ feedback = -2; } @@ -440,7 +473,7 @@ void go_looking(void) came_full_circle = FALSE; didfind = findnextstr(last_search, FALSE, JUSTFIND, NULL, FALSE, - openfile->current, openfile->current_x); + openfile->current, openfile->current_x, TRUE); /* If we found something, and we're back at the exact same spot * where we started searching, then this is the only occurrence. */ @@ -582,7 +615,7 @@ ssize_t do_replace_loop(const char *needle, bool whole_word_only, while (TRUE) { int i = 0; int result = findnextstr(needle, whole_word_only, modus, - &match_len, skipone, real_current, *real_current_x); + &match_len, skipone, real_current, *real_current_x, TRUE); /* If nothing more was found, or the user aborted, stop looping. */ if (result < 1) { diff --git a/src/text.c b/src/text.c index 34adfeab..f854973f 100644 --- a/src/text.c +++ b/src/text.c @@ -2619,7 +2619,7 @@ bool do_int_spell_fix(const char *word) } /* Find the first whole occurrence of word. */ - result = findnextstr(word, TRUE, INREGION, NULL, FALSE, NULL, 0); + result = findnextstr(word, TRUE, INREGION, NULL, FALSE, NULL, 0, TRUE); /* If the word isn't found, alert the user; if it is, allow correction. */ if (result == 0) { -- 2.11.0