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. 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. edit_mark_cmd
  89. edit_mark_current_word_cmd
  90. edit_mark_current_line_cmd
  91. edit_delete_line
  92. edit_push_key_press
  93. edit_find_bracket
  94. edit_execute_key_command
  95. edit_execute_cmd
  96. edit_stack_init
  97. edit_stack_free
  98. edit_move_up
  99. edit_move_down

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

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