Ticket #1652: autodetect line-endings
[midnight-commander.git] / src / editor / editdraw.c
blob724f78fd1815cbfa029186b7224343ef693a5b52
1 /*
2 Editor text drawing.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2011, 2012
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer, 1996, 1997
10 Andrew Borodin <aborodin@vmail.ru> 2012
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file
29 * \brief Source: editor text drawing
30 * \author Paul Sheer
31 * \date 1996, 1997
34 #include <config.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <sys/stat.h>
45 #include "lib/global.h"
46 #include "lib/tty/tty.h" /* tty_printf() */
47 #include "lib/tty/key.h" /* is_idle() */
48 #include "lib/skin.h"
49 #include "lib/strutil.h" /* utf string functions */
50 #include "lib/util.h" /* is_printable() */
51 #include "lib/widget.h"
52 #ifdef HAVE_CHARSET
53 #include "lib/charsets.h"
54 #endif
56 #include "src/setup.h" /* edit_tab_spacing */
57 #include "src/main.h" /* macro_index */
59 #include "edit-impl.h"
60 #include "editwidget.h"
62 /*** global variables ****************************************************************************/
64 /* Toggles statusbar draw style */
65 int simple_statusbar = 0;
67 int visible_tabs = 1, visible_tws = 1;
69 /*** file scope macro definitions ****************************************************************/
71 #define MAX_LINE_LEN 1024
73 /* Text styles */
74 #define MOD_ABNORMAL (1 << 8)
75 #define MOD_BOLD (1 << 9)
76 #define MOD_MARKED (1 << 10)
77 #define MOD_CURSOR (1 << 11)
78 #define MOD_WHITESPACE (1 << 12)
80 #define edit_move(x,y) widget_move(edit, y, x);
82 #define key_pending(x) (!is_idle())
84 #define EDITOR_MINIMUM_TERMINAL_WIDTH 30
86 /*** file scope type declarations ****************************************************************/
88 struct line_s
90 unsigned int ch;
91 unsigned int style;
94 /*** file scope variables ************************************************************************/
96 /*** file scope functions ************************************************************************/
98 static inline void
99 printwstr (const char *s, int len)
101 if (len > 0)
102 tty_printf ("%-*.*s", len, len, s);
105 /* --------------------------------------------------------------------------------------------- */
107 static inline void
108 status_string (WEdit * edit, char *s, int w)
110 char byte_str[16];
111 unsigned char cur_byte = 0;
112 unsigned int cur_utf = 0;
113 int cw = 1;
115 static const char *lb_names[LB_NAMES] = {
117 "LF",
118 "CRLF",
119 "CR"
123 * If we are at the end of file, print <EOF>,
124 * otherwise print the current character as is (if printable),
125 * as decimal and as hex.
127 if (edit->curs1 < edit->last_byte)
129 if (!edit->utf8)
131 cur_byte = edit_get_byte (edit, edit->curs1);
133 g_snprintf (byte_str, sizeof (byte_str), "%4d 0x%03X",
134 (int) cur_byte, (unsigned) cur_byte);
136 else
138 cur_utf = edit_get_utf (edit, edit->curs1, &cw);
139 if (cw > 0)
141 g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
142 (unsigned) cur_utf, (unsigned) cur_utf);
144 else
146 cur_utf = edit_get_byte (edit, edit->curs1);
147 g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
148 (int) cur_utf, (unsigned) cur_utf);
153 else
155 strcpy (byte_str, "<EOF> ");
158 /* The field lengths just prevent the status line from shortening too much */
159 if (simple_statusbar)
160 g_snprintf (s, w,
161 "%c%c%c%c %3ld %5ld/%ld %6ld/%ld %s %s %s",
162 edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
163 edit->modified ? 'M' : '-',
164 macro_index < 0 ? '-' : 'R',
165 edit->overwrite == 0 ? '-' : 'O',
166 edit->curs_col + edit->over_col,
167 edit->curs_line + 1,
168 edit->total_lines + 1, edit->curs1, edit->last_byte, byte_str,
169 #ifdef HAVE_CHARSET
170 mc_global.source_codepage >=
171 0 ? get_codepage_id (mc_global.source_codepage) : "",
172 #else
174 #endif
175 lb_names[edit->lb]);
176 else
177 g_snprintf (s, w,
178 "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb) %s %s %s",
179 edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
180 edit->modified ? 'M' : '-',
181 macro_index < 0 ? '-' : 'R',
182 edit->overwrite == 0 ? '-' : 'O',
183 edit->curs_col + edit->over_col,
184 edit->start_line + 1,
185 edit->curs_row,
186 edit->curs_line + 1,
187 edit->total_lines + 1, edit->curs1, edit->last_byte, byte_str,
188 #ifdef HAVE_CHARSET
189 mc_global.source_codepage >=
190 0 ? get_codepage_id (mc_global.source_codepage) : "",
191 #else
193 #endif
194 lb_names[edit->lb]);
197 /* --------------------------------------------------------------------------------------------- */
199 * Draw the status line at the top of the screen for fullscreen editor window.
201 * @param edit editor object
202 * @param color color pair
205 static inline void
206 edit_status_fullscreen (WEdit * edit, int color)
208 const int w = edit->widget.owner->cols;
209 const size_t status_size = w + 1;
210 char *const status = g_malloc (status_size);
211 int status_len;
212 const char *fname = "";
213 int fname_len;
214 const int gap = 3; /* between the filename and the status */
215 const int right_gap = 5; /* at the right end of the screen */
216 const int preferred_fname_len = 16;
218 status_string (edit, status, status_size);
219 status_len = (int) str_term_width1 (status);
221 if (edit->filename_vpath != NULL)
222 fname = x_basename (vfs_path_get_last_path_str (edit->filename_vpath));
224 fname_len = str_term_width1 (fname);
225 if (fname_len < preferred_fname_len)
226 fname_len = preferred_fname_len;
228 if (fname_len + gap + status_len + right_gap >= w)
230 if (preferred_fname_len + gap + status_len + right_gap >= w)
231 fname_len = preferred_fname_len;
232 else
233 fname_len = w - (gap + status_len + right_gap);
234 fname = str_trunc (fname, fname_len);
237 dlg_move (edit->widget.owner, 0, 0);
238 tty_setcolor (color);
239 printwstr (fname, fname_len + gap);
240 printwstr (status, w - (fname_len + gap));
242 if (simple_statusbar && w > EDITOR_MINIMUM_TERMINAL_WIDTH)
244 size_t percent = 100;
246 if (edit->total_lines + 1 != 0)
247 percent = (edit->curs_line + 1) * 100 / (edit->total_lines + 1);
248 dlg_move (edit->widget.owner, 0, w - 6 - 6);
249 tty_printf (" %3d%%", percent);
252 g_free (status);
255 /* --------------------------------------------------------------------------------------------- */
257 * Draw status line for editor window if window is not in fullscreen mode.
259 * @param edit editor object
262 static inline void
263 edit_status_window (WEdit * edit)
265 int y, x;
266 int cols = edit->widget.cols;
268 tty_setcolor (STATUSBAR_COLOR);
270 if (cols > 5)
272 const char *fname = N_("NoName");
273 char *full_fname = NULL;
275 if (edit->filename_vpath != NULL)
277 full_fname = vfs_path_to_str (edit->filename_vpath);
278 fname = x_basename (full_fname);
280 #ifdef ENABLE_NLS
281 else
282 fname = _(fname);
283 #endif
285 edit_move (2, 0);
286 tty_printf ("[%s]", str_term_trim (fname, edit->widget.cols - 8 - 6));
287 g_free (full_fname);
290 tty_getyx (&y, &x);
291 x -= edit->widget.x;
292 x += 4;
293 if (x + 6 <= cols - 2 - 6)
295 edit_move (x, 0);
296 tty_printf ("[%c%c%c%c]",
297 edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
298 edit->modified ? 'M' : '-',
299 macro_index < 0 ? '-' : 'R', edit->overwrite == 0 ? '-' : 'O');
302 if (cols > 30)
304 edit_move (2, edit->widget.lines - 1);
305 tty_printf ("%3ld %5ld/%ld %6ld/%ld",
306 edit->curs_col + edit->over_col,
307 edit->curs_line + 1, edit->total_lines + 1, edit->curs1, edit->last_byte);
311 * If we are at the end of file, print <EOF>,
312 * otherwise print the current character as is (if printable),
313 * as decimal and as hex.
315 if (cols > 46)
317 edit_move (32, edit->widget.lines - 1);
318 if (edit->curs1 >= edit->last_byte)
319 tty_print_string ("[<EOF> ]");
320 #ifdef HAVE_CHARSET
321 else if (edit->utf8)
323 unsigned int cur_utf;
324 int cw = 1;
326 cur_utf = edit_get_utf (edit, edit->curs1, &cw);
327 if (cw <= 0)
328 cur_utf = edit_get_byte (edit, edit->curs1);
329 tty_printf ("[%05d 0x%04X]", cur_utf, cur_utf);
331 #endif
332 else
334 unsigned char cur_byte;
336 cur_byte = edit_get_byte (edit, edit->curs1);
337 tty_printf ("[%05d 0x%04X]", (unsigned int) cur_byte, (unsigned int) cur_byte);
342 /* --------------------------------------------------------------------------------------------- */
344 * Draw a frame around edit area.
346 * @param edit editor object
347 * @param color color pair
348 * @param active TRUE if editor object is focused
351 static inline void
352 edit_draw_frame (const WEdit * edit, int color, gboolean active)
354 const Widget *w = (const Widget *) edit;
356 /* draw a frame around edit area */
357 tty_setcolor (color);
358 /* draw double frame for active window if skin supports that */
359 tty_draw_box (w->y, w->x, w->lines, w->cols, !active);
360 /* draw a drag marker */
361 if (edit->drag_state == MCEDIT_DRAG_NORMAL)
363 tty_setcolor (EDITOR_FRAME_DRAG);
364 widget_move (w, w->lines - 1, w->cols - 1);
365 tty_print_alt_char (ACS_LRCORNER, TRUE);
369 /* --------------------------------------------------------------------------------------------- */
371 * Draw a window control buttons.
373 * @param edit editor object
374 * @param color color pair
377 static inline void
378 edit_draw_window_icons (const WEdit * edit, int color)
380 const Widget *w = (const Widget *) edit;
381 char tmp[17];
383 tty_setcolor (color);
384 if (edit->fullscreen)
385 dlg_move (w->owner, 0, w->owner->cols - 6);
386 else
387 widget_move (w, 0, edit->widget.cols - 8);
388 g_snprintf (tmp, sizeof (tmp), "[%s][%s]", edit_window_state_char, edit_window_close_char);
389 tty_print_string (tmp);
392 /* --------------------------------------------------------------------------------------------- */
394 static inline void
395 print_to_widget (WEdit * edit, long row, int start_col, int start_col_real,
396 long end_col, struct line_s line[], char *status, int bookmarked)
398 struct line_s *p;
400 int x = start_col_real;
401 int x1 = start_col + EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
402 int y = row + EDIT_TEXT_VERTICAL_OFFSET;
403 int cols_to_skip = abs (x);
404 int i;
405 int wrap_start;
406 int len;
408 if (!edit->fullscreen)
410 x1++;
411 y++;
414 tty_setcolor (EDITOR_NORMAL_COLOR);
415 if (bookmarked != 0)
416 tty_setcolor (bookmarked);
418 len = end_col + 1 - start_col;
419 wrap_start = option_word_wrap_line_length + edit->start_col;
421 if (len > 0 && edit->widget.y + y >= 0)
423 if (!show_right_margin || wrap_start > end_col)
424 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', len);
425 else if (wrap_start < 0)
427 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
428 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', len);
430 else
432 if (wrap_start > 0)
433 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', wrap_start);
435 len -= wrap_start;
436 if (len > 0)
438 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
439 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1 + wrap_start, ' ', len);
444 if (option_line_state)
446 tty_setcolor (LINE_STATE_COLOR);
447 for (i = 0; i < LINE_STATE_WIDTH; i++)
449 edit_move (x1 + i - option_line_state_width, y);
450 if (status[i] == '\0')
451 status[i] = ' ';
452 tty_print_char (status[i]);
456 edit_move (x1, y);
457 i = 1;
458 for (p = line; p->ch != 0; p++)
460 int style;
461 unsigned int textchar;
462 int color;
464 if (cols_to_skip != 0)
466 cols_to_skip--;
467 continue;
470 style = p->style & 0xFF00;
471 textchar = p->ch;
472 color = p->style >> 16;
474 if (style & MOD_ABNORMAL)
476 /* Non-printable - use black background */
477 color = 0;
480 if (style & MOD_WHITESPACE)
482 if (style & MOD_MARKED)
484 textchar = ' ';
485 tty_setcolor (EDITOR_MARKED_COLOR);
487 else
488 tty_setcolor (EDITOR_WHITESPACE_COLOR);
490 else if (style & MOD_BOLD)
491 tty_setcolor (EDITOR_BOLD_COLOR);
492 else if (style & MOD_MARKED)
493 tty_setcolor (EDITOR_MARKED_COLOR);
494 else
495 tty_lowlevel_setcolor (color);
497 if (show_right_margin)
499 if (i > option_word_wrap_line_length + edit->start_col)
500 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
501 i++;
504 tty_print_anychar (textchar);
508 /* --------------------------------------------------------------------------------------------- */
509 /** b is a pointer to the beginning of the line */
511 static void
512 edit_draw_this_line (WEdit * edit, long b, long row, long start_col, long end_col)
514 struct line_s line[MAX_LINE_LEN];
515 struct line_s *p = line;
517 long m1 = 0, m2 = 0, q, c1, c2;
518 int col, start_col_real;
519 unsigned int c;
520 int color;
521 int abn_style;
522 int i;
523 int utf8lag = 0;
524 unsigned int cur_line = 0;
525 int book_mark = 0;
526 char line_stat[LINE_STATE_WIDTH + 1] = "\0";
528 if (row > edit->widget.lines - 1 - EDIT_TEXT_VERTICAL_OFFSET - 2 * (edit->fullscreen ? 0 : 1))
529 return;
531 if (book_mark_query_color (edit, edit->start_line + row, BOOK_MARK_COLOR))
532 book_mark = BOOK_MARK_COLOR;
533 else if (book_mark_query_color (edit, edit->start_line + row, BOOK_MARK_FOUND_COLOR))
534 book_mark = BOOK_MARK_FOUND_COLOR;
536 if (book_mark)
537 abn_style = book_mark << 16;
538 else
539 abn_style = MOD_ABNORMAL;
541 end_col -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
542 if (!edit->fullscreen)
544 const Widget *w = (const Widget *) edit;
546 end_col--;
547 if (w->x + w->cols <= w->owner->cols)
548 end_col--;
551 edit_get_syntax_color (edit, b - 1, &color);
552 q = edit_move_forward3 (edit, b, start_col - edit->start_col, 0);
553 start_col_real = (col = (int) edit_move_forward3 (edit, b, 0, q)) + edit->start_col;
555 if (option_line_state)
557 cur_line = edit->start_line + row;
558 if (cur_line <= (unsigned int) edit->total_lines)
560 g_snprintf (line_stat, LINE_STATE_WIDTH + 1, "%7i ", cur_line + 1);
562 else
564 memset (line_stat, ' ', LINE_STATE_WIDTH);
565 line_stat[LINE_STATE_WIDTH] = '\0';
567 if (book_mark_query_color (edit, cur_line, BOOK_MARK_COLOR))
569 g_snprintf (line_stat, 2, "*");
573 if (col + 16 > -edit->start_col)
575 eval_marks (edit, &m1, &m2);
577 if (row <= edit->total_lines - edit->start_line)
579 long tws = 0;
580 if (tty_use_colors () && visible_tws)
582 tws = edit_eol (edit, b);
583 while (tws > b && ((c = edit_get_byte (edit, tws - 1)) == ' ' || c == '\t'))
584 tws--;
587 while (col <= end_col - edit->start_col)
589 int cw = 1;
590 int tab_over = 0;
591 gboolean wide_width_char = FALSE;
592 gboolean control_char = FALSE;
594 p->ch = 0;
595 p->style = 0;
596 if (q == edit->curs1)
597 p->style |= MOD_CURSOR;
598 if (q >= m1 && q < m2)
600 if (edit->column_highlight)
602 int x;
603 x = edit_move_forward3 (edit, b, 0, q);
604 c1 = min (edit->column1, edit->column2);
605 c2 = max (edit->column1, edit->column2);
606 if (x >= c1 && x < c2)
607 p->style |= MOD_MARKED;
609 else
610 p->style |= MOD_MARKED;
612 if (q == edit->bracket)
613 p->style |= MOD_BOLD;
614 if (q >= edit->found_start && q < edit->found_start + edit->found_len)
615 p->style |= MOD_BOLD;
617 if (!edit->utf8)
619 c = edit_get_byte (edit, q);
621 else
623 c = edit_get_utf (edit, q, &cw);
625 /* we don't use bg for mc - fg contains both */
626 if (book_mark)
628 p->style |= book_mark << 16;
630 else
632 edit_get_syntax_color (edit, q, &color);
633 p->style |= color << 16;
635 switch (c)
637 case '\n':
638 col = (end_col + utf8lag) - edit->start_col + 1; /* quit */
639 break;
640 case '\t':
641 i = TAB_SIZE - ((int) col % TAB_SIZE);
642 tab_over = (end_col - edit->start_col) - (col + i - 1);
643 if (tab_over < 0)
644 i += tab_over;
645 col += i;
646 if (tty_use_colors () &&
647 ((visible_tabs || (visible_tws && q >= tws)) && enable_show_tabs_tws))
649 if (p->style & MOD_MARKED)
650 c = p->style;
651 else if (book_mark)
652 c |= book_mark << 16;
653 else
654 c = p->style | MOD_WHITESPACE;
655 if (i > 2)
657 p->ch = '<';
658 p->style = c;
659 p++;
660 while (--i > 1)
662 p->ch = '-';
663 p->style = c;
664 p++;
666 p->ch = '>';
667 p->style = c;
668 p++;
670 else if (i > 1)
672 p->ch = '<';
673 p->style = c;
674 p++;
675 p->ch = '>';
676 p->style = c;
677 p++;
679 else
681 p->ch = '>';
682 p->style = c;
683 p++;
686 else if (tty_use_colors () && visible_tws && q >= tws && enable_show_tabs_tws)
688 p->ch = '.';
689 p->style |= MOD_WHITESPACE;
690 c = p->style & ~MOD_CURSOR;
691 p++;
692 while (--i)
694 p->ch = ' ';
695 p->style = c;
696 p++;
699 else
701 p->ch |= ' ';
702 c = p->style & ~MOD_CURSOR;
703 p++;
704 while (--i)
706 p->ch = ' ';
707 p->style = c;
708 p++;
711 break;
712 case ' ':
713 if (tty_use_colors () && visible_tws && q >= tws && enable_show_tabs_tws)
715 p->ch = '.';
716 p->style |= MOD_WHITESPACE;
717 p++;
718 col++;
719 break;
721 /* fallthrough */
722 default:
723 #ifdef HAVE_CHARSET
724 if (mc_global.utf8_display)
726 if (!edit->utf8)
728 c = convert_from_8bit_to_utf_c ((unsigned char) c, edit->converter);
730 else
732 if (g_unichar_iswide (c))
734 wide_width_char = TRUE;
735 col++;
739 else if (edit->utf8)
740 c = convert_from_utf_to_current_c (c, edit->converter);
741 else
742 c = convert_to_display_c (c);
743 #endif
745 /* Caret notation for control characters */
746 if (c < 32)
748 p->ch = '^';
749 p->style = abn_style;
750 p++;
751 p->ch = c + 0x40;
752 p->style = abn_style;
753 p++;
754 col += 2;
755 control_char = TRUE;
756 break;
758 if (c == 127)
760 p->ch = '^';
761 p->style = abn_style;
762 p++;
763 p->ch = '?';
764 p->style = abn_style;
765 p++;
766 col += 2;
767 control_char = TRUE;
768 break;
770 if (!edit->utf8)
772 if ((mc_global.utf8_display && g_unichar_isprint (c)) ||
773 (!mc_global.utf8_display && is_printable (c)))
775 p->ch = c;
776 p++;
778 else
780 p->ch = '.';
781 p->style = abn_style;
782 p++;
785 else
787 if (g_unichar_isprint (c))
789 p->ch = c;
790 p++;
792 else
794 p->ch = '.';
795 p->style = abn_style;
796 p++;
799 col++;
800 break;
801 } /* case */
803 q++;
804 if (cw > 1)
806 q += cw - 1;
809 if (col > (end_col - edit->start_col + 1))
811 if (wide_width_char)
813 p--;
814 break;
816 if (control_char)
818 p -= 2;
819 break;
825 else
827 start_col_real = start_col = 0;
830 p->ch = 0;
832 print_to_widget (edit, row, start_col, start_col_real, end_col, line, line_stat, book_mark);
835 /* --------------------------------------------------------------------------------------------- */
837 static inline void
838 edit_draw_this_char (WEdit * edit, long curs, long row, long start_column, long end_column)
840 int b = edit_bol (edit, curs);
841 edit_draw_this_line (edit, b, row, start_column, end_column);
844 /* --------------------------------------------------------------------------------------------- */
845 /** cursor must be in screen for other than REDRAW_PAGE passed in force */
847 static inline void
848 render_edit_text (WEdit * edit, long start_row, long start_column, long end_row, long end_column)
850 static long prev_curs_row = 0;
851 static long prev_curs = 0;
853 Widget *w = (Widget *) edit;
854 Dlg_head *h = w->owner;
856 long row = 0, curs_row;
857 int force = edit->force;
858 long b;
859 int y1, x1, y2, x2;
861 int last_line;
862 int last_column;
864 /* draw only visible region */
866 last_line = h->y + h->lines - 1;
868 y1 = w->y;
869 if (y1 > last_line - 1 /* buttonbar */ )
870 return;
872 last_column = h->x + h->cols - 1;
874 x1 = w->x;
875 if (x1 > last_column)
876 return;
878 y2 = w->y + w->lines - 1;
879 if (y2 < h->y + 1 /* menubar */ )
880 return;
882 x2 = w->x + w->cols - 1;
883 if (x2 < h->x)
884 return;
886 if ((force & REDRAW_IN_BOUNDS) == 0)
888 /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
889 /* draw only visible region */
891 if (y2 <= last_line - 1 /* buttonbar */ )
892 end_row = w->lines - 1;
893 else if (y1 >= h->y + 1 /* menubar */ )
894 end_row = h->lines - 1 - y1 - 1;
895 else
896 end_row = start_row + h->lines - 1 - 1;
898 if (x2 <= last_column)
899 end_column = w->cols - 1;
900 else if (x1 >= h->x)
901 end_column = h->cols - 1 - x1;
902 else
903 end_column = start_column + h->cols - 1;
907 * If the position of the page has not moved then we can draw the cursor
908 * character only. This will prevent line flicker when using arrow keys.
910 if ((force & REDRAW_CHAR_ONLY) == 0 || (force & REDRAW_PAGE) != 0)
912 if ((force & REDRAW_PAGE) != 0)
914 row = start_row;
915 b = edit_move_forward (edit, edit->start_display, start_row, 0);
916 while (row <= end_row)
918 if (key_pending (edit))
919 return;
920 edit_draw_this_line (edit, b, row, start_column, end_column);
921 b = edit_move_forward (edit, b, 1, 0);
922 row++;
925 else
927 curs_row = edit->curs_row;
929 if ((force & REDRAW_BEFORE_CURSOR) != 0 && start_row < curs_row)
931 long upto = curs_row - 1 <= end_row ? curs_row - 1 : end_row;
933 row = start_row;
934 b = edit->start_display;
935 while (row <= upto)
937 if (key_pending (edit))
938 return;
939 edit_draw_this_line (edit, b, row, start_column, end_column);
940 b = edit_move_forward (edit, b, 1, 0);
944 /* if (force & REDRAW_LINE) ---> default */
945 b = edit_bol (edit, edit->curs1);
946 if (curs_row >= start_row && curs_row <= end_row)
948 if (key_pending (edit))
949 return;
950 edit_draw_this_line (edit, b, curs_row, start_column, end_column);
953 if ((force & REDRAW_AFTER_CURSOR) != 0 && end_row > curs_row)
955 row = curs_row + 1 < start_row ? start_row : curs_row + 1;
956 b = edit_move_forward (edit, b, 1, 0);
957 while (row <= end_row)
959 if (key_pending (edit))
960 return;
961 edit_draw_this_line (edit, b, row, start_column, end_column);
962 b = edit_move_forward (edit, b, 1, 0);
963 row++;
967 if ((force & REDRAW_LINE_ABOVE) != 0 && curs_row >= 1)
969 row = curs_row - 1;
970 b = edit_move_backward (edit, edit_bol (edit, edit->curs1), 1);
971 if (row >= start_row && row <= end_row)
973 if (key_pending (edit))
974 return;
975 edit_draw_this_line (edit, b, row, start_column, end_column);
979 if ((force & REDRAW_LINE_BELOW) != 0 && row < edit->widget.lines - 1)
981 row = curs_row + 1;
982 b = edit_bol (edit, edit->curs1);
983 b = edit_move_forward (edit, b, 1, 0);
984 if (row >= start_row && row <= end_row)
986 if (key_pending (edit))
987 return;
988 edit_draw_this_line (edit, b, row, start_column, end_column);
993 else if (prev_curs_row < edit->curs_row)
995 /* with the new text highlighting, we must draw from the top down */
996 edit_draw_this_char (edit, prev_curs, prev_curs_row, start_column, end_column);
997 edit_draw_this_char (edit, edit->curs1, edit->curs_row, start_column, end_column);
999 else
1001 edit_draw_this_char (edit, edit->curs1, edit->curs_row, start_column, end_column);
1002 edit_draw_this_char (edit, prev_curs, prev_curs_row, start_column, end_column);
1005 edit->force = 0;
1007 prev_curs_row = edit->curs_row;
1008 prev_curs = edit->curs1;
1011 /* --------------------------------------------------------------------------------------------- */
1013 static inline void
1014 edit_render (WEdit * edit, int page, int row_start, int col_start, int row_end, int col_end)
1016 if (page) /* if it was an expose event, 'page' would be set */
1017 edit->force |= REDRAW_PAGE | REDRAW_IN_BOUNDS;
1019 render_edit_text (edit, row_start, col_start, row_end, col_end);
1022 * edit->force != 0 means a key was pending and the redraw
1023 * was halted, so next time we must redraw everything in case stuff
1024 * was left undrawn from a previous key press.
1026 if (edit->force)
1027 edit->force |= REDRAW_PAGE;
1030 /* --------------------------------------------------------------------------------------------- */
1031 /*** public functions ****************************************************************************/
1032 /* --------------------------------------------------------------------------------------------- */
1034 void
1035 edit_status (WEdit * edit, gboolean active)
1037 int color;
1039 if (edit->fullscreen)
1041 color = STATUSBAR_COLOR;
1042 edit_status_fullscreen (edit, color);
1044 else
1046 color = edit->drag_state != MCEDIT_DRAG_NORMAL ? EDITOR_FRAME_DRAG : active ?
1047 EDITOR_FRAME_ACTIVE : EDITOR_FRAME;
1048 edit_draw_frame (edit, color, active);
1049 edit_status_window (edit);
1052 edit_draw_window_icons (edit, color);
1055 /* --------------------------------------------------------------------------------------------- */
1057 /** this scrolls the text so that cursor is on the screen */
1058 void
1059 edit_scroll_screen_over_cursor (WEdit * edit)
1061 int p;
1062 int outby;
1063 int b_extreme, t_extreme, l_extreme, r_extreme;
1065 if (edit->widget.lines <= 0 || edit->widget.cols <= 0)
1066 return;
1068 edit->widget.lines -= EDIT_TEXT_VERTICAL_OFFSET;
1069 edit->widget.cols -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
1071 if (!edit->fullscreen)
1073 edit->widget.x++;
1074 edit->widget.cols -= 2;
1075 edit->widget.y++;
1076 edit->widget.lines -= 2;
1079 r_extreme = EDIT_RIGHT_EXTREME;
1080 l_extreme = EDIT_LEFT_EXTREME;
1081 b_extreme = EDIT_BOTTOM_EXTREME;
1082 t_extreme = EDIT_TOP_EXTREME;
1083 if (edit->found_len)
1085 b_extreme = max (edit->widget.lines / 4, b_extreme);
1086 t_extreme = max (edit->widget.lines / 4, t_extreme);
1088 if (b_extreme + t_extreme + 1 > edit->widget.lines)
1090 int n;
1092 n = b_extreme + t_extreme;
1093 if (n == 0)
1094 n = 1;
1095 b_extreme = (b_extreme * (edit->widget.lines - 1)) / n;
1096 t_extreme = (t_extreme * (edit->widget.lines - 1)) / n;
1098 if (l_extreme + r_extreme + 1 > edit->widget.cols)
1100 int n;
1102 n = l_extreme + t_extreme;
1103 if (n == 0)
1104 n = 1;
1105 l_extreme = (l_extreme * (edit->widget.cols - 1)) / n;
1106 r_extreme = (r_extreme * (edit->widget.cols - 1)) / n;
1108 p = edit_get_col (edit) + edit->over_col;
1109 edit_update_curs_row (edit);
1110 outby = p + edit->start_col - edit->widget.cols + 1 + (r_extreme + edit->found_len);
1111 if (outby > 0)
1112 edit_scroll_right (edit, outby);
1113 outby = l_extreme - p - edit->start_col;
1114 if (outby > 0)
1115 edit_scroll_left (edit, outby);
1116 p = edit->curs_row;
1117 outby = p - edit->widget.lines + 1 + b_extreme;
1118 if (outby > 0)
1119 edit_scroll_downward (edit, outby);
1120 outby = t_extreme - p;
1121 if (outby > 0)
1122 edit_scroll_upward (edit, outby);
1123 edit_update_curs_row (edit);
1125 edit->widget.lines += EDIT_TEXT_VERTICAL_OFFSET;
1126 edit->widget.cols += EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
1127 if (!edit->fullscreen)
1129 edit->widget.x--;
1130 edit->widget.cols += 2;
1131 edit->widget.y--;
1132 edit->widget.lines += 2;
1136 /* --------------------------------------------------------------------------------------------- */
1138 void
1139 edit_render_keypress (WEdit * edit)
1141 edit_render (edit, 0, 0, 0, 0, 0);
1144 /* --------------------------------------------------------------------------------------------- */