aboutsummaryrefslogtreecommitdiff
path: root/textbox.c
diff options
context:
space:
mode:
Diffstat (limited to 'textbox.c')
-rw-r--r--textbox.c142
1 files changed, 81 insertions, 61 deletions
diff --git a/textbox.c b/textbox.c
index eb06eecf3721..ed1405097e74 100644
--- a/textbox.c
+++ b/textbox.c
@@ -1,9 +1,9 @@
/*
- * $Id: textbox.c,v 1.117 2018/06/19 22:57:01 tom Exp $
+ * $Id: textbox.c,v 1.127 2021/01/17 22:19:19 tom Exp $
*
* textbox.c -- implements the text box
*
- * Copyright 2000-2017,2018 Thomas E. Dickey
+ * Copyright 2000-2020,2021 Thomas E. Dickey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License, version 2.1
@@ -127,14 +127,14 @@ xalloc(size_t size)
static void
read_high(MY_OBJ * obj, size_t size_read)
{
- char *buftab, ch;
- int i = 0, j, n, tmpint;
- long begin_line;
+ char *buftab;
/* Allocate space for read buffer */
buftab = xalloc(size_read + 1);
if ((obj->fd_bytes_read = read(obj->fd, buftab, size_read)) != -1) {
+ int j;
+ long begin_line;
buftab[obj->fd_bytes_read] = '\0'; /* mark end of valid data */
@@ -179,19 +179,28 @@ read_high(MY_OBJ * obj, size_t size_read)
j = 0;
begin_line = 0;
- while (j < obj->fd_bytes_read)
- if (((ch = buftab[j++]) == TAB) && (dialog_vars.tab_correct != 0)) {
- tmpint = (dialog_state.tab_len
- - ((int) ((long) i - begin_line) % dialog_state.tab_len));
- for (n = 0; n < tmpint; n++)
- obj->buf[i++] = ' ';
- } else {
- if (ch == '\n')
- begin_line = i + 1;
- obj->buf[i++] = ch;
+ if (obj->buf != NULL) {
+ int i = 0;
+
+ while (j < obj->fd_bytes_read) {
+ char ch;
+
+ if (((ch = buftab[j++]) == TAB)
+ && (dialog_vars.tab_correct != 0)) {
+ int n;
+ int tmpint = (dialog_state.tab_len
+ - ((int) ((long) i - begin_line) % dialog_state.tab_len));
+ for (n = 0; n < tmpint; n++)
+ obj->buf[i++] = ' ';
+ } else {
+ if (ch == '\n')
+ begin_line = i + 1;
+ obj->buf[i++] = ch;
+ }
}
- obj->buf[i] = '\0'; /* mark end of valid data */
+ obj->buf[i] = '\0'; /* mark end of valid data */
+ }
}
if (obj->bytes_read == -1)
@@ -202,17 +211,19 @@ read_high(MY_OBJ * obj, size_t size_read)
static long
find_first(MY_OBJ * obj, char *buffer, long length)
{
- long recount = obj->page_length;
long result = 0;
- while (length > 0) {
- if (buffer[length] == '\n') {
- if (--recount < 0) {
- result = length;
- break;
+ if (buffer != NULL) {
+ long recount = obj->page_length;
+ while (length > 0) {
+ if (buffer[length] == '\n') {
+ if (--recount < 0) {
+ result = length;
+ break;
+ }
}
+ --length;
}
- --length;
}
return result;
}
@@ -269,30 +280,31 @@ static char *
get_line(MY_OBJ * obj)
{
int i = 0;
- long fpos;
obj->end_reached = FALSE;
- while (obj->buf[obj->in_buf] != '\n') {
- if (obj->buf[obj->in_buf] == '\0') { /* Either end of file or end of buffer reached */
- fpos = ftell_obj(obj);
-
- if (fpos < obj->file_size) { /* Not end of file yet */
- /* We've reached end of buffer, but not end of file yet, so
- * read next part of file into buffer
- */
- read_high(obj, BUF_SIZE);
- obj->in_buf = 0;
- } else {
- if (!obj->end_reached)
- obj->end_reached = TRUE;
- break;
+ if (obj->buf != NULL) {
+ while (obj->buf[obj->in_buf] != '\n') {
+ if (obj->buf[obj->in_buf] == '\0') { /* Either end of file or end of buffer reached */
+ long fpos = ftell_obj(obj);
+
+ if (fpos < obj->file_size) { /* Not end of file yet */
+ /* We've reached end of buffer, but not end of file yet, so
+ * read next part of file into buffer
+ */
+ read_high(obj, BUF_SIZE);
+ obj->in_buf = 0;
+ } else {
+ if (!obj->end_reached)
+ obj->end_reached = TRUE;
+ break;
+ }
+ } else if (i < MAX_LEN)
+ obj->line[i++] = obj->buf[obj->in_buf++];
+ else {
+ if (i == MAX_LEN) /* Truncate lines longer than MAX_LEN characters */
+ obj->line[i++] = '\0';
+ obj->in_buf++;
}
- } else if (i < MAX_LEN)
- obj->line[i++] = obj->buf[obj->in_buf++];
- else {
- if (i == MAX_LEN) /* Truncate lines longer than MAX_LEN characters */
- obj->line[i++] = '\0';
- obj->in_buf++;
}
}
if (i <= MAX_LEN)
@@ -357,7 +369,10 @@ back_lines(MY_OBJ * obj, long n)
}
}
obj->in_buf--;
- if (obj->buf[obj->in_buf] != '\n')
+ if (obj->buf == NULL
+ || obj->in_buf < 0
+ || obj->in_buf >= obj->bytes_read
+ || obj->buf[obj->in_buf] != '\n')
/* Something's wrong... */
dlg_exiterr("Internal error in back_lines().");
}
@@ -571,16 +586,17 @@ static bool
perform_search(MY_OBJ * obj, int height, int width, int key, char *search_term)
{
int dir;
- long tempinx;
- long fpos;
- int result;
- bool found;
- bool temp, temp1;
bool moved = FALSE;
/* set search direction */
dir = (key == '/' || key == 'n') ? 1 : 0;
if (dir ? !obj->end_reached : !obj->begin_reached) {
+ long tempinx;
+ long fpos;
+ int result;
+ bool found;
+ bool temp, temp1;
+
if (key == 'n' || key == 'N') {
if (search_term[0] == '\0') { /* No search term yet */
(void) beep();
@@ -597,6 +613,8 @@ perform_search(MY_OBJ * obj, int height, int width, int key, char *search_term)
ungetch(KEY_RESIZE);
/* FALLTHRU */
}
+#else
+ (void) result;
#endif
/* ESC pressed, or no search term, reprint page to clear box */
dlg_attrset(obj->text, dialog_attr);
@@ -698,9 +716,9 @@ dialog_textbox(const char *title, const char *filename, int height, int width)
#endif
long fpos;
int x, y, cur_x, cur_y;
- int key = 0, fkey;
+ int key, fkey;
int next = 0;
- int i, code, passed_end;
+ int i, passed_end;
char search_term[MAX_LEN + 1];
MY_OBJ obj;
WINDOW *dialog;
@@ -773,6 +791,7 @@ dialog_textbox(const char *title, const char *filename, int height, int width)
dlg_attr_clear(obj.text, PAGE_LENGTH, PAGE_WIDTH, dialog_attr);
while (result == DLG_EXIT_UNKNOWN) {
+ int code;
/*
* Update the screen according to whether we shifted up/down by a line
@@ -820,8 +839,10 @@ dialog_textbox(const char *title, const char *filename, int height, int width)
next = 0; /* ...but not scroll by a line */
key = dlg_mouse_wgetch(dialog, &fkey);
- if (dlg_result_key(key, fkey, &result))
- break;
+ if (dlg_result_key(key, fkey, &result)) {
+ if (!dlg_ok_button_key(result, &button, &key, &fkey))
+ break;
+ }
if (!fkey && (code = dlg_char_to_button(key, obj.buttons)) >= 0) {
result = dlg_ok_buttoncode(code);
@@ -858,10 +879,10 @@ dialog_textbox(const char *title, const char *filename, int height, int width)
FALSE, width);
break;
case DLGK_ENTER:
- if (dialog_vars.nook)
- result = DLG_EXIT_OK;
- else
- result = dlg_exit_buttoncode(button);
+ result = dlg_enter_buttoncode(button);
+ break;
+ case DLGK_LEAVE:
+ result = dlg_ok_buttoncode(button);
break;
case DLGK_PAGE_FIRST:
if (!obj.begin_reached) {
@@ -954,9 +975,7 @@ dialog_textbox(const char *title, const char *filename, int height, int width)
width = old_width;
back_lines(&obj, obj.page_length);
/* repaint */
- dlg_clear();
- dlg_del_window(dialog);
- dlg_mouse_free_regions();
+ _dlg_resize_cleanup(dialog);
goto retry;
#endif
}
@@ -975,6 +994,7 @@ dialog_textbox(const char *title, const char *filename, int height, int width)
}
}
}
+ dlg_add_last_key(-1);
dlg_del_window(dialog);
free(obj.buf);