* doc/es/mc.1.in: Cleanup. Fix key naming.
[midnight-commander.git] / edit / editdraw.c
blobb8d9c38f77d037a893626f7bf808ed01d8fd9b19
1 /* editor text drawing.
3 Copyright (C) 1996, 1997 the Free Software Foundation
5 Authors: 1996, 1997 Paul Sheer
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.
23 #include <config.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <sys/types.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31 #include <string.h>
32 #include <ctype.h>
33 #include <errno.h>
34 #include <sys/stat.h>
36 #include "../src/global.h"
38 #include "edit.h"
39 #include "edit-widget.h"
41 #define MAX_LINE_LEN 1024
43 #include "../src/color.h" /* EDITOR_NORMAL_COLOR */
44 #include "../src/tty.h" /* attrset() */
45 #include "../src/widget.h" /* buttonbar_redraw() */
46 #include "../src/key.h" /* is_idle() */
47 #include "../src/charsets.h"
49 /* Text styles */
50 #define MOD_ABNORMAL (1 << 8)
51 #define MOD_BOLD (1 << 9)
52 #define MOD_MARKED (1 << 10)
53 #define MOD_CURSOR (1 << 11)
55 #define FONT_OFFSET_X 0
56 #define FONT_OFFSET_Y 0
57 #define FIXED_FONT 1
58 #define FONT_PIX_PER_LINE 1
59 #define FONT_MEAN_WIDTH 1
62 static void status_string (WEdit * edit, char *s, int w)
64 char byte_str[16];
67 * If we are at the end of file, print <EOF>,
68 * otherwise print the current character as is (if printable),
69 * as decimal and as hex.
71 if (edit->curs1 < edit->last_byte) {
72 unsigned char cur_byte = edit_get_byte (edit, edit->curs1);
73 g_snprintf (byte_str, sizeof (byte_str), "%c %3d 0x%02X",
74 is_printable (cur_byte) ? cur_byte : '.',
75 (int) cur_byte,
76 (unsigned) cur_byte);
77 } else {
78 strcpy (byte_str, "<EOF>");
81 /* The field lengths just prevent the status line from shortening too much */
82 g_snprintf (s, w,
83 "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb)= %s",
84 edit->mark1 != edit->mark2 ? ( column_highlighting ? 'C' : 'B') : '-',
85 edit->modified ? 'M' : '-',
86 edit->macro_i < 0 ? '-' : 'R',
87 edit->overwrite == 0 ? '-' : 'O',
88 edit->curs_col,
90 edit->start_line + 1,
91 edit->curs_row,
92 edit->curs_line + 1,
93 edit->total_lines + 1,
95 edit->curs1,
96 edit->last_byte,
97 byte_str);
100 static inline void
101 printwstr (const char *s, int len)
103 if (len > 0)
104 tty_printf ("%-*.*s", len, len, s);
107 /* Draw the status line at the top of the widget. The size of the filename
108 * field varies depending on the width of the screen and the length of
109 * the filename. */
110 void
111 edit_status (WEdit *edit)
113 const int w = edit->widget.cols;
114 const size_t status_size = w + 1;
115 char * const status = g_malloc (status_size);
116 int status_len;
117 const char *fname = "";
118 int fname_len;
119 const int gap = 3; /* between the filename and the status */
120 const int right_gap = 2; /* at the right end of the screen */
121 const int preferred_fname_len = 16;
123 status_string (edit, status, status_size);
124 status_len = (int) strlen (status);
126 if (edit->filename)
127 fname = edit->filename;
128 fname_len = strlen(fname);
129 if (fname_len < preferred_fname_len)
130 fname_len = preferred_fname_len;
132 if (fname_len + gap + status_len + right_gap >= w) {
133 if (preferred_fname_len + gap + status_len + right_gap >= w)
134 fname_len = preferred_fname_len;
135 else
136 fname_len = w - (gap + status_len + right_gap);
137 fname = name_trunc (fname, fname_len);
140 widget_move (edit, 0, 0);
141 attrset (SELECTED_COLOR);
142 printwstr (fname, fname_len + gap);
143 printwstr (status, w - (fname_len + gap));
144 attrset (EDITOR_NORMAL_COLOR);
146 g_free (status);
149 /* this scrolls the text so that cursor is on the screen */
150 void edit_scroll_screen_over_cursor (WEdit * edit)
152 int p;
153 int outby;
154 int b_extreme, t_extreme, l_extreme, r_extreme;
156 if (edit->num_widget_lines <= 0 || edit->num_widget_columns <= 0)
157 return;
159 r_extreme = EDIT_RIGHT_EXTREME;
160 l_extreme = EDIT_LEFT_EXTREME;
161 b_extreme = EDIT_BOTTOM_EXTREME;
162 t_extreme = EDIT_TOP_EXTREME;
163 if (edit->found_len) {
164 b_extreme = max (edit->num_widget_lines / 4, b_extreme);
165 t_extreme = max (edit->num_widget_lines / 4, t_extreme);
167 if (b_extreme + t_extreme + 1 > edit->num_widget_lines) {
168 int n;
169 n = b_extreme + t_extreme;
170 b_extreme = (b_extreme * (edit->num_widget_lines - 1)) / n;
171 t_extreme = (t_extreme * (edit->num_widget_lines - 1)) / n;
173 if (l_extreme + r_extreme + 1 > edit->num_widget_columns) {
174 int n;
175 n = l_extreme + t_extreme;
176 l_extreme = (l_extreme * (edit->num_widget_columns - 1)) / n;
177 r_extreme = (r_extreme * (edit->num_widget_columns - 1)) / n;
179 p = edit_get_col (edit);
180 edit_update_curs_row (edit);
181 outby = p + edit->start_col - edit->num_widget_columns + 1 + (r_extreme + edit->found_len);
182 if (outby > 0)
183 edit_scroll_right (edit, outby);
184 outby = l_extreme - p - edit->start_col;
185 if (outby > 0)
186 edit_scroll_left (edit, outby);
187 p = edit->curs_row;
188 outby = p - edit->num_widget_lines + 1 + b_extreme;
189 if (outby > 0)
190 edit_scroll_downward (edit, outby);
191 outby = t_extreme - p;
192 if (outby > 0)
193 edit_scroll_upward (edit, outby);
194 edit_update_curs_row (edit);
197 #define set_color(font) attrset (font)
199 #define edit_move(x,y) widget_move(edit, y, x);
201 /* Set colorpair by index, don't interpret S-Lang "emulated attributes" */
202 #ifdef HAVE_SLANG
203 #define lowlevel_set_color(x) SLsmg_set_color(x & 0x7F)
204 #else
205 #define lowlevel_set_color(x) attrset(MY_COLOR_PAIR(color))
206 #endif
208 static void
209 print_to_widget (WEdit *edit, long row, int start_col, int start_col_real,
210 long end_col, unsigned int line[])
212 unsigned int *p;
214 int x = start_col_real + EDIT_TEXT_HORIZONTAL_OFFSET;
215 int x1 = start_col + EDIT_TEXT_HORIZONTAL_OFFSET;
216 int y = row + EDIT_TEXT_VERTICAL_OFFSET;
217 int cols_to_skip = abs (x);
219 set_color (EDITOR_NORMAL_COLOR);
220 edit_move (x1, y);
221 hline (' ', end_col + 1 - EDIT_TEXT_HORIZONTAL_OFFSET - x1);
223 edit_move (x1 + FONT_OFFSET_X, y + FONT_OFFSET_Y);
224 p = line;
226 while (*p) {
227 int style;
228 int textchar;
229 int color;
231 if (cols_to_skip) {
232 p++;
233 cols_to_skip--;
234 continue;
237 style = *p & 0xFF00;
238 textchar = *p & 0xFF;
239 color = *p >> 16;
241 if (style & MOD_ABNORMAL) {
242 /* Non-printable - use black background */
243 color = 0;
246 if (style & MOD_BOLD) {
247 set_color (EDITOR_BOLD_COLOR);
248 } else if (style & MOD_MARKED) {
249 set_color (EDITOR_MARKED_COLOR);
250 } else {
251 lowlevel_set_color (color);
254 addch (textchar);
255 p++;
259 /* b is a pointer to the beginning of the line */
260 static void
261 edit_draw_this_line (WEdit *edit, long b, long row, long start_col,
262 long end_col)
264 static unsigned int line[MAX_LINE_LEN];
265 unsigned int *p = line;
266 long m1 = 0, m2 = 0, q, c1, c2;
267 int col, start_col_real;
268 unsigned int c;
269 int color;
270 int i, book_mark = -1;
272 #if 0
273 if (!book_mark_query (edit, edit->start_line + row, &book_mark))
274 book_mark = -1;
275 #endif
277 edit_get_syntax_color (edit, b - 1, &color);
278 q = edit_move_forward3 (edit, b, start_col - edit->start_col, 0);
279 start_col_real = (col =
280 (int) edit_move_forward3 (edit, b, 0,
281 q)) + edit->start_col;
282 c1 = min (edit->column1, edit->column2);
283 c2 = max (edit->column1, edit->column2);
285 if (col + 16 > -edit->start_col) {
286 eval_marks (edit, &m1, &m2);
288 if (row <= edit->total_lines - edit->start_line) {
289 while (col <= end_col - edit->start_col) {
290 *p = 0;
291 if (q == edit->curs1)
292 *p |= MOD_CURSOR;
293 if (q >= m1 && q < m2) {
294 if (column_highlighting) {
295 int x;
296 x = edit_move_forward3 (edit, b, 0, q);
297 if (x >= c1 && x < c2)
298 *p |= MOD_MARKED;
299 } else
300 *p |= MOD_MARKED;
302 if (q == edit->bracket)
303 *p |= MOD_BOLD;
304 if (q >= edit->found_start
305 && q < edit->found_start + edit->found_len)
306 *p |= MOD_BOLD;
307 c = edit_get_byte (edit, q);
308 /* we don't use bg for mc - fg contains both */
309 if (book_mark == -1) {
310 edit_get_syntax_color (edit, q, &color);
311 *p |= color << 16;
312 } else {
313 *p |= book_mark << 16;
315 q++;
316 switch (c) {
317 case '\n':
318 col = end_col - edit->start_col + 1; /* quit */
319 *(p++) |= ' ';
320 break;
321 case '\t':
322 i = TAB_SIZE - ((int) col % TAB_SIZE);
323 *p |= ' ';
324 c = *(p++) & ~MOD_CURSOR;
325 col += i;
326 while (--i)
327 *(p++) = c;
328 break;
329 default:
330 c = convert_to_display_c (c);
332 /* Caret notation for control characters */
333 if (c < 32) {
334 *(p++) = '^' | MOD_ABNORMAL;
335 *(p++) = (c + 0x40) | MOD_ABNORMAL;
336 col += 2;
337 break;
339 if (c == 127) {
340 *(p++) = '^' | MOD_ABNORMAL;
341 *(p++) = '?' | MOD_ABNORMAL;
342 col += 2;
343 break;
346 if (is_printable (c)) {
347 *(p++) |= c;
348 } else {
349 *(p++) = '.' | MOD_ABNORMAL;
351 col++;
352 break;
356 } else {
357 start_col_real = start_col = 0;
359 *p = 0;
361 print_to_widget (edit, row, start_col, start_col_real, end_col, line);
364 #define key_pending(x) (!is_idle())
366 static void edit_draw_this_char (WEdit * edit, long curs, long row)
368 int b = edit_bol (edit, curs);
369 edit_draw_this_line (edit, b, row, 0, edit->num_widget_columns - 1);
372 /* cursor must be in screen for other than REDRAW_PAGE passed in force */
373 static void
374 render_edit_text (WEdit * edit, long start_row, long start_column, long end_row,
375 long end_column)
377 long row = 0, curs_row;
378 static int prev_curs_row = 0;
379 static long prev_curs = 0;
381 int force = edit->force;
382 long b;
385 * If the position of the page has not moved then we can draw the cursor
386 * character only. This will prevent line flicker when using arrow keys.
388 if ((!(force & REDRAW_CHAR_ONLY)) || (force & REDRAW_PAGE)) {
389 if (!(force & REDRAW_IN_BOUNDS)) { /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
390 start_row = 0;
391 end_row = edit->num_widget_lines - 1;
392 start_column = 0;
393 end_column = edit->num_widget_columns - 1;
395 if (force & REDRAW_PAGE) {
396 row = start_row;
397 b = edit_move_forward (edit, edit->start_display, start_row, 0);
398 while (row <= end_row) {
399 if (key_pending (edit))
400 goto exit_render;
401 edit_draw_this_line (edit, b, row, start_column, end_column);
402 b = edit_move_forward (edit, b, 1, 0);
403 row++;
405 } else {
406 curs_row = edit->curs_row;
408 if (force & REDRAW_BEFORE_CURSOR) {
409 if (start_row < curs_row) {
410 long upto = curs_row - 1 <= end_row ? curs_row - 1 : end_row;
411 row = start_row;
412 b = edit->start_display;
413 while (row <= upto) {
414 if (key_pending (edit))
415 goto exit_render;
416 edit_draw_this_line (edit, b, row, start_column, end_column);
417 b = edit_move_forward (edit, b, 1, 0);
421 /* if (force & REDRAW_LINE) ---> default */
422 b = edit_bol (edit, edit->curs1);
423 if (curs_row >= start_row && curs_row <= end_row) {
424 if (key_pending (edit))
425 goto exit_render;
426 edit_draw_this_line (edit, b, curs_row, start_column, end_column);
428 if (force & REDRAW_AFTER_CURSOR) {
429 if (end_row > curs_row) {
430 row = curs_row + 1 < start_row ? start_row : curs_row + 1;
431 b = edit_move_forward (edit, b, 1, 0);
432 while (row <= end_row) {
433 if (key_pending (edit))
434 goto exit_render;
435 edit_draw_this_line (edit, b, row, start_column, end_column);
436 b = edit_move_forward (edit, b, 1, 0);
437 row++;
441 if (force & REDRAW_LINE_ABOVE && curs_row >= 1) {
442 row = curs_row - 1;
443 b = edit_move_backward (edit, edit_bol (edit, edit->curs1), 1);
444 if (row >= start_row && row <= end_row) {
445 if (key_pending (edit))
446 goto exit_render;
447 edit_draw_this_line (edit, b, row, start_column, end_column);
450 if (force & REDRAW_LINE_BELOW && row < edit->num_widget_lines - 1) {
451 row = curs_row + 1;
452 b = edit_bol (edit, edit->curs1);
453 b = edit_move_forward (edit, b, 1, 0);
454 if (row >= start_row && row <= end_row) {
455 if (key_pending (edit))
456 goto exit_render;
457 edit_draw_this_line (edit, b, row, start_column, end_column);
461 } else {
462 if (prev_curs_row < edit->curs_row) { /* with the new text highlighting, we must draw from the top down */
463 edit_draw_this_char (edit, prev_curs, prev_curs_row);
464 edit_draw_this_char (edit, edit->curs1, edit->curs_row);
465 } else {
466 edit_draw_this_char (edit, edit->curs1, edit->curs_row);
467 edit_draw_this_char (edit, prev_curs, prev_curs_row);
471 edit->force = 0;
473 prev_curs_row = edit->curs_row;
474 prev_curs = edit->curs1;
476 exit_render:
477 edit->screen_modified = 0;
478 return;
481 static void
482 edit_render (WEdit * edit, int page, int row_start, int col_start, int row_end, int col_end)
484 if (page) /* if it was an expose event, 'page' would be set */
485 edit->force |= REDRAW_PAGE | REDRAW_IN_BOUNDS;
487 if (edit->force & REDRAW_COMPLETELY)
488 buttonbar_redraw (edit->widget.parent);
489 render_edit_text (edit, row_start, col_start, row_end, col_end);
491 * edit->force != 0 means a key was pending and the redraw
492 * was halted, so next time we must redraw everything in case stuff
493 * was left undrawn from a previous key press.
495 if (edit->force)
496 edit->force |= REDRAW_PAGE;
499 void edit_render_keypress (WEdit * edit)
501 edit_render (edit, 0, 0, 0, 0, 0);