Merge branch '1904_spec_bump_epoch'
[midnight-commander.git] / edit / editdraw.c
blob64572275eeb9c40ae9bd11b1d34b3e4f7ce1612d
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 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) {
87 if ( !edit->utf8 ) {
88 cur_byte = edit_get_byte (edit, edit->curs1);
90 g_snprintf (byte_str, sizeof (byte_str), "%4d 0x%03X",
91 (int) cur_byte,
92 (unsigned) cur_byte);
93 } else {
94 cur_utf = edit_get_utf (edit, edit->curs1, &cw);
95 if ( cw > 0 ) {
96 g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
97 (unsigned) cur_utf,
98 (unsigned) cur_utf);
99 } else {
100 cur_utf = edit_get_byte (edit, edit->curs1);
101 g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
102 (int) cur_utf,
103 (unsigned) cur_utf);
107 } else {
108 strcpy (byte_str, "<EOF> ");
111 /* The field lengths just prevent the status line from shortening too much */
112 if (simple_statusbar)
113 g_snprintf (s, w,
114 "%c%c%c%c %3ld %5ld/%ld %6ld/%ld %s %s",
115 edit->mark1 != edit->mark2 ? ( column_highlighting ? 'C' : 'B') : '-',
116 edit->modified ? 'M' : '-',
117 edit->macro_i < 0 ? '-' : 'R',
118 edit->overwrite == 0 ? '-' : 'O',
119 edit->curs_col + edit->over_col,
121 edit->curs_line + 1,
122 edit->total_lines + 1,
124 edit->curs1,
125 edit->last_byte,
126 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 ? ( column_highlighting ? 'C' : 'B') : '-',
138 edit->modified ? 'M' : '-',
139 edit->macro_i < 0 ? '-' : 'R',
140 edit->overwrite == 0 ? '-' : 'O',
141 edit->curs_col + edit->over_col,
143 edit->start_line + 1,
144 edit->curs_row,
145 edit->curs_line + 1,
146 edit->total_lines + 1,
148 edit->curs1,
149 edit->last_byte,
150 byte_str,
152 #ifdef HAVE_CHARSET
153 source_codepage >= 0 ? get_codepage_id (source_codepage) : ""
154 #else
156 #endif
160 static inline void
161 printwstr (const char *s, int len)
163 if (len > 0)
164 tty_printf ("%-*.*s", len, len, s);
167 /* Draw the status line at the top of the widget. The size of the filename
168 * field varies depending on the width of the screen and the length of
169 * the filename. */
170 void
171 edit_status (WEdit *edit)
173 const int w = edit->widget.cols;
174 const size_t status_size = w + 1;
175 char * const status = g_malloc (status_size);
176 int status_len;
177 const char *fname = "";
178 int fname_len;
179 const int gap = 3; /* between the filename and the status */
180 const int right_gap = 5; /* at the right end of the screen */
181 const int preferred_fname_len = 16;
183 status_string (edit, status, status_size);
184 status_len = (int) str_term_width1 (status);
186 if (edit->filename)
187 fname = edit->filename;
188 fname_len = str_term_width1 (fname);
189 if (fname_len < preferred_fname_len)
190 fname_len = preferred_fname_len;
192 if (fname_len + gap + status_len + right_gap >= w) {
193 if (preferred_fname_len + gap + status_len + right_gap >= w)
194 fname_len = preferred_fname_len;
195 else
196 fname_len = w - (gap + status_len + right_gap);
197 fname = str_trunc (fname, fname_len);
200 widget_move (edit, 0, 0);
201 tty_setcolor (SELECTED_COLOR);
202 printwstr (fname, fname_len + gap);
203 printwstr (status, w - (fname_len + gap));
205 if (simple_statusbar && edit->widget.cols > 30) {
206 size_t percent;
207 if (edit->total_lines + 1 != 0)
208 percent = (edit->curs_line + 1) * 100 / (edit->total_lines + 1);
209 else
210 percent = 100;
211 widget_move (edit, 0, edit->widget.cols - 5);
212 tty_printf (" %3d%%", percent);
214 tty_setcolor (EDITOR_NORMAL_COLOR);
216 g_free (status);
219 /* this scrolls the text so that cursor is on the screen */
220 void edit_scroll_screen_over_cursor (WEdit * edit)
222 int p;
223 int outby;
224 int b_extreme, t_extreme, l_extreme, r_extreme;
226 if (edit->num_widget_lines <= 0 || edit->num_widget_columns <= 0)
227 return;
229 edit->num_widget_columns -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
230 edit->num_widget_lines -= EDIT_TEXT_VERTICAL_OFFSET - 1;
232 r_extreme = EDIT_RIGHT_EXTREME;
233 l_extreme = EDIT_LEFT_EXTREME;
234 b_extreme = EDIT_BOTTOM_EXTREME;
235 t_extreme = EDIT_TOP_EXTREME;
236 if (edit->found_len) {
237 b_extreme = max (edit->num_widget_lines / 4, b_extreme);
238 t_extreme = max (edit->num_widget_lines / 4, t_extreme);
240 if (b_extreme + t_extreme + 1 > edit->num_widget_lines) {
241 int n;
242 n = b_extreme + t_extreme;
243 b_extreme = (b_extreme * (edit->num_widget_lines - 1)) / n;
244 t_extreme = (t_extreme * (edit->num_widget_lines - 1)) / n;
246 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 {
276 unsigned int ch;
277 unsigned int style;
280 static inline void
281 print_to_widget (WEdit *edit, long row, int start_col, int start_col_real,
282 long end_col, struct line_s line[], char *status)
284 struct line_s *p;
286 int x = start_col_real;
287 int x1 = start_col + EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
288 int y = row + EDIT_TEXT_VERTICAL_OFFSET;
289 int cols_to_skip = abs (x);
291 tty_setcolor (EDITOR_NORMAL_COLOR);
292 tty_draw_hline (edit->widget.y + y, edit->widget.x + x1,
293 ' ', end_col + 1 - start_col);
295 if (option_line_state) {
296 int i;
297 for (i = 0; i < LINE_STATE_WIDTH; i++)
298 if (status[i] == '\0')
299 status[i] = ' ';
301 tty_setcolor (LINE_STATE_COLOR);
302 edit_move (x1 + FONT_OFFSET_X - option_line_state_width, y + FONT_OFFSET_Y);
303 tty_print_string (status);
306 edit_move (x1 + FONT_OFFSET_X, y + FONT_OFFSET_Y);
307 p = line;
309 while (p->ch) {
310 int style;
311 unsigned int textchar;
312 int color;
314 if (cols_to_skip) {
315 p++;
316 cols_to_skip--;
317 continue;
320 style = p->style & 0xFF00;
321 textchar = p->ch;
322 color = p->style >> 16;
324 if (style & MOD_ABNORMAL) {
325 /* Non-printable - use black background */
326 color = 0;
329 if (style & MOD_WHITESPACE) {
330 if (style & MOD_MARKED) {
331 textchar = ' ';
332 tty_setcolor (EDITOR_MARKED_COLOR);
333 } else {
334 #if 0
335 if (color != EDITOR_NORMAL_COLOR) {
336 textchar = ' ';
337 tty_lowlevel_setcolor (color);
338 } else
339 #endif
340 tty_setcolor (EDITOR_WHITESPACE_COLOR);
342 } else {
343 if (style & MOD_BOLD) {
344 tty_setcolor (EDITOR_BOLD_COLOR);
345 } else if (style & MOD_MARKED) {
346 tty_setcolor (EDITOR_MARKED_COLOR);
347 } else {
348 tty_lowlevel_setcolor (color);
351 tty_print_anychar (textchar);
352 p++;
356 int visible_tabs = 1, visible_tws = 1;
358 /* b is a pointer to the beginning of the line */
359 static void
360 edit_draw_this_line (WEdit *edit, long b, long row, long start_col,
361 long end_col)
363 struct line_s line[MAX_LINE_LEN];
364 struct line_s *p = line;
366 long m1 = 0, m2 = 0, q, c1, c2;
367 int col, start_col_real;
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 int cw = 1;
426 p->ch = 0;
427 p->style = 0;
428 if (q == edit->curs1)
429 p->style |= MOD_CURSOR;
430 if (q >= m1 && q < m2) {
431 if (column_highlighting) {
432 int x;
433 x = edit_move_forward3 (edit, b, 0, q);
434 c1 = min (edit->column1, edit->column2);
435 c2 = max (edit->column1, edit->column2);
436 if (x >= c1 && x < c2)
437 p->style |= MOD_MARKED;
438 } else
439 p->style |= MOD_MARKED;
441 if (q == edit->bracket)
442 p->style |= MOD_BOLD;
443 if (q >= edit->found_start
444 && q < edit->found_start + edit->found_len)
445 p->style |= MOD_BOLD;
447 if ( !edit->utf8 ) {
448 c = edit_get_byte (edit, q);
449 } else {
450 c = edit_get_utf (edit, q, &cw);
452 /* we don't use bg for mc - fg contains both */
453 if (book_mark) {
454 p->style |= book_mark << 16;
455 } else {
456 edit_get_syntax_color (edit, q, &color);
457 p->style |= color << 16;
459 switch (c) {
460 case '\n':
461 col = (end_col + utf8lag) - edit->start_col + 1; /* quit */
462 break;
463 case '\t':
464 i = TAB_SIZE - ((int) col % TAB_SIZE);
465 col += i;
466 if (tty_use_colors() &&
467 ((visible_tabs || (visible_tws && q >= tws)) && enable_show_tabs_tws)) {
468 if (p->style & MOD_MARKED)
469 c = p->style;
470 else if (book_mark)
471 c |= book_mark << 16;
472 else
473 c = p->style | MOD_WHITESPACE;
474 if (i > 2) {
475 p->ch = '<';
476 p->style = c;
477 p++;
478 while (--i > 1) {
479 p->ch = '-';
480 p->style = c;
481 p++;
483 p->ch = '>';
484 p->style = c;
485 p++;
486 } else if (i > 1) {
487 p->ch = '<';
488 p->style = c;
489 p++;
490 p->ch = '>';
491 p->style = c;
492 p++;
493 } else {
494 p->ch = '>';
495 p->style = c;
496 p++;
498 } else if (tty_use_colors() && visible_tws && q >= tws && enable_show_tabs_tws) {
499 p->ch = '.';
500 p->style |= MOD_WHITESPACE;
501 c = p->style & ~MOD_CURSOR;
502 p++;
503 while (--i) {
504 p->ch = ' ';
505 p->style = c;
506 p++;
508 } else {
509 p->ch |= ' ';
510 c = p->style & ~MOD_CURSOR;
511 p++;
512 while (--i) {
513 p->ch = ' ';
514 p->style = c;
515 p++;
518 break;
519 case ' ':
520 if (tty_use_colors() && visible_tws && q >= tws && enable_show_tabs_tws) {
521 p->ch = '.';
522 p->style |= MOD_WHITESPACE;
523 p++;
524 col++;
525 break;
527 /* fallthrough */
528 default:
529 #ifdef HAVE_CHARSET
530 if ( utf8_display ) {
531 if ( !edit->utf8 ) {
532 c = convert_from_8bit_to_utf_c ((unsigned char) c, edit->converter);
534 } else if ( edit->utf8 )
535 c = convert_from_utf_to_current_c (c, edit->converter);
536 else
537 #endif
538 c = convert_to_display_c (c);
540 /* Caret notation for control characters */
541 if (c < 32) {
542 p->ch = '^';
543 p->style = abn_style;
544 p++;
545 p->ch = c + 0x40;
546 p->style = abn_style;
547 p++;
548 col += 2;
549 break;
551 if (c == 127) {
552 p->ch = '^';
553 p->style = abn_style;
554 p++;
555 p->ch = '?';
556 p->style = abn_style;
557 p++;
558 col += 2;
559 break;
561 if (!edit->utf8) {
562 if ( ( utf8_display && g_unichar_isprint (c) ) ||
563 ( !utf8_display && is_printable (c) ) ) {
564 p->ch = c;
565 p++;
566 } else {
567 p->ch = '.';
568 p->style = abn_style;
569 p++;
571 } else {
572 if ( g_unichar_isprint (c) ) {
573 p->ch = c;
574 p++;
575 } else {
576 p->ch = '.';
577 p->style = abn_style;
578 p++;
581 col++;
582 break;
583 } /* case */
585 q++;
586 if ( cw > 1) {
587 q += cw - 1;
591 } else {
592 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 inline void
603 edit_draw_this_char (WEdit * edit, long curs, long row)
605 int b = edit_bol (edit, curs);
606 edit_draw_this_line (edit, b, row, 0, edit->num_widget_columns - 1);
609 /* cursor must be in screen for other than REDRAW_PAGE passed in force */
610 static inline void
611 render_edit_text (WEdit * edit, long start_row, long start_column, long end_row,
612 long end_column)
614 long row = 0, curs_row;
615 static long prev_curs_row = 0;
616 static long prev_curs = 0;
618 int force = edit->force;
619 long b;
622 * If the position of the page has not moved then we can draw the cursor
623 * character only. This will prevent line flicker when using arrow keys.
625 if ((!(force & REDRAW_CHAR_ONLY)) || (force & REDRAW_PAGE)) {
626 if (!(force & REDRAW_IN_BOUNDS)) { /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
627 start_row = 0;
628 end_row = edit->num_widget_lines - 1;
629 start_column = 0;
630 end_column = edit->num_widget_columns - 1;
632 if (force & REDRAW_PAGE) {
633 row = start_row;
634 b = edit_move_forward (edit, edit->start_display, start_row, 0);
635 while (row <= end_row) {
636 if (key_pending (edit))
637 goto exit_render;
638 edit_draw_this_line (edit, b, row, start_column, end_column);
639 b = edit_move_forward (edit, b, 1, 0);
640 row++;
642 } else {
643 curs_row = edit->curs_row;
645 if (force & REDRAW_BEFORE_CURSOR) {
646 if (start_row < curs_row) {
647 long upto = curs_row - 1 <= end_row ? curs_row - 1 : end_row;
648 row = start_row;
649 b = edit->start_display;
650 while (row <= upto) {
651 if (key_pending (edit))
652 goto exit_render;
653 edit_draw_this_line (edit, b, row, start_column, end_column);
654 b = edit_move_forward (edit, b, 1, 0);
658 /* if (force & REDRAW_LINE) ---> default */
659 b = edit_bol (edit, edit->curs1);
660 if (curs_row >= start_row && curs_row <= end_row) {
661 if (key_pending (edit))
662 goto exit_render;
663 edit_draw_this_line (edit, b, curs_row, start_column, end_column);
665 if (force & REDRAW_AFTER_CURSOR) {
666 if (end_row > curs_row) {
667 row = curs_row + 1 < start_row ? start_row : curs_row + 1;
668 b = edit_move_forward (edit, b, 1, 0);
669 while (row <= end_row) {
670 if (key_pending (edit))
671 goto exit_render;
672 edit_draw_this_line (edit, b, row, start_column, end_column);
673 b = edit_move_forward (edit, b, 1, 0);
674 row++;
678 if (force & REDRAW_LINE_ABOVE && curs_row >= 1) {
679 row = curs_row - 1;
680 b = edit_move_backward (edit, edit_bol (edit, edit->curs1), 1);
681 if (row >= start_row && row <= end_row) {
682 if (key_pending (edit))
683 goto exit_render;
684 edit_draw_this_line (edit, b, row, start_column, end_column);
687 if (force & REDRAW_LINE_BELOW && row < edit->num_widget_lines - 1) {
688 row = curs_row + 1;
689 b = edit_bol (edit, edit->curs1);
690 b = edit_move_forward (edit, b, 1, 0);
691 if (row >= start_row && row <= end_row) {
692 if (key_pending (edit))
693 goto exit_render;
694 edit_draw_this_line (edit, b, row, start_column, end_column);
698 } else {
699 if (prev_curs_row < edit->curs_row) { /* with the new text highlighting, we must draw from the top down */
700 edit_draw_this_char (edit, prev_curs, prev_curs_row);
701 edit_draw_this_char (edit, edit->curs1, edit->curs_row);
702 } else {
703 edit_draw_this_char (edit, edit->curs1, edit->curs_row);
704 edit_draw_this_char (edit, prev_curs, prev_curs_row);
708 edit->force = 0;
710 prev_curs_row = edit->curs_row;
711 prev_curs = edit->curs1;
713 exit_render:
714 edit->screen_modified = 0;
717 static inline 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);