Ticket #2877: code cleanup before 4.8.1.6 release.
[midnight-commander.git] / src / editor / editdraw.c
blobea2813221bd84a98b445babf517dc214de421593
1 /*
2 Editor text drawing.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer, 1996, 1997
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /** \file
28 * \brief Source: editor text drawing
29 * \author Paul Sheer
30 * \date 1996, 1997
33 #include <config.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <ctype.h>
41 #include <errno.h>
42 #include <sys/stat.h>
44 #include "lib/global.h"
45 #include "lib/tty/tty.h" /* tty_printf() */
46 #include "lib/tty/key.h" /* is_idle() */
47 #include "lib/skin.h"
48 #include "lib/strutil.h" /* utf string functions */
49 #include "lib/util.h" /* is_printable() */
50 #include "lib/widget.h" /* buttonbar_redraw() */
51 #ifdef HAVE_CHARSET
52 #include "lib/charsets.h"
53 #endif
55 #include "src/setup.h" /* edit_tab_spacing */
56 #include "src/main.h" /* macro_index */
58 #include "edit-impl.h"
59 #include "editwidget.h"
61 /*** global variables ****************************************************************************/
63 /* Toggles statusbar draw style */
64 int simple_statusbar = 0;
66 int visible_tabs = 1, visible_tws = 1;
68 /*** file scope macro definitions ****************************************************************/
70 #define MAX_LINE_LEN 1024
72 /* Text styles */
73 #define MOD_ABNORMAL (1 << 8)
74 #define MOD_BOLD (1 << 9)
75 #define MOD_MARKED (1 << 10)
76 #define MOD_CURSOR (1 << 11)
77 #define MOD_WHITESPACE (1 << 12)
79 #define edit_move(x,y) widget_move(edit, y, x);
81 #define key_pending(x) (!is_idle())
83 #define EDITOR_MINIMUM_TERMINAL_WIDTH 30
85 /*** file scope type declarations ****************************************************************/
87 struct line_s
89 unsigned int ch;
90 unsigned int style;
93 /*** file scope variables ************************************************************************/
95 /*** file scope functions ************************************************************************/
96 /* --------------------------------------------------------------------------------------------- */
98 static inline void
99 status_string (WEdit * edit, char *s, int w)
101 char byte_str[16];
104 * If we are at the end of file, print <EOF>,
105 * otherwise print the current character as is (if printable),
106 * as decimal and as hex.
108 if (edit->curs1 < edit->last_byte)
110 unsigned char cur_byte = 0;
112 #ifdef HAVE_CHARSET
113 int cw = 1;
115 if (!edit->utf8)
117 cur_byte = edit_get_byte (edit, edit->curs1);
119 g_snprintf (byte_str, sizeof (byte_str), "%4d 0x%03X",
120 (int) cur_byte, (unsigned) cur_byte);
122 else
124 unsigned int cur_utf = 0;
126 cur_utf = edit_get_utf (edit, edit->curs1, &cw);
127 if (cw > 0)
129 g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
130 (unsigned) cur_utf, (unsigned) cur_utf);
132 else
133 #endif
135 cur_byte = edit_get_byte (edit, edit->curs1);
136 g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
137 (int) cur_byte, (unsigned) cur_utf);
141 else
143 strcpy (byte_str, "<EOF> ");
146 /* The field lengths just prevent the status line from shortening too much */
147 if (simple_statusbar)
148 g_snprintf (s, w,
149 "%c%c%c%c %3ld %5ld/%ld %6ld/%ld %s %s",
150 edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
151 edit->modified ? 'M' : '-',
152 macro_index < 0 ? '-' : 'R',
153 edit->overwrite == 0 ? '-' : 'O',
154 edit->curs_col + edit->over_col,
155 edit->curs_line + 1,
156 edit->total_lines + 1, (long) edit->curs1, (long) edit->last_byte, byte_str,
157 #ifdef HAVE_CHARSET
158 mc_global.source_codepage >=
159 0 ? get_codepage_id (mc_global.source_codepage) :
160 #endif
163 else
164 g_snprintf (s, w,
165 "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb) %s %s",
166 edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
167 edit->modified ? 'M' : '-',
168 macro_index < 0 ? '-' : 'R',
169 edit->overwrite == 0 ? '-' : 'O',
170 edit->curs_col + edit->over_col,
171 edit->start_line + 1,
172 edit->curs_row,
173 edit->curs_line + 1,
174 edit->total_lines + 1, (long) edit->curs1, (long) edit->last_byte, byte_str,
175 #ifdef HAVE_CHARSET
176 mc_global.source_codepage >=
177 0 ? get_codepage_id (mc_global.source_codepage) :
178 #endif
183 /* --------------------------------------------------------------------------------------------- */
185 static inline void
186 printwstr (const char *s, int len)
188 if (len > 0)
189 tty_printf ("%-*.*s", len, len, s);
192 /* --------------------------------------------------------------------------------------------- */
194 static inline void
195 print_to_widget (WEdit * edit, long row, int start_col, int start_col_real,
196 long end_col, struct line_s line[], char *status, int bookmarked)
198 struct line_s *p;
200 int x = start_col_real;
201 int x1 = start_col + EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
202 int y = row + EDIT_TEXT_VERTICAL_OFFSET;
203 int cols_to_skip = abs (x);
204 int i;
205 int wrap_start;
206 int len;
208 tty_setcolor (EDITOR_NORMAL_COLOR);
209 if (bookmarked != 0)
210 tty_setcolor (bookmarked);
212 len = end_col + 1 - start_col;
213 wrap_start = option_word_wrap_line_length + edit->start_col;
215 if (len > 0 && edit->widget.y + y >= 0)
217 if (!show_right_margin || wrap_start > end_col)
218 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', len);
219 else if (wrap_start < 0)
221 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
222 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', len);
224 else
226 if (wrap_start > 0)
227 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', wrap_start);
229 len -= wrap_start;
230 if (len > 0)
232 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
233 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1 + wrap_start, ' ', len);
238 if (option_line_state)
240 tty_setcolor (LINE_STATE_COLOR);
241 for (i = 0; i < LINE_STATE_WIDTH; i++)
243 edit_move (x1 + i - option_line_state_width, y);
244 if (status[i] == '\0')
245 status[i] = ' ';
246 tty_print_char (status[i]);
250 edit_move (x1, y);
251 i = 1;
252 for (p = line; p->ch != 0; p++)
254 int style;
255 unsigned int textchar;
256 int color;
258 if (cols_to_skip != 0)
260 cols_to_skip--;
261 continue;
264 style = p->style & 0xFF00;
265 textchar = p->ch;
266 color = p->style >> 16;
268 if (style & MOD_ABNORMAL)
270 /* Non-printable - use black background */
271 color = 0;
274 if (style & MOD_WHITESPACE)
276 if (style & MOD_MARKED)
278 textchar = ' ';
279 tty_setcolor (EDITOR_MARKED_COLOR);
281 else
282 tty_setcolor (EDITOR_WHITESPACE_COLOR);
284 else if (style & MOD_BOLD)
285 tty_setcolor (EDITOR_BOLD_COLOR);
286 else if (style & MOD_MARKED)
287 tty_setcolor (EDITOR_MARKED_COLOR);
288 else
289 tty_lowlevel_setcolor (color);
291 if (show_right_margin)
293 if (i > option_word_wrap_line_length + edit->start_col)
294 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
295 i++;
298 tty_print_anychar (textchar);
302 /* --------------------------------------------------------------------------------------------- */
303 /** b is a pointer to the beginning of the line */
305 static void
306 edit_draw_this_line (WEdit * edit, off_t b, long row, long start_col, long end_col)
308 struct line_s line[MAX_LINE_LEN];
309 struct line_s *p = line;
311 off_t m1 = 0, m2 = 0, q;
312 long c1, c2;
313 int col, start_col_real;
314 unsigned int c;
315 int color;
316 int abn_style;
317 int i;
318 unsigned int cur_line = 0;
319 int book_mark = 0;
320 char line_stat[LINE_STATE_WIDTH + 1] = "\0";
322 if (row > edit->widget.lines - 1 - EDIT_TEXT_VERTICAL_OFFSET)
323 return;
325 if (book_mark_query_color (edit, edit->start_line + row, BOOK_MARK_COLOR))
326 book_mark = BOOK_MARK_COLOR;
327 else if (book_mark_query_color (edit, edit->start_line + row, BOOK_MARK_FOUND_COLOR))
328 book_mark = BOOK_MARK_FOUND_COLOR;
330 if (book_mark)
331 abn_style = book_mark << 16;
332 else
333 abn_style = MOD_ABNORMAL;
335 end_col -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
337 edit_get_syntax_color (edit, b - 1, &color);
338 q = edit_move_forward3 (edit, b, start_col - edit->start_col, 0);
339 start_col_real = (col = (int) edit_move_forward3 (edit, b, 0, q)) + edit->start_col;
341 if (option_line_state)
343 cur_line = edit->start_line + row;
344 if (cur_line <= (unsigned int) edit->total_lines)
346 g_snprintf (line_stat, LINE_STATE_WIDTH + 1, "%7i ", cur_line + 1);
348 else
350 memset (line_stat, ' ', LINE_STATE_WIDTH);
351 line_stat[LINE_STATE_WIDTH] = '\0';
353 if (book_mark_query_color (edit, cur_line, BOOK_MARK_COLOR))
355 g_snprintf (line_stat, 2, "*");
359 if (col + 16 > -edit->start_col)
361 eval_marks (edit, &m1, &m2);
363 if (row <= edit->total_lines - edit->start_line)
365 off_t tws = 0;
366 if (tty_use_colors () && visible_tws)
368 tws = edit_eol (edit, b);
369 while (tws > b && ((c = edit_get_byte (edit, tws - 1)) == ' ' || c == '\t'))
370 tws--;
373 while (col <= end_col - edit->start_col)
375 int cw = 1;
376 int tab_over = 0;
377 gboolean wide_width_char = FALSE;
378 gboolean control_char = FALSE;
380 p->ch = 0;
381 p->style = 0;
382 if (q == edit->curs1)
383 p->style |= MOD_CURSOR;
384 if (q >= m1 && q < m2)
386 if (edit->column_highlight)
388 long x;
390 x = (long) edit_move_forward3 (edit, b, 0, q);
391 c1 = min (edit->column1, edit->column2);
392 c2 = max (edit->column1, edit->column2);
393 if (x >= c1 && x < c2)
394 p->style |= MOD_MARKED;
396 else
397 p->style |= MOD_MARKED;
399 if (q == edit->bracket)
400 p->style |= MOD_BOLD;
401 if (q >= edit->found_start && q < (off_t) (edit->found_start + edit->found_len))
402 p->style |= MOD_BOLD;
404 #ifdef HAVE_CHARSET
405 if (edit->utf8)
406 c = edit_get_utf (edit, q, &cw);
407 else
408 #endif
409 c = edit_get_byte (edit, q);
410 /* we don't use bg for mc - fg contains both */
411 if (book_mark)
413 p->style |= book_mark << 16;
415 else
417 edit_get_syntax_color (edit, q, &color);
418 p->style |= color << 16;
420 switch (c)
422 case '\n':
423 col = end_col - edit->start_col + 1; /* quit */
424 break;
425 case '\t':
426 i = TAB_SIZE - ((int) col % TAB_SIZE);
427 tab_over = (end_col - edit->start_col) - (col + i - 1);
428 if (tab_over < 0)
429 i += tab_over;
430 col += i;
431 if (tty_use_colors () &&
432 ((visible_tabs || (visible_tws && q >= tws)) && enable_show_tabs_tws))
434 if (p->style & MOD_MARKED)
435 c = p->style;
436 else if (book_mark)
437 c |= book_mark << 16;
438 else
439 c = p->style | MOD_WHITESPACE;
440 if (i > 2)
442 p->ch = '<';
443 p->style = c;
444 p++;
445 while (--i > 1)
447 p->ch = '-';
448 p->style = c;
449 p++;
451 p->ch = '>';
452 p->style = c;
453 p++;
455 else if (i > 1)
457 p->ch = '<';
458 p->style = c;
459 p++;
460 p->ch = '>';
461 p->style = c;
462 p++;
464 else
466 p->ch = '>';
467 p->style = c;
468 p++;
471 else if (tty_use_colors () && visible_tws && q >= tws && enable_show_tabs_tws)
473 p->ch = '.';
474 p->style |= MOD_WHITESPACE;
475 c = p->style & ~MOD_CURSOR;
476 p++;
477 while (--i)
479 p->ch = ' ';
480 p->style = c;
481 p++;
484 else
486 p->ch |= ' ';
487 c = p->style & ~MOD_CURSOR;
488 p++;
489 while (--i)
491 p->ch = ' ';
492 p->style = c;
493 p++;
496 break;
497 case ' ':
498 if (tty_use_colors () && visible_tws && q >= tws && enable_show_tabs_tws)
500 p->ch = '.';
501 p->style |= MOD_WHITESPACE;
502 p++;
503 col++;
504 break;
506 /* fallthrough */
507 default:
508 #ifdef HAVE_CHARSET
509 if (mc_global.utf8_display)
511 if (!edit->utf8)
513 c = convert_from_8bit_to_utf_c ((unsigned char) c, edit->converter);
515 else
517 if (g_unichar_iswide (c))
519 wide_width_char = TRUE;
520 col++;
524 else if (edit->utf8)
525 c = convert_from_utf_to_current_c (c, edit->converter);
526 else
527 c = convert_to_display_c (c);
528 #endif
530 /* Caret notation for control characters */
531 if (c < 32)
533 p->ch = '^';
534 p->style = abn_style;
535 p++;
536 p->ch = c + 0x40;
537 p->style = abn_style;
538 p++;
539 col += 2;
540 control_char = TRUE;
541 break;
543 if (c == 127)
545 p->ch = '^';
546 p->style = abn_style;
547 p++;
548 p->ch = '?';
549 p->style = abn_style;
550 p++;
551 col += 2;
552 control_char = TRUE;
553 break;
555 #ifdef HAVE_CHARSET
556 if (edit->utf8)
558 if (g_unichar_isprint (c))
559 p->ch = c;
560 else
562 p->ch = '.';
563 p->style = abn_style;
565 p++;
567 else
568 #endif
570 if ((mc_global.utf8_display && g_unichar_isprint (c)) ||
571 (!mc_global.utf8_display && is_printable (c)))
573 p->ch = c;
574 p++;
576 else
578 p->ch = '.';
579 p->style = abn_style;
580 p++;
583 col++;
584 break;
585 } /* case */
587 q++;
588 if (cw > 1)
590 q += cw - 1;
593 if (col > (end_col - edit->start_col + 1))
595 if (wide_width_char)
597 p--;
598 break;
600 if (control_char)
602 p -= 2;
603 break;
609 else
611 start_col_real = start_col = 0;
614 p->ch = 0;
616 print_to_widget (edit, row, start_col, start_col_real, end_col, line, line_stat, book_mark);
619 /* --------------------------------------------------------------------------------------------- */
621 static inline void
622 edit_draw_this_char (WEdit * edit, off_t curs, long row, long start_column, long end_column)
624 off_t b = edit_bol (edit, curs);
626 edit_draw_this_line (edit, b, row, start_column, end_column);
629 /* --------------------------------------------------------------------------------------------- */
630 /** cursor must be in screen for other than REDRAW_PAGE passed in force */
632 static inline void
633 render_edit_text (WEdit * edit, long start_row, long start_column, long end_row, long end_column)
635 static long prev_curs_row = 0;
636 static off_t prev_curs = 0;
638 Widget *w = (Widget *) edit;
639 Dlg_head *h = w->owner;
641 long row = 0, curs_row;
642 int force = edit->force;
643 long b;
644 int y1, x1, y2, x2;
646 int last_line;
647 int last_column;
649 /* draw only visible region */
651 last_line = h->y + h->lines - 1;
653 y1 = w->y;
654 if (y1 > last_line - 1 /* buttonbar */ )
655 return;
657 last_column = h->x + h->cols - 1;
659 x1 = w->x;
660 if (x1 > last_column)
661 return;
663 y2 = w->y + w->lines - 1;
664 if (y2 < h->y + 1 /* menubar */ )
665 return;
667 x2 = w->x + w->cols - 1;
668 if (x2 < h->x)
669 return;
671 if ((force & REDRAW_IN_BOUNDS) == 0)
673 /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
674 /* draw only visible region */
676 if (y2 <= last_line - 1 /* buttonbar */ )
677 end_row = w->lines - 1;
678 else if (y1 >= h->y + 1 /* menubar */ )
679 end_row = h->lines - 1 - y1 - 1;
680 else
681 end_row = start_row + h->lines - 1 - 1;
683 if (x2 <= last_column)
684 end_column = w->cols - 1;
685 else if (x1 >= h->x)
686 end_column = h->cols - 1 - x1;
687 else
688 end_column = start_column + h->cols - 1;
692 * If the position of the page has not moved then we can draw the cursor
693 * character only. This will prevent line flicker when using arrow keys.
695 if ((force & REDRAW_CHAR_ONLY) == 0 || (force & REDRAW_PAGE) != 0)
697 if ((force & REDRAW_PAGE) != 0)
699 row = start_row;
700 b = edit_move_forward (edit, edit->start_display, start_row, 0);
701 while (row <= end_row)
703 if (key_pending (edit))
704 return;
705 edit_draw_this_line (edit, b, row, start_column, end_column);
706 b = edit_move_forward (edit, b, 1, 0);
707 row++;
710 else
712 curs_row = edit->curs_row;
714 if ((force & REDRAW_BEFORE_CURSOR) != 0 && start_row < curs_row)
716 long upto = curs_row - 1 <= end_row ? curs_row - 1 : end_row;
718 row = start_row;
719 b = edit->start_display;
720 while (row <= upto)
722 if (key_pending (edit))
723 return;
724 edit_draw_this_line (edit, b, row, start_column, end_column);
725 b = edit_move_forward (edit, b, 1, 0);
729 /* if (force & REDRAW_LINE) ---> default */
730 b = edit_bol (edit, edit->curs1);
731 if (curs_row >= start_row && curs_row <= end_row)
733 if (key_pending (edit))
734 return;
735 edit_draw_this_line (edit, b, curs_row, start_column, end_column);
738 if ((force & REDRAW_AFTER_CURSOR) != 0 && end_row > curs_row)
740 row = curs_row + 1 < start_row ? start_row : curs_row + 1;
741 b = edit_move_forward (edit, b, 1, 0);
742 while (row <= end_row)
744 if (key_pending (edit))
745 return;
746 edit_draw_this_line (edit, b, row, start_column, end_column);
747 b = edit_move_forward (edit, b, 1, 0);
748 row++;
752 if ((force & REDRAW_LINE_ABOVE) != 0 && curs_row >= 1)
754 row = curs_row - 1;
755 b = edit_move_backward (edit, edit_bol (edit, edit->curs1), 1);
756 if (row >= start_row && row <= end_row)
758 if (key_pending (edit))
759 return;
760 edit_draw_this_line (edit, b, row, start_column, end_column);
764 if ((force & REDRAW_LINE_BELOW) != 0 && row < edit->widget.lines - 1)
766 row = curs_row + 1;
767 b = edit_bol (edit, edit->curs1);
768 b = edit_move_forward (edit, b, 1, 0);
769 if (row >= start_row && row <= end_row)
771 if (key_pending (edit))
772 return;
773 edit_draw_this_line (edit, b, row, start_column, end_column);
778 else if (prev_curs_row < edit->curs_row)
780 /* with the new text highlighting, we must draw from the top down */
781 edit_draw_this_char (edit, prev_curs, prev_curs_row, start_column, end_column);
782 edit_draw_this_char (edit, edit->curs1, edit->curs_row, start_column, end_column);
784 else
786 edit_draw_this_char (edit, edit->curs1, edit->curs_row, start_column, end_column);
787 edit_draw_this_char (edit, prev_curs, prev_curs_row, start_column, end_column);
790 edit->force = 0;
792 prev_curs_row = edit->curs_row;
793 prev_curs = edit->curs1;
796 /* --------------------------------------------------------------------------------------------- */
798 static inline void
799 edit_render (WEdit * edit, int page, int row_start, int col_start, int row_end, int col_end)
801 if (page) /* if it was an expose event, 'page' would be set */
802 edit->force |= REDRAW_PAGE | REDRAW_IN_BOUNDS;
804 if (edit->force & REDRAW_COMPLETELY)
805 buttonbar_redraw (find_buttonbar (edit->widget.owner));
806 render_edit_text (edit, row_start, col_start, row_end, col_end);
808 * edit->force != 0 means a key was pending and the redraw
809 * was halted, so next time we must redraw everything in case stuff
810 * was left undrawn from a previous key press.
812 if (edit->force)
813 edit->force |= REDRAW_PAGE;
816 /* --------------------------------------------------------------------------------------------- */
817 /*** public functions ****************************************************************************/
818 /* --------------------------------------------------------------------------------------------- */
820 /** Draw the status line at the top of the screen. The size of the filename
821 * field varies depending on the width of the screen and the length of
822 * the filename. */
823 void
824 edit_status (WEdit * edit)
826 const int w = edit->widget.owner->cols;
827 const size_t status_size = w + 1;
828 char *const status = g_malloc (status_size);
829 int status_len;
830 const char *fname = "";
831 int fname_len;
832 const int gap = 3; /* between the filename and the status */
833 const int right_gap = 5; /* at the right end of the screen */
834 const int preferred_fname_len = 16;
836 status_string (edit, status, status_size);
837 status_len = (int) str_term_width1 (status);
839 if (edit->filename_vpath != NULL)
840 fname = vfs_path_get_last_path_str (edit->filename_vpath);
842 fname_len = str_term_width1 (fname);
843 if (fname_len < preferred_fname_len)
844 fname_len = preferred_fname_len;
846 if (fname_len + gap + status_len + right_gap >= w)
848 if (preferred_fname_len + gap + status_len + right_gap >= w)
849 fname_len = preferred_fname_len;
850 else
851 fname_len = w - (gap + status_len + right_gap);
852 fname = str_trunc (fname, fname_len);
855 dlg_move (edit->widget.owner, 0, 0);
856 tty_setcolor (STATUSBAR_COLOR);
857 printwstr (fname, fname_len + gap);
858 printwstr (status, w - (fname_len + gap));
860 if (simple_statusbar && w > EDITOR_MINIMUM_TERMINAL_WIDTH)
862 size_t percent = 100;
864 if (edit->total_lines + 1 != 0)
865 percent = (edit->curs_line + 1) * 100 / (edit->total_lines + 1);
866 dlg_move (edit->widget.owner, 0, w - 5);
867 tty_printf (" %3d%%", percent);
870 g_free (status);
873 /* --------------------------------------------------------------------------------------------- */
874 /** this scrolls the text so that cursor is on the screen */
875 void
876 edit_scroll_screen_over_cursor (WEdit * edit)
878 long p;
879 long outby;
880 int b_extreme, t_extreme, l_extreme, r_extreme;
882 if (edit->widget.lines <= 0 || edit->widget.cols <= 0)
883 return;
885 edit->widget.cols -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
886 edit->widget.lines -= EDIT_TEXT_VERTICAL_OFFSET;
888 r_extreme = EDIT_RIGHT_EXTREME;
889 l_extreme = EDIT_LEFT_EXTREME;
890 b_extreme = EDIT_BOTTOM_EXTREME;
891 t_extreme = EDIT_TOP_EXTREME;
892 if (edit->found_len != 0)
894 b_extreme = max (edit->widget.lines / 4, b_extreme);
895 t_extreme = max (edit->widget.lines / 4, t_extreme);
897 if (b_extreme + t_extreme + 1 > edit->widget.lines)
899 int n;
901 n = b_extreme + t_extreme;
902 if (n == 0)
903 n = 1;
904 b_extreme = (b_extreme * (edit->widget.lines - 1)) / n;
905 t_extreme = (t_extreme * (edit->widget.lines - 1)) / n;
907 if (l_extreme + r_extreme + 1 > edit->widget.cols)
909 int n;
911 n = l_extreme + t_extreme;
912 if (n == 0)
913 n = 1;
914 l_extreme = (l_extreme * (edit->widget.cols - 1)) / n;
915 r_extreme = (r_extreme * (edit->widget.cols - 1)) / n;
917 p = edit_get_col (edit) + edit->over_col;
918 edit_update_curs_row (edit);
919 outby = p + edit->start_col - edit->widget.cols + 1 + (r_extreme + edit->found_len);
920 if (outby > 0)
921 edit_scroll_right (edit, outby);
922 outby = l_extreme - p - edit->start_col;
923 if (outby > 0)
924 edit_scroll_left (edit, outby);
925 p = edit->curs_row;
926 outby = p - edit->widget.lines + 1 + b_extreme;
927 if (outby > 0)
928 edit_scroll_downward (edit, outby);
929 outby = t_extreme - p;
930 if (outby > 0)
931 edit_scroll_upward (edit, outby);
932 edit_update_curs_row (edit);
934 edit->widget.lines += EDIT_TEXT_VERTICAL_OFFSET;
935 edit->widget.cols += EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
938 /* --------------------------------------------------------------------------------------------- */
940 void
941 edit_render_keypress (WEdit * edit)
943 edit_render (edit, 0, 0, 0, 0, 0);
946 /* --------------------------------------------------------------------------------------------- */