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
;
114 /* Need the line to go any further. */
116 (void)db_get(sp
, lno
, 0, &lp
, &len
);
121 /* Missing or empty lines are easy. */
123 done
: if (diffp
!= NULL
) /* XXX */
128 /* Store away the values of the list and leftright edit options. */
129 listset
= O_ISSET(sp
, O_LIST
);
130 leftright
= O_ISSET(sp
, O_LEFTRIGHT
);
133 * Initialize the pointer into the buffer and screen and current
139 /* Leading number if O_NUMBER option set. */
140 if (O_ISSET(sp
, O_NUMBER
))
141 scno
+= O_NUMBER_LENGTH
;
143 /* Macro to return the display length of any signal character. */
144 #define CHLEN(val) (ch = *(u_char *)p++) == '\t' && \
145 !listset ? TAB_OFF(val) : KEY_LEN(sp, ch);
148 * If folding screens (the historic vi screen format), past the end
149 * of the current screen, and the character was a tab, reset the
150 * current screen column to 0, and the total screen columns to the
151 * last column of the screen. Otherwise, display the rest of the
152 * character in the next screen.
154 #define TAB_RESET { \
156 if (!leftright && curoff >= sp->cols) \
159 scno -= scno % sp->cols; \
161 curoff -= sp->cols; \
165 chlen
= CHLEN(curoff
);
171 for (cno
= *cnop
;; --cno
) {
172 chlen
= CHLEN(curoff
);
180 /* Add the trailing '$' if the O_LIST option set. */
181 if (listset
&& cnop
== NULL
)
182 scno
+= KEY_LEN(sp
, '$');
185 * The text input screen code needs to know how much additional
186 * room the last two characters required, so that it can handle
187 * tab character displays correctly.
190 *diffp
= scno
- last
;
196 * Return the physical column from the line that will display a
197 * character closest to the currently most attractive character
198 * position (which is stored as a screen column).
200 * PUBLIC: size_t vs_rcm __P((SCR *, recno_t, int));
203 vs_rcm(sp
, lno
, islast
)
210 /* Last character is easy, and common. */
212 if (db_get(sp
, lno
, 0, NULL
, &len
) || len
== 0)
217 /* First character is easy, and common. */
221 return (vs_colpos(sp
, lno
, sp
->rcm
));
226 * Return the physical column from the line that will display a
227 * character closest to the specified screen column.
229 * PUBLIC: size_t vs_colpos __P((SCR *, recno_t, size_t));
232 vs_colpos(sp
, lno
, cno
)
237 size_t chlen
, curoff
, len
, llen
, off
, scno
;
238 int ch
, leftright
, listset
;
241 /* Need the line to go any further. */
242 (void)db_get(sp
, lno
, 0, &lp
, &llen
);
244 /* Missing or empty lines are easy. */
245 if (lp
== NULL
|| llen
== 0)
248 /* Store away the values of the list and leftright edit options. */
249 listset
= O_ISSET(sp
, O_LIST
);
250 leftright
= O_ISSET(sp
, O_LEFTRIGHT
);
252 /* Discard screen (logical) lines. */
253 off
= cno
/ sp
->cols
;
255 for (scno
= 0, p
= lp
, len
= llen
; off
--;) {
256 for (; len
&& scno
< sp
->cols
; --len
)
260 * If reached the end of the physical line, return the last
261 * physical character in the line.
267 * If folding screens (the historic vi screen format), past
268 * the end of the current screen, and the character was a tab,
269 * reset the current screen column to 0. Otherwise, the rest
270 * of the character is displayed in the next screen.
272 if (leftright
&& ch
== '\t')
278 /* Step through the line until reach the right character or EOL. */
279 for (curoff
= scno
; len
--;) {
280 chlen
= CHLEN(curoff
);
283 * If we've reached the specific character, there are three
286 * 1: scno == cno, i.e. the current character ends at the
287 * screen character we care about.
288 * a: off < llen - 1, i.e. not the last character in
289 * the line, return the offset of the next character.
290 * b: else return the offset of the last character.
291 * 2: scno != cno, i.e. this character overruns the character
292 * we care about, return the offset of this character.
294 if ((scno
+= chlen
) >= cno
) {
296 return (scno
== cno
?
297 (off
< llen
- 1 ? off
: llen
- 1) : off
- 1);
303 /* No such character; return the start of the last character. */