-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsearch.c
132 lines (110 loc) · 3.07 KB
/
search.c
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* search.c, femto, Hugh Barney, Public Domain, 2017
* Simple forward and reverse search.
*/
#include "header.h"
void search()
{
int cpos = 0;
int c;
point_t o_point = curbp->b_point;
point_t found;
searchtext[0] = '\0';
display_prompt_and_response(m_sprompt, searchtext);
cpos = strlen(searchtext);
for (;;) {
c = getch();
/* ignore control keys other than C-g, backspace, CR, C-s, C-R, ESC */
if (c < 32 && c != 07 && c != 0x08 && c != 0x13 && c != 0x12 && c != 0x1b)
continue;
switch(c) {
case 0x1b: /* esc */
searchtext[cpos] = '\0';
flushinp(); /* discard any escape sequence without writing in buffer */
return;
case 0x07: /* ctrl-g */
curbp->b_point = o_point;
return;
case 0x13: /* ctrl-s, do the search */
found = search_forward(searchtext);
display_search_result(found, FWD_SEARCH, m_sprompt, searchtext);
break;
case 0x12: /* ctrl-r, do the search */
found = search_backwards(searchtext);
display_search_result(found, REV_SEARCH, m_sprompt, searchtext);
break;
case 0x7f: /* del, erase */
case 0x08: /* backspace */
if (cpos == 0)
continue;
searchtext[--cpos] = '\0';
display_prompt_and_response(m_sprompt, searchtext);
break;
default:
if (cpos < STRBUF_M - 1) {
searchtext[cpos++] = c;
searchtext[cpos] = '\0';
display_prompt_and_response(m_sprompt, searchtext);
}
break;
}
}
}
void move_to_search_result(point_t found)
{
if (found != -1) {
curbp->b_point = found;
display(curwp, TRUE);
}
}
void display_search_result(point_t found, int dir, char *prompt, char *search)
{
if (found != -1 ) {
curbp->b_point = found;
msg("%s%s",prompt, search);
display(curwp, TRUE);
} else {
msg("Failing %s%s",prompt, search);
dispmsg();
curbp->b_point = (dir == FWD_SEARCH ? 0 : pos(curbp, curbp->b_ebuf));
}
}
point_t search_forward(char *stext)
{
point_t end_p = pos(curbp, curbp->b_ebuf);
point_t p,pp;
char* s;
if (0 == strlen(stext))
return curbp->b_point;
for (p=curbp->b_point; p < end_p; p++) {
for (s=stext, pp=p; *s == *(ptr(curbp, pp)) && *s !='\0' && pp < end_p; s++, pp++)
;
if (*s == '\0')
return pp;
}
return -1;
}
point_t search_backwards(char *stext)
{
point_t p,pp;
char* s;
if (0 == strlen(stext))
return curbp->b_point;
for (p=curbp->b_point; p > 0; p--) {
for (s=stext, pp=p; *s == *(ptr(curbp, pp)) && *s != '\0' && pp > 0; s++, pp++)
;
if (*s == '\0') {
if (p > 0)
p--;
return p;
}
}
return -1;
}
/*
* Local Variables:
* c-file-style: "k&r"
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/