Ticket #2846: code cleanup before 4.8.1.4 release.
[midnight-commander.git] / src / editor / editcmd.c
blob6482713f8f6d843efb673c5735565f4b05d7fed3
1 /*
2 Editor high level editing commands
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer, 1996, 1997
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /** \file
28 * \brief Source: editor high level editing commands
29 * \author Paul Sheer
30 * \date 1996, 1997
33 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
35 #include <config.h>
37 #ifdef HAVE_ASSERT_H
38 #include <assert.h>
39 #endif
40 #include <ctype.h>
42 #include <stdio.h>
43 #include <stdarg.h>
44 #include <sys/types.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include <errno.h>
48 #include <sys/stat.h>
49 #include <stdlib.h>
50 #include <fcntl.h>
52 #include "lib/global.h"
53 #include "lib/tty/tty.h"
54 #include "lib/tty/key.h" /* XCTRL */
55 #include "lib/mcconfig.h"
56 #include "lib/skin.h"
57 #include "lib/strutil.h" /* utf string functions */
58 #include "lib/lock.h"
59 #include "lib/util.h" /* tilde_expand() */
60 #include "lib/vfs/vfs.h"
61 #include "lib/widget.h"
62 #include "lib/event.h" /* mc_event_raise() */
63 #ifdef HAVE_CHARSET
64 #include "lib/charsets.h"
65 #endif
67 #include "src/history.h"
68 #include "src/setup.h" /* option_tab_spacing */
69 #include "src/main.h" /* mactos_t */
70 #ifdef HAVE_CHARSET
71 #include "src/selcodepage.h"
72 #endif
73 #include "src/keybind-defaults.h"
74 #include "src/util.h" /* check_for_default() */
75 #include "src/filemanager/layout.h" /* mc_refresh() */
77 #include "edit-impl.h"
78 #include "editwidget.h"
79 #include "editcmd_dialogs.h"
80 #include "etags.h"
82 /*** global variables ****************************************************************************/
84 /* search and replace: */
85 int search_create_bookmark = FALSE;
87 /* queries on a save */
88 int edit_confirm_save = 1;
90 /*** file scope macro definitions ****************************************************************/
92 #define space_width 1
94 #define TEMP_BUF_LEN 1024
96 #define INPUT_INDEX 9
98 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
99 (and the above) routines to work properly - paul */
101 #define is_digit(x) ((x) >= '0' && (x) <= '9')
103 #define MAIL_DLG_HEIGHT 12
105 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
107 /*** file scope type declarations ****************************************************************/
109 /*** file scope variables ************************************************************************/
111 /*** file scope functions ************************************************************************/
112 /* --------------------------------------------------------------------------------------------- */
114 /* If 0 (quick save) then a) create/truncate <filename> file,
115 b) save to <filename>;
116 if 1 (safe save) then a) save to <tempnam>,
117 b) rename <tempnam> to <filename>;
118 if 2 (do backups) then a) save to <tempnam>,
119 b) rename <filename> to <filename.backup_ext>,
120 c) rename <tempnam> to <filename>. */
122 /* returns 0 on error, -1 on abort */
124 static int
125 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
127 char *p;
128 gchar *tmp;
129 long filelen = 0;
130 int this_save_mode, fd = -1;
131 vfs_path_t *real_filename_vpath;
132 vfs_path_t *savename_vpath = NULL;
133 const char *start_filename;
134 const vfs_path_element_t *vpath_element;
136 vpath_element = vfs_path_get_by_index (filename_vpath, 0);
137 if (vpath_element == NULL)
138 return 0;
140 start_filename = vpath_element->path;
141 if (*start_filename == '\0')
142 return 0;
144 if (*start_filename != PATH_SEP && edit->dir_vpath != NULL)
146 real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
148 else
150 real_filename_vpath = vfs_path_clone (filename_vpath);
153 this_save_mode = option_save_mode;
154 if (this_save_mode != EDIT_QUICK_SAVE)
156 if (!vfs_file_is_local (real_filename_vpath)
157 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
160 * The file does not exists yet, so no safe save or
161 * backup are necessary.
163 this_save_mode = EDIT_QUICK_SAVE;
165 if (fd != -1)
166 mc_close (fd);
169 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt)
171 int rv;
172 struct stat sb;
174 rv = mc_stat (real_filename_vpath, &sb);
175 if (rv == 0 && sb.st_nlink > 1)
177 rv = edit_query_dialog3 (_("Warning"),
178 _("File has hard-links. Detach before saving?"),
179 _("&Yes"), _("&No"), _("&Cancel"));
180 switch (rv)
182 case 0:
183 this_save_mode = EDIT_SAFE_SAVE;
184 /* fallthrough */
185 case 1:
186 edit->skip_detach_prompt = 1;
187 break;
188 default:
189 vfs_path_free (real_filename_vpath);
190 return -1;
194 /* Prevent overwriting changes from other editor sessions. */
195 if (rv == 0 && edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
197 /* The default action is "Cancel". */
198 query_set_sel (1);
200 rv = edit_query_dialog2 (_("Warning"),
201 _("The file has been modified in the meantime. Save anyway?"),
202 _("&Yes"), _("&Cancel"));
203 if (rv != 0)
205 vfs_path_free (real_filename_vpath);
206 return -1;
211 if (this_save_mode != EDIT_QUICK_SAVE)
213 char *savedir, *saveprefix;
215 savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
216 if (savedir == NULL)
217 savedir = g_strdup (".");
219 saveprefix = mc_build_filename (savedir, "cooledit", NULL);
220 g_free (savedir);
221 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
222 g_free (saveprefix);
223 if (savename_vpath == NULL)
225 vfs_path_free (real_filename_vpath);
226 return 0;
228 /* FIXME:
229 * Close for now because mc_mkstemps use pure open system call
230 * to create temporary file and it needs to be reopened by
231 * VFS-aware mc_open().
233 close (fd);
235 else
236 savename_vpath = vfs_path_clone (real_filename_vpath);
238 (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
239 (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
241 fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
242 if (fd == -1)
243 goto error_save;
245 /* pipe save */
246 p = edit_get_write_filter (savename_vpath, real_filename_vpath);
247 if (p != NULL)
249 FILE *file;
251 mc_close (fd);
252 file = (FILE *) popen (p, "w");
254 if (file)
256 filelen = edit_write_stream (edit, file);
257 #if 1
258 pclose (file);
259 #else
260 if (pclose (file) != 0)
262 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
263 edit_error_dialog (_("Error"), tmp);
264 g_free (tmp);
265 g_free (p);
266 goto error_save;
268 #endif
270 else
272 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
273 edit_error_dialog (_("Error"), get_sys_error (tmp));
274 g_free (p);
275 g_free (tmp);
276 goto error_save;
278 g_free (p);
280 else if (edit->lb == LB_ASIS)
281 { /* do not change line breaks */
282 long buf;
283 buf = 0;
284 filelen = edit->last_byte;
285 while (buf <= (edit->curs1 >> S_EDIT_BUF_SIZE) - 1)
287 if (mc_write (fd, (char *) edit->buffers1[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
289 mc_close (fd);
290 goto error_save;
292 buf++;
294 if (mc_write
295 (fd, (char *) edit->buffers1[buf],
296 edit->curs1 & M_EDIT_BUF_SIZE) != (edit->curs1 & M_EDIT_BUF_SIZE))
298 filelen = -1;
300 else if (edit->curs2)
302 edit->curs2--;
303 buf = (edit->curs2 >> S_EDIT_BUF_SIZE);
304 if (mc_write
305 (fd,
306 (char *) edit->buffers2[buf] + EDIT_BUF_SIZE -
307 (edit->curs2 & M_EDIT_BUF_SIZE) - 1,
308 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) != 1 + (edit->curs2 & M_EDIT_BUF_SIZE))
310 filelen = -1;
312 else
314 while (--buf >= 0)
316 if (mc_write (fd, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
318 filelen = -1;
319 break;
323 edit->curs2++;
325 if (mc_close (fd))
326 goto error_save;
328 /* Update the file information, especially the mtime. */
329 if (mc_stat (savename_vpath, &edit->stat1) == -1)
330 goto error_save;
332 else
333 { /* change line breaks */
334 FILE *file;
335 const vfs_path_element_t *path_element;
337 mc_close (fd);
339 path_element = vfs_path_get_by_index (savename_vpath, -1);
340 file = (FILE *) fopen (path_element->path, "w");
341 if (file != NULL)
343 filelen = edit_write_stream (edit, file);
344 fclose (file);
346 else
348 char *msg;
350 msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
351 edit_error_dialog (_("Error"), msg);
352 g_free (msg);
353 goto error_save;
357 if (filelen != edit->last_byte)
358 goto error_save;
360 if (this_save_mode == EDIT_DO_BACKUP)
362 vfs_path_t *tmp_vpath;
363 gboolean ok;
365 #ifdef HAVE_ASSERT_H
366 assert (option_backup_ext != NULL);
367 #endif
368 tmp_vpath = vfs_path_append_new (real_filename_vpath, option_backup_ext, (char *) NULL);
369 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
370 vfs_path_free (tmp_vpath);
371 if (!ok)
372 goto error_save;
375 if (this_save_mode != EDIT_QUICK_SAVE)
376 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
377 goto error_save;
379 vfs_path_free (real_filename_vpath);
380 vfs_path_free (savename_vpath);
381 return 1;
382 error_save:
383 /* FIXME: Is this safe ?
384 * if (this_save_mode != EDIT_QUICK_SAVE)
385 * mc_unlink (savename);
387 vfs_path_free (real_filename_vpath);
388 vfs_path_free (savename_vpath);
389 return 0;
392 /* --------------------------------------------------------------------------------------------- */
394 static gboolean
395 edit_check_newline (WEdit * edit)
397 return !(option_check_nl_at_eof && edit->last_byte > 0
398 && edit_get_byte (edit, edit->last_byte - 1) != '\n'
399 && edit_query_dialog2 (_("Warning"),
400 _("The file you are saving is not finished with a newline"),
401 _("C&ontinue"), _("&Cancel")));
404 /* --------------------------------------------------------------------------------------------- */
406 static vfs_path_t *
407 edit_get_save_file_as (WEdit * edit)
409 #define DLG_WIDTH 64
410 #define DLG_HEIGHT 14
412 static LineBreaks cur_lb = LB_ASIS;
414 char *filename = vfs_path_to_str (edit->filename_vpath);
415 char *filename_res;
417 const char *lb_names[LB_NAMES] = {
418 N_("&Do not change"),
419 N_("&Unix format (LF)"),
420 N_("&Windows/DOS format (CR LF)"),
421 N_("&Macintosh format (CR)")
424 QuickWidget quick_widgets[] = {
425 QUICK_BUTTON (6, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
426 QUICK_BUTTON (2, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
427 QUICK_RADIO (5, DLG_WIDTH, DLG_HEIGHT - 8, DLG_HEIGHT, LB_NAMES, lb_names, (int *) &cur_lb),
428 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 9, DLG_HEIGHT, N_("Change line breaks to:")),
429 QUICK_INPUT (3, DLG_WIDTH, DLG_HEIGHT - 11, DLG_HEIGHT, filename, DLG_WIDTH - 6, 0,
430 "save-as", &filename_res),
431 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 12, DLG_HEIGHT, N_("Enter file name:")),
432 QUICK_END
435 QuickDialog Quick_options = {
436 DLG_WIDTH, DLG_HEIGHT, -1, -1,
437 N_("Save As"), "[Save File As]",
438 quick_widgets, NULL, FALSE
441 if (quick_dialog (&Quick_options) != B_CANCEL)
443 char *fname;
444 vfs_path_t *ret_vpath;
446 edit->lb = cur_lb;
447 fname = tilde_expand (filename_res);
448 g_free (filename_res);
449 ret_vpath = vfs_path_from_str (fname);
450 g_free (fname);
451 return ret_vpath;
453 g_free (filename);
455 return NULL;
457 #undef DLG_WIDTH
458 #undef DLG_HEIGHT
461 /* --------------------------------------------------------------------------------------------- */
463 /** returns 1 on success */
465 static int
466 edit_save_cmd (WEdit * edit)
468 int res, save_lock = 0;
470 if (!edit->locked && !edit->delete_file)
471 save_lock = lock_file (edit->filename_vpath);
472 res = edit_save_file (edit, edit->filename_vpath);
474 /* Maintain modify (not save) lock on failure */
475 if ((res > 0 && edit->locked) || save_lock)
476 edit->locked = unlock_file (edit->filename_vpath);
478 /* On failure try 'save as', it does locking on its own */
479 if (!res)
480 return edit_save_as_cmd (edit);
481 edit->force |= REDRAW_COMPLETELY;
482 if (res > 0)
484 edit->delete_file = 0;
485 edit->modified = 0;
488 return 1;
491 /* --------------------------------------------------------------------------------------------- */
492 /** returns 1 on error */
494 static int
495 edit_load_file_from_filename (WEdit * edit, const vfs_path_t * exp_vpath)
497 int prev_locked = edit->locked;
498 vfs_path_t *prev_filename;
499 int ret = 0;
501 prev_filename = vfs_path_clone (edit->filename_vpath);
502 if (!edit_reload (edit, exp_vpath))
503 ret = 1;
504 else if (prev_locked)
505 unlock_file (prev_filename);
507 vfs_path_free (prev_filename);
508 return ret;
511 /* --------------------------------------------------------------------------------------------- */
513 static void
514 edit_load_syntax_file (WEdit * edit)
516 vfs_path_t *extdir_vpath;
517 int dir = 0;
519 if (geteuid () == 0)
521 dir = query_dialog (_("Syntax file edit"),
522 _("Which syntax file you want to edit?"), D_NORMAL, 2,
523 _("&User"), _("&System Wide"));
526 extdir_vpath =
527 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
528 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
530 vfs_path_free (extdir_vpath);
531 extdir_vpath =
532 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
535 if (dir == 0)
537 vfs_path_t *user_syntax_file_vpath;
539 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
540 check_for_default (extdir_vpath, user_syntax_file_vpath);
541 edit_load_file_from_filename (edit, user_syntax_file_vpath);
542 vfs_path_free (user_syntax_file_vpath);
544 else if (dir == 1)
545 edit_load_file_from_filename (edit, extdir_vpath);
547 vfs_path_free (extdir_vpath);
550 /* --------------------------------------------------------------------------------------------- */
552 static void
553 edit_load_menu_file (WEdit * edit)
555 vfs_path_t *buffer_vpath;
556 vfs_path_t *menufile_vpath;
557 int dir = 0;
559 dir = query_dialog (_("Menu edit"),
560 _("Which menu file do you want to edit?"), D_NORMAL,
561 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
563 menufile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
565 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
567 vfs_path_free (menufile_vpath);
568 menufile_vpath = vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
571 switch (dir)
573 case 0:
574 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
575 check_for_default (menufile_vpath, buffer_vpath);
576 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
577 break;
579 case 1:
580 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
581 check_for_default (menufile_vpath, buffer_vpath);
582 break;
584 case 2:
585 buffer_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
586 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
588 vfs_path_free (buffer_vpath);
589 buffer_vpath =
590 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
592 break;
594 default:
595 vfs_path_free (menufile_vpath);
596 return;
599 edit_load_file_from_filename (edit, buffer_vpath);
601 vfs_path_free (buffer_vpath);
602 vfs_path_free (menufile_vpath);
605 /* --------------------------------------------------------------------------------------------- */
607 static void
608 edit_delete_column_of_text (WEdit * edit)
610 long p, q, r, m1, m2;
611 long b, c, d, n;
613 eval_marks (edit, &m1, &m2);
614 n = edit_move_forward (edit, m1, 0, m2) + 1;
615 c = edit_move_forward3 (edit, edit_bol (edit, m1), 0, m1);
616 d = edit_move_forward3 (edit, edit_bol (edit, m2), 0, m2);
617 b = max (min (c, d), min (edit->column1, edit->column2));
618 c = max (c, max (edit->column1, edit->column2));
620 while (n--)
622 r = edit_bol (edit, edit->curs1);
623 p = edit_move_forward3 (edit, r, b, 0);
624 q = edit_move_forward3 (edit, r, c, 0);
625 if (p < m1)
626 p = m1;
627 if (q > m2)
628 q = m2;
629 edit_cursor_move (edit, p - edit->curs1);
630 while (q > p)
632 /* delete line between margins */
633 if (edit_get_byte (edit, edit->curs1) != '\n')
634 edit_delete (edit, 1);
635 q--;
637 if (n)
638 /* move to next line except on the last delete */
639 edit_cursor_move (edit, edit_move_forward (edit, edit->curs1, 1, 0) - edit->curs1);
643 /* --------------------------------------------------------------------------------------------- */
644 /** if success return 0 */
646 static int
647 edit_block_delete (WEdit * edit)
649 long count;
650 long start_mark, end_mark;
651 int curs_pos, line_width;
652 long curs_line, c1, c2;
654 if (eval_marks (edit, &start_mark, &end_mark))
655 return 0;
656 if (edit->column_highlight && edit->mark2 < 0)
657 edit_mark_cmd (edit, 0);
658 if ((end_mark - start_mark) > option_max_undo / 2)
660 /* Warning message with a query to continue or cancel the operation */
661 if (edit_query_dialog2
662 (_("Warning"),
664 ("Block is large, you may not be able to undo this action"),
665 _("C&ontinue"), _("&Cancel")))
667 return 1;
670 c1 = min (edit->column1, edit->column2);
671 c2 = max (edit->column1, edit->column2);
672 edit->column1 = c1;
673 edit->column2 = c2;
675 edit_push_markers (edit);
677 curs_line = edit->curs_line;
679 /* calculate line width and cursor position before cut */
680 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
681 edit_eol (edit, edit->curs1));
682 curs_pos = edit->curs_col + edit->over_col;
684 /* move cursor to start of selection */
685 edit_cursor_move (edit, start_mark - edit->curs1);
686 edit_scroll_screen_over_cursor (edit);
687 count = start_mark;
688 if (start_mark < end_mark)
690 if (edit->column_highlight)
692 if (edit->mark2 < 0)
693 edit_mark_cmd (edit, 0);
694 edit_delete_column_of_text (edit);
695 /* move cursor to the saved position */
696 edit_move_to_line (edit, curs_line);
697 /* calculate line width after cut */
698 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
699 edit_eol (edit, edit->curs1));
700 if (option_cursor_beyond_eol && curs_pos > line_width)
701 edit->over_col = curs_pos - line_width;
703 else
705 while (count < end_mark)
707 edit_delete (edit, 1);
708 count++;
712 edit_set_markers (edit, 0, 0, 0, 0);
713 edit->force |= REDRAW_PAGE;
714 return 0;
717 /* --------------------------------------------------------------------------------------------- */
719 * Get EOL symbol for searching.
721 * @param edit editor object
722 * @return EOL symbol
725 static inline char
726 edit_search_get_current_end_line_char (const WEdit * edit)
728 switch (edit->lb)
730 case LB_MAC:
731 return '\r';
732 default:
733 return '\n';
737 /* --------------------------------------------------------------------------------------------- */
739 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
741 * @param search search object
742 * @return result of checks.
745 static edit_search_line_t
746 edit_get_search_line_type (mc_search_t * search)
748 edit_search_line_t search_line_type = 0;
750 if (search->search_type != MC_SEARCH_T_REGEX)
751 return search_line_type;
753 if (*search->original == '^')
754 search_line_type |= AT_START_LINE;
756 if (search->original[search->original_len - 1] == '$')
757 search_line_type |= AT_END_LINE;
758 return search_line_type;
761 /* --------------------------------------------------------------------------------------------- */
763 * Calculating the start position of next line.
765 * @param edit editor object
766 * @param current_pos current position
767 * @param max_pos max position
768 * @param end_string_symbol end of line symbol
769 * @return start position of next line
772 static off_t
773 edit_calculate_start_of_next_line (WEdit * edit, off_t current_pos, off_t max_pos,
774 char end_string_symbol)
776 off_t i;
778 for (i = current_pos; i < max_pos; i++)
780 current_pos++;
781 if (edit_get_byte (edit, i) == end_string_symbol)
782 break;
785 return current_pos;
788 /* --------------------------------------------------------------------------------------------- */
790 * Calculating the end position of previous line.
792 * @param edit editor object
793 * @param current_pos current position
794 * @param end_string_symbol end of line symbol
795 * @return end position of previous line
798 static off_t
799 edit_calculate_end_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
801 off_t i;
803 for (i = current_pos - 1; i >= 0; i--)
804 if (edit_get_byte (edit, i) == end_string_symbol)
805 break;
807 return i;
810 /* --------------------------------------------------------------------------------------------- */
812 * Calculating the start position of previous line.
814 * @param edit editor object
815 * @param current_pos current position
816 * @param end_string_symbol end of line symbol
817 * @return start position of previous line
820 static inline off_t
821 edit_calculate_start_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
823 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
824 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
826 return (current_pos + 1);
829 /* --------------------------------------------------------------------------------------------- */
831 * Calculating the start position of current line.
833 * @param edit editor object
834 * @param current_pos current position
835 * @param end_string_symbol end of line symbol
836 * @return start position of current line
839 static inline off_t
840 edit_calculate_start_of_current_line (WEdit * edit, off_t current_pos, char end_string_symbol)
842 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
844 return (current_pos + 1);
847 /* --------------------------------------------------------------------------------------------- */
849 * Fixing (if needed) search start position if 'only in selection' option present.
851 * @param edit editor object
854 static void
855 edit_search_fix_search_start_if_selection (WEdit * edit)
857 long start_mark = 0;
858 long end_mark = 0;
860 if (!edit_search_options.only_in_selection)
861 return;
863 if (eval_marks (edit, &start_mark, &end_mark) != 0)
864 return;
866 if (edit_search_options.backwards)
868 if (edit->search_start > end_mark || edit->search_start <= start_mark)
869 edit->search_start = end_mark;
871 else
873 if (edit->search_start < start_mark || edit->search_start >= end_mark)
874 edit->search_start = start_mark;
878 /* --------------------------------------------------------------------------------------------- */
880 static gboolean
881 editcmd_find (WEdit * edit, gsize * len)
883 off_t search_start = edit->search_start;
884 off_t search_end;
885 long start_mark = 0;
886 long end_mark = edit->last_byte;
887 int mark_res = 0;
888 char end_string_symbol;
890 end_string_symbol = edit_search_get_current_end_line_char (edit);
892 /* prepare for search */
893 if (edit_search_options.only_in_selection)
895 mark_res = eval_marks (edit, &start_mark, &end_mark);
896 if (mark_res != 0)
898 edit->search->error = MC_SEARCH_E_NOTFOUND;
899 edit->search->error_str = g_strdup (_("Search string not found"));
900 return FALSE;
903 /* fix the start and the end of search block positions */
904 if ((edit->search_line_type & AT_START_LINE) != 0
905 && (start_mark != 0 || edit_get_byte (edit, start_mark - 1) != end_string_symbol))
907 start_mark =
908 edit_calculate_start_of_next_line (edit, start_mark, edit->last_byte,
909 end_string_symbol);
911 if ((edit->search_line_type & AT_END_LINE) != 0
912 && (end_mark - 1 != edit->last_byte
913 || edit_get_byte (edit, end_mark) != end_string_symbol))
915 end_mark = edit_calculate_end_of_previous_line (edit, end_mark, end_string_symbol);
917 if (start_mark >= end_mark)
919 edit->search->error = MC_SEARCH_E_NOTFOUND;
920 edit->search->error_str = g_strdup (_("Search string not found"));
921 return FALSE;
924 else
926 if (edit_search_options.backwards)
927 end_mark = max (1, edit->curs1) - 1;
930 /* search */
931 if (edit_search_options.backwards)
933 /* backward search */
934 search_end = end_mark;
936 if ((edit->search_line_type & AT_START_LINE) != 0)
937 search_start =
938 edit_calculate_start_of_current_line (edit, search_start, end_string_symbol);
940 while ((int) search_start >= start_mark)
942 if (search_end > (off_t) (search_start + edit->search->original_len)
943 && mc_search_is_fixed_search_str (edit->search))
945 search_end = search_start + edit->search->original_len;
947 if (mc_search_run (edit->search, (void *) edit, search_start, search_end, len)
948 && edit->search->normal_offset == search_start)
950 return TRUE;
954 if ((edit->search_line_type & AT_START_LINE) != 0)
955 search_start =
956 edit_calculate_start_of_previous_line (edit, search_start, end_string_symbol);
957 else
958 search_start--;
960 edit->search->error_str = g_strdup (_("Search string not found"));
962 else
964 /* forward search */
965 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
966 search_start =
967 edit_calculate_start_of_next_line (edit, search_start, end_mark, end_string_symbol);
968 return mc_search_run (edit->search, (void *) edit, search_start, end_mark, len);
970 return FALSE;
973 /* --------------------------------------------------------------------------------------------- */
975 static char *
976 edit_replace_cmd__conv_to_display (char *str)
978 #ifdef HAVE_CHARSET
979 GString *tmp;
981 tmp = str_convert_to_display (str);
982 if (tmp != NULL)
984 if (tmp->len != 0)
985 return g_string_free (tmp, FALSE);
986 g_string_free (tmp, TRUE);
988 #endif
989 return g_strdup (str);
992 /* --------------------------------------------------------------------------------------------- */
994 static char *
995 edit_replace_cmd__conv_to_input (char *str)
997 #ifdef HAVE_CHARSET
998 GString *tmp;
1000 tmp = str_convert_to_input (str);
1001 if (tmp != NULL)
1003 if (tmp->len != 0)
1004 return g_string_free (tmp, FALSE);
1005 g_string_free (tmp, TRUE);
1007 #endif
1008 return g_strdup (str);
1011 /* --------------------------------------------------------------------------------------------- */
1013 static void
1014 edit_do_search (WEdit * edit)
1016 gsize len = 0;
1018 if (edit->search == NULL)
1019 edit->search_start = edit->curs1;
1021 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1023 if (search_create_bookmark)
1025 int found = 0, books = 0;
1026 long l = 0, l_last = -1;
1027 long q = 0;
1029 search_create_bookmark = FALSE;
1030 book_mark_flush (edit, -1);
1032 while (TRUE)
1034 if (!mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
1035 break;
1036 if (found == 0)
1037 edit->search_start = edit->search->normal_offset;
1038 found++;
1039 l += edit_count_lines (edit, q, edit->search->normal_offset);
1040 if (l != l_last)
1042 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
1043 books++;
1045 l_last = l;
1046 q = edit->search->normal_offset + 1;
1049 if (found == 0)
1050 edit_error_dialog (_("Search"), _("Search string not found"));
1051 else
1052 edit_cursor_move (edit, edit->search_start - edit->curs1);
1054 else
1056 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
1057 && edit_search_options.backwards)
1058 edit->search_start--;
1060 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
1061 && !edit_search_options.backwards)
1062 edit->search_start++;
1064 if (editcmd_find (edit, &len))
1066 edit->found_start = edit->search_start = edit->search->normal_offset;
1067 edit->found_len = len;
1068 edit->over_col = 0;
1069 edit_cursor_move (edit, edit->search_start - edit->curs1);
1070 edit_scroll_screen_over_cursor (edit);
1071 if (edit_search_options.backwards)
1072 edit->search_start--;
1073 else
1074 edit->search_start++;
1076 else
1078 edit->search_start = edit->curs1;
1079 if (edit->search->error_str != NULL)
1080 edit_error_dialog (_("Search"), edit->search->error_str);
1084 edit->force |= REDRAW_COMPLETELY;
1085 edit_scroll_screen_over_cursor (edit);
1088 /* --------------------------------------------------------------------------------------------- */
1090 static void
1091 edit_search (WEdit * edit)
1093 if (editcmd_dialog_search_show (edit))
1095 edit->search_line_type = edit_get_search_line_type (edit->search);
1096 edit_search_fix_search_start_if_selection (edit);
1097 edit_do_search (edit);
1101 /* --------------------------------------------------------------------------------------------- */
1102 /** Return a null terminated length of text. Result must be g_free'd */
1104 static unsigned char *
1105 edit_get_block (WEdit * edit, long start, long finish, int *l)
1107 unsigned char *s, *r;
1108 r = s = g_malloc0 (finish - start + 1);
1109 if (edit->column_highlight)
1111 *l = 0;
1112 /* copy from buffer, excluding chars that are out of the column 'margins' */
1113 while (start < finish)
1115 int c;
1116 long x;
1117 x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
1118 c = edit_get_byte (edit, start);
1119 if ((x >= edit->column1 && x < edit->column2)
1120 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1122 *s++ = c;
1123 (*l)++;
1125 start++;
1128 else
1130 *l = finish - start;
1131 while (start < finish)
1132 *s++ = edit_get_byte (edit, start++);
1134 *s = 0;
1135 return r;
1138 /* --------------------------------------------------------------------------------------------- */
1139 /** copies a block to clipboard file */
1141 static int
1142 edit_save_block_to_clip_file (WEdit * edit, long start, long finish)
1144 int ret;
1145 gchar *tmp;
1146 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1147 ret = edit_save_block (edit, tmp, start, finish);
1148 g_free (tmp);
1149 return ret;
1152 /* --------------------------------------------------------------------------------------------- */
1154 static void
1155 pipe_mail (WEdit * edit, char *to, char *subject, char *cc)
1157 FILE *p = 0;
1158 char *s;
1160 to = name_quote (to, 0);
1161 subject = name_quote (subject, 0);
1162 cc = name_quote (cc, 0);
1163 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1164 g_free (to);
1165 g_free (subject);
1166 g_free (cc);
1168 if (s)
1170 p = popen (s, "w");
1171 g_free (s);
1174 if (p)
1176 long i;
1177 for (i = 0; i < edit->last_byte; i++)
1178 fputc (edit_get_byte (edit, i), p);
1179 pclose (p);
1183 /* --------------------------------------------------------------------------------------------- */
1185 static gboolean
1186 is_break_char (char c)
1188 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
1191 /* --------------------------------------------------------------------------------------------- */
1192 /** find first character of current word */
1194 static int
1195 edit_find_word_start (WEdit * edit, long *word_start, gsize * word_len)
1197 int c, last;
1198 gsize i;
1200 /* return if at begin of file */
1201 if (edit->curs1 <= 0)
1202 return 0;
1204 c = (unsigned char) edit_get_byte (edit, edit->curs1 - 1);
1205 /* return if not at end or in word */
1206 if (is_break_char (c))
1207 return 0;
1209 /* search start of word to be completed */
1210 for (i = 2;; i++)
1212 /* return if at begin of file */
1213 if ((gsize) edit->curs1 < i)
1214 return 0;
1216 last = c;
1217 c = (unsigned char) edit_get_byte (edit, edit->curs1 - i);
1219 if (is_break_char (c))
1221 /* return if word starts with digit */
1222 if (isdigit (last))
1223 return 0;
1225 *word_start = edit->curs1 - (i - 1); /* start found */
1226 *word_len = i - 1;
1227 break;
1230 /* success */
1231 return 1;
1234 /* --------------------------------------------------------------------------------------------- */
1236 * Get current word under cursor
1238 * @param edit editor object
1239 * @param srch mc_search object
1240 * @param word_start start word position
1242 * @return newly allocated string or NULL if no any words under cursor
1245 static char *
1246 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, long word_start)
1248 gsize len = 0, i;
1249 GString *temp;
1251 if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len))
1252 return NULL;
1254 temp = g_string_sized_new (len);
1256 for (i = 0; i < len; i++)
1258 int chr;
1260 chr = edit_get_byte (edit, word_start + i);
1261 if (!isspace (chr))
1262 g_string_append_c (temp, chr);
1265 return g_string_free (temp, temp->len == 0);
1268 /* --------------------------------------------------------------------------------------------- */
1269 /** collect the possible completions */
1271 static gsize
1272 edit_collect_completions (WEdit * edit, long word_start, gsize word_len,
1273 char *match_expr, struct selection *compl, gsize * num)
1275 gsize len = 0;
1276 gsize max_len = 0;
1277 gsize i;
1278 int skip;
1279 GString *temp;
1280 mc_search_t *srch;
1281 long last_byte, start = -1;
1282 char *current_word;
1284 srch = mc_search_new (match_expr, -1);
1285 if (srch == NULL)
1286 return 0;
1288 if (mc_config_get_bool
1289 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1291 last_byte = edit->last_byte;
1293 else
1295 last_byte = word_start;
1298 srch->search_type = MC_SEARCH_T_REGEX;
1299 srch->is_case_sensitive = TRUE;
1300 srch->search_fn = edit_search_cmd_callback;
1302 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1304 temp = g_string_new ("");
1306 /* collect max MAX_WORD_COMPLETIONS completions */
1307 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1309 g_string_set_size (temp, 0);
1310 start = srch->normal_offset;
1312 /* add matched completion if not yet added */
1313 for (i = 0; i < len; i++)
1315 skip = edit_get_byte (edit, start + i);
1316 if (isspace (skip))
1317 continue;
1319 /* skip current word */
1320 if (start + (long) i == word_start)
1321 break;
1323 g_string_append_c (temp, skip);
1326 if (temp->len == 0)
1327 continue;
1329 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1330 continue;
1332 skip = 0;
1334 for (i = 0; i < *num; i++)
1336 if (strncmp
1337 ((char *) &compl[i].text[word_len],
1338 (char *) &temp->str[word_len], max (len, compl[i].len) - word_len) == 0)
1340 struct selection this = compl[i];
1341 for (++i; i < *num; i++)
1343 compl[i - 1] = compl[i];
1345 compl[*num - 1] = this;
1346 skip = 1;
1347 break; /* skip it, already added */
1350 if (skip != 0)
1351 continue;
1353 if (*num == MAX_WORD_COMPLETIONS)
1355 g_free (compl[0].text);
1356 for (i = 1; i < *num; i++)
1358 compl[i - 1] = compl[i];
1360 (*num)--;
1362 #ifdef HAVE_CHARSET
1364 GString *recoded;
1365 recoded = str_convert_to_display (temp->str);
1367 if (recoded && recoded->len)
1368 g_string_assign (temp, recoded->str);
1370 g_string_free (recoded, TRUE);
1372 #endif
1373 compl[*num].text = g_strdup (temp->str);
1374 compl[*num].len = temp->len;
1375 (*num)++;
1376 start += len;
1378 /* note the maximal length needed for the completion dialog */
1379 if (len > max_len)
1380 max_len = len;
1383 mc_search_free (srch);
1384 g_string_free (temp, TRUE);
1385 g_free (current_word);
1387 return max_len;
1390 /* --------------------------------------------------------------------------------------------- */
1392 static void
1393 edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width,
1394 long *start_pos, long *end_pos, int *col1, int *col2)
1396 long cursor;
1397 int i, col;
1399 cursor = edit->curs1;
1400 col = edit_get_col (edit);
1402 for (i = 0; i < size; i++)
1404 if (data[i] != '\n')
1405 edit_insert (edit, data[i]);
1406 else
1407 { /* fill in and move to next line */
1408 int l;
1409 long p;
1411 if (edit_get_byte (edit, edit->curs1) != '\n')
1413 l = width - (edit_get_col (edit) - col);
1414 while (l > 0)
1416 edit_insert (edit, ' ');
1417 l -= space_width;
1420 for (p = edit->curs1;; p++)
1422 if (p == edit->last_byte)
1424 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1425 edit_insert_ahead (edit, '\n');
1426 p++;
1427 break;
1429 if (edit_get_byte (edit, p) == '\n')
1431 p++;
1432 break;
1435 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1436 l = col - edit_get_col (edit);
1437 while (l >= space_width)
1439 edit_insert (edit, ' ');
1440 l -= space_width;
1445 *col1 = col;
1446 *col2 = col + width;
1447 *start_pos = cursor;
1448 *end_pos = edit->curs1;
1449 edit_cursor_move (edit, cursor - edit->curs1);
1452 /* --------------------------------------------------------------------------------------------- */
1454 static int
1455 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1457 const macros_t *m1 = (const macros_t *) macro1;
1458 const macros_t *m2 = (const macros_t *) macro2;
1460 return m1->hotkey - m2->hotkey;
1463 /* --------------------------------------------------------------------------------------------- */
1465 static void
1466 edit_macro_sort_by_hotkey (void)
1468 if (macros_list != NULL && macros_list->len != 0)
1469 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1472 /* --------------------------------------------------------------------------------------------- */
1474 static gboolean
1475 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1477 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1478 macros_t *result;
1479 macros_t search_macro;
1481 (void) edit;
1483 search_macro.hotkey = hotkey;
1484 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1485 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1487 if (result != NULL && result->macro != NULL)
1489 *indx = (result - array_start);
1490 *macros = result;
1491 return TRUE;
1493 *indx = 0;
1494 return FALSE;
1497 /* --------------------------------------------------------------------------------------------- */
1498 /** returns FALSE on error */
1500 static gboolean
1501 edit_delete_macro (WEdit * edit, int hotkey)
1503 mc_config_t *macros_config = NULL;
1504 const char *section_name = "editor";
1505 gchar *macros_fname;
1506 guint indx;
1507 char *skeyname;
1508 const macros_t *macros = NULL;
1510 /* clear array of actions for current hotkey */
1511 while (edit_get_macro (edit, hotkey, &macros, &indx))
1513 if (macros->macro != NULL)
1514 g_array_free (macros->macro, TRUE);
1515 macros = NULL;
1516 g_array_remove_index (macros_list, indx);
1517 edit_macro_sort_by_hotkey ();
1520 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1521 macros_config = mc_config_init (macros_fname);
1522 g_free (macros_fname);
1524 if (macros_config == NULL)
1525 return FALSE;
1527 skeyname = lookup_key_by_code (hotkey);
1528 while (mc_config_del_key (macros_config, section_name, skeyname))
1530 g_free (skeyname);
1531 mc_config_save_file (macros_config, NULL);
1532 mc_config_deinit (macros_config);
1533 return TRUE;
1537 /* --------------------------------------------------------------------------------------------- */
1538 /*** public functions ****************************************************************************/
1539 /* --------------------------------------------------------------------------------------------- */
1541 void
1542 edit_help_cmd (WEdit * edit)
1544 ev_help_t event_data = { NULL, "[Internal File Editor]" };
1545 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
1547 edit->force |= REDRAW_COMPLETELY;
1550 /* --------------------------------------------------------------------------------------------- */
1552 void
1553 edit_refresh_cmd (WEdit * edit)
1555 #ifdef HAVE_SLANG
1556 int color;
1558 edit_get_syntax_color (edit, -1, &color);
1559 tty_touch_screen ();
1560 mc_refresh ();
1561 #else
1562 (void) edit;
1564 clr_scr ();
1565 repaint_screen ();
1566 #endif /* !HAVE_SLANG */
1567 tty_keypad (TRUE);
1570 /* --------------------------------------------------------------------------------------------- */
1572 void
1573 menu_save_mode_cmd (void)
1575 /* diaog sizes */
1576 const int DLG_X = 38;
1577 const int DLG_Y = 13;
1579 char *str_result;
1581 const char *str[] = {
1582 N_("&Quick save"),
1583 N_("&Safe save"),
1584 N_("&Do backups with following extension:")
1587 QuickWidget widgets[] = {
1588 /* 0 */
1589 QUICK_BUTTON (18, DLG_X, DLG_Y - 3, DLG_Y, N_("&Cancel"), B_CANCEL, NULL),
1590 /* 1 */
1591 QUICK_BUTTON (6, DLG_X, DLG_Y - 3, DLG_Y, N_("&OK"), B_ENTER, NULL),
1592 /* 2 */
1593 QUICK_CHECKBOX (4, DLG_X, 8, DLG_Y, N_("Check &POSIX new line"), &option_check_nl_at_eof),
1594 /* 3 */
1595 QUICK_INPUT (8, DLG_X, 6, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result),
1596 /* 4 */
1597 QUICK_RADIO (4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode),
1598 QUICK_END
1601 QuickDialog dialog = {
1602 DLG_X, DLG_Y, -1, -1, N_("Edit Save Mode"),
1603 "[Edit Save Mode]", widgets, NULL, FALSE
1606 size_t i;
1607 size_t maxlen = 0;
1608 size_t w0, w1, b_len, w3;
1610 #ifdef HAVE_ASSERT_H
1611 assert (option_backup_ext != NULL);
1612 #endif
1614 /* OK/Cancel buttons */
1615 w0 = str_term_width1 (_(widgets[0].u.button.text)) + 3;
1616 w1 = str_term_width1 (_(widgets[1].u.button.text)) + 5; /* default button */
1617 b_len = w0 + w1 + 3;
1619 maxlen = max (b_len, (size_t) str_term_width1 (_(dialog.title)) + 2);
1621 w3 = 0;
1622 for (i = 0; i < 3; i++)
1624 #ifdef ENABLE_NLS
1625 str[i] = _(str[i]);
1626 #endif
1627 w3 = max (w3, (size_t) str_term_width1 (str[i]));
1630 maxlen = max (maxlen, w3 + 4);
1632 dialog.xlen = min ((size_t) COLS, maxlen + 8);
1634 widgets[3].u.input.len = w3;
1635 widgets[1].relative_x = (dialog.xlen - b_len) / 2;
1636 widgets[0].relative_x = widgets[1].relative_x + w0 + 2;
1638 for (i = 0; i < sizeof (widgets) / sizeof (widgets[0]); i++)
1639 widgets[i].x_divisions = dialog.xlen;
1641 if (quick_dialog (&dialog) != B_CANCEL)
1643 g_free (option_backup_ext);
1644 option_backup_ext = str_result;
1648 /* --------------------------------------------------------------------------------------------- */
1650 void
1651 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1653 vfs_path_free (edit->filename_vpath);
1654 edit->filename_vpath = vfs_path_clone (name_vpath);
1656 if (edit->dir_vpath == NULL)
1657 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1660 /* --------------------------------------------------------------------------------------------- */
1661 /* Here we want to warn the users of overwriting an existing file,
1662 but only if they have made a change to the filename */
1663 /* returns 1 on success */
1665 edit_save_as_cmd (WEdit * edit)
1667 /* This heads the 'Save As' dialog box */
1668 vfs_path_t *exp_vpath;
1669 int save_lock = 0;
1670 int different_filename = 0;
1672 if (!edit_check_newline (edit))
1673 return 0;
1675 exp_vpath = edit_get_save_file_as (edit);
1676 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1678 if (exp_vpath != NULL)
1680 if (vfs_path_len (exp_vpath) == 0)
1681 goto ret;
1682 else
1684 int rv;
1686 if (vfs_path_cmp (edit->filename_vpath, exp_vpath) != 0)
1688 int file;
1689 struct stat sb;
1691 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1693 edit_error_dialog (_("Save as"),
1694 get_sys_error (_
1695 ("Cannot save: destination is not a regular file")));
1696 goto ret;
1699 different_filename = 1;
1700 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1702 if (file != -1)
1704 /* the file exists */
1705 mc_close (file);
1706 /* Overwrite the current file or cancel the operation */
1707 if (edit_query_dialog2
1708 (_("Warning"),
1709 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1710 goto ret;
1712 else
1714 edit->stat1.st_mode |= S_IWUSR;
1716 save_lock = lock_file (exp_vpath);
1718 else
1720 /* filenames equal, check if already locked */
1721 if (!edit->locked && !edit->delete_file)
1722 save_lock = lock_file (exp_vpath);
1725 if (different_filename)
1728 * Allow user to write into saved (under another name) file
1729 * even if original file had r/o user permissions.
1731 edit->stat1.st_mode |= S_IWRITE;
1734 rv = edit_save_file (edit, exp_vpath);
1735 switch (rv)
1737 case 1:
1738 /* Succesful, so unlock both files */
1739 if (different_filename)
1741 if (save_lock)
1742 unlock_file (exp_vpath);
1743 if (edit->locked)
1744 edit->locked = unlock_file (edit->filename_vpath);
1746 else
1748 if (edit->locked || save_lock)
1749 edit->locked = unlock_file (edit->filename_vpath);
1752 edit_set_filename (edit, exp_vpath);
1753 if (edit->lb != LB_ASIS)
1754 edit_reload (edit, exp_vpath);
1755 edit->modified = 0;
1756 edit->delete_file = 0;
1757 if (different_filename)
1758 edit_load_syntax (edit, NULL, edit->syntax_type);
1759 vfs_path_free (exp_vpath);
1760 edit->force |= REDRAW_COMPLETELY;
1761 return 1;
1762 default:
1763 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1764 /* fallthrough */
1765 case -1:
1766 /* Failed, so maintain modify (not save) lock */
1767 if (save_lock)
1768 unlock_file (exp_vpath);
1769 break;
1774 ret:
1775 vfs_path_free (exp_vpath);
1776 edit->force |= REDRAW_COMPLETELY;
1777 return 0;
1780 /* {{{ Macro stuff starts here */
1781 /* --------------------------------------------------------------------------------------------- */
1783 void
1784 edit_delete_macro_cmd (WEdit * edit)
1786 int hotkey;
1788 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1790 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1791 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1794 /* --------------------------------------------------------------------------------------------- */
1796 /** returns FALSE on error */
1797 gboolean
1798 edit_execute_macro (WEdit * edit, int hotkey)
1800 gboolean res = FALSE;
1802 if (hotkey != 0)
1804 const macros_t *macros;
1805 guint indx;
1807 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1808 macros->macro != NULL && macros->macro->len != 0)
1810 guint i;
1812 edit->force |= REDRAW_PAGE;
1814 for (i = 0; i < macros->macro->len; i++)
1816 const macro_action_t *m_act;
1818 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1819 edit_execute_cmd (edit, m_act->action, m_act->ch);
1820 res = TRUE;
1825 return res;
1828 /* --------------------------------------------------------------------------------------------- */
1830 /** returns FALSE on error */
1831 gboolean
1832 edit_store_macro_cmd (WEdit * edit)
1834 int i;
1835 int hotkey;
1836 GString *marcros_string;
1837 mc_config_t *macros_config = NULL;
1838 const char *section_name = "editor";
1839 gchar *macros_fname;
1840 GArray *macros; /* current macro */
1841 int tmp_act;
1842 gboolean have_macro = FALSE;
1843 char *skeyname = NULL;
1845 hotkey = editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1846 if (hotkey == ESC_CHAR)
1847 return FALSE;
1849 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1851 /* return FALSE if try assign macro into restricted hotkeys */
1852 if (tmp_act == CK_MacroStartRecord
1853 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1854 return FALSE;
1856 edit_delete_macro (edit, hotkey);
1858 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1859 macros_config = mc_config_init (macros_fname);
1860 g_free (macros_fname);
1862 if (macros_config == NULL)
1863 return FALSE;
1865 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1867 marcros_string = g_string_sized_new (250);
1868 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1870 skeyname = lookup_key_by_code (hotkey);
1872 for (i = 0; i < macro_index; i++)
1874 macro_action_t m_act;
1875 const char *action_name;
1877 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1879 if (action_name == NULL)
1880 break;
1882 m_act.action = record_macro_buf[i].action;
1883 m_act.ch = record_macro_buf[i].ch;
1884 g_array_append_val (macros, m_act);
1885 have_macro = TRUE;
1886 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1887 (int) record_macro_buf[i].ch);
1889 if (have_macro)
1891 macros_t macro;
1892 macro.hotkey = hotkey;
1893 macro.macro = macros;
1894 g_array_append_val (macros_list, macro);
1895 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1897 else
1898 mc_config_del_key (macros_config, section_name, skeyname);
1900 g_free (skeyname);
1901 edit_macro_sort_by_hotkey ();
1903 g_string_free (marcros_string, TRUE);
1904 mc_config_save_file (macros_config, NULL);
1905 mc_config_deinit (macros_config);
1906 return TRUE;
1909 /* --------------------------------------------------------------------------------------------- */
1911 gboolean
1912 edit_repeat_macro_cmd (WEdit * edit)
1914 int i, j;
1915 char *f;
1916 long count_repeat;
1917 char *error = NULL;
1919 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL);
1920 if (f == NULL || *f == '\0')
1922 g_free (f);
1923 return FALSE;
1926 count_repeat = strtol (f, &error, 0);
1928 if (*error != '\0')
1930 g_free (f);
1931 return FALSE;
1934 g_free (f);
1936 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1937 edit->force |= REDRAW_PAGE;
1939 for (j = 0; j < count_repeat; j++)
1940 for (i = 0; i < macro_index; i++)
1941 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1942 edit_update_screen (edit);
1943 return TRUE;
1946 /* --------------------------------------------------------------------------------------------- */
1947 /** return FALSE on error */
1949 gboolean
1950 edit_load_macro_cmd (WEdit * edit)
1952 mc_config_t *macros_config = NULL;
1953 gchar **profile_keys, **keys;
1954 gchar **values, **curr_values;
1955 gsize len, values_len;
1956 const char *section_name = "editor";
1957 gchar *macros_fname;
1958 int hotkey;
1960 (void) edit;
1962 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1963 macros_config = mc_config_init (macros_fname);
1964 g_free (macros_fname);
1966 if (macros_config == NULL)
1967 return FALSE;
1969 profile_keys = keys = mc_config_get_keys (macros_config, section_name, &len);
1970 while (*profile_keys != NULL)
1972 gboolean have_macro;
1973 GArray *macros;
1974 macros_t macro;
1976 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1978 curr_values = values = mc_config_get_string_list (macros_config, section_name,
1979 *profile_keys, &values_len);
1980 hotkey = lookup_key (*profile_keys, NULL);
1981 have_macro = FALSE;
1983 while (*curr_values != NULL && *curr_values[0] != '\0')
1985 char **macro_pair = NULL;
1987 macro_pair = g_strsplit (*curr_values, ":", 2);
1989 if (macro_pair != NULL)
1991 macro_action_t m_act;
1992 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
1993 m_act.action = 0;
1994 else
1996 m_act.action = keybind_lookup_action (macro_pair[0]);
1997 g_free (macro_pair[0]);
1998 macro_pair[0] = NULL;
2000 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
2001 m_act.ch = -1;
2002 else
2004 m_act.ch = strtol (macro_pair[1], NULL, 0);
2005 g_free (macro_pair[1]);
2006 macro_pair[1] = NULL;
2008 if (m_act.action != 0)
2010 /* a shell command */
2011 if ((m_act.action / CK_PipeBlock (0)) == 1)
2013 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
2014 m_act.ch = -1;
2016 g_array_append_val (macros, m_act);
2017 have_macro = TRUE;
2019 g_strfreev (macro_pair);
2020 macro_pair = NULL;
2022 curr_values++;
2024 if (have_macro)
2026 macro.hotkey = hotkey;
2027 macro.macro = macros;
2028 g_array_append_val (macros_list, macro);
2030 profile_keys++;
2031 g_strfreev (values);
2033 g_strfreev (keys);
2034 mc_config_deinit (macros_config);
2035 edit_macro_sort_by_hotkey ();
2036 return TRUE;
2039 /* }}} Macro stuff end here */
2041 /* --------------------------------------------------------------------------------------------- */
2042 /** returns 1 on success */
2045 edit_save_confirm_cmd (WEdit * edit)
2047 gchar *f = NULL;
2049 if (edit->filename_vpath == NULL)
2050 return edit_save_as_cmd (edit);
2052 if (!edit_check_newline (edit))
2053 return 0;
2055 if (edit_confirm_save)
2057 char *filename;
2058 gboolean ok;
2060 filename = vfs_path_to_str (edit->filename_vpath);
2061 f = g_strdup_printf (_("Confirm save file: \"%s\""), filename);
2062 g_free (filename);
2063 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2064 g_free (f);
2065 if (!ok)
2066 return 0;
2068 return edit_save_cmd (edit);
2071 /* --------------------------------------------------------------------------------------------- */
2072 /** returns 1 on success */
2075 edit_new_cmd (WEdit * edit)
2077 if (edit->modified)
2079 if (edit_query_dialog2
2080 (_("Warning"),
2082 ("Current text was modified without a file save.\nContinue discards these changes"),
2083 _("C&ontinue"), _("&Cancel")))
2085 edit->force |= REDRAW_COMPLETELY;
2086 return 0;
2089 edit->force |= REDRAW_COMPLETELY;
2091 return edit_renew (edit); /* if this gives an error, something has really screwed up */
2094 /* --------------------------------------------------------------------------------------------- */
2097 edit_load_cmd (WEdit * edit, edit_current_file_t what)
2099 if (edit->modified
2100 && (edit_query_dialog2
2101 (_("Warning"),
2102 _("Current text was modified without a file save.\n"
2103 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")) == 1))
2105 edit->force |= REDRAW_COMPLETELY;
2106 return 0;
2109 switch (what)
2111 case EDIT_FILE_COMMON:
2113 char *filename, *exp;
2115 filename = vfs_path_to_str (edit->filename_vpath);
2116 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2117 MC_HISTORY_EDIT_LOAD, filename);
2118 g_free (filename);
2120 if (exp != NULL)
2122 if (*exp != '\0')
2124 vfs_path_t *exp_vpath;
2126 exp_vpath = vfs_path_from_str (exp);
2127 edit_load_file_from_filename (edit, exp_vpath);
2128 vfs_path_free (exp_vpath);
2130 g_free (exp);
2133 break;
2135 case EDIT_FILE_SYNTAX:
2136 edit_load_syntax_file (edit);
2137 break;
2139 case EDIT_FILE_MENU:
2140 edit_load_menu_file (edit);
2141 break;
2143 default:
2144 break;
2147 edit->force |= REDRAW_COMPLETELY;
2148 return 0;
2151 /* --------------------------------------------------------------------------------------------- */
2153 if mark2 is -1 then marking is from mark1 to the cursor.
2154 Otherwise its between the markers. This handles this.
2155 Returns 1 if no text is marked.
2159 eval_marks (WEdit * edit, long *start_mark, long *end_mark)
2161 if (edit->mark1 != edit->mark2)
2163 long start_bol, start_eol;
2164 long end_bol, end_eol;
2165 long col1, col2;
2166 long diff1, diff2;
2167 long end_mark_curs;
2169 if (edit->end_mark_curs < 0)
2170 end_mark_curs = edit->curs1;
2171 else
2172 end_mark_curs = edit->end_mark_curs;
2174 if (edit->mark2 >= 0)
2176 *start_mark = min (edit->mark1, edit->mark2);
2177 *end_mark = max (edit->mark1, edit->mark2);
2179 else
2181 *start_mark = min (edit->mark1, end_mark_curs);
2182 *end_mark = max (edit->mark1, end_mark_curs);
2183 edit->column2 = edit->curs_col + edit->over_col;
2186 if (edit->column_highlight
2187 && (((edit->mark1 > end_mark_curs) && (edit->column1 < edit->column2))
2188 || ((edit->mark1 < end_mark_curs) && (edit->column1 > edit->column2))))
2190 start_bol = edit_bol (edit, *start_mark);
2191 start_eol = edit_eol (edit, start_bol - 1) + 1;
2192 end_bol = edit_bol (edit, *end_mark);
2193 end_eol = edit_eol (edit, *end_mark);
2194 col1 = min (edit->column1, edit->column2);
2195 col2 = max (edit->column1, edit->column2);
2197 diff1 =
2198 edit_move_forward3 (edit, start_bol, col2, 0) - edit_move_forward3 (edit, start_bol,
2199 col1, 0);
2200 diff2 =
2201 edit_move_forward3 (edit, end_bol, col2, 0) - edit_move_forward3 (edit, end_bol,
2202 col1, 0);
2204 *start_mark -= diff1;
2205 *end_mark += diff2;
2206 *start_mark = max (*start_mark, start_eol);
2207 *end_mark = min (*end_mark, end_eol);
2209 return 0;
2211 else
2213 *start_mark = *end_mark = 0;
2214 edit->column2 = edit->column1 = 0;
2215 return 1;
2219 /* --------------------------------------------------------------------------------------------- */
2221 void
2222 edit_insert_over (WEdit * edit)
2224 int i;
2226 for (i = 0; i < edit->over_col; i++)
2228 edit_insert (edit, ' ');
2230 edit->over_col = 0;
2233 /* --------------------------------------------------------------------------------------------- */
2236 edit_insert_column_of_text_from_file (WEdit * edit, int file,
2237 long *start_pos, long *end_pos, int *col1, int *col2)
2239 long cursor;
2240 int col;
2241 int blocklen = -1, width = 0;
2242 unsigned char *data;
2244 cursor = edit->curs1;
2245 col = edit_get_col (edit);
2246 data = g_malloc0 (TEMP_BUF_LEN);
2248 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
2250 int i;
2251 for (width = 0; width < blocklen; width++)
2253 if (data[width] == '\n')
2254 break;
2256 for (i = 0; i < blocklen; i++)
2258 if (data[i] == '\n')
2259 { /* fill in and move to next line */
2260 int l;
2261 long p;
2262 if (edit_get_byte (edit, edit->curs1) != '\n')
2264 l = width - (edit_get_col (edit) - col);
2265 while (l > 0)
2267 edit_insert (edit, ' ');
2268 l -= space_width;
2271 for (p = edit->curs1;; p++)
2273 if (p == edit->last_byte)
2275 edit_cursor_move (edit, edit->last_byte - edit->curs1);
2276 edit_insert_ahead (edit, '\n');
2277 p++;
2278 break;
2280 if (edit_get_byte (edit, p) == '\n')
2282 p++;
2283 break;
2286 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
2287 l = col - edit_get_col (edit);
2288 while (l >= space_width)
2290 edit_insert (edit, ' ');
2291 l -= space_width;
2293 continue;
2295 edit_insert (edit, data[i]);
2298 *col1 = col;
2299 *col2 = col + width;
2300 *start_pos = cursor;
2301 *end_pos = edit->curs1;
2302 edit_cursor_move (edit, cursor - edit->curs1);
2303 g_free (data);
2305 return blocklen;
2308 /* --------------------------------------------------------------------------------------------- */
2310 void
2311 edit_block_copy_cmd (WEdit * edit)
2313 long start_mark, end_mark, current = edit->curs1;
2314 long col_delta = 0;
2315 long mark1, mark2;
2316 int c1, c2;
2317 int size;
2318 unsigned char *copy_buf;
2320 edit_update_curs_col (edit);
2321 if (eval_marks (edit, &start_mark, &end_mark))
2322 return;
2324 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2326 /* all that gets pushed are deletes hence little space is used on the stack */
2328 edit_push_markers (edit);
2330 if (edit->column_highlight)
2332 col_delta = abs (edit->column2 - edit->column1);
2333 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2335 else
2337 while (size--)
2338 edit_insert_ahead (edit, copy_buf[size]);
2341 g_free (copy_buf);
2342 edit_scroll_screen_over_cursor (edit);
2344 if (edit->column_highlight)
2345 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2346 else if (start_mark < current && end_mark > current)
2347 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2349 edit->force |= REDRAW_PAGE;
2353 /* --------------------------------------------------------------------------------------------- */
2355 void
2356 edit_block_move_cmd (WEdit * edit)
2358 long current;
2359 unsigned char *copy_buf = NULL;
2360 long start_mark, end_mark;
2361 long line;
2363 if (eval_marks (edit, &start_mark, &end_mark))
2364 return;
2366 line = edit->curs_line;
2367 if (edit->mark2 < 0)
2368 edit_mark_cmd (edit, 0);
2369 edit_push_markers (edit);
2371 if (edit->column_highlight)
2373 long mark1, mark2;
2374 int size;
2375 int b_width = 0;
2376 int c1, c2;
2377 int x, x2;
2379 c1 = min (edit->column1, edit->column2);
2380 c2 = max (edit->column1, edit->column2);
2381 b_width = (c2 - c1);
2383 edit_update_curs_col (edit);
2385 x = edit->curs_col;
2386 x2 = x + edit->over_col;
2388 /* do nothing when cursor inside first line of selected area */
2389 if ((edit_eol (edit, edit->curs1) == edit_eol (edit, start_mark)) && (x2 > c1 && x2 <= c2))
2390 return;
2392 if (edit->curs1 > start_mark && edit->curs1 < edit_eol (edit, end_mark))
2394 if (x > c2)
2395 x -= b_width;
2396 else if (x > c1 && x <= c2)
2397 x = c1;
2399 /* save current selection into buffer */
2400 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2402 /* remove current selection */
2403 edit_block_delete_cmd (edit);
2405 edit->over_col = max (0, edit->over_col - b_width);
2406 /* calculate the cursor pos after delete block */
2407 current = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), x, 0);
2408 edit_cursor_move (edit, current - edit->curs1);
2409 edit_scroll_screen_over_cursor (edit);
2411 /* add TWS if need before block insertion */
2412 if (option_cursor_beyond_eol && edit->over_col > 0)
2413 edit_insert_over (edit);
2415 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2416 edit_set_markers (edit, mark1, mark2, c1, c2);
2418 else
2420 long count;
2422 current = edit->curs1;
2423 copy_buf = g_malloc0 (end_mark - start_mark);
2424 edit_cursor_move (edit, start_mark - edit->curs1);
2425 edit_scroll_screen_over_cursor (edit);
2426 count = start_mark;
2427 while (count < end_mark)
2429 copy_buf[end_mark - count - 1] = edit_delete (edit, 1);
2430 count++;
2432 edit_scroll_screen_over_cursor (edit);
2433 edit_cursor_move (edit,
2434 current - edit->curs1 -
2435 (((current - edit->curs1) > 0) ? end_mark - start_mark : 0));
2436 edit_scroll_screen_over_cursor (edit);
2437 while (count-- > start_mark)
2438 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2439 edit_set_markers (edit, edit->curs1, edit->curs1 + end_mark - start_mark, 0, 0);
2442 edit_scroll_screen_over_cursor (edit);
2443 g_free (copy_buf);
2444 edit->force |= REDRAW_PAGE;
2447 /* --------------------------------------------------------------------------------------------- */
2448 /** returns 1 if canceelled by user */
2451 edit_block_delete_cmd (WEdit * edit)
2453 long start_mark, end_mark;
2454 if (eval_marks (edit, &start_mark, &end_mark))
2456 edit_delete_line (edit);
2457 return 0;
2459 return edit_block_delete (edit);
2462 /* --------------------------------------------------------------------------------------------- */
2463 /** call with edit = 0 before shutdown to close memory leaks */
2465 void
2466 edit_replace_cmd (WEdit * edit, int again)
2468 /* 1 = search string, 2 = replace with */
2469 static char *saved1 = NULL; /* saved default[123] */
2470 static char *saved2 = NULL;
2471 char *input1 = NULL; /* user input from the dialog */
2472 char *input2 = NULL;
2473 GString *input2_str = NULL;
2474 char *disp1 = NULL;
2475 char *disp2 = NULL;
2476 long times_replaced = 0;
2477 gboolean once_found = FALSE;
2479 if (!edit)
2481 g_free (saved1), saved1 = NULL;
2482 g_free (saved2), saved2 = NULL;
2483 return;
2486 edit->force |= REDRAW_COMPLETELY;
2488 if (again && !saved1 && !saved2)
2489 again = 0;
2491 if (again)
2493 input1 = g_strdup (saved1 ? saved1 : "");
2494 input2 = g_strdup (saved2 ? saved2 : "");
2496 else
2498 char *tmp_inp1, *tmp_inp2;
2500 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2501 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2503 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2505 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2507 g_free (disp1);
2508 g_free (disp2);
2510 if (input1 == NULL || *input1 == '\0')
2512 edit->force = REDRAW_COMPLETELY;
2513 goto cleanup;
2516 tmp_inp1 = input1;
2517 tmp_inp2 = input2;
2518 input1 = edit_replace_cmd__conv_to_input (input1);
2519 input2 = edit_replace_cmd__conv_to_input (input2);
2520 g_free (tmp_inp1);
2521 g_free (tmp_inp2);
2523 g_free (saved1), saved1 = g_strdup (input1);
2524 g_free (saved2), saved2 = g_strdup (input2);
2526 mc_search_free (edit->search);
2527 edit->search = NULL;
2530 input2_str = g_string_new (input2);
2532 if (!edit->search)
2534 edit->search = mc_search_new (input1, -1);
2535 if (edit->search == NULL)
2537 edit->search_start = edit->curs1;
2538 goto cleanup;
2540 edit->search->search_type = edit_search_options.type;
2541 edit->search->is_all_charsets = edit_search_options.all_codepages;
2542 edit->search->is_case_sensitive = edit_search_options.case_sens;
2543 edit->search->whole_words = edit_search_options.whole_words;
2544 edit->search->search_fn = edit_search_cmd_callback;
2545 edit->search_line_type = edit_get_search_line_type (edit->search);
2546 edit_search_fix_search_start_if_selection (edit);
2549 if (edit->found_len && edit->search_start == edit->found_start + 1
2550 && edit_search_options.backwards)
2551 edit->search_start--;
2553 if (edit->found_len && edit->search_start == edit->found_start - 1
2554 && !edit_search_options.backwards)
2555 edit->search_start++;
2559 gsize len = 0;
2561 if (!editcmd_find (edit, &len))
2563 if (!(edit->search->error == MC_SEARCH_E_OK ||
2564 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2566 edit_error_dialog (_("Search"), edit->search->error_str);
2568 break;
2570 once_found = TRUE;
2572 edit->search_start = edit->search->normal_offset;
2573 /*returns negative on not found or error in pattern */
2575 if ((edit->search_start >= 0) && (edit->search_start < edit->last_byte))
2577 gsize i;
2578 GString *repl_str;
2580 edit->found_start = edit->search_start;
2581 i = edit->found_len = len;
2583 edit_cursor_move (edit, edit->search_start - edit->curs1);
2584 edit_scroll_screen_over_cursor (edit);
2586 if (edit->replace_mode == 0)
2588 int l;
2589 int prompt;
2591 l = edit->curs_row - edit->widget.lines / 3;
2592 if (l > 0)
2593 edit_scroll_downward (edit, l);
2594 if (l < 0)
2595 edit_scroll_upward (edit, -l);
2597 edit_scroll_screen_over_cursor (edit);
2598 edit->force |= REDRAW_PAGE;
2599 edit_render_keypress (edit);
2601 /*so that undo stops at each query */
2602 edit_push_key_press (edit);
2603 /* and prompt 2/3 down */
2604 disp1 = edit_replace_cmd__conv_to_display (saved1);
2605 disp2 = edit_replace_cmd__conv_to_display (saved2);
2606 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2607 g_free (disp1);
2608 g_free (disp2);
2610 if (prompt == B_REPLACE_ALL)
2611 edit->replace_mode = 1;
2612 else if (prompt == B_SKIP_REPLACE)
2614 if (edit_search_options.backwards)
2615 edit->search_start--;
2616 else
2617 edit->search_start++;
2618 continue; /* loop */
2620 else if (prompt == B_CANCEL)
2622 edit->replace_mode = -1;
2623 break; /* loop */
2627 repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2629 if (edit->search->error != MC_SEARCH_E_OK)
2631 edit_error_dialog (_("Replace"), edit->search->error_str);
2632 g_string_free (repl_str, TRUE);
2633 break;
2636 /* delete then insert new */
2637 for (i = 0; i < len; i++)
2638 edit_delete (edit, 1);
2640 for (i = 0; i < repl_str->len; i++)
2641 edit_insert (edit, repl_str->str[i]);
2643 edit->found_len = repl_str->len;
2644 g_string_free (repl_str, TRUE);
2645 times_replaced++;
2647 /* so that we don't find the same string again */
2648 if (edit_search_options.backwards)
2650 edit->search_start--;
2652 else
2654 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2656 if (edit->search_start >= edit->last_byte)
2657 break;
2660 edit_scroll_screen_over_cursor (edit);
2662 else
2664 /* try and find from right here for next search */
2665 edit->search_start = edit->curs1;
2666 edit_update_curs_col (edit);
2668 edit->force |= REDRAW_PAGE;
2669 edit_render_keypress (edit);
2671 if (times_replaced == 0)
2672 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2673 break;
2676 while (edit->replace_mode >= 0);
2678 edit_scroll_screen_over_cursor (edit);
2679 edit->force |= REDRAW_COMPLETELY;
2680 edit_render_keypress (edit);
2682 if ((edit->replace_mode == 1) && (times_replaced != 0))
2683 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2685 cleanup:
2686 g_free (input1);
2687 g_free (input2);
2688 if (input2_str != NULL)
2689 g_string_free (input2_str, TRUE);
2692 /* --------------------------------------------------------------------------------------------- */
2694 mc_search_cbret_t
2695 edit_search_cmd_callback (const void *user_data, gsize char_offset, int *current_char)
2697 *current_char = edit_get_byte ((WEdit *) user_data, (long) char_offset);
2698 return MC_SEARCH_CB_OK;
2701 /* --------------------------------------------------------------------------------------------- */
2703 void
2704 edit_search_cmd (WEdit * edit, gboolean again)
2707 if (edit == NULL)
2708 return;
2710 if (!again)
2711 edit_search (edit);
2712 else if (edit->last_search_string != NULL)
2713 edit_do_search (edit);
2714 else
2716 /* find last search string in history */
2717 GList *history;
2719 history = history_get (MC_HISTORY_SHARED_SEARCH);
2720 if (history != NULL && history->data != NULL)
2722 edit->last_search_string = (char *) history->data;
2723 history->data = NULL;
2724 history = g_list_first (history);
2725 g_list_foreach (history, (GFunc) g_free, NULL);
2726 g_list_free (history);
2728 edit->search = mc_search_new (edit->last_search_string, -1);
2729 if (edit->search == NULL)
2731 /* if not... then ask for an expression */
2732 g_free (edit->last_search_string);
2733 edit->last_search_string = NULL;
2734 edit_search (edit);
2736 else
2738 edit->search->search_type = edit_search_options.type;
2739 edit->search->is_all_charsets = edit_search_options.all_codepages;
2740 edit->search->is_case_sensitive = edit_search_options.case_sens;
2741 edit->search->whole_words = edit_search_options.whole_words;
2742 edit->search->search_fn = edit_search_cmd_callback;
2743 edit->search_line_type = edit_get_search_line_type (edit->search);
2744 edit_do_search (edit);
2747 else
2749 /* if not... then ask for an expression */
2750 g_free (edit->last_search_string);
2751 edit->last_search_string = NULL;
2752 edit_search (edit);
2758 /* --------------------------------------------------------------------------------------------- */
2760 * Check if it's OK to close the editor. If there are unsaved changes,
2761 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2764 gboolean
2765 edit_ok_to_exit (WEdit * edit)
2767 int act;
2769 if (!edit->modified)
2770 return TRUE;
2772 if (!mc_global.midnight_shutdown)
2774 if (!edit_check_newline (edit))
2775 return FALSE;
2777 query_set_sel (2);
2778 act = edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2779 _("&Yes"), _("&No"), _("&Cancel quit"));
2781 else
2783 act =
2784 edit_query_dialog2 (_("Quit"),
2785 _("Midnight Commander is being shut down.\nSave modified file?"),
2786 _("&Yes"), _("&No"));
2788 /* Esc is No */
2789 if (act == -1)
2790 act = 1;
2793 switch (act)
2795 case 0: /* Yes */
2796 edit_push_markers (edit);
2797 edit_set_markers (edit, 0, 0, 0, 0);
2798 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2799 return mc_global.midnight_shutdown;
2800 break;
2801 case 1: /* No */
2802 break;
2803 case 2: /* Cancel quit */
2804 case -1: /* Esc */
2805 return FALSE;
2808 return TRUE;
2811 /* --------------------------------------------------------------------------------------------- */
2812 /** save block, returns 1 on success */
2815 edit_save_block (WEdit * edit, const char *filename, long start, long finish)
2817 int len, file;
2818 vfs_path_t *vpath;
2820 vpath = vfs_path_from_str (filename);
2821 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2822 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2823 vfs_path_free (vpath);
2824 if (file == -1)
2825 return 0;
2827 if (edit->column_highlight)
2829 int r;
2831 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2832 if (r > 0)
2834 unsigned char *block, *p;
2836 p = block = edit_get_block (edit, start, finish, &len);
2837 while (len)
2839 r = mc_write (file, p, len);
2840 if (r < 0)
2841 break;
2842 p += r;
2843 len -= r;
2845 g_free (block);
2848 else
2850 unsigned char *buf;
2851 int i = start, end;
2853 len = finish - start;
2854 buf = g_malloc0 (TEMP_BUF_LEN);
2855 while (start != finish)
2857 end = min (finish, start + TEMP_BUF_LEN);
2858 for (; i < end; i++)
2859 buf[i - start] = edit_get_byte (edit, i);
2860 len -= mc_write (file, (char *) buf, end - start);
2861 start = end;
2863 g_free (buf);
2865 mc_close (file);
2866 if (len)
2867 return 0;
2868 return 1;
2871 /* --------------------------------------------------------------------------------------------- */
2873 void
2874 edit_paste_from_history (WEdit * edit)
2876 (void) edit;
2877 edit_error_dialog (_("Error"), _("This function is not implemented"));
2880 /* --------------------------------------------------------------------------------------------- */
2883 edit_copy_to_X_buf_cmd (WEdit * edit)
2885 long start_mark, end_mark;
2886 if (eval_marks (edit, &start_mark, &end_mark))
2887 return 0;
2888 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2890 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2891 return 1;
2893 /* try use external clipboard utility */
2894 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2896 return 0;
2899 /* --------------------------------------------------------------------------------------------- */
2902 edit_cut_to_X_buf_cmd (WEdit * edit)
2904 long start_mark, end_mark;
2905 if (eval_marks (edit, &start_mark, &end_mark))
2906 return 0;
2907 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2909 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2910 return 1;
2912 /* try use external clipboard utility */
2913 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2915 edit_block_delete_cmd (edit);
2916 edit_mark_cmd (edit, 1);
2917 return 0;
2920 /* --------------------------------------------------------------------------------------------- */
2922 void
2923 edit_paste_from_X_buf_cmd (WEdit * edit)
2925 vfs_path_t *tmp;
2927 /* try use external clipboard utility */
2928 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
2929 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
2930 edit_insert_file (edit, tmp);
2931 vfs_path_free (tmp);
2935 /* --------------------------------------------------------------------------------------------- */
2937 * Ask user for the line and go to that line.
2938 * Negative numbers mean line from the end (i.e. -1 is the last line).
2941 void
2942 edit_goto_cmd (WEdit * edit)
2944 char *f;
2945 static long line = 0; /* line as typed, saved as default */
2946 long l;
2947 char *error;
2948 char s[32];
2950 g_snprintf (s, sizeof (s), "%ld", line);
2951 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "");
2952 if (!f)
2953 return;
2955 if (!*f)
2957 g_free (f);
2958 return;
2961 l = strtol (f, &error, 0);
2962 if (*error)
2964 g_free (f);
2965 return;
2968 line = l;
2969 if (l < 0)
2970 l = edit->total_lines + l + 2;
2971 edit_move_display (edit, l - edit->widget.lines / 2 - 1);
2972 edit_move_to_line (edit, l - 1);
2973 edit->force |= REDRAW_COMPLETELY;
2974 g_free (f);
2978 /* --------------------------------------------------------------------------------------------- */
2979 /** Return 1 on success */
2982 edit_save_block_cmd (WEdit * edit)
2984 long start_mark, end_mark;
2985 char *exp, *tmp;
2987 if (eval_marks (edit, &start_mark, &end_mark))
2988 return 1;
2990 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
2991 exp =
2992 input_expand_dialog (_("Save block"), _("Enter file name:"),
2993 MC_HISTORY_EDIT_SAVE_BLOCK, tmp);
2994 g_free (tmp);
2995 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2996 if (exp)
2998 if (!*exp)
3000 g_free (exp);
3001 return 0;
3003 else
3005 if (edit_save_block (edit, exp, start_mark, end_mark))
3007 g_free (exp);
3008 edit->force |= REDRAW_COMPLETELY;
3009 return 1;
3011 else
3013 g_free (exp);
3014 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3018 edit->force |= REDRAW_COMPLETELY;
3019 return 0;
3023 /* --------------------------------------------------------------------------------------------- */
3024 /** returns TRUE on success */
3026 gboolean
3027 edit_insert_file_cmd (WEdit * edit)
3029 char *tmp;
3030 char *exp;
3031 gboolean ret = FALSE;
3033 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3034 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3035 MC_HISTORY_EDIT_INSERT_FILE, tmp);
3036 g_free (tmp);
3038 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3040 if (exp != NULL && *exp != '\0')
3042 vfs_path_t *exp_vpath;
3044 exp_vpath = vfs_path_from_str (exp);
3045 ret = (edit_insert_file (edit, exp_vpath) >= 0);
3046 vfs_path_free (exp_vpath);
3048 if (!ret)
3049 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3052 g_free (exp);
3054 edit->force |= REDRAW_COMPLETELY;
3055 return ret;
3058 /* --------------------------------------------------------------------------------------------- */
3059 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3062 edit_sort_cmd (WEdit * edit)
3064 static char *old = 0;
3065 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3066 long start_mark, end_mark;
3067 int e;
3069 if (eval_marks (edit, &start_mark, &end_mark))
3071 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3072 return 0;
3075 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3076 edit_save_block (edit, tmp, start_mark, end_mark);
3077 g_free (tmp);
3079 exp = input_dialog (_("Run sort"),
3080 _("Enter sort options (see manpage) separated by whitespace:"),
3081 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "");
3083 if (!exp)
3084 return 1;
3085 g_free (old);
3086 old = exp;
3087 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3088 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3089 tmp =
3090 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3091 " > ", tmp_edit_temp_name, (char *) NULL);
3092 g_free (tmp_edit_temp_name);
3093 g_free (tmp_edit_block_name);
3095 e = system (tmp);
3096 g_free (tmp);
3097 if (e)
3099 if (e == -1 || e == 127)
3101 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3103 else
3105 char q[8];
3106 sprintf (q, "%d ", e);
3107 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3108 edit_error_dialog (_("Sort"), tmp);
3109 g_free (tmp);
3111 return -1;
3114 edit->force |= REDRAW_COMPLETELY;
3116 if (edit_block_delete_cmd (edit))
3117 return 1;
3120 vfs_path_t *tmp_vpath;
3122 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3123 edit_insert_file (edit, tmp_vpath);
3124 vfs_path_free (tmp_vpath);
3126 return 0;
3129 /* --------------------------------------------------------------------------------------------- */
3131 * Ask user for a command, execute it and paste its output back to the
3132 * editor.
3136 edit_ext_cmd (WEdit * edit)
3138 char *exp, *tmp, *tmp_edit_temp_file;
3139 int e;
3141 exp =
3142 input_dialog (_("Paste output of external command"),
3143 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL);
3145 if (!exp)
3146 return 1;
3148 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3149 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3150 g_free (tmp_edit_temp_file);
3151 e = system (tmp);
3152 g_free (tmp);
3153 g_free (exp);
3155 if (e)
3157 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3158 return -1;
3161 edit->force |= REDRAW_COMPLETELY;
3164 vfs_path_t *tmp_vpath;
3166 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3167 edit_insert_file (edit, tmp_vpath);
3168 vfs_path_free (tmp_vpath);
3170 return 0;
3173 /* --------------------------------------------------------------------------------------------- */
3174 /** if block is 1, a block must be highlighted and the shell command
3175 processes it. If block is 0 the shell command is a straight system
3176 command, that just produces some output which is to be inserted */
3178 void
3179 edit_block_process_cmd (WEdit * edit, int macro_number)
3181 char *fname;
3182 char *macros_fname = NULL;
3184 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3185 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3186 user_menu (edit, macros_fname, 0);
3187 g_free (fname);
3188 g_free (macros_fname);
3189 edit->force |= REDRAW_COMPLETELY;
3192 /* --------------------------------------------------------------------------------------------- */
3194 void
3195 edit_mail_dialog (WEdit * edit)
3197 char *tmail_to;
3198 char *tmail_subject;
3199 char *tmail_cc;
3201 static char *mail_cc_last = 0;
3202 static char *mail_subject_last = 0;
3203 static char *mail_to_last = 0;
3205 QuickWidget quick_widgets[] = {
3206 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
3207 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
3208 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input", &tmail_cc),
3209 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT, N_("Copies to")),
3210 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-2",
3211 &tmail_subject),
3212 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT, N_("Subject")),
3213 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-3", &tmail_to),
3214 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT, N_("To")),
3215 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT, N_("mail -s <subject> -c <cc> <to>")),
3216 QUICK_END
3219 QuickDialog Quick_input = {
3220 50, MAIL_DLG_HEIGHT, -1, -1, N_("Mail"),
3221 "[Input Line Keys]", quick_widgets, NULL, FALSE
3224 quick_widgets[2].u.input.text = mail_cc_last ? mail_cc_last : "";
3225 quick_widgets[4].u.input.text = mail_subject_last ? mail_subject_last : "";
3226 quick_widgets[6].u.input.text = mail_to_last ? mail_to_last : "";
3228 if (quick_dialog (&Quick_input) != B_CANCEL)
3230 g_free (mail_cc_last);
3231 g_free (mail_subject_last);
3232 g_free (mail_to_last);
3233 mail_cc_last = tmail_cc;
3234 mail_subject_last = tmail_subject;
3235 mail_to_last = tmail_to;
3236 pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
3241 /*******************/
3242 /* Word Completion */
3243 /*******************/
3245 /* --------------------------------------------------------------------------------------------- */
3247 * Complete current word using regular expression search
3248 * backwards beginning at the current cursor position.
3251 void
3252 edit_complete_word_cmd (WEdit * edit)
3254 gsize i, max_len, word_len = 0, num_compl = 0;
3255 long word_start = 0;
3256 unsigned char *bufpos;
3257 char *match_expr;
3258 struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
3260 /* search start of word to be completed */
3261 if (!edit_find_word_start (edit, &word_start, &word_len))
3262 return;
3264 /* prepare match expression */
3265 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3267 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3268 match_expr =
3269 g_strdup_printf
3270 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3271 (int) word_len, bufpos);
3273 /* collect the possible completions */
3274 /* start search from begin to end of file */
3275 max_len =
3276 edit_collect_completions (edit, word_start, word_len, match_expr,
3277 (struct selection *) &compl, &num_compl);
3279 if (num_compl > 0)
3281 /* insert completed word if there is only one match */
3282 if (num_compl == 1)
3284 for (i = word_len; i < compl[0].len; i++)
3285 edit_insert (edit, *(compl[0].text + i));
3287 /* more than one possible completion => ask the user */
3288 else
3290 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3291 /* !!! pressed again the selection dialog pops up, but that !!! */
3292 /* !!! seems to require a further internal state !!! */
3293 /*tty_beep (); */
3295 /* let the user select the preferred completion */
3296 editcmd_dialog_completion_show (edit, max_len, word_len,
3297 (struct selection *) &compl, num_compl);
3301 g_free (match_expr);
3302 /* release memory before return */
3303 for (i = 0; i < num_compl; i++)
3304 g_free (compl[i].text);
3307 /* --------------------------------------------------------------------------------------------- */
3309 void
3310 edit_select_codepage_cmd (WEdit * edit)
3312 #ifdef HAVE_CHARSET
3313 if (do_select_codepage ())
3314 edit_set_codeset (edit);
3316 edit->force = REDRAW_COMPLETELY;
3317 edit_refresh_cmd (edit);
3318 #else
3319 (void) edit;
3320 #endif
3323 /* --------------------------------------------------------------------------------------------- */
3325 void
3326 edit_insert_literal_cmd (WEdit * edit)
3328 int char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3329 _("Press any key:"), 0);
3330 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3333 /* --------------------------------------------------------------------------------------------- */
3335 void
3336 edit_begin_end_macro_cmd (WEdit * edit)
3338 /* edit is a pointer to the widget */
3339 if (edit != NULL)
3341 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3342 edit_execute_key_command (edit, command, -1);
3346 /* --------------------------------------------------------------------------------------------- */
3348 void
3349 edit_begin_end_repeat_cmd (WEdit * edit)
3351 /* edit is a pointer to the widget */
3352 if (edit != NULL)
3354 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3355 edit_execute_key_command (edit, command, -1);
3359 /* --------------------------------------------------------------------------------------------- */
3362 edit_load_forward_cmd (WEdit * edit)
3364 if (edit->modified)
3366 if (edit_query_dialog2
3367 (_("Warning"),
3368 _("Current text was modified without a file save\n"
3369 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3371 edit->force |= REDRAW_COMPLETELY;
3372 return 0;
3375 if (edit_stack_iterator + 1 < MAX_HISTORY_MOVETO)
3377 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3379 return 1;
3381 edit_stack_iterator++;
3382 if (edit_history_moveto[edit_stack_iterator].filename_vpath)
3384 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3385 edit_history_moveto[edit_stack_iterator].line);
3386 return 0;
3388 else
3390 return 1;
3393 else
3395 return 1;
3399 /* --------------------------------------------------------------------------------------------- */
3402 edit_load_back_cmd (WEdit * edit)
3404 if (edit->modified)
3406 if (edit_query_dialog2
3407 (_("Warning"),
3408 _("Current text was modified without a file save\n"
3409 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3411 edit->force |= REDRAW_COMPLETELY;
3412 return 0;
3415 if (edit_stack_iterator > 0)
3417 edit_stack_iterator--;
3418 if (edit_history_moveto[edit_stack_iterator].filename_vpath)
3420 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3421 edit_history_moveto[edit_stack_iterator].line);
3422 return 0;
3424 else
3426 return 1;
3429 else
3431 return 1;
3435 /* --------------------------------------------------------------------------------------------- */
3437 void
3438 edit_get_match_keyword_cmd (WEdit * edit)
3440 gsize word_len = 0, max_len = 0;
3441 int num_def = 0;
3442 int i;
3443 long word_start = 0;
3444 unsigned char *bufpos;
3445 char *match_expr;
3446 char *path = NULL;
3447 char *ptr = NULL;
3448 char *tagfile = NULL;
3450 etags_hash_t def_hash[MAX_DEFINITIONS];
3452 for (i = 0; i < MAX_DEFINITIONS; i++)
3454 def_hash[i].filename = NULL;
3457 /* search start of word to be completed */
3458 if (!edit_find_word_start (edit, &word_start, &word_len))
3459 return;
3461 /* prepare match expression */
3462 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3463 match_expr = g_strdup_printf ("%.*s", (int) word_len, bufpos);
3465 ptr = g_get_current_dir ();
3466 path = g_strconcat (ptr, G_DIR_SEPARATOR_S, (char *) NULL);
3467 g_free (ptr);
3469 /* Recursive search file 'TAGS' in parent dirs */
3472 ptr = g_path_get_dirname (path);
3473 g_free (path);
3474 path = ptr;
3475 g_free (tagfile);
3476 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3477 if (exist_file (tagfile))
3478 break;
3480 while (strcmp (path, G_DIR_SEPARATOR_S) != 0);
3482 if (tagfile)
3484 num_def =
3485 etags_set_definition_hash (tagfile, path, match_expr, (etags_hash_t *) & def_hash);
3486 g_free (tagfile);
3488 g_free (path);
3490 max_len = MAX_WIDTH_DEF_DIALOG;
3491 word_len = 0;
3492 if (num_def > 0)
3494 editcmd_dialog_select_definition_show (edit, match_expr, max_len, word_len,
3495 (etags_hash_t *) & def_hash, num_def);
3497 g_free (match_expr);
3500 /* --------------------------------------------------------------------------------------------- */