Return values of following functions are constants now:
[midnight-commander.git] / src / editor / editcmd.c
blob4696687248f0275e911ec5b9dcb5e19ceb2e5044
1 /*
2 Editor high level editing commands
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer, 1996, 1997
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /** \file
28 * \brief Source: editor high level editing commands
29 * \author Paul Sheer
30 * \date 1996, 1997
33 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
35 #include <config.h>
37 #include <assert.h>
38 #include <ctype.h>
40 #include <stdio.h>
41 #include <stdarg.h>
42 #include <sys/types.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <errno.h>
46 #include <sys/stat.h>
47 #include <stdlib.h>
48 #include <fcntl.h>
50 #include "lib/global.h"
51 #include "lib/tty/tty.h"
52 #include "lib/tty/key.h" /* XCTRL */
53 #include "lib/mcconfig.h"
54 #include "lib/skin.h"
55 #include "lib/strutil.h" /* utf string functions */
56 #include "lib/lock.h"
57 #include "lib/util.h" /* tilde_expand() */
58 #include "lib/vfs/vfs.h"
59 #include "lib/widget.h"
60 #include "lib/charsets.h"
61 #include "lib/event.h" /* mc_event_raise() */
63 #include "src/history.h"
64 #include "src/setup.h" /* option_tab_spacing */
65 #include "src/main.h" /* mactos_t */
66 #include "src/selcodepage.h"
67 #include "src/keybind-defaults.h"
68 #include "src/util.h" /* check_for_default() */
69 #include "src/filemanager/layout.h" /* mc_refresh() */
71 #include "edit-impl.h"
72 #include "edit-widget.h"
73 #include "editcmd_dialogs.h"
74 #include "etags.h"
76 /*** global variables ****************************************************************************/
78 /* search and replace: */
79 int search_create_bookmark = FALSE;
81 /* queries on a save */
82 int edit_confirm_save = 1;
84 /*** file scope macro definitions ****************************************************************/
86 #define space_width 1
88 #define TEMP_BUF_LEN 1024
90 #define INPUT_INDEX 9
92 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
93 (and the above) routines to work properly - paul */
95 #define is_digit(x) ((x) >= '0' && (x) <= '9')
97 #define MAIL_DLG_HEIGHT 12
99 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
101 /*** file scope type declarations ****************************************************************/
103 /*** file scope variables ************************************************************************/
105 /*** file scope functions ************************************************************************/
106 /* --------------------------------------------------------------------------------------------- */
108 /* If 0 (quick save) then a) create/truncate <filename> file,
109 b) save to <filename>;
110 if 1 (safe save) then a) save to <tempnam>,
111 b) rename <tempnam> to <filename>;
112 if 2 (do backups) then a) save to <tempnam>,
113 b) rename <filename> to <filename.backup_ext>,
114 c) rename <tempnam> to <filename>. */
116 /* returns 0 on error, -1 on abort */
118 static int
119 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
121 char *p;
122 gchar *tmp;
123 long filelen = 0;
124 int this_save_mode, fd = -1;
125 vfs_path_t *real_filename_vpath;
126 vfs_path_t *savename_vpath = NULL;
127 const char *start_filename;
129 start_filename = vfs_path_get_by_index (filename_vpath, 0)->path;
130 if (filename_vpath == NULL || *start_filename == '\0')
131 return 0;
133 if (*start_filename != PATH_SEP && edit->dir_vpath != NULL)
135 real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
137 else
139 real_filename_vpath = vfs_path_clone (filename_vpath);
142 this_save_mode = option_save_mode;
143 if (this_save_mode != EDIT_QUICK_SAVE)
145 if (!vfs_file_is_local (real_filename_vpath)
146 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
149 * The file does not exists yet, so no safe save or
150 * backup are necessary.
152 this_save_mode = EDIT_QUICK_SAVE;
154 if (fd != -1)
155 mc_close (fd);
158 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt)
160 int rv;
161 struct stat sb;
163 rv = mc_stat (real_filename_vpath, &sb);
164 if (rv == 0 && sb.st_nlink > 1)
166 rv = edit_query_dialog3 (_("Warning"),
167 _("File has hard-links. Detach before saving?"),
168 _("&Yes"), _("&No"), _("&Cancel"));
169 switch (rv)
171 case 0:
172 this_save_mode = EDIT_SAFE_SAVE;
173 /* fallthrough */
174 case 1:
175 edit->skip_detach_prompt = 1;
176 break;
177 default:
178 vfs_path_free (real_filename_vpath);
179 return -1;
183 /* Prevent overwriting changes from other editor sessions. */
184 if (rv == 0 && edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
186 /* The default action is "Cancel". */
187 query_set_sel (1);
189 rv = edit_query_dialog2 (_("Warning"),
190 _("The file has been modified in the meantime. Save anyway?"),
191 _("&Yes"), _("&Cancel"));
192 if (rv != 0)
194 vfs_path_free (real_filename_vpath);
195 return -1;
200 if (this_save_mode != EDIT_QUICK_SAVE)
202 char *savedir, *saveprefix;
204 savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
205 if (savedir == NULL)
206 savedir = g_strdup (".");
208 saveprefix = mc_build_filename (savedir, "cooledit", NULL);
209 g_free (savedir);
210 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
211 g_free (saveprefix);
212 if (savename_vpath == NULL)
214 vfs_path_free (real_filename_vpath);
215 return 0;
217 /* FIXME:
218 * Close for now because mc_mkstemps use pure open system call
219 * to create temporary file and it needs to be reopened by
220 * VFS-aware mc_open().
222 close (fd);
224 else
225 savename_vpath = vfs_path_clone (real_filename_vpath);
227 (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
228 (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
230 fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
231 if (fd == -1)
232 goto error_save;
234 /* pipe save */
235 p = edit_get_write_filter (savename_vpath, real_filename_vpath);
236 if (p != NULL)
238 FILE *file;
240 mc_close (fd);
241 file = (FILE *) popen (p, "w");
243 if (file)
245 filelen = edit_write_stream (edit, file);
246 #if 1
247 pclose (file);
248 #else
249 if (pclose (file) != 0)
251 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
252 edit_error_dialog (_("Error"), tmp);
253 g_free (tmp);
254 g_free (p);
255 goto error_save;
257 #endif
259 else
261 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
262 edit_error_dialog (_("Error"), get_sys_error (tmp));
263 g_free (p);
264 g_free (tmp);
265 goto error_save;
267 g_free (p);
269 else if (edit->lb == LB_ASIS)
270 { /* do not change line breaks */
271 long buf;
272 buf = 0;
273 filelen = edit->last_byte;
274 while (buf <= (edit->curs1 >> S_EDIT_BUF_SIZE) - 1)
276 if (mc_write (fd, (char *) edit->buffers1[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
278 mc_close (fd);
279 goto error_save;
281 buf++;
283 if (mc_write
284 (fd, (char *) edit->buffers1[buf],
285 edit->curs1 & M_EDIT_BUF_SIZE) != (edit->curs1 & M_EDIT_BUF_SIZE))
287 filelen = -1;
289 else if (edit->curs2)
291 edit->curs2--;
292 buf = (edit->curs2 >> S_EDIT_BUF_SIZE);
293 if (mc_write
294 (fd,
295 (char *) edit->buffers2[buf] + EDIT_BUF_SIZE -
296 (edit->curs2 & M_EDIT_BUF_SIZE) - 1,
297 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) != 1 + (edit->curs2 & M_EDIT_BUF_SIZE))
299 filelen = -1;
301 else
303 while (--buf >= 0)
305 if (mc_write (fd, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
307 filelen = -1;
308 break;
312 edit->curs2++;
314 if (mc_close (fd))
315 goto error_save;
317 /* Update the file information, especially the mtime. */
318 if (mc_stat (savename_vpath, &edit->stat1) == -1)
319 goto error_save;
321 else
322 { /* change line breaks */
323 FILE *file;
324 const vfs_path_element_t *path_element;
326 mc_close (fd);
328 path_element = vfs_path_get_by_index (savename_vpath, -1);
329 file = (FILE *) fopen (path_element->path, "w");
330 if (file != NULL)
332 filelen = edit_write_stream (edit, file);
333 fclose (file);
335 else
337 char *msg;
339 msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
340 edit_error_dialog (_("Error"), msg);
341 g_free (msg);
342 goto error_save;
346 if (filelen != edit->last_byte)
347 goto error_save;
349 if (this_save_mode == EDIT_DO_BACKUP)
351 vfs_path_t *tmp_vpath;
352 gboolean ok;
354 assert (option_backup_ext != NULL);
355 tmp_vpath = vfs_path_append_new (real_filename_vpath, option_backup_ext, (char *) NULL);
356 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
357 vfs_path_free (tmp_vpath);
358 if (!ok)
359 goto error_save;
362 if (this_save_mode != EDIT_QUICK_SAVE)
363 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
364 goto error_save;
366 vfs_path_free (real_filename_vpath);
367 vfs_path_free (savename_vpath);
368 return 1;
369 error_save:
370 /* FIXME: Is this safe ?
371 * if (this_save_mode != EDIT_QUICK_SAVE)
372 * mc_unlink (savename);
374 vfs_path_free (real_filename_vpath);
375 vfs_path_free (savename_vpath);
376 return 0;
379 /* --------------------------------------------------------------------------------------------- */
381 static gboolean
382 edit_check_newline (WEdit * edit)
384 return !(option_check_nl_at_eof && edit->last_byte > 0
385 && edit_get_byte (edit, edit->last_byte - 1) != '\n'
386 && edit_query_dialog2 (_("Warning"),
387 _("The file you are saving is not finished with a newline"),
388 _("C&ontinue"), _("&Cancel")));
391 /* --------------------------------------------------------------------------------------------- */
393 static vfs_path_t *
394 edit_get_save_file_as (WEdit * edit)
396 #define DLG_WIDTH 64
397 #define DLG_HEIGHT 14
399 static LineBreaks cur_lb = LB_ASIS;
401 char *filename = vfs_path_to_str (edit->filename_vpath);
402 char *filename_res;
404 const char *lb_names[LB_NAMES] = {
405 N_("&Do not change"),
406 N_("&Unix format (LF)"),
407 N_("&Windows/DOS format (CR LF)"),
408 N_("&Macintosh format (CR)")
411 QuickWidget quick_widgets[] = {
412 QUICK_BUTTON (6, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
413 QUICK_BUTTON (2, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
414 QUICK_RADIO (5, DLG_WIDTH, DLG_HEIGHT - 8, DLG_HEIGHT, LB_NAMES, lb_names, (int *) &cur_lb),
415 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 9, DLG_HEIGHT, N_("Change line breaks to:")),
416 QUICK_INPUT (3, DLG_WIDTH, DLG_HEIGHT - 11, DLG_HEIGHT, filename, DLG_WIDTH - 6, 0,
417 "save-as", &filename_res),
418 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 12, DLG_HEIGHT, N_("Enter file name:")),
419 QUICK_END
422 QuickDialog Quick_options = {
423 DLG_WIDTH, DLG_HEIGHT, -1, -1,
424 N_("Save As"), "[Save File As]",
425 quick_widgets, NULL, FALSE
428 if (quick_dialog (&Quick_options) != B_CANCEL)
430 char *fname;
431 vfs_path_t *ret_vpath;
433 edit->lb = cur_lb;
434 fname = tilde_expand (filename_res);
435 g_free (filename_res);
436 ret_vpath = vfs_path_from_str (fname);
437 g_free (fname);
438 return ret_vpath;
440 g_free (filename);
442 return NULL;
444 #undef DLG_WIDTH
445 #undef DLG_HEIGHT
448 /* --------------------------------------------------------------------------------------------- */
450 /** returns 1 on success */
452 static int
453 edit_save_cmd (WEdit * edit)
455 int res, save_lock = 0;
457 if (!edit->locked && !edit->delete_file)
458 save_lock = edit_lock_file (edit);
459 res = edit_save_file (edit, edit->filename_vpath);
461 /* Maintain modify (not save) lock on failure */
462 if ((res > 0 && edit->locked) || save_lock)
463 edit->locked = edit_unlock_file (edit);
465 /* On failure try 'save as', it does locking on its own */
466 if (!res)
467 return edit_save_as_cmd (edit);
468 edit->force |= REDRAW_COMPLETELY;
469 if (res > 0)
471 edit->delete_file = 0;
472 edit->modified = 0;
475 return 1;
478 /* --------------------------------------------------------------------------------------------- */
479 /** returns 1 on error */
481 static int
482 edit_load_file_from_filename (WEdit * edit, const vfs_path_t * exp_vpath)
484 int prev_locked = edit->locked;
485 vfs_path_t *prev_filename;
487 prev_filename = vfs_path_clone (edit->filename_vpath);
488 if (!edit_reload (edit, exp_vpath))
490 vfs_path_free (prev_filename);
491 return 1;
494 if (prev_locked)
496 vfs_path_t *fullpath;
498 fullpath = vfs_path_append_vpath_new (edit->dir_vpath, prev_filename, (char *) NULL);
499 unlock_file (fullpath);
500 vfs_path_free (fullpath);
502 vfs_path_free (prev_filename);
503 return 0;
506 /* --------------------------------------------------------------------------------------------- */
508 static void
509 edit_load_syntax_file (WEdit * edit)
511 vfs_path_t *extdir_vpath;
512 int dir = 0;
514 if (geteuid () == 0)
516 dir = query_dialog (_("Syntax file edit"),
517 _("Which syntax file you want to edit?"), D_NORMAL, 2,
518 _("&User"), _("&System Wide"));
521 extdir_vpath =
522 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
523 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
525 vfs_path_free (extdir_vpath);
526 extdir_vpath =
527 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
530 if (dir == 0)
532 vfs_path_t *user_syntax_file_vpath;
534 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
535 check_for_default (extdir_vpath, user_syntax_file_vpath);
536 edit_load_file_from_filename (edit, user_syntax_file_vpath);
537 vfs_path_free (user_syntax_file_vpath);
539 else if (dir == 1)
540 edit_load_file_from_filename (edit, extdir_vpath);
542 vfs_path_free (extdir_vpath);
545 /* --------------------------------------------------------------------------------------------- */
547 static void
548 edit_load_menu_file (WEdit * edit)
550 vfs_path_t *buffer_vpath;
551 vfs_path_t *menufile_vpath;
552 int dir = 0;
554 dir = query_dialog (_("Menu edit"),
555 _("Which menu file do you want to edit?"), D_NORMAL,
556 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
558 menufile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
560 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
562 vfs_path_free (menufile_vpath);
563 menufile_vpath = vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
566 switch (dir)
568 case 0:
569 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
570 check_for_default (menufile_vpath, buffer_vpath);
571 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
572 break;
574 case 1:
575 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
576 check_for_default (menufile_vpath, buffer_vpath);
577 break;
579 case 2:
580 buffer_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
581 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
583 vfs_path_free (buffer_vpath);
584 buffer_vpath =
585 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
587 break;
589 default:
590 vfs_path_free (menufile_vpath);
591 return;
594 edit_load_file_from_filename (edit, buffer_vpath);
596 vfs_path_free (buffer_vpath);
597 vfs_path_free (menufile_vpath);
600 /* --------------------------------------------------------------------------------------------- */
602 static void
603 edit_delete_column_of_text (WEdit * edit)
605 long p, q, r, m1, m2;
606 long b, c, d, n;
608 eval_marks (edit, &m1, &m2);
609 n = edit_move_forward (edit, m1, 0, m2) + 1;
610 c = edit_move_forward3 (edit, edit_bol (edit, m1), 0, m1);
611 d = edit_move_forward3 (edit, edit_bol (edit, m2), 0, m2);
612 b = max (min (c, d), min (edit->column1, edit->column2));
613 c = max (c, max (edit->column1, edit->column2));
615 while (n--)
617 r = edit_bol (edit, edit->curs1);
618 p = edit_move_forward3 (edit, r, b, 0);
619 q = edit_move_forward3 (edit, r, c, 0);
620 if (p < m1)
621 p = m1;
622 if (q > m2)
623 q = m2;
624 edit_cursor_move (edit, p - edit->curs1);
625 while (q > p)
627 /* delete line between margins */
628 if (edit_get_byte (edit, edit->curs1) != '\n')
629 edit_delete (edit, 1);
630 q--;
632 if (n)
633 /* move to next line except on the last delete */
634 edit_cursor_move (edit, edit_move_forward (edit, edit->curs1, 1, 0) - edit->curs1);
638 /* --------------------------------------------------------------------------------------------- */
639 /** if success return 0 */
641 static int
642 edit_block_delete (WEdit * edit)
644 long count;
645 long start_mark, end_mark;
646 int curs_pos, line_width;
647 long curs_line, c1, c2;
649 if (eval_marks (edit, &start_mark, &end_mark))
650 return 0;
651 if (edit->column_highlight && edit->mark2 < 0)
652 edit_mark_cmd (edit, 0);
653 if ((end_mark - start_mark) > option_max_undo / 2)
655 /* Warning message with a query to continue or cancel the operation */
656 if (edit_query_dialog2
657 (_("Warning"),
659 ("Block is large, you may not be able to undo this action"),
660 _("C&ontinue"), _("&Cancel")))
662 return 1;
665 c1 = min (edit->column1, edit->column2);
666 c2 = max (edit->column1, edit->column2);
667 edit->column1 = c1;
668 edit->column2 = c2;
670 edit_push_markers (edit);
672 curs_line = edit->curs_line;
674 /* calculate line width and cursor position before cut */
675 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
676 edit_eol (edit, edit->curs1));
677 curs_pos = edit->curs_col + edit->over_col;
679 /* move cursor to start of selection */
680 edit_cursor_move (edit, start_mark - edit->curs1);
681 edit_scroll_screen_over_cursor (edit);
682 count = start_mark;
683 if (start_mark < end_mark)
685 if (edit->column_highlight)
687 if (edit->mark2 < 0)
688 edit_mark_cmd (edit, 0);
689 edit_delete_column_of_text (edit);
690 /* move cursor to the saved position */
691 edit_move_to_line (edit, curs_line);
692 /* calculate line width after cut */
693 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
694 edit_eol (edit, edit->curs1));
695 if (option_cursor_beyond_eol && curs_pos > line_width)
696 edit->over_col = curs_pos - line_width;
698 else
700 while (count < end_mark)
702 edit_delete (edit, 1);
703 count++;
707 edit_set_markers (edit, 0, 0, 0, 0);
708 edit->force |= REDRAW_PAGE;
709 return 0;
712 /* --------------------------------------------------------------------------------------------- */
714 static gboolean
715 editcmd_find (WEdit * edit, gsize * len)
717 off_t search_start = edit->search_start;
718 off_t search_end;
719 long start_mark = 0;
720 long end_mark = edit->last_byte;
721 int mark_res = 0;
723 if (edit_search_options.only_in_selection)
725 mark_res = eval_marks (edit, &start_mark, &end_mark);
726 if (mark_res != 0)
728 edit->search->error = MC_SEARCH_E_NOTFOUND;
729 edit->search->error_str = g_strdup (_("Search string not found"));
730 return FALSE;
732 if (edit_search_options.backwards)
734 if (search_start > end_mark || search_start <= start_mark)
736 search_start = end_mark;
739 else
741 if (search_start < start_mark || search_start >= end_mark)
743 search_start = start_mark;
747 else
749 if (edit_search_options.backwards)
750 end_mark = max (1, edit->curs1) - 1;
752 if (edit_search_options.backwards)
754 search_end = end_mark;
755 while ((int) search_start >= start_mark)
757 if (search_end > (off_t) (search_start + edit->search->original_len) &&
758 mc_search_is_fixed_search_str (edit->search))
760 search_end = search_start + edit->search->original_len;
762 if (mc_search_run (edit->search, (void *) edit, search_start, search_end, len)
763 && edit->search->normal_offset == search_start)
765 return TRUE;
767 search_start--;
769 edit->search->error_str = g_strdup (_("Search string not found"));
771 else
773 return mc_search_run (edit->search, (void *) edit, search_start, end_mark, len);
775 return FALSE;
778 /* --------------------------------------------------------------------------------------------- */
780 static char *
781 edit_replace_cmd__conv_to_display (char *str)
783 #ifdef HAVE_CHARSET
784 GString *tmp;
786 tmp = str_convert_to_display (str);
787 if (tmp != NULL)
789 if (tmp->len != 0)
790 return g_string_free (tmp, FALSE);
791 g_string_free (tmp, TRUE);
793 #endif
794 return g_strdup (str);
797 /* --------------------------------------------------------------------------------------------- */
799 static char *
800 edit_replace_cmd__conv_to_input (char *str)
802 #ifdef HAVE_CHARSET
803 GString *tmp;
805 tmp = str_convert_to_input (str);
806 if (tmp != NULL)
808 if (tmp->len != 0)
809 return g_string_free (tmp, FALSE);
810 g_string_free (tmp, TRUE);
812 #endif
813 return g_strdup (str);
816 /* --------------------------------------------------------------------------------------------- */
818 static void
819 edit_do_search (WEdit * edit)
821 gsize len = 0;
823 if (edit->search == NULL)
824 edit->search_start = edit->curs1;
826 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
828 if (search_create_bookmark)
830 int found = 0, books = 0;
831 long l = 0, l_last = -1;
832 long q = 0;
834 search_create_bookmark = FALSE;
835 book_mark_flush (edit, -1);
837 while (TRUE)
839 if (!mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
840 break;
841 if (found == 0)
842 edit->search_start = edit->search->normal_offset;
843 found++;
844 l += edit_count_lines (edit, q, edit->search->normal_offset);
845 if (l != l_last)
847 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
848 books++;
850 l_last = l;
851 q = edit->search->normal_offset + 1;
854 if (found == 0)
855 edit_error_dialog (_("Search"), _("Search string not found"));
856 else
857 edit_cursor_move (edit, edit->search_start - edit->curs1);
859 else
861 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
862 && edit_search_options.backwards)
863 edit->search_start--;
865 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
866 && !edit_search_options.backwards)
867 edit->search_start++;
869 if (editcmd_find (edit, &len))
871 edit->found_start = edit->search_start = edit->search->normal_offset;
872 edit->found_len = len;
873 edit->over_col = 0;
874 edit_cursor_move (edit, edit->search_start - edit->curs1);
875 edit_scroll_screen_over_cursor (edit);
876 if (edit_search_options.backwards)
877 edit->search_start--;
878 else
879 edit->search_start++;
881 else
883 edit->search_start = edit->curs1;
884 if (edit->search->error_str != NULL)
885 edit_error_dialog (_("Search"), edit->search->error_str);
889 edit->force |= REDRAW_COMPLETELY;
890 edit_scroll_screen_over_cursor (edit);
893 /* --------------------------------------------------------------------------------------------- */
895 static void
896 edit_search (WEdit * edit)
898 if (editcmd_dialog_search_show (edit))
899 edit_do_search (edit);
902 /* --------------------------------------------------------------------------------------------- */
903 /** Return a null terminated length of text. Result must be g_free'd */
905 static unsigned char *
906 edit_get_block (WEdit * edit, long start, long finish, int *l)
908 unsigned char *s, *r;
909 r = s = g_malloc0 (finish - start + 1);
910 if (edit->column_highlight)
912 *l = 0;
913 /* copy from buffer, excluding chars that are out of the column 'margins' */
914 while (start < finish)
916 int c;
917 long x;
918 x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
919 c = edit_get_byte (edit, start);
920 if ((x >= edit->column1 && x < edit->column2)
921 || (x >= edit->column2 && x < edit->column1) || c == '\n')
923 *s++ = c;
924 (*l)++;
926 start++;
929 else
931 *l = finish - start;
932 while (start < finish)
933 *s++ = edit_get_byte (edit, start++);
935 *s = 0;
936 return r;
939 /* --------------------------------------------------------------------------------------------- */
940 /** copies a block to clipboard file */
942 static int
943 edit_save_block_to_clip_file (WEdit * edit, long start, long finish)
945 int ret;
946 gchar *tmp;
947 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
948 ret = edit_save_block (edit, tmp, start, finish);
949 g_free (tmp);
950 return ret;
953 /* --------------------------------------------------------------------------------------------- */
955 static void
956 pipe_mail (WEdit * edit, char *to, char *subject, char *cc)
958 FILE *p = 0;
959 char *s;
961 to = name_quote (to, 0);
962 subject = name_quote (subject, 0);
963 cc = name_quote (cc, 0);
964 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
965 g_free (to);
966 g_free (subject);
967 g_free (cc);
969 if (s)
971 p = popen (s, "w");
972 g_free (s);
975 if (p)
977 long i;
978 for (i = 0; i < edit->last_byte; i++)
979 fputc (edit_get_byte (edit, i), p);
980 pclose (p);
984 /* --------------------------------------------------------------------------------------------- */
986 static gboolean
987 is_break_char (char c)
989 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
992 /* --------------------------------------------------------------------------------------------- */
993 /** find first character of current word */
995 static int
996 edit_find_word_start (WEdit * edit, long *word_start, gsize * word_len)
998 int c, last;
999 gsize i;
1001 /* return if at begin of file */
1002 if (edit->curs1 <= 0)
1003 return 0;
1005 c = (unsigned char) edit_get_byte (edit, edit->curs1 - 1);
1006 /* return if not at end or in word */
1007 if (is_break_char (c))
1008 return 0;
1010 /* search start of word to be completed */
1011 for (i = 2;; i++)
1013 /* return if at begin of file */
1014 if ((gsize) edit->curs1 < i)
1015 return 0;
1017 last = c;
1018 c = (unsigned char) edit_get_byte (edit, edit->curs1 - i);
1020 if (is_break_char (c))
1022 /* return if word starts with digit */
1023 if (isdigit (last))
1024 return 0;
1026 *word_start = edit->curs1 - (i - 1); /* start found */
1027 *word_len = i - 1;
1028 break;
1031 /* success */
1032 return 1;
1035 /* --------------------------------------------------------------------------------------------- */
1037 * Get current word under cursor
1039 * @param edit editor object
1040 * @param srch mc_search object
1041 * @param word_start start word position
1043 * @return newly allocated string or NULL if no any words under cursor
1046 static char *
1047 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, long word_start)
1049 gsize len = 0, i;
1050 GString *temp;
1052 if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len))
1053 return NULL;
1055 temp = g_string_sized_new (len);
1057 for (i = 0; i < len; i++)
1059 int chr;
1061 chr = edit_get_byte (edit, word_start + i);
1062 if (!isspace (chr))
1063 g_string_append_c (temp, chr);
1066 return g_string_free (temp, temp->len == 0);
1069 /* --------------------------------------------------------------------------------------------- */
1070 /** collect the possible completions */
1072 static gsize
1073 edit_collect_completions (WEdit * edit, long word_start, gsize word_len,
1074 char *match_expr, struct selection *compl, gsize * num)
1076 gsize len = 0;
1077 gsize max_len = 0;
1078 gsize i;
1079 int skip;
1080 GString *temp;
1081 mc_search_t *srch;
1082 long last_byte, start = -1;
1083 char *current_word;
1085 srch = mc_search_new (match_expr, -1);
1086 if (srch == NULL)
1087 return 0;
1089 if (mc_config_get_bool
1090 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1092 last_byte = edit->last_byte;
1094 else
1096 last_byte = word_start;
1099 srch->search_type = MC_SEARCH_T_REGEX;
1100 srch->is_case_sensitive = TRUE;
1101 srch->search_fn = edit_search_cmd_callback;
1103 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1105 temp = g_string_new ("");
1107 /* collect max MAX_WORD_COMPLETIONS completions */
1108 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1110 g_string_set_size (temp, 0);
1111 start = srch->normal_offset;
1113 /* add matched completion if not yet added */
1114 for (i = 0; i < len; i++)
1116 skip = edit_get_byte (edit, start + i);
1117 if (isspace (skip))
1118 continue;
1120 /* skip current word */
1121 if (start + (long) i == word_start)
1122 break;
1124 g_string_append_c (temp, skip);
1127 if (temp->len == 0)
1128 continue;
1130 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1131 continue;
1133 skip = 0;
1135 for (i = 0; i < *num; i++)
1137 if (strncmp
1138 ((char *) &compl[i].text[word_len],
1139 (char *) &temp->str[word_len], max (len, compl[i].len) - word_len) == 0)
1141 struct selection this = compl[i];
1142 for (++i; i < *num; i++)
1144 compl[i - 1] = compl[i];
1146 compl[*num - 1] = this;
1147 skip = 1;
1148 break; /* skip it, already added */
1151 if (skip != 0)
1152 continue;
1154 if (*num == MAX_WORD_COMPLETIONS)
1156 g_free (compl[0].text);
1157 for (i = 1; i < *num; i++)
1159 compl[i - 1] = compl[i];
1161 (*num)--;
1163 #ifdef HAVE_CHARSET
1165 GString *recoded;
1166 recoded = str_convert_to_display (temp->str);
1168 if (recoded && recoded->len)
1169 g_string_assign (temp, recoded->str);
1171 g_string_free (recoded, TRUE);
1173 #endif
1174 compl[*num].text = g_strdup (temp->str);
1175 compl[*num].len = temp->len;
1176 (*num)++;
1177 start += len;
1179 /* note the maximal length needed for the completion dialog */
1180 if (len > max_len)
1181 max_len = len;
1184 mc_search_free (srch);
1185 g_string_free (temp, TRUE);
1186 g_free (current_word);
1188 return max_len;
1191 /* --------------------------------------------------------------------------------------------- */
1193 static void
1194 edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width,
1195 long *start_pos, long *end_pos, int *col1, int *col2)
1197 long cursor;
1198 int i, col;
1200 cursor = edit->curs1;
1201 col = edit_get_col (edit);
1203 for (i = 0; i < size; i++)
1205 if (data[i] != '\n')
1206 edit_insert (edit, data[i]);
1207 else
1208 { /* fill in and move to next line */
1209 int l;
1210 long p;
1212 if (edit_get_byte (edit, edit->curs1) != '\n')
1214 l = width - (edit_get_col (edit) - col);
1215 while (l > 0)
1217 edit_insert (edit, ' ');
1218 l -= space_width;
1221 for (p = edit->curs1;; p++)
1223 if (p == edit->last_byte)
1225 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1226 edit_insert_ahead (edit, '\n');
1227 p++;
1228 break;
1230 if (edit_get_byte (edit, p) == '\n')
1232 p++;
1233 break;
1236 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1237 l = col - edit_get_col (edit);
1238 while (l >= space_width)
1240 edit_insert (edit, ' ');
1241 l -= space_width;
1246 *col1 = col;
1247 *col2 = col + width;
1248 *start_pos = cursor;
1249 *end_pos = edit->curs1;
1250 edit_cursor_move (edit, cursor - edit->curs1);
1253 /* --------------------------------------------------------------------------------------------- */
1255 static int
1256 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1258 const macros_t *m1 = (const macros_t *) macro1;
1259 const macros_t *m2 = (const macros_t *) macro2;
1261 return m1->hotkey - m2->hotkey;
1264 /* --------------------------------------------------------------------------------------------- */
1266 static void
1267 edit_macro_sort_by_hotkey (void)
1269 if (macros_list != NULL && macros_list->len != 0)
1270 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1273 /* --------------------------------------------------------------------------------------------- */
1275 static gboolean
1276 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1278 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1279 macros_t *result;
1280 macros_t search_macro;
1282 (void) edit;
1284 search_macro.hotkey = hotkey;
1285 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1286 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1288 if (result != NULL && result->macro != NULL)
1290 *indx = (result - array_start);
1291 *macros = result;
1292 return TRUE;
1294 *indx = 0;
1295 return FALSE;
1298 /* --------------------------------------------------------------------------------------------- */
1299 /** returns FALSE on error */
1301 static gboolean
1302 edit_delete_macro (WEdit * edit, int hotkey)
1304 mc_config_t *macros_config = NULL;
1305 const char *section_name = "editor";
1306 gchar *macros_fname;
1307 guint indx;
1308 char *skeyname;
1309 const macros_t *macros = NULL;
1311 /* clear array of actions for current hotkey */
1312 while (edit_get_macro (edit, hotkey, &macros, &indx))
1314 if (macros->macro != NULL)
1315 g_array_free (macros->macro, TRUE);
1316 macros = NULL;
1317 g_array_remove_index (macros_list, indx);
1318 edit_macro_sort_by_hotkey ();
1321 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1322 macros_config = mc_config_init (macros_fname);
1323 g_free (macros_fname);
1325 if (macros_config == NULL)
1326 return FALSE;
1328 skeyname = lookup_key_by_code (hotkey);
1329 while (mc_config_del_key (macros_config, section_name, skeyname))
1331 g_free (skeyname);
1332 mc_config_save_file (macros_config, NULL);
1333 mc_config_deinit (macros_config);
1334 return TRUE;
1338 /* --------------------------------------------------------------------------------------------- */
1339 /*** public functions ****************************************************************************/
1340 /* --------------------------------------------------------------------------------------------- */
1342 void
1343 edit_help_cmd (WEdit * edit)
1345 ev_help_t event_data = { NULL, "[Internal File Editor]" };
1346 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
1348 edit->force |= REDRAW_COMPLETELY;
1351 /* --------------------------------------------------------------------------------------------- */
1353 void
1354 edit_refresh_cmd (WEdit * edit)
1356 #ifdef HAVE_SLANG
1357 int color;
1359 edit_get_syntax_color (edit, -1, &color);
1360 tty_touch_screen ();
1361 mc_refresh ();
1362 #else
1363 (void) edit;
1365 clr_scr ();
1366 repaint_screen ();
1367 #endif /* !HAVE_SLANG */
1368 tty_keypad (TRUE);
1371 /* --------------------------------------------------------------------------------------------- */
1373 void
1374 menu_save_mode_cmd (void)
1376 /* diaog sizes */
1377 const int DLG_X = 38;
1378 const int DLG_Y = 13;
1380 char *str_result;
1382 const char *str[] = {
1383 N_("&Quick save"),
1384 N_("&Safe save"),
1385 N_("&Do backups with following extension:")
1388 QuickWidget widgets[] = {
1389 /* 0 */
1390 QUICK_BUTTON (18, DLG_X, DLG_Y - 3, DLG_Y, N_("&Cancel"), B_CANCEL, NULL),
1391 /* 1 */
1392 QUICK_BUTTON (6, DLG_X, DLG_Y - 3, DLG_Y, N_("&OK"), B_ENTER, NULL),
1393 /* 2 */
1394 QUICK_CHECKBOX (4, DLG_X, 8, DLG_Y, N_("Check &POSIX new line"), &option_check_nl_at_eof),
1395 /* 3 */
1396 QUICK_INPUT (8, DLG_X, 6, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result),
1397 /* 4 */
1398 QUICK_RADIO (4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode),
1399 QUICK_END
1402 QuickDialog dialog = {
1403 DLG_X, DLG_Y, -1, -1, N_("Edit Save Mode"),
1404 "[Edit Save Mode]", widgets, NULL, FALSE
1407 size_t i;
1408 size_t maxlen = 0;
1409 size_t w0, w1, b_len, w3;
1411 assert (option_backup_ext != NULL);
1413 /* OK/Cancel buttons */
1414 w0 = str_term_width1 (_(widgets[0].u.button.text)) + 3;
1415 w1 = str_term_width1 (_(widgets[1].u.button.text)) + 5; /* default button */
1416 b_len = w0 + w1 + 3;
1418 maxlen = max (b_len, (size_t) str_term_width1 (_(dialog.title)) + 2);
1420 w3 = 0;
1421 for (i = 0; i < 3; i++)
1423 #ifdef ENABLE_NLS
1424 str[i] = _(str[i]);
1425 #endif
1426 w3 = max (w3, (size_t) str_term_width1 (str[i]));
1429 maxlen = max (maxlen, w3 + 4);
1431 dialog.xlen = min ((size_t) COLS, maxlen + 8);
1433 widgets[3].u.input.len = w3;
1434 widgets[1].relative_x = (dialog.xlen - b_len) / 2;
1435 widgets[0].relative_x = widgets[1].relative_x + w0 + 2;
1437 for (i = 0; i < sizeof (widgets) / sizeof (widgets[0]); i++)
1438 widgets[i].x_divisions = dialog.xlen;
1440 if (quick_dialog (&dialog) != B_CANCEL)
1442 g_free (option_backup_ext);
1443 option_backup_ext = str_result;
1447 /* --------------------------------------------------------------------------------------------- */
1449 void
1450 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1452 vfs_path_free (edit->filename_vpath);
1453 edit->filename_vpath = vfs_path_clone (name_vpath);
1455 if (edit->dir_vpath == NULL)
1456 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1459 /* --------------------------------------------------------------------------------------------- */
1460 /* Here we want to warn the users of overwriting an existing file,
1461 but only if they have made a change to the filename */
1462 /* returns 1 on success */
1464 edit_save_as_cmd (WEdit * edit)
1466 /* This heads the 'Save As' dialog box */
1467 vfs_path_t *exp_vpath;
1468 int save_lock = 0;
1469 int different_filename = 0;
1471 if (!edit_check_newline (edit))
1472 return 0;
1474 exp_vpath = edit_get_save_file_as (edit);
1475 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1477 if (exp_vpath != NULL)
1479 if (vfs_path_len (exp_vpath) == 0)
1480 goto ret;
1481 else
1483 int rv;
1485 if (vfs_path_cmp (edit->filename_vpath, exp_vpath) != 0)
1487 int file;
1489 different_filename = 1;
1490 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1492 if (file != -1)
1494 /* the file exists */
1495 mc_close (file);
1496 /* Overwrite the current file or cancel the operation */
1497 if (edit_query_dialog2
1498 (_("Warning"),
1499 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1500 goto ret;
1502 else
1504 edit->stat1.st_mode |= S_IWUSR;
1506 save_lock = lock_file (exp_vpath);
1508 else
1510 /* filenames equal, check if already locked */
1511 if (!edit->locked && !edit->delete_file)
1512 save_lock = lock_file (exp_vpath);
1515 if (different_filename)
1518 * Allow user to write into saved (under another name) file
1519 * even if original file had r/o user permissions.
1521 edit->stat1.st_mode |= S_IWRITE;
1524 rv = edit_save_file (edit, exp_vpath);
1525 switch (rv)
1527 case 1:
1528 /* Succesful, so unlock both files */
1529 if (different_filename)
1531 if (save_lock)
1532 unlock_file (exp_vpath);
1533 if (edit->locked)
1534 edit->locked = edit_unlock_file (edit);
1536 else
1538 if (edit->locked || save_lock)
1539 edit->locked = edit_unlock_file (edit);
1542 edit_set_filename (edit, exp_vpath);
1543 if (edit->lb != LB_ASIS)
1544 edit_reload (edit, exp_vpath);
1545 edit->modified = 0;
1546 edit->delete_file = 0;
1547 if (different_filename)
1548 edit_load_syntax (edit, NULL, edit->syntax_type);
1549 vfs_path_free (exp_vpath);
1550 edit->force |= REDRAW_COMPLETELY;
1551 return 1;
1552 default:
1553 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1554 /* fallthrough */
1555 case -1:
1556 /* Failed, so maintain modify (not save) lock */
1557 if (save_lock)
1558 unlock_file (exp_vpath);
1559 break;
1564 ret:
1565 vfs_path_free (exp_vpath);
1566 edit->force |= REDRAW_COMPLETELY;
1567 return 0;
1570 /* {{{ Macro stuff starts here */
1571 /* --------------------------------------------------------------------------------------------- */
1573 void
1574 edit_delete_macro_cmd (WEdit * edit)
1576 int hotkey;
1578 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1580 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1581 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1584 /* --------------------------------------------------------------------------------------------- */
1586 /** returns FALSE on error */
1587 gboolean
1588 edit_execute_macro (WEdit * edit, int hotkey)
1590 gboolean res = FALSE;
1592 if (hotkey != 0)
1594 const macros_t *macros;
1595 guint indx;
1597 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1598 macros->macro != NULL && macros->macro->len != 0)
1600 guint i;
1602 edit->force |= REDRAW_PAGE;
1604 for (i = 0; i < macros->macro->len; i++)
1606 const macro_action_t *m_act;
1608 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1609 edit_execute_cmd (edit, m_act->action, m_act->ch);
1610 res = TRUE;
1614 edit_update_screen (edit);
1615 return res;
1618 /* --------------------------------------------------------------------------------------------- */
1620 /** returns FALSE on error */
1621 gboolean
1622 edit_store_macro_cmd (WEdit * edit)
1624 int i;
1625 int hotkey;
1626 GString *marcros_string;
1627 mc_config_t *macros_config = NULL;
1628 const char *section_name = "editor";
1629 gchar *macros_fname;
1630 GArray *macros; /* current macro */
1631 int tmp_act;
1632 gboolean have_macro = FALSE;
1633 char *skeyname = NULL;
1635 hotkey = editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1636 if (hotkey == ESC_CHAR)
1637 return FALSE;
1639 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1641 /* return FALSE if try assign macro into restricted hotkeys */
1642 if (tmp_act == CK_MacroStartRecord
1643 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1644 return FALSE;
1646 edit_delete_macro (edit, hotkey);
1648 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1649 macros_config = mc_config_init (macros_fname);
1650 g_free (macros_fname);
1652 if (macros_config == NULL)
1653 return FALSE;
1655 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1657 marcros_string = g_string_sized_new (250);
1658 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1660 skeyname = lookup_key_by_code (hotkey);
1662 for (i = 0; i < macro_index; i++)
1664 macro_action_t m_act;
1665 const char *action_name;
1667 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1669 if (action_name == NULL)
1670 break;
1672 m_act.action = record_macro_buf[i].action;
1673 m_act.ch = record_macro_buf[i].ch;
1674 g_array_append_val (macros, m_act);
1675 have_macro = TRUE;
1676 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1677 (int) record_macro_buf[i].ch);
1679 if (have_macro)
1681 macros_t macro;
1682 macro.hotkey = hotkey;
1683 macro.macro = macros;
1684 g_array_append_val (macros_list, macro);
1685 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1687 else
1688 mc_config_del_key (macros_config, section_name, skeyname);
1690 g_free (skeyname);
1691 edit_macro_sort_by_hotkey ();
1693 g_string_free (marcros_string, TRUE);
1694 mc_config_save_file (macros_config, NULL);
1695 mc_config_deinit (macros_config);
1696 return TRUE;
1699 /* --------------------------------------------------------------------------------------------- */
1701 gboolean
1702 edit_repeat_macro_cmd (WEdit * edit)
1704 int i, j;
1705 char *f;
1706 long count_repeat;
1707 char *error = NULL;
1709 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL);
1710 if (f == NULL || *f == '\0')
1712 g_free (f);
1713 return FALSE;
1716 count_repeat = strtol (f, &error, 0);
1718 if (*error != '\0')
1720 g_free (f);
1721 return FALSE;
1724 g_free (f);
1726 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1727 edit->force |= REDRAW_PAGE;
1729 for (j = 0; j < count_repeat; j++)
1730 for (i = 0; i < macro_index; i++)
1731 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1732 edit_update_screen (edit);
1733 return TRUE;
1736 /* --------------------------------------------------------------------------------------------- */
1737 /** return FALSE on error */
1739 gboolean
1740 edit_load_macro_cmd (WEdit * edit)
1742 mc_config_t *macros_config = NULL;
1743 gchar **profile_keys, **keys;
1744 gchar **values, **curr_values;
1745 gsize len, values_len;
1746 const char *section_name = "editor";
1747 gchar *macros_fname;
1748 int hotkey;
1750 (void) edit;
1752 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1753 macros_config = mc_config_init (macros_fname);
1754 g_free (macros_fname);
1756 if (macros_config == NULL)
1757 return FALSE;
1759 profile_keys = keys = mc_config_get_keys (macros_config, section_name, &len);
1760 while (*profile_keys != NULL)
1762 gboolean have_macro;
1763 GArray *macros;
1764 macros_t macro;
1766 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1768 curr_values = values = mc_config_get_string_list (macros_config, section_name,
1769 *profile_keys, &values_len);
1770 hotkey = lookup_key (*profile_keys, NULL);
1771 have_macro = FALSE;
1773 while (*curr_values != NULL && *curr_values[0] != '\0')
1775 char **macro_pair = NULL;
1777 macro_pair = g_strsplit (*curr_values, ":", 2);
1779 if (macro_pair != NULL)
1781 macro_action_t m_act;
1782 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
1783 m_act.action = 0;
1784 else
1786 m_act.action = keybind_lookup_action (macro_pair[0]);
1787 g_free (macro_pair[0]);
1788 macro_pair[0] = NULL;
1790 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
1791 m_act.ch = -1;
1792 else
1794 m_act.ch = strtol (macro_pair[1], NULL, 0);
1795 g_free (macro_pair[1]);
1796 macro_pair[1] = NULL;
1798 if (m_act.action != 0)
1800 /* a shell command */
1801 if ((m_act.action / CK_PipeBlock (0)) == 1)
1803 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
1804 m_act.ch = -1;
1806 g_array_append_val (macros, m_act);
1807 have_macro = TRUE;
1809 g_strfreev (macro_pair);
1810 macro_pair = NULL;
1812 curr_values++;
1814 if (have_macro)
1816 macro.hotkey = hotkey;
1817 macro.macro = macros;
1818 g_array_append_val (macros_list, macro);
1820 profile_keys++;
1821 g_strfreev (values);
1823 g_strfreev (keys);
1824 mc_config_deinit (macros_config);
1825 edit_macro_sort_by_hotkey ();
1826 return TRUE;
1829 /* }}} Macro stuff end here */
1831 /* --------------------------------------------------------------------------------------------- */
1832 /** returns 1 on success */
1835 edit_save_confirm_cmd (WEdit * edit)
1837 gchar *f = NULL;
1839 if (edit->filename_vpath == NULL)
1840 return edit_save_as_cmd (edit);
1842 if (!edit_check_newline (edit))
1843 return 0;
1845 if (edit_confirm_save)
1847 char *filename;
1848 gboolean ok;
1850 filename = vfs_path_to_str (edit->filename_vpath);
1851 f = g_strdup_printf (_("Confirm save file: \"%s\""), filename);
1852 g_free (filename);
1853 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
1854 g_free (f);
1855 if (!ok)
1856 return 0;
1858 return edit_save_cmd (edit);
1861 /* --------------------------------------------------------------------------------------------- */
1862 /** returns 1 on success */
1865 edit_new_cmd (WEdit * edit)
1867 if (edit->modified)
1869 if (edit_query_dialog2
1870 (_("Warning"),
1872 ("Current text was modified without a file save.\nContinue discards these changes"),
1873 _("C&ontinue"), _("&Cancel")))
1875 edit->force |= REDRAW_COMPLETELY;
1876 return 0;
1879 edit->force |= REDRAW_COMPLETELY;
1881 return edit_renew (edit); /* if this gives an error, something has really screwed up */
1884 /* --------------------------------------------------------------------------------------------- */
1887 edit_load_cmd (WEdit * edit, edit_current_file_t what)
1889 if (edit->modified
1890 && (edit_query_dialog2
1891 (_("Warning"),
1892 _("Current text was modified without a file save.\n"
1893 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")) == 1))
1895 edit->force |= REDRAW_COMPLETELY;
1896 return 0;
1899 switch (what)
1901 case EDIT_FILE_COMMON:
1903 char *filename, *exp;
1905 filename = vfs_path_to_str (edit->filename_vpath);
1906 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
1907 MC_HISTORY_EDIT_LOAD, filename);
1908 g_free (filename);
1910 if (exp != NULL)
1912 if (*exp != '\0')
1914 vfs_path_t *exp_vpath;
1916 exp_vpath = vfs_path_from_str (exp);
1917 edit_load_file_from_filename (edit, exp_vpath);
1918 vfs_path_free (exp_vpath);
1920 g_free (exp);
1923 break;
1925 case EDIT_FILE_SYNTAX:
1926 edit_load_syntax_file (edit);
1927 break;
1929 case EDIT_FILE_MENU:
1930 edit_load_menu_file (edit);
1931 break;
1933 default:
1934 break;
1937 edit->force |= REDRAW_COMPLETELY;
1938 return 0;
1941 /* --------------------------------------------------------------------------------------------- */
1943 if mark2 is -1 then marking is from mark1 to the cursor.
1944 Otherwise its between the markers. This handles this.
1945 Returns 1 if no text is marked.
1949 eval_marks (WEdit * edit, long *start_mark, long *end_mark)
1951 if (edit->mark1 != edit->mark2)
1953 long start_bol, start_eol;
1954 long end_bol, end_eol;
1955 long col1, col2;
1956 long diff1, diff2;
1957 long end_mark_curs;
1959 if (edit->end_mark_curs < 0)
1960 end_mark_curs = edit->curs1;
1961 else
1962 end_mark_curs = edit->end_mark_curs;
1964 if (edit->mark2 >= 0)
1966 *start_mark = min (edit->mark1, edit->mark2);
1967 *end_mark = max (edit->mark1, edit->mark2);
1969 else
1971 *start_mark = min (edit->mark1, end_mark_curs);
1972 *end_mark = max (edit->mark1, end_mark_curs);
1973 edit->column2 = edit->curs_col + edit->over_col;
1976 if (edit->column_highlight
1977 && (((edit->mark1 > end_mark_curs) && (edit->column1 < edit->column2))
1978 || ((edit->mark1 < end_mark_curs) && (edit->column1 > edit->column2))))
1980 start_bol = edit_bol (edit, *start_mark);
1981 start_eol = edit_eol (edit, start_bol - 1) + 1;
1982 end_bol = edit_bol (edit, *end_mark);
1983 end_eol = edit_eol (edit, *end_mark);
1984 col1 = min (edit->column1, edit->column2);
1985 col2 = max (edit->column1, edit->column2);
1987 diff1 =
1988 edit_move_forward3 (edit, start_bol, col2, 0) - edit_move_forward3 (edit, start_bol,
1989 col1, 0);
1990 diff2 =
1991 edit_move_forward3 (edit, end_bol, col2, 0) - edit_move_forward3 (edit, end_bol,
1992 col1, 0);
1994 *start_mark -= diff1;
1995 *end_mark += diff2;
1996 *start_mark = max (*start_mark, start_eol);
1997 *end_mark = min (*end_mark, end_eol);
1999 return 0;
2001 else
2003 *start_mark = *end_mark = 0;
2004 edit->column2 = edit->column1 = 0;
2005 return 1;
2009 /* --------------------------------------------------------------------------------------------- */
2011 void
2012 edit_insert_over (WEdit * edit)
2014 int i;
2016 for (i = 0; i < edit->over_col; i++)
2018 edit_insert (edit, ' ');
2020 edit->over_col = 0;
2023 /* --------------------------------------------------------------------------------------------- */
2026 edit_insert_column_of_text_from_file (WEdit * edit, int file,
2027 long *start_pos, long *end_pos, int *col1, int *col2)
2029 long cursor;
2030 int col;
2031 int blocklen = -1, width = 0;
2032 unsigned char *data;
2034 cursor = edit->curs1;
2035 col = edit_get_col (edit);
2036 data = g_malloc0 (TEMP_BUF_LEN);
2038 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
2040 int i;
2041 for (width = 0; width < blocklen; width++)
2043 if (data[width] == '\n')
2044 break;
2046 for (i = 0; i < blocklen; i++)
2048 if (data[i] == '\n')
2049 { /* fill in and move to next line */
2050 int l;
2051 long p;
2052 if (edit_get_byte (edit, edit->curs1) != '\n')
2054 l = width - (edit_get_col (edit) - col);
2055 while (l > 0)
2057 edit_insert (edit, ' ');
2058 l -= space_width;
2061 for (p = edit->curs1;; p++)
2063 if (p == edit->last_byte)
2065 edit_cursor_move (edit, edit->last_byte - edit->curs1);
2066 edit_insert_ahead (edit, '\n');
2067 p++;
2068 break;
2070 if (edit_get_byte (edit, p) == '\n')
2072 p++;
2073 break;
2076 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
2077 l = col - edit_get_col (edit);
2078 while (l >= space_width)
2080 edit_insert (edit, ' ');
2081 l -= space_width;
2083 continue;
2085 edit_insert (edit, data[i]);
2088 *col1 = col;
2089 *col2 = col + width;
2090 *start_pos = cursor;
2091 *end_pos = edit->curs1;
2092 edit_cursor_move (edit, cursor - edit->curs1);
2093 g_free (data);
2095 return blocklen;
2098 /* --------------------------------------------------------------------------------------------- */
2100 void
2101 edit_block_copy_cmd (WEdit * edit)
2103 long start_mark, end_mark, current = edit->curs1;
2104 long col_delta = 0;
2105 long mark1, mark2;
2106 int c1, c2;
2107 int size;
2108 unsigned char *copy_buf;
2110 edit_update_curs_col (edit);
2111 if (eval_marks (edit, &start_mark, &end_mark))
2112 return;
2114 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2116 /* all that gets pushed are deletes hence little space is used on the stack */
2118 edit_push_markers (edit);
2120 if (edit->column_highlight)
2122 col_delta = abs (edit->column2 - edit->column1);
2123 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2125 else
2127 while (size--)
2128 edit_insert_ahead (edit, copy_buf[size]);
2131 g_free (copy_buf);
2132 edit_scroll_screen_over_cursor (edit);
2134 if (edit->column_highlight)
2135 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2136 else if (start_mark < current && end_mark > current)
2137 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2139 edit->force |= REDRAW_PAGE;
2143 /* --------------------------------------------------------------------------------------------- */
2145 void
2146 edit_block_move_cmd (WEdit * edit)
2148 long current;
2149 unsigned char *copy_buf = NULL;
2150 long start_mark, end_mark;
2151 long line;
2153 if (eval_marks (edit, &start_mark, &end_mark))
2154 return;
2156 line = edit->curs_line;
2157 if (edit->mark2 < 0)
2158 edit_mark_cmd (edit, 0);
2159 edit_push_markers (edit);
2161 if (edit->column_highlight)
2163 long mark1, mark2;
2164 int size;
2165 int b_width = 0;
2166 int c1, c2;
2167 int x, x2;
2169 c1 = min (edit->column1, edit->column2);
2170 c2 = max (edit->column1, edit->column2);
2171 b_width = (c2 - c1);
2173 edit_update_curs_col (edit);
2175 x = edit->curs_col;
2176 x2 = x + edit->over_col;
2178 /* do nothing when cursor inside first line of selected area */
2179 if ((edit_eol (edit, edit->curs1) == edit_eol (edit, start_mark)) && (x2 > c1 && x2 <= c2))
2180 return;
2182 if (edit->curs1 > start_mark && edit->curs1 < edit_eol (edit, end_mark))
2184 if (x > c2)
2185 x -= b_width;
2186 else if (x > c1 && x <= c2)
2187 x = c1;
2189 /* save current selection into buffer */
2190 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2192 /* remove current selection */
2193 edit_block_delete_cmd (edit);
2195 edit->over_col = max (0, edit->over_col - b_width);
2196 /* calculate the cursor pos after delete block */
2197 current = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), x, 0);
2198 edit_cursor_move (edit, current - edit->curs1);
2199 edit_scroll_screen_over_cursor (edit);
2201 /* add TWS if need before block insertion */
2202 if (option_cursor_beyond_eol && edit->over_col > 0)
2203 edit_insert_over (edit);
2205 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2206 edit_set_markers (edit, mark1, mark2, c1, c2);
2208 else
2210 long count;
2212 current = edit->curs1;
2213 copy_buf = g_malloc0 (end_mark - start_mark);
2214 edit_cursor_move (edit, start_mark - edit->curs1);
2215 edit_scroll_screen_over_cursor (edit);
2216 count = start_mark;
2217 while (count < end_mark)
2219 copy_buf[end_mark - count - 1] = edit_delete (edit, 1);
2220 count++;
2222 edit_scroll_screen_over_cursor (edit);
2223 edit_cursor_move (edit,
2224 current - edit->curs1 -
2225 (((current - edit->curs1) > 0) ? end_mark - start_mark : 0));
2226 edit_scroll_screen_over_cursor (edit);
2227 while (count-- > start_mark)
2228 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2229 edit_set_markers (edit, edit->curs1, edit->curs1 + end_mark - start_mark, 0, 0);
2232 edit_scroll_screen_over_cursor (edit);
2233 g_free (copy_buf);
2234 edit->force |= REDRAW_PAGE;
2237 /* --------------------------------------------------------------------------------------------- */
2238 /** returns 1 if canceelled by user */
2241 edit_block_delete_cmd (WEdit * edit)
2243 long start_mark, end_mark;
2244 if (eval_marks (edit, &start_mark, &end_mark))
2246 edit_delete_line (edit);
2247 return 0;
2249 return edit_block_delete (edit);
2252 /* --------------------------------------------------------------------------------------------- */
2253 /** call with edit = 0 before shutdown to close memory leaks */
2255 void
2256 edit_replace_cmd (WEdit * edit, int again)
2258 /* 1 = search string, 2 = replace with */
2259 static char *saved1 = NULL; /* saved default[123] */
2260 static char *saved2 = NULL;
2261 char *input1 = NULL; /* user input from the dialog */
2262 char *input2 = NULL;
2263 char *disp1 = NULL;
2264 char *disp2 = NULL;
2265 long times_replaced = 0;
2266 gboolean once_found = FALSE;
2268 if (!edit)
2270 g_free (saved1), saved1 = NULL;
2271 g_free (saved2), saved2 = NULL;
2272 return;
2275 edit->force |= REDRAW_COMPLETELY;
2277 if (again && !saved1 && !saved2)
2278 again = 0;
2280 if (again)
2282 input1 = g_strdup (saved1 ? saved1 : "");
2283 input2 = g_strdup (saved2 ? saved2 : "");
2285 else
2287 char *tmp_inp1, *tmp_inp2;
2288 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2289 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2291 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2293 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2295 g_free (disp1);
2296 g_free (disp2);
2298 if (input1 == NULL || *input1 == '\0')
2300 edit->force = REDRAW_COMPLETELY;
2301 goto cleanup;
2304 tmp_inp1 = input1;
2305 tmp_inp2 = input2;
2306 input1 = edit_replace_cmd__conv_to_input (input1);
2307 input2 = edit_replace_cmd__conv_to_input (input2);
2308 g_free (tmp_inp1);
2309 g_free (tmp_inp2);
2311 g_free (saved1), saved1 = g_strdup (input1);
2312 g_free (saved2), saved2 = g_strdup (input2);
2314 mc_search_free (edit->search);
2315 edit->search = NULL;
2318 if (!edit->search)
2320 edit->search = mc_search_new (input1, -1);
2321 if (edit->search == NULL)
2323 edit->search_start = edit->curs1;
2324 goto cleanup;
2326 edit->search->search_type = edit_search_options.type;
2327 edit->search->is_all_charsets = edit_search_options.all_codepages;
2328 edit->search->is_case_sensitive = edit_search_options.case_sens;
2329 edit->search->whole_words = edit_search_options.whole_words;
2330 edit->search->search_fn = edit_search_cmd_callback;
2333 if (edit->found_len && edit->search_start == edit->found_start + 1
2334 && edit_search_options.backwards)
2335 edit->search_start--;
2337 if (edit->found_len && edit->search_start == edit->found_start - 1
2338 && !edit_search_options.backwards)
2339 edit->search_start++;
2343 gsize len = 0;
2345 if (!editcmd_find (edit, &len))
2347 if (!(edit->search->error == MC_SEARCH_E_OK ||
2348 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2350 edit_error_dialog (_("Search"), edit->search->error_str);
2352 break;
2354 once_found = TRUE;
2356 edit->search_start = edit->search->normal_offset;
2357 /*returns negative on not found or error in pattern */
2359 if ((edit->search_start >= 0) && (edit->search_start < edit->last_byte))
2361 gsize i;
2362 GString *tmp_str, *repl_str;
2364 edit->found_start = edit->search_start;
2365 i = edit->found_len = len;
2367 edit_cursor_move (edit, edit->search_start - edit->curs1);
2368 edit_scroll_screen_over_cursor (edit);
2370 if (edit->replace_mode == 0)
2372 int l;
2373 int prompt;
2375 l = edit->curs_row - edit->widget.lines / 3;
2376 if (l > 0)
2377 edit_scroll_downward (edit, l);
2378 if (l < 0)
2379 edit_scroll_upward (edit, -l);
2381 edit_scroll_screen_over_cursor (edit);
2382 edit->force |= REDRAW_PAGE;
2383 edit_render_keypress (edit);
2385 /*so that undo stops at each query */
2386 edit_push_key_press (edit);
2387 /* and prompt 2/3 down */
2388 disp1 = edit_replace_cmd__conv_to_display (saved1);
2389 disp2 = edit_replace_cmd__conv_to_display (saved2);
2390 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2391 g_free (disp1);
2392 g_free (disp2);
2394 if (prompt == B_REPLACE_ALL)
2395 edit->replace_mode = 1;
2396 else if (prompt == B_SKIP_REPLACE)
2398 if (edit_search_options.backwards)
2399 edit->search_start--;
2400 else
2401 edit->search_start++;
2402 continue; /* loop */
2404 else if (prompt == B_CANCEL)
2406 edit->replace_mode = -1;
2407 break; /* loop */
2411 /* don't process string each time */
2412 tmp_str = g_string_new (input2);
2413 repl_str = mc_search_prepare_replace_str (edit->search, tmp_str);
2414 g_string_free (tmp_str, TRUE);
2416 if (edit->search->error != MC_SEARCH_E_OK)
2418 edit_error_dialog (_("Replace"), edit->search->error_str);
2419 g_string_free (repl_str, TRUE);
2420 break;
2423 /* delete then insert new */
2424 for (i = 0; i < len; i++)
2425 edit_delete (edit, 1);
2427 for (i = 0; i < repl_str->len; i++)
2428 edit_insert (edit, repl_str->str[i]);
2430 edit->found_len = repl_str->len;
2431 g_string_free (repl_str, TRUE);
2432 times_replaced++;
2434 /* so that we don't find the same string again */
2435 if (edit_search_options.backwards)
2436 edit->search_start--;
2437 else
2439 edit->search_start += edit->found_len;
2441 if (edit->search_start >= edit->last_byte)
2442 break;
2445 edit_scroll_screen_over_cursor (edit);
2447 else
2449 /* try and find from right here for next search */
2450 edit->search_start = edit->curs1;
2451 edit_update_curs_col (edit);
2453 edit->force |= REDRAW_PAGE;
2454 edit_render_keypress (edit);
2456 if (times_replaced == 0)
2457 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2458 break;
2461 while (edit->replace_mode >= 0);
2463 edit_scroll_screen_over_cursor (edit);
2464 edit->force |= REDRAW_COMPLETELY;
2465 edit_render_keypress (edit);
2467 if ((edit->replace_mode == 1) && (times_replaced != 0))
2468 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2470 cleanup:
2471 g_free (input1);
2472 g_free (input2);
2475 /* --------------------------------------------------------------------------------------------- */
2478 edit_search_cmd_callback (const void *user_data, gsize char_offset)
2480 return edit_get_byte ((WEdit *) user_data, (long) char_offset);
2483 /* --------------------------------------------------------------------------------------------- */
2485 void
2486 edit_search_cmd (WEdit * edit, gboolean again)
2488 if (edit == NULL)
2489 return;
2491 if (!again)
2492 edit_search (edit);
2493 else if (edit->last_search_string != NULL)
2494 edit_do_search (edit);
2495 else
2497 /* find last search string in history */
2498 GList *history;
2500 history = history_get (MC_HISTORY_SHARED_SEARCH);
2501 if (history != NULL && history->data != NULL)
2503 edit->last_search_string = (char *) history->data;
2504 history->data = NULL;
2505 history = g_list_first (history);
2506 g_list_foreach (history, (GFunc) g_free, NULL);
2507 g_list_free (history);
2509 edit->search = mc_search_new (edit->last_search_string, -1);
2510 if (edit->search == NULL)
2512 /* if not... then ask for an expression */
2513 g_free (edit->last_search_string);
2514 edit->last_search_string = NULL;
2515 edit_search (edit);
2517 else
2519 edit->search->search_type = edit_search_options.type;
2520 edit->search->is_all_charsets = edit_search_options.all_codepages;
2521 edit->search->is_case_sensitive = edit_search_options.case_sens;
2522 edit->search->whole_words = edit_search_options.whole_words;
2523 edit->search->search_fn = edit_search_cmd_callback;
2524 edit_do_search (edit);
2527 else
2529 /* if not... then ask for an expression */
2530 g_free (edit->last_search_string);
2531 edit->last_search_string = NULL;
2532 edit_search (edit);
2538 /* --------------------------------------------------------------------------------------------- */
2540 * Check if it's OK to close the editor. If there are unsaved changes,
2541 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2544 gboolean
2545 edit_ok_to_exit (WEdit * edit)
2547 int act;
2549 if (!edit->modified)
2550 return TRUE;
2552 if (!mc_global.widget.midnight_shutdown)
2554 if (!edit_check_newline (edit))
2555 return FALSE;
2557 query_set_sel (2);
2558 act = edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2559 _("&Yes"), _("&No"), _("&Cancel quit"));
2561 else
2563 act =
2564 edit_query_dialog2 (_("Quit"),
2565 _("Midnight Commander is being shut down.\nSave modified file?"),
2566 _("&Yes"), _("&No"));
2568 /* Esc is No */
2569 if (act == -1)
2570 act = 1;
2573 switch (act)
2575 case 0: /* Yes */
2576 edit_push_markers (edit);
2577 edit_set_markers (edit, 0, 0, 0, 0);
2578 if (!edit_save_cmd (edit) || mc_global.widget.midnight_shutdown)
2579 return mc_global.widget.midnight_shutdown;
2580 break;
2581 case 1: /* No */
2582 break;
2583 case 2: /* Cancel quit */
2584 case -1: /* Esc */
2585 return FALSE;
2588 return TRUE;
2591 /* --------------------------------------------------------------------------------------------- */
2592 /** save block, returns 1 on success */
2595 edit_save_block (WEdit * edit, const char *filename, long start, long finish)
2597 int len, file;
2598 vfs_path_t *vpath;
2600 vpath = vfs_path_from_str (filename);
2601 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2602 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2603 vfs_path_free (vpath);
2604 if (file == -1)
2605 return 0;
2607 if (edit->column_highlight)
2609 int r;
2611 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2612 if (r > 0)
2614 unsigned char *block, *p;
2616 p = block = edit_get_block (edit, start, finish, &len);
2617 while (len)
2619 r = mc_write (file, p, len);
2620 if (r < 0)
2621 break;
2622 p += r;
2623 len -= r;
2625 g_free (block);
2628 else
2630 unsigned char *buf;
2631 int i = start, end;
2633 len = finish - start;
2634 buf = g_malloc0 (TEMP_BUF_LEN);
2635 while (start != finish)
2637 end = min (finish, start + TEMP_BUF_LEN);
2638 for (; i < end; i++)
2639 buf[i - start] = edit_get_byte (edit, i);
2640 len -= mc_write (file, (char *) buf, end - start);
2641 start = end;
2643 g_free (buf);
2645 mc_close (file);
2646 if (len)
2647 return 0;
2648 return 1;
2651 /* --------------------------------------------------------------------------------------------- */
2653 void
2654 edit_paste_from_history (WEdit * edit)
2656 (void) edit;
2657 edit_error_dialog (_("Error"), _("This function is not implemented"));
2660 /* --------------------------------------------------------------------------------------------- */
2663 edit_copy_to_X_buf_cmd (WEdit * edit)
2665 long start_mark, end_mark;
2666 if (eval_marks (edit, &start_mark, &end_mark))
2667 return 0;
2668 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2670 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2671 return 1;
2673 /* try use external clipboard utility */
2674 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2676 return 0;
2679 /* --------------------------------------------------------------------------------------------- */
2682 edit_cut_to_X_buf_cmd (WEdit * edit)
2684 long start_mark, end_mark;
2685 if (eval_marks (edit, &start_mark, &end_mark))
2686 return 0;
2687 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2689 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2690 return 1;
2692 /* try use external clipboard utility */
2693 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2695 edit_block_delete_cmd (edit);
2696 edit_mark_cmd (edit, 1);
2697 return 0;
2700 /* --------------------------------------------------------------------------------------------- */
2702 void
2703 edit_paste_from_X_buf_cmd (WEdit * edit)
2705 vfs_path_t *tmp;
2707 /* try use external clipboard utility */
2708 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
2709 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
2710 edit_insert_file (edit, tmp);
2711 vfs_path_free (tmp);
2715 /* --------------------------------------------------------------------------------------------- */
2717 * Ask user for the line and go to that line.
2718 * Negative numbers mean line from the end (i.e. -1 is the last line).
2721 void
2722 edit_goto_cmd (WEdit * edit)
2724 char *f;
2725 static long line = 0; /* line as typed, saved as default */
2726 long l;
2727 char *error;
2728 char s[32];
2730 g_snprintf (s, sizeof (s), "%ld", line);
2731 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "");
2732 if (!f)
2733 return;
2735 if (!*f)
2737 g_free (f);
2738 return;
2741 l = strtol (f, &error, 0);
2742 if (*error)
2744 g_free (f);
2745 return;
2748 line = l;
2749 if (l < 0)
2750 l = edit->total_lines + l + 2;
2751 edit_move_display (edit, l - edit->widget.lines / 2 - 1);
2752 edit_move_to_line (edit, l - 1);
2753 edit->force |= REDRAW_COMPLETELY;
2754 g_free (f);
2758 /* --------------------------------------------------------------------------------------------- */
2759 /** Return 1 on success */
2762 edit_save_block_cmd (WEdit * edit)
2764 long start_mark, end_mark;
2765 char *exp, *tmp;
2767 if (eval_marks (edit, &start_mark, &end_mark))
2768 return 1;
2770 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
2771 exp =
2772 input_expand_dialog (_("Save block"), _("Enter file name:"),
2773 MC_HISTORY_EDIT_SAVE_BLOCK, tmp);
2774 g_free (tmp);
2775 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2776 if (exp)
2778 if (!*exp)
2780 g_free (exp);
2781 return 0;
2783 else
2785 if (edit_save_block (edit, exp, start_mark, end_mark))
2787 g_free (exp);
2788 edit->force |= REDRAW_COMPLETELY;
2789 return 1;
2791 else
2793 g_free (exp);
2794 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
2798 edit->force |= REDRAW_COMPLETELY;
2799 return 0;
2803 /* --------------------------------------------------------------------------------------------- */
2804 /** returns TRUE on success */
2806 gboolean
2807 edit_insert_file_cmd (WEdit * edit)
2809 char *tmp;
2810 char *exp;
2811 gboolean ret = FALSE;
2813 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
2814 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
2815 MC_HISTORY_EDIT_INSERT_FILE, tmp);
2816 g_free (tmp);
2818 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2820 if (exp != NULL && *exp != '\0')
2822 vfs_path_t *exp_vpath;
2824 exp_vpath = vfs_path_from_str (exp);
2825 ret = (edit_insert_file (edit, exp_vpath) >= 0);
2826 vfs_path_free (exp_vpath);
2828 if (!ret)
2829 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
2832 g_free (exp);
2834 edit->force |= REDRAW_COMPLETELY;
2835 return ret;
2838 /* --------------------------------------------------------------------------------------------- */
2839 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
2842 edit_sort_cmd (WEdit * edit)
2844 static char *old = 0;
2845 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
2846 long start_mark, end_mark;
2847 int e;
2849 if (eval_marks (edit, &start_mark, &end_mark))
2851 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
2852 return 0;
2855 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
2856 edit_save_block (edit, tmp, start_mark, end_mark);
2857 g_free (tmp);
2859 exp = input_dialog (_("Run sort"),
2860 _("Enter sort options (see manpage) separated by whitespace:"),
2861 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "");
2863 if (!exp)
2864 return 1;
2865 g_free (old);
2866 old = exp;
2867 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
2868 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
2869 tmp =
2870 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
2871 " > ", tmp_edit_temp_name, (char *) NULL);
2872 g_free (tmp_edit_temp_name);
2873 g_free (tmp_edit_block_name);
2875 e = system (tmp);
2876 g_free (tmp);
2877 if (e)
2879 if (e == -1 || e == 127)
2881 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
2883 else
2885 char q[8];
2886 sprintf (q, "%d ", e);
2887 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
2888 edit_error_dialog (_("Sort"), tmp);
2889 g_free (tmp);
2891 return -1;
2894 edit->force |= REDRAW_COMPLETELY;
2896 if (edit_block_delete_cmd (edit))
2897 return 1;
2900 vfs_path_t *tmp_vpath;
2902 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
2903 edit_insert_file (edit, tmp_vpath);
2904 vfs_path_free (tmp_vpath);
2906 return 0;
2909 /* --------------------------------------------------------------------------------------------- */
2911 * Ask user for a command, execute it and paste its output back to the
2912 * editor.
2916 edit_ext_cmd (WEdit * edit)
2918 char *exp, *tmp, *tmp_edit_temp_file;
2919 int e;
2921 exp =
2922 input_dialog (_("Paste output of external command"),
2923 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL);
2925 if (!exp)
2926 return 1;
2928 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
2929 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
2930 g_free (tmp_edit_temp_file);
2931 e = system (tmp);
2932 g_free (tmp);
2933 g_free (exp);
2935 if (e)
2937 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
2938 return -1;
2941 edit->force |= REDRAW_COMPLETELY;
2944 vfs_path_t *tmp_vpath;
2946 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
2947 edit_insert_file (edit, tmp_vpath);
2948 vfs_path_free (tmp_vpath);
2950 return 0;
2953 /* --------------------------------------------------------------------------------------------- */
2954 /** if block is 1, a block must be highlighted and the shell command
2955 processes it. If block is 0 the shell command is a straight system
2956 command, that just produces some output which is to be inserted */
2958 void
2959 edit_block_process_cmd (WEdit * edit, int macro_number)
2961 char *fname;
2962 char *macros_fname = NULL;
2964 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
2965 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
2966 user_menu (edit, macros_fname, 0);
2967 g_free (fname);
2968 g_free (macros_fname);
2969 edit->force |= REDRAW_COMPLETELY;
2972 /* --------------------------------------------------------------------------------------------- */
2974 void
2975 edit_mail_dialog (WEdit * edit)
2977 char *tmail_to;
2978 char *tmail_subject;
2979 char *tmail_cc;
2981 static char *mail_cc_last = 0;
2982 static char *mail_subject_last = 0;
2983 static char *mail_to_last = 0;
2985 QuickWidget quick_widgets[] = {
2986 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
2987 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
2988 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input", &tmail_cc),
2989 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT, N_("Copies to")),
2990 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-2",
2991 &tmail_subject),
2992 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT, N_("Subject")),
2993 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-3", &tmail_to),
2994 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT, N_("To")),
2995 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT, N_("mail -s <subject> -c <cc> <to>")),
2996 QUICK_END
2999 QuickDialog Quick_input = {
3000 50, MAIL_DLG_HEIGHT, -1, -1, N_("Mail"),
3001 "[Input Line Keys]", quick_widgets, NULL, FALSE
3004 quick_widgets[2].u.input.text = mail_cc_last ? mail_cc_last : "";
3005 quick_widgets[4].u.input.text = mail_subject_last ? mail_subject_last : "";
3006 quick_widgets[6].u.input.text = mail_to_last ? mail_to_last : "";
3008 if (quick_dialog (&Quick_input) != B_CANCEL)
3010 g_free (mail_cc_last);
3011 g_free (mail_subject_last);
3012 g_free (mail_to_last);
3013 mail_cc_last = tmail_cc;
3014 mail_subject_last = tmail_subject;
3015 mail_to_last = tmail_to;
3016 pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
3021 /*******************/
3022 /* Word Completion */
3023 /*******************/
3025 /* --------------------------------------------------------------------------------------------- */
3027 * Complete current word using regular expression search
3028 * backwards beginning at the current cursor position.
3031 void
3032 edit_complete_word_cmd (WEdit * edit)
3034 gsize i, max_len, word_len = 0, num_compl = 0;
3035 long word_start = 0;
3036 unsigned char *bufpos;
3037 char *match_expr;
3038 struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
3040 /* search start of word to be completed */
3041 if (!edit_find_word_start (edit, &word_start, &word_len))
3042 return;
3044 /* prepare match expression */
3045 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3047 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3048 match_expr =
3049 g_strdup_printf
3050 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3051 (int) word_len, bufpos);
3053 /* collect the possible completions */
3054 /* start search from begin to end of file */
3055 max_len =
3056 edit_collect_completions (edit, word_start, word_len, match_expr,
3057 (struct selection *) &compl, &num_compl);
3059 if (num_compl > 0)
3061 /* insert completed word if there is only one match */
3062 if (num_compl == 1)
3064 for (i = word_len; i < compl[0].len; i++)
3065 edit_insert (edit, *(compl[0].text + i));
3067 /* more than one possible completion => ask the user */
3068 else
3070 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3071 /* !!! pressed again the selection dialog pops up, but that !!! */
3072 /* !!! seems to require a further internal state !!! */
3073 /*tty_beep (); */
3075 /* let the user select the preferred completion */
3076 editcmd_dialog_completion_show (edit, max_len, word_len,
3077 (struct selection *) &compl, num_compl);
3081 g_free (match_expr);
3082 /* release memory before return */
3083 for (i = 0; i < num_compl; i++)
3084 g_free (compl[i].text);
3087 /* --------------------------------------------------------------------------------------------- */
3089 void
3090 edit_select_codepage_cmd (WEdit * edit)
3092 #ifdef HAVE_CHARSET
3093 if (do_select_codepage ())
3094 edit_set_codeset (edit);
3096 edit->force = REDRAW_COMPLETELY;
3097 edit_refresh_cmd (edit);
3098 #else
3099 (void) edit;
3100 #endif
3103 /* --------------------------------------------------------------------------------------------- */
3105 void
3106 edit_insert_literal_cmd (WEdit * edit)
3108 int char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3109 _("Press any key:"), 0);
3110 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3113 /* --------------------------------------------------------------------------------------------- */
3115 void
3116 edit_begin_end_macro_cmd (WEdit * edit)
3118 /* edit is a pointer to the widget */
3119 if (edit != NULL)
3121 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3122 edit_execute_key_command (edit, command, -1);
3126 /* --------------------------------------------------------------------------------------------- */
3128 void
3129 edit_begin_end_repeat_cmd (WEdit * edit)
3131 /* edit is a pointer to the widget */
3132 if (edit != NULL)
3134 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3135 edit_execute_key_command (edit, command, -1);
3139 /* --------------------------------------------------------------------------------------------- */
3142 edit_load_forward_cmd (WEdit * edit)
3144 if (edit->modified)
3146 if (edit_query_dialog2
3147 (_("Warning"),
3148 _("Current text was modified without a file save\n"
3149 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3151 edit->force |= REDRAW_COMPLETELY;
3152 return 0;
3155 if (edit_stack_iterator + 1 < MAX_HISTORY_MOVETO)
3157 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3159 return 1;
3161 edit_stack_iterator++;
3162 if (edit_history_moveto[edit_stack_iterator].filename_vpath)
3164 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3165 edit_history_moveto[edit_stack_iterator].line);
3166 return 0;
3168 else
3170 return 1;
3173 else
3175 return 1;
3179 /* --------------------------------------------------------------------------------------------- */
3182 edit_load_back_cmd (WEdit * edit)
3184 if (edit->modified)
3186 if (edit_query_dialog2
3187 (_("Warning"),
3188 _("Current text was modified without a file save\n"
3189 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3191 edit->force |= REDRAW_COMPLETELY;
3192 return 0;
3195 if (edit_stack_iterator > 0)
3197 edit_stack_iterator--;
3198 if (edit_history_moveto[edit_stack_iterator].filename_vpath)
3200 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3201 edit_history_moveto[edit_stack_iterator].line);
3202 return 0;
3204 else
3206 return 1;
3209 else
3211 return 1;
3215 /* --------------------------------------------------------------------------------------------- */
3217 void
3218 edit_get_match_keyword_cmd (WEdit * edit)
3220 gsize word_len = 0, max_len = 0;
3221 int num_def = 0;
3222 int i;
3223 long word_start = 0;
3224 unsigned char *bufpos;
3225 char *match_expr;
3226 char *path = NULL;
3227 char *ptr = NULL;
3228 char *tagfile = NULL;
3230 etags_hash_t def_hash[MAX_DEFINITIONS];
3232 for (i = 0; i < MAX_DEFINITIONS; i++)
3234 def_hash[i].filename = NULL;
3237 /* search start of word to be completed */
3238 if (!edit_find_word_start (edit, &word_start, &word_len))
3239 return;
3241 /* prepare match expression */
3242 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3243 match_expr = g_strdup_printf ("%.*s", (int) word_len, bufpos);
3245 ptr = g_get_current_dir ();
3246 path = g_strconcat (ptr, G_DIR_SEPARATOR_S, (char *) NULL);
3247 g_free (ptr);
3249 /* Recursive search file 'TAGS' in parent dirs */
3252 ptr = g_path_get_dirname (path);
3253 g_free (path);
3254 path = ptr;
3255 g_free (tagfile);
3256 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3257 if (exist_file (tagfile))
3258 break;
3260 while (strcmp (path, G_DIR_SEPARATOR_S) != 0);
3262 if (tagfile)
3264 num_def =
3265 etags_set_definition_hash (tagfile, path, match_expr, (etags_hash_t *) & def_hash);
3266 g_free (tagfile);
3268 g_free (path);
3270 max_len = MAX_WIDTH_DEF_DIALOG;
3271 word_len = 0;
3272 if (num_def > 0)
3274 editcmd_dialog_select_definition_show (edit, match_expr, max_len, word_len,
3275 (etags_hash_t *) & def_hash, num_def);
3277 g_free (match_expr);
3280 /* --------------------------------------------------------------------------------------------- */