Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / vi / vs_relative.c
blob4917516872f32585270d0a366bbe9002ca593a59
1 /* $NetBSD: vs_relative.c,v 1.1.1.2 2008/05/18 14:31:41 aymeric Exp $ */
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.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
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";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/time.h>
22 #include <bitstring.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <string.h>
27 #include "../common/common.h"
28 #include "vi.h"
31 * vs_column --
32 * Return the logical column of the cursor in the line.
34 * PUBLIC: int vs_column __P((SCR *, size_t *));
36 int
37 vs_column(SCR *sp, size_t *colp)
39 VI_PRIVATE *vip;
41 vip = VIP(sp);
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);
46 return (0);
50 * vs_screens --
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 *));
57 size_t
58 vs_screens(SCR *sp, db_recno_t lno, size_t *cnop)
60 size_t cols, screens;
62 /* Left-right screens are simple, it's always 1. */
63 if (O_ISSET(sp, O_LEFTRIGHT))
64 return (1);
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
70 * one.
72 if (cnop == NULL) {
73 if (VIP(sp)->ss_lno == lno)
74 return (VIP(sp)->ss_screens);
75 } else if (*cnop == 0)
76 return (1);
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));
82 if (screens == 0)
83 screens = 1;
85 /* Cache the value. */
86 if (cnop == NULL) {
87 VIP(sp)->ss_lno = lno;
88 VIP(sp)->ss_screens = screens;
90 return (screens);
94 * vs_columns --
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 *));
100 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;
105 CHAR_T *p;
108 * Initialize the screen offset.
110 scno = 0;
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. */
117 if (lp == NULL) {
118 (void)db_get(sp, lno, 0, &lp, &len);
119 if (len == 0)
120 goto done;
123 /* Missing or empty lines are easy. */
124 if (lp == NULL) {
125 done: if (diffp != NULL) /* XXX */
126 *diffp = 0;
127 return scno;
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.
137 p = lp;
138 curoff = 0;
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 { \
152 curoff += chlen; \
153 if (!leftright && curoff >= sp->cols) { \
154 if (ch == '\t') { \
155 curoff = 0; \
156 scno -= scno % sp->cols; \
157 } else \
158 curoff -= sp->cols; \
161 if (cnop == NULL)
162 while (len--) {
163 chlen = CHLEN(curoff);
164 last = scno;
165 scno += chlen;
166 TAB_RESET;
168 else
169 for (cno = *cnop;; --cno) {
170 chlen = CHLEN(curoff);
171 last = scno;
172 scno += chlen;
173 TAB_RESET;
174 if (cno == 0)
175 break;
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.
187 if (diffp != NULL)
188 *diffp = scno - last;
189 return (scno);
193 * vs_rcm --
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));
200 size_t
201 vs_rcm(SCR *sp, db_recno_t lno, int islast)
203 size_t len;
205 /* Last character is easy, and common. */
206 if (islast) {
207 if (db_get(sp, lno, 0, NULL, &len) || len == 0)
208 return (0);
209 return (len - 1);
212 /* First character is easy, and common. */
213 if (sp->rcm == 0)
214 return (0);
216 return (vs_colpos(sp, lno, sp->rcm));
220 * vs_colpos --
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));
226 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;
231 CHAR_T *lp, *p;
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)
238 return (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;
246 cno %= sp->cols;
247 for (scno = 0, p = lp, len = llen; off--;) {
248 for (; len && scno < sp->cols; --len)
249 scno += CHLEN(scno);
252 * If reached the end of the physical line, return the last
253 * physical character in the line.
255 if (len == 0)
256 return (llen - 1);
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')
265 scno = 0;
266 else
267 scno -= sp->cols;
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
276 * cases.
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) {
287 off = p - lp;
288 return (scno == cno ?
289 (off < llen - 1 ? off : llen - 1) : off - 1);
292 TAB_RESET;
295 /* No such character; return the start of the last character. */
296 return (llen - 1);