Actually hook powernow.4 into the build.
[dragonfly.git] / contrib / less / search.c
blob90db0eb68aa40ea12c892191f7ce15fdfdea0112
1 /*
2 * Copyright (C) 1984-2009 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
9 */
13 * Routines to search a file for a pattern.
16 #include "less.h"
17 #include "pattern.h"
18 #include "position.h"
19 #include "charset.h"
21 #define MINPOS(a,b) (((a) < (b)) ? (a) : (b))
22 #define MAXPOS(a,b) (((a) > (b)) ? (a) : (b))
24 extern int sigs;
25 extern int how_search;
26 extern int caseless;
27 extern int linenums;
28 extern int sc_height;
29 extern int jump_sline;
30 extern int bs_mode;
31 extern int ctldisp;
32 extern int status_col;
33 extern void * constant ml_search;
34 extern POSITION start_attnpos;
35 extern POSITION end_attnpos;
36 extern int utf_mode;
37 extern int screen_trashed;
38 #if HILITE_SEARCH
39 extern int hilite_search;
40 extern int size_linebuf;
41 extern int squished;
42 extern int can_goto_line;
43 static int hide_hilite;
44 static POSITION prep_startpos;
45 static POSITION prep_endpos;
46 static int is_caseless;
47 static int is_ucase_pattern;
49 struct hilite
51 struct hilite *hl_next;
52 POSITION hl_startpos;
53 POSITION hl_endpos;
55 static struct hilite hilite_anchor = { NULL, NULL_POSITION, NULL_POSITION };
56 static struct hilite filter_anchor = { NULL, NULL_POSITION, NULL_POSITION };
57 #define hl_first hl_next
58 #endif
61 * These are the static variables that represent the "remembered"
62 * search pattern and filter pattern.
64 struct pattern_info {
65 DEFINE_PATTERN(compiled);
66 char* text;
67 int search_type;
70 static struct pattern_info search_info;
71 static struct pattern_info filter_info;
74 * Compile and save a search pattern.
76 static int
77 set_pattern(info, pattern, search_type)
78 struct pattern_info *info;
79 char *pattern;
80 int search_type;
82 if (pattern == NULL)
83 CLEAR_PATTERN(search_info.compiled);
84 else if (compile_pattern(pattern, search_type, &info->compiled) < 0)
85 return -1;
86 /* Pattern compiled successfully; save the text too. */
87 if (info->text != NULL)
88 free(info->text);
89 info->text = NULL;
90 if (pattern != NULL)
92 info->text = (char *) ecalloc(1, strlen(pattern)+1);
93 strcpy(info->text, pattern);
95 info->search_type = search_type;
96 return 0;
100 * Discard a saved pattern.
102 static void
103 clear_pattern(info)
104 struct pattern_info *info;
106 if (info->text != NULL)
107 free(info->text);
108 info->text = NULL;
109 uncompile_pattern(&info->compiled);
113 * Initialize saved pattern to nothing.
115 static void
116 init_pattern(info)
117 struct pattern_info *info;
119 CLEAR_PATTERN(info->compiled);
120 info->text = NULL;
121 info->search_type = 0;
125 * Initialize search variables.
127 public void
128 init_search()
130 init_pattern(&search_info);
131 init_pattern(&filter_info);
135 * Determine which text conversions to perform before pattern matching.
137 static int
138 get_cvt_ops()
140 int ops = 0;
141 if (is_caseless || bs_mode == BS_SPECIAL)
143 if (is_caseless)
144 ops |= CVT_TO_LC;
145 if (bs_mode == BS_SPECIAL)
146 ops |= CVT_BS;
147 if (bs_mode != BS_CONTROL)
148 ops |= CVT_CRLF;
149 } else if (bs_mode != BS_CONTROL)
151 ops |= CVT_CRLF;
153 if (ctldisp == OPT_ONPLUS)
154 ops |= CVT_ANSI;
155 return (ops);
159 * Are there any uppercase letters in this string?
161 static int
162 is_ucase(str)
163 char *str;
165 char *str_end = str + strlen(str);
166 LWCHAR ch;
168 while (str < str_end)
170 ch = step_char(&str, +1, str_end);
171 if (IS_UPPER(ch))
172 return (1);
174 return (0);
178 * Is there a previous (remembered) search pattern?
180 static int
181 prev_pattern(info)
182 struct pattern_info *info;
184 if (info->search_type & SRCH_NO_REGEX)
185 return (info->text != NULL);
186 return (!is_null_pattern(info->compiled));
189 #if HILITE_SEARCH
191 * Repaint the hilites currently displayed on the screen.
192 * Repaint each line which contains highlighted text.
193 * If on==0, force all hilites off.
195 public void
196 repaint_hilite(on)
197 int on;
199 int slinenum;
200 POSITION pos;
201 POSITION epos;
202 int save_hide_hilite;
204 if (squished)
205 repaint();
207 save_hide_hilite = hide_hilite;
208 if (!on)
210 if (hide_hilite)
211 return;
212 hide_hilite = 1;
215 if (!can_goto_line)
217 repaint();
218 hide_hilite = save_hide_hilite;
219 return;
222 for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++)
224 pos = position(slinenum);
225 if (pos == NULL_POSITION)
226 continue;
227 epos = position(slinenum+1);
228 (void) forw_line(pos);
229 goto_line(slinenum);
230 put_line();
232 lower_left(); // if !oldbot
233 hide_hilite = save_hide_hilite;
237 * Clear the attn hilite.
239 public void
240 clear_attn()
242 int slinenum;
243 POSITION old_start_attnpos;
244 POSITION old_end_attnpos;
245 POSITION pos;
246 POSITION epos;
247 int moved = 0;
249 if (start_attnpos == NULL_POSITION)
250 return;
251 old_start_attnpos = start_attnpos;
252 old_end_attnpos = end_attnpos;
253 start_attnpos = end_attnpos = NULL_POSITION;
255 if (!can_goto_line)
257 repaint();
258 return;
260 if (squished)
261 repaint();
263 for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++)
265 pos = position(slinenum);
266 if (pos == NULL_POSITION)
267 continue;
268 epos = position(slinenum+1);
269 if (pos < old_end_attnpos &&
270 (epos == NULL_POSITION || epos > old_start_attnpos))
272 (void) forw_line(pos);
273 goto_line(slinenum);
274 put_line();
275 moved = 1;
278 if (moved)
279 lower_left();
281 #endif
284 * Hide search string highlighting.
286 public void
287 undo_search()
289 if (!prev_pattern(&search_info))
291 error("No previous regular expression", NULL_PARG);
292 return;
294 #if HILITE_SEARCH
295 hide_hilite = !hide_hilite;
296 repaint_hilite(1);
297 #endif
300 #if HILITE_SEARCH
302 * Clear the hilite list.
304 public void
305 clr_hlist(anchor)
306 struct hilite *anchor;
308 struct hilite *hl;
309 struct hilite *nexthl;
311 for (hl = anchor->hl_first; hl != NULL; hl = nexthl)
313 nexthl = hl->hl_next;
314 free((void*)hl);
316 anchor->hl_first = NULL;
317 prep_startpos = prep_endpos = NULL_POSITION;
320 public void
321 clr_hilite()
323 clr_hlist(&hilite_anchor);
326 public void
327 clr_filter()
329 clr_hlist(&filter_anchor);
333 * Should any characters in a specified range be highlighted?
335 static int
336 is_hilited_range(pos, epos)
337 POSITION pos;
338 POSITION epos;
340 struct hilite *hl;
343 * Look at each highlight and see if any part of it falls in the range.
345 for (hl = hilite_anchor.hl_first; hl != NULL; hl = hl->hl_next)
347 if (hl->hl_endpos > pos &&
348 (epos == NULL_POSITION || epos > hl->hl_startpos))
349 return (1);
351 return (0);
355 * Is a line "filtered" -- that is, should it be hidden?
357 public int
358 is_filtered(pos)
359 POSITION pos;
361 struct hilite *hl;
363 if (ch_getflags() & CH_HELPFILE)
364 return (0);
367 * Look at each filter and see if the start position
368 * equals the start position of the line.
370 for (hl = filter_anchor.hl_first; hl != NULL; hl = hl->hl_next)
372 if (hl->hl_startpos == pos)
373 return (1);
375 return (0);
379 * Should any characters in a specified range be highlighted?
380 * If nohide is nonzero, don't consider hide_hilite.
382 public int
383 is_hilited(pos, epos, nohide, p_matches)
384 POSITION pos;
385 POSITION epos;
386 int nohide;
387 int *p_matches;
389 int match;
391 if (p_matches != NULL)
392 *p_matches = 0;
394 if (!status_col &&
395 start_attnpos != NULL_POSITION &&
396 pos < end_attnpos &&
397 (epos == NULL_POSITION || epos > start_attnpos))
399 * The attn line overlaps this range.
401 return (1);
403 match = is_hilited_range(pos, epos);
404 if (!match)
405 return (0);
407 if (p_matches != NULL)
409 * Report matches, even if we're hiding highlights.
411 *p_matches = 1;
413 if (hilite_search == 0)
415 * Not doing highlighting.
417 return (0);
419 if (!nohide && hide_hilite)
421 * Highlighting is hidden.
423 return (0);
425 return (1);
429 * Add a new hilite to a hilite list.
431 static void
432 add_hilite(anchor, hl)
433 struct hilite *anchor;
434 struct hilite *hl;
436 struct hilite *ihl;
439 * Hilites are sorted in the list; find where new one belongs.
440 * Insert new one after ihl.
442 for (ihl = anchor; ihl->hl_next != NULL; ihl = ihl->hl_next)
444 if (ihl->hl_next->hl_startpos > hl->hl_startpos)
445 break;
449 * Truncate hilite so it doesn't overlap any existing ones
450 * above and below it.
452 if (ihl != anchor)
453 hl->hl_startpos = MAXPOS(hl->hl_startpos, ihl->hl_endpos);
454 if (ihl->hl_next != NULL)
455 hl->hl_endpos = MINPOS(hl->hl_endpos, ihl->hl_next->hl_startpos);
456 if (hl->hl_startpos >= hl->hl_endpos)
459 * Hilite was truncated out of existence.
461 free(hl);
462 return;
464 hl->hl_next = ihl->hl_next;
465 ihl->hl_next = hl;
469 * Make a hilite for each string in a physical line which matches
470 * the current pattern.
471 * sp,ep delimit the first match already found.
473 static void
474 hilite_line(linepos, line, line_len, chpos, sp, ep, cvt_ops)
475 POSITION linepos;
476 char *line;
477 int line_len;
478 int *chpos;
479 char *sp;
480 char *ep;
481 int cvt_ops;
483 char *searchp;
484 char *line_end = line + line_len;
485 struct hilite *hl;
487 if (sp == NULL || ep == NULL)
488 return;
490 * sp and ep delimit the first match in the line.
491 * Mark the corresponding file positions, then
492 * look for further matches and mark them.
493 * {{ This technique, of calling match_pattern on subsequent
494 * substrings of the line, may mark more than is correct
495 * if the pattern starts with "^". This bug is fixed
496 * for those regex functions that accept a notbol parameter
497 * (currently POSIX, PCRE and V8-with-regexec2). }}
499 searchp = line;
500 do {
501 if (ep > sp)
503 hl = (struct hilite *) ecalloc(1, sizeof(struct hilite));
504 hl->hl_startpos = linepos + chpos[sp-line];
505 hl->hl_endpos = linepos + chpos[ep-line];
506 add_hilite(&hilite_anchor, hl);
509 * If we matched more than zero characters,
510 * move to the first char after the string we matched.
511 * If we matched zero, just move to the next char.
513 if (ep > searchp)
514 searchp = ep;
515 else if (searchp != line_end)
516 searchp++;
517 else /* end of line */
518 break;
519 } while (match_pattern(search_info.compiled, search_info.text,
520 searchp, line_end - searchp, &sp, &ep, 1, search_info.search_type));
522 #endif
525 * Change the caseless-ness of searches.
526 * Updates the internal search state to reflect a change in the -i flag.
528 public void
529 chg_caseless()
531 if (!is_ucase_pattern)
533 * Pattern did not have uppercase.
534 * Just set the search caselessness to the global caselessness.
536 is_caseless = caseless;
537 else
539 * Pattern did have uppercase.
540 * Discard the pattern; we can't change search caselessness now.
542 clear_pattern(&search_info);
545 #if HILITE_SEARCH
547 * Find matching text which is currently on screen and highlight it.
549 static void
550 hilite_screen()
552 struct scrpos scrpos;
554 get_scrpos(&scrpos);
555 if (scrpos.pos == NULL_POSITION)
556 return;
557 prep_hilite(scrpos.pos, position(BOTTOM_PLUS_ONE), -1);
558 repaint_hilite(1);
562 * Change highlighting parameters.
564 public void
565 chg_hilite()
568 * Erase any highlights currently on screen.
570 clr_hilite();
571 hide_hilite = 0;
573 if (hilite_search == OPT_ONPLUS)
575 * Display highlights.
577 hilite_screen();
579 #endif
582 * Figure out where to start a search.
584 static POSITION
585 search_pos(search_type)
586 int search_type;
588 POSITION pos;
589 int linenum;
591 if (empty_screen())
594 * Start at the beginning (or end) of the file.
595 * The empty_screen() case is mainly for
596 * command line initiated searches;
597 * for example, "+/xyz" on the command line.
598 * Also for multi-file (SRCH_PAST_EOF) searches.
600 if (search_type & SRCH_FORW)
602 return (ch_zero());
603 } else
605 pos = ch_length();
606 if (pos == NULL_POSITION)
608 (void) ch_end_seek();
609 pos = ch_length();
611 return (pos);
614 if (how_search)
617 * Search does not include current screen.
619 if (search_type & SRCH_FORW)
620 linenum = BOTTOM_PLUS_ONE;
621 else
622 linenum = TOP;
623 pos = position(linenum);
624 } else
627 * Search includes current screen.
628 * It starts at the jump target (if searching backwards),
629 * or at the jump target plus one (if forwards).
631 linenum = adjsline(jump_sline);
632 pos = position(linenum);
633 if (search_type & SRCH_FORW)
635 pos = forw_raw_line(pos, (char **)NULL, (int *)NULL);
636 while (pos == NULL_POSITION)
638 if (++linenum >= sc_height)
639 break;
640 pos = position(linenum);
642 } else
644 while (pos == NULL_POSITION)
646 if (--linenum < 0)
647 break;
648 pos = position(linenum);
652 return (pos);
656 * Search a subset of the file, specified by start/end position.
658 static int
659 search_range(pos, endpos, search_type, matches, maxlines, plinepos, pendpos)
660 POSITION pos;
661 POSITION endpos;
662 int search_type;
663 int matches;
664 int maxlines;
665 POSITION *plinepos;
666 POSITION *pendpos;
668 char *line;
669 char *cline;
670 int line_len;
671 LINENUM linenum;
672 char *sp, *ep;
673 int line_match;
674 int cvt_ops;
675 int cvt_len;
676 int *chpos;
677 POSITION linepos, oldpos;
679 linenum = find_linenum(pos);
680 oldpos = pos;
681 for (;;)
684 * Get lines until we find a matching one or until
685 * we hit end-of-file (or beginning-of-file if we're
686 * going backwards), or until we hit the end position.
688 if (ABORT_SIGS())
691 * A signal aborts the search.
693 return (-1);
696 if ((endpos != NULL_POSITION && pos >= endpos) || maxlines == 0)
699 * Reached end position without a match.
701 if (pendpos != NULL)
702 *pendpos = pos;
703 return (matches);
705 if (maxlines > 0)
706 maxlines--;
708 if (search_type & SRCH_FORW)
711 * Read the next line, and save the
712 * starting position of that line in linepos.
714 linepos = pos;
715 pos = forw_raw_line(pos, &line, &line_len);
716 if (linenum != 0)
717 linenum++;
718 } else
721 * Read the previous line and save the
722 * starting position of that line in linepos.
724 pos = back_raw_line(pos, &line, &line_len);
725 linepos = pos;
726 if (linenum != 0)
727 linenum--;
730 if (pos == NULL_POSITION)
733 * Reached EOF/BOF without a match.
735 if (pendpos != NULL)
736 *pendpos = oldpos;
737 return (matches);
741 * If we're using line numbers, we might as well
742 * remember the information we have now (the position
743 * and line number of the current line).
744 * Don't do it for every line because it slows down
745 * the search. Remember the line number only if
746 * we're "far" from the last place we remembered it.
748 if (linenums && abs((int)(pos - oldpos)) > 2048)
749 add_lnum(linenum, pos);
750 oldpos = pos;
752 if (is_filtered(linepos))
753 continue;
756 * If it's a caseless search, convert the line to lowercase.
757 * If we're doing backspace processing, delete backspaces.
759 cvt_ops = get_cvt_ops();
760 cvt_len = cvt_length(line_len, cvt_ops);
761 cline = (char *) ecalloc(1, cvt_len);
762 chpos = cvt_alloc_chpos(cvt_len);
763 cvt_text(cline, line, chpos, &line_len, cvt_ops);
765 #if HILITE_SEARCH
767 * Check to see if the line matches the filter pattern.
768 * If so, add an entry to the filter list.
770 if ((search_type & SRCH_FIND_ALL) && prev_pattern(&filter_info)) {
771 int line_filter = match_pattern(filter_info.compiled, filter_info.text,
772 cline, line_len, &sp, &ep, 0, filter_info.search_type);
773 if (line_filter)
775 struct hilite *hl = (struct hilite *)
776 ecalloc(1, sizeof(struct hilite));
777 hl->hl_startpos = linepos;
778 hl->hl_endpos = pos;
779 add_hilite(&filter_anchor, hl);
782 #endif
785 * Test the next line to see if we have a match.
786 * We are successful if we either want a match and got one,
787 * or if we want a non-match and got one.
789 if (prev_pattern(&search_info))
791 line_match = match_pattern(search_info.compiled, search_info.text,
792 cline, line_len, &sp, &ep, 0, search_type); //FIXME search_info.search_type
793 if (line_match)
796 * Got a match.
798 if (search_type & SRCH_FIND_ALL)
800 #if HILITE_SEARCH
802 * We are supposed to find all matches in the range.
803 * Just add the matches in this line to the
804 * hilite list and keep searching.
806 hilite_line(linepos, cline, line_len, chpos, sp, ep, cvt_ops);
807 #endif
808 } else if (--matches <= 0)
811 * Found the one match we're looking for.
812 * Return it.
814 #if HILITE_SEARCH
815 if (hilite_search == OPT_ON)
818 * Clear the hilite list and add only
819 * the matches in this one line.
821 clr_hilite();
822 hilite_line(linepos, cline, line_len, chpos, sp, ep, cvt_ops);
824 #endif
825 free(cline);
826 free(chpos);
827 if (plinepos != NULL)
828 *plinepos = linepos;
829 return (0);
833 free(cline);
834 free(chpos);
839 * search for a pattern in history. If found, compile that pattern.
841 static int
842 hist_pattern(search_type)
843 int search_type;
845 #if CMD_HISTORY
846 char *pattern;
848 set_mlist(ml_search, 0);
849 pattern = cmd_lastpattern();
850 if (pattern == NULL)
851 return (0);
853 if (set_pattern(&search_info, pattern, search_type) < 0)
854 return (0);
856 is_ucase_pattern = is_ucase(pattern);
857 if (is_ucase_pattern && caseless != OPT_ONPLUS)
858 is_caseless = 0;
859 else
860 is_caseless = caseless;
862 #if HILITE_SEARCH
863 if (hilite_search == OPT_ONPLUS && !hide_hilite)
864 hilite_screen();
865 #endif
867 return (1);
868 #else /* CMD_HISTORY */
869 return (0);
870 #endif /* CMD_HISTORY */
874 * Search for the n-th occurrence of a specified pattern,
875 * either forward or backward.
876 * Return the number of matches not yet found in this file
877 * (that is, n minus the number of matches found).
878 * Return -1 if the search should be aborted.
879 * Caller may continue the search in another file
880 * if less than n matches are found in this file.
882 public int
883 search(search_type, pattern, n)
884 int search_type;
885 char *pattern;
886 int n;
888 POSITION pos;
890 if (pattern == NULL || *pattern == '\0')
893 * A null pattern means use the previously compiled pattern.
895 if (!prev_pattern(&search_info) && !hist_pattern(search_type))
897 error("No previous regular expression", NULL_PARG);
898 return (-1);
900 if ((search_type & SRCH_NO_REGEX) !=
901 (search_info.search_type & SRCH_NO_REGEX))
903 error("Please re-enter search pattern", NULL_PARG);
904 return -1;
906 #if HILITE_SEARCH
907 if (hilite_search == OPT_ON)
910 * Erase the highlights currently on screen.
911 * If the search fails, we'll redisplay them later.
913 repaint_hilite(0);
915 if (hilite_search == OPT_ONPLUS && hide_hilite)
918 * Highlight any matches currently on screen,
919 * before we actually start the search.
921 hide_hilite = 0;
922 hilite_screen();
924 hide_hilite = 0;
925 #endif
926 } else
929 * Compile the pattern.
931 if (set_pattern(&search_info, pattern, search_type) < 0)
932 return (-1);
934 * Ignore case if -I is set OR
935 * -i is set AND the pattern is all lowercase.
937 is_ucase_pattern = is_ucase(pattern);
938 if (is_ucase_pattern && caseless != OPT_ONPLUS)
939 is_caseless = 0;
940 else
941 is_caseless = caseless;
942 #if HILITE_SEARCH
943 if (hilite_search)
946 * Erase the highlights currently on screen.
947 * Also permanently delete them from the hilite list.
949 repaint_hilite(0);
950 hide_hilite = 0;
951 clr_hilite();
953 if (hilite_search == OPT_ONPLUS)
956 * Highlight any matches currently on screen,
957 * before we actually start the search.
959 hilite_screen();
961 #endif
965 * Figure out where to start the search.
967 pos = search_pos(search_type);
968 if (pos == NULL_POSITION)
971 * Can't find anyplace to start searching from.
973 if (search_type & SRCH_PAST_EOF)
974 return (n);
975 /* repaint(); -- why was this here? */
976 error("Nothing to search", NULL_PARG);
977 return (-1);
980 n = search_range(pos, NULL_POSITION, search_type, n, -1,
981 &pos, (POSITION*)NULL);
982 if (n != 0)
985 * Search was unsuccessful.
987 #if HILITE_SEARCH
988 if (hilite_search == OPT_ON && n > 0)
990 * Redisplay old hilites.
992 repaint_hilite(1);
993 #endif
994 return (n);
997 if (!(search_type & SRCH_NO_MOVE))
1000 * Go to the matching line.
1002 jump_loc(pos, jump_sline);
1005 #if HILITE_SEARCH
1006 if (hilite_search == OPT_ON)
1008 * Display new hilites in the matching line.
1010 repaint_hilite(1);
1011 #endif
1012 return (0);
1016 #if HILITE_SEARCH
1018 * Prepare hilites in a given range of the file.
1020 * The pair (prep_startpos,prep_endpos) delimits a contiguous region
1021 * of the file that has been "prepared"; that is, scanned for matches for
1022 * the current search pattern, and hilites have been created for such matches.
1023 * If prep_startpos == NULL_POSITION, the prep region is empty.
1024 * If prep_endpos == NULL_POSITION, the prep region extends to EOF.
1025 * prep_hilite asks that the range (spos,epos) be covered by the prep region.
1027 public void
1028 prep_hilite(spos, epos, maxlines)
1029 POSITION spos;
1030 POSITION epos;
1031 int maxlines;
1033 POSITION nprep_startpos = prep_startpos;
1034 POSITION nprep_endpos = prep_endpos;
1035 POSITION new_epos;
1036 POSITION max_epos;
1037 int result;
1038 int i;
1041 * Search beyond where we're asked to search, so the prep region covers
1042 * more than we need. Do one big search instead of a bunch of small ones.
1044 #define SEARCH_MORE (3*size_linebuf)
1046 if (!prev_pattern(&search_info) && !is_filtering())
1047 return;
1050 * If we're limited to a max number of lines, figure out the
1051 * file position we should stop at.
1053 if (maxlines < 0)
1054 max_epos = NULL_POSITION;
1055 else
1057 max_epos = spos;
1058 for (i = 0; i < maxlines; i++)
1059 max_epos = forw_raw_line(max_epos, (char **)NULL, (int *)NULL);
1063 * Find two ranges:
1064 * The range that we need to search (spos,epos); and the range that
1065 * the "prep" region will then cover (nprep_startpos,nprep_endpos).
1068 if (prep_startpos == NULL_POSITION ||
1069 (epos != NULL_POSITION && epos < prep_startpos) ||
1070 spos > prep_endpos)
1073 * New range is not contiguous with old prep region.
1074 * Discard the old prep region and start a new one.
1076 clr_hilite();
1077 clr_filter();
1078 if (epos != NULL_POSITION)
1079 epos += SEARCH_MORE;
1080 nprep_startpos = spos;
1081 } else
1084 * New range partially or completely overlaps old prep region.
1086 if (epos == NULL_POSITION)
1089 * New range goes to end of file.
1092 } else if (epos > prep_endpos)
1095 * New range ends after old prep region.
1096 * Extend prep region to end at end of new range.
1098 epos += SEARCH_MORE;
1099 } else /* (epos <= prep_endpos) */
1102 * New range ends within old prep region.
1103 * Truncate search to end at start of old prep region.
1105 epos = prep_startpos;
1108 if (spos < prep_startpos)
1111 * New range starts before old prep region.
1112 * Extend old prep region backwards to start at
1113 * start of new range.
1115 if (spos < SEARCH_MORE)
1116 spos = 0;
1117 else
1118 spos -= SEARCH_MORE;
1119 nprep_startpos = spos;
1120 } else /* (spos >= prep_startpos) */
1123 * New range starts within or after old prep region.
1124 * Trim search to start at end of old prep region.
1126 spos = prep_endpos;
1130 if (epos != NULL_POSITION && max_epos != NULL_POSITION &&
1131 epos > max_epos)
1133 * Don't go past the max position we're allowed.
1135 epos = max_epos;
1137 if (epos == NULL_POSITION || epos > spos)
1139 int search_type = SRCH_FORW | SRCH_FIND_ALL;
1140 search_type |= (search_info.search_type & SRCH_NO_REGEX);
1141 result = search_range(spos, epos, search_type, 0,
1142 maxlines, (POSITION*)NULL, &new_epos);
1143 if (result < 0)
1144 return;
1145 if (prep_endpos == NULL_POSITION || new_epos > prep_endpos)
1146 nprep_endpos = new_epos;
1148 prep_startpos = nprep_startpos;
1149 prep_endpos = nprep_endpos;
1153 * Set the pattern to be used for line filtering.
1155 public void
1156 set_filter_pattern(pattern, search_type)
1157 char *pattern;
1158 int search_type;
1160 clr_filter();
1161 if (pattern == NULL || *pattern == '\0')
1162 clear_pattern(&filter_info);
1163 else
1164 set_pattern(&filter_info, pattern, search_type);
1165 screen_trashed = 1;
1169 * Is there a line filter in effect?
1171 public int
1172 is_filtering()
1174 if (ch_getflags() & CH_HELPFILE)
1175 return (0);
1176 return prev_pattern(&filter_info);
1178 #endif
1180 #if HAVE_V8_REGCOMP
1182 * This function is called by the V8 regcomp to report
1183 * errors in regular expressions.
1185 void
1186 regerror(s)
1187 char *s;
1189 PARG parg;
1191 parg.p_string = s;
1192 error("%s", &parg);
1194 #endif