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.
13 static const char sccsid[] = "@(#)vs_relative.c 10.11 (Berkeley) 5/13/96";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
25 #include "../common/common.h"
30 * Return the logical column of the cursor in the line.
32 * PUBLIC: int vs_column __P((SCR *, size_t *));
43 *colp = (O_ISSET(sp, O_LEFTRIGHT) ?
44 vip->sc_smap->coff : (vip->sc_smap->soff - 1) * sp->cols) +
45 vip->sc_col - (O_ISSET(sp, O_NUMBER) ? O_NUMBER_LENGTH : 0);
51 * Return the screens necessary to display the line, or if specified,
52 * the physical character column within the line, including space
53 * required for the O_NUMBER and O_LIST options.
55 * PUBLIC: size_t vs_screens __P((SCR *, recno_t, size_t *));
58 vs_screens(sp, lno, cnop)
65 /* Left-right screens are simple, it's always 1. */
66 if (O_ISSET(sp, O_LEFTRIGHT))
70 * Check for a cached value. We maintain a cache because, if the
71 * line is large, this routine gets called repeatedly. One other
72 * hack, lots of time the cursor is on column one, which is an easy
76 if (VIP(sp)->ss_lno == lno)
77 return (VIP(sp)->ss_screens);
78 } else if (*cnop == 0)
81 /* Figure out how many columns the line/column needs. */
82 cols = vs_columns(sp, NULL, lno, cnop, NULL);
84 screens = (cols / sp->cols + (cols % sp->cols ? 1 : 0));
88 /* Cache the value. */
90 VIP(sp)->ss_lno = lno;
91 VIP(sp)->ss_screens = screens;
98 * Return the screen columns necessary to display the line, or,
99 * if specified, the physical character column within the line.
101 * PUBLIC: size_t vs_columns __P((SCR *, char *, recno_t, size_t *, size_t *));
104 vs_columns(sp, lp, lno, cnop, diffp)
108 size_t *cnop, *diffp;
110 size_t chlen, cno, curoff, last, len, scno;
111 int ch, leftright, listset;
115 * Initialize the screen offset.
119 /* Leading number if O_NUMBER option set. */
120 if (O_ISSET(sp, O_NUMBER))
121 scno += O_NUMBER_LENGTH;
123 /* Need the line to go any further. */
125 (void)db_get(sp, lno, 0, &lp, &len);
130 /* Missing or empty lines are easy. */
132 done: if (diffp != NULL) /* XXX */
137 /* Store away the values of the list and leftright edit options. */
138 listset = O_ISSET(sp, O_LIST);
139 leftright = O_ISSET(sp, O_LEFTRIGHT);
142 * Initialize the pointer into the buffer and current offset.
147 /* Macro to return the display length of any signal character. */
148 #define CHLEN(val) (ch = *(u_char *)p++) == '\t' && \
149 !listset ? TAB_OFF(val) : KEY_LEN(sp, ch);
152 * If folding screens (the historic vi screen format), past the end
153 * of the current screen, and the character was a tab, reset the
154 * current screen column to 0, and the total screen columns to the
155 * last column of the screen. Otherwise, display the rest of the
156 * character in the next screen.
158 #define TAB_RESET { \
160 if (!leftright && curoff >= sp->cols) \
163 scno -= scno % sp->cols; \
165 curoff -= sp->cols; \
169 chlen = CHLEN(curoff);
175 for (cno = *cnop;; --cno) {
176 chlen = CHLEN(curoff);
184 /* Add the trailing '$' if the O_LIST option set. */
185 if (listset && cnop == NULL)
186 scno += KEY_LEN(sp, '$');
189 * The text input screen code needs to know how much additional
190 * room the last two characters required, so that it can handle
191 * tab character displays correctly.
194 *diffp = scno - last;
200 * Return the physical column from the line that will display a
201 * character closest to the currently most attractive character
202 * position (which is stored as a screen column).
204 * PUBLIC: size_t vs_rcm __P((SCR *, recno_t, int));
207 vs_rcm(sp, lno, islast)
214 /* Last character is easy, and common. */
216 if (db_get(sp, lno, 0, NULL, &len) || len == 0)
221 /* First character is easy, and common. */
225 return (vs_colpos(sp, lno, sp->rcm));
230 * Return the physical column from the line that will display a
231 * character closest to the specified screen column.
233 * PUBLIC: size_t vs_colpos __P((SCR *, recno_t, size_t));
236 vs_colpos(sp, lno, cno)
241 size_t chlen, curoff, len, llen, off, scno;
242 int ch, leftright, listset;
245 /* Need the line to go any further. */
246 (void)db_get(sp, lno, 0, &lp, &llen);
248 /* Missing or empty lines are easy. */
249 if (lp == NULL || llen == 0)
252 /* Store away the values of the list and leftright edit options. */
253 listset = O_ISSET(sp, O_LIST);
254 leftright = O_ISSET(sp, O_LEFTRIGHT);
256 /* Discard screen (logical) lines. */
257 off = cno / sp->cols;
259 for (scno = 0, p = lp, len = llen; off--;) {
260 for (; len && scno < sp->cols; --len)
264 * If reached the end of the physical line, return the last
265 * physical character in the line.
271 * If folding screens (the historic vi screen format), past
272 * the end of the current screen, and the character was a tab,
273 * reset the current screen column to 0. Otherwise, the rest
274 * of the character is displayed in the next screen.
276 if (leftright && ch == '\t')
282 /* Step through the line until reach the right character or EOL. */
283 for (curoff = scno; len--;) {
284 chlen = CHLEN(curoff);
287 * If we've reached the specific character, there are three
290 * 1: scno == cno, i.e. the current character ends at the
291 * screen character we care about.
292 * a: off < llen - 1, i.e. not the last character in
293 * the line, return the offset of the next character.
294 * b: else return the offset of the last character.
295 * 2: scno != cno, i.e. this character overruns the character
296 * we care about, return the offset of this character.
298 if ((scno += chlen) >= cno) {
300 return (scno == cno ?
301 (off < llen - 1 ? off : llen - 1) : off - 1);
307 /* No such character; return the start of the last character. */