protoize most of the function prototypes
[nvi.git] / vi / vs_relative.c
blobc698d519d654100641234b7f91a432dca3376e9e
1 /*-
2 * Copyright (c) 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: vs_relative.c,v 10.17 2001/06/25 15:19:38 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:38 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <string.h>
25 #include "../common/common.h"
26 #include "vi.h"
29 * vs_column --
30 * Return the logical column of the cursor in the line.
32 * PUBLIC: int vs_column __P((SCR *, size_t *));
34 int
35 vs_column(SCR *sp, size_t *colp)
37 VI_PRIVATE *vip;
39 vip = VIP(sp);
41 *colp = (O_ISSET(sp, O_LEFTRIGHT) ?
42 vip->sc_smap->coff : (vip->sc_smap->soff - 1) * sp->cols) +
43 vip->sc_col - (O_ISSET(sp, O_NUMBER) ? O_NUMBER_LENGTH : 0);
44 return (0);
48 * vs_screens --
49 * Return the screens necessary to display the line, or if specified,
50 * the physical character column within the line, including space
51 * required for the O_NUMBER and O_LIST options.
53 * PUBLIC: size_t vs_screens __P((SCR *, db_recno_t, size_t *));
55 size_t
56 vs_screens(SCR *sp, db_recno_t lno, size_t *cnop)
58 size_t cols, screens;
60 /* Left-right screens are simple, it's always 1. */
61 if (O_ISSET(sp, O_LEFTRIGHT))
62 return (1);
65 * Check for a cached value. We maintain a cache because, if the
66 * line is large, this routine gets called repeatedly. One other
67 * hack, lots of time the cursor is on column one, which is an easy
68 * one.
70 if (cnop == NULL) {
71 if (VIP(sp)->ss_lno == lno)
72 return (VIP(sp)->ss_screens);
73 } else if (*cnop == 0)
74 return (1);
76 /* Figure out how many columns the line/column needs. */
77 cols = vs_columns(sp, NULL, lno, cnop, NULL);
79 screens = (cols / sp->cols + (cols % sp->cols ? 1 : 0));
80 if (screens == 0)
81 screens = 1;
83 /* Cache the value. */
84 if (cnop == NULL) {
85 VIP(sp)->ss_lno = lno;
86 VIP(sp)->ss_screens = screens;
88 return (screens);
92 * vs_columns --
93 * Return the screen columns necessary to display the line, or,
94 * if specified, the physical character column within the line.
96 * PUBLIC: size_t vs_columns __P((SCR *, CHAR_T *, db_recno_t, size_t *, size_t *));
98 size_t
99 vs_columns(SCR *sp, CHAR_T *lp, db_recno_t lno, size_t *cnop, size_t *diffp)
101 size_t chlen, cno, curoff, last, len, scno;
102 int ch, leftright, listset;
103 CHAR_T *p;
105 /* Need the line to go any further. */
106 if (lp == NULL) {
107 (void)db_get(sp, lno, 0, &lp, &len);
108 if (len == 0)
109 goto done;
112 /* Missing or empty lines are easy. */
113 if (lp == NULL) {
114 done: if (diffp != NULL) /* XXX */
115 *diffp = 0;
116 return (0);
119 /* Store away the values of the list and leftright edit options. */
120 listset = O_ISSET(sp, O_LIST);
121 leftright = O_ISSET(sp, O_LEFTRIGHT);
124 * Initialize the pointer into the buffer and screen and current
125 * offsets.
127 p = lp;
128 curoff = scno = 0;
130 /* Leading number if O_NUMBER option set. */
131 if (O_ISSET(sp, O_NUMBER))
132 scno += O_NUMBER_LENGTH;
134 /* Macro to return the display length of any signal character. */
135 #define CHLEN(val) (ch = *(UCHAR_T *)p++) == '\t' && \
136 !listset ? TAB_OFF(val) : KEY_COL(sp, ch);
139 * If folding screens (the historic vi screen format), past the end
140 * of the current screen, and the character was a tab, reset the
141 * current screen column to 0, and the total screen columns to the
142 * last column of the screen. Otherwise, display the rest of the
143 * character in the next screen.
145 #define TAB_RESET { \
146 curoff += chlen; \
147 if (!leftright && curoff >= sp->cols) \
148 if (ch == '\t') { \
149 curoff = 0; \
150 scno -= scno % sp->cols; \
151 } else \
152 curoff -= sp->cols; \
154 if (cnop == NULL)
155 while (len--) {
156 chlen = CHLEN(curoff);
157 last = scno;
158 scno += chlen;
159 TAB_RESET;
161 else
162 for (cno = *cnop;; --cno) {
163 chlen = CHLEN(curoff);
164 last = scno;
165 scno += chlen;
166 TAB_RESET;
167 if (cno == 0)
168 break;
171 /* Add the trailing '$' if the O_LIST option set. */
172 if (listset && cnop == NULL)
173 scno += KEY_LEN(sp, '$');
176 * The text input screen code needs to know how much additional
177 * room the last two characters required, so that it can handle
178 * tab character displays correctly.
180 if (diffp != NULL)
181 *diffp = scno - last;
182 return (scno);
186 * vs_rcm --
187 * Return the physical column from the line that will display a
188 * character closest to the currently most attractive character
189 * position (which is stored as a screen column).
191 * PUBLIC: size_t vs_rcm __P((SCR *, db_recno_t, int));
193 size_t
194 vs_rcm(SCR *sp, db_recno_t lno, int islast)
196 size_t len;
198 /* Last character is easy, and common. */
199 if (islast) {
200 if (db_get(sp, lno, 0, NULL, &len) || len == 0)
201 return (0);
202 return (len - 1);
205 /* First character is easy, and common. */
206 if (sp->rcm == 0)
207 return (0);
209 return (vs_colpos(sp, lno, sp->rcm));
213 * vs_colpos --
214 * Return the physical column from the line that will display a
215 * character closest to the specified screen column.
217 * PUBLIC: size_t vs_colpos __P((SCR *, db_recno_t, size_t));
219 size_t
220 vs_colpos(SCR *sp, db_recno_t lno, size_t cno)
222 size_t chlen, curoff, len, llen, off, scno;
223 int ch, leftright, listset;
224 CHAR_T *lp, *p;
226 /* Need the line to go any further. */
227 (void)db_get(sp, lno, 0, &lp, &llen);
229 /* Missing or empty lines are easy. */
230 if (lp == NULL || llen == 0)
231 return (0);
233 /* Store away the values of the list and leftright edit options. */
234 listset = O_ISSET(sp, O_LIST);
235 leftright = O_ISSET(sp, O_LEFTRIGHT);
237 /* Discard screen (logical) lines. */
238 off = cno / sp->cols;
239 cno %= sp->cols;
240 for (scno = 0, p = lp, len = llen; off--;) {
241 for (; len && scno < sp->cols; --len)
242 scno += CHLEN(scno);
245 * If reached the end of the physical line, return the last
246 * physical character in the line.
248 if (len == 0)
249 return (llen - 1);
252 * If folding screens (the historic vi screen format), past
253 * the end of the current screen, and the character was a tab,
254 * reset the current screen column to 0. Otherwise, the rest
255 * of the character is displayed in the next screen.
257 if (leftright && ch == '\t')
258 scno = 0;
259 else
260 scno -= sp->cols;
263 /* Step through the line until reach the right character or EOL. */
264 for (curoff = scno; len--;) {
265 chlen = CHLEN(curoff);
268 * If we've reached the specific character, there are three
269 * cases.
271 * 1: scno == cno, i.e. the current character ends at the
272 * screen character we care about.
273 * a: off < llen - 1, i.e. not the last character in
274 * the line, return the offset of the next character.
275 * b: else return the offset of the last character.
276 * 2: scno != cno, i.e. this character overruns the character
277 * we care about, return the offset of this character.
279 if ((scno += chlen) >= cno) {
280 off = p - lp;
281 return (scno == cno ?
282 (off < llen - 1 ? off : llen - 1) : off - 1);
285 TAB_RESET;
288 /* No such character; return the start of the last character. */
289 return (llen - 1);