summaryrefslogtreecommitdiff
path: root/src/getstring.c
diff options
context:
space:
mode:
authorLukas Fleischer <calcurse@cryptocrack.de>2013-04-13 23:31:06 +0200
committerLukas Fleischer <calcurse@cryptocrack.de>2013-04-14 00:19:01 +0200
commit694d28eb78dfad98e2e7ea670d93a153d8efd368 (patch)
tree485dc11d3644d9dadb5fd4d67c5ef8103be79623 /src/getstring.c
parent9907069f442c56c90b67accb2d8fbd046dfce6db (diff)
downloadcalcurse-694d28eb78dfad98e2e7ea670d93a153d8efd368.zip
Use tabs instead of spaces for indentation
This completes our switch to the Linux kernel coding style. Note that we still use deeply nested constructs at some places which need to be fixed up later. Converted using the `Lindent` script from the Linux kernel code base, along with some manual fixes. Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
Diffstat (limited to 'src/getstring.c')
-rw-r--r--src/getstring.c383
1 files changed, 198 insertions, 185 deletions
diff --git a/src/getstring.c b/src/getstring.c
index aedd842..513ad67 100644
--- a/src/getstring.c
+++ b/src/getstring.c
@@ -37,126 +37,134 @@
#include "calcurse.h"
struct getstr_charinfo {
- unsigned int offset, dpyoff;
+ unsigned int offset, dpyoff;
};
struct getstr_status {
- char *s;
- struct getstr_charinfo *ci;
- int pos, len;
- int scrpos;
+ char *s;
+ struct getstr_charinfo *ci;
+ int pos, len;
+ int scrpos;
};
/* 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);
-
- /* print scrolling indicator */
- if (st->scrpos > 0 && st->ci[st->len].dpyoff -
- st->ci[st->scrpos].dpyoff > col - 2)
- c = '*';
- else if (st->scrpos > 0)
- c = '<';
- else if (st->ci[st->len].dpyoff - st->ci[st->scrpos].dpyoff > col - 2)
- 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);
+ char c = 0;
+
+ /* print string */
+ 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 -
+ st->ci[st->scrpos].dpyoff > col - 2)
+ c = '*';
+ else if (st->scrpos > 0)
+ c = '<';
+ else if (st->ci[st->len].dpyoff - st->ci[st->scrpos].dpyoff >
+ col - 2)
+ 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);
}
/* Delete a character at the given position in string. */
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);
-
- 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;
- }
+ 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);
+
+ 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. */
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 i;
-
- 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;
- }
- st->len++;
+ char *str = st->s + st->ci[st->pos].offset;
+ int cl = UTF8_LENGTH(c[0]);
+ int cw = utf8_width(c);
+ int i;
+
+ 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;
+ }
+ st->len++;
}
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;
+ int width;
- st->s = str;
- st->ci = ci;
+ st->s = str;
+ 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 = 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->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;
+ st->pos = st->len;
+ st->scrpos = 0;
}
/* Scroll left/right if the cursor moves outside the window range. */
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->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++;
- }
- }
+ 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->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++;
+ }
+ }
}
/*
@@ -169,114 +177,119 @@ static void getstr_fixscr(struct getstr_status *st)
*/
enum getstr getstring(WINDOW * win, char *str, int l, int x, int y)
{
- struct getstr_status st;
- struct getstr_charinfo ci[l + 1];
-
- int ch, k;
- char c[UTF8_MAXLEN];
-
- 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++;
- }
- }
- }
-
- custom_remove_attr(win, ATTR_HIGHEST);
-
- return st.len == 0 ? GETSTRING_RET : GETSTRING_VALID;
+ struct getstr_status st;
+ struct getstr_charinfo ci[l + 1];
+
+ int ch, k;
+ char c[UTF8_MAXLEN];
+
+ 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++;
+ }
+ }
+ }
+
+ 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 len = strlen(*str);
- char *buf;
- enum getstr ret;
+ 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);
- return ret;
+ mem_free(buf);
+ return ret;
}