about summary refs log tree commit diff stats
path: root/src/LYSearch.c
blob: 752ad488221847013cda3b2bcc50af0c4321cd06 (plain) (blame)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <HTUtils.h>
#include <HTAlert.h>
#include <LYUtils.h>
#include <LYStrings.h>
#include <LYSearch.h>
#include <LYGlobalDefs.h>
#include <GridText.h>

#include <LYLeaks.h>

/*
 *  Search for the target string inside of the links
 *  that are currently displayed on the screen beginning
 *  with the one after the currently selected one.
 *  If found set cur to the new value and return TRUE.
 *  If not found do not reset cur and return FALSE.
 */

PRIVATE int check_for_target_in_links ARGS2(
	int *,		cur,
	char *,		new_target)
{
    int i = *cur + 1;
    OptionType *option;
    char *stars = NULL, *cp;

    if (nlinks == 0)
	return(FALSE);

    for (; i < nlinks; i++) {
	/*
	 *  Search the hightext string, and hightext2 if present,
	 *  taking the case_sensitive setting into account. - FM
	 */
	if (((links[i].hightext != NULL && case_sensitive == TRUE) &&
	     LYno_attr_char_strstr(links[i].hightext, new_target)) ||
	    ((links[i].hightext != NULL && case_sensitive == FALSE) &&
	     LYno_attr_char_case_strstr(links[i].hightext, new_target))) {
	    break;
	}
	if (((links[i].hightext2 != NULL && case_sensitive == TRUE) &&
	     LYno_attr_char_strstr(links[i].hightext2, new_target)) ||
	    ((links[i].hightext2 != NULL && case_sensitive == FALSE) &&
	     LYno_attr_char_case_strstr(links[i].hightext2, new_target))) {
	    break;
	}

	/*
	 *  Search the relevant form fields, taking the
	 *  case_sensitive setting into account. - FM
	 */
	if ((links[i].form != NULL && links[i].form->value != NULL) &&
	    links[i].form->type != F_HIDDEN_TYPE) {
	    if (links[i].form->type == F_PASSWORD_TYPE) {
		/*
		 *  Check the actual, hidden password, and then
		 *  the displayed string. - FM
		 */
		if (((case_sensitive == TRUE) &&
		     LYno_attr_char_strstr(links[i].form->value,
					   new_target)) ||
		    ((case_sensitive == FALSE) &&
		     LYno_attr_char_case_strstr(links[i].form->value,
						new_target))) {
		    break;
		}
		StrAllocCopy(stars, links[i].form->value);
		for (cp = stars; *cp != '\0'; cp++)
		    *cp = '*';
		if (((case_sensitive == TRUE) &&
		     LYno_attr_char_strstr(stars, new_target)) ||
		    ((case_sensitive == FALSE) &&
		     LYno_attr_char_case_strstr(stars, new_target))) {
		    FREE(stars);
		    break;
		}
		FREE(stars);
	    } else if (links[i].form->type == F_OPTION_LIST_TYPE) {
		/*
		 *  Search the option strings that are displayed
		 *  when the popup is invoked. - FM
		 */
		option = links[i].form->select_list;
		while (option != NULL) {
		    if (((option->name != NULL &&
			  case_sensitive == TRUE) &&
			 LYno_attr_char_strstr(option->name, new_target)) ||
			((option->name != NULL &&
			  case_sensitive == FALSE) &&
			 LYno_attr_char_case_strstr(option->name,
						    new_target))) {
			break;
		    }
		    option = option->next;
		}
		if (option != NULL) {
		    break;
		}
	    } else if (links[i].form->type == F_RADIO_TYPE) {
		/*
		 *  Search for checked or unchecked parens. - FM
		 */
		if (links[i].form->num_value) {
		    cp = checked_radio;
		} else {
		    cp = unchecked_radio;
		}
		if (((case_sensitive == TRUE) &&
		     LYno_attr_char_strstr(cp, new_target)) ||
		    ((case_sensitive == FALSE) &&
		     LYno_attr_char_case_strstr(cp, new_target))) {
		    break;
		}
	    } else if (links[i].form->type == F_CHECKBOX_TYPE) {
		/*
		 *  Search for checked or unchecked square brackets. - FM
		 */
		if (links[i].form->num_value) {
		    cp = checked_box;
		} else {
		    cp = unchecked_box;
		}
		if (((case_sensitive == TRUE) &&
		     LYno_attr_char_strstr(cp, new_target)) ||
		    ((case_sensitive == FALSE) &&
		     LYno_attr_char_case_strstr(cp, new_target))) {
		    break;
		}
	    } else {
		/*
		 *  Check the values intended for display.
		 *  May have been found already via the
		 *  hightext search, but make sure here
		 *  that the entire value is searched. - FM
		 */
		if (((case_sensitive == TRUE) &&
		     LYno_attr_char_strstr(links[i].form->value,
					   new_target)) ||
		    ((case_sensitive == FALSE) &&
		     LYno_attr_char_case_strstr(links[i].form->value,
						new_target))) {
		    break;
		}
	    }
	}
    }

    if (i == nlinks)
	return(FALSE);

    *cur = i;
    return(TRUE);
}

/*
 *  Textsearch checks the prev_target variable to see if it is empty.
 *  If it is then it requests a new search string.  It then searches
 *  the current file for the next instance of the search string and
 *  finds the line number that the string is on
 *
 *  This is the primary USER search engine and is case sensitive
 *  or case insensitive depending on the 'case_sensitive' global
 *  variable
 *
 */

PUBLIC BOOL textsearch ARGS3(
	document *,	cur_doc,
	char *,		prev_target,
	BOOL,		next)
{
    int offset;
    int oldcur = cur_doc->link;
    static char prev_target_buffer[512]; /* Search string buffer */
    static BOOL first = TRUE;
    char *cp;
    int ch = 0, recall;
    int QueryTotal;
    int QueryNum;
    BOOLEAN FirstRecall = TRUE;

    /*
     *  Initialize the search string buffer. - FM
     */
    if (first) {
	*prev_target_buffer = '\0';
	first = FALSE;
    }

    QueryTotal = (search_queries ? HTList_count(search_queries) : 0);
    recall = ((QueryTotal >= 1) ? RECALL : NORECALL);
    QueryNum = QueryTotal;

    if (next)
	/*
	 *  LYK_NEXT was pressed, so copy the
	 *  buffer into prev_target. - FM
	 */
	strcpy(prev_target, prev_target_buffer);

    if (strlen(prev_target) == 0 ) {
	/*
	 *  This is a new WHEREIS search ('/'), or
	 *  LYK_NEXT was pressed but there was no
	 *  previous search, so we need to get a
	 *  search string from the user. - FM
	 */
	_statusline(ENTER_WHEREIS_QUERY);

	if ((ch = LYgetstr(prev_target, VISIBLE,
			   sizeof(prev_target_buffer), recall)) < 0) {
	    /*
	     *  User cancelled the search via ^G.
	     *  Restore prev_target and return. - FM
	     */
	    strcpy(prev_target, prev_target_buffer);
	    HTInfoMsg(CANCELLED);
	    return(FALSE);
	}
    }

check_recall:
    if (strlen(prev_target) == 0 &&
	!(recall && (ch == UPARROW || ch == DNARROW))) {
	/*
	 *  No entry.  Simply return, retaining the current buffer.
	 *  Because prev_target is now reset, highlighting of the
	 *  previous search string will no longer occur, but it can
	 *  be used again via LYK_NEXT.   - FM
	 */
	HTInfoMsg(CANCELLED);
	return(FALSE);
    }

    if (recall && ch == UPARROW) {
	if (FirstRecall) {
	    /*
	     *  Use the current string or last query in the list. - FM
	     */
	    FirstRecall = FALSE;
	    if (*prev_target_buffer) {
		for (QueryNum = (QueryTotal - 1); QueryNum > 0; QueryNum--) {
		    if ((cp = (char *)HTList_objectAt(search_queries,
						      QueryNum)) != NULL &&
			!strcmp(prev_target_buffer, cp)) {
			break;
		    }
		 }
	     } else {
		QueryNum = 0;
	     }
	} else {
	    /*
	     *  Go back to the previous query in the list. - FM
	     */
	    QueryNum++;
	}
	if (QueryNum >= QueryTotal)
	    /*
	     *  Roll around to the last query in the list. - FM
	     */
	    QueryNum = 0;
	if ((cp = (char *)HTList_objectAt(search_queries,
					  QueryNum)) != NULL) {
	    strcpy(prev_target, cp);
	    if (*prev_target_buffer &&
		!strcmp(prev_target_buffer, prev_target)) {
		_statusline(EDIT_CURRENT_QUERY);
	    } else if ((*prev_target_buffer && QueryTotal == 2) ||
		       (!(*prev_target_buffer) && QueryTotal == 1)) {
		_statusline(EDIT_THE_PREV_QUERY);
	    } else {
		_statusline(EDIT_A_PREV_QUERY);
	    }
	    if ((ch = LYgetstr(prev_target, VISIBLE,
			       sizeof(prev_target_buffer), recall)) < 0) {
		/*
		 *  User canceled the search via ^G.
		 *  Restore prev_target and return. - FM
		 */
		strcpy(prev_target, prev_target_buffer);
		HTInfoMsg(CANCELLED);
		return(FALSE);
	    }
	    goto check_recall;
	}
    } else if (recall && ch == DNARROW) {
	if (FirstRecall) {
	    /*
	     *  Use the current string or first query in the list. - FM
	     */
	    FirstRecall = FALSE;
	    if (*prev_target_buffer) {
		for (QueryNum = 0; QueryNum < (QueryTotal - 1); QueryNum++) {
		    if ((cp = (char *)HTList_objectAt(search_queries,
						      QueryNum)) != NULL &&
			!strcmp(prev_target_buffer, cp)) {
			break;
		    }
		}
	    } else {
		QueryNum = QueryTotal - 1;
	    }
	} else {
	    /*
	     *  Advance to the next query in the list. - FM
	     */
	    QueryNum--;
	}
	if (QueryNum < 0)
	    /*
	     *  Roll around to the first query in the list. - FM
	     */
	    QueryNum = QueryTotal - 1;
	if ((cp = (char *)HTList_objectAt(search_queries,
					  QueryNum)) != NULL) {
	    strcpy(prev_target, cp);
	    if (*prev_target_buffer &&
		!strcmp(prev_target_buffer, prev_target)) {
		_statusline(EDIT_CURRENT_QUERY);
	    } else if ((*prev_target_buffer && QueryTotal == 2) ||
		       (!(*prev_target_buffer) && QueryTotal == 1)) {
		_statusline(EDIT_THE_PREV_QUERY);
	    } else {
		_statusline(EDIT_A_PREV_QUERY);
	    }
	    if ((ch = LYgetstr(prev_target, VISIBLE,
			       sizeof(prev_target_buffer), recall)) < 0) {
		/*
		 *  User cancelled the search via ^G.
		 *  Restore prev_target and return. - FM
		 */
		strcpy(prev_target, prev_target_buffer);
		HTInfoMsg(CANCELLED);
		return(FALSE);
	    }
	    goto check_recall;
	}
    }
    /*
     *  Replace the search string buffer with the new target. - FM
     */
    strcpy(prev_target_buffer, prev_target);
    HTAddSearchQuery(prev_target_buffer);

    /*
     *  Search the links on the currently displayed page for
     *  the string, starting after the current link. - FM
     */
    if (check_for_target_in_links(&cur_doc->link, prev_target)) {
	/*
	 *  Found in link, changed cur, we're done.
	 */
	highlight(OFF, oldcur, prev_target);
	return(TRUE);
    }

    /*
     *  We'll search the text starting from the
     *  link we are on, or the next page.
     */
    if (nlinks == 0)
	offset = (display_lines - 1);
    else
	offset = links[cur_doc->link].ly - 1;

    /*
     *  Resume search, this time for all text.
     *  Set www_search_result if string found,
     *  and position the hit near top of screen.
     */
    www_user_search((cur_doc->line + offset), cur_doc, prev_target);
    if (cur_doc->link != oldcur) {
	highlight(OFF, oldcur, prev_target);
	return(TRUE);
    }
    return (BOOL) (www_search_result > 0);
}