+#ifdef BB_FEATURE_VI_OPTIMIZE_CURSOR
+ char cm2[BUFSIZ];
+ Byte *screenp;
+ // char cm3[BUFSIZ];
+ int Rrow= last_row;
+#endif /* BB_FEATURE_VI_OPTIMIZE_CURSOR */
+
+ 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 == FALSE) goto pc0;
+
+#ifdef BB_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, 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 /* BB_FEATURE_VI_OPTIMIZE_CURSOR */
+ pc0:
+ l= strlen(cm);
+ if (l) write(1, cm, l); // move the cursor