#include "less.h"
#include "position.h"
extern int hit_eof;
extern int jump_sline;
extern int squished;
extern int screen_trashed;
extern int sc_width, sc_height;
extern int show_attn;
public void
jump_forw()
{
POSITION pos;
if (ch_end_seek())
{
error("Cannot seek to end of file", NULL_PARG);
return;
}
pos = back_line(ch_tell());
if (pos == NULL_POSITION)
jump_loc((POSITION)0, sc_height-1);
else
jump_loc(pos, sc_height-1);
}
public void
jump_back(n)
int n;
{
POSITION pos;
PARG parg;
pos = find_pos(n);
if (pos != NULL_POSITION && ch_seek(pos) == 0)
{
if (show_attn)
set_attnpos(pos);
jump_loc(pos, jump_sline);
} else if (n <= 1 && ch_beg_seek() == 0)
{
jump_loc(ch_tell(), jump_sline);
error("Cannot seek to beginning of file", NULL_PARG);
} else
{
parg.p_int = n;
error("Cannot seek to line number %d", &parg);
}
}
public void
repaint()
{
struct scrpos scrpos;
get_scrpos(&scrpos);
pos_clear();
jump_loc(scrpos.pos, scrpos.ln);
}
public void
jump_percent(percent)
int percent;
{
POSITION pos, len;
if ((len = ch_length()) == NULL_POSITION)
{
ierror("Determining length of file", NULL_PARG);
ch_end_seek();
}
if ((len = ch_length()) == NULL_POSITION)
{
error("Don't know length of file", NULL_PARG);
return;
}
pos = percent_pos(len, percent);
if (pos >= len)
pos = len-1;
jump_line_loc(pos, jump_sline);
}
public void
jump_line_loc(pos, sline)
POSITION pos;
int sline;
{
int c;
if (ch_seek(pos) == 0)
{
while ((c = ch_back_get()) != '\n' && c != EOI)
;
if (c == '\n')
(void) ch_forw_get();
pos = ch_tell();
}
if (show_attn)
set_attnpos(pos);
jump_loc(pos, sline);
}
public void
jump_loc(pos, sline)
POSITION pos;
int sline;
{
register int nline;
POSITION tpos;
POSITION bpos;
sline = adjsline(sline);
if ((nline = onscreen(pos)) >= 0)
{
nline -= sline;
if (nline > 0)
forw(nline, position(BOTTOM_PLUS_ONE), 1, 0, 0);
else
back(-nline, position(TOP), 1, 0);
if (show_attn)
repaint_hilite(1);
return;
}
if (ch_seek(pos))
{
error("Cannot seek to that file position", NULL_PARG);
return;
}
tpos = position(TOP);
bpos = position(BOTTOM_PLUS_ONE);
if (tpos == NULL_POSITION || pos >= tpos)
{
for (nline = 0; nline < sline; nline++)
{
if (bpos != NULL_POSITION && pos <= bpos)
{
forw(sc_height-sline+nline-1, bpos, 1, 0, 0);
if (show_attn)
repaint_hilite(1);
return;
}
pos = back_line(pos);
if (pos == NULL_POSITION)
{
break;
}
}
lastmark();
hit_eof = 0;
squished = 0;
screen_trashed = 0;
forw(sc_height-1, pos, 1, 0, sline-nline);
} else
{
for (nline = sline; nline < sc_height - 1; nline++)
{
pos = forw_line(pos);
if (pos == NULL_POSITION)
{
break;
}
if (pos >= tpos)
{
back(nline+1, tpos, 1, 0);
if (show_attn)
repaint_hilite(1);
return;
}
}
lastmark();
clear();
screen_trashed = 0;
add_back_pos(pos);
back(sc_height-1, pos, 1, 0);
}
}