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

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