1 /* $OpenBSD: vs_relative.c,v 1.8 2009/10/27 23:59:49 deraadt Exp $ */
2
3 /*-
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.
8 *
9 * See the LICENSE file for redistribution information.
10 */
11
12 #include "config.h"
13
14 #include <sys/types.h>
15 #include <sys/queue.h>
16 #include <sys/time.h>
17
18 #include <bitstring.h>
19 #include <limits.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 #include "../common/common.h"
24 #include "vi.h"
25
26 /*
27 * vs_column --
28 * Return the logical column of the cursor in the line.
29 *
30 * PUBLIC: int vs_column(SCR *, size_t *);
31 */
32 int
vs_column(sp,colp)33 vs_column(sp, colp)
34 SCR *sp;
35 size_t *colp;
36 {
37 VI_PRIVATE *vip;
38
39 vip = VIP(sp);
40
41 *colp = (O_ISSET(sp, O_LEFTRIGHT) ?
42 vip->sc_smap->coff : (vip->sc_smap->soff - 1) * sp->cols) +
43 vip->sc_col - (O_ISSET(sp, O_NUMBER) ? O_NUMBER_LENGTH : 0);
44 return (0);
45 }
46
47 /*
48 * vs_screens --
49 * Return the screens necessary to display the line, or if specified,
50 * the physical character column within the line, including space
51 * required for the O_NUMBER and O_LIST options.
52 *
53 * PUBLIC: size_t vs_screens(SCR *, recno_t, size_t *);
54 */
55 size_t
vs_screens(sp,lno,cnop)56 vs_screens(sp, lno, cnop)
57 SCR *sp;
58 recno_t lno;
59 size_t *cnop;
60 {
61 size_t cols, screens;
62
63 /* Left-right screens are simple, it's always 1. */
64 if (O_ISSET(sp, O_LEFTRIGHT))
65 return (1);
66
67 /*
68 * Check for a cached value. We maintain a cache because, if the
69 * line is large, this routine gets called repeatedly. One other
70 * hack, lots of time the cursor is on column one, which is an easy
71 * one.
72 */
73 if (cnop == NULL) {
74 if (VIP(sp)->ss_lno == lno)
75 return (VIP(sp)->ss_screens);
76 } else if (*cnop == 0)
77 return (1);
78
79 /* Figure out how many columns the line/column needs. */
80 cols = vs_columns(sp, NULL, lno, cnop, NULL);
81
82 screens = (cols / sp->cols + (cols % sp->cols ? 1 : 0));
83 if (screens == 0)
84 screens = 1;
85
86 /* Cache the value. */
87 if (cnop == NULL) {
88 VIP(sp)->ss_lno = lno;
89 VIP(sp)->ss_screens = screens;
90 }
91 return (screens);
92 }
93
94 /*
95 * vs_columns --
96 * Return the screen columns necessary to display the line, or,
97 * if specified, the physical character column within the line.
98 *
99 * PUBLIC: size_t vs_columns(SCR *, char *, recno_t, size_t *, size_t *);
100 */
101 size_t
vs_columns(sp,lp,lno,cnop,diffp)102 vs_columns(sp, lp, lno, cnop, diffp)
103 SCR *sp;
104 char *lp;
105 recno_t lno;
106 size_t *cnop, *diffp;
107 {
108 size_t chlen, cno, curoff, last, len, scno;
109 int ch, leftright, listset;
110 char *p;
111
112 /*
113 * Initialize the screen offset.
114 */
115 scno = 0;
116 curoff = 0;
117
118 /* Leading number if O_NUMBER option set. */
119 if (O_ISSET(sp, O_NUMBER)) {
120 scno += O_NUMBER_LENGTH;
121 curoff += O_NUMBER_LENGTH;
122 }
123
124 /* Need the line to go any further. */
125 if (lp == NULL) {
126 (void)db_get(sp, lno, 0, &lp, &len);
127 if (len == 0)
128 goto done;
129 }
130
131 /* Missing or empty lines are easy. */
132 if (lp == NULL) {
133 done: if (diffp != NULL) /* XXX */
134 *diffp = 0;
135 return (scno);
136 }
137
138 /* Store away the values of the list and leftright edit options. */
139 listset = O_ISSET(sp, O_LIST);
140 leftright = O_ISSET(sp, O_LEFTRIGHT);
141
142 /*
143 * Initialize the pointer into the buffer.
144 */
145 p = lp;
146
147 /* Macro to return the display length of any signal character. */
148 #define CHLEN(val) (ch = *(u_char *)p++) == '\t' && \
149 !listset ? TAB_OFF(val) : KEY_LEN(sp, ch);
150
151 /*
152 * If folding screens (the historic vi screen format), past the end
153 * of the current screen, and the character was a tab, reset the
154 * current screen column to 0, and the total screen columns to the
155 * last column of the screen. Otherwise, display the rest of the
156 * character in the next screen.
157 */
158 #define TAB_RESET { \
159 curoff += chlen; \
160 if (!leftright && curoff >= sp->cols) { \
161 if (ch == '\t') { \
162 curoff = 0; \
163 scno -= scno % sp->cols; \
164 } else \
165 curoff -= sp->cols; \
166 } \
167 }
168 if (cnop == NULL)
169 while (len--) {
170 chlen = CHLEN(curoff);
171 last = scno;
172 scno += chlen;
173 TAB_RESET;
174 }
175 else
176 for (cno = *cnop;; --cno) {
177 chlen = CHLEN(curoff);
178 last = scno;
179 scno += chlen;
180 TAB_RESET;
181 if (cno == 0)
182 break;
183 }
184
185 /* Add the trailing '$' if the O_LIST option set. */
186 if (listset && cnop == NULL)
187 scno += KEY_LEN(sp, '$');
188
189 /*
190 * The text input screen code needs to know how much additional
191 * room the last two characters required, so that it can handle
192 * tab character displays correctly.
193 */
194 if (diffp != NULL)
195 *diffp = scno - last;
196 return (scno);
197 }
198
199 /*
200 * vs_rcm --
201 * Return the physical column from the line that will display a
202 * character closest to the currently most attractive character
203 * position (which is stored as a screen column).
204 *
205 * PUBLIC: size_t vs_rcm(SCR *, recno_t, int);
206 */
207 size_t
vs_rcm(sp,lno,islast)208 vs_rcm(sp, lno, islast)
209 SCR *sp;
210 recno_t lno;
211 int islast;
212 {
213 size_t len;
214
215 /* Last character is easy, and common. */
216 if (islast) {
217 if (db_get(sp, lno, 0, NULL, &len) || len == 0)
218 return (0);
219 return (len - 1);
220 }
221
222 /* First character is easy, and common. */
223 if (sp->rcm == 0)
224 return (0);
225
226 return (vs_colpos(sp, lno, sp->rcm));
227 }
228
229 /*
230 * vs_colpos --
231 * Return the physical column from the line that will display a
232 * character closest to the specified screen column.
233 *
234 * PUBLIC: size_t vs_colpos(SCR *, recno_t, size_t);
235 */
236 size_t
vs_colpos(sp,lno,cno)237 vs_colpos(sp, lno, cno)
238 SCR *sp;
239 recno_t lno;
240 size_t cno;
241 {
242 size_t chlen, curoff, len, llen, off, scno;
243 int ch, leftright, listset;
244 char *lp, *p;
245
246 /* Need the line to go any further. */
247 (void)db_get(sp, lno, 0, &lp, &llen);
248
249 /* Missing or empty lines are easy. */
250 if (lp == NULL || llen == 0)
251 return (0);
252
253 /* Store away the values of the list and leftright edit options. */
254 listset = O_ISSET(sp, O_LIST);
255 leftright = O_ISSET(sp, O_LEFTRIGHT);
256
257 /* Discard screen (logical) lines. */
258 off = cno / sp->cols;
259 cno %= sp->cols;
260 for (scno = 0, p = lp, len = llen; off--;) {
261 for (; len && scno < sp->cols; --len)
262 scno += CHLEN(scno);
263
264 /*
265 * If reached the end of the physical line, return the last
266 * physical character in the line.
267 */
268 if (len == 0)
269 return (llen - 1);
270
271 /*
272 * If folding screens (the historic vi screen format), past
273 * the end of the current screen, and the character was a tab,
274 * reset the current screen column to 0. Otherwise, the rest
275 * of the character is displayed in the next screen.
276 */
277 if (leftright && ch == '\t')
278 scno = 0;
279 else
280 scno -= sp->cols;
281 }
282
283 /* Step through the line until reach the right character or EOL. */
284 for (curoff = scno; len--;) {
285 chlen = CHLEN(curoff);
286
287 /*
288 * If we've reached the specific character, there are three
289 * cases.
290 *
291 * 1: scno == cno, i.e. the current character ends at the
292 * screen character we care about.
293 * a: off < llen - 1, i.e. not the last character in
294 * the line, return the offset of the next character.
295 * b: else return the offset of the last character.
296 * 2: scno != cno, i.e. this character overruns the character
297 * we care about, return the offset of this character.
298 */
299 if ((scno += chlen) >= cno) {
300 off = p - lp;
301 return (scno == cno ?
302 (off < llen - 1 ? off : llen - 1) : off - 1);
303 }
304
305 TAB_RESET;
306 }
307
308 /* No such character; return the start of the last character. */
309 return (llen - 1);
310 }
311