Commit | Line | Data |
---|---|---|
bfe13c81 KB |
1 | /* |
2 | * Copyright (c) 1988 Mark Nudleman | |
1e3ab5ee KB |
3 | * Copyright (c) 1988, 1993 |
4 | * The Regents of the University of California. All rights reserved. | |
bfe13c81 | 5 | * |
f15db449 | 6 | * %sccs.include.redist.c% |
bfe13c81 KB |
7 | */ |
8 | ||
9 | #ifndef lint | |
1e3ab5ee | 10 | static char sccsid[] = "@(#)position.c 8.1 (Berkeley) %G%"; |
bfe13c81 KB |
11 | #endif /* not lint */ |
12 | ||
13 | /* | |
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. | |
17 | * | |
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. }} | |
21 | */ | |
22 | ||
966c6ec0 KB |
23 | #include <sys/types.h> |
24 | #include <less.h> | |
bfe13c81 | 25 | |
1a23310a DS |
26 | static off_t *table; /* The position table */ |
27 | static int tablesize; | |
bfe13c81 | 28 | |
966c6ec0 | 29 | extern int sc_height; |
bfe13c81 KB |
30 | |
31 | /* | |
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 | |
39 | */ | |
966c6ec0 | 40 | off_t |
bfe13c81 KB |
41 | position(where) |
42 | int where; | |
43 | { | |
44 | switch (where) | |
45 | { | |
46 | case BOTTOM: | |
47 | where = sc_height - 2; | |
48 | break; | |
49 | case BOTTOM_PLUS_ONE: | |
50 | where = sc_height - 1; | |
51 | break; | |
52 | case MIDDLE: | |
53 | where = sc_height / 2; | |
54 | } | |
55 | return (table[where]); | |
56 | } | |
57 | ||
58 | /* | |
59 | * Add a new file position to the bottom of the position table. | |
60 | */ | |
bfe13c81 | 61 | add_forw_pos(pos) |
966c6ec0 | 62 | off_t pos; |
bfe13c81 KB |
63 | { |
64 | register int i; | |
65 | ||
66 | /* | |
67 | * Scroll the position table up. | |
68 | */ | |
69 | for (i = 1; i < sc_height; i++) | |
70 | table[i-1] = table[i]; | |
71 | table[sc_height - 1] = pos; | |
72 | } | |
73 | ||
74 | /* | |
75 | * Add a new file position to the top of the position table. | |
76 | */ | |
bfe13c81 | 77 | add_back_pos(pos) |
966c6ec0 | 78 | off_t pos; |
bfe13c81 KB |
79 | { |
80 | register int i; | |
81 | ||
82 | /* | |
83 | * Scroll the position table down. | |
84 | */ | |
85 | for (i = sc_height - 1; i > 0; i--) | |
86 | table[i] = table[i-1]; | |
87 | table[0] = pos; | |
88 | } | |
89 | ||
4a7551ea KB |
90 | copytable() |
91 | { | |
92 | register int a, b; | |
93 | ||
94 | for (a = 0; a < sc_height && table[a] == NULL_POSITION; a++); | |
95 | for (b = 0; a < sc_height; a++, b++) { | |
96 | table[b] = table[a]; | |
97 | table[a] = NULL_POSITION; | |
98 | } | |
99 | } | |
100 | ||
bfe13c81 KB |
101 | /* |
102 | * Initialize the position table, done whenever we clear the screen. | |
103 | */ | |
bfe13c81 KB |
104 | pos_clear() |
105 | { | |
106 | register int i; | |
1a23310a DS |
107 | extern char *malloc(), *realloc(); |
108 | ||
109 | if (table == 0) { | |
110 | tablesize = sc_height > 25 ? sc_height : 25; | |
0b4c327a | 111 | table = (off_t *)malloc(tablesize * sizeof *table); |
1a23310a DS |
112 | } else if (sc_height >= tablesize) { |
113 | tablesize = sc_height; | |
0b4c327a | 114 | table = (off_t *)realloc(table, tablesize * sizeof *table); |
1a23310a | 115 | } |
bfe13c81 KB |
116 | |
117 | for (i = 0; i < sc_height; i++) | |
118 | table[i] = NULL_POSITION; | |
119 | } | |
120 | ||
121 | /* | |
122 | * See if the byte at a specified position is currently on the screen. | |
123 | * Check the position table to see if the position falls within its range. | |
124 | * Return the position table entry if found, -1 if not. | |
125 | */ | |
bfe13c81 | 126 | onscreen(pos) |
966c6ec0 | 127 | off_t pos; |
bfe13c81 KB |
128 | { |
129 | register int i; | |
130 | ||
131 | if (pos < table[0]) | |
132 | return (-1); | |
133 | for (i = 1; i < sc_height; i++) | |
134 | if (pos < table[i]) | |
135 | return (i-1); | |
136 | return (-1); | |
137 | } |