Manual pages: mcmcdiffmceditmcview

root/src/editor/editdraw.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. printwstr
  2. format_character_code
  3. status_string
  4. edit_status_fullscreen
  5. edit_status_window
  6. edit_draw_frame
  7. edit_draw_window_icons
  8. print_to_widget
  9. edit_draw_this_line
  10. edit_draw_this_char
  11. render_edit_text
  12. edit_render
  13. edit_status
  14. edit_scroll_screen_over_cursor
  15. edit_render_keypress

   1 /*
   2    Editor text drawing.
   3 
   4    Copyright (C) 1996-2026
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Paul Sheer, 1996, 1997
   9    Andrew Borodin <aborodin@vmail.ru> 2012-2022
  10    Slava Zanko <slavazanko@gmail.com>, 2013
  11 
  12    This file is part of the Midnight Commander.
  13 
  14    The Midnight Commander is free software: you can redistribute it
  15    and/or modify it under the terms of the GNU General Public License as
  16    published by the Free Software Foundation, either version 3 of the License,
  17    or (at your option) any later version.
  18 
  19    The Midnight Commander is distributed in the hope that it will be useful,
  20    but WITHOUT ANY WARRANTY; without even the implied warranty of
  21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22    GNU General Public License for more details.
  23 
  24    You should have received a copy of the GNU General Public License
  25    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  26  */
  27 
  28 /** \file
  29  *  \brief Source: editor text drawing
  30  *  \author Paul Sheer
  31  *  \date 1996, 1997
  32  */
  33 
  34 #include <config.h>
  35 #include <stdlib.h>
  36 #include <stdio.h>
  37 #include <stdarg.h>
  38 #include <sys/types.h>
  39 #include <unistd.h>
  40 #include <string.h>
  41 #include <ctype.h>
  42 #include <sys/stat.h>
  43 
  44 #include "lib/global.h"
  45 #include "lib/tty/tty.h"  // tty_printf()
  46 #include "lib/tty/key.h"  // is_idle()
  47 #include "lib/skin.h"
  48 #include "lib/strutil.h"  // utf string functions
  49 #include "lib/util.h"     // is_printable()
  50 #include "lib/widget.h"
  51 #include "lib/charsets.h"
  52 
  53 #include "edit-impl.h"
  54 #include "editwidget.h"
  55 
  56 /*** global variables ****************************************************************************/
  57 
  58 /*** file scope macro definitions ****************************************************************/
  59 
  60 #define MAX_LINE_LEN 1024
  61 
  62 /* Text styles */
  63 #define MOD_ABNORMAL                  (1 << 8)
  64 #define MOD_BOLD                      (1 << 9)
  65 #define MOD_MARKED                    (1 << 10)
  66 #define MOD_CURSOR                    (1 << 11)
  67 #define MOD_WHITESPACE                (1 << 12)
  68 
  69 #define edit_move(x, y)               widget_gotoyx (edit, y, x);
  70 
  71 #define key_pending(x)                (!is_idle ())
  72 
  73 #define EDITOR_MINIMUM_TERMINAL_WIDTH 30
  74 
  75 /*** file scope type declarations ****************************************************************/
  76 
  77 typedef struct
  78 {
  79     unsigned int ch;
  80     unsigned int style;
  81 } line_s;
  82 
  83 /*** forward declarations (file scope functions) *************************************************/
  84 
  85 /*** file scope variables ************************************************************************/
  86 
  87 /*** file scope functions ************************************************************************/
  88 
  89 static inline void
  90 printwstr (const char *s, int len)
     /* [previous][next][first][last][top][bottom][index][help]  */
  91 {
  92     if (len > 0)
  93         tty_printf ("%-*.*s", len, len, s);
  94 }
  95 
  96 /* --------------------------------------------------------------------------------------------- */
  97 
  98 #define CHAR_CODE_BUF_SIZE BUF_TINY
  99 
 100 static char *
 101 format_character_code (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 102 {
 103     char *buf;
 104 
 105     buf = g_malloc (CHAR_CODE_BUF_SIZE);
 106 
 107     if (edit->buffer.curs1 >= edit->buffer.size)
 108         strncpy (buf, "<EOF>   ", CHAR_CODE_BUF_SIZE);
 109     else if (edit->utf8)
 110     {
 111         unsigned int cur_utf;
 112         int char_length = 0;
 113 
 114         cur_utf = edit_buffer_get_utf (&edit->buffer, edit->buffer.curs1, &char_length);
 115         if (char_length > 0)
 116             if (cur_utf < 128)
 117                 g_snprintf (buf, CHAR_CODE_BUF_SIZE, "%3u 0x%02X", cur_utf, cur_utf);
 118             else
 119             {
 120                 char uplus[CHAR_CODE_BUF_SIZE];
 121 
 122                 g_snprintf (uplus, CHAR_CODE_BUF_SIZE, "U+%04X", cur_utf);
 123                 g_snprintf (buf, CHAR_CODE_BUF_SIZE, "%8s", uplus);
 124             }
 125         else
 126         {
 127             // cursor is over some invalid UTF-8 fragment
 128             cur_utf = edit_buffer_get_current_byte (&edit->buffer);
 129             g_snprintf (buf, CHAR_CODE_BUF_SIZE, "%3u 0x%02X", cur_utf, cur_utf);
 130         }
 131     }
 132     else
 133     {
 134         unsigned int cur_byte;
 135 
 136         cur_byte = edit_buffer_get_current_byte (&edit->buffer);
 137         g_snprintf (buf, CHAR_CODE_BUF_SIZE, "%3u 0x%02X", cur_byte, cur_byte);
 138     }
 139 
 140     return buf;
 141 }
 142 
 143 #undef CHAR_CODE_BUF_SIZE
 144 
 145 /* --------------------------------------------------------------------------------------------- */
 146 
 147 static inline void
 148 status_string (WEdit *edit, char *s, int w)
     /* [previous][next][first][last][top][bottom][index][help]  */
 149 {
 150     char *character_code;
 151 
 152     character_code = format_character_code (edit);
 153 
 154     // The field lengths just prevent the status line from shortening too much
 155     if (edit_options.simple_statusbar)
 156         g_snprintf (s, w, "%c%c%c%c %3ld %5ld/%ld %6ld/%ld [%s] %s",
 157                     edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',  //
 158                     edit->modified != 0 ? 'M' : '-',                                          //
 159                     macro_index < 0 ? '-' : 'R',                                              //
 160                     edit->overwrite == 0 ? '-' : 'O',                                         //
 161                     edit->curs_col + edit->over_col,                                          //
 162                     edit->buffer.curs_line + 1,                                               //
 163                     edit->buffer.lines + 1,                                                   //
 164                     (long) edit->buffer.curs1,                                                //
 165                     (long) edit->buffer.size,                                                 //
 166                     character_code,
 167                     mc_global.source_codepage >= 0 ? get_codepage_id (mc_global.source_codepage)
 168                                                    : "");
 169     else
 170         g_snprintf (s, w, "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb) [%s]  %s",
 171                     edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',  //
 172                     edit->modified != 0 ? 'M' : '-',                                          //
 173                     macro_index < 0 ? '-' : 'R',                                              //
 174                     edit->overwrite == 0 ? '-' : 'O',                                         //
 175                     edit->curs_col + edit->over_col,                                          //
 176                     edit->start_line + 1,                                                     //
 177                     edit->curs_row,                                                           //
 178                     edit->buffer.curs_line + 1,                                               //
 179                     edit->buffer.lines + 1,                                                   //
 180                     (long) edit->buffer.curs1,                                                //
 181                     (long) edit->buffer.size,                                                 //
 182                     character_code,
 183                     mc_global.source_codepage >= 0 ? get_codepage_id (mc_global.source_codepage)
 184                                                    : "");
 185 
 186     g_free (character_code);
 187 }
 188 
 189 /* --------------------------------------------------------------------------------------------- */
 190 /**
 191  * Draw the status line at the top of the screen for fullscreen editor window.
 192  *
 193  * @param edit  editor object
 194  * @param color color pair
 195  */
 196 
 197 static inline void
 198 edit_status_fullscreen (WEdit *edit, int color)
     /* [previous][next][first][last][top][bottom][index][help]  */
 199 {
 200     Widget *h = WIDGET (WIDGET (edit)->owner);
 201     const int w = h->rect.cols;
 202     const int gap = 3;        // between the filename and the status
 203     const int right_gap = 5;  // at the right end of the screen
 204     const int preferred_fname_len = 16;
 205     char *status;
 206     size_t status_size;
 207     int status_len;
 208     const char *fname = "";
 209     int fname_len;
 210 
 211     status_size = w + 1;
 212     status = g_malloc (status_size);
 213     status_string (edit, status, status_size);
 214     status_len = (int) str_term_width1 (status);
 215 
 216     if (edit->filename_vpath != NULL)
 217     {
 218         fname = vfs_path_get_last_path_str (edit->filename_vpath);
 219 
 220         if (!edit_options.state_full_filename)
 221             fname = x_basename (fname);
 222     }
 223 
 224     fname_len = str_term_width1 (fname);
 225     if (fname_len < preferred_fname_len)
 226         fname_len = preferred_fname_len;
 227 
 228     if (fname_len + gap + status_len + right_gap >= w)
 229     {
 230         if (preferred_fname_len + gap + status_len + right_gap >= w)
 231             fname_len = preferred_fname_len;
 232         else
 233             fname_len = w - (gap + status_len + right_gap);
 234         fname = str_trunc (fname, fname_len);
 235     }
 236 
 237     widget_gotoyx (h, 0, 0);
 238     tty_setcolor (color);
 239     printwstr (fname, fname_len + gap);
 240     printwstr (status, w - (fname_len + gap));
 241 
 242     if (edit_options.simple_statusbar && w > EDITOR_MINIMUM_TERMINAL_WIDTH)
 243     {
 244         int percent;
 245 
 246         percent = edit_buffer_calc_percent (&edit->buffer, edit->buffer.curs1);
 247         widget_gotoyx (h, 0, w - 6 - 6);
 248         tty_printf (" %3d%%", percent);
 249     }
 250 
 251     g_free (status);
 252 }
 253 
 254 /* --------------------------------------------------------------------------------------------- */
 255 /**
 256  * Draw status line for editor window if window is not in fullscreen mode.
 257  *
 258  * @param edit editor object
 259  */
 260 
 261 static inline void
 262 edit_status_window (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 263 {
 264     Widget *w = WIDGET (edit);
 265     int y, x;
 266     int cols = w->rect.cols;
 267 
 268     tty_setcolor (STATUSBAR_COLOR);
 269 
 270     if (cols > 5)
 271     {
 272         const char *fname;
 273 
 274         if (edit->filename_vpath != NULL)
 275         {
 276             fname = vfs_path_get_last_path_str (edit->filename_vpath);
 277 
 278             if (!edit_options.state_full_filename)
 279                 fname = x_basename (fname);
 280         }
 281         else
 282             fname = _ ("NoName");
 283 
 284         edit_move (2, 0);
 285         tty_printf ("[%s]", str_term_trim (fname, w->rect.cols - 8 - 6));
 286     }
 287 
 288     tty_getyx (&y, &x);
 289     x -= w->rect.x;
 290     x += 4;
 291     if (x + 6 <= cols - 2 - 6)
 292     {
 293         edit_move (x, 0);
 294         tty_printf ("[%c%c%c%c]",
 295                     edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-',
 296                     edit->modified != 0 ? 'M' : '-', macro_index < 0 ? '-' : 'R',
 297                     edit->overwrite == 0 ? '-' : 'O');
 298     }
 299 
 300     if (cols > 30)
 301     {
 302         edit_move (2, w->rect.lines - 1);
 303         tty_printf ("%3ld %5ld/%ld %6ld/%ld", edit->curs_col + edit->over_col,
 304                     edit->buffer.curs_line + 1, edit->buffer.lines + 1, (long) edit->buffer.curs1,
 305                     (long) edit->buffer.size);
 306     }
 307 
 308     /*
 309      * If we are at the end of file, print <EOF>,
 310      * otherwise print the current character as is (if printable),
 311      * as decimal and as hex.
 312      */
 313     if (cols > 42)
 314     {
 315         char *character_code;
 316 
 317         edit_move (32, w->rect.lines - 1);
 318         character_code = format_character_code (edit);
 319         tty_printf ("[%s]", character_code);
 320         g_free (character_code);
 321     }
 322 }
 323 
 324 /* --------------------------------------------------------------------------------------------- */
 325 /**
 326  * Draw a frame around edit area.
 327  *
 328  * @param edit   editor object
 329  * @param color  color pair
 330  * @param active TRUE if editor object is focused
 331  */
 332 
 333 static inline void
 334 edit_draw_frame (const WEdit *edit, int color, gboolean active)
     /* [previous][next][first][last][top][bottom][index][help]  */
 335 {
 336     const Widget *w = CONST_WIDGET (edit);
 337 
 338     // draw a frame around edit area
 339     tty_setcolor (color);
 340     // draw double frame for active window if skin supports that
 341     tty_draw_box (w->rect.y, w->rect.x, w->rect.lines, w->rect.cols, !active);
 342     // draw a drag marker
 343     if (edit->drag_state == MCEDIT_DRAG_NONE)
 344     {
 345         tty_setcolor (EDITOR_FRAME_DRAG_COLOR);
 346         widget_gotoyx (w, w->rect.lines - 1, w->rect.cols - 1);
 347         tty_print_char (mc_tty_frm[MC_TTY_FRM_RIGHTBOTTOM]);
 348     }
 349 }
 350 
 351 /* --------------------------------------------------------------------------------------------- */
 352 /**
 353  * Draw a window control buttons.
 354  *
 355  * @param edit  editor object
 356  * @param color color pair
 357  */
 358 
 359 static inline void
 360 edit_draw_window_icons (const WEdit *edit, int color)
     /* [previous][next][first][last][top][bottom][index][help]  */
 361 {
 362     const Widget *w = CONST_WIDGET (edit);
 363     char tmp[17];
 364 
 365     tty_setcolor (color);
 366     if (edit->fullscreen != 0)
 367         widget_gotoyx (w->owner, 0, WIDGET (w->owner)->rect.cols - 6);
 368     else
 369         widget_gotoyx (w, 0, w->rect.cols - 8);
 370     g_snprintf (tmp, sizeof (tmp), "[%s][%s]", edit_window_state_char, edit_window_close_char);
 371     tty_print_string (tmp);
 372 }
 373 
 374 /* --------------------------------------------------------------------------------------------- */
 375 
 376 static inline void
 377 print_to_widget (WEdit *edit, long row, int start_col, int start_col_real, long end_col,
     /* [previous][next][first][last][top][bottom][index][help]  */
 378                  line_s line[], char *status, int bookmarked)
 379 {
 380     Widget *w = WIDGET (edit);
 381     line_s *p;
 382     int x1, y, cols_to_skip;
 383     int i;
 384     int wrap_start;
 385     int len;
 386 
 387     x1 = start_col + EDIT_TEXT_HORIZONTAL_OFFSET + edit_options.line_state_width;
 388     y = row + EDIT_TEXT_VERTICAL_OFFSET;
 389     cols_to_skip = abs (start_col_real);
 390 
 391     if (edit->fullscreen == 0)
 392     {
 393         x1++;
 394         y++;
 395     }
 396 
 397     tty_setcolor (EDITOR_NORMAL_COLOR);
 398     if (bookmarked != 0)
 399         tty_setcolor (bookmarked);
 400 
 401     len = end_col + 1 - start_col;
 402     wrap_start = edit_options.word_wrap_line_length + edit->start_col;
 403 
 404     if (len > 0 && w->rect.y + y >= 0)
 405     {
 406         if (!edit_options.show_right_margin || wrap_start > end_col)
 407             tty_draw_hline (w->rect.y + y, w->rect.x + x1, ' ', len);
 408         else if (wrap_start < 0)
 409         {
 410             tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
 411             tty_draw_hline (w->rect.y + y, w->rect.x + x1, ' ', len);
 412         }
 413         else
 414         {
 415             if (wrap_start > 0)
 416                 tty_draw_hline (w->rect.y + y, w->rect.x + x1, ' ', wrap_start);
 417 
 418             len -= wrap_start;
 419             if (len > 0)
 420             {
 421                 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
 422                 tty_draw_hline (w->rect.y + y, w->rect.x + x1 + wrap_start, ' ', len);
 423             }
 424         }
 425     }
 426 
 427     if (edit_options.line_state)
 428     {
 429         tty_setcolor (EDITOR_LINE_STATE_COLOR);
 430 
 431         for (i = 0; i < LINE_STATE_WIDTH; i++)
 432         {
 433             edit_move (x1 + i - edit_options.line_state_width, y);
 434             if (status[i] == '\0')
 435                 status[i] = ' ';
 436             tty_print_char (status[i]);
 437         }
 438     }
 439 
 440     edit_move (x1, y);
 441 
 442     i = 1;
 443     for (p = line; p->ch != 0; p++)
 444     {
 445         int style;
 446         unsigned int textchar;
 447 
 448         if (cols_to_skip != 0)
 449         {
 450             cols_to_skip--;
 451             continue;
 452         }
 453 
 454         style = p->style & 0xFF00;
 455         textchar = p->ch;
 456 
 457         if ((style & MOD_WHITESPACE) != 0)
 458         {
 459             if ((style & MOD_MARKED) == 0)
 460                 tty_setcolor (EDITOR_WHITESPACE_COLOR);
 461             else
 462             {
 463                 textchar = ' ';
 464                 tty_setcolor (EDITOR_MARKED_COLOR);
 465             }
 466         }
 467         else if ((style & MOD_BOLD) != 0)
 468             tty_setcolor (EDITOR_BOLD_COLOR);
 469         else if ((style & MOD_MARKED) != 0)
 470             tty_setcolor (EDITOR_MARKED_COLOR);
 471         else if ((style & MOD_ABNORMAL) != 0)
 472             tty_setcolor (EDITOR_NONPRINTABLE_COLOR);
 473         else
 474             tty_setcolor (p->style >> 16);
 475 
 476         if (edit_options.show_right_margin)
 477         {
 478             if (i > edit_options.word_wrap_line_length + edit->start_col)
 479                 tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
 480             i++;
 481         }
 482 
 483         tty_print_anychar (textchar);
 484     }
 485 }
 486 
 487 /* --------------------------------------------------------------------------------------------- */
 488 /** b is a pointer to the beginning of the line */
 489 
 490 static void
 491 edit_draw_this_line (WEdit *edit, off_t b, long row, long start_col, long end_col)
     /* [previous][next][first][last][top][bottom][index][help]  */
 492 {
 493     Widget *w = WIDGET (edit);
 494     line_s line[MAX_LINE_LEN];
 495     line_s *p = line;
 496     off_t q;
 497     int col, start_col_real;
 498     int abn_style;
 499     int book_mark = 0;
 500     char line_stat[LINE_STATE_WIDTH + 1] = "\0";
 501 
 502     if (row > w->rect.lines - 1 - EDIT_TEXT_VERTICAL_OFFSET - 2 * (edit->fullscreen != 0 ? 0 : 1))
 503         return;
 504 
 505     if (book_mark_query_color (edit, edit->start_line + row, EDITOR_BOOKMARK_COLOR))
 506         book_mark = EDITOR_BOOKMARK_COLOR;
 507     else if (book_mark_query_color (edit, edit->start_line + row, EDITOR_BOOKMARK_FOUND_COLOR))
 508         book_mark = EDITOR_BOOKMARK_FOUND_COLOR;
 509 
 510     if (book_mark != 0)
 511         abn_style = book_mark << 16;
 512     else
 513         abn_style = MOD_ABNORMAL;
 514 
 515     end_col -= EDIT_TEXT_HORIZONTAL_OFFSET + edit_options.line_state_width;
 516     if (edit->fullscreen == 0)
 517     {
 518         end_col--;
 519         if (w->rect.x + w->rect.cols <= WIDGET (w->owner)->rect.cols)
 520             end_col--;
 521     }
 522 
 523     q = edit_move_forward3 (edit, b, start_col - edit->start_col, 0);
 524     col = (int) edit_move_forward3 (edit, b, 0, q);
 525     start_col_real = col + edit->start_col;
 526 
 527     if (edit_options.line_state)
 528     {
 529         long cur_line;
 530 
 531         cur_line = edit->start_line + row;
 532         if (cur_line <= edit->buffer.lines)
 533             g_snprintf (line_stat, sizeof (line_stat), "%7ld ", cur_line + 1);
 534         else
 535         {
 536             memset (line_stat, ' ', LINE_STATE_WIDTH);
 537             line_stat[LINE_STATE_WIDTH] = '\0';
 538         }
 539 
 540         if (book_mark_query_color (edit, cur_line, EDITOR_BOOKMARK_COLOR))
 541             g_snprintf (line_stat, 2, "*");
 542     }
 543 
 544     if (col <= -(edit->start_col + 16))
 545         start_col_real = start_col = 0;
 546     else
 547     {
 548         off_t m1 = 0, m2 = 0;
 549 
 550         eval_marks (edit, &m1, &m2);
 551 
 552         if (row <= edit->buffer.lines - edit->start_line)
 553         {
 554             off_t tws = 0;
 555 
 556             if (edit_options.visible_tws && tty_use_colors ())
 557                 for (tws = edit_buffer_get_eol (&edit->buffer, b); tws > b; tws--)
 558                 {
 559                     unsigned int c;
 560 
 561                     c = edit_buffer_get_byte (&edit->buffer, tws - 1);
 562                     if (!whitespace (c))
 563                         break;
 564                 }
 565 
 566             while (col <= end_col - edit->start_col)
 567             {
 568                 int char_length = 1;
 569                 unsigned int c;
 570                 gboolean wide_width_char = FALSE;
 571                 gboolean control_char = FALSE;
 572                 gboolean printable;
 573 
 574                 p->ch = 0;
 575                 p->style = q == edit->buffer.curs1 ? MOD_CURSOR : 0;
 576 
 577                 if (q >= m1 && q < m2)
 578                 {
 579                     if (!edit->column_highlight)
 580                         p->style |= MOD_MARKED;
 581                     else
 582                     {
 583                         long x, cl;
 584 
 585                         x = (long) edit_move_forward3 (edit, b, 0, q);
 586                         cl = MIN (edit->column1, edit->column2);
 587                         if (x >= cl)
 588                         {
 589                             cl = MAX (edit->column1, edit->column2);
 590                             if (x < cl)
 591                                 p->style |= MOD_MARKED;
 592                         }
 593                     }
 594                 }
 595 
 596                 if (q == edit->bracket)
 597                     p->style |= MOD_BOLD;
 598                 if (q >= edit->found_start && q < (off_t) (edit->found_start + edit->found_len))
 599                     p->style |= MOD_BOLD;
 600 
 601                 if (edit->utf8)
 602                     c = edit_buffer_get_utf (&edit->buffer, q, &char_length);
 603                 else
 604                     c = edit_buffer_get_byte (&edit->buffer, q);
 605 
 606                 // we don't use bg for mc - fg contains both
 607                 if (book_mark != 0)
 608                     p->style |= book_mark << 16;
 609                 else
 610                 {
 611                     int color;
 612 
 613                     color = edit_get_syntax_color (edit, q);
 614                     p->style |= color << 16;
 615                 }
 616 
 617                 switch (c)
 618                 {
 619                 case '\n':
 620                     col = end_col - edit->start_col + 1;  // quit
 621                     break;
 622 
 623                 case '\t':
 624                 {
 625                     int tab_over;
 626                     int i;
 627 
 628                     i = TAB_SIZE - ((int) col % TAB_SIZE);
 629                     tab_over = (end_col - edit->start_col) - (col + i - 1);
 630                     if (tab_over < 0)
 631                         i += tab_over;
 632                     col += i;
 633                     if ((edit_options.visible_tabs || (edit_options.visible_tws && q >= tws))
 634                         && enable_show_tabs_tws && tty_use_colors ())
 635                     {
 636                         int style;
 637 
 638                         if ((p->style & MOD_MARKED) != 0)
 639                             style = p->style;
 640                         else if (book_mark != 0)
 641                             style = c | (book_mark << 16);
 642                         else
 643                             style = p->style | MOD_WHITESPACE;
 644 
 645                         if (i > 1)
 646                         {
 647                             p->ch = '<';
 648                             p->style = style;
 649                             p++;
 650 
 651                             while (--i > 1)
 652                             {
 653                                 p->ch = '-';
 654                                 p->style = style;
 655                                 p++;
 656                             }
 657                         }
 658 
 659                         p->ch = '>';
 660                         p->style = style;
 661                         p++;
 662                     }
 663                     else
 664                     {
 665                         if (edit_options.visible_tws && q >= tws && enable_show_tabs_tws
 666                             && tty_use_colors ())
 667                         {
 668                             p->ch = '.';
 669                             p->style |= MOD_WHITESPACE;
 670                         }
 671                         else
 672                             p->ch |= ' ';
 673 
 674                         const int style = p->style & ~MOD_CURSOR;
 675 
 676                         p++;
 677                         while (--i != 0)
 678                         {
 679                             p->ch = ' ';
 680                             p->style = style;
 681                             p++;
 682                         }
 683                     }
 684                 }
 685                 break;
 686 
 687                 case ' ':
 688                     if (edit_options.visible_tws && q >= tws && enable_show_tabs_tws
 689                         && tty_use_colors ())
 690                     {
 691                         p->ch = '.';
 692                         p->style |= MOD_WHITESPACE;
 693                         p++;
 694                         col++;
 695                         break;
 696                     }
 697                     MC_FALLTHROUGH;
 698 
 699                 default:
 700                     if (mc_global.utf8_display)
 701                     {
 702                         if (!edit->utf8)
 703                             c = convert_from_8bit_to_utf_c ((unsigned char) c, edit->converter);
 704                         else if (g_unichar_iswide (c))
 705                         {
 706                             wide_width_char = TRUE;
 707                             col++;
 708                         }
 709                     }
 710                     else if (edit->utf8)
 711                         c = convert_from_utf_to_current_c (c, edit->converter);
 712                     else
 713                         c = convert_to_display_c (c);
 714 
 715                     // Caret notation for control characters
 716                     if (c < 32)
 717                     {
 718                         p->ch = '^';
 719                         p->style = abn_style;
 720                         p++;
 721                         p->ch = c + 0x40;
 722                         p->style = abn_style;
 723                         p++;
 724                         col += 2;
 725                         control_char = TRUE;
 726                         break;
 727                     }
 728                     if (c == 127)
 729                     {
 730                         p->ch = '^';
 731                         p->style = abn_style;
 732                         p++;
 733                         p->ch = '?';
 734                         p->style = abn_style;
 735                         p++;
 736                         col += 2;
 737                         control_char = TRUE;
 738                         break;
 739                     }
 740 
 741                     if (edit->utf8)
 742                     {
 743                         if (mc_global.utf8_display)
 744                             // c is gunichar
 745                             printable = g_unichar_isprint (c);
 746                         else
 747                             // c was gunichar; now c is 8-bit char converted from gunichar
 748                             printable = is_printable (c);
 749                     }
 750                     else
 751                         // c is 8-bit char
 752                         printable = is_printable (c);
 753 
 754                     if (printable)
 755                         p->ch = c;
 756                     else
 757                     {
 758                         p->ch = '.';
 759                         p->style = abn_style;
 760                     }
 761                     p++;
 762                     col++;
 763                     break;
 764                 }  // case
 765 
 766                 q++;
 767                 if (char_length > 1)
 768                     q += char_length - 1;
 769 
 770                 if (col > (end_col - edit->start_col + 1))
 771                 {
 772                     if (wide_width_char)
 773                     {
 774                         p--;
 775                         break;
 776                     }
 777                     if (control_char)
 778                     {
 779                         p -= 2;
 780                         break;
 781                     }
 782                 }
 783             }
 784         }
 785     }
 786 
 787     p->ch = 0;
 788 
 789     print_to_widget (edit, row, start_col, start_col_real, end_col, line, line_stat, book_mark);
 790 }
 791 
 792 /* --------------------------------------------------------------------------------------------- */
 793 
 794 static inline void
 795 edit_draw_this_char (WEdit *edit, off_t curs, long row, long start_column, long end_column)
     /* [previous][next][first][last][top][bottom][index][help]  */
 796 {
 797     off_t b;
 798 
 799     b = edit_buffer_get_bol (&edit->buffer, curs);
 800     edit_draw_this_line (edit, b, row, start_column, end_column);
 801 }
 802 
 803 /* --------------------------------------------------------------------------------------------- */
 804 /** cursor must be in screen for other than REDRAW_PAGE passed in force */
 805 
 806 static inline void
 807 render_edit_text (WEdit *edit, long start_row, long start_column, long end_row, long end_column)
     /* [previous][next][first][last][top][bottom][index][help]  */
 808 {
 809     static long prev_curs_row = 0;
 810     static off_t prev_curs = 0;
 811 
 812     Widget *we = WIDGET (edit);
 813     Widget *wh = WIDGET (we->owner);
 814     WRect *w = &we->rect;
 815 
 816     int force = edit->force;
 817     int y1, x1, y2, x2;
 818     int last_line, last_column;
 819 
 820     // draw only visible region
 821 
 822     last_line = wh->rect.y + wh->rect.lines - 1;
 823 
 824     y1 = w->y;
 825     if (y1 > last_line - 1)  // buttonbar
 826         return;
 827 
 828     last_column = wh->rect.x + wh->rect.cols - 1;
 829 
 830     x1 = w->x;
 831     if (x1 > last_column)
 832         return;
 833 
 834     y2 = w->y + w->lines - 1;
 835     if (y2 < wh->rect.y + 1)  // menubar
 836         return;
 837 
 838     x2 = w->x + w->cols - 1;
 839     if (x2 < wh->rect.x)
 840         return;
 841 
 842     if ((force & REDRAW_IN_BOUNDS) == 0)
 843     {
 844         // !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows
 845         // draw only visible region
 846 
 847         if (y2 <= last_line - 1)  // buttonbar
 848             end_row = w->lines - 1;
 849         else if (y1 >= wh->rect.y + 1)  // menubar
 850             end_row = wh->rect.lines - 1 - y1 - 1;
 851         else
 852             end_row = start_row + wh->rect.lines - 1 - 1;
 853 
 854         if (x2 <= last_column)
 855             end_column = w->cols - 1;
 856         else if (x1 >= wh->rect.x)
 857             end_column = wh->rect.cols - 1 - x1;
 858         else
 859             end_column = start_column + wh->rect.cols - 1;
 860     }
 861 
 862     /*
 863      * If the position of the page has not moved then we can draw the cursor
 864      * character only.  This will prevent line flicker when using arrow keys.
 865      */
 866     if ((force & REDRAW_CHAR_ONLY) == 0 || (force & REDRAW_PAGE) != 0)
 867     {
 868         long row = 0;
 869         long b;
 870 
 871         if ((force & REDRAW_PAGE) != 0)
 872         {
 873             b = edit_buffer_get_forward_offset (&edit->buffer, edit->start_display, start_row, 0);
 874             for (row = start_row; row <= end_row; row++)
 875             {
 876                 if (key_pending (edit))
 877                     return;
 878                 edit_draw_this_line (edit, b, row, start_column, end_column);
 879                 b = edit_buffer_get_forward_offset (&edit->buffer, b, 1, 0);
 880             }
 881         }
 882         else
 883         {
 884             long curs_row = edit->curs_row;
 885 
 886             if ((force & REDRAW_BEFORE_CURSOR) != 0 && start_row < curs_row)
 887             {
 888                 long upto;
 889 
 890                 b = edit->start_display;
 891                 upto = MIN (curs_row - 1, end_row);
 892                 for (row = start_row; row <= upto; row++)
 893                 {
 894                     if (key_pending (edit))
 895                         return;
 896                     edit_draw_this_line (edit, b, row, start_column, end_column);
 897                     b = edit_buffer_get_forward_offset (&edit->buffer, b, 1, 0);
 898                 }
 899             }
 900 
 901             //          if (force & REDRAW_LINE)          ---> default
 902             b = edit_buffer_get_current_bol (&edit->buffer);
 903             if (curs_row >= start_row && curs_row <= end_row)
 904             {
 905                 if (key_pending (edit))
 906                     return;
 907                 edit_draw_this_line (edit, b, curs_row, start_column, end_column);
 908             }
 909 
 910             if ((force & REDRAW_AFTER_CURSOR) != 0 && end_row > curs_row)
 911             {
 912                 b = edit_buffer_get_forward_offset (&edit->buffer, b, 1, 0);
 913                 for (row = MAX (curs_row + 1, start_row); row <= end_row; row++)
 914                 {
 915                     if (key_pending (edit))
 916                         return;
 917                     edit_draw_this_line (edit, b, row, start_column, end_column);
 918                     b = edit_buffer_get_forward_offset (&edit->buffer, b, 1, 0);
 919                 }
 920             }
 921 
 922             if ((force & REDRAW_LINE_ABOVE) != 0 && curs_row >= 1)
 923             {
 924                 row = curs_row - 1;
 925                 b = edit_buffer_get_current_bol (&edit->buffer);
 926                 b = edit_buffer_get_backward_offset (&edit->buffer, b, 1);
 927                 if (row >= start_row && row <= end_row)
 928                 {
 929                     if (key_pending (edit))
 930                         return;
 931                     edit_draw_this_line (edit, b, row, start_column, end_column);
 932                 }
 933             }
 934 
 935             if ((force & REDRAW_LINE_BELOW) != 0 && row < w->lines - 1)
 936             {
 937                 row = curs_row + 1;
 938                 b = edit_buffer_get_current_bol (&edit->buffer);
 939                 b = edit_buffer_get_forward_offset (&edit->buffer, b, 1, 0);
 940                 if (row >= start_row && row <= end_row)
 941                 {
 942                     if (key_pending (edit))
 943                         return;
 944                     edit_draw_this_line (edit, b, row, start_column, end_column);
 945                 }
 946             }
 947         }
 948     }
 949     else if (prev_curs_row < edit->curs_row)
 950     {
 951         // with the new text highlighting, we must draw from the top down
 952         edit_draw_this_char (edit, prev_curs, prev_curs_row, start_column, end_column);
 953         edit_draw_this_char (edit, edit->buffer.curs1, edit->curs_row, start_column, end_column);
 954     }
 955     else
 956     {
 957         edit_draw_this_char (edit, edit->buffer.curs1, edit->curs_row, start_column, end_column);
 958         edit_draw_this_char (edit, prev_curs, prev_curs_row, start_column, end_column);
 959     }
 960 
 961     edit->force = 0;
 962 
 963     prev_curs_row = edit->curs_row;
 964     prev_curs = edit->buffer.curs1;
 965 }
 966 
 967 /* --------------------------------------------------------------------------------------------- */
 968 
 969 static inline void
 970 edit_render (WEdit *edit, int page, int row_start, int col_start, int row_end, int col_end)
     /* [previous][next][first][last][top][bottom][index][help]  */
 971 {
 972     if (page != 0)  // if it was an expose event, 'page' would be set
 973         edit->force |= REDRAW_PAGE | REDRAW_IN_BOUNDS;
 974 
 975     render_edit_text (edit, row_start, col_start, row_end, col_end);
 976 
 977     /*
 978      * edit->force != 0 means a key was pending and the redraw
 979      * was halted, so next time we must redraw everything in case stuff
 980      * was left undrawn from a previous key press.
 981      */
 982     if (edit->force != 0)
 983         edit->force |= REDRAW_PAGE;
 984 }
 985 
 986 /* --------------------------------------------------------------------------------------------- */
 987 /*** public functions ****************************************************************************/
 988 /* --------------------------------------------------------------------------------------------- */
 989 
 990 void
 991 edit_status (WEdit *edit, gboolean active)
     /* [previous][next][first][last][top][bottom][index][help]  */
 992 {
 993     int color;
 994 
 995     if (edit->fullscreen != 0)
 996     {
 997         color = STATUSBAR_COLOR;
 998         edit_status_fullscreen (edit, color);
 999     }
1000     else
1001     {
1002         color = edit->drag_state != MCEDIT_DRAG_NONE ? EDITOR_FRAME_DRAG_COLOR
1003             : active                                 ? EDITOR_FRAME_ACTIVE_COLOR
1004                                                      : EDITOR_FRAME_COLOR;
1005         edit_draw_frame (edit, color, active);
1006         edit_status_window (edit);
1007     }
1008 
1009     edit_draw_window_icons (edit, color);
1010 }
1011 
1012 /* --------------------------------------------------------------------------------------------- */
1013 
1014 /** this scrolls the text so that cursor is on the screen */
1015 void
1016 edit_scroll_screen_over_cursor (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1017 {
1018     WRect *w = &WIDGET (edit)->rect;
1019 
1020     long p;
1021     long outby;
1022     int b_extreme, t_extreme, l_extreme, r_extreme;
1023 
1024     if (w->lines <= 0 || w->cols <= 0)
1025         return;
1026 
1027     rect_resize (w, -EDIT_TEXT_VERTICAL_OFFSET,
1028                  -(EDIT_TEXT_HORIZONTAL_OFFSET + edit_options.line_state_width));
1029 
1030     if (edit->fullscreen == 0)
1031         rect_grow (w, -1, -1);
1032 
1033     r_extreme = EDIT_RIGHT_EXTREME;
1034     l_extreme = EDIT_LEFT_EXTREME;
1035     b_extreme = EDIT_BOTTOM_EXTREME;
1036     t_extreme = EDIT_TOP_EXTREME;
1037     if (edit->found_len != 0)
1038     {
1039         b_extreme = MAX (w->lines / 4, b_extreme);
1040         t_extreme = MAX (w->lines / 4, t_extreme);
1041     }
1042     if (b_extreme + t_extreme + 1 > w->lines)
1043     {
1044         int n;
1045 
1046         n = b_extreme + t_extreme;
1047         if (n == 0)
1048             n = 1;
1049         b_extreme = (b_extreme * (w->lines - 1)) / n;
1050         t_extreme = (t_extreme * (w->lines - 1)) / n;
1051     }
1052     if (l_extreme + r_extreme + 1 > w->cols)
1053     {
1054         int n;
1055 
1056         n = l_extreme + r_extreme;
1057         if (n == 0)
1058             n = 1;
1059         l_extreme = (l_extreme * (w->cols - 1)) / n;
1060         r_extreme = (r_extreme * (w->cols - 1)) / n;
1061     }
1062     p = edit_get_col (edit) + edit->over_col;
1063     edit_update_curs_row (edit);
1064     outby = p + edit->start_col - w->cols + 1 + (r_extreme + edit->found_len);
1065     if (outby > 0)
1066         edit_scroll_right (edit, outby);
1067     outby = l_extreme - p - edit->start_col;
1068     if (outby > 0)
1069         edit_scroll_left (edit, outby);
1070     p = edit->curs_row;
1071     outby = p - w->lines + 1 + b_extreme;
1072     if (outby > 0)
1073         edit_scroll_downward (edit, outby);
1074     outby = t_extreme - p;
1075     if (outby > 0)
1076         edit_scroll_upward (edit, outby);
1077     edit_update_curs_row (edit);
1078 
1079     rect_resize (w, EDIT_TEXT_VERTICAL_OFFSET,
1080                  EDIT_TEXT_HORIZONTAL_OFFSET + edit_options.line_state_width);
1081     if (edit->fullscreen == 0)
1082         rect_grow (w, 1, 1);
1083 }
1084 
1085 /* --------------------------------------------------------------------------------------------- */
1086 
1087 void
1088 edit_render_keypress (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1089 {
1090     edit_render (edit, 0, 0, 0, 0, 0);
1091 }
1092 
1093 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */