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
25 * \brief Source: editor text drawing
34 #include <sys/types.h>
41 #include "lib/global.h"
42 #include "lib/tty/tty.h" /* tty_printf() */
44 #include "lib/tty/key.h" /* is_idle() */
45 #include "lib/strutil.h" /* utf string functions */
47 #include "edit-impl.h"
48 #include "edit-widget.h"
50 #define MAX_LINE_LEN 1024
53 #include "src/widget.h" /* buttonbar_redraw() */
54 #include "src/charsets.h"
55 #include "src/main.h" /* source_codepage */
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
67 #define FONT_PIX_PER_LINE 1
68 #define FONT_MEAN_WIDTH 1
70 /* Toggles statusbar draw style */
71 int simple_statusbar
= 0;
74 status_string (WEdit
* edit
, char *s
, int w
)
77 unsigned char cur_byte
= 0;
78 unsigned int cur_utf
= 0;
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 cur_byte
= edit_get_byte (edit
, edit
->curs1
);
90 g_snprintf (byte_str
, sizeof (byte_str
), "%4d 0x%03X",
94 cur_utf
= edit_get_utf (edit
, edit
->curs1
, &cw
);
96 g_snprintf (byte_str
, sizeof (byte_str
), "%04d 0x%03X",
100 cur_utf
= edit_get_byte (edit
, edit
->curs1
);
101 g_snprintf (byte_str
, sizeof (byte_str
), "%04d 0x%03X",
108 strcpy (byte_str
, "<EOF> ");
111 /* The field lengths just prevent the status line from shortening too much */
112 if (simple_statusbar
)
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
,
122 edit
->total_lines
+ 1,
129 source_codepage
>= 0 ? get_codepage_id (source_codepage
) : ""
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,
146 edit
->total_lines
+ 1,
153 source_codepage
>= 0 ? get_codepage_id (source_codepage
) : ""
161 printwstr (const char *s
, int len
)
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
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
);
177 const char *fname
= "";
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
);
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
;
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) {
207 if (edit
->total_lines
+ 1 != 0)
208 percent
= (edit
->curs_line
+ 1) * 100 / (edit
->total_lines
+ 1);
211 widget_move (edit
, 0, edit
->widget
.cols
- 5);
212 tty_printf (" %3d%%", percent
);
214 tty_setcolor (EDITOR_NORMAL_COLOR
);
219 /* this scrolls the text so that cursor is on the screen */
220 void edit_scroll_screen_over_cursor (WEdit
* edit
)
224 int b_extreme
, t_extreme
, l_extreme
, r_extreme
;
226 if (edit
->num_widget_lines
<= 0 || edit
->num_widget_columns
<= 0)
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
) {
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
) {
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
);
256 edit_scroll_right (edit
, outby
);
257 outby
= l_extreme
- p
- edit
->start_col
;
259 edit_scroll_left (edit
, outby
);
261 outby
= p
- edit
->num_widget_lines
+ 1 + b_extreme
;
263 edit_scroll_downward (edit
, outby
);
264 outby
= t_extreme
- p
;
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);
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
)
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
);
292 tty_setcolor (EDITOR_NORMAL_COLOR
);
294 if (!show_right_margin
) {
295 tty_draw_hline (edit
->widget
.y
+ y
, edit
->widget
.x
+ x1
,
296 ' ', end_col
+ 1 - start_col
);
297 } else if (edit
->start_col
< option_word_wrap_line_length
) {
298 tty_draw_hline (edit
->widget
.y
+ y
,
301 option_word_wrap_line_length
- edit
->start_col
);
303 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR
);
304 tty_draw_hline (edit
->widget
.y
+ y
,
305 edit
->widget
.x
+ x1
+ option_word_wrap_line_length
+ edit
->start_col
,
307 end_col
+ 1 - start_col
);
310 if (option_line_state
) {
311 for (i
= 0; i
< LINE_STATE_WIDTH
; i
++)
312 if (status
[i
] == '\0')
315 tty_setcolor (LINE_STATE_COLOR
);
316 edit_move (x1
+ FONT_OFFSET_X
- option_line_state_width
, y
+ FONT_OFFSET_Y
);
317 tty_print_string (status
);
320 edit_move (x1
+ FONT_OFFSET_X
, y
+ FONT_OFFSET_Y
);
325 unsigned int textchar
;
334 style
= p
->style
& 0xFF00;
336 color
= p
->style
>> 16;
338 if (style
& MOD_ABNORMAL
) {
339 /* Non-printable - use black background */
343 if (style
& MOD_WHITESPACE
) {
344 if (style
& MOD_MARKED
) {
346 tty_setcolor (EDITOR_MARKED_COLOR
);
349 if (color
!= EDITOR_NORMAL_COLOR
) {
351 tty_lowlevel_setcolor (color
);
354 tty_setcolor (EDITOR_WHITESPACE_COLOR
);
357 if (style
& MOD_BOLD
) {
358 tty_setcolor (EDITOR_BOLD_COLOR
);
359 } else if (style
& MOD_MARKED
) {
360 tty_setcolor (EDITOR_MARKED_COLOR
);
362 tty_lowlevel_setcolor (color
);
365 if (show_right_margin
) {
366 if (i
> option_word_wrap_line_length
+ edit
->start_col
)
367 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR
);
370 tty_print_anychar (textchar
);
375 int visible_tabs
= 1, visible_tws
= 1;
377 /* b is a pointer to the beginning of the line */
379 edit_draw_this_line (WEdit
*edit
, long b
, long row
, long start_col
,
382 struct line_s line
[MAX_LINE_LEN
];
383 struct line_s
*p
= line
;
385 long m1
= 0, m2
= 0, q
, c1
, c2
;
386 int col
, start_col_real
;
392 unsigned int cur_line
= 0;
394 char line_stat
[LINE_STATE_WIDTH
+ 1];
396 if (row
> edit
->num_widget_lines
- EDIT_TEXT_VERTICAL_OFFSET
) {
399 if (book_mark_query_color(edit
, edit
->start_line
+ row
, BOOK_MARK_COLOR
)) {
400 book_mark
= BOOK_MARK_COLOR
;
401 } else if (book_mark_query_color(edit
, edit
->start_line
+ row
, BOOK_MARK_FOUND_COLOR
)) {
402 book_mark
= BOOK_MARK_FOUND_COLOR
;
406 abn_style
= book_mark
<< 16;
408 abn_style
= MOD_ABNORMAL
;
410 end_col
-= EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
412 edit_get_syntax_color (edit
, b
- 1, &color
);
413 q
= edit_move_forward3 (edit
, b
, start_col
- edit
->start_col
, 0);
414 start_col_real
= (col
=
415 (int) edit_move_forward3 (edit
, b
, 0,
416 q
)) + edit
->start_col
;
417 if ( option_line_state
) {
418 cur_line
= edit
->start_line
+ row
;
419 if ( cur_line
<= (unsigned int) edit
->total_lines
) {
420 g_snprintf (line_stat
, LINE_STATE_WIDTH
+ 1, "%7i ", cur_line
+ 1);
422 memset(line_stat
, ' ', LINE_STATE_WIDTH
);
423 line_stat
[LINE_STATE_WIDTH
] = '\0';
425 if (book_mark_query_color (edit
, cur_line
, BOOK_MARK_COLOR
)){
426 g_snprintf (line_stat
, 2, "*");
430 if (col
+ 16 > -edit
->start_col
) {
431 eval_marks (edit
, &m1
, &m2
);
433 if (row
<= edit
->total_lines
- edit
->start_line
) {
435 if (tty_use_colors () && visible_tws
) {
436 tws
= edit_eol (edit
, b
);
437 while (tws
> b
&& ((c
= edit_get_byte (edit
, tws
- 1)) == ' '
442 while (col
<= end_col
- edit
->start_col
) {
447 if (q
== edit
->curs1
)
448 p
->style
|= MOD_CURSOR
;
449 if (q
>= m1
&& q
< m2
) {
450 if (column_highlighting
) {
452 x
= edit_move_forward3 (edit
, b
, 0, q
);
453 c1
= min (edit
->column1
, edit
->column2
);
454 c2
= max (edit
->column1
, edit
->column2
);
455 if (x
>= c1
&& x
< c2
)
456 p
->style
|= MOD_MARKED
;
458 p
->style
|= MOD_MARKED
;
460 if (q
== edit
->bracket
)
461 p
->style
|= MOD_BOLD
;
462 if (q
>= edit
->found_start
463 && q
< edit
->found_start
+ edit
->found_len
)
464 p
->style
|= MOD_BOLD
;
467 c
= edit_get_byte (edit
, q
);
469 c
= edit_get_utf (edit
, q
, &cw
);
471 /* we don't use bg for mc - fg contains both */
473 p
->style
|= book_mark
<< 16;
475 edit_get_syntax_color (edit
, q
, &color
);
476 p
->style
|= color
<< 16;
480 col
= (end_col
+ utf8lag
) - edit
->start_col
+ 1; /* quit */
483 i
= TAB_SIZE
- ((int) col
% TAB_SIZE
);
485 if (tty_use_colors() &&
486 ((visible_tabs
|| (visible_tws
&& q
>= tws
)) && enable_show_tabs_tws
)) {
487 if (p
->style
& MOD_MARKED
)
490 c
|= book_mark
<< 16;
492 c
= p
->style
| MOD_WHITESPACE
;
517 } else if (tty_use_colors() && visible_tws
&& q
>= tws
&& enable_show_tabs_tws
) {
519 p
->style
|= MOD_WHITESPACE
;
520 c
= p
->style
& ~MOD_CURSOR
;
529 c
= p
->style
& ~MOD_CURSOR
;
539 if (tty_use_colors() && visible_tws
&& q
>= tws
&& enable_show_tabs_tws
) {
541 p
->style
|= MOD_WHITESPACE
;
549 if ( utf8_display
) {
551 c
= convert_from_8bit_to_utf_c ((unsigned char) c
, edit
->converter
);
553 } else if ( edit
->utf8
)
554 c
= convert_from_utf_to_current_c (c
, edit
->converter
);
557 c
= convert_to_display_c (c
);
559 /* Caret notation for control characters */
562 p
->style
= abn_style
;
565 p
->style
= abn_style
;
572 p
->style
= abn_style
;
575 p
->style
= abn_style
;
581 if ( ( utf8_display
&& g_unichar_isprint (c
) ) ||
582 ( !utf8_display
&& is_printable (c
) ) ) {
587 p
->style
= abn_style
;
591 if ( g_unichar_isprint (c
) ) {
596 p
->style
= abn_style
;
611 start_col_real
= start_col
= 0;
616 print_to_widget (edit
, row
, start_col
, start_col_real
, end_col
, line
, line_stat
);
619 #define key_pending(x) (!is_idle())
622 edit_draw_this_char (WEdit
* edit
, long curs
, long row
)
624 int b
= edit_bol (edit
, curs
);
625 edit_draw_this_line (edit
, b
, row
, 0, edit
->num_widget_columns
- 1);
628 /* cursor must be in screen for other than REDRAW_PAGE passed in force */
630 render_edit_text (WEdit
* edit
, long start_row
, long start_column
, long end_row
,
633 long row
= 0, curs_row
;
634 static long prev_curs_row
= 0;
635 static long prev_curs
= 0;
637 int force
= edit
->force
;
641 * If the position of the page has not moved then we can draw the cursor
642 * character only. This will prevent line flicker when using arrow keys.
644 if ((!(force
& REDRAW_CHAR_ONLY
)) || (force
& REDRAW_PAGE
)) {
645 if (!(force
& REDRAW_IN_BOUNDS
)) { /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
647 end_row
= edit
->num_widget_lines
- 1;
649 end_column
= edit
->num_widget_columns
- 1;
651 if (force
& REDRAW_PAGE
) {
653 b
= edit_move_forward (edit
, edit
->start_display
, start_row
, 0);
654 while (row
<= end_row
) {
655 if (key_pending (edit
))
657 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
658 b
= edit_move_forward (edit
, b
, 1, 0);
662 curs_row
= edit
->curs_row
;
664 if (force
& REDRAW_BEFORE_CURSOR
) {
665 if (start_row
< curs_row
) {
666 long upto
= curs_row
- 1 <= end_row
? curs_row
- 1 : end_row
;
668 b
= edit
->start_display
;
669 while (row
<= upto
) {
670 if (key_pending (edit
))
672 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
673 b
= edit_move_forward (edit
, b
, 1, 0);
677 /* if (force & REDRAW_LINE) ---> default */
678 b
= edit_bol (edit
, edit
->curs1
);
679 if (curs_row
>= start_row
&& curs_row
<= end_row
) {
680 if (key_pending (edit
))
682 edit_draw_this_line (edit
, b
, curs_row
, start_column
, end_column
);
684 if (force
& REDRAW_AFTER_CURSOR
) {
685 if (end_row
> curs_row
) {
686 row
= curs_row
+ 1 < start_row
? start_row
: curs_row
+ 1;
687 b
= edit_move_forward (edit
, b
, 1, 0);
688 while (row
<= end_row
) {
689 if (key_pending (edit
))
691 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
692 b
= edit_move_forward (edit
, b
, 1, 0);
697 if (force
& REDRAW_LINE_ABOVE
&& curs_row
>= 1) {
699 b
= edit_move_backward (edit
, edit_bol (edit
, edit
->curs1
), 1);
700 if (row
>= start_row
&& row
<= end_row
) {
701 if (key_pending (edit
))
703 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
706 if (force
& REDRAW_LINE_BELOW
&& row
< edit
->num_widget_lines
- 1) {
708 b
= edit_bol (edit
, edit
->curs1
);
709 b
= edit_move_forward (edit
, b
, 1, 0);
710 if (row
>= start_row
&& row
<= end_row
) {
711 if (key_pending (edit
))
713 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
718 if (prev_curs_row
< edit
->curs_row
) { /* with the new text highlighting, we must draw from the top down */
719 edit_draw_this_char (edit
, prev_curs
, prev_curs_row
);
720 edit_draw_this_char (edit
, edit
->curs1
, edit
->curs_row
);
722 edit_draw_this_char (edit
, edit
->curs1
, edit
->curs_row
);
723 edit_draw_this_char (edit
, prev_curs
, prev_curs_row
);
729 prev_curs_row
= edit
->curs_row
;
730 prev_curs
= edit
->curs1
;
733 edit
->screen_modified
= 0;
737 edit_render (WEdit
* edit
, int page
, int row_start
, int col_start
, int row_end
, int col_end
)
739 if (page
) /* if it was an expose event, 'page' would be set */
740 edit
->force
|= REDRAW_PAGE
| REDRAW_IN_BOUNDS
;
742 if (edit
->force
& REDRAW_COMPLETELY
)
743 buttonbar_redraw (find_buttonbar (edit
->widget
.parent
));
744 render_edit_text (edit
, row_start
, col_start
, row_end
, col_end
);
746 * edit->force != 0 means a key was pending and the redraw
747 * was halted, so next time we must redraw everything in case stuff
748 * was left undrawn from a previous key press.
751 edit
->force
|= REDRAW_PAGE
;
754 void edit_render_keypress (WEdit
* edit
)
756 edit_render (edit
, 0, 0, 0, 0, 0);