From 5ab3ba58c944e45f7f5f790c7bd836f02bb18fb5 Mon Sep 17 00:00:00 2001
From: Slava Zanko <slavazanko@gmail.com>
Date: Fri, 29 May 2009 17:42:26 +0300
Subject: [PATCH] Fixed autocompletion in editor

Signed-off-by: Slava Zanko <slavazanko@gmail.com>
---
 edit/editcmd.c | 57 ++++++++++++++++++++++++++++----------------------
 1 file changed, 32 insertions(+), 25 deletions(-)

diff --git a/edit/editcmd.c b/edit/editcmd.c
index 17d9e183b..d81938b56 100644
--- a/edit/editcmd.c
+++ b/edit/editcmd.c
@@ -2278,52 +2278,59 @@ edit_collect_completions (WEdit *edit, long start, int word_len,
 			  char *match_expr, struct selection *compl,
 			  int *num)
 {
-    int len = 0, max_len = 0, i, skip;
-    unsigned char *bufpos;
+    int max_len = 0, i, skip;
+    gsize len = 0;
+    GString *temp;
+    mc_search_t *srch;
+
+    srch = mc_search_new(match_expr, -1);
+    if (srch == NULL)
+        return 0;
+
+    srch->search_type = MC_SEARCH_T_REGEX;
+    srch->is_case_sentitive = TRUE;
+    srch->search_fn = edit_search_cmd_callback;
 
-(void) match_expr;
     /* collect max MAX_WORD_COMPLETIONS completions */
     while (*num < MAX_WORD_COMPLETIONS) {
 	/* get next match */
-/*
-	start =
-	    edit_find (start - 1, (unsigned char *) match_expr, &len,
-		       edit->last_byte, edit_get_byte_ptr, (void *) edit, 0);
-*/
-start = -1;
-	/* not matched */
-	if (start < 0)
+	if (mc_search_run (srch, (void *) edit, start+1, edit->last_byte, &len) == FALSE)
 	    break;
+	start = srch->normal_offset;
 
 	/* add matched completion if not yet added */
-	bufpos =
-	    &edit->
-	    buffers1[start >> S_EDIT_BUF_SIZE][start & M_EDIT_BUF_SIZE];
+	temp = g_string_new("");
+	for (i = 0; i < len; i++)
+	    g_string_append_c (temp, edit_get_byte(edit, start+i));
+
 	skip = 0;
+
 	for (i = 0; i < *num; i++) {
 	    if (strncmp
-		((char *) &compl[i].text[word_len],
-		 (char *) &bufpos[word_len], max (len,
-						  compl[i].len) -
-		 word_len) == 0) {
+		    (
+			(char *) &compl[i].text[word_len],
+			(char *) &temp->str[word_len],
+			max (len, compl[i].len) - word_len
+		    ) == 0) {
 		skip = 1;
 		break;		/* skip it, already added */
 	    }
 	}
-	if (skip)
+	if (skip) {
+	    g_string_free(temp, TRUE);
 	    continue;
+	}
 
-	compl[*num].text = g_malloc (len + 1);
+	compl[*num].text = temp->str;
 	compl[*num].len = len;
-	for (i = 0; i < len; i++)
-	    compl[*num].text[i] = *(bufpos + i);
-	compl[*num].text[i] = '\0';
 	(*num)++;
+	g_string_free(temp, FALSE);
 
 	/* note the maximal length needed for the completion dialog */
 	if (len > max_len)
 	    max_len = len;
     }
+    mc_search_free(srch);
     return max_len;
 }
 
@@ -2358,9 +2365,9 @@ edit_complete_word_cmd (WEdit *edit)
     edit_set_search_parameters (edit, 0, 1, 1);
 
     /* collect the possible completions              */
-    /* start search from curs1 down to begin of file */
+    /* start search from begin to end of file */
     max_len =
-	edit_collect_completions (edit, word_start, word_len, match_expr,
+	edit_collect_completions (edit, 0, word_len, match_expr,
 				  (struct selection *) &compl, &num_compl);
 
     if (num_compl > 0) {