1 /* $NetBSD: vs_relative.c,v 1.3 2014/01/26 21:43:45 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.
13 #include <sys/cdefs.h>
16 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 ";
19 __RCSID("$NetBSD: vs_relative.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
26 #include <bitstring.h>
31 #include "../common/common.h"
36 * Return the logical column of the cursor in the line.
38 * PUBLIC: int vs_column __P((SCR *, size_t *));
41 vs_column(SCR
*sp
, size_t *colp
)
47 *colp
= (O_ISSET(sp
, O_LEFTRIGHT
) ?
48 vip
->sc_smap
->coff
: (vip
->sc_smap
->soff
- 1) * sp
->cols
) +
49 vip
->sc_col
- (O_ISSET(sp
, O_NUMBER
) ? O_NUMBER_LENGTH
: 0);
55 * Return the screens necessary to display the line, or if specified,
56 * the physical character column within the line, including space
57 * required for the O_NUMBER and O_LIST options.
59 * PUBLIC: size_t vs_screens __P((SCR *, db_recno_t, size_t *));
62 vs_screens(SCR
*sp
, db_recno_t lno
, size_t *cnop
)
66 /* Left-right screens are simple, it's always 1. */
67 if (O_ISSET(sp
, O_LEFTRIGHT
))
71 * Check for a cached value. We maintain a cache because, if the
72 * line is large, this routine gets called repeatedly. One other
73 * hack, lots of time the cursor is on column one, which is an easy
77 if (VIP(sp
)->ss_lno
== lno
)
78 return (VIP(sp
)->ss_screens
);
79 } else if (*cnop
== 0)
82 /* Figure out how many columns the line/column needs. */
83 cols
= vs_columns(sp
, NULL
, lno
, cnop
, NULL
);
85 screens
= (cols
/ sp
->cols
+ (cols
% sp
->cols
? 1 : 0));
89 /* Cache the value. */
91 VIP(sp
)->ss_lno
= lno
;
92 VIP(sp
)->ss_screens
= screens
;
99 * Return the screen columns necessary to display the line, or,
100 * if specified, the physical character column within the line.
102 * PUBLIC: size_t vs_columns __P((SCR *, CHAR_T *, db_recno_t, size_t *, size_t *));
105 vs_columns(SCR
*sp
, CHAR_T
*lp
, db_recno_t lno
, size_t *cnop
, size_t *diffp
)
107 size_t chlen
, cno
, curoff
, last
= 0, len
, scno
;
108 int ch
, leftright
, listset
;
112 * Initialize the screen offset.
116 /* Leading number if O_NUMBER option set. */
117 if (O_ISSET(sp
, O_NUMBER
))
118 scno
+= O_NUMBER_LENGTH
;
120 /* Need the line to go any further. */
122 (void)db_get(sp
, lno
, 0, &lp
, &len
);
127 /* Missing or empty lines are easy. */
129 done
: if (diffp
!= NULL
) /* XXX */
134 /* Store away the values of the list and leftright edit options. */
135 listset
= O_ISSET(sp
, O_LIST
);
136 leftright
= O_ISSET(sp
, O_LEFTRIGHT
);
139 * Initialize the pointer into the buffer and current offset.
144 /* Macro to return the display length of any signal character. */
145 #define CHLEN(val) (ch = *(UCHAR_T *)p++) == '\t' && \
146 !listset ? TAB_OFF(val) : KEY_COL(sp, ch);
149 * If folding screens (the historic vi screen format), past the end
150 * of the current screen, and the character was a tab, reset the
151 * current screen column to 0, and the total screen columns to the
152 * last column of the screen. Otherwise, display the rest of the
153 * character in the next screen.
155 #define TAB_RESET { \
157 if (!leftright && curoff >= sp->cols) { \
160 scno -= scno % sp->cols; \
162 curoff -= sp->cols; \
167 chlen
= CHLEN(curoff
);
173 for (cno
= *cnop
;; --cno
) {
174 chlen
= CHLEN(curoff
);
182 /* Add the trailing '$' if the O_LIST option set. */
183 if (listset
&& cnop
== NULL
)
184 scno
+= KEY_LEN(sp
, '$');
187 * The text input screen code needs to know how much additional
188 * room the last two characters required, so that it can handle
189 * tab character displays correctly.
192 *diffp
= scno
- last
;
198 * Return the physical column from the line that will display a
199 * character closest to the currently most attractive character
200 * position (which is stored as a screen column).
202 * PUBLIC: size_t vs_rcm __P((SCR *, db_recno_t, int));
205 vs_rcm(SCR
*sp
, db_recno_t lno
, int islast
)
209 /* Last character is easy, and common. */
211 if (db_get(sp
, lno
, 0, NULL
, &len
) || len
== 0)
216 /* First character is easy, and common. */
220 return (vs_colpos(sp
, lno
, sp
->rcm
));
225 * Return the physical column from the line that will display a
226 * character closest to the specified screen column.
228 * PUBLIC: size_t vs_colpos __P((SCR *, db_recno_t, size_t));
231 vs_colpos(SCR
*sp
, db_recno_t lno
, size_t cno
)
233 size_t chlen
, curoff
, len
, llen
, off
, scno
;
234 int ch
= 0, leftright
, listset
;
237 /* Need the line to go any further. */
238 (void)db_get(sp
, lno
, 0, &lp
, &llen
);
240 /* Missing or empty lines are easy. */
241 if (lp
== NULL
|| llen
== 0)
244 /* Store away the values of the list and leftright edit options. */
245 listset
= O_ISSET(sp
, O_LIST
);
246 leftright
= O_ISSET(sp
, O_LEFTRIGHT
);
248 /* Discard screen (logical) lines. */
249 off
= cno
/ sp
->cols
;
251 for (scno
= 0, p
= lp
, len
= llen
; off
--;) {
252 for (; len
&& scno
< sp
->cols
; --len
)
256 * If reached the end of the physical line, return the last
257 * physical character in the line.
263 * If folding screens (the historic vi screen format), past
264 * the end of the current screen, and the character was a tab,
265 * reset the current screen column to 0. Otherwise, the rest
266 * of the character is displayed in the next screen.
268 if (leftright
&& ch
== '\t')
274 /* Step through the line until reach the right character or EOL. */
275 for (curoff
= scno
; len
--;) {
276 chlen
= CHLEN(curoff
);
279 * If we've reached the specific character, there are three
282 * 1: scno == cno, i.e. the current character ends at the
283 * screen character we care about.
284 * a: off < llen - 1, i.e. not the last character in
285 * the line, return the offset of the next character.
286 * b: else return the offset of the last character.
287 * 2: scno != cno, i.e. this character overruns the character
288 * we care about, return the offset of this character.
290 if ((scno
+= chlen
) >= cno
) {
292 return (scno
== cno
?
293 (off
< llen
- 1 ? off
: llen
- 1) : off
- 1);
299 /* No such character; return the start of the last character. */