2 * $Id: textbox.c,v 1.110 2012/12/01 01:48:08 tom Exp $
4 * textbox.c -- implements the text box
6 * Copyright 2000-2011,2012 Thomas E. Dickey
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License, version 2.1
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
23 * An earlier version of this program lists as authors:
24 * Savio Lam (lam836@cs.cuhk.hk)
30 #define PAGE_LENGTH (height - 4)
31 #define PAGE_WIDTH (width - 2)
38 char line
[MAX_LEN
+ 1];
47 long page_length
; /* lines on the page which is shown */
48 long in_buf
; /* ending index into buf[] for page */
53 lseek_obj(MY_OBJ
* obj
, long offset
, int mode
)
56 if ((fpos
= (long) lseek(obj
->fd
, (off_t
) offset
, mode
)) == -1) {
60 dlg_exiterr("Cannot get file position");
63 dlg_exiterr("Cannot seek to end of file");
66 dlg_exiterr("Cannot set file position to %ld", offset
);
74 ftell_obj(MY_OBJ
* obj
)
76 return lseek_obj(obj
, 0L, SEEK_CUR
);
80 lseek_set(MY_OBJ
* obj
, long offset
)
82 long actual
= lseek_obj(obj
, offset
, SEEK_SET
);
84 if (actual
!= offset
) {
85 dlg_exiterr("Cannot set file position to %ld (actual %ld)\n",
91 lseek_end(MY_OBJ
* obj
, long offset
)
93 long actual
= lseek_obj(obj
, offset
, SEEK_END
);
95 if (actual
> offset
) {
96 obj
->file_size
= actual
;
101 lseek_cur(MY_OBJ
* obj
, long offset
)
103 long actual
= lseek_obj(obj
, offset
, SEEK_CUR
);
105 if (actual
!= offset
) {
106 dlg_trace_msg("Lseek returned %ld, expected %ld\n", actual
, offset
);
113 char *result
= dlg_malloc(char, size
);
114 assert_ptr(result
, "xalloc");
119 * read_high() substitutes read() for tab->spaces conversion
121 * buffer_len, fd_bytes_read, bytes_read are modified
124 * fd_bytes_read is the effective number of bytes read from file
125 * bytes_read is the length of buf, that can be different if tab_correct
128 read_high(MY_OBJ
* obj
, size_t size_read
)
131 int i
= 0, j
, n
, tmpint
;
134 /* Allocate space for read buffer */
135 buftab
= xalloc(size_read
+ 1);
137 if ((obj
->fd_bytes_read
= read(obj
->fd
, buftab
, size_read
)) != -1) {
139 buftab
[obj
->fd_bytes_read
] = '\0'; /* mark end of valid data */
141 if (dialog_vars
.tab_correct
) {
143 /* calculate bytes_read by buftab and fd_bytes_read */
144 obj
->bytes_read
= begin_line
= 0;
145 for (j
= 0; j
< obj
->fd_bytes_read
; j
++)
146 if (buftab
[j
] == TAB
)
147 obj
->bytes_read
+= dialog_state
.tab_len
148 - ((obj
->bytes_read
- begin_line
)
149 % dialog_state
.tab_len
);
150 else if (buftab
[j
] == '\n') {
152 begin_line
= obj
->bytes_read
;
156 if (obj
->bytes_read
> obj
->buffer_len
) {
157 if (obj
->buffer_first
)
158 obj
->buffer_first
= FALSE
; /* disp = 0 */
163 obj
->buffer_len
= obj
->bytes_read
;
165 /* Allocate space for read buffer */
166 obj
->buf
= xalloc((size_t) obj
->buffer_len
+ 1);
170 if (obj
->buffer_first
) {
171 obj
->buffer_first
= FALSE
;
173 /* Allocate space for read buffer */
174 obj
->buf
= xalloc(size_read
+ 1);
177 obj
->bytes_read
= obj
->fd_bytes_read
;
182 while (j
< obj
->fd_bytes_read
)
183 if (((ch
= buftab
[j
++]) == TAB
) && (dialog_vars
.tab_correct
!= 0)) {
184 tmpint
= (dialog_state
.tab_len
185 - ((int) ((long) i
- begin_line
) % dialog_state
.tab_len
));
186 for (n
= 0; n
< tmpint
; n
++)
194 obj
->buf
[i
] = '\0'; /* mark end of valid data */
197 if (obj
->bytes_read
== -1)
198 dlg_exiterr("Error reading file");
203 find_first(MY_OBJ
* obj
, char *buffer
, long length
)
205 long recount
= obj
->page_length
;
209 if (buffer
[length
] == '\n') {
221 tabize(MY_OBJ
* obj
, long val
, long *first_pos
)
224 long i
, count
, begin_line
;
227 if (!dialog_vars
.tab_correct
)
230 fpos
= ftell_obj(obj
);
232 lseek_set(obj
, fpos
- obj
->fd_bytes_read
);
234 /* Allocate space for read buffer */
235 buftab
= xalloc((size_t) val
+ 1);
237 if ((read(obj
->fd
, buftab
, (size_t) val
)) == -1)
238 dlg_exiterr("Error reading file in tabize().");
240 begin_line
= count
= 0;
244 for (i
= 0; i
< val
; i
++) {
245 if ((first_pos
!= 0) && (count
>= val
)) {
246 *first_pos
= find_first(obj
, buftab
, i
);
249 if (buftab
[i
] == TAB
)
250 count
+= dialog_state
.tab_len
251 - ((count
- begin_line
) % dialog_state
.tab_len
);
252 else if (buftab
[i
] == '\n') {
259 lseek_set(obj
, fpos
);
264 * Return current line of text.
265 * 'page' should point to start of current line before calling, and will be
266 * updated to point to start of next line.
269 get_line(MY_OBJ
* obj
)
274 obj
->end_reached
= FALSE
;
275 while (obj
->buf
[obj
->in_buf
] != '\n') {
276 if (obj
->buf
[obj
->in_buf
] == '\0') { /* Either end of file or end of buffer reached */
277 fpos
= ftell_obj(obj
);
279 if (fpos
< obj
->file_size
) { /* Not end of file yet */
280 /* We've reached end of buffer, but not end of file yet, so
281 * read next part of file into buffer
283 read_high(obj
, BUF_SIZE
);
286 if (!obj
->end_reached
)
287 obj
->end_reached
= TRUE
;
290 } else if (i
< MAX_LEN
)
291 obj
->line
[i
++] = obj
->buf
[obj
->in_buf
++];
293 if (i
== MAX_LEN
) /* Truncate lines longer than MAX_LEN characters */
294 obj
->line
[i
++] = '\0';
300 if (!obj
->end_reached
)
301 obj
->in_buf
++; /* move past '\n' */
307 match_string(MY_OBJ
* obj
, char *string
)
309 char *match
= get_line(obj
);
310 return strstr(match
, string
) != 0;
314 * Go back 'n' lines in text file. Called by dialog_textbox().
315 * 'in_buf' will be updated to point to the desired line in 'buf'.
318 back_lines(MY_OBJ
* obj
, long n
)
324 obj
->begin_reached
= FALSE
;
325 /* We have to distinguish between end_reached and !end_reached since at end
326 * of file, the line is not ended by a '\n'. The code inside 'if'
327 * basically does a '--in_buf' to move one character backward so as to
328 * skip '\n' of the previous line */
329 if (!obj
->end_reached
) {
330 /* Either beginning of buffer or beginning of file reached? */
332 if (obj
->in_buf
== 0) {
333 fpos
= ftell_obj(obj
);
335 if (fpos
> obj
->fd_bytes_read
) { /* Not beginning of file yet */
336 /* We've reached beginning of buffer, but not beginning of file
337 * yet, so read previous part of file into buffer. Note that
338 * we only move backward for BUF_SIZE/2 bytes, but not BUF_SIZE
339 * bytes to avoid re-reading again in print_page() later
341 /* Really possible to move backward BUF_SIZE/2 bytes? */
342 if (fpos
< BUF_SIZE
/ 2 + obj
->fd_bytes_read
) {
343 /* No, move less than */
345 val_to_tabize
= fpos
- obj
->fd_bytes_read
;
346 } else { /* Move backward BUF_SIZE/2 bytes */
347 lseek_cur(obj
, -(BUF_SIZE
/ 2 + obj
->fd_bytes_read
));
348 val_to_tabize
= BUF_SIZE
/ 2;
350 read_high(obj
, BUF_SIZE
);
352 obj
->in_buf
= tabize(obj
, val_to_tabize
, (long *) 0);
354 } else { /* Beginning of file reached */
355 obj
->begin_reached
= TRUE
;
360 if (obj
->buf
[obj
->in_buf
] != '\n')
361 /* Something's wrong... */
362 dlg_exiterr("Internal error in back_lines().");
365 /* Go back 'n' lines */
366 for (i
= 0; i
< n
; i
++) {
368 if (obj
->in_buf
== 0) {
369 fpos
= ftell_obj(obj
);
371 if (fpos
> obj
->fd_bytes_read
) {
372 /* Really possible to move backward BUF_SIZE/2 bytes? */
373 if (fpos
< BUF_SIZE
/ 2 + obj
->fd_bytes_read
) {
374 /* No, move less than */
376 val_to_tabize
= fpos
- obj
->fd_bytes_read
;
377 } else { /* Move backward BUF_SIZE/2 bytes */
378 lseek_cur(obj
, -(BUF_SIZE
/ 2 + obj
->fd_bytes_read
));
379 val_to_tabize
= BUF_SIZE
/ 2;
381 read_high(obj
, BUF_SIZE
);
383 obj
->in_buf
= tabize(obj
, val_to_tabize
, (long *) 0);
385 } else { /* Beginning of file reached */
386 obj
->begin_reached
= TRUE
;
390 } while (obj
->buf
[--(obj
->in_buf
)] != '\n');
396 * Print a new line of text.
399 print_line(MY_OBJ
* obj
, int row
, int width
)
401 if (wmove(obj
->text
, row
, 0) != ERR
) {
403 char *line
= get_line(obj
);
404 const int *cols
= dlg_index_columns(line
);
405 const int *indx
= dlg_index_wchars(line
);
406 int limit
= dlg_count_wchars(line
);
410 if (width
> getmaxx(obj
->text
))
411 width
= getmaxx(obj
->text
);
412 --width
; /* for the leading ' ' */
414 for (i
= 0; i
<= limit
&& cols
[i
] < obj
->hscroll
; ++i
)
417 for (i
= first
; (i
<= limit
) && ((cols
[i
] - cols
[first
]) < width
); ++i
)
420 (void) waddch(obj
->text
, ' ');
421 (void) waddnstr(obj
->text
, line
+ indx
[first
], indx
[last
] - indx
[first
]);
423 getyx(obj
->text
, y
, x
);
424 if (y
== row
) { /* Clear 'residue' of previous line */
425 for (i
= 0; i
<= width
- x
; i
++) {
426 (void) waddch(obj
->text
, ' ');
433 * Print a new page of text.
436 print_page(MY_OBJ
* obj
, int height
, int width
)
438 int i
, passed_end
= 0;
440 obj
->page_length
= 0;
441 for (i
= 0; i
< height
; i
++) {
442 print_line(obj
, i
, width
);
445 if (obj
->end_reached
&& !passed_end
)
448 (void) wnoutrefresh(obj
->text
);
449 dlg_trace_win(obj
->text
);
453 * Print current position
456 print_position(MY_OBJ
* obj
, WINDOW
*win
, int height
, int width
)
462 fpos
= ftell_obj(obj
);
463 if (dialog_vars
.tab_correct
)
464 size
= tabize(obj
, obj
->in_buf
, &first
);
466 first
= find_first(obj
, obj
->buf
, size
= obj
->in_buf
);
468 dlg_draw_scrollbar(win
,
470 fpos
- obj
->fd_bytes_read
+ size
,
471 fpos
- obj
->fd_bytes_read
+ size
,
480 * Display a dialog box and get the search term from user.
483 get_search_term(WINDOW
*dialog
, char *input
, int height
, int width
)
486 static DLG_KEYS_BINDING binding
[] = {
496 int box_height
, box_width
;
501 int result
= DLG_EXIT_UNKNOWN
;
502 const char *caption
= _("Search");
503 int len_caption
= dlg_count_columns(caption
);
508 getbegyx(dialog
, old_y
, old_x
);
510 box_height
= 1 + (2 * MARGIN
);
511 box_width
= len_caption
+ (2 * (MARGIN
+ 2));
512 box_width
= MAX(box_width
, 30);
513 box_width
= MIN(box_width
, getmaxx(dialog
) - 2 * MARGIN
);
514 len_caption
= MIN(len_caption
, box_width
- (2 * (MARGIN
+ 1)));
516 box_x
= (width
- box_width
) / 2;
517 box_y
= (height
- box_height
) / 2;
518 widget
= dlg_new_modal_window(dialog
,
519 box_height
, box_width
,
520 old_y
+ box_y
, old_x
+ box_x
);
521 keypad(widget
, TRUE
);
522 dlg_register_window(widget
, "searchbox", binding
);
524 dlg_draw_box2(widget
, 0, 0, box_height
, box_width
,
526 searchbox_border_attr
,
527 searchbox_border2_attr
);
528 (void) wattrset(widget
, searchbox_title_attr
);
529 (void) wmove(widget
, 0, (box_width
- len_caption
) / 2);
531 indx
= dlg_index_wchars(caption
);
532 limit
= dlg_limit_columns(caption
, len_caption
, 0);
533 (void) waddnstr(widget
, caption
+ indx
[0], indx
[limit
] - indx
[0]);
536 offset
= dlg_count_columns(input
);
538 while (result
== DLG_EXIT_UNKNOWN
) {
540 key
= dlg_getc(widget
, &fkey
);
545 result
= DLG_EXIT_CANCEL
;
549 result
= DLG_EXIT_OK
;
552 } else if (key
== ESC
) {
553 result
= DLG_EXIT_ESC
;
555 } else if (key
== ERR
) {
560 if (dlg_edit_string(input
, &offset
, key
, fkey
, first
)) {
561 dlg_show_string(widget
, input
, offset
, searchbox_attr
,
562 1, 1, box_width
, FALSE
, first
);
566 dlg_del_window(widget
);
571 perform_search(MY_OBJ
* obj
, int height
, int width
, int key
, char *search_term
)
581 /* set search direction */
582 dir
= (key
== '/' || key
== 'n') ? 1 : 0;
583 if (dir
? !obj
->end_reached
: !obj
->begin_reached
) {
584 if (key
== 'n' || key
== 'N') {
585 if (search_term
[0] == '\0') { /* No search term yet */
589 /* Get search term from user */
590 } else if ((result
= get_search_term(obj
->text
, search_term
,
592 PAGE_WIDTH
)) != DLG_EXIT_OK
593 || search_term
[0] == '\0') {
595 if (result
== DLG_EXIT_CANCEL
) {
601 /* ESC pressed, or no search term, reprint page to clear box */
602 (void) wattrset(obj
->text
, dialog_attr
);
603 back_lines(obj
, obj
->page_length
);
606 /* Save variables for restoring in case search term can't be found */
607 tempinx
= obj
->in_buf
;
608 temp
= obj
->begin_reached
;
609 temp1
= obj
->end_reached
;
610 fpos
= ftell_obj(obj
) - obj
->fd_bytes_read
;
611 /* update 'in_buf' to point to next (previous) line before
612 forward (backward) searching */
614 ? obj
->page_length
- 1
615 : obj
->page_length
+ 1));
616 if (dir
) { /* Forward search */
617 while ((found
= match_string(obj
, search_term
)) == FALSE
) {
618 if (obj
->end_reached
)
621 } else { /* Backward search */
622 while ((found
= match_string(obj
, search_term
)) == FALSE
) {
623 if (obj
->begin_reached
)
628 if (found
== FALSE
) { /* not found */
630 /* Restore program state to that before searching */
631 lseek_set(obj
, fpos
);
633 read_high(obj
, BUF_SIZE
);
635 obj
->in_buf
= tempinx
;
636 obj
->begin_reached
= temp
;
637 obj
->end_reached
= temp1
;
638 /* move 'in_buf' to point to start of current page to
639 * re-print current page. Note that 'in_buf' always points
640 * to start of next page, so this is necessary
642 back_lines(obj
, obj
->page_length
);
643 } else { /* Search term found */
647 (void) wattrset(obj
->text
, dialog_attr
);
649 } else { /* no need to find */
656 * Display text from a file in a dialog box.
659 dialog_textbox(const char *title
, const char *file
, int height
, int width
)
662 static DLG_KEYS_BINDING binding
[] = {
665 DLG_KEYS_DATA( DLGK_GRID_DOWN
, 'J' ),
666 DLG_KEYS_DATA( DLGK_GRID_DOWN
, 'j' ),
667 DLG_KEYS_DATA( DLGK_GRID_DOWN
, KEY_DOWN
),
668 DLG_KEYS_DATA( DLGK_GRID_LEFT
, 'H' ),
669 DLG_KEYS_DATA( DLGK_GRID_LEFT
, 'h' ),
670 DLG_KEYS_DATA( DLGK_GRID_LEFT
, KEY_LEFT
),
671 DLG_KEYS_DATA( DLGK_GRID_RIGHT
, 'L' ),
672 DLG_KEYS_DATA( DLGK_GRID_RIGHT
, 'l' ),
673 DLG_KEYS_DATA( DLGK_GRID_RIGHT
, KEY_RIGHT
),
674 DLG_KEYS_DATA( DLGK_GRID_UP
, 'K' ),
675 DLG_KEYS_DATA( DLGK_GRID_UP
, 'k' ),
676 DLG_KEYS_DATA( DLGK_GRID_UP
, KEY_UP
),
677 DLG_KEYS_DATA( DLGK_PAGE_FIRST
, 'g' ),
678 DLG_KEYS_DATA( DLGK_PAGE_FIRST
, KEY_HOME
),
679 DLG_KEYS_DATA( DLGK_PAGE_LAST
, 'G' ),
680 DLG_KEYS_DATA( DLGK_PAGE_LAST
, KEY_END
),
681 DLG_KEYS_DATA( DLGK_PAGE_LAST
, KEY_LL
),
682 DLG_KEYS_DATA( DLGK_PAGE_NEXT
, ' ' ),
683 DLG_KEYS_DATA( DLGK_PAGE_NEXT
, KEY_NPAGE
),
684 DLG_KEYS_DATA( DLGK_PAGE_PREV
, 'B' ),
685 DLG_KEYS_DATA( DLGK_PAGE_PREV
, 'b' ),
686 DLG_KEYS_DATA( DLGK_PAGE_PREV
, KEY_PPAGE
),
687 DLG_KEYS_DATA( DLGK_BEGIN
, '0' ),
688 DLG_KEYS_DATA( DLGK_BEGIN
, KEY_BEG
),
689 DLG_KEYS_DATA( DLGK_FIELD_NEXT
, TAB
),
690 DLG_KEYS_DATA( DLGK_FIELD_PREV
, KEY_BTAB
),
696 int old_height
= height
;
697 int old_width
= width
;
700 int x
, y
, cur_x
, cur_y
;
703 int i
, code
, passed_end
;
704 char search_term
[MAX_LEN
+ 1];
708 int result
= DLG_EXIT_UNKNOWN
;
709 int button
= dlg_default_button();
712 search_term
[0] = '\0'; /* no search term entered yet */
714 memset(&obj
, 0, sizeof(obj
));
716 obj
.begin_reached
= TRUE
;
717 obj
.buffer_first
= TRUE
;
718 obj
.end_reached
= FALSE
;
719 obj
.buttons
= dlg_exit_label();
721 /* Open input file for reading */
722 if ((obj
.fd
= open(file
, O_RDONLY
)) == -1)
723 dlg_exiterr("Can't open input file %s", file
);
725 /* Get file size. Actually, 'file_size' is the real file size - 1,
726 since it's only the last byte offset from the beginning */
729 /* Restore file pointer to beginning of file after getting file size */
732 read_high(&obj
, BUF_SIZE
);
734 dlg_button_layout(obj
.buttons
, &min_width
);
741 dlg_auto_sizefile(title
, file
, &height
, &width
, 2, min_width
);
742 dlg_print_size(height
, width
);
743 dlg_ctl_size(height
, width
);
745 x
= dlg_box_x_ordinate(width
);
746 y
= dlg_box_y_ordinate(height
);
748 dialog
= dlg_new_window(height
, width
, y
, x
);
749 dlg_register_window(dialog
, "textbox", binding
);
750 dlg_register_buttons(dialog
, "textbox", obj
.buttons
);
752 dlg_mouse_setbase(x
, y
);
754 /* Create window for text region, used for scrolling text */
755 obj
.text
= dlg_sub_window(dialog
, PAGE_LENGTH
, PAGE_WIDTH
, y
+ 1, x
+ 1);
757 /* register the new window, along with its borders */
758 dlg_mouse_mkbigregion(0, 0, PAGE_LENGTH
+ 2, width
, KEY_MAX
, 1, 1, 1 /* lines */ );
759 dlg_draw_box2(dialog
, 0, 0, height
, width
, dialog_attr
, border_attr
, border2_attr
);
760 dlg_draw_bottom_box2(dialog
, border_attr
, border2_attr
, dialog_attr
);
761 dlg_draw_title(dialog
, title
);
763 dlg_draw_buttons(dialog
, PAGE_LENGTH
+ 2, 0, obj
.buttons
, button
, FALSE
, width
);
764 (void) wnoutrefresh(dialog
);
765 getyx(dialog
, cur_y
, cur_x
); /* Save cursor position */
767 dlg_attr_clear(obj
.text
, PAGE_LENGTH
, PAGE_WIDTH
, dialog_attr
);
769 while (result
== DLG_EXIT_UNKNOWN
) {
772 * Update the screen according to whether we shifted up/down by a line
777 (void) scrollok(obj
.text
, TRUE
);
778 (void) scroll(obj
.text
); /* Scroll text region up one line */
779 (void) scrollok(obj
.text
, FALSE
);
780 print_line(&obj
, PAGE_LENGTH
- 1, PAGE_WIDTH
);
781 (void) wnoutrefresh(obj
.text
);
782 } else if (next
> 0) {
784 * We don't call print_page() here but use scrolling to ensure
785 * faster screen update. However, 'end_reached' and
786 * 'page_length' should still be updated, and 'in_buf' should
787 * point to start of next page. This is done by calling
788 * get_line() in the following 'for' loop.
790 (void) scrollok(obj
.text
, TRUE
);
791 (void) wscrl(obj
.text
, -1); /* Scroll text region down one line */
792 (void) scrollok(obj
.text
, FALSE
);
795 for (i
= 0; i
< PAGE_LENGTH
; i
++) {
797 print_line(&obj
, 0, PAGE_WIDTH
); /* print first line of page */
798 (void) wnoutrefresh(obj
.text
);
800 (void) get_line(&obj
); /* Called to update 'end_reached' and 'in_buf' */
803 if (obj
.end_reached
&& !passed_end
)
807 print_page(&obj
, PAGE_LENGTH
, PAGE_WIDTH
);
809 print_position(&obj
, dialog
, height
, width
);
810 (void) wmove(dialog
, cur_y
, cur_x
); /* Restore cursor position */
813 moved
= FALSE
; /* assume we'll not move */
814 next
= 0; /* ...but not scroll by a line */
816 key
= dlg_mouse_wgetch(dialog
, &fkey
);
817 if (dlg_result_key(key
, fkey
, &result
))
820 if (!fkey
&& (code
= dlg_char_to_button(key
, obj
.buttons
)) >= 0) {
821 result
= dlg_ok_buttoncode(code
);
828 if (is_DLGK_MOUSE(key
)) {
829 result
= dlg_exit_buttoncode(key
- M_EVENT
);
831 result
= DLG_EXIT_OK
;
836 case DLGK_FIELD_NEXT
:
837 button
= dlg_next_button(obj
.buttons
, button
);
840 dlg_draw_buttons(dialog
,
845 case DLGK_FIELD_PREV
:
846 button
= dlg_prev_button(obj
.buttons
, button
);
849 dlg_draw_buttons(dialog
,
855 if (dialog_vars
.nook
)
856 result
= DLG_EXIT_OK
;
858 result
= dlg_exit_buttoncode(button
);
860 case DLGK_PAGE_FIRST
:
861 if (!obj
.begin_reached
) {
862 obj
.begin_reached
= 1;
863 /* First page not in buffer? */
864 fpos
= ftell_obj(&obj
);
866 if (fpos
> obj
.fd_bytes_read
) {
867 /* Yes, we have to read it in */
870 read_high(&obj
, BUF_SIZE
);
877 obj
.end_reached
= TRUE
;
878 /* Last page not in buffer? */
879 fpos
= ftell_obj(&obj
);
881 if (fpos
< obj
.file_size
) {
882 /* Yes, we have to read it in */
883 lseek_end(&obj
, -BUF_SIZE
);
885 read_high(&obj
, BUF_SIZE
);
887 obj
.in_buf
= obj
.bytes_read
;
888 back_lines(&obj
, (long) PAGE_LENGTH
);
891 case DLGK_GRID_UP
: /* Previous line */
892 if (!obj
.begin_reached
) {
893 back_lines(&obj
, obj
.page_length
+ 1);
898 case DLGK_PAGE_PREV
: /* Previous page */
899 case DLGK_MOUSE(KEY_PPAGE
):
900 if (!obj
.begin_reached
) {
901 back_lines(&obj
, obj
.page_length
+ PAGE_LENGTH
);
905 case DLGK_GRID_DOWN
: /* Next line */
906 if (!obj
.end_reached
) {
907 obj
.begin_reached
= 0;
912 case DLGK_PAGE_NEXT
: /* Next page */
913 case DLGK_MOUSE(KEY_NPAGE
):
914 if (!obj
.end_reached
) {
915 obj
.begin_reached
= 0;
919 case DLGK_BEGIN
: /* Beginning of line */
920 if (obj
.hscroll
> 0) {
922 /* Reprint current page to scroll horizontally */
923 back_lines(&obj
, obj
.page_length
);
927 case DLGK_GRID_LEFT
: /* Scroll left */
928 if (obj
.hscroll
> 0) {
930 /* Reprint current page to scroll horizontally */
931 back_lines(&obj
, obj
.page_length
);
935 case DLGK_GRID_RIGHT
: /* Scroll right */
936 if (obj
.hscroll
< MAX_LEN
) {
938 /* Reprint current page to scroll horizontally */
939 back_lines(&obj
, obj
.page_length
);
948 back_lines(&obj
, obj
.page_length
);
951 dlg_del_window(dialog
);
953 dlg_mouse_free_regions();
959 case '/': /* Forward search */
960 case 'n': /* Repeat forward search */
961 case '?': /* Backward search */
962 case 'N': /* Repeat backward search */
963 moved
= perform_search(&obj
, height
, width
, key
, search_term
);
973 dlg_del_window(dialog
);
975 (void) close(obj
.fd
);
976 dlg_mouse_free_regions();