Anonymous avatar Anonymous committed 4a831ea

seems like everything works but scrolling, but i've said that before

Comments (0)

Files changed (1)

 		buf[i + delta] = buf[i];
 }
 
-int width(char *buf, int *ind, int crement)
+int width(char *buf, int *ind, int len, int crement)
 {
 	int nb, mask;
 	wchar_t wc;
 
-	for (; (buf[*ind] & 0xc0) != 0xc0; *ind += crement)
+	for (; *ind > 0 && *ind < len  &&
+		(buf[*ind] & 0xc0) != 0xc0 &&
+		(buf[*ind] & 0x80); *ind += crement)
 		;
-	for (nb = 0, mask = 0x80; buf[*ind] & mask; nb++, mask >>= 1)
+	// skip the first, if it's ascii we still want one byte (nb = 1)
+	for (nb = 1, mask = 0x40; buf[*ind] & mask; nb++, mask >>= 1)
 		;
 	if (mbtowc(&wc, buf + *ind, nb) < 0)
 		warnx("failed multibyte to wide char conversion");
 void run()
 {
 	static int escape = 0, wstart = 0, in_mb = 0;
-	int rdlen, i, mask;
+	int rdlen, i, mask, tmp;
 	char c;
 	wchar_t wc;
 	fd_set rd;
 					case '3' : // Delete
 						escape++;
 						if (cur == len) break;
-						for (i = cur + 1; i < len && (usrbuf[i] & 0xc0) == 0x80; i++)
-							;
-						shift_buf(usrbuf, cur + (i - cur), len, -(i - cur));
-						len -= (i - cur);
-						wlen = MAX(0, wlen - 1);
+						tmp = cur + 1;
+						i = width(usrbuf, &tmp, len, 1);
+						memmove(usrbuf + cur, usrbuf + tmp, len - tmp);
+						len -= tmp - cur;
+						wlen = MAX(0, wlen - i);
 						break;
 					case '7' : // Home
 						cur = wcur = 0;
 						escape++;
 						break;
 					case 'C' : // Right arrow
-						i = width(usrbuf, &cur, 1);
+						cur = MIN(cur + 1, len);
+						i = width(usrbuf, &cur, len, 1);
 						wcur = MIN(wcur + i, wlen);
 						break;
 					case 'D' : // Left Arrow
-						i = width(usrbuf, &cur, -1);
+						cur = MAX(cur - 1, 0);
+						i = width(usrbuf, &cur, len, -1);
 						wcur = MAX(wcur - i, 0);
 						break;
 					default  : escape++;
 						break;
 					case BACKSPACE :
 					case DEL :
-						for (i = cur - 1; i >= 0 && (usrbuf[i] & 0xc0) == 0x80; i--)
-							;
-						if (cur != len)
-							shift_buf(usrbuf, cur, len, cur - i);
-						if (mbtowc(&wc, usrbuf + i, cur - i) < 0)
-							warnx("failed mbtowc conversion");
-						len = MAX(0, len - (cur - i));
-						cur = MAX(0, i);
-						if ((i = wcwidth(wc)) < 0)
-							warnx("failed wcwidth");
+						tmp = cur;
+						cur = MAX(cur - 1, 0);
+						i = width(usrbuf, &cur, len, -1);
 						wlen = MAX(0, wlen - i);
 						wcur = MAX(0, wcur - i);
+						if (tmp != len)
+							memmove(usrbuf + cur, usrbuf + tmp, len - tmp);
+						len = MAX(0, len - (tmp - cur));
 						break;
 					case CTRL('a') : cur = wcur = 0;          break;
 					case CTRL('e') : cur = len; wcur = wlen;  break;
 						    wcur--;
 							wlen--;
 						}
-						for (; i >=0 && !strchr(word_separators, usrbuf[i]); i--) {
-						    if ((usrbuf[i] & 0xc0) == 0xc0 || !(usrbuf[i] & 0x80)) {
-								wcur--;
-								wlen--;
-							}
+						for (tmp = width(usrbuf, &i, len, -1);
+							 !strchr(word_separators, usrbuf[i]);
+							 i--, tmp = width(usrbuf, &i, len, -1)) {
+							wcur -= tmp;
+							wlen -= tmp;
 						}
 						i++;
-						shift_buf(usrbuf, cur, len, -(cur - i));
+						memmove(usrbuf + i, usrbuf + cur, len - cur);
 						len -= cur - i;
 						cur = i;
 						break;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.