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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
21 #include "../common/common.h"
26 * Return the logical column of the cursor in the line.
28 * PUBLIC: int vs_column(SCR *, size_t *);
31 vs_column(SCR *sp, size_t *colp)
37 *colp = (O_ISSET(sp, O_LEFTRIGHT) ?
38 vip->sc_smap->coff : (vip->sc_smap->soff - 1) * sp->cols) +
39 vip->sc_col - (O_ISSET(sp, O_NUMBER) ? O_NUMBER_LENGTH : 0);
45 * Return the screens necessary to display the line, or if specified,
46 * the physical character column within the line, including space
47 * required for the O_NUMBER and O_LIST options.
49 * PUBLIC: size_t vs_screens(SCR *, recno_t, size_t *);
52 vs_screens(SCR *sp, recno_t lno, size_t *cnop)
56 /* Left-right screens are simple, it's always 1. */
57 if (O_ISSET(sp, O_LEFTRIGHT))
61 * Check for a cached value. We maintain a cache because, if the
62 * line is large, this routine gets called repeatedly. One other
63 * hack, lots of time the cursor is on column one, which is an easy
67 if (VIP(sp)->ss_lno == lno)
68 return (VIP(sp)->ss_screens);
69 } else if (*cnop == 0)
72 /* Figure out how many columns the line/column needs. */
73 cols = vs_columns(sp, NULL, lno, cnop, NULL);
75 screens = (cols / sp->cols + (cols % sp->cols ? 1 : 0));
79 /* Cache the value. */
81 VIP(sp)->ss_lno = lno;
82 VIP(sp)->ss_screens = screens;
89 * Return the screen columns necessary to display the line, or,
90 * if specified, the physical character column within the line.
92 * PUBLIC: size_t vs_columns(SCR *, CHAR_T *, recno_t, size_t *, size_t *);
95 vs_columns(SCR *sp, CHAR_T *lp, recno_t lno, size_t *cnop, size_t *diffp)
97 size_t chlen, cno, curoff, last = 0, len, scno;
98 int ch, leftright, listset;
102 * Initialize the screen offset.
106 /* Leading number if O_NUMBER option set. */
107 if (O_ISSET(sp, O_NUMBER))
108 scno += O_NUMBER_LENGTH;
110 /* Need the line to go any further. */
112 (void)db_get(sp, lno, 0, &lp, &len);
117 /* Missing or empty lines are easy. */
119 done: if (diffp != NULL) /* XXX */
124 /* Store away the values of the list and leftright edit options. */
125 listset = O_ISSET(sp, O_LIST);
126 leftright = O_ISSET(sp, O_LEFTRIGHT);
129 * Initialize the pointer into the buffer and current offset.
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 do { \
147 if (!leftright && curoff >= sp->cols) { \
150 scno -= scno % sp->cols; \
152 curoff -= sp->cols; \
157 chlen = CHLEN(curoff);
163 for (cno = *cnop;; --cno) {
164 chlen = CHLEN(curoff);
172 /* Add the trailing '$' if the O_LIST option set. */
173 if (listset && cnop == NULL)
174 scno += KEY_LEN(sp, '$');
177 * The text input screen code needs to know how much additional
178 * room the last two characters required, so that it can handle
179 * tab character displays correctly.
182 *diffp = scno - last;
188 * Return the physical column from the line that will display a
189 * character closest to the currently most attractive character
190 * position (which is stored as a screen column).
192 * PUBLIC: size_t vs_rcm(SCR *, recno_t, int);
195 vs_rcm(SCR *sp, recno_t lno, int islast)
199 /* Last character is easy, and common. */
201 if (db_get(sp, lno, 0, NULL, &len) || len == 0)
206 /* First character is easy, and common. */
210 return (vs_colpos(sp, lno, sp->rcm));
215 * Return the physical column from the line that will display a
216 * character closest to the specified screen column.
218 * PUBLIC: size_t vs_colpos(SCR *, recno_t, size_t);
221 vs_colpos(SCR *sp, recno_t lno, size_t cno)
223 size_t chlen, curoff, len, llen, off, scno;
224 int ch = 0, leftright, listset;
227 /* Need the line to go any further. */
228 (void)db_get(sp, lno, 0, &lp, &llen);
230 /* Missing or empty lines are easy. */
231 if (lp == NULL || llen == 0)
234 /* Store away the values of the list and leftright edit options. */
235 listset = O_ISSET(sp, O_LIST);
236 leftright = O_ISSET(sp, O_LEFTRIGHT);
238 /* Discard screen (logical) lines. */
239 off = cno / sp->cols;
241 for (scno = 0, p = lp, len = llen; off--;) {
242 for (; len && scno < sp->cols; --len)
246 * If reached the end of the physical line, return the last
247 * physical character in the line.
253 * If folding screens (the historic vi screen format), past
254 * the end of the current screen, and the character was a tab,
255 * reset the current screen column to 0. Otherwise, the rest
256 * of the character is displayed in the next screen.
258 if (leftright && ch == '\t')
264 /* Step through the line until reach the right character or EOL. */
265 for (curoff = scno; len--;) {
266 chlen = CHLEN(curoff);
269 * If we've reached the specific character, there are three
272 * 1: scno == cno, i.e. the current character ends at the
273 * screen character we care about.
274 * a: off < llen - 1, i.e. not the last character in
275 * the line, return the offset of the next character.
276 * b: else return the offset of the last character.
277 * 2: scno != cno, i.e. this character overruns the character
278 * we care about, return the offset of this character.
280 if ((scno += chlen) >= cno) {
282 return (scno == cno ?
283 (off < llen - 1 ? off : llen - 1) : off - 1);
289 /* No such character; return the start of the last character. */