2012-07-23 21:31:53 +00:00
|
|
|
/*
|
2017-04-29 06:30:21 +00:00
|
|
|
* Copyright (C) 1984-2017 Mark Nudelman
|
2012-07-23 21:31:53 +00:00
|
|
|
*
|
|
|
|
* 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, see the README file.
|
|
|
|
*/
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Routines which jump to a new location in the file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "less.h"
|
|
|
|
#include "position.h"
|
|
|
|
|
|
|
|
extern int jump_sline;
|
|
|
|
extern int squished;
|
|
|
|
extern int screen_trashed;
|
|
|
|
extern int sc_width, sc_height;
|
|
|
|
extern int show_attn;
|
2006-08-20 15:49:51 +00:00
|
|
|
extern int top_scroll;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Jump to the end of the file.
|
|
|
|
*/
|
|
|
|
public void
|
2017-04-25 03:42:16 +00:00
|
|
|
jump_forw()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
POSITION pos;
|
2007-10-08 16:14:52 +00:00
|
|
|
POSITION end_pos;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
if (ch_end_seek())
|
|
|
|
{
|
|
|
|
error("Cannot seek to end of file", NULL_PARG);
|
|
|
|
return;
|
|
|
|
}
|
2009-05-08 23:45:27 +00:00
|
|
|
/*
|
|
|
|
* Note; lastmark will be called later by jump_loc, but it fails
|
|
|
|
* because the position table has been cleared by pos_clear below.
|
|
|
|
* So call it here before calling pos_clear.
|
|
|
|
*/
|
|
|
|
lastmark();
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
|
|
|
* Position the last line in the file at the last screen line.
|
|
|
|
* Go back one line from the end of the file
|
|
|
|
* to get to the beginning of the last line.
|
|
|
|
*/
|
2007-10-08 16:14:52 +00:00
|
|
|
pos_clear();
|
|
|
|
end_pos = ch_tell();
|
|
|
|
pos = back_line(end_pos);
|
2000-05-22 09:53:22 +00:00
|
|
|
if (pos == NULL_POSITION)
|
2017-11-20 06:53:49 +00:00
|
|
|
jump_loc(ch_zero(), sc_height-1);
|
2000-05-22 09:53:22 +00:00
|
|
|
else
|
2007-10-08 16:14:52 +00:00
|
|
|
{
|
2000-05-22 09:53:22 +00:00
|
|
|
jump_loc(pos, sc_height-1);
|
2007-10-08 16:14:52 +00:00
|
|
|
if (position(sc_height-1) != end_pos)
|
|
|
|
repaint();
|
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
2016-01-04 00:22:34 +00:00
|
|
|
/*
|
|
|
|
* Jump to the last buffered line in the file.
|
|
|
|
*/
|
|
|
|
public void
|
2017-04-25 03:42:16 +00:00
|
|
|
jump_forw_buffered()
|
2016-01-04 00:22:34 +00:00
|
|
|
{
|
|
|
|
POSITION end;
|
|
|
|
|
|
|
|
if (ch_end_buffer_seek())
|
|
|
|
{
|
|
|
|
error("Cannot seek to end of buffers", NULL_PARG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
end = ch_tell();
|
|
|
|
if (end != NULL_POSITION && end > 0)
|
|
|
|
jump_line_loc(end-1, sc_height-1);
|
|
|
|
}
|
|
|
|
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
|
|
|
* Jump to line n in the file.
|
|
|
|
*/
|
|
|
|
public void
|
2017-04-25 03:42:16 +00:00
|
|
|
jump_back(linenum)
|
|
|
|
LINENUM linenum;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
POSITION pos;
|
|
|
|
PARG parg;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the position of the specified line.
|
|
|
|
* If we can seek there, just jump to it.
|
|
|
|
* If we can't seek, but we're trying to go to line number 1,
|
|
|
|
* use ch_beg_seek() to get as close as we can.
|
|
|
|
*/
|
2004-04-17 07:16:34 +00:00
|
|
|
pos = find_pos(linenum);
|
2000-05-22 09:53:22 +00:00
|
|
|
if (pos != NULL_POSITION && ch_seek(pos) == 0)
|
|
|
|
{
|
|
|
|
if (show_attn)
|
|
|
|
set_attnpos(pos);
|
|
|
|
jump_loc(pos, jump_sline);
|
2004-04-17 07:16:34 +00:00
|
|
|
} else if (linenum <= 1 && ch_beg_seek() == 0)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
jump_loc(ch_tell(), jump_sline);
|
|
|
|
error("Cannot seek to beginning of file", NULL_PARG);
|
|
|
|
} else
|
|
|
|
{
|
2004-04-17 07:16:34 +00:00
|
|
|
parg.p_linenum = linenum;
|
|
|
|
error("Cannot seek to line number %n", &parg);
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Repaint the screen.
|
|
|
|
*/
|
|
|
|
public void
|
2017-04-25 03:42:16 +00:00
|
|
|
repaint()
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
struct scrpos scrpos;
|
|
|
|
/*
|
|
|
|
* Start at the line currently at the top of the screen
|
|
|
|
* and redisplay the screen.
|
|
|
|
*/
|
2017-11-20 06:53:49 +00:00
|
|
|
get_scrpos(&scrpos, TOP);
|
2000-05-22 09:53:22 +00:00
|
|
|
pos_clear();
|
2017-04-25 03:42:16 +00:00
|
|
|
if (scrpos.pos == NULL_POSITION)
|
|
|
|
/* Screen hasn't been drawn yet. */
|
2017-11-20 06:53:49 +00:00
|
|
|
jump_loc(ch_zero(), 1);
|
2017-04-25 03:42:16 +00:00
|
|
|
else
|
|
|
|
jump_loc(scrpos.pos, scrpos.ln);
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Jump to a specified percentage into the file.
|
|
|
|
*/
|
|
|
|
public void
|
2017-04-25 03:42:16 +00:00
|
|
|
jump_percent(percent, fraction)
|
|
|
|
int percent;
|
|
|
|
long fraction;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
POSITION pos, len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine the position in the file
|
|
|
|
* (the specified percentage of the file's length).
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
2007-06-04 01:42:54 +00:00
|
|
|
pos = percent_pos(len, percent, fraction);
|
2000-05-22 09:53:22 +00:00
|
|
|
if (pos >= len)
|
|
|
|
pos = len-1;
|
|
|
|
|
|
|
|
jump_line_loc(pos, jump_sline);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Jump to a specified position in the file.
|
|
|
|
* Like jump_loc, but the position need not be
|
|
|
|
* the first character in a line.
|
|
|
|
*/
|
|
|
|
public void
|
2017-04-25 03:42:16 +00:00
|
|
|
jump_line_loc(pos, sline)
|
|
|
|
POSITION pos;
|
|
|
|
int sline;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if (ch_seek(pos) == 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Back up to the beginning of the line.
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Jump to a specified position in the file.
|
|
|
|
* The position must be the first character in a line.
|
|
|
|
* Place the target line on a specified line on the screen.
|
|
|
|
*/
|
|
|
|
public void
|
2017-04-25 03:42:16 +00:00
|
|
|
jump_loc(pos, sline)
|
|
|
|
POSITION pos;
|
|
|
|
int sline;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2017-04-29 06:30:21 +00:00
|
|
|
int nline;
|
2017-11-20 06:53:49 +00:00
|
|
|
int sindex;
|
2000-05-22 09:53:22 +00:00
|
|
|
POSITION tpos;
|
|
|
|
POSITION bpos;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Normalize sline.
|
|
|
|
*/
|
2017-11-20 06:53:49 +00:00
|
|
|
sindex = sindex_from_sline(sline);
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
if ((nline = onscreen(pos)) >= 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The line is currently displayed.
|
|
|
|
* Just scroll there.
|
|
|
|
*/
|
2017-11-20 06:53:49 +00:00
|
|
|
nline -= sindex;
|
2000-05-22 09:53:22 +00:00
|
|
|
if (nline > 0)
|
|
|
|
forw(nline, position(BOTTOM_PLUS_ONE), 1, 0, 0);
|
2017-11-20 06:53:49 +00:00
|
|
|
else if (nline < 0)
|
2000-05-22 09:53:22 +00:00
|
|
|
back(-nline, position(TOP), 1, 0);
|
2009-05-08 23:45:27 +00:00
|
|
|
#if HILITE_SEARCH
|
2000-05-22 09:53:22 +00:00
|
|
|
if (show_attn)
|
|
|
|
repaint_hilite(1);
|
2009-05-08 23:45:27 +00:00
|
|
|
#endif
|
2000-05-22 09:53:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Line is not on screen.
|
|
|
|
* Seek to the desired location.
|
|
|
|
*/
|
|
|
|
if (ch_seek(pos))
|
|
|
|
{
|
|
|
|
error("Cannot seek to that file position", NULL_PARG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if the desired line is before or after
|
|
|
|
* the currently displayed screen.
|
|
|
|
*/
|
|
|
|
tpos = position(TOP);
|
|
|
|
bpos = position(BOTTOM_PLUS_ONE);
|
|
|
|
if (tpos == NULL_POSITION || pos >= tpos)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The desired line is after the current screen.
|
|
|
|
* Move back in the file far enough so that we can
|
|
|
|
* call forw() and put the desired line at the
|
|
|
|
* sline-th line on the screen.
|
|
|
|
*/
|
2017-11-20 06:53:49 +00:00
|
|
|
for (nline = 0; nline < sindex; nline++)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
if (bpos != NULL_POSITION && pos <= bpos)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Surprise! The desired line is
|
|
|
|
* close enough to the current screen
|
|
|
|
* that we can just scroll there after all.
|
|
|
|
*/
|
2017-11-20 06:53:49 +00:00
|
|
|
forw(sc_height-sindex+nline-1, bpos, 1, 0, 0);
|
2009-05-08 23:45:27 +00:00
|
|
|
#if HILITE_SEARCH
|
2000-05-22 09:53:22 +00:00
|
|
|
if (show_attn)
|
|
|
|
repaint_hilite(1);
|
2009-05-08 23:45:27 +00:00
|
|
|
#endif
|
2000-05-22 09:53:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
pos = back_line(pos);
|
|
|
|
if (pos == NULL_POSITION)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Oops. Ran into the beginning of the file.
|
|
|
|
* Exit the loop here and rely on forw()
|
|
|
|
* below to draw the required number of
|
|
|
|
* blank lines at the top of the screen.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastmark();
|
|
|
|
squished = 0;
|
|
|
|
screen_trashed = 0;
|
2017-11-20 06:53:49 +00:00
|
|
|
forw(sc_height-1, pos, 1, 0, sindex-nline);
|
2000-05-22 09:53:22 +00:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The desired line is before the current screen.
|
|
|
|
* Move forward in the file far enough so that we
|
|
|
|
* can call back() and put the desired line at the
|
2017-11-20 06:53:49 +00:00
|
|
|
* sindex-th line on the screen.
|
2000-05-22 09:53:22 +00:00
|
|
|
*/
|
2017-11-20 06:53:49 +00:00
|
|
|
for (nline = sindex; nline < sc_height - 1; nline++)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
pos = forw_line(pos);
|
|
|
|
if (pos == NULL_POSITION)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Ran into end of file.
|
|
|
|
* This shouldn't normally happen,
|
|
|
|
* but may if there is some kind of read error.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
2016-01-04 00:22:34 +00:00
|
|
|
#if HILITE_SEARCH
|
|
|
|
pos = next_unfiltered(pos);
|
|
|
|
#endif
|
2000-05-22 09:53:22 +00:00
|
|
|
if (pos >= tpos)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Surprise! The desired line is
|
|
|
|
* close enough to the current screen
|
|
|
|
* that we can just scroll there after all.
|
|
|
|
*/
|
|
|
|
back(nline+1, tpos, 1, 0);
|
2009-05-08 23:45:27 +00:00
|
|
|
#if HILITE_SEARCH
|
2000-05-22 09:53:22 +00:00
|
|
|
if (show_attn)
|
|
|
|
repaint_hilite(1);
|
2009-05-08 23:45:27 +00:00
|
|
|
#endif
|
2000-05-22 09:53:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastmark();
|
2007-06-04 01:42:54 +00:00
|
|
|
if (!top_scroll)
|
2006-08-20 15:49:51 +00:00
|
|
|
clear();
|
|
|
|
else
|
|
|
|
home();
|
2000-05-22 09:53:22 +00:00
|
|
|
screen_trashed = 0;
|
|
|
|
add_back_pos(pos);
|
|
|
|
back(sc_height-1, pos, 1, 0);
|
|
|
|
}
|
|
|
|
}
|