root/src/editor/editcmd.c

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

DEFINITIONS

This source file includes following definitions.
  1. edit_search_status_update_cb
  2. edit_save_mode_callback
  3. edit_save_file
  4. edit_check_newline
  5. edit_get_save_file_as
  6. edit_save_cmd
  7. edit_load_file_from_filename
  8. edit_delete_column_of_text
  9. edit_block_delete
  10. edit_search_get_current_end_line_char
  11. edit_get_search_line_type
  12. edit_calculate_start_of_next_line
  13. edit_calculate_end_of_previous_line
  14. edit_calculate_start_of_previous_line
  15. edit_calculate_start_of_current_line
  16. edit_search_fix_search_start_if_selection
  17. editcmd_find
  18. edit_replace_cmd__conv_to_display
  19. edit_replace_cmd__conv_to_input
  20. edit_show_search_error
  21. edit_do_search
  22. edit_search
  23. edit_get_block
  24. edit_save_block_to_clip_file
  25. pipe_mail
  26. edit_find_word_start
  27. edit_collect_completions_get_current_word
  28. edit_collect_completions
  29. edit_insert_column_of_text
  30. edit_macro_comparator
  31. edit_macro_sort_by_hotkey
  32. edit_get_macro
  33. edit_delete_macro
  34. edit_syntax_onoff_cb
  35. edit_complete_word_insert_recoded_completion
  36. edit_refresh_cmd
  37. edit_syntax_onoff_cmd
  38. edit_show_tabs_tws_cmd
  39. edit_show_margin_cmd
  40. edit_show_numbers_cmd
  41. edit_save_mode_cmd
  42. edit_set_filename
  43. edit_save_as_cmd
  44. edit_delete_macro_cmd
  45. edit_execute_macro
  46. edit_store_macro_cmd
  47. edit_repeat_macro_cmd
  48. edit_load_macro_cmd
  49. edit_save_confirm_cmd
  50. edit_load_cmd
  51. edit_load_file_from_history
  52. edit_load_syntax_file
  53. edit_load_menu_file
  54. edit_close_cmd
  55. eval_marks
  56. edit_block_copy_cmd
  57. edit_block_move_cmd
  58. edit_block_delete_cmd
  59. edit_replace_cmd
  60. edit_search_cmd_callback
  61. edit_search_update_callback
  62. edit_search_cmd
  63. edit_ok_to_exit
  64. edit_save_block
  65. edit_paste_from_history
  66. edit_copy_to_X_buf_cmd
  67. edit_cut_to_X_buf_cmd
  68. edit_paste_from_X_buf_cmd
  69. edit_goto_cmd
  70. edit_save_block_cmd
  71. edit_insert_file_cmd
  72. edit_sort_cmd
  73. edit_ext_cmd
  74. edit_block_process_cmd
  75. edit_mail_dialog
  76. edit_complete_word_cmd
  77. edit_select_codepage_cmd
  78. edit_insert_literal_cmd
  79. edit_begin_end_macro_cmd
  80. edit_begin_end_repeat_cmd
  81. edit_load_forward_cmd
  82. edit_load_back_cmd
  83. edit_get_match_keyword_cmd
  84. edit_suggest_current_word
  85. edit_spellcheck_file
  86. edit_set_spell_lang

   1 /*
   2    Editor high level editing commands
   3 
   4    Copyright (C) 1996-2020
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Paul Sheer, 1996, 1997
   9    Andrew Borodin <aborodin@vmail.ru>, 2012-2014
  10    Ilia Maslakov <il.smind@gmail.com>, 2012
  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 high level editing commands
  30  *  \author Paul Sheer
  31  *  \date 1996, 1997
  32  */
  33 
  34 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
  35 
  36 #include <config.h>
  37 
  38 #include <ctype.h>
  39 #include <stdio.h>
  40 #include <stdarg.h>
  41 #include <sys/types.h>
  42 #include <unistd.h>
  43 #include <string.h>
  44 #include <errno.h>
  45 #include <sys/stat.h>
  46 #include <stdlib.h>
  47 
  48 #include "lib/global.h"
  49 #include "lib/tty/tty.h"
  50 #include "lib/tty/key.h"        /* XCTRL */
  51 #include "lib/mcconfig.h"
  52 #include "lib/skin.h"
  53 #include "lib/strutil.h"        /* utf string functions */
  54 #include "lib/fileloc.h"
  55 #include "lib/lock.h"
  56 #include "lib/util.h"           /* tilde_expand() */
  57 #include "lib/vfs/vfs.h"
  58 #include "lib/widget.h"
  59 #include "lib/event.h"          /* mc_event_raise() */
  60 #ifdef HAVE_CHARSET
  61 #include "lib/charsets.h"
  62 #endif
  63 
  64 #include "src/history.h"
  65 #include "src/file_history.h"   /* show_file_history() */
  66 #include "src/setup.h"          /* option_tab_spacing */
  67 #ifdef HAVE_CHARSET
  68 #include "src/selcodepage.h"
  69 #endif
  70 #include "src/keybind-defaults.h"
  71 #include "src/util.h"           /* check_for_default() */
  72 
  73 #include "edit-impl.h"
  74 #include "editwidget.h"
  75 #include "editcmd_dialogs.h"
  76 #ifdef HAVE_ASPELL
  77 #include "spell.h"
  78 #include "spell_dialogs.h"
  79 #endif
  80 #include "etags.h"
  81 
  82 /*** global variables ****************************************************************************/
  83 
  84 /* search and replace: */
  85 int search_create_bookmark = FALSE;
  86 
  87 /* queries on a save */
  88 gboolean edit_confirm_save = TRUE;
  89 
  90 /* whether we need to drop selection on copy to buffer */
  91 gboolean option_drop_selection_on_copy = TRUE;
  92 
  93 /*** file scope macro definitions ****************************************************************/
  94 
  95 #define space_width 1
  96 
  97 #define TEMP_BUF_LEN 1024
  98 
  99 #define MAX_WORD_COMPLETIONS 100        /* in listbox */
 100 
 101 /*** file scope type declarations ****************************************************************/
 102 
 103 typedef struct
 104 {
 105     simple_status_msg_t status_msg;     /* base class */
 106 
 107     gboolean first;
 108     WEdit *edit;
 109     off_t offset;
 110 } edit_search_status_msg_t;
 111 
 112 /*** file scope variables ************************************************************************/
 113 
 114 static unsigned long edit_save_mode_radio_id, edit_save_mode_input_id;
 115 
 116 /* --------------------------------------------------------------------------------------------- */
 117 /*** file scope functions ************************************************************************/
 118 /* --------------------------------------------------------------------------------------------- */
 119 
 120 static int
 121 edit_search_status_update_cb (status_msg_t * sm)
     /* [previous][next][first][last][top][bottom][index][help]  */
 122 {
 123     simple_status_msg_t *ssm = SIMPLE_STATUS_MSG (sm);
 124     edit_search_status_msg_t *esm = (edit_search_status_msg_t *) sm;
 125     Widget *wd = WIDGET (sm->dlg);
 126 
 127     if (verbose)
 128         label_set_textv (ssm->label, _("Searching %s: %3d%%"), esm->edit->last_search_string,
 129                          edit_buffer_calc_percent (&esm->edit->buffer, esm->offset));
 130     else
 131         label_set_textv (ssm->label, _("Searching %s"), esm->edit->last_search_string);
 132 
 133     if (esm->first)
 134     {
 135         int wd_width;
 136         Widget *lw = WIDGET (ssm->label);
 137 
 138         wd_width = MAX (wd->cols, lw->cols + 6);
 139         widget_set_size (wd, wd->y, wd->x, wd->lines, wd_width);
 140         widget_set_size (lw, lw->y, wd->x + (wd->cols - lw->cols) / 2, lw->lines, lw->cols);
 141         esm->first = FALSE;
 142     }
 143 
 144     return status_msg_common_update (sm);
 145 }
 146 
 147 /* --------------------------------------------------------------------------------------------- */
 148 
 149 static cb_ret_t
 150 edit_save_mode_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 151 {
 152     switch (msg)
 153     {
 154     case MSG_CHANGED_FOCUS:
 155         if (sender != NULL && sender->id == edit_save_mode_radio_id)
 156         {
 157             Widget *ww;
 158 
 159             ww = widget_find_by_id (w, edit_save_mode_input_id);
 160             widget_disable (ww, RADIO (sender)->sel != 2);
 161             return MSG_HANDLED;
 162         }
 163         return MSG_NOT_HANDLED;
 164 
 165     default:
 166         return dlg_default_callback (w, sender, msg, parm, data);
 167     }
 168 }
 169 
 170 /* --------------------------------------------------------------------------------------------- */
 171 
 172 /*  If 0 (quick save) then  a) create/truncate <filename> file,
 173    b) save to <filename>;
 174    if 1 (safe save) then   a) save to <tempnam>,
 175    b) rename <tempnam> to <filename>;
 176    if 2 (do backups) then  a) save to <tempnam>,
 177    b) rename <filename> to <filename.backup_ext>,
 178    c) rename <tempnam> to <filename>. */
 179 
 180 /* returns 0 on error, -1 on abort */
 181 
 182 static int
 183 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 184 {
 185     char *p;
 186     gchar *tmp;
 187     off_t filelen = 0;
 188     int this_save_mode, rv, fd = -1;
 189     vfs_path_t *real_filename_vpath;
 190     vfs_path_t *savename_vpath = NULL;
 191     const char *start_filename;
 192     const vfs_path_element_t *vpath_element;
 193     struct stat sb;
 194 
 195     vpath_element = vfs_path_get_by_index (filename_vpath, 0);
 196     if (vpath_element == NULL)
 197         return 0;
 198 
 199     start_filename = vpath_element->path;
 200     if (*start_filename == '\0')
 201         return 0;
 202 
 203     if (!IS_PATH_SEP (*start_filename) && edit->dir_vpath != NULL)
 204         real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
 205     else
 206         real_filename_vpath = vfs_path_clone (filename_vpath);
 207 
 208     this_save_mode = option_save_mode;
 209     if (this_save_mode != EDIT_QUICK_SAVE)
 210     {
 211         if (!vfs_file_is_local (real_filename_vpath))
 212             /* The file does not exists yet, so no safe save or backup are necessary. */
 213             this_save_mode = EDIT_QUICK_SAVE;
 214         else
 215         {
 216             fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY);
 217             if (fd == -1)
 218                 /* The file does not exists yet, so no safe save or backup are necessary. */
 219                 this_save_mode = EDIT_QUICK_SAVE;
 220         }
 221 
 222         if (fd != -1)
 223             mc_close (fd);
 224     }
 225 
 226     rv = mc_stat (real_filename_vpath, &sb);
 227     if (rv == 0)
 228     {
 229         if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt && sb.st_nlink > 1)
 230         {
 231             rv = edit_query_dialog3 (_("Warning"),
 232                                      _("File has hard-links. Detach before saving?"),
 233                                      _("&Yes"), _("&No"), _("&Cancel"));
 234             switch (rv)
 235             {
 236             case 0:
 237                 this_save_mode = EDIT_SAFE_SAVE;
 238                 MC_FALLTHROUGH;
 239             case 1:
 240                 edit->skip_detach_prompt = 1;
 241                 break;
 242             default:
 243                 vfs_path_free (real_filename_vpath);
 244                 return -1;
 245             }
 246         }
 247 
 248         /* Prevent overwriting changes from other editor sessions. */
 249         if (edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
 250         {
 251             /* The default action is "Cancel". */
 252             query_set_sel (1);
 253 
 254             rv = edit_query_dialog2 (_("Warning"),
 255                                      _("The file has been modified in the meantime. Save anyway?"),
 256                                      _("&Yes"), _("&Cancel"));
 257             if (rv != 0)
 258             {
 259                 vfs_path_free (real_filename_vpath);
 260                 return -1;
 261             }
 262         }
 263     }
 264 
 265     if (this_save_mode == EDIT_QUICK_SAVE)
 266         savename_vpath = vfs_path_clone (real_filename_vpath);
 267     else
 268     {
 269         char *savedir, *saveprefix;
 270 
 271         savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
 272         if (savedir == NULL)
 273             savedir = g_strdup (".");
 274 
 275         /* Token-related function never return leading slash, so we need add it manually */
 276         saveprefix = mc_build_filename (PATH_SEP_STR, savedir, "cooledit", (char *) NULL);
 277         g_free (savedir);
 278         fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
 279         g_free (saveprefix);
 280         if (savename_vpath == NULL)
 281         {
 282             vfs_path_free (real_filename_vpath);
 283             return 0;
 284         }
 285         /* FIXME:
 286          * Close for now because mc_mkstemps use pure open system call
 287          * to create temporary file and it needs to be reopened by
 288          * VFS-aware mc_open().
 289          */
 290         close (fd);
 291     }
 292 
 293     (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
 294     (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
 295 
 296     fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
 297     if (fd == -1)
 298         goto error_save;
 299 
 300     /* pipe save */
 301     p = edit_get_write_filter (savename_vpath, real_filename_vpath);
 302     if (p != NULL)
 303     {
 304         FILE *file;
 305 
 306         mc_close (fd);
 307         file = (FILE *) popen (p, "w");
 308 
 309         if (file != NULL)
 310         {
 311             filelen = edit_write_stream (edit, file);
 312 #if 1
 313             pclose (file);
 314 #else
 315             if (pclose (file) != 0)
 316             {
 317                 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
 318                 edit_error_dialog (_("Error"), tmp);
 319                 g_free (tmp);
 320                 g_free (p);
 321                 goto error_save;
 322             }
 323 #endif
 324         }
 325         else
 326         {
 327             tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
 328             edit_error_dialog (_("Error"), get_sys_error (tmp));
 329             g_free (p);
 330             g_free (tmp);
 331             goto error_save;
 332         }
 333         g_free (p);
 334     }
 335     else if (edit->lb == LB_ASIS)
 336     {                           /* do not change line breaks */
 337         filelen = edit_buffer_write_file (&edit->buffer, fd);
 338 
 339         if (filelen != edit->buffer.size)
 340         {
 341             mc_close (fd);
 342             goto error_save;
 343         }
 344 
 345         if (mc_close (fd) != 0)
 346             goto error_save;
 347 
 348         /* Update the file information, especially the mtime. */
 349         if (mc_stat (savename_vpath, &edit->stat1) == -1)
 350             goto error_save;
 351     }
 352     else
 353     {                           /* change line breaks */
 354         FILE *file;
 355         const vfs_path_element_t *path_element;
 356 
 357         mc_close (fd);
 358 
 359         path_element = vfs_path_get_by_index (savename_vpath, -1);
 360         file = (FILE *) fopen (path_element->path, "w");
 361         if (file != NULL)
 362         {
 363             filelen = edit_write_stream (edit, file);
 364             fclose (file);
 365         }
 366         else
 367         {
 368             char *msg;
 369 
 370             msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
 371             edit_error_dialog (_("Error"), msg);
 372             g_free (msg);
 373             goto error_save;
 374         }
 375     }
 376 
 377     if (filelen != edit->buffer.size)
 378         goto error_save;
 379 
 380     if (this_save_mode == EDIT_DO_BACKUP)
 381     {
 382         char *tmp_store_filename;
 383         vfs_path_element_t *last_vpath_element;
 384         vfs_path_t *tmp_vpath;
 385         gboolean ok;
 386 
 387         g_assert (option_backup_ext != NULL);
 388 
 389         /* add backup extension to the path */
 390         tmp_vpath = vfs_path_clone (real_filename_vpath);
 391         last_vpath_element = (vfs_path_element_t *) vfs_path_get_by_index (tmp_vpath, -1);
 392         tmp_store_filename = last_vpath_element->path;
 393         last_vpath_element->path = g_strdup_printf ("%s%s", tmp_store_filename, option_backup_ext);
 394         g_free (tmp_store_filename);
 395 
 396         ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
 397         vfs_path_free (tmp_vpath);
 398         if (!ok)
 399             goto error_save;
 400     }
 401 
 402     if (this_save_mode != EDIT_QUICK_SAVE && mc_rename (savename_vpath, real_filename_vpath) == -1)
 403         goto error_save;
 404 
 405     vfs_path_free (real_filename_vpath);
 406     vfs_path_free (savename_vpath);
 407     return 1;
 408   error_save:
 409     /*  FIXME: Is this safe ?
 410      *  if (this_save_mode != EDIT_QUICK_SAVE)
 411      *      mc_unlink (savename);
 412      */
 413     vfs_path_free (real_filename_vpath);
 414     vfs_path_free (savename_vpath);
 415     return 0;
 416 }
 417 
 418 /* --------------------------------------------------------------------------------------------- */
 419 
 420 static gboolean
 421 edit_check_newline (const edit_buffer_t * buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
 422 {
 423     return !(option_check_nl_at_eof && buf->size > 0
 424              && edit_buffer_get_byte (buf, buf->size - 1) != '\n'
 425              && edit_query_dialog2 (_("Warning"),
 426                                     _("The file you are saving does not end with a newline."),
 427                                     _("C&ontinue"), _("&Cancel")) != 0);
 428 }
 429 
 430 /* --------------------------------------------------------------------------------------------- */
 431 
 432 static vfs_path_t *
 433 edit_get_save_file_as (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 434 {
 435     static LineBreaks cur_lb = LB_ASIS;
 436     char *filename_res;
 437     vfs_path_t *ret_vpath = NULL;
 438 
 439     const char *lb_names[LB_NAMES] = {
 440         N_("&Do not change"),
 441         N_("&Unix format (LF)"),
 442         N_("&Windows/DOS format (CR LF)"),
 443         N_("&Macintosh format (CR)")
 444     };
 445 
 446     quick_widget_t quick_widgets[] = {
 447         /* *INDENT-OFF* */
 448         QUICK_LABELED_INPUT (N_("Enter file name:"), input_label_above,
 449                              vfs_path_as_str (edit->filename_vpath), "save-as",
 450                              &filename_res, NULL, FALSE, FALSE, INPUT_COMPLETE_FILENAMES),
 451         QUICK_SEPARATOR (TRUE),
 452         QUICK_LABEL (N_("Change line breaks to:"), NULL),
 453         QUICK_RADIO (LB_NAMES, lb_names, (int *) &cur_lb, NULL),
 454         QUICK_BUTTONS_OK_CANCEL,
 455         QUICK_END
 456         /* *INDENT-ON* */
 457     };
 458 
 459     quick_dialog_t qdlg = {
 460         -1, -1, 64,
 461         N_("Save As"), "[Save File As]",
 462         quick_widgets, NULL, NULL
 463     };
 464 
 465     if (quick_dialog (&qdlg) != B_CANCEL)
 466     {
 467         char *fname;
 468 
 469         edit->lb = cur_lb;
 470         fname = tilde_expand (filename_res);
 471         g_free (filename_res);
 472         ret_vpath = vfs_path_from_str (fname);
 473         g_free (fname);
 474     }
 475 
 476     return ret_vpath;
 477 }
 478 
 479 /* --------------------------------------------------------------------------------------------- */
 480 
 481 /** returns TRUE on success */
 482 
 483 static gboolean
 484 edit_save_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 485 {
 486     int res, save_lock = 0;
 487 
 488     if (!edit->locked && !edit->delete_file)
 489         save_lock = lock_file (edit->filename_vpath);
 490     res = edit_save_file (edit, edit->filename_vpath);
 491 
 492     /* Maintain modify (not save) lock on failure */
 493     if ((res > 0 && edit->locked) || save_lock)
 494         edit->locked = unlock_file (edit->filename_vpath);
 495 
 496     /* On failure try 'save as', it does locking on its own */
 497     if (res == 0)
 498         return edit_save_as_cmd (edit);
 499 
 500     if (res > 0)
 501     {
 502         edit->delete_file = 0;
 503         edit->modified = 0;
 504     }
 505 
 506     edit->force |= REDRAW_COMPLETELY;
 507 
 508     return TRUE;
 509 }
 510 
 511 /* --------------------------------------------------------------------------------------------- */
 512 /**
 513  * Load file content
 514  *
 515  * @param h screen the owner of editor window
 516  * @param vpath vfs file path
 517  * @return TRUE if file content was successfully loaded, FALSE otherwise
 518  */
 519 
 520 static inline gboolean
 521 edit_load_file_from_filename (WDialog * h, const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 522 {
 523     Widget *w = WIDGET (h);
 524 
 525     return edit_add_window (h, w->y + 1, w->x, w->lines - 2, w->cols, vpath, 0);
 526 }
 527 
 528 /* --------------------------------------------------------------------------------------------- */
 529 
 530 static void
 531 edit_delete_column_of_text (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 532 {
 533     off_t m1, m2;
 534     off_t n;
 535     long b, c, d;
 536 
 537     eval_marks (edit, &m1, &m2);
 538     n = edit_buffer_get_forward_offset (&edit->buffer, m1, 0, m2) + 1;
 539     c = (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, m1), 0, m1);
 540     d = (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, m2), 0, m2);
 541     b = MAX (MIN (c, d), MIN (edit->column1, edit->column2));
 542     c = MAX (c, MAX (edit->column1, edit->column2));
 543 
 544     while (n-- != 0)
 545     {
 546         off_t r, p, q;
 547 
 548         r = edit_buffer_get_current_bol (&edit->buffer);
 549         p = edit_move_forward3 (edit, r, b, 0);
 550         q = edit_move_forward3 (edit, r, c, 0);
 551         p = MAX (p, m1);
 552         q = MIN (q, m2);
 553         edit_cursor_move (edit, p - edit->buffer.curs1);
 554         /* delete line between margins */
 555         for (; q > p; q--)
 556             if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
 557                 edit_delete (edit, TRUE);
 558 
 559         /* move to next line except on the last delete */
 560         if (n != 0)
 561             edit_cursor_move (edit,
 562                               edit_buffer_get_forward_offset (&edit->buffer, edit->buffer.curs1, 1,
 563                                                               0) - edit->buffer.curs1);
 564     }
 565 }
 566 
 567 /* --------------------------------------------------------------------------------------------- */
 568 /** if success return 0 */
 569 
 570 static int
 571 edit_block_delete (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 572 {
 573     off_t start_mark, end_mark;
 574     off_t curs_pos;
 575     long curs_line, c1, c2;
 576 
 577     if (!eval_marks (edit, &start_mark, &end_mark))
 578         return 0;
 579 
 580     if (edit->column_highlight && edit->mark2 < 0)
 581         edit_mark_cmd (edit, FALSE);
 582 
 583     /* Warning message with a query to continue or cancel the operation */
 584     if ((end_mark - start_mark) > option_max_undo / 2 &&
 585         edit_query_dialog2 (_("Warning"),
 586                             ("Block is large, you may not be able to undo this action"),
 587                             _("C&ontinue"), _("&Cancel")) != 0)
 588         return 1;
 589 
 590     c1 = MIN (edit->column1, edit->column2);
 591     c2 = MAX (edit->column1, edit->column2);
 592     edit->column1 = c1;
 593     edit->column2 = c2;
 594 
 595     edit_push_markers (edit);
 596 
 597     curs_line = edit->buffer.curs_line;
 598 
 599     curs_pos = edit->curs_col + edit->over_col;
 600 
 601     /* move cursor to start of selection */
 602     edit_cursor_move (edit, start_mark - edit->buffer.curs1);
 603     edit_scroll_screen_over_cursor (edit);
 604 
 605     if (start_mark < end_mark)
 606     {
 607         if (edit->column_highlight)
 608         {
 609             off_t line_width;
 610 
 611             if (edit->mark2 < 0)
 612                 edit_mark_cmd (edit, FALSE);
 613             edit_delete_column_of_text (edit);
 614             /* move cursor to the saved position */
 615             edit_move_to_line (edit, curs_line);
 616             /* calculate line width and cursor position before cut */
 617             line_width = edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), 0,
 618                                              edit_buffer_get_current_eol (&edit->buffer));
 619             if (option_cursor_beyond_eol && curs_pos > line_width)
 620                 edit->over_col = curs_pos - line_width;
 621         }
 622         else
 623         {
 624             off_t count;
 625 
 626             for (count = start_mark; count < end_mark; count++)
 627                 edit_delete (edit, TRUE);
 628         }
 629     }
 630 
 631     edit_set_markers (edit, 0, 0, 0, 0);
 632     edit->force |= REDRAW_PAGE;
 633 
 634     return 0;
 635 }
 636 
 637 /* --------------------------------------------------------------------------------------------- */
 638 /**
 639  * Get EOL symbol for searching.
 640  *
 641  * @param edit editor object
 642  * @return EOL symbol
 643  */
 644 
 645 static inline char
 646 edit_search_get_current_end_line_char (const WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 647 {
 648     switch (edit->lb)
 649     {
 650     case LB_MAC:
 651         return '\r';
 652     default:
 653         return '\n';
 654     }
 655 }
 656 
 657 /* --------------------------------------------------------------------------------------------- */
 658 /**
 659  * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
 660  *
 661  * @param search search object
 662  * @return result of checks.
 663  */
 664 
 665 static edit_search_line_t
 666 edit_get_search_line_type (mc_search_t * search)
     /* [previous][next][first][last][top][bottom][index][help]  */
 667 {
 668     edit_search_line_t search_line_type = 0;
 669 
 670     if (search->search_type != MC_SEARCH_T_REGEX)
 671         return search_line_type;
 672 
 673     if (*search->original == '^')
 674         search_line_type |= AT_START_LINE;
 675 
 676     if (search->original[search->original_len - 1] == '$')
 677         search_line_type |= AT_END_LINE;
 678     return search_line_type;
 679 }
 680 
 681 /* --------------------------------------------------------------------------------------------- */
 682 /**
 683  * Calculating the start position of next line.
 684  *
 685  * @param buf               editor buffer object
 686  * @param current_pos       current position
 687  * @param max_pos           max position
 688  * @param end_string_symbol end of line symbol
 689  * @return start position of next line
 690  */
 691 
 692 static off_t
 693 edit_calculate_start_of_next_line (const edit_buffer_t * buf, off_t current_pos, off_t max_pos,
     /* [previous][next][first][last][top][bottom][index][help]  */
 694                                    char end_string_symbol)
 695 {
 696     off_t i;
 697 
 698     for (i = current_pos; i < max_pos; i++)
 699     {
 700         current_pos++;
 701         if (edit_buffer_get_byte (buf, i) == end_string_symbol)
 702             break;
 703     }
 704 
 705     return current_pos;
 706 }
 707 
 708 /* --------------------------------------------------------------------------------------------- */
 709 /**
 710  * Calculating the end position of previous line.
 711  *
 712  * @param buf               editor buffer object
 713  * @param current_pos       current position
 714  * @param end_string_symbol end of line symbol
 715  * @return end position of previous line
 716  */
 717 
 718 static off_t
 719 edit_calculate_end_of_previous_line (const edit_buffer_t * buf, off_t current_pos,
     /* [previous][next][first][last][top][bottom][index][help]  */
 720                                      char end_string_symbol)
 721 {
 722     off_t i;
 723 
 724     for (i = current_pos - 1; i >= 0; i--)
 725         if (edit_buffer_get_byte (buf, i) == end_string_symbol)
 726             break;
 727 
 728     return i;
 729 }
 730 
 731 /* --------------------------------------------------------------------------------------------- */
 732 /**
 733  * Calculating the start position of previous line.
 734  *
 735  * @param buf               editor buffer object
 736  * @param current_pos       current position
 737  * @param end_string_symbol end of line symbol
 738  * @return start position of previous line
 739  */
 740 
 741 static inline off_t
 742 edit_calculate_start_of_previous_line (const edit_buffer_t * buf, off_t current_pos,
     /* [previous][next][first][last][top][bottom][index][help]  */
 743                                        char end_string_symbol)
 744 {
 745     current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
 746     current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
 747 
 748     return (current_pos + 1);
 749 }
 750 
 751 /* --------------------------------------------------------------------------------------------- */
 752 /**
 753  * Calculating the start position of current line.
 754  *
 755  * @param buf               editor buffer object
 756  * @param current_pos       current position
 757  * @param end_string_symbol end of line symbol
 758  * @return start position of current line
 759  */
 760 
 761 static inline off_t
 762 edit_calculate_start_of_current_line (const edit_buffer_t * buf, off_t current_pos,
     /* [previous][next][first][last][top][bottom][index][help]  */
 763                                       char end_string_symbol)
 764 {
 765     current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
 766 
 767     return (current_pos + 1);
 768 }
 769 
 770 /* --------------------------------------------------------------------------------------------- */
 771 /**
 772  * Fixing (if needed) search start position if 'only in selection' option present.
 773  *
 774  * @param edit              editor object
 775  */
 776 
 777 static void
 778 edit_search_fix_search_start_if_selection (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 779 {
 780     off_t start_mark = 0;
 781     off_t end_mark = 0;
 782 
 783     if (!edit_search_options.only_in_selection)
 784         return;
 785 
 786     if (!eval_marks (edit, &start_mark, &end_mark))
 787         return;
 788 
 789     if (edit_search_options.backwards)
 790     {
 791         if (edit->search_start > end_mark || edit->search_start <= start_mark)
 792             edit->search_start = end_mark;
 793     }
 794     else
 795     {
 796         if (edit->search_start < start_mark || edit->search_start >= end_mark)
 797             edit->search_start = start_mark;
 798     }
 799 }
 800 
 801 /* --------------------------------------------------------------------------------------------- */
 802 
 803 static gboolean
 804 editcmd_find (edit_search_status_msg_t * esm, gsize * len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 805 {
 806     WEdit *edit = esm->edit;
 807     off_t search_start = edit->search_start;
 808     off_t search_end;
 809     off_t start_mark = 0;
 810     off_t end_mark = edit->buffer.size;
 811     char end_string_symbol;
 812 
 813     end_string_symbol = edit_search_get_current_end_line_char (edit);
 814 
 815     /* prepare for search */
 816     if (edit_search_options.only_in_selection)
 817     {
 818         if (!eval_marks (edit, &start_mark, &end_mark))
 819         {
 820             mc_search_set_error (edit->search, MC_SEARCH_E_NOTFOUND, "%s", _(STR_E_NOTFOUND));
 821             return FALSE;
 822         }
 823 
 824         /* fix the start and the end of search block positions */
 825         if ((edit->search_line_type & AT_START_LINE) != 0
 826             && (start_mark != 0
 827                 || edit_buffer_get_byte (&edit->buffer, start_mark - 1) != end_string_symbol))
 828             start_mark =
 829                 edit_calculate_start_of_next_line (&edit->buffer, start_mark, edit->buffer.size,
 830                                                    end_string_symbol);
 831 
 832         if ((edit->search_line_type & AT_END_LINE) != 0
 833             && (end_mark - 1 != edit->buffer.size
 834                 || edit_buffer_get_byte (&edit->buffer, end_mark) != end_string_symbol))
 835             end_mark =
 836                 edit_calculate_end_of_previous_line (&edit->buffer, end_mark, end_string_symbol);
 837 
 838         if (start_mark >= end_mark)
 839         {
 840             mc_search_set_error (edit->search, MC_SEARCH_E_NOTFOUND, "%s", _(STR_E_NOTFOUND));
 841             return FALSE;
 842         }
 843     }
 844     else if (edit_search_options.backwards)
 845         end_mark = MAX (1, edit->buffer.curs1) - 1;
 846 
 847     /* search */
 848     if (edit_search_options.backwards)
 849     {
 850         /* backward search */
 851         search_end = end_mark;
 852 
 853         if ((edit->search_line_type & AT_START_LINE) != 0)
 854             search_start =
 855                 edit_calculate_start_of_current_line (&edit->buffer, search_start,
 856                                                       end_string_symbol);
 857 
 858         while (search_start >= start_mark)
 859         {
 860             gboolean ok;
 861 
 862             if (search_end > (off_t) (search_start + edit->search->original_len)
 863                 && mc_search_is_fixed_search_str (edit->search))
 864                 search_end = search_start + edit->search->original_len;
 865 
 866             ok = mc_search_run (edit->search, (void *) esm, search_start, search_end, len);
 867 
 868             if (ok && edit->search->normal_offset == search_start)
 869                 return TRUE;
 870 
 871             /* We abort the search in case of a pattern error, or if the user aborts
 872                the search. In other words: in all cases except "string not found". */
 873             if (!ok && edit->search->error != MC_SEARCH_E_NOTFOUND)
 874                 return FALSE;
 875 
 876             if ((edit->search_line_type & AT_START_LINE) != 0)
 877                 search_start =
 878                     edit_calculate_start_of_previous_line (&edit->buffer, search_start,
 879                                                            end_string_symbol);
 880             else
 881                 search_start--;
 882         }
 883 
 884         mc_search_set_error (edit->search, MC_SEARCH_E_NOTFOUND, "%s", _(STR_E_NOTFOUND));
 885         return FALSE;
 886     }
 887 
 888     /* forward search */
 889     if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
 890         search_start =
 891             edit_calculate_start_of_next_line (&edit->buffer, search_start, end_mark,
 892                                                end_string_symbol);
 893 
 894     return mc_search_run (edit->search, (void *) esm, search_start, end_mark, len);
 895 }
 896 
 897 /* --------------------------------------------------------------------------------------------- */
 898 
 899 static char *
 900 edit_replace_cmd__conv_to_display (const char *str)
     /* [previous][next][first][last][top][bottom][index][help]  */
 901 {
 902 #ifdef HAVE_CHARSET
 903     GString *tmp;
 904 
 905     tmp = str_convert_to_display (str);
 906     if (tmp != NULL)
 907     {
 908         if (tmp->len != 0)
 909             return g_string_free (tmp, FALSE);
 910         g_string_free (tmp, TRUE);
 911     }
 912 #endif
 913     return g_strdup (str);
 914 }
 915 
 916 /* --------------------------------------------------------------------------------------------- */
 917 
 918 static char *
 919 edit_replace_cmd__conv_to_input (char *str)
     /* [previous][next][first][last][top][bottom][index][help]  */
 920 {
 921 #ifdef HAVE_CHARSET
 922     GString *tmp;
 923 
 924     tmp = str_convert_to_input (str);
 925     if (tmp->len != 0)
 926         return g_string_free (tmp, FALSE);
 927     g_string_free (tmp, TRUE);
 928 #endif
 929     return g_strdup (str);
 930 }
 931 
 932 /* --------------------------------------------------------------------------------------------- */
 933 
 934 static void
 935 edit_show_search_error (const WEdit * edit, const char *title)
     /* [previous][next][first][last][top][bottom][index][help]  */
 936 {
 937     if (edit->search->error == MC_SEARCH_E_NOTFOUND)
 938         edit_query_dialog (title, _(STR_E_NOTFOUND));
 939     else if (edit->search->error_str != NULL)
 940         edit_query_dialog (title, edit->search->error_str);
 941 }
 942 
 943 /* --------------------------------------------------------------------------------------------- */
 944 
 945 static void
 946 edit_do_search (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
 947 {
 948     edit_search_status_msg_t esm;
 949     gsize len = 0;
 950 
 951     if (edit->search == NULL)
 952         edit->search_start = edit->buffer.curs1;
 953 
 954     edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
 955 
 956     esm.first = TRUE;
 957     esm.edit = edit;
 958     esm.offset = edit->search_start;
 959 
 960     status_msg_init (STATUS_MSG (&esm), _("Search"), 1.0, simple_status_msg_init_cb,
 961                      edit_search_status_update_cb, NULL);
 962 
 963     if (search_create_bookmark)
 964     {
 965         gboolean found = FALSE;
 966         long l = 0, l_last = -1;
 967         long q = 0;
 968 
 969         search_create_bookmark = FALSE;
 970         book_mark_flush (edit, -1);
 971 
 972         while (mc_search_run (edit->search, (void *) &esm, q, edit->buffer.size, &len))
 973         {
 974             if (!found)
 975                 edit->search_start = edit->search->normal_offset;
 976             found = TRUE;
 977 
 978             l += edit_buffer_count_lines (&edit->buffer, q, edit->search->normal_offset);
 979             if (l != l_last)
 980                 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
 981             l_last = l;
 982             q = edit->search->normal_offset + 1;
 983         }
 984 
 985         if (!found)
 986             edit_error_dialog (_("Search"), _(STR_E_NOTFOUND));
 987         else
 988             edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
 989     }
 990     else
 991     {
 992         if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
 993             && edit_search_options.backwards)
 994             edit->search_start--;
 995 
 996         if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
 997             && !edit_search_options.backwards)
 998             edit->search_start++;
 999 
1000         if (editcmd_find (&esm, &len))
1001         {
1002             edit->found_start = edit->search_start = edit->search->normal_offset;
1003             edit->found_len = len;
1004             edit->over_col = 0;
1005             edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
1006             edit_scroll_screen_over_cursor (edit);
1007             if (edit_search_options.backwards)
1008                 edit->search_start--;
1009             else
1010                 edit->search_start++;
1011         }
1012         else
1013         {
1014             edit->search_start = edit->buffer.curs1;
1015             edit_show_search_error (edit, _("Search"));
1016         }
1017     }
1018 
1019     status_msg_deinit (STATUS_MSG (&esm));
1020 
1021     edit->force |= REDRAW_COMPLETELY;
1022     edit_scroll_screen_over_cursor (edit);
1023 }
1024 
1025 /* --------------------------------------------------------------------------------------------- */
1026 
1027 static void
1028 edit_search (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1029 {
1030     if (editcmd_dialog_search_show (edit))
1031     {
1032         edit->search_line_type = edit_get_search_line_type (edit->search);
1033         edit_search_fix_search_start_if_selection (edit);
1034         edit_do_search (edit);
1035     }
1036 }
1037 
1038 /* --------------------------------------------------------------------------------------------- */
1039 /** Return a null terminated length of text. Result must be g_free'd */
1040 
1041 static unsigned char *
1042 edit_get_block (WEdit * edit, off_t start, off_t finish, off_t * l)
     /* [previous][next][first][last][top][bottom][index][help]  */
1043 {
1044     unsigned char *s, *r;
1045 
1046     r = s = g_malloc0 (finish - start + 1);
1047 
1048     if (edit->column_highlight)
1049     {
1050         *l = 0;
1051 
1052         /* copy from buffer, excluding chars that are out of the column 'margins' */
1053         for (; start < finish; start++)
1054         {
1055             int c;
1056             off_t x;
1057 
1058             x = edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, start), 0, start);
1059             c = edit_buffer_get_byte (&edit->buffer, start);
1060             if ((x >= edit->column1 && x < edit->column2)
1061                 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1062             {
1063                 *s++ = c;
1064                 (*l)++;
1065             }
1066         }
1067     }
1068     else
1069     {
1070         *l = finish - start;
1071 
1072         for (; start < finish; start++)
1073             *s++ = edit_buffer_get_byte (&edit->buffer, start);
1074     }
1075 
1076     *s = '\0';
1077 
1078     return r;
1079 }
1080 
1081 /* --------------------------------------------------------------------------------------------- */
1082 /** copies a block to clipboard file */
1083 
1084 static gboolean
1085 edit_save_block_to_clip_file (WEdit * edit, off_t start, off_t finish)
     /* [previous][next][first][last][top][bottom][index][help]  */
1086 {
1087     gboolean ret;
1088     gchar *tmp;
1089 
1090     tmp = mc_config_get_full_path (EDIT_HOME_CLIP_FILE);
1091     ret = edit_save_block (edit, tmp, start, finish);
1092     g_free (tmp);
1093 
1094     return ret;
1095 }
1096 
1097 /* --------------------------------------------------------------------------------------------- */
1098 
1099 static void
1100 pipe_mail (const edit_buffer_t * buf, char *to, char *subject, char *cc)
     /* [previous][next][first][last][top][bottom][index][help]  */
1101 {
1102     FILE *p = 0;
1103     char *s;
1104 
1105     to = name_quote (to, FALSE);
1106     subject = name_quote (subject, FALSE);
1107     cc = name_quote (cc, FALSE);
1108     s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1109     g_free (to);
1110     g_free (subject);
1111     g_free (cc);
1112 
1113     if (s != NULL)
1114     {
1115         p = popen (s, "w");
1116         g_free (s);
1117     }
1118 
1119     if (p != NULL)
1120     {
1121         off_t i;
1122 
1123         for (i = 0; i < buf->size; i++)
1124             fputc (edit_buffer_get_byte (buf, i), p);
1125         pclose (p);
1126     }
1127 }
1128 
1129 /* --------------------------------------------------------------------------------------------- */
1130 /** find first character of current word */
1131 
1132 static gboolean
1133 edit_find_word_start (const edit_buffer_t * buf, off_t * word_start, gsize * word_len)
     /* [previous][next][first][last][top][bottom][index][help]  */
1134 {
1135     int c;
1136     off_t i;
1137 
1138     /* return if at begin of file */
1139     if (buf->curs1 <= 0)
1140         return FALSE;
1141 
1142     c = edit_buffer_get_previous_byte (buf);
1143     /* return if not at end or in word */
1144     if (is_break_char (c))
1145         return FALSE;
1146 
1147     /* search start of word to be completed */
1148     for (i = 1;; i++)
1149     {
1150         int last;
1151 
1152         last = c;
1153         c = edit_buffer_get_byte (buf, buf->curs1 - i - 1);
1154 
1155         if (is_break_char (c))
1156         {
1157             /* return if word starts with digit */
1158             if (isdigit (last))
1159                 return FALSE;
1160 
1161             break;
1162         }
1163     }
1164 
1165     /* success */
1166     *word_start = buf->curs1 - i;       /* start found */
1167     *word_len = (gsize) i;
1168 
1169     return TRUE;
1170 }
1171 
1172 /* --------------------------------------------------------------------------------------------- */
1173 /**
1174  * Get current word under cursor
1175  *
1176  * @param esm status message window
1177  * @param srch mc_search object
1178  * @param word_start start word position
1179  *
1180  * @return newly allocated string or NULL if no any words under cursor
1181  */
1182 
1183 static char *
1184 edit_collect_completions_get_current_word (edit_search_status_msg_t * esm, mc_search_t * srch,
     /* [previous][next][first][last][top][bottom][index][help]  */
1185                                            off_t word_start)
1186 {
1187     WEdit *edit = esm->edit;
1188     gsize len = 0;
1189     off_t i;
1190     GString *temp;
1191 
1192     if (!mc_search_run (srch, (void *) esm, word_start, edit->buffer.size, &len))
1193         return NULL;
1194 
1195     temp = g_string_sized_new (len);
1196 
1197     for (i = 0; i < (off_t) len; i++)
1198     {
1199         int chr;
1200 
1201         chr = edit_buffer_get_byte (&edit->buffer, word_start + i);
1202         if (!isspace (chr))
1203             g_string_append_c (temp, chr);
1204     }
1205 
1206     return g_string_free (temp, temp->len == 0);
1207 }
1208 
1209 /* --------------------------------------------------------------------------------------------- */
1210 /** collect the possible completions */
1211 
1212 static gsize
1213 edit_collect_completions (WEdit * edit, off_t word_start, gsize word_len,
     /* [previous][next][first][last][top][bottom][index][help]  */
1214                           char *match_expr, GString ** compl, gsize * num)
1215 {
1216     gsize len = 0;
1217     gsize max_len = 0;
1218     gsize i;
1219     int skip;
1220     GString *temp;
1221     mc_search_t *srch;
1222     off_t last_byte, start = -1;
1223     char *current_word;
1224     gboolean entire_file;
1225     edit_search_status_msg_t esm;
1226 
1227 #ifdef HAVE_CHARSET
1228     srch = mc_search_new (match_expr, cp_source);
1229 #else
1230     srch = mc_search_new (match_expr, NULL);
1231 #endif
1232     if (srch == NULL)
1233         return 0;
1234 
1235     entire_file =
1236         mc_config_get_bool (mc_global.main_config, CONFIG_APP_SECTION,
1237                             "editor_wordcompletion_collect_entire_file", 0);
1238 
1239     last_byte = entire_file ? edit->buffer.size : word_start;
1240 
1241     srch->search_type = MC_SEARCH_T_REGEX;
1242     srch->is_case_sensitive = TRUE;
1243     srch->search_fn = edit_search_cmd_callback;
1244     srch->update_fn = edit_search_update_callback;
1245 
1246     esm.first = TRUE;
1247     esm.edit = edit;
1248     esm.offset = entire_file ? 0 : word_start;
1249 
1250     status_msg_init (STATUS_MSG (&esm), _("Collect completions"), 1.0, simple_status_msg_init_cb,
1251                      edit_search_status_update_cb, NULL);
1252 
1253     current_word = edit_collect_completions_get_current_word (&esm, srch, word_start);
1254 
1255     temp = g_string_new ("");
1256 
1257     /* collect max MAX_WORD_COMPLETIONS completions */
1258     while (mc_search_run (srch, (void *) &esm, start + 1, last_byte, &len))
1259     {
1260         g_string_set_size (temp, 0);
1261         start = srch->normal_offset;
1262 
1263         /* add matched completion if not yet added */
1264         for (i = 0; i < len; i++)
1265         {
1266             skip = edit_buffer_get_byte (&edit->buffer, start + i);
1267             if (isspace (skip))
1268                 continue;
1269 
1270             /* skip current word */
1271             if (start + (off_t) i == word_start)
1272                 break;
1273 
1274             g_string_append_c (temp, skip);
1275         }
1276 
1277         if (temp->len == 0)
1278             continue;
1279 
1280         if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1281             continue;
1282 
1283         skip = 0;
1284 
1285         for (i = 0; i < *num; i++)
1286         {
1287             if (strncmp
1288                 ((char *) &compl[i]->str[word_len],
1289                  (char *) &temp->str[word_len], MAX (len, compl[i]->len) - word_len) == 0)
1290             {
1291                 GString *this = compl[i];
1292 
1293                 for (++i; i < *num; i++)
1294                     compl[i - 1] = compl[i];
1295                 compl[*num - 1] = this;
1296 
1297                 skip = 1;
1298                 break;          /* skip it, already added */
1299             }
1300         }
1301         if (skip != 0)
1302             continue;
1303 
1304         if (*num == MAX_WORD_COMPLETIONS)
1305         {
1306             g_string_free (compl[0], TRUE);
1307             for (i = 1; i < *num; i++)
1308                 compl[i - 1] = compl[i];
1309             (*num)--;
1310         }
1311 #ifdef HAVE_CHARSET
1312         {
1313             GString *recoded;
1314 
1315             recoded = str_convert_to_display (temp->str);
1316             if (recoded->len != 0)
1317                 g_string_assign (temp, recoded->str);
1318 
1319             g_string_free (recoded, TRUE);
1320         }
1321 #endif
1322         compl[(*num)++] = g_string_new_len (temp->str, temp->len);
1323         start += len;
1324 
1325         /* note the maximal length needed for the completion dialog */
1326         if (len > max_len)
1327             max_len = len;
1328     }
1329 
1330     status_msg_deinit (STATUS_MSG (&esm));
1331     mc_search_free (srch);
1332     g_string_free (temp, TRUE);
1333     g_free (current_word);
1334 
1335     return max_len;
1336 }
1337 
1338 /* --------------------------------------------------------------------------------------------- */
1339 
1340 static void
1341 edit_insert_column_of_text (WEdit * edit, unsigned char *data, off_t size, long width,
     /* [previous][next][first][last][top][bottom][index][help]  */
1342                             off_t * start_pos, off_t * end_pos, long *col1, long *col2)
1343 {
1344     off_t i, cursor;
1345     long col;
1346 
1347     cursor = edit->buffer.curs1;
1348     col = edit_get_col (edit);
1349 
1350     for (i = 0; i < size; i++)
1351     {
1352         if (data[i] != '\n')
1353             edit_insert (edit, data[i]);
1354         else
1355         {                       /* fill in and move to next line */
1356             long l;
1357             off_t p;
1358 
1359             if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
1360             {
1361                 for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1362                     edit_insert (edit, ' ');
1363             }
1364             for (p = edit->buffer.curs1;; p++)
1365             {
1366                 if (p == edit->buffer.size)
1367                 {
1368                     edit_cursor_move (edit, edit->buffer.size - edit->buffer.curs1);
1369                     edit_insert_ahead (edit, '\n');
1370                     p++;
1371                     break;
1372                 }
1373                 if (edit_buffer_get_byte (&edit->buffer, p) == '\n')
1374                 {
1375                     p++;
1376                     break;
1377                 }
1378             }
1379             edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->buffer.curs1);
1380 
1381             for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1382                 edit_insert (edit, ' ');
1383         }
1384     }
1385 
1386     *col1 = col;
1387     *col2 = col + width;
1388     *start_pos = cursor;
1389     *end_pos = edit->buffer.curs1;
1390     edit_cursor_move (edit, cursor - edit->buffer.curs1);
1391 }
1392 
1393 /* --------------------------------------------------------------------------------------------- */
1394 
1395 static int
1396 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
     /* [previous][next][first][last][top][bottom][index][help]  */
1397 {
1398     const macros_t *m1 = (const macros_t *) macro1;
1399     const macros_t *m2 = (const macros_t *) macro2;
1400 
1401     return m1->hotkey - m2->hotkey;
1402 }
1403 
1404 /* --------------------------------------------------------------------------------------------- */
1405 
1406 static void
1407 edit_macro_sort_by_hotkey (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1408 {
1409     if (macros_list != NULL && macros_list->len != 0)
1410         g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1411 }
1412 
1413 /* --------------------------------------------------------------------------------------------- */
1414 
1415 static gboolean
1416 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
     /* [previous][next][first][last][top][bottom][index][help]  */
1417 {
1418     const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1419     macros_t *result;
1420     macros_t search_macro;
1421 
1422     (void) edit;
1423 
1424     search_macro.hotkey = hotkey;
1425     result = bsearch (&search_macro, macros_list->data, macros_list->len,
1426                       sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1427 
1428     if (result != NULL && result->macro != NULL)
1429     {
1430         *indx = (result - array_start);
1431         *macros = result;
1432         return TRUE;
1433     }
1434     *indx = 0;
1435     return FALSE;
1436 }
1437 
1438 /* --------------------------------------------------------------------------------------------- */
1439 /** returns FALSE on error */
1440 
1441 static gboolean
1442 edit_delete_macro (WEdit * edit, int hotkey)
     /* [previous][next][first][last][top][bottom][index][help]  */
1443 {
1444     mc_config_t *macros_config = NULL;
1445     const char *section_name = "editor";
1446     gchar *macros_fname;
1447     guint indx;
1448     char *skeyname;
1449     const macros_t *macros = NULL;
1450 
1451     /* clear array of actions for current hotkey */
1452     while (edit_get_macro (edit, hotkey, &macros, &indx))
1453     {
1454         if (macros->macro != NULL)
1455             g_array_free (macros->macro, TRUE);
1456         macros = NULL;
1457         g_array_remove_index (macros_list, indx);
1458         edit_macro_sort_by_hotkey ();
1459     }
1460 
1461     macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1462     macros_config = mc_config_init (macros_fname, FALSE);
1463     g_free (macros_fname);
1464 
1465     if (macros_config == NULL)
1466         return FALSE;
1467 
1468     skeyname = lookup_key_by_code (hotkey);
1469     while (mc_config_del_key (macros_config, section_name, skeyname))
1470         ;
1471     g_free (skeyname);
1472     mc_config_save_file (macros_config, NULL);
1473     mc_config_deinit (macros_config);
1474     return TRUE;
1475 }
1476 
1477 /* --------------------------------------------------------------------------------------------- */
1478 /**
1479  * Callback for the iteration of objects in the 'editors' array.
1480  * Toggle syntax highlighting in editor object.
1481  *
1482  * @param data      probably WEdit object
1483  * @param user_data unused
1484  */
1485 
1486 static void
1487 edit_syntax_onoff_cb (void *data, void *user_data)
     /* [previous][next][first][last][top][bottom][index][help]  */
1488 {
1489     (void) user_data;
1490 
1491     if (edit_widget_is_editor (CONST_WIDGET (data)))
1492     {
1493         WEdit *edit = (WEdit *) data;
1494 
1495         if (option_syntax_highlighting)
1496             edit_load_syntax (edit, NULL, edit->syntax_type);
1497         edit->force |= REDRAW_PAGE;
1498     }
1499 }
1500 
1501 /* --------------------------------------------------------------------------------------------- */
1502 /**
1503  * Insert autocompleted word into editor.
1504  *
1505  * @param edit       editor object
1506  * @param completion word for completion
1507  * @param word_len   offset from beginning for insert
1508  */
1509 
1510 static void
1511 edit_complete_word_insert_recoded_completion (WEdit * edit, char *completion, gsize word_len)
     /* [previous][next][first][last][top][bottom][index][help]  */
1512 {
1513 #ifdef HAVE_CHARSET
1514     GString *temp;
1515 
1516     temp = str_convert_to_input (completion);
1517 
1518     for (completion = temp->str + word_len; *completion != '\0'; completion++)
1519         edit_insert (edit, *completion);
1520     g_string_free (temp, TRUE);
1521 #else
1522     for (completion += word_len; *completion != '\0'; completion++)
1523         edit_insert (edit, *completion);
1524 #endif
1525 }
1526 
1527 /* --------------------------------------------------------------------------------------------- */
1528 /*** public functions ****************************************************************************/
1529 /* --------------------------------------------------------------------------------------------- */
1530 
1531 void
1532 edit_refresh_cmd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1533 {
1534     clr_scr ();
1535     repaint_screen ();
1536     tty_keypad (TRUE);
1537 }
1538 
1539 /* --------------------------------------------------------------------------------------------- */
1540 /**
1541  * Toggle syntax highlighting in all editor windows.
1542  *
1543  * @param h root widget for all windows
1544  */
1545 
1546 void
1547 edit_syntax_onoff_cmd (WDialog * h)
     /* [previous][next][first][last][top][bottom][index][help]  */
1548 {
1549     option_syntax_highlighting = !option_syntax_highlighting;
1550     g_list_foreach (GROUP (h)->widgets, edit_syntax_onoff_cb, NULL);
1551     widget_draw (WIDGET (h));
1552 }
1553 
1554 /* --------------------------------------------------------------------------------------------- */
1555 /**
1556  * Toggle tabs showing in all editor windows.
1557  *
1558  * @param h root widget for all windows
1559  */
1560 
1561 void
1562 edit_show_tabs_tws_cmd (WDialog * h)
     /* [previous][next][first][last][top][bottom][index][help]  */
1563 {
1564     enable_show_tabs_tws = !enable_show_tabs_tws;
1565     widget_draw (WIDGET (h));
1566 }
1567 
1568 /* --------------------------------------------------------------------------------------------- */
1569 /**
1570  * Toggle right margin showing in all editor windows.
1571  *
1572  * @param h root widget for all windows
1573  */
1574 
1575 void
1576 edit_show_margin_cmd (WDialog * h)
     /* [previous][next][first][last][top][bottom][index][help]  */
1577 {
1578     show_right_margin = !show_right_margin;
1579     widget_draw (WIDGET (h));
1580 }
1581 
1582 /* --------------------------------------------------------------------------------------------- */
1583 /**
1584  * Toggle line numbers showing in all editor windows.
1585  *
1586  * @param h root widget for all windows
1587  */
1588 
1589 void
1590 edit_show_numbers_cmd (WDialog * h)
     /* [previous][next][first][last][top][bottom][index][help]  */
1591 {
1592     option_line_state = !option_line_state;
1593     option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
1594     widget_draw (WIDGET (h));
1595 }
1596 
1597 /* --------------------------------------------------------------------------------------------- */
1598 
1599 void
1600 edit_save_mode_cmd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1601 {
1602     char *str_result = NULL;
1603 
1604     const char *str[] = {
1605         N_("&Quick save"),
1606         N_("&Safe save"),
1607         N_("&Do backups with following extension:")
1608     };
1609 
1610 #ifdef ENABLE_NLS
1611     size_t i;
1612 
1613     for (i = 0; i < 3; i++)
1614         str[i] = _(str[i]);
1615 #endif
1616 
1617     g_assert (option_backup_ext != NULL);
1618 
1619     {
1620         quick_widget_t quick_widgets[] = {
1621             /* *INDENT-OFF* */
1622             QUICK_RADIO (3, str, &option_save_mode, &edit_save_mode_radio_id),
1623             QUICK_INPUT (option_backup_ext, "edit-backup-ext", &str_result,
1624                          &edit_save_mode_input_id, FALSE, FALSE, INPUT_COMPLETE_NONE),
1625             QUICK_SEPARATOR (TRUE),
1626             QUICK_CHECKBOX (N_("Check &POSIX new line"), &option_check_nl_at_eof, NULL),
1627             QUICK_BUTTONS_OK_CANCEL,
1628             QUICK_END
1629             /* *INDENT-ON* */
1630         };
1631 
1632         quick_dialog_t qdlg = {
1633             -1, -1, 38,
1634             N_("Edit Save Mode"), "[Edit Save Mode]",
1635             quick_widgets, edit_save_mode_callback, NULL
1636         };
1637 
1638         if (quick_dialog (&qdlg) != B_CANCEL)
1639         {
1640             g_free (option_backup_ext);
1641             option_backup_ext = str_result;
1642         }
1643     }
1644 }
1645 
1646 /* --------------------------------------------------------------------------------------------- */
1647 
1648 void
1649 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1650 {
1651     vfs_path_free (edit->filename_vpath);
1652     edit->filename_vpath = vfs_path_clone (name_vpath);
1653 
1654     if (edit->dir_vpath == NULL)
1655         edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1656 }
1657 
1658 /* --------------------------------------------------------------------------------------------- */
1659 /* Here we want to warn the users of overwriting an existing file,
1660    but only if they have made a change to the filename */
1661 /* returns TRUE on success */
1662 gboolean
1663 edit_save_as_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1664 {
1665     /* This heads the 'Save As' dialog box */
1666     vfs_path_t *exp_vpath;
1667     int save_lock = 0;
1668     gboolean different_filename = FALSE;
1669     gboolean ret = FALSE;
1670 
1671     if (!edit_check_newline (&edit->buffer))
1672         return FALSE;
1673 
1674     exp_vpath = edit_get_save_file_as (edit);
1675     edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1676 
1677     if (exp_vpath != NULL && vfs_path_len (exp_vpath) != 0)
1678     {
1679         int rv;
1680 
1681         if (!vfs_path_equal (edit->filename_vpath, exp_vpath))
1682         {
1683             int file;
1684             struct stat sb;
1685 
1686             if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1687             {
1688                 edit_error_dialog (_("Save as"),
1689                                    get_sys_error (_
1690                                                   ("Cannot save: destination is not a regular file")));
1691                 goto ret;
1692             }
1693 
1694             different_filename = TRUE;
1695             file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1696 
1697             if (file == -1)
1698                 edit->stat1.st_mode |= S_IWUSR;
1699             else
1700             {
1701                 /* the file exists */
1702                 mc_close (file);
1703                 /* Overwrite the current file or cancel the operation */
1704                 if (edit_query_dialog2
1705                     (_("Warning"),
1706                      _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1707                     goto ret;
1708             }
1709 
1710             save_lock = lock_file (exp_vpath);
1711         }
1712         else if (!edit->locked && !edit->delete_file)
1713             /* filenames equal, check if already locked */
1714             save_lock = lock_file (exp_vpath);
1715 
1716         if (different_filename)
1717             /* Allow user to write into saved (under another name) file
1718              * even if original file had r/o user permissions. */
1719             edit->stat1.st_mode |= S_IWRITE;
1720 
1721         rv = edit_save_file (edit, exp_vpath);
1722         switch (rv)
1723         {
1724         case 1:
1725             /* Successful, so unlock both files */
1726             if (different_filename)
1727             {
1728                 if (save_lock)
1729                     unlock_file (exp_vpath);
1730                 if (edit->locked)
1731                     edit->locked = unlock_file (edit->filename_vpath);
1732             }
1733             else if (edit->locked || save_lock)
1734                 edit->locked = unlock_file (edit->filename_vpath);
1735 
1736             edit_set_filename (edit, exp_vpath);
1737             if (edit->lb != LB_ASIS)
1738                 edit_reload (edit, exp_vpath);
1739             edit->modified = 0;
1740             edit->delete_file = 0;
1741             if (different_filename)
1742                 edit_load_syntax (edit, NULL, edit->syntax_type);
1743             ret = TRUE;
1744             break;
1745 
1746         default:
1747             edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1748             MC_FALLTHROUGH;
1749 
1750         case -1:
1751             /* Failed, so maintain modify (not save) lock */
1752             if (save_lock)
1753                 unlock_file (exp_vpath);
1754             break;
1755         }
1756     }
1757 
1758   ret:
1759     vfs_path_free (exp_vpath);
1760     edit->force |= REDRAW_COMPLETELY;
1761     return ret;
1762 }
1763 
1764 /* {{{ Macro stuff starts here */
1765 /* --------------------------------------------------------------------------------------------- */
1766 
1767 void
1768 edit_delete_macro_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1769 {
1770     int hotkey;
1771 
1772     hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE);
1773 
1774     if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1775         message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1776 }
1777 
1778 /* --------------------------------------------------------------------------------------------- */
1779 
1780 /** returns FALSE on error */
1781 gboolean
1782 edit_execute_macro (WEdit * edit, int hotkey)
     /* [previous][next][first][last][top][bottom][index][help]  */
1783 {
1784     gboolean res = FALSE;
1785 
1786     if (hotkey != 0)
1787     {
1788         const macros_t *macros;
1789         guint indx;
1790 
1791         if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1792             macros->macro != NULL && macros->macro->len != 0)
1793         {
1794             guint i;
1795 
1796             edit->force |= REDRAW_PAGE;
1797 
1798             for (i = 0; i < macros->macro->len; i++)
1799             {
1800                 const macro_action_t *m_act;
1801 
1802                 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1803                 edit_execute_cmd (edit, m_act->action, m_act->ch);
1804                 res = TRUE;
1805             }
1806         }
1807     }
1808 
1809     return res;
1810 }
1811 
1812 /* --------------------------------------------------------------------------------------------- */
1813 
1814 /** returns FALSE on error */
1815 gboolean
1816 edit_store_macro_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1817 {
1818     int i;
1819     int hotkey;
1820     GString *marcros_string;
1821     mc_config_t *macros_config = NULL;
1822     const char *section_name = "editor";
1823     gchar *macros_fname;
1824     GArray *macros;             /* current macro */
1825     int tmp_act;
1826     gboolean have_macro = FALSE;
1827     char *skeyname = NULL;
1828 
1829     hotkey =
1830         editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE);
1831     if (hotkey == ESC_CHAR)
1832         return FALSE;
1833 
1834     tmp_act = keybind_lookup_keymap_command (WIDGET (edit)->keymap, hotkey);
1835 
1836     /* return FALSE if try assign macro into restricted hotkeys */
1837     if (tmp_act == CK_MacroStartRecord
1838         || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1839         return FALSE;
1840 
1841     edit_delete_macro (edit, hotkey);
1842 
1843     macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1844     macros_config = mc_config_init (macros_fname, FALSE);
1845     g_free (macros_fname);
1846 
1847     if (macros_config == NULL)
1848         return FALSE;
1849 
1850     edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1851 
1852     marcros_string = g_string_sized_new (250);
1853     macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1854 
1855     skeyname = lookup_key_by_code (hotkey);
1856 
1857     for (i = 0; i < macro_index; i++)
1858     {
1859         macro_action_t m_act;
1860         const char *action_name;
1861 
1862         action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1863 
1864         if (action_name == NULL)
1865             break;
1866 
1867         m_act.action = record_macro_buf[i].action;
1868         m_act.ch = record_macro_buf[i].ch;
1869         g_array_append_val (macros, m_act);
1870         have_macro = TRUE;
1871         g_string_append_printf (marcros_string, "%s:%i;", action_name,
1872                                 (int) record_macro_buf[i].ch);
1873     }
1874 
1875     if (!have_macro)
1876         mc_config_del_key (macros_config, section_name, skeyname);
1877     {
1878         macros_t macro;
1879 
1880         macro.hotkey = hotkey;
1881         macro.macro = macros;
1882         g_array_append_val (macros_list, macro);
1883         mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1884     }
1885 
1886     g_free (skeyname);
1887     edit_macro_sort_by_hotkey ();
1888 
1889     g_string_free (marcros_string, TRUE);
1890     mc_config_save_file (macros_config, NULL);
1891     mc_config_deinit (macros_config);
1892     return TRUE;
1893 }
1894 
1895  /* --------------------------------------------------------------------------------------------- */
1896 
1897 gboolean
1898 edit_repeat_macro_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1899 {
1900     int i, j;
1901     char *f;
1902     long count_repeat;
1903     char *error = NULL;
1904 
1905     f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL,
1906                       INPUT_COMPLETE_NONE);
1907     if (f == NULL || *f == '\0')
1908     {
1909         g_free (f);
1910         return FALSE;
1911     }
1912 
1913     count_repeat = strtol (f, &error, 0);
1914 
1915     if (*error != '\0')
1916     {
1917         g_free (f);
1918         return FALSE;
1919     }
1920 
1921     g_free (f);
1922 
1923     edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1924     edit->force |= REDRAW_PAGE;
1925 
1926     for (j = 0; j < count_repeat; j++)
1927         for (i = 0; i < macro_index; i++)
1928             edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1929 
1930     edit_update_screen (edit);
1931     return TRUE;
1932 }
1933 
1934 /* --------------------------------------------------------------------------------------------- */
1935 /** return FALSE on error */
1936 
1937 gboolean
1938 edit_load_macro_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
1939 {
1940     mc_config_t *macros_config = NULL;
1941     gchar **profile_keys, **keys;
1942     gchar **values, **curr_values;
1943     const char *section_name = "editor";
1944     gchar *macros_fname;
1945 
1946     (void) edit;
1947 
1948     if (macros_list == NULL || macros_list->len != 0)
1949         return FALSE;
1950 
1951     macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1952     macros_config = mc_config_init (macros_fname, TRUE);
1953     g_free (macros_fname);
1954 
1955     if (macros_config == NULL)
1956         return FALSE;
1957 
1958     keys = mc_config_get_keys (macros_config, section_name, NULL);
1959 
1960     for (profile_keys = keys; *profile_keys != NULL; profile_keys++)
1961     {
1962         int hotkey;
1963         gboolean have_macro = FALSE;
1964         GArray *macros;
1965         macros_t macro;
1966 
1967         macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1968         values = mc_config_get_string_list (macros_config, section_name, *profile_keys, NULL);
1969         hotkey = lookup_key (*profile_keys, NULL);
1970 
1971         for (curr_values = values; *curr_values != NULL && *curr_values[0] != '\0'; curr_values++)
1972         {
1973             char **macro_pair = NULL;
1974 
1975             macro_pair = g_strsplit (*curr_values, ":", 2);
1976             if (macro_pair != NULL)
1977             {
1978                 macro_action_t m_act;
1979                 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
1980                     m_act.action = 0;
1981                 else
1982                 {
1983                     m_act.action = keybind_lookup_action (macro_pair[0]);
1984                     MC_PTR_FREE (macro_pair[0]);
1985                 }
1986                 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
1987                     m_act.ch = -1;
1988                 else
1989                 {
1990                     m_act.ch = strtol (macro_pair[1], NULL, 0);
1991                     MC_PTR_FREE (macro_pair[1]);
1992                 }
1993                 if (m_act.action != 0)
1994                 {
1995                     /* a shell command */
1996                     if ((m_act.action / CK_PipeBlock (0)) == 1)
1997                     {
1998                         m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
1999                         m_act.ch = -1;
2000                     }
2001                     g_array_append_val (macros, m_act);
2002                     have_macro = TRUE;
2003                 }
2004                 g_strfreev (macro_pair);
2005                 macro_pair = NULL;
2006             }
2007         }
2008 
2009         if (have_macro)
2010         {
2011             macro.hotkey = hotkey;
2012             macro.macro = macros;
2013             g_array_append_val (macros_list, macro);
2014         }
2015 
2016         g_strfreev (values);
2017     }
2018 
2019     g_strfreev (keys);
2020     mc_config_deinit (macros_config);
2021     edit_macro_sort_by_hotkey ();
2022 
2023     return TRUE;
2024 }
2025 
2026 /* }}} Macro stuff end here */
2027 
2028 /* --------------------------------------------------------------------------------------------- */
2029 /** returns TRUE on success */
2030 
2031 gboolean
2032 edit_save_confirm_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2033 {
2034     if (edit->filename_vpath == NULL)
2035         return edit_save_as_cmd (edit);
2036 
2037     if (!edit_check_newline (&edit->buffer))
2038         return FALSE;
2039 
2040     if (edit_confirm_save)
2041     {
2042         char *f;
2043         gboolean ok;
2044 
2045         f = g_strdup_printf (_("Confirm save file: \"%s\""),
2046                              vfs_path_as_str (edit->filename_vpath));
2047         ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2048         g_free (f);
2049         if (!ok)
2050             return FALSE;
2051     }
2052 
2053     return edit_save_cmd (edit);
2054 }
2055 
2056 /* --------------------------------------------------------------------------------------------- */
2057 /**
2058   * Ask file to edit and load it.
2059   *
2060   * @return TRUE on success or cancel of ask.
2061   */
2062 
2063 gboolean
2064 edit_load_cmd (WDialog * h)
     /* [previous][next][first][last][top][bottom][index][help]  */
2065 {
2066     char *exp;
2067     gboolean ret = TRUE;        /* possible cancel */
2068 
2069     exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2070                                MC_HISTORY_EDIT_LOAD, INPUT_LAST_TEXT,
2071                                INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_CD);
2072 
2073     if (exp != NULL && *exp != '\0')
2074     {
2075         vfs_path_t *exp_vpath;
2076 
2077         exp_vpath = vfs_path_from_str (exp);
2078         ret = edit_load_file_from_filename (h, exp_vpath);
2079         vfs_path_free (exp_vpath);
2080     }
2081 
2082     g_free (exp);
2083 
2084     return ret;
2085 }
2086 
2087 /* --------------------------------------------------------------------------------------------- */
2088 /**
2089   * Show history od edited or viewed files and open selected file.
2090   *
2091   * @return TRUE on success, FALSE otherwise.
2092   */
2093 
2094 gboolean
2095 edit_load_file_from_history (WDialog * h)
     /* [previous][next][first][last][top][bottom][index][help]  */
2096 {
2097     char *exp;
2098     int action;
2099     gboolean ret = TRUE;        /* possible cancel */
2100 
2101     exp = show_file_history (CONST_WIDGET (h), &action);
2102     if (exp != NULL && (action == CK_Edit || action == CK_Enter))
2103     {
2104         vfs_path_t *exp_vpath;
2105 
2106         exp_vpath = vfs_path_from_str (exp);
2107         ret = edit_load_file_from_filename (h, exp_vpath);
2108         vfs_path_free (exp_vpath);
2109     }
2110 
2111     g_free (exp);
2112 
2113     return ret;
2114 }
2115 
2116 /* --------------------------------------------------------------------------------------------- */
2117 /**
2118   * Load syntax file to edit.
2119   *
2120   * @return TRUE on success
2121   */
2122 
2123 gboolean
2124 edit_load_syntax_file (WDialog * h)
     /* [previous][next][first][last][top][bottom][index][help]  */
2125 {
2126     vfs_path_t *extdir_vpath;
2127     int dir = 0;
2128     gboolean ret = FALSE;
2129 
2130     if (geteuid () == 0)
2131         dir = query_dialog (_("Syntax file edit"),
2132                             _("Which syntax file you want to edit?"), D_NORMAL, 2,
2133                             _("&User"), _("&System wide"));
2134 
2135     extdir_vpath =
2136         vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
2137     if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
2138     {
2139         vfs_path_free (extdir_vpath);
2140         extdir_vpath =
2141             vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
2142     }
2143 
2144     if (dir == 0)
2145     {
2146         vfs_path_t *user_syntax_file_vpath;
2147 
2148         user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_HOME_SYNTAX_FILE);
2149         check_for_default (extdir_vpath, user_syntax_file_vpath);
2150         ret = edit_load_file_from_filename (h, user_syntax_file_vpath);
2151         vfs_path_free (user_syntax_file_vpath);
2152     }
2153     else if (dir == 1)
2154         ret = edit_load_file_from_filename (h, extdir_vpath);
2155 
2156     vfs_path_free (extdir_vpath);
2157 
2158     return ret;
2159 }
2160 
2161 /* --------------------------------------------------------------------------------------------- */
2162 /**
2163   * Load menu file to edit.
2164   *
2165   * @return TRUE on success
2166   */
2167 
2168 gboolean
2169 edit_load_menu_file (WDialog * h)
     /* [previous][next][first][last][top][bottom][index][help]  */
2170 {
2171     vfs_path_t *buffer_vpath;
2172     vfs_path_t *menufile_vpath;
2173     int dir;
2174     gboolean ret;
2175 
2176     query_set_sel (1);
2177     dir = query_dialog (_("Menu edit"),
2178                         _("Which menu file do you want to edit?"), D_NORMAL,
2179                         geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2180 
2181     menufile_vpath =
2182         vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, (char *) NULL);
2183     if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
2184     {
2185         vfs_path_free (menufile_vpath);
2186         menufile_vpath =
2187             vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, (char *) NULL);
2188     }
2189 
2190     switch (dir)
2191     {
2192     case 0:
2193         buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
2194         check_for_default (menufile_vpath, buffer_vpath);
2195         chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
2196         break;
2197 
2198     case 1:
2199         buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
2200         check_for_default (menufile_vpath, buffer_vpath);
2201         break;
2202 
2203     case 2:
2204         buffer_vpath =
2205             vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, (char *) NULL);
2206         if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
2207         {
2208             vfs_path_free (buffer_vpath);
2209             buffer_vpath =
2210                 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, (char *) NULL);
2211         }
2212         break;
2213 
2214     default:
2215         vfs_path_free (menufile_vpath);
2216         return FALSE;
2217     }
2218 
2219     ret = edit_load_file_from_filename (h, buffer_vpath);
2220 
2221     vfs_path_free (buffer_vpath);
2222     vfs_path_free (menufile_vpath);
2223 
2224     return ret;
2225 }
2226 
2227 /* --------------------------------------------------------------------------------------------- */
2228 /**
2229   * Close window with opened file.
2230   *
2231   * @return TRUE if file was closed.
2232   */
2233 
2234 gboolean
2235 edit_close_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2236 {
2237     gboolean ret;
2238 
2239     ret = (edit != NULL) && edit_ok_to_exit (edit);
2240 
2241     if (ret)
2242     {
2243         Widget *w = WIDGET (edit);
2244         WGroup *g = w->owner;
2245 
2246         if (edit->locked != 0)
2247             unlock_file (edit->filename_vpath);
2248 
2249         group_remove_widget (w);
2250         widget_destroy (w);
2251 
2252         if (edit_widget_is_editor (CONST_WIDGET (g->current->data)))
2253             edit = (WEdit *) (g->current->data);
2254         else
2255         {
2256             edit = find_editor (DIALOG (g));
2257             if (edit != NULL)
2258                 widget_select (w);
2259         }
2260     }
2261 
2262     if (edit != NULL)
2263         edit->force |= REDRAW_COMPLETELY;
2264 
2265     return ret;
2266 }
2267 
2268 /* --------------------------------------------------------------------------------------------- */
2269 /**
2270    if mark2 is -1 then marking is from mark1 to the cursor.
2271    Otherwise its between the markers. This handles this.
2272    Returns FALSE if no text is marked.
2273  */
2274 
2275 gboolean
2276 eval_marks (WEdit * edit, off_t * start_mark, off_t * end_mark)
     /* [previous][next][first][last][top][bottom][index][help]  */
2277 {
2278     long end_mark_curs;
2279 
2280     if (edit->mark1 == edit->mark2)
2281     {
2282         *start_mark = *end_mark = 0;
2283         edit->column2 = edit->column1 = 0;
2284         return FALSE;
2285     }
2286 
2287     if (edit->end_mark_curs < 0)
2288         end_mark_curs = edit->buffer.curs1;
2289     else
2290         end_mark_curs = edit->end_mark_curs;
2291 
2292     if (edit->mark2 >= 0)
2293     {
2294         *start_mark = MIN (edit->mark1, edit->mark2);
2295         *end_mark = MAX (edit->mark1, edit->mark2);
2296     }
2297     else
2298     {
2299         *start_mark = MIN (edit->mark1, end_mark_curs);
2300         *end_mark = MAX (edit->mark1, end_mark_curs);
2301         edit->column2 = edit->curs_col + edit->over_col;
2302     }
2303 
2304     if (edit->column_highlight
2305         && ((edit->mark1 > end_mark_curs && edit->column1 < edit->column2)
2306             || (edit->mark1 < end_mark_curs && edit->column1 > edit->column2)))
2307     {
2308         off_t start_bol, start_eol;
2309         off_t end_bol, end_eol;
2310         long col1, col2;
2311         off_t diff1, diff2;
2312 
2313         start_bol = edit_buffer_get_bol (&edit->buffer, *start_mark);
2314         start_eol = edit_buffer_get_eol (&edit->buffer, start_bol - 1) + 1;
2315         end_bol = edit_buffer_get_bol (&edit->buffer, *end_mark);
2316         end_eol = edit_buffer_get_eol (&edit->buffer, *end_mark);
2317         col1 = MIN (edit->column1, edit->column2);
2318         col2 = MAX (edit->column1, edit->column2);
2319 
2320         diff1 = edit_move_forward3 (edit, start_bol, col2, 0) -
2321             edit_move_forward3 (edit, start_bol, col1, 0);
2322         diff2 = edit_move_forward3 (edit, end_bol, col2, 0) -
2323             edit_move_forward3 (edit, end_bol, col1, 0);
2324 
2325         *start_mark -= diff1;
2326         *end_mark += diff2;
2327         *start_mark = MAX (*start_mark, start_eol);
2328         *end_mark = MIN (*end_mark, end_eol);
2329     }
2330 
2331     return TRUE;
2332 }
2333 
2334 /* --------------------------------------------------------------------------------------------- */
2335 
2336 void
2337 edit_block_copy_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2338 {
2339     off_t start_mark, end_mark, current = edit->buffer.curs1;
2340     off_t mark1 = 0, mark2 = 0;
2341     long c1 = 0, c2 = 0;
2342     off_t size;
2343     unsigned char *copy_buf;
2344 
2345     edit_update_curs_col (edit);
2346     if (!eval_marks (edit, &start_mark, &end_mark))
2347         return;
2348 
2349     copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2350 
2351     /* all that gets pushed are deletes hence little space is used on the stack */
2352 
2353     edit_push_markers (edit);
2354 
2355     if (edit->column_highlight)
2356     {
2357         long col_delta;
2358 
2359         col_delta = labs (edit->column2 - edit->column1);
2360         edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2361     }
2362     else
2363     {
2364         int size_orig = size;
2365 
2366         while (size-- != 0)
2367             edit_insert_ahead (edit, copy_buf[size]);
2368 
2369         /* Place cursor at the end of text selection */
2370         if (option_cursor_after_inserted_block)
2371             edit_cursor_move (edit, size_orig);
2372     }
2373 
2374     g_free (copy_buf);
2375     edit_scroll_screen_over_cursor (edit);
2376 
2377     if (edit->column_highlight)
2378         edit_set_markers (edit, edit->buffer.curs1, mark2, c1, c2);
2379     else if (start_mark < current && end_mark > current)
2380         edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2381 
2382     edit->force |= REDRAW_PAGE;
2383 }
2384 
2385 
2386 /* --------------------------------------------------------------------------------------------- */
2387 
2388 void
2389 edit_block_move_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2390 {
2391     off_t current;
2392     unsigned char *copy_buf = NULL;
2393     off_t start_mark, end_mark;
2394 
2395     if (!eval_marks (edit, &start_mark, &end_mark))
2396         return;
2397 
2398     if (!edit->column_highlight && edit->buffer.curs1 > start_mark && edit->buffer.curs1 < end_mark)
2399         return;
2400 
2401     if (edit->mark2 < 0)
2402         edit_mark_cmd (edit, FALSE);
2403     edit_push_markers (edit);
2404 
2405     if (edit->column_highlight)
2406     {
2407         off_t mark1, mark2;
2408         off_t size;
2409         long c1, c2, b_width;
2410         long x, x2;
2411 
2412         c1 = MIN (edit->column1, edit->column2);
2413         c2 = MAX (edit->column1, edit->column2);
2414         b_width = c2 - c1;
2415 
2416         edit_update_curs_col (edit);
2417 
2418         x = edit->curs_col;
2419         x2 = x + edit->over_col;
2420 
2421         /* do nothing when cursor inside first line of selected area */
2422         if ((edit_buffer_get_eol (&edit->buffer, edit->buffer.curs1) ==
2423              edit_buffer_get_eol (&edit->buffer, start_mark)) && x2 > c1 && x2 <= c2)
2424             return;
2425 
2426         if (edit->buffer.curs1 > start_mark
2427             && edit->buffer.curs1 < edit_buffer_get_eol (&edit->buffer, end_mark))
2428         {
2429             if (x > c2)
2430                 x -= b_width;
2431             else if (x > c1 && x <= c2)
2432                 x = c1;
2433         }
2434         /* save current selection into buffer */
2435         copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2436 
2437         /* remove current selection */
2438         edit_block_delete_cmd (edit);
2439 
2440         edit->over_col = MAX (0, edit->over_col - b_width);
2441         /* calculate the cursor pos after delete block */
2442         current = edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), x, 0);
2443         edit_cursor_move (edit, current - edit->buffer.curs1);
2444         edit_scroll_screen_over_cursor (edit);
2445 
2446         /* add TWS if need before block insertion */
2447         if (option_cursor_beyond_eol && edit->over_col > 0)
2448             edit_insert_over (edit);
2449 
2450         edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2451         edit_set_markers (edit, mark1, mark2, c1, c2);
2452     }
2453     else
2454     {
2455         off_t count, count_orig;
2456 
2457         current = edit->buffer.curs1;
2458         copy_buf = g_malloc0 (end_mark - start_mark);
2459         edit_cursor_move (edit, start_mark - edit->buffer.curs1);
2460         edit_scroll_screen_over_cursor (edit);
2461 
2462         for (count = start_mark; count < end_mark; count++)
2463             copy_buf[end_mark - count - 1] = edit_delete (edit, TRUE);
2464 
2465         edit_scroll_screen_over_cursor (edit);
2466         edit_cursor_move (edit,
2467                           current - edit->buffer.curs1 -
2468                           (((current - edit->buffer.curs1) > 0) ? end_mark - start_mark : 0));
2469         edit_scroll_screen_over_cursor (edit);
2470         count_orig = count;
2471         while (count-- > start_mark)
2472             edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2473 
2474         edit_set_markers (edit, edit->buffer.curs1, edit->buffer.curs1 + end_mark - start_mark, 0,
2475                           0);
2476 
2477         /* Place cursor at the end of text selection */
2478         if (option_cursor_after_inserted_block)
2479             edit_cursor_move (edit, count_orig - start_mark);
2480     }
2481 
2482     edit_scroll_screen_over_cursor (edit);
2483     g_free (copy_buf);
2484     edit->force |= REDRAW_PAGE;
2485 }
2486 
2487 /* --------------------------------------------------------------------------------------------- */
2488 /** returns 1 if canceelled by user */
2489 
2490 int
2491 edit_block_delete_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2492 {
2493     off_t start_mark, end_mark;
2494 
2495     if (eval_marks (edit, &start_mark, &end_mark))
2496         return edit_block_delete (edit);
2497 
2498     edit_delete_line (edit);
2499 
2500     return 0;
2501 }
2502 
2503 /* --------------------------------------------------------------------------------------------- */
2504 /** call with edit = 0 before shutdown to close memory leaks */
2505 
2506 void
2507 edit_replace_cmd (WEdit * edit, gboolean again)
     /* [previous][next][first][last][top][bottom][index][help]  */
2508 {
2509     /* 1 = search string, 2 = replace with */
2510     static char *saved1 = NULL; /* saved default[123] */
2511     static char *saved2 = NULL;
2512     char *input1 = NULL;        /* user input from the dialog */
2513     char *input2 = NULL;
2514     GString *input2_str = NULL;
2515     char *disp1 = NULL;
2516     char *disp2 = NULL;
2517     long times_replaced = 0;
2518     gboolean once_found = FALSE;
2519     edit_search_status_msg_t esm;
2520 
2521     if (edit == NULL)
2522     {
2523         MC_PTR_FREE (saved1);
2524         MC_PTR_FREE (saved2);
2525         return;
2526     }
2527 
2528     edit->force |= REDRAW_COMPLETELY;
2529 
2530     if (again && saved1 == NULL && saved2 == NULL)
2531         again = FALSE;
2532 
2533     if (again)
2534     {
2535         input1 = g_strdup (saved1 != NULL ? saved1 : "");
2536         input2 = g_strdup (saved2 != NULL ? saved2 : "");
2537     }
2538     else
2539     {
2540         char *tmp_inp1, *tmp_inp2;
2541 
2542         disp1 = edit_replace_cmd__conv_to_display (saved1 != NULL ? saved1 : "");
2543         disp2 = edit_replace_cmd__conv_to_display (saved2 != NULL ? saved2 : "");
2544 
2545         edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2546 
2547         editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2548 
2549         g_free (disp1);
2550         g_free (disp2);
2551 
2552         if (input1 == NULL || *input1 == '\0')
2553         {
2554             edit->force = REDRAW_COMPLETELY;
2555             goto cleanup;
2556         }
2557 
2558         tmp_inp1 = input1;
2559         tmp_inp2 = input2;
2560         input1 = edit_replace_cmd__conv_to_input (input1);
2561         input2 = edit_replace_cmd__conv_to_input (input2);
2562         g_free (tmp_inp1);
2563         g_free (tmp_inp2);
2564 
2565         g_free (saved1);
2566         saved1 = g_strdup (input1);
2567         g_free (saved2);
2568         saved2 = g_strdup (input2);
2569 
2570         mc_search_free (edit->search);
2571         edit->search = NULL;
2572     }
2573 
2574     input2_str = g_string_new (input2);
2575 
2576     if (edit->search == NULL)
2577     {
2578 #ifdef HAVE_CHARSET
2579         edit->search = mc_search_new (input1, cp_source);
2580 #else
2581         edit->search = mc_search_new (input1, NULL);
2582 #endif
2583         if (edit->search == NULL)
2584         {
2585             edit->search_start = edit->buffer.curs1;
2586             goto cleanup;
2587         }
2588         edit->search->search_type = edit_search_options.type;
2589 #ifdef HAVE_CHARSET
2590         edit->search->is_all_charsets = edit_search_options.all_codepages;
2591 #endif
2592         edit->search->is_case_sensitive = edit_search_options.case_sens;
2593         edit->search->whole_words = edit_search_options.whole_words;
2594         edit->search->search_fn = edit_search_cmd_callback;
2595         edit->search->update_fn = edit_search_update_callback;
2596         edit->search_line_type = edit_get_search_line_type (edit->search);
2597         edit_search_fix_search_start_if_selection (edit);
2598     }
2599 
2600     if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
2601         && edit_search_options.backwards)
2602         edit->search_start--;
2603 
2604     if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
2605         && !edit_search_options.backwards)
2606         edit->search_start++;
2607 
2608     esm.first = TRUE;
2609     esm.edit = edit;
2610     esm.offset = edit->search_start;
2611 
2612     status_msg_init (STATUS_MSG (&esm), _("Search"), 1.0, simple_status_msg_init_cb,
2613                      edit_search_status_update_cb, NULL);
2614 
2615     do
2616     {
2617         gsize len = 0;
2618 
2619         if (!editcmd_find (&esm, &len))
2620         {
2621             if (!(edit->search->error == MC_SEARCH_E_OK ||
2622                   (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2623                 edit_show_search_error (edit, _("Search"));
2624             break;
2625         }
2626 
2627         once_found = TRUE;
2628 
2629         edit->search_start = edit->search->normal_offset;
2630         /* returns negative on not found or error in pattern */
2631 
2632         if (edit->search_start >= 0 && edit->search_start < edit->buffer.size)
2633         {
2634             gsize i;
2635             GString *repl_str;
2636 
2637             edit->found_start = edit->search_start;
2638             i = edit->found_len = len;
2639 
2640             edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
2641             edit_scroll_screen_over_cursor (edit);
2642 
2643             if (edit->replace_mode == 0)
2644             {
2645                 long l;
2646                 int prompt;
2647 
2648                 l = edit->curs_row - WIDGET (edit)->lines / 3;
2649                 if (l > 0)
2650                     edit_scroll_downward (edit, l);
2651                 if (l < 0)
2652                     edit_scroll_upward (edit, -l);
2653 
2654                 edit_scroll_screen_over_cursor (edit);
2655                 edit->force |= REDRAW_PAGE;
2656                 edit_render_keypress (edit);
2657 
2658                 /*so that undo stops at each query */
2659                 edit_push_key_press (edit);
2660                 /* and prompt 2/3 down */
2661                 disp1 = edit_replace_cmd__conv_to_display (saved1);
2662                 disp2 = edit_replace_cmd__conv_to_display (saved2);
2663                 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2664                 g_free (disp1);
2665                 g_free (disp2);
2666 
2667                 if (prompt == B_REPLACE_ALL)
2668                     edit->replace_mode = 1;
2669                 else if (prompt == B_SKIP_REPLACE)
2670                 {
2671                     if (edit_search_options.backwards)
2672                         edit->search_start--;
2673                     else
2674                         edit->search_start++;
2675                     continue;   /* loop */
2676                 }
2677                 else if (prompt == B_CANCEL)
2678                 {
2679                     edit->replace_mode = -1;
2680                     break;      /* loop */
2681                 }
2682             }
2683 
2684             repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2685 
2686             if (edit->search->error != MC_SEARCH_E_OK)
2687             {
2688                 edit_show_search_error (edit, _("Replace"));
2689                 if (repl_str != NULL)
2690                     g_string_free (repl_str, TRUE);
2691                 break;
2692             }
2693 
2694             /* delete then insert new */
2695             for (i = 0; i < len; i++)
2696                 edit_delete (edit, TRUE);
2697 
2698             for (i = 0; i < repl_str->len; i++)
2699                 edit_insert (edit, repl_str->str[i]);
2700 
2701             edit->found_len = repl_str->len;
2702             g_string_free (repl_str, TRUE);
2703             times_replaced++;
2704 
2705             /* so that we don't find the same string again */
2706             if (edit_search_options.backwards)
2707                 edit->search_start--;
2708             else
2709             {
2710                 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2711 
2712                 if (edit->search_start >= edit->buffer.size)
2713                     break;
2714             }
2715 
2716             edit_scroll_screen_over_cursor (edit);
2717         }
2718         else
2719         {
2720             /* try and find from right here for next search */
2721             edit->search_start = edit->buffer.curs1;
2722             edit_update_curs_col (edit);
2723 
2724             edit->force |= REDRAW_PAGE;
2725             edit_render_keypress (edit);
2726 
2727             if (times_replaced == 0)
2728                 query_dialog (_("Replace"), _(STR_E_NOTFOUND), D_NORMAL, 1, _("&OK"));
2729             break;
2730         }
2731     }
2732     while (edit->replace_mode >= 0);
2733 
2734     status_msg_deinit (STATUS_MSG (&esm));
2735     edit_scroll_screen_over_cursor (edit);
2736     edit->force |= REDRAW_COMPLETELY;
2737     edit_render_keypress (edit);
2738 
2739     if (edit->replace_mode == 1 && times_replaced != 0)
2740         message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2741 
2742   cleanup:
2743     g_free (input1);
2744     g_free (input2);
2745     if (input2_str != NULL)
2746         g_string_free (input2_str, TRUE);
2747 }
2748 
2749 /* --------------------------------------------------------------------------------------------- */
2750 
2751 mc_search_cbret_t
2752 edit_search_cmd_callback (const void *user_data, gsize char_offset, int *current_char)
     /* [previous][next][first][last][top][bottom][index][help]  */
2753 {
2754     WEdit *edit = ((const edit_search_status_msg_t *) user_data)->edit;
2755 
2756     *current_char = edit_buffer_get_byte (&edit->buffer, (off_t) char_offset);
2757 
2758     return MC_SEARCH_CB_OK;
2759 }
2760 
2761 /* --------------------------------------------------------------------------------------------- */
2762 
2763 mc_search_cbret_t
2764 edit_search_update_callback (const void *user_data, gsize char_offset)
     /* [previous][next][first][last][top][bottom][index][help]  */
2765 {
2766     status_msg_t *sm = STATUS_MSG (user_data);
2767 
2768     ((edit_search_status_msg_t *) sm)->offset = (off_t) char_offset;
2769 
2770     return (sm->update (sm) == B_CANCEL ? MC_SEARCH_CB_ABORT : MC_SEARCH_CB_OK);
2771 }
2772 
2773 /* --------------------------------------------------------------------------------------------- */
2774 
2775 void
2776 edit_search_cmd (WEdit * edit, gboolean again)
     /* [previous][next][first][last][top][bottom][index][help]  */
2777 {
2778     if (edit == NULL)
2779         return;
2780 
2781     if (!again)
2782         edit_search (edit);
2783     else if (edit->last_search_string != NULL)
2784         edit_do_search (edit);
2785     else
2786     {
2787         /* find last search string in history */
2788         GList *history;
2789 
2790         history = mc_config_history_get (MC_HISTORY_SHARED_SEARCH);
2791         if (history != NULL && history->data != NULL)
2792         {
2793             edit->last_search_string = (char *) history->data;
2794             history->data = NULL;
2795             history = g_list_first (history);
2796             g_list_free_full (history, g_free);
2797 
2798 #ifdef HAVE_CHARSET
2799             edit->search = mc_search_new (edit->last_search_string, cp_source);
2800 #else
2801             edit->search = mc_search_new (edit->last_search_string, NULL);
2802 #endif
2803             if (edit->search == NULL)
2804             {
2805                 /* if not... then ask for an expression */
2806                 MC_PTR_FREE (edit->last_search_string);
2807                 edit_search (edit);
2808             }
2809             else
2810             {
2811                 edit->search->search_type = edit_search_options.type;
2812 #ifdef HAVE_CHARSET
2813                 edit->search->is_all_charsets = edit_search_options.all_codepages;
2814 #endif
2815                 edit->search->is_case_sensitive = edit_search_options.case_sens;
2816                 edit->search->whole_words = edit_search_options.whole_words;
2817                 edit->search->search_fn = edit_search_cmd_callback;
2818                 edit->search->update_fn = edit_search_update_callback;
2819                 edit->search_line_type = edit_get_search_line_type (edit->search);
2820                 edit_do_search (edit);
2821             }
2822         }
2823         else
2824         {
2825             /* if not... then ask for an expression */
2826             MC_PTR_FREE (edit->last_search_string);
2827             edit_search (edit);
2828         }
2829     }
2830 }
2831 
2832 /* --------------------------------------------------------------------------------------------- */
2833 /**
2834   * Check if it's OK to close the file. If there are unsaved changes, ask user.
2835   *
2836   * @return TRUE if it's OK to exit, FALSE to continue editing.
2837   */
2838 
2839 gboolean
2840 edit_ok_to_exit (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2841 {
2842     const char *fname = N_("[NoName]");
2843     char *msg;
2844     int act;
2845 
2846     if (!edit->modified)
2847         return TRUE;
2848 
2849     if (edit->filename_vpath != NULL)
2850         fname = vfs_path_as_str (edit->filename_vpath);
2851 #ifdef ENABLE_NLS
2852     else
2853         fname = _(fname);
2854 #endif
2855 
2856     if (!mc_global.midnight_shutdown)
2857     {
2858         query_set_sel (2);
2859 
2860         msg = g_strdup_printf (_("File %s was modified.\nSave before close?"), fname);
2861         act = edit_query_dialog3 (_("Close file"), msg, _("&Yes"), _("&No"), _("&Cancel"));
2862     }
2863     else
2864     {
2865         msg = g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2866                                fname);
2867         act = edit_query_dialog2 (_("Quit"), msg, _("&Yes"), _("&No"));
2868 
2869         /* Esc is No */
2870         if (act == -1)
2871             act = 1;
2872     }
2873 
2874     g_free (msg);
2875 
2876     switch (act)
2877     {
2878     case 0:                    /* Yes */
2879         if (!mc_global.midnight_shutdown && !edit_check_newline (&edit->buffer))
2880             return FALSE;
2881         edit_push_markers (edit);
2882         edit_set_markers (edit, 0, 0, 0, 0);
2883         if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2884             return mc_global.midnight_shutdown;
2885         break;
2886     case 1:                    /* No */
2887     default:
2888         break;
2889     case 2:                    /* Cancel quit */
2890     case -1:                   /* Esc */
2891         return FALSE;
2892     }
2893 
2894     return TRUE;
2895 }
2896 
2897 /* --------------------------------------------------------------------------------------------- */
2898 /** save block, returns TRUE on success */
2899 
2900 gboolean
2901 edit_save_block (WEdit * edit, const char *filename, off_t start, off_t finish)
     /* [previous][next][first][last][top][bottom][index][help]  */
2902 {
2903     int file;
2904     off_t len = 1;
2905     vfs_path_t *vpath;
2906 
2907     vpath = vfs_path_from_str (filename);
2908     file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2909                     S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2910     vfs_path_free (vpath);
2911     if (file == -1)
2912         return FALSE;
2913 
2914     if (edit->column_highlight)
2915     {
2916         int r;
2917 
2918         r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2919         if (r > 0)
2920         {
2921             unsigned char *block, *p;
2922 
2923             p = block = edit_get_block (edit, start, finish, &len);
2924             while (len != 0)
2925             {
2926                 r = mc_write (file, p, len);
2927                 if (r < 0)
2928                     break;
2929                 p += r;
2930                 len -= r;
2931             }
2932             g_free (block);
2933         }
2934     }
2935     else
2936     {
2937         unsigned char *buf;
2938         off_t i = start;
2939 
2940         len = finish - start;
2941         buf = g_malloc0 (TEMP_BUF_LEN);
2942         while (start != finish)
2943         {
2944             off_t end;
2945 
2946             end = MIN (finish, start + TEMP_BUF_LEN);
2947             for (; i < end; i++)
2948                 buf[i - start] = edit_buffer_get_byte (&edit->buffer, i);
2949             len -= mc_write (file, (char *) buf, end - start);
2950             start = end;
2951         }
2952         g_free (buf);
2953     }
2954     mc_close (file);
2955 
2956     return (len == 0);
2957 }
2958 
2959 /* --------------------------------------------------------------------------------------------- */
2960 
2961 void
2962 edit_paste_from_history (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2963 {
2964     (void) edit;
2965     edit_error_dialog (_("Error"), _("This function is not implemented"));
2966 }
2967 
2968 /* --------------------------------------------------------------------------------------------- */
2969 
2970 gboolean
2971 edit_copy_to_X_buf_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2972 {
2973     off_t start_mark, end_mark;
2974 
2975     if (!eval_marks (edit, &start_mark, &end_mark))
2976         return TRUE;
2977 
2978     if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2979     {
2980         edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2981         return FALSE;
2982     }
2983     /* try use external clipboard utility */
2984     mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2985 
2986     if (option_drop_selection_on_copy)
2987         edit_mark_cmd (edit, TRUE);
2988 
2989     return TRUE;
2990 }
2991 
2992 /* --------------------------------------------------------------------------------------------- */
2993 
2994 gboolean
2995 edit_cut_to_X_buf_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
2996 {
2997     off_t start_mark, end_mark;
2998 
2999     if (!eval_marks (edit, &start_mark, &end_mark))
3000         return TRUE;
3001 
3002     if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
3003     {
3004         edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
3005         return FALSE;
3006     }
3007     /* try use external clipboard utility */
3008     mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
3009 
3010     edit_block_delete_cmd (edit);
3011     edit_mark_cmd (edit, TRUE);
3012 
3013     return TRUE;
3014 }
3015 
3016 /* --------------------------------------------------------------------------------------------- */
3017 
3018 gboolean
3019 edit_paste_from_X_buf_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3020 {
3021     vfs_path_t *tmp;
3022     gboolean ret;
3023 
3024     /* try use external clipboard utility */
3025     mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
3026     tmp = mc_config_get_full_vpath (EDIT_HOME_CLIP_FILE);
3027     ret = (edit_insert_file (edit, tmp) >= 0);
3028     vfs_path_free (tmp);
3029 
3030     return ret;
3031 }
3032 
3033 /* --------------------------------------------------------------------------------------------- */
3034 /**
3035  * Ask user for the line and go to that line.
3036  * Negative numbers mean line from the end (i.e. -1 is the last line).
3037  */
3038 
3039 void
3040 edit_goto_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3041 {
3042     static gboolean first_run = TRUE;
3043 
3044     char *f;
3045     long l;
3046     char *error;
3047 
3048     f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE,
3049                       first_run ? NULL : INPUT_LAST_TEXT, INPUT_COMPLETE_NONE);
3050     if (f == NULL || *f == '\0')
3051     {
3052         g_free (f);
3053         return;
3054     }
3055 
3056     l = strtol (f, &error, 0);
3057     if (*error != '\0')
3058     {
3059         g_free (f);
3060         return;
3061     }
3062 
3063     if (l < 0)
3064         l = edit->buffer.lines + l + 2;
3065 
3066     edit_move_display (edit, l - WIDGET (edit)->lines / 2 - 1);
3067     edit_move_to_line (edit, l - 1);
3068     edit->force |= REDRAW_COMPLETELY;
3069 
3070     g_free (f);
3071     first_run = FALSE;
3072 }
3073 
3074 /* --------------------------------------------------------------------------------------------- */
3075 /** Return TRUE on success */
3076 
3077 gboolean
3078 edit_save_block_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3079 {
3080     off_t start_mark, end_mark;
3081     char *exp, *tmp;
3082     gboolean ret = FALSE;
3083 
3084     if (!eval_marks (edit, &start_mark, &end_mark))
3085         return TRUE;
3086 
3087     tmp = mc_config_get_full_path (EDIT_HOME_CLIP_FILE);
3088     exp =
3089         input_expand_dialog (_("Save block"), _("Enter file name:"),
3090                              MC_HISTORY_EDIT_SAVE_BLOCK, tmp, INPUT_COMPLETE_FILENAMES);
3091     g_free (tmp);
3092     edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3093 
3094     if (exp != NULL && *exp != '\0')
3095     {
3096         if (edit_save_block (edit, exp, start_mark, end_mark))
3097             ret = TRUE;
3098         else
3099             edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3100 
3101         edit->force |= REDRAW_COMPLETELY;
3102     }
3103 
3104     g_free (exp);
3105 
3106     return ret;
3107 }
3108 
3109 /* --------------------------------------------------------------------------------------------- */
3110 
3111 /** returns TRUE on success */
3112 gboolean
3113 edit_insert_file_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3114 {
3115     char *tmp;
3116     char *exp;
3117     gboolean ret = FALSE;
3118 
3119     tmp = mc_config_get_full_path (EDIT_HOME_CLIP_FILE);
3120     exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3121                                MC_HISTORY_EDIT_INSERT_FILE, tmp, INPUT_COMPLETE_FILENAMES);
3122     g_free (tmp);
3123 
3124     edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3125 
3126     if (exp != NULL && *exp != '\0')
3127     {
3128         vfs_path_t *exp_vpath;
3129 
3130         exp_vpath = vfs_path_from_str (exp);
3131         ret = (edit_insert_file (edit, exp_vpath) >= 0);
3132         vfs_path_free (exp_vpath);
3133 
3134         if (!ret)
3135             edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3136     }
3137 
3138     g_free (exp);
3139 
3140     edit->force |= REDRAW_COMPLETELY;
3141     return ret;
3142 }
3143 
3144 /* --------------------------------------------------------------------------------------------- */
3145 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3146 
3147 int
3148 edit_sort_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3149 {
3150     char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3151     off_t start_mark, end_mark;
3152     int e;
3153 
3154     if (!eval_marks (edit, &start_mark, &end_mark))
3155     {
3156         edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3157         return 0;
3158     }
3159 
3160     tmp = mc_config_get_full_path (EDIT_HOME_BLOCK_FILE);
3161     edit_save_block (edit, tmp, start_mark, end_mark);
3162     g_free (tmp);
3163 
3164     exp = input_dialog (_("Run sort"),
3165                         _("Enter sort options (see manpage) separated by whitespace:"),
3166                         MC_HISTORY_EDIT_SORT, INPUT_LAST_TEXT, INPUT_COMPLETE_NONE);
3167 
3168     if (exp == NULL)
3169         return 1;
3170 
3171     tmp_edit_block_name = mc_config_get_full_path (EDIT_HOME_BLOCK_FILE);
3172     tmp_edit_temp_name = mc_config_get_full_path (EDIT_HOME_TEMP_FILE);
3173     tmp =
3174         g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3175                      " > ", tmp_edit_temp_name, (char *) NULL);
3176     g_free (tmp_edit_temp_name);
3177     g_free (tmp_edit_block_name);
3178     g_free (exp);
3179 
3180     e = system (tmp);
3181     g_free (tmp);
3182     if (e != 0)
3183     {
3184         if (e == -1 || e == 127)
3185             edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3186         else
3187         {
3188             char q[8];
3189 
3190             sprintf (q, "%d ", e);
3191             tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3192             edit_error_dialog (_("Sort"), tmp);
3193             g_free (tmp);
3194         }
3195 
3196         return -1;
3197     }
3198 
3199     edit->force |= REDRAW_COMPLETELY;
3200 
3201     if (edit_block_delete_cmd (edit))
3202         return 1;
3203 
3204     {
3205         vfs_path_t *tmp_vpath;
3206 
3207         tmp_vpath = mc_config_get_full_vpath (EDIT_HOME_TEMP_FILE);
3208         edit_insert_file (edit, tmp_vpath);
3209         vfs_path_free (tmp_vpath);
3210     }
3211 
3212     return 0;
3213 }
3214 
3215 /* --------------------------------------------------------------------------------------------- */
3216 /**
3217  * Ask user for a command, execute it and paste its output back to the
3218  * editor.
3219  */
3220 
3221 int
3222 edit_ext_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3223 {
3224     char *exp, *tmp, *tmp_edit_temp_file;
3225     int e;
3226 
3227     exp =
3228         input_dialog (_("Paste output of external command"),
3229                       _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, INPUT_LAST_TEXT,
3230                       INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_VARIABLES | INPUT_COMPLETE_USERNAMES
3231                       | INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_CD | INPUT_COMPLETE_COMMANDS |
3232                       INPUT_COMPLETE_SHELL_ESC);
3233 
3234     if (!exp)
3235         return 1;
3236 
3237     tmp_edit_temp_file = mc_config_get_full_path (EDIT_HOME_TEMP_FILE);
3238     tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3239     g_free (tmp_edit_temp_file);
3240     e = system (tmp);
3241     g_free (tmp);
3242     g_free (exp);
3243 
3244     if (e != 0)
3245     {
3246         edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3247         return -1;
3248     }
3249 
3250     edit->force |= REDRAW_COMPLETELY;
3251 
3252     {
3253         vfs_path_t *tmp_vpath;
3254 
3255         tmp_vpath = mc_config_get_full_vpath (EDIT_HOME_TEMP_FILE);
3256         edit_insert_file (edit, tmp_vpath);
3257         vfs_path_free (tmp_vpath);
3258     }
3259 
3260     return 0;
3261 }
3262 
3263 /* --------------------------------------------------------------------------------------------- */
3264 /** if block is 1, a block must be highlighted and the shell command
3265    processes it. If block is 0 the shell command is a straight system
3266    command, that just produces some output which is to be inserted */
3267 
3268 void
3269 edit_block_process_cmd (WEdit * edit, int macro_number)
     /* [previous][next][first][last][top][bottom][index][help]  */
3270 {
3271     char *fname;
3272     char *macros_fname = NULL;
3273 
3274     fname = g_strdup_printf ("%s.%i.sh", EDIT_HOME_MACRO_FILE, macro_number);
3275     macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3276     user_menu (edit, macros_fname, 0);
3277     g_free (fname);
3278     g_free (macros_fname);
3279     edit->force |= REDRAW_COMPLETELY;
3280 }
3281 
3282 /* --------------------------------------------------------------------------------------------- */
3283 
3284 void
3285 edit_mail_dialog (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3286 {
3287     char *mail_to, *mail_subject, *mail_cc;
3288 
3289     quick_widget_t quick_widgets[] = {
3290         /* *INDENT-OFF* */
3291         QUICK_LABEL (N_("mail -s <subject> -c <cc> <to>"), NULL),
3292         QUICK_LABELED_INPUT (N_("To"), input_label_above,
3293                              INPUT_LAST_TEXT, "mail-dlg-input-3",
3294                              &mail_to, NULL, FALSE, FALSE, INPUT_COMPLETE_USERNAMES),
3295         QUICK_LABELED_INPUT (N_("Subject"), input_label_above,
3296                               INPUT_LAST_TEXT, "mail-dlg-input-2",
3297                               &mail_subject, NULL, FALSE, FALSE, INPUT_COMPLETE_NONE),
3298         QUICK_LABELED_INPUT (N_("Copies to"), input_label_above,
3299                              INPUT_LAST_TEXT, "mail-dlg-input",
3300                              &mail_cc, NULL, FALSE, FALSE, INPUT_COMPLETE_USERNAMES),
3301         QUICK_BUTTONS_OK_CANCEL,
3302         QUICK_END
3303         /* *INDENT-ON* */
3304     };
3305 
3306     quick_dialog_t qdlg = {
3307         -1, -1, 50,
3308         N_("Mail"), "[Input Line Keys]",
3309         quick_widgets, NULL, NULL
3310     };
3311 
3312     if (quick_dialog (&qdlg) != B_CANCEL)
3313     {
3314         pipe_mail (&edit->buffer, mail_to, mail_subject, mail_cc);
3315         g_free (mail_to);
3316         g_free (mail_subject);
3317         g_free (mail_cc);
3318     }
3319 }
3320 
3321 /* --------------------------------------------------------------------------------------------- */
3322 
3323 /*******************/
3324 /* Word Completion */
3325 /*******************/
3326 
3327 /**
3328  * Complete current word using regular expression search
3329  * backwards beginning at the current cursor position.
3330  */
3331 
3332 void
3333 edit_complete_word_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3334 {
3335     gsize i, max_len, word_len = 0, num_compl = 0;
3336     off_t word_start = 0;
3337     GString *match_expr;
3338     GString *compl[MAX_WORD_COMPLETIONS];       /* completions */
3339 
3340     /* search start of word to be completed */
3341     if (!edit_find_word_start (&edit->buffer, &word_start, &word_len))
3342         return;
3343 
3344     /* prepare match expression */
3345     /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3346     match_expr = g_string_new ("(^|\\s+|\\b)");
3347     for (i = 0; i < word_len; i++)
3348         g_string_append_c (match_expr, edit_buffer_get_byte (&edit->buffer, word_start + i));
3349     g_string_append (match_expr,
3350                      "[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+");
3351 
3352     /* collect the possible completions              */
3353     /* start search from begin to end of file */
3354     max_len =
3355         edit_collect_completions (edit, word_start, word_len, match_expr->str, (GString **) & compl,
3356                                   &num_compl);
3357 
3358     if (num_compl > 0)
3359     {
3360         /* insert completed word if there is only one match */
3361         if (num_compl == 1)
3362             edit_complete_word_insert_recoded_completion (edit, compl[0]->str, word_len);
3363         /* more than one possible completion => ask the user */
3364         else
3365         {
3366             char *curr_compl;
3367 
3368             /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3369             /* !!! pressed again the selection dialog pops up, but that  !!! */
3370             /* !!! seems to require a further internal state             !!! */
3371             /*tty_beep (); */
3372 
3373             /* let the user select the preferred completion */
3374             curr_compl = editcmd_dialog_completion_show (edit, max_len,
3375                                                          (GString **) & compl, num_compl);
3376 
3377             if (curr_compl != NULL)
3378             {
3379                 edit_complete_word_insert_recoded_completion (edit, curr_compl, word_len);
3380                 g_free (curr_compl);
3381             }
3382         }
3383     }
3384 
3385     g_string_free (match_expr, TRUE);
3386     /* release memory before return */
3387     for (i = 0; i < num_compl; i++)
3388         g_string_free (compl[i], TRUE);
3389 }
3390 
3391 /* --------------------------------------------------------------------------------------------- */
3392 
3393 #ifdef HAVE_CHARSET
3394 void
3395 edit_select_codepage_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3396 {
3397     if (do_select_codepage ())
3398         edit_set_codeset (edit);
3399 
3400     edit->force = REDRAW_PAGE;
3401     widget_draw (WIDGET (edit));
3402 }
3403 #endif
3404 
3405 /* --------------------------------------------------------------------------------------------- */
3406 
3407 void
3408 edit_insert_literal_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3409 {
3410     int char_for_insertion;
3411 
3412     char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3413                                                        _("Press any key:"), FALSE);
3414     edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3415 }
3416 
3417 /* --------------------------------------------------------------------------------------------- */
3418 
3419 void
3420 edit_begin_end_macro_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3421 {
3422     /* edit is a pointer to the widget */
3423     if (edit != NULL)
3424     {
3425         long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3426 
3427         edit_execute_key_command (edit, command, -1);
3428     }
3429 }
3430 
3431  /* --------------------------------------------------------------------------------------------- */
3432 
3433 void
3434 edit_begin_end_repeat_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3435 {
3436     /* edit is a pointer to the widget */
3437     if (edit != NULL)
3438     {
3439         long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3440 
3441         edit_execute_key_command (edit, command, -1);
3442     }
3443 }
3444 
3445 /* --------------------------------------------------------------------------------------------- */
3446 
3447 gboolean
3448 edit_load_forward_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3449 {
3450     if (edit->modified
3451         && edit_query_dialog2 (_("Warning"),
3452                                _("Current text was modified without a file save.\n"
3453                                  "Continue discards these changes."), _("C&ontinue"),
3454                                _("&Cancel")) == 1)
3455     {
3456         edit->force |= REDRAW_COMPLETELY;
3457         return TRUE;
3458     }
3459 
3460     if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
3461         return FALSE;
3462 
3463     if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3464         return FALSE;
3465 
3466     edit_stack_iterator++;
3467     if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3468         return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3469                                  edit_history_moveto[edit_stack_iterator].line);
3470 
3471     return FALSE;
3472 }
3473 
3474 /* --------------------------------------------------------------------------------------------- */
3475 
3476 gboolean
3477 edit_load_back_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3478 {
3479     if (edit->modified
3480         && edit_query_dialog2 (_("Warning"),
3481                                _("Current text was modified without a file save.\n"
3482                                  "Continue discards these changes."), _("C&ontinue"),
3483                                _("&Cancel")) == 1)
3484     {
3485         edit->force |= REDRAW_COMPLETELY;
3486         return TRUE;
3487     }
3488 
3489     /* we are in the bottom of the stack, NO WAY! */
3490     if (edit_stack_iterator == 0)
3491         return FALSE;
3492 
3493     edit_stack_iterator--;
3494     if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3495         return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3496                                  edit_history_moveto[edit_stack_iterator].line);
3497 
3498     return FALSE;
3499 }
3500 
3501 /* --------------------------------------------------------------------------------------------- */
3502 
3503 void
3504 edit_get_match_keyword_cmd (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3505 {
3506     gsize word_len = 0, max_len = 0;
3507     int num_def = 0;
3508     gsize i;
3509     off_t word_start = 0;
3510     GString *match_expr;
3511     char *path = NULL;
3512     char *ptr = NULL;
3513     char *tagfile = NULL;
3514 
3515     etags_hash_t def_hash[MAX_DEFINITIONS];
3516 
3517     for (i = 0; i < MAX_DEFINITIONS; i++)
3518         def_hash[i].filename = NULL;
3519 
3520     /* search start of word to be completed */
3521     if (!edit_find_word_start (&edit->buffer, &word_start, &word_len))
3522         return;
3523 
3524     /* prepare match expression */
3525     match_expr = g_string_sized_new (word_len);
3526     for (i = 0; i < word_len; i++)
3527         g_string_append_c (match_expr, edit_buffer_get_byte (&edit->buffer, word_start + i));
3528 
3529     ptr = g_get_current_dir ();
3530     path = g_strconcat (ptr, PATH_SEP_STR, (char *) NULL);
3531     g_free (ptr);
3532 
3533     /* Recursive search file 'TAGS' in parent dirs */
3534     do
3535     {
3536         ptr = g_path_get_dirname (path);
3537         g_free (path);
3538         path = ptr;
3539         g_free (tagfile);
3540         tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3541         if (exist_file (tagfile))
3542             break;
3543     }
3544     while (strcmp (path, PATH_SEP_STR) != 0);
3545 
3546     if (tagfile != NULL)
3547     {
3548         num_def =
3549             etags_set_definition_hash (tagfile, path, match_expr->str, (etags_hash_t *) & def_hash);
3550         g_free (tagfile);
3551     }
3552     g_free (path);
3553 
3554     max_len = MAX_WIDTH_DEF_DIALOG;
3555     word_len = 0;
3556     if (num_def > 0)
3557         editcmd_dialog_select_definition_show (edit, match_expr->str, max_len, word_len,
3558                                                (etags_hash_t *) & def_hash, num_def);
3559     g_string_free (match_expr, TRUE);
3560 }
3561 
3562 /* --------------------------------------------------------------------------------------------- */
3563 
3564 #ifdef HAVE_ASPELL
3565 int
3566 edit_suggest_current_word (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3567 {
3568     gsize cut_len = 0;
3569     gsize word_len = 0;
3570     off_t word_start = 0;
3571     int retval = B_SKIP_WORD;
3572     GString *match_word;
3573 
3574     /* search start of word to spell check */
3575     match_word = edit_buffer_get_word_from_pos (&edit->buffer, edit->buffer.curs1, &word_start,
3576                                                 &cut_len);
3577     word_len = match_word->len;
3578 
3579 #ifdef HAVE_CHARSET
3580     if (mc_global.source_codepage >= 0 && mc_global.source_codepage != mc_global.display_codepage)
3581     {
3582         GString *tmp_word;
3583 
3584         tmp_word = str_convert_to_display (match_word->str);
3585         g_string_free (match_word, TRUE);
3586         match_word = tmp_word;
3587     }
3588 #endif
3589     if (!aspell_check (match_word->str, (int) word_len))
3590     {
3591         GArray *suggest;
3592         unsigned int res;
3593         guint i;
3594 
3595         suggest = g_array_new (TRUE, FALSE, sizeof (char *));
3596 
3597         res = aspell_suggest (suggest, match_word->str, (int) word_len);
3598         if (res != 0)
3599         {
3600             char *new_word = NULL;
3601 
3602             edit->found_start = word_start;
3603             edit->found_len = word_len;
3604             edit->force |= REDRAW_PAGE;
3605             edit_scroll_screen_over_cursor (edit);
3606             edit_render_keypress (edit);
3607 
3608             retval = spell_dialog_spell_suggest_show (edit, match_word->str, &new_word, suggest);
3609             edit_cursor_move (edit, word_len - cut_len);
3610 
3611             if (retval == B_ENTER && new_word != NULL)
3612             {
3613                 char *cp_word;
3614 
3615 #ifdef HAVE_CHARSET
3616                 if (mc_global.source_codepage >= 0 &&
3617                     (mc_global.source_codepage != mc_global.display_codepage))
3618                 {
3619                     GString *tmp_word;
3620 
3621                     tmp_word = str_convert_to_input (new_word);
3622                     g_free (new_word);
3623                     new_word = g_string_free (tmp_word, FALSE);
3624                 }
3625 #endif
3626                 cp_word = new_word;
3627                 for (i = 0; i < word_len; i++)
3628                     edit_backspace (edit, TRUE);
3629                 for (; *new_word; new_word++)
3630                     edit_insert (edit, *new_word);
3631                 g_free (cp_word);
3632             }
3633             else if (retval == B_ADD_WORD && match_word != NULL)
3634                 aspell_add_to_dict (match_word->str, (int) word_len);
3635         }
3636 
3637         for (i = 0; i < suggest->len; i++)
3638         {
3639             char *cur_sugg_word;
3640 
3641             cur_sugg_word = g_array_index (suggest, char *, i);
3642             g_free (cur_sugg_word);
3643         }
3644         g_array_free (suggest, TRUE);
3645         edit->found_start = 0;
3646         edit->found_len = 0;
3647     }
3648 
3649     g_string_free (match_word, TRUE);
3650 
3651     return retval;
3652 }
3653 
3654 /* --------------------------------------------------------------------------------------------- */
3655 
3656 void
3657 edit_spellcheck_file (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
3658 {
3659     if (edit->buffer.curs_line > 0)
3660     {
3661         edit_cursor_move (edit, -edit->buffer.curs1);
3662         edit_move_to_prev_col (edit, 0);
3663         edit_update_curs_row (edit);
3664     }
3665 
3666     do
3667     {
3668         int c1, c2;
3669 
3670         c2 = edit_buffer_get_current_byte (&edit->buffer);
3671 
3672         do
3673         {
3674             if (edit->buffer.curs1 >= edit->buffer.size)
3675                 return;
3676 
3677             c1 = c2;
3678             edit_cursor_move (edit, 1);
3679             c2 = edit_buffer_get_current_byte (&edit->buffer);
3680         }
3681         while (is_break_char (c1) || is_break_char (c2));
3682     }
3683     while (edit_suggest_current_word (edit) != B_CANCEL);
3684 }
3685 
3686 /* --------------------------------------------------------------------------------------------- */
3687 
3688 void
3689 edit_set_spell_lang (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
3690 {
3691     GArray *lang_list;
3692 
3693     lang_list = g_array_new (TRUE, FALSE, sizeof (char *));
3694     if (aspell_get_lang_list (lang_list) != 0)
3695     {
3696         char *lang;
3697 
3698         lang = spell_dialog_lang_list_show (lang_list);
3699         if (lang != NULL)
3700         {
3701             (void) aspell_set_lang (lang);
3702             g_free (lang);
3703         }
3704     }
3705     aspell_array_clean (lang_list);
3706 }
3707 #endif /* HAVE_ASPELL */
3708 
3709 /* --------------------------------------------------------------------------------------------- */

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