From fce9e72deacd9b9f9979e0f94ac4bd264edfd7a8 Mon Sep 17 00:00:00 2001 From: "sumedh.pendurkar" Date: Tue, 11 Oct 2016 12:44:04 +0530 Subject: [PATCH] fixed indentation issues, added more separators --- src/global.c | 7 +++- src/text.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 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..d423e88 100644 --- a/src/text.c +++ b/src/text.c @@ -46,6 +46,122 @@ static filestruct *jusbottom = NULL; /* Pointer to the end of the justify buffer. */ #endif +#define WORDLEN_MAX 64 +#define WORD_MAX 20 +#define TOFIND_MAX 40 + +/* Complete the common parts found in the results */ +void complete_max(char words[][WORDLEN_MAX], 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 + * also checks for error to avoid segfaults + */ +int copy_arrays(char words[][WORDLEN_MAX], char *check_line, int start, int *len_match) +{ + int i = start; + int j = 0; + while (is_word_mbchar(&check_line[i], FALSE)) { + if (j == WORDLEN_MAX) + return 1; + words[*len_match - 1][j++] = check_line[i]; + i++; + } + words[*len_match - 1][j] = '\0'; + if (*len_match == 1) + return 0; + 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; +} + +/* 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 words + */ +void auto_complete_void(void) +{ + filestruct *check_line = openfile->current; + char to_find[TOFIND_MAX]; + int curpos = openfile->current_x, len_to_find = 0, to_find_start_pos = curpos; + int to_find_end_pos = curpos, i = 0, j = 0, match = 0, curr_line_no = check_line->lineno; + char words[WORD_MAX][WORDLEN_MAX]; + while (to_find_start_pos-- != 0) + if (!is_word_mbchar(&check_line->data[to_find_start_pos], FALSE)) + break; + to_find_start_pos++; + if (to_find_end_pos - to_find_start_pos > TOFIND_MAX) { + statusline(HUSH, "The word to be found cannot be more than %d characters", TOFIND_MAX); + 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, "More than %d words are not supported yet", WORD_MAX); + return; + } + if (copy_arrays(words, check_line->data, i, &match) == 1) + statusline(HUSH, "Words are with more than %d characters are not supported", WORDLEN_MAX); + } + } + check_line = check_line->next; + } + /*No match found*/ + if (match == 0) + statusline(HUSH, "No matches found"); + /*One match found*/ + else if (match == 1) { + statusline(HUSH, "Done"); + do_output(&words[0][len_to_find], strlen(words[0]) - len_to_find, FALSE); + } + /*else find out the common parts and complete it and display the rest part as suggestions*/ + else { + complete_max(words, len_to_find, match); + char *to_print; + if ((to_print = (char *)malloc(65 * match)) == NULL) { + statusline(ALERT, "Internal error"); + return; + } + strcpy(to_print, words[0]); + for (i = 1; i < match;i++) { + strcat(to_print, " "); + strcat(to_print, words[i]); + } + statusline(HUSH, "Possible Completions : %s", to_print); + free(to_print); + } +} + #ifndef NANO_TINY /* Toggle the mark. */ void do_mark(void) -- 2.7.4