void vi() { REG int key; /* keystroke from user */ long count; /* numeric argument to some functions */ REG struct keystru *keyptr;/* pointer to vikeys[] element */ MARK tcurs; /* temporary cursor */ int prevkey;/* previous key, if d/c/y/</>/! */ MARK range; /* start of range for d/c/y/</>/! */ char text[132]; int dotkey; /* last "key" of a change */ int dotpkey;/* last "prevkey" of a change */ int dotkey2;/* last extra "getkey()" of a change */ int dotcnt; /* last "count" of a change */ int firstkey; REG int i; /* tell the redraw() function to start from scratch */ redraw(MARK_UNSET, FALSE); #ifdef lint /* lint says that "range" might be used before it is set. This * can't really happen due to the way "range" and "prevkey" are used, * but lint doesn't know that. This line is here ONLY to keep lint * happy. */ range = 0L; #endif /* safeguard against '.' with no previous command */ dotkey = dotpkey = dotkey2 = dotcnt = 0; /* go immediately into insert mode, if ":set inputmode" */ firstkey = 0; #ifndef NO_EXTENSIONS if (*o_inputmode) { firstkey = 'i'; } #endif /* Repeatedly handle VI commands */ for (count = 0, prevkey = '\0'; mode == MODE_VI; ) { /* if we've moved off the undoable line, then we can't undo it at all */ if (markline(cursor) != U_line) { U_line = 0L; } /* report any changes from the previous command */ if (rptlines >= *o_report) { redraw(cursor, FALSE); msg("%ld line%s %s", rptlines, (rptlines==1?"":"s"), rptlabel); } rptlines = 0L; /* get the next command key. It must be ASCII */ if (firstkey) { key = firstkey; firstkey = 0; } else { do { key = getkey(WHEN_VICMD); } while (key < 0 || key > 127); } #ifdef DEBUG2 debout("\nkey='%c'\n", key); #endif /* Convert a doubled-up operator such as "dd" into "d_" */ if (prevkey && key == prevkey) { key = '_'; } /* look up the structure describing this command */ keyptr = &vikeys[key]; /* '&' and uppercase operators always act like doubled */ if (!prevkey && keyptr->args == CURSOR_MOVED && (key == '&' || isupper(key))) { range = cursor; prevkey = key; key = '_'; keyptr = &vikeys[key]; } #ifndef NO_VISIBLE /* if we're in the middle of a v/V command, reject commands * that aren't operators or movement commands */ if (V_from && !(keyptr->flags & VIZ)) { beep(); prevkey = 0; count = 0; continue; } #endif /* if we're in the middle of a d/c/y/</>/! command, reject * anything but movement. */ if (prevkey && !(keyptr->flags & (MVMT|PTMV))) { beep(); prevkey = 0; count = 0; continue; } /* set the "dot" variables, if we're supposed to */ if (((keyptr->flags & SDOT) || (prevkey && vikeys[prevkey].flags & SDOT)) #ifndef NO_VISIBLE && !V_from #endif ) { dotkey = key; dotpkey = prevkey; dotkey2 = '\0'; dotcnt = count; /* remember the line before any changes are made */ if (U_line != markline(cursor)) { U_line = markline(cursor); strcpy(U_text, fetchline(U_line)); } } /* if this is "." then set other vars from the "dot" vars */ if (key == '.') { key = dotkey; keyptr = &vikeys[key]; prevkey = dotpkey; if (prevkey) { range = cursor; } if (count == 0) { count = dotcnt; } doingdot = TRUE; /* remember the line before any changes are made */ if (U_line != markline(cursor)) { U_line = markline(cursor); strcpy(U_text, fetchline(U_line)); } } else { doingdot = FALSE; } /* process the key as a command */ tcurs = cursor; force_flags = NO_FLAGS; switch (keyptr->args & ARGSMASK) { case ZERO: if (count == 0) { tcurs = cursor & ~(BLKSIZE - 1); break; } /* else fall through & treat like other digits... */ case DIGIT: count = count * 10 + key - '0'; break; case KEYWORD: /* if not on a keyword, fail */ pfetch(markline(cursor)); key = markidx(cursor); if (!isalnum(ptext[key])) { tcurs = MARK_UNSET; break; } /* find the start of the keyword */ while (key > 0 && isalnum(ptext[key - 1])) { key--; } tcurs = (cursor & ~(BLKSIZE - 1)) + key; /* copy it into a buffer, and NUL-terminate it */ i = 0; do { text[i++] = ptext[key++]; } while (isalnum(ptext[key])); text[i] = '\0'; /* call the function */ tcurs = (*keyptr->func)(text, tcurs, count); count = 0L; break; case NO_ARGS: if (keyptr->func) { (*keyptr->func)(); } else { beep(); } count = 0L; break; case CURSOR: tcurs = (*keyptr->func)(cursor, count, key, prevkey); count = 0L; break; case CURSOR_CNT_KEY: if (doingdot) { tcurs = (*keyptr->func)(cursor, count, dotkey2); } else { /* get a key */ i = getkey(KEYMODE(keyptr->args)); if (i == '\033') /* ESC */ { count = 0; tcurs = MARK_UNSET; break; /* exit from "case CURSOR_CNT_KEY" */ } else if (i == ctrl('V')) { i = getkey(0); } /* if part of an SDOT command, remember it */ if (keyptr->flags & SDOT || (prevkey && vikeys[prevkey].flags & SDOT)) { dotkey2 = i; } /* do it */ tcurs = (*keyptr->func)(cursor, count, i); } count = 0L; break; case CURSOR_MOVED: #ifndef NO_VISIBLE if (V_from) { range = cursor; tcurs = V_from; count = 0L; prevkey = key; key = (V_linemd ? 'V' : 'v'); keyptr = &vikeys[key]; } else #endif { prevkey = key; range = cursor; force_flags = LNMD|INCL; } break; case CURSOR_EOL: prevkey = key; /* a zero-length line needs special treatment */ pfetch(markline(cursor)); if (plen == 0) { /* act on a zero-length section of text */ range = tcurs = cursor; key = '0'; } else { /* act like CURSOR_MOVED with '$' movement */ range = cursor; tcurs = m_rear(cursor, 1L); key = '$'; } count = 0L; keyptr = &vikeys[key]; break; case CURSOR_TEXT: do { text[0] = key; text[1] = '\0'; if (doingdot || vgets(key, text + 1, sizeof text - 1) >= 0) { /* reassure user that <CR> was hit */ qaddch('\r'); refresh(); /* call the function with the text */ tcurs = (*keyptr->func)(cursor, text); } else { if (exwrote || mode == MODE_COLON) { redraw(MARK_UNSET, FALSE); } mode = MODE_VI; } } while (mode == MODE_COLON); count = 0L; break; } /* if that command took us out of vi mode, then exit the loop * NOW, without tweaking the cursor or anything. This is very * important when mode == MODE_QUIT. */ if (mode != MODE_VI) { break; } /* now move the cursor, as appropriate */ if (prevkey && ((keyptr->flags & MVMT) #ifndef NO_VISIBLE || V_from #endif ) && count == 0L) { /* movements used as targets are less strict */ tcurs = adjmove(cursor, tcurs, (int)(keyptr->flags | force_flags)); } else if (keyptr->args == CURSOR_MOVED) { /* the < and > keys have FRNT, * but it shouldn't be applied yet */ tcurs = adjmove(cursor, tcurs, FINL); } else { tcurs = adjmove(cursor, tcurs, (int)(keyptr->flags | force_flags | FINL)); } /* was that the end of a d/c/y/</>/! command? */ if (prevkey && ((keyptr->flags & MVMT) #ifndef NO_VISIBLE || V_from #endif ) && count == 0L) { #ifndef NO_VISIBLE /* turn off the hilight */ V_from = 0L; #endif /* if the movement command failed, cancel operation */ if (tcurs == MARK_UNSET) { prevkey = 0; count = 0; continue; } /* make sure range=front and tcurs=rear. Either way, * leave cursor=range since that's where we started. */ cursor = range; if (tcurs < range) { range = tcurs; tcurs = cursor; } /* The 'w' and 'W' destinations should never take us * to the front of a line. Instead, they should take * us only to the end of the preceding line. */ if ((keyptr->flags & NWRP) == NWRP && markline(range) < markline(tcurs) && (markline(tcurs) > nlines || tcurs == m_front(tcurs, 0L))) { tcurs = (tcurs & ~(BLKSIZE - 1)) - BLKSIZE; pfetch(markline(tcurs)); tcurs += plen; } /* adjust for line mode & inclusion of last char/line */ i = (keyptr->flags | vikeys[prevkey].flags); switch ((i | force_flags) & (INCL|LNMD)) { case INCL: tcurs++; break; case INCL|LNMD: tcurs += BLKSIZE; /* fall through... */ case LNMD: range &= ~(BLKSIZE - 1); tcurs &= ~(BLKSIZE - 1); break; } /* run the function */ tcurs = (*vikeys[prevkey].func)(range, tcurs); if (mode == MODE_VI) { (void)adjmove(cursor, cursor, FINL); cursor = adjmove(cursor, tcurs, (int)(vikeys[prevkey].flags | FINL)); } /* cleanup */ prevkey = 0; } else if (!prevkey) { if (tcurs != MARK_UNSET) cursor = tcurs; } } }
vi() { register int key; /* keystroke from user */ long count; /* numeric argument to some functions */ register struct keystru *keyptr;/* pointer to vikeys[] element */ MARK tcurs; /* temporary cursor */ int prevkey;/* previous key, if d/c/y/</>/! */ MARK range; /* start of range for d/c/y/</>/! */ char text[100]; int dotkey; /* last "key" of a change */ int dotpkey;/* last "prevkey" of a change */ int dotkey2;/* last extra "getkey()" of a change */ int dotcnt; /* last "count" of a change */ register int i; /* tell the redraw() function to start from scratch */ redraw(MARK_UNSET, FALSE); msg((char *)0); #ifdef lint /* lint says that "range" might be used before it is set. This * can't really happen due to the way "range" and "prevkey" are used, * but lint doesn't know that. This line is here ONLY to keep lint * happy. */ range = 0L; #endif /* safeguard against '.' with no previous command */ dotkey = 0; /* Repeatedly handle VI commands */ for (count = 0, prevkey = '\0'; mode == MODE_VI; ) { /* if we've moved off the undoable line, then we can't undo it at all */ if (markline(cursor) != U_line) { U_line = 0L; } /* report any changes from the previous command */ if (rptlines >= *o_report) { redraw(cursor, FALSE); msg("%ld lines %s", rptlines, rptlabel); } rptlines = 0L; /* get the next command key. It must be ASCII */ do { key = getkey(WHEN_VICMD); } while (key < 0 || key > 127); /* change cw and cW commands to ce and cE, respectively */ /* (Why? because the real vi does it that way!) */ if (prevkey == 'c') { if (key == 'w') key = 'e'; else if (key == 'W') key = 'E'; /* wouldn't work right at the end of a word unless we * backspace one character before doing the move. This * will fix most cases. */ if (markidx(cursor) > 0 && (key == 'e' || key == 'E')) { cursor--; } } /* look up the structure describing this command */ keyptr = &vikeys[key]; /* if we're in the middle of a d/c/y/</>/! command, reject * anything but movement or a doubled version like "dd". */ if (prevkey && key != prevkey && !(keyptr->flags & (MVMT|PTMV))) { beep(); prevkey = 0; count = 0; continue; } /* set the "dot" variables, if we're supposed to */ if ((keyptr->flags & SDOT) || (prevkey && vikeys[prevkey].flags & SDOT)) { dotkey = key; dotpkey = prevkey; dotkey2 = '\0'; dotcnt = count; /* remember the line before any changes are made */ if (U_line != markline(cursor)) { U_line = markline(cursor); strcpy(U_text, fetchline(U_line)); } } /* if this is "." then set other vars from the "dot" vars */ if (key == '.') { key = dotkey; keyptr = &vikeys[key]; prevkey = dotpkey; if (prevkey) { range = cursor; } if (count == 0) { count = dotcnt; } doingdot = TRUE; /* remember the line before any changes are made */ if (U_line != markline(cursor)) { U_line = markline(cursor); strcpy(U_text, fetchline(U_line)); } } else { doingdot = FALSE; } /* process the key as a command */ tcurs = cursor; switch (keyptr->args) { case ZERO: if (count == 0) { tcurs = cursor & ~(BLKSIZE - 1); break; } /* else fall through & treat like other digits... */ case DIGIT: count = count * 10 + key - '0'; break; case KEYWORD: /* if not on a keyword, fail */ pfetch(markline(cursor)); key = markidx(cursor); if (!isalnum(ptext[key]) && ptext[key] != '_') { tcurs = MARK_UNSET; break; } /* find the start of the keyword */ while (key > 0 && (isalnum(ptext[key - 1]) || ptext[key - 1] == '_')) { key--; } /* copy it into a buffer, and NUL-terminate it */ i = 0; do { text[i++] = ptext[key++]; } while (isalnum(ptext[key]) || ptext[key] == '_'); text[i] = '\0'; /* call the function */ tcurs = (*keyptr->func)(text); count = 0L; break; case NO_ARGS: if (keyptr->func) { (*keyptr->func)(); } else { beep(); } count = 0L; break; case CURSOR_COUNT: tcurs = (*keyptr->func)(cursor, count); count = 0L; break; case CURSOR: tcurs = (*keyptr->func)(cursor); count = 0L; break; case CURSOR_CNT_KEY: if (doingdot) { tcurs = (*keyptr->func)(cursor, count, dotkey2); } else if (keyptr->flags & SDOT || (prevkey && vikeys[prevkey].flags & SDOT)) { dotkey2 = getkey(0); tcurs = (*keyptr->func)(cursor, count, dotkey2); } else { tcurs = (*keyptr->func)(cursor, count, getkey(0)); } count = 0L; break; case CURSOR_MOVED: /* uppercase keys always act like doubled */ if (isupper(key)) { prevkey = key; range = cursor; } if (prevkey) { /* doubling up a command, use complete lines */ range &= ~(BLKSIZE - 1); if (count) { tcurs = range + MARK_AT_LINE(count); count = 0; } else { tcurs = range + BLKSIZE; } } else { prevkey = key; range = cursor; key = -1; /* so we don't think we doubled yet */ } break; case CURSOR_EOL: /* act like CURSOR_MOVED with '$' movement */ range = cursor; tcurs = moverear(cursor, 1L); count = 0L; prevkey = key; key = '$'; keyptr = &vikeys['$']; break; case CURSOR_TEXT: if (vgets(key, text, sizeof text) >= 0) { /* reassure user that <CR> was hit */ qaddch('\r'); refresh(); /* call the function with the text */ tcurs = (*keyptr->func)(cursor, text); } count = 0L; break; case CURSOR_CNT_CMD: tcurs = (*keyptr->func)(cursor, count, key); count = 0L; break; } /* if that command took us out of vi mode, then exit the loop * NOW, without tweaking the cursor or anything. This is very * important when mode == MODE_QUIT. */ if (mode != MODE_VI) { break; } /* now move the cursor, as appropriate */ if (prevkey && markline(tcurs) > nlines) { /* destination for operator may be nlines + 1 */ cursor = MARK_AT_LINE(nlines + 1); } else if (keyptr->args == CURSOR_MOVED) { /* the < and > keys have FRNT, * but it shouldn't be applied yet */ cursor = adjmove(cursor, tcurs, 0); } else { cursor = adjmove(cursor, tcurs, keyptr->flags); } /* was that the end of a d/c/y/</>/! command? */ if (prevkey && (prevkey == key || (keyptr->flags & MVMT))) { /* if the movement command failed, cancel operation */ if (tcurs == MARK_UNSET) { prevkey = 0; count = 0; continue; } /* make sure range=front and tcurs=rear */ if (cursor < range) { tcurs = range; range = cursor; } else { tcurs = cursor; } /* adjust for line mode */ if (keyptr->flags & LNMD) { range &= ~(BLKSIZE - 1); tcurs &= ~(BLKSIZE - 1); tcurs += BLKSIZE; } /* adjust for inclusion of last char */ if (keyptr->flags & INCL) { tcurs++; } /* temporarily move the cursor to "range" so that * beforedo() remembers the cursor's real location. * This is important if the user later does undo() */ cursor = range; /* run the function */ tcurs = (*vikeys[prevkey].func)(range, tcurs); cursor = adjmove(cursor, tcurs, vikeys[prevkey].flags); /* cleanup */ prevkey = 0; } } }