Minor optimization and type accuracy of some editor functions.
[midnight-commander.git] / src / editor / editcmd.c
blob75073d111ace0794274b8f4b24219eb30617cea2
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, line_width;
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 /* calculate line width and cursor position before cut */
687 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
688 edit_eol (edit, edit->curs1));
689 curs_pos = edit->curs_col + edit->over_col;
691 /* move cursor to start of selection */
692 edit_cursor_move (edit, start_mark - edit->curs1);
693 edit_scroll_screen_over_cursor (edit);
694 count = start_mark;
695 if (start_mark < end_mark)
697 if (edit->column_highlight)
699 if (edit->mark2 < 0)
700 edit_mark_cmd (edit, 0);
701 edit_delete_column_of_text (edit);
702 /* move cursor to the saved position */
703 edit_move_to_line (edit, curs_line);
704 /* calculate line width after cut */
705 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
706 edit_eol (edit, edit->curs1));
707 if (option_cursor_beyond_eol && curs_pos > line_width)
708 edit->over_col = curs_pos - line_width;
710 else
712 while (count < end_mark)
714 edit_delete (edit, 1);
715 count++;
719 edit_set_markers (edit, 0, 0, 0, 0);
720 edit->force |= REDRAW_PAGE;
721 return 0;
724 /* --------------------------------------------------------------------------------------------- */
726 * Get EOL symbol for searching.
728 * @param edit editor object
729 * @return EOL symbol
732 static inline char
733 edit_search_get_current_end_line_char (const WEdit * edit)
735 switch (edit->lb)
737 case LB_MAC:
738 return '\r';
739 default:
740 return '\n';
744 /* --------------------------------------------------------------------------------------------- */
746 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
748 * @param search search object
749 * @return result of checks.
752 static edit_search_line_t
753 edit_get_search_line_type (mc_search_t * search)
755 edit_search_line_t search_line_type = 0;
757 if (search->search_type != MC_SEARCH_T_REGEX)
758 return search_line_type;
760 if (*search->original == '^')
761 search_line_type |= AT_START_LINE;
763 if (search->original[search->original_len - 1] == '$')
764 search_line_type |= AT_END_LINE;
765 return search_line_type;
768 /* --------------------------------------------------------------------------------------------- */
770 * Calculating the start position of next line.
772 * @param edit editor object
773 * @param current_pos current position
774 * @param max_pos max position
775 * @param end_string_symbol end of line symbol
776 * @return start position of next line
779 static off_t
780 edit_calculate_start_of_next_line (WEdit * edit, off_t current_pos, off_t max_pos,
781 char end_string_symbol)
783 off_t i;
785 for (i = current_pos; i < max_pos; i++)
787 current_pos++;
788 if (edit_get_byte (edit, i) == end_string_symbol)
789 break;
792 return current_pos;
795 /* --------------------------------------------------------------------------------------------- */
797 * Calculating the end position of previous line.
799 * @param edit editor object
800 * @param current_pos current position
801 * @param end_string_symbol end of line symbol
802 * @return end position of previous line
805 static off_t
806 edit_calculate_end_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
808 off_t i;
810 for (i = current_pos - 1; i >= 0; i--)
811 if (edit_get_byte (edit, i) == end_string_symbol)
812 break;
814 return i;
817 /* --------------------------------------------------------------------------------------------- */
819 * Calculating the start position of previous line.
821 * @param edit editor object
822 * @param current_pos current position
823 * @param end_string_symbol end of line symbol
824 * @return start position of previous line
827 static inline off_t
828 edit_calculate_start_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
830 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
831 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
833 return (current_pos + 1);
836 /* --------------------------------------------------------------------------------------------- */
838 * Calculating the start position of current line.
840 * @param edit editor object
841 * @param current_pos current position
842 * @param end_string_symbol end of line symbol
843 * @return start position of current line
846 static inline off_t
847 edit_calculate_start_of_current_line (WEdit * edit, off_t current_pos, char end_string_symbol)
849 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
851 return (current_pos + 1);
854 /* --------------------------------------------------------------------------------------------- */
856 * Fixing (if needed) search start position if 'only in selection' option present.
858 * @param edit editor object
861 static void
862 edit_search_fix_search_start_if_selection (WEdit * edit)
864 long start_mark = 0;
865 long end_mark = 0;
867 if (!edit_search_options.only_in_selection)
868 return;
870 if (eval_marks (edit, &start_mark, &end_mark) != 0)
871 return;
873 if (edit_search_options.backwards)
875 if (edit->search_start > end_mark || edit->search_start <= start_mark)
876 edit->search_start = end_mark;
878 else
880 if (edit->search_start < start_mark || edit->search_start >= end_mark)
881 edit->search_start = start_mark;
885 /* --------------------------------------------------------------------------------------------- */
887 static gboolean
888 editcmd_find (WEdit * edit, gsize * len)
890 off_t search_start = edit->search_start;
891 off_t search_end;
892 long start_mark = 0;
893 long end_mark = edit->last_byte;
894 int mark_res = 0;
895 char end_string_symbol;
897 end_string_symbol = edit_search_get_current_end_line_char (edit);
899 /* prepare for search */
900 if (edit_search_options.only_in_selection)
902 mark_res = eval_marks (edit, &start_mark, &end_mark);
903 if (mark_res != 0)
905 edit->search->error = MC_SEARCH_E_NOTFOUND;
906 edit->search->error_str = g_strdup (_("Search string not found"));
907 return FALSE;
910 /* fix the start and the end of search block positions */
911 if ((edit->search_line_type & AT_START_LINE) != 0
912 && (start_mark != 0 || edit_get_byte (edit, start_mark - 1) != end_string_symbol))
914 start_mark =
915 edit_calculate_start_of_next_line (edit, start_mark, edit->last_byte,
916 end_string_symbol);
918 if ((edit->search_line_type & AT_END_LINE) != 0
919 && (end_mark - 1 != edit->last_byte
920 || edit_get_byte (edit, end_mark) != end_string_symbol))
922 end_mark = edit_calculate_end_of_previous_line (edit, end_mark, end_string_symbol);
924 if (start_mark >= end_mark)
926 edit->search->error = MC_SEARCH_E_NOTFOUND;
927 edit->search->error_str = g_strdup (_("Search string not found"));
928 return FALSE;
931 else
933 if (edit_search_options.backwards)
934 end_mark = max (1, edit->curs1) - 1;
937 /* search */
938 if (edit_search_options.backwards)
940 /* backward search */
941 search_end = end_mark;
943 if ((edit->search_line_type & AT_START_LINE) != 0)
944 search_start =
945 edit_calculate_start_of_current_line (edit, search_start, end_string_symbol);
947 while ((int) search_start >= start_mark)
949 if (search_end > (off_t) (search_start + edit->search->original_len)
950 && mc_search_is_fixed_search_str (edit->search))
952 search_end = search_start + edit->search->original_len;
954 if (mc_search_run (edit->search, (void *) edit, search_start, search_end, len)
955 && edit->search->normal_offset == search_start)
957 return TRUE;
961 if ((edit->search_line_type & AT_START_LINE) != 0)
962 search_start =
963 edit_calculate_start_of_previous_line (edit, search_start, end_string_symbol);
964 else
965 search_start--;
967 edit->search->error_str = g_strdup (_("Search string not found"));
969 else
971 /* forward search */
972 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
973 search_start =
974 edit_calculate_start_of_next_line (edit, search_start, end_mark, end_string_symbol);
975 return mc_search_run (edit->search, (void *) edit, search_start, end_mark, len);
977 return FALSE;
980 /* --------------------------------------------------------------------------------------------- */
982 static char *
983 edit_replace_cmd__conv_to_display (char *str)
985 #ifdef HAVE_CHARSET
986 GString *tmp;
988 tmp = str_convert_to_display (str);
989 if (tmp != NULL)
991 if (tmp->len != 0)
992 return g_string_free (tmp, FALSE);
993 g_string_free (tmp, TRUE);
995 #endif
996 return g_strdup (str);
999 /* --------------------------------------------------------------------------------------------- */
1001 static char *
1002 edit_replace_cmd__conv_to_input (char *str)
1004 #ifdef HAVE_CHARSET
1005 GString *tmp;
1007 tmp = str_convert_to_input (str);
1008 if (tmp != NULL)
1010 if (tmp->len != 0)
1011 return g_string_free (tmp, FALSE);
1012 g_string_free (tmp, TRUE);
1014 #endif
1015 return g_strdup (str);
1018 /* --------------------------------------------------------------------------------------------- */
1020 static void
1021 edit_do_search (WEdit * edit)
1023 gsize len = 0;
1025 if (edit->search == NULL)
1026 edit->search_start = edit->curs1;
1028 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1030 if (search_create_bookmark)
1032 int found = 0, books = 0;
1033 long l = 0, l_last = -1;
1034 long q = 0;
1036 search_create_bookmark = FALSE;
1037 book_mark_flush (edit, -1);
1039 while (TRUE)
1041 if (!mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
1042 break;
1043 if (found == 0)
1044 edit->search_start = edit->search->normal_offset;
1045 found++;
1046 l += edit_count_lines (edit, q, edit->search->normal_offset);
1047 if (l != l_last)
1049 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
1050 books++;
1052 l_last = l;
1053 q = edit->search->normal_offset + 1;
1056 if (found == 0)
1057 edit_error_dialog (_("Search"), _("Search string not found"));
1058 else
1059 edit_cursor_move (edit, edit->search_start - edit->curs1);
1061 else
1063 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
1064 && edit_search_options.backwards)
1065 edit->search_start--;
1067 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
1068 && !edit_search_options.backwards)
1069 edit->search_start++;
1071 if (editcmd_find (edit, &len))
1073 edit->found_start = edit->search_start = edit->search->normal_offset;
1074 edit->found_len = len;
1075 edit->over_col = 0;
1076 edit_cursor_move (edit, edit->search_start - edit->curs1);
1077 edit_scroll_screen_over_cursor (edit);
1078 if (edit_search_options.backwards)
1079 edit->search_start--;
1080 else
1081 edit->search_start++;
1083 else
1085 edit->search_start = edit->curs1;
1086 if (edit->search->error_str != NULL)
1087 edit_error_dialog (_("Search"), edit->search->error_str);
1091 edit->force |= REDRAW_COMPLETELY;
1092 edit_scroll_screen_over_cursor (edit);
1095 /* --------------------------------------------------------------------------------------------- */
1097 static void
1098 edit_search (WEdit * edit)
1100 if (editcmd_dialog_search_show (edit))
1102 edit->search_line_type = edit_get_search_line_type (edit->search);
1103 edit_search_fix_search_start_if_selection (edit);
1104 edit_do_search (edit);
1108 /* --------------------------------------------------------------------------------------------- */
1109 /** Return a null terminated length of text. Result must be g_free'd */
1111 static unsigned char *
1112 edit_get_block (WEdit * edit, long start, long finish, int *l)
1114 unsigned char *s, *r;
1115 r = s = g_malloc0 (finish - start + 1);
1116 if (edit->column_highlight)
1118 *l = 0;
1119 /* copy from buffer, excluding chars that are out of the column 'margins' */
1120 while (start < finish)
1122 int c;
1123 long x;
1124 x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
1125 c = edit_get_byte (edit, start);
1126 if ((x >= edit->column1 && x < edit->column2)
1127 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1129 *s++ = c;
1130 (*l)++;
1132 start++;
1135 else
1137 *l = finish - start;
1138 while (start < finish)
1139 *s++ = edit_get_byte (edit, start++);
1141 *s = 0;
1142 return r;
1145 /* --------------------------------------------------------------------------------------------- */
1146 /** copies a block to clipboard file */
1148 static int
1149 edit_save_block_to_clip_file (WEdit * edit, long start, long finish)
1151 int ret;
1152 gchar *tmp;
1153 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1154 ret = edit_save_block (edit, tmp, start, finish);
1155 g_free (tmp);
1156 return ret;
1159 /* --------------------------------------------------------------------------------------------- */
1161 static void
1162 pipe_mail (WEdit * edit, char *to, char *subject, char *cc)
1164 FILE *p = 0;
1165 char *s;
1167 to = name_quote (to, 0);
1168 subject = name_quote (subject, 0);
1169 cc = name_quote (cc, 0);
1170 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1171 g_free (to);
1172 g_free (subject);
1173 g_free (cc);
1175 if (s)
1177 p = popen (s, "w");
1178 g_free (s);
1181 if (p)
1183 long i;
1184 for (i = 0; i < edit->last_byte; i++)
1185 fputc (edit_get_byte (edit, i), p);
1186 pclose (p);
1190 /* --------------------------------------------------------------------------------------------- */
1192 static gboolean
1193 is_break_char (char c)
1195 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
1198 /* --------------------------------------------------------------------------------------------- */
1199 /** find first character of current word */
1201 static int
1202 edit_find_word_start (WEdit * edit, long *word_start, gsize * word_len)
1204 int c, last;
1205 gsize i;
1207 /* return if at begin of file */
1208 if (edit->curs1 <= 0)
1209 return 0;
1211 c = (unsigned char) edit_get_byte (edit, edit->curs1 - 1);
1212 /* return if not at end or in word */
1213 if (is_break_char (c))
1214 return 0;
1216 /* search start of word to be completed */
1217 for (i = 2;; i++)
1219 /* return if at begin of file */
1220 if ((gsize) edit->curs1 < i)
1221 return 0;
1223 last = c;
1224 c = (unsigned char) edit_get_byte (edit, edit->curs1 - i);
1226 if (is_break_char (c))
1228 /* return if word starts with digit */
1229 if (isdigit (last))
1230 return 0;
1232 *word_start = edit->curs1 - (i - 1); /* start found */
1233 *word_len = i - 1;
1234 break;
1237 /* success */
1238 return 1;
1241 /* --------------------------------------------------------------------------------------------- */
1243 * Get current word under cursor
1245 * @param edit editor object
1246 * @param srch mc_search object
1247 * @param word_start start word position
1249 * @return newly allocated string or NULL if no any words under cursor
1252 static char *
1253 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, long word_start)
1255 gsize len = 0, i;
1256 GString *temp;
1258 if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len))
1259 return NULL;
1261 temp = g_string_sized_new (len);
1263 for (i = 0; i < len; i++)
1265 int chr;
1267 chr = edit_get_byte (edit, word_start + i);
1268 if (!isspace (chr))
1269 g_string_append_c (temp, chr);
1272 return g_string_free (temp, temp->len == 0);
1275 /* --------------------------------------------------------------------------------------------- */
1276 /** collect the possible completions */
1278 static gsize
1279 edit_collect_completions (WEdit * edit, long word_start, gsize word_len,
1280 char *match_expr, struct selection *compl, gsize * num)
1282 gsize len = 0;
1283 gsize max_len = 0;
1284 gsize i;
1285 int skip;
1286 GString *temp;
1287 mc_search_t *srch;
1288 long last_byte, start = -1;
1289 char *current_word;
1291 srch = mc_search_new (match_expr, -1);
1292 if (srch == NULL)
1293 return 0;
1295 if (mc_config_get_bool
1296 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1298 last_byte = edit->last_byte;
1300 else
1302 last_byte = word_start;
1305 srch->search_type = MC_SEARCH_T_REGEX;
1306 srch->is_case_sensitive = TRUE;
1307 srch->search_fn = edit_search_cmd_callback;
1309 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1311 temp = g_string_new ("");
1313 /* collect max MAX_WORD_COMPLETIONS completions */
1314 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1316 g_string_set_size (temp, 0);
1317 start = srch->normal_offset;
1319 /* add matched completion if not yet added */
1320 for (i = 0; i < len; i++)
1322 skip = edit_get_byte (edit, start + i);
1323 if (isspace (skip))
1324 continue;
1326 /* skip current word */
1327 if (start + (long) i == word_start)
1328 break;
1330 g_string_append_c (temp, skip);
1333 if (temp->len == 0)
1334 continue;
1336 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1337 continue;
1339 skip = 0;
1341 for (i = 0; i < *num; i++)
1343 if (strncmp
1344 ((char *) &compl[i].text[word_len],
1345 (char *) &temp->str[word_len], max (len, compl[i].len) - word_len) == 0)
1347 struct selection this = compl[i];
1348 for (++i; i < *num; i++)
1350 compl[i - 1] = compl[i];
1352 compl[*num - 1] = this;
1353 skip = 1;
1354 break; /* skip it, already added */
1357 if (skip != 0)
1358 continue;
1360 if (*num == MAX_WORD_COMPLETIONS)
1362 g_free (compl[0].text);
1363 for (i = 1; i < *num; i++)
1365 compl[i - 1] = compl[i];
1367 (*num)--;
1369 #ifdef HAVE_CHARSET
1371 GString *recoded;
1372 recoded = str_convert_to_display (temp->str);
1374 if (recoded && recoded->len)
1375 g_string_assign (temp, recoded->str);
1377 g_string_free (recoded, TRUE);
1379 #endif
1380 compl[*num].text = g_strdup (temp->str);
1381 compl[*num].len = temp->len;
1382 (*num)++;
1383 start += len;
1385 /* note the maximal length needed for the completion dialog */
1386 if (len > max_len)
1387 max_len = len;
1390 mc_search_free (srch);
1391 g_string_free (temp, TRUE);
1392 g_free (current_word);
1394 return max_len;
1397 /* --------------------------------------------------------------------------------------------- */
1399 static void
1400 edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width,
1401 long *start_pos, long *end_pos, int *col1, int *col2)
1403 long cursor;
1404 int i, col;
1406 cursor = edit->curs1;
1407 col = edit_get_col (edit);
1409 for (i = 0; i < size; i++)
1411 if (data[i] != '\n')
1412 edit_insert (edit, data[i]);
1413 else
1414 { /* fill in and move to next line */
1415 int l;
1416 long p;
1418 if (edit_get_byte (edit, edit->curs1) != '\n')
1420 l = width - (edit_get_col (edit) - col);
1421 while (l > 0)
1423 edit_insert (edit, ' ');
1424 l -= space_width;
1427 for (p = edit->curs1;; p++)
1429 if (p == edit->last_byte)
1431 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1432 edit_insert_ahead (edit, '\n');
1433 p++;
1434 break;
1436 if (edit_get_byte (edit, p) == '\n')
1438 p++;
1439 break;
1442 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1443 l = col - edit_get_col (edit);
1444 while (l >= space_width)
1446 edit_insert (edit, ' ');
1447 l -= space_width;
1452 *col1 = col;
1453 *col2 = col + width;
1454 *start_pos = cursor;
1455 *end_pos = edit->curs1;
1456 edit_cursor_move (edit, cursor - edit->curs1);
1459 /* --------------------------------------------------------------------------------------------- */
1461 static int
1462 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1464 const macros_t *m1 = (const macros_t *) macro1;
1465 const macros_t *m2 = (const macros_t *) macro2;
1467 return m1->hotkey - m2->hotkey;
1470 /* --------------------------------------------------------------------------------------------- */
1472 static void
1473 edit_macro_sort_by_hotkey (void)
1475 if (macros_list != NULL && macros_list->len != 0)
1476 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1479 /* --------------------------------------------------------------------------------------------- */
1481 static gboolean
1482 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1484 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1485 macros_t *result;
1486 macros_t search_macro;
1488 (void) edit;
1490 search_macro.hotkey = hotkey;
1491 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1492 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1494 if (result != NULL && result->macro != NULL)
1496 *indx = (result - array_start);
1497 *macros = result;
1498 return TRUE;
1500 *indx = 0;
1501 return FALSE;
1504 /* --------------------------------------------------------------------------------------------- */
1505 /** returns FALSE on error */
1507 static gboolean
1508 edit_delete_macro (WEdit * edit, int hotkey)
1510 mc_config_t *macros_config = NULL;
1511 const char *section_name = "editor";
1512 gchar *macros_fname;
1513 guint indx;
1514 char *skeyname;
1515 const macros_t *macros = NULL;
1517 /* clear array of actions for current hotkey */
1518 while (edit_get_macro (edit, hotkey, &macros, &indx))
1520 if (macros->macro != NULL)
1521 g_array_free (macros->macro, TRUE);
1522 macros = NULL;
1523 g_array_remove_index (macros_list, indx);
1524 edit_macro_sort_by_hotkey ();
1527 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1528 macros_config = mc_config_init (macros_fname);
1529 g_free (macros_fname);
1531 if (macros_config == NULL)
1532 return FALSE;
1534 skeyname = lookup_key_by_code (hotkey);
1535 while (mc_config_del_key (macros_config, section_name, skeyname))
1537 g_free (skeyname);
1538 mc_config_save_file (macros_config, NULL);
1539 mc_config_deinit (macros_config);
1540 return TRUE;
1544 /* --------------------------------------------------------------------------------------------- */
1545 /*** public functions ****************************************************************************/
1546 /* --------------------------------------------------------------------------------------------- */
1548 void
1549 edit_help_cmd (WEdit * edit)
1551 ev_help_t event_data = { NULL, "[Internal File Editor]" };
1552 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
1554 edit->force |= REDRAW_COMPLETELY;
1557 /* --------------------------------------------------------------------------------------------- */
1559 void
1560 edit_refresh_cmd (WEdit * edit)
1562 #ifdef HAVE_SLANG
1563 int color;
1565 edit_get_syntax_color (edit, -1, &color);
1566 tty_touch_screen ();
1567 mc_refresh ();
1568 #else
1569 (void) edit;
1571 clr_scr ();
1572 repaint_screen ();
1573 #endif /* !HAVE_SLANG */
1574 tty_keypad (TRUE);
1577 /* --------------------------------------------------------------------------------------------- */
1579 void
1580 menu_save_mode_cmd (void)
1582 /* diaog sizes */
1583 const int DLG_X = 38;
1584 const int DLG_Y = 13;
1586 char *str_result;
1588 const char *str[] = {
1589 N_("&Quick save"),
1590 N_("&Safe save"),
1591 N_("&Do backups with following extension:")
1594 QuickWidget widgets[] = {
1595 /* 0 */
1596 QUICK_BUTTON (18, DLG_X, DLG_Y - 3, DLG_Y, N_("&Cancel"), B_CANCEL, NULL),
1597 /* 1 */
1598 QUICK_BUTTON (6, DLG_X, DLG_Y - 3, DLG_Y, N_("&OK"), B_ENTER, NULL),
1599 /* 2 */
1600 QUICK_CHECKBOX (4, DLG_X, 8, DLG_Y, N_("Check &POSIX new line"), &option_check_nl_at_eof),
1601 /* 3 */
1602 QUICK_INPUT (8, DLG_X, 6, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result),
1603 /* 4 */
1604 QUICK_RADIO (4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode),
1605 QUICK_END
1608 QuickDialog dialog = {
1609 DLG_X, DLG_Y, -1, -1, N_("Edit Save Mode"),
1610 "[Edit Save Mode]", widgets, NULL, FALSE
1613 size_t i;
1614 size_t maxlen = 0;
1615 size_t w0, w1, b_len, w3;
1617 #ifdef HAVE_ASSERT_H
1618 assert (option_backup_ext != NULL);
1619 #endif
1621 /* OK/Cancel buttons */
1622 w0 = str_term_width1 (_(widgets[0].u.button.text)) + 3;
1623 w1 = str_term_width1 (_(widgets[1].u.button.text)) + 5; /* default button */
1624 b_len = w0 + w1 + 3;
1626 maxlen = max (b_len, (size_t) str_term_width1 (_(dialog.title)) + 2);
1628 w3 = 0;
1629 for (i = 0; i < 3; i++)
1631 #ifdef ENABLE_NLS
1632 str[i] = _(str[i]);
1633 #endif
1634 w3 = max (w3, (size_t) str_term_width1 (str[i]));
1637 maxlen = max (maxlen, w3 + 4);
1639 dialog.xlen = min ((size_t) COLS, maxlen + 8);
1641 widgets[3].u.input.len = w3;
1642 widgets[1].relative_x = (dialog.xlen - b_len) / 2;
1643 widgets[0].relative_x = widgets[1].relative_x + w0 + 2;
1645 for (i = 0; i < sizeof (widgets) / sizeof (widgets[0]); i++)
1646 widgets[i].x_divisions = dialog.xlen;
1648 if (quick_dialog (&dialog) != B_CANCEL)
1650 g_free (option_backup_ext);
1651 option_backup_ext = str_result;
1655 /* --------------------------------------------------------------------------------------------- */
1657 void
1658 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1660 vfs_path_free (edit->filename_vpath);
1661 edit->filename_vpath = vfs_path_clone (name_vpath);
1663 if (edit->dir_vpath == NULL)
1664 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1667 /* --------------------------------------------------------------------------------------------- */
1668 /* Here we want to warn the users of overwriting an existing file,
1669 but only if they have made a change to the filename */
1670 /* returns 1 on success */
1672 edit_save_as_cmd (WEdit * edit)
1674 /* This heads the 'Save As' dialog box */
1675 vfs_path_t *exp_vpath;
1676 int save_lock = 0;
1677 int different_filename = 0;
1679 if (!edit_check_newline (edit))
1680 return 0;
1682 exp_vpath = edit_get_save_file_as (edit);
1683 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1685 if (exp_vpath != NULL)
1687 if (vfs_path_len (exp_vpath) == 0)
1688 goto ret;
1689 else
1691 int rv;
1693 if (vfs_path_cmp (edit->filename_vpath, exp_vpath) != 0)
1695 int file;
1696 struct stat sb;
1698 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1700 edit_error_dialog (_("Save as"),
1701 get_sys_error (_
1702 ("Cannot save: destination is not a regular file")));
1703 goto ret;
1706 different_filename = 1;
1707 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1709 if (file != -1)
1711 /* the file exists */
1712 mc_close (file);
1713 /* Overwrite the current file or cancel the operation */
1714 if (edit_query_dialog2
1715 (_("Warning"),
1716 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1717 goto ret;
1719 else
1721 edit->stat1.st_mode |= S_IWUSR;
1723 save_lock = lock_file (exp_vpath);
1725 else
1727 /* filenames equal, check if already locked */
1728 if (!edit->locked && !edit->delete_file)
1729 save_lock = lock_file (exp_vpath);
1732 if (different_filename)
1735 * Allow user to write into saved (under another name) file
1736 * even if original file had r/o user permissions.
1738 edit->stat1.st_mode |= S_IWRITE;
1741 rv = edit_save_file (edit, exp_vpath);
1742 switch (rv)
1744 case 1:
1745 /* Succesful, so unlock both files */
1746 if (different_filename)
1748 if (save_lock)
1749 unlock_file (exp_vpath);
1750 if (edit->locked)
1751 edit->locked = unlock_file (edit->filename_vpath);
1753 else
1755 if (edit->locked || save_lock)
1756 edit->locked = unlock_file (edit->filename_vpath);
1759 edit_set_filename (edit, exp_vpath);
1760 if (edit->lb != LB_ASIS)
1761 edit_reload (edit, exp_vpath);
1762 edit->modified = 0;
1763 edit->delete_file = 0;
1764 if (different_filename)
1765 edit_load_syntax (edit, NULL, edit->syntax_type);
1766 vfs_path_free (exp_vpath);
1767 edit->force |= REDRAW_COMPLETELY;
1768 return 1;
1769 default:
1770 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1771 /* fallthrough */
1772 case -1:
1773 /* Failed, so maintain modify (not save) lock */
1774 if (save_lock)
1775 unlock_file (exp_vpath);
1776 break;
1781 ret:
1782 vfs_path_free (exp_vpath);
1783 edit->force |= REDRAW_COMPLETELY;
1784 return 0;
1787 /* {{{ Macro stuff starts here */
1788 /* --------------------------------------------------------------------------------------------- */
1790 void
1791 edit_delete_macro_cmd (WEdit * edit)
1793 int hotkey;
1795 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1797 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1798 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1801 /* --------------------------------------------------------------------------------------------- */
1803 /** returns FALSE on error */
1804 gboolean
1805 edit_execute_macro (WEdit * edit, int hotkey)
1807 gboolean res = FALSE;
1809 if (hotkey != 0)
1811 const macros_t *macros;
1812 guint indx;
1814 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1815 macros->macro != NULL && macros->macro->len != 0)
1817 guint i;
1819 edit->force |= REDRAW_PAGE;
1821 for (i = 0; i < macros->macro->len; i++)
1823 const macro_action_t *m_act;
1825 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1826 edit_execute_cmd (edit, m_act->action, m_act->ch);
1827 res = TRUE;
1832 return res;
1835 /* --------------------------------------------------------------------------------------------- */
1837 /** returns FALSE on error */
1838 gboolean
1839 edit_store_macro_cmd (WEdit * edit)
1841 int i;
1842 int hotkey;
1843 GString *marcros_string;
1844 mc_config_t *macros_config = NULL;
1845 const char *section_name = "editor";
1846 gchar *macros_fname;
1847 GArray *macros; /* current macro */
1848 int tmp_act;
1849 gboolean have_macro = FALSE;
1850 char *skeyname = NULL;
1852 hotkey = editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1853 if (hotkey == ESC_CHAR)
1854 return FALSE;
1856 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1858 /* return FALSE if try assign macro into restricted hotkeys */
1859 if (tmp_act == CK_MacroStartRecord
1860 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1861 return FALSE;
1863 edit_delete_macro (edit, hotkey);
1865 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1866 macros_config = mc_config_init (macros_fname);
1867 g_free (macros_fname);
1869 if (macros_config == NULL)
1870 return FALSE;
1872 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1874 marcros_string = g_string_sized_new (250);
1875 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1877 skeyname = lookup_key_by_code (hotkey);
1879 for (i = 0; i < macro_index; i++)
1881 macro_action_t m_act;
1882 const char *action_name;
1884 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1886 if (action_name == NULL)
1887 break;
1889 m_act.action = record_macro_buf[i].action;
1890 m_act.ch = record_macro_buf[i].ch;
1891 g_array_append_val (macros, m_act);
1892 have_macro = TRUE;
1893 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1894 (int) record_macro_buf[i].ch);
1896 if (have_macro)
1898 macros_t macro;
1899 macro.hotkey = hotkey;
1900 macro.macro = macros;
1901 g_array_append_val (macros_list, macro);
1902 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1904 else
1905 mc_config_del_key (macros_config, section_name, skeyname);
1907 g_free (skeyname);
1908 edit_macro_sort_by_hotkey ();
1910 g_string_free (marcros_string, TRUE);
1911 mc_config_save_file (macros_config, NULL);
1912 mc_config_deinit (macros_config);
1913 return TRUE;
1916 /* --------------------------------------------------------------------------------------------- */
1918 gboolean
1919 edit_repeat_macro_cmd (WEdit * edit)
1921 int i, j;
1922 char *f;
1923 long count_repeat;
1924 char *error = NULL;
1926 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL);
1927 if (f == NULL || *f == '\0')
1929 g_free (f);
1930 return FALSE;
1933 count_repeat = strtol (f, &error, 0);
1935 if (*error != '\0')
1937 g_free (f);
1938 return FALSE;
1941 g_free (f);
1943 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1944 edit->force |= REDRAW_PAGE;
1946 for (j = 0; j < count_repeat; j++)
1947 for (i = 0; i < macro_index; i++)
1948 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1949 edit_update_screen (edit);
1950 return TRUE;
1953 /* --------------------------------------------------------------------------------------------- */
1954 /** return FALSE on error */
1956 gboolean
1957 edit_load_macro_cmd (WEdit * edit)
1959 mc_config_t *macros_config = NULL;
1960 gchar **profile_keys, **keys;
1961 gchar **values, **curr_values;
1962 gsize len, values_len;
1963 const char *section_name = "editor";
1964 gchar *macros_fname;
1965 int hotkey;
1967 (void) edit;
1969 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1970 macros_config = mc_config_init (macros_fname);
1971 g_free (macros_fname);
1973 if (macros_config == NULL)
1974 return FALSE;
1976 profile_keys = keys = mc_config_get_keys (macros_config, section_name, &len);
1977 while (*profile_keys != NULL)
1979 gboolean have_macro;
1980 GArray *macros;
1981 macros_t macro;
1983 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1985 curr_values = values = mc_config_get_string_list (macros_config, section_name,
1986 *profile_keys, &values_len);
1987 hotkey = lookup_key (*profile_keys, NULL);
1988 have_macro = FALSE;
1990 while (*curr_values != NULL && *curr_values[0] != '\0')
1992 char **macro_pair = NULL;
1994 macro_pair = g_strsplit (*curr_values, ":", 2);
1996 if (macro_pair != NULL)
1998 macro_action_t m_act;
1999 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
2000 m_act.action = 0;
2001 else
2003 m_act.action = keybind_lookup_action (macro_pair[0]);
2004 g_free (macro_pair[0]);
2005 macro_pair[0] = NULL;
2007 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
2008 m_act.ch = -1;
2009 else
2011 m_act.ch = strtol (macro_pair[1], NULL, 0);
2012 g_free (macro_pair[1]);
2013 macro_pair[1] = NULL;
2015 if (m_act.action != 0)
2017 /* a shell command */
2018 if ((m_act.action / CK_PipeBlock (0)) == 1)
2020 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
2021 m_act.ch = -1;
2023 g_array_append_val (macros, m_act);
2024 have_macro = TRUE;
2026 g_strfreev (macro_pair);
2027 macro_pair = NULL;
2029 curr_values++;
2031 if (have_macro)
2033 macro.hotkey = hotkey;
2034 macro.macro = macros;
2035 g_array_append_val (macros_list, macro);
2037 profile_keys++;
2038 g_strfreev (values);
2040 g_strfreev (keys);
2041 mc_config_deinit (macros_config);
2042 edit_macro_sort_by_hotkey ();
2043 return TRUE;
2046 /* }}} Macro stuff end here */
2048 /* --------------------------------------------------------------------------------------------- */
2049 /** returns 1 on success */
2052 edit_save_confirm_cmd (WEdit * edit)
2054 gchar *f = NULL;
2056 if (edit->filename_vpath == NULL)
2057 return edit_save_as_cmd (edit);
2059 if (!edit_check_newline (edit))
2060 return 0;
2062 if (edit_confirm_save)
2064 char *filename;
2065 gboolean ok;
2067 filename = vfs_path_to_str (edit->filename_vpath);
2068 f = g_strdup_printf (_("Confirm save file: \"%s\""), filename);
2069 g_free (filename);
2070 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2071 g_free (f);
2072 if (!ok)
2073 return 0;
2075 return edit_save_cmd (edit);
2078 /* --------------------------------------------------------------------------------------------- */
2080 /* returns TRUE on success */
2081 gboolean
2082 edit_new_cmd (WEdit * edit)
2084 if (edit->modified
2085 && edit_query_dialog2 (_("Warning"),
2086 _("Current text was modified without a file save.\n"
2087 "Continue discards these changes"),
2088 _("C&ontinue"), _("&Cancel")) == 1)
2090 edit->force |= REDRAW_COMPLETELY;
2091 return TRUE;
2094 edit->force |= REDRAW_COMPLETELY;
2096 return edit_renew (edit); /* if this gives an error, something has really screwed up */
2099 /* --------------------------------------------------------------------------------------------- */
2101 gboolean
2102 edit_load_cmd (WEdit * edit, edit_current_file_t what)
2104 gboolean ret = TRUE;
2106 if (edit->modified
2107 && edit_query_dialog2 (_("Warning"),
2108 _("Current text was modified without a file save.\n"
2109 "Continue discards these changes"), _("C&ontinue"),
2110 _("&Cancel")) == 1)
2112 edit->force |= REDRAW_COMPLETELY;
2113 return TRUE;
2116 switch (what)
2118 case EDIT_FILE_COMMON:
2120 char *filename, *exp;
2122 filename = vfs_path_to_str (edit->filename_vpath);
2123 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2124 MC_HISTORY_EDIT_LOAD, filename);
2125 g_free (filename);
2127 if (exp != NULL && *exp != '\0')
2129 vfs_path_t *exp_vpath;
2131 exp_vpath = vfs_path_from_str (exp);
2132 ret = edit_load_file_from_filename (edit, exp_vpath);
2133 vfs_path_free (exp_vpath);
2136 g_free (exp);
2138 break;
2140 case EDIT_FILE_SYNTAX:
2141 edit_load_syntax_file (edit);
2142 break;
2144 case EDIT_FILE_MENU:
2145 edit_load_menu_file (edit);
2146 break;
2148 default:
2149 break;
2152 edit->force |= REDRAW_COMPLETELY;
2153 return ret;
2156 /* --------------------------------------------------------------------------------------------- */
2158 if mark2 is -1 then marking is from mark1 to the cursor.
2159 Otherwise its between the markers. This handles this.
2160 Returns 1 if no text is marked.
2164 eval_marks (WEdit * edit, long *start_mark, long *end_mark)
2166 if (edit->mark1 != edit->mark2)
2168 long start_bol, start_eol;
2169 long end_bol, end_eol;
2170 long col1, col2;
2171 long diff1, diff2;
2172 long end_mark_curs;
2174 if (edit->end_mark_curs < 0)
2175 end_mark_curs = edit->curs1;
2176 else
2177 end_mark_curs = edit->end_mark_curs;
2179 if (edit->mark2 >= 0)
2181 *start_mark = min (edit->mark1, edit->mark2);
2182 *end_mark = max (edit->mark1, edit->mark2);
2184 else
2186 *start_mark = min (edit->mark1, end_mark_curs);
2187 *end_mark = max (edit->mark1, end_mark_curs);
2188 edit->column2 = edit->curs_col + edit->over_col;
2191 if (edit->column_highlight
2192 && (((edit->mark1 > end_mark_curs) && (edit->column1 < edit->column2))
2193 || ((edit->mark1 < end_mark_curs) && (edit->column1 > edit->column2))))
2195 start_bol = edit_bol (edit, *start_mark);
2196 start_eol = edit_eol (edit, start_bol - 1) + 1;
2197 end_bol = edit_bol (edit, *end_mark);
2198 end_eol = edit_eol (edit, *end_mark);
2199 col1 = min (edit->column1, edit->column2);
2200 col2 = max (edit->column1, edit->column2);
2202 diff1 =
2203 edit_move_forward3 (edit, start_bol, col2, 0) - edit_move_forward3 (edit, start_bol,
2204 col1, 0);
2205 diff2 =
2206 edit_move_forward3 (edit, end_bol, col2, 0) - edit_move_forward3 (edit, end_bol,
2207 col1, 0);
2209 *start_mark -= diff1;
2210 *end_mark += diff2;
2211 *start_mark = max (*start_mark, start_eol);
2212 *end_mark = min (*end_mark, end_eol);
2214 return 0;
2216 else
2218 *start_mark = *end_mark = 0;
2219 edit->column2 = edit->column1 = 0;
2220 return 1;
2224 /* --------------------------------------------------------------------------------------------- */
2226 void
2227 edit_insert_over (WEdit * edit)
2229 int i;
2231 for (i = 0; i < edit->over_col; i++)
2233 edit_insert (edit, ' ');
2235 edit->over_col = 0;
2238 /* --------------------------------------------------------------------------------------------- */
2241 edit_insert_column_of_text_from_file (WEdit * edit, int file,
2242 long *start_pos, long *end_pos, int *col1, int *col2)
2244 long cursor;
2245 int col;
2246 int blocklen = -1, width = 0;
2247 unsigned char *data;
2249 cursor = edit->curs1;
2250 col = edit_get_col (edit);
2251 data = g_malloc0 (TEMP_BUF_LEN);
2253 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
2255 int i;
2256 for (width = 0; width < blocklen; width++)
2258 if (data[width] == '\n')
2259 break;
2261 for (i = 0; i < blocklen; i++)
2263 if (data[i] == '\n')
2264 { /* fill in and move to next line */
2265 int l;
2266 long p;
2267 if (edit_get_byte (edit, edit->curs1) != '\n')
2269 l = width - (edit_get_col (edit) - col);
2270 while (l > 0)
2272 edit_insert (edit, ' ');
2273 l -= space_width;
2276 for (p = edit->curs1;; p++)
2278 if (p == edit->last_byte)
2280 edit_cursor_move (edit, edit->last_byte - edit->curs1);
2281 edit_insert_ahead (edit, '\n');
2282 p++;
2283 break;
2285 if (edit_get_byte (edit, p) == '\n')
2287 p++;
2288 break;
2291 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
2292 l = col - edit_get_col (edit);
2293 while (l >= space_width)
2295 edit_insert (edit, ' ');
2296 l -= space_width;
2298 continue;
2300 edit_insert (edit, data[i]);
2303 *col1 = col;
2304 *col2 = col + width;
2305 *start_pos = cursor;
2306 *end_pos = edit->curs1;
2307 edit_cursor_move (edit, cursor - edit->curs1);
2308 g_free (data);
2310 return blocklen;
2313 /* --------------------------------------------------------------------------------------------- */
2315 void
2316 edit_block_copy_cmd (WEdit * edit)
2318 long start_mark, end_mark, current = edit->curs1;
2319 long col_delta = 0;
2320 long mark1, mark2;
2321 int c1, c2;
2322 int size;
2323 unsigned char *copy_buf;
2325 edit_update_curs_col (edit);
2326 if (eval_marks (edit, &start_mark, &end_mark))
2327 return;
2329 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2331 /* all that gets pushed are deletes hence little space is used on the stack */
2333 edit_push_markers (edit);
2335 if (edit->column_highlight)
2337 col_delta = abs (edit->column2 - edit->column1);
2338 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2340 else
2342 while (size--)
2343 edit_insert_ahead (edit, copy_buf[size]);
2346 g_free (copy_buf);
2347 edit_scroll_screen_over_cursor (edit);
2349 if (edit->column_highlight)
2350 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2351 else if (start_mark < current && end_mark > current)
2352 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2354 edit->force |= REDRAW_PAGE;
2358 /* --------------------------------------------------------------------------------------------- */
2360 void
2361 edit_block_move_cmd (WEdit * edit)
2363 long current;
2364 unsigned char *copy_buf = NULL;
2365 long start_mark, end_mark;
2366 long line;
2368 if (eval_marks (edit, &start_mark, &end_mark))
2369 return;
2371 line = edit->curs_line;
2372 if (edit->mark2 < 0)
2373 edit_mark_cmd (edit, 0);
2374 edit_push_markers (edit);
2376 if (edit->column_highlight)
2378 long mark1, mark2;
2379 int size;
2380 int b_width = 0;
2381 int c1, c2;
2382 int x, x2;
2384 c1 = min (edit->column1, edit->column2);
2385 c2 = max (edit->column1, edit->column2);
2386 b_width = (c2 - c1);
2388 edit_update_curs_col (edit);
2390 x = edit->curs_col;
2391 x2 = x + edit->over_col;
2393 /* do nothing when cursor inside first line of selected area */
2394 if ((edit_eol (edit, edit->curs1) == edit_eol (edit, start_mark)) && (x2 > c1 && x2 <= c2))
2395 return;
2397 if (edit->curs1 > start_mark && edit->curs1 < edit_eol (edit, end_mark))
2399 if (x > c2)
2400 x -= b_width;
2401 else if (x > c1 && x <= c2)
2402 x = c1;
2404 /* save current selection into buffer */
2405 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2407 /* remove current selection */
2408 edit_block_delete_cmd (edit);
2410 edit->over_col = max (0, edit->over_col - b_width);
2411 /* calculate the cursor pos after delete block */
2412 current = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), x, 0);
2413 edit_cursor_move (edit, current - edit->curs1);
2414 edit_scroll_screen_over_cursor (edit);
2416 /* add TWS if need before block insertion */
2417 if (option_cursor_beyond_eol && edit->over_col > 0)
2418 edit_insert_over (edit);
2420 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2421 edit_set_markers (edit, mark1, mark2, c1, c2);
2423 else
2425 long count;
2427 current = edit->curs1;
2428 copy_buf = g_malloc0 (end_mark - start_mark);
2429 edit_cursor_move (edit, start_mark - edit->curs1);
2430 edit_scroll_screen_over_cursor (edit);
2431 count = start_mark;
2432 while (count < end_mark)
2434 copy_buf[end_mark - count - 1] = edit_delete (edit, 1);
2435 count++;
2437 edit_scroll_screen_over_cursor (edit);
2438 edit_cursor_move (edit,
2439 current - edit->curs1 -
2440 (((current - edit->curs1) > 0) ? end_mark - start_mark : 0));
2441 edit_scroll_screen_over_cursor (edit);
2442 while (count-- > start_mark)
2443 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2444 edit_set_markers (edit, edit->curs1, edit->curs1 + end_mark - start_mark, 0, 0);
2447 edit_scroll_screen_over_cursor (edit);
2448 g_free (copy_buf);
2449 edit->force |= REDRAW_PAGE;
2452 /* --------------------------------------------------------------------------------------------- */
2453 /** returns 1 if canceelled by user */
2456 edit_block_delete_cmd (WEdit * edit)
2458 long start_mark, end_mark;
2459 if (eval_marks (edit, &start_mark, &end_mark))
2461 edit_delete_line (edit);
2462 return 0;
2464 return edit_block_delete (edit);
2467 /* --------------------------------------------------------------------------------------------- */
2468 /** call with edit = 0 before shutdown to close memory leaks */
2470 void
2471 edit_replace_cmd (WEdit * edit, int again)
2473 /* 1 = search string, 2 = replace with */
2474 static char *saved1 = NULL; /* saved default[123] */
2475 static char *saved2 = NULL;
2476 char *input1 = NULL; /* user input from the dialog */
2477 char *input2 = NULL;
2478 GString *input2_str = NULL;
2479 char *disp1 = NULL;
2480 char *disp2 = NULL;
2481 long times_replaced = 0;
2482 gboolean once_found = FALSE;
2484 if (!edit)
2486 g_free (saved1), saved1 = NULL;
2487 g_free (saved2), saved2 = NULL;
2488 return;
2491 edit->force |= REDRAW_COMPLETELY;
2493 if (again && !saved1 && !saved2)
2494 again = 0;
2496 if (again)
2498 input1 = g_strdup (saved1 ? saved1 : "");
2499 input2 = g_strdup (saved2 ? saved2 : "");
2501 else
2503 char *tmp_inp1, *tmp_inp2;
2505 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2506 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2508 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2510 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2512 g_free (disp1);
2513 g_free (disp2);
2515 if (input1 == NULL || *input1 == '\0')
2517 edit->force = REDRAW_COMPLETELY;
2518 goto cleanup;
2521 tmp_inp1 = input1;
2522 tmp_inp2 = input2;
2523 input1 = edit_replace_cmd__conv_to_input (input1);
2524 input2 = edit_replace_cmd__conv_to_input (input2);
2525 g_free (tmp_inp1);
2526 g_free (tmp_inp2);
2528 g_free (saved1), saved1 = g_strdup (input1);
2529 g_free (saved2), saved2 = g_strdup (input2);
2531 mc_search_free (edit->search);
2532 edit->search = NULL;
2535 input2_str = g_string_new (input2);
2537 if (!edit->search)
2539 edit->search = mc_search_new (input1, -1);
2540 if (edit->search == NULL)
2542 edit->search_start = edit->curs1;
2543 goto cleanup;
2545 edit->search->search_type = edit_search_options.type;
2546 edit->search->is_all_charsets = edit_search_options.all_codepages;
2547 edit->search->is_case_sensitive = edit_search_options.case_sens;
2548 edit->search->whole_words = edit_search_options.whole_words;
2549 edit->search->search_fn = edit_search_cmd_callback;
2550 edit->search_line_type = edit_get_search_line_type (edit->search);
2551 edit_search_fix_search_start_if_selection (edit);
2554 if (edit->found_len && edit->search_start == edit->found_start + 1
2555 && edit_search_options.backwards)
2556 edit->search_start--;
2558 if (edit->found_len && edit->search_start == edit->found_start - 1
2559 && !edit_search_options.backwards)
2560 edit->search_start++;
2564 gsize len = 0;
2566 if (!editcmd_find (edit, &len))
2568 if (!(edit->search->error == MC_SEARCH_E_OK ||
2569 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2571 edit_error_dialog (_("Search"), edit->search->error_str);
2573 break;
2575 once_found = TRUE;
2577 edit->search_start = edit->search->normal_offset;
2578 /*returns negative on not found or error in pattern */
2580 if ((edit->search_start >= 0) && (edit->search_start < edit->last_byte))
2582 gsize i;
2583 GString *repl_str;
2585 edit->found_start = edit->search_start;
2586 i = edit->found_len = len;
2588 edit_cursor_move (edit, edit->search_start - edit->curs1);
2589 edit_scroll_screen_over_cursor (edit);
2591 if (edit->replace_mode == 0)
2593 int l;
2594 int prompt;
2596 l = edit->curs_row - edit->widget.lines / 3;
2597 if (l > 0)
2598 edit_scroll_downward (edit, l);
2599 if (l < 0)
2600 edit_scroll_upward (edit, -l);
2602 edit_scroll_screen_over_cursor (edit);
2603 edit->force |= REDRAW_PAGE;
2604 edit_render_keypress (edit);
2606 /*so that undo stops at each query */
2607 edit_push_key_press (edit);
2608 /* and prompt 2/3 down */
2609 disp1 = edit_replace_cmd__conv_to_display (saved1);
2610 disp2 = edit_replace_cmd__conv_to_display (saved2);
2611 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2612 g_free (disp1);
2613 g_free (disp2);
2615 if (prompt == B_REPLACE_ALL)
2616 edit->replace_mode = 1;
2617 else if (prompt == B_SKIP_REPLACE)
2619 if (edit_search_options.backwards)
2620 edit->search_start--;
2621 else
2622 edit->search_start++;
2623 continue; /* loop */
2625 else if (prompt == B_CANCEL)
2627 edit->replace_mode = -1;
2628 break; /* loop */
2632 repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2634 if (edit->search->error != MC_SEARCH_E_OK)
2636 edit_error_dialog (_("Replace"), edit->search->error_str);
2637 g_string_free (repl_str, TRUE);
2638 break;
2641 /* delete then insert new */
2642 for (i = 0; i < len; i++)
2643 edit_delete (edit, 1);
2645 for (i = 0; i < repl_str->len; i++)
2646 edit_insert (edit, repl_str->str[i]);
2648 edit->found_len = repl_str->len;
2649 g_string_free (repl_str, TRUE);
2650 times_replaced++;
2652 /* so that we don't find the same string again */
2653 if (edit_search_options.backwards)
2655 edit->search_start--;
2657 else
2659 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2661 if (edit->search_start >= edit->last_byte)
2662 break;
2665 edit_scroll_screen_over_cursor (edit);
2667 else
2669 /* try and find from right here for next search */
2670 edit->search_start = edit->curs1;
2671 edit_update_curs_col (edit);
2673 edit->force |= REDRAW_PAGE;
2674 edit_render_keypress (edit);
2676 if (times_replaced == 0)
2677 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2678 break;
2681 while (edit->replace_mode >= 0);
2683 edit_scroll_screen_over_cursor (edit);
2684 edit->force |= REDRAW_COMPLETELY;
2685 edit_render_keypress (edit);
2687 if ((edit->replace_mode == 1) && (times_replaced != 0))
2688 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2690 cleanup:
2691 g_free (input1);
2692 g_free (input2);
2693 if (input2_str != NULL)
2694 g_string_free (input2_str, TRUE);
2697 /* --------------------------------------------------------------------------------------------- */
2699 mc_search_cbret_t
2700 edit_search_cmd_callback (const void *user_data, gsize char_offset, int *current_char)
2702 *current_char = edit_get_byte ((WEdit *) user_data, (long) char_offset);
2703 return MC_SEARCH_CB_OK;
2706 /* --------------------------------------------------------------------------------------------- */
2708 void
2709 edit_search_cmd (WEdit * edit, gboolean again)
2712 if (edit == NULL)
2713 return;
2715 if (!again)
2716 edit_search (edit);
2717 else if (edit->last_search_string != NULL)
2718 edit_do_search (edit);
2719 else
2721 /* find last search string in history */
2722 GList *history;
2724 history = history_get (MC_HISTORY_SHARED_SEARCH);
2725 if (history != NULL && history->data != NULL)
2727 edit->last_search_string = (char *) history->data;
2728 history->data = NULL;
2729 history = g_list_first (history);
2730 g_list_foreach (history, (GFunc) g_free, NULL);
2731 g_list_free (history);
2733 edit->search = mc_search_new (edit->last_search_string, -1);
2734 if (edit->search == NULL)
2736 /* if not... then ask for an expression */
2737 g_free (edit->last_search_string);
2738 edit->last_search_string = NULL;
2739 edit_search (edit);
2741 else
2743 edit->search->search_type = edit_search_options.type;
2744 edit->search->is_all_charsets = edit_search_options.all_codepages;
2745 edit->search->is_case_sensitive = edit_search_options.case_sens;
2746 edit->search->whole_words = edit_search_options.whole_words;
2747 edit->search->search_fn = edit_search_cmd_callback;
2748 edit->search_line_type = edit_get_search_line_type (edit->search);
2749 edit_do_search (edit);
2752 else
2754 /* if not... then ask for an expression */
2755 g_free (edit->last_search_string);
2756 edit->last_search_string = NULL;
2757 edit_search (edit);
2763 /* --------------------------------------------------------------------------------------------- */
2765 * Check if it's OK to close the editor. If there are unsaved changes,
2766 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2769 gboolean
2770 edit_ok_to_exit (WEdit * edit)
2772 int act;
2774 if (!edit->modified)
2775 return TRUE;
2777 if (!mc_global.midnight_shutdown)
2779 if (!edit_check_newline (edit))
2780 return FALSE;
2782 query_set_sel (2);
2783 act = edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2784 _("&Yes"), _("&No"), _("&Cancel quit"));
2786 else
2788 act =
2789 edit_query_dialog2 (_("Quit"),
2790 _("Midnight Commander is being shut down.\nSave modified file?"),
2791 _("&Yes"), _("&No"));
2793 /* Esc is No */
2794 if (act == -1)
2795 act = 1;
2798 switch (act)
2800 case 0: /* Yes */
2801 edit_push_markers (edit);
2802 edit_set_markers (edit, 0, 0, 0, 0);
2803 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2804 return mc_global.midnight_shutdown;
2805 break;
2806 case 1: /* No */
2807 break;
2808 case 2: /* Cancel quit */
2809 case -1: /* Esc */
2810 return FALSE;
2813 return TRUE;
2816 /* --------------------------------------------------------------------------------------------- */
2817 /** save block, returns 1 on success */
2820 edit_save_block (WEdit * edit, const char *filename, long start, long finish)
2822 int len, file;
2823 vfs_path_t *vpath;
2825 vpath = vfs_path_from_str (filename);
2826 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2827 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2828 vfs_path_free (vpath);
2829 if (file == -1)
2830 return 0;
2832 if (edit->column_highlight)
2834 int r;
2836 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2837 if (r > 0)
2839 unsigned char *block, *p;
2841 p = block = edit_get_block (edit, start, finish, &len);
2842 while (len)
2844 r = mc_write (file, p, len);
2845 if (r < 0)
2846 break;
2847 p += r;
2848 len -= r;
2850 g_free (block);
2853 else
2855 unsigned char *buf;
2856 int i = start, end;
2858 len = finish - start;
2859 buf = g_malloc0 (TEMP_BUF_LEN);
2860 while (start != finish)
2862 end = min (finish, start + TEMP_BUF_LEN);
2863 for (; i < end; i++)
2864 buf[i - start] = edit_get_byte (edit, i);
2865 len -= mc_write (file, (char *) buf, end - start);
2866 start = end;
2868 g_free (buf);
2870 mc_close (file);
2871 if (len)
2872 return 0;
2873 return 1;
2876 /* --------------------------------------------------------------------------------------------- */
2878 void
2879 edit_paste_from_history (WEdit * edit)
2881 (void) edit;
2882 edit_error_dialog (_("Error"), _("This function is not implemented"));
2885 /* --------------------------------------------------------------------------------------------- */
2888 edit_copy_to_X_buf_cmd (WEdit * edit)
2890 long start_mark, end_mark;
2891 if (eval_marks (edit, &start_mark, &end_mark))
2892 return 0;
2893 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2895 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2896 return 1;
2898 /* try use external clipboard utility */
2899 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2901 return 0;
2904 /* --------------------------------------------------------------------------------------------- */
2907 edit_cut_to_X_buf_cmd (WEdit * edit)
2909 long start_mark, end_mark;
2910 if (eval_marks (edit, &start_mark, &end_mark))
2911 return 0;
2912 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2914 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2915 return 1;
2917 /* try use external clipboard utility */
2918 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2920 edit_block_delete_cmd (edit);
2921 edit_mark_cmd (edit, 1);
2922 return 0;
2925 /* --------------------------------------------------------------------------------------------- */
2927 void
2928 edit_paste_from_X_buf_cmd (WEdit * edit)
2930 vfs_path_t *tmp;
2932 /* try use external clipboard utility */
2933 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
2934 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
2935 edit_insert_file (edit, tmp);
2936 vfs_path_free (tmp);
2940 /* --------------------------------------------------------------------------------------------- */
2942 * Ask user for the line and go to that line.
2943 * Negative numbers mean line from the end (i.e. -1 is the last line).
2946 void
2947 edit_goto_cmd (WEdit * edit)
2949 char *f;
2950 static long line = 0; /* line as typed, saved as default */
2951 long l;
2952 char *error;
2953 char s[32];
2955 g_snprintf (s, sizeof (s), "%ld", line);
2956 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "");
2957 if (!f)
2958 return;
2960 if (!*f)
2962 g_free (f);
2963 return;
2966 l = strtol (f, &error, 0);
2967 if (*error)
2969 g_free (f);
2970 return;
2973 line = l;
2974 if (l < 0)
2975 l = edit->total_lines + l + 2;
2976 edit_move_display (edit, l - edit->widget.lines / 2 - 1);
2977 edit_move_to_line (edit, l - 1);
2978 edit->force |= REDRAW_COMPLETELY;
2979 g_free (f);
2983 /* --------------------------------------------------------------------------------------------- */
2984 /** Return 1 on success */
2987 edit_save_block_cmd (WEdit * edit)
2989 long start_mark, end_mark;
2990 char *exp, *tmp;
2992 if (eval_marks (edit, &start_mark, &end_mark))
2993 return 1;
2995 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
2996 exp =
2997 input_expand_dialog (_("Save block"), _("Enter file name:"),
2998 MC_HISTORY_EDIT_SAVE_BLOCK, tmp);
2999 g_free (tmp);
3000 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3001 if (exp)
3003 if (!*exp)
3005 g_free (exp);
3006 return 0;
3008 else
3010 if (edit_save_block (edit, exp, start_mark, end_mark))
3012 g_free (exp);
3013 edit->force |= REDRAW_COMPLETELY;
3014 return 1;
3016 else
3018 g_free (exp);
3019 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3023 edit->force |= REDRAW_COMPLETELY;
3024 return 0;
3028 /* --------------------------------------------------------------------------------------------- */
3030 /** returns TRUE on success */
3031 gboolean
3032 edit_insert_file_cmd (WEdit * edit)
3034 char *tmp;
3035 char *exp;
3036 gboolean ret = FALSE;
3038 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3039 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3040 MC_HISTORY_EDIT_INSERT_FILE, tmp);
3041 g_free (tmp);
3043 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3045 if (exp != NULL && *exp != '\0')
3047 vfs_path_t *exp_vpath;
3049 exp_vpath = vfs_path_from_str (exp);
3050 ret = (edit_insert_file (edit, exp_vpath) >= 0);
3051 vfs_path_free (exp_vpath);
3053 if (!ret)
3054 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3057 g_free (exp);
3059 edit->force |= REDRAW_COMPLETELY;
3060 return ret;
3063 /* --------------------------------------------------------------------------------------------- */
3064 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3067 edit_sort_cmd (WEdit * edit)
3069 static char *old = 0;
3070 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3071 long start_mark, end_mark;
3072 int e;
3074 if (eval_marks (edit, &start_mark, &end_mark))
3076 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3077 return 0;
3080 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3081 edit_save_block (edit, tmp, start_mark, end_mark);
3082 g_free (tmp);
3084 exp = input_dialog (_("Run sort"),
3085 _("Enter sort options (see manpage) separated by whitespace:"),
3086 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "");
3088 if (!exp)
3089 return 1;
3090 g_free (old);
3091 old = exp;
3092 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3093 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3094 tmp =
3095 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3096 " > ", tmp_edit_temp_name, (char *) NULL);
3097 g_free (tmp_edit_temp_name);
3098 g_free (tmp_edit_block_name);
3100 e = system (tmp);
3101 g_free (tmp);
3102 if (e)
3104 if (e == -1 || e == 127)
3106 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3108 else
3110 char q[8];
3111 sprintf (q, "%d ", e);
3112 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3113 edit_error_dialog (_("Sort"), tmp);
3114 g_free (tmp);
3116 return -1;
3119 edit->force |= REDRAW_COMPLETELY;
3121 if (edit_block_delete_cmd (edit))
3122 return 1;
3125 vfs_path_t *tmp_vpath;
3127 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3128 edit_insert_file (edit, tmp_vpath);
3129 vfs_path_free (tmp_vpath);
3131 return 0;
3134 /* --------------------------------------------------------------------------------------------- */
3136 * Ask user for a command, execute it and paste its output back to the
3137 * editor.
3141 edit_ext_cmd (WEdit * edit)
3143 char *exp, *tmp, *tmp_edit_temp_file;
3144 int e;
3146 exp =
3147 input_dialog (_("Paste output of external command"),
3148 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL);
3150 if (!exp)
3151 return 1;
3153 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3154 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3155 g_free (tmp_edit_temp_file);
3156 e = system (tmp);
3157 g_free (tmp);
3158 g_free (exp);
3160 if (e)
3162 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3163 return -1;
3166 edit->force |= REDRAW_COMPLETELY;
3169 vfs_path_t *tmp_vpath;
3171 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3172 edit_insert_file (edit, tmp_vpath);
3173 vfs_path_free (tmp_vpath);
3175 return 0;
3178 /* --------------------------------------------------------------------------------------------- */
3179 /** if block is 1, a block must be highlighted and the shell command
3180 processes it. If block is 0 the shell command is a straight system
3181 command, that just produces some output which is to be inserted */
3183 void
3184 edit_block_process_cmd (WEdit * edit, int macro_number)
3186 char *fname;
3187 char *macros_fname = NULL;
3189 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3190 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3191 user_menu (edit, macros_fname, 0);
3192 g_free (fname);
3193 g_free (macros_fname);
3194 edit->force |= REDRAW_COMPLETELY;
3197 /* --------------------------------------------------------------------------------------------- */
3199 void
3200 edit_mail_dialog (WEdit * edit)
3202 char *tmail_to;
3203 char *tmail_subject;
3204 char *tmail_cc;
3206 static char *mail_cc_last = 0;
3207 static char *mail_subject_last = 0;
3208 static char *mail_to_last = 0;
3210 QuickWidget quick_widgets[] = {
3211 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
3212 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
3213 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input", &tmail_cc),
3214 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT, N_("Copies to")),
3215 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-2",
3216 &tmail_subject),
3217 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT, N_("Subject")),
3218 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-3", &tmail_to),
3219 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT, N_("To")),
3220 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT, N_("mail -s <subject> -c <cc> <to>")),
3221 QUICK_END
3224 QuickDialog Quick_input = {
3225 50, MAIL_DLG_HEIGHT, -1, -1, N_("Mail"),
3226 "[Input Line Keys]", quick_widgets, NULL, FALSE
3229 quick_widgets[2].u.input.text = mail_cc_last ? mail_cc_last : "";
3230 quick_widgets[4].u.input.text = mail_subject_last ? mail_subject_last : "";
3231 quick_widgets[6].u.input.text = mail_to_last ? mail_to_last : "";
3233 if (quick_dialog (&Quick_input) != B_CANCEL)
3235 g_free (mail_cc_last);
3236 g_free (mail_subject_last);
3237 g_free (mail_to_last);
3238 mail_cc_last = tmail_cc;
3239 mail_subject_last = tmail_subject;
3240 mail_to_last = tmail_to;
3241 pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
3246 /*******************/
3247 /* Word Completion */
3248 /*******************/
3250 /* --------------------------------------------------------------------------------------------- */
3252 * Complete current word using regular expression search
3253 * backwards beginning at the current cursor position.
3256 void
3257 edit_complete_word_cmd (WEdit * edit)
3259 gsize i, max_len, word_len = 0, num_compl = 0;
3260 long word_start = 0;
3261 unsigned char *bufpos;
3262 char *match_expr;
3263 struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
3265 /* search start of word to be completed */
3266 if (!edit_find_word_start (edit, &word_start, &word_len))
3267 return;
3269 /* prepare match expression */
3270 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3272 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3273 match_expr =
3274 g_strdup_printf
3275 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3276 (int) word_len, bufpos);
3278 /* collect the possible completions */
3279 /* start search from begin to end of file */
3280 max_len =
3281 edit_collect_completions (edit, word_start, word_len, match_expr,
3282 (struct selection *) &compl, &num_compl);
3284 if (num_compl > 0)
3286 /* insert completed word if there is only one match */
3287 if (num_compl == 1)
3289 for (i = word_len; i < compl[0].len; i++)
3290 edit_insert (edit, *(compl[0].text + i));
3292 /* more than one possible completion => ask the user */
3293 else
3295 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3296 /* !!! pressed again the selection dialog pops up, but that !!! */
3297 /* !!! seems to require a further internal state !!! */
3298 /*tty_beep (); */
3300 /* let the user select the preferred completion */
3301 editcmd_dialog_completion_show (edit, max_len, word_len,
3302 (struct selection *) &compl, num_compl);
3306 g_free (match_expr);
3307 /* release memory before return */
3308 for (i = 0; i < num_compl; i++)
3309 g_free (compl[i].text);
3312 /* --------------------------------------------------------------------------------------------- */
3314 void
3315 edit_select_codepage_cmd (WEdit * edit)
3317 #ifdef HAVE_CHARSET
3318 if (do_select_codepage ())
3319 edit_set_codeset (edit);
3321 edit->force = REDRAW_COMPLETELY;
3322 edit_refresh_cmd (edit);
3323 #else
3324 (void) edit;
3325 #endif
3328 /* --------------------------------------------------------------------------------------------- */
3330 void
3331 edit_insert_literal_cmd (WEdit * edit)
3333 int char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3334 _("Press any key:"), 0);
3335 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3338 /* --------------------------------------------------------------------------------------------- */
3340 void
3341 edit_begin_end_macro_cmd (WEdit * edit)
3343 /* edit is a pointer to the widget */
3344 if (edit != NULL)
3346 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3347 edit_execute_key_command (edit, command, -1);
3351 /* --------------------------------------------------------------------------------------------- */
3353 void
3354 edit_begin_end_repeat_cmd (WEdit * edit)
3356 /* edit is a pointer to the widget */
3357 if (edit != NULL)
3359 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3360 edit_execute_key_command (edit, command, -1);
3364 /* --------------------------------------------------------------------------------------------- */
3366 gboolean
3367 edit_load_forward_cmd (WEdit * edit)
3369 if (edit->modified
3370 && edit_query_dialog2 (_("Warning"),
3371 _("Current text was modified without a file save.\n"
3372 "Continue discards these changes"), _("C&ontinue"),
3373 _("&Cancel")) == 1)
3375 edit->force |= REDRAW_COMPLETELY;
3376 return TRUE;
3379 if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
3380 return FALSE;
3382 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3383 return FALSE;
3385 edit_stack_iterator++;
3386 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3387 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3388 edit_history_moveto[edit_stack_iterator].line);
3390 return FALSE;
3393 /* --------------------------------------------------------------------------------------------- */
3395 gboolean
3396 edit_load_back_cmd (WEdit * edit)
3398 if (edit->modified
3399 && edit_query_dialog2 (_("Warning"),
3400 _("Current text was modified without a file save.\n"
3401 "Continue discards these changes"), _("C&ontinue"),
3402 _("&Cancel")) == 1)
3404 edit->force |= REDRAW_COMPLETELY;
3405 return TRUE;
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 /* --------------------------------------------------------------------------------------------- */