Merge branch '2123_crash_while_copy'
[midnight-commander.git] / src / editor / editdraw.c
blob9de4ac6d03efa7eb07b547f9c264634d28ade393
1 /* editor text drawing.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
24 /** \file
25 * \brief Source: editor text drawing
26 * \author Paul Sheer
27 * \date 1996, 1997
30 #include <config.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <sys/stat.h>
41 #include "lib/global.h"
42 #include "lib/tty/tty.h" /* tty_printf() */
43 #include "lib/tty/key.h" /* is_idle() */
44 #include "lib/skin.h"
45 #include "lib/strutil.h" /* utf string functions */
47 #include "src/widget.h" /* buttonbar_redraw() */
48 #include "src/charsets.h"
49 #include "src/main.h" /* source_codepage */
50 #include "src/setup.h" /* edit_tab_spacing */
52 #include "edit-impl.h"
53 #include "edit-widget.h"
55 #define MAX_LINE_LEN 1024
57 /* Text styles */
58 #define MOD_ABNORMAL (1 << 8)
59 #define MOD_BOLD (1 << 9)
60 #define MOD_MARKED (1 << 10)
61 #define MOD_CURSOR (1 << 11)
62 #define MOD_WHITESPACE (1 << 12)
64 #define FONT_OFFSET_X 0
65 #define FONT_OFFSET_Y 0
66 #define FIXED_FONT 1
67 #define FONT_PIX_PER_LINE 1
68 #define FONT_MEAN_WIDTH 1
70 /* Toggles statusbar draw style */
71 int simple_statusbar = 0;
73 static inline void
74 status_string (WEdit * edit, char *s, int w)
76 char byte_str[16];
77 unsigned char cur_byte = 0;
78 unsigned int cur_utf = 0;
79 int cw = 1;
82 * If we are at the end of file, print <EOF>,
83 * otherwise print the current character as is (if printable),
84 * as decimal and as hex.
86 if (edit->curs1 < edit->last_byte)
88 if (!edit->utf8)
90 cur_byte = edit_get_byte (edit, edit->curs1);
92 g_snprintf (byte_str, sizeof (byte_str), "%4d 0x%03X",
93 (int) cur_byte, (unsigned) cur_byte);
95 else
97 cur_utf = edit_get_utf (edit, edit->curs1, &cw);
98 if (cw > 0)
100 g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
101 (unsigned) cur_utf, (unsigned) cur_utf);
103 else
105 cur_utf = edit_get_byte (edit, edit->curs1);
106 g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
107 (int) cur_utf, (unsigned) cur_utf);
112 else
114 strcpy (byte_str, "<EOF> ");
117 /* The field lengths just prevent the status line from shortening too much */
118 if (simple_statusbar)
119 g_snprintf (s, w,
120 "%c%c%c%c %3ld %5ld/%ld %6ld/%ld %s %s",
121 edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
122 edit->modified ? 'M' : '-',
123 edit->macro_i < 0 ? '-' : 'R',
124 edit->overwrite == 0 ? '-' : 'O',
125 edit->curs_col + edit->over_col,
126 edit->curs_line + 1,
127 edit->total_lines + 1, edit->curs1, edit->last_byte, byte_str,
128 #ifdef HAVE_CHARSET
129 source_codepage >= 0 ? get_codepage_id (source_codepage) : ""
130 #else
132 #endif
134 else
135 g_snprintf (s, w,
136 "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb) %s %s",
137 edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
138 edit->modified ? 'M' : '-',
139 edit->macro_i < 0 ? '-' : 'R',
140 edit->overwrite == 0 ? '-' : 'O',
141 edit->curs_col + edit->over_col,
142 edit->start_line + 1,
143 edit->curs_row,
144 edit->curs_line + 1,
145 edit->total_lines + 1, edit->curs1, edit->last_byte, byte_str,
146 #ifdef HAVE_CHARSET
147 source_codepage >= 0 ? get_codepage_id (source_codepage) : ""
148 #else
150 #endif
154 static inline void
155 printwstr (const char *s, int len)
157 if (len > 0)
158 tty_printf ("%-*.*s", len, len, s);
161 /* Draw the status line at the top of the widget. The size of the filename
162 * field varies depending on the width of the screen and the length of
163 * the filename. */
164 void
165 edit_status (WEdit * edit)
167 const int w = edit->widget.cols;
168 const size_t status_size = w + 1;
169 char *const status = g_malloc (status_size);
170 int status_len;
171 const char *fname = "";
172 int fname_len;
173 const int gap = 3; /* between the filename and the status */
174 const int right_gap = 5; /* at the right end of the screen */
175 const int preferred_fname_len = 16;
177 status_string (edit, status, status_size);
178 status_len = (int) str_term_width1 (status);
180 if (edit->filename)
181 fname = edit->filename;
182 fname_len = str_term_width1 (fname);
183 if (fname_len < preferred_fname_len)
184 fname_len = preferred_fname_len;
186 if (fname_len + gap + status_len + right_gap >= w)
188 if (preferred_fname_len + gap + status_len + right_gap >= w)
189 fname_len = preferred_fname_len;
190 else
191 fname_len = w - (gap + status_len + right_gap);
192 fname = str_trunc (fname, fname_len);
195 widget_move (edit, 0, 0);
196 tty_setcolor (SELECTED_COLOR);
197 printwstr (fname, fname_len + gap);
198 printwstr (status, w - (fname_len + gap));
200 if (simple_statusbar && edit->widget.cols > 30)
202 size_t percent;
203 if (edit->total_lines + 1 != 0)
204 percent = (edit->curs_line + 1) * 100 / (edit->total_lines + 1);
205 else
206 percent = 100;
207 widget_move (edit, 0, edit->widget.cols - 5);
208 tty_printf (" %3d%%", percent);
210 tty_setcolor (EDITOR_NORMAL_COLOR);
212 g_free (status);
215 /* this scrolls the text so that cursor is on the screen */
216 void
217 edit_scroll_screen_over_cursor (WEdit * edit)
219 int p;
220 int outby;
221 int b_extreme, t_extreme, l_extreme, r_extreme;
223 if (edit->num_widget_lines <= 0 || edit->num_widget_columns <= 0)
224 return;
226 edit->num_widget_columns -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
227 edit->num_widget_lines -= EDIT_TEXT_VERTICAL_OFFSET - 1;
229 r_extreme = EDIT_RIGHT_EXTREME;
230 l_extreme = EDIT_LEFT_EXTREME;
231 b_extreme = EDIT_BOTTOM_EXTREME;
232 t_extreme = EDIT_TOP_EXTREME;
233 if (edit->found_len)
235 b_extreme = max (edit->num_widget_lines / 4, b_extreme);
236 t_extreme = max (edit->num_widget_lines / 4, t_extreme);
238 if (b_extreme + t_extreme + 1 > edit->num_widget_lines)
240 int n;
241 n = b_extreme + t_extreme;
242 b_extreme = (b_extreme * (edit->num_widget_lines - 1)) / n;
243 t_extreme = (t_extreme * (edit->num_widget_lines - 1)) / n;
245 if (l_extreme + r_extreme + 1 > edit->num_widget_columns)
247 int n;
248 n = l_extreme + t_extreme;
249 l_extreme = (l_extreme * (edit->num_widget_columns - 1)) / n;
250 r_extreme = (r_extreme * (edit->num_widget_columns - 1)) / n;
252 p = edit_get_col (edit) + edit->over_col;
253 edit_update_curs_row (edit);
254 outby = p + edit->start_col - edit->num_widget_columns + 1 + (r_extreme + edit->found_len);
255 if (outby > 0)
256 edit_scroll_right (edit, outby);
257 outby = l_extreme - p - edit->start_col;
258 if (outby > 0)
259 edit_scroll_left (edit, outby);
260 p = edit->curs_row;
261 outby = p - edit->num_widget_lines + 1 + b_extreme;
262 if (outby > 0)
263 edit_scroll_downward (edit, outby);
264 outby = t_extreme - p;
265 if (outby > 0)
266 edit_scroll_upward (edit, outby);
267 edit_update_curs_row (edit);
269 edit->num_widget_lines += EDIT_TEXT_VERTICAL_OFFSET - 1;
270 edit->num_widget_columns += EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
273 #define edit_move(x,y) widget_move(edit, y, x);
275 struct line_s
277 unsigned int ch;
278 unsigned int style;
281 static inline void
282 print_to_widget (WEdit * edit, long row, int start_col, int start_col_real,
283 long end_col, struct line_s line[], char *status)
285 struct line_s *p;
287 int x = start_col_real;
288 int x1 = start_col + EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
289 int y = row + EDIT_TEXT_VERTICAL_OFFSET;
290 int cols_to_skip = abs (x);
291 int i;
293 tty_setcolor (EDITOR_NORMAL_COLOR);
295 if (!show_right_margin)
297 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1, ' ', end_col + 1 - start_col);
299 else if (edit->start_col < option_word_wrap_line_length)
301 tty_draw_hline (edit->widget.y + y,
302 edit->widget.x + x1, ' ', option_word_wrap_line_length - edit->start_col);
304 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
305 tty_draw_hline (edit->widget.y + y,
306 edit->widget.x + x1 + option_word_wrap_line_length + edit->start_col,
307 ' ', end_col + 1 - start_col);
310 if (option_line_state)
312 for (i = 0; i < LINE_STATE_WIDTH; i++)
313 if (status[i] == '\0')
314 status[i] = ' ';
316 tty_setcolor (LINE_STATE_COLOR);
317 edit_move (x1 + FONT_OFFSET_X - option_line_state_width, y + FONT_OFFSET_Y);
318 tty_print_string (status);
321 edit_move (x1 + FONT_OFFSET_X, y + FONT_OFFSET_Y);
322 p = line;
323 i = 1;
324 while (p->ch)
326 int style;
327 unsigned int textchar;
328 int color;
330 if (cols_to_skip)
332 p++;
333 cols_to_skip--;
334 continue;
337 style = p->style & 0xFF00;
338 textchar = p->ch;
339 color = p->style >> 16;
341 if (style & MOD_ABNORMAL)
343 /* Non-printable - use black background */
344 color = 0;
347 if (style & MOD_WHITESPACE)
349 if (style & MOD_MARKED)
351 textchar = ' ';
352 tty_setcolor (EDITOR_MARKED_COLOR);
354 else
356 #if 0
357 if (color != EDITOR_NORMAL_COLOR)
359 textchar = ' ';
360 tty_lowlevel_setcolor (color);
362 else
363 #endif
364 tty_setcolor (EDITOR_WHITESPACE_COLOR);
367 else
369 if (style & MOD_BOLD)
371 tty_setcolor (EDITOR_BOLD_COLOR);
373 else if (style & MOD_MARKED)
375 tty_setcolor (EDITOR_MARKED_COLOR);
377 else
379 tty_lowlevel_setcolor (color);
382 if (show_right_margin)
384 if (i > option_word_wrap_line_length + edit->start_col)
385 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
386 i++;
388 tty_print_anychar (textchar);
389 p++;
393 int visible_tabs = 1, visible_tws = 1;
395 /* b is a pointer to the beginning of the line */
396 static void
397 edit_draw_this_line (WEdit * edit, long b, long row, long start_col, long end_col)
399 struct line_s line[MAX_LINE_LEN];
400 struct line_s *p = line;
402 long m1 = 0, m2 = 0, q, c1, c2;
403 int col, start_col_real;
404 unsigned int c;
405 int color;
406 int abn_style;
407 int i;
408 int utf8lag = 0;
409 unsigned int cur_line = 0;
410 int book_mark = 0;
411 char line_stat[LINE_STATE_WIDTH + 1];
413 if (row > edit->num_widget_lines - EDIT_TEXT_VERTICAL_OFFSET)
414 return;
416 if (book_mark_query_color (edit, edit->start_line + row, BOOK_MARK_COLOR))
417 book_mark = BOOK_MARK_COLOR;
418 else if (book_mark_query_color (edit, edit->start_line + row, BOOK_MARK_FOUND_COLOR))
419 book_mark = BOOK_MARK_FOUND_COLOR;
421 if (book_mark)
422 abn_style = book_mark << 16;
423 else
424 abn_style = MOD_ABNORMAL;
426 end_col -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
428 edit_get_syntax_color (edit, b - 1, &color);
429 q = edit_move_forward3 (edit, b, start_col - edit->start_col, 0);
430 start_col_real = (col = (int) edit_move_forward3 (edit, b, 0, q)) + edit->start_col;
431 if (option_line_state)
433 cur_line = edit->start_line + row;
434 if (cur_line <= (unsigned int) edit->total_lines)
436 g_snprintf (line_stat, LINE_STATE_WIDTH + 1, "%7i ", cur_line + 1);
438 else
440 memset (line_stat, ' ', LINE_STATE_WIDTH);
441 line_stat[LINE_STATE_WIDTH] = '\0';
443 if (book_mark_query_color (edit, cur_line, BOOK_MARK_COLOR))
445 g_snprintf (line_stat, 2, "*");
449 if (col + 16 > -edit->start_col)
451 eval_marks (edit, &m1, &m2);
453 if (row <= edit->total_lines - edit->start_line)
455 long tws = 0;
456 if (tty_use_colors () && visible_tws)
458 tws = edit_eol (edit, b);
459 while (tws > b && ((c = edit_get_byte (edit, tws - 1)) == ' ' || c == '\t'))
460 tws--;
463 while (col <= end_col - edit->start_col)
465 int cw = 1;
467 p->ch = 0;
468 p->style = 0;
469 if (q == edit->curs1)
470 p->style |= MOD_CURSOR;
471 if (q >= m1 && q < m2)
473 if (edit->column_highlight)
475 int x;
476 x = edit_move_forward3 (edit, b, 0, q);
477 c1 = min (edit->column1, edit->column2);
478 c2 = max (edit->column1, edit->column2);
479 if (x >= c1 && x < c2)
480 p->style |= MOD_MARKED;
482 else
483 p->style |= MOD_MARKED;
485 if (q == edit->bracket)
486 p->style |= MOD_BOLD;
487 if (q >= edit->found_start && q < edit->found_start + edit->found_len)
488 p->style |= MOD_BOLD;
490 if (!edit->utf8)
492 c = edit_get_byte (edit, q);
494 else
496 c = edit_get_utf (edit, q, &cw);
498 /* we don't use bg for mc - fg contains both */
499 if (book_mark)
501 p->style |= book_mark << 16;
503 else
505 edit_get_syntax_color (edit, q, &color);
506 p->style |= color << 16;
508 switch (c)
510 case '\n':
511 col = (end_col + utf8lag) - edit->start_col + 1; /* quit */
512 break;
513 case '\t':
514 i = TAB_SIZE - ((int) col % TAB_SIZE);
515 col += i;
516 if (tty_use_colors () &&
517 ((visible_tabs || (visible_tws && q >= tws)) && enable_show_tabs_tws))
519 if (p->style & MOD_MARKED)
520 c = p->style;
521 else if (book_mark)
522 c |= book_mark << 16;
523 else
524 c = p->style | MOD_WHITESPACE;
525 if (i > 2)
527 p->ch = '<';
528 p->style = c;
529 p++;
530 while (--i > 1)
532 p->ch = '-';
533 p->style = c;
534 p++;
536 p->ch = '>';
537 p->style = c;
538 p++;
540 else if (i > 1)
542 p->ch = '<';
543 p->style = c;
544 p++;
545 p->ch = '>';
546 p->style = c;
547 p++;
549 else
551 p->ch = '>';
552 p->style = c;
553 p++;
556 else if (tty_use_colors () && visible_tws && q >= tws && enable_show_tabs_tws)
558 p->ch = '.';
559 p->style |= MOD_WHITESPACE;
560 c = p->style & ~MOD_CURSOR;
561 p++;
562 while (--i)
564 p->ch = ' ';
565 p->style = c;
566 p++;
569 else
571 p->ch |= ' ';
572 c = p->style & ~MOD_CURSOR;
573 p++;
574 while (--i)
576 p->ch = ' ';
577 p->style = c;
578 p++;
581 break;
582 case ' ':
583 if (tty_use_colors () && visible_tws && q >= tws && enable_show_tabs_tws)
585 p->ch = '.';
586 p->style |= MOD_WHITESPACE;
587 p++;
588 col++;
589 break;
591 /* fallthrough */
592 default:
593 #ifdef HAVE_CHARSET
594 if (utf8_display)
596 if (!edit->utf8)
598 c = convert_from_8bit_to_utf_c ((unsigned char) c, edit->converter);
601 else if (edit->utf8)
602 c = convert_from_utf_to_current_c (c, edit->converter);
603 else
604 #endif
605 c = convert_to_display_c (c);
607 /* Caret notation for control characters */
608 if (c < 32)
610 p->ch = '^';
611 p->style = abn_style;
612 p++;
613 p->ch = c + 0x40;
614 p->style = abn_style;
615 p++;
616 col += 2;
617 break;
619 if (c == 127)
621 p->ch = '^';
622 p->style = abn_style;
623 p++;
624 p->ch = '?';
625 p->style = abn_style;
626 p++;
627 col += 2;
628 break;
630 if (!edit->utf8)
632 if ((utf8_display && g_unichar_isprint (c)) ||
633 (!utf8_display && is_printable (c)))
635 p->ch = c;
636 p++;
638 else
640 p->ch = '.';
641 p->style = abn_style;
642 p++;
645 else
647 if (g_unichar_isprint (c))
649 p->ch = c;
650 p++;
652 else
654 p->ch = '.';
655 p->style = abn_style;
656 p++;
659 col++;
660 break;
661 } /* case */
663 q++;
664 if (cw > 1)
666 q += cw - 1;
671 else
673 start_col_real = start_col = 0;
676 p->ch = '\0';
678 print_to_widget (edit, row, start_col, start_col_real, end_col, line, line_stat);
681 #define key_pending(x) (!is_idle())
683 static inline void
684 edit_draw_this_char (WEdit * edit, long curs, long row)
686 int b = edit_bol (edit, curs);
687 edit_draw_this_line (edit, b, row, 0, edit->num_widget_columns - 1);
690 /* cursor must be in screen for other than REDRAW_PAGE passed in force */
691 static inline void
692 render_edit_text (WEdit * edit, long start_row, long start_column, long end_row, long end_column)
694 long row = 0, curs_row;
695 static long prev_curs_row = 0;
696 static long prev_curs = 0;
698 int force = edit->force;
699 long b;
702 * If the position of the page has not moved then we can draw the cursor
703 * character only. This will prevent line flicker when using arrow keys.
705 if ((!(force & REDRAW_CHAR_ONLY)) || (force & REDRAW_PAGE))
707 if (!(force & REDRAW_IN_BOUNDS))
708 { /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
709 start_row = 0;
710 end_row = edit->num_widget_lines - 1;
711 start_column = 0;
712 end_column = edit->num_widget_columns - 1;
714 if (force & REDRAW_PAGE)
716 row = start_row;
717 b = edit_move_forward (edit, edit->start_display, start_row, 0);
718 while (row <= end_row)
720 if (key_pending (edit))
721 goto exit_render;
722 edit_draw_this_line (edit, b, row, start_column, end_column);
723 b = edit_move_forward (edit, b, 1, 0);
724 row++;
727 else
729 curs_row = edit->curs_row;
731 if (force & REDRAW_BEFORE_CURSOR)
733 if (start_row < curs_row)
735 long upto = curs_row - 1 <= end_row ? curs_row - 1 : end_row;
736 row = start_row;
737 b = edit->start_display;
738 while (row <= upto)
740 if (key_pending (edit))
741 goto exit_render;
742 edit_draw_this_line (edit, b, row, start_column, end_column);
743 b = edit_move_forward (edit, b, 1, 0);
747 /* if (force & REDRAW_LINE) ---> default */
748 b = edit_bol (edit, edit->curs1);
749 if (curs_row >= start_row && curs_row <= end_row)
751 if (key_pending (edit))
752 goto exit_render;
753 edit_draw_this_line (edit, b, curs_row, start_column, end_column);
755 if (force & REDRAW_AFTER_CURSOR)
757 if (end_row > curs_row)
759 row = curs_row + 1 < start_row ? start_row : curs_row + 1;
760 b = edit_move_forward (edit, b, 1, 0);
761 while (row <= end_row)
763 if (key_pending (edit))
764 goto exit_render;
765 edit_draw_this_line (edit, b, row, start_column, end_column);
766 b = edit_move_forward (edit, b, 1, 0);
767 row++;
771 if (force & REDRAW_LINE_ABOVE && curs_row >= 1)
773 row = curs_row - 1;
774 b = edit_move_backward (edit, edit_bol (edit, edit->curs1), 1);
775 if (row >= start_row && row <= end_row)
777 if (key_pending (edit))
778 goto exit_render;
779 edit_draw_this_line (edit, b, row, start_column, end_column);
782 if (force & REDRAW_LINE_BELOW && row < edit->num_widget_lines - 1)
784 row = curs_row + 1;
785 b = edit_bol (edit, edit->curs1);
786 b = edit_move_forward (edit, b, 1, 0);
787 if (row >= start_row && row <= end_row)
789 if (key_pending (edit))
790 goto exit_render;
791 edit_draw_this_line (edit, b, row, start_column, end_column);
796 else
798 if (prev_curs_row < edit->curs_row)
799 { /* with the new text highlighting, we must draw from the top down */
800 edit_draw_this_char (edit, prev_curs, prev_curs_row);
801 edit_draw_this_char (edit, edit->curs1, edit->curs_row);
803 else
805 edit_draw_this_char (edit, edit->curs1, edit->curs_row);
806 edit_draw_this_char (edit, prev_curs, prev_curs_row);
810 edit->force = 0;
812 prev_curs_row = edit->curs_row;
813 prev_curs = edit->curs1;
815 exit_render:
816 edit->screen_modified = 0;
819 static inline void
820 edit_render (WEdit * edit, int page, int row_start, int col_start, int row_end, int col_end)
822 if (page) /* if it was an expose event, 'page' would be set */
823 edit->force |= REDRAW_PAGE | REDRAW_IN_BOUNDS;
825 if (edit->force & REDRAW_COMPLETELY)
826 buttonbar_redraw (find_buttonbar (edit->widget.owner));
827 render_edit_text (edit, row_start, col_start, row_end, col_end);
829 * edit->force != 0 means a key was pending and the redraw
830 * was halted, so next time we must redraw everything in case stuff
831 * was left undrawn from a previous key press.
833 if (edit->force)
834 edit->force |= REDRAW_PAGE;
837 void
838 edit_render_keypress (WEdit * edit)
840 edit_render (edit, 0, 0, 0, 0, 0);