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