Home | History | Annotate | Line # | Download | only in more
position.c revision 1.2
      1 /*	$NetBSD: position.c,v 1.2 1998/01/09 08:03:35 perry Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988 Mark Nudleman
      5  * Copyright (c) 1988, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #ifndef lint
     38 static char sccsid[] = "@(#)position.c	8.1 (Berkeley) 6/6/93";
     39 #endif /* not lint */
     40 
     41 /*
     42  * Routines dealing with the "position" table.
     43  * This is a table which tells the position (in the input file) of the
     44  * first char on each currently displayed line.
     45  *
     46  * {{ The position table is scrolled by moving all the entries.
     47  *    Would be better to have a circular table
     48  *    and just change a couple of pointers. }}
     49  */
     50 
     51 #include <sys/types.h>
     52 #include <less.h>
     53 
     54 static off_t *table;		/* The position table */
     55 static int tablesize;
     56 
     57 extern int sc_height;
     58 
     59 /*
     60  * Return the starting file position of a line displayed on the screen.
     61  * The line may be specified as a line number relative to the top
     62  * of the screen, but is usually one of these special cases:
     63  *	the top (first) line on the screen
     64  *	the second line on the screen
     65  *	the bottom line on the screen
     66  *	the line after the bottom line on the screen
     67  */
     68 off_t
     69 position(where)
     70 	int where;
     71 {
     72 	switch (where)
     73 	{
     74 	case BOTTOM:
     75 		where = sc_height - 2;
     76 		break;
     77 	case BOTTOM_PLUS_ONE:
     78 		where = sc_height - 1;
     79 		break;
     80 	case MIDDLE:
     81 		where = sc_height / 2;
     82 	}
     83 	return (table[where]);
     84 }
     85 
     86 /*
     87  * Add a new file position to the bottom of the position table.
     88  */
     89 add_forw_pos(pos)
     90 	off_t pos;
     91 {
     92 	register int i;
     93 
     94 	/*
     95 	 * Scroll the position table up.
     96 	 */
     97 	for (i = 1;  i < sc_height;  i++)
     98 		table[i-1] = table[i];
     99 	table[sc_height - 1] = pos;
    100 }
    101 
    102 /*
    103  * Add a new file position to the top of the position table.
    104  */
    105 add_back_pos(pos)
    106 	off_t pos;
    107 {
    108 	register int i;
    109 
    110 	/*
    111 	 * Scroll the position table down.
    112 	 */
    113 	for (i = sc_height - 1;  i > 0;  i--)
    114 		table[i] = table[i-1];
    115 	table[0] = pos;
    116 }
    117 
    118 copytable()
    119 {
    120 	register int a, b;
    121 
    122 	for (a = 0; a < sc_height && table[a] == NULL_POSITION; a++);
    123 	for (b = 0; a < sc_height; a++, b++) {
    124 		table[b] = table[a];
    125 		table[a] = NULL_POSITION;
    126 	}
    127 }
    128 
    129 /*
    130  * Initialize the position table, done whenever we clear the screen.
    131  */
    132 pos_clear()
    133 {
    134 	register int i;
    135 	extern char *malloc(), *realloc();
    136 
    137 	if (table == 0) {
    138 		tablesize = sc_height > 25 ? sc_height : 25;
    139 		table = (off_t *)malloc(tablesize * sizeof *table);
    140 	} else if (sc_height >= tablesize) {
    141 		tablesize = sc_height;
    142 		table = (off_t *)realloc(table, tablesize * sizeof *table);
    143 	}
    144 
    145 	for (i = 0;  i < sc_height;  i++)
    146 		table[i] = NULL_POSITION;
    147 }
    148 
    149 /*
    150  * See if the byte at a specified position is currently on the screen.
    151  * Check the position table to see if the position falls within its range.
    152  * Return the position table entry if found, -1 if not.
    153  */
    154 onscreen(pos)
    155 	off_t pos;
    156 {
    157 	register int i;
    158 
    159 	if (pos < table[0])
    160 		return (-1);
    161 	for (i = 1;  i < sc_height;  i++)
    162 		if (pos < table[i])
    163 			return (i-1);
    164 	return (-1);
    165 }
    166