Added new fnction for manipulate vpath objects:
[midnight-commander.git] / src / editor / editcmd.c
blob8e78e71029ce90933fbc5b1861a6dfe6c7bfb3f3
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 char *filename)
123 char *p;
124 gchar *tmp;
125 long filelen = 0;
126 gchar *real_filename;
127 int this_save_mode, fd = -1;
128 vfs_path_t *real_filename_vpath;
129 vfs_path_t *savename_vpath = NULL;
131 if (!filename)
132 return 0;
133 if (!*filename)
134 return 0;
136 if (*filename != PATH_SEP && edit->dir)
138 real_filename = concat_dir_and_file (edit->dir, filename);
140 else
142 real_filename = g_strdup (filename);
144 real_filename_vpath = vfs_path_from_str (real_filename);
146 this_save_mode = option_save_mode;
147 if (this_save_mode != EDIT_QUICK_SAVE)
149 if (!vfs_file_is_local (real_filename_vpath)
150 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
153 * The file does not exists yet, so no safe save or
154 * backup are necessary.
156 this_save_mode = EDIT_QUICK_SAVE;
158 if (fd != -1)
159 mc_close (fd);
162 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt)
164 int rv;
165 struct stat sb;
167 rv = mc_stat (real_filename_vpath, &sb);
168 if (rv == 0 && sb.st_nlink > 1)
170 rv = edit_query_dialog3 (_("Warning"),
171 _("File has hard-links. Detach before saving?"),
172 _("&Yes"), _("&No"), _("&Cancel"));
173 switch (rv)
175 case 0:
176 this_save_mode = EDIT_SAFE_SAVE;
177 /* fallthrough */
178 case 1:
179 edit->skip_detach_prompt = 1;
180 break;
181 default:
182 g_free (real_filename);
183 vfs_path_free (real_filename_vpath);
184 return -1;
188 /* Prevent overwriting changes from other editor sessions. */
189 if (rv == 0 && edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
192 /* The default action is "Cancel". */
193 query_set_sel (1);
195 rv = edit_query_dialog2 (_("Warning"),
196 _("The file has been modified in the meantime. Save anyway?"),
197 _("&Yes"), _("&Cancel"));
198 if (rv != 0)
200 g_free (real_filename);
201 vfs_path_free (real_filename_vpath);
202 return -1;
207 if (this_save_mode != EDIT_QUICK_SAVE)
209 char *savedir, *saveprefix;
210 const char *slashpos;
211 slashpos = strrchr (real_filename, PATH_SEP);
212 if (slashpos)
214 savedir = g_strdup (real_filename);
215 savedir[slashpos - real_filename + 1] = '\0';
217 else
218 savedir = g_strdup (".");
219 saveprefix = mc_build_filename (savedir, "cooledit", NULL);
220 g_free (savedir);
221 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
222 g_free (saveprefix);
223 if (savename_vpath == NULL)
225 g_free (real_filename);
226 vfs_path_free (real_filename_vpath);
227 return 0;
229 /* FIXME:
230 * Close for now because mc_mkstemps use pure open system call
231 * to create temporary file and it needs to be reopened by
232 * VFS-aware mc_open().
234 close (fd);
236 else
237 savename_vpath = vfs_path_from_str (real_filename);
239 (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
240 (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
242 fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
243 if (fd == -1)
244 goto error_save;
246 /* pipe save */
247 p = edit_get_write_filter (savename_vpath, real_filename);
248 if (p != NULL)
250 FILE *file;
252 mc_close (fd);
253 file = (FILE *) popen (p, "w");
255 if (file)
257 filelen = edit_write_stream (edit, file);
258 #if 1
259 pclose (file);
260 #else
261 if (pclose (file) != 0)
263 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
264 edit_error_dialog (_("Error"), tmp);
265 g_free (tmp);
266 g_free (p);
267 goto error_save;
269 #endif
271 else
273 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
274 edit_error_dialog (_("Error"), get_sys_error (tmp));
275 g_free (p);
276 g_free (tmp);
277 goto error_save;
279 g_free (p);
281 else if (edit->lb == LB_ASIS)
282 { /* do not change line breaks */
283 long buf;
284 buf = 0;
285 filelen = edit->last_byte;
286 while (buf <= (edit->curs1 >> S_EDIT_BUF_SIZE) - 1)
288 if (mc_write (fd, (char *) edit->buffers1[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
290 mc_close (fd);
291 goto error_save;
293 buf++;
295 if (mc_write
296 (fd, (char *) edit->buffers1[buf],
297 edit->curs1 & M_EDIT_BUF_SIZE) != (edit->curs1 & M_EDIT_BUF_SIZE))
299 filelen = -1;
301 else if (edit->curs2)
303 edit->curs2--;
304 buf = (edit->curs2 >> S_EDIT_BUF_SIZE);
305 if (mc_write
306 (fd,
307 (char *) edit->buffers2[buf] + EDIT_BUF_SIZE -
308 (edit->curs2 & M_EDIT_BUF_SIZE) - 1,
309 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) != 1 + (edit->curs2 & M_EDIT_BUF_SIZE))
311 filelen = -1;
313 else
315 while (--buf >= 0)
317 if (mc_write (fd, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
319 filelen = -1;
320 break;
324 edit->curs2++;
326 if (mc_close (fd))
327 goto error_save;
329 /* Update the file information, especially the mtime. */
330 if (mc_stat (savename_vpath, &edit->stat1) == -1)
331 goto error_save;
333 else
334 { /* change line breaks */
335 FILE *file;
336 vfs_path_element_t *path_element;
338 mc_close (fd);
340 path_element = vfs_path_get_by_index (savename_vpath, -1);
341 file = (FILE *) fopen (path_element->path, "w");
342 if (file != NULL)
344 filelen = edit_write_stream (edit, file);
345 fclose (file);
347 else
349 char *msg;
351 msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
352 edit_error_dialog (_("Error"), msg);
353 g_free (msg);
354 goto error_save;
358 if (filelen != edit->last_byte)
359 goto error_save;
361 if (this_save_mode == EDIT_DO_BACKUP)
363 vfs_path_t *tmp_vpath;
364 gboolean ok;
366 #ifdef HAVE_ASSERT_H
367 assert (option_backup_ext != NULL);
368 #endif
369 tmp_vpath = vfs_path_append_new (real_filename_vpath, option_backup_ext, (char *) NULL);
370 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
371 vfs_path_free (tmp_vpath);
372 if (!ok)
373 goto error_save;
376 if (this_save_mode != EDIT_QUICK_SAVE)
377 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
378 goto error_save;
380 g_free (real_filename);
381 vfs_path_free (real_filename_vpath);
382 vfs_path_free (savename_vpath);
383 return 1;
384 error_save:
385 /* FIXME: Is this safe ?
386 * if (this_save_mode != EDIT_QUICK_SAVE)
387 * mc_unlink (savename);
389 g_free (real_filename);
390 vfs_path_free (real_filename_vpath);
391 vfs_path_free (savename_vpath);
392 return 0;
395 /* --------------------------------------------------------------------------------------------- */
397 static gboolean
398 edit_check_newline (WEdit * edit)
400 return !(option_check_nl_at_eof && edit->last_byte > 0
401 && edit_get_byte (edit, edit->last_byte - 1) != '\n'
402 && edit_query_dialog2 (_("Warning"),
403 _("The file you are saving is not finished with a newline"),
404 _("C&ontinue"), _("&Cancel")));
407 /* --------------------------------------------------------------------------------------------- */
409 static char *
410 edit_get_save_file_as (WEdit * edit)
412 #define DLG_WIDTH 64
413 #define DLG_HEIGHT 14
415 static LineBreaks cur_lb = LB_ASIS;
417 char *filename = edit->filename;
419 const char *lb_names[LB_NAMES] = {
420 N_("&Do not change"),
421 N_("&Unix format (LF)"),
422 N_("&Windows/DOS format (CR LF)"),
423 N_("&Macintosh format (CR)")
426 QuickWidget quick_widgets[] = {
427 QUICK_BUTTON (6, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
428 QUICK_BUTTON (2, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
429 QUICK_RADIO (5, DLG_WIDTH, DLG_HEIGHT - 8, DLG_HEIGHT, LB_NAMES, lb_names, (int *) &cur_lb),
430 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 9, DLG_HEIGHT, N_("Change line breaks to:")),
431 QUICK_INPUT (3, DLG_WIDTH, DLG_HEIGHT - 11, DLG_HEIGHT, filename, DLG_WIDTH - 6, 0,
432 "save-as", &filename),
433 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 12, DLG_HEIGHT, N_("Enter file name:")),
434 QUICK_END
437 QuickDialog Quick_options = {
438 DLG_WIDTH, DLG_HEIGHT, -1, -1,
439 N_("Save As"), "[Save File As]",
440 quick_widgets, NULL, FALSE
443 if (quick_dialog (&Quick_options) != B_CANCEL)
445 char *fname;
447 edit->lb = cur_lb;
448 fname = tilde_expand (filename);
449 g_free (filename);
450 return fname;
453 return NULL;
455 #undef DLG_WIDTH
456 #undef DLG_HEIGHT
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 = edit_lock_file (edit);
468 res = edit_save_file (edit, edit->filename);
470 /* Maintain modify (not save) lock on failure */
471 if ((res > 0 && edit->locked) || save_lock)
472 edit->locked = edit_unlock_file (edit);
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, char *exp)
493 int prev_locked = edit->locked;
494 char *prev_filename = g_strdup (edit->filename);
496 if (!edit_reload (edit, exp))
498 g_free (prev_filename);
499 return 1;
502 if (prev_locked)
504 char *fullpath;
506 fullpath = mc_build_filename (edit->dir, prev_filename, (char *) NULL);
507 unlock_file (fullpath);
508 g_free (fullpath);
510 g_free (prev_filename);
511 return 0;
514 /* --------------------------------------------------------------------------------------------- */
516 static void
517 edit_load_syntax_file (WEdit * edit)
519 char *extdir;
520 int dir = 0;
522 if (geteuid () == 0)
524 dir = query_dialog (_("Syntax file edit"),
525 _("Which syntax file you want to edit?"), D_NORMAL, 2,
526 _("&User"), _("&System Wide"));
529 extdir = g_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
530 if (!exist_file (extdir))
532 g_free (extdir);
533 extdir = g_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
536 if (dir == 0)
538 char *buffer;
540 buffer = mc_config_get_full_path (EDIT_SYNTAX_FILE);
541 check_for_default (extdir, buffer);
542 edit_load_file_from_filename (edit, buffer);
543 g_free (buffer);
545 else if (dir == 1)
546 edit_load_file_from_filename (edit, extdir);
548 g_free (extdir);
551 /* --------------------------------------------------------------------------------------------- */
553 static void
554 edit_load_menu_file (WEdit * edit)
556 char *buffer;
557 char *menufile;
558 int dir = 0;
560 dir = query_dialog (_("Menu edit"),
561 _("Which menu file do you want to edit?"), D_NORMAL,
562 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
564 menufile = concat_dir_and_file (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU);
566 if (!exist_file (menufile))
568 g_free (menufile);
569 menufile = concat_dir_and_file (mc_global.share_data_dir, EDIT_GLOBAL_MENU);
572 switch (dir)
574 case 0:
575 buffer = g_strdup (EDIT_LOCAL_MENU);
576 check_for_default (menufile, buffer);
577 chmod (buffer, 0600);
578 break;
580 case 1:
581 buffer = mc_config_get_full_path (EDIT_HOME_MENU);
582 check_for_default (menufile, buffer);
583 break;
585 case 2:
586 buffer = concat_dir_and_file (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU);
587 if (!exist_file (buffer))
589 g_free (buffer);
590 buffer = concat_dir_and_file (mc_global.share_data_dir, EDIT_GLOBAL_MENU);
592 break;
594 default:
595 g_free (menufile);
596 return;
599 edit_load_file_from_filename (edit, buffer);
601 g_free (buffer);
602 g_free (menufile);
605 /* --------------------------------------------------------------------------------------------- */
607 static void
608 edit_delete_column_of_text (WEdit * edit)
610 long p, q, r, m1, m2;
611 long b, c, d, n;
613 eval_marks (edit, &m1, &m2);
614 n = edit_move_forward (edit, m1, 0, m2) + 1;
615 c = edit_move_forward3 (edit, edit_bol (edit, m1), 0, m1);
616 d = edit_move_forward3 (edit, edit_bol (edit, m2), 0, m2);
617 b = max (min (c, d), min (edit->column1, edit->column2));
618 c = max (c, max (edit->column1, edit->column2));
620 while (n--)
622 r = edit_bol (edit, edit->curs1);
623 p = edit_move_forward3 (edit, r, b, 0);
624 q = edit_move_forward3 (edit, r, c, 0);
625 if (p < m1)
626 p = m1;
627 if (q > m2)
628 q = m2;
629 edit_cursor_move (edit, p - edit->curs1);
630 while (q > p)
632 /* delete line between margins */
633 if (edit_get_byte (edit, edit->curs1) != '\n')
634 edit_delete (edit, 1);
635 q--;
637 if (n)
638 /* move to next line except on the last delete */
639 edit_cursor_move (edit, edit_move_forward (edit, edit->curs1, 1, 0) - edit->curs1);
643 /* --------------------------------------------------------------------------------------------- */
644 /** if success return 0 */
646 static int
647 edit_block_delete (WEdit * edit)
649 long count;
650 long start_mark, end_mark;
651 int curs_pos, line_width;
652 long curs_line, c1, c2;
654 if (eval_marks (edit, &start_mark, &end_mark))
655 return 0;
656 if (edit->column_highlight && edit->mark2 < 0)
657 edit_mark_cmd (edit, 0);
658 if ((end_mark - start_mark) > option_max_undo / 2)
660 /* Warning message with a query to continue or cancel the operation */
661 if (edit_query_dialog2
662 (_("Warning"),
664 ("Block is large, you may not be able to undo this action"),
665 _("C&ontinue"), _("&Cancel")))
667 return 1;
670 c1 = min (edit->column1, edit->column2);
671 c2 = max (edit->column1, edit->column2);
672 edit->column1 = c1;
673 edit->column2 = c2;
675 edit_push_markers (edit);
677 curs_line = edit->curs_line;
679 /* calculate line width and cursor position before cut */
680 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
681 edit_eol (edit, edit->curs1));
682 curs_pos = edit->curs_col + edit->over_col;
684 /* move cursor to start of selection */
685 edit_cursor_move (edit, start_mark - edit->curs1);
686 edit_scroll_screen_over_cursor (edit);
687 count = start_mark;
688 if (start_mark < end_mark)
690 if (edit->column_highlight)
692 if (edit->mark2 < 0)
693 edit_mark_cmd (edit, 0);
694 edit_delete_column_of_text (edit);
695 /* move cursor to the saved position */
696 edit_move_to_line (edit, curs_line);
697 /* calculate line width after cut */
698 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
699 edit_eol (edit, edit->curs1));
700 if (option_cursor_beyond_eol && curs_pos > line_width)
701 edit->over_col = curs_pos - line_width;
703 else
705 while (count < end_mark)
707 edit_delete (edit, 1);
708 count++;
712 edit_set_markers (edit, 0, 0, 0, 0);
713 edit->force |= REDRAW_PAGE;
714 return 0;
717 /* --------------------------------------------------------------------------------------------- */
719 * Get EOL symbol for searching.
721 * @param edit editor object
722 * @return EOL symbol
725 static inline char
726 edit_search_get_current_end_line_char (const WEdit * edit)
728 switch (edit->lb)
730 case LB_MAC:
731 return '\r';
732 default:
733 return '\n';
737 /* --------------------------------------------------------------------------------------------- */
739 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
741 * @param search search object
742 * @return result of checks.
745 static edit_search_line_t
746 edit_get_search_line_type (mc_search_t * search)
748 edit_search_line_t search_line_type = 0;
750 if (search->search_type != MC_SEARCH_T_REGEX)
751 return search_line_type;
753 if (*search->original == '^')
754 search_line_type |= AT_START_LINE;
756 if (search->original[search->original_len - 1] == '$')
757 search_line_type |= AT_END_LINE;
758 return search_line_type;
761 /* --------------------------------------------------------------------------------------------- */
763 * Calculating the start position of next line.
765 * @param edit editor object
766 * @param current_pos current position
767 * @param max_pos max position
768 * @param end_string_symbol end of line symbol
769 * @return start position of next line
772 static off_t
773 edit_calculate_start_of_next_line (WEdit * edit, off_t current_pos, off_t max_pos,
774 char end_string_symbol)
776 off_t i;
778 for (i = current_pos; i < max_pos; i++)
780 current_pos++;
781 if (edit_get_byte (edit, i) == end_string_symbol)
782 break;
785 return current_pos;
788 /* --------------------------------------------------------------------------------------------- */
790 * Calculating the end position of previous line.
792 * @param edit editor object
793 * @param current_pos current position
794 * @param end_string_symbol end of line symbol
795 * @return end position of previous line
798 static off_t
799 edit_calculate_end_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
801 off_t i;
803 for (i = current_pos - 1; i >= 0; i--)
804 if (edit_get_byte (edit, i) == end_string_symbol)
805 break;
807 return i;
810 /* --------------------------------------------------------------------------------------------- */
812 * Calculating the start position of previous line.
814 * @param edit editor object
815 * @param current_pos current position
816 * @param end_string_symbol end of line symbol
817 * @return start position of previous line
820 static inline off_t
821 edit_calculate_start_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
823 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
824 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
826 return (current_pos + 1);
829 /* --------------------------------------------------------------------------------------------- */
831 * Calculating the start position of current line.
833 * @param edit editor object
834 * @param current_pos current position
835 * @param end_string_symbol end of line symbol
836 * @return start position of current line
839 static inline off_t
840 edit_calculate_start_of_current_line (WEdit * edit, off_t current_pos, char end_string_symbol)
842 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
844 return (current_pos + 1);
847 /* --------------------------------------------------------------------------------------------- */
849 * Fixing (if needed) search start position if 'only in selection' option present.
851 * @param edit editor object
854 static void
855 edit_search_fix_search_start_if_selection (WEdit * edit)
857 long start_mark = 0;
858 long end_mark = 0;
860 if (!edit_search_options.only_in_selection)
861 return;
863 if (eval_marks (edit, &start_mark, &end_mark) != 0)
864 return;
866 if (edit_search_options.backwards)
868 if (edit->search_start > end_mark || edit->search_start <= start_mark)
869 edit->search_start = end_mark;
871 else
873 if (edit->search_start < start_mark || edit->search_start >= end_mark)
874 edit->search_start = start_mark;
878 /* --------------------------------------------------------------------------------------------- */
880 static gboolean
881 editcmd_find (WEdit * edit, gsize * len)
883 off_t search_start = edit->search_start;
884 off_t search_end;
885 long start_mark = 0;
886 long end_mark = edit->last_byte;
887 int mark_res = 0;
888 char end_string_symbol;
890 end_string_symbol = edit_search_get_current_end_line_char (edit);
892 /* prepare for search */
893 if (edit_search_options.only_in_selection)
895 mark_res = eval_marks (edit, &start_mark, &end_mark);
896 if (mark_res != 0)
898 edit->search->error = MC_SEARCH_E_NOTFOUND;
899 edit->search->error_str = g_strdup (_("Search string not found"));
900 return FALSE;
903 /* fix the start and the end of search block positions */
904 if ((edit->search_line_type & AT_START_LINE) != 0
905 && (start_mark != 0 || edit_get_byte (edit, start_mark - 1) != end_string_symbol))
907 start_mark =
908 edit_calculate_start_of_next_line (edit, start_mark, edit->last_byte,
909 end_string_symbol);
911 if ((edit->search_line_type & AT_END_LINE) != 0
912 && (end_mark - 1 != edit->last_byte
913 || edit_get_byte (edit, end_mark) != end_string_symbol))
915 end_mark = edit_calculate_end_of_previous_line (edit, end_mark, end_string_symbol);
917 if (start_mark >= end_mark)
919 edit->search->error = MC_SEARCH_E_NOTFOUND;
920 edit->search->error_str = g_strdup (_("Search string not found"));
921 return FALSE;
924 else
926 if (edit_search_options.backwards)
927 end_mark = max (1, edit->curs1) - 1;
930 /* search */
931 if (edit_search_options.backwards)
933 /* backward search */
934 search_end = end_mark;
936 if ((edit->search_line_type & AT_START_LINE) != 0)
937 search_start =
938 edit_calculate_start_of_current_line (edit, search_start, end_string_symbol);
940 while ((int) search_start >= start_mark)
942 if (search_end > (off_t) (search_start + edit->search->original_len)
943 && mc_search_is_fixed_search_str (edit->search))
945 search_end = search_start + edit->search->original_len;
947 if (mc_search_run (edit->search, (void *) edit, search_start, search_end, len)
948 && edit->search->normal_offset == search_start)
950 return TRUE;
954 if ((edit->search_line_type & AT_START_LINE) != 0)
955 search_start =
956 edit_calculate_start_of_previous_line (edit, search_start, end_string_symbol);
957 else
958 search_start--;
960 edit->search->error_str = g_strdup (_("Search string not found"));
962 else
964 /* forward search */
965 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
966 search_start =
967 edit_calculate_start_of_next_line (edit, search_start, end_mark, end_string_symbol);
968 return mc_search_run (edit->search, (void *) edit, search_start, end_mark, len);
970 return FALSE;
973 /* --------------------------------------------------------------------------------------------- */
975 static char *
976 edit_replace_cmd__conv_to_display (char *str)
978 #ifdef HAVE_CHARSET
979 GString *tmp;
981 tmp = str_convert_to_display (str);
982 if (tmp != NULL)
984 if (tmp->len != 0)
985 return g_string_free (tmp, FALSE);
986 g_string_free (tmp, TRUE);
988 #endif
989 return g_strdup (str);
992 /* --------------------------------------------------------------------------------------------- */
994 static char *
995 edit_replace_cmd__conv_to_input (char *str)
997 #ifdef HAVE_CHARSET
998 GString *tmp;
1000 tmp = str_convert_to_input (str);
1001 if (tmp != NULL)
1003 if (tmp->len != 0)
1004 return g_string_free (tmp, FALSE);
1005 g_string_free (tmp, TRUE);
1007 #endif
1008 return g_strdup (str);
1011 /* --------------------------------------------------------------------------------------------- */
1013 static void
1014 edit_do_search (WEdit * edit)
1016 gsize len = 0;
1018 if (edit->search == NULL)
1019 edit->search_start = edit->curs1;
1021 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1023 if (search_create_bookmark)
1025 int found = 0, books = 0;
1026 long l = 0, l_last = -1;
1027 long q = 0;
1029 search_create_bookmark = FALSE;
1030 book_mark_flush (edit, -1);
1032 while (TRUE)
1034 if (!mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
1035 break;
1036 if (found == 0)
1037 edit->search_start = edit->search->normal_offset;
1038 found++;
1039 l += edit_count_lines (edit, q, edit->search->normal_offset);
1040 if (l != l_last)
1042 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
1043 books++;
1045 l_last = l;
1046 q = edit->search->normal_offset + 1;
1049 if (found == 0)
1050 edit_error_dialog (_("Search"), _("Search string not found"));
1051 else
1052 edit_cursor_move (edit, edit->search_start - edit->curs1);
1054 else
1056 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
1057 && edit_search_options.backwards)
1058 edit->search_start--;
1060 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
1061 && !edit_search_options.backwards)
1062 edit->search_start++;
1064 if (editcmd_find (edit, &len))
1066 edit->found_start = edit->search_start = edit->search->normal_offset;
1067 edit->found_len = len;
1068 edit->over_col = 0;
1069 edit_cursor_move (edit, edit->search_start - edit->curs1);
1070 edit_scroll_screen_over_cursor (edit);
1071 if (edit_search_options.backwards)
1072 edit->search_start--;
1073 else
1074 edit->search_start++;
1076 else
1078 edit->search_start = edit->curs1;
1079 if (edit->search->error_str != NULL)
1080 edit_error_dialog (_("Search"), edit->search->error_str);
1084 edit->force |= REDRAW_COMPLETELY;
1085 edit_scroll_screen_over_cursor (edit);
1088 /* --------------------------------------------------------------------------------------------- */
1090 static void
1091 edit_search (WEdit * edit)
1093 if (editcmd_dialog_search_show (edit))
1095 edit->search_line_type = edit_get_search_line_type (edit->search);
1096 edit_search_fix_search_start_if_selection (edit);
1097 edit_do_search (edit);
1101 /* --------------------------------------------------------------------------------------------- */
1102 /** Return a null terminated length of text. Result must be g_free'd */
1104 static unsigned char *
1105 edit_get_block (WEdit * edit, long start, long finish, int *l)
1107 unsigned char *s, *r;
1108 r = s = g_malloc0 (finish - start + 1);
1109 if (edit->column_highlight)
1111 *l = 0;
1112 /* copy from buffer, excluding chars that are out of the column 'margins' */
1113 while (start < finish)
1115 int c;
1116 long x;
1117 x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
1118 c = edit_get_byte (edit, start);
1119 if ((x >= edit->column1 && x < edit->column2)
1120 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1122 *s++ = c;
1123 (*l)++;
1125 start++;
1128 else
1130 *l = finish - start;
1131 while (start < finish)
1132 *s++ = edit_get_byte (edit, start++);
1134 *s = 0;
1135 return r;
1138 /* --------------------------------------------------------------------------------------------- */
1139 /** copies a block to clipboard file */
1141 static int
1142 edit_save_block_to_clip_file (WEdit * edit, long start, long finish)
1144 int ret;
1145 gchar *tmp;
1146 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1147 ret = edit_save_block (edit, tmp, start, finish);
1148 g_free (tmp);
1149 return ret;
1152 /* --------------------------------------------------------------------------------------------- */
1154 static void
1155 pipe_mail (WEdit * edit, char *to, char *subject, char *cc)
1157 FILE *p = 0;
1158 char *s;
1160 to = name_quote (to, 0);
1161 subject = name_quote (subject, 0);
1162 cc = name_quote (cc, 0);
1163 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1164 g_free (to);
1165 g_free (subject);
1166 g_free (cc);
1168 if (s)
1170 p = popen (s, "w");
1171 g_free (s);
1174 if (p)
1176 long i;
1177 for (i = 0; i < edit->last_byte; i++)
1178 fputc (edit_get_byte (edit, i), p);
1179 pclose (p);
1183 /* --------------------------------------------------------------------------------------------- */
1185 static gboolean
1186 is_break_char (char c)
1188 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
1191 /* --------------------------------------------------------------------------------------------- */
1192 /** find first character of current word */
1194 static int
1195 edit_find_word_start (WEdit * edit, long *word_start, gsize * word_len)
1197 int c, last;
1198 gsize i;
1200 /* return if at begin of file */
1201 if (edit->curs1 <= 0)
1202 return 0;
1204 c = (unsigned char) edit_get_byte (edit, edit->curs1 - 1);
1205 /* return if not at end or in word */
1206 if (is_break_char (c))
1207 return 0;
1209 /* search start of word to be completed */
1210 for (i = 2;; i++)
1212 /* return if at begin of file */
1213 if ((gsize) edit->curs1 < i)
1214 return 0;
1216 last = c;
1217 c = (unsigned char) edit_get_byte (edit, edit->curs1 - i);
1219 if (is_break_char (c))
1221 /* return if word starts with digit */
1222 if (isdigit (last))
1223 return 0;
1225 *word_start = edit->curs1 - (i - 1); /* start found */
1226 *word_len = i - 1;
1227 break;
1230 /* success */
1231 return 1;
1234 /* --------------------------------------------------------------------------------------------- */
1236 * Get current word under cursor
1238 * @param edit editor object
1239 * @param srch mc_search object
1240 * @param word_start start word position
1242 * @return newly allocated string or NULL if no any words under cursor
1245 static char *
1246 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, long word_start)
1248 gsize len = 0, i;
1249 GString *temp;
1251 if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len))
1252 return NULL;
1254 temp = g_string_sized_new (len);
1256 for (i = 0; i < len; i++)
1258 int chr;
1260 chr = edit_get_byte (edit, word_start + i);
1261 if (!isspace (chr))
1262 g_string_append_c (temp, chr);
1265 return g_string_free (temp, temp->len == 0);
1268 /* --------------------------------------------------------------------------------------------- */
1269 /** collect the possible completions */
1271 static gsize
1272 edit_collect_completions (WEdit * edit, long word_start, gsize word_len,
1273 char *match_expr, struct selection *compl, gsize * num)
1275 gsize len = 0;
1276 gsize max_len = 0;
1277 gsize i;
1278 int skip;
1279 GString *temp;
1280 mc_search_t *srch;
1281 long last_byte, start = -1;
1282 char *current_word;
1284 srch = mc_search_new (match_expr, -1);
1285 if (srch == NULL)
1286 return 0;
1288 if (mc_config_get_bool
1289 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1291 last_byte = edit->last_byte;
1293 else
1295 last_byte = word_start;
1298 srch->search_type = MC_SEARCH_T_REGEX;
1299 srch->is_case_sensitive = TRUE;
1300 srch->search_fn = edit_search_cmd_callback;
1302 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1304 temp = g_string_new ("");
1306 /* collect max MAX_WORD_COMPLETIONS completions */
1307 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1309 g_string_set_size (temp, 0);
1310 start = srch->normal_offset;
1312 /* add matched completion if not yet added */
1313 for (i = 0; i < len; i++)
1315 skip = edit_get_byte (edit, start + i);
1316 if (isspace (skip))
1317 continue;
1319 /* skip current word */
1320 if (start + (long) i == word_start)
1321 break;
1323 g_string_append_c (temp, skip);
1326 if (temp->len == 0)
1327 continue;
1329 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1330 continue;
1332 skip = 0;
1334 for (i = 0; i < *num; i++)
1336 if (strncmp
1337 ((char *) &compl[i].text[word_len],
1338 (char *) &temp->str[word_len], max (len, compl[i].len) - word_len) == 0)
1340 struct selection this = compl[i];
1341 for (++i; i < *num; i++)
1343 compl[i - 1] = compl[i];
1345 compl[*num - 1] = this;
1346 skip = 1;
1347 break; /* skip it, already added */
1350 if (skip != 0)
1351 continue;
1353 if (*num == MAX_WORD_COMPLETIONS)
1355 g_free (compl[0].text);
1356 for (i = 1; i < *num; i++)
1358 compl[i - 1] = compl[i];
1360 (*num)--;
1362 #ifdef HAVE_CHARSET
1364 GString *recoded;
1365 recoded = str_convert_to_display (temp->str);
1367 if (recoded && recoded->len)
1368 g_string_assign (temp, recoded->str);
1370 g_string_free (recoded, TRUE);
1372 #endif
1373 compl[*num].text = g_strdup (temp->str);
1374 compl[*num].len = temp->len;
1375 (*num)++;
1376 start += len;
1378 /* note the maximal length needed for the completion dialog */
1379 if (len > max_len)
1380 max_len = len;
1383 mc_search_free (srch);
1384 g_string_free (temp, TRUE);
1385 g_free (current_word);
1387 return max_len;
1390 /* --------------------------------------------------------------------------------------------- */
1392 static void
1393 edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width,
1394 long *start_pos, long *end_pos, int *col1, int *col2)
1396 long cursor;
1397 int i, col;
1399 cursor = edit->curs1;
1400 col = edit_get_col (edit);
1402 for (i = 0; i < size; i++)
1404 if (data[i] != '\n')
1405 edit_insert (edit, data[i]);
1406 else
1407 { /* fill in and move to next line */
1408 int l;
1409 long p;
1411 if (edit_get_byte (edit, edit->curs1) != '\n')
1413 l = width - (edit_get_col (edit) - col);
1414 while (l > 0)
1416 edit_insert (edit, ' ');
1417 l -= space_width;
1420 for (p = edit->curs1;; p++)
1422 if (p == edit->last_byte)
1424 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1425 edit_insert_ahead (edit, '\n');
1426 p++;
1427 break;
1429 if (edit_get_byte (edit, p) == '\n')
1431 p++;
1432 break;
1435 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1436 l = col - edit_get_col (edit);
1437 while (l >= space_width)
1439 edit_insert (edit, ' ');
1440 l -= space_width;
1445 *col1 = col;
1446 *col2 = col + width;
1447 *start_pos = cursor;
1448 *end_pos = edit->curs1;
1449 edit_cursor_move (edit, cursor - edit->curs1);
1452 /* --------------------------------------------------------------------------------------------- */
1454 static int
1455 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1457 const macros_t *m1 = (const macros_t *) macro1;
1458 const macros_t *m2 = (const macros_t *) macro2;
1460 return m1->hotkey - m2->hotkey;
1463 /* --------------------------------------------------------------------------------------------- */
1465 static void
1466 edit_macro_sort_by_hotkey (void)
1468 if (macros_list != NULL && macros_list->len != 0)
1469 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1472 /* --------------------------------------------------------------------------------------------- */
1474 static gboolean
1475 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1477 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1478 macros_t *result;
1479 macros_t search_macro;
1481 (void) edit;
1483 search_macro.hotkey = hotkey;
1484 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1485 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1487 if (result != NULL && result->macro != NULL)
1489 *indx = (result - array_start);
1490 *macros = result;
1491 return TRUE;
1493 *indx = 0;
1494 return FALSE;
1497 /* --------------------------------------------------------------------------------------------- */
1498 /** returns FALSE on error */
1500 static gboolean
1501 edit_delete_macro (WEdit * edit, int hotkey)
1503 mc_config_t *macros_config = NULL;
1504 const char *section_name = "editor";
1505 gchar *macros_fname;
1506 guint indx;
1507 char *skeyname;
1508 const macros_t *macros = NULL;
1510 /* clear array of actions for current hotkey */
1511 while (edit_get_macro (edit, hotkey, &macros, &indx))
1513 if (macros->macro != NULL)
1514 g_array_free (macros->macro, TRUE);
1515 macros = NULL;
1516 g_array_remove_index (macros_list, indx);
1517 edit_macro_sort_by_hotkey ();
1520 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1521 macros_config = mc_config_init (macros_fname);
1522 g_free (macros_fname);
1524 if (macros_config == NULL)
1525 return FALSE;
1527 skeyname = lookup_key_by_code (hotkey);
1528 while (mc_config_del_key (macros_config, section_name, skeyname))
1530 g_free (skeyname);
1531 mc_config_save_file (macros_config, NULL);
1532 mc_config_deinit (macros_config);
1533 return TRUE;
1537 /* --------------------------------------------------------------------------------------------- */
1538 /*** public functions ****************************************************************************/
1539 /* --------------------------------------------------------------------------------------------- */
1541 void
1542 edit_help_cmd (WEdit * edit)
1544 ev_help_t event_data = { NULL, "[Internal File Editor]" };
1545 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
1547 edit->force |= REDRAW_COMPLETELY;
1550 /* --------------------------------------------------------------------------------------------- */
1552 void
1553 edit_refresh_cmd (WEdit * edit)
1555 #ifdef HAVE_SLANG
1556 int color;
1558 edit_get_syntax_color (edit, -1, &color);
1559 tty_touch_screen ();
1560 mc_refresh ();
1561 #else
1562 (void) edit;
1564 clr_scr ();
1565 repaint_screen ();
1566 #endif /* !HAVE_SLANG */
1567 tty_keypad (TRUE);
1570 /* --------------------------------------------------------------------------------------------- */
1572 void
1573 menu_save_mode_cmd (void)
1575 /* diaog sizes */
1576 const int DLG_X = 38;
1577 const int DLG_Y = 13;
1579 char *str_result;
1581 const char *str[] = {
1582 N_("&Quick save"),
1583 N_("&Safe save"),
1584 N_("&Do backups with following extension:")
1587 QuickWidget widgets[] = {
1588 /* 0 */
1589 QUICK_BUTTON (18, DLG_X, DLG_Y - 3, DLG_Y, N_("&Cancel"), B_CANCEL, NULL),
1590 /* 1 */
1591 QUICK_BUTTON (6, DLG_X, DLG_Y - 3, DLG_Y, N_("&OK"), B_ENTER, NULL),
1592 /* 2 */
1593 QUICK_CHECKBOX (4, DLG_X, 8, DLG_Y, N_("Check &POSIX new line"), &option_check_nl_at_eof),
1594 /* 3 */
1595 QUICK_INPUT (8, DLG_X, 6, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result),
1596 /* 4 */
1597 QUICK_RADIO (4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode),
1598 QUICK_END
1601 QuickDialog dialog = {
1602 DLG_X, DLG_Y, -1, -1, N_("Edit Save Mode"),
1603 "[Edit Save Mode]", widgets, NULL, FALSE
1606 size_t i;
1607 size_t maxlen = 0;
1608 size_t w0, w1, b_len, w3;
1610 #ifdef HAVE_ASSERT_H
1611 assert (option_backup_ext != NULL);
1612 #endif
1614 /* OK/Cancel buttons */
1615 w0 = str_term_width1 (_(widgets[0].u.button.text)) + 3;
1616 w1 = str_term_width1 (_(widgets[1].u.button.text)) + 5; /* default button */
1617 b_len = w0 + w1 + 3;
1619 maxlen = max (b_len, (size_t) str_term_width1 (_(dialog.title)) + 2);
1621 w3 = 0;
1622 for (i = 0; i < 3; i++)
1624 #ifdef ENABLE_NLS
1625 str[i] = _(str[i]);
1626 #endif
1627 w3 = max (w3, (size_t) str_term_width1 (str[i]));
1630 maxlen = max (maxlen, w3 + 4);
1632 dialog.xlen = min ((size_t) COLS, maxlen + 8);
1634 widgets[3].u.input.len = w3;
1635 widgets[1].relative_x = (dialog.xlen - b_len) / 2;
1636 widgets[0].relative_x = widgets[1].relative_x + w0 + 2;
1638 for (i = 0; i < sizeof (widgets) / sizeof (widgets[0]); i++)
1639 widgets[i].x_divisions = dialog.xlen;
1641 if (quick_dialog (&dialog) != B_CANCEL)
1643 g_free (option_backup_ext);
1644 option_backup_ext = str_result;
1648 /* --------------------------------------------------------------------------------------------- */
1650 void
1651 edit_set_filename (WEdit * edit, const char *name)
1653 g_free (edit->filename);
1655 if (name == NULL)
1656 name = "";
1658 edit->filename = tilde_expand (name);
1659 if (edit->dir == NULL && !g_path_is_absolute (name))
1660 edit->dir = vfs_get_current_dir ();
1663 /* --------------------------------------------------------------------------------------------- */
1664 /* Here we want to warn the users of overwriting an existing file,
1665 but only if they have made a change to the filename */
1666 /* returns 1 on success */
1668 edit_save_as_cmd (WEdit * edit)
1670 /* This heads the 'Save As' dialog box */
1671 char *exp;
1672 int save_lock = 0;
1673 int different_filename = 0;
1675 if (!edit_check_newline (edit))
1676 return 0;
1678 exp = edit_get_save_file_as (edit);
1679 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1681 if (exp)
1683 if (!*exp)
1685 g_free (exp);
1686 edit->force |= REDRAW_COMPLETELY;
1687 return 0;
1689 else
1691 int rv;
1693 if (strcmp (edit->filename, exp))
1695 int file;
1696 vfs_path_t *tmp_vpath;
1697 struct stat sb;
1699 tmp_vpath = vfs_path_from_str (exp);
1700 if (mc_stat (tmp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1702 edit_error_dialog (_("Save as"),
1703 get_sys_error (_
1704 ("Cannot save: destination is not a regular file")));
1705 g_free (exp);
1706 edit->force |= REDRAW_COMPLETELY;
1707 vfs_path_free (tmp_vpath);
1708 return 0;
1711 different_filename = 1;
1712 file = mc_open (tmp_vpath, O_RDONLY | O_BINARY);
1713 vfs_path_free (tmp_vpath);
1714 if (file != -1)
1716 /* the file exists */
1717 mc_close (file);
1718 /* Overwrite the current file or cancel the operation */
1719 if (edit_query_dialog2
1720 (_("Warning"),
1721 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1723 edit->force |= REDRAW_COMPLETELY;
1724 g_free (exp);
1725 return 0;
1728 else
1730 edit->stat1.st_mode |= S_IWUSR;
1732 save_lock = lock_file (exp);
1734 else
1736 /* filenames equal, check if already locked */
1737 if (!edit->locked && !edit->delete_file)
1738 save_lock = lock_file (exp);
1741 if (different_filename)
1744 * Allow user to write into saved (under another name) file
1745 * even if original file had r/o user permissions.
1747 edit->stat1.st_mode |= S_IWRITE;
1750 rv = edit_save_file (edit, exp);
1751 switch (rv)
1753 case 1:
1754 /* Succesful, so unlock both files */
1755 if (different_filename)
1757 if (save_lock)
1758 unlock_file (exp);
1759 if (edit->locked)
1760 edit->locked = edit_unlock_file (edit);
1762 else
1764 if (edit->locked || save_lock)
1765 edit->locked = edit_unlock_file (edit);
1768 edit_set_filename (edit, exp);
1769 if (edit->lb != LB_ASIS)
1770 edit_reload (edit, exp);
1771 g_free (exp);
1772 edit->modified = 0;
1773 edit->delete_file = 0;
1774 if (different_filename)
1775 edit_load_syntax (edit, NULL, edit->syntax_type);
1776 edit->force |= REDRAW_COMPLETELY;
1777 return 1;
1778 default:
1779 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1780 /* fallthrough */
1781 case -1:
1782 /* Failed, so maintain modify (not save) lock */
1783 if (save_lock)
1784 unlock_file (exp);
1785 g_free (exp);
1786 edit->force |= REDRAW_COMPLETELY;
1787 return 0;
1791 edit->force |= REDRAW_COMPLETELY;
1792 return 0;
1795 /* {{{ Macro stuff starts here */
1796 /* --------------------------------------------------------------------------------------------- */
1798 void
1799 edit_delete_macro_cmd (WEdit * edit)
1801 int hotkey;
1803 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1805 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1806 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1809 /* --------------------------------------------------------------------------------------------- */
1811 /** returns FALSE on error */
1812 gboolean
1813 edit_execute_macro (WEdit * edit, int hotkey)
1815 gboolean res = FALSE;
1817 if (hotkey != 0)
1819 const macros_t *macros;
1820 guint indx;
1822 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1823 macros->macro != NULL && macros->macro->len != 0)
1825 guint i;
1827 edit->force |= REDRAW_PAGE;
1829 for (i = 0; i < macros->macro->len; i++)
1831 const macro_action_t *m_act;
1833 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1834 edit_execute_cmd (edit, m_act->action, m_act->ch);
1835 res = TRUE;
1840 return res;
1843 /* --------------------------------------------------------------------------------------------- */
1845 /** returns FALSE on error */
1846 gboolean
1847 edit_store_macro_cmd (WEdit * edit)
1849 int i;
1850 int hotkey;
1851 GString *marcros_string;
1852 mc_config_t *macros_config = NULL;
1853 const char *section_name = "editor";
1854 gchar *macros_fname;
1855 GArray *macros; /* current macro */
1856 int tmp_act;
1857 gboolean have_macro = FALSE;
1858 char *skeyname = NULL;
1860 hotkey = editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1861 if (hotkey == ESC_CHAR)
1862 return FALSE;
1864 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1866 /* return FALSE if try assign macro into restricted hotkeys */
1867 if (tmp_act == CK_MacroStartRecord
1868 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1869 return FALSE;
1871 edit_delete_macro (edit, hotkey);
1873 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1874 macros_config = mc_config_init (macros_fname);
1875 g_free (macros_fname);
1877 if (macros_config == NULL)
1878 return FALSE;
1880 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1882 marcros_string = g_string_sized_new (250);
1883 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1885 skeyname = lookup_key_by_code (hotkey);
1887 for (i = 0; i < macro_index; i++)
1889 macro_action_t m_act;
1890 const char *action_name;
1892 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1894 if (action_name == NULL)
1895 break;
1897 m_act.action = record_macro_buf[i].action;
1898 m_act.ch = record_macro_buf[i].ch;
1899 g_array_append_val (macros, m_act);
1900 have_macro = TRUE;
1901 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1902 (int) record_macro_buf[i].ch);
1904 if (have_macro)
1906 macros_t macro;
1907 macro.hotkey = hotkey;
1908 macro.macro = macros;
1909 g_array_append_val (macros_list, macro);
1910 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1912 else
1913 mc_config_del_key (macros_config, section_name, skeyname);
1915 g_free (skeyname);
1916 edit_macro_sort_by_hotkey ();
1918 g_string_free (marcros_string, TRUE);
1919 mc_config_save_file (macros_config, NULL);
1920 mc_config_deinit (macros_config);
1921 return TRUE;
1924 /* --------------------------------------------------------------------------------------------- */
1926 gboolean
1927 edit_repeat_macro_cmd (WEdit * edit)
1929 int i, j;
1930 char *f;
1931 long count_repeat;
1932 char *error = NULL;
1934 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL);
1935 if (f == NULL || *f == '\0')
1937 g_free (f);
1938 return FALSE;
1941 count_repeat = strtol (f, &error, 0);
1943 if (*error != '\0')
1945 g_free (f);
1946 return FALSE;
1949 g_free (f);
1951 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1952 edit->force |= REDRAW_PAGE;
1954 for (j = 0; j < count_repeat; j++)
1955 for (i = 0; i < macro_index; i++)
1956 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1957 edit_update_screen (edit);
1958 return TRUE;
1961 /* --------------------------------------------------------------------------------------------- */
1962 /** return FALSE on error */
1964 gboolean
1965 edit_load_macro_cmd (WEdit * edit)
1967 mc_config_t *macros_config = NULL;
1968 gchar **profile_keys, **keys;
1969 gchar **values, **curr_values;
1970 gsize len, values_len;
1971 const char *section_name = "editor";
1972 gchar *macros_fname;
1973 int hotkey;
1975 (void) edit;
1977 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1978 macros_config = mc_config_init (macros_fname);
1979 g_free (macros_fname);
1981 if (macros_config == NULL)
1982 return FALSE;
1984 profile_keys = keys = mc_config_get_keys (macros_config, section_name, &len);
1985 while (*profile_keys != NULL)
1987 gboolean have_macro;
1988 GArray *macros;
1989 macros_t macro;
1991 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1993 curr_values = values = mc_config_get_string_list (macros_config, section_name,
1994 *profile_keys, &values_len);
1995 hotkey = lookup_key (*profile_keys, NULL);
1996 have_macro = FALSE;
1998 while (*curr_values != NULL && *curr_values[0] != '\0')
2000 char **macro_pair = NULL;
2002 macro_pair = g_strsplit (*curr_values, ":", 2);
2004 if (macro_pair != NULL)
2006 macro_action_t m_act;
2007 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
2008 m_act.action = 0;
2009 else
2011 m_act.action = keybind_lookup_action (macro_pair[0]);
2012 g_free (macro_pair[0]);
2013 macro_pair[0] = NULL;
2015 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
2016 m_act.ch = -1;
2017 else
2019 m_act.ch = strtol (macro_pair[1], NULL, 0);
2020 g_free (macro_pair[1]);
2021 macro_pair[1] = NULL;
2023 if (m_act.action != 0)
2025 /* a shell command */
2026 if ((m_act.action / CK_PipeBlock (0)) == 1)
2028 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
2029 m_act.ch = -1;
2031 g_array_append_val (macros, m_act);
2032 have_macro = TRUE;
2034 g_strfreev (macro_pair);
2035 macro_pair = NULL;
2037 curr_values++;
2039 if (have_macro)
2041 macro.hotkey = hotkey;
2042 macro.macro = macros;
2043 g_array_append_val (macros_list, macro);
2045 profile_keys++;
2046 g_strfreev (values);
2048 g_strfreev (keys);
2049 mc_config_deinit (macros_config);
2050 edit_macro_sort_by_hotkey ();
2051 return TRUE;
2054 /* }}} Macro stuff end here */
2056 /* --------------------------------------------------------------------------------------------- */
2057 /** returns 1 on success */
2060 edit_save_confirm_cmd (WEdit * edit)
2062 gchar *f = NULL;
2064 if (!edit_check_newline (edit))
2065 return 0;
2067 if (edit_confirm_save)
2069 f = g_strdup_printf (_("Confirm save file: \"%s\""), edit->filename);
2070 if (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")))
2072 g_free (f);
2073 return 0;
2075 g_free (f);
2077 return edit_save_cmd (edit);
2080 /* --------------------------------------------------------------------------------------------- */
2081 /** returns 1 on success */
2084 edit_new_cmd (WEdit * edit)
2086 if (edit->modified)
2088 if (edit_query_dialog2
2089 (_("Warning"),
2091 ("Current text was modified without a file save.\nContinue discards these changes"),
2092 _("C&ontinue"), _("&Cancel")))
2094 edit->force |= REDRAW_COMPLETELY;
2095 return 0;
2098 edit->force |= REDRAW_COMPLETELY;
2100 return edit_renew (edit); /* if this gives an error, something has really screwed up */
2103 /* --------------------------------------------------------------------------------------------- */
2106 edit_load_cmd (WEdit * edit, edit_current_file_t what)
2108 char *exp;
2110 if (edit->modified
2111 && (edit_query_dialog2
2112 (_("Warning"),
2113 _("Current text was modified without a file save.\n"
2114 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")) == 1))
2116 edit->force |= REDRAW_COMPLETELY;
2117 return 0;
2120 switch (what)
2122 case EDIT_FILE_COMMON:
2123 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2124 MC_HISTORY_EDIT_LOAD, edit->filename);
2126 if (exp)
2128 if (*exp)
2129 edit_load_file_from_filename (edit, exp);
2130 g_free (exp);
2132 break;
2134 case EDIT_FILE_SYNTAX:
2135 edit_load_syntax_file (edit);
2136 break;
2138 case EDIT_FILE_MENU:
2139 edit_load_menu_file (edit);
2140 break;
2142 default:
2143 break;
2146 edit->force |= REDRAW_COMPLETELY;
2147 return 0;
2150 /* --------------------------------------------------------------------------------------------- */
2152 if mark2 is -1 then marking is from mark1 to the cursor.
2153 Otherwise its between the markers. This handles this.
2154 Returns 1 if no text is marked.
2158 eval_marks (WEdit * edit, long *start_mark, long *end_mark)
2160 if (edit->mark1 != edit->mark2)
2162 long start_bol, start_eol;
2163 long end_bol, end_eol;
2164 long col1, col2;
2165 long diff1, diff2;
2166 long end_mark_curs;
2168 if (edit->end_mark_curs < 0)
2169 end_mark_curs = edit->curs1;
2170 else
2171 end_mark_curs = edit->end_mark_curs;
2173 if (edit->mark2 >= 0)
2175 *start_mark = min (edit->mark1, edit->mark2);
2176 *end_mark = max (edit->mark1, edit->mark2);
2178 else
2180 *start_mark = min (edit->mark1, end_mark_curs);
2181 *end_mark = max (edit->mark1, end_mark_curs);
2182 edit->column2 = edit->curs_col + edit->over_col;
2185 if (edit->column_highlight
2186 && (((edit->mark1 > end_mark_curs) && (edit->column1 < edit->column2))
2187 || ((edit->mark1 < end_mark_curs) && (edit->column1 > edit->column2))))
2189 start_bol = edit_bol (edit, *start_mark);
2190 start_eol = edit_eol (edit, start_bol - 1) + 1;
2191 end_bol = edit_bol (edit, *end_mark);
2192 end_eol = edit_eol (edit, *end_mark);
2193 col1 = min (edit->column1, edit->column2);
2194 col2 = max (edit->column1, edit->column2);
2196 diff1 =
2197 edit_move_forward3 (edit, start_bol, col2, 0) - edit_move_forward3 (edit, start_bol,
2198 col1, 0);
2199 diff2 =
2200 edit_move_forward3 (edit, end_bol, col2, 0) - edit_move_forward3 (edit, end_bol,
2201 col1, 0);
2203 *start_mark -= diff1;
2204 *end_mark += diff2;
2205 *start_mark = max (*start_mark, start_eol);
2206 *end_mark = min (*end_mark, end_eol);
2208 return 0;
2210 else
2212 *start_mark = *end_mark = 0;
2213 edit->column2 = edit->column1 = 0;
2214 return 1;
2218 /* --------------------------------------------------------------------------------------------- */
2220 void
2221 edit_insert_over (WEdit * edit)
2223 int i;
2225 for (i = 0; i < edit->over_col; i++)
2227 edit_insert (edit, ' ');
2229 edit->over_col = 0;
2232 /* --------------------------------------------------------------------------------------------- */
2235 edit_insert_column_of_text_from_file (WEdit * edit, int file,
2236 long *start_pos, long *end_pos, int *col1, int *col2)
2238 long cursor;
2239 int col;
2240 int blocklen = -1, width = 0;
2241 unsigned char *data;
2243 cursor = edit->curs1;
2244 col = edit_get_col (edit);
2245 data = g_malloc0 (TEMP_BUF_LEN);
2247 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
2249 int i;
2250 for (width = 0; width < blocklen; width++)
2252 if (data[width] == '\n')
2253 break;
2255 for (i = 0; i < blocklen; i++)
2257 if (data[i] == '\n')
2258 { /* fill in and move to next line */
2259 int l;
2260 long p;
2261 if (edit_get_byte (edit, edit->curs1) != '\n')
2263 l = width - (edit_get_col (edit) - col);
2264 while (l > 0)
2266 edit_insert (edit, ' ');
2267 l -= space_width;
2270 for (p = edit->curs1;; p++)
2272 if (p == edit->last_byte)
2274 edit_cursor_move (edit, edit->last_byte - edit->curs1);
2275 edit_insert_ahead (edit, '\n');
2276 p++;
2277 break;
2279 if (edit_get_byte (edit, p) == '\n')
2281 p++;
2282 break;
2285 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
2286 l = col - edit_get_col (edit);
2287 while (l >= space_width)
2289 edit_insert (edit, ' ');
2290 l -= space_width;
2292 continue;
2294 edit_insert (edit, data[i]);
2297 *col1 = col;
2298 *col2 = col + width;
2299 *start_pos = cursor;
2300 *end_pos = edit->curs1;
2301 edit_cursor_move (edit, cursor - edit->curs1);
2302 g_free (data);
2304 return blocklen;
2307 /* --------------------------------------------------------------------------------------------- */
2309 void
2310 edit_block_copy_cmd (WEdit * edit)
2312 long start_mark, end_mark, current = edit->curs1;
2313 long col_delta = 0;
2314 long mark1, mark2;
2315 int c1, c2;
2316 int size;
2317 unsigned char *copy_buf;
2319 edit_update_curs_col (edit);
2320 if (eval_marks (edit, &start_mark, &end_mark))
2321 return;
2323 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2325 /* all that gets pushed are deletes hence little space is used on the stack */
2327 edit_push_markers (edit);
2329 if (edit->column_highlight)
2331 col_delta = abs (edit->column2 - edit->column1);
2332 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2334 else
2336 while (size--)
2337 edit_insert_ahead (edit, copy_buf[size]);
2340 g_free (copy_buf);
2341 edit_scroll_screen_over_cursor (edit);
2343 if (edit->column_highlight)
2344 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2345 else if (start_mark < current && end_mark > current)
2346 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2348 edit->force |= REDRAW_PAGE;
2352 /* --------------------------------------------------------------------------------------------- */
2354 void
2355 edit_block_move_cmd (WEdit * edit)
2357 long current;
2358 unsigned char *copy_buf = NULL;
2359 long start_mark, end_mark;
2360 long line;
2362 if (eval_marks (edit, &start_mark, &end_mark))
2363 return;
2365 line = edit->curs_line;
2366 if (edit->mark2 < 0)
2367 edit_mark_cmd (edit, 0);
2368 edit_push_markers (edit);
2370 if (edit->column_highlight)
2372 long mark1, mark2;
2373 int size;
2374 int b_width = 0;
2375 int c1, c2;
2376 int x, x2;
2378 c1 = min (edit->column1, edit->column2);
2379 c2 = max (edit->column1, edit->column2);
2380 b_width = (c2 - c1);
2382 edit_update_curs_col (edit);
2384 x = edit->curs_col;
2385 x2 = x + edit->over_col;
2387 /* do nothing when cursor inside first line of selected area */
2388 if ((edit_eol (edit, edit->curs1) == edit_eol (edit, start_mark)) && (x2 > c1 && x2 <= c2))
2389 return;
2391 if (edit->curs1 > start_mark && edit->curs1 < edit_eol (edit, end_mark))
2393 if (x > c2)
2394 x -= b_width;
2395 else if (x > c1 && x <= c2)
2396 x = c1;
2398 /* save current selection into buffer */
2399 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2401 /* remove current selection */
2402 edit_block_delete_cmd (edit);
2404 edit->over_col = max (0, edit->over_col - b_width);
2405 /* calculate the cursor pos after delete block */
2406 current = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), x, 0);
2407 edit_cursor_move (edit, current - edit->curs1);
2408 edit_scroll_screen_over_cursor (edit);
2410 /* add TWS if need before block insertion */
2411 if (option_cursor_beyond_eol && edit->over_col > 0)
2412 edit_insert_over (edit);
2414 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2415 edit_set_markers (edit, mark1, mark2, c1, c2);
2417 else
2419 long count;
2421 current = edit->curs1;
2422 copy_buf = g_malloc0 (end_mark - start_mark);
2423 edit_cursor_move (edit, start_mark - edit->curs1);
2424 edit_scroll_screen_over_cursor (edit);
2425 count = start_mark;
2426 while (count < end_mark)
2428 copy_buf[end_mark - count - 1] = edit_delete (edit, 1);
2429 count++;
2431 edit_scroll_screen_over_cursor (edit);
2432 edit_cursor_move (edit,
2433 current - edit->curs1 -
2434 (((current - edit->curs1) > 0) ? end_mark - start_mark : 0));
2435 edit_scroll_screen_over_cursor (edit);
2436 while (count-- > start_mark)
2437 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2438 edit_set_markers (edit, edit->curs1, edit->curs1 + end_mark - start_mark, 0, 0);
2441 edit_scroll_screen_over_cursor (edit);
2442 g_free (copy_buf);
2443 edit->force |= REDRAW_PAGE;
2446 /* --------------------------------------------------------------------------------------------- */
2447 /** returns 1 if canceelled by user */
2450 edit_block_delete_cmd (WEdit * edit)
2452 long start_mark, end_mark;
2453 if (eval_marks (edit, &start_mark, &end_mark))
2455 edit_delete_line (edit);
2456 return 0;
2458 return edit_block_delete (edit);
2461 /* --------------------------------------------------------------------------------------------- */
2462 /** call with edit = 0 before shutdown to close memory leaks */
2464 void
2465 edit_replace_cmd (WEdit * edit, int again)
2467 /* 1 = search string, 2 = replace with */
2468 static char *saved1 = NULL; /* saved default[123] */
2469 static char *saved2 = NULL;
2470 char *input1 = NULL; /* user input from the dialog */
2471 char *input2 = NULL;
2472 GString *input2_str = NULL;
2473 char *disp1 = NULL;
2474 char *disp2 = NULL;
2475 long times_replaced = 0;
2476 gboolean once_found = FALSE;
2478 if (!edit)
2480 g_free (saved1), saved1 = NULL;
2481 g_free (saved2), saved2 = NULL;
2482 return;
2485 edit->force |= REDRAW_COMPLETELY;
2487 if (again && !saved1 && !saved2)
2488 again = 0;
2490 if (again)
2492 input1 = g_strdup (saved1 ? saved1 : "");
2493 input2 = g_strdup (saved2 ? saved2 : "");
2495 else
2497 char *tmp_inp1, *tmp_inp2;
2499 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2500 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2502 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2504 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2506 g_free (disp1);
2507 g_free (disp2);
2509 if (input1 == NULL || *input1 == '\0')
2511 edit->force = REDRAW_COMPLETELY;
2512 goto cleanup;
2515 tmp_inp1 = input1;
2516 tmp_inp2 = input2;
2517 input1 = edit_replace_cmd__conv_to_input (input1);
2518 input2 = edit_replace_cmd__conv_to_input (input2);
2519 g_free (tmp_inp1);
2520 g_free (tmp_inp2);
2522 g_free (saved1), saved1 = g_strdup (input1);
2523 g_free (saved2), saved2 = g_strdup (input2);
2525 mc_search_free (edit->search);
2526 edit->search = NULL;
2529 input2_str = g_string_new (input2);
2531 if (!edit->search)
2533 edit->search = mc_search_new (input1, -1);
2534 if (edit->search == NULL)
2536 edit->search_start = edit->curs1;
2537 goto cleanup;
2539 edit->search->search_type = edit_search_options.type;
2540 edit->search->is_all_charsets = edit_search_options.all_codepages;
2541 edit->search->is_case_sensitive = edit_search_options.case_sens;
2542 edit->search->whole_words = edit_search_options.whole_words;
2543 edit->search->search_fn = edit_search_cmd_callback;
2544 edit->search_line_type = edit_get_search_line_type (edit->search);
2545 edit_search_fix_search_start_if_selection (edit);
2548 if (edit->found_len && edit->search_start == edit->found_start + 1
2549 && edit_search_options.backwards)
2550 edit->search_start--;
2552 if (edit->found_len && edit->search_start == edit->found_start - 1
2553 && !edit_search_options.backwards)
2554 edit->search_start++;
2558 gsize len = 0;
2560 if (!editcmd_find (edit, &len))
2562 if (!(edit->search->error == MC_SEARCH_E_OK ||
2563 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2565 edit_error_dialog (_("Search"), edit->search->error_str);
2567 break;
2569 once_found = TRUE;
2571 edit->search_start = edit->search->normal_offset;
2572 /*returns negative on not found or error in pattern */
2574 if ((edit->search_start >= 0) && (edit->search_start < edit->last_byte))
2576 gsize i;
2577 GString *repl_str;
2579 edit->found_start = edit->search_start;
2580 i = edit->found_len = len;
2582 edit_cursor_move (edit, edit->search_start - edit->curs1);
2583 edit_scroll_screen_over_cursor (edit);
2585 if (edit->replace_mode == 0)
2587 int l;
2588 int prompt;
2590 l = edit->curs_row - edit->widget.lines / 3;
2591 if (l > 0)
2592 edit_scroll_downward (edit, l);
2593 if (l < 0)
2594 edit_scroll_upward (edit, -l);
2596 edit_scroll_screen_over_cursor (edit);
2597 edit->force |= REDRAW_PAGE;
2598 edit_render_keypress (edit);
2600 /*so that undo stops at each query */
2601 edit_push_key_press (edit);
2602 /* and prompt 2/3 down */
2603 disp1 = edit_replace_cmd__conv_to_display (saved1);
2604 disp2 = edit_replace_cmd__conv_to_display (saved2);
2605 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2606 g_free (disp1);
2607 g_free (disp2);
2609 if (prompt == B_REPLACE_ALL)
2610 edit->replace_mode = 1;
2611 else if (prompt == B_SKIP_REPLACE)
2613 if (edit_search_options.backwards)
2614 edit->search_start--;
2615 else
2616 edit->search_start++;
2617 continue; /* loop */
2619 else if (prompt == B_CANCEL)
2621 edit->replace_mode = -1;
2622 break; /* loop */
2626 repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2628 if (edit->search->error != MC_SEARCH_E_OK)
2630 edit_error_dialog (_("Replace"), edit->search->error_str);
2631 g_string_free (repl_str, TRUE);
2632 break;
2635 /* delete then insert new */
2636 for (i = 0; i < len; i++)
2637 edit_delete (edit, 1);
2639 for (i = 0; i < repl_str->len; i++)
2640 edit_insert (edit, repl_str->str[i]);
2642 edit->found_len = repl_str->len;
2643 g_string_free (repl_str, TRUE);
2644 times_replaced++;
2646 /* so that we don't find the same string again */
2647 if (edit_search_options.backwards)
2649 edit->search_start--;
2651 else
2653 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2655 if (edit->search_start >= edit->last_byte)
2656 break;
2659 edit_scroll_screen_over_cursor (edit);
2661 else
2663 /* try and find from right here for next search */
2664 edit->search_start = edit->curs1;
2665 edit_update_curs_col (edit);
2667 edit->force |= REDRAW_PAGE;
2668 edit_render_keypress (edit);
2670 if (times_replaced == 0)
2671 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2672 break;
2675 while (edit->replace_mode >= 0);
2677 edit_scroll_screen_over_cursor (edit);
2678 edit->force |= REDRAW_COMPLETELY;
2679 edit_render_keypress (edit);
2681 if ((edit->replace_mode == 1) && (times_replaced != 0))
2682 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2684 cleanup:
2685 g_free (input1);
2686 g_free (input2);
2687 if (input2_str != NULL)
2688 g_string_free (input2_str, TRUE);
2691 /* --------------------------------------------------------------------------------------------- */
2694 edit_search_cmd_callback (const void *user_data, gsize char_offset)
2696 return edit_get_byte ((WEdit *) user_data, (long) char_offset);
2699 /* --------------------------------------------------------------------------------------------- */
2701 void
2702 edit_search_cmd (WEdit * edit, gboolean again)
2705 if (edit == NULL)
2706 return;
2708 if (!again)
2709 edit_search (edit);
2710 else if (edit->last_search_string != NULL)
2711 edit_do_search (edit);
2712 else
2714 /* find last search string in history */
2715 GList *history;
2717 history = history_get (MC_HISTORY_SHARED_SEARCH);
2718 if (history != NULL && history->data != NULL)
2720 edit->last_search_string = (char *) history->data;
2721 history->data = NULL;
2722 history = g_list_first (history);
2723 g_list_foreach (history, (GFunc) g_free, NULL);
2724 g_list_free (history);
2726 edit->search = mc_search_new (edit->last_search_string, -1);
2727 if (edit->search == NULL)
2729 /* if not... then ask for an expression */
2730 g_free (edit->last_search_string);
2731 edit->last_search_string = NULL;
2732 edit_search (edit);
2734 else
2736 edit->search->search_type = edit_search_options.type;
2737 edit->search->is_all_charsets = edit_search_options.all_codepages;
2738 edit->search->is_case_sensitive = edit_search_options.case_sens;
2739 edit->search->whole_words = edit_search_options.whole_words;
2740 edit->search->search_fn = edit_search_cmd_callback;
2741 edit->search_line_type = edit_get_search_line_type (edit->search);
2742 edit_do_search (edit);
2745 else
2747 /* if not... then ask for an expression */
2748 g_free (edit->last_search_string);
2749 edit->last_search_string = NULL;
2750 edit_search (edit);
2756 /* --------------------------------------------------------------------------------------------- */
2758 * Check if it's OK to close the editor. If there are unsaved changes,
2759 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2762 gboolean
2763 edit_ok_to_exit (WEdit * edit)
2765 int act;
2767 if (!edit->modified)
2768 return TRUE;
2770 if (!mc_global.midnight_shutdown)
2772 if (!edit_check_newline (edit))
2773 return FALSE;
2775 query_set_sel (2);
2776 act = edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2777 _("&Yes"), _("&No"), _("&Cancel quit"));
2779 else
2781 act =
2782 edit_query_dialog2 (_("Quit"),
2783 _("Midnight Commander is being shut down.\nSave modified file?"),
2784 _("&Yes"), _("&No"));
2786 /* Esc is No */
2787 if (act == -1)
2788 act = 1;
2791 switch (act)
2793 case 0: /* Yes */
2794 edit_push_markers (edit);
2795 edit_set_markers (edit, 0, 0, 0, 0);
2796 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2797 return mc_global.midnight_shutdown;
2798 break;
2799 case 1: /* No */
2800 break;
2801 case 2: /* Cancel quit */
2802 case -1: /* Esc */
2803 return FALSE;
2806 return TRUE;
2809 /* --------------------------------------------------------------------------------------------- */
2810 /** save block, returns 1 on success */
2813 edit_save_block (WEdit * edit, const char *filename, long start, long finish)
2815 int len, file;
2816 vfs_path_t *vpath;
2818 vpath = vfs_path_from_str (filename);
2819 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2820 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2821 vfs_path_free (vpath);
2822 if (file == -1)
2823 return 0;
2825 if (edit->column_highlight)
2827 int r;
2829 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2830 if (r > 0)
2832 unsigned char *block, *p;
2834 p = block = edit_get_block (edit, start, finish, &len);
2835 while (len)
2837 r = mc_write (file, p, len);
2838 if (r < 0)
2839 break;
2840 p += r;
2841 len -= r;
2843 g_free (block);
2846 else
2848 unsigned char *buf;
2849 int i = start, end;
2851 len = finish - start;
2852 buf = g_malloc0 (TEMP_BUF_LEN);
2853 while (start != finish)
2855 end = min (finish, start + TEMP_BUF_LEN);
2856 for (; i < end; i++)
2857 buf[i - start] = edit_get_byte (edit, i);
2858 len -= mc_write (file, (char *) buf, end - start);
2859 start = end;
2861 g_free (buf);
2863 mc_close (file);
2864 if (len)
2865 return 0;
2866 return 1;
2869 /* --------------------------------------------------------------------------------------------- */
2871 void
2872 edit_paste_from_history (WEdit * edit)
2874 (void) edit;
2875 edit_error_dialog (_("Error"), _("This function is not implemented"));
2878 /* --------------------------------------------------------------------------------------------- */
2881 edit_copy_to_X_buf_cmd (WEdit * edit)
2883 long start_mark, end_mark;
2884 if (eval_marks (edit, &start_mark, &end_mark))
2885 return 0;
2886 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2888 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2889 return 1;
2891 /* try use external clipboard utility */
2892 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2894 return 0;
2897 /* --------------------------------------------------------------------------------------------- */
2900 edit_cut_to_X_buf_cmd (WEdit * edit)
2902 long start_mark, end_mark;
2903 if (eval_marks (edit, &start_mark, &end_mark))
2904 return 0;
2905 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2907 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2908 return 1;
2910 /* try use external clipboard utility */
2911 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2913 edit_block_delete_cmd (edit);
2914 edit_mark_cmd (edit, 1);
2915 return 0;
2918 /* --------------------------------------------------------------------------------------------- */
2920 void
2921 edit_paste_from_X_buf_cmd (WEdit * edit)
2923 gchar *tmp;
2924 /* try use external clipboard utility */
2925 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
2926 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
2927 edit_insert_file (edit, tmp);
2928 g_free (tmp);
2932 /* --------------------------------------------------------------------------------------------- */
2934 * Ask user for the line and go to that line.
2935 * Negative numbers mean line from the end (i.e. -1 is the last line).
2938 void
2939 edit_goto_cmd (WEdit * edit)
2941 char *f;
2942 static long line = 0; /* line as typed, saved as default */
2943 long l;
2944 char *error;
2945 char s[32];
2947 g_snprintf (s, sizeof (s), "%ld", line);
2948 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "");
2949 if (!f)
2950 return;
2952 if (!*f)
2954 g_free (f);
2955 return;
2958 l = strtol (f, &error, 0);
2959 if (*error)
2961 g_free (f);
2962 return;
2965 line = l;
2966 if (l < 0)
2967 l = edit->total_lines + l + 2;
2968 edit_move_display (edit, l - edit->widget.lines / 2 - 1);
2969 edit_move_to_line (edit, l - 1);
2970 edit->force |= REDRAW_COMPLETELY;
2971 g_free (f);
2975 /* --------------------------------------------------------------------------------------------- */
2976 /** Return 1 on success */
2979 edit_save_block_cmd (WEdit * edit)
2981 long start_mark, end_mark;
2982 char *exp, *tmp;
2984 if (eval_marks (edit, &start_mark, &end_mark))
2985 return 1;
2987 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
2988 exp =
2989 input_expand_dialog (_("Save block"), _("Enter file name:"),
2990 MC_HISTORY_EDIT_SAVE_BLOCK, tmp);
2991 g_free (tmp);
2992 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2993 if (exp)
2995 if (!*exp)
2997 g_free (exp);
2998 return 0;
3000 else
3002 if (edit_save_block (edit, exp, start_mark, end_mark))
3004 g_free (exp);
3005 edit->force |= REDRAW_COMPLETELY;
3006 return 1;
3008 else
3010 g_free (exp);
3011 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3015 edit->force |= REDRAW_COMPLETELY;
3016 return 0;
3020 /* --------------------------------------------------------------------------------------------- */
3021 /** returns TRUE on success */
3023 gboolean
3024 edit_insert_file_cmd (WEdit * edit)
3026 gchar *tmp;
3027 char *exp;
3028 gboolean ret = FALSE;
3030 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3031 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3032 MC_HISTORY_EDIT_INSERT_FILE, tmp);
3033 g_free (tmp);
3035 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3037 if (exp != NULL && *exp != '\0')
3039 ret = (edit_insert_file (edit, exp) >= 0);
3040 if (!ret)
3041 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3044 g_free (exp);
3046 edit->force |= REDRAW_COMPLETELY;
3047 return ret;
3050 /* --------------------------------------------------------------------------------------------- */
3051 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3054 edit_sort_cmd (WEdit * edit)
3056 static char *old = 0;
3057 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3058 long start_mark, end_mark;
3059 int e;
3061 if (eval_marks (edit, &start_mark, &end_mark))
3063 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3064 return 0;
3067 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3068 edit_save_block (edit, tmp, start_mark, end_mark);
3069 g_free (tmp);
3071 exp = input_dialog (_("Run sort"),
3072 _("Enter sort options (see manpage) separated by whitespace:"),
3073 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "");
3075 if (!exp)
3076 return 1;
3077 g_free (old);
3078 old = exp;
3079 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3080 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3081 tmp =
3082 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3083 " > ", tmp_edit_temp_name, (char *) NULL);
3084 g_free (tmp_edit_temp_name);
3085 g_free (tmp_edit_block_name);
3087 e = system (tmp);
3088 g_free (tmp);
3089 if (e)
3091 if (e == -1 || e == 127)
3093 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3095 else
3097 char q[8];
3098 sprintf (q, "%d ", e);
3099 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3100 edit_error_dialog (_("Sort"), tmp);
3101 g_free (tmp);
3103 return -1;
3106 edit->force |= REDRAW_COMPLETELY;
3108 if (edit_block_delete_cmd (edit))
3109 return 1;
3110 tmp = mc_config_get_full_path (EDIT_TEMP_FILE);
3111 edit_insert_file (edit, tmp);
3112 g_free (tmp);
3113 return 0;
3116 /* --------------------------------------------------------------------------------------------- */
3118 * Ask user for a command, execute it and paste its output back to the
3119 * editor.
3123 edit_ext_cmd (WEdit * edit)
3125 char *exp, *tmp, *tmp_edit_temp_file;
3126 int e;
3128 exp =
3129 input_dialog (_("Paste output of external command"),
3130 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL);
3132 if (!exp)
3133 return 1;
3135 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3136 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3137 g_free (tmp_edit_temp_file);
3138 e = system (tmp);
3139 g_free (tmp);
3140 g_free (exp);
3142 if (e)
3144 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3145 return -1;
3148 edit->force |= REDRAW_COMPLETELY;
3149 tmp = mc_config_get_full_path (EDIT_TEMP_FILE);
3150 edit_insert_file (edit, tmp);
3151 g_free (tmp);
3152 return 0;
3155 /* --------------------------------------------------------------------------------------------- */
3156 /** if block is 1, a block must be highlighted and the shell command
3157 processes it. If block is 0 the shell command is a straight system
3158 command, that just produces some output which is to be inserted */
3160 void
3161 edit_block_process_cmd (WEdit * edit, int macro_number)
3163 char *fname;
3164 char *macros_fname = NULL;
3166 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3167 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3168 user_menu (edit, macros_fname, 0);
3169 g_free (fname);
3170 g_free (macros_fname);
3171 edit->force |= REDRAW_COMPLETELY;
3174 /* --------------------------------------------------------------------------------------------- */
3176 void
3177 edit_mail_dialog (WEdit * edit)
3179 char *tmail_to;
3180 char *tmail_subject;
3181 char *tmail_cc;
3183 static char *mail_cc_last = 0;
3184 static char *mail_subject_last = 0;
3185 static char *mail_to_last = 0;
3187 QuickWidget quick_widgets[] = {
3188 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
3189 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
3190 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input", &tmail_cc),
3191 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT, N_("Copies to")),
3192 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-2",
3193 &tmail_subject),
3194 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT, N_("Subject")),
3195 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-3", &tmail_to),
3196 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT, N_("To")),
3197 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT, N_("mail -s <subject> -c <cc> <to>")),
3198 QUICK_END
3201 QuickDialog Quick_input = {
3202 50, MAIL_DLG_HEIGHT, -1, -1, N_("Mail"),
3203 "[Input Line Keys]", quick_widgets, NULL, FALSE
3206 quick_widgets[2].u.input.text = mail_cc_last ? mail_cc_last : "";
3207 quick_widgets[4].u.input.text = mail_subject_last ? mail_subject_last : "";
3208 quick_widgets[6].u.input.text = mail_to_last ? mail_to_last : "";
3210 if (quick_dialog (&Quick_input) != B_CANCEL)
3212 g_free (mail_cc_last);
3213 g_free (mail_subject_last);
3214 g_free (mail_to_last);
3215 mail_cc_last = tmail_cc;
3216 mail_subject_last = tmail_subject;
3217 mail_to_last = tmail_to;
3218 pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
3223 /*******************/
3224 /* Word Completion */
3225 /*******************/
3227 /* --------------------------------------------------------------------------------------------- */
3229 * Complete current word using regular expression search
3230 * backwards beginning at the current cursor position.
3233 void
3234 edit_complete_word_cmd (WEdit * edit)
3236 gsize i, max_len, word_len = 0, num_compl = 0;
3237 long word_start = 0;
3238 unsigned char *bufpos;
3239 char *match_expr;
3240 struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
3242 /* search start of word to be completed */
3243 if (!edit_find_word_start (edit, &word_start, &word_len))
3244 return;
3246 /* prepare match expression */
3247 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3249 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3250 match_expr =
3251 g_strdup_printf
3252 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3253 (int) word_len, bufpos);
3255 /* collect the possible completions */
3256 /* start search from begin to end of file */
3257 max_len =
3258 edit_collect_completions (edit, word_start, word_len, match_expr,
3259 (struct selection *) &compl, &num_compl);
3261 if (num_compl > 0)
3263 /* insert completed word if there is only one match */
3264 if (num_compl == 1)
3266 for (i = word_len; i < compl[0].len; i++)
3267 edit_insert (edit, *(compl[0].text + i));
3269 /* more than one possible completion => ask the user */
3270 else
3272 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3273 /* !!! pressed again the selection dialog pops up, but that !!! */
3274 /* !!! seems to require a further internal state !!! */
3275 /*tty_beep (); */
3277 /* let the user select the preferred completion */
3278 editcmd_dialog_completion_show (edit, max_len, word_len,
3279 (struct selection *) &compl, num_compl);
3283 g_free (match_expr);
3284 /* release memory before return */
3285 for (i = 0; i < num_compl; i++)
3286 g_free (compl[i].text);
3289 /* --------------------------------------------------------------------------------------------- */
3291 void
3292 edit_select_codepage_cmd (WEdit * edit)
3294 #ifdef HAVE_CHARSET
3295 if (do_select_codepage ())
3296 edit_set_codeset (edit);
3298 edit->force = REDRAW_COMPLETELY;
3299 edit_refresh_cmd (edit);
3300 #else
3301 (void) edit;
3302 #endif
3305 /* --------------------------------------------------------------------------------------------- */
3307 void
3308 edit_insert_literal_cmd (WEdit * edit)
3310 int char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3311 _("Press any key:"), 0);
3312 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3315 /* --------------------------------------------------------------------------------------------- */
3317 void
3318 edit_begin_end_macro_cmd (WEdit * edit)
3320 /* edit is a pointer to the widget */
3321 if (edit != NULL)
3323 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3324 edit_execute_key_command (edit, command, -1);
3328 /* --------------------------------------------------------------------------------------------- */
3330 void
3331 edit_begin_end_repeat_cmd (WEdit * edit)
3333 /* edit is a pointer to the widget */
3334 if (edit != NULL)
3336 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3337 edit_execute_key_command (edit, command, -1);
3341 /* --------------------------------------------------------------------------------------------- */
3344 edit_load_forward_cmd (WEdit * edit)
3346 if (edit->modified)
3348 if (edit_query_dialog2
3349 (_("Warning"),
3350 _("Current text was modified without a file save\n"
3351 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3353 edit->force |= REDRAW_COMPLETELY;
3354 return 0;
3357 if (edit_stack_iterator + 1 < MAX_HISTORY_MOVETO)
3359 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3361 return 1;
3363 edit_stack_iterator++;
3364 if (edit_history_moveto[edit_stack_iterator].filename)
3366 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename,
3367 edit_history_moveto[edit_stack_iterator].line);
3368 return 0;
3370 else
3372 return 1;
3375 else
3377 return 1;
3381 /* --------------------------------------------------------------------------------------------- */
3384 edit_load_back_cmd (WEdit * edit)
3386 if (edit->modified)
3388 if (edit_query_dialog2
3389 (_("Warning"),
3390 _("Current text was modified without a file save\n"
3391 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3393 edit->force |= REDRAW_COMPLETELY;
3394 return 0;
3397 if (edit_stack_iterator > 0)
3399 edit_stack_iterator--;
3400 if (edit_history_moveto[edit_stack_iterator].filename)
3402 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename,
3403 edit_history_moveto[edit_stack_iterator].line);
3404 return 0;
3406 else
3408 return 1;
3411 else
3413 return 1;
3417 /* --------------------------------------------------------------------------------------------- */
3419 void
3420 edit_get_match_keyword_cmd (WEdit * edit)
3422 gsize word_len = 0, max_len = 0;
3423 int num_def = 0;
3424 int i;
3425 long word_start = 0;
3426 unsigned char *bufpos;
3427 char *match_expr;
3428 char *path = NULL;
3429 char *ptr = NULL;
3430 char *tagfile = NULL;
3432 etags_hash_t def_hash[MAX_DEFINITIONS];
3434 for (i = 0; i < MAX_DEFINITIONS; i++)
3436 def_hash[i].filename = NULL;
3439 /* search start of word to be completed */
3440 if (!edit_find_word_start (edit, &word_start, &word_len))
3441 return;
3443 /* prepare match expression */
3444 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3445 match_expr = g_strdup_printf ("%.*s", (int) word_len, bufpos);
3447 ptr = g_get_current_dir ();
3448 path = g_strconcat (ptr, G_DIR_SEPARATOR_S, (char *) NULL);
3449 g_free (ptr);
3451 /* Recursive search file 'TAGS' in parent dirs */
3454 ptr = g_path_get_dirname (path);
3455 g_free (path);
3456 path = ptr;
3457 g_free (tagfile);
3458 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3459 if (exist_file (tagfile))
3460 break;
3462 while (strcmp (path, G_DIR_SEPARATOR_S) != 0);
3464 if (tagfile)
3466 num_def =
3467 etags_set_definition_hash (tagfile, path, match_expr, (etags_hash_t *) & def_hash);
3468 g_free (tagfile);
3470 g_free (path);
3472 max_len = MAX_WIDTH_DEF_DIALOG;
3473 word_len = 0;
3474 if (num_def > 0)
3476 editcmd_dialog_select_definition_show (edit, match_expr, max_len, word_len,
3477 (etags_hash_t *) & def_hash, num_def);
3479 g_free (match_expr);
3482 /* --------------------------------------------------------------------------------------------- */