/* * v_key -- * Return the next event. */ static gcret_t v_key( SCR *sp, int command_events, EVENT *evp, u_int32_t ec_flags) { u_int32_t quote; for (quote = 0;;) { if (v_event_get(sp, evp, 0, ec_flags | quote)) return (GC_FATAL); quote = 0; switch (evp->e_event) { case E_CHARACTER: /* * !!! * Historically, ^V was ignored in the command stream, * although it had a useful side-effect of interrupting * mappings. Adding a quoting bit to the call probably * extends historic practice, but it feels right. */ if (evp->e_value == K_VLNEXT) { quote = EC_QUOTED; break; } return (GC_OK); case E_ERR: case E_EOF: return (GC_FATAL); case E_INTERRUPT: /* * !!! * Historically, vi beeped on command level interrupts. * * Historically, vi exited to ex mode if no file was * named on the command line, and two interrupts were * generated in a row. (Just figured you might want * to know that.) */ (void)sp->gp->scr_bell(sp); return (GC_INTERRUPT); case E_REPAINT: if (vs_repaint(sp, evp)) return (GC_FATAL); break; case E_WRESIZE: return (GC_ERR); /* FALLTHROUGH */ default: v_event_err(sp, evp); return (GC_ERR); } } /* NOTREACHED */ }
/* * v_replace -- [count]r<char> * * !!! * The r command in historic vi was almost beautiful in its badness. For * example, "r<erase>" and "r<word erase>" beeped the terminal and deleted * a single character. "Nr<carriage return>", where N was greater than 1, * inserted a single carriage return. "r<escape>" did cancel the command, * but "r<literal><escape>" erased a single character. To enter a literal * <literal> character, it required three <literal> characters after the * command. This may not be right, but at least it's not insane. * * PUBLIC: int v_replace __P((SCR *, VICMD *)); */ int v_replace(SCR *sp, VICMD *vp) { EVENT ev; VI_PRIVATE *vip; TEXT *tp; size_t blen, len; u_long cnt; int quote, rval; CHAR_T *bp; CHAR_T *p; vip = VIP(sp); /* * If the line doesn't exist, or it's empty, replacement isn't * allowed. It's not hard to implement, but: * * 1: It's historic practice (vi beeped before the replacement * character was even entered). * 2: For consistency, this change would require that the more * general case, "Nr", when the user is < N characters from * the end of the line, also work, which would be a bit odd. * 3: Replacing with a <newline> has somewhat odd semantics. */ if (db_get(sp, vp->m_start.lno, DBG_FATAL, &p, &len)) return (1); if (len == 0) { msgq(sp, M_BERR, "186|No characters to replace"); return (1); } /* * Figure out how many characters to be replace. For no particular * reason (other than that the semantics of replacing the newline * are confusing) only permit the replacement of the characters in * the current line. I suppose we could append replacement characters * to the line, but I see no compelling reason to do so. Check this * before we get the character to match historic practice, where Nr * failed immediately if there were less than N characters from the * cursor to the end of the line. */ cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; vp->m_stop.lno = vp->m_start.lno; vp->m_stop.cno = vp->m_start.cno + cnt - 1; if (vp->m_stop.cno > len - 1) { v_eol(sp, &vp->m_start); return (1); } /* * If it's not a repeat, reset the current mode and get a replacement * character. */ quote = 0; if (!F_ISSET(vp, VC_ISDOT)) { sp->showmode = SM_REPLACE; if (vs_refresh(sp, 0)) return (1); next: if (v_event_get(sp, &ev, 0, 0)) return (1); switch (ev.e_event) { case E_CHARACTER: /* * <literal_next> means escape the next character. * <escape> means they changed their minds. */ if (!quote) { if (ev.e_value == K_VLNEXT) { quote = 1; goto next; } if (ev.e_value == K_ESCAPE) return (0); } vip->rlast = ev.e_c; vip->rvalue = ev.e_value; break; case E_ERR: case E_EOF: F_SET(sp, SC_EXIT_FORCE); return (1); case E_INTERRUPT: /* <interrupt> means they changed their minds. */ return (0); case E_WRESIZE: /* <resize> interrupts the input mode. */ v_emsg(sp, NULL, VIM_WRESIZE); return (0); case E_REPAINT: if (vs_repaint(sp, &ev)) return (1); goto next; default: v_event_err(sp, &ev); return (0); } } /* Copy the line. */ GET_SPACE_RETW(sp, bp, blen, len); MEMMOVE(bp, p, len); p = bp; /* * Versions of nvi before 1.57 created N new lines when they replaced * N characters with <carriage-return> or <newline> characters. This * is different from the historic vi, which replaced N characters with * a single new line. Users complained, so we match historic practice. */ if ((!quote && vip->rvalue == K_CR) || vip->rvalue == K_NL) { /* Set return line. */ vp->m_stop.lno = vp->m_start.lno + 1; vp->m_stop.cno = 0; /* The first part of the current line. */ if (db_set(sp, vp->m_start.lno, p, vp->m_start.cno)) goto err_ret; /* * The rest of the current line. And, of course, now it gets * tricky. If there are characters left in the line and if * the autoindent edit option is set, white space after the * replaced character is discarded, autoindent is applied, and * the cursor moves to the last indent character. */ p += vp->m_start.cno + cnt; len -= vp->m_start.cno + cnt; if (len != 0 && O_ISSET(sp, O_AUTOINDENT)) for (; len && isblank(*p); --len, ++p); if ((tp = text_init(sp, p, len, len)) == NULL) goto err_ret; if (len != 0 && O_ISSET(sp, O_AUTOINDENT)) { if (v_txt_auto(sp, vp->m_start.lno, NULL, 0, tp)) goto err_ret; vp->m_stop.cno = tp->ai ? tp->ai - 1 : 0; } else vp->m_stop.cno = 0; vp->m_stop.cno = tp->ai ? tp->ai - 1 : 0; if (db_append(sp, 1, vp->m_start.lno, tp->lb, tp->len)) err_ret: rval = 1; else { text_free(tp); rval = 0; } } else { STRSET(bp + vp->m_start.cno, vip->rlast, cnt); rval = db_set(sp, vp->m_start.lno, bp, len); } FREE_SPACEW(sp, bp, blen); vp->m_final = vp->m_stop; return (rval); }
/* * vs_resolve -- * Deal with message output. * * PUBLIC: int vs_resolve(SCR *, SCR *, int); */ int vs_resolve(SCR *sp, SCR *csp, int forcewait) { EVENT ev; GS *gp; MSGS *mp; VI_PRIVATE *vip; size_t oldy, oldx; int redraw; /* * Vs_resolve is called from the main vi loop and the refresh function * to periodically ensure that the user has seen any messages that have * been displayed and that any status lines are correct. The sp screen * is the screen we're checking, usually the current screen. When it's * not, csp is the current screen, used for final cursor positioning. */ gp = sp->gp; vip = VIP(sp); if (csp == NULL) csp = sp; /* Save the cursor position. */ (void)gp->scr_cursor(csp, &oldy, &oldx); /* Ring the bell if it's scheduled. */ if (F_ISSET(gp, G_BELLSCHED)) { F_CLR(gp, G_BELLSCHED); (void)gp->scr_bell(sp); } /* Display new file status line. */ if (F_ISSET(sp, SC_STATUS)) { F_CLR(sp, SC_STATUS); msgq_status(sp, sp->lno, MSTAT_TRUNCATE); } /* Report on line modifications. */ mod_rpt(sp); /* * Flush any saved messages. If the screen isn't ready, refresh * it. (A side-effect of screen refresh is that we can display * messages.) Once this is done, don't trust the cursor. That * extra refresh screwed the pooch. */ if (LIST_FIRST(&gp->msgq) != NULL) { if (!F_ISSET(sp, SC_SCR_VI) && vs_refresh(sp, 1)) return (1); while ((mp = LIST_FIRST(&gp->msgq)) != NULL) { gp->scr_msg(sp, mp->mtype, mp->buf, mp->len); LIST_REMOVE(mp, q); free(mp->buf); free(mp); } F_SET(vip, VIP_CUR_INVALID); } switch (vip->totalcount) { case 0: redraw = 0; break; case 1: /* * If we're switching screens, we have to wait for messages, * regardless. If we don't wait, skip updating the modeline. */ if (forcewait) vs_scroll(sp, NULL, SCROLL_W); else F_SET(vip, VIP_S_MODELINE); redraw = 0; break; default: /* * If >1 message line in use, prompt the user to continue and * repaint overwritten lines. */ vs_scroll(sp, NULL, SCROLL_W); ev.e_event = E_REPAINT; ev.e_flno = vip->totalcount >= sp->rows ? 1 : sp->rows - vip->totalcount; ev.e_tlno = sp->rows; redraw = 1; break; } /* Reset the count of overwriting lines. */ vip->linecount = vip->lcontinue = vip->totalcount = 0; /* Redraw. */ if (redraw) (void)vs_repaint(sp, &ev); /* Restore the cursor position. */ (void)gp->scr_move(csp, oldy, oldx); return (0); }
/* * vs_ex_resolve -- * Deal with ex message output. * * This routine is called when exiting a colon command to resolve any ex * output that may have occurred. * * PUBLIC: int vs_ex_resolve(SCR *, int *); */ int vs_ex_resolve(SCR *sp, int *continuep) { EVENT ev; GS *gp; VI_PRIVATE *vip; sw_t wtype; gp = sp->gp; vip = VIP(sp); *continuep = 0; /* If we ran any ex command, we can't trust the cursor position. */ F_SET(vip, VIP_CUR_INVALID); /* Terminate any partially written message. */ if (vip->lcontinue != 0) { vs_output(sp, vip->mtype, ".", 1); vip->lcontinue = 0; vip->mtype = M_NONE; } /* * If we switched out of the vi screen into ex, switch back while we * figure out what to do with the screen and potentially get another * command to execute. * * If we didn't switch into ex, we're not required to wait, and less * than 2 lines of output, we can continue without waiting for the * wait. * * Note, all other code paths require waiting, so we leave the report * of modified lines until later, so that we won't wait for no other * reason than a threshold number of lines were modified. This means * we display cumulative line modification reports for groups of ex * commands. That seems right to me (well, at least not wrong). */ if (F_ISSET(sp, SC_SCR_EXWROTE)) { if (sp->gp->scr_screen(sp, SC_VI)) return (1); } else if (!F_ISSET(sp, SC_EX_WAIT_YES) && vip->totalcount < 2) { F_CLR(sp, SC_EX_WAIT_NO); return (0); } /* Clear the required wait flag, it's no longer needed. */ F_CLR(sp, SC_EX_WAIT_YES); /* * Wait, unless explicitly told not to wait or the user interrupted * the command. If the user is leaving the screen, for any reason, * they can't continue with further ex commands. */ if (!F_ISSET(sp, SC_EX_WAIT_NO) && !INTERRUPTED(sp)) { wtype = F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_FSWITCH | SC_SSWITCH) ? SCROLL_W : SCROLL_W_EX; if (F_ISSET(sp, SC_SCR_EXWROTE)) vs_wait(sp, continuep, wtype); else vs_scroll(sp, continuep, wtype); if (*continuep) return (0); } /* If ex wrote on the screen, refresh the screen image. */ if (F_ISSET(sp, SC_SCR_EXWROTE)) F_SET(vip, VIP_N_EX_PAINT); /* * If we're not the bottom of the split screen stack, the screen * image itself is wrong, so redraw everything. */ if (TAILQ_NEXT(sp, q)) F_SET(sp, SC_SCR_REDRAW); /* If ex changed the underlying file, the map itself is wrong. */ if (F_ISSET(vip, VIP_N_EX_REDRAW)) F_SET(sp, SC_SCR_REFORMAT); /* Ex may have switched out of the alternate screen, return. */ (void)gp->scr_attr(sp, SA_ALTERNATE, 1); /* * Whew. We're finally back home, after what feels like years. * Kiss the ground. */ F_CLR(sp, SC_SCR_EXWROTE | SC_EX_WAIT_NO); /* * We may need to repaint some of the screen, e.g.: * * :set * :!ls * * gives us a combination of some lines that are "wrong", and a need * for a full refresh. */ if (vip->totalcount > 1) { /* Set up the redraw of the overwritten lines. */ ev.e_event = E_REPAINT; ev.e_flno = vip->totalcount >= sp->rows ? 1 : sp->rows - vip->totalcount; ev.e_tlno = sp->rows; /* Reset the count of overwriting lines. */ vip->linecount = vip->lcontinue = vip->totalcount = 0; /* Redraw. */ (void)vs_repaint(sp, &ev); } else /* Reset the count of overwriting lines. */ vip->linecount = vip->lcontinue = vip->totalcount = 0; return (0); }