=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/less/search.c,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- src/usr.bin/less/search.c 2011/09/21 19:01:49 1.7 +++ src/usr.bin/less/search.c 2014/04/25 13:38:21 1.8 @@ -1,11 +1,10 @@ /* - * Copyright (C) 1984-2011 Mark Nudelman + * Copyright (C) 1984-2012 Mark Nudelman * * You may distribute under the terms of either the GNU General Public * License or the Less License, as specified in the README file. * - * For more information about less, or for information on how to - * contact the author, see the README file. + * For more information, see the README file. */ @@ -66,6 +65,12 @@ char* text; int search_type; }; + +#if NO_REGEX +#define info_compiled(info) ((void*)0) +#else +#define info_compiled(info) ((info)->compiled) +#endif static struct pattern_info search_info; static struct pattern_info filter_info; @@ -98,10 +103,12 @@ char *pattern; int search_type; { +#if !NO_REGEX if (pattern == NULL) - CLEAR_PATTERN(search_info.compiled); + CLEAR_PATTERN(info->compiled); else if (compile_pattern(pattern, search_type, &info->compiled) < 0) return -1; +#endif /* Pattern compiled successfully; save the text too. */ if (info->text != NULL) free(info->text); @@ -132,7 +139,9 @@ if (info->text != NULL) free(info->text); info->text = NULL; +#if !NO_REGEX uncompile_pattern(&info->compiled); +#endif } /* @@ -188,9 +197,11 @@ prev_pattern(info) struct pattern_info *info; { - if (info->search_type & SRCH_NO_REGEX) - return (info->text != NULL); - return (!is_null_pattern(info->compiled)); +#if !NO_REGEX + if ((info->search_type & SRCH_NO_REGEX) == 0) + return (!is_null_pattern(info->compiled)); +#endif + return (info->text != NULL); } #if HILITE_SEARCH @@ -473,6 +484,47 @@ } /* + * Hilight every character in a range of displayed characters. + */ + static void +create_hilites(linepos, start_index, end_index, chpos) + POSITION linepos; + int start_index; + int end_index; + int *chpos; +{ + struct hilite *hl; + int i; + + /* Start the first hilite. */ + hl = (struct hilite *) ecalloc(1, sizeof(struct hilite)); + hl->hl_startpos = linepos + chpos[start_index]; + + /* + * Step through the displayed chars. + * If the source position (before cvt) of the char is one more + * than the source pos of the previous char (the usual case), + * just increase the size of the current hilite by one. + * Otherwise (there are backspaces or something involved), + * finish the current hilite and start a new one. + */ + for (i = start_index+1; i <= end_index; i++) + { + if (chpos[i] != chpos[i-1] + 1 || i == end_index) + { + hl->hl_endpos = linepos + chpos[i-1] + 1; + add_hilite(&hilite_anchor, hl); + /* Start new hilite unless this is the last char. */ + if (i < end_index) + { + hl = (struct hilite *) ecalloc(1, sizeof(struct hilite)); + hl->hl_startpos = linepos + chpos[i]; + } + } + } +} + +/* * Make a hilite for each string in a physical line which matches * the current pattern. * sp,ep delimit the first match already found. @@ -489,7 +541,6 @@ { char *searchp; char *line_end = line + line_len; - struct hilite *hl; if (sp == NULL || ep == NULL) return; @@ -505,13 +556,7 @@ */ searchp = line; do { - if (ep > sp) - { - hl = (struct hilite *) ecalloc(1, sizeof(struct hilite)); - hl->hl_startpos = linepos + chpos[sp-line]; - hl->hl_endpos = linepos + chpos[ep-line]; - add_hilite(&hilite_anchor, hl); - } + create_hilites(linepos, sp-line, ep-line, chpos); /* * If we matched more than zero characters, * move to the first char after the string we matched. @@ -523,7 +568,7 @@ searchp++; else /* end of line */ break; - } while (match_pattern(search_info.compiled, search_info.text, + } while (match_pattern(info_compiled(&search_info), search_info.text, searchp, line_end - searchp, &sp, &ep, 1, search_info.search_type)); } #endif @@ -795,7 +840,7 @@ * If so, add an entry to the filter list. */ if ((search_type & SRCH_FIND_ALL) && prev_pattern(&filter_info)) { - int line_filter = match_pattern(filter_info.compiled, filter_info.text, + int line_filter = match_pattern(info_compiled(&filter_info), filter_info.text, cline, line_len, &sp, &ep, 0, filter_info.search_type); if (line_filter) { @@ -815,7 +860,7 @@ */ if (prev_pattern(&search_info)) { - line_match = match_pattern(search_info.compiled, search_info.text, + line_match = match_pattern(info_compiled(&search_info), search_info.text, cline, line_len, &sp, &ep, 0, search_type); if (line_match) {