1 /* $OpenBSD: random.c,v 1.31 2012/05/18 02:13:44 lum Exp $ */
3 /* This file is in the public domain. */
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.
17 * Display a bunch of useful information about the current location of dot.
18 * The character under the cursor (in octal), the current line, row, and
19 * column, and approximate position of the cursor in the file (as a
20 * percentage) is displayed. The column position assumes an infinite
21 * position display; it does not truncate just because the screen does.
22 * This is normally bound to "C-X =".
26 showcpos(int f, int n)
31 int cline, cbyte; /* Current line/char/byte */
34 /* collect the data */
35 clp = bfirstlp(curbp);
44 if (clp == curwp->w_dotp) {
47 cchar = nchar + curwp->w_doto;
48 if (curwp->w_doto == llength(clp))
51 cbyte = lgetc(clp, curwp->w_doto);
53 /* now count the chars */
54 nchar += llength(clp);
56 if (clp == curbp->b_headp)
58 /* count the newline */
62 row = curwp->w_toprow + 1;
64 while (clp != curbp->b_headp && clp != curwp->w_dotp) {
69 ratio = nchar ? (100L * cchar) / nchar : 100;
70 ewprintf("Char: %c (0%o) point=%ld(%d%%) line=%d row=%d col=%d",
71 cbyte, cbyte, cchar, ratio, cline, row, getcolpos());
81 /* determine column */
84 for (i = 0; i < curwp->w_doto; ++i) {
85 c = lgetc(curwp->w_dotp, i);
88 && !(curbp->b_flag & BFNOTAB)
93 } else if (ISCTRL(c) != FALSE)
95 else if (isprint(c)) {
98 col += snprintf(tmp, sizeof(tmp), "\\%o", c);
106 * Twiddle the two characters on either side of dot. If dot is at the end
107 * of the line twiddle the two characters before it. Return with an error
108 * if dot is at the beginning of line; it seems to be a bit pointless to
109 * make this work. This fixes up a very common typo with a single stroke.
110 * Normally bound to "C-T". This always works within a line, so "WFEDIT"
115 twiddle(int f, int n)
121 dotp = curwp->w_dotp;
122 doto = curwp->w_doto;
123 if (doto == llength(dotp)) {
126 (void)backchar(FFRAND, 1);
132 undo_boundary_enable(FFRAND, 0);
133 cr = lgetc(dotp, doto - 1);
134 (void)backdel(FFRAND, 1);
135 (void)forwchar(FFRAND, 1);
138 (void)backchar(FFRAND, 1);
139 undo_boundary_enable(FFRAND, 1);
145 * Open up some blank space. The basic plan is to insert a bunch of
146 * newlines, and then back up over them. Everything is done by the
147 * subcommand processors. They even handle the looping. Normally this
152 openline(int f, int n)
161 /* insert newlines */
162 undo_boundary_enable(FFRAND, 0);
166 } while (s == TRUE && --i);
168 /* then go back up overtop of them all */
170 s = backchar(f | FFRAND, n);
171 undo_boundary_enable(FFRAND, 1);
180 newline(int f, int n)
188 if ((s = lnewline()) != TRUE)
195 * Delete blank lines around dot. What this command does depends if dot is
196 * sitting on a blank line. If dot is sitting on a blank line, this command
197 * deletes all the blank lines above and below the current line. If it is
198 * sitting on a non blank line then it deletes all of the blank lines after
199 * the line. Normally this command is bound to "C-X C-O". Any argument is
204 deblank(int f, int n)
206 struct line *lp1, *lp2;
210 while (llength(lp1) == 0 && (lp2 = lback(lp1)) != curbp->b_headp)
214 while ((lp2 = lforw(lp2)) != curbp->b_headp && llength(lp2) == 0)
218 curwp->w_dotp = lforw(lp1);
220 return (ldelete((RSIZE)nld, KNONE));
224 * Delete any whitespace around dot, then insert a space.
227 justone(int f, int n)
229 undo_boundary_enable(FFRAND, 0);
230 (void)delwhite(f, n);
232 undo_boundary_enable(FFRAND, 1);
237 * Delete any whitespace around dot.
241 delwhite(int f, int n)
247 while (col < llength(curwp->w_dotp) &&
248 (isspace(lgetc(curwp->w_dotp, col))))
251 if (curwp->w_doto == 0) {
255 if ((s = backchar(FFRAND, 1)) != TRUE)
257 } while (isspace(lgetc(curwp->w_dotp, curwp->w_doto)));
260 (void)forwchar(FFRAND, 1);
261 (void)ldelete((RSIZE)(col - curwp->w_doto), KNONE);
266 * Delete any leading whitespace on the current line
269 delleadwhite(int f, int n)
274 /* Save current position */
276 soff = curwp->w_doto;
278 for (ls = 0; ls < llength(slp); ls++)
279 if (!isspace(lgetc(slp, ls)))
286 forwchar(FFRAND, soff);
292 * Delete any trailing whitespace on the current line
295 deltrailwhite(int f, int n)
299 /* Save current position */
300 soff = curwp->w_doto;
305 /* restore original position, if possible */
306 if (soff < curwp->w_doto)
307 curwp->w_doto = soff;
315 * Insert a newline, then enough tabs and spaces to duplicate the indentation
316 * of the previous line. Assumes tabs are every eight characters. Quite
317 * simple. Figure out the indentation of the current line. Insert a newline
318 * by calling the standard routine. Insert the indentation by inserting the
319 * right number of tabs and spaces. Return TRUE if all ok. Return FALSE if
320 * one of the subcommands failed. Normally bound to "C-M".
324 lfindent(int f, int n)
332 undo_boundary_enable(FFRAND, 0);
335 for (i = 0; i < llength(curwp->w_dotp); ++i) {
336 c = lgetc(curwp->w_dotp, i);
337 if (c != ' ' && c != '\t')
343 if (lnewline() == FALSE || ((
345 curbp->b_flag & BFNOTAB) ? linsert(nicol, ' ') == FALSE : (
347 ((i = nicol / 8) != 0 && linsert(i, '\t') == FALSE) ||
348 ((i = nicol % 8) != 0 && linsert(i, ' ') == FALSE)))) {
353 undo_boundary_enable(FFRAND, 1);
358 * Indent the current line. Delete existing leading whitespace,
359 * and use tabs/spaces to achieve correct indentation. Try
360 * to leave dot where it started.
370 delleadwhite(FFRAND, 1);
372 /* If not invoked with a numerical argument, done */
376 /* insert appropriate whitespace */
377 soff = curwp->w_doto;
378 (void)gotobol(FFRAND, 1);
381 (curbp->b_flag & BFNOTAB) ? linsert(n, ' ') == FALSE :
383 (((i = n / 8) != 0 && linsert(i, '\t') == FALSE) ||
384 ((i = n % 8) != 0 && linsert(i, ' ') == FALSE)))
387 forwchar(FFRAND, soff);
394 * Delete forward. This is real easy, because the basic delete routine does
395 * all of the work. Watches for negative arguments, and does the right thing.
396 * If any argument is present, it kills rather than deletes, to prevent loss
397 * of text if typed with a big argument. Normally bound to "C-D".
401 forwdel(int f, int n)
404 return (backdel(f | FFRAND, -n));
408 if ((lastflag & CFKILL) == 0)
413 return (ldelete((RSIZE) n, (f & FFARG) ? KFORW : KNONE));
417 * Delete backwards. This is quite easy too, because it's all done with
418 * other functions. Just move the cursor back, and delete forwards. Like
419 * delete forward, this actually does a kill if presented with an argument.
423 backdel(int f, int n)
428 return (forwdel(f | FFRAND, -n));
432 if ((lastflag & CFKILL) == 0)
436 if ((s = backchar(f | FFRAND, n)) == TRUE)
437 s = ldelete((RSIZE)n, (f & FFARG) ? KFORW : KNONE);
445 space_to_tabstop(int f, int n)
451 return (linsert((n << 3) - (curwp->w_doto & 7), ' '));
456 * Move the dot to the first non-whitespace character of the current line.
459 backtoindent(int f, int n)
462 while (curwp->w_doto < llength(curwp->w_dotp) &&
463 (isspace(lgetc(curwp->w_dotp, curwp->w_doto))))
469 * Join the current line to the previous, or with arg, the next line
470 * to the current one. If the former line is not empty, leave exactly
471 * one space at the joint. Otherwise, leave no whitespace.
474 joinline(int f, int n)
478 undo_boundary_enable(FFRAND, 0);
489 if ((doto = curwp->w_doto) > 0) {
491 curwp->w_doto = doto;
493 undo_boundary_enable(FFRAND, 1);