2012-07-23 21:31:53 +00:00
|
|
|
/*
|
2017-04-25 03:42:16 +00:00
|
|
|
* Copyright (C) 1984-2016 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
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* High level routines dealing with getting lines of input
|
|
|
|
* from the file being viewed.
|
|
|
|
*
|
|
|
|
* When we speak of "lines" here, we mean PRINTABLE lines;
|
|
|
|
* lines processed with respect to the screen width.
|
|
|
|
* We use the term "raw line" to refer to lines simply
|
|
|
|
* delimited by newlines; not processed with respect to screen width.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "less.h"
|
|
|
|
|
|
|
|
extern int squeeze;
|
|
|
|
extern int chopline;
|
2000-07-14 09:51:40 +00:00
|
|
|
extern int hshift;
|
2000-05-22 09:53:22 +00:00
|
|
|
extern int quit_if_one_screen;
|
|
|
|
extern int sigs;
|
|
|
|
extern int ignore_eoi;
|
2006-08-20 15:49:51 +00:00
|
|
|
extern int status_col;
|
2000-05-22 09:53:22 +00:00
|
|
|
extern POSITION start_attnpos;
|
|
|
|
extern POSITION end_attnpos;
|
|
|
|
#if HILITE_SEARCH
|
|
|
|
extern int hilite_search;
|
|
|
|
extern int size_linebuf;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the next line.
|
|
|
|
* A "current" position is passed and a "new" position is returned.
|
|
|
|
* The current position is the position of the first character of
|
|
|
|
* 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
|
2017-04-25 03:42:16 +00:00
|
|
|
forw_line(curr_pos)
|
|
|
|
POSITION curr_pos;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2006-08-20 15:49:51 +00:00
|
|
|
POSITION base_pos;
|
2000-05-22 09:53:22 +00:00
|
|
|
POSITION new_pos;
|
2017-04-25 03:42:16 +00:00
|
|
|
register int c;
|
2000-05-22 09:53:22 +00:00
|
|
|
int blankline;
|
|
|
|
int endline;
|
2006-08-20 15:49:51 +00:00
|
|
|
int backchars;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
2009-05-08 23:45:27 +00:00
|
|
|
get_forw_line:
|
2000-05-22 09:53:22 +00:00
|
|
|
if (curr_pos == NULL_POSITION)
|
|
|
|
{
|
|
|
|
null_line();
|
|
|
|
return (NULL_POSITION);
|
|
|
|
}
|
|
|
|
#if HILITE_SEARCH
|
2009-05-08 23:45:27 +00:00
|
|
|
if (hilite_search == OPT_ONPLUS || is_filtering() || status_col)
|
2016-01-04 00:22:34 +00:00
|
|
|
{
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
|
|
|
* If we are ignoring EOI (command F), only prepare
|
|
|
|
* one line ahead, to avoid getting stuck waiting for
|
|
|
|
* slow data without displaying the data we already have.
|
|
|
|
* If we're not ignoring EOI, we *could* do the same, but
|
|
|
|
* for efficiency we prepare several lines ahead at once.
|
|
|
|
*/
|
|
|
|
prep_hilite(curr_pos, curr_pos + 3*size_linebuf,
|
|
|
|
ignore_eoi ? 1 : -1);
|
2016-01-04 00:22:34 +00:00
|
|
|
curr_pos = next_unfiltered(curr_pos);
|
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
#endif
|
|
|
|
if (ch_seek(curr_pos))
|
|
|
|
{
|
|
|
|
null_line();
|
|
|
|
return (NULL_POSITION);
|
|
|
|
}
|
|
|
|
|
2009-05-08 23:45:27 +00:00
|
|
|
/*
|
|
|
|
* Step back to the beginning of the line.
|
|
|
|
*/
|
2006-08-20 15:49:51 +00:00
|
|
|
base_pos = curr_pos;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (ABORT_SIGS())
|
|
|
|
{
|
|
|
|
null_line();
|
|
|
|
return (NULL_POSITION);
|
|
|
|
}
|
|
|
|
c = ch_back_get();
|
|
|
|
if (c == EOI)
|
|
|
|
break;
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
(void) ch_forw_get();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
--base_pos;
|
|
|
|
}
|
|
|
|
|
2009-05-08 23:45:27 +00:00
|
|
|
/*
|
|
|
|
* Read forward again to the position we should start at.
|
|
|
|
*/
|
2006-08-20 15:49:51 +00:00
|
|
|
prewind();
|
|
|
|
plinenum(base_pos);
|
|
|
|
(void) ch_seek(base_pos);
|
2009-05-08 23:45:27 +00:00
|
|
|
new_pos = base_pos;
|
|
|
|
while (new_pos < curr_pos)
|
2006-08-20 15:49:51 +00:00
|
|
|
{
|
|
|
|
if (ABORT_SIGS())
|
|
|
|
{
|
|
|
|
null_line();
|
|
|
|
return (NULL_POSITION);
|
|
|
|
}
|
|
|
|
c = ch_forw_get();
|
2009-05-08 23:45:27 +00:00
|
|
|
backchars = pappend(c, new_pos);
|
|
|
|
new_pos++;
|
2006-08-20 15:49:51 +00:00
|
|
|
if (backchars > 0)
|
|
|
|
{
|
|
|
|
pshift_all();
|
2009-05-08 23:45:27 +00:00
|
|
|
new_pos -= backchars;
|
2006-08-20 15:49:51 +00:00
|
|
|
while (--backchars >= 0)
|
|
|
|
(void) ch_back_get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(void) pflushmbc();
|
|
|
|
pshift_all();
|
2000-05-22 09:53:22 +00:00
|
|
|
|
2009-05-08 23:45:27 +00:00
|
|
|
/*
|
|
|
|
* Read the first character to display.
|
|
|
|
*/
|
2000-05-22 09:53:22 +00:00
|
|
|
c = ch_forw_get();
|
|
|
|
if (c == EOI)
|
|
|
|
{
|
|
|
|
null_line();
|
|
|
|
return (NULL_POSITION);
|
|
|
|
}
|
|
|
|
blankline = (c == '\n' || c == '\r');
|
|
|
|
|
2009-05-08 23:45:27 +00:00
|
|
|
/*
|
|
|
|
* Read each character in the line and append to the line buffer.
|
|
|
|
*/
|
2000-05-22 09:53:22 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (ABORT_SIGS())
|
|
|
|
{
|
|
|
|
null_line();
|
|
|
|
return (NULL_POSITION);
|
|
|
|
}
|
|
|
|
if (c == '\n' || c == EOI)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* End of the line.
|
|
|
|
*/
|
2006-08-20 15:49:51 +00:00
|
|
|
backchars = pflushmbc();
|
2000-05-22 09:53:22 +00:00
|
|
|
new_pos = ch_tell();
|
2006-08-20 15:49:51 +00:00
|
|
|
if (backchars > 0 && !chopline && hshift == 0)
|
|
|
|
{
|
|
|
|
new_pos -= backchars + 1;
|
|
|
|
endline = FALSE;
|
|
|
|
} else
|
|
|
|
endline = TRUE;
|
2000-05-22 09:53:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-08-20 15:49:51 +00:00
|
|
|
if (c != '\r')
|
|
|
|
blankline = 0;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Append the char to the line and get the next char.
|
|
|
|
*/
|
2006-08-20 15:49:51 +00:00
|
|
|
backchars = pappend(c, ch_tell()-1);
|
|
|
|
if (backchars > 0)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The char won't fit in the line; the line
|
|
|
|
* is too long to print in the screen width.
|
|
|
|
* End the line here.
|
|
|
|
*/
|
2000-07-14 09:51:40 +00:00
|
|
|
if (chopline || hshift > 0)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2011-05-09 18:30:23 +00:00
|
|
|
if (ABORT_SIGS())
|
|
|
|
{
|
|
|
|
null_line();
|
|
|
|
return (NULL_POSITION);
|
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
c = ch_forw_get();
|
|
|
|
} while (c != '\n' && c != EOI);
|
|
|
|
new_pos = ch_tell();
|
|
|
|
endline = TRUE;
|
|
|
|
quit_if_one_screen = FALSE;
|
|
|
|
} else
|
|
|
|
{
|
2006-08-20 15:49:51 +00:00
|
|
|
new_pos = ch_tell() - backchars;
|
2000-05-22 09:53:22 +00:00
|
|
|
endline = FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c = ch_forw_get();
|
|
|
|
}
|
2009-05-08 23:45:27 +00:00
|
|
|
|
2009-07-27 07:05:08 +00:00
|
|
|
pdone(endline, 1);
|
2009-05-08 23:45:27 +00:00
|
|
|
|
|
|
|
#if HILITE_SEARCH
|
|
|
|
if (is_filtered(base_pos))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We don't want to display this line.
|
|
|
|
* Get the next line.
|
|
|
|
*/
|
|
|
|
curr_pos = new_pos;
|
|
|
|
goto get_forw_line;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status_col && is_hilited(base_pos, ch_tell()-1, 1, NULL))
|
|
|
|
set_status_col('*');
|
|
|
|
#endif
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
if (squeeze && blankline)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This line is blank.
|
|
|
|
* Skip down to the last contiguous blank line
|
|
|
|
* and pretend it is the one which we are returning.
|
|
|
|
*/
|
|
|
|
while ((c = ch_forw_get()) == '\n' || c == '\r')
|
|
|
|
if (ABORT_SIGS())
|
|
|
|
{
|
|
|
|
null_line();
|
|
|
|
return (NULL_POSITION);
|
|
|
|
}
|
|
|
|
if (c != EOI)
|
|
|
|
(void) ch_back_get();
|
|
|
|
new_pos = ch_tell();
|
|
|
|
}
|
|
|
|
|
|
|
|
return (new_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the previous line.
|
|
|
|
* A "current" position is passed and a "new" position is returned.
|
|
|
|
* The current position is the position of the first character of
|
|
|
|
* a line. The new position is the position of the first character
|
|
|
|
* of the PREVIOUS line. The line obtained is the one starting at new_pos.
|
|
|
|
*/
|
|
|
|
public POSITION
|
2017-04-25 03:42:16 +00:00
|
|
|
back_line(curr_pos)
|
|
|
|
POSITION curr_pos;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2009-05-08 23:45:27 +00:00
|
|
|
POSITION new_pos, begin_new_pos, base_pos;
|
2000-05-22 09:53:22 +00:00
|
|
|
int c;
|
|
|
|
int endline;
|
2006-08-20 15:49:51 +00:00
|
|
|
int backchars;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
2009-05-08 23:45:27 +00:00
|
|
|
get_back_line:
|
2000-05-22 09:53:22 +00:00
|
|
|
if (curr_pos == NULL_POSITION || curr_pos <= ch_zero())
|
|
|
|
{
|
|
|
|
null_line();
|
|
|
|
return (NULL_POSITION);
|
|
|
|
}
|
|
|
|
#if HILITE_SEARCH
|
2009-05-08 23:45:27 +00:00
|
|
|
if (hilite_search == OPT_ONPLUS || is_filtering() || status_col)
|
2000-05-22 09:53:22 +00:00
|
|
|
prep_hilite((curr_pos < 3*size_linebuf) ?
|
|
|
|
0 : curr_pos - 3*size_linebuf, curr_pos, -1);
|
|
|
|
#endif
|
|
|
|
if (ch_seek(curr_pos-1))
|
|
|
|
{
|
|
|
|
null_line();
|
|
|
|
return (NULL_POSITION);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (squeeze)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Find out if the "current" line was blank.
|
|
|
|
*/
|
2009-05-08 23:45:27 +00:00
|
|
|
(void) ch_forw_get(); /* Skip the newline */
|
|
|
|
c = ch_forw_get(); /* First char of "current" line */
|
|
|
|
(void) ch_back_get(); /* Restore our position */
|
2000-05-22 09:53:22 +00:00
|
|
|
(void) ch_back_get();
|
|
|
|
|
|
|
|
if (c == '\n' || c == '\r')
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The "current" line was blank.
|
|
|
|
* Skip over any preceding blank lines,
|
|
|
|
* since we skipped them in forw_line().
|
|
|
|
*/
|
|
|
|
while ((c = ch_back_get()) == '\n' || c == '\r')
|
|
|
|
if (ABORT_SIGS())
|
|
|
|
{
|
|
|
|
null_line();
|
|
|
|
return (NULL_POSITION);
|
|
|
|
}
|
|
|
|
if (c == EOI)
|
|
|
|
{
|
|
|
|
null_line();
|
|
|
|
return (NULL_POSITION);
|
|
|
|
}
|
|
|
|
(void) ch_forw_get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan backwards until we hit the beginning of the line.
|
|
|
|
*/
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (ABORT_SIGS())
|
|
|
|
{
|
|
|
|
null_line();
|
|
|
|
return (NULL_POSITION);
|
|
|
|
}
|
|
|
|
c = ch_back_get();
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This is the newline ending the previous line.
|
|
|
|
* We have hit the beginning of the line.
|
|
|
|
*/
|
2009-05-08 23:45:27 +00:00
|
|
|
base_pos = ch_tell() + 1;
|
2000-05-22 09:53:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c == EOI)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We have hit the beginning of the file.
|
|
|
|
* This must be the first line in the file.
|
|
|
|
* This must, of course, be the beginning of the line.
|
|
|
|
*/
|
2009-05-08 23:45:27 +00:00
|
|
|
base_pos = ch_tell();
|
2000-05-22 09:53:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now scan forwards from the beginning of this line.
|
|
|
|
* We keep discarding "printable lines" (based on screen width)
|
|
|
|
* until we reach the curr_pos.
|
|
|
|
*
|
|
|
|
* {{ This algorithm is pretty inefficient if the lines
|
|
|
|
* are much longer than the screen width,
|
|
|
|
* but I don't know of any better way. }}
|
|
|
|
*/
|
2009-05-08 23:45:27 +00:00
|
|
|
new_pos = base_pos;
|
2000-05-22 09:53:22 +00:00
|
|
|
if (ch_seek(new_pos))
|
|
|
|
{
|
|
|
|
null_line();
|
|
|
|
return (NULL_POSITION);
|
|
|
|
}
|
|
|
|
endline = FALSE;
|
|
|
|
prewind();
|
|
|
|
plinenum(new_pos);
|
2006-08-20 15:49:51 +00:00
|
|
|
loop:
|
|
|
|
begin_new_pos = new_pos;
|
2000-05-22 09:53:22 +00:00
|
|
|
(void) ch_seek(new_pos);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
c = ch_forw_get();
|
|
|
|
if (c == EOI || ABORT_SIGS())
|
|
|
|
{
|
|
|
|
null_line();
|
|
|
|
return (NULL_POSITION);
|
|
|
|
}
|
|
|
|
new_pos++;
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
2006-08-20 15:49:51 +00:00
|
|
|
backchars = pflushmbc();
|
|
|
|
if (backchars > 0 && !chopline && hshift == 0)
|
|
|
|
{
|
|
|
|
backchars++;
|
|
|
|
goto shift;
|
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
endline = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2006-08-20 15:49:51 +00:00
|
|
|
backchars = pappend(c, ch_tell()-1);
|
|
|
|
if (backchars > 0)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Got a full printable line, but we haven't
|
|
|
|
* reached our curr_pos yet. Discard the line
|
|
|
|
* and start a new one.
|
|
|
|
*/
|
2000-07-14 09:51:40 +00:00
|
|
|
if (chopline || hshift > 0)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
endline = TRUE;
|
|
|
|
quit_if_one_screen = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2006-08-20 15:49:51 +00:00
|
|
|
shift:
|
|
|
|
pshift_all();
|
|
|
|
while (backchars-- > 0)
|
|
|
|
{
|
|
|
|
(void) ch_back_get();
|
|
|
|
new_pos--;
|
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
} while (new_pos < curr_pos);
|
|
|
|
|
2009-07-27 07:05:08 +00:00
|
|
|
pdone(endline, 0);
|
2009-05-08 23:45:27 +00:00
|
|
|
|
|
|
|
#if HILITE_SEARCH
|
|
|
|
if (is_filtered(base_pos))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We don't want to display this line.
|
|
|
|
* Get the previous line.
|
|
|
|
*/
|
|
|
|
curr_pos = begin_new_pos;
|
|
|
|
goto get_back_line;
|
|
|
|
}
|
|
|
|
|
2012-06-26 20:38:53 +00:00
|
|
|
if (status_col && curr_pos > 0 && is_hilited(base_pos, curr_pos-1, 1, NULL))
|
2009-05-08 23:45:27 +00:00
|
|
|
set_status_col('*');
|
|
|
|
#endif
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
return (begin_new_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set attnpos.
|
|
|
|
*/
|
|
|
|
public void
|
2017-04-25 03:42:16 +00:00
|
|
|
set_attnpos(pos)
|
|
|
|
POSITION pos;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if (pos != NULL_POSITION)
|
|
|
|
{
|
|
|
|
if (ch_seek(pos))
|
|
|
|
return;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
c = ch_forw_get();
|
|
|
|
if (c == EOI)
|
|
|
|
break;
|
2016-01-04 00:22:34 +00:00
|
|
|
if (c == '\n' || c == '\r')
|
|
|
|
{
|
|
|
|
(void) ch_back_get();
|
|
|
|
break;
|
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
pos++;
|
|
|
|
}
|
2016-01-04 00:22:34 +00:00
|
|
|
end_attnpos = pos;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
c = ch_back_get();
|
|
|
|
if (c == EOI || c == '\n' || c == '\r')
|
|
|
|
break;
|
|
|
|
pos--;
|
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
start_attnpos = pos;
|
|
|
|
}
|