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
28 #include <sys/types.h>
37 #include "../src/global.h"
40 #include "edit-widget.h"
42 #define MAX_LINE_LEN 1024
44 #include "../src/color.h" /* EDITOR_NORMAL_COLOR */
45 #include "../src/tty.h" /* attrset() */
46 #include "../src/widget.h" /* buttonbar_redraw() */
47 #include "../src/key.h" /* is_idle() */
48 #include "../src/charsets.h"
51 #define MOD_ABNORMAL (1 << 8)
52 #define MOD_BOLD (1 << 9)
53 #define MOD_MARKED (1 << 10)
54 #define MOD_CURSOR (1 << 11)
55 #define MOD_WHITESPACE (1 << 12)
57 #define FONT_OFFSET_X 0
58 #define FONT_OFFSET_Y 0
60 #define FONT_PIX_PER_LINE 1
61 #define FONT_MEAN_WIDTH 1
64 static void status_string (WEdit
* edit
, char *s
, int w
)
69 * If we are at the end of file, print <EOF>,
70 * otherwise print the current character as is (if printable),
71 * as decimal and as hex.
73 if (edit
->curs1
< edit
->last_byte
) {
74 unsigned char cur_byte
= edit_get_byte (edit
, edit
->curs1
);
75 snprintf (byte_str
, sizeof (byte_str
), "%c %3d 0x%02X",
76 is_printable (cur_byte
) ? cur_byte
: '.',
80 strcpy (byte_str
, "<EOF>");
83 /* The field lengths just prevent the status line from shortening too much */
85 "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb)= %s",
86 edit
->mark1
!= edit
->mark2
? ( column_highlighting
? 'C' : 'B') : '-',
87 edit
->modified
? 'M' : '-',
88 edit
->macro_i
< 0 ? '-' : 'R',
89 edit
->overwrite
== 0 ? '-' : 'O',
95 edit
->total_lines
+ 1,
103 printwstr (const char *s
, int len
)
106 tty_printf ("%-*.*s", len
, len
, s
);
109 /* Draw the status line at the top of the widget. The size of the filename
110 * field varies depending on the width of the screen and the length of
113 edit_status (WEdit
*edit
)
115 const int w
= edit
->widget
.cols
;
116 const size_t status_size
= w
+ 1;
117 char * const status
= g_malloc (status_size
);
119 const char *fname
= "";
121 const int gap
= 3; /* between the filename and the status */
122 const int right_gap
= 2; /* at the right end of the screen */
123 const int preferred_fname_len
= 16;
125 status_string (edit
, status
, status_size
);
126 status_len
= (int) strlen (status
);
129 fname
= edit
->filename
;
130 fname_len
= strlen(fname
);
131 if (fname_len
< preferred_fname_len
)
132 fname_len
= preferred_fname_len
;
134 if (fname_len
+ gap
+ status_len
+ right_gap
>= w
) {
135 if (preferred_fname_len
+ gap
+ status_len
+ right_gap
>= w
)
136 fname_len
= preferred_fname_len
;
138 fname_len
= w
- (gap
+ status_len
+ right_gap
);
139 fname
= name_trunc (fname
, fname_len
);
142 widget_move (edit
, 0, 0);
143 attrset (SELECTED_COLOR
);
144 printwstr (fname
, fname_len
+ gap
);
145 printwstr (status
, w
- (fname_len
+ gap
));
146 attrset (EDITOR_NORMAL_COLOR
);
151 /* this scrolls the text so that cursor is on the screen */
152 void edit_scroll_screen_over_cursor (WEdit
* edit
)
156 int b_extreme
, t_extreme
, l_extreme
, r_extreme
;
158 if (edit
->num_widget_lines
<= 0 || edit
->num_widget_columns
<= 0)
161 r_extreme
= EDIT_RIGHT_EXTREME
;
162 l_extreme
= EDIT_LEFT_EXTREME
;
163 b_extreme
= EDIT_BOTTOM_EXTREME
;
164 t_extreme
= EDIT_TOP_EXTREME
;
165 if (edit
->found_len
) {
166 b_extreme
= max (edit
->num_widget_lines
/ 4, b_extreme
);
167 t_extreme
= max (edit
->num_widget_lines
/ 4, t_extreme
);
169 if (b_extreme
+ t_extreme
+ 1 > edit
->num_widget_lines
) {
171 n
= b_extreme
+ t_extreme
;
172 b_extreme
= (b_extreme
* (edit
->num_widget_lines
- 1)) / n
;
173 t_extreme
= (t_extreme
* (edit
->num_widget_lines
- 1)) / n
;
175 if (l_extreme
+ r_extreme
+ 1 > edit
->num_widget_columns
) {
177 n
= l_extreme
+ t_extreme
;
178 l_extreme
= (l_extreme
* (edit
->num_widget_columns
- 1)) / n
;
179 r_extreme
= (r_extreme
* (edit
->num_widget_columns
- 1)) / n
;
181 p
= edit_get_col (edit
);
182 edit_update_curs_row (edit
);
183 outby
= p
+ edit
->start_col
- edit
->num_widget_columns
+ 1 + (r_extreme
+ edit
->found_len
);
185 edit_scroll_right (edit
, outby
);
186 outby
= l_extreme
- p
- edit
->start_col
;
188 edit_scroll_left (edit
, outby
);
190 outby
= p
- edit
->num_widget_lines
+ 1 + b_extreme
;
192 edit_scroll_downward (edit
, outby
);
193 outby
= t_extreme
- p
;
195 edit_scroll_upward (edit
, outby
);
196 edit_update_curs_row (edit
);
199 #define set_color(font) attrset (font)
201 #define edit_move(x,y) widget_move(edit, y, x);
203 /* Set colorpair by index, don't interpret S-Lang "emulated attributes" */
205 #define lowlevel_set_color(x) SLsmg_set_color(x & 0x7F)
207 #define lowlevel_set_color(x) attrset(MY_COLOR_PAIR(color))
211 print_to_widget (WEdit
*edit
, long row
, int start_col
, int start_col_real
,
212 long end_col
, unsigned int line
[])
216 int x
= start_col_real
+ EDIT_TEXT_HORIZONTAL_OFFSET
;
217 int x1
= start_col
+ EDIT_TEXT_HORIZONTAL_OFFSET
;
218 int y
= row
+ EDIT_TEXT_VERTICAL_OFFSET
;
219 int cols_to_skip
= abs (x
);
221 set_color (EDITOR_NORMAL_COLOR
);
223 hline (' ', end_col
+ 1 - EDIT_TEXT_HORIZONTAL_OFFSET
- x1
);
225 edit_move (x1
+ FONT_OFFSET_X
, y
+ FONT_OFFSET_Y
);
240 textchar
= *p
& 0xFF;
243 if (style
& MOD_ABNORMAL
) {
244 /* Non-printable - use black background */
248 if (style
& MOD_WHITESPACE
) {
249 if (style
& MOD_MARKED
) {
251 set_color (EDITOR_MARKED_COLOR
);
254 if (color
!= EDITOR_NORMAL_COLOR
) {
256 lowlevel_set_color (color
);
259 set_color (EDITOR_WHITESPACE_COLOR
);
262 if (style
& MOD_BOLD
) {
263 set_color (EDITOR_BOLD_COLOR
);
264 } else if (style
& MOD_MARKED
) {
265 set_color (EDITOR_MARKED_COLOR
);
267 lowlevel_set_color (color
);
276 int visible_tabs
= 1, visible_tws
= 1;
278 /* b is a pointer to the beginning of the line */
280 edit_draw_this_line (WEdit
*edit
, long b
, long row
, long start_col
,
283 static unsigned int line
[MAX_LINE_LEN
];
284 unsigned int *p
= line
;
285 long m1
= 0, m2
= 0, q
, c1
, c2
;
286 int col
, start_col_real
;
291 edit_get_syntax_color (edit
, b
- 1, &color
);
292 q
= edit_move_forward3 (edit
, b
, start_col
- edit
->start_col
, 0);
293 start_col_real
= (col
=
294 (int) edit_move_forward3 (edit
, b
, 0,
295 q
)) + edit
->start_col
;
296 c1
= min (edit
->column1
, edit
->column2
);
297 c2
= max (edit
->column1
, edit
->column2
);
299 if (col
+ 16 > -edit
->start_col
) {
300 eval_marks (edit
, &m1
, &m2
);
302 if (row
<= edit
->total_lines
- edit
->start_line
) {
304 if (use_colors
&& visible_tws
) {
305 tws
= edit_eol (edit
, b
);
306 while (tws
> b
&& ((c
= edit_get_byte (edit
, tws
- 1)) == ' '
311 while (col
<= end_col
- edit
->start_col
) {
313 if (q
== edit
->curs1
)
315 if (q
>= m1
&& q
< m2
) {
316 if (column_highlighting
) {
318 x
= edit_move_forward3 (edit
, b
, 0, q
);
319 if (x
>= c1
&& x
< c2
)
324 if (q
== edit
->bracket
)
326 if (q
>= edit
->found_start
327 && q
< edit
->found_start
+ edit
->found_len
)
329 c
= edit_get_byte (edit
, q
);
330 /* we don't use bg for mc - fg contains both */
331 edit_get_syntax_color (edit
, q
, &color
);
335 col
= end_col
- edit
->start_col
+ 1; /* quit */
339 i
= TAB_SIZE
- ((int) col
% TAB_SIZE
);
341 if (use_colors
&& visible_tabs
) {
342 c
= (*p
& ~MOD_CURSOR
) | MOD_WHITESPACE
;
344 *(p
++) |= '<' | MOD_WHITESPACE
;
349 *(p
++) |= '<' | MOD_WHITESPACE
;
352 *(p
++) |= '>' | MOD_WHITESPACE
;
353 } else if (use_colors
&& visible_tws
&& q
>= tws
) {
354 *p
|= '.' | MOD_WHITESPACE
;
355 c
= *(p
++) & ~MOD_CURSOR
;
360 c
= *(p
++) & ~MOD_CURSOR
;
366 if (use_colors
&& visible_tws
&& q
>= tws
) {
367 *(p
++) |= '.' | MOD_WHITESPACE
;
373 c
= convert_to_display_c (c
);
375 /* Caret notation for control characters */
377 *(p
++) = '^' | MOD_ABNORMAL
;
378 *(p
++) = (c
+ 0x40) | MOD_ABNORMAL
;
383 *(p
++) = '^' | MOD_ABNORMAL
;
384 *(p
++) = '?' | MOD_ABNORMAL
;
389 if (is_printable (c
)) {
392 *(p
++) = '.' | MOD_ABNORMAL
;
401 start_col_real
= start_col
= 0;
405 print_to_widget (edit
, row
, start_col
, start_col_real
, end_col
, line
);
408 #define key_pending(x) (!is_idle())
410 static void edit_draw_this_char (WEdit
* edit
, long curs
, long row
)
412 int b
= edit_bol (edit
, curs
);
413 edit_draw_this_line (edit
, b
, row
, 0, edit
->num_widget_columns
- 1);
416 /* cursor must be in screen for other than REDRAW_PAGE passed in force */
418 render_edit_text (WEdit
* edit
, long start_row
, long start_column
, long end_row
,
421 long row
= 0, curs_row
;
422 static int prev_curs_row
= 0;
423 static long prev_curs
= 0;
425 int force
= edit
->force
;
429 * If the position of the page has not moved then we can draw the cursor
430 * character only. This will prevent line flicker when using arrow keys.
432 if ((!(force
& REDRAW_CHAR_ONLY
)) || (force
& REDRAW_PAGE
)) {
433 if (!(force
& REDRAW_IN_BOUNDS
)) { /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
435 end_row
= edit
->num_widget_lines
- 1;
437 end_column
= edit
->num_widget_columns
- 1;
439 if (force
& REDRAW_PAGE
) {
441 b
= edit_move_forward (edit
, edit
->start_display
, start_row
, 0);
442 while (row
<= end_row
) {
443 if (key_pending (edit
))
445 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
446 b
= edit_move_forward (edit
, b
, 1, 0);
450 curs_row
= edit
->curs_row
;
452 if (force
& REDRAW_BEFORE_CURSOR
) {
453 if (start_row
< curs_row
) {
454 long upto
= curs_row
- 1 <= end_row
? curs_row
- 1 : end_row
;
456 b
= edit
->start_display
;
457 while (row
<= upto
) {
458 if (key_pending (edit
))
460 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
461 b
= edit_move_forward (edit
, b
, 1, 0);
465 /* if (force & REDRAW_LINE) ---> default */
466 b
= edit_bol (edit
, edit
->curs1
);
467 if (curs_row
>= start_row
&& curs_row
<= end_row
) {
468 if (key_pending (edit
))
470 edit_draw_this_line (edit
, b
, curs_row
, start_column
, end_column
);
472 if (force
& REDRAW_AFTER_CURSOR
) {
473 if (end_row
> curs_row
) {
474 row
= curs_row
+ 1 < start_row
? start_row
: curs_row
+ 1;
475 b
= edit_move_forward (edit
, b
, 1, 0);
476 while (row
<= end_row
) {
477 if (key_pending (edit
))
479 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
480 b
= edit_move_forward (edit
, b
, 1, 0);
485 if (force
& REDRAW_LINE_ABOVE
&& curs_row
>= 1) {
487 b
= edit_move_backward (edit
, edit_bol (edit
, edit
->curs1
), 1);
488 if (row
>= start_row
&& row
<= end_row
) {
489 if (key_pending (edit
))
491 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
494 if (force
& REDRAW_LINE_BELOW
&& row
< edit
->num_widget_lines
- 1) {
496 b
= edit_bol (edit
, edit
->curs1
);
497 b
= edit_move_forward (edit
, b
, 1, 0);
498 if (row
>= start_row
&& row
<= end_row
) {
499 if (key_pending (edit
))
501 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
506 if (prev_curs_row
< edit
->curs_row
) { /* with the new text highlighting, we must draw from the top down */
507 edit_draw_this_char (edit
, prev_curs
, prev_curs_row
);
508 edit_draw_this_char (edit
, edit
->curs1
, edit
->curs_row
);
510 edit_draw_this_char (edit
, edit
->curs1
, edit
->curs_row
);
511 edit_draw_this_char (edit
, prev_curs
, prev_curs_row
);
517 prev_curs_row
= edit
->curs_row
;
518 prev_curs
= edit
->curs1
;
521 edit
->screen_modified
= 0;
526 edit_render (WEdit
* edit
, int page
, int row_start
, int col_start
, int row_end
, int col_end
)
528 if (page
) /* if it was an expose event, 'page' would be set */
529 edit
->force
|= REDRAW_PAGE
| REDRAW_IN_BOUNDS
;
531 if (edit
->force
& REDRAW_COMPLETELY
)
532 buttonbar_redraw (edit
->widget
.parent
);
533 render_edit_text (edit
, row_start
, col_start
, row_end
, col_end
);
535 * edit->force != 0 means a key was pending and the redraw
536 * was halted, so next time we must redraw everything in case stuff
537 * was left undrawn from a previous key press.
540 edit
->force
|= REDRAW_PAGE
;
543 void edit_render_keypress (WEdit
* edit
)
545 edit_render (edit
, 0, 0, 0, 0, 0);