-//----- Erase from cursor to end of screen -----------------------
-static void clear_to_eos()
-{
- write(1, Ceos, strlen(Ceos)); // Erase from cursor to end of screen
-}
-
-//----- Start standout mode ------------------------------------
-static void standout_start() // send "start reverse video" sequence
-{
- write(1, SOs, strlen(SOs)); // Start reverse video mode
-}
-
-//----- End standout mode --------------------------------------
-static void standout_end() // send "end reverse video" sequence
-{
- write(1, SOn, strlen(SOn)); // End reverse video mode
-}
-
-//----- Flash the screen --------------------------------------
-static void flash(int h)
-{
- standout_start(); // send "start reverse video" sequence
- redraw(TRUE);
- (void) mysleep(h);
- standout_end(); // send "end reverse video" sequence
- redraw(TRUE);
-}
-
-static void beep()
-{
- write(1, bell, strlen(bell)); // send out a bell character
-}
-
-static void indicate_error(char c)
-{
-#ifdef BB_FEATURE_VI_CRASHME
- if (crashme > 0)
- return; // generate a random command
-#endif /* BB_FEATURE_VI_CRASHME */
- if (err_method == 0) {
- beep();
- } else {
- flash(10);
- }
-}
-
-//----- Screen[] Routines --------------------------------------
-//----- Erase the Screen[] memory ------------------------------
-static void screen_erase()
-{
- memset(screen, ' ', screensize); // clear new screen
-}
-
-//----- Draw the status line at bottom of the screen -------------
-static void show_status_line(void)
-{
- static int last_cksum;
- int l, cnt, cksum;
-
- cnt = strlen((char *) status_buffer);
- for (cksum= l= 0; l < cnt; l++) { cksum += (int)(status_buffer[l]); }
- // don't write the status line unless it changes
- if (cnt > 0 && last_cksum != cksum) {
- last_cksum= cksum; // remember if we have seen this line
- place_cursor(rows - 1, 0, FALSE); // put cursor on status line
- write(1, status_buffer, cnt);
- clear_to_eol();
- place_cursor(crow, ccol, FALSE); // put cursor back in correct place
- }
-}
-
-//----- format the status buffer, the bottom line of screen ------
-// print status buffer, with STANDOUT mode
-static void psbs(char *format, ...)
-{
- va_list args;
-
- va_start(args, format);
- strcpy((char *) status_buffer, SOs); // Terminal standout mode on
- vsprintf((char *) status_buffer + strlen((char *) status_buffer), format,
- args);
- strcat((char *) status_buffer, SOn); // Terminal standout mode off
- va_end(args);
-
- return;
-}
-
-// print status buffer
-static void psb(char *format, ...)
-{
- va_list args;
-
- va_start(args, format);
- vsprintf((char *) status_buffer, format, args);
- va_end(args);
- return;
-}
-
-static void ni(Byte * s) // display messages
-{
- Byte buf[BUFSIZ];
-
- print_literal(buf, s);
- psbs("\'%s\' is not implemented", buf);
-}
-
-static void edit_status(void) // show file status on status line
-{
- int cur, tot, percent;
-
- cur = count_lines(text, dot);
- tot = count_lines(text, end - 1);
- // current line percent
- // ------------- ~~ ----------
- // total lines 100
- if (tot > 0) {
- percent = (100 * cur) / tot;
- } else {
- cur = tot = 0;
- percent = 100;
- }
- psb("\"%s\""
-#ifdef BB_FEATURE_VI_READONLY
- "%s"
-#endif /* BB_FEATURE_VI_READONLY */
- "%s line %d of %d --%d%%--",
- (cfn != 0 ? (char *) cfn : "No file"),
-#ifdef BB_FEATURE_VI_READONLY
- ((vi_readonly == TRUE || readonly == TRUE) ? " [Read only]" : ""),
-#endif /* BB_FEATURE_VI_READONLY */
- (file_modified == TRUE ? " [modified]" : ""),
- cur, tot, percent);
-}
-
-//----- Force refresh of all Lines -----------------------------
-static void redraw(int full_screen)
-{
- place_cursor(0, 0, FALSE); // put cursor in correct place
- clear_to_eos(); // tel terminal to erase display
- screen_erase(); // erase the internal screen buffer
- refresh(full_screen); // this will redraw the entire display
-}
-
-//----- Format a text[] line into a buffer ---------------------
-static void format_line(Byte *dest, Byte *src, int li)
-{
- int co;
- Byte c;
-
- for (co= 0; co < MAX_SCR_COLS; co++) {
- c= ' '; // assume blank
- if (li > 0 && co == 0) {
- c = '~'; // not first line, assume Tilde
+key_cmd_mode:
+ switch (c) {
+ //case 0x01: // soh
+ //case 0x09: // ht
+ //case 0x0b: // vt
+ //case 0x0e: // so
+ //case 0x0f: // si
+ //case 0x10: // dle
+ //case 0x11: // dc1
+ //case 0x13: // dc3
+#ifdef CONFIG_FEATURE_VI_CRASHME
+ case 0x14: // dc4 ctrl-T
+ crashme = (crashme == 0) ? 1 : 0;
+ break;
+#endif /* CONFIG_FEATURE_VI_CRASHME */
+ //case 0x16: // syn
+ //case 0x17: // etb
+ //case 0x18: // can
+ //case 0x1c: // fs
+ //case 0x1d: // gs
+ //case 0x1e: // rs
+ //case 0x1f: // us
+ //case '!': // !-
+ //case '#': // #-
+ //case '&': // &-
+ //case '(': // (-
+ //case ')': // )-
+ //case '*': // *-
+ //case ',': // ,-
+ //case '=': // =-
+ //case '@': // @-
+ //case 'F': // F-
+ //case 'K': // K-
+ //case 'Q': // Q-
+ //case 'S': // S-
+ //case 'T': // T-
+ //case 'V': // V-
+ //case '[': // [-
+ //case '\\': // \-
+ //case ']': // ]-
+ //case '_': // _-
+ //case '`': // `-
+ //case 'g': // g-
+ //case 'u': // u- FIXME- there is no undo
+ //case 'v': // v-
+ default: // unrecognised command
+ buf[0] = c;
+ buf[1] = '\0';
+ if (c <= ' ') {
+ buf[0] = '^';
+ buf[1] = c + '@';
+ buf[2] = '\0';
+ }
+ ni((Byte *) buf);
+ end_cmd_q(); // stop adding to q
+ case 0x00: // nul- ignore
+ break;
+ case 2: // ctrl-B scroll up full screen
+ case VI_K_PAGEUP: // Cursor Key Page Up
+ dot_scroll(rows - 2, -1);
+ break;
+#ifdef CONFIG_FEATURE_VI_USE_SIGNALS
+ case 0x03: // ctrl-C interrupt
+ longjmp(restart, 1);
+ break;
+ case 26: // ctrl-Z suspend
+ suspend_sig(SIGTSTP);
+ break;
+#endif /* CONFIG_FEATURE_VI_USE_SIGNALS */
+ case 4: // ctrl-D scroll down half screen
+ dot_scroll((rows - 2) / 2, 1);
+ break;
+ case 5: // ctrl-E scroll down one line
+ dot_scroll(1, 1);
+ break;
+ case 6: // ctrl-F scroll down full screen
+ case VI_K_PAGEDOWN: // Cursor Key Page Down
+ dot_scroll(rows - 2, 1);
+ break;
+ case 7: // ctrl-G show current status
+ edit_status();
+ break;
+ case 'h': // h- move left
+ case VI_K_LEFT: // cursor key Left
+ case 8: // ctrl-H- move left (This may be ERASE char)
+ case 127: // DEL- move left (This may be ERASE char)
+ if (cmdcnt-- > 1) {
+ do_cmd(c);
+ } // repeat cnt
+ dot_left();
+ break;
+ case 10: // Newline ^J
+ case 'j': // j- goto next line, same col
+ case VI_K_DOWN: // cursor key Down
+ if (cmdcnt-- > 1) {
+ do_cmd(c);
+ } // repeat cnt
+ dot_next(); // go to next B-o-l
+ dot = move_to_col(dot, ccol + offset); // try stay in same col
+ break;
+ case 12: // ctrl-L force redraw whole screen
+ case 18: // ctrl-R force redraw
+ place_cursor(0, 0, FALSE); // put cursor in correct place
+ clear_to_eos(); // tel terminal to erase display
+ (void) mysleep(10);
+ screen_erase(); // erase the internal screen buffer
+ refresh(TRUE); // this will redraw the entire display
+ break;
+ case 13: // Carriage Return ^M
+ case '+': // +- goto next line
+ if (cmdcnt-- > 1) {
+ do_cmd(c);
+ } // repeat cnt
+ dot_next();
+ dot_skip_over_ws();
+ break;
+ case 21: // ctrl-U scroll up half screen
+ dot_scroll((rows - 2) / 2, -1);
+ break;
+ case 25: // ctrl-Y scroll up one line
+ dot_scroll(1, -1);
+ break;
+ case 27: // esc
+ if (cmd_mode == 0)
+ indicate_error(c);
+ cmd_mode = 0; // stop insrting
+ end_cmd_q();
+ *status_buffer = '\0'; // clear status buffer
+ break;
+ case ' ': // move right
+ case 'l': // move right
+ case VI_K_RIGHT: // Cursor Key Right
+ if (cmdcnt-- > 1) {
+ do_cmd(c);
+ } // repeat cnt
+ dot_right();
+ break;
+#ifdef CONFIG_FEATURE_VI_YANKMARK
+ case '"': // "- name a register to use for Delete/Yank
+ c1 = get_one_char();
+ c1 = tolower(c1);
+ if (islower(c1)) {
+ YDreg = c1 - 'a';
+ } else {
+ indicate_error(c);
+ }
+ break;
+ case '\'': // '- goto a specific mark
+ c1 = get_one_char();
+ c1 = tolower(c1);
+ if (islower(c1)) {
+ c1 = c1 - 'a';
+ // get the b-o-l
+ q = mark[(int) c1];
+ if (text <= q && q < end) {
+ dot = q;
+ dot_begin(); // go to B-o-l
+ dot_skip_over_ws();
+ }
+ } else if (c1 == '\'') { // goto previous context
+ dot = swap_context(dot); // swap current and previous context
+ dot_begin(); // go to B-o-l
+ dot_skip_over_ws();
+ } else {
+ indicate_error(c);
+ }
+ break;
+ case 'm': // m- Mark a line
+ // this is really stupid. If there are any inserts or deletes
+ // between text[0] and dot then this mark will not point to the
+ // correct location! It could be off by many lines!
+ // Well..., at least its quick and dirty.
+ c1 = get_one_char();
+ c1 = tolower(c1);
+ if (islower(c1)) {
+ c1 = c1 - 'a';
+ // remember the line
+ mark[(int) c1] = dot;
+ } else {
+ indicate_error(c);
+ }
+ break;
+ case 'P': // P- Put register before
+ case 'p': // p- put register after
+ p = reg[YDreg];
+ if (p == 0) {
+ psbs("Nothing in register %c", what_reg());
+ break;
+ }
+ // are we putting whole lines or strings
+ if (strchr((char *) p, '\n') != NULL) {
+ if (c == 'P') {
+ dot_begin(); // putting lines- Put above
+ }
+ if (c == 'p') {
+ // are we putting after very last line?
+ if (end_line(dot) == (end - 1)) {
+ dot = end; // force dot to end of text[]
+ } else {
+ dot_next(); // next line, then put before
+ }
+ }
+ } else {
+ if (c == 'p')
+ dot_right(); // move to right, can move to NL
+ }
+ dot = string_insert(dot, p); // insert the string
+ end_cmd_q(); // stop adding to q
+ break;
+ case 'U': // U- Undo; replace current line with original version
+ if (reg[Ureg] != 0) {
+ p = begin_line(dot);
+ q = end_line(dot);
+ p = text_hole_delete(p, q); // delete cur line
+ p = string_insert(p, reg[Ureg]); // insert orig line
+ dot = p;
+ dot_skip_over_ws();
+ }
+ break;
+#endif /* CONFIG_FEATURE_VI_YANKMARK */
+ case '$': // $- goto end of line
+ case VI_K_END: // Cursor Key End
+ if (cmdcnt-- > 1) {
+ do_cmd(c);
+ } // repeat cnt
+ dot = end_line(dot + 1);
+ break;
+ case '%': // %- find matching char of pair () [] {}
+ for (q = dot; q < end && *q != '\n'; q++) {
+ if (strchr("()[]{}", *q) != NULL) {
+ // we found half of a pair
+ p = find_pair(q, *q);
+ if (p == NULL) {
+ indicate_error(c);
+ } else {
+ dot = p;
+ }
+ break;
+ }
+ }
+ if (*q == '\n')
+ indicate_error(c);
+ break;
+ case 'f': // f- forward to a user specified char
+ last_forward_char = get_one_char(); // get the search char
+ //
+ // dont seperate these two commands. 'f' depends on ';'
+ //
+ //**** fall thru to ... 'i'
+ case ';': // ;- look at rest of line for last forward char
+ if (cmdcnt-- > 1) {
+ do_cmd(';');
+ } // repeat cnt
+ if (last_forward_char == 0) break;
+ q = dot + 1;
+ while (q < end - 1 && *q != '\n' && *q != last_forward_char) {
+ q++;
+ }
+ if (*q == last_forward_char)
+ dot = q;
+ break;
+ case '-': // -- goto prev line
+ if (cmdcnt-- > 1) {
+ do_cmd(c);
+ } // repeat cnt
+ dot_prev();
+ dot_skip_over_ws();
+ break;
+#ifdef CONFIG_FEATURE_VI_DOT_CMD
+ case '.': // .- repeat the last modifying command
+ // Stuff the last_modifying_cmd back into stdin
+ // and let it be re-executed.
+ if (last_modifying_cmd != 0) {
+ ioq = ioq_start = (Byte *) xstrdup((char *) last_modifying_cmd);
+ }
+ break;
+#endif /* CONFIG_FEATURE_VI_DOT_CMD */
+#ifdef CONFIG_FEATURE_VI_SEARCH
+ case '?': // /- search for a pattern
+ case '/': // /- search for a pattern
+ buf[0] = c;
+ buf[1] = '\0';
+ q = get_input_line(buf); // get input line- use "status line"
+ if (strlen((char *) q) == 1)
+ goto dc3; // if no pat re-use old pat
+ if (strlen((char *) q) > 1) { // new pat- save it and find
+ // there is a new pat
+ if (last_search_pattern != 0) {
+ free(last_search_pattern);
+ }
+ last_search_pattern = (Byte *) xstrdup((char *) q);
+ goto dc3; // now find the pattern
+ }
+ // user changed mind and erased the "/"- do nothing
+ break;
+ case 'N': // N- backward search for last pattern
+ if (cmdcnt-- > 1) {
+ do_cmd(c);
+ } // repeat cnt
+ dir = BACK; // assume BACKWARD search
+ p = dot - 1;
+ if (last_search_pattern[0] == '?') {
+ dir = FORWARD;
+ p = dot + 1;
+ }
+ goto dc4; // now search for pattern
+ break;
+ case 'n': // n- repeat search for last pattern
+ // search rest of text[] starting at next char
+ // if search fails return orignal "p" not the "p+1" address
+ if (cmdcnt-- > 1) {
+ do_cmd(c);
+ } // repeat cnt
+ dc3:
+ if (last_search_pattern == 0) {
+ msg = (Byte *) "No previous regular expression";
+ goto dc2;
+ }
+ if (last_search_pattern[0] == '/') {
+ dir = FORWARD; // assume FORWARD search
+ p = dot + 1;
+ }
+ if (last_search_pattern[0] == '?') {
+ dir = BACK;
+ p = dot - 1;
+ }
+ dc4:
+ q = char_search(p, last_search_pattern + 1, dir, FULL);
+ if (q != NULL) {
+ dot = q; // good search, update "dot"
+ msg = (Byte *) "";
+ goto dc2;
+ }
+ // no pattern found between "dot" and "end"- continue at top
+ p = text;
+ if (dir == BACK) {
+ p = end - 1;
+ }
+ q = char_search(p, last_search_pattern + 1, dir, FULL);
+ if (q != NULL) { // found something
+ dot = q; // found new pattern- goto it
+ msg = (Byte *) "search hit BOTTOM, continuing at TOP";
+ if (dir == BACK) {
+ msg = (Byte *) "search hit TOP, continuing at BOTTOM";
+ }
+ } else {
+ msg = (Byte *) "Pattern not found";
+ }
+ dc2:
+ psbs("%s", msg);
+ break;
+ case '{': // {- move backward paragraph
+ q = char_search(dot, (Byte *) "\n\n", BACK, FULL);
+ if (q != NULL) { // found blank line
+ dot = next_line(q); // move to next blank line
+ }
+ break;
+ case '}': // }- move forward paragraph
+ q = char_search(dot, (Byte *) "\n\n", FORWARD, FULL);
+ if (q != NULL) { // found blank line
+ dot = next_line(q); // move to next blank line
+ }
+ break;
+#endif /* CONFIG_FEATURE_VI_SEARCH */
+ case '0': // 0- goto begining of line
+ case '1': // 1-
+ case '2': // 2-
+ case '3': // 3-
+ case '4': // 4-
+ case '5': // 5-
+ case '6': // 6-
+ case '7': // 7-
+ case '8': // 8-
+ case '9': // 9-
+ if (c == '0' && cmdcnt < 1) {
+ dot_begin(); // this was a standalone zero
+ } else {
+ cmdcnt = cmdcnt * 10 + (c - '0'); // this 0 is part of a number