1 /* $NetBSD: v_section.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */ 2 /*- 3 * Copyright (c) 1992, 1993, 1994 4 * The Regents of the University of California. All rights reserved. 5 * Copyright (c) 1992, 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 #include <sys/cdefs.h> 14 #if 0 15 #ifndef lint 16 static const char sccsid[] = "Id: v_section.c,v 10.10 2001/06/25 15:19:35 skimo Exp (Berkeley) Date: 2001/06/25 15:19:35 "; 17 #endif /* not lint */ 18 #else 19 __RCSID("$NetBSD: v_section.c,v 1.3 2014/01/26 21:43:45 christos Exp $"); 20 #endif 21 22 #include <sys/types.h> 23 #include <sys/queue.h> 24 #include <sys/time.h> 25 26 #include <bitstring.h> 27 #include <limits.h> 28 #include <stdio.h> 29 #include <string.h> 30 31 #include "../common/common.h" 32 #include "vi.h" 33 34 /* 35 * !!! 36 * In historic vi, the section commands ignored empty lines, unlike the 37 * paragraph commands, which was probably okay. However, they also moved 38 * to the start of the last line when there where no more sections instead 39 * of the end of the last line like the paragraph commands. I've changed 40 * the latter behavior to match the paragraph commands. 41 * 42 * In historic vi, a section was defined as the first character(s) of the 43 * line matching, which could be followed by anything. This implementation 44 * follows that historic practice. 45 * 46 * !!! 47 * The historic vi documentation (USD:15-10) claimed: 48 * The section commands interpret a preceding count as a different 49 * window size in which to redraw the screen at the new location, 50 * and this window size is the base size for newly drawn windows 51 * until another size is specified. This is very useful if you are 52 * on a slow terminal ... 53 * 54 * I can't get the 4BSD vi to do this, it just beeps at me. For now, a 55 * count to the section commands simply repeats the command. 56 */ 57 58 /* 59 * v_sectionf -- [count]]] 60 * Move forward count sections/functions. 61 * 62 * !!! 63 * Using ]] as a motion command was a bit special, historically. It could 64 * match } as well as the usual { and section values. If it matched a { or 65 * a section, it did NOT include the matched line. If it matched a }, it 66 * did include the line. No clue why. 67 * 68 * PUBLIC: int v_sectionf __P((SCR *, VICMD *)); 69 */ 70 int 71 v_sectionf(SCR *sp, VICMD *vp) 72 { 73 db_recno_t cnt, lno; 74 size_t len; 75 CHAR_T *p; 76 const char *list, *lp; 77 78 /* Get the macro list. */ 79 if ((list = O_STR(sp, O_SECTIONS)) == NULL) 80 return (1); 81 82 /* 83 * !!! 84 * If the starting cursor position is at or before any non-blank 85 * characters in the line, i.e. the movement is cutting all of the 86 * line's text, the buffer is in line mode. It's a lot easier to 87 * check here, because we know that the end is going to be the start 88 * or end of a line. 89 */ 90 if (ISMOTION(vp)) { 91 if (vp->m_start.cno == 0) 92 F_SET(vp, VM_LMODE); 93 else { 94 vp->m_stop = vp->m_start; 95 vp->m_stop.cno = 0; 96 if (nonblank(sp, vp->m_stop.lno, &vp->m_stop.cno)) 97 return (1); 98 if (vp->m_start.cno <= vp->m_stop.cno) 99 F_SET(vp, VM_LMODE); 100 } 101 } 102 103 cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; 104 for (lno = vp->m_start.lno; !db_get(sp, ++lno, 0, &p, &len);) { 105 if (len == 0) 106 continue; 107 if (p[0] == '{' || (ISMOTION(vp) && p[0] == '}')) { 108 if (!--cnt) { 109 if (p[0] == '{') 110 goto adjust1; 111 goto adjust2; 112 } 113 continue; 114 } 115 /* 116 * !!! 117 * Historic documentation (USD:15-11, 4.2) said that formfeed 118 * characters (^L) in the first column delimited sections. 119 * The historic code mentions formfeed characters, but never 120 * implements them. Seems reasonable, do it. 121 */ 122 if (p[0] == '\014') { 123 if (!--cnt) 124 goto adjust1; 125 continue; 126 } 127 if (p[0] != '.' || len < 2) 128 continue; 129 for (lp = list; *lp != '\0'; lp += 2 * sizeof(*lp)) 130 if (lp[0] == p[1] && 131 ((lp[1] == ' ' && len == 2) || lp[1] == p[2]) && 132 !--cnt) { 133 /* 134 * !!! 135 * If not cutting this line, adjust to the end 136 * of the previous one. Otherwise, position to 137 * column 0. 138 */ 139 adjust1: if (ISMOTION(vp)) 140 goto ret1; 141 142 adjust2: vp->m_stop.lno = lno; 143 vp->m_stop.cno = 0; 144 goto ret2; 145 } 146 } 147 148 /* If moving forward, reached EOF, check to see if we started there. */ 149 if (vp->m_start.lno == lno - 1) { 150 v_eof(sp, NULL); 151 return (1); 152 } 153 154 ret1: if (db_get(sp, --lno, DBG_FATAL, NULL, &len)) 155 return (1); 156 vp->m_stop.lno = lno; 157 vp->m_stop.cno = len ? len - 1 : 0; 158 159 /* 160 * Non-motion commands go to the end of the range. Delete and 161 * yank stay at the start of the range. Ignore others. 162 */ 163 ret2: if (ISMOTION(vp)) { 164 vp->m_final = vp->m_start; 165 if (F_ISSET(vp, VM_LMODE)) 166 vp->m_final.cno = 0; 167 } else 168 vp->m_final = vp->m_stop; 169 return (0); 170 } 171 172 /* 173 * v_sectionb -- [count][[ 174 * Move backward count sections/functions. 175 * 176 * PUBLIC: int v_sectionb __P((SCR *, VICMD *)); 177 */ 178 int 179 v_sectionb(SCR *sp, VICMD *vp) 180 { 181 size_t len; 182 db_recno_t cnt, lno; 183 CHAR_T *p; 184 const char *list, *lp; 185 186 /* An empty file or starting from line 1 is always illegal. */ 187 if (vp->m_start.lno <= 1) { 188 v_sof(sp, NULL); 189 return (1); 190 } 191 192 /* Get the macro list. */ 193 if ((list = O_STR(sp, O_SECTIONS)) == NULL) 194 return (1); 195 196 cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; 197 for (lno = vp->m_start.lno; !db_get(sp, --lno, 0, &p, &len);) { 198 if (len == 0) 199 continue; 200 if (p[0] == '{') { 201 if (!--cnt) 202 goto adjust1; 203 continue; 204 } 205 /* 206 * !!! 207 * Historic documentation (USD:15-11, 4.2) said that formfeed 208 * characters (^L) in the first column delimited sections. 209 * The historic code mentions formfeed characters, but never 210 * implements them. Seems reasonable, do it. 211 */ 212 if (p[0] == '\014') { 213 if (!--cnt) 214 goto adjust1; 215 continue; 216 } 217 if (p[0] != '.' || len < 2) 218 continue; 219 for (lp = list; *lp != '\0'; lp += 2 * sizeof(*lp)) 220 if (lp[0] == p[1] && 221 ((lp[1] == ' ' && len == 2) || lp[1] == p[2]) && 222 !--cnt) { 223 adjust1: vp->m_stop.lno = lno; 224 vp->m_stop.cno = 0; 225 goto ret1; 226 } 227 } 228 229 /* 230 * If moving backward, reached SOF, which is a movement sink. 231 * We already checked for starting there. 232 */ 233 vp->m_stop.lno = 1; 234 vp->m_stop.cno = 0; 235 236 /* 237 * All commands move to the end of the range. 238 * 239 * !!! 240 * Historic practice is the section cut was in line mode if it started 241 * from column 0 and was in the backward direction. Otherwise, left 242 * motion commands adjust the starting point to the character before 243 * the current one. What makes this worse is that if it cut to line 244 * mode it also went to the first non-<blank>. 245 */ 246 ret1: if (vp->m_start.cno == 0) { 247 F_CLR(vp, VM_RCM_MASK); 248 F_SET(vp, VM_RCM_SETFNB); 249 250 --vp->m_start.lno; 251 F_SET(vp, VM_LMODE); 252 } else 253 --vp->m_start.cno; 254 255 vp->m_final = vp->m_stop; 256 return (0); 257 } 258