3 * Copyright (C) 1984-2000 Mark Nudelman
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Less License, as specified in the README file.
8 * For more information about less, or for information on how to
9 * contact the author, see the README file.
14 * Routines to search a file for a pattern.
20 #define MINPOS(a,b) (((a) < (b)) ? (a) : (b))
21 #define MAXPOS(a,b) (((a) > (b)) ? (a) : (b))
23 #if HAVE_POSIX_REGCOMP
26 #define REGCOMP_FLAG (more_mode ? 0 : REG_EXTENDED)
28 #define REGCOMP_FLAG 0
50 extern int how_search;
54 extern int jump_sline;
57 extern int status_col;
58 extern POSITION start_attnpos;
59 extern POSITION end_attnpos;
61 extern int hilite_search;
62 extern int screen_trashed;
63 extern int size_linebuf;
65 extern int can_goto_line;
66 static int hide_hilite;
67 static POSITION prep_startpos;
68 static POSITION prep_endpos;
72 struct hilite *hl_next;
76 static struct hilite hilite_anchor = { NULL, NULL_POSITION, NULL_POSITION };
77 #define hl_first hl_next
81 * These are the static variables that represent the "remembered"
84 #if HAVE_POSIX_REGCOMP
85 static regex_t *regpattern = NULL;
88 pcre *regpattern = NULL;
94 static char *cpattern = NULL;
97 static struct regexp *regpattern = NULL;
100 static int is_caseless;
101 static int is_ucase_pattern;
102 static int last_search_type;
103 static char *last_pattern = NULL;
106 * Convert text. Perform one or more of these transformations:
108 #define CVT_TO_LC 01 /* Convert upper-case to lower-case */
109 #define CVT_BS 02 /* Do backspace processing */
110 #define CVT_CRLF 04 /* Remove CR after LF */
113 cvt_text(odst, osrc, ops)
121 for (src = osrc, dst = odst; *src != '\0'; src++, dst++)
123 if ((ops & CVT_TO_LC) && isupper((unsigned char) *src))
124 /* Convert uppercase to lowercase. */
125 *dst = tolower((unsigned char) *src);
126 else if ((ops & CVT_BS) && *src == '\b' && dst > odst)
127 /* Delete BS and preceding char. */
133 if ((ops & CVT_CRLF) && dst > odst && dst[-1] == '\r')
139 * Are there any uppercase letters in this string?
147 for (p = s; *p != '\0'; p++)
148 if (isupper((unsigned char) *p))
154 * Is there a previous (remembered) search pattern?
159 if (last_search_type & SRCH_NO_REGEX)
160 return (last_pattern != NULL);
161 #if HAVE_POSIX_REGCOMP
162 return (regpattern != NULL);
165 return (regpattern != NULL);
168 return (re_pattern != 0);
171 return (cpattern != NULL);
174 return (regpattern != NULL);
177 return (last_pattern != NULL);
183 * Repaint the hilites currently displayed on the screen.
184 * Repaint each line which contains highlighted text.
185 * If on==0, force all hilites off.
194 int save_hide_hilite;
199 save_hide_hilite = hide_hilite;
210 hide_hilite = save_hide_hilite;
214 for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++)
216 pos = position(slinenum);
217 if (pos == NULL_POSITION)
219 epos = position(slinenum+1);
221 * If any character in the line is highlighted,
224 if (is_hilited(pos, epos, 1))
226 (void) forw_line(pos);
231 hide_hilite = save_hide_hilite;
235 * Clear the attn hilite.
241 POSITION old_start_attnpos;
242 POSITION old_end_attnpos;
246 if (start_attnpos == NULL_POSITION)
248 old_start_attnpos = start_attnpos;
249 old_end_attnpos = end_attnpos;
250 start_attnpos = end_attnpos = NULL_POSITION;
260 for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++)
262 pos = position(slinenum);
263 if (pos == NULL_POSITION)
265 epos = position(slinenum+1);
266 if (pos < old_end_attnpos &&
267 (epos == NULL_POSITION || epos > old_start_attnpos))
269 (void) forw_line(pos);
278 * Hide search string highlighting.
285 error("No previous regular expression", NULL_PARG);
289 hide_hilite = !hide_hilite;
295 * Compile a search pattern, for future use by match_pattern.
298 compile_pattern(pattern, search_type)
302 if ((search_type & SRCH_NO_REGEX) == 0)
304 #if HAVE_POSIX_REGCOMP
305 regex_t *s = (regex_t *) ecalloc(1, sizeof(regex_t));
306 if (regcomp(s, pattern, REGCOMP_FLAG))
309 error("Invalid pattern", NULL_PARG);
312 if (regpattern != NULL)
318 const char *errstring;
321 comp = pcre_compile(pattern, 0,
322 &errstring, &erroffset, NULL);
325 parg.p_string = (char *) errstring;
333 if ((parg.p_string = re_comp(pattern)) != NULL)
342 if ((s = regcmp(pattern, 0)) == NULL)
344 error("Invalid pattern", NULL_PARG);
347 if (cpattern != NULL)
353 if ((s = regcomp(pattern)) == NULL)
356 * regcomp has already printed an error message
361 if (regpattern != NULL)
367 if (last_pattern != NULL)
369 last_pattern = (char *) calloc(1, strlen(pattern)+1);
370 if (last_pattern != NULL)
371 strcpy(last_pattern, pattern);
373 last_search_type = search_type;
378 * Forget that we have a compiled pattern.
383 #if HAVE_POSIX_REGCOMP
384 if (regpattern != NULL)
389 if (regpattern != NULL)
390 pcre_free(regpattern);
397 if (cpattern != NULL)
402 if (regpattern != NULL)
410 * Perform a pattern match with the previously compiled pattern.
411 * Set sp and ep to the start and end of the matched string.
414 match_pattern(line, sp, ep, notbol)
422 if (last_search_type & SRCH_NO_REGEX)
423 return (match(last_pattern, line, sp, ep));
425 #if HAVE_POSIX_REGCOMP
428 int flags = (notbol) ? REG_NOTBOL : 0;
429 matched = !regexec(regpattern, line, 1, &rm, flags);
433 *sp = line + rm.rm_so;
434 *ep = line + rm.rm_eo;
443 int flags = (notbol) ? PCRE_NOTBOL : 0;
445 matched = pcre_exec(regpattern, NULL, line, strlen(line),
446 0, flags, ovector, 3) >= 0;
449 *sp = line + ovector[0];
450 *ep = line + ovector[1];
454 matched = (re_exec(line) == 1);
456 * re_exec doesn't seem to provide a way to get the matched string.
461 *ep = regex(cpattern, line);
462 matched = (*ep != NULL);
469 matched = regexec2(regpattern, line, notbol);
471 matched = regexec(regpattern, line);
475 *sp = regpattern->startp[0];
476 *ep = regpattern->endp[0];
479 matched = match(last_pattern, line, sp, ep);
486 * Clear the hilite list.
492 struct hilite *nexthl;
494 for (hl = hilite_anchor.hl_first; hl != NULL; hl = nexthl)
496 nexthl = hl->hl_next;
499 hilite_anchor.hl_first = NULL;
500 prep_startpos = prep_endpos = NULL_POSITION;
504 * Should any characters in a specified range be highlighted?
505 * If nohide is nonzero, don't consider hide_hilite.
508 is_hilited(pos, epos, nohide)
516 start_attnpos != NULL_POSITION &&
518 (epos == NULL_POSITION || epos > start_attnpos))
520 * The attn line overlaps this range.
524 if (hilite_search == 0)
526 * Not doing highlighting.
530 if (!nohide && hide_hilite)
532 * Highlighting is hidden.
537 * Look at each highlight and see if any part of it falls in the range.
539 for (hl = hilite_anchor.hl_first; hl != NULL; hl = hl->hl_next)
541 if (hl->hl_endpos > pos &&
542 (epos == NULL_POSITION || epos > hl->hl_startpos))
549 * Add a new hilite to a hilite list.
552 add_hilite(anchor, hl)
553 struct hilite *anchor;
559 * Hilites are sorted in the list; find where new one belongs.
560 * Insert new one after ihl.
562 for (ihl = anchor; ihl->hl_next != NULL; ihl = ihl->hl_next)
564 if (ihl->hl_next->hl_startpos > hl->hl_startpos)
569 * Truncate hilite so it doesn't overlap any existing ones
570 * above and below it.
573 hl->hl_startpos = MAXPOS(hl->hl_startpos, ihl->hl_endpos);
574 if (ihl->hl_next != NULL)
575 hl->hl_endpos = MINPOS(hl->hl_endpos, ihl->hl_next->hl_startpos);
576 if (hl->hl_startpos >= hl->hl_endpos)
579 * Hilite was truncated out of existence.
584 hl->hl_next = ihl->hl_next;
589 * Adjust hl_startpos & hl_endpos to account for backspace processing.
592 adj_hilite(anchor, linepos)
593 struct hilite *anchor;
603 * The line was already scanned and hilites were added (in hilite_line).
604 * But it was assumed that each char position in the line
605 * correponds to one char position in the file.
606 * This may not be true if there are backspaces in the line.
607 * Get the raw line again. Look at each character.
609 (void) forw_raw_line(linepos, &line);
610 opos = npos = linepos;
611 hl = anchor->hl_first;
616 * See if we need to adjust the current hl_startpos or
617 * hl_endpos. After adjusting startpos[i], move to endpos[i].
618 * After adjusting endpos[i], move to startpos[i+1].
619 * The hilite list must be sorted thus:
620 * startpos[0] < endpos[0] <= startpos[1] < endpos[1] <= etc.
622 if (checkstart && hl->hl_startpos == opos)
624 hl->hl_startpos = npos;
626 continue; /* {{ not really necessary }} */
627 } else if (!checkstart && hl->hl_endpos == opos)
629 hl->hl_endpos = npos;
632 continue; /* {{ necessary }} */
639 while (line[0] == '\b' && line[1] != '\0')
642 * Found a backspace. The file position moves
643 * forward by 2 relative to the processed line
644 * which was searched in hilite_line.
653 * Make a hilite for each string in a physical line which matches
654 * the current pattern.
655 * sp,ep delimit the first match already found.
658 hilite_line(linepos, line, sp, ep)
666 struct hilite hilites;
668 if (sp == NULL || ep == NULL)
671 * sp and ep delimit the first match in the line.
672 * Mark the corresponding file positions, then
673 * look for further matches and mark them.
674 * {{ This technique, of calling match_pattern on subsequent
675 * substrings of the line, may mark more than is correct
676 * if the pattern starts with "^". This bug is fixed
677 * for those regex functions that accept a notbol parameter
678 * (currently POSIX and V8-with-regexec2). }}
682 * Put the hilites into a temporary list until they're adjusted.
684 hilites.hl_first = NULL;
689 * Assume that each char position in the "line"
690 * buffer corresponds to one char position in the file.
691 * This is not quite true; we need to adjust later.
693 hl = (struct hilite *) ecalloc(1, sizeof(struct hilite));
694 hl->hl_startpos = linepos + (sp-line);
695 hl->hl_endpos = linepos + (ep-line);
696 add_hilite(&hilites, hl);
699 * If we matched more than zero characters,
700 * move to the first char after the string we matched.
701 * If we matched zero, just move to the next char.
705 else if (*searchp != '\0')
707 else /* end of line */
709 } while (match_pattern(searchp, &sp, &ep, 1));
711 if (bs_mode == BS_SPECIAL)
714 * If there were backspaces in the original line, they
715 * were removed, and hl_startpos/hl_endpos are not correct.
716 * {{ This is very ugly. }}
718 adj_hilite(&hilites, linepos);
721 * Now put the hilites into the real list.
723 while ((hl = hilites.hl_next) != NULL)
725 hilites.hl_next = hl->hl_next;
726 add_hilite(&hilite_anchor, hl);
732 * Change the caseless-ness of searches.
733 * Updates the internal search state to reflect a change in the -i flag.
738 if (!is_ucase_pattern)
740 * Pattern did not have uppercase.
741 * Just set the search caselessness to the global caselessness.
743 is_caseless = caseless;
746 * Pattern did have uppercase.
747 * Discard the pattern; we can't change search caselessness now.
754 * Find matching text which is currently on screen and highlight it.
759 struct scrpos scrpos;
762 if (scrpos.pos == NULL_POSITION)
764 prep_hilite(scrpos.pos, position(BOTTOM_PLUS_ONE), -1);
769 * Change highlighting parameters.
775 * Erase any highlights currently on screen.
780 if (hilite_search == OPT_ONPLUS)
782 * Display highlights.
789 * Figure out where to start a search.
792 search_pos(search_type)
801 * Start at the beginning (or end) of the file.
802 * The empty_screen() case is mainly for
803 * command line initiated searches;
804 * for example, "+/xyz" on the command line.
805 * Also for multi-file (SRCH_PAST_EOF) searches.
807 if (search_type & SRCH_FORW)
813 if (pos == NULL_POSITION)
815 (void) ch_end_seek();
824 * Search does not include current screen.
826 if (search_type & SRCH_FORW)
827 linenum = BOTTOM_PLUS_ONE;
830 pos = position(linenum);
834 * Search includes current screen.
835 * It starts at the jump target (if searching backwards),
836 * or at the jump target plus one (if forwards).
838 linenum = adjsline(jump_sline);
839 pos = position(linenum);
840 if (search_type & SRCH_FORW)
842 pos = forw_raw_line(pos, (char **)NULL);
843 while (pos == NULL_POSITION)
845 if (++linenum >= sc_height)
847 pos = position(linenum);
851 while (pos == NULL_POSITION)
855 pos = position(linenum);
863 * Search a subset of the file, specified by start/end position.
866 search_range(pos, endpos, search_type, matches, maxlines, plinepos, pendpos)
879 POSITION linepos, oldpos;
881 linenum = find_linenum(pos);
886 * Get lines until we find a matching one or until
887 * we hit end-of-file (or beginning-of-file if we're
888 * going backwards), or until we hit the end position.
893 * A signal aborts the search.
898 if ((endpos != NULL_POSITION && pos >= endpos) || maxlines == 0)
901 * Reached end position without a match.
910 if (search_type & SRCH_FORW)
913 * Read the next line, and save the
914 * starting position of that line in linepos.
917 pos = forw_raw_line(pos, &line);
923 * Read the previous line and save the
924 * starting position of that line in linepos.
926 pos = back_raw_line(pos, &line);
932 if (pos == NULL_POSITION)
935 * Reached EOF/BOF without a match.
943 * If we're using line numbers, we might as well
944 * remember the information we have now (the position
945 * and line number of the current line).
946 * Don't do it for every line because it slows down
947 * the search. Remember the line number only if
948 * we're "far" from the last place we remembered it.
950 if (linenums && abs((int)(pos - oldpos)) > 1024)
951 add_lnum(linenum, pos);
955 * If it's a caseless search, convert the line to lowercase.
956 * If we're doing backspace processing, delete backspaces.
958 if (is_caseless || bs_mode == BS_SPECIAL)
963 if (bs_mode == BS_SPECIAL)
965 if (bs_mode != BS_CONTROL)
967 cvt_text(line, line, ops);
968 } else if (bs_mode != BS_CONTROL)
970 cvt_text(line, line, CVT_CRLF);
974 * Test the next line to see if we have a match.
975 * We are successful if we either want a match and got one,
976 * or if we want a non-match and got one.
978 line_match = match_pattern(line, &sp, &ep, 0);
979 line_match = (!(search_type & SRCH_NO_MATCH) && line_match) ||
980 ((search_type & SRCH_NO_MATCH) && !line_match);
986 if (search_type & SRCH_FIND_ALL)
990 * We are supposed to find all matches in the range.
991 * Just add the matches in this line to the
992 * hilite list and keep searching.
995 hilite_line(linepos, line, sp, ep);
997 } else if (--matches <= 0)
1000 * Found the one match we're looking for.
1004 if (hilite_search == 1)
1007 * Clear the hilite list and add only
1008 * the matches in this one line.
1012 hilite_line(linepos, line, sp, ep);
1015 if (plinepos != NULL)
1016 *plinepos = linepos;
1023 * Search for the n-th occurrence of a specified pattern,
1024 * either forward or backward.
1025 * Return the number of matches not yet found in this file
1026 * (that is, n minus the number of matches found).
1027 * Return -1 if the search should be aborted.
1028 * Caller may continue the search in another file
1029 * if less than n matches are found in this file.
1032 search(search_type, pattern, n)
1040 if (pattern == NULL || *pattern == '\0')
1043 * A null pattern means use the previously compiled pattern.
1045 if (!prev_pattern())
1047 error("No previous regular expression", NULL_PARG);
1050 if ((search_type & SRCH_NO_REGEX) !=
1051 (last_search_type & SRCH_NO_REGEX))
1053 error("Please re-enter search pattern", NULL_PARG);
1057 if (hilite_search == OPT_ON)
1060 * Erase the highlights currently on screen.
1061 * If the search fails, we'll redisplay them later.
1065 if (hilite_search == OPT_ONPLUS && hide_hilite)
1068 * Highlight any matches currently on screen,
1069 * before we actually start the search.
1079 * Compile the pattern.
1081 ucase = is_ucase(pattern);
1082 if (caseless == OPT_ONPLUS)
1083 cvt_text(pattern, pattern, CVT_TO_LC);
1084 if (compile_pattern(pattern, search_type) < 0)
1087 * Ignore case if -I is set OR
1088 * -i is set AND the pattern is all lowercase.
1090 is_ucase_pattern = ucase;
1091 if (is_ucase_pattern && caseless != OPT_ONPLUS)
1094 is_caseless = caseless;
1099 * Erase the highlights currently on screen.
1100 * Also permanently delete them from the hilite list.
1106 if (hilite_search == OPT_ONPLUS)
1109 * Highlight any matches currently on screen,
1110 * before we actually start the search.
1118 * Figure out where to start the search.
1120 pos = search_pos(search_type);
1121 if (pos == NULL_POSITION)
1124 * Can't find anyplace to start searching from.
1126 if (search_type & SRCH_PAST_EOF)
1128 /* repaint(); -- why was this here? */
1129 error("Nothing to search", NULL_PARG);
1133 n = search_range(pos, NULL_POSITION, search_type, n, -1,
1134 &pos, (POSITION*)NULL);
1138 * Search was unsuccessful.
1141 if (hilite_search == OPT_ON && n > 0)
1143 * Redisplay old hilites.
1150 if (!(search_type & SRCH_NO_MOVE))
1153 * Go to the matching line.
1155 jump_loc(pos, jump_sline);
1159 if (hilite_search == OPT_ON)
1161 * Display new hilites in the matching line.
1171 * Prepare hilites in a given range of the file.
1173 * The pair (prep_startpos,prep_endpos) delimits a contiguous region
1174 * of the file that has been "prepared"; that is, scanned for matches for
1175 * the current search pattern, and hilites have been created for such matches.
1176 * If prep_startpos == NULL_POSITION, the prep region is empty.
1177 * If prep_endpos == NULL_POSITION, the prep region extends to EOF.
1178 * prep_hilite asks that the range (spos,epos) be covered by the prep region.
1181 prep_hilite(spos, epos, maxlines)
1186 POSITION nprep_startpos = prep_startpos;
1187 POSITION nprep_endpos = prep_endpos;
1193 * Search beyond where we're asked to search, so the prep region covers
1194 * more than we need. Do one big search instead of a bunch of small ones.
1196 #define SEARCH_MORE (3*size_linebuf)
1198 if (!prev_pattern())
1202 * If we're limited to a max number of lines, figure out the
1203 * file position we should stop at.
1206 max_epos = NULL_POSITION;
1210 for (i = 0; i < maxlines; i++)
1211 max_epos = forw_raw_line(max_epos, (char **)NULL);
1216 * The range that we need to search (spos,epos); and the range that
1217 * the "prep" region will then cover (nprep_startpos,nprep_endpos).
1220 if (prep_startpos == NULL_POSITION ||
1221 (epos != NULL_POSITION && epos < prep_startpos) ||
1225 * New range is not contiguous with old prep region.
1226 * Discard the old prep region and start a new one.
1229 if (epos != NULL_POSITION)
1230 epos += SEARCH_MORE;
1231 nprep_startpos = spos;
1235 * New range partially or completely overlaps old prep region.
1237 if (epos == NULL_POSITION)
1240 * New range goes to end of file.
1243 } else if (epos > prep_endpos)
1246 * New range ends after old prep region.
1247 * Extend prep region to end at end of new range.
1249 epos += SEARCH_MORE;
1250 } else /* (epos <= prep_endpos) */
1253 * New range ends within old prep region.
1254 * Truncate search to end at start of old prep region.
1256 epos = prep_startpos;
1259 if (spos < prep_startpos)
1262 * New range starts before old prep region.
1263 * Extend old prep region backwards to start at
1264 * start of new range.
1266 if (spos < SEARCH_MORE)
1269 spos -= SEARCH_MORE;
1270 nprep_startpos = spos;
1271 } else /* (spos >= prep_startpos) */
1274 * New range starts within or after old prep region.
1275 * Trim search to start at end of old prep region.
1281 if (epos != NULL_POSITION && max_epos != NULL_POSITION &&
1284 * Don't go past the max position we're allowed.
1288 if (epos == NULL_POSITION || epos > spos)
1290 result = search_range(spos, epos, SRCH_FORW|SRCH_FIND_ALL, 0,
1291 maxlines, (POSITION*)NULL, &new_epos);
1294 if (prep_endpos == NULL_POSITION || new_epos > prep_endpos)
1295 nprep_endpos = new_epos;
1297 prep_startpos = nprep_startpos;
1298 prep_endpos = nprep_endpos;
1303 * Simple pattern matching function.
1304 * It supports no metacharacters like *, etc.
1307 match(pattern, buf, pfound, pend)
1308 char *pattern, *buf;
1309 char **pfound, **pend;
1311 register char *pp, *lp;
1313 for ( ; *buf != '\0'; buf++)
1315 for (pp = pattern, lp = buf; *pp == *lp; pp++, lp++)
1316 if (*pp == '\0' || *lp == '\0')
1332 * This function is called by the V8 regcomp to report
1333 * errors in regular expressions.