Make getstring() UTF-8 compatible
Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
This commit is contained in:
parent
edc20ba443
commit
39d2671495
174
src/getstring.c
174
src/getstring.c
@ -36,50 +36,80 @@
|
|||||||
|
|
||||||
#include "calcurse.h"
|
#include "calcurse.h"
|
||||||
|
|
||||||
|
struct getstr_charinfo {
|
||||||
|
unsigned int offset, dpyoff;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct getstr_status {
|
||||||
|
char *s;
|
||||||
|
struct getstr_charinfo *ci;
|
||||||
|
int pos, len;
|
||||||
|
int scrpos;
|
||||||
|
};
|
||||||
|
|
||||||
/* Print the string at the desired position. */
|
/* Print the string at the desired position. */
|
||||||
static void
|
static void
|
||||||
showstring (WINDOW *win, int x, int y, char *str, int len, int scroff,
|
showstring (WINDOW *win, int x, int y, struct getstr_status *st)
|
||||||
int curpos)
|
|
||||||
{
|
{
|
||||||
char c = 0;
|
char c = 0;
|
||||||
|
|
||||||
/* print string */
|
/* print string */
|
||||||
mvwaddnstr (win, y, x, &str[scroff], -1);
|
mvwaddnstr (win, y, x, &st->s[st->ci[st->scrpos].offset], -1);
|
||||||
wclrtoeol (win);
|
wclrtoeol (win);
|
||||||
|
|
||||||
/* print scrolling indicator */
|
/* print scrolling indicator */
|
||||||
if (scroff > 0 && scroff < len - col)
|
if (st->scrpos > 0 && st->ci[st->len].dpyoff -
|
||||||
|
st->ci[st->scrpos].dpyoff > col - 2)
|
||||||
c = '*';
|
c = '*';
|
||||||
else if (scroff > 0)
|
else if (st->scrpos > 0)
|
||||||
c = '<';
|
c = '<';
|
||||||
else if (scroff < len - col)
|
else if (st->ci[st->len].dpyoff - st->ci[st->scrpos].dpyoff > col - 2)
|
||||||
c = '>';
|
c = '>';
|
||||||
mvwprintw (win, y, col - 1, "%c", c);
|
mvwprintw (win, y, col - 2, " %c", c);
|
||||||
|
|
||||||
/* print cursor */
|
/* print cursor */
|
||||||
wmove (win, y, curpos - scroff);
|
wmove (win, y, st->ci[st->pos].dpyoff - st->ci[st->scrpos].dpyoff);
|
||||||
|
wchgat (win, 1, A_REVERSE, COLR_CUSTOM, NULL);
|
||||||
if (curpos >= len)
|
|
||||||
waddch (win, SPACE | A_REVERSE);
|
|
||||||
else
|
|
||||||
waddch (win, str[curpos] | A_REVERSE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Delete a character at the given position in string. */
|
/* Delete a character at the given position in string. */
|
||||||
static void
|
static void
|
||||||
del_char (int pos, char *str)
|
del_char (struct getstr_status *st)
|
||||||
{
|
{
|
||||||
str += pos;
|
char *str = st->s + st->ci[st->pos].offset;
|
||||||
memmove (str, str + 1, strlen (str) + 1);
|
int cl = st->ci[st->pos + 1].offset - st->ci[st->pos].offset;
|
||||||
|
int cw = st->ci[st->pos + 1].dpyoff - st->ci[st->pos].dpyoff;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
memmove (str, str + cl, strlen (str) + 1);
|
||||||
|
|
||||||
|
st->len--;
|
||||||
|
for (i = st->pos; i <= st->len; i++)
|
||||||
|
{
|
||||||
|
st->ci[i].offset = st->ci[i + 1].offset - cl;
|
||||||
|
st->ci[i].dpyoff = st->ci[i + 1].dpyoff - cw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add a character at the given position in string. */
|
/* Add a character at the given position in string. */
|
||||||
static void
|
static void
|
||||||
ins_char (int pos, int ch, char *str)
|
ins_char (struct getstr_status *st, char *c)
|
||||||
{
|
{
|
||||||
str += pos;
|
char *str = st->s + st->ci[st->pos].offset;
|
||||||
memmove (str + 1, str, strlen (str) + 1);
|
int cl = UTF8_LENGTH (c[0]);
|
||||||
*str = ch;
|
int cw = utf8_width (c);
|
||||||
|
int i;
|
||||||
|
|
||||||
|
memmove (str + cl, str, strlen (str) + 1);
|
||||||
|
for (i = 0; c[i]; i++, str++)
|
||||||
|
*str = c[i];
|
||||||
|
|
||||||
|
for (i = st->len; i >= st->pos; i--)
|
||||||
|
{
|
||||||
|
st->ci[i + 1].offset = st->ci[i].offset + cl;
|
||||||
|
st->ci[i + 1].dpyoff = st->ci[i].dpyoff + cw;
|
||||||
|
}
|
||||||
|
st->len++;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -100,21 +130,57 @@ enum getstr
|
|||||||
getstring (WINDOW *win, char *str, int l, int x, int y)
|
getstring (WINDOW *win, char *str, int l, int x, int y)
|
||||||
{
|
{
|
||||||
const int pgsize = col / 3;
|
const int pgsize = col / 3;
|
||||||
|
int pgskip;
|
||||||
|
|
||||||
int len = strlen (str);
|
struct getstr_status st;
|
||||||
int curpos = len;
|
struct getstr_charinfo ci[l + 1];
|
||||||
int scroff = 0;
|
|
||||||
int ch;
|
int width;
|
||||||
|
int ch, k;
|
||||||
|
char c[6];
|
||||||
|
|
||||||
|
st.s = str;
|
||||||
|
st.ci = ci;
|
||||||
|
st.len = 0;
|
||||||
|
width = 0;
|
||||||
|
while (*str)
|
||||||
|
{
|
||||||
|
st.ci[st.len].offset = str - st.s;
|
||||||
|
st.ci[st.len].dpyoff = width;
|
||||||
|
|
||||||
|
st.len++;
|
||||||
|
width += utf8_width (str);
|
||||||
|
str += UTF8_LENGTH (*str);
|
||||||
|
}
|
||||||
|
st.ci[st.len].offset = str - st.s;
|
||||||
|
st.ci[st.len].dpyoff = width;
|
||||||
|
|
||||||
|
st.pos = st.len;
|
||||||
|
st.scrpos = 0;
|
||||||
|
|
||||||
custom_apply_attr (win, ATTR_HIGHEST);
|
custom_apply_attr (win, ATTR_HIGHEST);
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
while (curpos < scroff)
|
while (st.pos < st.scrpos)
|
||||||
scroff -= pgsize;
|
{
|
||||||
while (curpos >= scroff + col - 1)
|
pgskip = 0;
|
||||||
scroff += pgsize;
|
while (pgskip < pgsize)
|
||||||
|
{
|
||||||
|
st.scrpos--;
|
||||||
|
pgskip += st.ci[st.scrpos + 1].dpyoff - st.ci[st.scrpos].dpyoff;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (st.ci[st.pos].dpyoff - st.ci[st.scrpos].dpyoff > col - 2)
|
||||||
|
{
|
||||||
|
pgskip = 0;
|
||||||
|
while (pgskip < pgsize)
|
||||||
|
{
|
||||||
|
pgskip += st.ci[st.scrpos + 1].dpyoff - st.ci[st.scrpos].dpyoff;
|
||||||
|
st.scrpos++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
showstring (win, x, y, str, len, scroff, curpos);
|
showstring (win, x, y, &st);
|
||||||
wins_doupdate ();
|
wins_doupdate ();
|
||||||
|
|
||||||
if ((ch = wgetch (win)) == '\n') break;
|
if ((ch = wgetch (win)) == '\n') break;
|
||||||
@ -124,72 +190,72 @@ getstring (WINDOW *win, char *str, int l, int x, int y)
|
|||||||
case 330:
|
case 330:
|
||||||
case 127:
|
case 127:
|
||||||
case CTRL ('H'):
|
case CTRL ('H'):
|
||||||
if (curpos > 0)
|
if (st.pos > 0)
|
||||||
{
|
{
|
||||||
del_char ((--curpos), str);
|
st.pos--;
|
||||||
len--;
|
del_char (&st);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
bell ();
|
bell ();
|
||||||
break;
|
break;
|
||||||
case CTRL ('D'): /* delete next character */
|
case CTRL ('D'): /* delete next character */
|
||||||
if (curpos < len)
|
if (st.pos < st.len)
|
||||||
{
|
del_char (&st);
|
||||||
del_char (curpos, str);
|
|
||||||
len--;
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
bell ();
|
bell ();
|
||||||
break;
|
break;
|
||||||
case CTRL ('W'): /* delete a word */
|
case CTRL ('W'): /* delete a word */
|
||||||
if (curpos > 0) {
|
if (st.pos > 0) {
|
||||||
while (curpos && str[curpos - 1] == ' ')
|
while (st.pos && st.s[st.ci[st.pos - 1].offset] == ' ')
|
||||||
{
|
{
|
||||||
del_char ((--curpos), str);
|
st.pos--;
|
||||||
len--;
|
del_char (&st);
|
||||||
}
|
}
|
||||||
while (curpos && str[curpos - 1] != ' ')
|
while (st.pos && st.s[st.ci[st.pos - 1].offset] != ' ')
|
||||||
{
|
{
|
||||||
del_char ((--curpos), str);
|
st.pos--;
|
||||||
len--;
|
del_char (&st);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
bell ();
|
bell ();
|
||||||
break;
|
break;
|
||||||
case CTRL ('K'): /* delete to end-of-line */
|
case CTRL ('K'): /* delete to end-of-line */
|
||||||
str[curpos] = 0;
|
st.s[st.ci[st.pos].offset] = 0;
|
||||||
len = curpos;
|
st.len = st.pos;
|
||||||
break;
|
break;
|
||||||
case CTRL ('A'): /* go to begginning of string */
|
case CTRL ('A'): /* go to begginning of string */
|
||||||
curpos = 0;
|
st.pos = 0;
|
||||||
break;
|
break;
|
||||||
case CTRL ('E'): /* go to end of string */
|
case CTRL ('E'): /* go to end of string */
|
||||||
curpos = len;
|
st.pos = st.len;
|
||||||
break;
|
break;
|
||||||
case KEY_LEFT: /* move one char backward */
|
case KEY_LEFT: /* move one char backward */
|
||||||
case CTRL ('B'):
|
case CTRL ('B'):
|
||||||
if (curpos > 0) curpos--;
|
if (st.pos > 0) st.pos--;
|
||||||
break;
|
break;
|
||||||
case KEY_RIGHT: /* move one char forward */
|
case KEY_RIGHT: /* move one char forward */
|
||||||
case CTRL ('F'):
|
case CTRL ('F'):
|
||||||
if (curpos < len) curpos++;
|
if (st.pos < st.len) st.pos++;
|
||||||
break;
|
break;
|
||||||
case ESCAPE: /* cancel editing */
|
case ESCAPE: /* cancel editing */
|
||||||
return (GETSTRING_ESC);
|
return (GETSTRING_ESC);
|
||||||
break;
|
break;
|
||||||
default: /* insert one character */
|
default: /* insert one character */
|
||||||
if (len < l - 1)
|
for (c[0] = ch, k = 1; k < MIN (UTF8_LENGTH (c[0]), 6); k++)
|
||||||
|
c[k] = (unsigned char)wgetch (win);
|
||||||
|
c[k] = 0;
|
||||||
|
if (st.ci[st.len].offset + k < l)
|
||||||
{
|
{
|
||||||
ins_char ((curpos++), ch, str);
|
ins_char (&st, c);
|
||||||
len++;
|
st.pos++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
custom_remove_attr (win, ATTR_HIGHEST);
|
custom_remove_attr (win, ATTR_HIGHEST);
|
||||||
|
|
||||||
return (len == 0 ? GETSTRING_RET : GETSTRING_VALID);
|
return (st.len == 0 ? GETSTRING_RET : GETSTRING_VALID);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Update an already existing string. */
|
/* Update an already existing string. */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user