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