1 /* $NetBSD: search.c,v 1.4 2013/09/04 19:44:21 tron Exp $ */
4 * Copyright (C) 1984-2012 Mark Nudelman
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
9 * For more information, see the README file.
14 * Routines to search a file for a pattern.
22 #define MINPOS(a,b) (((a) < (b)) ? (a) : (b))
23 #define MAXPOS(a,b) (((a) > (b)) ? (a) : (b))
26 extern int how_search
;
30 extern int jump_sline
;
33 extern int status_col
;
34 extern void * constant ml_search
;
35 extern POSITION start_attnpos
;
36 extern POSITION end_attnpos
;
38 extern int screen_trashed
;
40 extern int hilite_search
;
41 extern int size_linebuf
;
43 extern int can_goto_line
;
44 static int hide_hilite
;
45 static POSITION prep_startpos
;
46 static POSITION prep_endpos
;
47 static int is_caseless
;
48 static int is_ucase_pattern
;
52 struct hilite
*hl_next
;
56 static struct hilite hilite_anchor
= { NULL
, NULL_POSITION
, NULL_POSITION
};
57 static struct hilite filter_anchor
= { NULL
, NULL_POSITION
, NULL_POSITION
};
58 #define hl_first hl_next
62 * These are the static variables that represent the "remembered"
63 * search pattern and filter pattern.
66 DEFINE_PATTERN(compiled
);
72 #define info_compiled(info) ((void*)0)
74 #define info_compiled(info) ((info)->compiled)
77 static struct pattern_info search_info
;
78 static struct pattern_info filter_info
;
81 * Are there any uppercase letters in this string?
87 char *str_end
= str
+ strlen(str
);
92 ch
= step_char(&str
, +1, str_end
);
100 * Compile and save a search pattern.
103 set_pattern(info
, pattern
, search_type
)
104 struct pattern_info
*info
;
110 CLEAR_PATTERN(info
->compiled
);
111 else if (compile_pattern(pattern
, search_type
, &info
->compiled
) < 0)
114 /* Pattern compiled successfully; save the text too. */
115 if (info
->text
!= NULL
)
120 info
->text
= (char *) ecalloc(1, strlen(pattern
)+1);
121 strcpy(info
->text
, pattern
);
123 info
->search_type
= search_type
;
126 * Ignore case if -I is set OR
127 * -i is set AND the pattern is all lowercase.
129 is_ucase_pattern
= is_ucase(pattern
);
130 if (is_ucase_pattern
&& caseless
!= OPT_ONPLUS
)
133 is_caseless
= caseless
;
138 * Discard a saved pattern.
142 struct pattern_info
*info
;
144 if (info
->text
!= NULL
)
148 uncompile_pattern(&info
->compiled
);
153 * Initialize saved pattern to nothing.
157 struct pattern_info
*info
;
159 CLEAR_PATTERN(info
->compiled
);
161 info
->search_type
= 0;
165 * Initialize search variables.
170 init_pattern(&search_info
);
171 init_pattern(&filter_info
);
175 * Determine which text conversions to perform before pattern matching.
181 if (is_caseless
|| bs_mode
== BS_SPECIAL
)
185 if (bs_mode
== BS_SPECIAL
)
187 if (bs_mode
!= BS_CONTROL
)
189 } else if (bs_mode
!= BS_CONTROL
)
193 if (ctldisp
== OPT_ONPLUS
)
199 * Is there a previous (remembered) search pattern?
203 struct pattern_info
*info
;
206 if ((info
->search_type
& SRCH_NO_REGEX
) == 0)
207 return (!is_null_pattern(info
->compiled
));
209 return (info
->text
!= NULL
);
214 * Repaint the hilites currently displayed on the screen.
215 * Repaint each line which contains highlighted text.
216 * If on==0, force all hilites off.
225 int save_hide_hilite
;
230 save_hide_hilite
= hide_hilite
;
241 hide_hilite
= save_hide_hilite
;
245 for (slinenum
= TOP
; slinenum
< TOP
+ sc_height
-1; slinenum
++)
247 pos
= position(slinenum
);
248 if (pos
== NULL_POSITION
)
250 epos
= position(slinenum
+1);
251 (void) forw_line(pos
);
256 hide_hilite
= save_hide_hilite
;
260 * Clear the attn hilite.
266 POSITION old_start_attnpos
;
267 POSITION old_end_attnpos
;
272 if (start_attnpos
== NULL_POSITION
)
274 old_start_attnpos
= start_attnpos
;
275 old_end_attnpos
= end_attnpos
;
276 start_attnpos
= end_attnpos
= NULL_POSITION
;
286 for (slinenum
= TOP
; slinenum
< TOP
+ sc_height
-1; slinenum
++)
288 pos
= position(slinenum
);
289 if (pos
== NULL_POSITION
)
291 epos
= position(slinenum
+1);
292 if (pos
< old_end_attnpos
&&
293 (epos
== NULL_POSITION
|| epos
> old_start_attnpos
))
295 (void) forw_line(pos
);
307 * Hide search string highlighting.
312 if (!prev_pattern(&search_info
))
314 error("No previous regular expression", NULL_PARG
);
318 hide_hilite
= !hide_hilite
;
325 * Clear the hilite list.
329 struct hilite
*anchor
;
332 struct hilite
*nexthl
;
334 for (hl
= anchor
->hl_first
; hl
!= NULL
; hl
= nexthl
)
336 nexthl
= hl
->hl_next
;
339 anchor
->hl_first
= NULL
;
340 prep_startpos
= prep_endpos
= NULL_POSITION
;
346 clr_hlist(&hilite_anchor
);
352 clr_hlist(&filter_anchor
);
356 * Should any characters in a specified range be highlighted?
359 is_hilited_range(pos
, epos
)
366 * Look at each highlight and see if any part of it falls in the range.
368 for (hl
= hilite_anchor
.hl_first
; hl
!= NULL
; hl
= hl
->hl_next
)
370 if (hl
->hl_endpos
> pos
&&
371 (epos
== NULL_POSITION
|| epos
> hl
->hl_startpos
))
378 * Is a line "filtered" -- that is, should it be hidden?
386 if (ch_getflags() & CH_HELPFILE
)
390 * Look at each filter and see if the start position
391 * equals the start position of the line.
393 for (hl
= filter_anchor
.hl_first
; hl
!= NULL
; hl
= hl
->hl_next
)
395 if (hl
->hl_startpos
== pos
)
402 * Should any characters in a specified range be highlighted?
403 * If nohide is nonzero, don't consider hide_hilite.
406 is_hilited(pos
, epos
, nohide
, p_matches
)
414 if (p_matches
!= NULL
)
418 start_attnpos
!= NULL_POSITION
&&
420 (epos
== NULL_POSITION
|| epos
> start_attnpos
))
422 * The attn line overlaps this range.
426 match
= is_hilited_range(pos
, epos
);
430 if (p_matches
!= NULL
)
432 * Report matches, even if we're hiding highlights.
436 if (hilite_search
== 0)
438 * Not doing highlighting.
442 if (!nohide
&& hide_hilite
)
444 * Highlighting is hidden.
452 * Add a new hilite to a hilite list.
455 add_hilite(anchor
, hl
)
456 struct hilite
*anchor
;
462 * Hilites are sorted in the list; find where new one belongs.
463 * Insert new one after ihl.
465 for (ihl
= anchor
; ihl
->hl_next
!= NULL
; ihl
= ihl
->hl_next
)
467 if (ihl
->hl_next
->hl_startpos
> hl
->hl_startpos
)
472 * Truncate hilite so it doesn't overlap any existing ones
473 * above and below it.
476 hl
->hl_startpos
= MAXPOS(hl
->hl_startpos
, ihl
->hl_endpos
);
477 if (ihl
->hl_next
!= NULL
)
478 hl
->hl_endpos
= MINPOS(hl
->hl_endpos
, ihl
->hl_next
->hl_startpos
);
479 if (hl
->hl_startpos
>= hl
->hl_endpos
)
482 * Hilite was truncated out of existence.
487 hl
->hl_next
= ihl
->hl_next
;
492 * Hilight every character in a range of displayed characters.
495 create_hilites(linepos
, start_index
, end_index
, chpos
)
504 /* Start the first hilite. */
505 hl
= (struct hilite
*) ecalloc(1, sizeof(struct hilite
));
506 hl
->hl_startpos
= linepos
+ chpos
[start_index
];
509 * Step through the displayed chars.
510 * If the source position (before cvt) of the char is one more
511 * than the source pos of the previous char (the usual case),
512 * just increase the size of the current hilite by one.
513 * Otherwise (there are backspaces or something involved),
514 * finish the current hilite and start a new one.
516 for (i
= start_index
+1; i
<= end_index
; i
++)
518 if (chpos
[i
] != chpos
[i
-1] + 1 || i
== end_index
)
520 hl
->hl_endpos
= linepos
+ chpos
[i
-1] + 1;
521 add_hilite(&hilite_anchor
, hl
);
522 /* Start new hilite unless this is the last char. */
525 hl
= (struct hilite
*) ecalloc(1, sizeof(struct hilite
));
526 hl
->hl_startpos
= linepos
+ chpos
[i
];
533 * Make a hilite for each string in a physical line which matches
534 * the current pattern.
535 * sp,ep delimit the first match already found.
538 hilite_line(linepos
, line
, line_len
, chpos
, sp
, ep
, cvt_ops
)
548 char *line_end
= line
+ line_len
;
550 if (sp
== NULL
|| ep
== NULL
)
553 * sp and ep delimit the first match in the line.
554 * Mark the corresponding file positions, then
555 * look for further matches and mark them.
556 * {{ This technique, of calling match_pattern on subsequent
557 * substrings of the line, may mark more than is correct
558 * if the pattern starts with "^". This bug is fixed
559 * for those regex functions that accept a notbol parameter
560 * (currently POSIX, PCRE and V8-with-regexec2). }}
564 create_hilites(linepos
, sp
-line
, ep
-line
, chpos
);
566 * If we matched more than zero characters,
567 * move to the first char after the string we matched.
568 * If we matched zero, just move to the next char.
572 else if (searchp
!= line_end
)
574 else /* end of line */
576 } while (match_pattern(info_compiled(&search_info
), search_info
.text
,
577 searchp
, line_end
- searchp
, &sp
, &ep
, 1, search_info
.search_type
));
582 * Change the caseless-ness of searches.
583 * Updates the internal search state to reflect a change in the -i flag.
588 if (!is_ucase_pattern
)
590 * Pattern did not have uppercase.
591 * Just set the search caselessness to the global caselessness.
593 is_caseless
= caseless
;
596 * Pattern did have uppercase.
597 * Discard the pattern; we can't change search caselessness now.
599 clear_pattern(&search_info
);
604 * Find matching text which is currently on screen and highlight it.
609 struct scrpos scrpos
;
612 if (scrpos
.pos
== NULL_POSITION
)
614 prep_hilite(scrpos
.pos
, position(BOTTOM_PLUS_ONE
), -1);
619 * Change highlighting parameters.
625 * Erase any highlights currently on screen.
630 if (hilite_search
== OPT_ONPLUS
)
632 * Display highlights.
639 * Figure out where to start a search.
642 search_pos(search_type
)
651 * Start at the beginning (or end) of the file.
652 * The empty_screen() case is mainly for
653 * command line initiated searches;
654 * for example, "+/xyz" on the command line.
655 * Also for multi-file (SRCH_PAST_EOF) searches.
657 if (search_type
& SRCH_FORW
)
663 if (pos
== NULL_POSITION
)
665 (void) ch_end_seek();
674 if (how_search
== OPT_ON
)
677 * Search does not include current screen.
679 if (search_type
& SRCH_FORW
)
680 linenum
= BOTTOM_PLUS_ONE
;
683 } else if (how_search
== OPT_ONPLUS
&& !(search_type
& SRCH_AFTER_TARGET
))
686 * Search includes all of displayed screen.
688 if (search_type
& SRCH_FORW
)
691 linenum
= BOTTOM_PLUS_ONE
;
695 * Search includes the part of current screen beyond the jump target.
696 * It starts at the jump target (if searching backwards),
697 * or at the jump target plus one (if forwards).
699 linenum
= jump_sline
;
700 if (search_type
& SRCH_FORW
)
703 linenum
= adjsline(linenum
);
704 pos
= position(linenum
);
706 pos
= forw_raw_line(pos
, (char **)NULL
, (int *)NULL
);
710 * If the line is empty, look around for a plausible starting place.
712 if (search_type
& SRCH_FORW
)
714 while (pos
== NULL_POSITION
)
716 if (++linenum
>= sc_height
)
718 pos
= position(linenum
);
722 while (pos
== NULL_POSITION
)
726 pos
= position(linenum
);
733 * Search a subset of the file, specified by start/end position.
736 search_range(pos
, endpos
, search_type
, matches
, maxlines
, plinepos
, pendpos
)
749 char *sp
= NULL
, *ep
= NULL
; /* XXX: GCC */
754 POSITION linepos
, oldpos
;
756 linenum
= find_linenum(pos
);
761 * Get lines until we find a matching one or until
762 * we hit end-of-file (or beginning-of-file if we're
763 * going backwards), or until we hit the end position.
768 * A signal aborts the search.
773 if ((endpos
!= NULL_POSITION
&& pos
>= endpos
) || maxlines
== 0)
776 * Reached end position without a match.
785 if (search_type
& SRCH_FORW
)
788 * Read the next line, and save the
789 * starting position of that line in linepos.
792 pos
= forw_raw_line(pos
, &line
, &line_len
);
798 * Read the previous line and save the
799 * starting position of that line in linepos.
801 pos
= back_raw_line(pos
, &line
, &line_len
);
807 if (pos
== NULL_POSITION
)
810 * Reached EOF/BOF without a match.
818 * If we're using line numbers, we might as well
819 * remember the information we have now (the position
820 * and line number of the current line).
821 * Don't do it for every line because it slows down
822 * the search. Remember the line number only if
823 * we're "far" from the last place we remembered it.
825 if (linenums
&& abs((int)(pos
- oldpos
)) > 2048)
826 add_lnum(linenum
, pos
);
829 if (is_filtered(linepos
))
833 * If it's a caseless search, convert the line to lowercase.
834 * If we're doing backspace processing, delete backspaces.
836 cvt_ops
= get_cvt_ops();
837 cvt_len
= cvt_length(line_len
, cvt_ops
);
838 cline
= (char *) ecalloc(1, cvt_len
);
839 chpos
= cvt_alloc_chpos(cvt_len
);
840 cvt_text(cline
, line
, chpos
, &line_len
, cvt_ops
);
844 * Check to see if the line matches the filter pattern.
845 * If so, add an entry to the filter list.
847 if ((search_type
& SRCH_FIND_ALL
) && prev_pattern(&filter_info
)) {
848 int line_filter
= match_pattern(info_compiled(&filter_info
), filter_info
.text
,
849 cline
, line_len
, &sp
, &ep
, 0, filter_info
.search_type
);
852 struct hilite
*hl
= (struct hilite
*)
853 ecalloc(1, sizeof(struct hilite
));
854 hl
->hl_startpos
= linepos
;
856 add_hilite(&filter_anchor
, hl
);
862 * Test the next line to see if we have a match.
863 * We are successful if we either want a match and got one,
864 * or if we want a non-match and got one.
866 if (prev_pattern(&search_info
))
868 line_match
= match_pattern(info_compiled(&search_info
), search_info
.text
,
869 cline
, line_len
, &sp
, &ep
, 0, search_type
);
875 if (search_type
& SRCH_FIND_ALL
)
879 * We are supposed to find all matches in the range.
880 * Just add the matches in this line to the
881 * hilite list and keep searching.
883 hilite_line(linepos
, cline
, line_len
, chpos
, sp
, ep
, cvt_ops
);
885 } else if (--matches
<= 0)
888 * Found the one match we're looking for.
892 if (hilite_search
== OPT_ON
)
895 * Clear the hilite list and add only
896 * the matches in this one line.
899 hilite_line(linepos
, cline
, line_len
, chpos
, sp
, ep
, cvt_ops
);
904 if (plinepos
!= NULL
)
916 * search for a pattern in history. If found, compile that pattern.
919 hist_pattern(search_type
)
925 set_mlist(ml_search
, 0);
926 pattern
= cmd_lastpattern();
930 if (set_pattern(&search_info
, pattern
, search_type
) < 0)
934 if (hilite_search
== OPT_ONPLUS
&& !hide_hilite
)
939 #else /* CMD_HISTORY */
941 #endif /* CMD_HISTORY */
945 * Search for the n-th occurrence of a specified pattern,
946 * either forward or backward.
947 * Return the number of matches not yet found in this file
948 * (that is, n minus the number of matches found).
949 * Return -1 if the search should be aborted.
950 * Caller may continue the search in another file
951 * if less than n matches are found in this file.
954 search(search_type
, pattern
, n
)
961 if (pattern
== NULL
|| *pattern
== '\0')
964 * A null pattern means use the previously compiled pattern.
966 search_type
|= SRCH_AFTER_TARGET
;
967 if (!prev_pattern(&search_info
) && !hist_pattern(search_type
))
969 error("No previous regular expression", NULL_PARG
);
972 if ((search_type
& SRCH_NO_REGEX
) !=
973 (search_info
.search_type
& SRCH_NO_REGEX
))
975 error("Please re-enter search pattern", NULL_PARG
);
979 if (hilite_search
== OPT_ON
)
982 * Erase the highlights currently on screen.
983 * If the search fails, we'll redisplay them later.
987 if (hilite_search
== OPT_ONPLUS
&& hide_hilite
)
990 * Highlight any matches currently on screen,
991 * before we actually start the search.
1001 * Compile the pattern.
1003 if (set_pattern(&search_info
, pattern
, search_type
) < 0)
1009 * Erase the highlights currently on screen.
1010 * Also permanently delete them from the hilite list.
1016 if (hilite_search
== OPT_ONPLUS
)
1019 * Highlight any matches currently on screen,
1020 * before we actually start the search.
1028 * Figure out where to start the search.
1030 pos
= search_pos(search_type
);
1031 if (pos
== NULL_POSITION
)
1034 * Can't find anyplace to start searching from.
1036 if (search_type
& SRCH_PAST_EOF
)
1038 /* repaint(); -- why was this here? */
1039 error("Nothing to search", NULL_PARG
);
1043 n
= search_range(pos
, NULL_POSITION
, search_type
, n
, -1,
1044 &pos
, (POSITION
*)NULL
);
1048 * Search was unsuccessful.
1051 if (hilite_search
== OPT_ON
&& n
> 0)
1053 * Redisplay old hilites.
1060 if (!(search_type
& SRCH_NO_MOVE
))
1063 * Go to the matching line.
1065 jump_loc(pos
, jump_sline
);
1069 if (hilite_search
== OPT_ON
)
1071 * Display new hilites in the matching line.
1081 * Prepare hilites in a given range of the file.
1083 * The pair (prep_startpos,prep_endpos) delimits a contiguous region
1084 * of the file that has been "prepared"; that is, scanned for matches for
1085 * the current search pattern, and hilites have been created for such matches.
1086 * If prep_startpos == NULL_POSITION, the prep region is empty.
1087 * If prep_endpos == NULL_POSITION, the prep region extends to EOF.
1088 * prep_hilite asks that the range (spos,epos) be covered by the prep region.
1091 prep_hilite(spos
, epos
, maxlines
)
1096 POSITION nprep_startpos
= prep_startpos
;
1097 POSITION nprep_endpos
= prep_endpos
;
1104 * Search beyond where we're asked to search, so the prep region covers
1105 * more than we need. Do one big search instead of a bunch of small ones.
1107 #define SEARCH_MORE (3*size_linebuf)
1109 if (!prev_pattern(&search_info
) && !is_filtering())
1113 * If we're limited to a max number of lines, figure out the
1114 * file position we should stop at.
1117 max_epos
= NULL_POSITION
;
1121 for (i
= 0; i
< maxlines
; i
++)
1122 max_epos
= forw_raw_line(max_epos
, (char **)NULL
, (int *)NULL
);
1127 * The range that we need to search (spos,epos); and the range that
1128 * the "prep" region will then cover (nprep_startpos,nprep_endpos).
1131 if (prep_startpos
== NULL_POSITION
||
1132 (epos
!= NULL_POSITION
&& epos
< prep_startpos
) ||
1136 * New range is not contiguous with old prep region.
1137 * Discard the old prep region and start a new one.
1141 if (epos
!= NULL_POSITION
)
1142 epos
+= SEARCH_MORE
;
1143 nprep_startpos
= spos
;
1147 * New range partially or completely overlaps old prep region.
1149 if (epos
== NULL_POSITION
)
1152 * New range goes to end of file.
1155 } else if (epos
> prep_endpos
)
1158 * New range ends after old prep region.
1159 * Extend prep region to end at end of new range.
1161 epos
+= SEARCH_MORE
;
1162 } else /* (epos <= prep_endpos) */
1165 * New range ends within old prep region.
1166 * Truncate search to end at start of old prep region.
1168 epos
= prep_startpos
;
1171 if (spos
< prep_startpos
)
1174 * New range starts before old prep region.
1175 * Extend old prep region backwards to start at
1176 * start of new range.
1178 if (spos
< SEARCH_MORE
)
1181 spos
-= SEARCH_MORE
;
1182 nprep_startpos
= spos
;
1183 } else /* (spos >= prep_startpos) */
1186 * New range starts within or after old prep region.
1187 * Trim search to start at end of old prep region.
1193 if (epos
!= NULL_POSITION
&& max_epos
!= NULL_POSITION
&&
1196 * Don't go past the max position we're allowed.
1200 if (epos
== NULL_POSITION
|| epos
> spos
)
1202 int search_type
= SRCH_FORW
| SRCH_FIND_ALL
;
1203 search_type
|= (search_info
.search_type
& SRCH_NO_REGEX
);
1204 result
= search_range(spos
, epos
, search_type
, 0,
1205 maxlines
, (POSITION
*)NULL
, &new_epos
);
1208 if (prep_endpos
== NULL_POSITION
|| new_epos
> prep_endpos
)
1209 nprep_endpos
= new_epos
;
1211 prep_startpos
= nprep_startpos
;
1212 prep_endpos
= nprep_endpos
;
1216 * Set the pattern to be used for line filtering.
1219 set_filter_pattern(pattern
, search_type
)
1224 if (pattern
== NULL
|| *pattern
== '\0')
1225 clear_pattern(&filter_info
);
1227 set_pattern(&filter_info
, pattern
, search_type
);
1232 * Is there a line filter in effect?
1237 if (ch_getflags() & CH_HELPFILE
)
1239 return prev_pattern(&filter_info
);
1245 * This function is called by the V8 regcomp to report
1246 * errors in regular expressions.