GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/vi/build/../vi/vs_relative.c Lines: 0 79 0.0 %
Date: 2017-11-13 Branches: 0 128 0.0 %

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