factor out common code (convert from "byte offset from cursor" to number of characters)

This commit is contained in:
tg 2012-07-20 20:33:15 +00:00
parent 98ff25a001
commit bea2b756b3

36
edit.c
View File

@ -28,7 +28,7 @@
#ifndef MKSH_NO_CMDLINE_EDITING #ifndef MKSH_NO_CMDLINE_EDITING
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.242 2012/07/20 20:16:26 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/edit.c,v 1.243 2012/07/20 20:33:15 tg Exp $");
/* /*
* in later versions we might use libtermcap for this, but since external * in later versions we might use libtermcap for this, but since external
@ -942,7 +942,7 @@ static bool prompt_redraw; /* false if newline forced after prompt */
static int x_ins(const char *); static int x_ins(const char *);
static void x_delete(int, int); static void x_delete(int, int);
static int x_bword(void); static int x_bword(void);
static int x_fword(int); static int x_fword(bool);
static void x_goto(char *); static void x_goto(char *);
static void x_bs3(char **); static void x_bs3(char **);
static int x_size_str(char *); static int x_size_str(char *);
@ -974,6 +974,7 @@ static int x_fold_case(int);
#endif #endif
static char *x_lastcp(void); static char *x_lastcp(void);
static void do_complete(int, Comp_type); static void do_complete(int, Comp_type);
static int x_nb2nc(int);
static int unget_char = -1; static int unget_char = -1;
@ -1092,6 +1093,18 @@ static struct x_defbindings const x_defbindings[] = {
#endif #endif
}; };
/* want size_t, not int */
static int
x_nb2nc(int nb)
{
char *cp;
size_t nc = 0;
for (cp = xcp; cp < (xcp + nb); ++nc)
cp += utf_ptradj(cp);
return (nc);
}
static void static void
x_modified(void) x_modified(void)
{ {
@ -1472,21 +1485,21 @@ x_mv_bword(int c MKSH_A_UNUSED)
static int static int
x_mv_fword(int c MKSH_A_UNUSED) x_mv_fword(int c MKSH_A_UNUSED)
{ {
x_fword(1); x_fword(true);
return (KSTD); return (KSTD);
} }
static int static int
x_del_fword(int c MKSH_A_UNUSED) x_del_fword(int c MKSH_A_UNUSED)
{ {
x_delete(x_fword(0), true); x_delete(x_fword(false), true);
return (KSTD); return (KSTD);
} }
static int static int
x_bword(void) x_bword(void)
{ {
int nc = 0, nb = 0; int nb = 0;
char *cp = xcp; char *cp = xcp;
if (cp == xbuf) { if (cp == xbuf) {
@ -1504,16 +1517,14 @@ x_bword(void)
} }
} }
x_goto(cp); x_goto(cp);
for (cp = xcp; cp < (xcp + nb); ++nc) return (x_nb2nc(nb));
cp += utf_ptradj(cp);
return (nc);
} }
static int static int
x_fword(int move) x_fword(bool move)
{ {
int nc = 0; int nc;
char *cp = xcp, *cp2; char *cp = xcp;
if (cp == xep) { if (cp == xep) {
x_e_putc2(7); x_e_putc2(7);
@ -1525,8 +1536,7 @@ x_fword(int move)
while (cp != xep && !is_mfs(*cp)) while (cp != xep && !is_mfs(*cp))
cp++; cp++;
} }
for (cp2 = xcp; cp2 < cp; ++nc) nc = x_nb2nc(cp - xcp);
cp2 += utf_ptradj(cp2);
if (move) if (move)
x_goto(cp); x_goto(cp);
return (nc); return (nc);