Ticket #1858: Segmentation fault when search in different encodings
[midnight-commander.git] / edit / editdraw.c
blob467bedd0703d075d51e15b94478f4f9068a966e9
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 "../src/global.h"
43 #include "edit-impl.h"
44 #include "edit-widget.h"
46 #define MAX_LINE_LEN 1024
48 #include "../src/tty/tty.h" /* tty_printf() */
49 #include "../src/skin/skin.h"
50 #include "../src/tty/key.h" /* is_idle() */
52 #include "../src/widget.h" /* buttonbar_redraw() */
53 #include "../src/charsets.h"
54 #include "../src/strutil.h" /* utf string functions */
55 #include "../src/main.h" /* source_codepage */
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 void status_string (WEdit * edit, char *s, int w)
75 char byte_str[16];
76 unsigned char cur_byte = 0;
77 unsigned int cur_utf = 0;
78 int cw = 1;
81 * If we are at the end of file, print <EOF>,
82 * otherwise print the current character as is (if printable),
83 * as decimal and as hex.
85 if (edit->curs1 < edit->last_byte) {
86 if ( !edit->utf8 ) {
87 cur_byte = edit_get_byte (edit, edit->curs1);
89 g_snprintf (byte_str, sizeof (byte_str), "%4d 0x%03X",
90 (int) cur_byte,
91 (unsigned) cur_byte);
92 } else {
93 cur_utf = edit_get_utf (edit, edit->curs1, &cw);
94 if ( cw > 0 ) {
95 g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
96 (unsigned) cur_utf,
97 (unsigned) cur_utf);
98 } else {
99 cur_utf = edit_get_byte (edit, edit->curs1);
100 g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
101 (int) cur_utf,
102 (unsigned) cur_utf);
106 } else {
107 strcpy (byte_str, "<EOF> ");
110 /* The field lengths just prevent the status line from shortening too much */
111 if (simple_statusbar)
112 g_snprintf (s, w,
113 "%c%c%c%c %3ld %5ld/%ld %6ld/%ld %s %s",
114 edit->mark1 != edit->mark2 ? ( column_highlighting ? 'C' : 'B') : '-',
115 edit->modified ? 'M' : '-',
116 edit->macro_i < 0 ? '-' : 'R',
117 edit->overwrite == 0 ? '-' : 'O',
118 edit->curs_col + edit->over_col,
120 edit->curs_line + 1,
121 edit->total_lines + 1,
123 edit->curs1,
124 edit->last_byte,
125 byte_str,
127 #ifdef HAVE_CHARSET
128 source_codepage >= 0 ? get_codepage_id (source_codepage) : ""
129 #else
131 #endif
133 else
134 g_snprintf (s, w,
135 "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb) %s %s",
136 edit->mark1 != edit->mark2 ? ( column_highlighting ? 'C' : 'B') : '-',
137 edit->modified ? 'M' : '-',
138 edit->macro_i < 0 ? '-' : 'R',
139 edit->overwrite == 0 ? '-' : 'O',
140 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,
147 edit->curs1,
148 edit->last_byte,
149 byte_str,
151 #ifdef HAVE_CHARSET
152 source_codepage >= 0 ? get_codepage_id (source_codepage) : ""
153 #else
155 #endif
159 static inline void
160 printwstr (const char *s, int len)
162 if (len > 0)
163 tty_printf ("%-*.*s", len, len, s);
166 /* Draw the status line at the top of the widget. The size of the filename
167 * field varies depending on the width of the screen and the length of
168 * the filename. */
169 void
170 edit_status (WEdit *edit)
172 const int w = edit->widget.cols;
173 const size_t status_size = w + 1;
174 char * const status = g_malloc (status_size);
175 int status_len;
176 const char *fname = "";
177 int fname_len;
178 const int gap = 3; /* between the filename and the status */
179 const int right_gap = 5; /* at the right end of the screen */
180 const int preferred_fname_len = 16;
182 status_string (edit, status, status_size);
183 status_len = (int) str_term_width1 (status);
185 if (edit->filename)
186 fname = edit->filename;
187 fname_len = str_term_width1 (fname);
188 if (fname_len < preferred_fname_len)
189 fname_len = preferred_fname_len;
191 if (fname_len + gap + status_len + right_gap >= w) {
192 if (preferred_fname_len + gap + status_len + right_gap >= w)
193 fname_len = preferred_fname_len;
194 else
195 fname_len = w - (gap + status_len + right_gap);
196 fname = str_trunc (fname, fname_len);
199 widget_move (edit, 0, 0);
200 tty_setcolor (SELECTED_COLOR);
201 printwstr (fname, fname_len + gap);
202 printwstr (status, w - (fname_len + gap));
204 if (simple_statusbar && edit->widget.cols > 30) {
205 size_t percent;
206 if (edit->total_lines + 1 != 0)
207 percent = (edit->curs_line + 1) * 100 / (edit->total_lines + 1);
208 else
209 percent = 100;
210 widget_move (edit, 0, edit->widget.cols - 5);
211 tty_printf (" %3d%%", percent);
213 tty_setcolor (EDITOR_NORMAL_COLOR);
215 g_free (status);
218 /* this scrolls the text so that cursor is on the screen */
219 void edit_scroll_screen_over_cursor (WEdit * edit)
221 int p;
222 int outby;
223 int b_extreme, t_extreme, l_extreme, r_extreme;
225 if (edit->num_widget_lines <= 0 || edit->num_widget_columns <= 0)
226 return;
228 edit->num_widget_columns -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
229 edit->num_widget_lines -= EDIT_TEXT_VERTICAL_OFFSET - 1;
231 r_extreme = EDIT_RIGHT_EXTREME;
232 l_extreme = EDIT_LEFT_EXTREME;
233 b_extreme = EDIT_BOTTOM_EXTREME;
234 t_extreme = EDIT_TOP_EXTREME;
235 if (edit->found_len) {
236 b_extreme = max (edit->num_widget_lines / 4, b_extreme);
237 t_extreme = max (edit->num_widget_lines / 4, t_extreme);
239 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) {
246 int n;
247 n = l_extreme + t_extreme;
248 l_extreme = (l_extreme * (edit->num_widget_columns - 1)) / n;
249 r_extreme = (r_extreme * (edit->num_widget_columns - 1)) / n;
251 p = edit_get_col (edit) + edit->over_col;
252 edit_update_curs_row (edit);
253 outby = p + edit->start_col - edit->num_widget_columns + 1 + (r_extreme + edit->found_len);
254 if (outby > 0)
255 edit_scroll_right (edit, outby);
256 outby = l_extreme - p - edit->start_col;
257 if (outby > 0)
258 edit_scroll_left (edit, outby);
259 p = edit->curs_row;
260 outby = p - edit->num_widget_lines + 1 + b_extreme;
261 if (outby > 0)
262 edit_scroll_downward (edit, outby);
263 outby = t_extreme - p;
264 if (outby > 0)
265 edit_scroll_upward (edit, outby);
266 edit_update_curs_row (edit);
268 edit->num_widget_lines += EDIT_TEXT_VERTICAL_OFFSET - 1;
269 edit->num_widget_columns += EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
272 #define edit_move(x,y) widget_move(edit, y, x);
274 struct line_s {
275 unsigned int ch;
276 unsigned int style;
279 static void
280 print_to_widget (WEdit *edit, long row, int start_col, int start_col_real,
281 long end_col, struct line_s line[], char *status)
283 struct line_s *p;
285 int x = start_col_real;
286 int x1 = start_col + EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
287 int y = row + EDIT_TEXT_VERTICAL_OFFSET;
288 int cols_to_skip = abs (x);
290 tty_setcolor (EDITOR_NORMAL_COLOR);
291 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1,
292 ' ', end_col + 1 - start_col);
294 if (option_line_state) {
295 int i;
296 for (i = 0; i < LINE_STATE_WIDTH; i++)
297 if (status[i] == '\0')
298 status[i] = ' ';
300 tty_setcolor (LINE_STATE_COLOR);
301 edit_move (x1 + FONT_OFFSET_X - option_line_state_width, y + FONT_OFFSET_Y);
302 tty_print_string (status);
305 edit_move (x1 + FONT_OFFSET_X, y + FONT_OFFSET_Y);
306 p = line;
308 while (p->ch) {
309 int style;
310 unsigned int textchar;
311 int color;
313 if (cols_to_skip) {
314 p++;
315 cols_to_skip--;
316 continue;
319 style = p->style & 0xFF00;
320 textchar = p->ch;
321 color = p->style >> 16;
323 if (style & MOD_ABNORMAL) {
324 /* Non-printable - use black background */
325 color = 0;
328 if (style & MOD_WHITESPACE) {
329 if (style & MOD_MARKED) {
330 textchar = ' ';
331 tty_setcolor (EDITOR_MARKED_COLOR);
332 } else {
333 #if 0
334 if (color != EDITOR_NORMAL_COLOR) {
335 textchar = ' ';
336 tty_lowlevel_setcolor (color);
337 } else
338 #endif
339 tty_setcolor (EDITOR_WHITESPACE_COLOR);
341 } else {
342 if (style & MOD_BOLD) {
343 tty_setcolor (EDITOR_BOLD_COLOR);
344 } else if (style & MOD_MARKED) {
345 tty_setcolor (EDITOR_MARKED_COLOR);
346 } else {
347 tty_lowlevel_setcolor (color);
350 tty_print_anychar (textchar);
351 p++;
355 int visible_tabs = 1, visible_tws = 1;
357 /* b is a pointer to the beginning of the line */
358 static void
359 edit_draw_this_line (WEdit *edit, long b, long row, long start_col,
360 long end_col)
362 struct line_s line[MAX_LINE_LEN];
363 struct line_s *p = line;
365 long m1 = 0, m2 = 0, q, c1, c2;
366 int col, start_col_real;
367 int cw;
368 unsigned int c;
369 int color;
370 int abn_style;
371 int i;
372 int utf8lag = 0;
373 unsigned int cur_line = 0;
374 int book_mark = 0;
375 char line_stat[LINE_STATE_WIDTH + 1];
377 if (row > edit->num_widget_lines - EDIT_TEXT_VERTICAL_OFFSET) {
378 return;
380 if (book_mark_query_color(edit, edit->start_line + row, BOOK_MARK_COLOR)) {
381 book_mark = BOOK_MARK_COLOR;
382 } else if (book_mark_query_color(edit, edit->start_line + row, BOOK_MARK_FOUND_COLOR)) {
383 book_mark = BOOK_MARK_FOUND_COLOR;
386 if (book_mark)
387 abn_style = book_mark << 16;
388 else
389 abn_style = MOD_ABNORMAL;
391 end_col -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
393 edit_get_syntax_color (edit, b - 1, &color);
394 q = edit_move_forward3 (edit, b, start_col - edit->start_col, 0);
395 start_col_real = (col =
396 (int) edit_move_forward3 (edit, b, 0,
397 q)) + edit->start_col;
398 if ( option_line_state ) {
399 cur_line = edit->start_line + row;
400 if ( cur_line <= (unsigned int) edit->total_lines ) {
401 g_snprintf (line_stat, LINE_STATE_WIDTH + 1, "%7i ", cur_line + 1);
402 } else {
403 memset(line_stat, ' ', LINE_STATE_WIDTH);
404 line_stat[LINE_STATE_WIDTH] = '\0';
406 if (book_mark_query_color (edit, cur_line, BOOK_MARK_COLOR)){
407 g_snprintf (line_stat, 2, "*");
411 if (col + 16 > -edit->start_col) {
412 eval_marks (edit, &m1, &m2);
414 if (row <= edit->total_lines - edit->start_line) {
415 long tws = 0;
416 if (tty_use_colors () && visible_tws) {
417 tws = edit_eol (edit, b);
418 while (tws > b && ((c = edit_get_byte (edit, tws - 1)) == ' '
419 || c == '\t'))
420 tws--;
423 while (col <= end_col - edit->start_col) {
424 p->ch = 0;
425 p->style = 0;
426 if (q == edit->curs1)
427 p->style |= MOD_CURSOR;
428 if (q >= m1 && q < m2) {
429 if (column_highlighting) {
430 int x;
431 x = edit_move_forward3 (edit, b, 0, q);
432 c1 = min (edit->column1, edit->column2);
433 c2 = max (edit->column1, edit->column2);
434 if (x >= c1 && x < c2)
435 p->style |= MOD_MARKED;
436 } else
437 p->style |= MOD_MARKED;
439 if (q == edit->bracket)
440 p->style |= MOD_BOLD;
441 if (q >= edit->found_start
442 && q < edit->found_start + edit->found_len)
443 p->style |= MOD_BOLD;
444 cw = 1;
445 if ( !edit->utf8 ) {
446 c = edit_get_byte (edit, q);
447 } else {
448 c = edit_get_utf (edit, q, &cw);
450 /* we don't use bg for mc - fg contains both */
451 if (book_mark) {
452 p->style |= book_mark << 16;
453 } else {
454 edit_get_syntax_color (edit, q, &color);
455 p->style |= color << 16;
457 switch (c) {
458 case '\n':
459 col = (end_col + utf8lag) - edit->start_col + 1; /* quit */
460 break;
461 case '\t':
462 i = TAB_SIZE - ((int) col % TAB_SIZE);
463 col += i;
464 if (tty_use_colors() &&
465 ((visible_tabs || (visible_tws && q >= tws)) && enable_show_tabs_tws)) {
466 if (p->style & MOD_MARKED)
467 c = p->style;
468 else if (book_mark)
469 c |= book_mark << 16;
470 else
471 c = p->style | MOD_WHITESPACE;
472 if (i > 2) {
473 p->ch = '<';
474 p->style = c;
475 p++;
476 while (--i > 1) {
477 p->ch = '-';
478 p->style = c;
479 p++;
481 p->ch = '>';
482 p->style = c;
483 p++;
484 } else if (i > 1) {
485 p->ch = '<';
486 p->style = c;
487 p++;
488 p->ch = '>';
489 p->style = c;
490 p++;
491 } else {
492 p->ch = '>';
493 p->style = c;
494 p++;
496 } else if (tty_use_colors() && visible_tws && q >= tws && enable_show_tabs_tws) {
497 p->ch = '.';
498 p->style |= MOD_WHITESPACE;
499 c = p->style & ~MOD_CURSOR;
500 p++;
501 while (--i) {
502 p->ch = ' ';
503 p->style = c;
504 p++;
506 } else {
507 p->ch |= ' ';
508 c = p->style & ~MOD_CURSOR;
509 p++;
510 while (--i) {
511 p->ch = ' ';
512 p->style = c;
513 p++;
516 break;
517 case ' ':
518 if (tty_use_colors() && visible_tws && q >= tws && enable_show_tabs_tws) {
519 p->ch = '.';
520 p->style |= MOD_WHITESPACE;
521 p++;
522 col++;
523 break;
525 /* fallthrough */
526 default:
527 #ifdef HAVE_CHARSET
528 if ( utf8_display ) {
529 if ( !edit->utf8 ) {
530 c = convert_from_8bit_to_utf_c ((unsigned char) c, edit->converter);
532 } else {
533 if ( edit->utf8 ) {
534 c = convert_from_utf_to_current_c (c, edit->converter);
535 } else {
536 #endif
537 c = convert_to_display_c (c);
538 #ifdef HAVE_CHARSET
541 #endif
542 /* Caret notation for control characters */
543 if (c < 32) {
544 p->ch = '^';
545 p->style = abn_style;
546 p++;
547 p->ch = c + 0x40;
548 p->style = abn_style;
549 p++;
550 col += 2;
551 break;
553 if (c == 127) {
554 p->ch = '^';
555 p->style = abn_style;
556 p++;
557 p->ch = '?';
558 p->style = abn_style;
559 p++;
560 col += 2;
561 break;
563 if (!edit->utf8) {
564 if ( ( utf8_display && g_unichar_isprint (c) ) ||
565 ( !utf8_display && is_printable (c) ) ) {
566 p->ch = c;
567 p++;
568 } else {
569 p->ch = '.';
570 p->style = abn_style;
571 p++;
573 } else {
574 if ( g_unichar_isprint (c) ) {
575 p->ch = c;
576 p++;
577 } else {
578 p->ch = '.';
579 p->style = abn_style;
580 p++;
583 col++;
584 break;
586 q++;
587 if ( cw > 1) {
588 q += cw - 1;
592 } else {
593 start_col_real = start_col = 0;
595 p->ch = 0;
597 print_to_widget (edit, row, start_col, start_col_real, end_col, line, line_stat);
600 #define key_pending(x) (!is_idle())
602 static void edit_draw_this_char (WEdit * edit, long curs, long row)
604 int b = edit_bol (edit, curs);
605 edit_draw_this_line (edit, b, row, 0, edit->num_widget_columns - 1);
608 /* cursor must be in screen for other than REDRAW_PAGE passed in force */
609 static void
610 render_edit_text (WEdit * edit, long start_row, long start_column, long end_row,
611 long end_column)
613 long row = 0, curs_row;
614 static int prev_curs_row = 0;
615 static long prev_curs = 0;
617 int force = edit->force;
618 long b;
621 * If the position of the page has not moved then we can draw the cursor
622 * character only. This will prevent line flicker when using arrow keys.
624 if ((!(force & REDRAW_CHAR_ONLY)) || (force & REDRAW_PAGE)) {
625 if (!(force & REDRAW_IN_BOUNDS)) { /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
626 start_row = 0;
627 end_row = edit->num_widget_lines - 1;
628 start_column = 0;
629 end_column = edit->num_widget_columns - 1;
631 if (force & REDRAW_PAGE) {
632 row = start_row;
633 b = edit_move_forward (edit, edit->start_display, start_row, 0);
634 while (row <= end_row) {
635 if (key_pending (edit))
636 goto exit_render;
637 edit_draw_this_line (edit, b, row, start_column, end_column);
638 b = edit_move_forward (edit, b, 1, 0);
639 row++;
641 } else {
642 curs_row = edit->curs_row;
644 if (force & REDRAW_BEFORE_CURSOR) {
645 if (start_row < curs_row) {
646 long upto = curs_row - 1 <= end_row ? curs_row - 1 : end_row;
647 row = start_row;
648 b = edit->start_display;
649 while (row <= upto) {
650 if (key_pending (edit))
651 goto exit_render;
652 edit_draw_this_line (edit, b, row, start_column, end_column);
653 b = edit_move_forward (edit, b, 1, 0);
657 /* if (force & REDRAW_LINE) ---> default */
658 b = edit_bol (edit, edit->curs1);
659 if (curs_row >= start_row && curs_row <= end_row) {
660 if (key_pending (edit))
661 goto exit_render;
662 edit_draw_this_line (edit, b, curs_row, start_column, end_column);
664 if (force & REDRAW_AFTER_CURSOR) {
665 if (end_row > curs_row) {
666 row = curs_row + 1 < start_row ? start_row : curs_row + 1;
667 b = edit_move_forward (edit, b, 1, 0);
668 while (row <= end_row) {
669 if (key_pending (edit))
670 goto exit_render;
671 edit_draw_this_line (edit, b, row, start_column, end_column);
672 b = edit_move_forward (edit, b, 1, 0);
673 row++;
677 if (force & REDRAW_LINE_ABOVE && curs_row >= 1) {
678 row = curs_row - 1;
679 b = edit_move_backward (edit, edit_bol (edit, edit->curs1), 1);
680 if (row >= start_row && row <= end_row) {
681 if (key_pending (edit))
682 goto exit_render;
683 edit_draw_this_line (edit, b, row, start_column, end_column);
686 if (force & REDRAW_LINE_BELOW && row < edit->num_widget_lines - 1) {
687 row = curs_row + 1;
688 b = edit_bol (edit, edit->curs1);
689 b = edit_move_forward (edit, b, 1, 0);
690 if (row >= start_row && row <= end_row) {
691 if (key_pending (edit))
692 goto exit_render;
693 edit_draw_this_line (edit, b, row, start_column, end_column);
697 } else {
698 if (prev_curs_row < edit->curs_row) { /* with the new text highlighting, we must draw from the top down */
699 edit_draw_this_char (edit, prev_curs, prev_curs_row);
700 edit_draw_this_char (edit, edit->curs1, edit->curs_row);
701 } else {
702 edit_draw_this_char (edit, edit->curs1, edit->curs_row);
703 edit_draw_this_char (edit, prev_curs, prev_curs_row);
707 edit->force = 0;
709 prev_curs_row = edit->curs_row;
710 prev_curs = edit->curs1;
712 exit_render:
713 edit->screen_modified = 0;
714 return;
717 static void
718 edit_render (WEdit * edit, int page, int row_start, int col_start, int row_end, int col_end)
720 if (page) /* if it was an expose event, 'page' would be set */
721 edit->force |= REDRAW_PAGE | REDRAW_IN_BOUNDS;
723 if (edit->force & REDRAW_COMPLETELY)
724 buttonbar_redraw (find_buttonbar (edit->widget.parent));
725 render_edit_text (edit, row_start, col_start, row_end, col_end);
727 * edit->force != 0 means a key was pending and the redraw
728 * was halted, so next time we must redraw everything in case stuff
729 * was left undrawn from a previous key press.
731 if (edit->force)
732 edit->force |= REDRAW_PAGE;
735 void edit_render_keypress (WEdit * edit)
737 edit_render (edit, 0, 0, 0, 0, 0);