From 2edbaf1134fe234513a23b687071714755f52cf1 Mon Sep 17 00:00:00 2001 From: "sumedh.pendurkar" Date: Mon, 17 Oct 2016 19:15:36 +0530 Subject: [PATCH] autocomplete:cycle through the words using ^] --- src/global.c | 7 ++- src/text.c | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+), 1 deletion(-) diff --git a/src/global.c b/src/global.c index 029b68b..9bcf57f 100644 --- a/src/global.c +++ b/src/global.c @@ -246,6 +246,7 @@ size_t length_of_list(int menu) #define TOGETHER FALSE /* Just throw this here. */ +void auto_complete_void(void); void case_sens_void(void) { } @@ -483,6 +484,7 @@ const char *unjust_tag = N_("Unjustify"); /* Initialize the list of functions and the list of shortcuts. */ void shortcut_init(void) { + const char *auto_complete_tag = N_("Auto Complete"); const char *read_file_tag = N_("Read File"); const char *whereis_tag = N_("Where Is"); const char *replace_tag = N_("Replace"); @@ -526,6 +528,7 @@ void shortcut_init(void) const char *nano_browser_lefthand_msg = N_("Go to lefthand column"); const char *nano_browser_righthand_msg = N_("Go to righthand column"); #endif + const char *nano_auto_complete_msg = ("Look up for the current word in the file"); const char *nano_prevpage_msg = N_("Go one screenful up"); const char *nano_nextpage_msg = N_("Go one screenful down"); const char *nano_cut_msg = @@ -721,7 +724,8 @@ void shortcut_init(void) add_to_funcs(goto_dir_void, MBROWSER, N_("Go To Dir"), IFSCHELP(nano_gotodir_msg), BLANKAFTER, VIEW); #endif - + add_to_funcs(auto_complete_void, MMAIN, auto_complete_tag, + IFSCHELP(nano_auto_complete_msg), TOGETHER, VIEW); #ifndef DISABLE_HELP /* The description ("x") and blank_after (0) are irrelevant, * because the help viewer does not have a help text. */ @@ -1064,6 +1068,7 @@ void shortcut_init(void) add_to_sclist(MMAIN, "^\\", do_replace, 0); add_to_sclist(MMAIN, "M-R", do_replace, 0); add_to_sclist(MMAIN, "F14", do_replace, 0); + add_to_sclist(MMAIN, "^]", auto_complete_void, 0); add_to_sclist(MMOST, "^K", do_cut_text_void, 0); add_to_sclist(MMOST, "F9", do_cut_text_void, 0); add_to_sclist(MMAIN, "^U", do_uncut_text, 0); diff --git a/src/text.c b/src/text.c index d0d1955..ecdb4a4 100644 --- a/src/text.c +++ b/src/text.c @@ -46,6 +46,171 @@ static filestruct *jusbottom = NULL; /* Pointer to the end of the justify buffer. */ #endif +#define WORD_MAX 30 + +/* Complete the common parts found in the results */ +void complete_max(char **words, int start, int no_of_words) +{ + int i = start, j; + + while (1) { + for (j = 0; j < no_of_words - 1; j++) { + if (words[j][i] != words[j + 1][i]) + return; + } + do_output(&words[j][i], 1, FALSE); + i++; + } +} + +/* Copy the word from the line which contains the pattern + * Removes duplicates from the words + * returns -1 for error + * else returns string length of the word + */ +int copy_arrays(char **words, char *check_line, int start, int *len_match) +{ + int i = start; + int len_of_word = 0; + + while (is_word_mbchar(&check_line[i], FALSE)) { + i++; + len_of_word++; + } + words[*len_match - 1] = (char *)malloc((len_of_word + 1) * sizeof(char)); + if (words[*len_match - 1] == NULL) + return 1; + i = start; + int j = 0; + + while (is_word_mbchar(&check_line[i], FALSE)) { + words[*len_match - 1][j++] = check_line[i]; + i++; + } + words[*len_match - 1][j] = '\0'; + if (*len_match == 1) + return len_of_word; + for (i = *len_match - 2; i >= 0; i--) + if (strcmp(words[i], words[*len_match - 1]) == 0) + break; + if (i != -1) { + (*len_match)--; + return 0; + } + return len_of_word; +} + +/* This function finds out the current word which the user is typing + * searches for the word in the entire file + * replaces it, if only 1 match is found, else it suggests the possible completions + */ +void auto_complete_void(void) +{ + filestruct *check_line = openfile->current; + char *to_find; + int curpos = openfile->current_x, len_to_find = 0; + int to_find_start_pos = curpos, len_of_words = 0; + int to_find_end_pos = curpos, i = 0, j = 0, match = 0, curr_line_no = check_line->lineno; + char *words[WORD_MAX]; + + while (to_find_start_pos-- != 0) + if (!is_word_mbchar(&check_line->data[to_find_start_pos], FALSE)) + break; + to_find_start_pos++; + to_find = (char *)malloc((to_find_end_pos - to_find_start_pos + 1) * sizeof(char)); + if (to_find == NULL) { + statusline(HUSH, "Insufficient Memory"); + return; + } + /*copy what is to be searched in to find*/ + while (to_find_start_pos + i < curpos) + to_find[len_to_find++] = check_line->data[to_find_start_pos + i++]; + to_find[len_to_find] = '\0'; + if (len_to_find == 0) + return; + +/*Finding the current word in the entire file*/ + check_line = openfile->fileage; + while (check_line != NULL) { + int len_data = strlen(check_line->data); + + for (i = 0; i < len_data; i++) { + for (j = 0; (i == 0 || !is_word_mbchar(&check_line->data[i - 1], FALSE)) && j < len_to_find; j++) + if (to_find[j] != check_line->data[i + j] || + (check_line->lineno == curr_line_no && i == to_find_start_pos)) + break; + if (j == len_to_find && (i + j != len_data && is_word_mbchar(&check_line->data[i + j], FALSE))) { + match++; + if (match >= WORD_MAX) { + statusline(HUSH, "Please type more characters for suggestions"); + return; + } + int tmp = copy_arrays(words, check_line->data, i , &match); + + if (tmp == -1) { + statusline(HUSH, "Insufficient Memory"); + return; + } + /*Add the length of word to len_of_words require to make the final string */ + else + len_of_words+=tmp; + } + } + check_line = check_line->next; + } + /*No match found*/ + if (match == 0) + statusline(HUSH, "No matches found"); + /*match found*/ + else { + int val = 0; + int iterator = 0; + bool not_found = FALSE; + while (1) { + if (!not_found) { + val = strlen(words[iterator]) - len_to_find; + do_output(&words[iterator][len_to_find], val, FALSE); + } + edit_refresh(); + /*read a character if it is "^]" print the next element after deleting the existing suggestion*/ + int t = get_kbinput(edit); + int i = 0; + if (t == ']' - 64) + for (; i < val && !not_found; i++) + do_backspace(); + else { + /*user didn't hit auto-complete so put that key back in keystroke buffer */ + unget_input(&t, 1); + do_input(TRUE); + blank_statusbar(); + wnoutrefresh(bottomwin); + break; + } + if (!not_found) + iterator++; + /*end of words, start from the first element*/ + if (iterator == match) { + statusline(HUSH, "No match found, starting from first element"); + curs_set(1); + edit_refresh(); + iterator = 0; + not_found = TRUE; + } + else { + if (not_found == TRUE) { + blank_statusbar(); + wnoutrefresh(bottomwin); + } + not_found = FALSE; + } + } + } + free(to_find); + /*free words*/ + for (i = 0; i < match; i++) + free(words[i]); +} + #ifndef NANO_TINY /* Toggle the mark. */ void do_mark(void) -- 2.7.4