Home | History | Annotate | Line # | Download | only in dist
position.c revision 1.1.1.2
      1 /*	$NetBSD: position.c,v 1.1.1.2 2013/09/04 19:35:04 tron Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1984-2012  Mark Nudelman
      5  *
      6  * You may distribute under the terms of either the GNU General Public
      7  * License or the Less License, as specified in the README file.
      8  *
      9  * For more information, see the README file.
     10  */
     11 
     12 
     13 /*
     14  * Routines dealing with the "position" table.
     15  * This is a table which tells the position (in the input file) of the
     16  * first char on each currently displayed line.
     17  *
     18  * {{ The position table is scrolled by moving all the entries.
     19  *    Would be better to have a circular table
     20  *    and just change a couple of pointers. }}
     21  */
     22 
     23 #include "less.h"
     24 #include "position.h"
     25 
     26 static POSITION *table = NULL;	/* The position table */
     27 static int table_size;
     28 
     29 extern int sc_width, sc_height;
     30 
     31 /*
     32  * Return the starting file position of a line displayed on the screen.
     33  * The line may be specified as a line number relative to the top
     34  * of the screen, but is usually one of these special cases:
     35  *	the top (first) line on the screen
     36  *	the second line on the screen
     37  *	the bottom line on the screen
     38  *	the line after the bottom line on the screen
     39  */
     40 	public POSITION
     41 position(where)
     42 	int where;
     43 {
     44 	switch (where)
     45 	{
     46 	case BOTTOM:
     47 		where = sc_height - 2;
     48 		break;
     49 	case BOTTOM_PLUS_ONE:
     50 		where = sc_height - 1;
     51 		break;
     52 	case MIDDLE:
     53 		where = (sc_height - 1) / 2;
     54 	}
     55 	return (table[where]);
     56 }
     57 
     58 /*
     59  * Add a new file position to the bottom of the position table.
     60  */
     61 	public void
     62 add_forw_pos(pos)
     63 	POSITION pos;
     64 {
     65 	register int i;
     66 
     67 	/*
     68 	 * Scroll the position table up.
     69 	 */
     70 	for (i = 1;  i < sc_height;  i++)
     71 		table[i-1] = table[i];
     72 	table[sc_height - 1] = pos;
     73 }
     74 
     75 /*
     76  * Add a new file position to the top of the position table.
     77  */
     78 	public void
     79 add_back_pos(pos)
     80 	POSITION pos;
     81 {
     82 	register int i;
     83 
     84 	/*
     85 	 * Scroll the position table down.
     86 	 */
     87 	for (i = sc_height - 1;  i > 0;  i--)
     88 		table[i] = table[i-1];
     89 	table[0] = pos;
     90 }
     91 
     92 /*
     93  * Initialize the position table, done whenever we clear the screen.
     94  */
     95 	public void
     96 pos_clear()
     97 {
     98 	register int i;
     99 
    100 	for (i = 0;  i < sc_height;  i++)
    101 		table[i] = NULL_POSITION;
    102 }
    103 
    104 /*
    105  * Allocate or reallocate the position table.
    106  */
    107 	public void
    108 pos_init()
    109 {
    110 	struct scrpos scrpos;
    111 
    112 	if (sc_height <= table_size)
    113 		return;
    114 	/*
    115 	 * If we already have a table, remember the first line in it
    116 	 * before we free it, so we can copy that line to the new table.
    117 	 */
    118 	if (table != NULL)
    119 	{
    120 		get_scrpos(&scrpos);
    121 		free((char*)table);
    122 	} else
    123 		scrpos.pos = NULL_POSITION;
    124 	table = (POSITION *) ecalloc(sc_height, sizeof(POSITION));
    125 	table_size = sc_height;
    126 	pos_clear();
    127 	if (scrpos.pos != NULL_POSITION)
    128 		table[scrpos.ln-1] = scrpos.pos;
    129 }
    130 
    131 /*
    132  * See if the byte at a specified position is currently on the screen.
    133  * Check the position table to see if the position falls within its range.
    134  * Return the position table entry if found, -1 if not.
    135  */
    136 	public int
    137 onscreen(pos)
    138 	POSITION pos;
    139 {
    140 	register int i;
    141 
    142 	if (pos < table[0])
    143 		return (-1);
    144 	for (i = 1;  i < sc_height;  i++)
    145 		if (pos < table[i])
    146 			return (i-1);
    147 	return (-1);
    148 }
    149 
    150 /*
    151  * See if the entire screen is empty.
    152  */
    153 	public int
    154 empty_screen()
    155 {
    156 	return (empty_lines(0, sc_height-1));
    157 }
    158 
    159 	public int
    160 empty_lines(s, e)
    161 	int s;
    162 	int e;
    163 {
    164 	register int i;
    165 
    166 	for (i = s;  i <= e;  i++)
    167 		if (table[i] != NULL_POSITION && table[i] != 0)
    168 			return (0);
    169 	return (1);
    170 }
    171 
    172 /*
    173  * Get the current screen position.
    174  * The screen position consists of both a file position and
    175  * a screen line number where the file position is placed on the screen.
    176  * Normally the screen line number is 0, but if we are positioned
    177  * such that the top few lines are empty, we may have to set
    178  * the screen line to a number > 0.
    179  */
    180 	public void
    181 get_scrpos(scrpos)
    182 	struct scrpos *scrpos;
    183 {
    184 	register int i;
    185 
    186 	/*
    187 	 * Find the first line on the screen which has something on it,
    188 	 * and return the screen line number and the file position.
    189 	 */
    190 	for (i = 0; i < sc_height;  i++)
    191 		if (table[i] != NULL_POSITION)
    192 		{
    193 			scrpos->ln = i+1;
    194 			scrpos->pos = table[i];
    195 			return;
    196 		}
    197 	/*
    198 	 * The screen is empty.
    199 	 */
    200 	scrpos->pos = NULL_POSITION;
    201 }
    202 
    203 /*
    204  * Adjust a screen line number to be a simple positive integer
    205  * in the range { 0 .. sc_height-2 }.
    206  * (The bottom line, sc_height-1, is reserved for prompts, etc.)
    207  * The given "sline" may be in the range { 1 .. sc_height-1 }
    208  * to refer to lines relative to the top of the screen (starting from 1),
    209  * or it may be in { -1 .. -(sc_height-1) } to refer to lines
    210  * relative to the bottom of the screen.
    211  */
    212 	public int
    213 adjsline(sline)
    214 	int sline;
    215 {
    216 	/*
    217 	 * Negative screen line number means
    218 	 * relative to the bottom of the screen.
    219 	 */
    220 	if (sline < 0)
    221 		sline += sc_height;
    222 	/*
    223 	 * Can't be less than 1 or greater than sc_height-1.
    224 	 */
    225 	if (sline <= 0)
    226 		sline = 1;
    227 	if (sline >= sc_height)
    228 		sline = sc_height - 1;
    229 	/*
    230 	 * Return zero-based line number, not one-based.
    231 	 */
    232 	return (sline-1);
    233 }
    234