summaryrefslogtreecommitdiff
path: root/alpine/backwards-search.patch
blob: 8926609766b591a50d7229de7449262830ef1939 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
diff --git a/alpine/mailindx.c b/alpine/mailindx.c
index 18583c3..f1dd874 100644
--- a/alpine/mailindx.c
+++ b/alpine/mailindx.c
@@ -2954,19 +2954,21 @@ away.
 void
 index_search(struct pine *state, MAILSTREAM *stream, int command_line, MSGNO_S *msgmap)
 {
-    int         rc, select_all = 0, flags, prefetch, we_turned_on = 0;
+    int         rc, select_all = 0, flags, prefetch, searchbound, otherbound, offset = 0, we_turned_on = 0;
     long        i, sorted_msg, selected = 0L;
     char        prompt[MAX_SEARCH+50], new_string[MAX_SEARCH+1];
     char        buf[MAX_SCREEN_COLS+1], *p;
     HelpType	help;
     char        search_string[MAX_SEARCH+1];
     ICE_S      *ice, *ic;
+    static int direction = 1;
     static HISTORY_S *history = NULL;
     static ESCKEY_S header_search_key[] = { {0, 0, NULL, NULL },
 					    {ctrl('Y'), 10, "^Y", N_("First Msg")},
 					    {ctrl('V'), 11, "^V", N_("Last Msg")},
 					    {KEY_UP,    30, "", ""},
 					    {KEY_DOWN,  31, "", ""},
+					    {ctrl('W'), 32, "^W", N_("Direction")},
 					    {-1, 0, NULL, NULL} };
 #define KU_IS (3)	/* index of KEY_UP */
 #define PREFETCH_THIS_MANY_LINES (50)
@@ -2995,7 +2997,8 @@ index_search(struct pine *state, MAILSTREAM *stream, int command_line, MSGNO_S *
     new_string[0] = '\0';
 
     while(1) {
-	snprintf(prompt, sizeof(prompt), _("Word to search for [%s] : "), search_string);
+	snprintf(prompt, sizeof(prompt), _("Word to search %sfor [%s] : "),
+		direction == -1 ? _("(backwards) ") : "", search_string);
 
 	if(F_ON(F_ENABLE_AGG_OPS, ps_global)){
 	    header_search_key[0].ch    = ctrl('X');
@@ -3092,6 +3095,10 @@ index_search(struct pine *state, MAILSTREAM *stream, int command_line, MSGNO_S *
 
 	    continue;
 	}
+	else if(rc == 32){
+	    direction *= -1;
+	    continue;
+	}
 
         if(rc != 4){			/* 4 is redraw */
 	    save_hist(history, new_string, 0, NULL);
@@ -3115,9 +3122,18 @@ index_search(struct pine *state, MAILSTREAM *stream, int command_line, MSGNO_S *
     we_turned_on = intr_handling_on();
 
     prefetch = 0;
-    for(i = sorted_msg + ((select_all)?0:1);
-	i <= mn_get_total(msgmap) && !ps_global->intr_pending;
-	i++){
+    if (direction == -1) {
+	searchbound = -1;
+	otherbound = mn_get_total(msgmap);
+	offset = 2;
+    } else {
+	searchbound = mn_get_total(msgmap);
+	otherbound = 1;
+    }
+
+    for(i = sorted_msg + ((select_all)?0:1) - offset;
+	i * direction <= searchbound && !ps_global->intr_pending;
+	i += direction){
       if(msgline_hidden(stream, msgmap, i, 0))
 	continue;
 
@@ -3139,8 +3155,11 @@ index_search(struct pine *state, MAILSTREAM *stream, int command_line, MSGNO_S *
     }
 
     prefetch = 0;
-    if(i > mn_get_total(msgmap)){
-      for(i = 1; i < sorted_msg && !ps_global->intr_pending; i++){
+    if(i * direction > searchbound){
+      for(i = otherbound;
+	i * direction < sorted_msg * direction && !ps_global->intr_pending;
+	i += direction ){
+
         if(msgline_hidden(stream, msgmap, i, 0))
 	  continue;
 
@@ -3194,7 +3213,7 @@ index_search(struct pine *state, MAILSTREAM *stream, int command_line, MSGNO_S *
     }
     else if(selected){
 	q_status_message1(SM_ORDER, 0, 3, _("Word found%s"),
-			  (i < sorted_msg) ? _(". Search wrapped to beginning") :
+			  (i * direction < sorted_msg * direction) ? _(". Search wrapped to beginning") :
 			   (i == sorted_msg) ? _(". Current line contains only match") : "");
 	mn_set_cur(msgmap, i);
     }