Editor: avoid extra screen redraw.
[midnight-commander.git] / src / editor / editcmd.c
blobccbeb01a3ef6d07c53055e1fe4144fb1ca6ac50d
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 #ifdef HAVE_ASSERT_H
38 #include <assert.h>
39 #endif
40 #include <ctype.h>
42 #include <stdio.h>
43 #include <stdarg.h>
44 #include <sys/types.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include <errno.h>
48 #include <sys/stat.h>
49 #include <stdlib.h>
50 #include <fcntl.h>
52 #include "lib/global.h"
53 #include "lib/tty/tty.h"
54 #include "lib/tty/key.h" /* XCTRL */
55 #include "lib/mcconfig.h"
56 #include "lib/skin.h"
57 #include "lib/strutil.h" /* utf string functions */
58 #include "lib/lock.h"
59 #include "lib/util.h" /* tilde_expand() */
60 #include "lib/vfs/vfs.h"
61 #include "lib/widget.h"
62 #include "lib/charsets.h"
63 #include "lib/event.h" /* mc_event_raise() */
65 #include "src/history.h"
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/main.h" /* mactos_t */
68 #include "src/selcodepage.h"
69 #include "src/keybind-defaults.h"
70 #include "src/util.h" /* check_for_default() */
71 #include "src/filemanager/layout.h" /* mc_refresh() */
73 #include "edit-impl.h"
74 #include "edit-widget.h"
75 #include "editcmd_dialogs.h"
76 #include "etags.h"
78 /*** global variables ****************************************************************************/
80 /* search and replace: */
81 int search_create_bookmark = FALSE;
83 /* queries on a save */
84 int edit_confirm_save = 1;
86 /*** file scope macro definitions ****************************************************************/
88 #define space_width 1
90 #define TEMP_BUF_LEN 1024
92 #define INPUT_INDEX 9
94 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
95 (and the above) routines to work properly - paul */
97 #define is_digit(x) ((x) >= '0' && (x) <= '9')
99 #define MAIL_DLG_HEIGHT 12
101 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
103 /*** file scope type declarations ****************************************************************/
105 /*** file scope variables ************************************************************************/
107 /*** file scope functions ************************************************************************/
108 /* --------------------------------------------------------------------------------------------- */
110 /* If 0 (quick save) then a) create/truncate <filename> file,
111 b) save to <filename>;
112 if 1 (safe save) then a) save to <tempnam>,
113 b) rename <tempnam> to <filename>;
114 if 2 (do backups) then a) save to <tempnam>,
115 b) rename <filename> to <filename.backup_ext>,
116 c) rename <tempnam> to <filename>. */
118 /* returns 0 on error, -1 on abort */
120 static int
121 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
123 char *p;
124 gchar *tmp;
125 long filelen = 0;
126 int this_save_mode, fd = -1;
127 vfs_path_t *real_filename_vpath;
128 vfs_path_t *savename_vpath = NULL;
129 const char *start_filename;
130 const vfs_path_element_t *vpath_element;
132 vpath_element = vfs_path_get_by_index (filename_vpath, 0);
133 if (vpath_element == NULL)
134 return 0;
136 start_filename = vpath_element->path;
137 if (*start_filename == '\0')
138 return 0;
140 if (*start_filename != PATH_SEP && edit->dir_vpath != NULL)
142 real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
144 else
146 real_filename_vpath = vfs_path_clone (filename_vpath);
149 this_save_mode = option_save_mode;
150 if (this_save_mode != EDIT_QUICK_SAVE)
152 if (!vfs_file_is_local (real_filename_vpath)
153 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
156 * The file does not exists yet, so no safe save or
157 * backup are necessary.
159 this_save_mode = EDIT_QUICK_SAVE;
161 if (fd != -1)
162 mc_close (fd);
165 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt)
167 int rv;
168 struct stat sb;
170 rv = mc_stat (real_filename_vpath, &sb);
171 if (rv == 0 && sb.st_nlink > 1)
173 rv = edit_query_dialog3 (_("Warning"),
174 _("File has hard-links. Detach before saving?"),
175 _("&Yes"), _("&No"), _("&Cancel"));
176 switch (rv)
178 case 0:
179 this_save_mode = EDIT_SAFE_SAVE;
180 /* fallthrough */
181 case 1:
182 edit->skip_detach_prompt = 1;
183 break;
184 default:
185 vfs_path_free (real_filename_vpath);
186 return -1;
190 /* Prevent overwriting changes from other editor sessions. */
191 if (rv == 0 && edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
193 /* The default action is "Cancel". */
194 query_set_sel (1);
196 rv = edit_query_dialog2 (_("Warning"),
197 _("The file has been modified in the meantime. Save anyway?"),
198 _("&Yes"), _("&Cancel"));
199 if (rv != 0)
201 vfs_path_free (real_filename_vpath);
202 return -1;
207 if (this_save_mode != EDIT_QUICK_SAVE)
209 char *savedir, *saveprefix;
211 savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
212 if (savedir == NULL)
213 savedir = g_strdup (".");
215 saveprefix = mc_build_filename (savedir, "cooledit", NULL);
216 g_free (savedir);
217 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
218 g_free (saveprefix);
219 if (savename_vpath == NULL)
221 vfs_path_free (real_filename_vpath);
222 return 0;
224 /* FIXME:
225 * Close for now because mc_mkstemps use pure open system call
226 * to create temporary file and it needs to be reopened by
227 * VFS-aware mc_open().
229 close (fd);
231 else
232 savename_vpath = vfs_path_clone (real_filename_vpath);
234 (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
235 (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
237 fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
238 if (fd == -1)
239 goto error_save;
241 /* pipe save */
242 p = edit_get_write_filter (savename_vpath, real_filename_vpath);
243 if (p != NULL)
245 FILE *file;
247 mc_close (fd);
248 file = (FILE *) popen (p, "w");
250 if (file)
252 filelen = edit_write_stream (edit, file);
253 #if 1
254 pclose (file);
255 #else
256 if (pclose (file) != 0)
258 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
259 edit_error_dialog (_("Error"), tmp);
260 g_free (tmp);
261 g_free (p);
262 goto error_save;
264 #endif
266 else
268 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
269 edit_error_dialog (_("Error"), get_sys_error (tmp));
270 g_free (p);
271 g_free (tmp);
272 goto error_save;
274 g_free (p);
276 else if (edit->lb == LB_ASIS)
277 { /* do not change line breaks */
278 long buf;
279 buf = 0;
280 filelen = edit->last_byte;
281 while (buf <= (edit->curs1 >> S_EDIT_BUF_SIZE) - 1)
283 if (mc_write (fd, (char *) edit->buffers1[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
285 mc_close (fd);
286 goto error_save;
288 buf++;
290 if (mc_write
291 (fd, (char *) edit->buffers1[buf],
292 edit->curs1 & M_EDIT_BUF_SIZE) != (edit->curs1 & M_EDIT_BUF_SIZE))
294 filelen = -1;
296 else if (edit->curs2)
298 edit->curs2--;
299 buf = (edit->curs2 >> S_EDIT_BUF_SIZE);
300 if (mc_write
301 (fd,
302 (char *) edit->buffers2[buf] + EDIT_BUF_SIZE -
303 (edit->curs2 & M_EDIT_BUF_SIZE) - 1,
304 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) != 1 + (edit->curs2 & M_EDIT_BUF_SIZE))
306 filelen = -1;
308 else
310 while (--buf >= 0)
312 if (mc_write (fd, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
314 filelen = -1;
315 break;
319 edit->curs2++;
321 if (mc_close (fd))
322 goto error_save;
324 /* Update the file information, especially the mtime. */
325 if (mc_stat (savename_vpath, &edit->stat1) == -1)
326 goto error_save;
328 else
329 { /* change line breaks */
330 FILE *file;
331 const vfs_path_element_t *path_element;
333 mc_close (fd);
335 path_element = vfs_path_get_by_index (savename_vpath, -1);
336 file = (FILE *) fopen (path_element->path, "w");
337 if (file != NULL)
339 filelen = edit_write_stream (edit, file);
340 fclose (file);
342 else
344 char *msg;
346 msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
347 edit_error_dialog (_("Error"), msg);
348 g_free (msg);
349 goto error_save;
353 if (filelen != edit->last_byte)
354 goto error_save;
356 if (this_save_mode == EDIT_DO_BACKUP)
358 vfs_path_t *tmp_vpath;
359 gboolean ok;
361 #ifdef HAVE_ASSERT_H
362 assert (option_backup_ext != NULL);
363 #endif
364 tmp_vpath = vfs_path_append_new (real_filename_vpath, option_backup_ext, (char *) NULL);
365 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
366 vfs_path_free (tmp_vpath);
367 if (!ok)
368 goto error_save;
371 if (this_save_mode != EDIT_QUICK_SAVE)
372 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
373 goto error_save;
375 vfs_path_free (real_filename_vpath);
376 vfs_path_free (savename_vpath);
377 return 1;
378 error_save:
379 /* FIXME: Is this safe ?
380 * if (this_save_mode != EDIT_QUICK_SAVE)
381 * mc_unlink (savename);
383 vfs_path_free (real_filename_vpath);
384 vfs_path_free (savename_vpath);
385 return 0;
388 /* --------------------------------------------------------------------------------------------- */
390 static gboolean
391 edit_check_newline (WEdit * edit)
393 return !(option_check_nl_at_eof && edit->last_byte > 0
394 && edit_get_byte (edit, edit->last_byte - 1) != '\n'
395 && edit_query_dialog2 (_("Warning"),
396 _("The file you are saving is not finished with a newline"),
397 _("C&ontinue"), _("&Cancel")));
400 /* --------------------------------------------------------------------------------------------- */
402 static vfs_path_t *
403 edit_get_save_file_as (WEdit * edit)
405 #define DLG_WIDTH 64
406 #define DLG_HEIGHT 14
408 static LineBreaks cur_lb = LB_ASIS;
410 char *filename = vfs_path_to_str (edit->filename_vpath);
411 char *filename_res;
413 const char *lb_names[LB_NAMES] = {
414 N_("&Do not change"),
415 N_("&Unix format (LF)"),
416 N_("&Windows/DOS format (CR LF)"),
417 N_("&Macintosh format (CR)")
420 QuickWidget quick_widgets[] = {
421 QUICK_BUTTON (6, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
422 QUICK_BUTTON (2, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
423 QUICK_RADIO (5, DLG_WIDTH, DLG_HEIGHT - 8, DLG_HEIGHT, LB_NAMES, lb_names, (int *) &cur_lb),
424 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 9, DLG_HEIGHT, N_("Change line breaks to:")),
425 QUICK_INPUT (3, DLG_WIDTH, DLG_HEIGHT - 11, DLG_HEIGHT, filename, DLG_WIDTH - 6, 0,
426 "save-as", &filename_res),
427 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 12, DLG_HEIGHT, N_("Enter file name:")),
428 QUICK_END
431 QuickDialog Quick_options = {
432 DLG_WIDTH, DLG_HEIGHT, -1, -1,
433 N_("Save As"), "[Save File As]",
434 quick_widgets, NULL, FALSE
437 if (quick_dialog (&Quick_options) != B_CANCEL)
439 char *fname;
440 vfs_path_t *ret_vpath;
442 edit->lb = cur_lb;
443 fname = tilde_expand (filename_res);
444 g_free (filename_res);
445 ret_vpath = vfs_path_from_str (fname);
446 g_free (fname);
447 return ret_vpath;
449 g_free (filename);
451 return NULL;
453 #undef DLG_WIDTH
454 #undef DLG_HEIGHT
457 /* --------------------------------------------------------------------------------------------- */
459 /** returns 1 on success */
461 static int
462 edit_save_cmd (WEdit * edit)
464 int res, save_lock = 0;
466 if (!edit->locked && !edit->delete_file)
467 save_lock = lock_file (edit->filename_vpath);
468 res = edit_save_file (edit, edit->filename_vpath);
470 /* Maintain modify (not save) lock on failure */
471 if ((res > 0 && edit->locked) || save_lock)
472 edit->locked = unlock_file (edit->filename_vpath);
474 /* On failure try 'save as', it does locking on its own */
475 if (!res)
476 return edit_save_as_cmd (edit);
477 edit->force |= REDRAW_COMPLETELY;
478 if (res > 0)
480 edit->delete_file = 0;
481 edit->modified = 0;
484 return 1;
487 /* --------------------------------------------------------------------------------------------- */
488 /** returns 1 on error */
490 static int
491 edit_load_file_from_filename (WEdit * edit, const vfs_path_t * exp_vpath)
493 int prev_locked = edit->locked;
494 vfs_path_t *prev_filename;
496 prev_filename = vfs_path_clone (edit->filename_vpath);
497 if (!edit_reload (edit, exp_vpath))
499 vfs_path_free (prev_filename);
500 return 1;
503 if (prev_locked)
505 vfs_path_t *fullpath;
507 fullpath = vfs_path_append_vpath_new (edit->dir_vpath, prev_filename, (char *) NULL);
508 unlock_file (fullpath);
509 vfs_path_free (fullpath);
511 vfs_path_free (prev_filename);
512 return 0;
515 /* --------------------------------------------------------------------------------------------- */
517 static void
518 edit_load_syntax_file (WEdit * edit)
520 vfs_path_t *extdir_vpath;
521 int dir = 0;
523 if (geteuid () == 0)
525 dir = query_dialog (_("Syntax file edit"),
526 _("Which syntax file you want to edit?"), D_NORMAL, 2,
527 _("&User"), _("&System Wide"));
530 extdir_vpath =
531 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
532 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
534 vfs_path_free (extdir_vpath);
535 extdir_vpath =
536 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
539 if (dir == 0)
541 vfs_path_t *user_syntax_file_vpath;
543 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
544 check_for_default (extdir_vpath, user_syntax_file_vpath);
545 edit_load_file_from_filename (edit, user_syntax_file_vpath);
546 vfs_path_free (user_syntax_file_vpath);
548 else if (dir == 1)
549 edit_load_file_from_filename (edit, extdir_vpath);
551 vfs_path_free (extdir_vpath);
554 /* --------------------------------------------------------------------------------------------- */
556 static void
557 edit_load_menu_file (WEdit * edit)
559 vfs_path_t *buffer_vpath;
560 vfs_path_t *menufile_vpath;
561 int dir = 0;
563 dir = query_dialog (_("Menu edit"),
564 _("Which menu file do you want to edit?"), D_NORMAL,
565 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
567 menufile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
569 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
571 vfs_path_free (menufile_vpath);
572 menufile_vpath = vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
575 switch (dir)
577 case 0:
578 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
579 check_for_default (menufile_vpath, buffer_vpath);
580 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
581 break;
583 case 1:
584 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
585 check_for_default (menufile_vpath, buffer_vpath);
586 break;
588 case 2:
589 buffer_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
590 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
592 vfs_path_free (buffer_vpath);
593 buffer_vpath =
594 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
596 break;
598 default:
599 vfs_path_free (menufile_vpath);
600 return;
603 edit_load_file_from_filename (edit, buffer_vpath);
605 vfs_path_free (buffer_vpath);
606 vfs_path_free (menufile_vpath);
609 /* --------------------------------------------------------------------------------------------- */
611 static void
612 edit_delete_column_of_text (WEdit * edit)
614 long p, q, r, m1, m2;
615 long b, c, d, n;
617 eval_marks (edit, &m1, &m2);
618 n = edit_move_forward (edit, m1, 0, m2) + 1;
619 c = edit_move_forward3 (edit, edit_bol (edit, m1), 0, m1);
620 d = edit_move_forward3 (edit, edit_bol (edit, m2), 0, m2);
621 b = max (min (c, d), min (edit->column1, edit->column2));
622 c = max (c, max (edit->column1, edit->column2));
624 while (n--)
626 r = edit_bol (edit, edit->curs1);
627 p = edit_move_forward3 (edit, r, b, 0);
628 q = edit_move_forward3 (edit, r, c, 0);
629 if (p < m1)
630 p = m1;
631 if (q > m2)
632 q = m2;
633 edit_cursor_move (edit, p - edit->curs1);
634 while (q > p)
636 /* delete line between margins */
637 if (edit_get_byte (edit, edit->curs1) != '\n')
638 edit_delete (edit, 1);
639 q--;
641 if (n)
642 /* move to next line except on the last delete */
643 edit_cursor_move (edit, edit_move_forward (edit, edit->curs1, 1, 0) - edit->curs1);
647 /* --------------------------------------------------------------------------------------------- */
648 /** if success return 0 */
650 static int
651 edit_block_delete (WEdit * edit)
653 long count;
654 long start_mark, end_mark;
655 int curs_pos, line_width;
656 long curs_line, c1, c2;
658 if (eval_marks (edit, &start_mark, &end_mark))
659 return 0;
660 if (edit->column_highlight && edit->mark2 < 0)
661 edit_mark_cmd (edit, 0);
662 if ((end_mark - start_mark) > option_max_undo / 2)
664 /* Warning message with a query to continue or cancel the operation */
665 if (edit_query_dialog2
666 (_("Warning"),
668 ("Block is large, you may not be able to undo this action"),
669 _("C&ontinue"), _("&Cancel")))
671 return 1;
674 c1 = min (edit->column1, edit->column2);
675 c2 = max (edit->column1, edit->column2);
676 edit->column1 = c1;
677 edit->column2 = c2;
679 edit_push_markers (edit);
681 curs_line = edit->curs_line;
683 /* calculate line width and cursor position before cut */
684 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
685 edit_eol (edit, edit->curs1));
686 curs_pos = edit->curs_col + edit->over_col;
688 /* move cursor to start of selection */
689 edit_cursor_move (edit, start_mark - edit->curs1);
690 edit_scroll_screen_over_cursor (edit);
691 count = start_mark;
692 if (start_mark < end_mark)
694 if (edit->column_highlight)
696 if (edit->mark2 < 0)
697 edit_mark_cmd (edit, 0);
698 edit_delete_column_of_text (edit);
699 /* move cursor to the saved position */
700 edit_move_to_line (edit, curs_line);
701 /* calculate line width after cut */
702 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
703 edit_eol (edit, edit->curs1));
704 if (option_cursor_beyond_eol && curs_pos > line_width)
705 edit->over_col = curs_pos - line_width;
707 else
709 while (count < end_mark)
711 edit_delete (edit, 1);
712 count++;
716 edit_set_markers (edit, 0, 0, 0, 0);
717 edit->force |= REDRAW_PAGE;
718 return 0;
721 /* --------------------------------------------------------------------------------------------- */
723 static gboolean
724 editcmd_find (WEdit * edit, gsize * len)
726 off_t search_start = edit->search_start;
727 off_t search_end;
728 long start_mark = 0;
729 long end_mark = edit->last_byte;
730 int mark_res = 0;
732 if (edit_search_options.only_in_selection)
734 mark_res = eval_marks (edit, &start_mark, &end_mark);
735 if (mark_res != 0)
737 edit->search->error = MC_SEARCH_E_NOTFOUND;
738 edit->search->error_str = g_strdup (_("Search string not found"));
739 return FALSE;
741 if (edit_search_options.backwards)
743 if (search_start > end_mark || search_start <= start_mark)
745 search_start = end_mark;
748 else
750 if (search_start < start_mark || search_start >= end_mark)
752 search_start = start_mark;
756 else
758 if (edit_search_options.backwards)
759 end_mark = max (1, edit->curs1) - 1;
761 if (edit_search_options.backwards)
763 search_end = end_mark;
764 while ((int) search_start >= start_mark)
766 if (search_end > (off_t) (search_start + edit->search->original_len) &&
767 mc_search_is_fixed_search_str (edit->search))
769 search_end = search_start + edit->search->original_len;
771 if (mc_search_run (edit->search, (void *) edit, search_start, search_end, len)
772 && edit->search->normal_offset == search_start)
774 return TRUE;
776 search_start--;
778 edit->search->error_str = g_strdup (_("Search string not found"));
780 else
782 return mc_search_run (edit->search, (void *) edit, search_start, end_mark, len);
784 return FALSE;
787 /* --------------------------------------------------------------------------------------------- */
789 static char *
790 edit_replace_cmd__conv_to_display (char *str)
792 #ifdef HAVE_CHARSET
793 GString *tmp;
795 tmp = str_convert_to_display (str);
796 if (tmp != NULL)
798 if (tmp->len != 0)
799 return g_string_free (tmp, FALSE);
800 g_string_free (tmp, TRUE);
802 #endif
803 return g_strdup (str);
806 /* --------------------------------------------------------------------------------------------- */
808 static char *
809 edit_replace_cmd__conv_to_input (char *str)
811 #ifdef HAVE_CHARSET
812 GString *tmp;
814 tmp = str_convert_to_input (str);
815 if (tmp != NULL)
817 if (tmp->len != 0)
818 return g_string_free (tmp, FALSE);
819 g_string_free (tmp, TRUE);
821 #endif
822 return g_strdup (str);
825 /* --------------------------------------------------------------------------------------------- */
827 static void
828 edit_do_search (WEdit * edit)
830 gsize len = 0;
832 if (edit->search == NULL)
833 edit->search_start = edit->curs1;
835 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
837 if (search_create_bookmark)
839 int found = 0, books = 0;
840 long l = 0, l_last = -1;
841 long q = 0;
843 search_create_bookmark = FALSE;
844 book_mark_flush (edit, -1);
846 while (TRUE)
848 if (!mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
849 break;
850 if (found == 0)
851 edit->search_start = edit->search->normal_offset;
852 found++;
853 l += edit_count_lines (edit, q, edit->search->normal_offset);
854 if (l != l_last)
856 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
857 books++;
859 l_last = l;
860 q = edit->search->normal_offset + 1;
863 if (found == 0)
864 edit_error_dialog (_("Search"), _("Search string not found"));
865 else
866 edit_cursor_move (edit, edit->search_start - edit->curs1);
868 else
870 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
871 && edit_search_options.backwards)
872 edit->search_start--;
874 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
875 && !edit_search_options.backwards)
876 edit->search_start++;
878 if (editcmd_find (edit, &len))
880 edit->found_start = edit->search_start = edit->search->normal_offset;
881 edit->found_len = len;
882 edit->over_col = 0;
883 edit_cursor_move (edit, edit->search_start - edit->curs1);
884 edit_scroll_screen_over_cursor (edit);
885 if (edit_search_options.backwards)
886 edit->search_start--;
887 else
888 edit->search_start++;
890 else
892 edit->search_start = edit->curs1;
893 if (edit->search->error_str != NULL)
894 edit_error_dialog (_("Search"), edit->search->error_str);
898 edit->force |= REDRAW_COMPLETELY;
899 edit_scroll_screen_over_cursor (edit);
902 /* --------------------------------------------------------------------------------------------- */
904 static void
905 edit_search (WEdit * edit)
907 if (editcmd_dialog_search_show (edit))
908 edit_do_search (edit);
911 /* --------------------------------------------------------------------------------------------- */
912 /** Return a null terminated length of text. Result must be g_free'd */
914 static unsigned char *
915 edit_get_block (WEdit * edit, long start, long finish, int *l)
917 unsigned char *s, *r;
918 r = s = g_malloc0 (finish - start + 1);
919 if (edit->column_highlight)
921 *l = 0;
922 /* copy from buffer, excluding chars that are out of the column 'margins' */
923 while (start < finish)
925 int c;
926 long x;
927 x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
928 c = edit_get_byte (edit, start);
929 if ((x >= edit->column1 && x < edit->column2)
930 || (x >= edit->column2 && x < edit->column1) || c == '\n')
932 *s++ = c;
933 (*l)++;
935 start++;
938 else
940 *l = finish - start;
941 while (start < finish)
942 *s++ = edit_get_byte (edit, start++);
944 *s = 0;
945 return r;
948 /* --------------------------------------------------------------------------------------------- */
949 /** copies a block to clipboard file */
951 static int
952 edit_save_block_to_clip_file (WEdit * edit, long start, long finish)
954 int ret;
955 gchar *tmp;
956 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
957 ret = edit_save_block (edit, tmp, start, finish);
958 g_free (tmp);
959 return ret;
962 /* --------------------------------------------------------------------------------------------- */
964 static void
965 pipe_mail (WEdit * edit, char *to, char *subject, char *cc)
967 FILE *p = 0;
968 char *s;
970 to = name_quote (to, 0);
971 subject = name_quote (subject, 0);
972 cc = name_quote (cc, 0);
973 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
974 g_free (to);
975 g_free (subject);
976 g_free (cc);
978 if (s)
980 p = popen (s, "w");
981 g_free (s);
984 if (p)
986 long i;
987 for (i = 0; i < edit->last_byte; i++)
988 fputc (edit_get_byte (edit, i), p);
989 pclose (p);
993 /* --------------------------------------------------------------------------------------------- */
995 static gboolean
996 is_break_char (char c)
998 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
1001 /* --------------------------------------------------------------------------------------------- */
1002 /** find first character of current word */
1004 static int
1005 edit_find_word_start (WEdit * edit, long *word_start, gsize * word_len)
1007 int c, last;
1008 gsize i;
1010 /* return if at begin of file */
1011 if (edit->curs1 <= 0)
1012 return 0;
1014 c = (unsigned char) edit_get_byte (edit, edit->curs1 - 1);
1015 /* return if not at end or in word */
1016 if (is_break_char (c))
1017 return 0;
1019 /* search start of word to be completed */
1020 for (i = 2;; i++)
1022 /* return if at begin of file */
1023 if ((gsize) edit->curs1 < i)
1024 return 0;
1026 last = c;
1027 c = (unsigned char) edit_get_byte (edit, edit->curs1 - i);
1029 if (is_break_char (c))
1031 /* return if word starts with digit */
1032 if (isdigit (last))
1033 return 0;
1035 *word_start = edit->curs1 - (i - 1); /* start found */
1036 *word_len = i - 1;
1037 break;
1040 /* success */
1041 return 1;
1044 /* --------------------------------------------------------------------------------------------- */
1046 * Get current word under cursor
1048 * @param edit editor object
1049 * @param srch mc_search object
1050 * @param word_start start word position
1052 * @return newly allocated string or NULL if no any words under cursor
1055 static char *
1056 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, long word_start)
1058 gsize len = 0, i;
1059 GString *temp;
1061 if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len))
1062 return NULL;
1064 temp = g_string_sized_new (len);
1066 for (i = 0; i < len; i++)
1068 int chr;
1070 chr = edit_get_byte (edit, word_start + i);
1071 if (!isspace (chr))
1072 g_string_append_c (temp, chr);
1075 return g_string_free (temp, temp->len == 0);
1078 /* --------------------------------------------------------------------------------------------- */
1079 /** collect the possible completions */
1081 static gsize
1082 edit_collect_completions (WEdit * edit, long word_start, gsize word_len,
1083 char *match_expr, struct selection *compl, gsize * num)
1085 gsize len = 0;
1086 gsize max_len = 0;
1087 gsize i;
1088 int skip;
1089 GString *temp;
1090 mc_search_t *srch;
1091 long last_byte, start = -1;
1092 char *current_word;
1094 srch = mc_search_new (match_expr, -1);
1095 if (srch == NULL)
1096 return 0;
1098 if (mc_config_get_bool
1099 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1101 last_byte = edit->last_byte;
1103 else
1105 last_byte = word_start;
1108 srch->search_type = MC_SEARCH_T_REGEX;
1109 srch->is_case_sensitive = TRUE;
1110 srch->search_fn = edit_search_cmd_callback;
1112 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1114 temp = g_string_new ("");
1116 /* collect max MAX_WORD_COMPLETIONS completions */
1117 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1119 g_string_set_size (temp, 0);
1120 start = srch->normal_offset;
1122 /* add matched completion if not yet added */
1123 for (i = 0; i < len; i++)
1125 skip = edit_get_byte (edit, start + i);
1126 if (isspace (skip))
1127 continue;
1129 /* skip current word */
1130 if (start + (long) i == word_start)
1131 break;
1133 g_string_append_c (temp, skip);
1136 if (temp->len == 0)
1137 continue;
1139 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1140 continue;
1142 skip = 0;
1144 for (i = 0; i < *num; i++)
1146 if (strncmp
1147 ((char *) &compl[i].text[word_len],
1148 (char *) &temp->str[word_len], max (len, compl[i].len) - word_len) == 0)
1150 struct selection this = compl[i];
1151 for (++i; i < *num; i++)
1153 compl[i - 1] = compl[i];
1155 compl[*num - 1] = this;
1156 skip = 1;
1157 break; /* skip it, already added */
1160 if (skip != 0)
1161 continue;
1163 if (*num == MAX_WORD_COMPLETIONS)
1165 g_free (compl[0].text);
1166 for (i = 1; i < *num; i++)
1168 compl[i - 1] = compl[i];
1170 (*num)--;
1172 #ifdef HAVE_CHARSET
1174 GString *recoded;
1175 recoded = str_convert_to_display (temp->str);
1177 if (recoded && recoded->len)
1178 g_string_assign (temp, recoded->str);
1180 g_string_free (recoded, TRUE);
1182 #endif
1183 compl[*num].text = g_strdup (temp->str);
1184 compl[*num].len = temp->len;
1185 (*num)++;
1186 start += len;
1188 /* note the maximal length needed for the completion dialog */
1189 if (len > max_len)
1190 max_len = len;
1193 mc_search_free (srch);
1194 g_string_free (temp, TRUE);
1195 g_free (current_word);
1197 return max_len;
1200 /* --------------------------------------------------------------------------------------------- */
1202 static void
1203 edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width,
1204 long *start_pos, long *end_pos, int *col1, int *col2)
1206 long cursor;
1207 int i, col;
1209 cursor = edit->curs1;
1210 col = edit_get_col (edit);
1212 for (i = 0; i < size; i++)
1214 if (data[i] != '\n')
1215 edit_insert (edit, data[i]);
1216 else
1217 { /* fill in and move to next line */
1218 int l;
1219 long p;
1221 if (edit_get_byte (edit, edit->curs1) != '\n')
1223 l = width - (edit_get_col (edit) - col);
1224 while (l > 0)
1226 edit_insert (edit, ' ');
1227 l -= space_width;
1230 for (p = edit->curs1;; p++)
1232 if (p == edit->last_byte)
1234 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1235 edit_insert_ahead (edit, '\n');
1236 p++;
1237 break;
1239 if (edit_get_byte (edit, p) == '\n')
1241 p++;
1242 break;
1245 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1246 l = col - edit_get_col (edit);
1247 while (l >= space_width)
1249 edit_insert (edit, ' ');
1250 l -= space_width;
1255 *col1 = col;
1256 *col2 = col + width;
1257 *start_pos = cursor;
1258 *end_pos = edit->curs1;
1259 edit_cursor_move (edit, cursor - edit->curs1);
1262 /* --------------------------------------------------------------------------------------------- */
1264 static int
1265 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1267 const macros_t *m1 = (const macros_t *) macro1;
1268 const macros_t *m2 = (const macros_t *) macro2;
1270 return m1->hotkey - m2->hotkey;
1273 /* --------------------------------------------------------------------------------------------- */
1275 static void
1276 edit_macro_sort_by_hotkey (void)
1278 if (macros_list != NULL && macros_list->len != 0)
1279 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1282 /* --------------------------------------------------------------------------------------------- */
1284 static gboolean
1285 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1287 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1288 macros_t *result;
1289 macros_t search_macro;
1291 (void) edit;
1293 search_macro.hotkey = hotkey;
1294 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1295 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1297 if (result != NULL && result->macro != NULL)
1299 *indx = (result - array_start);
1300 *macros = result;
1301 return TRUE;
1303 *indx = 0;
1304 return FALSE;
1307 /* --------------------------------------------------------------------------------------------- */
1308 /** returns FALSE on error */
1310 static gboolean
1311 edit_delete_macro (WEdit * edit, int hotkey)
1313 mc_config_t *macros_config = NULL;
1314 const char *section_name = "editor";
1315 gchar *macros_fname;
1316 guint indx;
1317 char *skeyname;
1318 const macros_t *macros = NULL;
1320 /* clear array of actions for current hotkey */
1321 while (edit_get_macro (edit, hotkey, &macros, &indx))
1323 if (macros->macro != NULL)
1324 g_array_free (macros->macro, TRUE);
1325 macros = NULL;
1326 g_array_remove_index (macros_list, indx);
1327 edit_macro_sort_by_hotkey ();
1330 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1331 macros_config = mc_config_init (macros_fname);
1332 g_free (macros_fname);
1334 if (macros_config == NULL)
1335 return FALSE;
1337 skeyname = lookup_key_by_code (hotkey);
1338 while (mc_config_del_key (macros_config, section_name, skeyname))
1340 g_free (skeyname);
1341 mc_config_save_file (macros_config, NULL);
1342 mc_config_deinit (macros_config);
1343 return TRUE;
1347 /* --------------------------------------------------------------------------------------------- */
1348 /*** public functions ****************************************************************************/
1349 /* --------------------------------------------------------------------------------------------- */
1351 void
1352 edit_help_cmd (WEdit * edit)
1354 ev_help_t event_data = { NULL, "[Internal File Editor]" };
1355 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
1357 edit->force |= REDRAW_COMPLETELY;
1360 /* --------------------------------------------------------------------------------------------- */
1362 void
1363 edit_refresh_cmd (WEdit * edit)
1365 #ifdef HAVE_SLANG
1366 int color;
1368 edit_get_syntax_color (edit, -1, &color);
1369 tty_touch_screen ();
1370 mc_refresh ();
1371 #else
1372 (void) edit;
1374 clr_scr ();
1375 repaint_screen ();
1376 #endif /* !HAVE_SLANG */
1377 tty_keypad (TRUE);
1380 /* --------------------------------------------------------------------------------------------- */
1382 void
1383 menu_save_mode_cmd (void)
1385 /* diaog sizes */
1386 const int DLG_X = 38;
1387 const int DLG_Y = 13;
1389 char *str_result;
1391 const char *str[] = {
1392 N_("&Quick save"),
1393 N_("&Safe save"),
1394 N_("&Do backups with following extension:")
1397 QuickWidget widgets[] = {
1398 /* 0 */
1399 QUICK_BUTTON (18, DLG_X, DLG_Y - 3, DLG_Y, N_("&Cancel"), B_CANCEL, NULL),
1400 /* 1 */
1401 QUICK_BUTTON (6, DLG_X, DLG_Y - 3, DLG_Y, N_("&OK"), B_ENTER, NULL),
1402 /* 2 */
1403 QUICK_CHECKBOX (4, DLG_X, 8, DLG_Y, N_("Check &POSIX new line"), &option_check_nl_at_eof),
1404 /* 3 */
1405 QUICK_INPUT (8, DLG_X, 6, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result),
1406 /* 4 */
1407 QUICK_RADIO (4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode),
1408 QUICK_END
1411 QuickDialog dialog = {
1412 DLG_X, DLG_Y, -1, -1, N_("Edit Save Mode"),
1413 "[Edit Save Mode]", widgets, NULL, FALSE
1416 size_t i;
1417 size_t maxlen = 0;
1418 size_t w0, w1, b_len, w3;
1420 #ifdef HAVE_ASSERT_H
1421 assert (option_backup_ext != NULL);
1422 #endif
1424 /* OK/Cancel buttons */
1425 w0 = str_term_width1 (_(widgets[0].u.button.text)) + 3;
1426 w1 = str_term_width1 (_(widgets[1].u.button.text)) + 5; /* default button */
1427 b_len = w0 + w1 + 3;
1429 maxlen = max (b_len, (size_t) str_term_width1 (_(dialog.title)) + 2);
1431 w3 = 0;
1432 for (i = 0; i < 3; i++)
1434 #ifdef ENABLE_NLS
1435 str[i] = _(str[i]);
1436 #endif
1437 w3 = max (w3, (size_t) str_term_width1 (str[i]));
1440 maxlen = max (maxlen, w3 + 4);
1442 dialog.xlen = min ((size_t) COLS, maxlen + 8);
1444 widgets[3].u.input.len = w3;
1445 widgets[1].relative_x = (dialog.xlen - b_len) / 2;
1446 widgets[0].relative_x = widgets[1].relative_x + w0 + 2;
1448 for (i = 0; i < sizeof (widgets) / sizeof (widgets[0]); i++)
1449 widgets[i].x_divisions = dialog.xlen;
1451 if (quick_dialog (&dialog) != B_CANCEL)
1453 g_free (option_backup_ext);
1454 option_backup_ext = str_result;
1458 /* --------------------------------------------------------------------------------------------- */
1460 void
1461 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1463 vfs_path_free (edit->filename_vpath);
1464 edit->filename_vpath = vfs_path_clone (name_vpath);
1466 if (edit->dir_vpath == NULL)
1467 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1470 /* --------------------------------------------------------------------------------------------- */
1471 /* Here we want to warn the users of overwriting an existing file,
1472 but only if they have made a change to the filename */
1473 /* returns 1 on success */
1475 edit_save_as_cmd (WEdit * edit)
1477 /* This heads the 'Save As' dialog box */
1478 vfs_path_t *exp_vpath;
1479 int save_lock = 0;
1480 int different_filename = 0;
1482 if (!edit_check_newline (edit))
1483 return 0;
1485 exp_vpath = edit_get_save_file_as (edit);
1486 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1488 if (exp_vpath != NULL)
1490 if (vfs_path_len (exp_vpath) == 0)
1491 goto ret;
1492 else
1494 int rv;
1496 if (vfs_path_cmp (edit->filename_vpath, exp_vpath) != 0)
1498 int file;
1500 different_filename = 1;
1501 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1503 if (file != -1)
1505 /* the file exists */
1506 mc_close (file);
1507 /* Overwrite the current file or cancel the operation */
1508 if (edit_query_dialog2
1509 (_("Warning"),
1510 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1511 goto ret;
1513 else
1515 edit->stat1.st_mode |= S_IWUSR;
1517 save_lock = lock_file (exp_vpath);
1519 else
1521 /* filenames equal, check if already locked */
1522 if (!edit->locked && !edit->delete_file)
1523 save_lock = lock_file (exp_vpath);
1526 if (different_filename)
1529 * Allow user to write into saved (under another name) file
1530 * even if original file had r/o user permissions.
1532 edit->stat1.st_mode |= S_IWRITE;
1535 rv = edit_save_file (edit, exp_vpath);
1536 switch (rv)
1538 case 1:
1539 /* Succesful, so unlock both files */
1540 if (different_filename)
1542 if (save_lock)
1543 unlock_file (exp_vpath);
1544 if (edit->locked)
1545 edit->locked = unlock_file (edit->filename_vpath);
1547 else
1549 if (edit->locked || save_lock)
1550 edit->locked = unlock_file (edit->filename_vpath);
1553 edit_set_filename (edit, exp_vpath);
1554 if (edit->lb != LB_ASIS)
1555 edit_reload (edit, exp_vpath);
1556 edit->modified = 0;
1557 edit->delete_file = 0;
1558 if (different_filename)
1559 edit_load_syntax (edit, NULL, edit->syntax_type);
1560 vfs_path_free (exp_vpath);
1561 edit->force |= REDRAW_COMPLETELY;
1562 return 1;
1563 default:
1564 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1565 /* fallthrough */
1566 case -1:
1567 /* Failed, so maintain modify (not save) lock */
1568 if (save_lock)
1569 unlock_file (exp_vpath);
1570 break;
1575 ret:
1576 vfs_path_free (exp_vpath);
1577 edit->force |= REDRAW_COMPLETELY;
1578 return 0;
1581 /* {{{ Macro stuff starts here */
1582 /* --------------------------------------------------------------------------------------------- */
1584 void
1585 edit_delete_macro_cmd (WEdit * edit)
1587 int hotkey;
1589 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1591 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1592 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1595 /* --------------------------------------------------------------------------------------------- */
1597 /** returns FALSE on error */
1598 gboolean
1599 edit_execute_macro (WEdit * edit, int hotkey)
1601 gboolean res = FALSE;
1603 if (hotkey != 0)
1605 const macros_t *macros;
1606 guint indx;
1608 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1609 macros->macro != NULL && macros->macro->len != 0)
1611 guint i;
1613 edit->force |= REDRAW_PAGE;
1615 for (i = 0; i < macros->macro->len; i++)
1617 const macro_action_t *m_act;
1619 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1620 edit_execute_cmd (edit, m_act->action, m_act->ch);
1621 res = TRUE;
1626 return res;
1629 /* --------------------------------------------------------------------------------------------- */
1631 /** returns FALSE on error */
1632 gboolean
1633 edit_store_macro_cmd (WEdit * edit)
1635 int i;
1636 int hotkey;
1637 GString *marcros_string;
1638 mc_config_t *macros_config = NULL;
1639 const char *section_name = "editor";
1640 gchar *macros_fname;
1641 GArray *macros; /* current macro */
1642 int tmp_act;
1643 gboolean have_macro = FALSE;
1644 char *skeyname = NULL;
1646 hotkey = editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1647 if (hotkey == ESC_CHAR)
1648 return FALSE;
1650 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1652 /* return FALSE if try assign macro into restricted hotkeys */
1653 if (tmp_act == CK_MacroStartRecord
1654 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1655 return FALSE;
1657 edit_delete_macro (edit, hotkey);
1659 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1660 macros_config = mc_config_init (macros_fname);
1661 g_free (macros_fname);
1663 if (macros_config == NULL)
1664 return FALSE;
1666 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1668 marcros_string = g_string_sized_new (250);
1669 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1671 skeyname = lookup_key_by_code (hotkey);
1673 for (i = 0; i < macro_index; i++)
1675 macro_action_t m_act;
1676 const char *action_name;
1678 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1680 if (action_name == NULL)
1681 break;
1683 m_act.action = record_macro_buf[i].action;
1684 m_act.ch = record_macro_buf[i].ch;
1685 g_array_append_val (macros, m_act);
1686 have_macro = TRUE;
1687 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1688 (int) record_macro_buf[i].ch);
1690 if (have_macro)
1692 macros_t macro;
1693 macro.hotkey = hotkey;
1694 macro.macro = macros;
1695 g_array_append_val (macros_list, macro);
1696 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1698 else
1699 mc_config_del_key (macros_config, section_name, skeyname);
1701 g_free (skeyname);
1702 edit_macro_sort_by_hotkey ();
1704 g_string_free (marcros_string, TRUE);
1705 mc_config_save_file (macros_config, NULL);
1706 mc_config_deinit (macros_config);
1707 return TRUE;
1710 /* --------------------------------------------------------------------------------------------- */
1712 gboolean
1713 edit_repeat_macro_cmd (WEdit * edit)
1715 int i, j;
1716 char *f;
1717 long count_repeat;
1718 char *error = NULL;
1720 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL);
1721 if (f == NULL || *f == '\0')
1723 g_free (f);
1724 return FALSE;
1727 count_repeat = strtol (f, &error, 0);
1729 if (*error != '\0')
1731 g_free (f);
1732 return FALSE;
1735 g_free (f);
1737 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1738 edit->force |= REDRAW_PAGE;
1740 for (j = 0; j < count_repeat; j++)
1741 for (i = 0; i < macro_index; i++)
1742 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1743 edit_update_screen (edit);
1744 return TRUE;
1747 /* --------------------------------------------------------------------------------------------- */
1748 /** return FALSE on error */
1750 gboolean
1751 edit_load_macro_cmd (WEdit * edit)
1753 mc_config_t *macros_config = NULL;
1754 gchar **profile_keys, **keys;
1755 gchar **values, **curr_values;
1756 gsize len, values_len;
1757 const char *section_name = "editor";
1758 gchar *macros_fname;
1759 int hotkey;
1761 (void) edit;
1763 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1764 macros_config = mc_config_init (macros_fname);
1765 g_free (macros_fname);
1767 if (macros_config == NULL)
1768 return FALSE;
1770 profile_keys = keys = mc_config_get_keys (macros_config, section_name, &len);
1771 while (*profile_keys != NULL)
1773 gboolean have_macro;
1774 GArray *macros;
1775 macros_t macro;
1777 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1779 curr_values = values = mc_config_get_string_list (macros_config, section_name,
1780 *profile_keys, &values_len);
1781 hotkey = lookup_key (*profile_keys, NULL);
1782 have_macro = FALSE;
1784 while (*curr_values != NULL && *curr_values[0] != '\0')
1786 char **macro_pair = NULL;
1788 macro_pair = g_strsplit (*curr_values, ":", 2);
1790 if (macro_pair != NULL)
1792 macro_action_t m_act;
1793 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
1794 m_act.action = 0;
1795 else
1797 m_act.action = keybind_lookup_action (macro_pair[0]);
1798 g_free (macro_pair[0]);
1799 macro_pair[0] = NULL;
1801 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
1802 m_act.ch = -1;
1803 else
1805 m_act.ch = strtol (macro_pair[1], NULL, 0);
1806 g_free (macro_pair[1]);
1807 macro_pair[1] = NULL;
1809 if (m_act.action != 0)
1811 /* a shell command */
1812 if ((m_act.action / CK_PipeBlock (0)) == 1)
1814 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
1815 m_act.ch = -1;
1817 g_array_append_val (macros, m_act);
1818 have_macro = TRUE;
1820 g_strfreev (macro_pair);
1821 macro_pair = NULL;
1823 curr_values++;
1825 if (have_macro)
1827 macro.hotkey = hotkey;
1828 macro.macro = macros;
1829 g_array_append_val (macros_list, macro);
1831 profile_keys++;
1832 g_strfreev (values);
1834 g_strfreev (keys);
1835 mc_config_deinit (macros_config);
1836 edit_macro_sort_by_hotkey ();
1837 return TRUE;
1840 /* }}} Macro stuff end here */
1842 /* --------------------------------------------------------------------------------------------- */
1843 /** returns 1 on success */
1846 edit_save_confirm_cmd (WEdit * edit)
1848 gchar *f = NULL;
1850 if (edit->filename_vpath == NULL)
1851 return edit_save_as_cmd (edit);
1853 if (!edit_check_newline (edit))
1854 return 0;
1856 if (edit_confirm_save)
1858 char *filename;
1859 gboolean ok;
1861 filename = vfs_path_to_str (edit->filename_vpath);
1862 f = g_strdup_printf (_("Confirm save file: \"%s\""), filename);
1863 g_free (filename);
1864 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
1865 g_free (f);
1866 if (!ok)
1867 return 0;
1869 return edit_save_cmd (edit);
1872 /* --------------------------------------------------------------------------------------------- */
1873 /** returns 1 on success */
1876 edit_new_cmd (WEdit * edit)
1878 if (edit->modified)
1880 if (edit_query_dialog2
1881 (_("Warning"),
1883 ("Current text was modified without a file save.\nContinue discards these changes"),
1884 _("C&ontinue"), _("&Cancel")))
1886 edit->force |= REDRAW_COMPLETELY;
1887 return 0;
1890 edit->force |= REDRAW_COMPLETELY;
1892 return edit_renew (edit); /* if this gives an error, something has really screwed up */
1895 /* --------------------------------------------------------------------------------------------- */
1898 edit_load_cmd (WEdit * edit, edit_current_file_t what)
1900 if (edit->modified
1901 && (edit_query_dialog2
1902 (_("Warning"),
1903 _("Current text was modified without a file save.\n"
1904 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")) == 1))
1906 edit->force |= REDRAW_COMPLETELY;
1907 return 0;
1910 switch (what)
1912 case EDIT_FILE_COMMON:
1914 char *filename, *exp;
1916 filename = vfs_path_to_str (edit->filename_vpath);
1917 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
1918 MC_HISTORY_EDIT_LOAD, filename);
1919 g_free (filename);
1921 if (exp != NULL)
1923 if (*exp != '\0')
1925 vfs_path_t *exp_vpath;
1927 exp_vpath = vfs_path_from_str (exp);
1928 edit_load_file_from_filename (edit, exp_vpath);
1929 vfs_path_free (exp_vpath);
1931 g_free (exp);
1934 break;
1936 case EDIT_FILE_SYNTAX:
1937 edit_load_syntax_file (edit);
1938 break;
1940 case EDIT_FILE_MENU:
1941 edit_load_menu_file (edit);
1942 break;
1944 default:
1945 break;
1948 edit->force |= REDRAW_COMPLETELY;
1949 return 0;
1952 /* --------------------------------------------------------------------------------------------- */
1954 if mark2 is -1 then marking is from mark1 to the cursor.
1955 Otherwise its between the markers. This handles this.
1956 Returns 1 if no text is marked.
1960 eval_marks (WEdit * edit, long *start_mark, long *end_mark)
1962 if (edit->mark1 != edit->mark2)
1964 long start_bol, start_eol;
1965 long end_bol, end_eol;
1966 long col1, col2;
1967 long diff1, diff2;
1968 long end_mark_curs;
1970 if (edit->end_mark_curs < 0)
1971 end_mark_curs = edit->curs1;
1972 else
1973 end_mark_curs = edit->end_mark_curs;
1975 if (edit->mark2 >= 0)
1977 *start_mark = min (edit->mark1, edit->mark2);
1978 *end_mark = max (edit->mark1, edit->mark2);
1980 else
1982 *start_mark = min (edit->mark1, end_mark_curs);
1983 *end_mark = max (edit->mark1, end_mark_curs);
1984 edit->column2 = edit->curs_col + edit->over_col;
1987 if (edit->column_highlight
1988 && (((edit->mark1 > end_mark_curs) && (edit->column1 < edit->column2))
1989 || ((edit->mark1 < end_mark_curs) && (edit->column1 > edit->column2))))
1991 start_bol = edit_bol (edit, *start_mark);
1992 start_eol = edit_eol (edit, start_bol - 1) + 1;
1993 end_bol = edit_bol (edit, *end_mark);
1994 end_eol = edit_eol (edit, *end_mark);
1995 col1 = min (edit->column1, edit->column2);
1996 col2 = max (edit->column1, edit->column2);
1998 diff1 =
1999 edit_move_forward3 (edit, start_bol, col2, 0) - edit_move_forward3 (edit, start_bol,
2000 col1, 0);
2001 diff2 =
2002 edit_move_forward3 (edit, end_bol, col2, 0) - edit_move_forward3 (edit, end_bol,
2003 col1, 0);
2005 *start_mark -= diff1;
2006 *end_mark += diff2;
2007 *start_mark = max (*start_mark, start_eol);
2008 *end_mark = min (*end_mark, end_eol);
2010 return 0;
2012 else
2014 *start_mark = *end_mark = 0;
2015 edit->column2 = edit->column1 = 0;
2016 return 1;
2020 /* --------------------------------------------------------------------------------------------- */
2022 void
2023 edit_insert_over (WEdit * edit)
2025 int i;
2027 for (i = 0; i < edit->over_col; i++)
2029 edit_insert (edit, ' ');
2031 edit->over_col = 0;
2034 /* --------------------------------------------------------------------------------------------- */
2037 edit_insert_column_of_text_from_file (WEdit * edit, int file,
2038 long *start_pos, long *end_pos, int *col1, int *col2)
2040 long cursor;
2041 int col;
2042 int blocklen = -1, width = 0;
2043 unsigned char *data;
2045 cursor = edit->curs1;
2046 col = edit_get_col (edit);
2047 data = g_malloc0 (TEMP_BUF_LEN);
2049 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
2051 int i;
2052 for (width = 0; width < blocklen; width++)
2054 if (data[width] == '\n')
2055 break;
2057 for (i = 0; i < blocklen; i++)
2059 if (data[i] == '\n')
2060 { /* fill in and move to next line */
2061 int l;
2062 long p;
2063 if (edit_get_byte (edit, edit->curs1) != '\n')
2065 l = width - (edit_get_col (edit) - col);
2066 while (l > 0)
2068 edit_insert (edit, ' ');
2069 l -= space_width;
2072 for (p = edit->curs1;; p++)
2074 if (p == edit->last_byte)
2076 edit_cursor_move (edit, edit->last_byte - edit->curs1);
2077 edit_insert_ahead (edit, '\n');
2078 p++;
2079 break;
2081 if (edit_get_byte (edit, p) == '\n')
2083 p++;
2084 break;
2087 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
2088 l = col - edit_get_col (edit);
2089 while (l >= space_width)
2091 edit_insert (edit, ' ');
2092 l -= space_width;
2094 continue;
2096 edit_insert (edit, data[i]);
2099 *col1 = col;
2100 *col2 = col + width;
2101 *start_pos = cursor;
2102 *end_pos = edit->curs1;
2103 edit_cursor_move (edit, cursor - edit->curs1);
2104 g_free (data);
2106 return blocklen;
2109 /* --------------------------------------------------------------------------------------------- */
2111 void
2112 edit_block_copy_cmd (WEdit * edit)
2114 long start_mark, end_mark, current = edit->curs1;
2115 long col_delta = 0;
2116 long mark1, mark2;
2117 int c1, c2;
2118 int size;
2119 unsigned char *copy_buf;
2121 edit_update_curs_col (edit);
2122 if (eval_marks (edit, &start_mark, &end_mark))
2123 return;
2125 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2127 /* all that gets pushed are deletes hence little space is used on the stack */
2129 edit_push_markers (edit);
2131 if (edit->column_highlight)
2133 col_delta = abs (edit->column2 - edit->column1);
2134 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2136 else
2138 while (size--)
2139 edit_insert_ahead (edit, copy_buf[size]);
2142 g_free (copy_buf);
2143 edit_scroll_screen_over_cursor (edit);
2145 if (edit->column_highlight)
2146 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2147 else if (start_mark < current && end_mark > current)
2148 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2150 edit->force |= REDRAW_PAGE;
2154 /* --------------------------------------------------------------------------------------------- */
2156 void
2157 edit_block_move_cmd (WEdit * edit)
2159 long current;
2160 unsigned char *copy_buf = NULL;
2161 long start_mark, end_mark;
2162 long line;
2164 if (eval_marks (edit, &start_mark, &end_mark))
2165 return;
2167 line = edit->curs_line;
2168 if (edit->mark2 < 0)
2169 edit_mark_cmd (edit, 0);
2170 edit_push_markers (edit);
2172 if (edit->column_highlight)
2174 long mark1, mark2;
2175 int size;
2176 int b_width = 0;
2177 int c1, c2;
2178 int x, x2;
2180 c1 = min (edit->column1, edit->column2);
2181 c2 = max (edit->column1, edit->column2);
2182 b_width = (c2 - c1);
2184 edit_update_curs_col (edit);
2186 x = edit->curs_col;
2187 x2 = x + edit->over_col;
2189 /* do nothing when cursor inside first line of selected area */
2190 if ((edit_eol (edit, edit->curs1) == edit_eol (edit, start_mark)) && (x2 > c1 && x2 <= c2))
2191 return;
2193 if (edit->curs1 > start_mark && edit->curs1 < edit_eol (edit, end_mark))
2195 if (x > c2)
2196 x -= b_width;
2197 else if (x > c1 && x <= c2)
2198 x = c1;
2200 /* save current selection into buffer */
2201 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2203 /* remove current selection */
2204 edit_block_delete_cmd (edit);
2206 edit->over_col = max (0, edit->over_col - b_width);
2207 /* calculate the cursor pos after delete block */
2208 current = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), x, 0);
2209 edit_cursor_move (edit, current - edit->curs1);
2210 edit_scroll_screen_over_cursor (edit);
2212 /* add TWS if need before block insertion */
2213 if (option_cursor_beyond_eol && edit->over_col > 0)
2214 edit_insert_over (edit);
2216 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2217 edit_set_markers (edit, mark1, mark2, c1, c2);
2219 else
2221 long count;
2223 current = edit->curs1;
2224 copy_buf = g_malloc0 (end_mark - start_mark);
2225 edit_cursor_move (edit, start_mark - edit->curs1);
2226 edit_scroll_screen_over_cursor (edit);
2227 count = start_mark;
2228 while (count < end_mark)
2230 copy_buf[end_mark - count - 1] = edit_delete (edit, 1);
2231 count++;
2233 edit_scroll_screen_over_cursor (edit);
2234 edit_cursor_move (edit,
2235 current - edit->curs1 -
2236 (((current - edit->curs1) > 0) ? end_mark - start_mark : 0));
2237 edit_scroll_screen_over_cursor (edit);
2238 while (count-- > start_mark)
2239 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2240 edit_set_markers (edit, edit->curs1, edit->curs1 + end_mark - start_mark, 0, 0);
2243 edit_scroll_screen_over_cursor (edit);
2244 g_free (copy_buf);
2245 edit->force |= REDRAW_PAGE;
2248 /* --------------------------------------------------------------------------------------------- */
2249 /** returns 1 if canceelled by user */
2252 edit_block_delete_cmd (WEdit * edit)
2254 long start_mark, end_mark;
2255 if (eval_marks (edit, &start_mark, &end_mark))
2257 edit_delete_line (edit);
2258 return 0;
2260 return edit_block_delete (edit);
2263 /* --------------------------------------------------------------------------------------------- */
2264 /** call with edit = 0 before shutdown to close memory leaks */
2266 void
2267 edit_replace_cmd (WEdit * edit, int again)
2269 /* 1 = search string, 2 = replace with */
2270 static char *saved1 = NULL; /* saved default[123] */
2271 static char *saved2 = NULL;
2272 char *input1 = NULL; /* user input from the dialog */
2273 char *input2 = NULL;
2274 char *disp1 = NULL;
2275 char *disp2 = NULL;
2276 long times_replaced = 0;
2277 gboolean once_found = FALSE;
2279 if (!edit)
2281 g_free (saved1), saved1 = NULL;
2282 g_free (saved2), saved2 = NULL;
2283 return;
2286 edit->force |= REDRAW_COMPLETELY;
2288 if (again && !saved1 && !saved2)
2289 again = 0;
2291 if (again)
2293 input1 = g_strdup (saved1 ? saved1 : "");
2294 input2 = g_strdup (saved2 ? saved2 : "");
2296 else
2298 char *tmp_inp1, *tmp_inp2;
2299 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2300 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2302 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2304 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2306 g_free (disp1);
2307 g_free (disp2);
2309 if (input1 == NULL || *input1 == '\0')
2311 edit->force = REDRAW_COMPLETELY;
2312 goto cleanup;
2315 tmp_inp1 = input1;
2316 tmp_inp2 = input2;
2317 input1 = edit_replace_cmd__conv_to_input (input1);
2318 input2 = edit_replace_cmd__conv_to_input (input2);
2319 g_free (tmp_inp1);
2320 g_free (tmp_inp2);
2322 g_free (saved1), saved1 = g_strdup (input1);
2323 g_free (saved2), saved2 = g_strdup (input2);
2325 mc_search_free (edit->search);
2326 edit->search = NULL;
2329 if (!edit->search)
2331 edit->search = mc_search_new (input1, -1);
2332 if (edit->search == NULL)
2334 edit->search_start = edit->curs1;
2335 goto cleanup;
2337 edit->search->search_type = edit_search_options.type;
2338 edit->search->is_all_charsets = edit_search_options.all_codepages;
2339 edit->search->is_case_sensitive = edit_search_options.case_sens;
2340 edit->search->whole_words = edit_search_options.whole_words;
2341 edit->search->search_fn = edit_search_cmd_callback;
2344 if (edit->found_len && edit->search_start == edit->found_start + 1
2345 && edit_search_options.backwards)
2346 edit->search_start--;
2348 if (edit->found_len && edit->search_start == edit->found_start - 1
2349 && !edit_search_options.backwards)
2350 edit->search_start++;
2354 gsize len = 0;
2356 if (!editcmd_find (edit, &len))
2358 if (!(edit->search->error == MC_SEARCH_E_OK ||
2359 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2361 edit_error_dialog (_("Search"), edit->search->error_str);
2363 break;
2365 once_found = TRUE;
2367 edit->search_start = edit->search->normal_offset;
2368 /*returns negative on not found or error in pattern */
2370 if ((edit->search_start >= 0) && (edit->search_start < edit->last_byte))
2372 gsize i;
2373 GString *tmp_str, *repl_str;
2375 edit->found_start = edit->search_start;
2376 i = edit->found_len = len;
2378 edit_cursor_move (edit, edit->search_start - edit->curs1);
2379 edit_scroll_screen_over_cursor (edit);
2381 if (edit->replace_mode == 0)
2383 int l;
2384 int prompt;
2386 l = edit->curs_row - edit->widget.lines / 3;
2387 if (l > 0)
2388 edit_scroll_downward (edit, l);
2389 if (l < 0)
2390 edit_scroll_upward (edit, -l);
2392 edit_scroll_screen_over_cursor (edit);
2393 edit->force |= REDRAW_PAGE;
2394 edit_render_keypress (edit);
2396 /*so that undo stops at each query */
2397 edit_push_key_press (edit);
2398 /* and prompt 2/3 down */
2399 disp1 = edit_replace_cmd__conv_to_display (saved1);
2400 disp2 = edit_replace_cmd__conv_to_display (saved2);
2401 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2402 g_free (disp1);
2403 g_free (disp2);
2405 if (prompt == B_REPLACE_ALL)
2406 edit->replace_mode = 1;
2407 else if (prompt == B_SKIP_REPLACE)
2409 if (edit_search_options.backwards)
2410 edit->search_start--;
2411 else
2412 edit->search_start++;
2413 continue; /* loop */
2415 else if (prompt == B_CANCEL)
2417 edit->replace_mode = -1;
2418 break; /* loop */
2422 /* don't process string each time */
2423 tmp_str = g_string_new (input2);
2424 repl_str = mc_search_prepare_replace_str (edit->search, tmp_str);
2425 g_string_free (tmp_str, TRUE);
2427 if (edit->search->error != MC_SEARCH_E_OK)
2429 edit_error_dialog (_("Replace"), edit->search->error_str);
2430 g_string_free (repl_str, TRUE);
2431 break;
2434 /* delete then insert new */
2435 for (i = 0; i < len; i++)
2436 edit_delete (edit, 1);
2438 for (i = 0; i < repl_str->len; i++)
2439 edit_insert (edit, repl_str->str[i]);
2441 edit->found_len = repl_str->len;
2442 g_string_free (repl_str, TRUE);
2443 times_replaced++;
2445 /* so that we don't find the same string again */
2446 if (edit_search_options.backwards)
2447 edit->search_start--;
2448 else
2450 edit->search_start += edit->found_len;
2452 if (edit->search_start >= edit->last_byte)
2453 break;
2456 edit_scroll_screen_over_cursor (edit);
2458 else
2460 /* try and find from right here for next search */
2461 edit->search_start = edit->curs1;
2462 edit_update_curs_col (edit);
2464 edit->force |= REDRAW_PAGE;
2465 edit_render_keypress (edit);
2467 if (times_replaced == 0)
2468 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2469 break;
2472 while (edit->replace_mode >= 0);
2474 edit_scroll_screen_over_cursor (edit);
2475 edit->force |= REDRAW_COMPLETELY;
2476 edit_render_keypress (edit);
2478 if ((edit->replace_mode == 1) && (times_replaced != 0))
2479 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2481 cleanup:
2482 g_free (input1);
2483 g_free (input2);
2486 /* --------------------------------------------------------------------------------------------- */
2489 edit_search_cmd_callback (const void *user_data, gsize char_offset)
2491 return edit_get_byte ((WEdit *) user_data, (long) char_offset);
2494 /* --------------------------------------------------------------------------------------------- */
2496 void
2497 edit_search_cmd (WEdit * edit, gboolean again)
2499 if (edit == NULL)
2500 return;
2502 if (!again)
2503 edit_search (edit);
2504 else if (edit->last_search_string != NULL)
2505 edit_do_search (edit);
2506 else
2508 /* find last search string in history */
2509 GList *history;
2511 history = history_get (MC_HISTORY_SHARED_SEARCH);
2512 if (history != NULL && history->data != NULL)
2514 edit->last_search_string = (char *) history->data;
2515 history->data = NULL;
2516 history = g_list_first (history);
2517 g_list_foreach (history, (GFunc) g_free, NULL);
2518 g_list_free (history);
2520 edit->search = mc_search_new (edit->last_search_string, -1);
2521 if (edit->search == NULL)
2523 /* if not... then ask for an expression */
2524 g_free (edit->last_search_string);
2525 edit->last_search_string = NULL;
2526 edit_search (edit);
2528 else
2530 edit->search->search_type = edit_search_options.type;
2531 edit->search->is_all_charsets = edit_search_options.all_codepages;
2532 edit->search->is_case_sensitive = edit_search_options.case_sens;
2533 edit->search->whole_words = edit_search_options.whole_words;
2534 edit->search->search_fn = edit_search_cmd_callback;
2535 edit_do_search (edit);
2538 else
2540 /* if not... then ask for an expression */
2541 g_free (edit->last_search_string);
2542 edit->last_search_string = NULL;
2543 edit_search (edit);
2549 /* --------------------------------------------------------------------------------------------- */
2551 * Check if it's OK to close the editor. If there are unsaved changes,
2552 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2555 gboolean
2556 edit_ok_to_exit (WEdit * edit)
2558 int act;
2560 if (!edit->modified)
2561 return TRUE;
2563 if (!mc_global.widget.midnight_shutdown)
2565 if (!edit_check_newline (edit))
2566 return FALSE;
2568 query_set_sel (2);
2569 act = edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2570 _("&Yes"), _("&No"), _("&Cancel quit"));
2572 else
2574 act =
2575 edit_query_dialog2 (_("Quit"),
2576 _("Midnight Commander is being shut down.\nSave modified file?"),
2577 _("&Yes"), _("&No"));
2579 /* Esc is No */
2580 if (act == -1)
2581 act = 1;
2584 switch (act)
2586 case 0: /* Yes */
2587 edit_push_markers (edit);
2588 edit_set_markers (edit, 0, 0, 0, 0);
2589 if (!edit_save_cmd (edit) || mc_global.widget.midnight_shutdown)
2590 return mc_global.widget.midnight_shutdown;
2591 break;
2592 case 1: /* No */
2593 break;
2594 case 2: /* Cancel quit */
2595 case -1: /* Esc */
2596 return FALSE;
2599 return TRUE;
2602 /* --------------------------------------------------------------------------------------------- */
2603 /** save block, returns 1 on success */
2606 edit_save_block (WEdit * edit, const char *filename, long start, long finish)
2608 int len, file;
2609 vfs_path_t *vpath;
2611 vpath = vfs_path_from_str (filename);
2612 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2613 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2614 vfs_path_free (vpath);
2615 if (file == -1)
2616 return 0;
2618 if (edit->column_highlight)
2620 int r;
2622 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2623 if (r > 0)
2625 unsigned char *block, *p;
2627 p = block = edit_get_block (edit, start, finish, &len);
2628 while (len)
2630 r = mc_write (file, p, len);
2631 if (r < 0)
2632 break;
2633 p += r;
2634 len -= r;
2636 g_free (block);
2639 else
2641 unsigned char *buf;
2642 int i = start, end;
2644 len = finish - start;
2645 buf = g_malloc0 (TEMP_BUF_LEN);
2646 while (start != finish)
2648 end = min (finish, start + TEMP_BUF_LEN);
2649 for (; i < end; i++)
2650 buf[i - start] = edit_get_byte (edit, i);
2651 len -= mc_write (file, (char *) buf, end - start);
2652 start = end;
2654 g_free (buf);
2656 mc_close (file);
2657 if (len)
2658 return 0;
2659 return 1;
2662 /* --------------------------------------------------------------------------------------------- */
2664 void
2665 edit_paste_from_history (WEdit * edit)
2667 (void) edit;
2668 edit_error_dialog (_("Error"), _("This function is not implemented"));
2671 /* --------------------------------------------------------------------------------------------- */
2674 edit_copy_to_X_buf_cmd (WEdit * edit)
2676 long start_mark, end_mark;
2677 if (eval_marks (edit, &start_mark, &end_mark))
2678 return 0;
2679 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2681 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2682 return 1;
2684 /* try use external clipboard utility */
2685 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2687 return 0;
2690 /* --------------------------------------------------------------------------------------------- */
2693 edit_cut_to_X_buf_cmd (WEdit * edit)
2695 long start_mark, end_mark;
2696 if (eval_marks (edit, &start_mark, &end_mark))
2697 return 0;
2698 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2700 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2701 return 1;
2703 /* try use external clipboard utility */
2704 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2706 edit_block_delete_cmd (edit);
2707 edit_mark_cmd (edit, 1);
2708 return 0;
2711 /* --------------------------------------------------------------------------------------------- */
2713 void
2714 edit_paste_from_X_buf_cmd (WEdit * edit)
2716 vfs_path_t *tmp;
2718 /* try use external clipboard utility */
2719 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
2720 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
2721 edit_insert_file (edit, tmp);
2722 vfs_path_free (tmp);
2726 /* --------------------------------------------------------------------------------------------- */
2728 * Ask user for the line and go to that line.
2729 * Negative numbers mean line from the end (i.e. -1 is the last line).
2732 void
2733 edit_goto_cmd (WEdit * edit)
2735 char *f;
2736 static long line = 0; /* line as typed, saved as default */
2737 long l;
2738 char *error;
2739 char s[32];
2741 g_snprintf (s, sizeof (s), "%ld", line);
2742 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "");
2743 if (!f)
2744 return;
2746 if (!*f)
2748 g_free (f);
2749 return;
2752 l = strtol (f, &error, 0);
2753 if (*error)
2755 g_free (f);
2756 return;
2759 line = l;
2760 if (l < 0)
2761 l = edit->total_lines + l + 2;
2762 edit_move_display (edit, l - edit->widget.lines / 2 - 1);
2763 edit_move_to_line (edit, l - 1);
2764 edit->force |= REDRAW_COMPLETELY;
2765 g_free (f);
2769 /* --------------------------------------------------------------------------------------------- */
2770 /** Return 1 on success */
2773 edit_save_block_cmd (WEdit * edit)
2775 long start_mark, end_mark;
2776 char *exp, *tmp;
2778 if (eval_marks (edit, &start_mark, &end_mark))
2779 return 1;
2781 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
2782 exp =
2783 input_expand_dialog (_("Save block"), _("Enter file name:"),
2784 MC_HISTORY_EDIT_SAVE_BLOCK, tmp);
2785 g_free (tmp);
2786 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2787 if (exp)
2789 if (!*exp)
2791 g_free (exp);
2792 return 0;
2794 else
2796 if (edit_save_block (edit, exp, start_mark, end_mark))
2798 g_free (exp);
2799 edit->force |= REDRAW_COMPLETELY;
2800 return 1;
2802 else
2804 g_free (exp);
2805 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
2809 edit->force |= REDRAW_COMPLETELY;
2810 return 0;
2814 /* --------------------------------------------------------------------------------------------- */
2815 /** returns TRUE on success */
2817 gboolean
2818 edit_insert_file_cmd (WEdit * edit)
2820 char *tmp;
2821 char *exp;
2822 gboolean ret = FALSE;
2824 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
2825 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
2826 MC_HISTORY_EDIT_INSERT_FILE, tmp);
2827 g_free (tmp);
2829 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2831 if (exp != NULL && *exp != '\0')
2833 vfs_path_t *exp_vpath;
2835 exp_vpath = vfs_path_from_str (exp);
2836 ret = (edit_insert_file (edit, exp_vpath) >= 0);
2837 vfs_path_free (exp_vpath);
2839 if (!ret)
2840 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
2843 g_free (exp);
2845 edit->force |= REDRAW_COMPLETELY;
2846 return ret;
2849 /* --------------------------------------------------------------------------------------------- */
2850 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
2853 edit_sort_cmd (WEdit * edit)
2855 static char *old = 0;
2856 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
2857 long start_mark, end_mark;
2858 int e;
2860 if (eval_marks (edit, &start_mark, &end_mark))
2862 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
2863 return 0;
2866 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
2867 edit_save_block (edit, tmp, start_mark, end_mark);
2868 g_free (tmp);
2870 exp = input_dialog (_("Run sort"),
2871 _("Enter sort options (see manpage) separated by whitespace:"),
2872 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "");
2874 if (!exp)
2875 return 1;
2876 g_free (old);
2877 old = exp;
2878 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
2879 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
2880 tmp =
2881 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
2882 " > ", tmp_edit_temp_name, (char *) NULL);
2883 g_free (tmp_edit_temp_name);
2884 g_free (tmp_edit_block_name);
2886 e = system (tmp);
2887 g_free (tmp);
2888 if (e)
2890 if (e == -1 || e == 127)
2892 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
2894 else
2896 char q[8];
2897 sprintf (q, "%d ", e);
2898 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
2899 edit_error_dialog (_("Sort"), tmp);
2900 g_free (tmp);
2902 return -1;
2905 edit->force |= REDRAW_COMPLETELY;
2907 if (edit_block_delete_cmd (edit))
2908 return 1;
2911 vfs_path_t *tmp_vpath;
2913 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
2914 edit_insert_file (edit, tmp_vpath);
2915 vfs_path_free (tmp_vpath);
2917 return 0;
2920 /* --------------------------------------------------------------------------------------------- */
2922 * Ask user for a command, execute it and paste its output back to the
2923 * editor.
2927 edit_ext_cmd (WEdit * edit)
2929 char *exp, *tmp, *tmp_edit_temp_file;
2930 int e;
2932 exp =
2933 input_dialog (_("Paste output of external command"),
2934 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL);
2936 if (!exp)
2937 return 1;
2939 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
2940 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
2941 g_free (tmp_edit_temp_file);
2942 e = system (tmp);
2943 g_free (tmp);
2944 g_free (exp);
2946 if (e)
2948 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
2949 return -1;
2952 edit->force |= REDRAW_COMPLETELY;
2955 vfs_path_t *tmp_vpath;
2957 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
2958 edit_insert_file (edit, tmp_vpath);
2959 vfs_path_free (tmp_vpath);
2961 return 0;
2964 /* --------------------------------------------------------------------------------------------- */
2965 /** if block is 1, a block must be highlighted and the shell command
2966 processes it. If block is 0 the shell command is a straight system
2967 command, that just produces some output which is to be inserted */
2969 void
2970 edit_block_process_cmd (WEdit * edit, int macro_number)
2972 char *fname;
2973 char *macros_fname = NULL;
2975 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
2976 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
2977 user_menu (edit, macros_fname, 0);
2978 g_free (fname);
2979 g_free (macros_fname);
2980 edit->force |= REDRAW_COMPLETELY;
2983 /* --------------------------------------------------------------------------------------------- */
2985 void
2986 edit_mail_dialog (WEdit * edit)
2988 char *tmail_to;
2989 char *tmail_subject;
2990 char *tmail_cc;
2992 static char *mail_cc_last = 0;
2993 static char *mail_subject_last = 0;
2994 static char *mail_to_last = 0;
2996 QuickWidget quick_widgets[] = {
2997 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
2998 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
2999 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input", &tmail_cc),
3000 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT, N_("Copies to")),
3001 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-2",
3002 &tmail_subject),
3003 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT, N_("Subject")),
3004 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-3", &tmail_to),
3005 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT, N_("To")),
3006 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT, N_("mail -s <subject> -c <cc> <to>")),
3007 QUICK_END
3010 QuickDialog Quick_input = {
3011 50, MAIL_DLG_HEIGHT, -1, -1, N_("Mail"),
3012 "[Input Line Keys]", quick_widgets, NULL, FALSE
3015 quick_widgets[2].u.input.text = mail_cc_last ? mail_cc_last : "";
3016 quick_widgets[4].u.input.text = mail_subject_last ? mail_subject_last : "";
3017 quick_widgets[6].u.input.text = mail_to_last ? mail_to_last : "";
3019 if (quick_dialog (&Quick_input) != B_CANCEL)
3021 g_free (mail_cc_last);
3022 g_free (mail_subject_last);
3023 g_free (mail_to_last);
3024 mail_cc_last = tmail_cc;
3025 mail_subject_last = tmail_subject;
3026 mail_to_last = tmail_to;
3027 pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
3032 /*******************/
3033 /* Word Completion */
3034 /*******************/
3036 /* --------------------------------------------------------------------------------------------- */
3038 * Complete current word using regular expression search
3039 * backwards beginning at the current cursor position.
3042 void
3043 edit_complete_word_cmd (WEdit * edit)
3045 gsize i, max_len, word_len = 0, num_compl = 0;
3046 long word_start = 0;
3047 unsigned char *bufpos;
3048 char *match_expr;
3049 struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
3051 /* search start of word to be completed */
3052 if (!edit_find_word_start (edit, &word_start, &word_len))
3053 return;
3055 /* prepare match expression */
3056 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3058 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3059 match_expr =
3060 g_strdup_printf
3061 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3062 (int) word_len, bufpos);
3064 /* collect the possible completions */
3065 /* start search from begin to end of file */
3066 max_len =
3067 edit_collect_completions (edit, word_start, word_len, match_expr,
3068 (struct selection *) &compl, &num_compl);
3070 if (num_compl > 0)
3072 /* insert completed word if there is only one match */
3073 if (num_compl == 1)
3075 for (i = word_len; i < compl[0].len; i++)
3076 edit_insert (edit, *(compl[0].text + i));
3078 /* more than one possible completion => ask the user */
3079 else
3081 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3082 /* !!! pressed again the selection dialog pops up, but that !!! */
3083 /* !!! seems to require a further internal state !!! */
3084 /*tty_beep (); */
3086 /* let the user select the preferred completion */
3087 editcmd_dialog_completion_show (edit, max_len, word_len,
3088 (struct selection *) &compl, num_compl);
3092 g_free (match_expr);
3093 /* release memory before return */
3094 for (i = 0; i < num_compl; i++)
3095 g_free (compl[i].text);
3098 /* --------------------------------------------------------------------------------------------- */
3100 void
3101 edit_select_codepage_cmd (WEdit * edit)
3103 #ifdef HAVE_CHARSET
3104 if (do_select_codepage ())
3105 edit_set_codeset (edit);
3107 edit->force = REDRAW_COMPLETELY;
3108 edit_refresh_cmd (edit);
3109 #else
3110 (void) edit;
3111 #endif
3114 /* --------------------------------------------------------------------------------------------- */
3116 void
3117 edit_insert_literal_cmd (WEdit * edit)
3119 int char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3120 _("Press any key:"), 0);
3121 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3124 /* --------------------------------------------------------------------------------------------- */
3126 void
3127 edit_begin_end_macro_cmd (WEdit * edit)
3129 /* edit is a pointer to the widget */
3130 if (edit != NULL)
3132 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3133 edit_execute_key_command (edit, command, -1);
3137 /* --------------------------------------------------------------------------------------------- */
3139 void
3140 edit_begin_end_repeat_cmd (WEdit * edit)
3142 /* edit is a pointer to the widget */
3143 if (edit != NULL)
3145 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3146 edit_execute_key_command (edit, command, -1);
3150 /* --------------------------------------------------------------------------------------------- */
3153 edit_load_forward_cmd (WEdit * edit)
3155 if (edit->modified)
3157 if (edit_query_dialog2
3158 (_("Warning"),
3159 _("Current text was modified without a file save\n"
3160 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3162 edit->force |= REDRAW_COMPLETELY;
3163 return 0;
3166 if (edit_stack_iterator + 1 < MAX_HISTORY_MOVETO)
3168 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3170 return 1;
3172 edit_stack_iterator++;
3173 if (edit_history_moveto[edit_stack_iterator].filename_vpath)
3175 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3176 edit_history_moveto[edit_stack_iterator].line);
3177 return 0;
3179 else
3181 return 1;
3184 else
3186 return 1;
3190 /* --------------------------------------------------------------------------------------------- */
3193 edit_load_back_cmd (WEdit * edit)
3195 if (edit->modified)
3197 if (edit_query_dialog2
3198 (_("Warning"),
3199 _("Current text was modified without a file save\n"
3200 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3202 edit->force |= REDRAW_COMPLETELY;
3203 return 0;
3206 if (edit_stack_iterator > 0)
3208 edit_stack_iterator--;
3209 if (edit_history_moveto[edit_stack_iterator].filename_vpath)
3211 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3212 edit_history_moveto[edit_stack_iterator].line);
3213 return 0;
3215 else
3217 return 1;
3220 else
3222 return 1;
3226 /* --------------------------------------------------------------------------------------------- */
3228 void
3229 edit_get_match_keyword_cmd (WEdit * edit)
3231 gsize word_len = 0, max_len = 0;
3232 int num_def = 0;
3233 int i;
3234 long word_start = 0;
3235 unsigned char *bufpos;
3236 char *match_expr;
3237 char *path = NULL;
3238 char *ptr = NULL;
3239 char *tagfile = NULL;
3241 etags_hash_t def_hash[MAX_DEFINITIONS];
3243 for (i = 0; i < MAX_DEFINITIONS; i++)
3245 def_hash[i].filename = NULL;
3248 /* search start of word to be completed */
3249 if (!edit_find_word_start (edit, &word_start, &word_len))
3250 return;
3252 /* prepare match expression */
3253 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3254 match_expr = g_strdup_printf ("%.*s", (int) word_len, bufpos);
3256 ptr = g_get_current_dir ();
3257 path = g_strconcat (ptr, G_DIR_SEPARATOR_S, (char *) NULL);
3258 g_free (ptr);
3260 /* Recursive search file 'TAGS' in parent dirs */
3263 ptr = g_path_get_dirname (path);
3264 g_free (path);
3265 path = ptr;
3266 g_free (tagfile);
3267 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3268 if (exist_file (tagfile))
3269 break;
3271 while (strcmp (path, G_DIR_SEPARATOR_S) != 0);
3273 if (tagfile)
3275 num_def =
3276 etags_set_definition_hash (tagfile, path, match_expr, (etags_hash_t *) & def_hash);
3277 g_free (tagfile);
3279 g_free (path);
3281 max_len = MAX_WIDTH_DEF_DIALOG;
3282 word_len = 0;
3283 if (num_def > 0)
3285 editcmd_dialog_select_definition_show (edit, match_expr, max_len, word_len,
3286 (etags_hash_t *) & def_hash, num_def);
3288 g_free (match_expr);
3291 /* --------------------------------------------------------------------------------------------- */