root/src/editor/edit.c

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

DEFINITIONS

This source file includes following definitions.
  1. edit_load_status_update_cb
  2. edit_load_file_fast
  3. edit_find_filter
  4. edit_get_filter
  5. edit_insert_stream
  6. check_file_access
  7. edit_load_file
  8. edit_load_position
  9. edit_save_position
  10. edit_purge_widget
  11. edit_pop_undo_action
  12. edit_pop_redo_action
  13. get_prev_undo_action
  14. edit_modification
  15. is_in_indent
  16. is_blank
  17. edit_find_line
  18. edit_move_up_paragraph
  19. edit_move_down_paragraph
  20. edit_begin_page
  21. edit_end_page
  22. edit_move_to_top
  23. edit_move_to_bottom
  24. edit_cursor_to_bol
  25. edit_cursor_to_eol
  26. my_type_of
  27. edit_left_word_move
  28. edit_left_word_move_cmd
  29. edit_right_word_move
  30. edit_right_word_move_cmd
  31. edit_right_char_move_cmd
  32. edit_left_char_move_cmd
  33. edit_move_updown
  34. edit_right_delete_word
  35. edit_left_delete_word
  36. edit_do_undo
  37. edit_do_redo
  38. edit_group_undo
  39. edit_delete_to_line_end
  40. edit_delete_to_line_begin
  41. is_aligned_on_a_tab
  42. right_of_four_spaces
  43. left_of_four_spaces
  44. edit_auto_indent
  45. edit_double_newline
  46. insert_spaces_tab
  47. edit_tab_cmd
  48. check_and_wrap_line
  49. edit_get_bracket
  50. edit_goto_matching_bracket
  51. edit_move_block_to_right
  52. edit_move_block_to_left
  53. edit_print_string
  54. edit_insert_column_from_file
  55. edit_user_menu
  56. edit_get_write_filter
  57. edit_write_stream
  58. is_break_char
  59. edit_insert_file
  60. edit_init
  61. edit_clean
  62. edit_reload_line
  63. edit_set_codeset
  64. edit_push_undo_action
  65. edit_push_redo_action
  66. edit_insert
  67. edit_insert_ahead
  68. edit_insert_over
  69. edit_delete
  70. edit_backspace
  71. edit_cursor_move
  72. edit_move_forward3
  73. edit_get_cursor_offset
  74. edit_get_col
  75. edit_update_curs_row
  76. edit_update_curs_col
  77. edit_get_curs_col
  78. edit_scroll_upward
  79. edit_scroll_downward
  80. edit_scroll_right
  81. edit_scroll_left
  82. edit_move_to_prev_col
  83. edit_line_is_blank
  84. edit_move_to_line
  85. edit_move_display
  86. edit_push_markers
  87. edit_set_markers
  88. eval_marks
  89. edit_mark_cmd
  90. edit_mark_current_word_cmd
  91. edit_mark_current_line_cmd
  92. edit_delete_line
  93. edit_push_key_press
  94. edit_find_bracket
  95. edit_execute_key_command
  96. edit_execute_cmd
  97. edit_stack_init
  98. edit_stack_free
  99. edit_move_up
  100. edit_move_down
  101. edit_arg_vpath_new
  102. edit_arg_new
  103. edit_arg_init
  104. edit_arg_assign
  105. edit_arg_free
  106. edit_get_file_name

   1 /*
   2    Editor low level data handling and cursor fundamentals.
   3 
   4    Copyright (C) 1996-2025
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Paul Sheer 1996, 1997
   9    Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
  10    Andrew Borodin <aborodin@vmail.ru> 2012-2022
  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 low level data handling and cursor fundamentals
  30  *  \author Paul Sheer
  31  *  \date 1996, 1997
  32  */
  33 
  34 #include <config.h>
  35 #include <stdio.h>
  36 #include <stdarg.h>
  37 #include <sys/types.h>
  38 #include <unistd.h>
  39 #include <string.h>
  40 #include <ctype.h>
  41 #include <sys/stat.h>
  42 #include <stdint.h>  // UINTMAX_MAX
  43 #include <stdlib.h>
  44 
  45 #include "lib/global.h"
  46 
  47 #include "lib/tty/color.h"
  48 #include "lib/tty/tty.h"  // attrset()
  49 #include "lib/tty/key.h"  // is_idle()
  50 #include "lib/skin.h"     // EDITOR_NORMAL_COLOR
  51 #include "lib/fileloc.h"  // EDIT_HOME_BLOCK_FILE
  52 #include "lib/vfs/vfs.h"
  53 #include "lib/strutil.h"  // utf string functions
  54 #include "lib/util.h"     // load_file_position(), save_file_position()
  55 #include "lib/timefmt.h"  // time formatting
  56 #include "lib/lock.h"
  57 #include "lib/widget.h"
  58 
  59 #ifdef HAVE_CHARSET
  60 #    include "lib/charsets.h"  // get_codepage_id
  61 #endif
  62 
  63 #include "src/usermenu.h"  // user_menu_cmd()
  64 
  65 #include "src/keymap.h"
  66 
  67 #include "edit-impl.h"
  68 #include "editwidget.h"
  69 #include "editsearch.h"
  70 #include "editcomplete.h"  // edit_complete_word_cmd()
  71 #include "editmacros.h"
  72 #include "etags.h"  // edit_get_match_keyword_cmd()
  73 #ifdef HAVE_ASPELL
  74 #    include "spell.h"
  75 #endif
  76 
  77 /*** global variables ****************************************************************************/
  78 
  79 edit_options_t edit_options = {
  80     .word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH,
  81     .typewriter_wrap = FALSE,
  82     .auto_para_formatting = FALSE,
  83     .fill_tabs_with_spaces = FALSE,
  84     .return_does_auto_indent = TRUE,
  85     .backspace_through_tabs = FALSE,
  86     .fake_half_tabs = TRUE,
  87     .persistent_selections = TRUE,
  88     .drop_selection_on_copy = TRUE,
  89     .cursor_beyond_eol = FALSE,
  90     .cursor_after_inserted_block = FALSE,
  91     .state_full_filename = FALSE,
  92     .line_state = FALSE,
  93     .line_state_width = 0,
  94     .save_mode = EDIT_QUICK_SAVE,
  95     .confirm_save = TRUE,
  96     .save_position = TRUE,
  97     .syntax_highlighting = TRUE,
  98     .group_undo = FALSE,
  99     .backup_ext = NULL,
 100     .filesize_threshold = NULL,
 101     .stop_format_chars = NULL,
 102     .visible_tabs = TRUE,
 103     .visible_tws = TRUE,
 104     .show_right_margin = FALSE,
 105     .simple_statusbar = FALSE,
 106     .check_nl_at_eof = FALSE,
 107 };
 108 
 109 int max_undo = 32768;
 110 
 111 gboolean enable_show_tabs_tws = TRUE;
 112 
 113 unsigned int edit_stack_iterator = 0;
 114 edit_arg_t edit_history_moveto[MAX_HISTORY_MOVETO];
 115 /* magic sequence for say than block is vertical */
 116 const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
 117 
 118 /*** file scope macro definitions ****************************************************************/
 119 
 120 #define TEMP_BUF_LEN 1024
 121 
 122 #define space_width  1
 123 
 124 /*** file scope type declarations ****************************************************************/
 125 
 126 /*** forward declarations (file scope functions) *************************************************/
 127 
 128 /*** file scope variables ************************************************************************/
 129 
 130 /* detecting an error on save is easy: just check if every byte has been written. */
 131 /* detecting an error on read, is not so easy 'cos there is not way to tell
 132    whether you read everything or not. */
 133 /* FIXME: add proper 'triple_pipe_open' to read, write and check errors. */
 134 static const struct edit_filters
 135 {
 136     const char *read, *write, *extension;
 137 } all_filters[] = {
 138     { "xz -cd %s 2>&1", "xz > %s", ".xz" },         //
 139     { "zstd -cd %s 2>&1", "zstd > %s", ".zst" },    //
 140     { "lz4 -cd %s 2>&1", "lz4 > %s", ".lz4" },      //
 141     { "lzip -cd %s 2>&1", "lzip > %s", ".lz" },     //
 142     { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },   //
 143     { "lzop -cd %s 2>&1", "lzop > %s", ".lzo" },    //
 144     { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },  //
 145     { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },     //
 146     { "gzip -cd %s 2>&1", "gzip > %s", ".Z" },
 147 };
 148 
 149 static const off_t filesize_default_threshold = 64 * 1024 * 1024;  // 64 MB
 150 
 151 /* --------------------------------------------------------------------------------------------- */
 152 /*** file scope functions ************************************************************************/
 153 /* --------------------------------------------------------------------------------------------- */
 154 
 155 static int
 156 edit_load_status_update_cb (status_msg_t *sm)
     /* [previous][next][first][last][top][bottom][index][help]  */
 157 {
 158     simple_status_msg_t *ssm = SIMPLE_STATUS_MSG (sm);
 159     edit_buffer_read_file_status_msg_t *rsm = (edit_buffer_read_file_status_msg_t *) sm;
 160     Widget *wd = WIDGET (sm->dlg);
 161 
 162     if (verbose)
 163         label_set_textv (ssm->label, _ ("Loading: %3d%%"),
 164                          edit_buffer_calc_percent (rsm->buf, rsm->loaded));
 165     else
 166         label_set_text (ssm->label, _ ("Loading..."));
 167 
 168     if (rsm->first)
 169     {
 170         Widget *lw = WIDGET (ssm->label);
 171         WRect r;
 172 
 173         r = wd->rect;
 174         r.cols = MAX (r.cols, lw->rect.cols + 6);
 175         widget_set_size_rect (wd, &r);
 176         r = lw->rect;
 177         r.x = wd->rect.x + (wd->rect.cols - r.cols) / 2;
 178         widget_set_size_rect (lw, &r);
 179         rsm->first = FALSE;
 180     }
 181 
 182     return status_msg_common_update (sm);
 183 }
 184 
 185 /* --------------------------------------------------------------------------------------------- */
 186 /**
 187  * Load file OR text into buffers.  Set cursor to the beginning of file.
 188  *
 189  * @return FALSE on error.
 190  */
 191 
 192 static gboolean
 193 edit_load_file_fast (edit_buffer_t *buf, const vfs_path_t *filename_vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 194 {
 195     int file;
 196     gboolean ret;
 197     edit_buffer_read_file_status_msg_t rsm;
 198     gboolean aborted;
 199 
 200     file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
 201     if (file < 0)
 202     {
 203         message (D_ERROR, MSG_ERROR, _ ("Cannot open %s for reading"),
 204                  vfs_path_as_str (filename_vpath));
 205         return FALSE;
 206     }
 207 
 208     rsm.first = TRUE;
 209     rsm.buf = buf;
 210     rsm.loaded = 0;
 211 
 212     status_msg_init (STATUS_MSG (&rsm), _ ("Load file"), 1.0, simple_status_msg_init_cb,
 213                      edit_load_status_update_cb, NULL);
 214 
 215     ret = (edit_buffer_read_file (buf, file, buf->size, &rsm, &aborted) == buf->size);
 216 
 217     status_msg_deinit (STATUS_MSG (&rsm));
 218 
 219     if (!ret && !aborted)
 220         message (D_ERROR, MSG_ERROR, _ ("Error reading %s"), vfs_path_as_str (filename_vpath));
 221 
 222     mc_close (file);
 223     return ret;
 224 }
 225 
 226 /* --------------------------------------------------------------------------------------------- */
 227 /** Return index of the filter or -1 is there is no appropriate filter */
 228 
 229 static int
 230 edit_find_filter (const vfs_path_t *filename_vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 231 {
 232     if (filename_vpath != NULL)
 233     {
 234         const char *s;
 235         size_t i;
 236 
 237         s = vfs_path_as_str (filename_vpath);
 238 
 239         for (i = 0; i < G_N_ELEMENTS (all_filters); i++)
 240             if (g_str_has_suffix (s, all_filters[i].extension))
 241                 return i;
 242     }
 243 
 244     return -1;
 245 }
 246 
 247 /* --------------------------------------------------------------------------------------------- */
 248 
 249 static char *
 250 edit_get_filter (const vfs_path_t *filename_vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 251 {
 252     int i;
 253     char *quoted_name;
 254     char *p = NULL;
 255 
 256     i = edit_find_filter (filename_vpath);
 257     if (i < 0)
 258         return NULL;
 259 
 260     quoted_name = name_quote (vfs_path_as_str (filename_vpath), FALSE);
 261     if (quoted_name != NULL)
 262     {
 263         p = g_strdup_printf (all_filters[i].read, quoted_name);
 264         g_free (quoted_name);
 265     }
 266 
 267     return p;
 268 }
 269 
 270 /* --------------------------------------------------------------------------------------------- */
 271 
 272 static off_t
 273 edit_insert_stream (WEdit *edit, FILE *f)
     /* [previous][next][first][last][top][bottom][index][help]  */
 274 {
 275     int c;
 276     off_t i;
 277 
 278     for (i = 0; (c = fgetc (f)) >= 0; i++)
 279         edit_insert (edit, c);
 280 
 281     return i;
 282 }
 283 
 284 /* --------------------------------------------------------------------------------------------- */
 285 /**
 286  * Open file and create it if necessary.
 287  *
 288  * @param edit           editor object
 289  * @param filename_vpath file name
 290  * @param st             buffer for store stat info
 291  * @return TRUE for success, FALSE for error.
 292  */
 293 
 294 static gboolean
 295 check_file_access (WEdit *edit, const vfs_path_t *filename_vpath, struct stat *st)
     /* [previous][next][first][last][top][bottom][index][help]  */
 296 {
 297     static uintmax_t threshold = UINTMAX_MAX;
 298     int file;
 299     gchar *errmsg = NULL;
 300     gboolean ret = TRUE;
 301 
 302     // Try opening an existing file
 303     file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
 304     if (file < 0)
 305     {
 306         /*
 307          * Try creating the file. O_EXCL prevents following broken links
 308          * and opening existing files.
 309          */
 310         file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
 311         if (file < 0)
 312         {
 313             errmsg = g_strdup_printf (_ ("Cannot open %s for reading"),
 314                                       vfs_path_as_str (filename_vpath));
 315             goto cleanup;
 316         }
 317 
 318         // New file, delete it if it's not modified or saved
 319         edit->delete_file = 1;
 320     }
 321 
 322     // Check what we have opened
 323     if (mc_fstat (file, st) < 0)
 324     {
 325         errmsg = g_strdup_printf (_ ("Cannot get size/permissions for %s"),
 326                                   vfs_path_as_str (filename_vpath));
 327         goto cleanup;
 328     }
 329 
 330     // We want to open regular files only
 331     if (!S_ISREG (st->st_mode))
 332     {
 333         errmsg =
 334             g_strdup_printf (_ ("\"%s\" is not a regular file"), vfs_path_as_str (filename_vpath));
 335         goto cleanup;
 336     }
 337 
 338     // get file size threshold for alarm
 339     if (threshold == UINTMAX_MAX)
 340     {
 341         gboolean err = FALSE;
 342 
 343         threshold = parse_integer (edit_options.filesize_threshold, &err);
 344         if (err)
 345             threshold = filesize_default_threshold;
 346     }
 347 
 348     /*
 349      * Don't delete non-empty files.
 350      * O_EXCL should prevent it, but let's be on the safe side.
 351      */
 352     if (st->st_size > 0)
 353         edit->delete_file = 0;
 354 
 355     if ((uintmax_t) st->st_size > threshold)
 356     {
 357         int act;
 358 
 359         errmsg = g_strdup_printf (_ ("File \"%s\" is too large.\nOpen it anyway?"),
 360                                   vfs_path_as_str (filename_vpath));
 361         act = edit_query_dialog2 (_ ("Warning"), errmsg, _ ("&Yes"), _ ("&No"));
 362         MC_PTR_FREE (errmsg);
 363 
 364         if (act != 0)
 365             ret = FALSE;
 366     }
 367 
 368 cleanup:
 369     (void) mc_close (file);
 370 
 371     if (errmsg != NULL)
 372     {
 373         message (D_ERROR, MSG_ERROR, "%s", errmsg);
 374         g_free (errmsg);
 375         ret = FALSE;
 376     }
 377 
 378     return ret;
 379 }
 380 
 381 /* --------------------------------------------------------------------------------------------- */
 382 
 383 /**
 384  * Open the file and load it into the buffers, either directly or using
 385  * a filter.  Return TRUE on success, FALSE on error.
 386  *
 387  * Fast loading (edit_load_file_fast) is used when the file size is
 388  * known.  In this case the data is read into the buffers by blocks.
 389  * If the file size is not known, the data is loaded byte by byte in
 390  * edit_insert_file.
 391  *
 392  * @param edit editor object
 393  * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
 394  */
 395 static gboolean
 396 edit_load_file (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 397 {
 398     gboolean fast_load = TRUE;
 399 
 400     // Cannot do fast load if a filter is used
 401     if (edit_find_filter (edit->filename_vpath) >= 0)
 402         fast_load = FALSE;
 403 
 404     /*
 405      * FIXME: line end translation should disable fast loading as well
 406      * Consider doing fseek() to the end and ftell() for the real size.
 407      */
 408     if (edit->filename_vpath != NULL)
 409     {
 410         /*
 411          * VFS may report file size incorrectly, and slow load is not a big
 412          * deal considering overhead in VFS.
 413          */
 414         if (!vfs_file_is_local (edit->filename_vpath))
 415             fast_load = FALSE;
 416 
 417         // If we are dealing with a real file, check that it exists
 418         if (!check_file_access (edit, edit->filename_vpath, &edit->stat1))
 419         {
 420             edit_clean (edit);
 421             return FALSE;
 422         }
 423     }
 424     else
 425     {
 426         // nothing to load
 427         fast_load = FALSE;
 428     }
 429 
 430     if (fast_load)
 431     {
 432         edit_buffer_init (&edit->buffer, edit->stat1.st_size);
 433 
 434         if (!edit_load_file_fast (&edit->buffer, edit->filename_vpath))
 435         {
 436             edit_clean (edit);
 437             return FALSE;
 438         }
 439     }
 440     else
 441     {
 442         edit_buffer_init (&edit->buffer, 0);
 443 
 444         if (edit->filename_vpath != NULL
 445             && *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) != '\0')
 446         {
 447             edit->undo_stack_disable = 1;
 448             if (edit_insert_file (edit, edit->filename_vpath) < 0)
 449             {
 450                 edit_clean (edit);
 451                 return FALSE;
 452             }
 453             edit->undo_stack_disable = 0;
 454         }
 455     }
 456     edit->lb = LB_ASIS;
 457     return TRUE;
 458 }
 459 
 460 /* --------------------------------------------------------------------------------------------- */
 461 /**
 462  * Restore saved cursor position and/or bookmarks in the file
 463  *
 464  * @param edit editor object
 465  * @param load_position If TRUE, load bookmarks and cursor position and apply them.
 466  *                      If FALSE, load bookmarks only.
 467  */
 468 
 469 static void
 470 edit_load_position (WEdit *edit, gboolean load_position)
     /* [previous][next][first][last][top][bottom][index][help]  */
 471 {
 472     long line, column;
 473     off_t offset;
 474     off_t b;
 475 
 476     if (edit->filename_vpath == NULL
 477         || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
 478         return;
 479 
 480     load_file_position (edit->filename_vpath, &line, &column, &offset, &edit->serialized_bookmarks);
 481     // apply bookmarks in any case
 482     book_mark_restore (edit, BOOK_MARK_COLOR);
 483 
 484     if (!load_position)
 485         return;
 486 
 487     if (line > 0)
 488     {
 489         edit_move_to_line (edit, line - 1);
 490         edit->prev_col = column;
 491     }
 492     else if (offset > 0)
 493     {
 494         edit_cursor_move (edit, offset);
 495         line = edit->buffer.curs_line;
 496         edit->search_start = edit->buffer.curs1;
 497     }
 498 
 499     b = edit_buffer_get_current_bol (&edit->buffer);
 500     edit_move_to_prev_col (edit, b);
 501     edit_move_display (edit, line - (WIDGET (edit)->rect.lines / 2));
 502 }
 503 
 504 /* --------------------------------------------------------------------------------------------- */
 505 /** Save cursor position in the file */
 506 
 507 static void
 508 edit_save_position (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 509 {
 510     if (edit->filename_vpath == NULL
 511         || *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
 512         return;
 513 
 514     book_mark_serialize (edit, BOOK_MARK_COLOR);
 515     save_file_position (edit->filename_vpath, edit->buffer.curs_line + 1, edit->curs_col,
 516                         edit->buffer.curs1, edit->serialized_bookmarks);
 517     edit->serialized_bookmarks = NULL;
 518 }
 519 
 520 /* --------------------------------------------------------------------------------------------- */
 521 /** Clean the WEdit stricture except the widget part */
 522 
 523 static void
 524 edit_purge_widget (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 525 {
 526     size_t len = sizeof (WEdit) - sizeof (Widget);
 527     char *start = (char *) edit + sizeof (Widget);
 528     memset (start, 0, len);
 529 }
 530 
 531 /* --------------------------------------------------------------------------------------------- */
 532 
 533 /*
 534    TODO: if the user undos until the stack bottom, and the stack has not wrapped,
 535    then the file should be as it was when he loaded up. Then set edit->modified to 0.
 536  */
 537 
 538 static long
 539 edit_pop_undo_action (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 540 {
 541     long c;
 542     unsigned long sp = edit->undo_stack_pointer;
 543 
 544     if (sp == edit->undo_stack_bottom)
 545         return STACK_BOTTOM;
 546 
 547     sp = (sp - 1) & edit->undo_stack_size_mask;
 548     c = edit->undo_stack[sp];
 549     if (c >= 0)
 550     {
 551         //      edit->undo_stack[sp] = '@';
 552         edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
 553         return c;
 554     }
 555 
 556     if (sp == edit->undo_stack_bottom)
 557         return STACK_BOTTOM;
 558 
 559     c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
 560     if (edit->undo_stack[sp] == -2)
 561     {
 562         //      edit->undo_stack[sp] = '@';
 563         edit->undo_stack_pointer = sp;
 564     }
 565     else
 566         edit->undo_stack[sp]++;
 567 
 568     return c;
 569 }
 570 
 571 /* --------------------------------------------------------------------------------------------- */
 572 
 573 static long
 574 edit_pop_redo_action (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 575 {
 576     long c;
 577     unsigned long sp = edit->redo_stack_pointer;
 578 
 579     if (sp == edit->redo_stack_bottom)
 580         return STACK_BOTTOM;
 581 
 582     sp = (sp - 1) & edit->redo_stack_size_mask;
 583     c = edit->redo_stack[sp];
 584     if (c >= 0)
 585     {
 586         edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
 587         return c;
 588     }
 589 
 590     if (sp == edit->redo_stack_bottom)
 591         return STACK_BOTTOM;
 592 
 593     c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
 594     if (edit->redo_stack[sp] == -2)
 595         edit->redo_stack_pointer = sp;
 596     else
 597         edit->redo_stack[sp]++;
 598 
 599     return c;
 600 }
 601 
 602 /* --------------------------------------------------------------------------------------------- */
 603 
 604 static long
 605 get_prev_undo_action (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 606 {
 607     long c;
 608     unsigned long sp = edit->undo_stack_pointer;
 609 
 610     if (sp == edit->undo_stack_bottom)
 611         return STACK_BOTTOM;
 612 
 613     sp = (sp - 1) & edit->undo_stack_size_mask;
 614     c = edit->undo_stack[sp];
 615     if (c >= 0)
 616         return c;
 617 
 618     if (sp == edit->undo_stack_bottom)
 619         return STACK_BOTTOM;
 620 
 621     c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
 622     return c;
 623 }
 624 
 625 /* --------------------------------------------------------------------------------------------- */
 626 /** is called whenever a modification is made by one of the four routines below */
 627 
 628 static void
 629 edit_modification (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 630 {
 631     edit->caches_valid = FALSE;
 632 
 633     // raise lock when file modified
 634     if (edit->modified == 0 && edit->delete_file == 0)
 635         edit->locked = lock_file (edit->filename_vpath);
 636     edit->modified = 1;
 637 }
 638 
 639 /* --------------------------------------------------------------------------------------------- */
 640 /* high level cursor movement commands */
 641 /* --------------------------------------------------------------------------------------------- */
 642 /** check whether cursor is in indent part of line
 643  *
 644  * @param edit editor object
 645  *
 646  * @return TRUE if cursor is in indent, FALSE otherwise
 647  */
 648 
 649 static gboolean
 650 is_in_indent (const edit_buffer_t *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
 651 {
 652     off_t p;
 653 
 654     for (p = edit_buffer_get_current_bol (buf); p < buf->curs1; p++)
 655         if (strchr (" \t", edit_buffer_get_byte (buf, p)) == NULL)
 656             return FALSE;
 657 
 658     return TRUE;
 659 }
 660 
 661 /* --------------------------------------------------------------------------------------------- */
 662 /** check whether line in editor is blank or not
 663  *
 664  * @param edit editor object
 665  * @param offset position in file
 666  *
 667  * @return TRUE if line in blank, FALSE otherwise
 668  */
 669 
 670 static gboolean
 671 is_blank (const edit_buffer_t *buf, off_t offset)
     /* [previous][next][first][last][top][bottom][index][help]  */
 672 {
 673     off_t s, f;
 674 
 675     s = edit_buffer_get_bol (buf, offset);
 676     f = edit_buffer_get_eol (buf, offset);
 677     for (; s < f; s++)
 678     {
 679         int c;
 680 
 681         c = edit_buffer_get_byte (buf, s);
 682         if (!isspace (c))
 683             return FALSE;
 684     }
 685     return TRUE;
 686 }
 687 
 688 /* --------------------------------------------------------------------------------------------- */
 689 /** returns the offset of line i */
 690 
 691 static off_t
 692 edit_find_line (WEdit *edit, long line)
     /* [previous][next][first][last][top][bottom][index][help]  */
 693 {
 694     long i;
 695     long j = 0;
 696     long m = 2000000000;  // what is the magic number?
 697 
 698     if (!edit->caches_valid)
 699     {
 700         memset (edit->line_numbers, 0, sizeof (edit->line_numbers));
 701         memset (edit->line_offsets, 0, sizeof (edit->line_offsets));
 702         // three offsets that we *know* are line 0 at 0 and these two:
 703         edit->line_numbers[1] = edit->buffer.curs_line;
 704         edit->line_offsets[1] = edit_buffer_get_current_bol (&edit->buffer);
 705         edit->line_numbers[2] = edit->buffer.lines;
 706         edit->line_offsets[2] = edit_buffer_get_bol (&edit->buffer, edit->buffer.size);
 707         edit->caches_valid = TRUE;
 708     }
 709     if (line >= edit->buffer.lines)
 710         return edit->line_offsets[2];
 711     if (line <= 0)
 712         return 0;
 713     // find the closest known point
 714     for (i = 0; i < N_LINE_CACHES; i++)
 715     {
 716         long n;
 717 
 718         n = labs (edit->line_numbers[i] - line);
 719         if (n < m)
 720         {
 721             m = n;
 722             j = i;
 723         }
 724     }
 725     if (m == 0)
 726         return edit->line_offsets[j];  // know the offset exactly
 727     if (m == 1 && j >= 3)
 728         i = j;  // one line different - caller might be looping, so stay in this cache
 729     else
 730         i = 3 + (rand () % (N_LINE_CACHES - 3));
 731     if (line > edit->line_numbers[j])
 732         edit->line_offsets[i] = edit_buffer_get_forward_offset (
 733             &edit->buffer, edit->line_offsets[j], line - edit->line_numbers[j], 0);
 734     else
 735         edit->line_offsets[i] = edit_buffer_get_backward_offset (
 736             &edit->buffer, edit->line_offsets[j], edit->line_numbers[j] - line);
 737     edit->line_numbers[i] = line;
 738     return edit->line_offsets[i];
 739 }
 740 
 741 /* --------------------------------------------------------------------------------------------- */
 742 /** moves up until a blank line is reached, or until just
 743    before a non-blank line is reached */
 744 
 745 static void
 746 edit_move_up_paragraph (WEdit *edit, gboolean do_scroll)
     /* [previous][next][first][last][top][bottom][index][help]  */
 747 {
 748     long i = 0;
 749 
 750     if (edit->buffer.curs_line > 1)
 751     {
 752         if (!edit_line_is_blank (edit, edit->buffer.curs_line))
 753         {
 754             for (i = edit->buffer.curs_line - 1; i != 0; i--)
 755                 if (edit_line_is_blank (edit, i))
 756                     break;
 757         }
 758         else if (edit_line_is_blank (edit, edit->buffer.curs_line - 1))
 759         {
 760             for (i = edit->buffer.curs_line - 1; i != 0; i--)
 761                 if (!edit_line_is_blank (edit, i))
 762                 {
 763                     i++;
 764                     break;
 765                 }
 766         }
 767         else
 768         {
 769             for (i = edit->buffer.curs_line - 1; i != 0; i--)
 770                 if (edit_line_is_blank (edit, i))
 771                     break;
 772         }
 773     }
 774 
 775     edit_move_up (edit, edit->buffer.curs_line - i, do_scroll);
 776 }
 777 
 778 /* --------------------------------------------------------------------------------------------- */
 779 /** moves down until a blank line is reached, or until just
 780    before a non-blank line is reached */
 781 
 782 static void
 783 edit_move_down_paragraph (WEdit *edit, gboolean do_scroll)
     /* [previous][next][first][last][top][bottom][index][help]  */
 784 {
 785     long i;
 786 
 787     if (edit->buffer.curs_line >= edit->buffer.lines - 1)
 788         i = edit->buffer.lines;
 789     else if (!edit_line_is_blank (edit, edit->buffer.curs_line))
 790     {
 791         for (i = edit->buffer.curs_line + 1; i != 0; i++)
 792             if (edit_line_is_blank (edit, i) || i >= edit->buffer.lines)
 793                 break;
 794     }
 795     else if (edit_line_is_blank (edit, edit->buffer.curs_line + 1))
 796     {
 797         for (i = edit->buffer.curs_line + 1; i != 0; i++)
 798             if (!edit_line_is_blank (edit, i) || i > edit->buffer.lines)
 799             {
 800                 i--;
 801                 break;
 802             }
 803     }
 804     else
 805     {
 806         for (i = edit->buffer.curs_line + 1; i != 0; i++)
 807             if (edit_line_is_blank (edit, i) || i >= edit->buffer.lines)
 808                 break;
 809     }
 810     edit_move_down (edit, i - edit->buffer.curs_line, do_scroll);
 811 }
 812 
 813 /* --------------------------------------------------------------------------------------------- */
 814 
 815 static void
 816 edit_begin_page (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 817 {
 818     edit_update_curs_row (edit);
 819     edit_move_up (edit, edit->curs_row, FALSE);
 820 }
 821 
 822 /* --------------------------------------------------------------------------------------------- */
 823 
 824 static void
 825 edit_end_page (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 826 {
 827     edit_update_curs_row (edit);
 828     edit_move_down (edit, WIDGET (edit)->rect.lines - edit->curs_row - (edit->fullscreen ? 1 : 3),
 829                     FALSE);
 830 }
 831 
 832 /* --------------------------------------------------------------------------------------------- */
 833 /** goto beginning of text */
 834 
 835 static void
 836 edit_move_to_top (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 837 {
 838     if (edit->buffer.curs_line != 0)
 839     {
 840         edit_cursor_move (edit, -edit->buffer.curs1);
 841         edit_move_to_prev_col (edit, 0);
 842         edit->force |= REDRAW_PAGE;
 843         edit->search_start = 0;
 844         edit_update_curs_row (edit);
 845     }
 846 }
 847 
 848 /* --------------------------------------------------------------------------------------------- */
 849 /** goto end of text */
 850 
 851 static void
 852 edit_move_to_bottom (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 853 {
 854     if (edit->buffer.curs_line < edit->buffer.lines)
 855     {
 856         edit_move_down (edit, edit->buffer.lines - edit->curs_row, FALSE);
 857         edit->start_display = edit->buffer.size;
 858         edit->start_line = edit->buffer.lines;
 859         edit_scroll_upward (edit, WIDGET (edit)->rect.lines - 1);
 860         edit->force |= REDRAW_PAGE;
 861     }
 862 }
 863 
 864 /* --------------------------------------------------------------------------------------------- */
 865 /** goto beginning of line */
 866 
 867 static void
 868 edit_cursor_to_bol (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 869 {
 870     off_t b;
 871 
 872     b = edit_buffer_get_current_bol (&edit->buffer);
 873     edit_cursor_move (edit, b - edit->buffer.curs1);
 874     edit->search_start = edit->buffer.curs1;
 875     edit->prev_col = edit_get_col (edit);
 876     edit->over_col = 0;
 877 }
 878 
 879 /* --------------------------------------------------------------------------------------------- */
 880 /** goto end of line */
 881 
 882 static void
 883 edit_cursor_to_eol (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 884 {
 885     off_t b;
 886 
 887     b = edit_buffer_get_current_eol (&edit->buffer);
 888     edit_cursor_move (edit, b - edit->buffer.curs1);
 889     edit->search_start = edit->buffer.curs1;
 890     edit->prev_col = edit_get_col (edit);
 891     edit->over_col = 0;
 892 }
 893 
 894 /* --------------------------------------------------------------------------------------------- */
 895 
 896 static unsigned long
 897 my_type_of (int c)
     /* [previous][next][first][last][top][bottom][index][help]  */
 898 {
 899     unsigned long r = 0;
 900     const char *q;
 901     const char chars_move_whole_word[] =
 902         "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
 903 
 904     if (c == 0)
 905         return 0;
 906     if (c == '!')
 907         return 2;
 908 
 909     if (g_ascii_isupper ((gchar) c))
 910         c = 'A';
 911     else if (g_ascii_islower ((gchar) c))
 912         c = 'a';
 913     else if (g_ascii_isalpha (c))
 914         c = 'a';
 915     else if (isdigit (c))
 916         c = '0';
 917     else if (isspace (c))
 918         c = ' ';
 919     q = strchr (chars_move_whole_word, c);
 920     if (q == NULL)
 921         return 0xFFFFFFFFUL;
 922 
 923     do
 924     {
 925         unsigned long x;
 926         const char *p;
 927 
 928         for (x = 1, p = chars_move_whole_word; p < q; p++)
 929             if (*p == '!')
 930                 x <<= 1;
 931         r |= x;
 932     }
 933     while ((q = strchr (q + 1, c)) != NULL);
 934 
 935     return r;
 936 }
 937 
 938 /* --------------------------------------------------------------------------------------------- */
 939 
 940 static void
 941 edit_left_word_move (WEdit *edit, int s)
     /* [previous][next][first][last][top][bottom][index][help]  */
 942 {
 943     while (TRUE)
 944     {
 945         int c1, c2;
 946 
 947         if (edit->column_highlight && edit->mark1 != edit->mark2 && edit->over_col == 0
 948             && edit->buffer.curs1 == edit_buffer_get_current_bol (&edit->buffer))
 949             break;
 950         edit_cursor_move (edit, -1);
 951         if (edit->buffer.curs1 == 0)
 952             break;
 953         c1 = edit_buffer_get_previous_byte (&edit->buffer);
 954         if (c1 == '\n')
 955             break;
 956         c2 = edit_buffer_get_current_byte (&edit->buffer);
 957         if (c2 == '\n')
 958             break;
 959         if ((my_type_of (c1) & my_type_of (c2)) == 0)
 960             break;
 961         if (isspace (c1) && !isspace (c2))
 962             break;
 963         if (s != 0 && !isspace (c1) && isspace (c2))
 964             break;
 965     }
 966 }
 967 
 968 /* --------------------------------------------------------------------------------------------- */
 969 
 970 static void
 971 edit_left_word_move_cmd (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 972 {
 973     edit_left_word_move (edit, 0);
 974     edit->force |= REDRAW_PAGE;
 975 }
 976 
 977 /* --------------------------------------------------------------------------------------------- */
 978 
 979 static void
 980 edit_right_word_move (WEdit *edit, int s)
     /* [previous][next][first][last][top][bottom][index][help]  */
 981 {
 982     while (TRUE)
 983     {
 984         int c1, c2;
 985 
 986         if (edit->column_highlight && edit->mark1 != edit->mark2 && edit->over_col == 0
 987             && edit->buffer.curs1 == edit_buffer_get_current_eol (&edit->buffer))
 988             break;
 989         edit_cursor_move (edit, 1);
 990         if (edit->buffer.curs1 >= edit->buffer.size)
 991             break;
 992         c1 = edit_buffer_get_previous_byte (&edit->buffer);
 993         if (c1 == '\n')
 994             break;
 995         c2 = edit_buffer_get_current_byte (&edit->buffer);
 996         if (c2 == '\n')
 997             break;
 998         if ((my_type_of (c1) & my_type_of (c2)) == 0)
 999             break;
1000         if (isspace (c1) && !isspace (c2))
1001             break;
1002         if (s != 0 && !isspace (c1) && isspace (c2))
1003             break;
1004     }
1005 }
1006 
1007 /* --------------------------------------------------------------------------------------------- */
1008 
1009 static void
1010 edit_right_word_move_cmd (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1011 {
1012     edit_right_word_move (edit, 0);
1013     edit->force |= REDRAW_PAGE;
1014 }
1015 
1016 /* --------------------------------------------------------------------------------------------- */
1017 
1018 static void
1019 edit_right_char_move_cmd (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1020 {
1021     int char_length = 1;
1022     int c;
1023 
1024 #ifdef HAVE_CHARSET
1025     if (edit->utf8)
1026     {
1027         c = edit_buffer_get_utf (&edit->buffer, edit->buffer.curs1, &char_length);
1028         if (char_length < 1)
1029             char_length = 1;
1030     }
1031     else
1032 #endif
1033         c = edit_buffer_get_current_byte (&edit->buffer);
1034 
1035     if (edit_options.cursor_beyond_eol && c == '\n')
1036         edit->over_col++;
1037     else
1038         edit_cursor_move (edit, char_length);
1039 }
1040 
1041 /* --------------------------------------------------------------------------------------------- */
1042 
1043 static void
1044 edit_left_char_move_cmd (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1045 {
1046     int char_length = 1;
1047 
1048     if (edit->column_highlight && edit_options.cursor_beyond_eol && edit->mark1 != edit->mark2
1049         && edit->over_col == 0 && edit->buffer.curs1 == edit_buffer_get_current_bol (&edit->buffer))
1050         return;
1051 #ifdef HAVE_CHARSET
1052     if (edit->utf8)
1053     {
1054         edit_buffer_get_prev_utf (&edit->buffer, edit->buffer.curs1, &char_length);
1055         if (char_length < 1)
1056             char_length = 1;
1057     }
1058 #endif
1059 
1060     if (edit_options.cursor_beyond_eol && edit->over_col > 0)
1061         edit->over_col--;
1062     else
1063         edit_cursor_move (edit, -char_length);
1064 }
1065 
1066 /* --------------------------------------------------------------------------------------------- */
1067 /** Up or down cursor moving.
1068  direction = TRUE - move up
1069            = FALSE - move down
1070 */
1071 
1072 static void
1073 edit_move_updown (WEdit *edit, long lines, gboolean do_scroll, gboolean direction)
     /* [previous][next][first][last][top][bottom][index][help]  */
1074 {
1075     long p;
1076     long l = direction ? edit->buffer.curs_line : edit->buffer.lines - edit->buffer.curs_line;
1077 
1078     if (lines > l)
1079         lines = l;
1080 
1081     if (lines == 0)
1082         return;
1083 
1084     if (lines > 1)
1085         edit->force |= REDRAW_PAGE;
1086     if (do_scroll)
1087     {
1088         if (direction)
1089             edit_scroll_upward (edit, lines);
1090         else
1091             edit_scroll_downward (edit, lines);
1092     }
1093     p = edit_buffer_get_current_bol (&edit->buffer);
1094     p = direction ? edit_buffer_get_backward_offset (&edit->buffer, p, lines)
1095                   : edit_buffer_get_forward_offset (&edit->buffer, p, lines, 0);
1096     edit_cursor_move (edit, p - edit->buffer.curs1);
1097     edit_move_to_prev_col (edit, p);
1098 
1099 #ifdef HAVE_CHARSET
1100     // search start of current multibyte char (like CJK)
1101     if (edit->buffer.curs1 > 0 && edit->buffer.curs1 + 1 < edit->buffer.size
1102         && edit_buffer_get_current_byte (&edit->buffer) >= 256)
1103     {
1104         edit_right_char_move_cmd (edit);
1105         edit_left_char_move_cmd (edit);
1106     }
1107 #endif
1108 
1109     edit->search_start = edit->buffer.curs1;
1110     edit->found_len = 0;
1111 }
1112 
1113 /* --------------------------------------------------------------------------------------------- */
1114 
1115 static void
1116 edit_right_delete_word (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1117 {
1118     while (edit->buffer.curs1 < edit->buffer.size)
1119     {
1120         int c1, c2;
1121 
1122         c1 = edit_delete (edit, TRUE);
1123         if (c1 == '\n')
1124             break;
1125         c2 = edit_buffer_get_current_byte (&edit->buffer);
1126         if (c2 == '\n')
1127             break;
1128         if ((isspace (c1) == 0) != (isspace (c2) == 0))
1129             break;
1130         if ((my_type_of (c1) & my_type_of (c2)) == 0)
1131             break;
1132     }
1133 }
1134 
1135 /* --------------------------------------------------------------------------------------------- */
1136 
1137 static void
1138 edit_left_delete_word (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1139 {
1140     while (edit->buffer.curs1 > 0)
1141     {
1142         int c1, c2;
1143 
1144         c1 = edit_backspace (edit, TRUE);
1145         if (c1 == '\n')
1146             break;
1147         c2 = edit_buffer_get_previous_byte (&edit->buffer);
1148         if (c2 == '\n')
1149             break;
1150         if ((isspace (c1) == 0) != (isspace (c2) == 0))
1151             break;
1152         if ((my_type_of (c1) & my_type_of (c2)) == 0)
1153             break;
1154     }
1155 }
1156 
1157 /* --------------------------------------------------------------------------------------------- */
1158 /**
1159    the start column position is not recorded, and hence does not
1160    undo as it happed. But who would notice.
1161  */
1162 
1163 static void
1164 edit_do_undo (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1165 {
1166     long ac;
1167     long count = 0;
1168 
1169     edit->undo_stack_disable = 1;  // don't record undo's onto undo stack!
1170     edit->over_col = 0;
1171 
1172     while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
1173     {
1174         off_t b;
1175 
1176         switch ((int) ac)
1177         {
1178         case STACK_BOTTOM:
1179             goto done_undo;
1180         case CURS_RIGHT:
1181             edit_cursor_move (edit, 1);
1182             break;
1183         case CURS_LEFT:
1184             edit_cursor_move (edit, -1);
1185             break;
1186         case BACKSPACE:
1187         case BACKSPACE_BR:
1188             edit_backspace (edit, TRUE);
1189             break;
1190         case DELCHAR:
1191         case DELCHAR_BR:
1192             edit_delete (edit, TRUE);
1193             break;
1194         case COLUMN_ON:
1195             edit->column_highlight = 1;
1196             break;
1197         case COLUMN_OFF:
1198             edit->column_highlight = 0;
1199             break;
1200         default:
1201             break;
1202         }
1203         if (ac >= 256 && ac < 512)
1204             edit_insert_ahead (edit, ac - 256);
1205         if (ac >= 0 && ac < 256)
1206             edit_insert (edit, ac);
1207 
1208         if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1209         {
1210             edit->mark1 = ac - MARK_1;
1211             b = edit_buffer_get_bol (&edit->buffer, edit->mark1);
1212             edit->column1 = (long) edit_move_forward3 (edit, b, 0, edit->mark1);
1213         }
1214         if (ac >= MARK_2 - 2 && ac < MARK_CURS - 2)
1215         {
1216             edit->mark2 = ac - MARK_2;
1217             b = edit_buffer_get_bol (&edit->buffer, edit->mark2);
1218             edit->column2 = (long) edit_move_forward3 (edit, b, 0, edit->mark2);
1219         }
1220         else if (ac >= MARK_CURS - 2 && ac < KEY_PRESS)
1221         {
1222             edit->end_mark_curs = ac - MARK_CURS;
1223         }
1224         if (count++)
1225             edit->force |= REDRAW_PAGE;  // more than one pop usually means something big
1226     }
1227 
1228     if (edit->start_display > ac - KEY_PRESS)
1229     {
1230         edit->start_line -=
1231             edit_buffer_count_lines (&edit->buffer, ac - KEY_PRESS, edit->start_display);
1232         edit->force |= REDRAW_PAGE;
1233     }
1234     else if (edit->start_display < ac - KEY_PRESS)
1235     {
1236         edit->start_line +=
1237             edit_buffer_count_lines (&edit->buffer, edit->start_display, ac - KEY_PRESS);
1238         edit->force |= REDRAW_PAGE;
1239     }
1240     edit->start_display = ac - KEY_PRESS;  // see push and pop above
1241     edit_update_curs_row (edit);
1242 
1243 done_undo:
1244     edit->undo_stack_disable = 0;
1245 }
1246 
1247 /* --------------------------------------------------------------------------------------------- */
1248 
1249 static void
1250 edit_do_redo (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1251 {
1252     long ac;
1253     long count = 0;
1254 
1255     if (edit->redo_stack_reset)
1256         return;
1257 
1258     edit->over_col = 0;
1259 
1260     while ((ac = edit_pop_redo_action (edit)) < KEY_PRESS)
1261     {
1262         off_t b;
1263 
1264         switch ((int) ac)
1265         {
1266         case STACK_BOTTOM:
1267             goto done_redo;
1268         case CURS_RIGHT:
1269             edit_cursor_move (edit, 1);
1270             break;
1271         case CURS_LEFT:
1272             edit_cursor_move (edit, -1);
1273             break;
1274         case BACKSPACE:
1275             edit_backspace (edit, TRUE);
1276             break;
1277         case DELCHAR:
1278             edit_delete (edit, TRUE);
1279             break;
1280         case COLUMN_ON:
1281             edit->column_highlight = 1;
1282             break;
1283         case COLUMN_OFF:
1284             edit->column_highlight = 0;
1285             break;
1286         default:
1287             break;
1288         }
1289         if (ac >= 256 && ac < 512)
1290             edit_insert_ahead (edit, ac - 256);
1291         if (ac >= 0 && ac < 256)
1292             edit_insert (edit, ac);
1293 
1294         if (ac >= MARK_1 - 2 && ac < MARK_2 - 2)
1295         {
1296             edit->mark1 = ac - MARK_1;
1297             b = edit_buffer_get_bol (&edit->buffer, edit->mark1);
1298             edit->column1 = (long) edit_move_forward3 (edit, b, 0, edit->mark1);
1299         }
1300         else if (ac >= MARK_2 - 2 && ac < KEY_PRESS)
1301         {
1302             edit->mark2 = ac - MARK_2;
1303             b = edit_buffer_get_bol (&edit->buffer, edit->mark2);
1304             edit->column2 = (long) edit_move_forward3 (edit, b, 0, edit->mark2);
1305         }
1306         // more than one pop usually means something big
1307         if (count++ != 0)
1308             edit->force |= REDRAW_PAGE;
1309     }
1310 
1311     if (edit->start_display > ac - KEY_PRESS)
1312     {
1313         edit->start_line -=
1314             edit_buffer_count_lines (&edit->buffer, ac - KEY_PRESS, edit->start_display);
1315         edit->force |= REDRAW_PAGE;
1316     }
1317     else if (edit->start_display < ac - KEY_PRESS)
1318     {
1319         edit->start_line +=
1320             edit_buffer_count_lines (&edit->buffer, edit->start_display, ac - KEY_PRESS);
1321         edit->force |= REDRAW_PAGE;
1322     }
1323     edit->start_display = ac - KEY_PRESS;  // see push and pop above
1324     edit_update_curs_row (edit);
1325 
1326 done_redo:;
1327 }
1328 
1329 /* --------------------------------------------------------------------------------------------- */
1330 
1331 static void
1332 edit_group_undo (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1333 {
1334     long ac = KEY_PRESS;
1335     long cur_ac = KEY_PRESS;
1336 
1337     while (ac != STACK_BOTTOM && ac == cur_ac)
1338     {
1339         cur_ac = get_prev_undo_action (edit);
1340         edit_do_undo (edit);
1341         ac = get_prev_undo_action (edit);
1342         /* exit from cycle if edit_options.group_undo is not set,
1343          * and make single UNDO operation
1344          */
1345         if (!edit_options.group_undo)
1346             ac = STACK_BOTTOM;
1347     }
1348 }
1349 
1350 /* --------------------------------------------------------------------------------------------- */
1351 
1352 static void
1353 edit_delete_to_line_end (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1354 {
1355     while (edit_buffer_get_current_byte (&edit->buffer) != '\n' && edit->buffer.curs2 != 0)
1356         edit_delete (edit, TRUE);
1357 }
1358 
1359 /* --------------------------------------------------------------------------------------------- */
1360 
1361 static void
1362 edit_delete_to_line_begin (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1363 {
1364     while (edit_buffer_get_previous_byte (&edit->buffer) != '\n' && edit->buffer.curs1 != 0)
1365         edit_backspace (edit, TRUE);
1366 }
1367 
1368 /* --------------------------------------------------------------------------------------------- */
1369 
1370 static gboolean
1371 is_aligned_on_a_tab (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1372 {
1373     long curs_col;
1374 
1375     edit_update_curs_col (edit);
1376     curs_col = edit->curs_col % (TAB_SIZE * space_width);
1377     return (curs_col == 0 || curs_col == (HALF_TAB_SIZE * space_width));
1378 }
1379 
1380 /* --------------------------------------------------------------------------------------------- */
1381 
1382 static gboolean
1383 right_of_four_spaces (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1384 {
1385     int i;
1386     int ch = 0;
1387 
1388     for (i = 1; i <= HALF_TAB_SIZE; i++)
1389         ch |= edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 - i);
1390 
1391     return (ch == ' ' && is_aligned_on_a_tab (edit));
1392 }
1393 
1394 /* --------------------------------------------------------------------------------------------- */
1395 
1396 static gboolean
1397 left_of_four_spaces (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1398 {
1399     int i, ch = 0;
1400 
1401     for (i = 0; i < HALF_TAB_SIZE; i++)
1402         ch |= edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 + i);
1403 
1404     return (ch == ' ' && is_aligned_on_a_tab (edit));
1405 }
1406 
1407 /* --------------------------------------------------------------------------------------------- */
1408 
1409 static void
1410 edit_auto_indent (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1411 {
1412     off_t p;
1413 
1414     p = edit->buffer.curs1;
1415     // use the previous line as a template
1416     p = edit_buffer_get_backward_offset (&edit->buffer, p, 1);
1417     // copy the leading whitespace of the line
1418     while (TRUE)
1419     {  // no range check - the line _is_ \n-terminated
1420         char c;
1421 
1422         c = edit_buffer_get_byte (&edit->buffer, p++);
1423         if (!whitespace (c))
1424             break;
1425         edit_insert (edit, c);
1426     }
1427 }
1428 
1429 /* --------------------------------------------------------------------------------------------- */
1430 
1431 static inline void
1432 edit_double_newline (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1433 {
1434     edit_insert (edit, '\n');
1435     if (edit_buffer_get_current_byte (&edit->buffer) == '\n'
1436         || edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 - 2) == '\n')
1437         return;
1438     edit->force |= REDRAW_PAGE;
1439     edit_insert (edit, '\n');
1440 }
1441 
1442 /* --------------------------------------------------------------------------------------------- */
1443 
1444 static void
1445 insert_spaces_tab (WEdit *edit, gboolean half)
     /* [previous][next][first][last][top][bottom][index][help]  */
1446 {
1447     long i;
1448 
1449     edit_update_curs_col (edit);
1450     i = TAB_SIZE * space_width;
1451     if (half)
1452         i /= 2;
1453     if (i != 0)
1454         for (i = ((edit->curs_col / i) + 1) * i - edit->curs_col; i > 0; i -= space_width)
1455             edit_insert (edit, ' ');
1456 }
1457 
1458 /* --------------------------------------------------------------------------------------------- */
1459 
1460 static inline void
1461 edit_tab_cmd (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1462 {
1463     if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer))
1464     {
1465         /* insert a half tab (usually four spaces) unless there is a
1466            half tab already behind, then delete it and insert a
1467            full tab. */
1468         if (edit_options.fill_tabs_with_spaces || !right_of_four_spaces (edit))
1469             insert_spaces_tab (edit, TRUE);
1470         else
1471         {
1472             int i;
1473 
1474             for (i = 1; i <= HALF_TAB_SIZE; i++)
1475                 edit_backspace (edit, TRUE);
1476             edit_insert (edit, '\t');
1477         }
1478     }
1479     else if (edit_options.fill_tabs_with_spaces)
1480         insert_spaces_tab (edit, FALSE);
1481     else
1482         edit_insert (edit, '\t');
1483 }
1484 
1485 /* --------------------------------------------------------------------------------------------- */
1486 
1487 static void
1488 check_and_wrap_line (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1489 {
1490     off_t curs;
1491 
1492     if (!edit_options.typewriter_wrap)
1493         return;
1494     edit_update_curs_col (edit);
1495     if (edit->curs_col < edit_options.word_wrap_line_length)
1496         return;
1497     curs = edit->buffer.curs1;
1498     while (TRUE)
1499     {
1500         int c;
1501 
1502         curs--;
1503         c = edit_buffer_get_byte (&edit->buffer, curs);
1504         if (c == '\n' || curs <= 0)
1505         {
1506             edit_insert (edit, '\n');
1507             return;
1508         }
1509         if (whitespace (c))
1510         {
1511             off_t current = edit->buffer.curs1;
1512             edit_cursor_move (edit, curs - edit->buffer.curs1 + 1);
1513             edit_insert (edit, '\n');
1514             edit_cursor_move (edit, current - edit->buffer.curs1 + 1);
1515             return;
1516         }
1517     }
1518 }
1519 
1520 /* --------------------------------------------------------------------------------------------- */
1521 /** this find the matching bracket in either direction, and sets edit->bracket
1522  *
1523  * @param edit editor object
1524  * @param in_screen search only on the current screen
1525  * @param furthest_bracket_search count of the bytes for search
1526  *
1527  * @return position of the found bracket (-1 if no match)
1528  */
1529 
1530 static off_t
1531 edit_get_bracket (WEdit *edit, gboolean in_screen, unsigned long furthest_bracket_search)
     /* [previous][next][first][last][top][bottom][index][help]  */
1532 {
1533     const char *const b = "{}{[][()(", *p;
1534     int i = 1, inc = -1, c, d, n = 0;
1535     unsigned long j = 0;
1536     off_t q;
1537 
1538     edit_update_curs_row (edit);
1539     c = edit_buffer_get_current_byte (&edit->buffer);
1540     p = strchr (b, c);
1541     // not on a bracket at all
1542     if (p == NULL || *p == '\0')
1543         return -1;
1544     // the matching bracket
1545     d = p[1];
1546     // going left or right?
1547     if (strchr ("{[(", c) != NULL)
1548         inc = 1;
1549     // no limit
1550     if (furthest_bracket_search == 0)
1551         furthest_bracket_search--;  // ULONG_MAX
1552     for (q = edit->buffer.curs1 + inc;; q += inc)
1553     {
1554         int a;
1555 
1556         // out of buffer?
1557         if (q >= edit->buffer.size || q < 0)
1558             break;
1559         a = edit_buffer_get_byte (&edit->buffer, q);
1560         // don't want to eat CPU
1561         if (j++ > furthest_bracket_search)
1562             break;
1563         // out of screen?
1564         if (in_screen)
1565         {
1566             if (q < edit->start_display)
1567                 break;
1568             // count lines if searching downward
1569             if (inc > 0 && a == '\n')
1570                 if (n++ >= WIDGET (edit)->rect.lines - edit->curs_row)  // out of screen
1571                     break;
1572         }
1573         // count bracket depth
1574         i += (a == c) - (a == d);
1575         // return if bracket depth is zero
1576         if (i == 0)
1577             return q;
1578     }
1579     // no match
1580     return -1;
1581 }
1582 
1583 /* --------------------------------------------------------------------------------------------- */
1584 
1585 static inline void
1586 edit_goto_matching_bracket (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1587 {
1588     off_t q;
1589 
1590     q = edit_get_bracket (edit, 0, 0);
1591     if (q >= 0)
1592     {
1593         edit->bracket = edit->buffer.curs1;
1594         edit->force |= REDRAW_PAGE;
1595         edit_cursor_move (edit, q - edit->buffer.curs1);
1596     }
1597 }
1598 
1599 /* --------------------------------------------------------------------------------------------- */
1600 
1601 static void
1602 edit_move_block_to_right (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1603 {
1604     off_t start_mark, end_mark;
1605     long cur_bol, start_bol;
1606 
1607     if (!eval_marks (edit, &start_mark, &end_mark))
1608         return;
1609 
1610     start_bol = edit_buffer_get_bol (&edit->buffer, start_mark);
1611     cur_bol = edit_buffer_get_bol (&edit->buffer, end_mark - 1);
1612 
1613     do
1614     {
1615         off_t b;
1616 
1617         edit_cursor_move (edit, cur_bol - edit->buffer.curs1);
1618         if (!edit_line_is_blank (edit, edit->buffer.curs_line))
1619         {
1620             if (edit_options.fill_tabs_with_spaces)
1621                 insert_spaces_tab (edit, edit_options.fake_half_tabs);
1622             else
1623                 edit_insert (edit, '\t');
1624 
1625             b = edit_buffer_get_bol (&edit->buffer, cur_bol);
1626             edit_cursor_move (edit, b - edit->buffer.curs1);
1627         }
1628 
1629         if (cur_bol == 0)
1630             break;
1631 
1632         cur_bol = edit_buffer_get_bol (&edit->buffer, cur_bol - 1);
1633     }
1634     while (cur_bol >= start_bol);
1635 
1636     edit->force |= REDRAW_PAGE;
1637 }
1638 
1639 /* --------------------------------------------------------------------------------------------- */
1640 
1641 static void
1642 edit_move_block_to_left (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1643 {
1644     off_t start_mark, end_mark;
1645     off_t cur_bol, start_bol;
1646 
1647     if (!eval_marks (edit, &start_mark, &end_mark))
1648         return;
1649 
1650     start_bol = edit_buffer_get_bol (&edit->buffer, start_mark);
1651     cur_bol = edit_buffer_get_bol (&edit->buffer, end_mark - 1);
1652 
1653     do
1654     {
1655         int del_tab_width;
1656         int next_char;
1657 
1658         edit_cursor_move (edit, cur_bol - edit->buffer.curs1);
1659 
1660         del_tab_width = edit_options.fake_half_tabs ? HALF_TAB_SIZE : TAB_SIZE;
1661 
1662         next_char = edit_buffer_get_current_byte (&edit->buffer);
1663         if (next_char == '\t')
1664             edit_delete (edit, TRUE);
1665         else if (next_char == ' ')
1666         {
1667             int i;
1668 
1669             for (i = 0; i < del_tab_width; i++)
1670             {
1671                 if (next_char == ' ')
1672                     edit_delete (edit, TRUE);
1673                 next_char = edit_buffer_get_current_byte (&edit->buffer);
1674             }
1675         }
1676 
1677         if (cur_bol == 0)
1678             break;
1679 
1680         cur_bol = edit_buffer_get_bol (&edit->buffer, cur_bol - 1);
1681     }
1682     while (cur_bol >= start_bol);
1683 
1684     edit->force |= REDRAW_PAGE;
1685 }
1686 
1687 /* --------------------------------------------------------------------------------------------- */
1688 /**
1689  * prints at the cursor
1690  * @return number of chars printed
1691  */
1692 
1693 static size_t
1694 edit_print_string (WEdit *e, const char *s)
     /* [previous][next][first][last][top][bottom][index][help]  */
1695 {
1696     size_t i;
1697 
1698     for (i = 0; s[i] != '\0'; i++)
1699         edit_execute_cmd (e, CK_InsertChar, (unsigned char) s[i]);
1700     e->force |= REDRAW_COMPLETELY;
1701     edit_update_screen (e);
1702     return i;
1703 }
1704 
1705 /* --------------------------------------------------------------------------------------------- */
1706 
1707 static off_t
1708 edit_insert_column_from_file (WEdit *edit, int file, off_t *start_pos, off_t *end_pos, long *col1,
     /* [previous][next][first][last][top][bottom][index][help]  */
1709                               long *col2)
1710 {
1711     off_t cursor;
1712     long col;
1713     off_t blocklen = -1, width = 0;
1714     unsigned char *data;
1715 
1716     cursor = edit->buffer.curs1;
1717     col = edit_get_col (edit);
1718     data = g_malloc0 (TEMP_BUF_LEN);
1719 
1720     while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
1721     {
1722         off_t i;
1723         char *pn;
1724 
1725         pn = strchr ((char *) data, '\n');
1726         width = pn == NULL ? blocklen : pn - (char *) data;
1727 
1728         for (i = 0; i < blocklen; i++)
1729         {
1730             if (data[i] != '\n')
1731                 edit_insert (edit, data[i]);
1732             else
1733             {  // fill in and move to next line
1734                 long l;
1735                 off_t p;
1736 
1737                 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
1738                     for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1739                         edit_insert (edit, ' ');
1740 
1741                 for (p = edit->buffer.curs1;; p++)
1742                 {
1743                     if (p == edit->buffer.size)
1744                     {
1745                         edit_cursor_move (edit, edit->buffer.size - edit->buffer.curs1);
1746                         edit_insert_ahead (edit, '\n');
1747                         p++;
1748                         break;
1749                     }
1750                     if (edit_buffer_get_byte (&edit->buffer, p) == '\n')
1751                     {
1752                         p++;
1753                         break;
1754                     }
1755                 }
1756 
1757                 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->buffer.curs1);
1758 
1759                 for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1760                     edit_insert (edit, ' ');
1761             }
1762         }
1763     }
1764     *col1 = col;
1765     *col2 = col + width;
1766     *start_pos = cursor;
1767     *end_pos = edit->buffer.curs1;
1768     edit_cursor_move (edit, cursor - edit->buffer.curs1);
1769     g_free (data);
1770 
1771     return blocklen;
1772 }
1773 
1774 /* --------------------------------------------------------------------------------------------- */
1775 /*** public functions ****************************************************************************/
1776 /* --------------------------------------------------------------------------------------------- */
1777 
1778 /** User edit menu, like user menu (F2) but only in editor. */
1779 
1780 void
1781 edit_user_menu (WEdit *edit, const char *menu_file, int selected_entry)
     /* [previous][next][first][last][top][bottom][index][help]  */
1782 {
1783     char *block_file;
1784     gboolean mark;
1785     off_t curs;
1786     off_t start_mark, end_mark;
1787     struct stat status;
1788     vfs_path_t *block_file_vpath;
1789 
1790     block_file = mc_config_get_full_path (EDIT_HOME_BLOCK_FILE);
1791     block_file_vpath = vfs_path_from_str (block_file);
1792     curs = edit->buffer.curs1;
1793     mark = eval_marks (edit, &start_mark, &end_mark);
1794     if (mark)
1795         edit_save_block (edit, block_file, start_mark, end_mark);
1796 
1797     // run shell scripts from menu
1798     if (user_menu_cmd (CONST_WIDGET (edit), menu_file, selected_entry)
1799         && (mc_stat (block_file_vpath, &status) == 0) && (status.st_size != 0))
1800     {
1801         gboolean rc = TRUE;
1802         FILE *fd;
1803 
1804         // i.e. we have marked block
1805         if (mark)
1806             rc = edit_block_delete_cmd (edit);
1807 
1808         if (rc)
1809         {
1810             off_t ins_len;
1811 
1812             ins_len = edit_insert_file (edit, block_file_vpath);
1813             if (mark && ins_len > 0)
1814                 edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0);
1815         }
1816         // truncate block file
1817         fd = fopen (block_file, "w");
1818         if (fd != NULL)
1819             fclose (fd);
1820     }
1821     g_free (block_file);
1822     vfs_path_free (block_file_vpath, TRUE);
1823 
1824     edit_cursor_move (edit, curs - edit->buffer.curs1);
1825     edit->force |= REDRAW_PAGE;
1826     widget_draw (WIDGET (edit));
1827 }
1828 
1829 /* --------------------------------------------------------------------------------------------- */
1830 
1831 char *
1832 edit_get_write_filter (const vfs_path_t *write_name_vpath, const vfs_path_t *filename_vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1833 {
1834     int i;
1835     const char *write_name;
1836     char *write_name_quoted;
1837     char *p = NULL;
1838 
1839     i = edit_find_filter (filename_vpath);
1840     if (i < 0)
1841         return NULL;
1842 
1843     write_name = vfs_path_get_last_path_str (write_name_vpath);
1844     write_name_quoted = name_quote (write_name, FALSE);
1845     if (write_name_quoted != NULL)
1846     {
1847         p = g_strdup_printf (all_filters[i].write, write_name_quoted);
1848         g_free (write_name_quoted);
1849     }
1850     return p;
1851 }
1852 
1853 /* --------------------------------------------------------------------------------------------- */
1854 /**
1855  * @param edit   editor object
1856  * @param f      value of stream file
1857  * @return       the length of the file
1858  */
1859 
1860 off_t
1861 edit_write_stream (WEdit *edit, FILE *f)
     /* [previous][next][first][last][top][bottom][index][help]  */
1862 {
1863     long i;
1864 
1865     if (edit->lb == LB_ASIS)
1866     {
1867         for (i = 0; i < edit->buffer.size; i++)
1868             if (fputc (edit_buffer_get_byte (&edit->buffer, i), f) < 0)
1869                 break;
1870         return i;
1871     }
1872 
1873     // change line breaks
1874     for (i = 0; i < edit->buffer.size; i++)
1875     {
1876         unsigned char c;
1877 
1878         c = edit_buffer_get_byte (&edit->buffer, i);
1879         if (!(c == '\n' || c == '\r'))
1880         {
1881             // not line break
1882             if (fputc (c, f) < 0)
1883                 return i;
1884         }
1885         else
1886         {  // (c == '\n' || c == '\r')
1887             unsigned char c1;
1888 
1889             c1 = edit_buffer_get_byte (&edit->buffer, i + 1);  // next char
1890 
1891             switch (edit->lb)
1892             {
1893             case LB_UNIX:  // replace "\r\n" or '\r' to '\n'
1894                 // put one line break unconditionally
1895                 if (fputc ('\n', f) < 0)
1896                     return i;
1897 
1898                 i++;  // 2 chars are processed
1899 
1900                 if (c == '\r' && c1 == '\n')
1901                     // Windows line break; go to the next char
1902                     break;
1903 
1904                 if (c == '\r' && c1 == '\r')
1905                 {
1906                     // two Macintosh line breaks; put second line break
1907                     if (fputc ('\n', f) < 0)
1908                         return i;
1909                     break;
1910                 }
1911 
1912                 if (fputc (c1, f) < 0)
1913                     return i;
1914                 break;
1915 
1916             case LB_WIN:  // replace '\n' or '\r' to "\r\n"
1917                 // put one line break unconditionally
1918                 if (fputc ('\r', f) < 0 || fputc ('\n', f) < 0)
1919                     return i;
1920 
1921                 if (c == '\r' && c1 == '\n')
1922                     // Windows line break; go to the next char
1923                     i++;
1924                 break;
1925 
1926             case LB_MAC:  // replace "\r\n" or '\n' to '\r'
1927                 // put one line break unconditionally
1928                 if (fputc ('\r', f) < 0)
1929                     return i;
1930 
1931                 i++;  // 2 chars are processed
1932 
1933                 if (c == '\r' && c1 == '\n')
1934                     // Windows line break; go to the next char
1935                     break;
1936 
1937                 if (c == '\n' && c1 == '\n')
1938                 {
1939                     // two Windows line breaks; put second line break
1940                     if (fputc ('\r', f) < 0)
1941                         return i;
1942                     break;
1943                 }
1944 
1945                 if (fputc (c1, f) < 0)
1946                     return i;
1947                 break;
1948             case LB_ASIS:  // default without changes
1949             default:
1950                 break;
1951             }
1952         }
1953     }
1954 
1955     return edit->buffer.size;
1956 }
1957 
1958 /* --------------------------------------------------------------------------------------------- */
1959 
1960 gboolean
1961 is_break_char (char c)
     /* [previous][next][first][last][top][bottom][index][help]  */
1962 {
1963     return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c) != NULL);
1964 }
1965 
1966 /* --------------------------------------------------------------------------------------------- */
1967 /** inserts a file at the cursor, returns count of inserted bytes on success */
1968 
1969 off_t
1970 edit_insert_file (WEdit *edit, const vfs_path_t *filename_vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1971 {
1972     char *p;
1973     off_t current;
1974     off_t ins_len = 0;
1975 
1976     p = edit_get_filter (filename_vpath);
1977     current = edit->buffer.curs1;
1978 
1979     if (p != NULL)
1980     {
1981         FILE *f;
1982 
1983         f = (FILE *) popen (p, "r");
1984         if (f != NULL)
1985         {
1986             edit_insert_stream (edit, f);
1987 
1988             // Place cursor at the end of text selection
1989             if (!edit_options.cursor_after_inserted_block)
1990             {
1991                 ins_len = edit->buffer.curs1 - current;
1992                 edit_cursor_move (edit, -ins_len);
1993             }
1994             if (pclose (f) > 0)
1995             {
1996                 message (D_ERROR, MSG_ERROR, _ ("Error reading from pipe: %s"), p);
1997                 ins_len = -1;
1998             }
1999         }
2000         else
2001         {
2002             message (D_ERROR, MSG_ERROR, _ ("Cannot open pipe for reading: %s"), p);
2003             ins_len = -1;
2004         }
2005         g_free (p);
2006     }
2007     else
2008     {
2009         int file;
2010         off_t blocklen;
2011         gboolean vertical_insertion = FALSE;
2012         char *buf;
2013 
2014         file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
2015         if (file == -1)
2016             return -1;
2017 
2018         buf = g_malloc0 (TEMP_BUF_LEN);
2019         blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
2020         if (blocklen > 0)
2021         {
2022             // if contain signature VERTICAL_MAGIC then it vertical block
2023             if (memcmp (buf, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)) == 0)
2024                 vertical_insertion = TRUE;
2025             else
2026                 mc_lseek (file, 0, SEEK_SET);
2027         }
2028 
2029         if (vertical_insertion)
2030         {
2031             off_t mark1, mark2;
2032             long c1, c2;
2033 
2034             blocklen = edit_insert_column_from_file (edit, file, &mark1, &mark2, &c1, &c2);
2035             edit_set_markers (edit, edit->buffer.curs1, mark2, c1, c2);
2036 
2037             // highlight inserted text then not persistent blocks
2038             if (!edit_options.persistent_selections && edit->modified != 0)
2039             {
2040                 if (edit->column_highlight == 0)
2041                     edit_push_undo_action (edit, COLUMN_OFF);
2042                 edit->column_highlight = 1;
2043             }
2044         }
2045         else
2046         {
2047             off_t i;
2048 
2049             while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0)
2050             {
2051                 for (i = 0; i < blocklen; i++)
2052                     edit_insert (edit, buf[i]);
2053             }
2054             // highlight inserted text then not persistent blocks
2055             if (!edit_options.persistent_selections && edit->modified != 0)
2056             {
2057                 edit_set_markers (edit, edit->buffer.curs1, current, 0, 0);
2058                 if (edit->column_highlight != 0)
2059                     edit_push_undo_action (edit, COLUMN_ON);
2060                 edit->column_highlight = 0;
2061             }
2062 
2063             // Place cursor at the end of text selection
2064             if (!edit_options.cursor_after_inserted_block)
2065             {
2066                 ins_len = edit->buffer.curs1 - current;
2067                 edit_cursor_move (edit, -ins_len);
2068             }
2069         }
2070 
2071         edit->force |= REDRAW_PAGE;
2072         g_free (buf);
2073         mc_close (file);
2074         if (blocklen != 0)
2075             ins_len = 0;
2076     }
2077 
2078     return ins_len;
2079 }
2080 
2081 /* --------------------------------------------------------------------------------------------- */
2082 /**
2083  * Fill in the edit structure.  Return NULL on failure.  Pass edit as
2084  * NULL to allocate a new structure.
2085  *
2086  * If arg is NULL or arg->line_number is 0, try to restore saved position.  Otherwise put the
2087  * cursor on that line and show it in the middle of the screen.
2088  */
2089 
2090 WEdit *
2091 edit_init (WEdit *edit, const WRect *r, const edit_arg_t *arg)
     /* [previous][next][first][last][top][bottom][index][help]  */
2092 {
2093     gboolean to_free = FALSE;
2094     long line;
2095 
2096     auto_syntax = TRUE;  // Resetting to auto on every invocation
2097     edit_options.line_state_width = edit_options.line_state ? LINE_STATE_WIDTH : 0;
2098 
2099     if (edit != NULL)
2100     {
2101         int fullscreen;
2102         WRect loc_prev;
2103 
2104         // save some widget parameters
2105         fullscreen = edit->fullscreen;
2106         loc_prev = edit->loc_prev;
2107 
2108         edit_purge_widget (edit);
2109 
2110         // restore saved parameters
2111         edit->fullscreen = fullscreen;
2112         edit->loc_prev = loc_prev;
2113     }
2114     else
2115     {
2116         Widget *w;
2117 
2118         edit = g_malloc0 (sizeof (WEdit));
2119         to_free = TRUE;
2120 
2121         w = WIDGET (edit);
2122         widget_init (w, r, NULL, NULL);
2123         w->options |= WOP_SELECTABLE | WOP_TOP_SELECT | WOP_WANT_CURSOR;
2124         w->keymap = editor_map;
2125         w->ext_keymap = editor_x_map;
2126         edit->fullscreen = 1;
2127         edit_save_size (edit);
2128     }
2129 
2130     edit->drag_state = MCEDIT_DRAG_NONE;
2131 
2132     edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2133     edit->stat1.st_uid = getuid ();
2134     edit->stat1.st_gid = getgid ();
2135     edit->stat1.st_mtime = 0;
2136 
2137     if (arg != NULL)
2138         edit->attrs_ok = (mc_fgetflags (arg->file_vpath, &edit->attrs) == 0);
2139     else
2140         edit->attrs_ok = FALSE;
2141 
2142     edit->over_col = 0;
2143     edit->bracket = -1;
2144     edit->last_bracket = -1;
2145     edit->force |= REDRAW_PAGE;
2146 
2147     // set file name before load file
2148     if (arg != NULL)
2149     {
2150         edit_set_filename (edit, arg->file_vpath);
2151         line = arg->line_number;
2152     }
2153     else
2154     {
2155         edit_set_filename (edit, NULL);
2156         line = 0;
2157     }
2158 
2159     edit->undo_stack_size = START_STACK_SIZE;
2160     edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2161     edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2162 
2163     edit->redo_stack_size = START_STACK_SIZE;
2164     edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2165     edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2166 
2167 #ifdef HAVE_CHARSET
2168     edit->utf8 = FALSE;
2169     edit->converter = str_cnv_from_term;
2170     edit_set_codeset (edit);
2171 #endif
2172 
2173     if (!edit_load_file (edit))
2174     {
2175         // edit_load_file already gives an error message
2176         if (to_free)
2177             g_free (edit);
2178         return NULL;
2179     }
2180 
2181     edit->loading_done = 1;
2182     edit->modified = 0;
2183     edit->locked = 0;
2184     edit_load_syntax (edit, NULL, NULL);
2185     edit_get_syntax_color (edit, -1);
2186 
2187     // load saved cursor position and/or boolmarks
2188     if ((line == 0) && edit_options.save_position)
2189         edit_load_position (edit, TRUE);
2190     else
2191     {
2192         edit_load_position (edit, FALSE);
2193         if (line <= 0)
2194             line = 1;
2195         edit_move_display (edit, line - 1);
2196         edit_move_to_line (edit, line - 1);
2197     }
2198 
2199     edit_load_macro_cmd (edit);
2200 
2201     return edit;
2202 }
2203 
2204 /* --------------------------------------------------------------------------------------------- */
2205 
2206 /** Clear the edit struct, freeing everything in it.  Return TRUE on success */
2207 gboolean
2208 edit_clean (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2209 {
2210     if (edit == NULL)
2211         return FALSE;
2212 
2213     // a stale lock, remove it
2214     if (edit->locked)
2215         edit->locked = unlock_file (edit->filename_vpath);
2216 
2217     // save cursor position
2218     if (edit_options.save_position)
2219         edit_save_position (edit);
2220     else if (edit->serialized_bookmarks != NULL)
2221         g_array_free (edit->serialized_bookmarks, TRUE);
2222 
2223     // File specified on the mcedit command line and never saved
2224     if (edit->delete_file != 0)
2225         unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2226 
2227     edit_free_syntax_rules (edit);
2228     book_mark_flush (edit, -1);
2229 
2230     edit_buffer_clean (&edit->buffer);
2231 
2232     g_free (edit->undo_stack);
2233     g_free (edit->redo_stack);
2234     vfs_path_free (edit->filename_vpath, TRUE);
2235     vfs_path_free (edit->dir_vpath, TRUE);
2236     edit_search_deinit (edit);
2237 
2238 #ifdef HAVE_CHARSET
2239     if (edit->converter != str_cnv_from_term)
2240         str_close_conv (edit->converter);
2241 #endif
2242 
2243     edit_purge_widget (edit);
2244 
2245     return TRUE;
2246 }
2247 
2248 /* --------------------------------------------------------------------------------------------- */
2249 
2250 /**
2251  * Load a new file into the editor and set line.  If it fails, preserve the old file.
2252  * To do it, allocate a new widget, initialize it and, if the new file
2253  * was loaded, copy the data to the old widget.
2254  *
2255  * @return TRUE on success, FALSE on failure.
2256  */
2257 gboolean
2258 edit_reload_line (WEdit *edit, const edit_arg_t *arg)
     /* [previous][next][first][last][top][bottom][index][help]  */
2259 {
2260     Widget *w = WIDGET (edit);
2261     WEdit *e;
2262 
2263     e = g_malloc0 (sizeof (WEdit));
2264     *WIDGET (e) = *w;
2265     // save some widget parameters
2266     e->fullscreen = edit->fullscreen;
2267     e->loc_prev = edit->loc_prev;
2268 
2269     if (edit_init (e, &w->rect, arg) == NULL)
2270     {
2271         g_free (e);
2272         return FALSE;
2273     }
2274 
2275     edit_clean (edit);
2276     memcpy (edit, e, sizeof (*edit));
2277     g_free (e);
2278 
2279     return TRUE;
2280 }
2281 
2282 /* --------------------------------------------------------------------------------------------- */
2283 
2284 #ifdef HAVE_CHARSET
2285 void
2286 edit_set_codeset (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2287 {
2288     const char *cp_id;
2289 
2290     cp_id = get_codepage_id (mc_global.source_codepage >= 0 ? mc_global.source_codepage
2291                                                             : mc_global.display_codepage);
2292 
2293     if (cp_id != NULL)
2294     {
2295         GIConv conv;
2296         conv = str_crt_conv_from (cp_id);
2297         if (conv != INVALID_CONV)
2298         {
2299             if (edit->converter != str_cnv_from_term)
2300                 str_close_conv (edit->converter);
2301             edit->converter = conv;
2302         }
2303     }
2304 
2305     if (cp_id != NULL)
2306         edit->utf8 = str_isutf8 (cp_id);
2307 }
2308 #endif
2309 
2310 /* --------------------------------------------------------------------------------------------- */
2311 
2312 /**
2313  * Recording stack for undo:
2314  * The following is an implementation of a compressed stack. Identical
2315  * pushes are recorded by a negative prefix indicating the number of times the
2316  * same char was pushed. This saves space for repeated curs-left or curs-right
2317  * delete etc.
2318  *
2319  * eg:
2320  *
2321  * pushed:       stored:
2322  *
2323  * a
2324  * b             a
2325  * b            -3
2326  * b             b
2327  * c  -->       -4
2328  * c             c
2329  * c             d
2330  * c
2331  * d
2332  *
2333  * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2334  * 256-512 is an insert ahead (from a delete), If it is between 600 and 700 it is one
2335  * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2336  * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2337  * position.
2338  *
2339  * The only way the cursor moves or the buffer is changed is through the routines:
2340  * insert, backspace, insert_ahead, delete, and cursor_move.
2341  * These record the reverse undo movements onto the stack each time they are
2342  * called.
2343  *
2344  * Each key press results in a set of actions (insert; delete ...). So each time
2345  * a key is pressed the current position of start_display is pushed as
2346  * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2347  * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2348  * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2349  *
2350  *
2351  *
2352  * @param edit editor object
2353  * @param c code of the action
2354  */
2355 
2356 void
2357 edit_push_undo_action (WEdit *edit, long c)
     /* [previous][next][first][last][top][bottom][index][help]  */
2358 {
2359     unsigned long sp = edit->undo_stack_pointer;
2360     unsigned long spm1;
2361 
2362     // first enlarge the stack if necessary
2363     if (sp > edit->undo_stack_size - 10)
2364     {  // say
2365         if (max_undo < 256)
2366             max_undo = 256;
2367         if (edit->undo_stack_size < (unsigned long) max_undo)
2368         {
2369             long *t;
2370 
2371             t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2372             if (t != NULL)
2373             {
2374                 edit->undo_stack = t;
2375                 edit->undo_stack_size <<= 1;
2376                 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2377             }
2378         }
2379     }
2380     spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2381     if (edit->undo_stack_disable)
2382     {
2383         edit_push_redo_action (edit, KEY_PRESS);
2384         edit_push_redo_action (edit, c);
2385         return;
2386     }
2387 
2388     if (edit->redo_stack_reset)
2389         edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2390 
2391     if (edit->undo_stack_bottom != sp && spm1 != edit->undo_stack_bottom
2392         && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2393     {
2394         long d;
2395 
2396         if (edit->undo_stack[spm1] < 0)
2397         {
2398             d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2399             if (d == c && edit->undo_stack[spm1] > -1000000000)
2400             {
2401                 if (c < KEY_PRESS)  // --> no need to push multiple do-nothings
2402                     edit->undo_stack[spm1]--;
2403                 return;
2404             }
2405         }
2406         else
2407         {
2408             d = edit->undo_stack[spm1];
2409             if (d == c)
2410             {
2411                 if (c >= KEY_PRESS)
2412                     return;  // --> no need to push multiple do-nothings
2413                 edit->undo_stack[sp] = -2;
2414                 goto check_bottom;
2415             }
2416         }
2417     }
2418     edit->undo_stack[sp] = c;
2419 
2420 check_bottom:
2421     edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2422 
2423     /* if the sp wraps round and catches the undo_stack_bottom then erase
2424      * the first set of actions on the stack to make space - by moving
2425      * undo_stack_bottom forward one "key press" */
2426     c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2427     if ((unsigned long) c == edit->undo_stack_bottom
2428         || (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2429         do
2430         {
2431             edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2432         }
2433         while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2434                && edit->undo_stack_bottom != edit->undo_stack_pointer);
2435 
2436     // If a single key produced enough pushes to wrap all the way round then we would notice that
2437     // the [undo_stack_bottom] does not contain KEY_PRESS. The stack is then initialised:
2438     if (edit->undo_stack_pointer != edit->undo_stack_bottom
2439         && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2440     {
2441         edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2442     }
2443 }
2444 
2445 /* --------------------------------------------------------------------------------------------- */
2446 
2447 void
2448 edit_push_redo_action (WEdit *edit, long c)
     /* [previous][next][first][last][top][bottom][index][help]  */
2449 {
2450     unsigned long sp = edit->redo_stack_pointer;
2451     unsigned long spm1;
2452     // first enlarge the stack if necessary
2453     if (sp > edit->redo_stack_size - 10)
2454     {  // say
2455         if (max_undo < 256)
2456             max_undo = 256;
2457         if (edit->redo_stack_size < (unsigned long) max_undo)
2458         {
2459             long *t;
2460 
2461             t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2462             if (t != NULL)
2463             {
2464                 edit->redo_stack = t;
2465                 edit->redo_stack_size <<= 1;
2466                 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2467             }
2468         }
2469     }
2470     spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2471 
2472     if (edit->redo_stack_bottom != sp && spm1 != edit->redo_stack_bottom
2473         && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2474     {
2475         long d;
2476 
2477         if (edit->redo_stack[spm1] < 0)
2478         {
2479             d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2480             if (d == c && edit->redo_stack[spm1] > -1000000000)
2481             {
2482                 if (c < KEY_PRESS)  // --> no need to push multiple do-nothings
2483                     edit->redo_stack[spm1]--;
2484                 return;
2485             }
2486         }
2487         else
2488         {
2489             d = edit->redo_stack[spm1];
2490             if (d == c)
2491             {
2492                 if (c >= KEY_PRESS)
2493                     return;  // --> no need to push multiple do-nothings
2494                 edit->redo_stack[sp] = -2;
2495                 goto redo_check_bottom;
2496             }
2497         }
2498     }
2499     edit->redo_stack[sp] = c;
2500 
2501 redo_check_bottom:
2502     edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2503 
2504     /* if the sp wraps round and catches the redo_stack_bottom then erase
2505      * the first set of actions on the stack to make space - by moving
2506      * redo_stack_bottom forward one "key press" */
2507     c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2508     if ((unsigned long) c == edit->redo_stack_bottom
2509         || (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2510         do
2511         {
2512             edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2513         }
2514         while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2515                && edit->redo_stack_bottom != edit->redo_stack_pointer);
2516 
2517     /*
2518      * If a single key produced enough pushes to wrap all the way round then
2519      * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2520      * The stack is then initialised:
2521      */
2522 
2523     if (edit->redo_stack_pointer != edit->redo_stack_bottom
2524         && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2525         edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2526 }
2527 
2528 /* --------------------------------------------------------------------------------------------- */
2529 /**
2530    Basic low level single character buffer alterations and movements at the cursor.
2531  */
2532 
2533 void
2534 edit_insert (WEdit *edit, int c)
     /* [previous][next][first][last][top][bottom][index][help]  */
2535 {
2536     // first we must update the position of the display window
2537     if (edit->buffer.curs1 < edit->start_display)
2538     {
2539         edit->start_display++;
2540         if (c == '\n')
2541             edit->start_line++;
2542     }
2543 
2544     // Mark file as modified, unless the file hasn't been fully loaded
2545     if (edit->loading_done != 0)
2546         edit_modification (edit);
2547 
2548     // now we must update some info on the file and check if a redraw is required
2549     if (c == '\n')
2550     {
2551         book_mark_inc (edit, edit->buffer.curs_line);
2552         edit->buffer.curs_line++;
2553         edit->buffer.lines++;
2554         edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2555     }
2556 
2557     // save the reverse command onto the undo stack
2558     // ordinary char and not space
2559     if (c > 32)
2560         edit_push_undo_action (edit, BACKSPACE);
2561     else
2562         edit_push_undo_action (edit, BACKSPACE_BR);
2563     // update markers
2564     edit->mark1 += (edit->mark1 > edit->buffer.curs1) ? 1 : 0;
2565     edit->mark2 += (edit->mark2 > edit->buffer.curs1) ? 1 : 0;
2566     edit->last_get_rule += (edit->last_get_rule > edit->buffer.curs1) ? 1 : 0;
2567 
2568     edit_buffer_insert (&edit->buffer, c);
2569 }
2570 
2571 /* --------------------------------------------------------------------------------------------- */
2572 /** same as edit_insert and move left */
2573 
2574 void
2575 edit_insert_ahead (WEdit *edit, int c)
     /* [previous][next][first][last][top][bottom][index][help]  */
2576 {
2577     if (edit->buffer.curs1 < edit->start_display)
2578     {
2579         edit->start_display++;
2580         if (c == '\n')
2581             edit->start_line++;
2582     }
2583     edit_modification (edit);
2584     if (c == '\n')
2585     {
2586         book_mark_inc (edit, edit->buffer.curs_line);
2587         edit->buffer.lines++;
2588         edit->force |= REDRAW_AFTER_CURSOR;
2589     }
2590     // ordinary char and not space
2591     if (c > 32)
2592         edit_push_undo_action (edit, DELCHAR);
2593     else
2594         edit_push_undo_action (edit, DELCHAR_BR);
2595 
2596     edit->mark1 += (edit->mark1 >= edit->buffer.curs1) ? 1 : 0;
2597     edit->mark2 += (edit->mark2 >= edit->buffer.curs1) ? 1 : 0;
2598     edit->last_get_rule += (edit->last_get_rule >= edit->buffer.curs1) ? 1 : 0;
2599 
2600     edit_buffer_insert_ahead (&edit->buffer, c);
2601 }
2602 
2603 /* --------------------------------------------------------------------------------------------- */
2604 
2605 void
2606 edit_insert_over (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2607 {
2608     long i;
2609 
2610     for (i = 0; i < edit->over_col; i++)
2611         edit_insert (edit, ' ');
2612 
2613     edit->over_col = 0;
2614 }
2615 
2616 /* --------------------------------------------------------------------------------------------- */
2617 
2618 int
2619 edit_delete (WEdit *edit, gboolean byte_delete)
     /* [previous][next][first][last][top][bottom][index][help]  */
2620 {
2621     int p = 0;
2622     int char_length = 1;
2623     int i;
2624 
2625     if (edit->buffer.curs2 == 0)
2626         return 0;
2627 
2628 #ifdef HAVE_CHARSET
2629     // if byte_delete == TRUE then delete only one byte not multibyte char
2630     if (edit->utf8 && !byte_delete)
2631     {
2632         edit_buffer_get_utf (&edit->buffer, edit->buffer.curs1, &char_length);
2633         if (char_length < 1)
2634             char_length = 1;
2635     }
2636 #else
2637     (void) byte_delete;
2638 #endif
2639 
2640     if (edit->mark2 != edit->mark1)
2641         edit_push_markers (edit);
2642 
2643     for (i = 1; i <= char_length; i++)
2644     {
2645         if (edit->mark1 > edit->buffer.curs1)
2646         {
2647             edit->mark1--;
2648             edit->end_mark_curs--;
2649         }
2650         if (edit->mark2 > edit->buffer.curs1)
2651             edit->mark2--;
2652         if (edit->last_get_rule > edit->buffer.curs1)
2653             edit->last_get_rule--;
2654 
2655         p = edit_buffer_delete (&edit->buffer);
2656 
2657         edit_push_undo_action (edit, p + 256);
2658     }
2659 
2660     edit_modification (edit);
2661     if (p == '\n')
2662     {
2663         book_mark_dec (edit, edit->buffer.curs_line);
2664         edit->buffer.lines--;
2665         edit->force |= REDRAW_AFTER_CURSOR;
2666     }
2667     if (edit->buffer.curs1 < edit->start_display)
2668     {
2669         edit->start_display--;
2670         if (p == '\n')
2671             edit->start_line--;
2672     }
2673 
2674     return p;
2675 }
2676 
2677 /* --------------------------------------------------------------------------------------------- */
2678 
2679 int
2680 edit_backspace (WEdit *edit, gboolean byte_delete)
     /* [previous][next][first][last][top][bottom][index][help]  */
2681 {
2682     int p = 0;
2683     int char_length = 1;
2684     int i;
2685 
2686     if (edit->buffer.curs1 == 0)
2687         return 0;
2688 
2689     if (edit->mark2 != edit->mark1)
2690         edit_push_markers (edit);
2691 
2692 #ifdef HAVE_CHARSET
2693     if (edit->utf8 && !byte_delete)
2694     {
2695         edit_buffer_get_prev_utf (&edit->buffer, edit->buffer.curs1, &char_length);
2696         if (char_length < 1)
2697             char_length = 1;
2698     }
2699 #else
2700     (void) byte_delete;
2701 #endif
2702 
2703     for (i = 1; i <= char_length; i++)
2704     {
2705         if (edit->mark1 >= edit->buffer.curs1)
2706         {
2707             edit->mark1--;
2708             edit->end_mark_curs--;
2709         }
2710         if (edit->mark2 >= edit->buffer.curs1)
2711             edit->mark2--;
2712         if (edit->last_get_rule >= edit->buffer.curs1)
2713             edit->last_get_rule--;
2714 
2715         p = edit_buffer_backspace (&edit->buffer);
2716 
2717         edit_push_undo_action (edit, p);
2718     }
2719     edit_modification (edit);
2720     if (p == '\n')
2721     {
2722         book_mark_dec (edit, edit->buffer.curs_line);
2723         edit->buffer.curs_line--;
2724         edit->buffer.lines--;
2725         edit->force |= REDRAW_AFTER_CURSOR;
2726     }
2727 
2728     if (edit->buffer.curs1 < edit->start_display)
2729     {
2730         edit->start_display--;
2731         if (p == '\n')
2732             edit->start_line--;
2733     }
2734 
2735     return p;
2736 }
2737 
2738 /* --------------------------------------------------------------------------------------------- */
2739 /** moves the cursor right or left: increment positive or negative respectively */
2740 
2741 void
2742 edit_cursor_move (WEdit *edit, off_t increment)
     /* [previous][next][first][last][top][bottom][index][help]  */
2743 {
2744     if (increment < 0)
2745     {
2746         for (; increment < 0 && edit->buffer.curs1 != 0; increment++)
2747         {
2748             int c;
2749 
2750             edit_push_undo_action (edit, CURS_RIGHT);
2751 
2752             c = edit_buffer_get_previous_byte (&edit->buffer);
2753             edit_buffer_insert_ahead (&edit->buffer, c);
2754             c = edit_buffer_backspace (&edit->buffer);
2755             if (c == '\n')
2756             {
2757                 edit->buffer.curs_line--;
2758                 edit->force |= REDRAW_LINE_BELOW;
2759             }
2760         }
2761     }
2762     else
2763     {
2764         for (; increment > 0 && edit->buffer.curs2 != 0; increment--)
2765         {
2766             int c;
2767 
2768             edit_push_undo_action (edit, CURS_LEFT);
2769 
2770             c = edit_buffer_get_current_byte (&edit->buffer);
2771             edit_buffer_insert (&edit->buffer, c);
2772             c = edit_buffer_delete (&edit->buffer);
2773             if (c == '\n')
2774             {
2775                 edit->buffer.curs_line++;
2776                 edit->force |= REDRAW_LINE_ABOVE;
2777             }
2778         }
2779     }
2780 }
2781 
2782 /* --------------------------------------------------------------------------------------------- */
2783 /* If cols is zero this returns the count of columns from current to upto. */
2784 /* If upto is zero returns index of cols across from current. */
2785 
2786 off_t
2787 edit_move_forward3 (const WEdit *edit, off_t current, long cols, off_t upto)
     /* [previous][next][first][last][top][bottom][index][help]  */
2788 {
2789     off_t p, q;
2790     long col;
2791 
2792     if (upto != 0)
2793     {
2794         q = upto;
2795         cols = -10;
2796     }
2797     else
2798         q = edit->buffer.size + 2;
2799 
2800     for (col = 0, p = current; p < q; p++)
2801     {
2802         int c, orig_c;
2803 
2804         if (cols != -10)
2805         {
2806             if (col == cols)
2807                 return p;
2808             if (col > cols)
2809                 return p - 1;
2810         }
2811 
2812         orig_c = c = edit_buffer_get_byte (&edit->buffer, p);
2813 
2814 #ifdef HAVE_CHARSET
2815         if (edit->utf8)
2816         {
2817             int utf_ch;
2818             int char_length = 1;
2819 
2820             utf_ch = edit_buffer_get_utf (&edit->buffer, p, &char_length);
2821             if (mc_global.utf8_display)
2822             {
2823                 if (char_length > 1)
2824                     col -= char_length - 1;
2825                 if (g_unichar_iswide (utf_ch))
2826                     col++;
2827             }
2828             else if (char_length > 1 && g_unichar_isprint (utf_ch))
2829                 col -= char_length - 1;
2830         }
2831 
2832         c = convert_to_display_c (c);
2833 #endif
2834 
2835         if (c == '\n')
2836             return (upto != 0 ? (off_t) col : p);
2837         if (c == '\t')
2838             col += TAB_SIZE - col % TAB_SIZE;
2839         else if ((c < 32 || c == 127)
2840                  && (orig_c == c
2841 #ifdef HAVE_CHARSET
2842                      || (!mc_global.utf8_display && !edit->utf8)
2843 #endif
2844                          ))
2845             // '\r' is shown as ^M, so we must advance 2 characters
2846             // Caret notation for control characters
2847             col += 2;
2848         else
2849             col++;
2850     }
2851     return (off_t) col;
2852 }
2853 
2854 /* --------------------------------------------------------------------------------------------- */
2855 /** returns the current offset of the cursor from the beginning of a file */
2856 
2857 off_t
2858 edit_get_cursor_offset (const WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2859 {
2860     return edit->buffer.curs1;
2861 }
2862 
2863 /* --------------------------------------------------------------------------------------------- */
2864 /** returns the current column position of the cursor */
2865 
2866 long
2867 edit_get_col (const WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2868 {
2869     off_t b;
2870 
2871     b = edit_buffer_get_current_bol (&edit->buffer);
2872     return (long) edit_move_forward3 (edit, b, 0, edit->buffer.curs1);
2873 }
2874 
2875 /* --------------------------------------------------------------------------------------------- */
2876 /* Scrolling functions */
2877 /* --------------------------------------------------------------------------------------------- */
2878 
2879 void
2880 edit_update_curs_row (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2881 {
2882     edit->curs_row = edit->buffer.curs_line - edit->start_line;
2883 }
2884 
2885 /* --------------------------------------------------------------------------------------------- */
2886 
2887 void
2888 edit_update_curs_col (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2889 {
2890     off_t b;
2891 
2892     b = edit_buffer_get_current_bol (&edit->buffer);
2893     edit->curs_col = (long) edit_move_forward3 (edit, b, 0, edit->buffer.curs1);
2894 }
2895 
2896 /* --------------------------------------------------------------------------------------------- */
2897 
2898 long
2899 edit_get_curs_col (const WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2900 {
2901     return edit->curs_col;
2902 }
2903 
2904 /* --------------------------------------------------------------------------------------------- */
2905 /** moves the display start position up by i lines */
2906 
2907 void
2908 edit_scroll_upward (WEdit *edit, long i)
     /* [previous][next][first][last][top][bottom][index][help]  */
2909 {
2910     long lines_above = edit->start_line;
2911 
2912     if (i > lines_above)
2913         i = lines_above;
2914     if (i != 0)
2915     {
2916         edit->start_line -= i;
2917         edit->start_display =
2918             edit_buffer_get_backward_offset (&edit->buffer, edit->start_display, i);
2919         edit->force |= REDRAW_PAGE;
2920         edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2921     }
2922     edit_update_curs_row (edit);
2923 }
2924 
2925 /* --------------------------------------------------------------------------------------------- */
2926 
2927 void
2928 edit_scroll_downward (WEdit *edit, long i)
     /* [previous][next][first][last][top][bottom][index][help]  */
2929 {
2930     long lines_below;
2931 
2932     lines_below = edit->buffer.lines - edit->start_line - (WIDGET (edit)->rect.lines - 1);
2933     if (lines_below > 0)
2934     {
2935         if (i > lines_below)
2936             i = lines_below;
2937         edit->start_line += i;
2938         edit->start_display =
2939             edit_buffer_get_forward_offset (&edit->buffer, edit->start_display, i, 0);
2940         edit->force |= REDRAW_PAGE;
2941         edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2942     }
2943     edit_update_curs_row (edit);
2944 }
2945 
2946 /* --------------------------------------------------------------------------------------------- */
2947 
2948 void
2949 edit_scroll_right (WEdit *edit, long i)
     /* [previous][next][first][last][top][bottom][index][help]  */
2950 {
2951     edit->force |= REDRAW_PAGE;
2952     edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2953     edit->start_col -= i;
2954 }
2955 
2956 /* --------------------------------------------------------------------------------------------- */
2957 
2958 void
2959 edit_scroll_left (WEdit *edit, long i)
     /* [previous][next][first][last][top][bottom][index][help]  */
2960 {
2961     if (edit->start_col)
2962     {
2963         edit->start_col += i;
2964         if (edit->start_col > 0)
2965             edit->start_col = 0;
2966         edit->force |= REDRAW_PAGE;
2967         edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2968     }
2969 }
2970 
2971 /* --------------------------------------------------------------------------------------------- */
2972 /* high level cursor movement commands */
2973 /* --------------------------------------------------------------------------------------------- */
2974 
2975 void
2976 edit_move_to_prev_col (WEdit *edit, off_t p)
     /* [previous][next][first][last][top][bottom][index][help]  */
2977 {
2978     long prev = edit->prev_col;
2979     long over = edit->over_col;
2980     off_t b;
2981 
2982     edit_cursor_move (edit,
2983                       edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->buffer.curs1);
2984 
2985     if (edit_options.cursor_beyond_eol)
2986     {
2987         off_t e;
2988         long line_len;
2989 
2990         b = edit_buffer_get_current_bol (&edit->buffer);
2991         e = edit_buffer_get_current_eol (&edit->buffer);
2992         line_len = (long) edit_move_forward3 (edit, b, 0, e);
2993         if (line_len < prev + edit->over_col)
2994         {
2995             edit->over_col = prev + over - line_len;
2996             edit->prev_col = line_len;
2997             edit->curs_col = line_len;
2998         }
2999         else
3000         {
3001             edit->curs_col = prev + over;
3002             edit->prev_col = edit->curs_col;
3003             edit->over_col = 0;
3004         }
3005     }
3006     else
3007     {
3008         edit->over_col = 0;
3009         if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer))
3010         {
3011             long fake_half_tabs;
3012 
3013             edit_update_curs_col (edit);
3014 
3015             fake_half_tabs = HALF_TAB_SIZE * space_width;
3016             if (fake_half_tabs != 0 && edit->curs_col % fake_half_tabs != 0)
3017             {
3018                 long q;
3019 
3020                 q = edit->curs_col;
3021                 edit->curs_col -= (edit->curs_col % fake_half_tabs);
3022                 p = edit_buffer_get_current_bol (&edit->buffer);
3023                 b = edit_move_forward3 (edit, p, edit->curs_col, 0);
3024                 edit_cursor_move (edit, b - edit->buffer.curs1);
3025                 if (!left_of_four_spaces (edit))
3026                 {
3027                     b = edit_move_forward3 (edit, p, q, 0);
3028                     edit_cursor_move (edit, b - edit->buffer.curs1);
3029                 }
3030             }
3031         }
3032     }
3033 }
3034 
3035 /* --------------------------------------------------------------------------------------------- */
3036 /** check whether line in editor is blank or not
3037  *
3038  * @param edit editor object
3039  * @param line number of line
3040  *
3041  * @return TRUE if line in blank, FALSE otherwise
3042  */
3043 
3044 gboolean
3045 edit_line_is_blank (WEdit *edit, long line)
     /* [previous][next][first][last][top][bottom][index][help]  */
3046 {
3047     return is_blank (&edit->buffer, edit_find_line (edit, line));
3048 }
3049 
3050 /* --------------------------------------------------------------------------------------------- */
3051 /** move cursor to line 'line' */
3052 
3053 void
3054 edit_move_to_line (WEdit *e, long line)
     /* [previous][next][first][last][top][bottom][index][help]  */
3055 {
3056     if (line < e->buffer.curs_line)
3057         edit_move_up (e, e->buffer.curs_line - line, FALSE);
3058     else
3059         edit_move_down (e, line - e->buffer.curs_line, FALSE);
3060     edit_scroll_screen_over_cursor (e);
3061 }
3062 
3063 /* --------------------------------------------------------------------------------------------- */
3064 /** scroll window so that first visible line is 'line' */
3065 
3066 void
3067 edit_move_display (WEdit *e, long line)
     /* [previous][next][first][last][top][bottom][index][help]  */
3068 {
3069     if (line < e->start_line)
3070         edit_scroll_upward (e, e->start_line - line);
3071     else
3072         edit_scroll_downward (e, line - e->start_line);
3073 }
3074 
3075 /* --------------------------------------------------------------------------------------------- */
3076 /** save markers onto undo stack */
3077 
3078 void
3079 edit_push_markers (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3080 {
3081     edit_push_undo_action (edit, MARK_1 + edit->mark1);
3082     edit_push_undo_action (edit, MARK_2 + edit->mark2);
3083     edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3084 }
3085 
3086 /* --------------------------------------------------------------------------------------------- */
3087 
3088 void
3089 edit_set_markers (WEdit *edit, off_t m1, off_t m2, long c1, long c2)
     /* [previous][next][first][last][top][bottom][index][help]  */
3090 {
3091     edit->mark1 = m1;
3092     edit->mark2 = m2;
3093     edit->column1 = c1;
3094     edit->column2 = c2;
3095 }
3096 
3097 /* --------------------------------------------------------------------------------------------- */
3098 /**
3099    if mark2 is -1 then marking is from mark1 to the cursor.
3100    Otherwise its between the markers. This handles this.
3101    Returns FALSE if no text is marked.
3102  */
3103 
3104 gboolean
3105 eval_marks (WEdit *edit, off_t *start_mark, off_t *end_mark)
     /* [previous][next][first][last][top][bottom][index][help]  */
3106 {
3107     long end_mark_curs;
3108 
3109     if (edit->mark1 == edit->mark2)
3110     {
3111         *start_mark = *end_mark = 0;
3112         edit->column2 = edit->column1 = 0;
3113         return FALSE;
3114     }
3115 
3116     if (edit->end_mark_curs < 0)
3117         end_mark_curs = edit->buffer.curs1;
3118     else
3119         end_mark_curs = edit->end_mark_curs;
3120 
3121     if (edit->mark2 >= 0)
3122     {
3123         *start_mark = MIN (edit->mark1, edit->mark2);
3124         *end_mark = MAX (edit->mark1, edit->mark2);
3125     }
3126     else
3127     {
3128         *start_mark = MIN (edit->mark1, end_mark_curs);
3129         *end_mark = MAX (edit->mark1, end_mark_curs);
3130         edit->column2 = edit->curs_col + edit->over_col;
3131     }
3132 
3133     if (edit->column_highlight != 0
3134         && ((edit->mark1 > end_mark_curs && edit->column1 < edit->column2)
3135             || (edit->mark1 < end_mark_curs && edit->column1 > edit->column2)))
3136     {
3137         off_t start_bol, start_eol;
3138         off_t end_bol, end_eol;
3139         long col1, col2;
3140         off_t diff1, diff2;
3141 
3142         start_bol = edit_buffer_get_bol (&edit->buffer, *start_mark);
3143         start_eol = edit_buffer_get_eol (&edit->buffer, start_bol - 1) + 1;
3144         end_bol = edit_buffer_get_bol (&edit->buffer, *end_mark);
3145         end_eol = edit_buffer_get_eol (&edit->buffer, *end_mark);
3146         col1 = MIN (edit->column1, edit->column2);
3147         col2 = MAX (edit->column1, edit->column2);
3148 
3149         diff1 = edit_move_forward3 (edit, start_bol, col2, 0)
3150             - edit_move_forward3 (edit, start_bol, col1, 0);
3151         diff2 = edit_move_forward3 (edit, end_bol, col2, 0)
3152             - edit_move_forward3 (edit, end_bol, col1, 0);
3153 
3154         *start_mark -= diff1;
3155         *end_mark += diff2;
3156         *start_mark = MAX (*start_mark, start_eol);
3157         *end_mark = MIN (*end_mark, end_eol);
3158     }
3159 
3160     return TRUE;
3161 }
3162 
3163 /* --------------------------------------------------------------------------------------------- */
3164 /** highlight marker toggle */
3165 
3166 void
3167 edit_mark_cmd (WEdit *edit, gboolean unmark)
     /* [previous][next][first][last][top][bottom][index][help]  */
3168 {
3169     edit_push_markers (edit);
3170     if (unmark)
3171     {
3172         edit_set_markers (edit, 0, 0, 0, 0);
3173         edit->force |= REDRAW_PAGE;
3174     }
3175     else if (edit->mark2 >= 0)
3176     {
3177         edit->end_mark_curs = -1;
3178         edit_set_markers (edit, edit->buffer.curs1, -1, edit->curs_col + edit->over_col,
3179                           edit->curs_col + edit->over_col);
3180         edit->force |= REDRAW_PAGE;
3181     }
3182     else
3183     {
3184         edit->end_mark_curs = edit->buffer.curs1;
3185         edit_set_markers (edit, edit->mark1, edit->buffer.curs1, edit->column1,
3186                           edit->curs_col + edit->over_col);
3187     }
3188 }
3189 
3190 /* --------------------------------------------------------------------------------------------- */
3191 /** highlight the word under cursor */
3192 
3193 void
3194 edit_mark_current_word_cmd (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3195 {
3196     long pos;
3197 
3198     for (pos = edit->buffer.curs1; pos != 0; pos--)
3199     {
3200         int c1, c2;
3201 
3202         c1 = edit_buffer_get_byte (&edit->buffer, pos);
3203         c2 = edit_buffer_get_byte (&edit->buffer, pos - 1);
3204         if (!isspace (c1) && isspace (c2))
3205             break;
3206         if ((my_type_of (c1) & my_type_of (c2)) == 0)
3207             break;
3208     }
3209     edit->mark1 = pos;
3210 
3211     for (; pos < edit->buffer.size; pos++)
3212     {
3213         int c1, c2;
3214 
3215         c1 = edit_buffer_get_byte (&edit->buffer, pos);
3216         c2 = edit_buffer_get_byte (&edit->buffer, pos + 1);
3217         if (!isspace (c1) && isspace (c2))
3218             break;
3219         if ((my_type_of (c1) & my_type_of (c2)) == 0)
3220             break;
3221     }
3222     edit->mark2 = MIN (pos + 1, edit->buffer.size);
3223 
3224     edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3225 }
3226 
3227 /* --------------------------------------------------------------------------------------------- */
3228 
3229 void
3230 edit_mark_current_line_cmd (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3231 {
3232     edit->mark1 = edit_buffer_get_current_bol (&edit->buffer);
3233     edit->mark2 = edit_buffer_get_current_eol (&edit->buffer);
3234 
3235     edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3236 }
3237 
3238 /* --------------------------------------------------------------------------------------------- */
3239 
3240 void
3241 edit_delete_line (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3242 {
3243     /*
3244      * Delete right part of the line.
3245      * Note that edit_buffer_get_byte() returns '\n' when byte position is
3246      *   beyond EOF.
3247      */
3248     while (edit_buffer_get_current_byte (&edit->buffer) != '\n')
3249         (void) edit_delete (edit, TRUE);
3250 
3251     /*
3252      * Delete '\n' char.
3253      * Note that edit_delete() will not corrupt anything if called while
3254      *   cursor position is EOF.
3255      */
3256     (void) edit_delete (edit, TRUE);
3257 
3258     /*
3259      * Delete left part of the line.
3260      * Note, that edit_buffer_get_byte() returns '\n' when byte position is < 0.
3261      */
3262     while (edit_buffer_get_previous_byte (&edit->buffer) != '\n')
3263         (void) edit_backspace (edit, TRUE);
3264 }
3265 
3266 /* --------------------------------------------------------------------------------------------- */
3267 
3268 void
3269 edit_push_key_press (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3270 {
3271     edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3272     if (edit->mark2 == -1)
3273     {
3274         edit_push_undo_action (edit, MARK_1 + edit->mark1);
3275         edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3276     }
3277 }
3278 
3279 /* --------------------------------------------------------------------------------------------- */
3280 
3281 void
3282 edit_find_bracket (WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3283 {
3284     edit->bracket = edit_get_bracket (edit, 1, 10000);
3285     if (edit->last_bracket != edit->bracket)
3286         edit->force |= REDRAW_PAGE;
3287     edit->last_bracket = edit->bracket;
3288 }
3289 
3290 /* --------------------------------------------------------------------------------------------- */
3291 /**
3292  * This executes a command as though the user initiated it through a key
3293  * press.  Callback with MSG_KEY as a message calls this after
3294  * translating the key press.  This function can be used to pass any
3295  * command to the editor.  Note that the screen wouldn't update
3296  * automatically.  Either of command or char_for_insertion must be
3297  * passed as -1.  Commands are executed, and char_for_insertion is
3298  * inserted at the cursor.
3299  */
3300 
3301 void
3302 edit_execute_key_command (WEdit *edit, long command, int char_for_insertion)
     /* [previous][next][first][last][top][bottom][index][help]  */
3303 {
3304     if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3305         || (macro_index < 0
3306             && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3307     {
3308         macro_index = 0;
3309         edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3310         return;
3311     }
3312     if (macro_index != -1)
3313     {
3314         edit->force |= REDRAW_COMPLETELY;
3315         if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3316         {
3317             edit_store_macro_cmd (edit);
3318             macro_index = -1;
3319             return;
3320         }
3321         if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3322         {
3323             edit_repeat_macro_cmd (edit);
3324             macro_index = -1;
3325             return;
3326         }
3327     }
3328 
3329     if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3330     {
3331         record_macro_buf[macro_index].action = command;
3332         record_macro_buf[macro_index++].ch = char_for_insertion;
3333     }
3334     // record the beginning of a set of editing actions initiated by a key press
3335     if (command != CK_Undo && command != CK_ExtendedKeyMap)
3336         edit_push_key_press (edit);
3337 
3338     edit_execute_cmd (edit, command, char_for_insertion);
3339     if (edit->column_highlight != 0)
3340         edit->force |= REDRAW_PAGE;
3341 }
3342 
3343 /* --------------------------------------------------------------------------------------------- */
3344 /**
3345    This executes a command at a lower level than macro recording.
3346    It also does not push a key_press onto the undo stack. This means
3347    that if it is called many times, a single undo command will undo
3348    all of them. It also does not check for the Undo command.
3349  */
3350 void
3351 edit_execute_cmd (WEdit *edit, long command, int char_for_insertion)
     /* [previous][next][first][last][top][bottom][index][help]  */
3352 {
3353     WRect *w = &WIDGET (edit)->rect;
3354 
3355     if (command == CK_WindowFullscreen)
3356     {
3357         edit_toggle_fullscreen (edit);
3358         return;
3359     }
3360 
3361     // handle window state
3362     if (edit_handle_move_resize (edit, command))
3363         return;
3364 
3365     edit->force |= REDRAW_LINE;
3366 
3367     /* The next key press will unhighlight the found string, so update
3368      * the whole page */
3369     if (edit->found_len != 0 || edit->column_highlight != 0)
3370         edit->force |= REDRAW_PAGE;
3371 
3372     switch (command)
3373     {
3374         // a mark command with shift-arrow
3375     case CK_MarkLeft:
3376     case CK_MarkRight:
3377     case CK_MarkToWordBegin:
3378     case CK_MarkToWordEnd:
3379     case CK_MarkToHome:
3380     case CK_MarkToEnd:
3381     case CK_MarkUp:
3382     case CK_MarkDown:
3383     case CK_MarkPageUp:
3384     case CK_MarkPageDown:
3385     case CK_MarkToFileBegin:
3386     case CK_MarkToFileEnd:
3387     case CK_MarkToPageBegin:
3388     case CK_MarkToPageEnd:
3389     case CK_MarkScrollUp:
3390     case CK_MarkScrollDown:
3391     case CK_MarkParagraphUp:
3392     case CK_MarkParagraphDown:
3393         // a mark command with alt-arrow
3394     case CK_MarkColumnPageUp:
3395     case CK_MarkColumnPageDown:
3396     case CK_MarkColumnLeft:
3397     case CK_MarkColumnRight:
3398     case CK_MarkColumnUp:
3399     case CK_MarkColumnDown:
3400     case CK_MarkColumnScrollUp:
3401     case CK_MarkColumnScrollDown:
3402     case CK_MarkColumnParagraphUp:
3403     case CK_MarkColumnParagraphDown:
3404         edit->column_highlight = 0;
3405         if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3406         {
3407             edit_mark_cmd (edit, TRUE);   // clear
3408             edit_mark_cmd (edit, FALSE);  // marking on
3409         }
3410         edit->highlight = 1;
3411         break;
3412 
3413         // any other command
3414     default:
3415         if (edit->highlight != 0)
3416             edit_mark_cmd (edit, FALSE);  // clear
3417         edit->highlight = 0;
3418     }
3419 
3420     // first check for undo
3421     if (command == CK_Undo)
3422     {
3423         edit->redo_stack_reset = 0;
3424         edit_group_undo (edit);
3425         edit->found_len = 0;
3426         edit->prev_col = edit_get_col (edit);
3427         edit->search_start = edit->buffer.curs1;
3428         return;
3429     }
3430     //  check for redo
3431     if (command == CK_Redo)
3432     {
3433         edit->redo_stack_reset = 0;
3434         edit_do_redo (edit);
3435         edit->found_len = 0;
3436         edit->prev_col = edit_get_col (edit);
3437         edit->search_start = edit->buffer.curs1;
3438         return;
3439     }
3440 
3441     edit->redo_stack_reset = 1;
3442 
3443     // An ordinary key press
3444     if (char_for_insertion >= 0)
3445     {
3446         // if non persistent selection and text selected
3447         if (!edit_options.persistent_selections && edit->mark1 != edit->mark2)
3448             edit_block_delete_cmd (edit);
3449 
3450         if (edit->overwrite != 0)
3451         {
3452             // remove char only one time, after input first byte, multibyte chars
3453 #ifdef HAVE_CHARSET
3454             if (!mc_global.utf8_display || edit->charpoint == 0)
3455 #endif
3456                 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
3457                     edit_delete (edit, FALSE);
3458         }
3459         if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3460             edit_insert_over (edit);
3461 #ifdef HAVE_CHARSET
3462         /**
3463            Encode 8-bit input as UTF-8, if display (locale) is *not* UTF-8,
3464            *but* source encoding *is* set to UTF-8; see ticket #3843 for the details.
3465         */
3466         if (char_for_insertion > 127 && str_isutf8 (get_codepage_id (mc_global.source_codepage))
3467             && !mc_global.utf8_display)
3468         {
3469             unsigned char str[UTF8_CHAR_LEN + 1];
3470             size_t i;
3471             int res;
3472 
3473             res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3474             if (res == 0)
3475             {
3476                 str[0] = '.';
3477                 str[1] = '\0';
3478             }
3479             else
3480                 str[res] = '\0';
3481 
3482             for (i = 0; i <= UTF8_CHAR_LEN && str[i] != '\0'; i++)
3483             {
3484                 char_for_insertion = str[i];
3485                 edit_insert (edit, char_for_insertion);
3486             }
3487         }
3488         else
3489 #endif
3490             edit_insert (edit, char_for_insertion);
3491 
3492         if (edit_options.auto_para_formatting)
3493         {
3494             format_paragraph (edit, FALSE);
3495             edit->force |= REDRAW_PAGE;
3496         }
3497         else
3498             check_and_wrap_line (edit);
3499         edit->found_len = 0;
3500         edit->prev_col = edit_get_col (edit);
3501         edit->search_start = edit->buffer.curs1;
3502         edit_find_bracket (edit);
3503         return;
3504     }
3505 
3506     switch (command)
3507     {
3508     case CK_TopOnScreen:
3509     case CK_BottomOnScreen:
3510     case CK_Top:
3511     case CK_Bottom:
3512     case CK_PageUp:
3513     case CK_PageDown:
3514     case CK_Home:
3515     case CK_End:
3516     case CK_Up:
3517     case CK_Down:
3518     case CK_Left:
3519     case CK_Right:
3520     case CK_WordLeft:
3521     case CK_WordRight:
3522         if (!edit_options.persistent_selections && edit->mark2 >= 0)
3523         {
3524             if (edit->column_highlight != 0)
3525                 edit_push_undo_action (edit, COLUMN_ON);
3526             edit->column_highlight = 0;
3527             edit_mark_cmd (edit, TRUE);
3528         }
3529         break;
3530     default:
3531         break;
3532     }
3533 
3534     switch (command)
3535     {
3536     case CK_TopOnScreen:
3537     case CK_BottomOnScreen:
3538     case CK_MarkToPageBegin:
3539     case CK_MarkToPageEnd:
3540     case CK_Up:
3541     case CK_Down:
3542     case CK_WordLeft:
3543     case CK_WordRight:
3544     case CK_MarkToWordBegin:
3545     case CK_MarkToWordEnd:
3546     case CK_MarkUp:
3547     case CK_MarkDown:
3548     case CK_MarkColumnUp:
3549     case CK_MarkColumnDown:
3550         if (edit->mark2 == -1)
3551             break;  // marking is following the cursor: may need to highlight a whole line
3552         MC_FALLTHROUGH;
3553     case CK_Left:
3554     case CK_Right:
3555     case CK_MarkLeft:
3556     case CK_MarkRight:
3557         edit->force |= REDRAW_CHAR_ONLY;
3558         break;
3559     default:
3560         break;
3561     }
3562 
3563     // basic cursor key commands
3564     switch (command)
3565     {
3566     case CK_BackSpace:
3567         // if non persistent selection and text selected
3568         if (!edit_options.persistent_selections && edit->mark1 != edit->mark2)
3569             edit_block_delete_cmd (edit);
3570         else if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3571             edit->over_col--;
3572         else if (edit_options.backspace_through_tabs && is_in_indent (&edit->buffer))
3573         {
3574             while (edit_buffer_get_previous_byte (&edit->buffer) != '\n' && edit->buffer.curs1 > 0)
3575                 edit_backspace (edit, TRUE);
3576         }
3577         else if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer)
3578                  && right_of_four_spaces (edit))
3579         {
3580             int i;
3581 
3582             for (i = 0; i < HALF_TAB_SIZE; i++)
3583                 edit_backspace (edit, TRUE);
3584         }
3585         else
3586             edit_backspace (edit, FALSE);
3587         break;
3588     case CK_Delete:
3589         // if non persistent selection and text selected
3590         if (!edit_options.persistent_selections && edit->mark1 != edit->mark2)
3591             edit_block_delete_cmd (edit);
3592         else
3593         {
3594             if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3595                 edit_insert_over (edit);
3596 
3597             if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer)
3598                 && left_of_four_spaces (edit))
3599             {
3600                 int i;
3601 
3602                 for (i = 1; i <= HALF_TAB_SIZE; i++)
3603                     edit_delete (edit, TRUE);
3604             }
3605             else
3606                 edit_delete (edit, FALSE);
3607         }
3608         break;
3609     case CK_DeleteToWordBegin:
3610         edit->over_col = 0;
3611         edit_left_delete_word (edit);
3612         break;
3613     case CK_DeleteToWordEnd:
3614         if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3615             edit_insert_over (edit);
3616 
3617         edit_right_delete_word (edit);
3618         break;
3619     case CK_DeleteLine:
3620         edit_delete_line (edit);
3621         break;
3622     case CK_DeleteToHome:
3623         edit_delete_to_line_begin (edit);
3624         break;
3625     case CK_DeleteToEnd:
3626         edit_delete_to_line_end (edit);
3627         break;
3628     case CK_Enter:
3629         edit->over_col = 0;
3630         if (edit_options.auto_para_formatting)
3631         {
3632             edit_double_newline (edit);
3633             if (edit_options.return_does_auto_indent && !bracketed_pasting_in_progress)
3634                 edit_auto_indent (edit);
3635             format_paragraph (edit, FALSE);
3636         }
3637         else
3638         {
3639             edit_insert (edit, '\n');
3640             if (edit_options.return_does_auto_indent && !bracketed_pasting_in_progress)
3641                 edit_auto_indent (edit);
3642         }
3643         break;
3644     case CK_Return:
3645         edit_insert (edit, '\n');
3646         break;
3647 
3648     case CK_MarkColumnPageUp:
3649         edit->column_highlight = 1;
3650         MC_FALLTHROUGH;
3651     case CK_PageUp:
3652     case CK_MarkPageUp:
3653         edit_move_up (edit, w->lines - (edit->fullscreen ? 1 : 2), TRUE);
3654         break;
3655     case CK_MarkColumnPageDown:
3656         edit->column_highlight = 1;
3657         MC_FALLTHROUGH;
3658     case CK_PageDown:
3659     case CK_MarkPageDown:
3660         edit_move_down (edit, w->lines - (edit->fullscreen ? 1 : 2), TRUE);
3661         break;
3662     case CK_MarkColumnLeft:
3663         edit->column_highlight = 1;
3664         MC_FALLTHROUGH;
3665     case CK_Left:
3666     case CK_MarkLeft:
3667         if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer)
3668             && right_of_four_spaces (edit))
3669         {
3670             if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3671                 edit->over_col--;
3672             else
3673                 edit_cursor_move (edit, -HALF_TAB_SIZE);
3674             edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3675         }
3676         else
3677             edit_left_char_move_cmd (edit);
3678         break;
3679     case CK_MarkColumnRight:
3680         edit->column_highlight = 1;
3681         MC_FALLTHROUGH;
3682     case CK_Right:
3683     case CK_MarkRight:
3684         if (edit_options.fake_half_tabs && is_in_indent (&edit->buffer)
3685             && left_of_four_spaces (edit))
3686         {
3687             edit_cursor_move (edit, HALF_TAB_SIZE);
3688             edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3689         }
3690         else
3691             edit_right_char_move_cmd (edit);
3692         break;
3693     case CK_TopOnScreen:
3694     case CK_MarkToPageBegin:
3695         edit_begin_page (edit);
3696         break;
3697     case CK_BottomOnScreen:
3698     case CK_MarkToPageEnd:
3699         edit_end_page (edit);
3700         break;
3701     case CK_WordLeft:
3702     case CK_MarkToWordBegin:
3703         edit->over_col = 0;
3704         edit_left_word_move_cmd (edit);
3705         break;
3706     case CK_WordRight:
3707     case CK_MarkToWordEnd:
3708         edit->over_col = 0;
3709         edit_right_word_move_cmd (edit);
3710         break;
3711     case CK_MarkColumnUp:
3712         edit->column_highlight = 1;
3713         MC_FALLTHROUGH;
3714     case CK_Up:
3715     case CK_MarkUp:
3716         edit_move_up (edit, 1, FALSE);
3717         break;
3718     case CK_MarkColumnDown:
3719         edit->column_highlight = 1;
3720         MC_FALLTHROUGH;
3721     case CK_Down:
3722     case CK_MarkDown:
3723         edit_move_down (edit, 1, FALSE);
3724         break;
3725     case CK_MarkColumnParagraphUp:
3726         edit->column_highlight = 1;
3727         MC_FALLTHROUGH;
3728     case CK_ParagraphUp:
3729     case CK_MarkParagraphUp:
3730         edit_move_up_paragraph (edit, FALSE);
3731         break;
3732     case CK_MarkColumnParagraphDown:
3733         edit->column_highlight = 1;
3734         MC_FALLTHROUGH;
3735     case CK_ParagraphDown:
3736     case CK_MarkParagraphDown:
3737         edit_move_down_paragraph (edit, FALSE);
3738         break;
3739     case CK_MarkColumnScrollUp:
3740         edit->column_highlight = 1;
3741         MC_FALLTHROUGH;
3742     case CK_ScrollUp:
3743     case CK_MarkScrollUp:
3744         edit_move_up (edit, 1, TRUE);
3745         break;
3746     case CK_MarkColumnScrollDown:
3747         edit->column_highlight = 1;
3748         MC_FALLTHROUGH;
3749     case CK_ScrollDown:
3750     case CK_MarkScrollDown:
3751         edit_move_down (edit, 1, TRUE);
3752         break;
3753     case CK_Home:
3754     case CK_MarkToHome:
3755         edit_cursor_to_bol (edit);
3756         break;
3757     case CK_End:
3758     case CK_MarkToEnd:
3759         edit_cursor_to_eol (edit);
3760         break;
3761     case CK_Tab:
3762         // if text marked shift block
3763         if (edit->mark1 != edit->mark2 && !edit_options.persistent_selections)
3764         {
3765             if (edit->mark2 < 0)
3766                 edit_mark_cmd (edit, FALSE);
3767             edit_move_block_to_right (edit);
3768         }
3769         else
3770         {
3771             if (edit_options.cursor_beyond_eol)
3772                 edit_insert_over (edit);
3773             edit_tab_cmd (edit);
3774             if (edit_options.auto_para_formatting)
3775             {
3776                 format_paragraph (edit, FALSE);
3777                 edit->force |= REDRAW_PAGE;
3778             }
3779             else
3780                 check_and_wrap_line (edit);
3781         }
3782         break;
3783 
3784     case CK_InsertOverwrite:
3785         edit->overwrite = !edit->overwrite;
3786         break;
3787 
3788     case CK_Mark:
3789         if (edit->mark2 >= 0)
3790         {
3791             if (edit->column_highlight != 0)
3792                 edit_push_undo_action (edit, COLUMN_ON);
3793             edit->column_highlight = 0;
3794         }
3795         edit_mark_cmd (edit, FALSE);
3796         break;
3797     case CK_MarkColumn:
3798         if (edit->column_highlight == 0)
3799             edit_push_undo_action (edit, COLUMN_OFF);
3800         edit->column_highlight = 1;
3801         edit_mark_cmd (edit, FALSE);
3802         break;
3803     case CK_MarkAll:
3804         edit_set_markers (edit, 0, edit->buffer.size, 0, 0);
3805         edit->force |= REDRAW_PAGE;
3806         break;
3807     case CK_Unmark:
3808         if (edit->column_highlight != 0)
3809             edit_push_undo_action (edit, COLUMN_ON);
3810         edit->column_highlight = 0;
3811         edit_mark_cmd (edit, TRUE);
3812         break;
3813     case CK_MarkWord:
3814         if (edit->column_highlight != 0)
3815             edit_push_undo_action (edit, COLUMN_ON);
3816         edit->column_highlight = 0;
3817         edit_mark_current_word_cmd (edit);
3818         break;
3819     case CK_MarkLine:
3820         if (edit->column_highlight != 0)
3821             edit_push_undo_action (edit, COLUMN_ON);
3822         edit->column_highlight = 0;
3823         edit_mark_current_line_cmd (edit);
3824         break;
3825 
3826     case CK_Bookmark:
3827         book_mark_clear (edit, edit->buffer.curs_line, BOOK_MARK_FOUND_COLOR);
3828         if (book_mark_query_color (edit, edit->buffer.curs_line, BOOK_MARK_COLOR))
3829             book_mark_clear (edit, edit->buffer.curs_line, BOOK_MARK_COLOR);
3830         else
3831             book_mark_insert (edit, edit->buffer.curs_line, BOOK_MARK_COLOR);
3832         break;
3833     case CK_BookmarkFlush:
3834         book_mark_flush (edit, BOOK_MARK_COLOR);
3835         book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3836         edit->force |= REDRAW_PAGE;
3837         break;
3838     case CK_BookmarkNext:
3839         if (edit->book_mark != NULL)
3840         {
3841             edit_book_mark_t *p;
3842 
3843             p = book_mark_find (edit, edit->buffer.curs_line);
3844             if (p->next != NULL)
3845             {
3846                 p = p->next;
3847                 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
3848                     edit_move_display (edit, p->line - w->lines / 2);
3849                 edit_move_to_line (edit, p->line);
3850             }
3851         }
3852         break;
3853     case CK_BookmarkPrev:
3854         if (edit->book_mark != NULL)
3855         {
3856             edit_book_mark_t *p;
3857 
3858             p = book_mark_find (edit, edit->buffer.curs_line);
3859             while (p->line == edit->buffer.curs_line)
3860                 if (p->prev != NULL)
3861                     p = p->prev;
3862             if (p->line >= 0)
3863             {
3864                 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
3865                     edit_move_display (edit, p->line - w->lines / 2);
3866                 edit_move_to_line (edit, p->line);
3867             }
3868         }
3869         break;
3870 
3871     case CK_Top:
3872     case CK_MarkToFileBegin:
3873         edit_move_to_top (edit);
3874         break;
3875     case CK_Bottom:
3876     case CK_MarkToFileEnd:
3877         edit_move_to_bottom (edit);
3878         break;
3879 
3880     case CK_Copy:
3881         if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3882             edit_insert_over (edit);
3883         edit_block_copy_cmd (edit);
3884         break;
3885     case CK_Remove:
3886         edit_block_delete_cmd (edit);
3887         break;
3888     case CK_Move:
3889         edit_block_move_cmd (edit);
3890         break;
3891 
3892     case CK_BlockShiftLeft:
3893         if (edit->mark1 != edit->mark2)
3894             edit_move_block_to_left (edit);
3895         break;
3896     case CK_BlockShiftRight:
3897         if (edit->mark1 != edit->mark2)
3898             edit_move_block_to_right (edit);
3899         break;
3900     case CK_Store:
3901         edit_copy_to_X_buf_cmd (edit);
3902         break;
3903     case CK_Cut:
3904         edit_cut_to_X_buf_cmd (edit);
3905         break;
3906     case CK_Paste:
3907         // if non persistent selection and text selected
3908         if (!edit_options.persistent_selections && edit->mark1 != edit->mark2)
3909             edit_block_delete_cmd (edit);
3910         if (edit_options.cursor_beyond_eol && edit->over_col > 0)
3911             edit_insert_over (edit);
3912         edit_paste_from_X_buf_cmd (edit);
3913         if (!edit_options.persistent_selections && edit->mark2 >= 0)
3914         {
3915             if (edit->column_highlight != 0)
3916                 edit_push_undo_action (edit, COLUMN_ON);
3917             edit->column_highlight = 0;
3918             edit_mark_cmd (edit, TRUE);
3919         }
3920         break;
3921     case CK_History:
3922         edit_paste_from_history (edit);
3923         break;
3924 
3925     case CK_SaveAs:
3926         edit_save_as_cmd (edit);
3927         break;
3928     case CK_Save:
3929         edit_save_confirm_cmd (edit);
3930         break;
3931     case CK_BlockSave:
3932         edit_save_block_cmd (edit);
3933         break;
3934     case CK_InsertFile:
3935         edit_insert_file_cmd (edit);
3936         break;
3937 
3938     case CK_FilePrev:
3939         edit_load_back_cmd (edit);
3940         break;
3941     case CK_FileNext:
3942         edit_load_forward_cmd (edit);
3943         break;
3944 
3945     case CK_SyntaxChoose:
3946         edit_syntax_dialog (edit);
3947         break;
3948 
3949     case CK_Search:
3950         edit_search_cmd (edit, FALSE);
3951         break;
3952     case CK_SearchContinue:
3953         edit_search_cmd (edit, TRUE);
3954         break;
3955     case CK_Replace:
3956         edit_replace_cmd (edit, FALSE);
3957         break;
3958     case CK_ReplaceContinue:
3959         edit_replace_cmd (edit, TRUE);
3960         break;
3961     case CK_Complete:
3962         // if text marked shift block
3963         if (edit->mark1 != edit->mark2 && !edit_options.persistent_selections)
3964             edit_move_block_to_left (edit);
3965         else
3966             edit_complete_word_cmd (edit);
3967         break;
3968     case CK_Find:
3969         edit_get_match_keyword_cmd (edit);
3970         break;
3971 
3972 #ifdef HAVE_ASPELL
3973     case CK_SpellCheckCurrentWord:
3974         edit_suggest_current_word (edit);
3975         break;
3976     case CK_SpellCheck:
3977         edit_spellcheck_file (edit);
3978         break;
3979     case CK_SpellCheckSelectLang:
3980         edit_set_spell_lang ();
3981         break;
3982 #endif
3983 
3984     case CK_Date:
3985     {
3986         char s[BUF_MEDIUM];
3987         // fool gcc to prevent a Y2K warning
3988         char time_format[] = "_c";
3989         time_format[0] = '%';
3990 
3991         FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
3992         edit_print_string (edit, s);
3993         edit->force |= REDRAW_PAGE;
3994     }
3995     break;
3996     case CK_Goto:
3997         edit_goto_cmd (edit);
3998         break;
3999     case CK_ParagraphFormat:
4000         format_paragraph (edit, TRUE);
4001         edit->force |= REDRAW_PAGE;
4002         break;
4003     case CK_MacroDelete:
4004         edit_delete_macro_cmd (edit);
4005         break;
4006     case CK_MatchBracket:
4007         edit_goto_matching_bracket (edit);
4008         break;
4009     case CK_UserMenu:
4010         edit_user_menu (edit, NULL, -1);
4011         break;
4012     case CK_Sort:
4013         edit_sort_cmd (edit);
4014         break;
4015     case CK_ExternalCommand:
4016         edit_ext_cmd (edit);
4017         break;
4018     case CK_EditMail:
4019         edit_mail_dialog (edit);
4020         break;
4021 #ifdef HAVE_CHARSET
4022     case CK_SelectCodepage:
4023         edit_select_codepage_cmd (edit);
4024         break;
4025 #endif
4026     case CK_InsertLiteral:
4027         edit_insert_literal_cmd (edit);
4028         break;
4029     case CK_MacroStartStopRecord:
4030         edit_begin_end_macro_cmd (edit);
4031         break;
4032     case CK_RepeatStartStopRecord:
4033         edit_begin_end_repeat_cmd (edit);
4034         break;
4035     case CK_ExtendedKeyMap:
4036         WIDGET (edit)->ext_mode = TRUE;
4037         break;
4038     default:
4039         break;
4040     }
4041 
4042     // CK_PipeBlock
4043     if ((command / CK_PipeBlock (0)) == 1)
4044         edit_block_process_cmd (edit, command - CK_PipeBlock (0));
4045 
4046     // keys which must set the col position, and the search vars
4047     switch (command)
4048     {
4049     case CK_Search:
4050     case CK_SearchContinue:
4051     case CK_Replace:
4052     case CK_ReplaceContinue:
4053     case CK_Complete:
4054         edit->prev_col = edit_get_col (edit);
4055         break;
4056     case CK_Up:
4057     case CK_MarkUp:
4058     case CK_MarkColumnUp:
4059     case CK_Down:
4060     case CK_MarkDown:
4061     case CK_MarkColumnDown:
4062     case CK_PageUp:
4063     case CK_MarkPageUp:
4064     case CK_MarkColumnPageUp:
4065     case CK_PageDown:
4066     case CK_MarkPageDown:
4067     case CK_MarkColumnPageDown:
4068     case CK_Top:
4069     case CK_MarkToFileBegin:
4070     case CK_Bottom:
4071     case CK_MarkToFileEnd:
4072     case CK_ParagraphUp:
4073     case CK_MarkParagraphUp:
4074     case CK_MarkColumnParagraphUp:
4075     case CK_ParagraphDown:
4076     case CK_MarkParagraphDown:
4077     case CK_MarkColumnParagraphDown:
4078     case CK_ScrollUp:
4079     case CK_MarkScrollUp:
4080     case CK_MarkColumnScrollUp:
4081     case CK_ScrollDown:
4082     case CK_MarkScrollDown:
4083     case CK_MarkColumnScrollDown:
4084         edit->search_start = edit->buffer.curs1;
4085         edit->found_len = 0;
4086         break;
4087     default:
4088         edit->found_len = 0;
4089         edit->prev_col = edit_get_col (edit);
4090         edit->search_start = edit->buffer.curs1;
4091     }
4092     edit_find_bracket (edit);
4093 
4094     if (edit_options.auto_para_formatting)
4095     {
4096         switch (command)
4097         {
4098         case CK_BackSpace:
4099         case CK_Delete:
4100         case CK_DeleteToWordBegin:
4101         case CK_DeleteToWordEnd:
4102         case CK_DeleteToHome:
4103         case CK_DeleteToEnd:
4104             format_paragraph (edit, FALSE);
4105             edit->force |= REDRAW_PAGE;
4106             break;
4107         default:
4108             break;
4109         }
4110     }
4111 }
4112 
4113 /* --------------------------------------------------------------------------------------------- */
4114 
4115 void
4116 edit_stack_init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
4117 {
4118     for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4119         edit_arg_init (&edit_history_moveto[edit_stack_iterator], NULL, -1);
4120 
4121     edit_stack_iterator = 0;
4122 }
4123 
4124 /* --------------------------------------------------------------------------------------------- */
4125 
4126 void
4127 edit_stack_free (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
4128 {
4129     for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4130         vfs_path_free (edit_history_moveto[edit_stack_iterator].file_vpath, TRUE);
4131 }
4132 
4133 /* --------------------------------------------------------------------------------------------- */
4134 /** move i lines */
4135 
4136 void
4137 edit_move_up (WEdit *edit, long i, gboolean do_scroll)
     /* [previous][next][first][last][top][bottom][index][help]  */
4138 {
4139     edit_move_updown (edit, i, do_scroll, TRUE);
4140 }
4141 
4142 /* --------------------------------------------------------------------------------------------- */
4143 /** move i lines */
4144 
4145 void
4146 edit_move_down (WEdit *edit, long i, gboolean do_scroll)
     /* [previous][next][first][last][top][bottom][index][help]  */
4147 {
4148     edit_move_updown (edit, i, do_scroll, FALSE);
4149 }
4150 
4151 /* --------------------------------------------------------------------------------------------- */
4152 /**
4153  * Create edit_arg_t object from vfs_path_t object and the line number.
4154  *
4155  * @param file_vpath  file path object
4156  * @param line_number line number. If value is 0, try to restore saved position.
4157  * @return edit_arg_t object
4158  */
4159 
4160 edit_arg_t *
4161 edit_arg_vpath_new (vfs_path_t *file_vpath, long line_number)
     /* [previous][next][first][last][top][bottom][index][help]  */
4162 {
4163     edit_arg_t *arg;
4164 
4165     arg = g_new (edit_arg_t, 1);
4166     arg->file_vpath = file_vpath;
4167     arg->line_number = line_number;
4168 
4169     return arg;
4170 }
4171 
4172 /* --------------------------------------------------------------------------------------------- */
4173 /**
4174  * Create edit_arg_t object from file name and the line number.
4175  *
4176  * @param file_name   file name
4177  * @param line_number line number. If value is 0, try to restore saved position.
4178  * @return edit_arg_t object
4179  */
4180 
4181 edit_arg_t *
4182 edit_arg_new (const char *file_name, long line_number)
     /* [previous][next][first][last][top][bottom][index][help]  */
4183 {
4184     return edit_arg_vpath_new (vfs_path_from_str (file_name), line_number);
4185 }
4186 
4187 /* --------------------------------------------------------------------------------------------- */
4188 /**
4189  * Initialize edit_arg_t object.
4190  *
4191  * @param arg  edit_arg_t object
4192  * @param vpath vfs_path_t object
4193  * @param line line number
4194  */
4195 
4196 void
4197 edit_arg_init (edit_arg_t *arg, vfs_path_t *vpath, long line)
     /* [previous][next][first][last][top][bottom][index][help]  */
4198 {
4199     arg->file_vpath = (vfs_path_t *) vpath;
4200     arg->line_number = line;
4201 }
4202 
4203 /* --------------------------------------------------------------------------------------------- */
4204 /**
4205  * Apply new values to edit_arg_t object members.
4206  *
4207  * @param arg  edit_arg_t object
4208  * @param vpath vfs_path_t object
4209  * @param line line number
4210  */
4211 
4212 void
4213 edit_arg_assign (edit_arg_t *arg, vfs_path_t *vpath, long line)
     /* [previous][next][first][last][top][bottom][index][help]  */
4214 {
4215     vfs_path_free (arg->file_vpath, TRUE);
4216     edit_arg_init (arg, vpath, line);
4217 }
4218 
4219 /* --------------------------------------------------------------------------------------------- */
4220 /**
4221  * Free the edit_arg_t object.
4222  *
4223  * @param arg edit_arg_t object
4224  */
4225 
4226 void
4227 edit_arg_free (edit_arg_t *arg)
     /* [previous][next][first][last][top][bottom][index][help]  */
4228 {
4229     vfs_path_free (arg->file_vpath, TRUE);
4230     g_free (arg);
4231 }
4232 
4233 /* --------------------------------------------------------------------------------------------- */
4234 
4235 const char *
4236 edit_get_file_name (const WEdit *edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
4237 {
4238     return vfs_path_as_str (edit->filename_vpath);
4239 }
4240 
4241 /* --------------------------------------------------------------------------------------------- */

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