Ticket #1868: mcedit hang up on replace with regexp.
[midnight-commander.git] / src / editor / editcmd.c
blobd8f39c6c24d87640435dcfef64a24602188be421
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 char *savename = 0;
127 gchar *real_filename;
128 int this_save_mode, fd = -1;
130 if (!filename)
131 return 0;
132 if (!*filename)
133 return 0;
135 if (*filename != PATH_SEP && edit->dir)
137 real_filename = concat_dir_and_file (edit->dir, filename);
139 else
141 real_filename = g_strdup (filename);
144 this_save_mode = option_save_mode;
145 if (this_save_mode != EDIT_QUICK_SAVE)
147 vfs_path_t *vpath = vfs_path_from_str (real_filename);
148 if (!vfs_file_is_local (vpath) || (fd = mc_open (real_filename, O_RDONLY | O_BINARY)) == -1)
151 * The file does not exists yet, so no safe save or
152 * backup are necessary.
154 this_save_mode = EDIT_QUICK_SAVE;
156 vfs_path_free (vpath);
157 if (fd != -1)
158 mc_close (fd);
161 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt)
163 int rv;
164 struct stat sb;
166 rv = mc_stat (real_filename, &sb);
167 if (rv == 0 && sb.st_nlink > 1)
169 rv = edit_query_dialog3 (_("Warning"),
170 _("File has hard-links. Detach before saving?"),
171 _("&Yes"), _("&No"), _("&Cancel"));
172 switch (rv)
174 case 0:
175 this_save_mode = EDIT_SAFE_SAVE;
176 /* fallthrough */
177 case 1:
178 edit->skip_detach_prompt = 1;
179 break;
180 default:
181 g_free (real_filename);
182 return -1;
186 /* Prevent overwriting changes from other editor sessions. */
187 if (rv == 0 && edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
190 /* The default action is "Cancel". */
191 query_set_sel (1);
193 rv = edit_query_dialog2 (_("Warning"),
194 _("The file has been modified in the meantime. Save anyway?"),
195 _("&Yes"), _("&Cancel"));
196 if (rv != 0)
198 g_free (real_filename);
199 return -1;
204 if (this_save_mode != EDIT_QUICK_SAVE)
206 char *savedir, *saveprefix;
207 const char *slashpos;
208 slashpos = strrchr (real_filename, PATH_SEP);
209 if (slashpos)
211 savedir = g_strdup (real_filename);
212 savedir[slashpos - real_filename + 1] = '\0';
214 else
215 savedir = g_strdup (".");
216 saveprefix = concat_dir_and_file (savedir, "cooledit");
217 g_free (savedir);
218 fd = mc_mkstemps (&savename, saveprefix, NULL);
219 g_free (saveprefix);
220 if (!savename)
222 g_free (real_filename);
223 return 0;
225 /* FIXME:
226 * Close for now because mc_mkstemps use pure open system call
227 * to create temporary file and it needs to be reopened by
228 * VFS-aware mc_open().
230 close (fd);
232 else
233 savename = g_strdup (real_filename);
235 int ret;
236 ret = mc_chown (savename, edit->stat1.st_uid, edit->stat1.st_gid);
237 ret = mc_chmod (savename, edit->stat1.st_mode);
240 fd = mc_open (savename, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
241 if (fd == -1)
242 goto error_save;
244 /* pipe save */
245 p = edit_get_write_filter (savename, real_filename);
246 if (p != NULL)
248 FILE *file;
250 mc_close (fd);
251 file = (FILE *) popen (p, "w");
253 if (file)
255 filelen = edit_write_stream (edit, file);
256 #if 1
257 pclose (file);
258 #else
259 if (pclose (file) != 0)
261 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
262 edit_error_dialog (_("Error"), tmp);
263 g_free (tmp);
264 g_free (p);
265 goto error_save;
267 #endif
269 else
271 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
272 edit_error_dialog (_("Error"), get_sys_error (tmp));
273 g_free (p);
274 g_free (tmp);
275 goto error_save;
277 g_free (p);
279 else if (edit->lb == LB_ASIS)
280 { /* do not change line breaks */
281 long buf;
282 buf = 0;
283 filelen = edit->last_byte;
284 while (buf <= (edit->curs1 >> S_EDIT_BUF_SIZE) - 1)
286 if (mc_write (fd, (char *) edit->buffers1[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
288 mc_close (fd);
289 goto error_save;
291 buf++;
293 if (mc_write
294 (fd, (char *) edit->buffers1[buf],
295 edit->curs1 & M_EDIT_BUF_SIZE) != (edit->curs1 & M_EDIT_BUF_SIZE))
297 filelen = -1;
299 else if (edit->curs2)
301 edit->curs2--;
302 buf = (edit->curs2 >> S_EDIT_BUF_SIZE);
303 if (mc_write
304 (fd,
305 (char *) edit->buffers2[buf] + EDIT_BUF_SIZE -
306 (edit->curs2 & M_EDIT_BUF_SIZE) - 1,
307 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) != 1 + (edit->curs2 & M_EDIT_BUF_SIZE))
309 filelen = -1;
311 else
313 while (--buf >= 0)
315 if (mc_write (fd, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
317 filelen = -1;
318 break;
322 edit->curs2++;
324 if (mc_close (fd))
325 goto error_save;
327 /* Update the file information, especially the mtime. */
328 if (mc_stat (savename, &edit->stat1) == -1)
329 goto error_save;
331 else
332 { /* change line breaks */
333 FILE *file;
335 mc_close (fd);
337 file = (FILE *) fopen (savename, "w");
339 if (file)
341 filelen = edit_write_stream (edit, file);
342 fclose (file);
344 else
346 char *msg;
348 msg = g_strdup_printf (_("Cannot open file for writing: %s"), savename);
349 edit_error_dialog (_("Error"), msg);
350 g_free (msg);
351 goto error_save;
355 if (filelen != edit->last_byte)
356 goto error_save;
358 if (this_save_mode == EDIT_DO_BACKUP)
360 #ifdef HAVE_ASSERT_H
361 assert (option_backup_ext != NULL);
362 #endif
363 tmp = g_strconcat (real_filename, option_backup_ext, (char *) NULL);
364 if (mc_rename (real_filename, tmp) == -1)
366 g_free (tmp);
367 goto error_save;
371 if (this_save_mode != EDIT_QUICK_SAVE)
372 if (mc_rename (savename, real_filename) == -1)
373 goto error_save;
374 g_free (savename);
375 g_free (real_filename);
376 return 1;
377 error_save:
378 /* FIXME: Is this safe ?
379 * if (this_save_mode != EDIT_QUICK_SAVE)
380 * mc_unlink (savename);
382 g_free (real_filename);
383 g_free (savename);
384 return 0;
387 /* --------------------------------------------------------------------------------------------- */
389 static gboolean
390 edit_check_newline (WEdit * edit)
392 return !(option_check_nl_at_eof && edit->last_byte > 0
393 && edit_get_byte (edit, edit->last_byte - 1) != '\n'
394 && edit_query_dialog2 (_("Warning"),
395 _("The file you are saving is not finished with a newline"),
396 _("C&ontinue"), _("&Cancel")));
399 /* --------------------------------------------------------------------------------------------- */
401 static char *
402 edit_get_save_file_as (WEdit * edit)
404 #define DLG_WIDTH 64
405 #define DLG_HEIGHT 14
407 static LineBreaks cur_lb = LB_ASIS;
409 char *filename = edit->filename;
411 const char *lb_names[LB_NAMES] = {
412 N_("&Do not change"),
413 N_("&Unix format (LF)"),
414 N_("&Windows/DOS format (CR LF)"),
415 N_("&Macintosh format (CR)")
418 QuickWidget quick_widgets[] = {
419 QUICK_BUTTON (6, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
420 QUICK_BUTTON (2, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
421 QUICK_RADIO (5, DLG_WIDTH, DLG_HEIGHT - 8, DLG_HEIGHT, LB_NAMES, lb_names, (int *) &cur_lb),
422 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 9, DLG_HEIGHT, N_("Change line breaks to:")),
423 QUICK_INPUT (3, DLG_WIDTH, DLG_HEIGHT - 11, DLG_HEIGHT, filename, DLG_WIDTH - 6, 0,
424 "save-as", &filename),
425 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 12, DLG_HEIGHT, N_("Enter file name:")),
426 QUICK_END
429 QuickDialog Quick_options = {
430 DLG_WIDTH, DLG_HEIGHT, -1, -1,
431 N_("Save As"), "[Save File As]",
432 quick_widgets, NULL, FALSE
435 if (quick_dialog (&Quick_options) != B_CANCEL)
437 char *fname;
439 edit->lb = cur_lb;
440 fname = tilde_expand (filename);
441 g_free (filename);
442 return fname;
445 return NULL;
447 #undef DLG_WIDTH
448 #undef DLG_HEIGHT
451 /** returns 1 on success */
453 static int
454 edit_save_cmd (WEdit * edit)
456 int res, save_lock = 0;
458 if (!edit->locked && !edit->delete_file)
459 save_lock = edit_lock_file (edit);
460 res = edit_save_file (edit, edit->filename);
462 /* Maintain modify (not save) lock on failure */
463 if ((res > 0 && edit->locked) || save_lock)
464 edit->locked = edit_unlock_file (edit);
466 /* On failure try 'save as', it does locking on its own */
467 if (!res)
468 return edit_save_as_cmd (edit);
469 edit->force |= REDRAW_COMPLETELY;
470 if (res > 0)
472 edit->delete_file = 0;
473 edit->modified = 0;
476 return 1;
479 /* --------------------------------------------------------------------------------------------- */
480 /** returns 1 on error */
482 static int
483 edit_load_file_from_filename (WEdit * edit, char *exp)
485 int prev_locked = edit->locked;
486 char *prev_filename = g_strdup (edit->filename);
488 if (!edit_reload (edit, exp))
490 g_free (prev_filename);
491 return 1;
494 if (prev_locked)
496 char *fullpath;
498 fullpath = mc_build_filename (edit->dir, prev_filename, (char *) NULL);
499 unlock_file (fullpath);
500 g_free (fullpath);
502 g_free (prev_filename);
503 return 0;
506 /* --------------------------------------------------------------------------------------------- */
508 static void
509 edit_load_syntax_file (WEdit * edit)
511 char *extdir;
512 int dir = 0;
514 if (geteuid () == 0)
516 dir = query_dialog (_("Syntax file edit"),
517 _("Which syntax file you want to edit?"), D_NORMAL, 2,
518 _("&User"), _("&System Wide"));
521 extdir = g_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
522 if (!exist_file (extdir))
524 g_free (extdir);
525 extdir = g_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
528 if (dir == 0)
530 char *buffer;
532 buffer = mc_config_get_full_path (EDIT_SYNTAX_FILE);
533 check_for_default (extdir, buffer);
534 edit_load_file_from_filename (edit, buffer);
535 g_free (buffer);
537 else if (dir == 1)
538 edit_load_file_from_filename (edit, extdir);
540 g_free (extdir);
543 /* --------------------------------------------------------------------------------------------- */
545 static void
546 edit_load_menu_file (WEdit * edit)
548 char *buffer;
549 char *menufile;
550 int dir = 0;
552 dir = query_dialog (_("Menu edit"),
553 _("Which menu file do you want to edit?"), D_NORMAL,
554 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
556 menufile = concat_dir_and_file (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU);
558 if (!exist_file (menufile))
560 g_free (menufile);
561 menufile = concat_dir_and_file (mc_global.share_data_dir, EDIT_GLOBAL_MENU);
564 switch (dir)
566 case 0:
567 buffer = g_strdup (EDIT_LOCAL_MENU);
568 check_for_default (menufile, buffer);
569 chmod (buffer, 0600);
570 break;
572 case 1:
573 buffer = mc_config_get_full_path (EDIT_HOME_MENU);
574 check_for_default (menufile, buffer);
575 break;
577 case 2:
578 buffer = concat_dir_and_file (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU);
579 if (!exist_file (buffer))
581 g_free (buffer);
582 buffer = concat_dir_and_file (mc_global.share_data_dir, EDIT_GLOBAL_MENU);
584 break;
586 default:
587 g_free (menufile);
588 return;
591 edit_load_file_from_filename (edit, buffer);
593 g_free (buffer);
594 g_free (menufile);
597 /* --------------------------------------------------------------------------------------------- */
599 static void
600 edit_delete_column_of_text (WEdit * edit)
602 long p, q, r, m1, m2;
603 long b, c, d, n;
605 eval_marks (edit, &m1, &m2);
606 n = edit_move_forward (edit, m1, 0, m2) + 1;
607 c = edit_move_forward3 (edit, edit_bol (edit, m1), 0, m1);
608 d = edit_move_forward3 (edit, edit_bol (edit, m2), 0, m2);
609 b = max (min (c, d), min (edit->column1, edit->column2));
610 c = max (c, max (edit->column1, edit->column2));
612 while (n--)
614 r = edit_bol (edit, edit->curs1);
615 p = edit_move_forward3 (edit, r, b, 0);
616 q = edit_move_forward3 (edit, r, c, 0);
617 if (p < m1)
618 p = m1;
619 if (q > m2)
620 q = m2;
621 edit_cursor_move (edit, p - edit->curs1);
622 while (q > p)
624 /* delete line between margins */
625 if (edit_get_byte (edit, edit->curs1) != '\n')
626 edit_delete (edit, 1);
627 q--;
629 if (n)
630 /* move to next line except on the last delete */
631 edit_cursor_move (edit, edit_move_forward (edit, edit->curs1, 1, 0) - edit->curs1);
635 /* --------------------------------------------------------------------------------------------- */
636 /** if success return 0 */
638 static int
639 edit_block_delete (WEdit * edit)
641 long count;
642 long start_mark, end_mark;
643 int curs_pos, line_width;
644 long curs_line, c1, c2;
646 if (eval_marks (edit, &start_mark, &end_mark))
647 return 0;
648 if (edit->column_highlight && edit->mark2 < 0)
649 edit_mark_cmd (edit, 0);
650 if ((end_mark - start_mark) > option_max_undo / 2)
652 /* Warning message with a query to continue or cancel the operation */
653 if (edit_query_dialog2
654 (_("Warning"),
656 ("Block is large, you may not be able to undo this action"),
657 _("C&ontinue"), _("&Cancel")))
659 return 1;
662 c1 = min (edit->column1, edit->column2);
663 c2 = max (edit->column1, edit->column2);
664 edit->column1 = c1;
665 edit->column2 = c2;
667 edit_push_markers (edit);
669 curs_line = edit->curs_line;
671 /* calculate line width and cursor position before cut */
672 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
673 edit_eol (edit, edit->curs1));
674 curs_pos = edit->curs_col + edit->over_col;
676 /* move cursor to start of selection */
677 edit_cursor_move (edit, start_mark - edit->curs1);
678 edit_scroll_screen_over_cursor (edit);
679 count = start_mark;
680 if (start_mark < end_mark)
682 if (edit->column_highlight)
684 if (edit->mark2 < 0)
685 edit_mark_cmd (edit, 0);
686 edit_delete_column_of_text (edit);
687 /* move cursor to the saved position */
688 edit_move_to_line (edit, curs_line);
689 /* calculate line width after cut */
690 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
691 edit_eol (edit, edit->curs1));
692 if (option_cursor_beyond_eol && curs_pos > line_width)
693 edit->over_col = curs_pos - line_width;
695 else
697 while (count < end_mark)
699 edit_delete (edit, 1);
700 count++;
704 edit_set_markers (edit, 0, 0, 0, 0);
705 edit->force |= REDRAW_PAGE;
706 return 0;
709 /* --------------------------------------------------------------------------------------------- */
711 * Get EOL symbol for searching.
713 * @param edit editor object
714 * @return EOL symbol
717 static inline char
718 edit_search_get_current_end_line_char (const WEdit * edit)
720 switch (edit->lb)
722 case LB_MAC:
723 return '\r';
724 default:
725 return '\n';
729 /* --------------------------------------------------------------------------------------------- */
731 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
733 * @param search search object
734 * @return result of checks.
737 static edit_search_line_t
738 edit_get_search_line_type (mc_search_t * search)
740 edit_search_line_t search_line_type = 0;
742 if (search->search_type != MC_SEARCH_T_REGEX)
743 return search_line_type;
745 if (*search->original == '^')
746 search_line_type |= AT_START_LINE;
748 if (search->original[search->original_len - 1] == '$')
749 search_line_type |= AT_END_LINE;
750 return search_line_type;
753 /* --------------------------------------------------------------------------------------------- */
755 * Calculating the start position of next line.
757 * @param edit editor object
758 * @param current_pos current position
759 * @param max_pos max position
760 * @param end_string_symbol end of line symbol
761 * @return start position of next line
764 static off_t
765 edit_calculate_start_of_next_line (WEdit * edit, off_t current_pos, off_t max_pos,
766 char end_string_symbol)
768 off_t i;
770 for (i = current_pos; i < max_pos; i++)
772 current_pos++;
773 if (edit_get_byte (edit, i) == end_string_symbol)
774 break;
777 return current_pos;
780 /* --------------------------------------------------------------------------------------------- */
782 * Calculating the end position of previous line.
784 * @param edit editor object
785 * @param current_pos current position
786 * @param end_string_symbol end of line symbol
787 * @return end position of previous line
790 static off_t
791 edit_calculate_end_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
793 off_t i;
795 for (i = current_pos - 1; i >= 0; i--)
796 if (edit_get_byte (edit, i) == end_string_symbol)
797 break;
799 return i;
802 /* --------------------------------------------------------------------------------------------- */
804 * Calculating the start position of previous line.
806 * @param edit editor object
807 * @param current_pos current position
808 * @param end_string_symbol end of line symbol
809 * @return start position of previous line
812 static inline off_t
813 edit_calculate_start_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
815 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
816 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
818 return (current_pos + 1);
821 /* --------------------------------------------------------------------------------------------- */
823 * Calculating the start position of current line.
825 * @param edit editor object
826 * @param current_pos current position
827 * @param end_string_symbol end of line symbol
828 * @return start position of current line
831 static inline off_t
832 edit_calculate_start_of_current_line (WEdit * edit, off_t current_pos, char end_string_symbol)
834 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
836 return (current_pos + 1);
839 /* --------------------------------------------------------------------------------------------- */
841 * Fixing (if needed) search start position if 'only in selection' option present.
843 * @param edit editor object
846 static void
847 edit_search_fix_search_start_if_selection (WEdit * edit)
849 long start_mark = 0;
850 long end_mark = 0;
852 if (!edit_search_options.only_in_selection)
853 return;
855 if (eval_marks (edit, &start_mark, &end_mark) != 0)
856 return;
858 if (edit_search_options.backwards)
860 if (edit->search_start > end_mark || edit->search_start <= start_mark)
861 edit->search_start = end_mark;
863 else
865 if (edit->search_start < start_mark || edit->search_start >= end_mark)
866 edit->search_start = start_mark;
870 /* --------------------------------------------------------------------------------------------- */
872 static gboolean
873 editcmd_find (WEdit * edit, gsize * len)
875 off_t search_start = edit->search_start;
876 off_t search_end;
877 long start_mark = 0;
878 long end_mark = edit->last_byte;
879 int mark_res = 0;
880 char end_string_symbol;
882 end_string_symbol = edit_search_get_current_end_line_char (edit);
884 /* prepare for search */
885 if (edit_search_options.only_in_selection)
887 mark_res = eval_marks (edit, &start_mark, &end_mark);
888 if (mark_res != 0)
890 edit->search->error = MC_SEARCH_E_NOTFOUND;
891 edit->search->error_str = g_strdup (_("Search string not found"));
892 return FALSE;
895 /* fix the start and the end of search block positions */
896 if ((edit->search_line_type & AT_START_LINE) != 0
897 && (start_mark != 0 || edit_get_byte (edit, start_mark - 1) != end_string_symbol))
899 start_mark =
900 edit_calculate_start_of_next_line (edit, start_mark, edit->last_byte,
901 end_string_symbol);
903 if ((edit->search_line_type & AT_END_LINE) != 0
904 && (end_mark - 1 != edit->last_byte
905 || edit_get_byte (edit, end_mark) != end_string_symbol))
907 end_mark = edit_calculate_end_of_previous_line (edit, end_mark, end_string_symbol);
909 if (start_mark >= end_mark)
911 edit->search->error = MC_SEARCH_E_NOTFOUND;
912 edit->search->error_str = g_strdup (_("Search string not found"));
913 return FALSE;
916 else
918 if (edit_search_options.backwards)
919 end_mark = max (1, edit->curs1) - 1;
922 /* search */
923 if (edit_search_options.backwards)
925 /* backward search */
926 search_end = end_mark;
928 if ((edit->search_line_type & AT_START_LINE) != 0)
929 search_start =
930 edit_calculate_start_of_current_line (edit, search_start, end_string_symbol);
932 while ((int) search_start >= start_mark)
934 if (search_end > (off_t) (search_start + edit->search->original_len)
935 && mc_search_is_fixed_search_str (edit->search))
937 search_end = search_start + edit->search->original_len;
939 if (mc_search_run (edit->search, (void *) edit, search_start, search_end, len)
940 && edit->search->normal_offset == search_start)
942 return TRUE;
946 if ((edit->search_line_type & AT_START_LINE) != 0)
947 search_start =
948 edit_calculate_start_of_previous_line (edit, search_start, end_string_symbol);
949 else
950 search_start--;
952 edit->search->error_str = g_strdup (_("Search string not found"));
954 else
956 /* forward search */
957 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
958 search_start =
959 edit_calculate_start_of_next_line (edit, search_start, end_mark, end_string_symbol);
960 return mc_search_run (edit->search, (void *) edit, search_start, end_mark, len);
962 return FALSE;
965 /* --------------------------------------------------------------------------------------------- */
967 static char *
968 edit_replace_cmd__conv_to_display (char *str)
970 #ifdef HAVE_CHARSET
971 GString *tmp;
973 tmp = str_convert_to_display (str);
974 if (tmp != NULL)
976 if (tmp->len != 0)
977 return g_string_free (tmp, FALSE);
978 g_string_free (tmp, TRUE);
980 #endif
981 return g_strdup (str);
984 /* --------------------------------------------------------------------------------------------- */
986 static char *
987 edit_replace_cmd__conv_to_input (char *str)
989 #ifdef HAVE_CHARSET
990 GString *tmp;
992 tmp = str_convert_to_input (str);
993 if (tmp != NULL)
995 if (tmp->len != 0)
996 return g_string_free (tmp, FALSE);
997 g_string_free (tmp, TRUE);
999 #endif
1000 return g_strdup (str);
1003 /* --------------------------------------------------------------------------------------------- */
1005 static void
1006 edit_do_search (WEdit * edit)
1008 gsize len = 0;
1010 if (edit->search == NULL)
1011 edit->search_start = edit->curs1;
1013 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1015 if (search_create_bookmark)
1017 int found = 0, books = 0;
1018 long l = 0, l_last = -1;
1019 long q = 0;
1021 search_create_bookmark = FALSE;
1022 book_mark_flush (edit, -1);
1024 while (TRUE)
1026 if (!mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
1027 break;
1028 if (found == 0)
1029 edit->search_start = edit->search->normal_offset;
1030 found++;
1031 l += edit_count_lines (edit, q, edit->search->normal_offset);
1032 if (l != l_last)
1034 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
1035 books++;
1037 l_last = l;
1038 q = edit->search->normal_offset + 1;
1041 if (found == 0)
1042 edit_error_dialog (_("Search"), _("Search string not found"));
1043 else
1044 edit_cursor_move (edit, edit->search_start - edit->curs1);
1046 else
1048 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
1049 && edit_search_options.backwards)
1050 edit->search_start--;
1052 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
1053 && !edit_search_options.backwards)
1054 edit->search_start++;
1056 if (editcmd_find (edit, &len))
1058 edit->found_start = edit->search_start = edit->search->normal_offset;
1059 edit->found_len = len;
1060 edit->over_col = 0;
1061 edit_cursor_move (edit, edit->search_start - edit->curs1);
1062 edit_scroll_screen_over_cursor (edit);
1063 if (edit_search_options.backwards)
1064 edit->search_start--;
1065 else
1066 edit->search_start++;
1068 else
1070 edit->search_start = edit->curs1;
1071 if (edit->search->error_str != NULL)
1072 edit_error_dialog (_("Search"), edit->search->error_str);
1076 edit->force |= REDRAW_COMPLETELY;
1077 edit_scroll_screen_over_cursor (edit);
1080 /* --------------------------------------------------------------------------------------------- */
1082 static void
1083 edit_search (WEdit * edit)
1085 if (editcmd_dialog_search_show (edit))
1087 edit->search_line_type = edit_get_search_line_type (edit->search);
1088 edit_search_fix_search_start_if_selection (edit);
1089 edit_do_search (edit);
1093 /* --------------------------------------------------------------------------------------------- */
1094 /** Return a null terminated length of text. Result must be g_free'd */
1096 static unsigned char *
1097 edit_get_block (WEdit * edit, long start, long finish, int *l)
1099 unsigned char *s, *r;
1100 r = s = g_malloc0 (finish - start + 1);
1101 if (edit->column_highlight)
1103 *l = 0;
1104 /* copy from buffer, excluding chars that are out of the column 'margins' */
1105 while (start < finish)
1107 int c;
1108 long x;
1109 x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
1110 c = edit_get_byte (edit, start);
1111 if ((x >= edit->column1 && x < edit->column2)
1112 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1114 *s++ = c;
1115 (*l)++;
1117 start++;
1120 else
1122 *l = finish - start;
1123 while (start < finish)
1124 *s++ = edit_get_byte (edit, start++);
1126 *s = 0;
1127 return r;
1130 /* --------------------------------------------------------------------------------------------- */
1131 /** copies a block to clipboard file */
1133 static int
1134 edit_save_block_to_clip_file (WEdit * edit, long start, long finish)
1136 int ret;
1137 gchar *tmp;
1138 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1139 ret = edit_save_block (edit, tmp, start, finish);
1140 g_free (tmp);
1141 return ret;
1144 /* --------------------------------------------------------------------------------------------- */
1146 static void
1147 pipe_mail (WEdit * edit, char *to, char *subject, char *cc)
1149 FILE *p = 0;
1150 char *s;
1152 to = name_quote (to, 0);
1153 subject = name_quote (subject, 0);
1154 cc = name_quote (cc, 0);
1155 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1156 g_free (to);
1157 g_free (subject);
1158 g_free (cc);
1160 if (s)
1162 p = popen (s, "w");
1163 g_free (s);
1166 if (p)
1168 long i;
1169 for (i = 0; i < edit->last_byte; i++)
1170 fputc (edit_get_byte (edit, i), p);
1171 pclose (p);
1175 /* --------------------------------------------------------------------------------------------- */
1177 static gboolean
1178 is_break_char (char c)
1180 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
1183 /* --------------------------------------------------------------------------------------------- */
1184 /** find first character of current word */
1186 static int
1187 edit_find_word_start (WEdit * edit, long *word_start, gsize * word_len)
1189 int c, last;
1190 gsize i;
1192 /* return if at begin of file */
1193 if (edit->curs1 <= 0)
1194 return 0;
1196 c = (unsigned char) edit_get_byte (edit, edit->curs1 - 1);
1197 /* return if not at end or in word */
1198 if (is_break_char (c))
1199 return 0;
1201 /* search start of word to be completed */
1202 for (i = 2;; i++)
1204 /* return if at begin of file */
1205 if ((gsize) edit->curs1 < i)
1206 return 0;
1208 last = c;
1209 c = (unsigned char) edit_get_byte (edit, edit->curs1 - i);
1211 if (is_break_char (c))
1213 /* return if word starts with digit */
1214 if (isdigit (last))
1215 return 0;
1217 *word_start = edit->curs1 - (i - 1); /* start found */
1218 *word_len = i - 1;
1219 break;
1222 /* success */
1223 return 1;
1226 /* --------------------------------------------------------------------------------------------- */
1228 * Get current word under cursor
1230 * @param edit editor object
1231 * @param srch mc_search object
1232 * @param word_start start word position
1234 * @return newly allocated string or NULL if no any words under cursor
1237 static char *
1238 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, long word_start)
1240 gsize len = 0, i;
1241 GString *temp;
1243 if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len))
1244 return NULL;
1246 temp = g_string_sized_new (len);
1248 for (i = 0; i < len; i++)
1250 int chr;
1252 chr = edit_get_byte (edit, word_start + i);
1253 if (!isspace (chr))
1254 g_string_append_c (temp, chr);
1257 return g_string_free (temp, temp->len == 0);
1260 /* --------------------------------------------------------------------------------------------- */
1261 /** collect the possible completions */
1263 static gsize
1264 edit_collect_completions (WEdit * edit, long word_start, gsize word_len,
1265 char *match_expr, struct selection *compl, gsize * num)
1267 gsize len = 0;
1268 gsize max_len = 0;
1269 gsize i;
1270 int skip;
1271 GString *temp;
1272 mc_search_t *srch;
1273 long last_byte, start = -1;
1274 char *current_word;
1276 srch = mc_search_new (match_expr, -1);
1277 if (srch == NULL)
1278 return 0;
1280 if (mc_config_get_bool
1281 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1283 last_byte = edit->last_byte;
1285 else
1287 last_byte = word_start;
1290 srch->search_type = MC_SEARCH_T_REGEX;
1291 srch->is_case_sensitive = TRUE;
1292 srch->search_fn = edit_search_cmd_callback;
1294 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1296 temp = g_string_new ("");
1298 /* collect max MAX_WORD_COMPLETIONS completions */
1299 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1301 g_string_set_size (temp, 0);
1302 start = srch->normal_offset;
1304 /* add matched completion if not yet added */
1305 for (i = 0; i < len; i++)
1307 skip = edit_get_byte (edit, start + i);
1308 if (isspace (skip))
1309 continue;
1311 /* skip current word */
1312 if (start + (long) i == word_start)
1313 break;
1315 g_string_append_c (temp, skip);
1318 if (temp->len == 0)
1319 continue;
1321 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1322 continue;
1324 skip = 0;
1326 for (i = 0; i < *num; i++)
1328 if (strncmp
1329 ((char *) &compl[i].text[word_len],
1330 (char *) &temp->str[word_len], max (len, compl[i].len) - word_len) == 0)
1332 struct selection this = compl[i];
1333 for (++i; i < *num; i++)
1335 compl[i - 1] = compl[i];
1337 compl[*num - 1] = this;
1338 skip = 1;
1339 break; /* skip it, already added */
1342 if (skip != 0)
1343 continue;
1345 if (*num == MAX_WORD_COMPLETIONS)
1347 g_free (compl[0].text);
1348 for (i = 1; i < *num; i++)
1350 compl[i - 1] = compl[i];
1352 (*num)--;
1354 #ifdef HAVE_CHARSET
1356 GString *recoded;
1357 recoded = str_convert_to_display (temp->str);
1359 if (recoded && recoded->len)
1360 g_string_assign (temp, recoded->str);
1362 g_string_free (recoded, TRUE);
1364 #endif
1365 compl[*num].text = g_strdup (temp->str);
1366 compl[*num].len = temp->len;
1367 (*num)++;
1368 start += len;
1370 /* note the maximal length needed for the completion dialog */
1371 if (len > max_len)
1372 max_len = len;
1375 mc_search_free (srch);
1376 g_string_free (temp, TRUE);
1377 g_free (current_word);
1379 return max_len;
1382 /* --------------------------------------------------------------------------------------------- */
1384 static void
1385 edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width,
1386 long *start_pos, long *end_pos, int *col1, int *col2)
1388 long cursor;
1389 int i, col;
1391 cursor = edit->curs1;
1392 col = edit_get_col (edit);
1394 for (i = 0; i < size; i++)
1396 if (data[i] != '\n')
1397 edit_insert (edit, data[i]);
1398 else
1399 { /* fill in and move to next line */
1400 int l;
1401 long p;
1403 if (edit_get_byte (edit, edit->curs1) != '\n')
1405 l = width - (edit_get_col (edit) - col);
1406 while (l > 0)
1408 edit_insert (edit, ' ');
1409 l -= space_width;
1412 for (p = edit->curs1;; p++)
1414 if (p == edit->last_byte)
1416 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1417 edit_insert_ahead (edit, '\n');
1418 p++;
1419 break;
1421 if (edit_get_byte (edit, p) == '\n')
1423 p++;
1424 break;
1427 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1428 l = col - edit_get_col (edit);
1429 while (l >= space_width)
1431 edit_insert (edit, ' ');
1432 l -= space_width;
1437 *col1 = col;
1438 *col2 = col + width;
1439 *start_pos = cursor;
1440 *end_pos = edit->curs1;
1441 edit_cursor_move (edit, cursor - edit->curs1);
1444 /* --------------------------------------------------------------------------------------------- */
1446 static int
1447 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1449 const macros_t *m1 = (const macros_t *) macro1;
1450 const macros_t *m2 = (const macros_t *) macro2;
1452 return m1->hotkey - m2->hotkey;
1455 /* --------------------------------------------------------------------------------------------- */
1457 static void
1458 edit_macro_sort_by_hotkey (void)
1460 if (macros_list != NULL && macros_list->len != 0)
1461 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1464 /* --------------------------------------------------------------------------------------------- */
1466 static gboolean
1467 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1469 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1470 macros_t *result;
1471 macros_t search_macro;
1473 (void) edit;
1475 search_macro.hotkey = hotkey;
1476 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1477 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1479 if (result != NULL && result->macro != NULL)
1481 *indx = (result - array_start);
1482 *macros = result;
1483 return TRUE;
1485 *indx = 0;
1486 return FALSE;
1489 /* --------------------------------------------------------------------------------------------- */
1490 /** returns FALSE on error */
1492 static gboolean
1493 edit_delete_macro (WEdit * edit, int hotkey)
1495 mc_config_t *macros_config = NULL;
1496 const char *section_name = "editor";
1497 gchar *macros_fname;
1498 guint indx;
1499 char *skeyname;
1500 const macros_t *macros = NULL;
1502 /* clear array of actions for current hotkey */
1503 while (edit_get_macro (edit, hotkey, &macros, &indx))
1505 if (macros->macro != NULL)
1506 g_array_free (macros->macro, TRUE);
1507 macros = NULL;
1508 g_array_remove_index (macros_list, indx);
1509 edit_macro_sort_by_hotkey ();
1512 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1513 macros_config = mc_config_init (macros_fname);
1514 g_free (macros_fname);
1516 if (macros_config == NULL)
1517 return FALSE;
1519 skeyname = lookup_key_by_code (hotkey);
1520 while (mc_config_del_key (macros_config, section_name, skeyname))
1522 g_free (skeyname);
1523 mc_config_save_file (macros_config, NULL);
1524 mc_config_deinit (macros_config);
1525 return TRUE;
1529 /* --------------------------------------------------------------------------------------------- */
1530 /*** public functions ****************************************************************************/
1531 /* --------------------------------------------------------------------------------------------- */
1533 void
1534 edit_help_cmd (WEdit * edit)
1536 ev_help_t event_data = { NULL, "[Internal File Editor]" };
1537 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
1539 edit->force |= REDRAW_COMPLETELY;
1542 /* --------------------------------------------------------------------------------------------- */
1544 void
1545 edit_refresh_cmd (WEdit * edit)
1547 #ifdef HAVE_SLANG
1548 int color;
1550 edit_get_syntax_color (edit, -1, &color);
1551 tty_touch_screen ();
1552 mc_refresh ();
1553 #else
1554 (void) edit;
1556 clr_scr ();
1557 repaint_screen ();
1558 #endif /* !HAVE_SLANG */
1559 tty_keypad (TRUE);
1562 /* --------------------------------------------------------------------------------------------- */
1564 void
1565 menu_save_mode_cmd (void)
1567 /* diaog sizes */
1568 const int DLG_X = 38;
1569 const int DLG_Y = 13;
1571 char *str_result;
1573 const char *str[] = {
1574 N_("&Quick save"),
1575 N_("&Safe save"),
1576 N_("&Do backups with following extension:")
1579 QuickWidget widgets[] = {
1580 /* 0 */
1581 QUICK_BUTTON (18, DLG_X, DLG_Y - 3, DLG_Y, N_("&Cancel"), B_CANCEL, NULL),
1582 /* 1 */
1583 QUICK_BUTTON (6, DLG_X, DLG_Y - 3, DLG_Y, N_("&OK"), B_ENTER, NULL),
1584 /* 2 */
1585 QUICK_CHECKBOX (4, DLG_X, 8, DLG_Y, N_("Check &POSIX new line"), &option_check_nl_at_eof),
1586 /* 3 */
1587 QUICK_INPUT (8, DLG_X, 6, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result),
1588 /* 4 */
1589 QUICK_RADIO (4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode),
1590 QUICK_END
1593 QuickDialog dialog = {
1594 DLG_X, DLG_Y, -1, -1, N_("Edit Save Mode"),
1595 "[Edit Save Mode]", widgets, NULL, FALSE
1598 size_t i;
1599 size_t maxlen = 0;
1600 size_t w0, w1, b_len, w3;
1602 #ifdef HAVE_ASSERT_H
1603 assert (option_backup_ext != NULL);
1604 #endif
1606 /* OK/Cancel buttons */
1607 w0 = str_term_width1 (_(widgets[0].u.button.text)) + 3;
1608 w1 = str_term_width1 (_(widgets[1].u.button.text)) + 5; /* default button */
1609 b_len = w0 + w1 + 3;
1611 maxlen = max (b_len, (size_t) str_term_width1 (_(dialog.title)) + 2);
1613 w3 = 0;
1614 for (i = 0; i < 3; i++)
1616 #ifdef ENABLE_NLS
1617 str[i] = _(str[i]);
1618 #endif
1619 w3 = max (w3, (size_t) str_term_width1 (str[i]));
1622 maxlen = max (maxlen, w3 + 4);
1624 dialog.xlen = min ((size_t) COLS, maxlen + 8);
1626 widgets[3].u.input.len = w3;
1627 widgets[1].relative_x = (dialog.xlen - b_len) / 2;
1628 widgets[0].relative_x = widgets[1].relative_x + w0 + 2;
1630 for (i = 0; i < sizeof (widgets) / sizeof (widgets[0]); i++)
1631 widgets[i].x_divisions = dialog.xlen;
1633 if (quick_dialog (&dialog) != B_CANCEL)
1635 g_free (option_backup_ext);
1636 option_backup_ext = str_result;
1640 /* --------------------------------------------------------------------------------------------- */
1642 void
1643 edit_set_filename (WEdit * edit, const char *name)
1645 g_free (edit->filename);
1647 if (name == NULL)
1648 name = "";
1650 edit->filename = tilde_expand (name);
1651 if (edit->dir == NULL && !g_path_is_absolute (name))
1652 edit->dir = vfs_get_current_dir ();
1655 /* --------------------------------------------------------------------------------------------- */
1656 /* Here we want to warn the users of overwriting an existing file,
1657 but only if they have made a change to the filename */
1658 /* returns 1 on success */
1660 edit_save_as_cmd (WEdit * edit)
1662 /* This heads the 'Save As' dialog box */
1663 char *exp;
1664 int save_lock = 0;
1665 int different_filename = 0;
1667 if (!edit_check_newline (edit))
1668 return 0;
1670 exp = edit_get_save_file_as (edit);
1671 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1673 if (exp)
1675 if (!*exp)
1677 g_free (exp);
1678 edit->force |= REDRAW_COMPLETELY;
1679 return 0;
1681 else
1683 int rv;
1684 if (strcmp (edit->filename, exp))
1686 int file;
1687 struct stat sb;
1689 if (mc_stat (exp, &sb) == 0 && !S_ISREG (sb.st_mode))
1691 edit_error_dialog (_("Save as"),
1692 get_sys_error (_
1693 ("Cannot save: destination is not a regular file")));
1694 g_free (exp);
1695 edit->force |= REDRAW_COMPLETELY;
1696 return 0;
1699 different_filename = 1;
1700 file = mc_open (exp, O_RDONLY | O_BINARY);
1701 if (file != -1)
1703 /* the file exists */
1704 mc_close (file);
1705 /* Overwrite the current file or cancel the operation */
1706 if (edit_query_dialog2
1707 (_("Warning"),
1708 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1710 edit->force |= REDRAW_COMPLETELY;
1711 g_free (exp);
1712 return 0;
1715 else
1717 edit->stat1.st_mode |= S_IWUSR;
1719 save_lock = lock_file (exp);
1721 else
1723 /* filenames equal, check if already locked */
1724 if (!edit->locked && !edit->delete_file)
1725 save_lock = lock_file (exp);
1728 if (different_filename)
1731 * Allow user to write into saved (under another name) file
1732 * even if original file had r/o user permissions.
1734 edit->stat1.st_mode |= S_IWRITE;
1737 rv = edit_save_file (edit, exp);
1738 switch (rv)
1740 case 1:
1741 /* Succesful, so unlock both files */
1742 if (different_filename)
1744 if (save_lock)
1745 unlock_file (exp);
1746 if (edit->locked)
1747 edit->locked = edit_unlock_file (edit);
1749 else
1751 if (edit->locked || save_lock)
1752 edit->locked = edit_unlock_file (edit);
1755 edit_set_filename (edit, exp);
1756 if (edit->lb != LB_ASIS)
1757 edit_reload (edit, exp);
1758 g_free (exp);
1759 edit->modified = 0;
1760 edit->delete_file = 0;
1761 if (different_filename)
1762 edit_load_syntax (edit, NULL, edit->syntax_type);
1763 edit->force |= REDRAW_COMPLETELY;
1764 return 1;
1765 default:
1766 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1767 /* fallthrough */
1768 case -1:
1769 /* Failed, so maintain modify (not save) lock */
1770 if (save_lock)
1771 unlock_file (exp);
1772 g_free (exp);
1773 edit->force |= REDRAW_COMPLETELY;
1774 return 0;
1778 edit->force |= REDRAW_COMPLETELY;
1779 return 0;
1782 /* {{{ Macro stuff starts here */
1783 /* --------------------------------------------------------------------------------------------- */
1785 void
1786 edit_delete_macro_cmd (WEdit * edit)
1788 int hotkey;
1790 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1792 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1793 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1796 /* --------------------------------------------------------------------------------------------- */
1798 /** returns FALSE on error */
1799 gboolean
1800 edit_execute_macro (WEdit * edit, int hotkey)
1802 gboolean res = FALSE;
1804 if (hotkey != 0)
1806 const macros_t *macros;
1807 guint indx;
1809 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1810 macros->macro != NULL && macros->macro->len != 0)
1812 guint i;
1814 edit->force |= REDRAW_PAGE;
1816 for (i = 0; i < macros->macro->len; i++)
1818 const macro_action_t *m_act;
1820 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1821 edit_execute_cmd (edit, m_act->action, m_act->ch);
1822 res = TRUE;
1827 return res;
1830 /* --------------------------------------------------------------------------------------------- */
1832 /** returns FALSE on error */
1833 gboolean
1834 edit_store_macro_cmd (WEdit * edit)
1836 int i;
1837 int hotkey;
1838 GString *marcros_string;
1839 mc_config_t *macros_config = NULL;
1840 const char *section_name = "editor";
1841 gchar *macros_fname;
1842 GArray *macros; /* current macro */
1843 int tmp_act;
1844 gboolean have_macro = FALSE;
1845 char *skeyname = NULL;
1847 hotkey = editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1848 if (hotkey == ESC_CHAR)
1849 return FALSE;
1851 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1853 /* return FALSE if try assign macro into restricted hotkeys */
1854 if (tmp_act == CK_MacroStartRecord
1855 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1856 return FALSE;
1858 edit_delete_macro (edit, hotkey);
1860 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1861 macros_config = mc_config_init (macros_fname);
1862 g_free (macros_fname);
1864 if (macros_config == NULL)
1865 return FALSE;
1867 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1869 marcros_string = g_string_sized_new (250);
1870 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1872 skeyname = lookup_key_by_code (hotkey);
1874 for (i = 0; i < macro_index; i++)
1876 macro_action_t m_act;
1877 const char *action_name;
1879 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1881 if (action_name == NULL)
1882 break;
1884 m_act.action = record_macro_buf[i].action;
1885 m_act.ch = record_macro_buf[i].ch;
1886 g_array_append_val (macros, m_act);
1887 have_macro = TRUE;
1888 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1889 (int) record_macro_buf[i].ch);
1891 if (have_macro)
1893 macros_t macro;
1894 macro.hotkey = hotkey;
1895 macro.macro = macros;
1896 g_array_append_val (macros_list, macro);
1897 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1899 else
1900 mc_config_del_key (macros_config, section_name, skeyname);
1902 g_free (skeyname);
1903 edit_macro_sort_by_hotkey ();
1905 g_string_free (marcros_string, TRUE);
1906 mc_config_save_file (macros_config, NULL);
1907 mc_config_deinit (macros_config);
1908 return TRUE;
1911 /* --------------------------------------------------------------------------------------------- */
1913 gboolean
1914 edit_repeat_macro_cmd (WEdit * edit)
1916 int i, j;
1917 char *f;
1918 long count_repeat;
1919 char *error = NULL;
1921 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL);
1922 if (f == NULL || *f == '\0')
1924 g_free (f);
1925 return FALSE;
1928 count_repeat = strtol (f, &error, 0);
1930 if (*error != '\0')
1932 g_free (f);
1933 return FALSE;
1936 g_free (f);
1938 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1939 edit->force |= REDRAW_PAGE;
1941 for (j = 0; j < count_repeat; j++)
1942 for (i = 0; i < macro_index; i++)
1943 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1944 edit_update_screen (edit);
1945 return TRUE;
1948 /* --------------------------------------------------------------------------------------------- */
1949 /** return FALSE on error */
1951 gboolean
1952 edit_load_macro_cmd (WEdit * edit)
1954 mc_config_t *macros_config = NULL;
1955 gchar **profile_keys, **keys;
1956 gchar **values, **curr_values;
1957 gsize len, values_len;
1958 const char *section_name = "editor";
1959 gchar *macros_fname;
1960 int hotkey;
1962 (void) edit;
1964 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1965 macros_config = mc_config_init (macros_fname);
1966 g_free (macros_fname);
1968 if (macros_config == NULL)
1969 return FALSE;
1971 profile_keys = keys = mc_config_get_keys (macros_config, section_name, &len);
1972 while (*profile_keys != NULL)
1974 gboolean have_macro;
1975 GArray *macros;
1976 macros_t macro;
1978 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1980 curr_values = values = mc_config_get_string_list (macros_config, section_name,
1981 *profile_keys, &values_len);
1982 hotkey = lookup_key (*profile_keys, NULL);
1983 have_macro = FALSE;
1985 while (*curr_values != NULL && *curr_values[0] != '\0')
1987 char **macro_pair = NULL;
1989 macro_pair = g_strsplit (*curr_values, ":", 2);
1991 if (macro_pair != NULL)
1993 macro_action_t m_act;
1994 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
1995 m_act.action = 0;
1996 else
1998 m_act.action = keybind_lookup_action (macro_pair[0]);
1999 g_free (macro_pair[0]);
2000 macro_pair[0] = NULL;
2002 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
2003 m_act.ch = -1;
2004 else
2006 m_act.ch = strtol (macro_pair[1], NULL, 0);
2007 g_free (macro_pair[1]);
2008 macro_pair[1] = NULL;
2010 if (m_act.action != 0)
2012 /* a shell command */
2013 if ((m_act.action / CK_PipeBlock (0)) == 1)
2015 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
2016 m_act.ch = -1;
2018 g_array_append_val (macros, m_act);
2019 have_macro = TRUE;
2021 g_strfreev (macro_pair);
2022 macro_pair = NULL;
2024 curr_values++;
2026 if (have_macro)
2028 macro.hotkey = hotkey;
2029 macro.macro = macros;
2030 g_array_append_val (macros_list, macro);
2032 profile_keys++;
2033 g_strfreev (values);
2035 g_strfreev (keys);
2036 mc_config_deinit (macros_config);
2037 edit_macro_sort_by_hotkey ();
2038 return TRUE;
2041 /* }}} Macro stuff end here */
2043 /* --------------------------------------------------------------------------------------------- */
2044 /** returns 1 on success */
2047 edit_save_confirm_cmd (WEdit * edit)
2049 gchar *f = NULL;
2051 if (!edit_check_newline (edit))
2052 return 0;
2054 if (edit_confirm_save)
2056 f = g_strdup_printf (_("Confirm save file: \"%s\""), edit->filename);
2057 if (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")))
2059 g_free (f);
2060 return 0;
2062 g_free (f);
2064 return edit_save_cmd (edit);
2067 /* --------------------------------------------------------------------------------------------- */
2068 /** returns 1 on success */
2071 edit_new_cmd (WEdit * edit)
2073 if (edit->modified)
2075 if (edit_query_dialog2
2076 (_("Warning"),
2078 ("Current text was modified without a file save.\nContinue discards these changes"),
2079 _("C&ontinue"), _("&Cancel")))
2081 edit->force |= REDRAW_COMPLETELY;
2082 return 0;
2085 edit->force |= REDRAW_COMPLETELY;
2087 return edit_renew (edit); /* if this gives an error, something has really screwed up */
2090 /* --------------------------------------------------------------------------------------------- */
2093 edit_load_cmd (WEdit * edit, edit_current_file_t what)
2095 char *exp;
2097 if (edit->modified
2098 && (edit_query_dialog2
2099 (_("Warning"),
2100 _("Current text was modified without a file save.\n"
2101 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")) == 1))
2103 edit->force |= REDRAW_COMPLETELY;
2104 return 0;
2107 switch (what)
2109 case EDIT_FILE_COMMON:
2110 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2111 MC_HISTORY_EDIT_LOAD, edit->filename);
2113 if (exp)
2115 if (*exp)
2116 edit_load_file_from_filename (edit, exp);
2117 g_free (exp);
2119 break;
2121 case EDIT_FILE_SYNTAX:
2122 edit_load_syntax_file (edit);
2123 break;
2125 case EDIT_FILE_MENU:
2126 edit_load_menu_file (edit);
2127 break;
2129 default:
2130 break;
2133 edit->force |= REDRAW_COMPLETELY;
2134 return 0;
2137 /* --------------------------------------------------------------------------------------------- */
2139 if mark2 is -1 then marking is from mark1 to the cursor.
2140 Otherwise its between the markers. This handles this.
2141 Returns 1 if no text is marked.
2145 eval_marks (WEdit * edit, long *start_mark, long *end_mark)
2147 if (edit->mark1 != edit->mark2)
2149 long start_bol, start_eol;
2150 long end_bol, end_eol;
2151 long col1, col2;
2152 long diff1, diff2;
2153 long end_mark_curs;
2155 if (edit->end_mark_curs < 0)
2156 end_mark_curs = edit->curs1;
2157 else
2158 end_mark_curs = edit->end_mark_curs;
2160 if (edit->mark2 >= 0)
2162 *start_mark = min (edit->mark1, edit->mark2);
2163 *end_mark = max (edit->mark1, edit->mark2);
2165 else
2167 *start_mark = min (edit->mark1, end_mark_curs);
2168 *end_mark = max (edit->mark1, end_mark_curs);
2169 edit->column2 = edit->curs_col + edit->over_col;
2172 if (edit->column_highlight
2173 && (((edit->mark1 > end_mark_curs) && (edit->column1 < edit->column2))
2174 || ((edit->mark1 < end_mark_curs) && (edit->column1 > edit->column2))))
2176 start_bol = edit_bol (edit, *start_mark);
2177 start_eol = edit_eol (edit, start_bol - 1) + 1;
2178 end_bol = edit_bol (edit, *end_mark);
2179 end_eol = edit_eol (edit, *end_mark);
2180 col1 = min (edit->column1, edit->column2);
2181 col2 = max (edit->column1, edit->column2);
2183 diff1 =
2184 edit_move_forward3 (edit, start_bol, col2, 0) - edit_move_forward3 (edit, start_bol,
2185 col1, 0);
2186 diff2 =
2187 edit_move_forward3 (edit, end_bol, col2, 0) - edit_move_forward3 (edit, end_bol,
2188 col1, 0);
2190 *start_mark -= diff1;
2191 *end_mark += diff2;
2192 *start_mark = max (*start_mark, start_eol);
2193 *end_mark = min (*end_mark, end_eol);
2195 return 0;
2197 else
2199 *start_mark = *end_mark = 0;
2200 edit->column2 = edit->column1 = 0;
2201 return 1;
2205 /* --------------------------------------------------------------------------------------------- */
2207 void
2208 edit_insert_over (WEdit * edit)
2210 int i;
2212 for (i = 0; i < edit->over_col; i++)
2214 edit_insert (edit, ' ');
2216 edit->over_col = 0;
2219 /* --------------------------------------------------------------------------------------------- */
2222 edit_insert_column_of_text_from_file (WEdit * edit, int file,
2223 long *start_pos, long *end_pos, int *col1, int *col2)
2225 long cursor;
2226 int col;
2227 int blocklen = -1, width = 0;
2228 unsigned char *data;
2230 cursor = edit->curs1;
2231 col = edit_get_col (edit);
2232 data = g_malloc0 (TEMP_BUF_LEN);
2234 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
2236 int i;
2237 for (width = 0; width < blocklen; width++)
2239 if (data[width] == '\n')
2240 break;
2242 for (i = 0; i < blocklen; i++)
2244 if (data[i] == '\n')
2245 { /* fill in and move to next line */
2246 int l;
2247 long p;
2248 if (edit_get_byte (edit, edit->curs1) != '\n')
2250 l = width - (edit_get_col (edit) - col);
2251 while (l > 0)
2253 edit_insert (edit, ' ');
2254 l -= space_width;
2257 for (p = edit->curs1;; p++)
2259 if (p == edit->last_byte)
2261 edit_cursor_move (edit, edit->last_byte - edit->curs1);
2262 edit_insert_ahead (edit, '\n');
2263 p++;
2264 break;
2266 if (edit_get_byte (edit, p) == '\n')
2268 p++;
2269 break;
2272 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
2273 l = col - edit_get_col (edit);
2274 while (l >= space_width)
2276 edit_insert (edit, ' ');
2277 l -= space_width;
2279 continue;
2281 edit_insert (edit, data[i]);
2284 *col1 = col;
2285 *col2 = col + width;
2286 *start_pos = cursor;
2287 *end_pos = edit->curs1;
2288 edit_cursor_move (edit, cursor - edit->curs1);
2289 g_free (data);
2291 return blocklen;
2294 /* --------------------------------------------------------------------------------------------- */
2296 void
2297 edit_block_copy_cmd (WEdit * edit)
2299 long start_mark, end_mark, current = edit->curs1;
2300 long col_delta = 0;
2301 long mark1, mark2;
2302 int c1, c2;
2303 int size;
2304 unsigned char *copy_buf;
2306 edit_update_curs_col (edit);
2307 if (eval_marks (edit, &start_mark, &end_mark))
2308 return;
2310 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2312 /* all that gets pushed are deletes hence little space is used on the stack */
2314 edit_push_markers (edit);
2316 if (edit->column_highlight)
2318 col_delta = abs (edit->column2 - edit->column1);
2319 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2321 else
2323 while (size--)
2324 edit_insert_ahead (edit, copy_buf[size]);
2327 g_free (copy_buf);
2328 edit_scroll_screen_over_cursor (edit);
2330 if (edit->column_highlight)
2331 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2332 else if (start_mark < current && end_mark > current)
2333 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2335 edit->force |= REDRAW_PAGE;
2339 /* --------------------------------------------------------------------------------------------- */
2341 void
2342 edit_block_move_cmd (WEdit * edit)
2344 long current;
2345 unsigned char *copy_buf = NULL;
2346 long start_mark, end_mark;
2347 long line;
2349 if (eval_marks (edit, &start_mark, &end_mark))
2350 return;
2352 line = edit->curs_line;
2353 if (edit->mark2 < 0)
2354 edit_mark_cmd (edit, 0);
2355 edit_push_markers (edit);
2357 if (edit->column_highlight)
2359 long mark1, mark2;
2360 int size;
2361 int b_width = 0;
2362 int c1, c2;
2363 int x, x2;
2365 c1 = min (edit->column1, edit->column2);
2366 c2 = max (edit->column1, edit->column2);
2367 b_width = (c2 - c1);
2369 edit_update_curs_col (edit);
2371 x = edit->curs_col;
2372 x2 = x + edit->over_col;
2374 /* do nothing when cursor inside first line of selected area */
2375 if ((edit_eol (edit, edit->curs1) == edit_eol (edit, start_mark)) && (x2 > c1 && x2 <= c2))
2376 return;
2378 if (edit->curs1 > start_mark && edit->curs1 < edit_eol (edit, end_mark))
2380 if (x > c2)
2381 x -= b_width;
2382 else if (x > c1 && x <= c2)
2383 x = c1;
2385 /* save current selection into buffer */
2386 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2388 /* remove current selection */
2389 edit_block_delete_cmd (edit);
2391 edit->over_col = max (0, edit->over_col - b_width);
2392 /* calculate the cursor pos after delete block */
2393 current = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), x, 0);
2394 edit_cursor_move (edit, current - edit->curs1);
2395 edit_scroll_screen_over_cursor (edit);
2397 /* add TWS if need before block insertion */
2398 if (option_cursor_beyond_eol && edit->over_col > 0)
2399 edit_insert_over (edit);
2401 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2402 edit_set_markers (edit, mark1, mark2, c1, c2);
2404 else
2406 long count;
2408 current = edit->curs1;
2409 copy_buf = g_malloc0 (end_mark - start_mark);
2410 edit_cursor_move (edit, start_mark - edit->curs1);
2411 edit_scroll_screen_over_cursor (edit);
2412 count = start_mark;
2413 while (count < end_mark)
2415 copy_buf[end_mark - count - 1] = edit_delete (edit, 1);
2416 count++;
2418 edit_scroll_screen_over_cursor (edit);
2419 edit_cursor_move (edit,
2420 current - edit->curs1 -
2421 (((current - edit->curs1) > 0) ? end_mark - start_mark : 0));
2422 edit_scroll_screen_over_cursor (edit);
2423 while (count-- > start_mark)
2424 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2425 edit_set_markers (edit, edit->curs1, edit->curs1 + end_mark - start_mark, 0, 0);
2428 edit_scroll_screen_over_cursor (edit);
2429 g_free (copy_buf);
2430 edit->force |= REDRAW_PAGE;
2433 /* --------------------------------------------------------------------------------------------- */
2434 /** returns 1 if canceelled by user */
2437 edit_block_delete_cmd (WEdit * edit)
2439 long start_mark, end_mark;
2440 if (eval_marks (edit, &start_mark, &end_mark))
2442 edit_delete_line (edit);
2443 return 0;
2445 return edit_block_delete (edit);
2448 /* --------------------------------------------------------------------------------------------- */
2449 /** call with edit = 0 before shutdown to close memory leaks */
2451 void
2452 edit_replace_cmd (WEdit * edit, int again)
2454 /* 1 = search string, 2 = replace with */
2455 static char *saved1 = NULL; /* saved default[123] */
2456 static char *saved2 = NULL;
2457 char *input1 = NULL; /* user input from the dialog */
2458 char *input2 = NULL;
2459 char *disp1 = NULL;
2460 char *disp2 = NULL;
2461 long times_replaced = 0;
2462 gboolean once_found = FALSE;
2464 if (!edit)
2466 g_free (saved1), saved1 = NULL;
2467 g_free (saved2), saved2 = NULL;
2468 return;
2471 edit->force |= REDRAW_COMPLETELY;
2473 if (again && !saved1 && !saved2)
2474 again = 0;
2476 if (again)
2478 input1 = g_strdup (saved1 ? saved1 : "");
2479 input2 = g_strdup (saved2 ? saved2 : "");
2481 else
2483 char *tmp_inp1, *tmp_inp2;
2484 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2485 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2487 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2489 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2491 g_free (disp1);
2492 g_free (disp2);
2494 if (input1 == NULL || *input1 == '\0')
2496 edit->force = REDRAW_COMPLETELY;
2497 goto cleanup;
2500 tmp_inp1 = input1;
2501 tmp_inp2 = input2;
2502 input1 = edit_replace_cmd__conv_to_input (input1);
2503 input2 = edit_replace_cmd__conv_to_input (input2);
2504 g_free (tmp_inp1);
2505 g_free (tmp_inp2);
2507 g_free (saved1), saved1 = g_strdup (input1);
2508 g_free (saved2), saved2 = g_strdup (input2);
2510 mc_search_free (edit->search);
2511 edit->search = NULL;
2515 if (!edit->search)
2517 edit->search = mc_search_new (input1, -1);
2518 if (edit->search == NULL)
2520 edit->search_start = edit->curs1;
2521 goto cleanup;
2523 edit->search->search_type = edit_search_options.type;
2524 edit->search->is_all_charsets = edit_search_options.all_codepages;
2525 edit->search->is_case_sensitive = edit_search_options.case_sens;
2526 edit->search->whole_words = edit_search_options.whole_words;
2527 edit->search->search_fn = edit_search_cmd_callback;
2528 edit->search_line_type = edit_get_search_line_type (edit->search);
2529 edit_search_fix_search_start_if_selection (edit);
2532 if (edit->found_len && edit->search_start == edit->found_start + 1
2533 && edit_search_options.backwards)
2534 edit->search_start--;
2536 if (edit->found_len && edit->search_start == edit->found_start - 1
2537 && !edit_search_options.backwards)
2538 edit->search_start++;
2542 gsize len = 0;
2544 if (!editcmd_find (edit, &len))
2546 if (!(edit->search->error == MC_SEARCH_E_OK ||
2547 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2549 edit_error_dialog (_("Search"), edit->search->error_str);
2551 break;
2553 once_found = TRUE;
2555 edit->search_start = edit->search->normal_offset;
2556 /*returns negative on not found or error in pattern */
2558 if ((edit->search_start >= 0) && (edit->search_start < edit->last_byte))
2560 gsize i;
2561 GString *tmp_str, *repl_str;
2563 edit->found_start = edit->search_start;
2564 i = edit->found_len = len;
2566 edit_cursor_move (edit, edit->search_start - edit->curs1);
2567 edit_scroll_screen_over_cursor (edit);
2569 if (edit->replace_mode == 0)
2571 int l;
2572 int prompt;
2574 l = edit->curs_row - edit->widget.lines / 3;
2575 if (l > 0)
2576 edit_scroll_downward (edit, l);
2577 if (l < 0)
2578 edit_scroll_upward (edit, -l);
2580 edit_scroll_screen_over_cursor (edit);
2581 edit->force |= REDRAW_PAGE;
2582 edit_render_keypress (edit);
2584 /*so that undo stops at each query */
2585 edit_push_key_press (edit);
2586 /* and prompt 2/3 down */
2587 disp1 = edit_replace_cmd__conv_to_display (saved1);
2588 disp2 = edit_replace_cmd__conv_to_display (saved2);
2589 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2590 g_free (disp1);
2591 g_free (disp2);
2593 if (prompt == B_REPLACE_ALL)
2594 edit->replace_mode = 1;
2595 else if (prompt == B_SKIP_REPLACE)
2597 if (edit_search_options.backwards)
2598 edit->search_start--;
2599 else
2600 edit->search_start++;
2601 continue; /* loop */
2603 else if (prompt == B_CANCEL)
2605 edit->replace_mode = -1;
2606 break; /* loop */
2610 /* don't process string each time */
2611 tmp_str = g_string_new (input2);
2612 repl_str = mc_search_prepare_replace_str (edit->search, tmp_str);
2613 g_string_free (tmp_str, TRUE);
2615 if (edit->search->error != MC_SEARCH_E_OK)
2617 edit_error_dialog (_("Replace"), edit->search->error_str);
2618 g_string_free (repl_str, TRUE);
2619 break;
2622 /* delete then insert new */
2623 for (i = 0; i < len; i++)
2624 edit_delete (edit, 1);
2626 for (i = 0; i < repl_str->len; i++)
2627 edit_insert (edit, repl_str->str[i]);
2629 edit->found_len = repl_str->len;
2630 g_string_free (repl_str, TRUE);
2631 times_replaced++;
2633 /* so that we don't find the same string again */
2634 if (edit_search_options.backwards)
2636 edit->search_start--;
2638 else
2640 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2642 if (edit->search_start >= edit->last_byte)
2643 break;
2646 edit_scroll_screen_over_cursor (edit);
2648 else
2650 /* try and find from right here for next search */
2651 edit->search_start = edit->curs1;
2652 edit_update_curs_col (edit);
2654 edit->force |= REDRAW_PAGE;
2655 edit_render_keypress (edit);
2657 if (times_replaced == 0)
2658 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2659 break;
2662 while (edit->replace_mode >= 0);
2664 edit_scroll_screen_over_cursor (edit);
2665 edit->force |= REDRAW_COMPLETELY;
2666 edit_render_keypress (edit);
2668 if ((edit->replace_mode == 1) && (times_replaced != 0))
2669 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2671 cleanup:
2672 g_free (input1);
2673 g_free (input2);
2676 /* --------------------------------------------------------------------------------------------- */
2679 edit_search_cmd_callback (const void *user_data, gsize char_offset)
2681 return edit_get_byte ((WEdit *) user_data, (long) char_offset);
2684 /* --------------------------------------------------------------------------------------------- */
2686 void
2687 edit_search_cmd (WEdit * edit, gboolean again)
2690 if (edit == NULL)
2691 return;
2693 if (!again)
2694 edit_search (edit);
2695 else if (edit->last_search_string != NULL)
2696 edit_do_search (edit);
2697 else
2699 /* find last search string in history */
2700 GList *history;
2702 history = history_get (MC_HISTORY_SHARED_SEARCH);
2703 if (history != NULL && history->data != NULL)
2705 edit->last_search_string = (char *) history->data;
2706 history->data = NULL;
2707 history = g_list_first (history);
2708 g_list_foreach (history, (GFunc) g_free, NULL);
2709 g_list_free (history);
2711 edit->search = mc_search_new (edit->last_search_string, -1);
2712 if (edit->search == NULL)
2714 /* if not... then ask for an expression */
2715 g_free (edit->last_search_string);
2716 edit->last_search_string = NULL;
2717 edit_search (edit);
2719 else
2721 edit->search->search_type = edit_search_options.type;
2722 edit->search->is_all_charsets = edit_search_options.all_codepages;
2723 edit->search->is_case_sensitive = edit_search_options.case_sens;
2724 edit->search->whole_words = edit_search_options.whole_words;
2725 edit->search->search_fn = edit_search_cmd_callback;
2726 edit->search_line_type = edit_get_search_line_type (edit->search);
2727 edit_do_search (edit);
2730 else
2732 /* if not... then ask for an expression */
2733 g_free (edit->last_search_string);
2734 edit->last_search_string = NULL;
2735 edit_search (edit);
2741 /* --------------------------------------------------------------------------------------------- */
2743 * Check if it's OK to close the editor. If there are unsaved changes,
2744 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2747 gboolean
2748 edit_ok_to_exit (WEdit * edit)
2750 int act;
2752 if (!edit->modified)
2753 return TRUE;
2755 if (!mc_global.midnight_shutdown)
2757 if (!edit_check_newline (edit))
2758 return FALSE;
2760 query_set_sel (2);
2761 act = edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2762 _("&Yes"), _("&No"), _("&Cancel quit"));
2764 else
2766 act =
2767 edit_query_dialog2 (_("Quit"),
2768 _("Midnight Commander is being shut down.\nSave modified file?"),
2769 _("&Yes"), _("&No"));
2771 /* Esc is No */
2772 if (act == -1)
2773 act = 1;
2776 switch (act)
2778 case 0: /* Yes */
2779 edit_push_markers (edit);
2780 edit_set_markers (edit, 0, 0, 0, 0);
2781 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2782 return mc_global.midnight_shutdown;
2783 break;
2784 case 1: /* No */
2785 break;
2786 case 2: /* Cancel quit */
2787 case -1: /* Esc */
2788 return FALSE;
2791 return TRUE;
2794 /* --------------------------------------------------------------------------------------------- */
2795 /** save block, returns 1 on success */
2798 edit_save_block (WEdit * edit, const char *filename, long start, long finish)
2800 int len, file;
2802 file = mc_open (filename, O_CREAT | O_WRONLY | O_TRUNC,
2803 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2804 if (file == -1)
2805 return 0;
2807 if (edit->column_highlight)
2809 int r;
2810 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2811 if (r > 0)
2813 unsigned char *block, *p;
2814 p = block = edit_get_block (edit, start, finish, &len);
2815 while (len)
2817 r = mc_write (file, p, len);
2818 if (r < 0)
2819 break;
2820 p += r;
2821 len -= r;
2823 g_free (block);
2826 else
2828 unsigned char *buf;
2829 int i = start, end;
2830 len = finish - start;
2831 buf = g_malloc0 (TEMP_BUF_LEN);
2832 while (start != finish)
2834 end = min (finish, start + TEMP_BUF_LEN);
2835 for (; i < end; i++)
2836 buf[i - start] = edit_get_byte (edit, i);
2837 len -= mc_write (file, (char *) buf, end - start);
2838 start = end;
2840 g_free (buf);
2842 mc_close (file);
2843 if (len)
2844 return 0;
2845 return 1;
2848 /* --------------------------------------------------------------------------------------------- */
2850 void
2851 edit_paste_from_history (WEdit * edit)
2853 (void) edit;
2854 edit_error_dialog (_("Error"), _("This function is not implemented"));
2857 /* --------------------------------------------------------------------------------------------- */
2860 edit_copy_to_X_buf_cmd (WEdit * edit)
2862 long start_mark, end_mark;
2863 if (eval_marks (edit, &start_mark, &end_mark))
2864 return 0;
2865 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2867 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2868 return 1;
2870 /* try use external clipboard utility */
2871 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2873 return 0;
2876 /* --------------------------------------------------------------------------------------------- */
2879 edit_cut_to_X_buf_cmd (WEdit * edit)
2881 long start_mark, end_mark;
2882 if (eval_marks (edit, &start_mark, &end_mark))
2883 return 0;
2884 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2886 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2887 return 1;
2889 /* try use external clipboard utility */
2890 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2892 edit_block_delete_cmd (edit);
2893 edit_mark_cmd (edit, 1);
2894 return 0;
2897 /* --------------------------------------------------------------------------------------------- */
2899 void
2900 edit_paste_from_X_buf_cmd (WEdit * edit)
2902 gchar *tmp;
2903 /* try use external clipboard utility */
2904 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
2905 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
2906 edit_insert_file (edit, tmp);
2907 g_free (tmp);
2911 /* --------------------------------------------------------------------------------------------- */
2913 * Ask user for the line and go to that line.
2914 * Negative numbers mean line from the end (i.e. -1 is the last line).
2917 void
2918 edit_goto_cmd (WEdit * edit)
2920 char *f;
2921 static long line = 0; /* line as typed, saved as default */
2922 long l;
2923 char *error;
2924 char s[32];
2926 g_snprintf (s, sizeof (s), "%ld", line);
2927 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "");
2928 if (!f)
2929 return;
2931 if (!*f)
2933 g_free (f);
2934 return;
2937 l = strtol (f, &error, 0);
2938 if (*error)
2940 g_free (f);
2941 return;
2944 line = l;
2945 if (l < 0)
2946 l = edit->total_lines + l + 2;
2947 edit_move_display (edit, l - edit->widget.lines / 2 - 1);
2948 edit_move_to_line (edit, l - 1);
2949 edit->force |= REDRAW_COMPLETELY;
2950 g_free (f);
2954 /* --------------------------------------------------------------------------------------------- */
2955 /** Return 1 on success */
2958 edit_save_block_cmd (WEdit * edit)
2960 long start_mark, end_mark;
2961 char *exp, *tmp;
2963 if (eval_marks (edit, &start_mark, &end_mark))
2964 return 1;
2966 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
2967 exp =
2968 input_expand_dialog (_("Save block"), _("Enter file name:"),
2969 MC_HISTORY_EDIT_SAVE_BLOCK, tmp);
2970 g_free (tmp);
2971 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2972 if (exp)
2974 if (!*exp)
2976 g_free (exp);
2977 return 0;
2979 else
2981 if (edit_save_block (edit, exp, start_mark, end_mark))
2983 g_free (exp);
2984 edit->force |= REDRAW_COMPLETELY;
2985 return 1;
2987 else
2989 g_free (exp);
2990 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
2994 edit->force |= REDRAW_COMPLETELY;
2995 return 0;
2999 /* --------------------------------------------------------------------------------------------- */
3000 /** returns TRUE on success */
3002 gboolean
3003 edit_insert_file_cmd (WEdit * edit)
3005 gchar *tmp;
3006 char *exp;
3007 gboolean ret = FALSE;
3009 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3010 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3011 MC_HISTORY_EDIT_INSERT_FILE, tmp);
3012 g_free (tmp);
3014 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3016 if (exp != NULL && *exp != '\0')
3018 ret = (edit_insert_file (edit, exp) >= 0);
3019 if (!ret)
3020 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3023 g_free (exp);
3025 edit->force |= REDRAW_COMPLETELY;
3026 return ret;
3029 /* --------------------------------------------------------------------------------------------- */
3030 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3033 edit_sort_cmd (WEdit * edit)
3035 static char *old = 0;
3036 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3037 long start_mark, end_mark;
3038 int e;
3040 if (eval_marks (edit, &start_mark, &end_mark))
3042 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3043 return 0;
3046 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3047 edit_save_block (edit, tmp, start_mark, end_mark);
3048 g_free (tmp);
3050 exp = input_dialog (_("Run sort"),
3051 _("Enter sort options (see manpage) separated by whitespace:"),
3052 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "");
3054 if (!exp)
3055 return 1;
3056 g_free (old);
3057 old = exp;
3058 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3059 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3060 tmp =
3061 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3062 " > ", tmp_edit_temp_name, (char *) NULL);
3063 g_free (tmp_edit_temp_name);
3064 g_free (tmp_edit_block_name);
3066 e = system (tmp);
3067 g_free (tmp);
3068 if (e)
3070 if (e == -1 || e == 127)
3072 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3074 else
3076 char q[8];
3077 sprintf (q, "%d ", e);
3078 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3079 edit_error_dialog (_("Sort"), tmp);
3080 g_free (tmp);
3082 return -1;
3085 edit->force |= REDRAW_COMPLETELY;
3087 if (edit_block_delete_cmd (edit))
3088 return 1;
3089 tmp = mc_config_get_full_path (EDIT_TEMP_FILE);
3090 edit_insert_file (edit, tmp);
3091 g_free (tmp);
3092 return 0;
3095 /* --------------------------------------------------------------------------------------------- */
3097 * Ask user for a command, execute it and paste its output back to the
3098 * editor.
3102 edit_ext_cmd (WEdit * edit)
3104 char *exp, *tmp, *tmp_edit_temp_file;
3105 int e;
3107 exp =
3108 input_dialog (_("Paste output of external command"),
3109 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL);
3111 if (!exp)
3112 return 1;
3114 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3115 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3116 g_free (tmp_edit_temp_file);
3117 e = system (tmp);
3118 g_free (tmp);
3119 g_free (exp);
3121 if (e)
3123 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3124 return -1;
3127 edit->force |= REDRAW_COMPLETELY;
3128 tmp = mc_config_get_full_path (EDIT_TEMP_FILE);
3129 edit_insert_file (edit, tmp);
3130 g_free (tmp);
3131 return 0;
3134 /* --------------------------------------------------------------------------------------------- */
3135 /** if block is 1, a block must be highlighted and the shell command
3136 processes it. If block is 0 the shell command is a straight system
3137 command, that just produces some output which is to be inserted */
3139 void
3140 edit_block_process_cmd (WEdit * edit, int macro_number)
3142 char *fname;
3143 char *macros_fname = NULL;
3145 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3146 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3147 user_menu (edit, macros_fname, 0);
3148 g_free (fname);
3149 g_free (macros_fname);
3150 edit->force |= REDRAW_COMPLETELY;
3153 /* --------------------------------------------------------------------------------------------- */
3155 void
3156 edit_mail_dialog (WEdit * edit)
3158 char *tmail_to;
3159 char *tmail_subject;
3160 char *tmail_cc;
3162 static char *mail_cc_last = 0;
3163 static char *mail_subject_last = 0;
3164 static char *mail_to_last = 0;
3166 QuickWidget quick_widgets[] = {
3167 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
3168 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
3169 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input", &tmail_cc),
3170 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT, N_("Copies to")),
3171 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-2",
3172 &tmail_subject),
3173 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT, N_("Subject")),
3174 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-3", &tmail_to),
3175 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT, N_("To")),
3176 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT, N_("mail -s <subject> -c <cc> <to>")),
3177 QUICK_END
3180 QuickDialog Quick_input = {
3181 50, MAIL_DLG_HEIGHT, -1, -1, N_("Mail"),
3182 "[Input Line Keys]", quick_widgets, NULL, FALSE
3185 quick_widgets[2].u.input.text = mail_cc_last ? mail_cc_last : "";
3186 quick_widgets[4].u.input.text = mail_subject_last ? mail_subject_last : "";
3187 quick_widgets[6].u.input.text = mail_to_last ? mail_to_last : "";
3189 if (quick_dialog (&Quick_input) != B_CANCEL)
3191 g_free (mail_cc_last);
3192 g_free (mail_subject_last);
3193 g_free (mail_to_last);
3194 mail_cc_last = tmail_cc;
3195 mail_subject_last = tmail_subject;
3196 mail_to_last = tmail_to;
3197 pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
3202 /*******************/
3203 /* Word Completion */
3204 /*******************/
3206 /* --------------------------------------------------------------------------------------------- */
3208 * Complete current word using regular expression search
3209 * backwards beginning at the current cursor position.
3212 void
3213 edit_complete_word_cmd (WEdit * edit)
3215 gsize i, max_len, word_len = 0, num_compl = 0;
3216 long word_start = 0;
3217 unsigned char *bufpos;
3218 char *match_expr;
3219 struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
3221 /* search start of word to be completed */
3222 if (!edit_find_word_start (edit, &word_start, &word_len))
3223 return;
3225 /* prepare match expression */
3226 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3228 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3229 match_expr =
3230 g_strdup_printf
3231 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3232 (int) word_len, bufpos);
3234 /* collect the possible completions */
3235 /* start search from begin to end of file */
3236 max_len =
3237 edit_collect_completions (edit, word_start, word_len, match_expr,
3238 (struct selection *) &compl, &num_compl);
3240 if (num_compl > 0)
3242 /* insert completed word if there is only one match */
3243 if (num_compl == 1)
3245 for (i = word_len; i < compl[0].len; i++)
3246 edit_insert (edit, *(compl[0].text + i));
3248 /* more than one possible completion => ask the user */
3249 else
3251 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3252 /* !!! pressed again the selection dialog pops up, but that !!! */
3253 /* !!! seems to require a further internal state !!! */
3254 /*tty_beep (); */
3256 /* let the user select the preferred completion */
3257 editcmd_dialog_completion_show (edit, max_len, word_len,
3258 (struct selection *) &compl, num_compl);
3262 g_free (match_expr);
3263 /* release memory before return */
3264 for (i = 0; i < num_compl; i++)
3265 g_free (compl[i].text);
3268 /* --------------------------------------------------------------------------------------------- */
3270 void
3271 edit_select_codepage_cmd (WEdit * edit)
3273 #ifdef HAVE_CHARSET
3274 if (do_select_codepage ())
3275 edit_set_codeset (edit);
3277 edit->force = REDRAW_COMPLETELY;
3278 edit_refresh_cmd (edit);
3279 #else
3280 (void) edit;
3281 #endif
3284 /* --------------------------------------------------------------------------------------------- */
3286 void
3287 edit_insert_literal_cmd (WEdit * edit)
3289 int char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3290 _("Press any key:"), 0);
3291 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3294 /* --------------------------------------------------------------------------------------------- */
3296 void
3297 edit_begin_end_macro_cmd (WEdit * edit)
3299 /* edit is a pointer to the widget */
3300 if (edit != NULL)
3302 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3303 edit_execute_key_command (edit, command, -1);
3307 /* --------------------------------------------------------------------------------------------- */
3309 void
3310 edit_begin_end_repeat_cmd (WEdit * edit)
3312 /* edit is a pointer to the widget */
3313 if (edit != NULL)
3315 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3316 edit_execute_key_command (edit, command, -1);
3320 /* --------------------------------------------------------------------------------------------- */
3323 edit_load_forward_cmd (WEdit * edit)
3325 if (edit->modified)
3327 if (edit_query_dialog2
3328 (_("Warning"),
3329 _("Current text was modified without a file save\n"
3330 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3332 edit->force |= REDRAW_COMPLETELY;
3333 return 0;
3336 if (edit_stack_iterator + 1 < MAX_HISTORY_MOVETO)
3338 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3340 return 1;
3342 edit_stack_iterator++;
3343 if (edit_history_moveto[edit_stack_iterator].filename)
3345 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename,
3346 edit_history_moveto[edit_stack_iterator].line);
3347 return 0;
3349 else
3351 return 1;
3354 else
3356 return 1;
3360 /* --------------------------------------------------------------------------------------------- */
3363 edit_load_back_cmd (WEdit * edit)
3365 if (edit->modified)
3367 if (edit_query_dialog2
3368 (_("Warning"),
3369 _("Current text was modified without a file save\n"
3370 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3372 edit->force |= REDRAW_COMPLETELY;
3373 return 0;
3376 if (edit_stack_iterator > 0)
3378 edit_stack_iterator--;
3379 if (edit_history_moveto[edit_stack_iterator].filename)
3381 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename,
3382 edit_history_moveto[edit_stack_iterator].line);
3383 return 0;
3385 else
3387 return 1;
3390 else
3392 return 1;
3396 /* --------------------------------------------------------------------------------------------- */
3398 void
3399 edit_get_match_keyword_cmd (WEdit * edit)
3401 gsize word_len = 0, max_len = 0;
3402 int num_def = 0;
3403 int i;
3404 long word_start = 0;
3405 unsigned char *bufpos;
3406 char *match_expr;
3407 char *path = NULL;
3408 char *ptr = NULL;
3409 char *tagfile = NULL;
3411 etags_hash_t def_hash[MAX_DEFINITIONS];
3413 for (i = 0; i < MAX_DEFINITIONS; i++)
3415 def_hash[i].filename = NULL;
3418 /* search start of word to be completed */
3419 if (!edit_find_word_start (edit, &word_start, &word_len))
3420 return;
3422 /* prepare match expression */
3423 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3424 match_expr = g_strdup_printf ("%.*s", (int) word_len, bufpos);
3426 ptr = g_get_current_dir ();
3427 path = g_strconcat (ptr, G_DIR_SEPARATOR_S, (char *) NULL);
3428 g_free (ptr);
3430 /* Recursive search file 'TAGS' in parent dirs */
3433 ptr = g_path_get_dirname (path);
3434 g_free (path);
3435 path = ptr;
3436 g_free (tagfile);
3437 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3438 if (exist_file (tagfile))
3439 break;
3441 while (strcmp (path, G_DIR_SEPARATOR_S) != 0);
3443 if (tagfile)
3445 num_def =
3446 etags_set_definition_hash (tagfile, path, match_expr, (etags_hash_t *) & def_hash);
3447 g_free (tagfile);
3449 g_free (path);
3451 max_len = MAX_WIDTH_DEF_DIALOG;
3452 word_len = 0;
3453 if (num_def > 0)
3455 editcmd_dialog_select_definition_show (edit, match_expr, max_len, word_len,
3456 (etags_hash_t *) & def_hash, num_def);
3458 g_free (match_expr);
3461 /* --------------------------------------------------------------------------------------------- */