4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
6 The Free Software Foundation, Inc.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * \brief Source: editor text drawing
37 #include <sys/types.h>
44 #include "lib/global.h"
45 #include "lib/tty/tty.h" /* tty_printf() */
46 #include "lib/tty/key.h" /* is_idle() */
48 #include "lib/strutil.h" /* utf string functions */
49 #include "lib/util.h" /* is_printable() */
50 #include "lib/widget.h" /* buttonbar_redraw() */
51 #include "lib/charsets.h"
53 #include "src/setup.h" /* edit_tab_spacing */
54 #include "src/main.h" /* macro_index */
56 #include "edit-impl.h"
57 #include "editwidget.h"
59 /*** global variables ****************************************************************************/
61 /* Toggles statusbar draw style */
62 int simple_statusbar
= 0;
64 int visible_tabs
= 1, visible_tws
= 1;
66 /*** file scope macro definitions ****************************************************************/
68 #define MAX_LINE_LEN 1024
71 #define MOD_ABNORMAL (1 << 8)
72 #define MOD_BOLD (1 << 9)
73 #define MOD_MARKED (1 << 10)
74 #define MOD_CURSOR (1 << 11)
75 #define MOD_WHITESPACE (1 << 12)
77 #define edit_move(x,y) widget_move(edit, y, x);
79 #define key_pending(x) (!is_idle())
81 #define EDITOR_MINIMUM_TERMINAL_WIDTH 30
83 /*** file scope type declarations ****************************************************************/
91 /*** file scope variables ************************************************************************/
93 /*** file scope functions ************************************************************************/
94 /* --------------------------------------------------------------------------------------------- */
97 status_string (WEdit
* edit
, char *s
, int w
)
100 unsigned char cur_byte
= 0;
101 unsigned int cur_utf
= 0;
105 * If we are at the end of file, print <EOF>,
106 * otherwise print the current character as is (if printable),
107 * as decimal and as hex.
109 if (edit
->curs1
< edit
->last_byte
)
113 cur_byte
= edit_get_byte (edit
, edit
->curs1
);
115 g_snprintf (byte_str
, sizeof (byte_str
), "%4d 0x%03X",
116 (int) cur_byte
, (unsigned) cur_byte
);
120 cur_utf
= edit_get_utf (edit
, edit
->curs1
, &cw
);
123 g_snprintf (byte_str
, sizeof (byte_str
), "%04d 0x%03X",
124 (unsigned) cur_utf
, (unsigned) cur_utf
);
128 cur_utf
= edit_get_byte (edit
, edit
->curs1
);
129 g_snprintf (byte_str
, sizeof (byte_str
), "%04d 0x%03X",
130 (int) cur_utf
, (unsigned) cur_utf
);
137 strcpy (byte_str
, "<EOF> ");
140 /* The field lengths just prevent the status line from shortening too much */
141 if (simple_statusbar
)
143 "%c%c%c%c %3ld %5ld/%ld %6ld/%ld %s %s",
144 edit
->mark1
!= edit
->mark2
? (edit
->column_highlight
? 'C' : 'B') : '-',
145 edit
->modified
? 'M' : '-',
146 macro_index
< 0 ? '-' : 'R',
147 edit
->overwrite
== 0 ? '-' : 'O',
148 edit
->curs_col
+ edit
->over_col
,
150 edit
->total_lines
+ 1, edit
->curs1
, edit
->last_byte
, byte_str
,
152 mc_global
.source_codepage
>=
153 0 ? get_codepage_id (mc_global
.source_codepage
) : ""
160 "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb) %s %s",
161 edit
->mark1
!= edit
->mark2
? (edit
->column_highlight
? 'C' : 'B') : '-',
162 edit
->modified
? 'M' : '-',
163 macro_index
< 0 ? '-' : 'R',
164 edit
->overwrite
== 0 ? '-' : 'O',
165 edit
->curs_col
+ edit
->over_col
,
166 edit
->start_line
+ 1,
169 edit
->total_lines
+ 1, edit
->curs1
, edit
->last_byte
, byte_str
,
171 mc_global
.source_codepage
>=
172 0 ? get_codepage_id (mc_global
.source_codepage
) : ""
179 /* --------------------------------------------------------------------------------------------- */
182 printwstr (const char *s
, int len
)
185 tty_printf ("%-*.*s", len
, len
, s
);
188 /* --------------------------------------------------------------------------------------------- */
191 print_to_widget (WEdit
* edit
, long row
, int start_col
, int start_col_real
,
192 long end_col
, struct line_s line
[], char *status
, int bookmarked
)
196 int x
= start_col_real
;
197 int x1
= start_col
+ EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
198 int y
= row
+ EDIT_TEXT_VERTICAL_OFFSET
;
199 int cols_to_skip
= abs (x
);
204 tty_setcolor (EDITOR_NORMAL_COLOR
);
206 tty_setcolor (bookmarked
);
208 len
= end_col
+ 1 - start_col
;
209 wrap_start
= option_word_wrap_line_length
+ edit
->start_col
;
211 if (len
> 0 && edit
->widget
.y
+ y
>= 0)
213 if (!show_right_margin
|| wrap_start
> end_col
)
214 tty_draw_hline (edit
->widget
.y
+ y
, edit
->widget
.x
+ x1
, ' ', len
);
215 else if (wrap_start
< 0)
217 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR
);
218 tty_draw_hline (edit
->widget
.y
+ y
, edit
->widget
.x
+ x1
, ' ', len
);
223 tty_draw_hline (edit
->widget
.y
+ y
, edit
->widget
.x
+ x1
, ' ', wrap_start
);
228 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR
);
229 tty_draw_hline (edit
->widget
.y
+ y
, edit
->widget
.x
+ x1
+ wrap_start
, ' ', len
);
234 if (option_line_state
)
236 tty_setcolor (LINE_STATE_COLOR
);
237 for (i
= 0; i
< LINE_STATE_WIDTH
; i
++)
239 edit_move (x1
+ i
- option_line_state_width
, y
);
240 if (status
[i
] == '\0')
242 tty_print_char (status
[i
]);
248 for (p
= line
; p
->ch
!= 0; p
++)
251 unsigned int textchar
;
254 if (cols_to_skip
!= 0)
260 style
= p
->style
& 0xFF00;
262 color
= p
->style
>> 16;
264 if (style
& MOD_ABNORMAL
)
266 /* Non-printable - use black background */
270 if (style
& MOD_WHITESPACE
)
272 if (style
& MOD_MARKED
)
275 tty_setcolor (EDITOR_MARKED_COLOR
);
278 tty_setcolor (EDITOR_WHITESPACE_COLOR
);
280 else if (style
& MOD_BOLD
)
281 tty_setcolor (EDITOR_BOLD_COLOR
);
282 else if (style
& MOD_MARKED
)
283 tty_setcolor (EDITOR_MARKED_COLOR
);
285 tty_lowlevel_setcolor (color
);
287 if (show_right_margin
)
289 if (i
> option_word_wrap_line_length
+ edit
->start_col
)
290 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR
);
294 tty_print_anychar (textchar
);
298 /* --------------------------------------------------------------------------------------------- */
299 /** b is a pointer to the beginning of the line */
302 edit_draw_this_line (WEdit
* edit
, long b
, long row
, long start_col
, long end_col
)
304 struct line_s line
[MAX_LINE_LEN
];
305 struct line_s
*p
= line
;
307 long m1
= 0, m2
= 0, q
, c1
, c2
;
308 int col
, start_col_real
;
314 unsigned int cur_line
= 0;
316 char line_stat
[LINE_STATE_WIDTH
+ 1] = "\0";
318 if (row
> edit
->widget
.lines
- 1 - EDIT_TEXT_VERTICAL_OFFSET
)
321 if (book_mark_query_color (edit
, edit
->start_line
+ row
, BOOK_MARK_COLOR
))
322 book_mark
= BOOK_MARK_COLOR
;
323 else if (book_mark_query_color (edit
, edit
->start_line
+ row
, BOOK_MARK_FOUND_COLOR
))
324 book_mark
= BOOK_MARK_FOUND_COLOR
;
327 abn_style
= book_mark
<< 16;
329 abn_style
= MOD_ABNORMAL
;
331 end_col
-= EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
333 edit_get_syntax_color (edit
, b
- 1, &color
);
334 q
= edit_move_forward3 (edit
, b
, start_col
- edit
->start_col
, 0);
335 start_col_real
= (col
= (int) edit_move_forward3 (edit
, b
, 0, q
)) + edit
->start_col
;
337 if (option_line_state
)
339 cur_line
= edit
->start_line
+ row
;
340 if (cur_line
<= (unsigned int) edit
->total_lines
)
342 g_snprintf (line_stat
, LINE_STATE_WIDTH
+ 1, "%7i ", cur_line
+ 1);
346 memset (line_stat
, ' ', LINE_STATE_WIDTH
);
347 line_stat
[LINE_STATE_WIDTH
] = '\0';
349 if (book_mark_query_color (edit
, cur_line
, BOOK_MARK_COLOR
))
351 g_snprintf (line_stat
, 2, "*");
355 if (col
+ 16 > -edit
->start_col
)
357 eval_marks (edit
, &m1
, &m2
);
359 if (row
<= edit
->total_lines
- edit
->start_line
)
362 if (tty_use_colors () && visible_tws
)
364 tws
= edit_eol (edit
, b
);
365 while (tws
> b
&& ((c
= edit_get_byte (edit
, tws
- 1)) == ' ' || c
== '\t'))
369 while (col
<= end_col
- edit
->start_col
)
373 gboolean wide_width_char
= FALSE
;
374 gboolean control_char
= FALSE
;
378 if (q
== edit
->curs1
)
379 p
->style
|= MOD_CURSOR
;
380 if (q
>= m1
&& q
< m2
)
382 if (edit
->column_highlight
)
385 x
= edit_move_forward3 (edit
, b
, 0, q
);
386 c1
= min (edit
->column1
, edit
->column2
);
387 c2
= max (edit
->column1
, edit
->column2
);
388 if (x
>= c1
&& x
< c2
)
389 p
->style
|= MOD_MARKED
;
392 p
->style
|= MOD_MARKED
;
394 if (q
== edit
->bracket
)
395 p
->style
|= MOD_BOLD
;
396 if (q
>= edit
->found_start
&& q
< edit
->found_start
+ edit
->found_len
)
397 p
->style
|= MOD_BOLD
;
401 c
= edit_get_byte (edit
, q
);
405 c
= edit_get_utf (edit
, q
, &cw
);
407 /* we don't use bg for mc - fg contains both */
410 p
->style
|= book_mark
<< 16;
414 edit_get_syntax_color (edit
, q
, &color
);
415 p
->style
|= color
<< 16;
420 col
= (end_col
+ utf8lag
) - edit
->start_col
+ 1; /* quit */
423 i
= TAB_SIZE
- ((int) col
% TAB_SIZE
);
424 tab_over
= (end_col
- edit
->start_col
) - (col
+ i
- 1);
428 if (tty_use_colors () &&
429 ((visible_tabs
|| (visible_tws
&& q
>= tws
)) && enable_show_tabs_tws
))
431 if (p
->style
& MOD_MARKED
)
434 c
|= book_mark
<< 16;
436 c
= p
->style
| MOD_WHITESPACE
;
468 else if (tty_use_colors () && visible_tws
&& q
>= tws
&& enable_show_tabs_tws
)
471 p
->style
|= MOD_WHITESPACE
;
472 c
= p
->style
& ~MOD_CURSOR
;
484 c
= p
->style
& ~MOD_CURSOR
;
495 if (tty_use_colors () && visible_tws
&& q
>= tws
&& enable_show_tabs_tws
)
498 p
->style
|= MOD_WHITESPACE
;
506 if (mc_global
.utf8_display
)
510 c
= convert_from_8bit_to_utf_c ((unsigned char) c
, edit
->converter
);
514 if (g_unichar_iswide (c
))
516 wide_width_char
= TRUE
;
522 c
= convert_from_utf_to_current_c (c
, edit
->converter
);
525 c
= convert_to_display_c (c
);
527 /* Caret notation for control characters */
531 p
->style
= abn_style
;
534 p
->style
= abn_style
;
543 p
->style
= abn_style
;
546 p
->style
= abn_style
;
554 if ((mc_global
.utf8_display
&& g_unichar_isprint (c
)) ||
555 (!mc_global
.utf8_display
&& is_printable (c
)))
563 p
->style
= abn_style
;
569 if (g_unichar_isprint (c
))
577 p
->style
= abn_style
;
591 if (col
> (end_col
- edit
->start_col
+ 1))
609 start_col_real
= start_col
= 0;
614 print_to_widget (edit
, row
, start_col
, start_col_real
, end_col
, line
, line_stat
, book_mark
);
617 /* --------------------------------------------------------------------------------------------- */
620 edit_draw_this_char (WEdit
* edit
, long curs
, long row
, long start_column
, long end_column
)
622 int b
= edit_bol (edit
, curs
);
623 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
626 /* --------------------------------------------------------------------------------------------- */
627 /** 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
, long end_column
)
632 static long prev_curs_row
= 0;
633 static long prev_curs
= 0;
635 Widget
*w
= (Widget
*) edit
;
636 Dlg_head
*h
= w
->owner
;
638 long row
= 0, curs_row
;
639 int force
= edit
->force
;
646 /* draw only visible region */
648 last_line
= h
->y
+ h
->lines
- 1;
651 if (y1
> last_line
- 1 /* buttonbar */ )
654 last_column
= h
->x
+ h
->cols
- 1;
657 if (x1
> last_column
)
660 y2
= w
->y
+ w
->lines
- 1;
661 if (y2
< h
->y
+ 1 /* menubar */ )
664 x2
= w
->x
+ w
->cols
- 1;
668 if ((force
& REDRAW_IN_BOUNDS
) == 0)
670 /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
671 /* draw only visible region */
673 if (y2
<= last_line
- 1 /* buttonbar */ )
674 end_row
= w
->lines
- 1;
675 else if (y1
>= h
->y
+ 1 /* menubar */ )
676 end_row
= h
->lines
- 1 - y1
- 1;
678 end_row
= start_row
+ h
->lines
- 1 - 1;
680 if (x2
<= last_column
)
681 end_column
= w
->cols
- 1;
683 end_column
= h
->cols
- 1 - x1
;
685 end_column
= start_column
+ h
->cols
- 1;
689 * If the position of the page has not moved then we can draw the cursor
690 * character only. This will prevent line flicker when using arrow keys.
692 if ((force
& REDRAW_CHAR_ONLY
) == 0 || (force
& REDRAW_PAGE
) != 0)
694 if ((force
& REDRAW_PAGE
) != 0)
697 b
= edit_move_forward (edit
, edit
->start_display
, start_row
, 0);
698 while (row
<= end_row
)
700 if (key_pending (edit
))
702 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
703 b
= edit_move_forward (edit
, b
, 1, 0);
709 curs_row
= edit
->curs_row
;
711 if ((force
& REDRAW_BEFORE_CURSOR
) != 0 && start_row
< curs_row
)
713 long upto
= curs_row
- 1 <= end_row
? curs_row
- 1 : end_row
;
716 b
= edit
->start_display
;
719 if (key_pending (edit
))
721 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
722 b
= edit_move_forward (edit
, b
, 1, 0);
726 /* if (force & REDRAW_LINE) ---> default */
727 b
= edit_bol (edit
, edit
->curs1
);
728 if (curs_row
>= start_row
&& curs_row
<= end_row
)
730 if (key_pending (edit
))
732 edit_draw_this_line (edit
, b
, curs_row
, start_column
, end_column
);
735 if ((force
& REDRAW_AFTER_CURSOR
) != 0 && end_row
> curs_row
)
737 row
= curs_row
+ 1 < start_row
? start_row
: curs_row
+ 1;
738 b
= edit_move_forward (edit
, b
, 1, 0);
739 while (row
<= end_row
)
741 if (key_pending (edit
))
743 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
744 b
= edit_move_forward (edit
, b
, 1, 0);
749 if ((force
& REDRAW_LINE_ABOVE
) != 0 && curs_row
>= 1)
752 b
= edit_move_backward (edit
, edit_bol (edit
, edit
->curs1
), 1);
753 if (row
>= start_row
&& row
<= end_row
)
755 if (key_pending (edit
))
757 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
761 if ((force
& REDRAW_LINE_BELOW
) != 0 && row
< edit
->widget
.lines
- 1)
764 b
= edit_bol (edit
, edit
->curs1
);
765 b
= edit_move_forward (edit
, b
, 1, 0);
766 if (row
>= start_row
&& row
<= end_row
)
768 if (key_pending (edit
))
770 edit_draw_this_line (edit
, b
, row
, start_column
, end_column
);
775 else if (prev_curs_row
< edit
->curs_row
)
777 /* with the new text highlighting, we must draw from the top down */
778 edit_draw_this_char (edit
, prev_curs
, prev_curs_row
, start_column
, end_column
);
779 edit_draw_this_char (edit
, edit
->curs1
, edit
->curs_row
, start_column
, end_column
);
783 edit_draw_this_char (edit
, edit
->curs1
, edit
->curs_row
, start_column
, end_column
);
784 edit_draw_this_char (edit
, prev_curs
, prev_curs_row
, start_column
, end_column
);
789 prev_curs_row
= edit
->curs_row
;
790 prev_curs
= edit
->curs1
;
793 /* --------------------------------------------------------------------------------------------- */
796 edit_render (WEdit
* edit
, int page
, int row_start
, int col_start
, int row_end
, int col_end
)
798 if (page
) /* if it was an expose event, 'page' would be set */
799 edit
->force
|= REDRAW_PAGE
| REDRAW_IN_BOUNDS
;
801 if (edit
->force
& REDRAW_COMPLETELY
)
802 buttonbar_redraw (find_buttonbar (edit
->widget
.owner
));
803 render_edit_text (edit
, row_start
, col_start
, row_end
, col_end
);
805 * edit->force != 0 means a key was pending and the redraw
806 * was halted, so next time we must redraw everything in case stuff
807 * was left undrawn from a previous key press.
810 edit
->force
|= REDRAW_PAGE
;
813 /* --------------------------------------------------------------------------------------------- */
814 /*** public functions ****************************************************************************/
815 /* --------------------------------------------------------------------------------------------- */
817 /** Draw the status line at the top of the screen. The size of the filename
818 * field varies depending on the width of the screen and the length of
821 edit_status (WEdit
* edit
)
823 const int w
= edit
->widget
.owner
->cols
;
824 const size_t status_size
= w
+ 1;
825 char *const status
= g_malloc (status_size
);
827 const char *fname
= "";
829 const int gap
= 3; /* between the filename and the status */
830 const int right_gap
= 5; /* at the right end of the screen */
831 const int preferred_fname_len
= 16;
833 status_string (edit
, status
, status_size
);
834 status_len
= (int) str_term_width1 (status
);
836 if (edit
->filename_vpath
!= NULL
)
837 fname
= vfs_path_get_last_path_str (edit
->filename_vpath
);
839 fname_len
= str_term_width1 (fname
);
840 if (fname_len
< preferred_fname_len
)
841 fname_len
= preferred_fname_len
;
843 if (fname_len
+ gap
+ status_len
+ right_gap
>= w
)
845 if (preferred_fname_len
+ gap
+ status_len
+ right_gap
>= w
)
846 fname_len
= preferred_fname_len
;
848 fname_len
= w
- (gap
+ status_len
+ right_gap
);
849 fname
= str_trunc (fname
, fname_len
);
852 dlg_move (edit
->widget
.owner
, 0, 0);
853 tty_setcolor (STATUSBAR_COLOR
);
854 printwstr (fname
, fname_len
+ gap
);
855 printwstr (status
, w
- (fname_len
+ gap
));
857 if (simple_statusbar
&& w
> EDITOR_MINIMUM_TERMINAL_WIDTH
)
859 size_t percent
= 100;
861 if (edit
->total_lines
+ 1 != 0)
862 percent
= (edit
->curs_line
+ 1) * 100 / (edit
->total_lines
+ 1);
863 dlg_move (edit
->widget
.owner
, 0, w
- 5);
864 tty_printf (" %3d%%", percent
);
870 /* --------------------------------------------------------------------------------------------- */
871 /** this scrolls the text so that cursor is on the screen */
873 edit_scroll_screen_over_cursor (WEdit
* edit
)
877 int b_extreme
, t_extreme
, l_extreme
, r_extreme
;
879 if (edit
->widget
.lines
<= 0 || edit
->widget
.cols
<= 0)
882 edit
->widget
.cols
-= EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
883 edit
->widget
.lines
-= EDIT_TEXT_VERTICAL_OFFSET
;
885 r_extreme
= EDIT_RIGHT_EXTREME
;
886 l_extreme
= EDIT_LEFT_EXTREME
;
887 b_extreme
= EDIT_BOTTOM_EXTREME
;
888 t_extreme
= EDIT_TOP_EXTREME
;
891 b_extreme
= max (edit
->widget
.lines
/ 4, b_extreme
);
892 t_extreme
= max (edit
->widget
.lines
/ 4, t_extreme
);
894 if (b_extreme
+ t_extreme
+ 1 > edit
->widget
.lines
)
898 n
= b_extreme
+ t_extreme
;
901 b_extreme
= (b_extreme
* (edit
->widget
.lines
- 1)) / n
;
902 t_extreme
= (t_extreme
* (edit
->widget
.lines
- 1)) / n
;
904 if (l_extreme
+ r_extreme
+ 1 > edit
->widget
.cols
)
908 n
= l_extreme
+ t_extreme
;
911 l_extreme
= (l_extreme
* (edit
->widget
.cols
- 1)) / n
;
912 r_extreme
= (r_extreme
* (edit
->widget
.cols
- 1)) / n
;
914 p
= edit_get_col (edit
) + edit
->over_col
;
915 edit_update_curs_row (edit
);
916 outby
= p
+ edit
->start_col
- edit
->widget
.cols
+ 1 + (r_extreme
+ edit
->found_len
);
918 edit_scroll_right (edit
, outby
);
919 outby
= l_extreme
- p
- edit
->start_col
;
921 edit_scroll_left (edit
, outby
);
923 outby
= p
- edit
->widget
.lines
+ 1 + b_extreme
;
925 edit_scroll_downward (edit
, outby
);
926 outby
= t_extreme
- p
;
928 edit_scroll_upward (edit
, outby
);
929 edit_update_curs_row (edit
);
931 edit
->widget
.lines
+= EDIT_TEXT_VERTICAL_OFFSET
;
932 edit
->widget
.cols
+= EDIT_TEXT_HORIZONTAL_OFFSET
+ option_line_state_width
;
935 /* --------------------------------------------------------------------------------------------- */
938 edit_render_keypress (WEdit
* edit
)
940 edit_render (edit
, 0, 0, 0, 0, 0);
943 /* --------------------------------------------------------------------------------------------- */