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