First bunch of mhl_mem_free removal patches
[midnight-commander.git] / edit / editdraw.c
blobb19139163bff42388eedb815749143517f2efcc9
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 #include <config.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <sys/types.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <string.h>
33 #include <ctype.h>
34 #include <errno.h>
35 #include <sys/stat.h>
37 #include "../src/global.h"
39 #include "edit.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"
50 /* Text styles */
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
59 #define FIXED_FONT 1
60 #define FONT_PIX_PER_LINE 1
61 #define FONT_MEAN_WIDTH 1
64 static void status_string (WEdit * edit, char *s, int w)
66 char byte_str[16];
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 : '.',
77 (int) cur_byte,
78 (unsigned) cur_byte);
79 } else {
80 strcpy (byte_str, "<EOF>");
83 /* The field lengths just prevent the status line from shortening too much */
84 snprintf (s, w,
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',
90 edit->curs_col,
92 edit->start_line + 1,
93 edit->curs_row,
94 edit->curs_line + 1,
95 edit->total_lines + 1,
97 edit->curs1,
98 edit->last_byte,
99 byte_str);
102 static inline void
103 printwstr (const char *s, int len)
105 if (len > 0)
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
111 * the filename. */
112 void
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);
118 int status_len;
119 const char *fname = "";
120 int fname_len;
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);
128 if (edit->filename)
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;
137 else
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);
148 g_free (status);
151 /* this scrolls the text so that cursor is on the screen */
152 void edit_scroll_screen_over_cursor (WEdit * edit)
154 int p;
155 int outby;
156 int b_extreme, t_extreme, l_extreme, r_extreme;
158 if (edit->num_widget_lines <= 0 || edit->num_widget_columns <= 0)
159 return;
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) {
170 int n;
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) {
176 int n;
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);
184 if (outby > 0)
185 edit_scroll_right (edit, outby);
186 outby = l_extreme - p - edit->start_col;
187 if (outby > 0)
188 edit_scroll_left (edit, outby);
189 p = edit->curs_row;
190 outby = p - edit->num_widget_lines + 1 + b_extreme;
191 if (outby > 0)
192 edit_scroll_downward (edit, outby);
193 outby = t_extreme - p;
194 if (outby > 0)
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" */
204 #ifdef HAVE_SLANG
205 #define lowlevel_set_color(x) SLsmg_set_color(x & 0x7F)
206 #else
207 #define lowlevel_set_color(x) attrset(MY_COLOR_PAIR(color))
208 #endif
210 static void
211 print_to_widget (WEdit *edit, long row, int start_col, int start_col_real,
212 long end_col, unsigned int line[])
214 unsigned int *p;
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);
222 edit_move (x1, y);
223 hline (' ', end_col + 1 - EDIT_TEXT_HORIZONTAL_OFFSET - x1);
225 edit_move (x1 + FONT_OFFSET_X, y + FONT_OFFSET_Y);
226 p = line;
228 while (*p) {
229 int style;
230 int textchar;
231 int color;
233 if (cols_to_skip) {
234 p++;
235 cols_to_skip--;
236 continue;
239 style = *p & 0xFF00;
240 textchar = *p & 0xFF;
241 color = *p >> 16;
243 if (style & MOD_ABNORMAL) {
244 /* Non-printable - use black background */
245 color = 0;
248 if (style & MOD_WHITESPACE) {
249 if (style & MOD_MARKED) {
250 textchar = ' ';
251 set_color (EDITOR_MARKED_COLOR);
252 } else {
253 #if 0
254 if (color != EDITOR_NORMAL_COLOR) {
255 textchar = ' ';
256 lowlevel_set_color (color);
257 } else
258 #endif
259 set_color (EDITOR_WHITESPACE_COLOR);
261 } else {
262 if (style & MOD_BOLD) {
263 set_color (EDITOR_BOLD_COLOR);
264 } else if (style & MOD_MARKED) {
265 set_color (EDITOR_MARKED_COLOR);
266 } else {
267 lowlevel_set_color (color);
271 addch (textchar);
272 p++;
276 int visible_tabs = 1, visible_tws = 1;
278 /* b is a pointer to the beginning of the line */
279 static void
280 edit_draw_this_line (WEdit *edit, long b, long row, long start_col,
281 long end_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;
287 unsigned int c;
288 int color;
289 int i;
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) {
303 long tws;
304 if (use_colors && visible_tws) {
305 tws = edit_eol (edit, b);
306 while (tws > b && ((c = edit_get_byte (edit, tws - 1)) == ' '
307 || c == '\t'))
308 tws--;
311 while (col <= end_col - edit->start_col) {
312 *p = 0;
313 if (q == edit->curs1)
314 *p |= MOD_CURSOR;
315 if (q >= m1 && q < m2) {
316 if (column_highlighting) {
317 int x;
318 x = edit_move_forward3 (edit, b, 0, q);
319 if (x >= c1 && x < c2)
320 *p |= MOD_MARKED;
321 } else
322 *p |= MOD_MARKED;
324 if (q == edit->bracket)
325 *p |= MOD_BOLD;
326 if (q >= edit->found_start
327 && q < edit->found_start + edit->found_len)
328 *p |= MOD_BOLD;
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);
332 *p |= color << 16;
333 switch (c) {
334 case '\n':
335 col = end_col - edit->start_col + 1; /* quit */
336 *(p++) |= ' ';
337 break;
338 case '\t':
339 i = TAB_SIZE - ((int) col % TAB_SIZE);
340 col += i;
341 if (use_colors && visible_tabs) {
342 c = (*p & ~MOD_CURSOR) | MOD_WHITESPACE;
343 if (i > 2) {
344 *(p++) |= '<' | MOD_WHITESPACE;
345 while (--i > 1)
346 *(p++) = c | '-';
347 *(p++) = c | '>';
348 } else if (i > 1) {
349 *(p++) |= '<' | MOD_WHITESPACE;
350 *(p++) = c | '>';
351 } else
352 *(p++) |= '>' | MOD_WHITESPACE;
353 } else if (use_colors && visible_tws && q >= tws) {
354 *p |= '.' | MOD_WHITESPACE;
355 c = *(p++) & ~MOD_CURSOR;
356 while (--i)
357 *(p++) = c;
358 } else {
359 *p |= ' ';
360 c = *(p++) & ~MOD_CURSOR;
361 while (--i)
362 *(p++) = c;
364 break;
365 case ' ':
366 if (use_colors && visible_tws && q >= tws) {
367 *(p++) |= '.' | MOD_WHITESPACE;
368 col++;
369 break;
371 /* fallthrough */
372 default:
373 c = convert_to_display_c (c);
375 /* Caret notation for control characters */
376 if (c < 32) {
377 *(p++) = '^' | MOD_ABNORMAL;
378 *(p++) = (c + 0x40) | MOD_ABNORMAL;
379 col += 2;
380 break;
382 if (c == 127) {
383 *(p++) = '^' | MOD_ABNORMAL;
384 *(p++) = '?' | MOD_ABNORMAL;
385 col += 2;
386 break;
389 if (is_printable (c)) {
390 *(p++) |= c;
391 } else {
392 *(p++) = '.' | MOD_ABNORMAL;
394 col++;
395 break;
397 q++;
400 } else {
401 start_col_real = start_col = 0;
403 *p = 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 */
417 static void
418 render_edit_text (WEdit * edit, long start_row, long start_column, long end_row,
419 long end_column)
421 long row = 0, curs_row;
422 static int prev_curs_row = 0;
423 static long prev_curs = 0;
425 int force = edit->force;
426 long b;
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 */
434 start_row = 0;
435 end_row = edit->num_widget_lines - 1;
436 start_column = 0;
437 end_column = edit->num_widget_columns - 1;
439 if (force & REDRAW_PAGE) {
440 row = start_row;
441 b = edit_move_forward (edit, edit->start_display, start_row, 0);
442 while (row <= end_row) {
443 if (key_pending (edit))
444 goto exit_render;
445 edit_draw_this_line (edit, b, row, start_column, end_column);
446 b = edit_move_forward (edit, b, 1, 0);
447 row++;
449 } else {
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;
455 row = start_row;
456 b = edit->start_display;
457 while (row <= upto) {
458 if (key_pending (edit))
459 goto exit_render;
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))
469 goto exit_render;
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))
478 goto exit_render;
479 edit_draw_this_line (edit, b, row, start_column, end_column);
480 b = edit_move_forward (edit, b, 1, 0);
481 row++;
485 if (force & REDRAW_LINE_ABOVE && curs_row >= 1) {
486 row = 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))
490 goto exit_render;
491 edit_draw_this_line (edit, b, row, start_column, end_column);
494 if (force & REDRAW_LINE_BELOW && row < edit->num_widget_lines - 1) {
495 row = curs_row + 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))
500 goto exit_render;
501 edit_draw_this_line (edit, b, row, start_column, end_column);
505 } else {
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);
509 } else {
510 edit_draw_this_char (edit, edit->curs1, edit->curs_row);
511 edit_draw_this_char (edit, prev_curs, prev_curs_row);
515 edit->force = 0;
517 prev_curs_row = edit->curs_row;
518 prev_curs = edit->curs1;
520 exit_render:
521 edit->screen_modified = 0;
522 return;
525 static void
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.
539 if (edit->force)
540 edit->force |= REDRAW_PAGE;
543 void edit_render_keypress (WEdit * edit)
545 edit_render (edit, 0, 0, 0, 0, 0);