(edit_get_syntax_color): return color directly.
[midnight-commander.git] / src / editor / editdraw.c
blobbfadfeb0cbcbee2228c2900817ccda900a6ab572
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_byte);
139 #ifdef HAVE_CHARSET
141 #endif
143 else
145 strcpy (byte_str, "<EOF> ");
148 /* The field lengths just prevent the status line from shortening too much */
149 if (simple_statusbar)
150 g_snprintf (s, w,
151 "%c%c%c%c %3ld %5ld/%ld %6ld/%ld %s %s",
152 edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
153 edit->modified ? 'M' : '-',
154 macro_index < 0 ? '-' : 'R',
155 edit->overwrite == 0 ? '-' : 'O',
156 edit->curs_col + edit->over_col,
157 edit->curs_line + 1,
158 edit->total_lines + 1, (long) edit->curs1, (long) edit->last_byte, byte_str,
159 #ifdef HAVE_CHARSET
160 mc_global.source_codepage >= 0 ? get_codepage_id (mc_global.source_codepage) :
161 #endif
162 "");
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 >= 0 ? get_codepage_id (mc_global.source_codepage) :
177 #endif
178 "");
181 /* --------------------------------------------------------------------------------------------- */
183 static inline void
184 printwstr (const char *s, int len)
186 if (len > 0)
187 tty_printf ("%-*.*s", len, len, s);
190 /* --------------------------------------------------------------------------------------------- */
192 static inline void
193 print_to_widget (WEdit * edit, long row, int start_col, int start_col_real,
194 long end_col, struct line_s line[], char *status, int bookmarked)
196 struct line_s *p;
198 int x = start_col_real;
199 int x1 = start_col + EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
200 int y = row + EDIT_TEXT_VERTICAL_OFFSET;
201 int cols_to_skip = abs (x);
202 int i;
203 int wrap_start;
204 int len;
206 tty_setcolor (EDITOR_NORMAL_COLOR);
207 if (bookmarked != 0)
208 tty_setcolor (bookmarked);
210 len = end_col + 1 - start_col;
211 wrap_start = option_word_wrap_line_length + edit->start_col;
213 if (len > 0 && edit->widget.y + y >= 0)
215 if (!show_right_margin || wrap_start > end_col)
216 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', len);
217 else if (wrap_start < 0)
219 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
220 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', len);
222 else
224 if (wrap_start > 0)
225 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', wrap_start);
227 len -= wrap_start;
228 if (len > 0)
230 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
231 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1 + wrap_start, ' ', len);
236 if (option_line_state)
238 tty_setcolor (LINE_STATE_COLOR);
239 for (i = 0; i < LINE_STATE_WIDTH; i++)
241 edit_move (x1 + i - option_line_state_width, y);
242 if (status[i] == '\0')
243 status[i] = ' ';
244 tty_print_char (status[i]);
248 edit_move (x1, y);
249 i = 1;
250 for (p = line; p->ch != 0; p++)
252 int style;
253 unsigned int textchar;
254 int color;
256 if (cols_to_skip != 0)
258 cols_to_skip--;
259 continue;
262 style = p->style & 0xFF00;
263 textchar = p->ch;
264 color = p->style >> 16;
266 if (style & MOD_ABNORMAL)
268 /* Non-printable - use black background */
269 color = 0;
272 if (style & MOD_WHITESPACE)
274 if (style & MOD_MARKED)
276 textchar = ' ';
277 tty_setcolor (EDITOR_MARKED_COLOR);
279 else
280 tty_setcolor (EDITOR_WHITESPACE_COLOR);
282 else if (style & MOD_BOLD)
283 tty_setcolor (EDITOR_BOLD_COLOR);
284 else if (style & MOD_MARKED)
285 tty_setcolor (EDITOR_MARKED_COLOR);
286 else
287 tty_lowlevel_setcolor (color);
289 if (show_right_margin)
291 if (i > option_word_wrap_line_length + edit->start_col)
292 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
293 i++;
296 tty_print_anychar (textchar);
300 /* --------------------------------------------------------------------------------------------- */
301 /** b is a pointer to the beginning of the line */
303 static void
304 edit_draw_this_line (WEdit * edit, off_t b, long row, long start_col, long end_col)
306 struct line_s line[MAX_LINE_LEN];
307 struct line_s *p = line;
309 off_t m1 = 0, m2 = 0, q;
310 long c1, c2;
311 int col, start_col_real;
312 unsigned int c;
313 int color;
314 int abn_style;
315 int i;
316 unsigned int cur_line = 0;
317 int book_mark = 0;
318 char line_stat[LINE_STATE_WIDTH + 1] = "\0";
320 if (row > edit->widget.lines - 1 - EDIT_TEXT_VERTICAL_OFFSET)
321 return;
323 if (book_mark_query_color (edit, edit->start_line + row, BOOK_MARK_COLOR))
324 book_mark = BOOK_MARK_COLOR;
325 else if (book_mark_query_color (edit, edit->start_line + row, BOOK_MARK_FOUND_COLOR))
326 book_mark = BOOK_MARK_FOUND_COLOR;
328 if (book_mark)
329 abn_style = book_mark << 16;
330 else
331 abn_style = MOD_ABNORMAL;
333 end_col -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
335 color = edit_get_syntax_color (edit, b - 1);
336 q = edit_move_forward3 (edit, b, start_col - edit->start_col, 0);
337 start_col_real = (col = (int) edit_move_forward3 (edit, b, 0, q)) + edit->start_col;
339 if (option_line_state)
341 cur_line = edit->start_line + row;
342 if (cur_line <= (unsigned int) edit->total_lines)
344 g_snprintf (line_stat, LINE_STATE_WIDTH + 1, "%7i ", cur_line + 1);
346 else
348 memset (line_stat, ' ', LINE_STATE_WIDTH);
349 line_stat[LINE_STATE_WIDTH] = '\0';
351 if (book_mark_query_color (edit, cur_line, BOOK_MARK_COLOR))
353 g_snprintf (line_stat, 2, "*");
357 if (col + 16 > -edit->start_col)
359 eval_marks (edit, &m1, &m2);
361 if (row <= edit->total_lines - edit->start_line)
363 off_t tws = 0;
364 if (tty_use_colors () && visible_tws)
366 tws = edit_eol (edit, b);
367 while (tws > b && ((c = edit_get_byte (edit, tws - 1)) == ' ' || c == '\t'))
368 tws--;
371 while (col <= end_col - edit->start_col)
373 int cw = 1;
374 int tab_over = 0;
375 gboolean wide_width_char = FALSE;
376 gboolean control_char = FALSE;
378 p->ch = 0;
379 p->style = 0;
380 if (q == edit->curs1)
381 p->style |= MOD_CURSOR;
382 if (q >= m1 && q < m2)
384 if (edit->column_highlight)
386 long x;
388 x = (long) edit_move_forward3 (edit, b, 0, q);
389 c1 = min (edit->column1, edit->column2);
390 c2 = max (edit->column1, edit->column2);
391 if (x >= c1 && x < c2)
392 p->style |= MOD_MARKED;
394 else
395 p->style |= MOD_MARKED;
397 if (q == edit->bracket)
398 p->style |= MOD_BOLD;
399 if (q >= edit->found_start && q < (off_t) (edit->found_start + edit->found_len))
400 p->style |= MOD_BOLD;
402 #ifdef HAVE_CHARSET
403 if (edit->utf8)
404 c = edit_get_utf (edit, q, &cw);
405 else
406 #endif
407 c = edit_get_byte (edit, q);
408 /* we don't use bg for mc - fg contains both */
409 if (book_mark)
411 p->style |= book_mark << 16;
413 else
415 color = edit_get_syntax_color (edit, q);
416 p->style |= color << 16;
418 switch (c)
420 case '\n':
421 col = end_col - edit->start_col + 1; /* quit */
422 break;
423 case '\t':
424 i = TAB_SIZE - ((int) col % TAB_SIZE);
425 tab_over = (end_col - edit->start_col) - (col + i - 1);
426 if (tab_over < 0)
427 i += tab_over;
428 col += i;
429 if (tty_use_colors () &&
430 ((visible_tabs || (visible_tws && q >= tws)) && enable_show_tabs_tws))
432 if (p->style & MOD_MARKED)
433 c = p->style;
434 else if (book_mark)
435 c |= book_mark << 16;
436 else
437 c = p->style | MOD_WHITESPACE;
438 if (i > 2)
440 p->ch = '<';
441 p->style = c;
442 p++;
443 while (--i > 1)
445 p->ch = '-';
446 p->style = c;
447 p++;
449 p->ch = '>';
450 p->style = c;
451 p++;
453 else if (i > 1)
455 p->ch = '<';
456 p->style = c;
457 p++;
458 p->ch = '>';
459 p->style = c;
460 p++;
462 else
464 p->ch = '>';
465 p->style = c;
466 p++;
469 else if (tty_use_colors () && visible_tws && q >= tws && enable_show_tabs_tws)
471 p->ch = '.';
472 p->style |= MOD_WHITESPACE;
473 c = p->style & ~MOD_CURSOR;
474 p++;
475 while (--i)
477 p->ch = ' ';
478 p->style = c;
479 p++;
482 else
484 p->ch |= ' ';
485 c = p->style & ~MOD_CURSOR;
486 p++;
487 while (--i)
489 p->ch = ' ';
490 p->style = c;
491 p++;
494 break;
495 case ' ':
496 if (tty_use_colors () && visible_tws && q >= tws && enable_show_tabs_tws)
498 p->ch = '.';
499 p->style |= MOD_WHITESPACE;
500 p++;
501 col++;
502 break;
504 /* fallthrough */
505 default:
506 #ifdef HAVE_CHARSET
507 if (mc_global.utf8_display)
509 if (!edit->utf8)
511 c = convert_from_8bit_to_utf_c ((unsigned char) c, edit->converter);
513 else
515 if (g_unichar_iswide (c))
517 wide_width_char = TRUE;
518 col++;
522 else if (edit->utf8)
523 c = convert_from_utf_to_current_c (c, edit->converter);
524 else
525 c = convert_to_display_c (c);
526 #endif
528 /* Caret notation for control characters */
529 if (c < 32)
531 p->ch = '^';
532 p->style = abn_style;
533 p++;
534 p->ch = c + 0x40;
535 p->style = abn_style;
536 p++;
537 col += 2;
538 control_char = TRUE;
539 break;
541 if (c == 127)
543 p->ch = '^';
544 p->style = abn_style;
545 p++;
546 p->ch = '?';
547 p->style = abn_style;
548 p++;
549 col += 2;
550 control_char = TRUE;
551 break;
553 #ifdef HAVE_CHARSET
554 if (edit->utf8)
556 if (g_unichar_isprint (c))
557 p->ch = c;
558 else
560 p->ch = '.';
561 p->style = abn_style;
563 p++;
565 else
566 #endif
568 if ((mc_global.utf8_display && g_unichar_isprint (c)) ||
569 (!mc_global.utf8_display && is_printable (c)))
571 p->ch = c;
572 p++;
574 else
576 p->ch = '.';
577 p->style = abn_style;
578 p++;
581 col++;
582 break;
583 } /* case */
585 q++;
586 if (cw > 1)
588 q += cw - 1;
591 if (col > (end_col - edit->start_col + 1))
593 if (wide_width_char)
595 p--;
596 break;
598 if (control_char)
600 p -= 2;
601 break;
607 else
609 start_col_real = start_col = 0;
612 p->ch = 0;
614 print_to_widget (edit, row, start_col, start_col_real, end_col, line, line_stat, book_mark);
617 /* --------------------------------------------------------------------------------------------- */
619 static inline void
620 edit_draw_this_char (WEdit * edit, off_t curs, long row, long start_column, long end_column)
622 off_t b = edit_bol (edit, curs);
624 edit_draw_this_line (edit, b, row, start_column, end_column);
627 /* --------------------------------------------------------------------------------------------- */
628 /** cursor must be in screen for other than REDRAW_PAGE passed in force */
630 static inline void
631 render_edit_text (WEdit * edit, long start_row, long start_column, long end_row, long end_column)
633 static long prev_curs_row = 0;
634 static off_t prev_curs = 0;
636 Widget *w = (Widget *) edit;
637 Dlg_head *h = w->owner;
639 long row = 0, curs_row;
640 int force = edit->force;
641 long b;
642 int y1, x1, y2, x2;
644 int last_line;
645 int last_column;
647 /* draw only visible region */
649 last_line = h->y + h->lines - 1;
651 y1 = w->y;
652 if (y1 > last_line - 1 /* buttonbar */ )
653 return;
655 last_column = h->x + h->cols - 1;
657 x1 = w->x;
658 if (x1 > last_column)
659 return;
661 y2 = w->y + w->lines - 1;
662 if (y2 < h->y + 1 /* menubar */ )
663 return;
665 x2 = w->x + w->cols - 1;
666 if (x2 < h->x)
667 return;
669 if ((force & REDRAW_IN_BOUNDS) == 0)
671 /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
672 /* draw only visible region */
674 if (y2 <= last_line - 1 /* buttonbar */ )
675 end_row = w->lines - 1;
676 else if (y1 >= h->y + 1 /* menubar */ )
677 end_row = h->lines - 1 - y1 - 1;
678 else
679 end_row = start_row + h->lines - 1 - 1;
681 if (x2 <= last_column)
682 end_column = w->cols - 1;
683 else if (x1 >= h->x)
684 end_column = h->cols - 1 - x1;
685 else
686 end_column = start_column + h->cols - 1;
690 * If the position of the page has not moved then we can draw the cursor
691 * character only. This will prevent line flicker when using arrow keys.
693 if ((force & REDRAW_CHAR_ONLY) == 0 || (force & REDRAW_PAGE) != 0)
695 if ((force & REDRAW_PAGE) != 0)
697 row = start_row;
698 b = edit_move_forward (edit, edit->start_display, start_row, 0);
699 while (row <= end_row)
701 if (key_pending (edit))
702 return;
703 edit_draw_this_line (edit, b, row, start_column, end_column);
704 b = edit_move_forward (edit, b, 1, 0);
705 row++;
708 else
710 curs_row = edit->curs_row;
712 if ((force & REDRAW_BEFORE_CURSOR) != 0 && start_row < curs_row)
714 long upto = curs_row - 1 <= end_row ? curs_row - 1 : end_row;
716 row = start_row;
717 b = edit->start_display;
718 while (row <= upto)
720 if (key_pending (edit))
721 return;
722 edit_draw_this_line (edit, b, row, start_column, end_column);
723 b = edit_move_forward (edit, b, 1, 0);
727 /* if (force & REDRAW_LINE) ---> default */
728 b = edit_bol (edit, edit->curs1);
729 if (curs_row >= start_row && curs_row <= end_row)
731 if (key_pending (edit))
732 return;
733 edit_draw_this_line (edit, b, curs_row, start_column, end_column);
736 if ((force & REDRAW_AFTER_CURSOR) != 0 && end_row > curs_row)
738 row = curs_row + 1 < start_row ? start_row : curs_row + 1;
739 b = edit_move_forward (edit, b, 1, 0);
740 while (row <= end_row)
742 if (key_pending (edit))
743 return;
744 edit_draw_this_line (edit, b, row, start_column, end_column);
745 b = edit_move_forward (edit, b, 1, 0);
746 row++;
750 if ((force & REDRAW_LINE_ABOVE) != 0 && curs_row >= 1)
752 row = curs_row - 1;
753 b = edit_move_backward (edit, edit_bol (edit, edit->curs1), 1);
754 if (row >= start_row && row <= end_row)
756 if (key_pending (edit))
757 return;
758 edit_draw_this_line (edit, b, row, start_column, end_column);
762 if ((force & REDRAW_LINE_BELOW) != 0 && row < edit->widget.lines - 1)
764 row = curs_row + 1;
765 b = edit_bol (edit, edit->curs1);
766 b = edit_move_forward (edit, b, 1, 0);
767 if (row >= start_row && row <= end_row)
769 if (key_pending (edit))
770 return;
771 edit_draw_this_line (edit, b, row, start_column, end_column);
776 else if (prev_curs_row < edit->curs_row)
778 /* with the new text highlighting, we must draw from the top down */
779 edit_draw_this_char (edit, prev_curs, prev_curs_row, start_column, end_column);
780 edit_draw_this_char (edit, edit->curs1, edit->curs_row, start_column, end_column);
782 else
784 edit_draw_this_char (edit, edit->curs1, edit->curs_row, start_column, end_column);
785 edit_draw_this_char (edit, prev_curs, prev_curs_row, start_column, end_column);
788 edit->force = 0;
790 prev_curs_row = edit->curs_row;
791 prev_curs = edit->curs1;
794 /* --------------------------------------------------------------------------------------------- */
796 static inline void
797 edit_render (WEdit * edit, int page, int row_start, int col_start, int row_end, int col_end)
799 if (page) /* if it was an expose event, 'page' would be set */
800 edit->force |= REDRAW_PAGE | REDRAW_IN_BOUNDS;
802 if (edit->force & REDRAW_COMPLETELY)
803 buttonbar_redraw (find_buttonbar (edit->widget.owner));
804 render_edit_text (edit, row_start, col_start, row_end, col_end);
806 * edit->force != 0 means a key was pending and the redraw
807 * was halted, so next time we must redraw everything in case stuff
808 * was left undrawn from a previous key press.
810 if (edit->force)
811 edit->force |= REDRAW_PAGE;
814 /* --------------------------------------------------------------------------------------------- */
815 /*** public functions ****************************************************************************/
816 /* --------------------------------------------------------------------------------------------- */
818 /** Draw the status line at the top of the screen. The size of the filename
819 * field varies depending on the width of the screen and the length of
820 * the filename. */
821 void
822 edit_status (WEdit * edit)
824 const int w = edit->widget.owner->cols;
825 const size_t status_size = w + 1;
826 char *const status = g_malloc (status_size);
827 int status_len;
828 const char *fname = "";
829 int fname_len;
830 const int gap = 3; /* between the filename and the status */
831 const int right_gap = 5; /* at the right end of the screen */
832 const int preferred_fname_len = 16;
834 status_string (edit, status, status_size);
835 status_len = (int) str_term_width1 (status);
837 if (edit->filename_vpath != NULL)
838 fname = vfs_path_get_last_path_str (edit->filename_vpath);
840 fname_len = str_term_width1 (fname);
841 if (fname_len < preferred_fname_len)
842 fname_len = preferred_fname_len;
844 if (fname_len + gap + status_len + right_gap >= w)
846 if (preferred_fname_len + gap + status_len + right_gap >= w)
847 fname_len = preferred_fname_len;
848 else
849 fname_len = w - (gap + status_len + right_gap);
850 fname = str_trunc (fname, fname_len);
853 dlg_move (edit->widget.owner, 0, 0);
854 tty_setcolor (STATUSBAR_COLOR);
855 printwstr (fname, fname_len + gap);
856 printwstr (status, w - (fname_len + gap));
858 if (simple_statusbar && w > EDITOR_MINIMUM_TERMINAL_WIDTH)
860 size_t percent = 100;
862 if (edit->total_lines + 1 != 0)
863 percent = (edit->curs_line + 1) * 100 / (edit->total_lines + 1);
864 dlg_move (edit->widget.owner, 0, w - 5);
865 tty_printf (" %3d%%", percent);
868 g_free (status);
871 /* --------------------------------------------------------------------------------------------- */
872 /** this scrolls the text so that cursor is on the screen */
873 void
874 edit_scroll_screen_over_cursor (WEdit * edit)
876 long p;
877 long outby;
878 int b_extreme, t_extreme, l_extreme, r_extreme;
880 if (edit->widget.lines <= 0 || edit->widget.cols <= 0)
881 return;
883 edit->widget.cols -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
884 edit->widget.lines -= EDIT_TEXT_VERTICAL_OFFSET;
886 r_extreme = EDIT_RIGHT_EXTREME;
887 l_extreme = EDIT_LEFT_EXTREME;
888 b_extreme = EDIT_BOTTOM_EXTREME;
889 t_extreme = EDIT_TOP_EXTREME;
890 if (edit->found_len != 0)
892 b_extreme = max (edit->widget.lines / 4, b_extreme);
893 t_extreme = max (edit->widget.lines / 4, t_extreme);
895 if (b_extreme + t_extreme + 1 > edit->widget.lines)
897 int n;
899 n = b_extreme + t_extreme;
900 if (n == 0)
901 n = 1;
902 b_extreme = (b_extreme * (edit->widget.lines - 1)) / n;
903 t_extreme = (t_extreme * (edit->widget.lines - 1)) / n;
905 if (l_extreme + r_extreme + 1 > edit->widget.cols)
907 int n;
909 n = l_extreme + t_extreme;
910 if (n == 0)
911 n = 1;
912 l_extreme = (l_extreme * (edit->widget.cols - 1)) / n;
913 r_extreme = (r_extreme * (edit->widget.cols - 1)) / n;
915 p = edit_get_col (edit) + edit->over_col;
916 edit_update_curs_row (edit);
917 outby = p + edit->start_col - edit->widget.cols + 1 + (r_extreme + edit->found_len);
918 if (outby > 0)
919 edit_scroll_right (edit, outby);
920 outby = l_extreme - p - edit->start_col;
921 if (outby > 0)
922 edit_scroll_left (edit, outby);
923 p = edit->curs_row;
924 outby = p - edit->widget.lines + 1 + b_extreme;
925 if (outby > 0)
926 edit_scroll_downward (edit, outby);
927 outby = t_extreme - p;
928 if (outby > 0)
929 edit_scroll_upward (edit, outby);
930 edit_update_curs_row (edit);
932 edit->widget.lines += EDIT_TEXT_VERTICAL_OFFSET;
933 edit->widget.cols += EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
936 /* --------------------------------------------------------------------------------------------- */
938 void
939 edit_render_keypress (WEdit * edit)
941 edit_render (edit, 0, 0, 0, 0, 0);
944 /* --------------------------------------------------------------------------------------------- */