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-2019
   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_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_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_redraw (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         /* save some widget parameters */
2088         gboolean fullscreen = edit->fullscreen;
2089         int y_prev = edit->y_prev;
2090         int x_prev = edit->x_prev;
2091         int lines_prev = edit->lines_prev;
2092         int cols_prev = edit->cols_prev;
2093 
2094         edit_purge_widget (edit);
2095 
2096         /* restore saved parameters */
2097         edit->fullscreen = fullscreen;
2098         edit->y_prev = y_prev;
2099         edit->x_prev = x_prev;
2100         edit->lines_prev = lines_prev;
2101         edit->cols_prev = cols_prev;
2102     }
2103     else
2104     {
2105         Widget *w;
2106         edit = g_malloc0 (sizeof (WEdit));
2107         to_free = TRUE;
2108 
2109         w = WIDGET (edit);
2110         widget_init (w, y, x, lines, cols, NULL, NULL);
2111         w->options |= WOP_SELECTABLE | WOP_TOP_SELECT | WOP_WANT_CURSOR;
2112         edit->fullscreen = TRUE;
2113         edit_save_size (edit);
2114     }
2115 
2116     edit->drag_state = MCEDIT_DRAG_NONE;
2117 
2118     edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2119     edit->stat1.st_uid = getuid ();
2120     edit->stat1.st_gid = getgid ();
2121     edit->stat1.st_mtime = 0;
2122 
2123     edit->over_col = 0;
2124     edit->bracket = -1;
2125     edit->last_bracket = -1;
2126     edit->force |= REDRAW_PAGE;
2127 
2128     /* set file name before load file */
2129     edit_set_filename (edit, filename_vpath);
2130 
2131     edit->undo_stack_size = START_STACK_SIZE;
2132     edit->undo_stack_size_mask = START_STACK_SIZE - 1;
2133     edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
2134 
2135     edit->redo_stack_size = START_STACK_SIZE;
2136     edit->redo_stack_size_mask = START_STACK_SIZE - 1;
2137     edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long));
2138 
2139 #ifdef HAVE_CHARSET
2140     edit->utf8 = FALSE;
2141     edit->converter = str_cnv_from_term;
2142     edit_set_codeset (edit);
2143 #endif
2144 
2145     if (!edit_load_file (edit))
2146     {
2147         /* edit_load_file already gives an error message */
2148         if (to_free)
2149             g_free (edit);
2150         return NULL;
2151     }
2152 
2153     edit->loading_done = 1;
2154     edit->modified = 0;
2155     edit->locked = 0;
2156     edit_load_syntax (edit, NULL, NULL);
2157     edit_get_syntax_color (edit, -1);
2158 
2159     /* load saved cursor position and/or boolmarks */
2160     if ((line == 0) && option_save_position)
2161         edit_load_position (edit, TRUE);
2162     else
2163     {
2164         edit_load_position (edit, FALSE);
2165         if (line <= 0)
2166             line = 1;
2167         edit_move_display (edit, line - 1);
2168         edit_move_to_line (edit, line - 1);
2169     }
2170 
2171     edit_load_macro_cmd (edit);
2172 
2173     return edit;
2174 }
2175 
2176 /* --------------------------------------------------------------------------------------------- */
2177 
2178 /** Clear the edit struct, freeing everything in it.  Return TRUE on success */
2179 gboolean
2180 edit_clean (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2181 {
2182     if (edit == NULL)
2183         return FALSE;
2184 
2185     /* a stale lock, remove it */
2186     if (edit->locked)
2187         (void) unlock_file (edit->filename_vpath);
2188 
2189     /* save cursor position */
2190     if (option_save_position)
2191         edit_save_position (edit);
2192     else if (edit->serialized_bookmarks != NULL)
2193         g_array_free (edit->serialized_bookmarks, TRUE);
2194 
2195     /* File specified on the mcedit command line and never saved */
2196     if (edit->delete_file)
2197         unlink (vfs_path_get_last_path_str (edit->filename_vpath));
2198 
2199     edit_free_syntax_rules (edit);
2200     book_mark_flush (edit, -1);
2201 
2202     edit_buffer_clean (&edit->buffer);
2203 
2204     g_free (edit->undo_stack);
2205     g_free (edit->redo_stack);
2206     vfs_path_free (edit->filename_vpath);
2207     vfs_path_free (edit->dir_vpath);
2208     mc_search_free (edit->search);
2209     g_free (edit->last_search_string);
2210 
2211 #ifdef HAVE_CHARSET
2212     if (edit->converter != str_cnv_from_term)
2213         str_close_conv (edit->converter);
2214 #endif
2215 
2216     edit_purge_widget (edit);
2217 
2218     return TRUE;
2219 }
2220 
2221 /* --------------------------------------------------------------------------------------------- */
2222 
2223 /**
2224  * Load a new file into the editor and set line.  If it fails, preserve the old file.
2225  * To do it, allocate a new widget, initialize it and, if the new file
2226  * was loaded, copy the data to the old widget.
2227  *
2228  * @return TRUE on success, FALSE on failure.
2229  */
2230 gboolean
2231 edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
     /* [previous][next][first][last][top][bottom][index][help]  */
2232 {
2233     Widget *w = WIDGET (edit);
2234     WEdit *e;
2235 
2236     e = g_malloc0 (sizeof (WEdit));
2237     *WIDGET (e) = *w;
2238     /* save some widget parameters */
2239     e->fullscreen = edit->fullscreen;
2240     e->y_prev = edit->y_prev;
2241     e->x_prev = edit->x_prev;
2242     e->lines_prev = edit->lines_prev;
2243     e->cols_prev = edit->cols_prev;
2244 
2245     if (edit_init (e, w->y, w->x, w->lines, w->cols, filename_vpath, line) == NULL)
2246     {
2247         g_free (e);
2248         return FALSE;
2249     }
2250 
2251     edit_clean (edit);
2252     memcpy (edit, e, sizeof (*edit));
2253     g_free (e);
2254 
2255     return TRUE;
2256 }
2257 
2258 /* --------------------------------------------------------------------------------------------- */
2259 
2260 #ifdef HAVE_CHARSET
2261 void
2262 edit_set_codeset (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2263 {
2264     const char *cp_id;
2265 
2266     cp_id =
2267         get_codepage_id (mc_global.source_codepage >=
2268                          0 ? mc_global.source_codepage : mc_global.display_codepage);
2269 
2270     if (cp_id != NULL)
2271     {
2272         GIConv conv;
2273         conv = str_crt_conv_from (cp_id);
2274         if (conv != INVALID_CONV)
2275         {
2276             if (edit->converter != str_cnv_from_term)
2277                 str_close_conv (edit->converter);
2278             edit->converter = conv;
2279         }
2280     }
2281 
2282     if (cp_id != NULL)
2283         edit->utf8 = str_isutf8 (cp_id);
2284 }
2285 #endif
2286 
2287 /* --------------------------------------------------------------------------------------------- */
2288 
2289 /**
2290  * Recording stack for undo:
2291  * The following is an implementation of a compressed stack. Identical
2292  * pushes are recorded by a negative prefix indicating the number of times the
2293  * same char was pushed. This saves space for repeated curs-left or curs-right
2294  * delete etc.
2295  *
2296  * eg:
2297  *
2298  * pushed:       stored:
2299  *
2300  * a
2301  * b             a
2302  * b            -3
2303  * b             b
2304  * c  -->       -4
2305  * c             c
2306  * c             d
2307  * c
2308  * d
2309  *
2310  * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2311  * 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2312  * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2313  * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2314  * position.
2315  *
2316  * The only way the cursor moves or the buffer is changed is through the routines:
2317  * insert, backspace, insert_ahead, delete, and cursor_move.
2318  * These record the reverse undo movements onto the stack each time they are
2319  * called.
2320  *
2321  * Each key press results in a set of actions (insert; delete ...). So each time
2322  * a key is pressed the current position of start_display is pushed as
2323  * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2324  * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2325  * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2326  *
2327  *
2328  *
2329  * @param edit editor object
2330  * @param c code of the action
2331  */
2332 
2333 void
2334 edit_push_undo_action (WEdit * edit, long c)
     /* [previous][next][first][last][top][bottom][index][help]  */
2335 {
2336     unsigned long sp = edit->undo_stack_pointer;
2337     unsigned long spm1;
2338     long *t;
2339 
2340     /* first enlarge the stack if necessary */
2341     if (sp > edit->undo_stack_size - 10)
2342     {                           /* say */
2343         if (option_max_undo < 256)
2344             option_max_undo = 256;
2345         if (edit->undo_stack_size < (unsigned long) option_max_undo)
2346         {
2347             t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
2348             if (t)
2349             {
2350                 edit->undo_stack = t;
2351                 edit->undo_stack_size <<= 1;
2352                 edit->undo_stack_size_mask = edit->undo_stack_size - 1;
2353             }
2354         }
2355     }
2356     spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
2357     if (edit->undo_stack_disable)
2358     {
2359         edit_push_redo_action (edit, KEY_PRESS);
2360         edit_push_redo_action (edit, c);
2361         return;
2362     }
2363 
2364     if (edit->redo_stack_reset)
2365         edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2366 
2367     if (edit->undo_stack_bottom != sp
2368         && spm1 != edit->undo_stack_bottom
2369         && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
2370     {
2371         long d;
2372         if (edit->undo_stack[spm1] < 0)
2373         {
2374             d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
2375             if (d == c && edit->undo_stack[spm1] > -1000000000)
2376             {
2377                 if (c < KEY_PRESS)      /* --> no need to push multiple do-nothings */
2378                     edit->undo_stack[spm1]--;
2379                 return;
2380             }
2381         }
2382         else
2383         {
2384             d = edit->undo_stack[spm1];
2385             if (d == c)
2386             {
2387                 if (c >= KEY_PRESS)
2388                     return;     /* --> no need to push multiple do-nothings */
2389                 edit->undo_stack[sp] = -2;
2390                 goto check_bottom;
2391             }
2392         }
2393     }
2394     edit->undo_stack[sp] = c;
2395 
2396   check_bottom:
2397     edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
2398 
2399     /* if the sp wraps round and catches the undo_stack_bottom then erase
2400      * the first set of actions on the stack to make space - by moving
2401      * undo_stack_bottom forward one "key press" */
2402     c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
2403     if ((unsigned long) c == edit->undo_stack_bottom ||
2404         (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
2405         do
2406         {
2407             edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
2408         }
2409         while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
2410                && edit->undo_stack_bottom != edit->undo_stack_pointer);
2411 
2412     /*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: */
2413     if (edit->undo_stack_pointer != edit->undo_stack_bottom
2414         && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
2415     {
2416         edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
2417     }
2418 }
2419 
2420 /* --------------------------------------------------------------------------------------------- */
2421 
2422 void
2423 edit_push_redo_action (WEdit * edit, long c)
     /* [previous][next][first][last][top][bottom][index][help]  */
2424 {
2425     unsigned long sp = edit->redo_stack_pointer;
2426     unsigned long spm1;
2427     long *t;
2428     /* first enlarge the stack if necessary */
2429     if (sp > edit->redo_stack_size - 10)
2430     {                           /* say */
2431         if (option_max_undo < 256)
2432             option_max_undo = 256;
2433         if (edit->redo_stack_size < (unsigned long) option_max_undo)
2434         {
2435             t = g_realloc (edit->redo_stack, (edit->redo_stack_size * 2 + 10) * sizeof (long));
2436             if (t)
2437             {
2438                 edit->redo_stack = t;
2439                 edit->redo_stack_size <<= 1;
2440                 edit->redo_stack_size_mask = edit->redo_stack_size - 1;
2441             }
2442         }
2443     }
2444     spm1 = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
2445 
2446     if (edit->redo_stack_bottom != sp
2447         && spm1 != edit->redo_stack_bottom
2448         && ((sp - 2) & edit->redo_stack_size_mask) != edit->redo_stack_bottom)
2449     {
2450         long d;
2451         if (edit->redo_stack[spm1] < 0)
2452         {
2453             d = edit->redo_stack[(sp - 2) & edit->redo_stack_size_mask];
2454             if (d == c && edit->redo_stack[spm1] > -1000000000)
2455             {
2456                 if (c < KEY_PRESS)      /* --> no need to push multiple do-nothings */
2457                     edit->redo_stack[spm1]--;
2458                 return;
2459             }
2460         }
2461         else
2462         {
2463             d = edit->redo_stack[spm1];
2464             if (d == c)
2465             {
2466                 if (c >= KEY_PRESS)
2467                     return;     /* --> no need to push multiple do-nothings */
2468                 edit->redo_stack[sp] = -2;
2469                 goto redo_check_bottom;
2470             }
2471         }
2472     }
2473     edit->redo_stack[sp] = c;
2474 
2475   redo_check_bottom:
2476     edit->redo_stack_pointer = (edit->redo_stack_pointer + 1) & edit->redo_stack_size_mask;
2477 
2478     /* if the sp wraps round and catches the redo_stack_bottom then erase
2479      * the first set of actions on the stack to make space - by moving
2480      * redo_stack_bottom forward one "key press" */
2481     c = (edit->redo_stack_pointer + 2) & edit->redo_stack_size_mask;
2482     if ((unsigned long) c == edit->redo_stack_bottom ||
2483         (((unsigned long) c + 1) & edit->redo_stack_size_mask) == edit->redo_stack_bottom)
2484         do
2485         {
2486             edit->redo_stack_bottom = (edit->redo_stack_bottom + 1) & edit->redo_stack_size_mask;
2487         }
2488         while (edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS
2489                && edit->redo_stack_bottom != edit->redo_stack_pointer);
2490 
2491     /*
2492      * If a single key produced enough pushes to wrap all the way round then
2493      * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2494      * The stack is then initialised:
2495      */
2496 
2497     if (edit->redo_stack_pointer != edit->redo_stack_bottom
2498         && edit->redo_stack[edit->redo_stack_bottom] < KEY_PRESS)
2499         edit->redo_stack_bottom = edit->redo_stack_pointer = 0;
2500 }
2501 
2502 /* --------------------------------------------------------------------------------------------- */
2503 /**
2504    Basic low level single character buffer alterations and movements at the cursor.
2505  */
2506 
2507 void
2508 edit_insert (WEdit * edit, int c)
     /* [previous][next][first][last][top][bottom][index][help]  */
2509 {
2510     /* first we must update the position of the display window */
2511     if (edit->buffer.curs1 < edit->start_display)
2512     {
2513         edit->start_display++;
2514         if (c == '\n')
2515             edit->start_line++;
2516     }
2517 
2518     /* Mark file as modified, unless the file hasn't been fully loaded */
2519     if (edit->loading_done)
2520         edit_modification (edit);
2521 
2522     /* now we must update some info on the file and check if a redraw is required */
2523     if (c == '\n')
2524     {
2525         book_mark_inc (edit, edit->buffer.curs_line);
2526         edit->buffer.curs_line++;
2527         edit->buffer.lines++;
2528         edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
2529     }
2530 
2531     /* save the reverse command onto the undo stack */
2532     /* ordinary char and not space */
2533     if (c > 32)
2534         edit_push_undo_action (edit, BACKSPACE);
2535     else
2536         edit_push_undo_action (edit, BACKSPACE_BR);
2537     /* update markers */
2538     edit->mark1 += (edit->mark1 > edit->buffer.curs1) ? 1 : 0;
2539     edit->mark2 += (edit->mark2 > edit->buffer.curs1) ? 1 : 0;
2540     edit->last_get_rule += (edit->last_get_rule > edit->buffer.curs1) ? 1 : 0;
2541 
2542     edit_buffer_insert (&edit->buffer, c);
2543 }
2544 
2545 /* --------------------------------------------------------------------------------------------- */
2546 /** same as edit_insert and move left */
2547 
2548 void
2549 edit_insert_ahead (WEdit * edit, int c)
     /* [previous][next][first][last][top][bottom][index][help]  */
2550 {
2551     if (edit->buffer.curs1 < edit->start_display)
2552     {
2553         edit->start_display++;
2554         if (c == '\n')
2555             edit->start_line++;
2556     }
2557     edit_modification (edit);
2558     if (c == '\n')
2559     {
2560         book_mark_inc (edit, edit->buffer.curs_line);
2561         edit->buffer.lines++;
2562         edit->force |= REDRAW_AFTER_CURSOR;
2563     }
2564     /* ordinary char and not space */
2565     if (c > 32)
2566         edit_push_undo_action (edit, DELCHAR);
2567     else
2568         edit_push_undo_action (edit, DELCHAR_BR);
2569 
2570     edit->mark1 += (edit->mark1 >= edit->buffer.curs1) ? 1 : 0;
2571     edit->mark2 += (edit->mark2 >= edit->buffer.curs1) ? 1 : 0;
2572     edit->last_get_rule += (edit->last_get_rule >= edit->buffer.curs1) ? 1 : 0;
2573 
2574     edit_buffer_insert_ahead (&edit->buffer, c);
2575 }
2576 
2577 /* --------------------------------------------------------------------------------------------- */
2578 
2579 void
2580 edit_insert_over (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2581 {
2582     long i;
2583 
2584     for (i = 0; i < edit->over_col; i++)
2585         edit_insert (edit, ' ');
2586 
2587     edit->over_col = 0;
2588 }
2589 
2590 /* --------------------------------------------------------------------------------------------- */
2591 
2592 int
2593 edit_delete (WEdit * edit, gboolean byte_delete)
     /* [previous][next][first][last][top][bottom][index][help]  */
2594 {
2595     int p = 0;
2596     int char_length = 1;
2597     int i;
2598 
2599     if (edit->buffer.curs2 == 0)
2600         return 0;
2601 
2602 #ifdef HAVE_CHARSET
2603     /* if byte_delete == TRUE then delete only one byte not multibyte char */
2604     if (edit->utf8 && !byte_delete)
2605     {
2606         edit_buffer_get_utf (&edit->buffer, edit->buffer.curs1, &char_length);
2607         if (char_length < 1)
2608             char_length = 1;
2609     }
2610 #else
2611     (void) byte_delete;
2612 #endif
2613 
2614     if (edit->mark2 != edit->mark1)
2615         edit_push_markers (edit);
2616 
2617     for (i = 1; i <= char_length; i++)
2618     {
2619         if (edit->mark1 > edit->buffer.curs1)
2620         {
2621             edit->mark1--;
2622             edit->end_mark_curs--;
2623         }
2624         if (edit->mark2 > edit->buffer.curs1)
2625             edit->mark2--;
2626         if (edit->last_get_rule > edit->buffer.curs1)
2627             edit->last_get_rule--;
2628 
2629         p = edit_buffer_delete (&edit->buffer);
2630 
2631         edit_push_undo_action (edit, p + 256);
2632     }
2633 
2634     edit_modification (edit);
2635     if (p == '\n')
2636     {
2637         book_mark_dec (edit, edit->buffer.curs_line);
2638         edit->buffer.lines--;
2639         edit->force |= REDRAW_AFTER_CURSOR;
2640     }
2641     if (edit->buffer.curs1 < edit->start_display)
2642     {
2643         edit->start_display--;
2644         if (p == '\n')
2645             edit->start_line--;
2646     }
2647 
2648     return p;
2649 }
2650 
2651 /* --------------------------------------------------------------------------------------------- */
2652 
2653 int
2654 edit_backspace (WEdit * edit, gboolean byte_delete)
     /* [previous][next][first][last][top][bottom][index][help]  */
2655 {
2656     int p = 0;
2657     int char_length = 1;
2658     int i;
2659 
2660     if (edit->buffer.curs1 == 0)
2661         return 0;
2662 
2663     if (edit->mark2 != edit->mark1)
2664         edit_push_markers (edit);
2665 
2666 #ifdef HAVE_CHARSET
2667     if (edit->utf8 && !byte_delete)
2668     {
2669         edit_buffer_get_prev_utf (&edit->buffer, edit->buffer.curs1, &char_length);
2670         if (char_length < 1)
2671             char_length = 1;
2672     }
2673 #else
2674     (void) byte_delete;
2675 #endif
2676 
2677     for (i = 1; i <= char_length; i++)
2678     {
2679         if (edit->mark1 >= edit->buffer.curs1)
2680         {
2681             edit->mark1--;
2682             edit->end_mark_curs--;
2683         }
2684         if (edit->mark2 >= edit->buffer.curs1)
2685             edit->mark2--;
2686         if (edit->last_get_rule >= edit->buffer.curs1)
2687             edit->last_get_rule--;
2688 
2689         p = edit_buffer_backspace (&edit->buffer);
2690 
2691         edit_push_undo_action (edit, p);
2692     }
2693     edit_modification (edit);
2694     if (p == '\n')
2695     {
2696         book_mark_dec (edit, edit->buffer.curs_line);
2697         edit->buffer.curs_line--;
2698         edit->buffer.lines--;
2699         edit->force |= REDRAW_AFTER_CURSOR;
2700     }
2701 
2702     if (edit->buffer.curs1 < edit->start_display)
2703     {
2704         edit->start_display--;
2705         if (p == '\n')
2706             edit->start_line--;
2707     }
2708 
2709     return p;
2710 }
2711 
2712 /* --------------------------------------------------------------------------------------------- */
2713 /** moves the cursor right or left: increment positive or negative respectively */
2714 
2715 void
2716 edit_cursor_move (WEdit * edit, off_t increment)
     /* [previous][next][first][last][top][bottom][index][help]  */
2717 {
2718     if (increment < 0)
2719     {
2720         for (; increment < 0 && edit->buffer.curs1 != 0; increment++)
2721         {
2722             int c;
2723 
2724             edit_push_undo_action (edit, CURS_RIGHT);
2725 
2726             c = edit_buffer_get_previous_byte (&edit->buffer);
2727             edit_buffer_insert_ahead (&edit->buffer, c);
2728             c = edit_buffer_backspace (&edit->buffer);
2729             if (c == '\n')
2730             {
2731                 edit->buffer.curs_line--;
2732                 edit->force |= REDRAW_LINE_BELOW;
2733             }
2734         }
2735     }
2736     else
2737     {
2738         for (; increment > 0 && edit->buffer.curs2 != 0; increment--)
2739         {
2740             int c;
2741 
2742             edit_push_undo_action (edit, CURS_LEFT);
2743 
2744             c = edit_buffer_get_current_byte (&edit->buffer);
2745             edit_buffer_insert (&edit->buffer, c);
2746             c = edit_buffer_delete (&edit->buffer);
2747             if (c == '\n')
2748             {
2749                 edit->buffer.curs_line++;
2750                 edit->force |= REDRAW_LINE_ABOVE;
2751             }
2752         }
2753     }
2754 }
2755 
2756 /* --------------------------------------------------------------------------------------------- */
2757 /* If cols is zero this returns the count of columns from current to upto. */
2758 /* If upto is zero returns index of cols across from current. */
2759 
2760 off_t
2761 edit_move_forward3 (const WEdit * edit, off_t current, long cols, off_t upto)
     /* [previous][next][first][last][top][bottom][index][help]  */
2762 {
2763     off_t p, q;
2764     long col;
2765 
2766     if (upto != 0)
2767     {
2768         q = upto;
2769         cols = -10;
2770     }
2771     else
2772         q = edit->buffer.size + 2;
2773 
2774     for (col = 0, p = current; p < q; p++)
2775     {
2776         int c, orig_c;
2777 
2778         if (cols != -10)
2779         {
2780             if (col == cols)
2781                 return p;
2782             if (col > cols)
2783                 return p - 1;
2784         }
2785 
2786         orig_c = c = edit_buffer_get_byte (&edit->buffer, p);
2787 
2788 #ifdef HAVE_CHARSET
2789         if (edit->utf8)
2790         {
2791             int utf_ch;
2792             int char_length = 1;
2793 
2794             utf_ch = edit_buffer_get_utf (&edit->buffer, p, &char_length);
2795             if (mc_global.utf8_display)
2796             {
2797                 if (char_length > 1)
2798                     col -= char_length - 1;
2799                 if (g_unichar_iswide (utf_ch))
2800                     col++;
2801             }
2802             else if (char_length > 1 && g_unichar_isprint (utf_ch))
2803                 col -= char_length - 1;
2804         }
2805 
2806         c = convert_to_display_c (c);
2807 #endif
2808 
2809         if (c == '\n')
2810             return (upto != 0 ? (off_t) col : p);
2811         if (c == '\t')
2812             col += TAB_SIZE - col % TAB_SIZE;
2813         else if ((c < 32 || c == 127) && (orig_c == c
2814 #ifdef HAVE_CHARSET
2815                                           || (!mc_global.utf8_display && !edit->utf8)
2816 #endif
2817                  ))
2818             /* '\r' is shown as ^M, so we must advance 2 characters */
2819             /* Caret notation for control characters */
2820             col += 2;
2821         else
2822             col++;
2823     }
2824     return (off_t) col;
2825 }
2826 
2827 /* --------------------------------------------------------------------------------------------- */
2828 /** returns the current offset of the cursor from the beginning of a file */
2829 
2830 off_t
2831 edit_get_cursor_offset (const WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2832 {
2833     return edit->buffer.curs1;
2834 }
2835 
2836 /* --------------------------------------------------------------------------------------------- */
2837 /** returns the current column position of the cursor */
2838 
2839 long
2840 edit_get_col (const WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2841 {
2842     return (long) edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), 0,
2843                                       edit->buffer.curs1);
2844 }
2845 
2846 /* --------------------------------------------------------------------------------------------- */
2847 /* Scrolling functions */
2848 /* --------------------------------------------------------------------------------------------- */
2849 
2850 void
2851 edit_update_curs_row (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2852 {
2853     edit->curs_row = edit->buffer.curs_line - edit->start_line;
2854 }
2855 
2856 /* --------------------------------------------------------------------------------------------- */
2857 
2858 void
2859 edit_update_curs_col (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2860 {
2861     edit->curs_col = (long) edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer),
2862                                                 0, edit->buffer.curs1);
2863 }
2864 
2865 /* --------------------------------------------------------------------------------------------- */
2866 
2867 long
2868 edit_get_curs_col (const WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2869 {
2870     return edit->curs_col;
2871 }
2872 
2873 /* --------------------------------------------------------------------------------------------- */
2874 /** moves the display start position up by i lines */
2875 
2876 void
2877 edit_scroll_upward (WEdit * edit, long i)
     /* [previous][next][first][last][top][bottom][index][help]  */
2878 {
2879     long lines_above = edit->start_line;
2880 
2881     if (i > lines_above)
2882         i = lines_above;
2883     if (i != 0)
2884     {
2885         edit->start_line -= i;
2886         edit->start_display =
2887             edit_buffer_get_backward_offset (&edit->buffer, edit->start_display, i);
2888         edit->force |= REDRAW_PAGE;
2889         edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2890     }
2891     edit_update_curs_row (edit);
2892 }
2893 
2894 
2895 /* --------------------------------------------------------------------------------------------- */
2896 
2897 void
2898 edit_scroll_downward (WEdit * edit, long i)
     /* [previous][next][first][last][top][bottom][index][help]  */
2899 {
2900     long lines_below;
2901 
2902     lines_below = edit->buffer.lines - edit->start_line - (WIDGET (edit)->lines - 1);
2903     if (lines_below > 0)
2904     {
2905         if (i > lines_below)
2906             i = lines_below;
2907         edit->start_line += i;
2908         edit->start_display =
2909             edit_buffer_get_forward_offset (&edit->buffer, edit->start_display, i, 0);
2910         edit->force |= REDRAW_PAGE;
2911         edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2912     }
2913     edit_update_curs_row (edit);
2914 }
2915 
2916 /* --------------------------------------------------------------------------------------------- */
2917 
2918 void
2919 edit_scroll_right (WEdit * edit, long i)
     /* [previous][next][first][last][top][bottom][index][help]  */
2920 {
2921     edit->force |= REDRAW_PAGE;
2922     edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2923     edit->start_col -= i;
2924 }
2925 
2926 /* --------------------------------------------------------------------------------------------- */
2927 
2928 void
2929 edit_scroll_left (WEdit * edit, long i)
     /* [previous][next][first][last][top][bottom][index][help]  */
2930 {
2931     if (edit->start_col)
2932     {
2933         edit->start_col += i;
2934         if (edit->start_col > 0)
2935             edit->start_col = 0;
2936         edit->force |= REDRAW_PAGE;
2937         edit->force &= (0xfff - REDRAW_CHAR_ONLY);
2938     }
2939 }
2940 
2941 /* --------------------------------------------------------------------------------------------- */
2942 /* high level cursor movement commands */
2943 /* --------------------------------------------------------------------------------------------- */
2944 
2945 void
2946 edit_move_to_prev_col (WEdit * edit, off_t p)
     /* [previous][next][first][last][top][bottom][index][help]  */
2947 {
2948     long prev = edit->prev_col;
2949     long over = edit->over_col;
2950 
2951     edit_cursor_move (edit,
2952                       edit_move_forward3 (edit, p, prev + edit->over_col, 0) - edit->buffer.curs1);
2953 
2954     if (option_cursor_beyond_eol)
2955     {
2956         long line_len;
2957 
2958         line_len = (long) edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), 0,
2959                                               edit_buffer_get_current_eol (&edit->buffer));
2960         if (line_len < prev + edit->over_col)
2961         {
2962             edit->over_col = prev + over - line_len;
2963             edit->prev_col = line_len;
2964             edit->curs_col = line_len;
2965         }
2966         else
2967         {
2968             edit->curs_col = prev + over;
2969             edit->prev_col = edit->curs_col;
2970             edit->over_col = 0;
2971         }
2972     }
2973     else
2974     {
2975         edit->over_col = 0;
2976         if (option_fake_half_tabs && is_in_indent (&edit->buffer))
2977         {
2978             long fake_half_tabs;
2979 
2980             edit_update_curs_col (edit);
2981 
2982             fake_half_tabs = HALF_TAB_SIZE * space_width;
2983             if (fake_half_tabs != 0 && edit->curs_col % fake_half_tabs != 0)
2984             {
2985                 long q;
2986 
2987                 q = edit->curs_col;
2988                 edit->curs_col -= (edit->curs_col % fake_half_tabs);
2989                 p = edit_buffer_get_current_bol (&edit->buffer);
2990                 edit_cursor_move (edit,
2991                                   edit_move_forward3 (edit, p, edit->curs_col,
2992                                                       0) - edit->buffer.curs1);
2993                 if (!left_of_four_spaces (edit))
2994                     edit_cursor_move (edit,
2995                                       edit_move_forward3 (edit, p, q, 0) - edit->buffer.curs1);
2996             }
2997         }
2998     }
2999 }
3000 
3001 /* --------------------------------------------------------------------------------------------- */
3002 /** check whether line in editor is blank or not
3003  *
3004  * @param edit editor object
3005  * @param line number of line
3006  *
3007  * @return TRUE if line in blank, FALSE otherwise
3008  */
3009 
3010 gboolean
3011 edit_line_is_blank (WEdit * edit, long line)
     /* [previous][next][first][last][top][bottom][index][help]  */
3012 {
3013     return is_blank (&edit->buffer, edit_find_line (edit, line));
3014 }
3015 
3016 /* --------------------------------------------------------------------------------------------- */
3017 /** move cursor to line 'line' */
3018 
3019 void
3020 edit_move_to_line (WEdit * e, long line)
     /* [previous][next][first][last][top][bottom][index][help]  */
3021 {
3022     if (line < e->buffer.curs_line)
3023         edit_move_up (e, e->buffer.curs_line - line, FALSE);
3024     else
3025         edit_move_down (e, line - e->buffer.curs_line, FALSE);
3026     edit_scroll_screen_over_cursor (e);
3027 }
3028 
3029 /* --------------------------------------------------------------------------------------------- */
3030 /** scroll window so that first visible line is 'line' */
3031 
3032 void
3033 edit_move_display (WEdit * e, long line)
     /* [previous][next][first][last][top][bottom][index][help]  */
3034 {
3035     if (line < e->start_line)
3036         edit_scroll_upward (e, e->start_line - line);
3037     else
3038         edit_scroll_downward (e, line - e->start_line);
3039 }
3040 
3041 /* --------------------------------------------------------------------------------------------- */
3042 /** save markers onto undo stack */
3043 
3044 void
3045 edit_push_markers (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3046 {
3047     edit_push_undo_action (edit, MARK_1 + edit->mark1);
3048     edit_push_undo_action (edit, MARK_2 + edit->mark2);
3049     edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3050 }
3051 
3052 /* --------------------------------------------------------------------------------------------- */
3053 
3054 void
3055 edit_set_markers (WEdit * edit, off_t m1, off_t m2, long c1, long c2)
     /* [previous][next][first][last][top][bottom][index][help]  */
3056 {
3057     edit->mark1 = m1;
3058     edit->mark2 = m2;
3059     edit->column1 = c1;
3060     edit->column2 = c2;
3061 }
3062 
3063 
3064 /* --------------------------------------------------------------------------------------------- */
3065 /** highlight marker toggle */
3066 
3067 void
3068 edit_mark_cmd (WEdit * edit, gboolean unmark)
     /* [previous][next][first][last][top][bottom][index][help]  */
3069 {
3070     edit_push_markers (edit);
3071     if (unmark)
3072     {
3073         edit_set_markers (edit, 0, 0, 0, 0);
3074         edit->force |= REDRAW_PAGE;
3075     }
3076     else if (edit->mark2 >= 0)
3077     {
3078         edit->end_mark_curs = -1;
3079         edit_set_markers (edit, edit->buffer.curs1, -1, edit->curs_col + edit->over_col,
3080                           edit->curs_col + edit->over_col);
3081         edit->force |= REDRAW_PAGE;
3082     }
3083     else
3084     {
3085         edit->end_mark_curs = edit->buffer.curs1;
3086         edit_set_markers (edit, edit->mark1, edit->buffer.curs1, edit->column1,
3087                           edit->curs_col + edit->over_col);
3088     }
3089 }
3090 
3091 /* --------------------------------------------------------------------------------------------- */
3092 /** highlight the word under cursor */
3093 
3094 void
3095 edit_mark_current_word_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3096 {
3097     long pos;
3098 
3099     for (pos = edit->buffer.curs1; pos != 0; pos--)
3100     {
3101         int c1, c2;
3102 
3103         c1 = edit_buffer_get_byte (&edit->buffer, pos);
3104         c2 = edit_buffer_get_byte (&edit->buffer, pos - 1);
3105         if (!isspace (c1) && isspace (c2))
3106             break;
3107         if ((my_type_of (c1) & my_type_of (c2)) == 0)
3108             break;
3109     }
3110     edit->mark1 = pos;
3111 
3112     for (; pos < edit->buffer.size; pos++)
3113     {
3114         int c1, c2;
3115 
3116         c1 = edit_buffer_get_byte (&edit->buffer, pos);
3117         c2 = edit_buffer_get_byte (&edit->buffer, pos + 1);
3118         if (!isspace (c1) && isspace (c2))
3119             break;
3120         if ((my_type_of (c1) & my_type_of (c2)) == 0)
3121             break;
3122     }
3123     edit->mark2 = MIN (pos + 1, edit->buffer.size);
3124 
3125     edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3126 }
3127 
3128 /* --------------------------------------------------------------------------------------------- */
3129 
3130 void
3131 edit_mark_current_line_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3132 {
3133     edit->mark1 = edit_buffer_get_current_bol (&edit->buffer);
3134     edit->mark2 = edit_buffer_get_current_eol (&edit->buffer);
3135 
3136     edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR;
3137 }
3138 
3139 /* --------------------------------------------------------------------------------------------- */
3140 
3141 void
3142 edit_delete_line (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3143 {
3144     /*
3145      * Delete right part of the line.
3146      * Note that edit_buffer_get_byte() returns '\n' when byte position is
3147      *   beyond EOF.
3148      */
3149     while (edit_buffer_get_current_byte (&edit->buffer) != '\n')
3150         (void) edit_delete (edit, TRUE);
3151 
3152     /*
3153      * Delete '\n' char.
3154      * Note that edit_delete() will not corrupt anything if called while
3155      *   cursor position is EOF.
3156      */
3157     (void) edit_delete (edit, TRUE);
3158 
3159     /*
3160      * Delete left part of the line.
3161      * Note, that edit_buffer_get_byte() returns '\n' when byte position is < 0.
3162      */
3163     while (edit_buffer_get_previous_byte (&edit->buffer) != '\n')
3164         (void) edit_backspace (edit, TRUE);
3165 }
3166 
3167 /* --------------------------------------------------------------------------------------------- */
3168 
3169 void
3170 edit_push_key_press (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3171 {
3172     edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3173     if (edit->mark2 == -1)
3174     {
3175         edit_push_undo_action (edit, MARK_1 + edit->mark1);
3176         edit_push_undo_action (edit, MARK_CURS + edit->end_mark_curs);
3177     }
3178 }
3179 
3180 /* --------------------------------------------------------------------------------------------- */
3181 
3182 void
3183 edit_find_bracket (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3184 {
3185     edit->bracket = edit_get_bracket (edit, 1, 10000);
3186     if (edit->last_bracket != edit->bracket)
3187         edit->force |= REDRAW_PAGE;
3188     edit->last_bracket = edit->bracket;
3189 }
3190 
3191 /* --------------------------------------------------------------------------------------------- */
3192 /**
3193  * This executes a command as though the user initiated it through a key
3194  * press.  Callback with MSG_KEY as a message calls this after
3195  * translating the key press.  This function can be used to pass any
3196  * command to the editor.  Note that the screen wouldn't update
3197  * automatically.  Either of command or char_for_insertion must be
3198  * passed as -1.  Commands are executed, and char_for_insertion is
3199  * inserted at the cursor.
3200  */
3201 
3202 void
3203 edit_execute_key_command (WEdit * edit, long command, int char_for_insertion)
     /* [previous][next][first][last][top][bottom][index][help]  */
3204 {
3205     if (command == CK_MacroStartRecord || command == CK_RepeatStartRecord
3206         || (macro_index < 0
3207             && (command == CK_MacroStartStopRecord || command == CK_RepeatStartStopRecord)))
3208     {
3209         macro_index = 0;
3210         edit->force |= REDRAW_CHAR_ONLY | REDRAW_LINE;
3211         return;
3212     }
3213     if (macro_index != -1)
3214     {
3215         edit->force |= REDRAW_COMPLETELY;
3216         if (command == CK_MacroStopRecord || command == CK_MacroStartStopRecord)
3217         {
3218             edit_store_macro_cmd (edit);
3219             macro_index = -1;
3220             return;
3221         }
3222         if (command == CK_RepeatStopRecord || command == CK_RepeatStartStopRecord)
3223         {
3224             edit_repeat_macro_cmd (edit);
3225             macro_index = -1;
3226             return;
3227         }
3228     }
3229 
3230     if (macro_index >= 0 && macro_index < MAX_MACRO_LENGTH - 1)
3231     {
3232         record_macro_buf[macro_index].action = command;
3233         record_macro_buf[macro_index++].ch = char_for_insertion;
3234     }
3235     /* record the beginning of a set of editing actions initiated by a key press */
3236     if (command != CK_Undo && command != CK_ExtendedKeyMap)
3237         edit_push_key_press (edit);
3238 
3239     edit_execute_cmd (edit, command, char_for_insertion);
3240     if (edit->column_highlight)
3241         edit->force |= REDRAW_PAGE;
3242 }
3243 
3244 /* --------------------------------------------------------------------------------------------- */
3245 /**
3246    This executes a command at a lower level than macro recording.
3247    It also does not push a key_press onto the undo stack. This means
3248    that if it is called many times, a single undo command will undo
3249    all of them. It also does not check for the Undo command.
3250  */
3251 void
3252 edit_execute_cmd (WEdit * edit, long command, int char_for_insertion)
     /* [previous][next][first][last][top][bottom][index][help]  */
3253 {
3254     Widget *w = WIDGET (edit);
3255 
3256     if (command == CK_WindowFullscreen)
3257     {
3258         edit_toggle_fullscreen (edit);
3259         return;
3260     }
3261 
3262     /* handle window state */
3263     if (edit_handle_move_resize (edit, command))
3264         return;
3265 
3266     edit->force |= REDRAW_LINE;
3267 
3268     /* The next key press will unhighlight the found string, so update
3269      * the whole page */
3270     if (edit->found_len || edit->column_highlight)
3271         edit->force |= REDRAW_PAGE;
3272 
3273     switch (command)
3274     {
3275         /* a mark command with shift-arrow */
3276     case CK_MarkLeft:
3277     case CK_MarkRight:
3278     case CK_MarkToWordBegin:
3279     case CK_MarkToWordEnd:
3280     case CK_MarkToHome:
3281     case CK_MarkToEnd:
3282     case CK_MarkUp:
3283     case CK_MarkDown:
3284     case CK_MarkPageUp:
3285     case CK_MarkPageDown:
3286     case CK_MarkToFileBegin:
3287     case CK_MarkToFileEnd:
3288     case CK_MarkToPageBegin:
3289     case CK_MarkToPageEnd:
3290     case CK_MarkScrollUp:
3291     case CK_MarkScrollDown:
3292     case CK_MarkParagraphUp:
3293     case CK_MarkParagraphDown:
3294         /* a mark command with alt-arrow */
3295     case CK_MarkColumnPageUp:
3296     case CK_MarkColumnPageDown:
3297     case CK_MarkColumnLeft:
3298     case CK_MarkColumnRight:
3299     case CK_MarkColumnUp:
3300     case CK_MarkColumnDown:
3301     case CK_MarkColumnScrollUp:
3302     case CK_MarkColumnScrollDown:
3303     case CK_MarkColumnParagraphUp:
3304     case CK_MarkColumnParagraphDown:
3305         edit->column_highlight = 0;
3306         if (edit->highlight == 0 || (edit->mark2 != -1 && edit->mark1 != edit->mark2))
3307         {
3308             edit_mark_cmd (edit, TRUE); /* clear */
3309             edit_mark_cmd (edit, FALSE);        /* marking on */
3310         }
3311         edit->highlight = 1;
3312         break;
3313 
3314         /* any other command */
3315     default:
3316         if (edit->highlight)
3317             edit_mark_cmd (edit, FALSE);        /* clear */
3318         edit->highlight = 0;
3319     }
3320 
3321     /* first check for undo */
3322     if (command == CK_Undo)
3323     {
3324         edit->redo_stack_reset = 0;
3325         edit_group_undo (edit);
3326         edit->found_len = 0;
3327         edit->prev_col = edit_get_col (edit);
3328         edit->search_start = edit->buffer.curs1;
3329         return;
3330     }
3331     /*  check for redo */
3332     if (command == CK_Redo)
3333     {
3334         edit->redo_stack_reset = 0;
3335         edit_do_redo (edit);
3336         edit->found_len = 0;
3337         edit->prev_col = edit_get_col (edit);
3338         edit->search_start = edit->buffer.curs1;
3339         return;
3340     }
3341 
3342     edit->redo_stack_reset = 1;
3343 
3344     /* An ordinary key press */
3345     if (char_for_insertion >= 0)
3346     {
3347         /* if non persistent selection and text selected */
3348         if (!option_persistent_selections && edit->mark1 != edit->mark2)
3349             edit_block_delete_cmd (edit);
3350 
3351         if (edit->overwrite)
3352         {
3353             /* remove char only one time, after input first byte, multibyte chars */
3354 #ifdef HAVE_CHARSET
3355             if (!mc_global.utf8_display || edit->charpoint == 0)
3356 #endif
3357                 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
3358 
3359                     edit_delete (edit, FALSE);
3360         }
3361         if (option_cursor_beyond_eol && edit->over_col > 0)
3362             edit_insert_over (edit);
3363 #ifdef HAVE_CHARSET
3364         /**
3365            Encode 8-bit input as UTF-8, if display (locale) is *not* UTF-8,
3366            *but* source encoding *is* set to UTF-8; see ticket #3843 for the details.
3367         */
3368         if (char_for_insertion > 127 && str_isutf8 (get_codepage_id (mc_global.source_codepage))
3369             && !mc_global.utf8_display)
3370         {
3371             unsigned char str[UTF8_CHAR_LEN + 1];
3372             size_t i = 0;
3373             int res;
3374 
3375             res = g_unichar_to_utf8 (char_for_insertion, (char *) str);
3376             if (res == 0)
3377             {
3378                 str[0] = '.';
3379                 str[1] = '\0';
3380             }
3381             else
3382             {
3383                 str[res] = '\0';
3384             }
3385             while (i <= UTF8_CHAR_LEN && str[i] != '\0')
3386             {
3387                 char_for_insertion = str[i];
3388                 edit_insert (edit, char_for_insertion);
3389                 i++;
3390             }
3391         }
3392         else
3393 #endif
3394             edit_insert (edit, char_for_insertion);
3395 
3396         if (option_auto_para_formatting)
3397         {
3398             format_paragraph (edit, FALSE);
3399             edit->force |= REDRAW_PAGE;
3400         }
3401         else
3402             check_and_wrap_line (edit);
3403         edit->found_len = 0;
3404         edit->prev_col = edit_get_col (edit);
3405         edit->search_start = edit->buffer.curs1;
3406         edit_find_bracket (edit);
3407         return;
3408     }
3409 
3410     switch (command)
3411     {
3412     case CK_TopOnScreen:
3413     case CK_BottomOnScreen:
3414     case CK_Top:
3415     case CK_Bottom:
3416     case CK_PageUp:
3417     case CK_PageDown:
3418     case CK_Home:
3419     case CK_End:
3420     case CK_Up:
3421     case CK_Down:
3422     case CK_Left:
3423     case CK_Right:
3424     case CK_WordLeft:
3425     case CK_WordRight:
3426         if (!option_persistent_selections && edit->mark2 >= 0)
3427         {
3428             if (edit->column_highlight)
3429                 edit_push_undo_action (edit, COLUMN_ON);
3430             edit->column_highlight = 0;
3431             edit_mark_cmd (edit, TRUE);
3432         }
3433     default:
3434         break;
3435     }
3436 
3437     switch (command)
3438     {
3439     case CK_TopOnScreen:
3440     case CK_BottomOnScreen:
3441     case CK_MarkToPageBegin:
3442     case CK_MarkToPageEnd:
3443     case CK_Up:
3444     case CK_Down:
3445     case CK_WordLeft:
3446     case CK_WordRight:
3447     case CK_MarkToWordBegin:
3448     case CK_MarkToWordEnd:
3449     case CK_MarkUp:
3450     case CK_MarkDown:
3451     case CK_MarkColumnUp:
3452     case CK_MarkColumnDown:
3453         if (edit->mark2 == -1)
3454             break;              /*marking is following the cursor: may need to highlight a whole line */
3455         MC_FALLTHROUGH;
3456     case CK_Left:
3457     case CK_Right:
3458     case CK_MarkLeft:
3459     case CK_MarkRight:
3460         edit->force |= REDRAW_CHAR_ONLY;
3461     default:
3462         break;
3463     }
3464 
3465     /* basic cursor key commands */
3466     switch (command)
3467     {
3468     case CK_BackSpace:
3469         /* if non persistent selection and text selected */
3470         if (!option_persistent_selections && edit->mark1 != edit->mark2)
3471             edit_block_delete_cmd (edit);
3472         else if (option_cursor_beyond_eol && edit->over_col > 0)
3473             edit->over_col--;
3474         else if (option_backspace_through_tabs && is_in_indent (&edit->buffer))
3475         {
3476             while (edit_buffer_get_previous_byte (&edit->buffer) != '\n' && edit->buffer.curs1 > 0)
3477                 edit_backspace (edit, TRUE);
3478         }
3479         else if (option_fake_half_tabs && is_in_indent (&edit->buffer)
3480                  && right_of_four_spaces (edit))
3481         {
3482             int i;
3483 
3484             for (i = 0; i < HALF_TAB_SIZE; i++)
3485                 edit_backspace (edit, TRUE);
3486         }
3487         else
3488             edit_backspace (edit, FALSE);
3489         break;
3490     case CK_Delete:
3491         /* if non persistent selection and text selected */
3492         if (!option_persistent_selections && edit->mark1 != edit->mark2)
3493             edit_block_delete_cmd (edit);
3494         else
3495         {
3496             if (option_cursor_beyond_eol && edit->over_col > 0)
3497                 edit_insert_over (edit);
3498 
3499             if (option_fake_half_tabs && is_in_indent (&edit->buffer) && left_of_four_spaces (edit))
3500             {
3501                 int i;
3502 
3503                 for (i = 1; i <= HALF_TAB_SIZE; i++)
3504                     edit_delete (edit, TRUE);
3505             }
3506             else
3507                 edit_delete (edit, FALSE);
3508         }
3509         break;
3510     case CK_DeleteToWordBegin:
3511         edit->over_col = 0;
3512         edit_left_delete_word (edit);
3513         break;
3514     case CK_DeleteToWordEnd:
3515         if (option_cursor_beyond_eol && edit->over_col > 0)
3516             edit_insert_over (edit);
3517 
3518         edit_right_delete_word (edit);
3519         break;
3520     case CK_DeleteLine:
3521         edit_delete_line (edit);
3522         break;
3523     case CK_DeleteToHome:
3524         edit_delete_to_line_begin (edit);
3525         break;
3526     case CK_DeleteToEnd:
3527         edit_delete_to_line_end (edit);
3528         break;
3529     case CK_Enter:
3530         edit->over_col = 0;
3531         if (option_auto_para_formatting)
3532         {
3533             edit_double_newline (edit);
3534             if (option_return_does_auto_indent && !bracketed_pasting_in_progress)
3535                 edit_auto_indent (edit);
3536             format_paragraph (edit, FALSE);
3537         }
3538         else
3539         {
3540             edit_insert (edit, '\n');
3541             if (option_return_does_auto_indent && !bracketed_pasting_in_progress)
3542                 edit_auto_indent (edit);
3543         }
3544         break;
3545     case CK_Return:
3546         edit_insert (edit, '\n');
3547         break;
3548 
3549     case CK_MarkColumnPageUp:
3550         edit->column_highlight = 1;
3551         MC_FALLTHROUGH;
3552     case CK_PageUp:
3553     case CK_MarkPageUp:
3554         edit_move_up (edit, w->lines - 1, TRUE);
3555         break;
3556     case CK_MarkColumnPageDown:
3557         edit->column_highlight = 1;
3558         MC_FALLTHROUGH;
3559     case CK_PageDown:
3560     case CK_MarkPageDown:
3561         edit_move_down (edit, w->lines - 1, TRUE);
3562         break;
3563     case CK_MarkColumnLeft:
3564         edit->column_highlight = 1;
3565         MC_FALLTHROUGH;
3566     case CK_Left:
3567     case CK_MarkLeft:
3568         if (option_fake_half_tabs && is_in_indent (&edit->buffer) && right_of_four_spaces (edit))
3569         {
3570             if (option_cursor_beyond_eol && edit->over_col > 0)
3571                 edit->over_col--;
3572             else
3573                 edit_cursor_move (edit, -HALF_TAB_SIZE);
3574             edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3575         }
3576         else
3577             edit_left_char_move_cmd (edit);
3578         break;
3579     case CK_MarkColumnRight:
3580         edit->column_highlight = 1;
3581         MC_FALLTHROUGH;
3582     case CK_Right:
3583     case CK_MarkRight:
3584         if (option_fake_half_tabs && is_in_indent (&edit->buffer) && left_of_four_spaces (edit))
3585         {
3586             edit_cursor_move (edit, HALF_TAB_SIZE);
3587             edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
3588         }
3589         else
3590             edit_right_char_move_cmd (edit);
3591         break;
3592     case CK_TopOnScreen:
3593     case CK_MarkToPageBegin:
3594         edit_begin_page (edit);
3595         break;
3596     case CK_BottomOnScreen:
3597     case CK_MarkToPageEnd:
3598         edit_end_page (edit);
3599         break;
3600     case CK_WordLeft:
3601     case CK_MarkToWordBegin:
3602         edit->over_col = 0;
3603         edit_left_word_move_cmd (edit);
3604         break;
3605     case CK_WordRight:
3606     case CK_MarkToWordEnd:
3607         edit->over_col = 0;
3608         edit_right_word_move_cmd (edit);
3609         break;
3610     case CK_MarkColumnUp:
3611         edit->column_highlight = 1;
3612         MC_FALLTHROUGH;
3613     case CK_Up:
3614     case CK_MarkUp:
3615         edit_move_up (edit, 1, FALSE);
3616         break;
3617     case CK_MarkColumnDown:
3618         edit->column_highlight = 1;
3619         MC_FALLTHROUGH;
3620     case CK_Down:
3621     case CK_MarkDown:
3622         edit_move_down (edit, 1, FALSE);
3623         break;
3624     case CK_MarkColumnParagraphUp:
3625         edit->column_highlight = 1;
3626         MC_FALLTHROUGH;
3627     case CK_ParagraphUp:
3628     case CK_MarkParagraphUp:
3629         edit_move_up_paragraph (edit, FALSE);
3630         break;
3631     case CK_MarkColumnParagraphDown:
3632         edit->column_highlight = 1;
3633         MC_FALLTHROUGH;
3634     case CK_ParagraphDown:
3635     case CK_MarkParagraphDown:
3636         edit_move_down_paragraph (edit, FALSE);
3637         break;
3638     case CK_MarkColumnScrollUp:
3639         edit->column_highlight = 1;
3640         MC_FALLTHROUGH;
3641     case CK_ScrollUp:
3642     case CK_MarkScrollUp:
3643         edit_move_up (edit, 1, TRUE);
3644         break;
3645     case CK_MarkColumnScrollDown:
3646         edit->column_highlight = 1;
3647         MC_FALLTHROUGH;
3648     case CK_ScrollDown:
3649     case CK_MarkScrollDown:
3650         edit_move_down (edit, 1, TRUE);
3651         break;
3652     case CK_Home:
3653     case CK_MarkToHome:
3654         edit_cursor_to_bol (edit);
3655         break;
3656     case CK_End:
3657     case CK_MarkToEnd:
3658         edit_cursor_to_eol (edit);
3659         break;
3660     case CK_Tab:
3661         /* if text marked shift block */
3662         if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3663         {
3664             if (edit->mark2 < 0)
3665                 edit_mark_cmd (edit, FALSE);
3666             edit_move_block_to_right (edit);
3667         }
3668         else
3669         {
3670             if (option_cursor_beyond_eol)
3671                 edit_insert_over (edit);
3672             edit_tab_cmd (edit);
3673             if (option_auto_para_formatting)
3674             {
3675                 format_paragraph (edit, FALSE);
3676                 edit->force |= REDRAW_PAGE;
3677             }
3678             else
3679                 check_and_wrap_line (edit);
3680         }
3681         break;
3682 
3683     case CK_InsertOverwrite:
3684         edit->overwrite = !edit->overwrite;
3685         break;
3686 
3687     case CK_Mark:
3688         if (edit->mark2 >= 0)
3689         {
3690             if (edit->column_highlight)
3691                 edit_push_undo_action (edit, COLUMN_ON);
3692             edit->column_highlight = 0;
3693         }
3694         edit_mark_cmd (edit, FALSE);
3695         break;
3696     case CK_MarkColumn:
3697         if (!edit->column_highlight)
3698             edit_push_undo_action (edit, COLUMN_OFF);
3699         edit->column_highlight = 1;
3700         edit_mark_cmd (edit, FALSE);
3701         break;
3702     case CK_MarkAll:
3703         edit_set_markers (edit, 0, edit->buffer.size, 0, 0);
3704         edit->force |= REDRAW_PAGE;
3705         break;
3706     case CK_Unmark:
3707         if (edit->column_highlight)
3708             edit_push_undo_action (edit, COLUMN_ON);
3709         edit->column_highlight = 0;
3710         edit_mark_cmd (edit, TRUE);
3711         break;
3712     case CK_MarkWord:
3713         if (edit->column_highlight)
3714             edit_push_undo_action (edit, COLUMN_ON);
3715         edit->column_highlight = 0;
3716         edit_mark_current_word_cmd (edit);
3717         break;
3718     case CK_MarkLine:
3719         if (edit->column_highlight)
3720             edit_push_undo_action (edit, COLUMN_ON);
3721         edit->column_highlight = 0;
3722         edit_mark_current_line_cmd (edit);
3723         break;
3724 
3725     case CK_Bookmark:
3726         book_mark_clear (edit, edit->buffer.curs_line, BOOK_MARK_FOUND_COLOR);
3727         if (book_mark_query_color (edit, edit->buffer.curs_line, BOOK_MARK_COLOR))
3728             book_mark_clear (edit, edit->buffer.curs_line, BOOK_MARK_COLOR);
3729         else
3730             book_mark_insert (edit, edit->buffer.curs_line, BOOK_MARK_COLOR);
3731         break;
3732     case CK_BookmarkFlush:
3733         book_mark_flush (edit, BOOK_MARK_COLOR);
3734         book_mark_flush (edit, BOOK_MARK_FOUND_COLOR);
3735         edit->force |= REDRAW_PAGE;
3736         break;
3737     case CK_BookmarkNext:
3738         if (edit->book_mark != NULL)
3739         {
3740             edit_book_mark_t *p;
3741 
3742             p = book_mark_find (edit, edit->buffer.curs_line);
3743             if (p->next != NULL)
3744             {
3745                 p = p->next;
3746                 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
3747                     edit_move_display (edit, p->line - w->lines / 2);
3748                 edit_move_to_line (edit, p->line);
3749             }
3750         }
3751         break;
3752     case CK_BookmarkPrev:
3753         if (edit->book_mark != NULL)
3754         {
3755             edit_book_mark_t *p;
3756 
3757             p = book_mark_find (edit, edit->buffer.curs_line);
3758             while (p->line == edit->buffer.curs_line)
3759                 if (p->prev != NULL)
3760                     p = p->prev;
3761             if (p->line >= 0)
3762             {
3763                 if (p->line >= edit->start_line + w->lines || p->line < edit->start_line)
3764                     edit_move_display (edit, p->line - w->lines / 2);
3765                 edit_move_to_line (edit, p->line);
3766             }
3767         }
3768         break;
3769 
3770     case CK_Top:
3771     case CK_MarkToFileBegin:
3772         edit_move_to_top (edit);
3773         break;
3774     case CK_Bottom:
3775     case CK_MarkToFileEnd:
3776         edit_move_to_bottom (edit);
3777         break;
3778 
3779     case CK_Copy:
3780         if (option_cursor_beyond_eol && edit->over_col > 0)
3781             edit_insert_over (edit);
3782         edit_block_copy_cmd (edit);
3783         break;
3784     case CK_Remove:
3785         edit_block_delete_cmd (edit);
3786         break;
3787     case CK_Move:
3788         edit_block_move_cmd (edit);
3789         break;
3790 
3791     case CK_BlockShiftLeft:
3792         if (edit->mark1 != edit->mark2)
3793             edit_move_block_to_left (edit);
3794         break;
3795     case CK_BlockShiftRight:
3796         if (edit->mark1 != edit->mark2)
3797             edit_move_block_to_right (edit);
3798         break;
3799     case CK_Store:
3800         edit_copy_to_X_buf_cmd (edit);
3801         break;
3802     case CK_Cut:
3803         edit_cut_to_X_buf_cmd (edit);
3804         break;
3805     case CK_Paste:
3806         /* if non persistent selection and text selected */
3807         if (!option_persistent_selections && edit->mark1 != edit->mark2)
3808             edit_block_delete_cmd (edit);
3809         if (option_cursor_beyond_eol && edit->over_col > 0)
3810             edit_insert_over (edit);
3811         edit_paste_from_X_buf_cmd (edit);
3812         if (!option_persistent_selections && edit->mark2 >= 0)
3813         {
3814             if (edit->column_highlight)
3815                 edit_push_undo_action (edit, COLUMN_ON);
3816             edit->column_highlight = 0;
3817             edit_mark_cmd (edit, TRUE);
3818         }
3819         break;
3820     case CK_History:
3821         edit_paste_from_history (edit);
3822         break;
3823 
3824     case CK_SaveAs:
3825         edit_save_as_cmd (edit);
3826         break;
3827     case CK_Save:
3828         edit_save_confirm_cmd (edit);
3829         break;
3830     case CK_BlockSave:
3831         edit_save_block_cmd (edit);
3832         break;
3833     case CK_InsertFile:
3834         edit_insert_file_cmd (edit);
3835         break;
3836 
3837     case CK_FilePrev:
3838         edit_load_back_cmd (edit);
3839         break;
3840     case CK_FileNext:
3841         edit_load_forward_cmd (edit);
3842         break;
3843 
3844     case CK_SyntaxChoose:
3845         edit_syntax_dialog (edit);
3846         break;
3847 
3848     case CK_Search:
3849         edit_search_cmd (edit, FALSE);
3850         break;
3851     case CK_SearchContinue:
3852         edit_search_cmd (edit, TRUE);
3853         break;
3854     case CK_Replace:
3855         edit_replace_cmd (edit, FALSE);
3856         break;
3857     case CK_ReplaceContinue:
3858         edit_replace_cmd (edit, TRUE);
3859         break;
3860     case CK_Complete:
3861         /* if text marked shift block */
3862         if (edit->mark1 != edit->mark2 && !option_persistent_selections)
3863             edit_move_block_to_left (edit);
3864         else
3865             edit_complete_word_cmd (edit);
3866         break;
3867     case CK_Find:
3868         edit_get_match_keyword_cmd (edit);
3869         break;
3870 
3871 #ifdef HAVE_ASPELL
3872     case CK_SpellCheckCurrentWord:
3873         edit_suggest_current_word (edit);
3874         break;
3875     case CK_SpellCheck:
3876         edit_spellcheck_file (edit);
3877         break;
3878     case CK_SpellCheckSelectLang:
3879         edit_set_spell_lang ();
3880         break;
3881 #endif
3882 
3883     case CK_Date:
3884         {
3885             char s[BUF_MEDIUM];
3886             /* fool gcc to prevent a Y2K warning */
3887             char time_format[] = "_c";
3888             time_format[0] = '%';
3889 
3890             FMT_LOCALTIME_CURRENT (s, sizeof (s), time_format);
3891             edit_print_string (edit, s);
3892             edit->force |= REDRAW_PAGE;
3893         }
3894         break;
3895     case CK_Goto:
3896         edit_goto_cmd (edit);
3897         break;
3898     case CK_ParagraphFormat:
3899         format_paragraph (edit, TRUE);
3900         edit->force |= REDRAW_PAGE;
3901         break;
3902     case CK_MacroDelete:
3903         edit_delete_macro_cmd (edit);
3904         break;
3905     case CK_MatchBracket:
3906         edit_goto_matching_bracket (edit);
3907         break;
3908     case CK_UserMenu:
3909         user_menu (edit, NULL, -1);
3910         break;
3911     case CK_Sort:
3912         edit_sort_cmd (edit);
3913         break;
3914     case CK_ExternalCommand:
3915         edit_ext_cmd (edit);
3916         break;
3917     case CK_Mail:
3918         edit_mail_dialog (edit);
3919         break;
3920 #ifdef HAVE_CHARSET
3921     case CK_SelectCodepage:
3922         edit_select_codepage_cmd (edit);
3923         break;
3924 #endif
3925     case CK_InsertLiteral:
3926         edit_insert_literal_cmd (edit);
3927         break;
3928     case CK_MacroStartStopRecord:
3929         edit_begin_end_macro_cmd (edit);
3930         break;
3931     case CK_RepeatStartStopRecord:
3932         edit_begin_end_repeat_cmd (edit);
3933         break;
3934     case CK_ExtendedKeyMap:
3935         edit->extmod = TRUE;
3936         break;
3937     default:
3938         break;
3939     }
3940 
3941     /* CK_PipeBlock */
3942     if ((command / CK_PipeBlock (0)) == 1)
3943         edit_block_process_cmd (edit, command - CK_PipeBlock (0));
3944 
3945     /* keys which must set the col position, and the search vars */
3946     switch (command)
3947     {
3948     case CK_Search:
3949     case CK_SearchContinue:
3950     case CK_Replace:
3951     case CK_ReplaceContinue:
3952     case CK_Complete:
3953         edit->prev_col = edit_get_col (edit);
3954         break;
3955     case CK_Up:
3956     case CK_MarkUp:
3957     case CK_MarkColumnUp:
3958     case CK_Down:
3959     case CK_MarkDown:
3960     case CK_MarkColumnDown:
3961     case CK_PageUp:
3962     case CK_MarkPageUp:
3963     case CK_MarkColumnPageUp:
3964     case CK_PageDown:
3965     case CK_MarkPageDown:
3966     case CK_MarkColumnPageDown:
3967     case CK_Top:
3968     case CK_MarkToFileBegin:
3969     case CK_Bottom:
3970     case CK_MarkToFileEnd:
3971     case CK_ParagraphUp:
3972     case CK_MarkParagraphUp:
3973     case CK_MarkColumnParagraphUp:
3974     case CK_ParagraphDown:
3975     case CK_MarkParagraphDown:
3976     case CK_MarkColumnParagraphDown:
3977     case CK_ScrollUp:
3978     case CK_MarkScrollUp:
3979     case CK_MarkColumnScrollUp:
3980     case CK_ScrollDown:
3981     case CK_MarkScrollDown:
3982     case CK_MarkColumnScrollDown:
3983         edit->search_start = edit->buffer.curs1;
3984         edit->found_len = 0;
3985         break;
3986     default:
3987         edit->found_len = 0;
3988         edit->prev_col = edit_get_col (edit);
3989         edit->search_start = edit->buffer.curs1;
3990     }
3991     edit_find_bracket (edit);
3992 
3993     if (option_auto_para_formatting)
3994     {
3995         switch (command)
3996         {
3997         case CK_BackSpace:
3998         case CK_Delete:
3999         case CK_DeleteToWordBegin:
4000         case CK_DeleteToWordEnd:
4001         case CK_DeleteToHome:
4002         case CK_DeleteToEnd:
4003             format_paragraph (edit, FALSE);
4004             edit->force |= REDRAW_PAGE;
4005         default:
4006             break;
4007         }
4008     }
4009 }
4010 
4011 /* --------------------------------------------------------------------------------------------- */
4012 
4013 void
4014 edit_stack_init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
4015 {
4016     for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4017     {
4018         edit_history_moveto[edit_stack_iterator].filename_vpath = NULL;
4019         edit_history_moveto[edit_stack_iterator].line = -1;
4020     }
4021 
4022     edit_stack_iterator = 0;
4023 }
4024 
4025 /* --------------------------------------------------------------------------------------------- */
4026 
4027 void
4028 edit_stack_free (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
4029 {
4030     for (edit_stack_iterator = 0; edit_stack_iterator < MAX_HISTORY_MOVETO; edit_stack_iterator++)
4031         vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
4032 }
4033 
4034 /* --------------------------------------------------------------------------------------------- */
4035 /** move i lines */
4036 
4037 void
4038 edit_move_up (WEdit * edit, long i, gboolean do_scroll)
     /* [previous][next][first][last][top][bottom][index][help]  */
4039 {
4040     edit_move_updown (edit, i, do_scroll, TRUE);
4041 }
4042 
4043 /* --------------------------------------------------------------------------------------------- */
4044 /** move i lines */
4045 
4046 void
4047 edit_move_down (WEdit * edit, long i, gboolean do_scroll)
     /* [previous][next][first][last][top][bottom][index][help]  */
4048 {
4049     edit_move_updown (edit, i, do_scroll, FALSE);
4050 }
4051 
4052 /* --------------------------------------------------------------------------------------------- */

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