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