1 /* $OpenBSD: util.c,v 1.43 2021/03/01 10:51:14 lum Exp $ */ 2 3 /* This file is in the public domain. */ 4 5 /* 6 * Assorted commands. 7 * This file contains the command processors for a large assortment of 8 * unrelated commands. The only thing they have in common is that they 9 * are all command processors. 10 */ 11 12 #include <sys/queue.h> 13 #include <ctype.h> 14 #include <signal.h> 15 #include <stdio.h> 16 17 #include "def.h" 18 19 /* 20 * Display a bunch of useful information about the current location of dot. 21 * The character under the cursor (in octal), the current line, row, and 22 * column, and approximate position of the cursor in the file (as a 23 * percentage) is displayed. 24 * Also included at the moment are some values in parenthesis for debugging 25 * explicit newline inclusion into the buffer. 26 * The column position assumes an infinite 27 * position display; it does not truncate just because the screen does. 28 * This is normally bound to "C-x =". 29 */ 30 /* ARGSUSED */ 31 int 32 showcpos(int f, int n) 33 { 34 struct line *clp; 35 char *msg; 36 long nchar, cchar; 37 int nline, row; 38 int cline, cbyte; /* Current line/char/byte */ 39 int ratio; 40 41 /* collect the data */ 42 clp = bfirstlp(curbp); 43 msg = "Char:"; 44 cchar = 0; 45 cline = 0; 46 cbyte = 0; 47 nchar = 0; 48 nline = 0; 49 for (;;) { 50 /* count lines and display total as (raw) 'lines' and 51 compare with b_lines */ 52 ++nline; 53 if (clp == curwp->w_dotp) { 54 /* obtain (raw) dot line # and compare with w_dotline */ 55 cline = nline; 56 cchar = nchar + curwp->w_doto; 57 if (curwp->w_doto == llength(clp)) 58 /* fake a \n at end of line */ 59 cbyte = *curbp->b_nlchr; 60 else 61 cbyte = lgetc(clp, curwp->w_doto); 62 } 63 /* include # of chars in this line for point-thru-buff ratio */ 64 nchar += llength(clp); 65 clp = lforw(clp); 66 if (clp == curbp->b_headp) { 67 if (cbyte == *curbp->b_nlchr && 68 cline == curbp->b_lines) { 69 /* swap faked \n for EOB msg */ 70 cbyte = EOF; 71 msg = "(EOB)"; 72 } 73 break; 74 } 75 /* count the implied newline */ 76 nchar++; 77 } 78 /* determine row # within current window */ 79 row = curwp->w_toprow + 1; 80 clp = curwp->w_linep; 81 while (clp != curbp->b_headp && clp != curwp->w_dotp) { 82 ++row; 83 clp = lforw(clp); 84 } 85 ratio = nchar ? (100L * cchar) / nchar : 100; 86 ewprintf("%s %c (0%o) point=%ld(%d%%) line=%d row=%d col=%d" \ 87 " (blines=%d rlines=%d l_size=%d)", msg, 88 cbyte, cbyte, cchar, ratio, cline, row, getcolpos(curwp), 89 curbp->b_lines, nline, clp->l_size); 90 return (TRUE); 91 } 92 93 int 94 getcolpos(struct mgwin *wp) 95 { 96 int col, i, c; 97 char tmp[5]; 98 99 /* determine column */ 100 col = 0; 101 102 for (i = 0; i < wp->w_doto; ++i) { 103 c = lgetc(wp->w_dotp, i); 104 if (c == '\t' 105 #ifdef NOTAB 106 && !(wp->w_bufp->b_flag & BFNOTAB) 107 #endif /* NOTAB */ 108 ) { 109 col |= 0x07; 110 col++; 111 } else if (ISCTRL(c) != FALSE) 112 col += 2; 113 else if (isprint(c)) { 114 col++; 115 } else { 116 col += snprintf(tmp, sizeof(tmp), "\\%o", c); 117 } 118 119 } 120 return (col); 121 } 122 123 /* 124 * Twiddle the two characters in front of and under dot, then move forward 125 * one character. Treat new-line characters the same as any other. 126 * Normally bound to "C-t". This always works within a line, so "WFEDIT" 127 * is good enough. 128 */ 129 /* ARGSUSED */ 130 int 131 twiddle(int f, int n) 132 { 133 struct line *dotp; 134 int doto, cr; 135 136 if (n == 0) 137 return (TRUE); 138 139 dotp = curwp->w_dotp; 140 doto = curwp->w_doto; 141 142 /* Don't twiddle if the dot is on the first char of buffer */ 143 if (doto == 0 && lback(dotp) == curbp->b_headp) { 144 dobeep(); 145 ewprintf("Beginning of buffer"); 146 return(FALSE); 147 } 148 /* Don't twiddle if the dot is on the last char of buffer */ 149 if (doto == llength(dotp) && lforw(dotp) == curbp->b_headp) { 150 dobeep(); 151 return(FALSE); 152 } 153 undo_boundary_enable(FFRAND, 0); 154 if (doto == 0 && doto == llength(dotp)) { /* only '\n' on this line */ 155 (void)forwline(FFRAND, 1); 156 curwp->w_doto = 0; 157 } else { 158 if (doto == 0) { /* 1st twiddle is on 1st character of a line */ 159 cr = lgetc(dotp, doto); 160 (void)backdel(FFRAND, 1); 161 (void)forwchar(FFRAND, 1); 162 lnewline(); 163 linsert(1, cr); 164 (void)backdel(FFRAND, 1); 165 } else { /* twiddle is elsewhere in line */ 166 cr = lgetc(dotp, doto - 1); 167 (void)backdel(FFRAND, 1); 168 (void)forwchar(FFRAND, 1); 169 linsert(1, cr); 170 } 171 } 172 undo_boundary_enable(FFRAND, 1); 173 lchange(WFEDIT); 174 return (TRUE); 175 } 176 177 /* 178 * Open up some blank space. The basic plan is to insert a bunch of 179 * newlines, and then back up over them. Everything is done by the 180 * subcommand processors. They even handle the looping. Normally this 181 * is bound to "C-o". 182 */ 183 /* ARGSUSED */ 184 int 185 openline(int f, int n) 186 { 187 int i, s; 188 189 if (n < 0) 190 return (FALSE); 191 if (n == 0) 192 return (TRUE); 193 194 /* insert newlines */ 195 undo_boundary_enable(FFRAND, 0); 196 i = n; 197 do { 198 s = lnewline(); 199 } while (s == TRUE && --i); 200 201 /* then go back up overtop of them all */ 202 if (s == TRUE) 203 s = backchar(f | FFRAND, n); 204 undo_boundary_enable(FFRAND, 1); 205 return (s); 206 } 207 208 /* 209 * Insert a newline. 210 */ 211 /* ARGSUSED */ 212 int 213 enewline(int f, int n) 214 { 215 int s; 216 217 if (n < 0) 218 return (FALSE); 219 220 while (n--) { 221 if ((s = lnewline()) != TRUE) 222 return (s); 223 } 224 return (TRUE); 225 } 226 227 /* 228 * Delete blank lines around dot. What this command does depends if dot is 229 * sitting on a blank line. If dot is sitting on a blank line, this command 230 * deletes all the blank lines above and below the current line. If it is 231 * sitting on a non blank line then it deletes all of the blank lines after 232 * the line. Normally this command is bound to "C-x C-o". Any argument is 233 * ignored. 234 */ 235 /* ARGSUSED */ 236 int 237 deblank(int f, int n) 238 { 239 struct line *lp1, *lp2; 240 RSIZE nld; 241 242 lp1 = curwp->w_dotp; 243 while (llength(lp1) == 0 && (lp2 = lback(lp1)) != curbp->b_headp) 244 lp1 = lp2; 245 lp2 = lp1; 246 nld = (RSIZE)0; 247 while ((lp2 = lforw(lp2)) != curbp->b_headp && llength(lp2) == 0) 248 ++nld; 249 if (nld == 0) 250 return (TRUE); 251 curwp->w_dotp = lforw(lp1); 252 curwp->w_doto = 0; 253 return (ldelete((RSIZE)nld, KNONE)); 254 } 255 256 /* 257 * Delete any whitespace around dot, then insert a space. 258 */ 259 int 260 justone(int f, int n) 261 { 262 undo_boundary_enable(FFRAND, 0); 263 (void)delwhite(f, n); 264 linsert(1, ' '); 265 undo_boundary_enable(FFRAND, 1); 266 return (TRUE); 267 } 268 269 /* 270 * Delete any whitespace around dot. 271 */ 272 /* ARGSUSED */ 273 int 274 delwhite(int f, int n) 275 { 276 int col, s; 277 278 col = curwp->w_doto; 279 280 while (col < llength(curwp->w_dotp) && 281 (isspace(lgetc(curwp->w_dotp, col)))) 282 ++col; 283 do { 284 if (curwp->w_doto == 0) { 285 s = FALSE; 286 break; 287 } 288 if ((s = backchar(FFRAND, 1)) != TRUE) 289 break; 290 } while (isspace(lgetc(curwp->w_dotp, curwp->w_doto))); 291 292 if (s == TRUE) 293 (void)forwchar(FFRAND, 1); 294 (void)ldelete((RSIZE)(col - curwp->w_doto), KNONE); 295 return (TRUE); 296 } 297 298 /* 299 * Delete any leading whitespace on the current line 300 */ 301 int 302 delleadwhite(int f, int n) 303 { 304 int soff, ls; 305 struct line *slp; 306 307 /* Save current position */ 308 slp = curwp->w_dotp; 309 soff = curwp->w_doto; 310 311 for (ls = 0; ls < llength(slp); ls++) 312 if (!isspace(lgetc(slp, ls))) 313 break; 314 gotobol(FFRAND, 1); 315 forwdel(FFRAND, ls); 316 soff -= ls; 317 if (soff < 0) 318 soff = 0; 319 forwchar(FFRAND, soff); 320 321 return (TRUE); 322 } 323 324 /* 325 * Delete any trailing whitespace on the current line 326 */ 327 int 328 deltrailwhite(int f, int n) 329 { 330 int soff; 331 332 /* Save current position */ 333 soff = curwp->w_doto; 334 335 gotoeol(FFRAND, 1); 336 delwhite(FFRAND, 1); 337 338 /* restore original position, if possible */ 339 if (soff < curwp->w_doto) 340 curwp->w_doto = soff; 341 342 return (TRUE); 343 } 344 345 346 347 /* 348 * Insert a newline, then enough tabs and spaces to duplicate the indentation 349 * of the previous line. Assumes tabs are every eight characters. Quite 350 * simple. Figure out the indentation of the current line. Insert a newline 351 * by calling the standard routine. Insert the indentation by inserting the 352 * right number of tabs and spaces. Return TRUE if all ok. Return FALSE if 353 * one of the subcommands failed. Normally bound to "C-m". 354 */ 355 /* ARGSUSED */ 356 int 357 lfindent(int f, int n) 358 { 359 int c, i, nicol; 360 int s = TRUE; 361 362 if (n < 0) 363 return (FALSE); 364 365 undo_boundary_enable(FFRAND, 0); 366 while (n--) { 367 nicol = 0; 368 for (i = 0; i < llength(curwp->w_dotp); ++i) { 369 c = lgetc(curwp->w_dotp, i); 370 if (c != ' ' && c != '\t') 371 break; 372 if (c == '\t') 373 nicol |= 0x07; 374 ++nicol; 375 } 376 if (lnewline() == FALSE || (( 377 #ifdef NOTAB 378 curbp->b_flag & BFNOTAB) ? linsert(nicol, ' ') == FALSE : ( 379 #endif /* NOTAB */ 380 ((i = nicol / 8) != 0 && linsert(i, '\t') == FALSE) || 381 ((i = nicol % 8) != 0 && linsert(i, ' ') == FALSE)))) { 382 s = FALSE; 383 break; 384 } 385 } 386 undo_boundary_enable(FFRAND, 1); 387 return (s); 388 } 389 390 /* 391 * Indent the current line. Delete existing leading whitespace, 392 * and use tabs/spaces to achieve correct indentation. Try 393 * to leave dot where it started. 394 */ 395 int 396 indent(int f, int n) 397 { 398 int soff, i; 399 400 if (n < 0) 401 return (FALSE); 402 403 delleadwhite(FFRAND, 1); 404 405 /* If not invoked with a numerical argument, done */ 406 if (!(f & FFARG)) 407 return (TRUE); 408 409 /* insert appropriate whitespace */ 410 soff = curwp->w_doto; 411 (void)gotobol(FFRAND, 1); 412 if ( 413 #ifdef NOTAB 414 (curbp->b_flag & BFNOTAB) ? linsert(n, ' ') == FALSE : 415 #endif /* NOTAB */ 416 (((i = n / 8) != 0 && linsert(i, '\t') == FALSE) || 417 ((i = n % 8) != 0 && linsert(i, ' ') == FALSE))) 418 return (FALSE); 419 420 forwchar(FFRAND, soff); 421 422 return (TRUE); 423 } 424 425 426 /* 427 * Delete forward. This is real easy, because the basic delete routine does 428 * all of the work. Watches for negative arguments, and does the right thing. 429 * If any argument is present, it kills rather than deletes, to prevent loss 430 * of text if typed with a big argument. Normally bound to "C-d". 431 */ 432 /* ARGSUSED */ 433 int 434 forwdel(int f, int n) 435 { 436 if (n < 0) 437 return (backdel(f | FFRAND, -n)); 438 439 /* really a kill */ 440 if (f & FFARG) { 441 if ((lastflag & CFKILL) == 0) 442 kdelete(); 443 thisflag |= CFKILL; 444 } 445 446 return (ldelete((RSIZE) n, (f & FFARG) ? KFORW : KNONE)); 447 } 448 449 /* 450 * Delete backwards. This is quite easy too, because it's all done with 451 * other functions. Just move the cursor back, and delete forwards. Like 452 * delete forward, this actually does a kill if presented with an argument. 453 */ 454 /* ARGSUSED */ 455 int 456 backdel(int f, int n) 457 { 458 int s; 459 460 if (n < 0) 461 return (forwdel(f | FFRAND, -n)); 462 463 /* really a kill */ 464 if (f & FFARG) { 465 if ((lastflag & CFKILL) == 0) 466 kdelete(); 467 thisflag |= CFKILL; 468 } 469 if ((s = backchar(f | FFRAND, n)) == TRUE) 470 s = ldelete((RSIZE)n, (f & FFARG) ? KFORW : KNONE); 471 472 return (s); 473 } 474 475 #ifdef NOTAB 476 /* ARGSUSED */ 477 int 478 space_to_tabstop(int f, int n) 479 { 480 if (n < 0) 481 return (FALSE); 482 if (n == 0) 483 return (TRUE); 484 return (linsert((n << 3) - (curwp->w_doto & 7), ' ')); 485 } 486 #endif /* NOTAB */ 487 488 /* 489 * Move the dot to the first non-whitespace character of the current line. 490 */ 491 int 492 backtoindent(int f, int n) 493 { 494 gotobol(FFRAND, 1); 495 while (curwp->w_doto < llength(curwp->w_dotp) && 496 (isspace(lgetc(curwp->w_dotp, curwp->w_doto)))) 497 ++curwp->w_doto; 498 return (TRUE); 499 } 500 501 /* 502 * Join the current line to the previous, or with arg, the next line 503 * to the current one. If the former line is not empty, leave exactly 504 * one space at the joint. Otherwise, leave no whitespace. 505 */ 506 int 507 joinline(int f, int n) 508 { 509 int doto; 510 511 undo_boundary_enable(FFRAND, 0); 512 if (f & FFARG) { 513 gotoeol(FFRAND, 1); 514 forwdel(FFRAND, 1); 515 } else { 516 gotobol(FFRAND, 1); 517 backdel(FFRAND, 1); 518 } 519 520 delwhite(FFRAND, 1); 521 522 if ((doto = curwp->w_doto) > 0) { 523 linsert(1, ' '); 524 curwp->w_doto = doto; 525 } 526 undo_boundary_enable(FFRAND, 1); 527 528 return (TRUE); 529 } 530