1 /* $NetBSD: vs_relative.c,v 1.1.1.2 2008/05/18 14:31:41 aymeric Exp $ */
4 * Copyright (c) 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
15 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";
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <bitstring.h>
27 #include "../common/common.h"
32 * Return the logical column of the cursor in the line.
34 * PUBLIC: int vs_column __P((SCR *, size_t *));
37 vs_column(SCR
*sp
, size_t *colp
)
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 *, db_recno_t, size_t *));
58 vs_screens(SCR
*sp
, db_recno_t lno
, size_t *cnop
)
62 /* Left-right screens are simple, it's always 1. */
63 if (O_ISSET(sp
, O_LEFTRIGHT
))
67 * Check for a cached value. We maintain a cache because, if the
68 * line is large, this routine gets called repeatedly. One other
69 * hack, lots of time the cursor is on column one, which is an easy
73 if (VIP(sp
)->ss_lno
== lno
)
74 return (VIP(sp
)->ss_screens
);
75 } else if (*cnop
== 0)
78 /* Figure out how many columns the line/column needs. */
79 cols
= vs_columns(sp
, NULL
, lno
, cnop
, NULL
);
81 screens
= (cols
/ sp
->cols
+ (cols
% sp
->cols
? 1 : 0));
85 /* Cache the value. */
87 VIP(sp
)->ss_lno
= lno
;
88 VIP(sp
)->ss_screens
= screens
;
95 * Return the screen columns necessary to display the line, or,
96 * if specified, the physical character column within the line.
98 * PUBLIC: size_t vs_columns __P((SCR *, CHAR_T *, db_recno_t, size_t *, size_t *));
101 vs_columns(SCR
*sp
, CHAR_T
*lp
, db_recno_t lno
, size_t *cnop
, size_t *diffp
)
103 size_t chlen
, cno
, curoff
, last
= 0, len
, scno
;
104 int ch
, leftright
, listset
;
108 * Initialize the screen offset.
112 /* Leading number if O_NUMBER option set. */
113 if (O_ISSET(sp
, O_NUMBER
))
114 scno
+= O_NUMBER_LENGTH
;
116 /* Need the line to go any further. */
118 (void)db_get(sp
, lno
, 0, &lp
, &len
);
123 /* Missing or empty lines are easy. */
125 done
: if (diffp
!= NULL
) /* XXX */
130 /* Store away the values of the list and leftright edit options. */
131 listset
= O_ISSET(sp
, O_LIST
);
132 leftright
= O_ISSET(sp
, O_LEFTRIGHT
);
135 * Initialize the pointer into the buffer and current offset.
140 /* Macro to return the display length of any signal character. */
141 #define CHLEN(val) (ch = *(UCHAR_T *)p++) == '\t' && \
142 !listset ? TAB_OFF(val) : KEY_COL(sp, ch);
145 * If folding screens (the historic vi screen format), past the end
146 * of the current screen, and the character was a tab, reset the
147 * current screen column to 0, and the total screen columns to the
148 * last column of the screen. Otherwise, display the rest of the
149 * character in the next screen.
151 #define TAB_RESET { \
153 if (!leftright && curoff >= sp->cols) { \
156 scno -= scno % sp->cols; \
158 curoff -= sp->cols; \
163 chlen
= CHLEN(curoff
);
169 for (cno
= *cnop
;; --cno
) {
170 chlen
= CHLEN(curoff
);
178 /* Add the trailing '$' if the O_LIST option set. */
179 if (listset
&& cnop
== NULL
)
180 scno
+= KEY_LEN(sp
, '$');
183 * The text input screen code needs to know how much additional
184 * room the last two characters required, so that it can handle
185 * tab character displays correctly.
188 *diffp
= scno
- last
;
194 * Return the physical column from the line that will display a
195 * character closest to the currently most attractive character
196 * position (which is stored as a screen column).
198 * PUBLIC: size_t vs_rcm __P((SCR *, db_recno_t, int));
201 vs_rcm(SCR
*sp
, db_recno_t lno
, int islast
)
205 /* Last character is easy, and common. */
207 if (db_get(sp
, lno
, 0, NULL
, &len
) || len
== 0)
212 /* First character is easy, and common. */
216 return (vs_colpos(sp
, lno
, sp
->rcm
));
221 * Return the physical column from the line that will display a
222 * character closest to the specified screen column.
224 * PUBLIC: size_t vs_colpos __P((SCR *, db_recno_t, size_t));
227 vs_colpos(SCR
*sp
, db_recno_t lno
, size_t cno
)
229 size_t chlen
, curoff
, len
, llen
, off
, scno
;
230 int ch
= 0, leftright
, listset
;
233 /* Need the line to go any further. */
234 (void)db_get(sp
, lno
, 0, &lp
, &llen
);
236 /* Missing or empty lines are easy. */
237 if (lp
== NULL
|| llen
== 0)
240 /* Store away the values of the list and leftright edit options. */
241 listset
= O_ISSET(sp
, O_LIST
);
242 leftright
= O_ISSET(sp
, O_LEFTRIGHT
);
244 /* Discard screen (logical) lines. */
245 off
= cno
/ sp
->cols
;
247 for (scno
= 0, p
= lp
, len
= llen
; off
--;) {
248 for (; len
&& scno
< sp
->cols
; --len
)
252 * If reached the end of the physical line, return the last
253 * physical character in the line.
259 * If folding screens (the historic vi screen format), past
260 * the end of the current screen, and the character was a tab,
261 * reset the current screen column to 0. Otherwise, the rest
262 * of the character is displayed in the next screen.
264 if (leftright
&& ch
== '\t')
270 /* Step through the line until reach the right character or EOL. */
271 for (curoff
= scno
; len
--;) {
272 chlen
= CHLEN(curoff
);
275 * If we've reached the specific character, there are three
278 * 1: scno == cno, i.e. the current character ends at the
279 * screen character we care about.
280 * a: off < llen - 1, i.e. not the last character in
281 * the line, return the offset of the next character.
282 * b: else return the offset of the last character.
283 * 2: scno != cno, i.e. this character overruns the character
284 * we care about, return the offset of this character.
286 if ((scno
+= chlen
) >= cno
) {
288 return (scno
== cno
?
289 (off
< llen
- 1 ? off
: llen
- 1) : off
- 1);
295 /* No such character; return the start of the last character. */