2 * Copyright (C) 1984-2011 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.
13 * Routines to search a file for a pattern.
21 #define MINPOS(a,b) (((a) < (b)) ? (a) : (b))
22 #define MAXPOS(a,b) (((a) > (b)) ? (a) : (b))
25 extern int how_search;
29 extern int jump_sline;
32 extern int status_col;
33 extern void * constant ml_search;
34 extern POSITION start_attnpos;
35 extern POSITION end_attnpos;
37 extern int screen_trashed;
39 extern int hilite_search;
40 extern int size_linebuf;
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;
51 struct hilite *hl_next;
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
61 * These are the static variables that represent the "remembered"
62 * search pattern and filter pattern.
65 DEFINE_PATTERN(compiled);
70 static struct pattern_info search_info;
71 static struct pattern_info filter_info;
74 * Are there any uppercase letters in this string?
80 char *str_end = str + strlen(str);
85 ch = step_char(&str, +1, str_end);
93 * Compile and save a search pattern.
96 set_pattern(info, pattern, search_type)
97 struct pattern_info *info;
102 CLEAR_PATTERN(search_info.compiled);
103 else if (compile_pattern(pattern, search_type, &info->compiled) < 0)
105 /* Pattern compiled successfully; save the text too. */
106 if (info->text != NULL)
111 info->text = (char *) ecalloc(1, strlen(pattern)+1);
112 strcpy(info->text, pattern);
114 info->search_type = search_type;
117 * Ignore case if -I is set OR
118 * -i is set AND the pattern is all lowercase.
120 is_ucase_pattern = is_ucase(pattern);
121 if (is_ucase_pattern && caseless != OPT_ONPLUS)
124 is_caseless = caseless;
129 * Discard a saved pattern.
133 struct pattern_info *info;
135 if (info->text != NULL)
138 uncompile_pattern(&info->compiled);
142 * Initialize saved pattern to nothing.
146 struct pattern_info *info;
148 CLEAR_PATTERN(info->compiled);
150 info->search_type = 0;
154 * Initialize search variables.
159 init_pattern(&search_info);
160 init_pattern(&filter_info);
164 * Determine which text conversions to perform before pattern matching.
170 if (is_caseless || bs_mode == BS_SPECIAL)
174 if (bs_mode == BS_SPECIAL)
176 if (bs_mode != BS_CONTROL)
178 } else if (bs_mode != BS_CONTROL)
182 if (ctldisp == OPT_ONPLUS)
188 * Is there a previous (remembered) search pattern?
192 struct pattern_info *info;
194 if (info->search_type & SRCH_NO_REGEX)
195 return (info->text != NULL);
196 return (!is_null_pattern(info->compiled));
201 * Repaint the hilites currently displayed on the screen.
202 * Repaint each line which contains highlighted text.
203 * If on==0, force all hilites off.
212 int save_hide_hilite;
217 save_hide_hilite = hide_hilite;
228 hide_hilite = save_hide_hilite;
232 for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++)
234 pos = position(slinenum);
235 if (pos == NULL_POSITION)
237 epos = position(slinenum+1);
238 (void) forw_line(pos);
243 hide_hilite = save_hide_hilite;
247 * Clear the attn hilite.
253 POSITION old_start_attnpos;
254 POSITION old_end_attnpos;
259 if (start_attnpos == NULL_POSITION)
261 old_start_attnpos = start_attnpos;
262 old_end_attnpos = end_attnpos;
263 start_attnpos = end_attnpos = NULL_POSITION;
273 for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++)
275 pos = position(slinenum);
276 if (pos == NULL_POSITION)
278 epos = position(slinenum+1);
279 if (pos < old_end_attnpos &&
280 (epos == NULL_POSITION || epos > old_start_attnpos))
282 (void) forw_line(pos);
294 * Hide search string highlighting.
299 if (!prev_pattern(&search_info))
301 error("No previous regular expression", NULL_PARG);
305 hide_hilite = !hide_hilite;
312 * Clear the hilite list.
316 struct hilite *anchor;
319 struct hilite *nexthl;
321 for (hl = anchor->hl_first; hl != NULL; hl = nexthl)
323 nexthl = hl->hl_next;
326 anchor->hl_first = NULL;
327 prep_startpos = prep_endpos = NULL_POSITION;
333 clr_hlist(&hilite_anchor);
339 clr_hlist(&filter_anchor);
343 * Should any characters in a specified range be highlighted?
346 is_hilited_range(pos, epos)
353 * Look at each highlight and see if any part of it falls in the range.
355 for (hl = hilite_anchor.hl_first; hl != NULL; hl = hl->hl_next)
357 if (hl->hl_endpos > pos &&
358 (epos == NULL_POSITION || epos > hl->hl_startpos))
365 * Is a line "filtered" -- that is, should it be hidden?
373 if (ch_getflags() & CH_HELPFILE)
377 * Look at each filter and see if the start position
378 * equals the start position of the line.
380 for (hl = filter_anchor.hl_first; hl != NULL; hl = hl->hl_next)
382 if (hl->hl_startpos == pos)
389 * Should any characters in a specified range be highlighted?
390 * If nohide is nonzero, don't consider hide_hilite.
393 is_hilited(pos, epos, nohide, p_matches)
401 if (p_matches != NULL)
405 start_attnpos != NULL_POSITION &&
407 (epos == NULL_POSITION || epos > start_attnpos))
409 * The attn line overlaps this range.
413 match = is_hilited_range(pos, epos);
417 if (p_matches != NULL)
419 * Report matches, even if we're hiding highlights.
423 if (hilite_search == 0)
425 * Not doing highlighting.
429 if (!nohide && hide_hilite)
431 * Highlighting is hidden.
439 * Add a new hilite to a hilite list.
442 add_hilite(anchor, hl)
443 struct hilite *anchor;
449 * Hilites are sorted in the list; find where new one belongs.
450 * Insert new one after ihl.
452 for (ihl = anchor; ihl->hl_next != NULL; ihl = ihl->hl_next)
454 if (ihl->hl_next->hl_startpos > hl->hl_startpos)
459 * Truncate hilite so it doesn't overlap any existing ones
460 * above and below it.
463 hl->hl_startpos = MAXPOS(hl->hl_startpos, ihl->hl_endpos);
464 if (ihl->hl_next != NULL)
465 hl->hl_endpos = MINPOS(hl->hl_endpos, ihl->hl_next->hl_startpos);
466 if (hl->hl_startpos >= hl->hl_endpos)
469 * Hilite was truncated out of existence.
474 hl->hl_next = ihl->hl_next;
479 * Make a hilite for each string in a physical line which matches
480 * the current pattern.
481 * sp,ep delimit the first match already found.
484 hilite_line(linepos, line, line_len, chpos, sp, ep, cvt_ops)
494 char *line_end = line + line_len;
497 if (sp == NULL || ep == NULL)
500 * sp and ep delimit the first match in the line.
501 * Mark the corresponding file positions, then
502 * look for further matches and mark them.
503 * {{ This technique, of calling match_pattern on subsequent
504 * substrings of the line, may mark more than is correct
505 * if the pattern starts with "^". This bug is fixed
506 * for those regex functions that accept a notbol parameter
507 * (currently POSIX, PCRE and V8-with-regexec2). }}
513 hl = (struct hilite *) ecalloc(1, sizeof(struct hilite));
514 hl->hl_startpos = linepos + chpos[sp-line];
515 hl->hl_endpos = linepos + chpos[ep-line];
516 add_hilite(&hilite_anchor, hl);
519 * If we matched more than zero characters,
520 * move to the first char after the string we matched.
521 * If we matched zero, just move to the next char.
525 else if (searchp != line_end)
527 else /* end of line */
529 } while (match_pattern(search_info.compiled, search_info.text,
530 searchp, line_end - searchp, &sp, &ep, 1, search_info.search_type));
535 * Change the caseless-ness of searches.
536 * Updates the internal search state to reflect a change in the -i flag.
541 if (!is_ucase_pattern)
543 * Pattern did not have uppercase.
544 * Just set the search caselessness to the global caselessness.
546 is_caseless = caseless;
549 * Pattern did have uppercase.
550 * Discard the pattern; we can't change search caselessness now.
552 clear_pattern(&search_info);
557 * Find matching text which is currently on screen and highlight it.
562 struct scrpos scrpos;
565 if (scrpos.pos == NULL_POSITION)
567 prep_hilite(scrpos.pos, position(BOTTOM_PLUS_ONE), -1);
572 * Change highlighting parameters.
578 * Erase any highlights currently on screen.
583 if (hilite_search == OPT_ONPLUS)
585 * Display highlights.
592 * Figure out where to start a search.
595 search_pos(search_type)
604 * Start at the beginning (or end) of the file.
605 * The empty_screen() case is mainly for
606 * command line initiated searches;
607 * for example, "+/xyz" on the command line.
608 * Also for multi-file (SRCH_PAST_EOF) searches.
610 if (search_type & SRCH_FORW)
616 if (pos == NULL_POSITION)
618 (void) ch_end_seek();
627 if (how_search == OPT_ON)
630 * Search does not include current screen.
632 if (search_type & SRCH_FORW)
633 linenum = BOTTOM_PLUS_ONE;
636 } else if (how_search == OPT_ONPLUS && !(search_type & SRCH_AFTER_TARGET))
639 * Search includes all of displayed screen.
641 if (search_type & SRCH_FORW)
644 linenum = BOTTOM_PLUS_ONE;
648 * Search includes the part of current screen beyond the jump target.
649 * It starts at the jump target (if searching backwards),
650 * or at the jump target plus one (if forwards).
652 linenum = jump_sline;
653 if (search_type & SRCH_FORW)
656 linenum = adjsline(linenum);
657 pos = position(linenum);
659 pos = forw_raw_line(pos, (char **)NULL, (int *)NULL);
663 * If the line is empty, look around for a plausible starting place.
665 if (search_type & SRCH_FORW)
667 while (pos == NULL_POSITION)
669 if (++linenum >= sc_height)
671 pos = position(linenum);
675 while (pos == NULL_POSITION)
679 pos = position(linenum);
686 * Search a subset of the file, specified by start/end position.
689 search_range(pos, endpos, search_type, matches, maxlines, plinepos, pendpos)
707 POSITION linepos, oldpos;
709 linenum = find_linenum(pos);
714 * Get lines until we find a matching one or until
715 * we hit end-of-file (or beginning-of-file if we're
716 * going backwards), or until we hit the end position.
721 * A signal aborts the search.
726 if ((endpos != NULL_POSITION && pos >= endpos) || maxlines == 0)
729 * Reached end position without a match.
738 if (search_type & SRCH_FORW)
741 * Read the next line, and save the
742 * starting position of that line in linepos.
745 pos = forw_raw_line(pos, &line, &line_len);
751 * Read the previous line and save the
752 * starting position of that line in linepos.
754 pos = back_raw_line(pos, &line, &line_len);
760 if (pos == NULL_POSITION)
763 * Reached EOF/BOF without a match.
771 * If we're using line numbers, we might as well
772 * remember the information we have now (the position
773 * and line number of the current line).
774 * Don't do it for every line because it slows down
775 * the search. Remember the line number only if
776 * we're "far" from the last place we remembered it.
778 if (linenums && abs((int)(pos - oldpos)) > 2048)
779 add_lnum(linenum, pos);
782 if (is_filtered(linepos))
786 * If it's a caseless search, convert the line to lowercase.
787 * If we're doing backspace processing, delete backspaces.
789 cvt_ops = get_cvt_ops();
790 cvt_len = cvt_length(line_len, cvt_ops);
791 cline = (char *) ecalloc(1, cvt_len);
792 chpos = cvt_alloc_chpos(cvt_len);
793 cvt_text(cline, line, chpos, &line_len, cvt_ops);
797 * Check to see if the line matches the filter pattern.
798 * If so, add an entry to the filter list.
800 if ((search_type & SRCH_FIND_ALL) && prev_pattern(&filter_info)) {
801 int line_filter = match_pattern(filter_info.compiled, filter_info.text,
802 cline, line_len, &sp, &ep, 0, filter_info.search_type);
805 struct hilite *hl = (struct hilite *)
806 ecalloc(1, sizeof(struct hilite));
807 hl->hl_startpos = linepos;
809 add_hilite(&filter_anchor, hl);
815 * Test the next line to see if we have a match.
816 * We are successful if we either want a match and got one,
817 * or if we want a non-match and got one.
819 if (prev_pattern(&search_info))
821 line_match = match_pattern(search_info.compiled, search_info.text,
822 cline, line_len, &sp, &ep, 0, search_type);
828 if (search_type & SRCH_FIND_ALL)
832 * We are supposed to find all matches in the range.
833 * Just add the matches in this line to the
834 * hilite list and keep searching.
836 hilite_line(linepos, cline, line_len, chpos, sp, ep, cvt_ops);
838 } else if (--matches <= 0)
841 * Found the one match we're looking for.
845 if (hilite_search == OPT_ON)
848 * Clear the hilite list and add only
849 * the matches in this one line.
852 hilite_line(linepos, cline, line_len, chpos, sp, ep, cvt_ops);
857 if (plinepos != NULL)
869 * search for a pattern in history. If found, compile that pattern.
872 hist_pattern(search_type)
878 set_mlist(ml_search, 0);
879 pattern = cmd_lastpattern();
883 if (set_pattern(&search_info, pattern, search_type) < 0)
887 if (hilite_search == OPT_ONPLUS && !hide_hilite)
892 #else /* CMD_HISTORY */
894 #endif /* CMD_HISTORY */
898 * Search for the n-th occurrence of a specified pattern,
899 * either forward or backward.
900 * Return the number of matches not yet found in this file
901 * (that is, n minus the number of matches found).
902 * Return -1 if the search should be aborted.
903 * Caller may continue the search in another file
904 * if less than n matches are found in this file.
907 search(search_type, pattern, n)
914 if (pattern == NULL || *pattern == '\0')
917 * A null pattern means use the previously compiled pattern.
919 search_type |= SRCH_AFTER_TARGET;
920 if (!prev_pattern(&search_info) && !hist_pattern(search_type))
922 error("No previous regular expression", NULL_PARG);
925 if ((search_type & SRCH_NO_REGEX) !=
926 (search_info.search_type & SRCH_NO_REGEX))
928 error("Please re-enter search pattern", NULL_PARG);
932 if (hilite_search == OPT_ON)
935 * Erase the highlights currently on screen.
936 * If the search fails, we'll redisplay them later.
940 if (hilite_search == OPT_ONPLUS && hide_hilite)
943 * Highlight any matches currently on screen,
944 * before we actually start the search.
954 * Compile the pattern.
956 if (set_pattern(&search_info, pattern, search_type) < 0)
962 * Erase the highlights currently on screen.
963 * Also permanently delete them from the hilite list.
969 if (hilite_search == OPT_ONPLUS)
972 * Highlight any matches currently on screen,
973 * before we actually start the search.
981 * Figure out where to start the search.
983 pos = search_pos(search_type);
984 if (pos == NULL_POSITION)
987 * Can't find anyplace to start searching from.
989 if (search_type & SRCH_PAST_EOF)
991 /* repaint(); -- why was this here? */
992 error("Nothing to search", NULL_PARG);
996 n = search_range(pos, NULL_POSITION, search_type, n, -1,
997 &pos, (POSITION*)NULL);
1001 * Search was unsuccessful.
1004 if (hilite_search == OPT_ON && n > 0)
1006 * Redisplay old hilites.
1013 if (!(search_type & SRCH_NO_MOVE))
1016 * Go to the matching line.
1018 jump_loc(pos, jump_sline);
1022 if (hilite_search == OPT_ON)
1024 * Display new hilites in the matching line.
1034 * Prepare hilites in a given range of the file.
1036 * The pair (prep_startpos,prep_endpos) delimits a contiguous region
1037 * of the file that has been "prepared"; that is, scanned for matches for
1038 * the current search pattern, and hilites have been created for such matches.
1039 * If prep_startpos == NULL_POSITION, the prep region is empty.
1040 * If prep_endpos == NULL_POSITION, the prep region extends to EOF.
1041 * prep_hilite asks that the range (spos,epos) be covered by the prep region.
1044 prep_hilite(spos, epos, maxlines)
1049 POSITION nprep_startpos = prep_startpos;
1050 POSITION nprep_endpos = prep_endpos;
1057 * Search beyond where we're asked to search, so the prep region covers
1058 * more than we need. Do one big search instead of a bunch of small ones.
1060 #define SEARCH_MORE (3*size_linebuf)
1062 if (!prev_pattern(&search_info) && !is_filtering())
1066 * If we're limited to a max number of lines, figure out the
1067 * file position we should stop at.
1070 max_epos = NULL_POSITION;
1074 for (i = 0; i < maxlines; i++)
1075 max_epos = forw_raw_line(max_epos, (char **)NULL, (int *)NULL);
1080 * The range that we need to search (spos,epos); and the range that
1081 * the "prep" region will then cover (nprep_startpos,nprep_endpos).
1084 if (prep_startpos == NULL_POSITION ||
1085 (epos != NULL_POSITION && epos < prep_startpos) ||
1089 * New range is not contiguous with old prep region.
1090 * Discard the old prep region and start a new one.
1094 if (epos != NULL_POSITION)
1095 epos += SEARCH_MORE;
1096 nprep_startpos = spos;
1100 * New range partially or completely overlaps old prep region.
1102 if (epos == NULL_POSITION)
1105 * New range goes to end of file.
1108 } else if (epos > prep_endpos)
1111 * New range ends after old prep region.
1112 * Extend prep region to end at end of new range.
1114 epos += SEARCH_MORE;
1115 } else /* (epos <= prep_endpos) */
1118 * New range ends within old prep region.
1119 * Truncate search to end at start of old prep region.
1121 epos = prep_startpos;
1124 if (spos < prep_startpos)
1127 * New range starts before old prep region.
1128 * Extend old prep region backwards to start at
1129 * start of new range.
1131 if (spos < SEARCH_MORE)
1134 spos -= SEARCH_MORE;
1135 nprep_startpos = spos;
1136 } else /* (spos >= prep_startpos) */
1139 * New range starts within or after old prep region.
1140 * Trim search to start at end of old prep region.
1146 if (epos != NULL_POSITION && max_epos != NULL_POSITION &&
1149 * Don't go past the max position we're allowed.
1153 if (epos == NULL_POSITION || epos > spos)
1155 int search_type = SRCH_FORW | SRCH_FIND_ALL;
1156 search_type |= (search_info.search_type & SRCH_NO_REGEX);
1157 result = search_range(spos, epos, search_type, 0,
1158 maxlines, (POSITION*)NULL, &new_epos);
1161 if (prep_endpos == NULL_POSITION || new_epos > prep_endpos)
1162 nprep_endpos = new_epos;
1164 prep_startpos = nprep_startpos;
1165 prep_endpos = nprep_endpos;
1169 * Set the pattern to be used for line filtering.
1172 set_filter_pattern(pattern, search_type)
1177 if (pattern == NULL || *pattern == '\0')
1178 clear_pattern(&filter_info);
1180 set_pattern(&filter_info, pattern, search_type);
1185 * Is there a line filter in effect?
1190 if (ch_getflags() & CH_HELPFILE)
1192 return prev_pattern(&filter_info);
1198 * This function is called by the V8 regcomp to report
1199 * errors in regular expressions.