/*1* Copyright (C) 1984-2025 Mark Nudelman2*3* You may distribute under the terms of either the GNU General Public4* License or the Less License, as specified in the README file.5*6* For more information, see the README file.7*/8910/*11* Routines which jump to a new location in the file.12*/1314#include "less.h"15#include "position.h"1617extern int jump_sline;18extern lbool squished;19extern int sc_width, sc_height;20extern int show_attn;21extern int top_scroll;22extern POSITION header_start_pos;2324/*25* Jump to the end of the file.26*/27public void jump_forw(void)28{29POSITION pos;30POSITION end_pos;3132if (ch_end_seek())33{34error("Cannot seek to end of file", NULL_PARG);35return;36}37end_pos = ch_tell();38if (position(sc_height-1) == end_pos)39{40eof_bell();41return;42}43/*44* Note; lastmark will be called later by jump_loc, but it fails45* because the position table has been cleared by pos_clear below.46* So call it here before calling pos_clear.47*/48lastmark();49/*50* Position the last line in the file at the last screen line.51* Go back one line from the end of the file52* to get to the beginning of the last line.53*/54pos_clear();55pos = back_line(end_pos, NULL);56if (pos == NULL_POSITION)57jump_loc(ch_zero(), sc_height-1);58else59{60jump_loc(pos, sc_height-1);61if (position(sc_height-1) != end_pos)62repaint();63}64}6566/*67* Jump to the last buffered line in the file.68*/69public void jump_forw_buffered(void)70{71POSITION end;7273if (ch_end_buffer_seek())74{75error("Cannot seek to end of buffers", NULL_PARG);76return;77}78end = ch_tell();79if (end != NULL_POSITION && end > 0)80jump_line_loc(end-1, sc_height-1);81}8283/*84* Jump to line n in the file.85*/86public void jump_back(LINENUM linenum)87{88POSITION pos;89PARG parg;9091/*92* Find the position of the specified line.93* If we can seek there, just jump to it.94* If we can't seek, but we're trying to go to line number 1,95* use ch_beg_seek() to get as close as we can.96*/97pos = find_pos(linenum);98if (pos != NULL_POSITION && ch_seek(pos) == 0)99{100if (show_attn)101set_attnpos(pos);102jump_loc(pos, jump_sline);103} else if (linenum <= 1 && ch_beg_seek() == 0)104{105jump_loc(ch_tell(), jump_sline);106error("Cannot seek to beginning of file", NULL_PARG);107} else108{109parg.p_linenum = linenum;110error("Cannot seek to line number %n", &parg);111}112}113114/*115* Repaint the screen.116*/117public void repaint(void)118{119struct scrpos scrpos;120/*121* Start at the line currently at the top of the screen122* and redisplay the screen.123*/124get_scrpos(&scrpos, TOP);125pos_clear();126if (scrpos.pos == NULL_POSITION)127/* Screen hasn't been drawn yet. */128jump_loc(ch_zero(), 1);129else130jump_loc(scrpos.pos, scrpos.ln);131}132133/*134* Jump to a specified percentage into the file.135*/136public void jump_percent(int percent, long fraction)137{138POSITION pos, len;139140/*141* Determine the position in the file142* (the specified percentage of the file's length).143*/144if ((len = ch_length()) == NULL_POSITION)145{146ierror("Determining length of file", NULL_PARG);147ch_end_seek();148}149if ((len = ch_length()) == NULL_POSITION)150{151error("Don't know length of file", NULL_PARG);152return;153}154pos = percent_pos(len, percent, fraction);155if (pos >= len)156pos = len-1;157158jump_line_loc(pos, jump_sline);159}160161/*162* Jump to a specified position in the file.163* Like jump_loc, but the position need not be164* the first character in a line.165*/166public void jump_line_loc(POSITION pos, int sline)167{168int c;169170if (ch_seek(pos) == 0)171{172/*173* Back up to the beginning of the line.174*/175while ((c = ch_back_get()) != '\n' && c != EOI)176;177if (c == '\n')178(void) ch_forw_get();179pos = ch_tell();180}181if (show_attn)182set_attnpos(pos);183jump_loc(pos, sline);184}185186static void after_header_message(void)187{188#if HAVE_TIME189#define MSG_FREQ 1 /* seconds */190static time_type last_msg = (time_type) 0;191time_type now = get_time();192if (now < last_msg + MSG_FREQ)193return;194last_msg = now;195#endif196bell();197/* {{ This message displays before the file text is updated, which is not a good UX. }} */198/** error("Cannot display text before header; use --header=- to disable header", NULL_PARG); */199}200201/*202* Ensure that a position is not before the header.203* If it is, print a message and return the position of the start of the header.204* {{ This is probably not being used correctly in all cases.205* It does not account for the location of pos on the screen,206* so lines before pos could be displayed. }}207*/208public POSITION after_header_pos(POSITION pos)209{210if (header_start_pos != NULL_POSITION && pos < header_start_pos)211{212after_header_message();213pos = header_start_pos;214}215return pos;216}217218/*219* Jump to a specified position in the file.220* The position must be the first character in a line.221* Place the target line on a specified line on the screen.222*/223public void jump_loc(POSITION pos, int sline)224{225int nline;226int sindex;227POSITION tpos;228POSITION bpos;229230/*231* Normalize sline.232*/233pos = after_header_pos(pos);234pos = next_unfiltered(pos);235sindex = sindex_from_sline(sline);236237if ((nline = onscreen(pos)) >= 0)238{239/*240* The line is currently displayed.241* Just scroll there.242*/243nline -= sindex;244if (nline > 0)245forw(nline, position(BOTTOM_PLUS_ONE), TRUE, FALSE, FALSE, 0);246else247back(-nline, position(TOP), TRUE, FALSE, FALSE);248#if HILITE_SEARCH249if (show_attn)250repaint_hilite(TRUE);251#endif252return;253}254255/*256* Line is not on screen.257* Seek to the desired location.258*/259if (ch_seek(pos))260{261error("Cannot seek to that file position", NULL_PARG);262return;263}264265/*266* See if the desired line is before or after267* the currently displayed screen.268*/269tpos = position(TOP);270bpos = position(BOTTOM_PLUS_ONE);271if (tpos == NULL_POSITION || pos >= tpos)272{273/*274* The desired line is after the current screen.275* Move back in the file far enough so that we can276* call forw() and put the desired line at the277* sline-th line on the screen.278*/279for (nline = 0; nline < sindex; nline++)280{281if (bpos != NULL_POSITION && pos <= bpos)282{283/*284* Surprise! The desired line is285* close enough to the current screen286* that we can just scroll there after all.287*/288forw(sc_height-sindex+nline-1, bpos, TRUE, FALSE, FALSE, 0);289#if HILITE_SEARCH290if (show_attn)291repaint_hilite(TRUE);292#endif293return;294}295pos = back_line(pos, NULL);296if (pos == NULL_POSITION)297{298/*299* Oops. Ran into the beginning of the file.300* Exit the loop here and rely on forw()301* below to draw the required number of302* blank lines at the top of the screen.303*/304break;305}306}307lastmark();308squished = FALSE;309screen_trashed_num(0);310forw(sc_height-1, pos, TRUE, FALSE, FALSE, sindex-nline);311} else312{313/*314* The desired line is before the current screen.315* Move forward in the file far enough so that we316* can call back() and put the desired line at the317* sindex-th line on the screen.318*/319for (nline = sindex; nline < sc_height - 1; nline++)320{321POSITION linepos;322pos = forw_line(pos, &linepos, NULL);323if (pos == NULL_POSITION)324{325/*326* Ran into end of file.327* This shouldn't normally happen,328* but may if there is some kind of read error.329*/330break;331}332if (linepos >= tpos)333{334/*335* Surprise! The desired line is336* close enough to the current screen337* that we can just scroll there after all.338*/339back(nline, tpos, TRUE, FALSE, FALSE);340#if HILITE_SEARCH341if (show_attn)342repaint_hilite(TRUE);343#endif344return;345}346}347lastmark();348if (!top_scroll)349clear();350else351home();352screen_trashed_num(0);353add_back_pos(pos);354back(sc_height-1, pos, TRUE, FALSE, FALSE);355}356}357358359