Skip to content

Commit

Permalink
Improve search exposure changes (in
Browse files Browse the repository at this point in the history
ddae693 and
4a35631) to
handle multibyte chars etc.
  • Loading branch information
gwsw committed Jan 26, 2021
1 parent f617547 commit d1fe3e1
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 23 deletions.
23 changes: 21 additions & 2 deletions input.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ extern int show_attn;
* a line. The new position is the position of the first character
* of the NEXT line. The line obtained is the line starting at curr_pos.
*/
public POSITION
forw_line(curr_pos)
static POSITION
forw_line2(curr_pos, psegpos)
POSITION curr_pos;
int *psegpos;
{
POSITION base_pos;
POSITION new_pos;
Expand Down Expand Up @@ -192,6 +193,8 @@ forw_line(curr_pos)
} else
{
new_pos = ch_tell() - backchars;
if (psegpos != NULL)
*psegpos = new_pos;
endline = FALSE;
}
break;
Expand Down Expand Up @@ -244,6 +247,22 @@ forw_line(curr_pos)
return (new_pos);
}

public POSITION
forw_line(curr_pos)
POSITION curr_pos;
{
return forw_line2(curr_pos, NULL);
}

public POSITION
forw_line_seg(curr_pos)
POSITION curr_pos;
{
POSITION segpos;
(void) forw_line2(curr_pos, &segpos);
return segpos;
}

/*
* Get the previous line.
* A "current" position is passed and a "new" position is returned.
Expand Down
94 changes: 73 additions & 21 deletions search.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ extern int can_goto_line;
static int hide_hilite;
static POSITION prep_startpos;
static POSITION prep_endpos;
extern POSITION xxpos;

/*
* Structures for maintaining a set of ranges for hilites and filtered-out
Expand Down Expand Up @@ -1190,19 +1191,64 @@ matches_filters(pos, cline, line_len, chpos, linepos, sp, ep)
}
#endif

/*
* Get the position of the first char in the screen line which
* puts tpos on screen.
*/
static POSITION
get_lastlinepos(pos, tpos, sheight)
POSITION pos;
POSITION tpos;
int sheight;
{
int nlines;

for (nlines = 0;; nlines++)
{
POSITION npos = forw_line(pos);
if (npos > tpos)
{
if (nlines < sheight)
return NULL_POSITION;
return pos;
}
pos = npos;
}
}

/*
* Get the segment index of tpos in the line starting at pos.
* A segment is a string of printable chars that fills the screen width.
*/
static int
get_seg(pos, tpos)
POSITION pos;
POSITION tpos;
{
int seg;

for (seg = 0;; seg++)
{
POSITION segpos = forw_line_seg(pos);
if (segpos > tpos)
return seg;
pos = segpos;
}
}

/*
* Search a subset of the file, specified by start/end position.
*/
static int
search_range(pos, endpos, search_type, matches, maxlines, plinepos, pendpos, pbotpos)
search_range(pos, endpos, search_type, matches, maxlines, plinepos, pendpos, plastlinepos)
POSITION pos;
POSITION endpos;
int search_type;
int matches;
int maxlines;
POSITION *plinepos;
POSITION *pendpos;
POSITION *pbotpos;
POSITION *plastlinepos;
{
char *line;
char *cline;
Expand All @@ -1215,6 +1261,7 @@ search_range(pos, endpos, search_type, matches, maxlines, plinepos, pendpos, pbo
int *chpos;
POSITION linepos, oldpos;
int swidth = sc_width - line_pfx_width();
int sheight = sc_height - sindex_from_sline(jump_sline);

linenum = find_linenum(pos);
oldpos = pos;
Expand Down Expand Up @@ -1400,33 +1447,38 @@ search_range(pos, endpos, search_type, matches, maxlines, plinepos, pendpos, pbo
*/
int start_off = sp - cline;
int end_off = ep - cline;
int min_hshift = end_off - swidth + 1;
int max_hshift = start_off;
int new_hshift = hshift;
if (new_hshift > max_hshift)
new_hshift = max_hshift;
if (new_hshift < min_hshift)
new_hshift = min_hshift;
if (new_hshift != hshift)
int save_hshift = hshift;
int sshift;
int eshift;
hshift = 0; /* make get_seg count screen lines */
chopline = FALSE;
//start_off += 1;
//if (start_off >= cvt_len)
// start_off = cvt_len - 1;
sshift = swidth * get_seg(linepos, linepos + chpos[start_off]);
eshift = swidth * get_seg(linepos, linepos + chpos[end_off]);
chopline = TRUE;
if (sshift >= save_hshift && eshift <= save_hshift)
{
hshift = save_hshift;
} else
{
hshift = new_hshift;
hshift = sshift;
screen_trashed = 1;
}
} else if (pbotpos != NULL)
} else if (plastlinepos != NULL)
{
/*
* If the line is so long that the highlighted match
* won't be seen when the line is displayed normally
* (starting at the first char) because it fills the whole
* screen and more, scroll forward until the last char
* of the match appears in the last line on the screen.
* botpos is the position of the first char of that last line.
* lastlinepos is the position of the first char of that last line.
*/
int sheight = sc_height - sindex_from_sline(jump_sline) - 1;
int scr_size = swidth * sheight;
int end_off = ep - cline;
if (end_off >= scr_size)
*pbotpos = linepos + chpos[(end_off / swidth) * swidth];
if (end_off >= swidth * sheight / 4) /* heuristic */
*plastlinepos = get_lastlinepos(linepos, linepos + chpos[end_off], sheight);
}
free(cline);
free(chpos);
Expand Down Expand Up @@ -1511,7 +1563,7 @@ search(search_type, pattern, n)
{
POSITION pos;
POSITION opos;
POSITION botpos = NULL_POSITION;
POSITION lastlinepos = NULL_POSITION;

if (pattern == NULL || *pattern == '\0')
{
Expand Down Expand Up @@ -1604,7 +1656,7 @@ search(search_type, pattern, n)
}

n = search_range(pos, NULL_POSITION, search_type, n, -1,
&pos, (POSITION*)NULL, &botpos);
&pos, (POSITION*)NULL, &lastlinepos);
if (n != 0)
{
/*
Expand All @@ -1625,8 +1677,8 @@ search(search_type, pattern, n)
/*
* Go to the matching line.
*/
if (botpos != NULL_POSITION)
jump_loc(botpos, BOTTOM);
if (lastlinepos != NULL_POSITION)
jump_loc(lastlinepos, BOTTOM);
else if (pos != opos)
jump_loc(pos, jump_sline);
}
Expand Down

0 comments on commit d1fe3e1

Please sign in to comment.