diff options
Diffstat (limited to 'src/getstring.c')
-rw-r--r-- | src/getstring.c | 275 |
1 files changed, 128 insertions, 147 deletions
diff --git a/src/getstring.c b/src/getstring.c index 045af68..cc2a949 100644 --- a/src/getstring.c +++ b/src/getstring.c @@ -48,14 +48,13 @@ struct getstr_status { }; /* Print the string at the desired position. */ -static void -getstr_print (WINDOW *win, int x, int y, struct getstr_status *st) +static void getstr_print(WINDOW * win, int x, int y, struct getstr_status *st) { char c = 0; /* print string */ - mvwaddnstr (win, y, x, &st->s[st->ci[st->scrpos].offset], -1); - wclrtoeol (win); + mvwaddnstr(win, y, x, &st->s[st->ci[st->scrpos].offset], -1); + wclrtoeol(win); /* print scrolling indicator */ if (st->scrpos > 0 && st->ci[st->len].dpyoff - @@ -65,62 +64,57 @@ getstr_print (WINDOW *win, int x, int y, struct getstr_status *st) c = '<'; else if (st->ci[st->len].dpyoff - st->ci[st->scrpos].dpyoff > col - 2) c = '>'; - mvwprintw (win, y, col - 2, " %c", c); + mvwprintw(win, y, col - 2, " %c", c); /* print cursor */ - wmove (win, y, st->ci[st->pos].dpyoff - st->ci[st->scrpos].dpyoff); - wchgat (win, 1, A_REVERSE, COLR_CUSTOM, NULL); + wmove(win, y, st->ci[st->pos].dpyoff - st->ci[st->scrpos].dpyoff); + wchgat(win, 1, A_REVERSE, COLR_CUSTOM, NULL); } /* Delete a character at the given position in string. */ -static void -getstr_del_char (struct getstr_status *st) +static void getstr_del_char(struct getstr_status *st) { char *str = st->s + st->ci[st->pos].offset; 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); + 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; - } + 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. */ -static void -getstr_ins_char (struct getstr_status *st, char *c) +static void getstr_ins_char(struct getstr_status *st, char *c) { char *str = st->s + st->ci[st->pos].offset; - int cl = UTF8_LENGTH (c[0]); - int cw = utf8_width (c); + int cl = UTF8_LENGTH(c[0]); + int cw = utf8_width(c); int i; - memmove (str + cl, str, strlen (str) + 1); + memmove(str + cl, str, strlen(str) + 1); for (i = 0; i < cl; 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; - } + 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 -bell (void) +static void bell(void) { - putchar ('\a'); + putchar('\a'); } /* Initialize getstring data structure. */ static void -getstr_init (struct getstr_status *st, char *str, struct getstr_charinfo *ci) +getstr_init(struct getstr_status *st, char *str, struct getstr_charinfo *ci) { int width; @@ -128,15 +122,14 @@ getstr_init (struct getstr_status *st, char *str, struct getstr_charinfo *ci) st->ci = ci; st->len = 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); - } + 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; @@ -145,30 +138,25 @@ getstr_init (struct getstr_status *st, char *str, struct getstr_charinfo *ci) } /* Scroll left/right if the cursor moves outside the window range. */ -static void -getstr_fixscr (struct getstr_status *st) +static void getstr_fixscr(struct getstr_status *st) { const int pgsize = col / 3; int pgskip; - while (st->pos < st->scrpos) - { - pgskip = 0; - while (pgskip < pgsize && st->scrpos > 0) - { - st->scrpos--; - pgskip += st->ci[st->scrpos + 1].dpyoff - st->ci[st->scrpos].dpyoff; - } + while (st->pos < st->scrpos) { + pgskip = 0; + while (pgskip < pgsize && st->scrpos > 0) { + 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 && st->scrpos < st->len) - { - pgskip += st->ci[st->scrpos + 1].dpyoff - st->ci[st->scrpos].dpyoff; - st->scrpos++; - } + } + while (st->ci[st->pos].dpyoff - st->ci[st->scrpos].dpyoff > col - 2) { + pgskip = 0; + while (pgskip < pgsize && st->scrpos < st->len) { + pgskip += st->ci[st->scrpos + 1].dpyoff - st->ci[st->scrpos].dpyoff; + st->scrpos++; } + } } /* @@ -179,8 +167,7 @@ getstr_fixscr (struct getstr_status *st) * environment, otherwise the cursor would move from place to place without * control. */ -enum getstr -getstring (WINDOW *win, char *str, int l, int x, int y) +enum getstr getstring(WINDOW * win, char *str, int l, int x, int y) { struct getstr_status st; struct getstr_charinfo ci[l + 1]; @@ -188,112 +175,106 @@ getstring (WINDOW *win, char *str, int l, int x, int y) int ch, k; char c[UTF8_MAXLEN]; - getstr_init (&st, str, ci); - custom_apply_attr (win, ATTR_HIGHEST); + getstr_init(&st, str, ci); + custom_apply_attr(win, ATTR_HIGHEST); for (;;) { - getstr_fixscr (&st); - getstr_print (win, x, y, &st); - wins_doupdate (); - - if ((ch = wgetch (win)) == '\n') break; - switch (ch) - { - case KEY_BACKSPACE: /* delete one character */ - case 330: - case 127: - case CTRL ('H'): - if (st.pos > 0) - { - st.pos--; - getstr_del_char (&st); - } - else - bell (); - break; - case CTRL ('D'): /* delete next character */ - if (st.pos < st.len) - getstr_del_char (&st); - else - bell (); - break; - case CTRL ('W'): /* delete a word */ - if (st.pos > 0) { - while (st.pos && st.s[st.ci[st.pos - 1].offset] == ' ') - { - st.pos--; - getstr_del_char (&st); - } - while (st.pos && st.s[st.ci[st.pos - 1].offset] != ' ') - { - st.pos--; - getstr_del_char (&st); - } - } - else - bell (); - break; - case CTRL ('K'): /* delete to end-of-line */ - st.s[st.ci[st.pos].offset] = 0; - st.len = st.pos; - break; - case CTRL ('A'): /* go to begginning of string */ - st.pos = 0; - break; - case CTRL ('E'): /* go to end of string */ - st.pos = st.len; - break; - case KEY_LEFT: /* move one char backward */ - case CTRL ('B'): - if (st.pos > 0) st.pos--; - break; - case KEY_RIGHT: /* move one char forward */ - case CTRL ('F'): - if (st.pos < st.len) st.pos++; - break; - case ESCAPE: /* cancel editing */ - return GETSTRING_ESC; - break; - default: /* insert one character */ - c[0] = ch; - for (k = 1; k < MIN (UTF8_LENGTH (c[0]), UTF8_MAXLEN); k++) - c[k] = (unsigned char)wgetch (win); - if (st.ci[st.len].offset + k < l) - { - getstr_ins_char (&st, c); - st.pos++; - } + getstr_fixscr(&st); + getstr_print(win, x, y, &st); + wins_doupdate(); + + if ((ch = wgetch(win)) == '\n') + break; + switch (ch) { + case KEY_BACKSPACE: /* delete one character */ + case 330: + case 127: + case CTRL('H'): + if (st.pos > 0) { + st.pos--; + getstr_del_char(&st); + } else + bell(); + break; + case CTRL('D'): /* delete next character */ + if (st.pos < st.len) + getstr_del_char(&st); + else + bell(); + break; + case CTRL('W'): /* delete a word */ + if (st.pos > 0) { + while (st.pos && st.s[st.ci[st.pos - 1].offset] == ' ') { + st.pos--; + getstr_del_char(&st); + } + while (st.pos && st.s[st.ci[st.pos - 1].offset] != ' ') { + st.pos--; + getstr_del_char(&st); + } + } else + bell(); + break; + case CTRL('K'): /* delete to end-of-line */ + st.s[st.ci[st.pos].offset] = 0; + st.len = st.pos; + break; + case CTRL('A'): /* go to begginning of string */ + st.pos = 0; + break; + case CTRL('E'): /* go to end of string */ + st.pos = st.len; + break; + case KEY_LEFT: /* move one char backward */ + case CTRL('B'): + if (st.pos > 0) + st.pos--; + break; + case KEY_RIGHT: /* move one char forward */ + case CTRL('F'): + if (st.pos < st.len) + st.pos++; + break; + case ESCAPE: /* cancel editing */ + return GETSTRING_ESC; + break; + default: /* insert one character */ + c[0] = ch; + for (k = 1; k < MIN(UTF8_LENGTH(c[0]), UTF8_MAXLEN); k++) + c[k] = (unsigned char)wgetch(win); + if (st.ci[st.len].offset + k < l) { + getstr_ins_char(&st, c); + st.pos++; } + } } - custom_remove_attr (win, ATTR_HIGHEST); + custom_remove_attr(win, ATTR_HIGHEST); return st.len == 0 ? GETSTRING_RET : GETSTRING_VALID; } /* Update an already existing string. */ -int -updatestring (WINDOW *win, char **str, int x, int y) +int updatestring(WINDOW * win, char **str, int x, int y) { - int len = strlen (*str); + int len = strlen(*str); char *buf; enum getstr ret; - EXIT_IF (len + 1 > BUFSIZ, _("Internal error: line too long")); + EXIT_IF(len + 1 > BUFSIZ, _("Internal error: line too long")); - buf = mem_malloc (BUFSIZ); - memcpy (buf, *str, len + 1); + buf = mem_malloc(BUFSIZ); + memcpy(buf, *str, len + 1); - ret = getstring (win, buf, BUFSIZ, x, y); + ret = getstring(win, buf, BUFSIZ, x, y); - if (ret == GETSTRING_VALID) - { - len = strlen (buf); - *str = mem_realloc (*str, len + 1, 1); - EXIT_IF (*str == NULL, _("out of memory")); - memcpy (*str, buf, len + 1); - } + if (ret == GETSTRING_VALID) { + len = strlen(buf); + *str = mem_realloc(*str, len + 1, 1); + EXIT_IF(*str == NULL, _("out of memory")); + memcpy(*str, buf, len + 1); + } - mem_free (buf); + mem_free(buf); return ret; } |