less -> more
[unix-history] / usr / src / usr.bin / more / position.c
CommitLineData
bfe13c81
KB
1/*
2 * Copyright (c) 1988 Mark Nudleman
3 * Copyright (c) 1988 Regents of the University of California.
4 * All rights reserved.
5 *
bfe13c81
KB
6 * Redistribution and use in source and binary forms are permitted
7 * provided that the above copyright notice and this paragraph are
8 * duplicated in all such forms and that any documentation,
9 * advertising materials, and other materials related to such
10 * distribution and use acknowledge that the software was developed
a942b40b
KB
11 * by Mark Nudleman and the University of California, Berkeley. The
12 * name of Mark Nudleman or the
bfe13c81
KB
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 */
19
20#ifndef lint
a942b40b 21static char sccsid[] = "@(#)position.c 5.2 (Berkeley) %G%";
bfe13c81
KB
22#endif /* not lint */
23
24/*
25 * Routines dealing with the "position" table.
26 * This is a table which tells the position (in the input file) of the
27 * first char on each currently displayed line.
28 *
29 * {{ The position table is scrolled by moving all the entries.
30 * Would be better to have a circular table
31 * and just change a couple of pointers. }}
32 */
33
34#include "less.h"
35#include "position.h"
36
37#define NPOS 100 /* {{ sc_height must be less than NPOS }} */
38static POSITION table[NPOS]; /* The position table */
39
40extern int sc_width, sc_height;
41
42/*
43 * Return the starting file position of a line displayed on the screen.
44 * The line may be specified as a line number relative to the top
45 * of the screen, but is usually one of these special cases:
46 * the top (first) line on the screen
47 * the second line on the screen
48 * the bottom line on the screen
49 * the line after the bottom line on the screen
50 */
51 public POSITION
52position(where)
53 int where;
54{
55 switch (where)
56 {
57 case BOTTOM:
58 where = sc_height - 2;
59 break;
60 case BOTTOM_PLUS_ONE:
61 where = sc_height - 1;
62 break;
63 case MIDDLE:
64 where = sc_height / 2;
65 }
66 return (table[where]);
67}
68
69/*
70 * Add a new file position to the bottom of the position table.
71 */
72 public void
73add_forw_pos(pos)
74 POSITION pos;
75{
76 register int i;
77
78 /*
79 * Scroll the position table up.
80 */
81 for (i = 1; i < sc_height; i++)
82 table[i-1] = table[i];
83 table[sc_height - 1] = pos;
84}
85
86/*
87 * Add a new file position to the top of the position table.
88 */
89 public void
90add_back_pos(pos)
91 POSITION pos;
92{
93 register int i;
94
95 /*
96 * Scroll the position table down.
97 */
98 for (i = sc_height - 1; i > 0; i--)
99 table[i] = table[i-1];
100 table[0] = pos;
101}
102
103/*
104 * Initialize the position table, done whenever we clear the screen.
105 */
106 public void
107pos_clear()
108{
109 register int i;
110
111 for (i = 0; i < sc_height; i++)
112 table[i] = NULL_POSITION;
113}
114
115/*
116 * See if the byte at a specified position is currently on the screen.
117 * Check the position table to see if the position falls within its range.
118 * Return the position table entry if found, -1 if not.
119 */
120 public int
121onscreen(pos)
122 POSITION pos;
123{
124 register int i;
125
126 if (pos < table[0])
127 return (-1);
128 for (i = 1; i < sc_height; i++)
129 if (pos < table[i])
130 return (i-1);
131 return (-1);
132}