1 /* $NetBSD: position.c,v 1.4 2013/09/04 19:44:21 tron Exp $ */
4 * Copyright (C) 1984-2012 Mark Nudelman
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.
9 * For more information, see the README file.
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.
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. }}
26 static POSITION
*table
= NULL
; /* The position table */
27 static int table_size
;
29 extern int sc_width
, sc_height
;
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
47 where
= sc_height
- 2;
50 where
= sc_height
- 1;
53 where
= (sc_height
- 1) / 2;
55 return (table
[where
]);
59 * Add a new file position to the bottom of the position table.
68 * Scroll the position table up.
70 for (i
= 1; i
< sc_height
; i
++)
71 table
[i
-1] = table
[i
];
72 table
[sc_height
- 1] = pos
;
76 * Add a new file position to the top of the position table.
85 * Scroll the position table down.
87 for (i
= sc_height
- 1; i
> 0; i
--)
88 table
[i
] = table
[i
-1];
93 * Initialize the position table, done whenever we clear the screen.
100 for (i
= 0; i
< sc_height
; i
++)
101 table
[i
] = NULL_POSITION
;
105 * Allocate or reallocate the position table.
110 struct scrpos scrpos
;
111 scrpos
.pos
= scrpos
.ln
= 0; /* XXX: GCC */
113 if (sc_height
<= table_size
)
116 * If we already have a table, remember the first line in it
117 * before we free it, so we can copy that line to the new table.
124 scrpos
.pos
= NULL_POSITION
;
125 table
= (POSITION
*) ecalloc(sc_height
, sizeof(POSITION
));
126 table_size
= sc_height
;
128 if (scrpos
.pos
!= NULL_POSITION
)
129 table
[scrpos
.ln
-1] = scrpos
.pos
;
133 * See if the byte at a specified position is currently on the screen.
134 * Check the position table to see if the position falls within its range.
135 * Return the position table entry if found, -1 if not.
145 for (i
= 1; i
< sc_height
; i
++)
152 * See if the entire screen is empty.
157 return (empty_lines(0, sc_height
-1));
167 for (i
= s
; i
<= e
; i
++)
168 if (table
[i
] != NULL_POSITION
&& table
[i
] != 0)
174 * Get the current screen position.
175 * The screen position consists of both a file position and
176 * a screen line number where the file position is placed on the screen.
177 * Normally the screen line number is 0, but if we are positioned
178 * such that the top few lines are empty, we may have to set
179 * the screen line to a number > 0.
183 struct scrpos
*scrpos
;
188 * Find the first line on the screen which has something on it,
189 * and return the screen line number and the file position.
191 for (i
= 0; i
< sc_height
; i
++)
192 if (table
[i
] != NULL_POSITION
)
195 scrpos
->pos
= table
[i
];
199 * The screen is empty.
201 scrpos
->pos
= NULL_POSITION
;
205 * Adjust a screen line number to be a simple positive integer
206 * in the range { 0 .. sc_height-2 }.
207 * (The bottom line, sc_height-1, is reserved for prompts, etc.)
208 * The given "sline" may be in the range { 1 .. sc_height-1 }
209 * to refer to lines relative to the top of the screen (starting from 1),
210 * or it may be in { -1 .. -(sc_height-1) } to refer to lines
211 * relative to the bottom of the screen.
218 * Negative screen line number means
219 * relative to the bottom of the screen.
224 * Can't be less than 1 or greater than sc_height-1.
228 if (sline
>= sc_height
)
229 sline
= sc_height
- 1;
231 * Return zero-based line number, not one-based.