Re: Backward search in the internal viewer



Pavel Roskin wrote:
On Wed, 23 Oct 2002, Andrew V. Samoilov wrote:

I didn't realize at that time that regex search backwards is hard. Please resend your patch or just apply it if you are certain that it's OK.

I resend this patch, please test it for some days before applying,
I sow some strange effects in the hex editor (tab was not working and highlighting was wrong) but cannot reproduce it and I cannot say it is side effect of this patch. Also is it right to change direction by clicking "Backward" check button and entering empty string as search string?

--
Regards,
Andrew V. Samoilov
--- view.c.cvs	Wed Oct 23 18:30:24 2002
+++ view.c	Wed Oct 23 19:04:47 2002
@@ -1376,11 +1376,32 @@ icase_search_p (WView *view, char *text,
     char *q;
     int lng;
 
+#ifdef	NO_BK_SEARCH
     if ((q = _icase_search (text, data, &lng)) != 0) {
 	view->found_len = lng;
 	view->search_start = q - data - lng;
 	return 1;
     }
+#else
+    int direction = view->direction;
+
+    if (direction == -1)
+	reverse_string (text);
+
+    q = _icase_search (text, data, &lng);
+
+    if (direction == -1)
+	reverse_string (text);
+
+    if (q != 0) {
+	if (direction > 0)
+	    view->search_start = q - data - lng;
+	else
+	    view->search_start = strlen (data) - (q - data);
+	view->found_len = lng;
+	return 1;
+    }
+#endif /* NO_BK_SEARCH */
     return 0;
 }
 
@@ -1410,8 +1431,13 @@ get_line_at (WView *view, unsigned long 
     long i = 0;
     int  prev = 0;
 
+    if (!pos && direction == -1)
+	return 0;
+
     /* skip over all the possible zeros in the file */
     while ((ch = get_byte (view, pos)) == 0) {
+	if (!pos && direction == -1)
+	    return 0;
 	pos += direction; i++;
     }
     *skipped = i;
@@ -1429,21 +1455,27 @@ get_line_at (WView *view, unsigned long 
 	    usable_size = buffer_size - 2;
 	}
 
-	pos += direction; i++;
+	i++;
+	buffer [i] = ch;
 
-	if (ch == '\n' || !ch){
+	if (!pos && direction == -1)
+	    break;
+
+	pos += direction;
+
+	if (ch == '\n' || !ch)
 	    break;
-	}
-	buffer [i] = ch;
     }
+
     if (buffer){
 	buffer [0] = prev;
 	buffer [i] = 0;
-
+#if 0
 	/* If we are searching backwards, reverse the string */
 	if (direction < 0) {
 	    reverse_string (buffer + 1);
 	}
+#endif
     }
 
     *p = pos;
@@ -1502,7 +1534,7 @@ search (WView *view, char *text, int (*s
     if (view->direction == 1){
 	p = found_len ? search_start + 1 : search_start;
     } else {
-	p = (found_len ? search_start : view->last) - 1;
+	p = (found_len && search_start) ? search_start - 1 : search_start;
     }
     beginning = p;
 
@@ -1552,7 +1584,7 @@ search (WView *view, char *text, int (*s
 	if (view->direction == 1)
 	    t += forward_line_start;
 	else
-	    t += reverse_line_start ? reverse_line_start + 3 : 0;
+	    t = reverse_line_start ? reverse_line_start + 2 : 0;
 	view->search_start += t;
 
 	if (t != beginning){
@@ -1582,18 +1614,49 @@ static long
 block_search (WView *view, char *buffer, int len)
 {
     int w = view->widget.cols - view->have_frame + 1;
-
+    int direction = view->direction;
     char *d = buffer, b;
     unsigned long e;
 
     /* clear interrupt status */
     got_interrupt ();
     enable_interrupt_key ();
-    e = view->found_len ? view->search_start + 1 : view->search_start;
+    if (direction == 1)
+	e = view->found_len ? view->search_start + 1 : view->search_start;
+    else
+	e = (view->found_len && view->search_start) ? view->search_start - 1 : view->search_start;
 
     search_update_steps (view);
     update_activate = 0;
-    
+
+    if (direction == -1) {
+
+	for (d += len - 1; ; e--) {
+	    if (e <= update_activate){
+		update_activate -= update_steps;
+		if (verbose){
+		    view_percent (view, e, w, TRUE);
+		    mc_refresh ();
+		}
+		if (got_interrupt ())
+		    break;
+	    }
+	    b = get_byte (view, e);
+
+	    if (*d == b){
+		if (d == buffer){
+		    disable_interrupt_key ();
+		    return e;
+		}
+		d--;
+	    } else {
+		e += buffer + len - 1 - d;
+		d = buffer + len - 1;
+	    }
+	    if (e == 0)
+		break;
+	}
+    } else
     while (e < view->last_byte){
 	if (e >= update_activate){
 	    update_activate += update_steps;
@@ -1953,20 +2016,63 @@ static void
 normal_search (WView *view, int direction)
 {
     static char *old;
-    char *exp = "";
+    char *exp = old ? old : "";
 
-    exp = old ? old : exp;
+    enum {
+	SEARCH_DLG_HEIGHT = 8,
+	SEARCH_DLG_WIDTH = 58
+    };
+
+    static int replace_backwards;
+    int treplace_backwards = replace_backwards;
+
+    static QuickWidget quick_widgets[] = {
+	{quick_button, 6, 10, 5, SEARCH_DLG_HEIGHT, N_("&Cancel"), 0,
+	 B_CANCEL,
+	 0, 0, NULL},
+	{quick_button, 2, 10, 5, SEARCH_DLG_HEIGHT, N_("&Ok"), 0, B_ENTER,
+	 0, 0, NULL},
+	{quick_checkbox, 3, SEARCH_DLG_WIDTH, 4, SEARCH_DLG_HEIGHT,
+	 N_("&Backwards"), 0, 0,
+	 0, 0, NULL},
+	{quick_input, 3, SEARCH_DLG_WIDTH, 3, SEARCH_DLG_HEIGHT, "", 52, 0,
+	 0, 0, N_("Search")},
+	{quick_label, 2, SEARCH_DLG_WIDTH, 2, SEARCH_DLG_HEIGHT,
+	 N_(" Enter search string:"), 0, 0,
+	 0, 0, 0},
+	{0}
+    };
+    static QuickDialog Quick_input = {
+	SEARCH_DLG_WIDTH, SEARCH_DLG_HEIGHT, -1, 0, N_("Search"),
+	"[Input Line Keys]", 0, 0
+    };
 
 #ifdef HAVE_CHARSET
-    if ( *exp )
-	convert_to_display( exp );
+    if (*exp)
+	convert_to_display (exp);
 #endif
+    quick_widgets[2].result = &treplace_backwards;
+    quick_widgets[3].str_result = &exp;
+    quick_widgets[3].text = exp;
+
+    Quick_input.widgets = quick_widgets;
+    if (quick_dialog (&Quick_input) == B_CANCEL) {
+#ifdef HAVE_CHARSET
+	if (*exp)
+	    convert_from_input (old);
+#endif
+	return;
+    }
+    replace_backwards = treplace_backwards;
+    direction = (replace_backwards) ? -1 : 1;
+
+    if (old && *old)
+	convert_from_input (old);
 
-    exp = input_dialog (_("Search"), _(" Enter search string:"), exp);
-    if ((!exp)){
+    if ((!exp)) {
 	return;
     }
-    if ((!*exp)){
+    if ((!*exp)) {
 	g_free (exp);
 	return;
     }
@@ -1975,7 +2081,7 @@ normal_search (WView *view, int directio
     old = exp;
 
 #ifdef HAVE_CHARSET
-    convert_from_input( exp );
+    convert_from_input (exp);
 #endif
 
     view->direction = direction;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]