1 /* $OpenBSD: paragraph.c,v 1.33 2014/10/13 21:01:05 lum Exp $ */ 2 3 /* This file is in the public domain. */ 4 5 /* 6 * Code for dealing with paragraphs and filling. Adapted from MicroEMACS 3.6 7 * and GNU-ified by mwm@ucbvax. Several bug fixes by blarson@usc-oberon. 8 */ 9 10 #include <ctype.h> 11 12 #include "def.h" 13 14 static int fillcol = 70; 15 16 #define MAXWORD 256 17 18 /* 19 * Move to start of paragraph. 20 * Move backwards by line, checking from the 1st character forwards for the 21 * existence a non-space. If a non-space character is found, move to the 22 * preceding line. Keep doing this until a line with only spaces is found or 23 * the start of buffer. 24 */ 25 /* ARGSUSED */ 26 int 27 gotobop(int f, int n) 28 { 29 int col, nospace; 30 31 /* the other way... */ 32 if (n < 0) 33 return (gotoeop(f, -n)); 34 35 while (n-- > 0) { 36 nospace = 0; 37 while (lback(curwp->w_dotp) != curbp->b_headp) { 38 curwp->w_doto = 0; 39 col = 0; 40 41 while (col < llength(curwp->w_dotp) && 42 (isspace(lgetc(curwp->w_dotp, col)))) 43 col++; 44 45 if (col >= llength(curwp->w_dotp)) { 46 if (nospace) 47 break; 48 } else 49 nospace = 1; 50 51 curwp->w_dotline--; 52 curwp->w_dotp = lback(curwp->w_dotp); 53 } 54 } 55 /* force screen update */ 56 curwp->w_rflag |= WFMOVE; 57 return (TRUE); 58 } 59 60 /* 61 * Move to end of paragraph. 62 * See comments for gotobop(). Same, but moving forwards. 63 */ 64 /* ARGSUSED */ 65 int 66 gotoeop(int f, int n) 67 { 68 int col, nospace; 69 70 /* the other way... */ 71 if (n < 0) 72 return (gotobop(f, -n)); 73 74 /* for each one asked for */ 75 while (n-- > 0) { 76 nospace = 0; 77 while (lforw(curwp->w_dotp) != curbp->b_headp) { 78 col = 0; 79 curwp->w_doto = 0; 80 81 while (col < llength(curwp->w_dotp) && 82 (isspace(lgetc(curwp->w_dotp, col)))) 83 col++; 84 85 if (col >= llength(curwp->w_dotp)) { 86 if (nospace) 87 break; 88 } else 89 nospace = 1; 90 91 curwp->w_dotp = lforw(curwp->w_dotp); 92 curwp->w_dotline++; 93 } 94 } 95 /* covers corner case of no '\n' at end of buffer */ 96 if (lforw(curwp->w_dotp) == curbp->b_headp) 97 gotoeol(FFRAND, 1); 98 99 /* force screen update */ 100 curwp->w_rflag |= WFMOVE; 101 return (TRUE); 102 } 103 104 /* 105 * Justify a paragraph. Fill the current paragraph according to the current 106 * fill column. 107 */ 108 /* ARGSUSED */ 109 int 110 fillpara(int f, int n) 111 { 112 int c; /* current char during scan */ 113 int wordlen; /* length of current word */ 114 int clength; /* position on line during fill */ 115 int i; /* index during word copy */ 116 int eopflag; /* Are we at the End-Of-Paragraph? */ 117 int firstflag; /* first word? (needs no space) */ 118 int newlength; /* tentative new line length */ 119 int eolflag; /* was at end of line */ 120 int retval; /* return value */ 121 struct line *eopline; /* pointer to line just past EOP */ 122 char wbuf[MAXWORD]; /* buffer for current word */ 123 124 undo_boundary_enable(FFRAND, 0); 125 126 /* record the pointer to the line just past the EOP */ 127 (void)gotoeop(FFRAND, 1); 128 if (curwp->w_doto != 0) { 129 /* paragraph ends at end of buffer */ 130 (void)lnewline(); 131 eopline = lforw(curwp->w_dotp); 132 } else 133 eopline = curwp->w_dotp; 134 135 /* and back top the begining of the paragraph */ 136 (void)gotobop(FFRAND, 1); 137 138 /* initialize various info */ 139 while (inword() == 0 && forwchar(FFRAND, 1)); 140 141 clength = curwp->w_doto; 142 wordlen = 0; 143 144 /* scan through lines, filling words */ 145 firstflag = TRUE; 146 eopflag = FALSE; 147 while (!eopflag) { 148 149 /* get the next character in the paragraph */ 150 if ((eolflag = (curwp->w_doto == llength(curwp->w_dotp)))) { 151 c = ' '; 152 if (lforw(curwp->w_dotp) == eopline) 153 eopflag = TRUE; 154 } else 155 c = lgetc(curwp->w_dotp, curwp->w_doto); 156 157 /* and then delete it */ 158 if (ldelete((RSIZE) 1, KNONE) == FALSE && !eopflag) { 159 retval = FALSE; 160 goto cleanup; 161 } 162 163 /* if not a separator, just add it in */ 164 if (c != ' ' && c != '\t') { 165 if (wordlen < MAXWORD - 1) 166 wbuf[wordlen++] = c; 167 else { 168 /* 169 * You lose chars beyond MAXWORD if the word 170 * is too long. I'm too lazy to fix it now; it 171 * just silently truncated the word before, 172 * so I get to feel smug. 173 */ 174 ewprintf("Word too long!"); 175 } 176 } else if (wordlen) { 177 178 /* calculate tentative new length with word added */ 179 newlength = clength + 1 + wordlen; 180 181 /* 182 * if at end of line or at doublespace and previous 183 * character was one of '.','?','!' doublespace here. 184 * behave the same way if a ')' is preceded by a 185 * [.?!] and followed by a doublespace. 186 */ 187 if ((eolflag || 188 curwp->w_doto == llength(curwp->w_dotp) || 189 (c = lgetc(curwp->w_dotp, curwp->w_doto)) == ' ' 190 || c == '\t') && (ISEOSP(wbuf[wordlen - 1]) || 191 (wbuf[wordlen - 1] == ')' && wordlen >= 2 && 192 ISEOSP(wbuf[wordlen - 2]))) && 193 wordlen < MAXWORD - 1) 194 wbuf[wordlen++] = ' '; 195 196 /* at a word break with a word waiting */ 197 if (newlength <= fillcol) { 198 /* add word to current line */ 199 if (!firstflag) { 200 (void)linsert(1, ' '); 201 ++clength; 202 } 203 firstflag = FALSE; 204 } else { 205 if (curwp->w_doto > 0 && 206 lgetc(curwp->w_dotp, curwp->w_doto - 1) == ' ') { 207 curwp->w_doto -= 1; 208 (void)ldelete((RSIZE) 1, KNONE); 209 } 210 /* start a new line */ 211 (void)lnewline(); 212 clength = 0; 213 } 214 215 /* and add the word in in either case */ 216 for (i = 0; i < wordlen; i++) { 217 (void)linsert(1, wbuf[i]); 218 ++clength; 219 } 220 wordlen = 0; 221 } 222 } 223 /* and add a last newline for the end of our new paragraph */ 224 (void)lnewline(); 225 226 /* 227 * We really should wind up where we started, (which is hard to keep 228 * track of) but I think the end of the last line is better than the 229 * beginning of the blank line. 230 */ 231 (void)backchar(FFRAND, 1); 232 retval = TRUE; 233 cleanup: 234 undo_boundary_enable(FFRAND, 1); 235 return (retval); 236 } 237 238 /* 239 * Delete a paragraph. Delete n paragraphs starting with the current one. 240 */ 241 /* ARGSUSED */ 242 int 243 killpara(int f, int n) 244 { 245 int status; /* returned status of functions */ 246 247 /* for each paragraph to delete */ 248 while (n--) { 249 250 /* mark out the end and beginning of the para to delete */ 251 (void)gotoeop(FFRAND, 1); 252 253 /* set the mark here */ 254 curwp->w_markp = curwp->w_dotp; 255 curwp->w_marko = curwp->w_doto; 256 257 /* go to the beginning of the paragraph */ 258 (void)gotobop(FFRAND, 1); 259 260 /* force us to the beginning of line */ 261 curwp->w_doto = 0; 262 263 /* and delete it */ 264 if ((status = killregion(FFRAND, 1)) != TRUE) 265 return (status); 266 } 267 return (TRUE); 268 } 269 270 /* 271 * Insert char with work wrap. Check to see if we're past fillcol, and if so, 272 * justify this line. As a last step, justify the line. 273 */ 274 /* ARGSUSED */ 275 int 276 fillword(int f, int n) 277 { 278 char c; 279 int col, i, nce; 280 281 for (i = col = 0; col <= fillcol; ++i, ++col) { 282 if (i == curwp->w_doto) 283 return selfinsert(f, n); 284 c = lgetc(curwp->w_dotp, i); 285 if (c == '\t' 286 #ifdef NOTAB 287 && !(curbp->b_flag & BFNOTAB) 288 #endif 289 ) 290 col |= 0x07; 291 else if (ISCTRL(c) != FALSE) 292 ++col; 293 } 294 if (curwp->w_doto != llength(curwp->w_dotp)) { 295 (void)selfinsert(f, n); 296 nce = llength(curwp->w_dotp) - curwp->w_doto; 297 } else 298 nce = 0; 299 curwp->w_doto = i; 300 301 if ((c = lgetc(curwp->w_dotp, curwp->w_doto)) != ' ' && c != '\t') 302 do { 303 (void)backchar(FFRAND, 1); 304 } while ((c = lgetc(curwp->w_dotp, curwp->w_doto)) != ' ' && 305 c != '\t' && curwp->w_doto > 0); 306 307 if (curwp->w_doto == 0) 308 do { 309 (void)forwchar(FFRAND, 1); 310 } while ((c = lgetc(curwp->w_dotp, curwp->w_doto)) != ' ' && 311 c != '\t' && curwp->w_doto < llength(curwp->w_dotp)); 312 313 (void)delwhite(FFRAND, 1); 314 (void)lnewline(); 315 i = llength(curwp->w_dotp) - nce; 316 curwp->w_doto = i > 0 ? i : 0; 317 curwp->w_rflag |= WFMOVE; 318 if (nce == 0 && curwp->w_doto != 0) 319 return (fillword(f, n)); 320 return (TRUE); 321 } 322 323 /* 324 * Set fill column to n for justify. 325 */ 326 int 327 setfillcol(int f, int n) 328 { 329 char buf[32], *rep; 330 const char *es; 331 int nfill; 332 333 if ((f & FFARG) != 0) { 334 fillcol = n; 335 } else { 336 if ((rep = eread("Set fill-column: ", buf, sizeof(buf), 337 EFNEW | EFCR)) == NULL) 338 return (ABORT); 339 else if (rep[0] == '\0') 340 return (FALSE); 341 nfill = strtonum(rep, 0, INT_MAX, &es); 342 if (es != NULL) { 343 dobeep(); 344 ewprintf("Invalid fill column: %s", rep); 345 return (FALSE); 346 } 347 fillcol = nfill; 348 ewprintf("Fill column set to %d", fillcol); 349 } 350 return (TRUE); 351 } 352