1 /* $NetBSD: vs_relative.c,v 1.2 2013/11/22 15:52:06 christos Exp $ */
3 * Copyright (c) 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
14 static const char sccsid
[] = "Id: vs_relative.c,v 10.18 2001/07/08 13:02:48 skimo Exp (Berkeley) Date: 2001/07/08 13:02:48 ";
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <bitstring.h>
26 #include "../common/common.h"
31 * Return the logical column of the cursor in the line.
33 * PUBLIC: int vs_column __P((SCR *, size_t *));
36 vs_column(SCR
*sp
, size_t *colp
)
42 *colp
= (O_ISSET(sp
, O_LEFTRIGHT
) ?
43 vip
->sc_smap
->coff
: (vip
->sc_smap
->soff
- 1) * sp
->cols
) +
44 vip
->sc_col
- (O_ISSET(sp
, O_NUMBER
) ? O_NUMBER_LENGTH
: 0);
50 * Return the screens necessary to display the line, or if specified,
51 * the physical character column within the line, including space
52 * required for the O_NUMBER and O_LIST options.
54 * PUBLIC: size_t vs_screens __P((SCR *, db_recno_t, size_t *));
57 vs_screens(SCR
*sp
, db_recno_t lno
, size_t *cnop
)
61 /* Left-right screens are simple, it's always 1. */
62 if (O_ISSET(sp
, O_LEFTRIGHT
))
66 * Check for a cached value. We maintain a cache because, if the
67 * line is large, this routine gets called repeatedly. One other
68 * hack, lots of time the cursor is on column one, which is an easy
72 if (VIP(sp
)->ss_lno
== lno
)
73 return (VIP(sp
)->ss_screens
);
74 } else if (*cnop
== 0)
77 /* Figure out how many columns the line/column needs. */
78 cols
= vs_columns(sp
, NULL
, lno
, cnop
, NULL
);
80 screens
= (cols
/ sp
->cols
+ (cols
% sp
->cols
? 1 : 0));
84 /* Cache the value. */
86 VIP(sp
)->ss_lno
= lno
;
87 VIP(sp
)->ss_screens
= screens
;
94 * Return the screen columns necessary to display the line, or,
95 * if specified, the physical character column within the line.
97 * PUBLIC: size_t vs_columns __P((SCR *, CHAR_T *, db_recno_t, size_t *, size_t *));
100 vs_columns(SCR
*sp
, CHAR_T
*lp
, db_recno_t lno
, size_t *cnop
, size_t *diffp
)
102 size_t chlen
, cno
, curoff
, last
= 0, len
, scno
;
103 int ch
, leftright
, listset
;
107 * Initialize the screen offset.
111 /* Leading number if O_NUMBER option set. */
112 if (O_ISSET(sp
, O_NUMBER
))
113 scno
+= O_NUMBER_LENGTH
;
115 /* Need the line to go any further. */
117 (void)db_get(sp
, lno
, 0, &lp
, &len
);
122 /* Missing or empty lines are easy. */
124 done
: if (diffp
!= NULL
) /* XXX */
129 /* Store away the values of the list and leftright edit options. */
130 listset
= O_ISSET(sp
, O_LIST
);
131 leftright
= O_ISSET(sp
, O_LEFTRIGHT
);
134 * Initialize the pointer into the buffer and current offset.
139 /* Macro to return the display length of any signal character. */
140 #define CHLEN(val) (ch = *(UCHAR_T *)p++) == '\t' && \
141 !listset ? TAB_OFF(val) : KEY_COL(sp, ch);
144 * If folding screens (the historic vi screen format), past the end
145 * of the current screen, and the character was a tab, reset the
146 * current screen column to 0, and the total screen columns to the
147 * last column of the screen. Otherwise, display the rest of the
148 * character in the next screen.
150 #define TAB_RESET { \
152 if (!leftright && curoff >= sp->cols) { \
155 scno -= scno % sp->cols; \
157 curoff -= sp->cols; \
162 chlen
= CHLEN(curoff
);
168 for (cno
= *cnop
;; --cno
) {
169 chlen
= CHLEN(curoff
);
177 /* Add the trailing '$' if the O_LIST option set. */
178 if (listset
&& cnop
== NULL
)
179 scno
+= KEY_LEN(sp
, '$');
182 * The text input screen code needs to know how much additional
183 * room the last two characters required, so that it can handle
184 * tab character displays correctly.
187 *diffp
= scno
- last
;
193 * Return the physical column from the line that will display a
194 * character closest to the currently most attractive character
195 * position (which is stored as a screen column).
197 * PUBLIC: size_t vs_rcm __P((SCR *, db_recno_t, int));
200 vs_rcm(SCR
*sp
, db_recno_t lno
, int islast
)
204 /* Last character is easy, and common. */
206 if (db_get(sp
, lno
, 0, NULL
, &len
) || len
== 0)
211 /* First character is easy, and common. */
215 return (vs_colpos(sp
, lno
, sp
->rcm
));
220 * Return the physical column from the line that will display a
221 * character closest to the specified screen column.
223 * PUBLIC: size_t vs_colpos __P((SCR *, db_recno_t, size_t));
226 vs_colpos(SCR
*sp
, db_recno_t lno
, size_t cno
)
228 size_t chlen
, curoff
, len
, llen
, off
, scno
;
229 int ch
= 0, leftright
, listset
;
232 /* Need the line to go any further. */
233 (void)db_get(sp
, lno
, 0, &lp
, &llen
);
235 /* Missing or empty lines are easy. */
236 if (lp
== NULL
|| llen
== 0)
239 /* Store away the values of the list and leftright edit options. */
240 listset
= O_ISSET(sp
, O_LIST
);
241 leftright
= O_ISSET(sp
, O_LEFTRIGHT
);
243 /* Discard screen (logical) lines. */
244 off
= cno
/ sp
->cols
;
246 for (scno
= 0, p
= lp
, len
= llen
; off
--;) {
247 for (; len
&& scno
< sp
->cols
; --len
)
251 * If reached the end of the physical line, return the last
252 * physical character in the line.
258 * If folding screens (the historic vi screen format), past
259 * the end of the current screen, and the character was a tab,
260 * reset the current screen column to 0. Otherwise, the rest
261 * of the character is displayed in the next screen.
263 if (leftright
&& ch
== '\t')
269 /* Step through the line until reach the right character or EOL. */
270 for (curoff
= scno
; len
--;) {
271 chlen
= CHLEN(curoff
);
274 * If we've reached the specific character, there are three
277 * 1: scno == cno, i.e. the current character ends at the
278 * screen character we care about.
279 * a: off < llen - 1, i.e. not the last character in
280 * the line, return the offset of the next character.
281 * b: else return the offset of the last character.
282 * 2: scno != cno, i.e. this character overruns the character
283 * we care about, return the offset of this character.
285 if ((scno
+= chlen
) >= cno
) {
287 return (scno
== cno
?
288 (off
< llen
- 1 ? off
: llen
- 1) : off
- 1);
294 /* No such character; return the start of the last character. */