Optimization of ini files load.
[midnight-commander.git] / src / editor / editcmd.c
blob9004127287d33409cfc247965ae61ced2b34cff2
1 /*
2 Editor high level editing commands
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2011, 2012
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer, 1996, 1997
10 Andrew Borodin <aborodin@vmail.ru> 2012
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file
29 * \brief Source: editor high level editing commands
30 * \author Paul Sheer
31 * \date 1996, 1997
34 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
36 #include <config.h>
38 #ifdef HAVE_ASSERT_H
39 #include <assert.h>
40 #endif
41 #include <ctype.h>
43 #include <stdio.h>
44 #include <stdarg.h>
45 #include <sys/types.h>
46 #include <unistd.h>
47 #include <string.h>
48 #include <errno.h>
49 #include <sys/stat.h>
50 #include <stdlib.h>
51 #include <fcntl.h>
53 #include "lib/global.h"
54 #include "lib/tty/tty.h"
55 #include "lib/tty/key.h" /* XCTRL */
56 #include "lib/mcconfig.h"
57 #include "lib/skin.h"
58 #include "lib/strutil.h" /* utf string functions */
59 #include "lib/lock.h"
60 #include "lib/util.h" /* tilde_expand() */
61 #include "lib/vfs/vfs.h"
62 #include "lib/widget.h"
63 #include "lib/event.h" /* mc_event_raise() */
64 #ifdef HAVE_CHARSET
65 #include "lib/charsets.h"
66 #endif
68 #include "src/history.h"
69 #include "src/setup.h" /* option_tab_spacing */
70 #include "src/main.h" /* mactos_t */
71 #ifdef HAVE_CHARSET
72 #include "src/selcodepage.h"
73 #endif
74 #include "src/keybind-defaults.h"
75 #include "src/util.h" /* check_for_default() */
76 #include "src/filemanager/layout.h" /* mc_refresh() */
78 #include "edit-impl.h"
79 #include "editwidget.h"
80 #include "editcmd_dialogs.h"
81 #include "etags.h"
83 /*** global variables ****************************************************************************/
85 /* search and replace: */
86 int search_create_bookmark = FALSE;
88 /* queries on a save */
89 int edit_confirm_save = 1;
91 /*** file scope macro definitions ****************************************************************/
93 #define space_width 1
95 #define TEMP_BUF_LEN 1024
97 #define INPUT_INDEX 9
99 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
100 (and the above) routines to work properly - paul */
102 #define is_digit(x) ((x) >= '0' && (x) <= '9')
104 #define MAIL_DLG_HEIGHT 12
106 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
108 /*** file scope type declarations ****************************************************************/
110 /*** file scope variables ************************************************************************/
112 /*** file scope functions ************************************************************************/
113 /* --------------------------------------------------------------------------------------------- */
115 /* If 0 (quick save) then a) create/truncate <filename> file,
116 b) save to <filename>;
117 if 1 (safe save) then a) save to <tempnam>,
118 b) rename <tempnam> to <filename>;
119 if 2 (do backups) then a) save to <tempnam>,
120 b) rename <filename> to <filename.backup_ext>,
121 c) rename <tempnam> to <filename>. */
123 /* returns 0 on error, -1 on abort */
125 static int
126 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
128 char *p;
129 gchar *tmp;
130 long filelen = 0;
131 int this_save_mode, fd = -1;
132 vfs_path_t *real_filename_vpath;
133 vfs_path_t *savename_vpath = NULL;
134 const char *start_filename;
135 const vfs_path_element_t *vpath_element;
137 vpath_element = vfs_path_get_by_index (filename_vpath, 0);
138 if (vpath_element == NULL)
139 return 0;
141 start_filename = vpath_element->path;
142 if (*start_filename == '\0')
143 return 0;
145 if (*start_filename != PATH_SEP && edit->dir_vpath != NULL)
147 real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
149 else
151 real_filename_vpath = vfs_path_clone (filename_vpath);
154 this_save_mode = option_save_mode;
155 if (this_save_mode != EDIT_QUICK_SAVE)
157 if (!vfs_file_is_local (real_filename_vpath)
158 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
161 * The file does not exists yet, so no safe save or
162 * backup are necessary.
164 this_save_mode = EDIT_QUICK_SAVE;
166 if (fd != -1)
167 mc_close (fd);
170 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt)
172 int rv;
173 struct stat sb;
175 rv = mc_stat (real_filename_vpath, &sb);
176 if (rv == 0 && sb.st_nlink > 1)
178 rv = edit_query_dialog3 (_("Warning"),
179 _("File has hard-links. Detach before saving?"),
180 _("&Yes"), _("&No"), _("&Cancel"));
181 switch (rv)
183 case 0:
184 this_save_mode = EDIT_SAFE_SAVE;
185 /* fallthrough */
186 case 1:
187 edit->skip_detach_prompt = 1;
188 break;
189 default:
190 vfs_path_free (real_filename_vpath);
191 return -1;
195 /* Prevent overwriting changes from other editor sessions. */
196 if (rv == 0 && edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
198 /* The default action is "Cancel". */
199 query_set_sel (1);
201 rv = edit_query_dialog2 (_("Warning"),
202 _("The file has been modified in the meantime. Save anyway?"),
203 _("&Yes"), _("&Cancel"));
204 if (rv != 0)
206 vfs_path_free (real_filename_vpath);
207 return -1;
212 if (this_save_mode != EDIT_QUICK_SAVE)
214 char *savedir, *saveprefix;
216 savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
217 if (savedir == NULL)
218 savedir = g_strdup (".");
220 saveprefix = mc_build_filename (savedir, "cooledit", NULL);
221 g_free (savedir);
222 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
223 g_free (saveprefix);
224 if (savename_vpath == NULL)
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_clone (real_filename_vpath);
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_vpath);
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 const 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 vfs_path_free (real_filename_vpath);
381 vfs_path_free (savename_vpath);
382 return 1;
383 error_save:
384 /* FIXME: Is this safe ?
385 * if (this_save_mode != EDIT_QUICK_SAVE)
386 * mc_unlink (savename);
388 vfs_path_free (real_filename_vpath);
389 vfs_path_free (savename_vpath);
390 return 0;
393 /* --------------------------------------------------------------------------------------------- */
395 static gboolean
396 edit_check_newline (WEdit * edit)
398 return !(option_check_nl_at_eof && edit->last_byte > 0
399 && edit_get_byte (edit, edit->last_byte - 1) != '\n'
400 && edit_query_dialog2 (_("Warning"),
401 _("The file you are saving is not finished with a newline"),
402 _("C&ontinue"), _("&Cancel")));
405 /* --------------------------------------------------------------------------------------------- */
407 static vfs_path_t *
408 edit_get_save_file_as (WEdit * edit)
410 #define DLG_WIDTH 64
411 #define DLG_HEIGHT 14
413 static LineBreaks cur_lb = LB_ASIS;
415 char *filename = vfs_path_to_str (edit->filename_vpath);
416 char *filename_res;
418 const char *lb_names[LB_NAMES] = {
419 N_("&Do not change"),
420 N_("&Unix format (LF)"),
421 N_("&Windows/DOS format (CR LF)"),
422 N_("&Macintosh format (CR)")
425 QuickWidget quick_widgets[] = {
426 QUICK_BUTTON (6, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
427 QUICK_BUTTON (2, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
428 QUICK_RADIO (5, DLG_WIDTH, DLG_HEIGHT - 8, DLG_HEIGHT, LB_NAMES, lb_names, (int *) &cur_lb),
429 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 9, DLG_HEIGHT, N_("Change line breaks to:")),
430 QUICK_INPUT (3, DLG_WIDTH, DLG_HEIGHT - 11, DLG_HEIGHT, filename, DLG_WIDTH - 6, 0,
431 "save-as", &filename_res),
432 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 12, DLG_HEIGHT, N_("Enter file name:")),
433 QUICK_END
436 QuickDialog Quick_options = {
437 DLG_WIDTH, DLG_HEIGHT, -1, -1,
438 N_("Save As"), "[Save File As]",
439 quick_widgets, NULL, FALSE
442 if (quick_dialog (&Quick_options) != B_CANCEL)
444 char *fname;
445 vfs_path_t *ret_vpath;
447 edit->lb = cur_lb;
448 fname = tilde_expand (filename_res);
449 g_free (filename_res);
450 ret_vpath = vfs_path_from_str (fname);
451 g_free (fname);
452 return ret_vpath;
454 g_free (filename);
456 return NULL;
458 #undef DLG_WIDTH
459 #undef DLG_HEIGHT
462 /* --------------------------------------------------------------------------------------------- */
464 /** returns 1 on success */
466 static int
467 edit_save_cmd (WEdit * edit)
469 int res, save_lock = 0;
471 if (!edit->locked && !edit->delete_file)
472 save_lock = lock_file (edit->filename_vpath);
473 res = edit_save_file (edit, edit->filename_vpath);
475 /* Maintain modify (not save) lock on failure */
476 if ((res > 0 && edit->locked) || save_lock)
477 edit->locked = unlock_file (edit->filename_vpath);
479 /* On failure try 'save as', it does locking on its own */
480 if (!res)
481 return edit_save_as_cmd (edit);
482 edit->force |= REDRAW_COMPLETELY;
483 if (res > 0)
485 edit->delete_file = 0;
486 edit->modified = 0;
489 return 1;
492 /* --------------------------------------------------------------------------------------------- */
494 * Load file content
496 * @param edit widget object
497 * @param exp_vpath vfs file path
498 * @return TRUE if file content was successfully loaded, FALSE otherwise
501 static gboolean
502 edit_load_file_from_filename (WEdit * edit, const vfs_path_t * exp_vpath)
504 int prev_locked = edit->locked;
505 vfs_path_t *prev_filename;
506 gboolean ret = TRUE;
508 prev_filename = vfs_path_clone (edit->filename_vpath);
509 if (!edit_reload (edit, exp_vpath))
510 ret = FALSE;
511 else if (prev_locked)
512 unlock_file (prev_filename);
514 vfs_path_free (prev_filename);
515 return ret;
518 /* --------------------------------------------------------------------------------------------- */
520 static void
521 edit_load_syntax_file (WEdit * edit)
523 vfs_path_t *extdir_vpath;
524 int dir = 0;
526 if (geteuid () == 0)
528 dir = query_dialog (_("Syntax file edit"),
529 _("Which syntax file you want to edit?"), D_NORMAL, 2,
530 _("&User"), _("&System Wide"));
533 extdir_vpath =
534 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
535 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
537 vfs_path_free (extdir_vpath);
538 extdir_vpath =
539 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
542 if (dir == 0)
544 vfs_path_t *user_syntax_file_vpath;
546 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
547 check_for_default (extdir_vpath, user_syntax_file_vpath);
548 edit_load_file_from_filename (edit, user_syntax_file_vpath);
549 vfs_path_free (user_syntax_file_vpath);
551 else if (dir == 1)
552 edit_load_file_from_filename (edit, extdir_vpath);
554 vfs_path_free (extdir_vpath);
557 /* --------------------------------------------------------------------------------------------- */
559 static void
560 edit_load_menu_file (WEdit * edit)
562 vfs_path_t *buffer_vpath;
563 vfs_path_t *menufile_vpath;
564 int dir = 0;
566 dir = query_dialog (_("Menu edit"),
567 _("Which menu file do you want to edit?"), D_NORMAL,
568 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
570 menufile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
572 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
574 vfs_path_free (menufile_vpath);
575 menufile_vpath = vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
578 switch (dir)
580 case 0:
581 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
582 check_for_default (menufile_vpath, buffer_vpath);
583 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
584 break;
586 case 1:
587 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
588 check_for_default (menufile_vpath, buffer_vpath);
589 break;
591 case 2:
592 buffer_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
593 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
595 vfs_path_free (buffer_vpath);
596 buffer_vpath =
597 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
599 break;
601 default:
602 vfs_path_free (menufile_vpath);
603 return;
606 edit_load_file_from_filename (edit, buffer_vpath);
608 vfs_path_free (buffer_vpath);
609 vfs_path_free (menufile_vpath);
612 /* --------------------------------------------------------------------------------------------- */
614 static void
615 edit_delete_column_of_text (WEdit * edit)
617 long p, q, r, m1, m2;
618 long b, c, d, n;
620 eval_marks (edit, &m1, &m2);
621 n = edit_move_forward (edit, m1, 0, m2) + 1;
622 c = edit_move_forward3 (edit, edit_bol (edit, m1), 0, m1);
623 d = edit_move_forward3 (edit, edit_bol (edit, m2), 0, m2);
624 b = max (min (c, d), min (edit->column1, edit->column2));
625 c = max (c, max (edit->column1, edit->column2));
627 while (n--)
629 r = edit_bol (edit, edit->curs1);
630 p = edit_move_forward3 (edit, r, b, 0);
631 q = edit_move_forward3 (edit, r, c, 0);
632 if (p < m1)
633 p = m1;
634 if (q > m2)
635 q = m2;
636 edit_cursor_move (edit, p - edit->curs1);
637 while (q > p)
639 /* delete line between margins */
640 if (edit_get_byte (edit, edit->curs1) != '\n')
641 edit_delete (edit, 1);
642 q--;
644 if (n)
645 /* move to next line except on the last delete */
646 edit_cursor_move (edit, edit_move_forward (edit, edit->curs1, 1, 0) - edit->curs1);
650 /* --------------------------------------------------------------------------------------------- */
651 /** if success return 0 */
653 static int
654 edit_block_delete (WEdit * edit)
656 long count;
657 long start_mark, end_mark;
658 int curs_pos;
659 long curs_line, c1, c2;
661 if (eval_marks (edit, &start_mark, &end_mark))
662 return 0;
663 if (edit->column_highlight && edit->mark2 < 0)
664 edit_mark_cmd (edit, 0);
665 if ((end_mark - start_mark) > option_max_undo / 2)
667 /* Warning message with a query to continue or cancel the operation */
668 if (edit_query_dialog2
669 (_("Warning"),
671 ("Block is large, you may not be able to undo this action"),
672 _("C&ontinue"), _("&Cancel")))
674 return 1;
677 c1 = min (edit->column1, edit->column2);
678 c2 = max (edit->column1, edit->column2);
679 edit->column1 = c1;
680 edit->column2 = c2;
682 edit_push_markers (edit);
684 curs_line = edit->curs_line;
686 curs_pos = edit->curs_col + edit->over_col;
688 /* move cursor to start of selection */
689 edit_cursor_move (edit, start_mark - edit->curs1);
690 edit_scroll_screen_over_cursor (edit);
691 count = start_mark;
692 if (start_mark < end_mark)
694 if (edit->column_highlight)
696 int line_width;
698 if (edit->mark2 < 0)
699 edit_mark_cmd (edit, 0);
700 edit_delete_column_of_text (edit);
701 /* move cursor to the saved position */
702 edit_move_to_line (edit, curs_line);
703 /* calculate line width and cursor position before cut */
704 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
705 edit_eol (edit, edit->curs1));
706 if (option_cursor_beyond_eol && curs_pos > line_width)
707 edit->over_col = curs_pos - line_width;
709 else
711 while (count < end_mark)
713 edit_delete (edit, 1);
714 count++;
718 edit_set_markers (edit, 0, 0, 0, 0);
719 edit->force |= REDRAW_PAGE;
720 return 0;
723 /* --------------------------------------------------------------------------------------------- */
725 * Get EOL symbol for searching.
727 * @param edit editor object
728 * @return EOL symbol
731 static inline char
732 edit_search_get_current_end_line_char (const WEdit * edit)
734 switch (edit->lb)
736 case LB_MAC:
737 return '\r';
738 default:
739 return '\n';
743 /* --------------------------------------------------------------------------------------------- */
745 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
747 * @param search search object
748 * @return result of checks.
751 static edit_search_line_t
752 edit_get_search_line_type (mc_search_t * search)
754 edit_search_line_t search_line_type = 0;
756 if (search->search_type != MC_SEARCH_T_REGEX)
757 return search_line_type;
759 if (*search->original == '^')
760 search_line_type |= AT_START_LINE;
762 if (search->original[search->original_len - 1] == '$')
763 search_line_type |= AT_END_LINE;
764 return search_line_type;
767 /* --------------------------------------------------------------------------------------------- */
769 * Calculating the start position of next line.
771 * @param edit editor object
772 * @param current_pos current position
773 * @param max_pos max position
774 * @param end_string_symbol end of line symbol
775 * @return start position of next line
778 static off_t
779 edit_calculate_start_of_next_line (WEdit * edit, off_t current_pos, off_t max_pos,
780 char end_string_symbol)
782 off_t i;
784 for (i = current_pos; i < max_pos; i++)
786 current_pos++;
787 if (edit_get_byte (edit, i) == end_string_symbol)
788 break;
791 return current_pos;
794 /* --------------------------------------------------------------------------------------------- */
796 * Calculating the end position of previous line.
798 * @param edit editor object
799 * @param current_pos current position
800 * @param end_string_symbol end of line symbol
801 * @return end position of previous line
804 static off_t
805 edit_calculate_end_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
807 off_t i;
809 for (i = current_pos - 1; i >= 0; i--)
810 if (edit_get_byte (edit, i) == end_string_symbol)
811 break;
813 return i;
816 /* --------------------------------------------------------------------------------------------- */
818 * Calculating the start position of previous line.
820 * @param edit editor object
821 * @param current_pos current position
822 * @param end_string_symbol end of line symbol
823 * @return start position of previous line
826 static inline off_t
827 edit_calculate_start_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
829 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
830 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
832 return (current_pos + 1);
835 /* --------------------------------------------------------------------------------------------- */
837 * Calculating the start position of current line.
839 * @param edit editor object
840 * @param current_pos current position
841 * @param end_string_symbol end of line symbol
842 * @return start position of current line
845 static inline off_t
846 edit_calculate_start_of_current_line (WEdit * edit, off_t current_pos, char end_string_symbol)
848 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
850 return (current_pos + 1);
853 /* --------------------------------------------------------------------------------------------- */
855 * Fixing (if needed) search start position if 'only in selection' option present.
857 * @param edit editor object
860 static void
861 edit_search_fix_search_start_if_selection (WEdit * edit)
863 long start_mark = 0;
864 long end_mark = 0;
866 if (!edit_search_options.only_in_selection)
867 return;
869 if (eval_marks (edit, &start_mark, &end_mark) != 0)
870 return;
872 if (edit_search_options.backwards)
874 if (edit->search_start > end_mark || edit->search_start <= start_mark)
875 edit->search_start = end_mark;
877 else
879 if (edit->search_start < start_mark || edit->search_start >= end_mark)
880 edit->search_start = start_mark;
884 /* --------------------------------------------------------------------------------------------- */
886 static gboolean
887 editcmd_find (WEdit * edit, gsize * len)
889 off_t search_start = edit->search_start;
890 off_t search_end;
891 long start_mark = 0;
892 long end_mark = edit->last_byte;
893 int mark_res = 0;
894 char end_string_symbol;
896 end_string_symbol = edit_search_get_current_end_line_char (edit);
898 /* prepare for search */
899 if (edit_search_options.only_in_selection)
901 mark_res = eval_marks (edit, &start_mark, &end_mark);
902 if (mark_res != 0)
904 edit->search->error = MC_SEARCH_E_NOTFOUND;
905 edit->search->error_str = g_strdup (_("Search string not found"));
906 return FALSE;
909 /* fix the start and the end of search block positions */
910 if ((edit->search_line_type & AT_START_LINE) != 0
911 && (start_mark != 0 || edit_get_byte (edit, start_mark - 1) != end_string_symbol))
913 start_mark =
914 edit_calculate_start_of_next_line (edit, start_mark, edit->last_byte,
915 end_string_symbol);
917 if ((edit->search_line_type & AT_END_LINE) != 0
918 && (end_mark - 1 != edit->last_byte
919 || edit_get_byte (edit, end_mark) != end_string_symbol))
921 end_mark = edit_calculate_end_of_previous_line (edit, end_mark, end_string_symbol);
923 if (start_mark >= end_mark)
925 edit->search->error = MC_SEARCH_E_NOTFOUND;
926 edit->search->error_str = g_strdup (_("Search string not found"));
927 return FALSE;
930 else
932 if (edit_search_options.backwards)
933 end_mark = max (1, edit->curs1) - 1;
936 /* search */
937 if (edit_search_options.backwards)
939 /* backward search */
940 search_end = end_mark;
942 if ((edit->search_line_type & AT_START_LINE) != 0)
943 search_start =
944 edit_calculate_start_of_current_line (edit, search_start, end_string_symbol);
946 while ((int) search_start >= start_mark)
948 if (search_end > (off_t) (search_start + edit->search->original_len)
949 && mc_search_is_fixed_search_str (edit->search))
951 search_end = search_start + edit->search->original_len;
953 if (mc_search_run (edit->search, (void *) edit, search_start, search_end, len)
954 && edit->search->normal_offset == search_start)
956 return TRUE;
960 if ((edit->search_line_type & AT_START_LINE) != 0)
961 search_start =
962 edit_calculate_start_of_previous_line (edit, search_start, end_string_symbol);
963 else
964 search_start--;
966 edit->search->error_str = g_strdup (_("Search string not found"));
968 else
970 /* forward search */
971 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
972 search_start =
973 edit_calculate_start_of_next_line (edit, search_start, end_mark, end_string_symbol);
974 return mc_search_run (edit->search, (void *) edit, search_start, end_mark, len);
976 return FALSE;
979 /* --------------------------------------------------------------------------------------------- */
981 static char *
982 edit_replace_cmd__conv_to_display (char *str)
984 #ifdef HAVE_CHARSET
985 GString *tmp;
987 tmp = str_convert_to_display (str);
988 if (tmp != NULL)
990 if (tmp->len != 0)
991 return g_string_free (tmp, FALSE);
992 g_string_free (tmp, TRUE);
994 #endif
995 return g_strdup (str);
998 /* --------------------------------------------------------------------------------------------- */
1000 static char *
1001 edit_replace_cmd__conv_to_input (char *str)
1003 #ifdef HAVE_CHARSET
1004 GString *tmp;
1006 tmp = str_convert_to_input (str);
1007 if (tmp != NULL)
1009 if (tmp->len != 0)
1010 return g_string_free (tmp, FALSE);
1011 g_string_free (tmp, TRUE);
1013 #endif
1014 return g_strdup (str);
1017 /* --------------------------------------------------------------------------------------------- */
1019 static void
1020 edit_do_search (WEdit * edit)
1022 gsize len = 0;
1024 if (edit->search == NULL)
1025 edit->search_start = edit->curs1;
1027 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1029 if (search_create_bookmark)
1031 int found = 0, books = 0;
1032 long l = 0, l_last = -1;
1033 long q = 0;
1035 search_create_bookmark = FALSE;
1036 book_mark_flush (edit, -1);
1038 while (TRUE)
1040 if (!mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
1041 break;
1042 if (found == 0)
1043 edit->search_start = edit->search->normal_offset;
1044 found++;
1045 l += edit_count_lines (edit, q, edit->search->normal_offset);
1046 if (l != l_last)
1048 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
1049 books++;
1051 l_last = l;
1052 q = edit->search->normal_offset + 1;
1055 if (found == 0)
1056 edit_error_dialog (_("Search"), _("Search string not found"));
1057 else
1058 edit_cursor_move (edit, edit->search_start - edit->curs1);
1060 else
1062 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
1063 && edit_search_options.backwards)
1064 edit->search_start--;
1066 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
1067 && !edit_search_options.backwards)
1068 edit->search_start++;
1070 if (editcmd_find (edit, &len))
1072 edit->found_start = edit->search_start = edit->search->normal_offset;
1073 edit->found_len = len;
1074 edit->over_col = 0;
1075 edit_cursor_move (edit, edit->search_start - edit->curs1);
1076 edit_scroll_screen_over_cursor (edit);
1077 if (edit_search_options.backwards)
1078 edit->search_start--;
1079 else
1080 edit->search_start++;
1082 else
1084 edit->search_start = edit->curs1;
1085 if (edit->search->error_str != NULL)
1086 edit_error_dialog (_("Search"), edit->search->error_str);
1090 edit->force |= REDRAW_COMPLETELY;
1091 edit_scroll_screen_over_cursor (edit);
1094 /* --------------------------------------------------------------------------------------------- */
1096 static void
1097 edit_search (WEdit * edit)
1099 if (editcmd_dialog_search_show (edit))
1101 edit->search_line_type = edit_get_search_line_type (edit->search);
1102 edit_search_fix_search_start_if_selection (edit);
1103 edit_do_search (edit);
1107 /* --------------------------------------------------------------------------------------------- */
1108 /** Return a null terminated length of text. Result must be g_free'd */
1110 static unsigned char *
1111 edit_get_block (WEdit * edit, long start, long finish, int *l)
1113 unsigned char *s, *r;
1114 r = s = g_malloc0 (finish - start + 1);
1115 if (edit->column_highlight)
1117 *l = 0;
1118 /* copy from buffer, excluding chars that are out of the column 'margins' */
1119 while (start < finish)
1121 int c;
1122 long x;
1123 x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
1124 c = edit_get_byte (edit, start);
1125 if ((x >= edit->column1 && x < edit->column2)
1126 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1128 *s++ = c;
1129 (*l)++;
1131 start++;
1134 else
1136 *l = finish - start;
1137 while (start < finish)
1138 *s++ = edit_get_byte (edit, start++);
1140 *s = 0;
1141 return r;
1144 /* --------------------------------------------------------------------------------------------- */
1145 /** copies a block to clipboard file */
1147 static int
1148 edit_save_block_to_clip_file (WEdit * edit, long start, long finish)
1150 int ret;
1151 gchar *tmp;
1152 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1153 ret = edit_save_block (edit, tmp, start, finish);
1154 g_free (tmp);
1155 return ret;
1158 /* --------------------------------------------------------------------------------------------- */
1160 static void
1161 pipe_mail (WEdit * edit, char *to, char *subject, char *cc)
1163 FILE *p = 0;
1164 char *s;
1166 to = name_quote (to, 0);
1167 subject = name_quote (subject, 0);
1168 cc = name_quote (cc, 0);
1169 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1170 g_free (to);
1171 g_free (subject);
1172 g_free (cc);
1174 if (s)
1176 p = popen (s, "w");
1177 g_free (s);
1180 if (p)
1182 long i;
1183 for (i = 0; i < edit->last_byte; i++)
1184 fputc (edit_get_byte (edit, i), p);
1185 pclose (p);
1189 /* --------------------------------------------------------------------------------------------- */
1191 static gboolean
1192 is_break_char (char c)
1194 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
1197 /* --------------------------------------------------------------------------------------------- */
1198 /** find first character of current word */
1200 static int
1201 edit_find_word_start (WEdit * edit, long *word_start, gsize * word_len)
1203 int c, last;
1204 gsize i;
1206 /* return if at begin of file */
1207 if (edit->curs1 <= 0)
1208 return 0;
1210 c = (unsigned char) edit_get_byte (edit, edit->curs1 - 1);
1211 /* return if not at end or in word */
1212 if (is_break_char (c))
1213 return 0;
1215 /* search start of word to be completed */
1216 for (i = 2;; i++)
1218 /* return if at begin of file */
1219 if ((gsize) edit->curs1 < i)
1220 return 0;
1222 last = c;
1223 c = (unsigned char) edit_get_byte (edit, edit->curs1 - i);
1225 if (is_break_char (c))
1227 /* return if word starts with digit */
1228 if (isdigit (last))
1229 return 0;
1231 *word_start = edit->curs1 - (i - 1); /* start found */
1232 *word_len = i - 1;
1233 break;
1236 /* success */
1237 return 1;
1240 /* --------------------------------------------------------------------------------------------- */
1242 * Get current word under cursor
1244 * @param edit editor object
1245 * @param srch mc_search object
1246 * @param word_start start word position
1248 * @return newly allocated string or NULL if no any words under cursor
1251 static char *
1252 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, long word_start)
1254 gsize len = 0, i;
1255 GString *temp;
1257 if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len))
1258 return NULL;
1260 temp = g_string_sized_new (len);
1262 for (i = 0; i < len; i++)
1264 int chr;
1266 chr = edit_get_byte (edit, word_start + i);
1267 if (!isspace (chr))
1268 g_string_append_c (temp, chr);
1271 return g_string_free (temp, temp->len == 0);
1274 /* --------------------------------------------------------------------------------------------- */
1275 /** collect the possible completions */
1277 static gsize
1278 edit_collect_completions (WEdit * edit, long word_start, gsize word_len,
1279 char *match_expr, struct selection *compl, gsize * num)
1281 gsize len = 0;
1282 gsize max_len = 0;
1283 gsize i;
1284 int skip;
1285 GString *temp;
1286 mc_search_t *srch;
1287 long last_byte, start = -1;
1288 char *current_word;
1290 srch = mc_search_new (match_expr, -1);
1291 if (srch == NULL)
1292 return 0;
1294 if (mc_config_get_bool
1295 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1297 last_byte = edit->last_byte;
1299 else
1301 last_byte = word_start;
1304 srch->search_type = MC_SEARCH_T_REGEX;
1305 srch->is_case_sensitive = TRUE;
1306 srch->search_fn = edit_search_cmd_callback;
1308 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1310 temp = g_string_new ("");
1312 /* collect max MAX_WORD_COMPLETIONS completions */
1313 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1315 g_string_set_size (temp, 0);
1316 start = srch->normal_offset;
1318 /* add matched completion if not yet added */
1319 for (i = 0; i < len; i++)
1321 skip = edit_get_byte (edit, start + i);
1322 if (isspace (skip))
1323 continue;
1325 /* skip current word */
1326 if (start + (long) i == word_start)
1327 break;
1329 g_string_append_c (temp, skip);
1332 if (temp->len == 0)
1333 continue;
1335 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1336 continue;
1338 skip = 0;
1340 for (i = 0; i < *num; i++)
1342 if (strncmp
1343 ((char *) &compl[i].text[word_len],
1344 (char *) &temp->str[word_len], max (len, compl[i].len) - word_len) == 0)
1346 struct selection this = compl[i];
1347 for (++i; i < *num; i++)
1349 compl[i - 1] = compl[i];
1351 compl[*num - 1] = this;
1352 skip = 1;
1353 break; /* skip it, already added */
1356 if (skip != 0)
1357 continue;
1359 if (*num == MAX_WORD_COMPLETIONS)
1361 g_free (compl[0].text);
1362 for (i = 1; i < *num; i++)
1364 compl[i - 1] = compl[i];
1366 (*num)--;
1368 #ifdef HAVE_CHARSET
1370 GString *recoded;
1371 recoded = str_convert_to_display (temp->str);
1373 if (recoded && recoded->len)
1374 g_string_assign (temp, recoded->str);
1376 g_string_free (recoded, TRUE);
1378 #endif
1379 compl[*num].text = g_strdup (temp->str);
1380 compl[*num].len = temp->len;
1381 (*num)++;
1382 start += len;
1384 /* note the maximal length needed for the completion dialog */
1385 if (len > max_len)
1386 max_len = len;
1389 mc_search_free (srch);
1390 g_string_free (temp, TRUE);
1391 g_free (current_word);
1393 return max_len;
1396 /* --------------------------------------------------------------------------------------------- */
1398 static void
1399 edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width,
1400 long *start_pos, long *end_pos, int *col1, int *col2)
1402 long cursor;
1403 int i, col;
1405 cursor = edit->curs1;
1406 col = edit_get_col (edit);
1408 for (i = 0; i < size; i++)
1410 if (data[i] != '\n')
1411 edit_insert (edit, data[i]);
1412 else
1413 { /* fill in and move to next line */
1414 int l;
1415 long p;
1417 if (edit_get_byte (edit, edit->curs1) != '\n')
1419 l = width - (edit_get_col (edit) - col);
1420 while (l > 0)
1422 edit_insert (edit, ' ');
1423 l -= space_width;
1426 for (p = edit->curs1;; p++)
1428 if (p == edit->last_byte)
1430 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1431 edit_insert_ahead (edit, '\n');
1432 p++;
1433 break;
1435 if (edit_get_byte (edit, p) == '\n')
1437 p++;
1438 break;
1441 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1442 l = col - edit_get_col (edit);
1443 while (l >= space_width)
1445 edit_insert (edit, ' ');
1446 l -= space_width;
1451 *col1 = col;
1452 *col2 = col + width;
1453 *start_pos = cursor;
1454 *end_pos = edit->curs1;
1455 edit_cursor_move (edit, cursor - edit->curs1);
1458 /* --------------------------------------------------------------------------------------------- */
1460 static int
1461 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1463 const macros_t *m1 = (const macros_t *) macro1;
1464 const macros_t *m2 = (const macros_t *) macro2;
1466 return m1->hotkey - m2->hotkey;
1469 /* --------------------------------------------------------------------------------------------- */
1471 static void
1472 edit_macro_sort_by_hotkey (void)
1474 if (macros_list != NULL && macros_list->len != 0)
1475 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1478 /* --------------------------------------------------------------------------------------------- */
1480 static gboolean
1481 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1483 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1484 macros_t *result;
1485 macros_t search_macro;
1487 (void) edit;
1489 search_macro.hotkey = hotkey;
1490 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1491 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1493 if (result != NULL && result->macro != NULL)
1495 *indx = (result - array_start);
1496 *macros = result;
1497 return TRUE;
1499 *indx = 0;
1500 return FALSE;
1503 /* --------------------------------------------------------------------------------------------- */
1504 /** returns FALSE on error */
1506 static gboolean
1507 edit_delete_macro (WEdit * edit, int hotkey)
1509 mc_config_t *macros_config = NULL;
1510 const char *section_name = "editor";
1511 gchar *macros_fname;
1512 guint indx;
1513 char *skeyname;
1514 const macros_t *macros = NULL;
1516 /* clear array of actions for current hotkey */
1517 while (edit_get_macro (edit, hotkey, &macros, &indx))
1519 if (macros->macro != NULL)
1520 g_array_free (macros->macro, TRUE);
1521 macros = NULL;
1522 g_array_remove_index (macros_list, indx);
1523 edit_macro_sort_by_hotkey ();
1526 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1527 macros_config = mc_config_init (macros_fname, FALSE);
1528 g_free (macros_fname);
1530 if (macros_config == NULL)
1531 return FALSE;
1533 skeyname = lookup_key_by_code (hotkey);
1534 while (mc_config_del_key (macros_config, section_name, skeyname))
1536 g_free (skeyname);
1537 mc_config_save_file (macros_config, NULL);
1538 mc_config_deinit (macros_config);
1539 return TRUE;
1543 /* --------------------------------------------------------------------------------------------- */
1544 /*** public functions ****************************************************************************/
1545 /* --------------------------------------------------------------------------------------------- */
1547 void
1548 edit_help_cmd (WEdit * edit)
1550 ev_help_t event_data = { NULL, "[Internal File Editor]" };
1551 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
1553 edit->force |= REDRAW_COMPLETELY;
1556 /* --------------------------------------------------------------------------------------------- */
1558 void
1559 edit_refresh_cmd (WEdit * edit)
1561 #ifdef HAVE_SLANG
1562 int color;
1564 edit_get_syntax_color (edit, -1, &color);
1565 tty_touch_screen ();
1566 mc_refresh ();
1567 #else
1568 (void) edit;
1570 clr_scr ();
1571 repaint_screen ();
1572 #endif /* !HAVE_SLANG */
1573 tty_keypad (TRUE);
1576 /* --------------------------------------------------------------------------------------------- */
1578 void
1579 menu_save_mode_cmd (void)
1581 /* diaog sizes */
1582 const int DLG_X = 38;
1583 const int DLG_Y = 13;
1585 char *str_result;
1587 const char *str[] = {
1588 N_("&Quick save"),
1589 N_("&Safe save"),
1590 N_("&Do backups with following extension:")
1593 QuickWidget widgets[] = {
1594 /* 0 */
1595 QUICK_BUTTON (18, DLG_X, DLG_Y - 3, DLG_Y, N_("&Cancel"), B_CANCEL, NULL),
1596 /* 1 */
1597 QUICK_BUTTON (6, DLG_X, DLG_Y - 3, DLG_Y, N_("&OK"), B_ENTER, NULL),
1598 /* 2 */
1599 QUICK_CHECKBOX (4, DLG_X, 8, DLG_Y, N_("Check &POSIX new line"), &option_check_nl_at_eof),
1600 /* 3 */
1601 QUICK_INPUT (8, DLG_X, 6, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result),
1602 /* 4 */
1603 QUICK_RADIO (4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode),
1604 QUICK_END
1607 QuickDialog dialog = {
1608 DLG_X, DLG_Y, -1, -1, N_("Edit Save Mode"),
1609 "[Edit Save Mode]", widgets, NULL, FALSE
1612 size_t i;
1613 size_t maxlen = 0;
1614 size_t w0, w1, b_len, w3;
1616 #ifdef HAVE_ASSERT_H
1617 assert (option_backup_ext != NULL);
1618 #endif
1620 /* OK/Cancel buttons */
1621 w0 = str_term_width1 (_(widgets[0].u.button.text)) + 3;
1622 w1 = str_term_width1 (_(widgets[1].u.button.text)) + 5; /* default button */
1623 b_len = w0 + w1 + 3;
1625 maxlen = max (b_len, (size_t) str_term_width1 (_(dialog.title)) + 2);
1627 w3 = 0;
1628 for (i = 0; i < 3; i++)
1630 #ifdef ENABLE_NLS
1631 str[i] = _(str[i]);
1632 #endif
1633 w3 = max (w3, (size_t) str_term_width1 (str[i]));
1636 maxlen = max (maxlen, w3 + 4);
1638 dialog.xlen = min ((size_t) COLS, maxlen + 8);
1640 widgets[3].u.input.len = w3;
1641 widgets[1].relative_x = (dialog.xlen - b_len) / 2;
1642 widgets[0].relative_x = widgets[1].relative_x + w0 + 2;
1644 for (i = 0; i < sizeof (widgets) / sizeof (widgets[0]); i++)
1645 widgets[i].x_divisions = dialog.xlen;
1647 if (quick_dialog (&dialog) != B_CANCEL)
1649 g_free (option_backup_ext);
1650 option_backup_ext = str_result;
1654 /* --------------------------------------------------------------------------------------------- */
1656 void
1657 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1659 vfs_path_free (edit->filename_vpath);
1660 edit->filename_vpath = vfs_path_clone (name_vpath);
1662 if (edit->dir_vpath == NULL)
1663 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1666 /* --------------------------------------------------------------------------------------------- */
1667 /* Here we want to warn the users of overwriting an existing file,
1668 but only if they have made a change to the filename */
1669 /* returns 1 on success */
1671 edit_save_as_cmd (WEdit * edit)
1673 /* This heads the 'Save As' dialog box */
1674 vfs_path_t *exp_vpath;
1675 int save_lock = 0;
1676 int different_filename = 0;
1678 if (!edit_check_newline (edit))
1679 return 0;
1681 exp_vpath = edit_get_save_file_as (edit);
1682 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1684 if (exp_vpath != NULL)
1686 if (vfs_path_len (exp_vpath) == 0)
1687 goto ret;
1688 else
1690 int rv;
1692 if (vfs_path_cmp (edit->filename_vpath, exp_vpath) != 0)
1694 int file;
1695 struct stat sb;
1697 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1699 edit_error_dialog (_("Save as"),
1700 get_sys_error (_
1701 ("Cannot save: destination is not a regular file")));
1702 goto ret;
1705 different_filename = 1;
1706 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1708 if (file != -1)
1710 /* the file exists */
1711 mc_close (file);
1712 /* Overwrite the current file or cancel the operation */
1713 if (edit_query_dialog2
1714 (_("Warning"),
1715 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1716 goto ret;
1718 else
1720 edit->stat1.st_mode |= S_IWUSR;
1722 save_lock = lock_file (exp_vpath);
1724 else
1726 /* filenames equal, check if already locked */
1727 if (!edit->locked && !edit->delete_file)
1728 save_lock = lock_file (exp_vpath);
1731 if (different_filename)
1734 * Allow user to write into saved (under another name) file
1735 * even if original file had r/o user permissions.
1737 edit->stat1.st_mode |= S_IWRITE;
1740 rv = edit_save_file (edit, exp_vpath);
1741 switch (rv)
1743 case 1:
1744 /* Succesful, so unlock both files */
1745 if (different_filename)
1747 if (save_lock)
1748 unlock_file (exp_vpath);
1749 if (edit->locked)
1750 edit->locked = unlock_file (edit->filename_vpath);
1752 else
1754 if (edit->locked || save_lock)
1755 edit->locked = unlock_file (edit->filename_vpath);
1758 edit_set_filename (edit, exp_vpath);
1759 if (edit->lb != LB_ASIS)
1760 edit_reload (edit, exp_vpath);
1761 edit->modified = 0;
1762 edit->delete_file = 0;
1763 if (different_filename)
1764 edit_load_syntax (edit, NULL, edit->syntax_type);
1765 vfs_path_free (exp_vpath);
1766 edit->force |= REDRAW_COMPLETELY;
1767 return 1;
1768 default:
1769 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1770 /* fallthrough */
1771 case -1:
1772 /* Failed, so maintain modify (not save) lock */
1773 if (save_lock)
1774 unlock_file (exp_vpath);
1775 break;
1780 ret:
1781 vfs_path_free (exp_vpath);
1782 edit->force |= REDRAW_COMPLETELY;
1783 return 0;
1786 /* {{{ Macro stuff starts here */
1787 /* --------------------------------------------------------------------------------------------- */
1789 void
1790 edit_delete_macro_cmd (WEdit * edit)
1792 int hotkey;
1794 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1796 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1797 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1800 /* --------------------------------------------------------------------------------------------- */
1802 /** returns FALSE on error */
1803 gboolean
1804 edit_execute_macro (WEdit * edit, int hotkey)
1806 gboolean res = FALSE;
1808 if (hotkey != 0)
1810 const macros_t *macros;
1811 guint indx;
1813 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1814 macros->macro != NULL && macros->macro->len != 0)
1816 guint i;
1818 edit->force |= REDRAW_PAGE;
1820 for (i = 0; i < macros->macro->len; i++)
1822 const macro_action_t *m_act;
1824 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1825 edit_execute_cmd (edit, m_act->action, m_act->ch);
1826 res = TRUE;
1831 return res;
1834 /* --------------------------------------------------------------------------------------------- */
1836 /** returns FALSE on error */
1837 gboolean
1838 edit_store_macro_cmd (WEdit * edit)
1840 int i;
1841 int hotkey;
1842 GString *marcros_string;
1843 mc_config_t *macros_config = NULL;
1844 const char *section_name = "editor";
1845 gchar *macros_fname;
1846 GArray *macros; /* current macro */
1847 int tmp_act;
1848 gboolean have_macro = FALSE;
1849 char *skeyname = NULL;
1851 hotkey = editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1852 if (hotkey == ESC_CHAR)
1853 return FALSE;
1855 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1857 /* return FALSE if try assign macro into restricted hotkeys */
1858 if (tmp_act == CK_MacroStartRecord
1859 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1860 return FALSE;
1862 edit_delete_macro (edit, hotkey);
1864 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1865 macros_config = mc_config_init (macros_fname, FALSE);
1866 g_free (macros_fname);
1868 if (macros_config == NULL)
1869 return FALSE;
1871 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1873 marcros_string = g_string_sized_new (250);
1874 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1876 skeyname = lookup_key_by_code (hotkey);
1878 for (i = 0; i < macro_index; i++)
1880 macro_action_t m_act;
1881 const char *action_name;
1883 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1885 if (action_name == NULL)
1886 break;
1888 m_act.action = record_macro_buf[i].action;
1889 m_act.ch = record_macro_buf[i].ch;
1890 g_array_append_val (macros, m_act);
1891 have_macro = TRUE;
1892 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1893 (int) record_macro_buf[i].ch);
1895 if (have_macro)
1897 macros_t macro;
1898 macro.hotkey = hotkey;
1899 macro.macro = macros;
1900 g_array_append_val (macros_list, macro);
1901 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1903 else
1904 mc_config_del_key (macros_config, section_name, skeyname);
1906 g_free (skeyname);
1907 edit_macro_sort_by_hotkey ();
1909 g_string_free (marcros_string, TRUE);
1910 mc_config_save_file (macros_config, NULL);
1911 mc_config_deinit (macros_config);
1912 return TRUE;
1915 /* --------------------------------------------------------------------------------------------- */
1917 gboolean
1918 edit_repeat_macro_cmd (WEdit * edit)
1920 int i, j;
1921 char *f;
1922 long count_repeat;
1923 char *error = NULL;
1925 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL);
1926 if (f == NULL || *f == '\0')
1928 g_free (f);
1929 return FALSE;
1932 count_repeat = strtol (f, &error, 0);
1934 if (*error != '\0')
1936 g_free (f);
1937 return FALSE;
1940 g_free (f);
1942 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1943 edit->force |= REDRAW_PAGE;
1945 for (j = 0; j < count_repeat; j++)
1946 for (i = 0; i < macro_index; i++)
1947 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1948 edit_update_screen (edit);
1949 return TRUE;
1952 /* --------------------------------------------------------------------------------------------- */
1953 /** return FALSE on error */
1955 gboolean
1956 edit_load_macro_cmd (WEdit * edit)
1958 mc_config_t *macros_config = NULL;
1959 gchar **profile_keys, **keys;
1960 gchar **values, **curr_values;
1961 gsize len, values_len;
1962 const char *section_name = "editor";
1963 gchar *macros_fname;
1964 int hotkey;
1966 (void) edit;
1968 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1969 macros_config = mc_config_init (macros_fname, TRUE);
1970 g_free (macros_fname);
1972 if (macros_config == NULL)
1973 return FALSE;
1975 profile_keys = keys = mc_config_get_keys (macros_config, section_name, &len);
1976 while (*profile_keys != NULL)
1978 gboolean have_macro;
1979 GArray *macros;
1980 macros_t macro;
1982 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1984 curr_values = values = mc_config_get_string_list (macros_config, section_name,
1985 *profile_keys, &values_len);
1986 hotkey = lookup_key (*profile_keys, NULL);
1987 have_macro = FALSE;
1989 while (*curr_values != NULL && *curr_values[0] != '\0')
1991 char **macro_pair = NULL;
1993 macro_pair = g_strsplit (*curr_values, ":", 2);
1995 if (macro_pair != NULL)
1997 macro_action_t m_act;
1998 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
1999 m_act.action = 0;
2000 else
2002 m_act.action = keybind_lookup_action (macro_pair[0]);
2003 g_free (macro_pair[0]);
2004 macro_pair[0] = NULL;
2006 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
2007 m_act.ch = -1;
2008 else
2010 m_act.ch = strtol (macro_pair[1], NULL, 0);
2011 g_free (macro_pair[1]);
2012 macro_pair[1] = NULL;
2014 if (m_act.action != 0)
2016 /* a shell command */
2017 if ((m_act.action / CK_PipeBlock (0)) == 1)
2019 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
2020 m_act.ch = -1;
2022 g_array_append_val (macros, m_act);
2023 have_macro = TRUE;
2025 g_strfreev (macro_pair);
2026 macro_pair = NULL;
2028 curr_values++;
2030 if (have_macro)
2032 macro.hotkey = hotkey;
2033 macro.macro = macros;
2034 g_array_append_val (macros_list, macro);
2036 profile_keys++;
2037 g_strfreev (values);
2039 g_strfreev (keys);
2040 mc_config_deinit (macros_config);
2041 edit_macro_sort_by_hotkey ();
2042 return TRUE;
2045 /* }}} Macro stuff end here */
2047 /* --------------------------------------------------------------------------------------------- */
2048 /** returns 1 on success */
2051 edit_save_confirm_cmd (WEdit * edit)
2053 gchar *f = NULL;
2055 if (edit->filename_vpath == NULL)
2056 return edit_save_as_cmd (edit);
2058 if (!edit_check_newline (edit))
2059 return 0;
2061 if (edit_confirm_save)
2063 char *filename;
2064 gboolean ok;
2066 filename = vfs_path_to_str (edit->filename_vpath);
2067 f = g_strdup_printf (_("Confirm save file: \"%s\""), filename);
2068 g_free (filename);
2069 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2070 g_free (f);
2071 if (!ok)
2072 return 0;
2074 return edit_save_cmd (edit);
2077 /* --------------------------------------------------------------------------------------------- */
2079 /* returns TRUE on success */
2080 gboolean
2081 edit_new_cmd (WEdit * edit)
2083 if (edit->modified
2084 && edit_query_dialog2 (_("Warning"),
2085 _("Current text was modified without a file save.\n"
2086 "Continue discards these changes"),
2087 _("C&ontinue"), _("&Cancel")) == 1)
2089 edit->force |= REDRAW_COMPLETELY;
2090 return TRUE;
2093 edit->force |= REDRAW_COMPLETELY;
2095 return edit_renew (edit); /* if this gives an error, something has really screwed up */
2098 /* --------------------------------------------------------------------------------------------- */
2100 gboolean
2101 edit_load_cmd (WEdit * edit, edit_current_file_t what)
2103 gboolean ret = TRUE;
2105 if (edit->modified
2106 && edit_query_dialog2 (_("Warning"),
2107 _("Current text was modified without a file save.\n"
2108 "Continue discards these changes"), _("C&ontinue"),
2109 _("&Cancel")) == 1)
2111 edit->force |= REDRAW_COMPLETELY;
2112 return TRUE;
2115 switch (what)
2117 case EDIT_FILE_COMMON:
2119 char *filename, *exp;
2121 filename = vfs_path_to_str (edit->filename_vpath);
2122 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2123 MC_HISTORY_EDIT_LOAD, filename);
2124 g_free (filename);
2126 if (exp != NULL && *exp != '\0')
2128 vfs_path_t *exp_vpath;
2130 exp_vpath = vfs_path_from_str (exp);
2131 ret = edit_load_file_from_filename (edit, exp_vpath);
2132 vfs_path_free (exp_vpath);
2135 g_free (exp);
2137 break;
2139 case EDIT_FILE_SYNTAX:
2140 edit_load_syntax_file (edit);
2141 break;
2143 case EDIT_FILE_MENU:
2144 edit_load_menu_file (edit);
2145 break;
2147 default:
2148 break;
2151 edit->force |= REDRAW_COMPLETELY;
2152 return ret;
2155 /* --------------------------------------------------------------------------------------------- */
2157 if mark2 is -1 then marking is from mark1 to the cursor.
2158 Otherwise its between the markers. This handles this.
2159 Returns 1 if no text is marked.
2163 eval_marks (WEdit * edit, long *start_mark, long *end_mark)
2165 if (edit->mark1 != edit->mark2)
2167 long start_bol, start_eol;
2168 long end_bol, end_eol;
2169 long col1, col2;
2170 long diff1, diff2;
2171 long end_mark_curs;
2173 if (edit->end_mark_curs < 0)
2174 end_mark_curs = edit->curs1;
2175 else
2176 end_mark_curs = edit->end_mark_curs;
2178 if (edit->mark2 >= 0)
2180 *start_mark = min (edit->mark1, edit->mark2);
2181 *end_mark = max (edit->mark1, edit->mark2);
2183 else
2185 *start_mark = min (edit->mark1, end_mark_curs);
2186 *end_mark = max (edit->mark1, end_mark_curs);
2187 edit->column2 = edit->curs_col + edit->over_col;
2190 if (edit->column_highlight
2191 && (((edit->mark1 > end_mark_curs) && (edit->column1 < edit->column2))
2192 || ((edit->mark1 < end_mark_curs) && (edit->column1 > edit->column2))))
2194 start_bol = edit_bol (edit, *start_mark);
2195 start_eol = edit_eol (edit, start_bol - 1) + 1;
2196 end_bol = edit_bol (edit, *end_mark);
2197 end_eol = edit_eol (edit, *end_mark);
2198 col1 = min (edit->column1, edit->column2);
2199 col2 = max (edit->column1, edit->column2);
2201 diff1 =
2202 edit_move_forward3 (edit, start_bol, col2, 0) - edit_move_forward3 (edit, start_bol,
2203 col1, 0);
2204 diff2 =
2205 edit_move_forward3 (edit, end_bol, col2, 0) - edit_move_forward3 (edit, end_bol,
2206 col1, 0);
2208 *start_mark -= diff1;
2209 *end_mark += diff2;
2210 *start_mark = max (*start_mark, start_eol);
2211 *end_mark = min (*end_mark, end_eol);
2213 return 0;
2215 else
2217 *start_mark = *end_mark = 0;
2218 edit->column2 = edit->column1 = 0;
2219 return 1;
2223 /* --------------------------------------------------------------------------------------------- */
2225 void
2226 edit_insert_over (WEdit * edit)
2228 int i;
2230 for (i = 0; i < edit->over_col; i++)
2232 edit_insert (edit, ' ');
2234 edit->over_col = 0;
2237 /* --------------------------------------------------------------------------------------------- */
2240 edit_insert_column_of_text_from_file (WEdit * edit, int file,
2241 long *start_pos, long *end_pos, int *col1, int *col2)
2243 long cursor;
2244 int col;
2245 int blocklen = -1, width = 0;
2246 unsigned char *data;
2248 cursor = edit->curs1;
2249 col = edit_get_col (edit);
2250 data = g_malloc0 (TEMP_BUF_LEN);
2252 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
2254 int i;
2255 for (width = 0; width < blocklen; width++)
2257 if (data[width] == '\n')
2258 break;
2260 for (i = 0; i < blocklen; i++)
2262 if (data[i] == '\n')
2263 { /* fill in and move to next line */
2264 int l;
2265 long p;
2266 if (edit_get_byte (edit, edit->curs1) != '\n')
2268 l = width - (edit_get_col (edit) - col);
2269 while (l > 0)
2271 edit_insert (edit, ' ');
2272 l -= space_width;
2275 for (p = edit->curs1;; p++)
2277 if (p == edit->last_byte)
2279 edit_cursor_move (edit, edit->last_byte - edit->curs1);
2280 edit_insert_ahead (edit, '\n');
2281 p++;
2282 break;
2284 if (edit_get_byte (edit, p) == '\n')
2286 p++;
2287 break;
2290 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
2291 l = col - edit_get_col (edit);
2292 while (l >= space_width)
2294 edit_insert (edit, ' ');
2295 l -= space_width;
2297 continue;
2299 edit_insert (edit, data[i]);
2302 *col1 = col;
2303 *col2 = col + width;
2304 *start_pos = cursor;
2305 *end_pos = edit->curs1;
2306 edit_cursor_move (edit, cursor - edit->curs1);
2307 g_free (data);
2309 return blocklen;
2312 /* --------------------------------------------------------------------------------------------- */
2314 void
2315 edit_block_copy_cmd (WEdit * edit)
2317 long start_mark, end_mark, current = edit->curs1;
2318 long col_delta = 0;
2319 long mark1, mark2;
2320 int c1, c2;
2321 int size;
2322 unsigned char *copy_buf;
2324 edit_update_curs_col (edit);
2325 if (eval_marks (edit, &start_mark, &end_mark))
2326 return;
2328 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2330 /* all that gets pushed are deletes hence little space is used on the stack */
2332 edit_push_markers (edit);
2334 if (edit->column_highlight)
2336 col_delta = abs (edit->column2 - edit->column1);
2337 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2339 else
2341 while (size--)
2342 edit_insert_ahead (edit, copy_buf[size]);
2345 g_free (copy_buf);
2346 edit_scroll_screen_over_cursor (edit);
2348 if (edit->column_highlight)
2349 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2350 else if (start_mark < current && end_mark > current)
2351 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2353 edit->force |= REDRAW_PAGE;
2357 /* --------------------------------------------------------------------------------------------- */
2359 void
2360 edit_block_move_cmd (WEdit * edit)
2362 long current;
2363 unsigned char *copy_buf = NULL;
2364 long start_mark, end_mark;
2365 long line;
2367 if (eval_marks (edit, &start_mark, &end_mark))
2368 return;
2370 line = edit->curs_line;
2371 if (edit->mark2 < 0)
2372 edit_mark_cmd (edit, 0);
2373 edit_push_markers (edit);
2375 if (edit->column_highlight)
2377 long mark1, mark2;
2378 int size;
2379 int b_width = 0;
2380 int c1, c2;
2381 int x, x2;
2383 c1 = min (edit->column1, edit->column2);
2384 c2 = max (edit->column1, edit->column2);
2385 b_width = (c2 - c1);
2387 edit_update_curs_col (edit);
2389 x = edit->curs_col;
2390 x2 = x + edit->over_col;
2392 /* do nothing when cursor inside first line of selected area */
2393 if ((edit_eol (edit, edit->curs1) == edit_eol (edit, start_mark)) && (x2 > c1 && x2 <= c2))
2394 return;
2396 if (edit->curs1 > start_mark && edit->curs1 < edit_eol (edit, end_mark))
2398 if (x > c2)
2399 x -= b_width;
2400 else if (x > c1 && x <= c2)
2401 x = c1;
2403 /* save current selection into buffer */
2404 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2406 /* remove current selection */
2407 edit_block_delete_cmd (edit);
2409 edit->over_col = max (0, edit->over_col - b_width);
2410 /* calculate the cursor pos after delete block */
2411 current = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), x, 0);
2412 edit_cursor_move (edit, current - edit->curs1);
2413 edit_scroll_screen_over_cursor (edit);
2415 /* add TWS if need before block insertion */
2416 if (option_cursor_beyond_eol && edit->over_col > 0)
2417 edit_insert_over (edit);
2419 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2420 edit_set_markers (edit, mark1, mark2, c1, c2);
2422 else
2424 long count;
2426 current = edit->curs1;
2427 copy_buf = g_malloc0 (end_mark - start_mark);
2428 edit_cursor_move (edit, start_mark - edit->curs1);
2429 edit_scroll_screen_over_cursor (edit);
2430 count = start_mark;
2431 while (count < end_mark)
2433 copy_buf[end_mark - count - 1] = edit_delete (edit, 1);
2434 count++;
2436 edit_scroll_screen_over_cursor (edit);
2437 edit_cursor_move (edit,
2438 current - edit->curs1 -
2439 (((current - edit->curs1) > 0) ? end_mark - start_mark : 0));
2440 edit_scroll_screen_over_cursor (edit);
2441 while (count-- > start_mark)
2442 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2443 edit_set_markers (edit, edit->curs1, edit->curs1 + end_mark - start_mark, 0, 0);
2446 edit_scroll_screen_over_cursor (edit);
2447 g_free (copy_buf);
2448 edit->force |= REDRAW_PAGE;
2451 /* --------------------------------------------------------------------------------------------- */
2452 /** returns 1 if canceelled by user */
2455 edit_block_delete_cmd (WEdit * edit)
2457 long start_mark, end_mark;
2458 if (eval_marks (edit, &start_mark, &end_mark))
2460 edit_delete_line (edit);
2461 return 0;
2463 return edit_block_delete (edit);
2466 /* --------------------------------------------------------------------------------------------- */
2467 /** call with edit = 0 before shutdown to close memory leaks */
2469 void
2470 edit_replace_cmd (WEdit * edit, int again)
2472 /* 1 = search string, 2 = replace with */
2473 static char *saved1 = NULL; /* saved default[123] */
2474 static char *saved2 = NULL;
2475 char *input1 = NULL; /* user input from the dialog */
2476 char *input2 = NULL;
2477 GString *input2_str = NULL;
2478 char *disp1 = NULL;
2479 char *disp2 = NULL;
2480 long times_replaced = 0;
2481 gboolean once_found = FALSE;
2483 if (!edit)
2485 g_free (saved1), saved1 = NULL;
2486 g_free (saved2), saved2 = NULL;
2487 return;
2490 edit->force |= REDRAW_COMPLETELY;
2492 if (again && !saved1 && !saved2)
2493 again = 0;
2495 if (again)
2497 input1 = g_strdup (saved1 ? saved1 : "");
2498 input2 = g_strdup (saved2 ? saved2 : "");
2500 else
2502 char *tmp_inp1, *tmp_inp2;
2504 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2505 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2507 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2509 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2511 g_free (disp1);
2512 g_free (disp2);
2514 if (input1 == NULL || *input1 == '\0')
2516 edit->force = REDRAW_COMPLETELY;
2517 goto cleanup;
2520 tmp_inp1 = input1;
2521 tmp_inp2 = input2;
2522 input1 = edit_replace_cmd__conv_to_input (input1);
2523 input2 = edit_replace_cmd__conv_to_input (input2);
2524 g_free (tmp_inp1);
2525 g_free (tmp_inp2);
2527 g_free (saved1), saved1 = g_strdup (input1);
2528 g_free (saved2), saved2 = g_strdup (input2);
2530 mc_search_free (edit->search);
2531 edit->search = NULL;
2534 input2_str = g_string_new (input2);
2536 if (!edit->search)
2538 edit->search = mc_search_new (input1, -1);
2539 if (edit->search == NULL)
2541 edit->search_start = edit->curs1;
2542 goto cleanup;
2544 edit->search->search_type = edit_search_options.type;
2545 edit->search->is_all_charsets = edit_search_options.all_codepages;
2546 edit->search->is_case_sensitive = edit_search_options.case_sens;
2547 edit->search->whole_words = edit_search_options.whole_words;
2548 edit->search->search_fn = edit_search_cmd_callback;
2549 edit->search_line_type = edit_get_search_line_type (edit->search);
2550 edit_search_fix_search_start_if_selection (edit);
2553 if (edit->found_len && edit->search_start == edit->found_start + 1
2554 && edit_search_options.backwards)
2555 edit->search_start--;
2557 if (edit->found_len && edit->search_start == edit->found_start - 1
2558 && !edit_search_options.backwards)
2559 edit->search_start++;
2563 gsize len = 0;
2565 if (!editcmd_find (edit, &len))
2567 if (!(edit->search->error == MC_SEARCH_E_OK ||
2568 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2570 edit_error_dialog (_("Search"), edit->search->error_str);
2572 break;
2574 once_found = TRUE;
2576 edit->search_start = edit->search->normal_offset;
2577 /*returns negative on not found or error in pattern */
2579 if ((edit->search_start >= 0) && (edit->search_start < edit->last_byte))
2581 gsize i;
2582 GString *repl_str;
2584 edit->found_start = edit->search_start;
2585 i = edit->found_len = len;
2587 edit_cursor_move (edit, edit->search_start - edit->curs1);
2588 edit_scroll_screen_over_cursor (edit);
2590 if (edit->replace_mode == 0)
2592 int l;
2593 int prompt;
2595 l = edit->curs_row - edit->widget.lines / 3;
2596 if (l > 0)
2597 edit_scroll_downward (edit, l);
2598 if (l < 0)
2599 edit_scroll_upward (edit, -l);
2601 edit_scroll_screen_over_cursor (edit);
2602 edit->force |= REDRAW_PAGE;
2603 edit_render_keypress (edit);
2605 /*so that undo stops at each query */
2606 edit_push_key_press (edit);
2607 /* and prompt 2/3 down */
2608 disp1 = edit_replace_cmd__conv_to_display (saved1);
2609 disp2 = edit_replace_cmd__conv_to_display (saved2);
2610 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2611 g_free (disp1);
2612 g_free (disp2);
2614 if (prompt == B_REPLACE_ALL)
2615 edit->replace_mode = 1;
2616 else if (prompt == B_SKIP_REPLACE)
2618 if (edit_search_options.backwards)
2619 edit->search_start--;
2620 else
2621 edit->search_start++;
2622 continue; /* loop */
2624 else if (prompt == B_CANCEL)
2626 edit->replace_mode = -1;
2627 break; /* loop */
2631 repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2633 if (edit->search->error != MC_SEARCH_E_OK)
2635 edit_error_dialog (_("Replace"), edit->search->error_str);
2636 g_string_free (repl_str, TRUE);
2637 break;
2640 /* delete then insert new */
2641 for (i = 0; i < len; i++)
2642 edit_delete (edit, 1);
2644 for (i = 0; i < repl_str->len; i++)
2645 edit_insert (edit, repl_str->str[i]);
2647 edit->found_len = repl_str->len;
2648 g_string_free (repl_str, TRUE);
2649 times_replaced++;
2651 /* so that we don't find the same string again */
2652 if (edit_search_options.backwards)
2654 edit->search_start--;
2656 else
2658 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2660 if (edit->search_start >= edit->last_byte)
2661 break;
2664 edit_scroll_screen_over_cursor (edit);
2666 else
2668 /* try and find from right here for next search */
2669 edit->search_start = edit->curs1;
2670 edit_update_curs_col (edit);
2672 edit->force |= REDRAW_PAGE;
2673 edit_render_keypress (edit);
2675 if (times_replaced == 0)
2676 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2677 break;
2680 while (edit->replace_mode >= 0);
2682 edit_scroll_screen_over_cursor (edit);
2683 edit->force |= REDRAW_COMPLETELY;
2684 edit_render_keypress (edit);
2686 if ((edit->replace_mode == 1) && (times_replaced != 0))
2687 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2689 cleanup:
2690 g_free (input1);
2691 g_free (input2);
2692 if (input2_str != NULL)
2693 g_string_free (input2_str, TRUE);
2696 /* --------------------------------------------------------------------------------------------- */
2698 mc_search_cbret_t
2699 edit_search_cmd_callback (const void *user_data, gsize char_offset, int *current_char)
2701 *current_char = edit_get_byte ((WEdit *) user_data, (long) char_offset);
2702 return MC_SEARCH_CB_OK;
2705 /* --------------------------------------------------------------------------------------------- */
2707 void
2708 edit_search_cmd (WEdit * edit, gboolean again)
2711 if (edit == NULL)
2712 return;
2714 if (!again)
2715 edit_search (edit);
2716 else if (edit->last_search_string != NULL)
2717 edit_do_search (edit);
2718 else
2720 /* find last search string in history */
2721 GList *history;
2723 history = history_get (MC_HISTORY_SHARED_SEARCH);
2724 if (history != NULL && history->data != NULL)
2726 edit->last_search_string = (char *) history->data;
2727 history->data = NULL;
2728 history = g_list_first (history);
2729 g_list_foreach (history, (GFunc) g_free, NULL);
2730 g_list_free (history);
2732 edit->search = mc_search_new (edit->last_search_string, -1);
2733 if (edit->search == NULL)
2735 /* if not... then ask for an expression */
2736 g_free (edit->last_search_string);
2737 edit->last_search_string = NULL;
2738 edit_search (edit);
2740 else
2742 edit->search->search_type = edit_search_options.type;
2743 edit->search->is_all_charsets = edit_search_options.all_codepages;
2744 edit->search->is_case_sensitive = edit_search_options.case_sens;
2745 edit->search->whole_words = edit_search_options.whole_words;
2746 edit->search->search_fn = edit_search_cmd_callback;
2747 edit->search_line_type = edit_get_search_line_type (edit->search);
2748 edit_do_search (edit);
2751 else
2753 /* if not... then ask for an expression */
2754 g_free (edit->last_search_string);
2755 edit->last_search_string = NULL;
2756 edit_search (edit);
2762 /* --------------------------------------------------------------------------------------------- */
2764 * Check if it's OK to close the editor. If there are unsaved changes,
2765 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2768 gboolean
2769 edit_ok_to_exit (WEdit * edit)
2771 int act;
2773 if (!edit->modified)
2774 return TRUE;
2776 if (!mc_global.midnight_shutdown)
2778 if (!edit_check_newline (edit))
2779 return FALSE;
2781 query_set_sel (2);
2782 act = edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2783 _("&Yes"), _("&No"), _("&Cancel quit"));
2785 else
2787 act =
2788 edit_query_dialog2 (_("Quit"),
2789 _("Midnight Commander is being shut down.\nSave modified file?"),
2790 _("&Yes"), _("&No"));
2792 /* Esc is No */
2793 if (act == -1)
2794 act = 1;
2797 switch (act)
2799 case 0: /* Yes */
2800 edit_push_markers (edit);
2801 edit_set_markers (edit, 0, 0, 0, 0);
2802 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2803 return mc_global.midnight_shutdown;
2804 break;
2805 case 1: /* No */
2806 break;
2807 case 2: /* Cancel quit */
2808 case -1: /* Esc */
2809 return FALSE;
2812 return TRUE;
2815 /* --------------------------------------------------------------------------------------------- */
2816 /** save block, returns 1 on success */
2819 edit_save_block (WEdit * edit, const char *filename, long start, long finish)
2821 int len, file;
2822 vfs_path_t *vpath;
2824 vpath = vfs_path_from_str (filename);
2825 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2826 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2827 vfs_path_free (vpath);
2828 if (file == -1)
2829 return 0;
2831 if (edit->column_highlight)
2833 int r;
2835 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2836 if (r > 0)
2838 unsigned char *block, *p;
2840 p = block = edit_get_block (edit, start, finish, &len);
2841 while (len)
2843 r = mc_write (file, p, len);
2844 if (r < 0)
2845 break;
2846 p += r;
2847 len -= r;
2849 g_free (block);
2852 else
2854 unsigned char *buf;
2855 int i = start, end;
2857 len = finish - start;
2858 buf = g_malloc0 (TEMP_BUF_LEN);
2859 while (start != finish)
2861 end = min (finish, start + TEMP_BUF_LEN);
2862 for (; i < end; i++)
2863 buf[i - start] = edit_get_byte (edit, i);
2864 len -= mc_write (file, (char *) buf, end - start);
2865 start = end;
2867 g_free (buf);
2869 mc_close (file);
2870 if (len)
2871 return 0;
2872 return 1;
2875 /* --------------------------------------------------------------------------------------------- */
2877 void
2878 edit_paste_from_history (WEdit * edit)
2880 (void) edit;
2881 edit_error_dialog (_("Error"), _("This function is not implemented"));
2884 /* --------------------------------------------------------------------------------------------- */
2887 edit_copy_to_X_buf_cmd (WEdit * edit)
2889 long start_mark, end_mark;
2890 if (eval_marks (edit, &start_mark, &end_mark))
2891 return 0;
2892 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2894 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2895 return 1;
2897 /* try use external clipboard utility */
2898 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2900 return 0;
2903 /* --------------------------------------------------------------------------------------------- */
2906 edit_cut_to_X_buf_cmd (WEdit * edit)
2908 long start_mark, end_mark;
2909 if (eval_marks (edit, &start_mark, &end_mark))
2910 return 0;
2911 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2913 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2914 return 1;
2916 /* try use external clipboard utility */
2917 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2919 edit_block_delete_cmd (edit);
2920 edit_mark_cmd (edit, 1);
2921 return 0;
2924 /* --------------------------------------------------------------------------------------------- */
2926 void
2927 edit_paste_from_X_buf_cmd (WEdit * edit)
2929 vfs_path_t *tmp;
2931 /* try use external clipboard utility */
2932 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
2933 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
2934 edit_insert_file (edit, tmp);
2935 vfs_path_free (tmp);
2939 /* --------------------------------------------------------------------------------------------- */
2941 * Ask user for the line and go to that line.
2942 * Negative numbers mean line from the end (i.e. -1 is the last line).
2945 void
2946 edit_goto_cmd (WEdit * edit)
2948 char *f;
2949 static long line = 0; /* line as typed, saved as default */
2950 long l;
2951 char *error;
2952 char s[32];
2954 g_snprintf (s, sizeof (s), "%ld", line);
2955 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "");
2956 if (!f)
2957 return;
2959 if (!*f)
2961 g_free (f);
2962 return;
2965 l = strtol (f, &error, 0);
2966 if (*error)
2968 g_free (f);
2969 return;
2972 line = l;
2973 if (l < 0)
2974 l = edit->total_lines + l + 2;
2975 edit_move_display (edit, l - edit->widget.lines / 2 - 1);
2976 edit_move_to_line (edit, l - 1);
2977 edit->force |= REDRAW_COMPLETELY;
2978 g_free (f);
2982 /* --------------------------------------------------------------------------------------------- */
2983 /** Return 1 on success */
2986 edit_save_block_cmd (WEdit * edit)
2988 long start_mark, end_mark;
2989 char *exp, *tmp;
2991 if (eval_marks (edit, &start_mark, &end_mark))
2992 return 1;
2994 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
2995 exp =
2996 input_expand_dialog (_("Save block"), _("Enter file name:"),
2997 MC_HISTORY_EDIT_SAVE_BLOCK, tmp);
2998 g_free (tmp);
2999 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3000 if (exp)
3002 if (!*exp)
3004 g_free (exp);
3005 return 0;
3007 else
3009 if (edit_save_block (edit, exp, start_mark, end_mark))
3011 g_free (exp);
3012 edit->force |= REDRAW_COMPLETELY;
3013 return 1;
3015 else
3017 g_free (exp);
3018 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3022 edit->force |= REDRAW_COMPLETELY;
3023 return 0;
3027 /* --------------------------------------------------------------------------------------------- */
3029 /** returns TRUE on success */
3030 gboolean
3031 edit_insert_file_cmd (WEdit * edit)
3033 char *tmp;
3034 char *exp;
3035 gboolean ret = FALSE;
3037 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3038 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3039 MC_HISTORY_EDIT_INSERT_FILE, tmp);
3040 g_free (tmp);
3042 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3044 if (exp != NULL && *exp != '\0')
3046 vfs_path_t *exp_vpath;
3048 exp_vpath = vfs_path_from_str (exp);
3049 ret = (edit_insert_file (edit, exp_vpath) >= 0);
3050 vfs_path_free (exp_vpath);
3052 if (!ret)
3053 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3056 g_free (exp);
3058 edit->force |= REDRAW_COMPLETELY;
3059 return ret;
3062 /* --------------------------------------------------------------------------------------------- */
3063 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3066 edit_sort_cmd (WEdit * edit)
3068 static char *old = 0;
3069 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3070 long start_mark, end_mark;
3071 int e;
3073 if (eval_marks (edit, &start_mark, &end_mark))
3075 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3076 return 0;
3079 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3080 edit_save_block (edit, tmp, start_mark, end_mark);
3081 g_free (tmp);
3083 exp = input_dialog (_("Run sort"),
3084 _("Enter sort options (see manpage) separated by whitespace:"),
3085 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "");
3087 if (!exp)
3088 return 1;
3089 g_free (old);
3090 old = exp;
3091 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3092 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3093 tmp =
3094 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3095 " > ", tmp_edit_temp_name, (char *) NULL);
3096 g_free (tmp_edit_temp_name);
3097 g_free (tmp_edit_block_name);
3099 e = system (tmp);
3100 g_free (tmp);
3101 if (e)
3103 if (e == -1 || e == 127)
3105 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3107 else
3109 char q[8];
3110 sprintf (q, "%d ", e);
3111 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3112 edit_error_dialog (_("Sort"), tmp);
3113 g_free (tmp);
3115 return -1;
3118 edit->force |= REDRAW_COMPLETELY;
3120 if (edit_block_delete_cmd (edit))
3121 return 1;
3124 vfs_path_t *tmp_vpath;
3126 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3127 edit_insert_file (edit, tmp_vpath);
3128 vfs_path_free (tmp_vpath);
3130 return 0;
3133 /* --------------------------------------------------------------------------------------------- */
3135 * Ask user for a command, execute it and paste its output back to the
3136 * editor.
3140 edit_ext_cmd (WEdit * edit)
3142 char *exp, *tmp, *tmp_edit_temp_file;
3143 int e;
3145 exp =
3146 input_dialog (_("Paste output of external command"),
3147 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL);
3149 if (!exp)
3150 return 1;
3152 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3153 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3154 g_free (tmp_edit_temp_file);
3155 e = system (tmp);
3156 g_free (tmp);
3157 g_free (exp);
3159 if (e)
3161 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3162 return -1;
3165 edit->force |= REDRAW_COMPLETELY;
3168 vfs_path_t *tmp_vpath;
3170 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3171 edit_insert_file (edit, tmp_vpath);
3172 vfs_path_free (tmp_vpath);
3174 return 0;
3177 /* --------------------------------------------------------------------------------------------- */
3178 /** if block is 1, a block must be highlighted and the shell command
3179 processes it. If block is 0 the shell command is a straight system
3180 command, that just produces some output which is to be inserted */
3182 void
3183 edit_block_process_cmd (WEdit * edit, int macro_number)
3185 char *fname;
3186 char *macros_fname = NULL;
3188 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3189 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3190 user_menu (edit, macros_fname, 0);
3191 g_free (fname);
3192 g_free (macros_fname);
3193 edit->force |= REDRAW_COMPLETELY;
3196 /* --------------------------------------------------------------------------------------------- */
3198 void
3199 edit_mail_dialog (WEdit * edit)
3201 char *tmail_to;
3202 char *tmail_subject;
3203 char *tmail_cc;
3205 static char *mail_cc_last = 0;
3206 static char *mail_subject_last = 0;
3207 static char *mail_to_last = 0;
3209 QuickWidget quick_widgets[] = {
3210 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
3211 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
3212 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input", &tmail_cc),
3213 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT, N_("Copies to")),
3214 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-2",
3215 &tmail_subject),
3216 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT, N_("Subject")),
3217 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-3", &tmail_to),
3218 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT, N_("To")),
3219 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT, N_("mail -s <subject> -c <cc> <to>")),
3220 QUICK_END
3223 QuickDialog Quick_input = {
3224 50, MAIL_DLG_HEIGHT, -1, -1, N_("Mail"),
3225 "[Input Line Keys]", quick_widgets, NULL, FALSE
3228 quick_widgets[2].u.input.text = mail_cc_last ? mail_cc_last : "";
3229 quick_widgets[4].u.input.text = mail_subject_last ? mail_subject_last : "";
3230 quick_widgets[6].u.input.text = mail_to_last ? mail_to_last : "";
3232 if (quick_dialog (&Quick_input) != B_CANCEL)
3234 g_free (mail_cc_last);
3235 g_free (mail_subject_last);
3236 g_free (mail_to_last);
3237 mail_cc_last = tmail_cc;
3238 mail_subject_last = tmail_subject;
3239 mail_to_last = tmail_to;
3240 pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
3245 /*******************/
3246 /* Word Completion */
3247 /*******************/
3249 /* --------------------------------------------------------------------------------------------- */
3251 * Complete current word using regular expression search
3252 * backwards beginning at the current cursor position.
3255 void
3256 edit_complete_word_cmd (WEdit * edit)
3258 gsize i, max_len, word_len = 0, num_compl = 0;
3259 long word_start = 0;
3260 unsigned char *bufpos;
3261 char *match_expr;
3262 struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
3264 /* search start of word to be completed */
3265 if (!edit_find_word_start (edit, &word_start, &word_len))
3266 return;
3268 /* prepare match expression */
3269 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3271 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3272 match_expr =
3273 g_strdup_printf
3274 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3275 (int) word_len, bufpos);
3277 /* collect the possible completions */
3278 /* start search from begin to end of file */
3279 max_len =
3280 edit_collect_completions (edit, word_start, word_len, match_expr,
3281 (struct selection *) &compl, &num_compl);
3283 if (num_compl > 0)
3285 /* insert completed word if there is only one match */
3286 if (num_compl == 1)
3288 for (i = word_len; i < compl[0].len; i++)
3289 edit_insert (edit, *(compl[0].text + i));
3291 /* more than one possible completion => ask the user */
3292 else
3294 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3295 /* !!! pressed again the selection dialog pops up, but that !!! */
3296 /* !!! seems to require a further internal state !!! */
3297 /*tty_beep (); */
3299 /* let the user select the preferred completion */
3300 editcmd_dialog_completion_show (edit, max_len, word_len,
3301 (struct selection *) &compl, num_compl);
3305 g_free (match_expr);
3306 /* release memory before return */
3307 for (i = 0; i < num_compl; i++)
3308 g_free (compl[i].text);
3311 /* --------------------------------------------------------------------------------------------- */
3313 void
3314 edit_select_codepage_cmd (WEdit * edit)
3316 #ifdef HAVE_CHARSET
3317 if (do_select_codepage ())
3318 edit_set_codeset (edit);
3320 edit->force = REDRAW_COMPLETELY;
3321 edit_refresh_cmd (edit);
3322 #else
3323 (void) edit;
3324 #endif
3327 /* --------------------------------------------------------------------------------------------- */
3329 void
3330 edit_insert_literal_cmd (WEdit * edit)
3332 int char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3333 _("Press any key:"), 0);
3334 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3337 /* --------------------------------------------------------------------------------------------- */
3339 void
3340 edit_begin_end_macro_cmd (WEdit * edit)
3342 /* edit is a pointer to the widget */
3343 if (edit != NULL)
3345 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3346 edit_execute_key_command (edit, command, -1);
3350 /* --------------------------------------------------------------------------------------------- */
3352 void
3353 edit_begin_end_repeat_cmd (WEdit * edit)
3355 /* edit is a pointer to the widget */
3356 if (edit != NULL)
3358 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3359 edit_execute_key_command (edit, command, -1);
3363 /* --------------------------------------------------------------------------------------------- */
3365 gboolean
3366 edit_load_forward_cmd (WEdit * edit)
3368 if (edit->modified
3369 && edit_query_dialog2 (_("Warning"),
3370 _("Current text was modified without a file save.\n"
3371 "Continue discards these changes"), _("C&ontinue"),
3372 _("&Cancel")) == 1)
3374 edit->force |= REDRAW_COMPLETELY;
3375 return TRUE;
3378 if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
3379 return FALSE;
3381 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3382 return FALSE;
3384 edit_stack_iterator++;
3385 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3386 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3387 edit_history_moveto[edit_stack_iterator].line);
3389 return FALSE;
3392 /* --------------------------------------------------------------------------------------------- */
3394 gboolean
3395 edit_load_back_cmd (WEdit * edit)
3397 if (edit->modified
3398 && edit_query_dialog2 (_("Warning"),
3399 _("Current text was modified without a file save.\n"
3400 "Continue discards these changes"), _("C&ontinue"),
3401 _("&Cancel")) == 1)
3403 edit->force |= REDRAW_COMPLETELY;
3404 return TRUE;
3407 /* we are in the bottom of the stack, NO WAY! */
3408 if (edit_stack_iterator == 0)
3409 return FALSE;
3411 edit_stack_iterator--;
3412 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3413 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3414 edit_history_moveto[edit_stack_iterator].line);
3416 return FALSE;
3419 /* --------------------------------------------------------------------------------------------- */
3421 void
3422 edit_get_match_keyword_cmd (WEdit * edit)
3424 gsize word_len = 0, max_len = 0;
3425 int num_def = 0;
3426 int i;
3427 long word_start = 0;
3428 unsigned char *bufpos;
3429 char *match_expr;
3430 char *path = NULL;
3431 char *ptr = NULL;
3432 char *tagfile = NULL;
3434 etags_hash_t def_hash[MAX_DEFINITIONS];
3436 for (i = 0; i < MAX_DEFINITIONS; i++)
3438 def_hash[i].filename = NULL;
3441 /* search start of word to be completed */
3442 if (!edit_find_word_start (edit, &word_start, &word_len))
3443 return;
3445 /* prepare match expression */
3446 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3447 match_expr = g_strdup_printf ("%.*s", (int) word_len, bufpos);
3449 ptr = g_get_current_dir ();
3450 path = g_strconcat (ptr, G_DIR_SEPARATOR_S, (char *) NULL);
3451 g_free (ptr);
3453 /* Recursive search file 'TAGS' in parent dirs */
3456 ptr = g_path_get_dirname (path);
3457 g_free (path);
3458 path = ptr;
3459 g_free (tagfile);
3460 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3461 if (exist_file (tagfile))
3462 break;
3464 while (strcmp (path, G_DIR_SEPARATOR_S) != 0);
3466 if (tagfile)
3468 num_def =
3469 etags_set_definition_hash (tagfile, path, match_expr, (etags_hash_t *) & def_hash);
3470 g_free (tagfile);
3472 g_free (path);
3474 max_len = MAX_WIDTH_DEF_DIALOG;
3475 word_len = 0;
3476 if (num_def > 0)
3478 editcmd_dialog_select_definition_show (edit, match_expr, max_len, word_len,
3479 (etags_hash_t *) & def_hash, num_def);
3481 g_free (match_expr);
3484 /* --------------------------------------------------------------------------------------------- */