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