+ int r = read(0, (void *) (readbuffer + n), BUFSIZ - n);
+ if (r > 0) {
+ n += r;
+ }
+ }
+ }
+ readed_for_parse = n;
+ }
+ c = readbuffer[0];
+ if(c == 27 && n > 1) {
+ // Maybe cursor or function key?
+ const struct esc_cmds *eindex;
+
+ for (eindex = esccmds; eindex < &esccmds[ESCCMDS_COUNT]; eindex++) {
+ int cnt = strlen(eindex->seq);
+
+ if(n <= cnt)
+ continue;
+ if(strncmp(eindex->seq, (char *) readbuffer + 1, cnt))
+ continue;
+ // is a Cursor key- put derived value back into Q
+ c = eindex->val;
+ // for squeeze out the ESC sequence
+ n = cnt + 1;
+ break;
+ }
+ if(eindex == &esccmds[ESCCMDS_COUNT]) {
+ /* defined ESC sequence not found, set only one ESC */
+ n = 1;
+ }
+ } else {
+ n = 1;
+ }
+ // remove key sequence from Q
+ readed_for_parse -= n;
+ memmove(readbuffer, readbuffer + n, BUFSIZ - n);
+ (void) alarm(3); // we are done waiting for input, turn alarm ON
+ return c;
+}
+
+//----- IO Routines --------------------------------------------
+static Byte get_one_char(void)
+{
+ static Byte c;
+
+#if ENABLE_FEATURE_VI_DOT_CMD
+ // ! adding2q && ioq == 0 read()
+ // ! adding2q && ioq != 0 *ioq
+ // adding2q *last_modifying_cmd= read()
+ if (!adding2q) {
+ // we are not adding to the q.
+ // but, we may be reading from a q
+ if (ioq == 0) {
+ // there is no current q, read from STDIN
+ c = readit(); // get the users input
+ } else {
+ // there is a queue to get chars from first
+ c = *ioq++;
+ if (c == '\0') {
+ // the end of the q, read from STDIN
+ free(ioq_start);
+ ioq_start = ioq = 0;
+ c = readit(); // get the users input
+ }
+ }
+ } else {
+ // adding STDIN chars to q
+ c = readit(); // get the users input
+ if (last_modifying_cmd != 0) {
+ int len = strlen((char *) last_modifying_cmd);
+ if (len + 1 >= BUFSIZ) {
+ psbs("last_modifying_cmd overrun");
+ } else {
+ // add new char to q
+ last_modifying_cmd[len] = c;
+ }
+ }
+ }
+#else
+ c = readit(); // get the users input
+#endif /* FEATURE_VI_DOT_CMD */
+ return c; // return the char, where ever it came from
+}
+
+static Byte *get_input_line(Byte * prompt) // get input line- use "status line"
+{
+ Byte buf[BUFSIZ];
+ Byte c;
+ int i;
+ static Byte *obufp = NULL;
+
+ strcpy((char *) buf, (char *) prompt);
+ last_status_cksum = 0; // force status update
+ place_cursor(rows - 1, 0, FALSE); // go to Status line, bottom of screen
+ clear_to_eol(); // clear the line
+ write1((char *) prompt); // write out the :, /, or ? prompt
+
+ for (i = strlen((char *) buf); i < BUFSIZ;) {
+ c = get_one_char(); // read user input
+ if (c == '\n' || c == '\r' || c == 27)
+ break; // is this end of input
+ if (c == erase_char || c == 8 || c == 127) {
+ // user wants to erase prev char
+ i--; // backup to prev char
+ buf[i] = '\0'; // erase the char
+ buf[i + 1] = '\0'; // null terminate buffer
+ write1("\b \b"); // erase char on screen
+ if (i <= 0) { // user backs up before b-o-l, exit
+ break;
+ }
+ } else {
+ buf[i] = c; // save char in buffer
+ buf[i + 1] = '\0'; // make sure buffer is null terminated
+ putchar(c); // echo the char back to user
+ i++;
+ }
+ }
+ refresh(FALSE);
+ free(obufp);
+ obufp = (Byte *) xstrdup((char *) buf);
+ return obufp;
+}
+
+static int file_size(const Byte * fn) // what is the byte size of "fn"
+{
+ struct stat st_buf;
+ int cnt, sr;
+
+ if (fn == 0 || strlen((char *)fn) <= 0)
+ return -1;
+ cnt = -1;
+ sr = stat((char *) fn, &st_buf); // see if file exists
+ if (sr >= 0) {
+ cnt = (int) st_buf.st_size;
+ }
+ return cnt;
+}
+
+static int file_insert(Byte * fn, Byte * p, int size)
+{
+ int fd, cnt;
+
+ cnt = -1;
+#if ENABLE_FEATURE_VI_READONLY
+ readonly = FALSE;
+#endif
+ if (fn == 0 || strlen((char*) fn) <= 0) {
+ psbs("No filename given");
+ goto fi0;
+ }
+ if (size == 0) {
+ // OK- this is just a no-op
+ cnt = 0;
+ goto fi0;
+ }
+ if (size < 0) {
+ psbs("Trying to insert a negative number (%d) of characters", size);
+ goto fi0;
+ }
+ if (p < text || p > end) {
+ psbs("Trying to insert file outside of memory");
+ goto fi0;
+ }
+
+ // see if we can open the file
+#if ENABLE_FEATURE_VI_READONLY
+ if (vi_readonly) goto fi1; // do not try write-mode
+#endif
+ fd = open((char *) fn, O_RDWR); // assume read & write
+ if (fd < 0) {
+ // could not open for writing- maybe file is read only
+#if ENABLE_FEATURE_VI_READONLY
+ fi1:
+#endif
+ fd = open((char *) fn, O_RDONLY); // try read-only
+ if (fd < 0) {
+ psbs("\"%s\" %s", fn, "cannot open file");
+ goto fi0;
+ }
+#if ENABLE_FEATURE_VI_READONLY
+ // got the file- read-only
+ readonly = TRUE;
+#endif
+ }
+ p = text_hole_make(p, size);
+ cnt = read(fd, p, size);
+ close(fd);
+ if (cnt < 0) {
+ cnt = -1;
+ p = text_hole_delete(p, p + size - 1); // un-do buffer insert
+ psbs("cannot read file \"%s\"", fn);
+ } else if (cnt < size) {
+ // There was a partial read, shrink unused space text[]
+ p = text_hole_delete(p + cnt, p + (size - cnt) - 1); // un-do buffer insert
+ psbs("cannot read all of file \"%s\"", fn);
+ }
+ if (cnt >= size)
+ file_modified++;
+ fi0:
+ return cnt;
+}
+
+static int file_write(Byte * fn, Byte * first, Byte * last)
+{
+ int fd, cnt, charcnt;
+
+ if (fn == 0) {
+ psbs("No current filename");
+ return -2;
+ }
+ charcnt = 0;
+ // FIXIT- use the correct umask()
+ fd = open((char *) fn, (O_WRONLY | O_CREAT | O_TRUNC), 0664);
+ if (fd < 0)
+ return -1;
+ cnt = last - first + 1;
+ charcnt = write(fd, first, cnt);
+ if (charcnt == cnt) {
+ // good write
+ //file_modified= FALSE; // the file has not been modified
+ } else {
+ charcnt = 0;
+ }
+ close(fd);
+ return charcnt;
+}
+
+//----- Terminal Drawing ---------------------------------------
+// The terminal is made up of 'rows' line of 'columns' columns.
+// classically this would be 24 x 80.
+// screen coordinates
+// 0,0 ... 0,79
+// 1,0 ... 1,79
+// . ... .
+// . ... .
+// 22,0 ... 22,79
+// 23,0 ... 23,79 status line
+//
+
+//----- Move the cursor to row x col (count from 0, not 1) -------
+static void place_cursor(int row, int col, int opti)
+{
+ char cm1[BUFSIZ];
+ char *cm;
+#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
+ char cm2[BUFSIZ];
+ Byte *screenp;
+ // char cm3[BUFSIZ];
+ int Rrow= last_row;
+#endif
+
+ memset(cm1, '\0', BUFSIZ - 1); // clear the buffer
+
+ if (row < 0) row = 0;
+ if (row >= rows) row = rows - 1;
+ if (col < 0) col = 0;
+ if (col >= columns) col = columns - 1;
+
+ //----- 1. Try the standard terminal ESC sequence
+ sprintf((char *) cm1, CMrc, row + 1, col + 1);
+ cm= cm1;
+ if (! opti) goto pc0;
+
+#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
+ //----- find the minimum # of chars to move cursor -------------
+ //----- 2. Try moving with discreet chars (Newline, [back]space, ...)
+ memset(cm2, '\0', BUFSIZ - 1); // clear the buffer
+
+ // move to the correct row
+ while (row < Rrow) {
+ // the cursor has to move up
+ strcat(cm2, CMup);
+ Rrow--;
+ }
+ while (row > Rrow) {
+ // the cursor has to move down
+ strcat(cm2, CMdown);
+ Rrow++;
+ }
+
+ // now move to the correct column
+ strcat(cm2, "\r"); // start at col 0
+ // just send out orignal source char to get to correct place
+ screenp = &screen[row * columns]; // start of screen line
+ strncat(cm2, (char* )screenp, col);
+
+ //----- 3. Try some other way of moving cursor
+ //---------------------------------------------
+
+ // pick the shortest cursor motion to send out
+ cm= cm1;
+ if (strlen(cm2) < strlen(cm)) {
+ cm= cm2;
+ } /* else if (strlen(cm3) < strlen(cm)) {
+ cm= cm3;
+ } */
+#endif /* FEATURE_VI_OPTIMIZE_CURSOR */
+ pc0:
+ write1(cm); // move the cursor
+}
+
+//----- Erase from cursor to end of line -----------------------
+static void clear_to_eol(void)
+{
+ write1(Ceol); // Erase from cursor to end of line
+}
+
+//----- Erase from cursor to end of screen -----------------------
+static void clear_to_eos(void)
+{
+ write1(Ceos); // Erase from cursor to end of screen
+}
+
+//----- Start standout mode ------------------------------------
+static void standout_start(void) // send "start reverse video" sequence
+{
+ write1(SOs); // Start reverse video mode
+}
+
+//----- End standout mode --------------------------------------
+static void standout_end(void) // send "end reverse video" sequence
+{
+ write1(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 Indicate_Error(void)
+{
+#if ENABLE_FEATURE_VI_CRASHME
+ if (crashme > 0)
+ return; // generate a random command
+#endif
+ if (!err_method) {
+ write1(bell); // send out a bell character
+ } else {
+ flash(10);
+ }
+}
+
+//----- Screen[] Routines --------------------------------------
+//----- Erase the Screen[] memory ------------------------------
+static void screen_erase(void)
+{
+ memset(screen, ' ', screensize); // clear new screen
+}
+
+static int bufsum(unsigned char *buf, int count)
+{
+ int sum = 0;
+ unsigned char *e = buf + count;
+ while (buf < e)
+ sum += *buf++;
+ return sum;
+}
+
+//----- Draw the status line at bottom of the screen -------------
+static void show_status_line(void)
+{
+ int cnt = 0, cksum = 0;
+
+ // either we already have an error or status message, or we
+ // create one.
+ if (!have_status_msg) {
+ cnt = format_edit_status();
+ cksum = bufsum(status_buffer, cnt);
+ }
+ if (have_status_msg || ((cnt > 0 && last_status_cksum != cksum))) {
+ last_status_cksum= cksum; // remember if we have seen this line
+ place_cursor(rows - 1, 0, FALSE); // put cursor on status line
+ write1((char*)status_buffer);
+ clear_to_eol();
+ if (have_status_msg) {
+ if (((int)strlen((char*)status_buffer) - (have_status_msg - 1)) >
+ (columns - 1) ) {
+ have_status_msg = 0;
+ Hit_Return();
+ }
+ have_status_msg = 0;
+ }
+ place_cursor(crow, ccol, FALSE); // put cursor back in correct place
+ }
+ fflush(stdout);
+}
+
+//----- format the status buffer, the bottom line of screen ------
+// format status buffer, with STANDOUT mode
+static void psbs(const 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);
+
+ have_status_msg = 1 + sizeof(SOs) + sizeof(SOn) - 2;
+
+ return;
+}
+
+// format status buffer
+static void psb(const char *format, ...)
+{
+ va_list args;
+
+ va_start(args, format);
+ vsprintf((char *) status_buffer, format, args);
+ va_end(args);
+
+ have_status_msg = 1;
+
+ return;
+}
+
+static void ni(Byte * s) // display messages
+{
+ Byte buf[BUFSIZ];
+
+ print_literal(buf, s);
+ psbs("\'%s\' is not implemented", buf);
+}
+
+static int format_edit_status(void) // show file status on status line
+{
+ int cur, percent, ret, trunc_at;
+ static int tot;
+
+ // file_modified is now a counter rather than a flag. this
+ // helps reduce the amount of line counting we need to do.
+ // (this will cause a mis-reporting of modified status
+ // once every MAXINT editing operations.)
+
+ // it would be nice to do a similar optimization here -- if
+ // we haven't done a motion that could have changed which line
+ // we're on, then we shouldn't have to do this count_lines()
+ cur = count_lines(text, dot);
+
+ // reduce counting -- the total lines can't have
+ // changed if we haven't done any edits.
+ if (file_modified != last_file_modified) {
+ tot = cur + count_lines(dot, end - 1) - 1;
+ last_file_modified = file_modified;
+ }
+
+ // current line percent
+ // ------------- ~~ ----------
+ // total lines 100
+ if (tot > 0) {
+ percent = (100 * cur) / tot;
+ } else {
+ cur = tot = 0;
+ percent = 100;
+ }
+
+ trunc_at = columns < STATUS_BUFFER_LEN-1 ?
+ columns : STATUS_BUFFER_LEN-1;
+
+ ret = snprintf((char *) status_buffer, trunc_at+1,
+#if ENABLE_FEATURE_VI_READONLY
+ "%c %s%s%s %d/%d %d%%",
+#else
+ "%c %s%s %d/%d %d%%",
+#endif
+ (cmd_mode ? (cmd_mode == 2 ? 'R':'I'):'-'),
+ (cfn != 0 ? (char *) cfn : "No file"),
+#if ENABLE_FEATURE_VI_READONLY
+ ((vi_readonly || readonly) ? " [Read-only]" : ""),
+#endif
+ (file_modified ? " [modified]" : ""),
+ cur, tot, percent);
+
+ if (ret >= 0 && ret < trunc_at)
+ return ret; /* it all fit */
+
+ return trunc_at; /* had to truncate */
+}
+
+//----- 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
+ last_status_cksum = 0; // force status update
+ refresh(full_screen); // this will redraw the entire display
+ show_status_line();
+}
+
+//----- 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
+ }
+ // are there chars in text[] and have we gone past the end
+ if (text < end && src < end) {
+ c = *src++;
+ }
+ if (c == '\n')
+ break;
+ if (c > 127 && !Isprint(c)) {
+ c = '.';
+ }
+ if (c < ' ' || c == 127) {
+ if (c == '\t') {
+ c = ' ';
+ // co % 8 != 7
+ for (; (co % tabstop) != (tabstop - 1); co++) {
+ dest[co] = c;
+ }
+ } else {
+ dest[co++] = '^';
+ if(c == 127)
+ c = '?';
+ else
+ c += '@'; // make it visible
+ }
+ }
+ // the co++ is done here so that the column will
+ // not be overwritten when we blank-out the rest of line
+ dest[co] = c;
+ if (src >= end)
+ break;
+ }
+}
+
+//----- Refresh the changed screen lines -----------------------
+// Copy the source line from text[] into the buffer and note
+// if the current screenline is different from the new buffer.
+// If they differ then that line needs redrawing on the terminal.
+//
+static void refresh(int full_screen)
+{
+ static int old_offset;
+ int li, changed;
+ Byte buf[MAX_SCR_COLS];
+ Byte *tp, *sp; // pointer into text[] and screen[]
+#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
+ int last_li= -2; // last line that changed- for optimizing cursor movement
+#endif
+
+ if (ENABLE_FEATURE_VI_WIN_RESIZE)
+ get_terminal_width_height(0, &columns, &rows);
+ sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot")
+ tp = screenbegin; // index into text[] of top line
+
+ // compare text[] to screen[] and mark screen[] lines that need updating
+ for (li = 0; li < rows - 1; li++) {
+ int cs, ce; // column start & end
+ memset(buf, ' ', MAX_SCR_COLS); // blank-out the buffer
+ buf[MAX_SCR_COLS-1] = 0; // NULL terminate the buffer
+ // format current text line into buf
+ format_line(buf, tp, li);
+
+ // skip to the end of the current text[] line
+ while (tp < end && *tp++ != '\n') /*no-op*/ ;
+
+ // see if there are any changes between vitual screen and buf
+ changed = FALSE; // assume no change
+ cs= 0;
+ ce= columns-1;
+ sp = &screen[li * columns]; // start of screen line
+ if (full_screen) {
+ // force re-draw of every single column from 0 - columns-1
+ goto re0;
+ }
+ // compare newly formatted buffer with virtual screen
+ // look forward for first difference between buf and screen
+ for ( ; cs <= ce; cs++) {
+ if (buf[cs + offset] != sp[cs]) {
+ changed = TRUE; // mark for redraw
+ break;
+ }
+ }
+
+ // look backward for last difference between buf and screen
+ for ( ; ce >= cs; ce--) {
+ if (buf[ce + offset] != sp[ce]) {
+ changed = TRUE; // mark for redraw
+ break;
+ }
+ }
+ // now, cs is index of first diff, and ce is index of last diff
+
+ // if horz offset has changed, force a redraw
+ if (offset != old_offset) {
+ re0:
+ changed = TRUE;
+ }
+
+ // make a sanity check of columns indexes
+ if (cs < 0) cs= 0;
+ if (ce > columns-1) ce= columns-1;
+ if (cs > ce) { cs= 0; ce= columns-1; }
+ // is there a change between vitual screen and buf
+ if (changed) {
+ // copy changed part of buffer to virtual screen
+ memmove(sp+cs, buf+(cs+offset), ce-cs+1);
+
+ // move cursor to column of first change
+ if (offset != old_offset) {
+ // opti_cur_move is still too stupid
+ // to handle offsets correctly
+ place_cursor(li, cs, FALSE);
+ } else {
+#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
+ // if this just the next line
+ // try to optimize cursor movement
+ // otherwise, use standard ESC sequence
+ place_cursor(li, cs, li == (last_li+1) ? TRUE : FALSE);
+ last_li= li;
+#else
+ place_cursor(li, cs, FALSE); // use standard ESC sequence
+#endif /* FEATURE_VI_OPTIMIZE_CURSOR */
+ }
+
+ // write line out to terminal
+ {
+ int nic = ce-cs+1;
+ char *out = (char*)sp+cs;
+
+ while (nic-- > 0) {
+ putchar(*out);
+ out++;
+ }
+ }
+#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
+ last_row = li;
+#endif
+ }
+ }
+
+#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
+ place_cursor(crow, ccol, (crow == last_row) ? TRUE : FALSE);
+ last_row = crow;
+#else
+ place_cursor(crow, ccol, FALSE);
+#endif
+
+ if (offset != old_offset)
+ old_offset = offset;
+}
+
+//---------------------------------------------------------------------
+//----- the Ascii Chart -----------------------------------------------
+//
+// 00 nul 01 soh 02 stx 03 etx 04 eot 05 enq 06 ack 07 bel
+// 08 bs 09 ht 0a nl 0b vt 0c np 0d cr 0e so 0f si
+// 10 dle 11 dc1 12 dc2 13 dc3 14 dc4 15 nak 16 syn 17 etb
+// 18 can 19 em 1a sub 1b esc 1c fs 1d gs 1e rs 1f us
+// 20 sp 21 ! 22 " 23 # 24 $ 25 % 26 & 27 '
+// 28 ( 29 ) 2a * 2b + 2c , 2d - 2e . 2f /
+// 30 0 31 1 32 2 33 3 34 4 35 5 36 6 37 7
+// 38 8 39 9 3a : 3b ; 3c < 3d = 3e > 3f ?
+// 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G
+// 48 H 49 I 4a J 4b K 4c L 4d M 4e N 4f O
+// 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W
+// 58 X 59 Y 5a Z 5b [ 5c \ 5d ] 5e ^ 5f _
+// 60 ` 61 a 62 b 63 c 64 d 65 e 66 f 67 g
+// 68 h 69 i 6a j 6b k 6c l 6d m 6e n 6f o
+// 70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w
+// 78 x 79 y 7a z 7b { 7c | 7d } 7e ~ 7f del
+//---------------------------------------------------------------------
+
+//----- Execute a Vi Command -----------------------------------
+static void do_cmd(Byte c)
+{
+ Byte c1, *p, *q, *msg, buf[9], *save_dot;
+ int cnt, i, j, dir, yf;
+
+ c1 = c; // quiet the compiler
+ cnt = yf = dir = 0; // quiet the compiler
+ p = q = save_dot = msg = buf; // quiet the compiler
+ memset(buf, '\0', 9); // clear buf
+
+ show_status_line();
+
+ /* if this is a cursor key, skip these checks */
+ switch (c) {
+ case VI_K_UP:
+ case VI_K_DOWN:
+ case VI_K_LEFT:
+ case VI_K_RIGHT:
+ case VI_K_HOME:
+ case VI_K_END:
+ case VI_K_PAGEUP:
+ case VI_K_PAGEDOWN:
+ goto key_cmd_mode;
+ }
+
+ if (cmd_mode == 2) {
+ // flip-flop Insert/Replace mode
+ if (c == VI_K_INSERT) goto dc_i;
+ // we are 'R'eplacing the current *dot with new char
+ if (*dot == '\n') {
+ // don't Replace past E-o-l
+ cmd_mode = 1; // convert to insert
+ } else {
+ if (1 <= c || Isprint(c)) {
+ if (c != 27)
+ dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
+ dot = char_insert(dot, c); // insert new char
+ }
+ goto dc1;
+ }
+ }
+ if (cmd_mode == 1) {
+ // hitting "Insert" twice means "R" replace mode
+ if (c == VI_K_INSERT) goto dc5;
+ // insert the char c at "dot"
+ if (1 <= c || Isprint(c)) {
+ dot = char_insert(dot, c);
+ }
+ goto dc1;
+ }
+
+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
+#if ENABLE_FEATURE_VI_CRASHME
+ case 0x14: // dc4 ctrl-T
+ crashme = (crashme == 0) ? 1 : 0;
+ break;
+#endif
+ //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;
+#if ENABLE_FEATURE_VI_USE_SIGNALS
+ case 0x03: // ctrl-C interrupt
+ longjmp(restart, 1);
+ break;
+ case 26: // ctrl-Z suspend
+ suspend_sig(SIGTSTP);
+ break;
+#endif
+ 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
+ last_status_cksum = 0; // force status update
+ 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
+ last_status_cksum = 0; // force status update
+ 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();
+ last_status_cksum = 0; // force status update
+ 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;
+#if ENABLE_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 /* 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);
+ 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 separate these two commands. 'f' depends on ';'
+ //
+ //**** fall thru to ... ';'
+ 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;
+#if ENABLE_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
+#if ENABLE_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
+ 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:
+ if (*msg) 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 /* 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
+ }
+ break;
+ case ':': // :- the colon mode commands
+ p = get_input_line((Byte *) ":"); // get input line- use "status line"
+#if ENABLE_FEATURE_VI_COLON
+ colon(p); // execute the command
+#else
+ if (*p == ':')
+ p++; // move past the ':'
+ cnt = strlen((char *) p);
+ if (cnt <= 0)
+ break;
+ if (strncasecmp((char *) p, "quit", cnt) == 0 ||
+ strncasecmp((char *) p, "q!", cnt) == 0) { // delete lines
+ if (file_modified && p[1] != '!') {
+ psbs("No write since last change (:quit! overrides)");
+ } else {
+ editing = 0;
+ }
+ } else if (strncasecmp((char *) p, "write", cnt) == 0
+ || strncasecmp((char *) p, "wq", cnt) == 0
+ || strncasecmp((char *) p, "wn", cnt) == 0
+ || strncasecmp((char *) p, "x", cnt) == 0) {
+ cnt = file_write(cfn, text, end - 1);
+ if (cnt < 0) {
+ if (cnt == -1)
+ psbs("Write error: %s", strerror(errno));
+ } else {
+ file_modified = 0;
+ last_file_modified = -1;
+ psb("\"%s\" %dL, %dC", cfn, count_lines(text, end - 1), cnt);
+ if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n' ||
+ p[0] == 'X' || p[1] == 'Q' || p[1] == 'N') {
+ editing = 0;
+ }
+ }
+ } else if (strncasecmp((char *) p, "file", cnt) == 0 ) {
+ last_status_cksum = 0; // force status update
+ } else if (sscanf((char *) p, "%d", &j) > 0) {
+ dot = find_line(j); // go to line # j
+ dot_skip_over_ws();
+ } else { // unrecognised cmd
+ ni((Byte *) p);
+ }
+#endif /* !FEATURE_VI_COLON */
+ break;
+ case '<': // <- Left shift something
+ case '>': // >- Right shift something
+ cnt = count_lines(text, dot); // remember what line we are on
+ c1 = get_one_char(); // get the type of thing to delete
+ find_range(&p, &q, c1);
+ (void) yank_delete(p, q, 1, YANKONLY); // save copy before change
+ p = begin_line(p);
+ q = end_line(q);
+ i = count_lines(p, q); // # of lines we are shifting
+ for ( ; i > 0; i--, p = next_line(p)) {
+ if (c == '<') {
+ // shift left- remove tab or 8 spaces
+ if (*p == '\t') {
+ // shrink buffer 1 char
+ (void) text_hole_delete(p, p);
+ } else if (*p == ' ') {
+ // we should be calculating columns, not just SPACE
+ for (j = 0; *p == ' ' && j < tabstop; j++) {
+ (void) text_hole_delete(p, p);
+ }
+ }
+ } else if (c == '>') {
+ // shift right -- add tab or 8 spaces
+ (void) char_insert(p, '\t');
+ }
+ }
+ dot = find_line(cnt); // what line were we on
+ dot_skip_over_ws();
+ end_cmd_q(); // stop adding to q
+ break;
+ case 'A': // A- append at e-o-l
+ dot_end(); // go to e-o-l
+ //**** fall thru to ... 'a'
+ case 'a': // a- append after current char
+ if (*dot != '\n')
+ dot++;
+ goto dc_i;
+ break;
+ case 'B': // B- back a blank-delimited Word
+ case 'E': // E- end of a blank-delimited word
+ case 'W': // W- forward a blank-delimited word
+ if (cmdcnt-- > 1) {
+ do_cmd(c);
+ } // repeat cnt
+ dir = FORWARD;
+ if (c == 'B')
+ dir = BACK;
+ if (c == 'W' || isspace(dot[dir])) {
+ dot = skip_thing(dot, 1, dir, S_TO_WS);
+ dot = skip_thing(dot, 2, dir, S_OVER_WS);
+ }
+ if (c != 'W')
+ dot = skip_thing(dot, 1, dir, S_BEFORE_WS);
+ break;
+ case 'C': // C- Change to e-o-l
+ case 'D': // D- delete to e-o-l
+ save_dot = dot;
+ dot = dollar_line(dot); // move to before NL
+ // copy text into a register and delete
+ dot = yank_delete(save_dot, dot, 0, YANKDEL); // delete to e-o-l
+ if (c == 'C')
+ goto dc_i; // start inserting
+#if ENABLE_FEATURE_VI_DOT_CMD
+ if (c == 'D')
+ end_cmd_q(); // stop adding to q
+#endif
+ break;
+ case 'G': // G- goto to a line number (default= E-O-F)
+ dot = end - 1; // assume E-O-F
+ if (cmdcnt > 0) {
+ dot = find_line(cmdcnt); // what line is #cmdcnt
+ }
+ dot_skip_over_ws();
+ break;
+ case 'H': // H- goto top line on screen
+ dot = screenbegin;
+ if (cmdcnt > (rows - 1)) {
+ cmdcnt = (rows - 1);
+ }
+ if (cmdcnt-- > 1) {
+ do_cmd('+');
+ } // repeat cnt
+ dot_skip_over_ws();
+ break;
+ case 'I': // I- insert before first non-blank
+ dot_begin(); // 0
+ dot_skip_over_ws();
+ //**** fall thru to ... 'i'
+ case 'i': // i- insert before current char
+ case VI_K_INSERT: // Cursor Key Insert
+ dc_i:
+ cmd_mode = 1; // start insrting
+ break;
+ case 'J': // J- join current and next lines together
+ if (cmdcnt-- > 2) {
+ do_cmd(c);
+ } // repeat cnt
+ dot_end(); // move to NL
+ if (dot < end - 1) { // make sure not last char in text[]
+ *dot++ = ' '; // replace NL with space
+ file_modified++;
+ while (isblnk(*dot)) { // delete leading WS
+ dot_delete();
+ }
+ }
+ end_cmd_q(); // stop adding to q
+ break;
+ case 'L': // L- goto bottom line on screen
+ dot = end_screen();
+ if (cmdcnt > (rows - 1)) {
+ cmdcnt = (rows - 1);
+ }
+ if (cmdcnt-- > 1) {
+ do_cmd('-');
+ } // repeat cnt
+ dot_begin();
+ dot_skip_over_ws();
+ break;
+ case 'M': // M- goto middle line on screen
+ dot = screenbegin;
+ for (cnt = 0; cnt < (rows-1) / 2; cnt++)
+ dot = next_line(dot);
+ break;
+ case 'O': // O- open a empty line above
+ // 0i\n ESC -i
+ p = begin_line(dot);
+ if (p[-1] == '\n') {
+ dot_prev();
+ case 'o': // o- open a empty line below; Yes, I know it is in the middle of the "if (..."
+ dot_end();
+ dot = char_insert(dot, '\n');
+ } else {
+ dot_begin(); // 0
+ dot = char_insert(dot, '\n'); // i\n ESC
+ dot_prev(); // -
+ }
+ goto dc_i;
+ break;
+ case 'R': // R- continuous Replace char
+ dc5:
+ cmd_mode = 2;
+ break;
+ case 'X': // X- delete char before dot
+ case 'x': // x- delete the current char
+ case 's': // s- substitute the current char
+ if (cmdcnt-- > 1) {
+ do_cmd(c);
+ } // repeat cnt
+ dir = 0;
+ if (c == 'X')
+ dir = -1;
+ if (dot[dir] != '\n') {
+ if (c == 'X')
+ dot--; // delete prev char
+ dot = yank_delete(dot, dot, 0, YANKDEL); // delete char