Commits

Anonymous committed 457a234

such a pain in the

  • Participants
  • Parent commits 4f44bbf
  • Branches utf8

Comments (0)

Files changed (1)

 		buf[i + delta] = buf[i];
 }
 
+int width(char *buf, int *ind, int crement)
+{
+	int nb, mask;
+	wchar_t wc;
+
+	for (; (buf[*ind] & 0xc0) != 0xc0; *ind += crement)
+		;
+	for (nb = 0, mask = 0x80; buf[*ind] & mask; nb++, mask >>= 1)
+		;
+	if (mbtowc(&wc, buf + *ind, nb) < 0)
+		warnx("failed multibyte to wide char conversion");
+	if ((nb = wcwidth(wc)) < 0)
+		warnx("failed to find width of wide char");
+
+	return nb;
+}
+
 void run()
 {
 	static int escape = 0, wstart = 0, in_mb = 0;
 						escape++;
 						break;
 					case 'C' : // Right arrow
-						for (i = cur + 1; i < len && (usrbuf[i] & 0xc0) == 0x80; i++)
-							;
-						cur = i;
-						wcur = MIN(wcur + 1, wlen);
+						i = width(usrbuf, &cur, 1);
+						wcur = MIN(wcur + i, wlen);
 						break;
 					case 'D' : // Left Arrow
-						for (i = cur - 1; i > 0 && (usrbuf[i] & 0xc0) == 0x80; i--)
-							;
-						cur = i;
-						wcur = MAX(wcur - 1, 0);
+						i = width(usrbuf, &cur, -1);
+						wcur = MAX(wcur - i, 0);
 						break;
 					default  : escape++;
 				}