Ticket #2832: Fixed mcedit: unable to save changes in "Safe save" mode
[midnight-commander.git] / src / editor / editcmd.c
blobaec521e2fce2828d4c2e66e378aedb8f002dff78
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 /* Token-related function never return leading slash, so we need add it manually */
221 saveprefix = mc_build_filename ("/", savedir, "cooledit", NULL);
222 g_free (savedir);
223 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
224 g_free (saveprefix);
225 if (savename_vpath == NULL)
227 vfs_path_free (real_filename_vpath);
228 return 0;
230 /* FIXME:
231 * Close for now because mc_mkstemps use pure open system call
232 * to create temporary file and it needs to be reopened by
233 * VFS-aware mc_open().
235 close (fd);
237 else
238 savename_vpath = vfs_path_clone (real_filename_vpath);
240 (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
241 (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
243 fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
244 if (fd == -1)
245 goto error_save;
247 /* pipe save */
248 p = edit_get_write_filter (savename_vpath, real_filename_vpath);
249 if (p != NULL)
251 FILE *file;
253 mc_close (fd);
254 file = (FILE *) popen (p, "w");
256 if (file)
258 filelen = edit_write_stream (edit, file);
259 #if 1
260 pclose (file);
261 #else
262 if (pclose (file) != 0)
264 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
265 edit_error_dialog (_("Error"), tmp);
266 g_free (tmp);
267 g_free (p);
268 goto error_save;
270 #endif
272 else
274 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
275 edit_error_dialog (_("Error"), get_sys_error (tmp));
276 g_free (p);
277 g_free (tmp);
278 goto error_save;
280 g_free (p);
282 else if (edit->lb == LB_ASIS)
283 { /* do not change line breaks */
284 long buf;
285 buf = 0;
286 filelen = edit->last_byte;
287 while (buf <= (edit->curs1 >> S_EDIT_BUF_SIZE) - 1)
289 if (mc_write (fd, (char *) edit->buffers1[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
291 mc_close (fd);
292 goto error_save;
294 buf++;
296 if (mc_write
297 (fd, (char *) edit->buffers1[buf],
298 edit->curs1 & M_EDIT_BUF_SIZE) != (edit->curs1 & M_EDIT_BUF_SIZE))
300 filelen = -1;
302 else if (edit->curs2)
304 edit->curs2--;
305 buf = (edit->curs2 >> S_EDIT_BUF_SIZE);
306 if (mc_write
307 (fd,
308 (char *) edit->buffers2[buf] + EDIT_BUF_SIZE -
309 (edit->curs2 & M_EDIT_BUF_SIZE) - 1,
310 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) != 1 + (edit->curs2 & M_EDIT_BUF_SIZE))
312 filelen = -1;
314 else
316 while (--buf >= 0)
318 if (mc_write (fd, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
320 filelen = -1;
321 break;
325 edit->curs2++;
327 if (mc_close (fd))
328 goto error_save;
330 /* Update the file information, especially the mtime. */
331 if (mc_stat (savename_vpath, &edit->stat1) == -1)
332 goto error_save;
334 else
335 { /* change line breaks */
336 FILE *file;
337 const vfs_path_element_t *path_element;
339 mc_close (fd);
341 path_element = vfs_path_get_by_index (savename_vpath, -1);
342 file = (FILE *) fopen (path_element->path, "w");
343 if (file != NULL)
345 filelen = edit_write_stream (edit, file);
346 fclose (file);
348 else
350 char *msg;
352 msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
353 edit_error_dialog (_("Error"), msg);
354 g_free (msg);
355 goto error_save;
359 if (filelen != edit->last_byte)
360 goto error_save;
362 if (this_save_mode == EDIT_DO_BACKUP)
364 char *tmp_store_filename;
365 vfs_path_element_t *last_vpath_element;
366 vfs_path_t *tmp_vpath;
367 gboolean ok;
369 #ifdef HAVE_ASSERT_H
370 assert (option_backup_ext != NULL);
371 #endif
372 /* add backup extention to the path */
373 tmp_vpath = vfs_path_clone (real_filename_vpath);
374 last_vpath_element = (vfs_path_element_t *) vfs_path_get_by_index (tmp_vpath, -1);
375 tmp_store_filename = last_vpath_element->path;
376 last_vpath_element->path = g_strdup_printf ("%s%s", tmp_store_filename, option_backup_ext);
377 g_free (tmp_store_filename);
379 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
380 vfs_path_free (tmp_vpath);
381 if (!ok)
382 goto error_save;
385 if (this_save_mode != EDIT_QUICK_SAVE)
386 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
387 goto error_save;
389 vfs_path_free (real_filename_vpath);
390 vfs_path_free (savename_vpath);
391 return 1;
392 error_save:
393 /* FIXME: Is this safe ?
394 * if (this_save_mode != EDIT_QUICK_SAVE)
395 * mc_unlink (savename);
397 vfs_path_free (real_filename_vpath);
398 vfs_path_free (savename_vpath);
399 return 0;
402 /* --------------------------------------------------------------------------------------------- */
404 static gboolean
405 edit_check_newline (WEdit * edit)
407 return !(option_check_nl_at_eof && edit->last_byte > 0
408 && edit_get_byte (edit, edit->last_byte - 1) != '\n'
409 && edit_query_dialog2 (_("Warning"),
410 _("The file you are saving is not finished with a newline"),
411 _("C&ontinue"), _("&Cancel")));
414 /* --------------------------------------------------------------------------------------------- */
416 static vfs_path_t *
417 edit_get_save_file_as (WEdit * edit)
419 #define DLG_WIDTH 64
420 #define DLG_HEIGHT 14
422 static LineBreaks cur_lb = LB_ASIS;
424 char *filename = vfs_path_to_str (edit->filename_vpath);
425 char *filename_res;
427 const char *lb_names[LB_NAMES] = {
428 N_("&Do not change"),
429 N_("&Unix format (LF)"),
430 N_("&Windows/DOS format (CR LF)"),
431 N_("&Macintosh format (CR)")
434 QuickWidget quick_widgets[] = {
435 QUICK_BUTTON (6, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
436 QUICK_BUTTON (2, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
437 QUICK_RADIO (5, DLG_WIDTH, DLG_HEIGHT - 8, DLG_HEIGHT, LB_NAMES, lb_names, (int *) &cur_lb),
438 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 9, DLG_HEIGHT, N_("Change line breaks to:")),
439 QUICK_INPUT (3, DLG_WIDTH, DLG_HEIGHT - 11, DLG_HEIGHT, filename, DLG_WIDTH - 6, 0,
440 "save-as", &filename_res),
441 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 12, DLG_HEIGHT, N_("Enter file name:")),
442 QUICK_END
445 QuickDialog Quick_options = {
446 DLG_WIDTH, DLG_HEIGHT, -1, -1,
447 N_("Save As"), "[Save File As]",
448 quick_widgets, NULL, FALSE
451 if (quick_dialog (&Quick_options) != B_CANCEL)
453 char *fname;
454 vfs_path_t *ret_vpath;
456 edit->lb = cur_lb;
457 fname = tilde_expand (filename_res);
458 g_free (filename_res);
459 ret_vpath = vfs_path_from_str (fname);
460 g_free (fname);
461 return ret_vpath;
463 g_free (filename);
465 return NULL;
467 #undef DLG_WIDTH
468 #undef DLG_HEIGHT
471 /* --------------------------------------------------------------------------------------------- */
473 /** returns 1 on success */
475 static int
476 edit_save_cmd (WEdit * edit)
478 int res, save_lock = 0;
480 if (!edit->locked && !edit->delete_file)
481 save_lock = lock_file (edit->filename_vpath);
482 res = edit_save_file (edit, edit->filename_vpath);
484 /* Maintain modify (not save) lock on failure */
485 if ((res > 0 && edit->locked) || save_lock)
486 edit->locked = unlock_file (edit->filename_vpath);
488 /* On failure try 'save as', it does locking on its own */
489 if (!res)
490 return edit_save_as_cmd (edit);
491 edit->force |= REDRAW_COMPLETELY;
492 if (res > 0)
494 edit->delete_file = 0;
495 edit->modified = 0;
498 return 1;
501 /* --------------------------------------------------------------------------------------------- */
503 * Load file content
505 * @param edit widget object
506 * @param exp_vpath vfs file path
507 * @return TRUE if file content was successfully loaded, FALSE otherwise
510 static gboolean
511 edit_load_file_from_filename (WEdit * edit, const vfs_path_t * exp_vpath)
513 int prev_locked = edit->locked;
514 vfs_path_t *prev_filename;
515 gboolean ret = TRUE;
517 prev_filename = vfs_path_clone (edit->filename_vpath);
518 if (!edit_reload (edit, exp_vpath))
519 ret = FALSE;
520 else if (prev_locked)
521 unlock_file (prev_filename);
523 vfs_path_free (prev_filename);
524 return ret;
527 /* --------------------------------------------------------------------------------------------- */
529 static void
530 edit_load_syntax_file (WEdit * edit)
532 vfs_path_t *extdir_vpath;
533 int dir = 0;
535 if (geteuid () == 0)
537 dir = query_dialog (_("Syntax file edit"),
538 _("Which syntax file you want to edit?"), D_NORMAL, 2,
539 _("&User"), _("&System Wide"));
542 extdir_vpath =
543 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
544 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
546 vfs_path_free (extdir_vpath);
547 extdir_vpath =
548 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
551 if (dir == 0)
553 vfs_path_t *user_syntax_file_vpath;
555 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
556 check_for_default (extdir_vpath, user_syntax_file_vpath);
557 edit_load_file_from_filename (edit, user_syntax_file_vpath);
558 vfs_path_free (user_syntax_file_vpath);
560 else if (dir == 1)
561 edit_load_file_from_filename (edit, extdir_vpath);
563 vfs_path_free (extdir_vpath);
566 /* --------------------------------------------------------------------------------------------- */
568 static void
569 edit_load_menu_file (WEdit * edit)
571 vfs_path_t *buffer_vpath;
572 vfs_path_t *menufile_vpath;
573 int dir = 0;
575 dir = query_dialog (_("Menu edit"),
576 _("Which menu file do you want to edit?"), D_NORMAL,
577 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
579 menufile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
581 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
583 vfs_path_free (menufile_vpath);
584 menufile_vpath = vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
587 switch (dir)
589 case 0:
590 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
591 check_for_default (menufile_vpath, buffer_vpath);
592 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
593 break;
595 case 1:
596 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
597 check_for_default (menufile_vpath, buffer_vpath);
598 break;
600 case 2:
601 buffer_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
602 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
604 vfs_path_free (buffer_vpath);
605 buffer_vpath =
606 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
608 break;
610 default:
611 vfs_path_free (menufile_vpath);
612 return;
615 edit_load_file_from_filename (edit, buffer_vpath);
617 vfs_path_free (buffer_vpath);
618 vfs_path_free (menufile_vpath);
621 /* --------------------------------------------------------------------------------------------- */
623 static void
624 edit_delete_column_of_text (WEdit * edit)
626 long p, q, r, m1, m2;
627 long b, c, d, n;
629 eval_marks (edit, &m1, &m2);
630 n = edit_move_forward (edit, m1, 0, m2) + 1;
631 c = edit_move_forward3 (edit, edit_bol (edit, m1), 0, m1);
632 d = edit_move_forward3 (edit, edit_bol (edit, m2), 0, m2);
633 b = max (min (c, d), min (edit->column1, edit->column2));
634 c = max (c, max (edit->column1, edit->column2));
636 while (n--)
638 r = edit_bol (edit, edit->curs1);
639 p = edit_move_forward3 (edit, r, b, 0);
640 q = edit_move_forward3 (edit, r, c, 0);
641 if (p < m1)
642 p = m1;
643 if (q > m2)
644 q = m2;
645 edit_cursor_move (edit, p - edit->curs1);
646 while (q > p)
648 /* delete line between margins */
649 if (edit_get_byte (edit, edit->curs1) != '\n')
650 edit_delete (edit, 1);
651 q--;
653 if (n)
654 /* move to next line except on the last delete */
655 edit_cursor_move (edit, edit_move_forward (edit, edit->curs1, 1, 0) - edit->curs1);
659 /* --------------------------------------------------------------------------------------------- */
660 /** if success return 0 */
662 static int
663 edit_block_delete (WEdit * edit)
665 long count;
666 long start_mark, end_mark;
667 int curs_pos;
668 long curs_line, c1, c2;
670 if (eval_marks (edit, &start_mark, &end_mark))
671 return 0;
672 if (edit->column_highlight && edit->mark2 < 0)
673 edit_mark_cmd (edit, 0);
674 if ((end_mark - start_mark) > option_max_undo / 2)
676 /* Warning message with a query to continue or cancel the operation */
677 if (edit_query_dialog2
678 (_("Warning"),
680 ("Block is large, you may not be able to undo this action"),
681 _("C&ontinue"), _("&Cancel")))
683 return 1;
686 c1 = min (edit->column1, edit->column2);
687 c2 = max (edit->column1, edit->column2);
688 edit->column1 = c1;
689 edit->column2 = c2;
691 edit_push_markers (edit);
693 curs_line = edit->curs_line;
695 curs_pos = edit->curs_col + edit->over_col;
697 /* move cursor to start of selection */
698 edit_cursor_move (edit, start_mark - edit->curs1);
699 edit_scroll_screen_over_cursor (edit);
700 count = start_mark;
701 if (start_mark < end_mark)
703 if (edit->column_highlight)
705 int line_width;
707 if (edit->mark2 < 0)
708 edit_mark_cmd (edit, 0);
709 edit_delete_column_of_text (edit);
710 /* move cursor to the saved position */
711 edit_move_to_line (edit, curs_line);
712 /* calculate line width and cursor position before cut */
713 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
714 edit_eol (edit, edit->curs1));
715 if (option_cursor_beyond_eol && curs_pos > line_width)
716 edit->over_col = curs_pos - line_width;
718 else
720 while (count < end_mark)
722 edit_delete (edit, 1);
723 count++;
727 edit_set_markers (edit, 0, 0, 0, 0);
728 edit->force |= REDRAW_PAGE;
729 return 0;
732 /* --------------------------------------------------------------------------------------------- */
734 * Get EOL symbol for searching.
736 * @param edit editor object
737 * @return EOL symbol
740 static inline char
741 edit_search_get_current_end_line_char (const WEdit * edit)
743 switch (edit->lb)
745 case LB_MAC:
746 return '\r';
747 default:
748 return '\n';
752 /* --------------------------------------------------------------------------------------------- */
754 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
756 * @param search search object
757 * @return result of checks.
760 static edit_search_line_t
761 edit_get_search_line_type (mc_search_t * search)
763 edit_search_line_t search_line_type = 0;
765 if (search->search_type != MC_SEARCH_T_REGEX)
766 return search_line_type;
768 if (*search->original == '^')
769 search_line_type |= AT_START_LINE;
771 if (search->original[search->original_len - 1] == '$')
772 search_line_type |= AT_END_LINE;
773 return search_line_type;
776 /* --------------------------------------------------------------------------------------------- */
778 * Calculating the start position of next line.
780 * @param edit editor object
781 * @param current_pos current position
782 * @param max_pos max position
783 * @param end_string_symbol end of line symbol
784 * @return start position of next line
787 static off_t
788 edit_calculate_start_of_next_line (WEdit * edit, off_t current_pos, off_t max_pos,
789 char end_string_symbol)
791 off_t i;
793 for (i = current_pos; i < max_pos; i++)
795 current_pos++;
796 if (edit_get_byte (edit, i) == end_string_symbol)
797 break;
800 return current_pos;
803 /* --------------------------------------------------------------------------------------------- */
805 * Calculating the end position of previous line.
807 * @param edit editor object
808 * @param current_pos current position
809 * @param end_string_symbol end of line symbol
810 * @return end position of previous line
813 static off_t
814 edit_calculate_end_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
816 off_t i;
818 for (i = current_pos - 1; i >= 0; i--)
819 if (edit_get_byte (edit, i) == end_string_symbol)
820 break;
822 return i;
825 /* --------------------------------------------------------------------------------------------- */
827 * Calculating the start position of previous line.
829 * @param edit editor object
830 * @param current_pos current position
831 * @param end_string_symbol end of line symbol
832 * @return start position of previous line
835 static inline off_t
836 edit_calculate_start_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
838 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
839 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
841 return (current_pos + 1);
844 /* --------------------------------------------------------------------------------------------- */
846 * Calculating the start position of current line.
848 * @param edit editor object
849 * @param current_pos current position
850 * @param end_string_symbol end of line symbol
851 * @return start position of current line
854 static inline off_t
855 edit_calculate_start_of_current_line (WEdit * edit, off_t current_pos, char end_string_symbol)
857 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
859 return (current_pos + 1);
862 /* --------------------------------------------------------------------------------------------- */
864 * Fixing (if needed) search start position if 'only in selection' option present.
866 * @param edit editor object
869 static void
870 edit_search_fix_search_start_if_selection (WEdit * edit)
872 long start_mark = 0;
873 long end_mark = 0;
875 if (!edit_search_options.only_in_selection)
876 return;
878 if (eval_marks (edit, &start_mark, &end_mark) != 0)
879 return;
881 if (edit_search_options.backwards)
883 if (edit->search_start > end_mark || edit->search_start <= start_mark)
884 edit->search_start = end_mark;
886 else
888 if (edit->search_start < start_mark || edit->search_start >= end_mark)
889 edit->search_start = start_mark;
893 /* --------------------------------------------------------------------------------------------- */
895 static gboolean
896 editcmd_find (WEdit * edit, gsize * len)
898 off_t search_start = edit->search_start;
899 off_t search_end;
900 long start_mark = 0;
901 long end_mark = edit->last_byte;
902 int mark_res = 0;
903 char end_string_symbol;
905 end_string_symbol = edit_search_get_current_end_line_char (edit);
907 /* prepare for search */
908 if (edit_search_options.only_in_selection)
910 mark_res = eval_marks (edit, &start_mark, &end_mark);
911 if (mark_res != 0)
913 edit->search->error = MC_SEARCH_E_NOTFOUND;
914 edit->search->error_str = g_strdup (_("Search string not found"));
915 return FALSE;
918 /* fix the start and the end of search block positions */
919 if ((edit->search_line_type & AT_START_LINE) != 0
920 && (start_mark != 0 || edit_get_byte (edit, start_mark - 1) != end_string_symbol))
922 start_mark =
923 edit_calculate_start_of_next_line (edit, start_mark, edit->last_byte,
924 end_string_symbol);
926 if ((edit->search_line_type & AT_END_LINE) != 0
927 && (end_mark - 1 != edit->last_byte
928 || edit_get_byte (edit, end_mark) != end_string_symbol))
930 end_mark = edit_calculate_end_of_previous_line (edit, end_mark, end_string_symbol);
932 if (start_mark >= end_mark)
934 edit->search->error = MC_SEARCH_E_NOTFOUND;
935 edit->search->error_str = g_strdup (_("Search string not found"));
936 return FALSE;
939 else
941 if (edit_search_options.backwards)
942 end_mark = max (1, edit->curs1) - 1;
945 /* search */
946 if (edit_search_options.backwards)
948 /* backward search */
949 search_end = end_mark;
951 if ((edit->search_line_type & AT_START_LINE) != 0)
952 search_start =
953 edit_calculate_start_of_current_line (edit, search_start, end_string_symbol);
955 while ((int) search_start >= start_mark)
957 if (search_end > (off_t) (search_start + edit->search->original_len)
958 && mc_search_is_fixed_search_str (edit->search))
960 search_end = search_start + edit->search->original_len;
962 if (mc_search_run (edit->search, (void *) edit, search_start, search_end, len)
963 && edit->search->normal_offset == search_start)
965 return TRUE;
969 if ((edit->search_line_type & AT_START_LINE) != 0)
970 search_start =
971 edit_calculate_start_of_previous_line (edit, search_start, end_string_symbol);
972 else
973 search_start--;
975 edit->search->error_str = g_strdup (_("Search string not found"));
977 else
979 /* forward search */
980 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
981 search_start =
982 edit_calculate_start_of_next_line (edit, search_start, end_mark, end_string_symbol);
983 return mc_search_run (edit->search, (void *) edit, search_start, end_mark, len);
985 return FALSE;
988 /* --------------------------------------------------------------------------------------------- */
990 static char *
991 edit_replace_cmd__conv_to_display (char *str)
993 #ifdef HAVE_CHARSET
994 GString *tmp;
996 tmp = str_convert_to_display (str);
997 if (tmp != NULL)
999 if (tmp->len != 0)
1000 return g_string_free (tmp, FALSE);
1001 g_string_free (tmp, TRUE);
1003 #endif
1004 return g_strdup (str);
1007 /* --------------------------------------------------------------------------------------------- */
1009 static char *
1010 edit_replace_cmd__conv_to_input (char *str)
1012 #ifdef HAVE_CHARSET
1013 GString *tmp;
1015 tmp = str_convert_to_input (str);
1016 if (tmp != NULL)
1018 if (tmp->len != 0)
1019 return g_string_free (tmp, FALSE);
1020 g_string_free (tmp, TRUE);
1022 #endif
1023 return g_strdup (str);
1026 /* --------------------------------------------------------------------------------------------- */
1028 static void
1029 edit_do_search (WEdit * edit)
1031 gsize len = 0;
1033 if (edit->search == NULL)
1034 edit->search_start = edit->curs1;
1036 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1038 if (search_create_bookmark)
1040 int found = 0, books = 0;
1041 long l = 0, l_last = -1;
1042 long q = 0;
1044 search_create_bookmark = FALSE;
1045 book_mark_flush (edit, -1);
1047 while (TRUE)
1049 if (!mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
1050 break;
1051 if (found == 0)
1052 edit->search_start = edit->search->normal_offset;
1053 found++;
1054 l += edit_count_lines (edit, q, edit->search->normal_offset);
1055 if (l != l_last)
1057 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
1058 books++;
1060 l_last = l;
1061 q = edit->search->normal_offset + 1;
1064 if (found == 0)
1065 edit_error_dialog (_("Search"), _("Search string not found"));
1066 else
1067 edit_cursor_move (edit, edit->search_start - edit->curs1);
1069 else
1071 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
1072 && edit_search_options.backwards)
1073 edit->search_start--;
1075 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
1076 && !edit_search_options.backwards)
1077 edit->search_start++;
1079 if (editcmd_find (edit, &len))
1081 edit->found_start = edit->search_start = edit->search->normal_offset;
1082 edit->found_len = len;
1083 edit->over_col = 0;
1084 edit_cursor_move (edit, edit->search_start - edit->curs1);
1085 edit_scroll_screen_over_cursor (edit);
1086 if (edit_search_options.backwards)
1087 edit->search_start--;
1088 else
1089 edit->search_start++;
1091 else
1093 edit->search_start = edit->curs1;
1094 if (edit->search->error_str != NULL)
1095 edit_error_dialog (_("Search"), edit->search->error_str);
1099 edit->force |= REDRAW_COMPLETELY;
1100 edit_scroll_screen_over_cursor (edit);
1103 /* --------------------------------------------------------------------------------------------- */
1105 static void
1106 edit_search (WEdit * edit)
1108 if (editcmd_dialog_search_show (edit))
1110 edit->search_line_type = edit_get_search_line_type (edit->search);
1111 edit_search_fix_search_start_if_selection (edit);
1112 edit_do_search (edit);
1116 /* --------------------------------------------------------------------------------------------- */
1117 /** Return a null terminated length of text. Result must be g_free'd */
1119 static unsigned char *
1120 edit_get_block (WEdit * edit, long start, long finish, int *l)
1122 unsigned char *s, *r;
1123 r = s = g_malloc0 (finish - start + 1);
1124 if (edit->column_highlight)
1126 *l = 0;
1127 /* copy from buffer, excluding chars that are out of the column 'margins' */
1128 while (start < finish)
1130 int c;
1131 long x;
1132 x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
1133 c = edit_get_byte (edit, start);
1134 if ((x >= edit->column1 && x < edit->column2)
1135 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1137 *s++ = c;
1138 (*l)++;
1140 start++;
1143 else
1145 *l = finish - start;
1146 while (start < finish)
1147 *s++ = edit_get_byte (edit, start++);
1149 *s = 0;
1150 return r;
1153 /* --------------------------------------------------------------------------------------------- */
1154 /** copies a block to clipboard file */
1156 static int
1157 edit_save_block_to_clip_file (WEdit * edit, long start, long finish)
1159 int ret;
1160 gchar *tmp;
1161 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1162 ret = edit_save_block (edit, tmp, start, finish);
1163 g_free (tmp);
1164 return ret;
1167 /* --------------------------------------------------------------------------------------------- */
1169 static void
1170 pipe_mail (WEdit * edit, char *to, char *subject, char *cc)
1172 FILE *p = 0;
1173 char *s;
1175 to = name_quote (to, 0);
1176 subject = name_quote (subject, 0);
1177 cc = name_quote (cc, 0);
1178 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1179 g_free (to);
1180 g_free (subject);
1181 g_free (cc);
1183 if (s)
1185 p = popen (s, "w");
1186 g_free (s);
1189 if (p)
1191 long i;
1192 for (i = 0; i < edit->last_byte; i++)
1193 fputc (edit_get_byte (edit, i), p);
1194 pclose (p);
1198 /* --------------------------------------------------------------------------------------------- */
1200 static gboolean
1201 is_break_char (char c)
1203 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
1206 /* --------------------------------------------------------------------------------------------- */
1207 /** find first character of current word */
1209 static int
1210 edit_find_word_start (WEdit * edit, long *word_start, gsize * word_len)
1212 int c, last;
1213 gsize i;
1215 /* return if at begin of file */
1216 if (edit->curs1 <= 0)
1217 return 0;
1219 c = (unsigned char) edit_get_byte (edit, edit->curs1 - 1);
1220 /* return if not at end or in word */
1221 if (is_break_char (c))
1222 return 0;
1224 /* search start of word to be completed */
1225 for (i = 2;; i++)
1227 /* return if at begin of file */
1228 if ((gsize) edit->curs1 < i)
1229 return 0;
1231 last = c;
1232 c = (unsigned char) edit_get_byte (edit, edit->curs1 - i);
1234 if (is_break_char (c))
1236 /* return if word starts with digit */
1237 if (isdigit (last))
1238 return 0;
1240 *word_start = edit->curs1 - (i - 1); /* start found */
1241 *word_len = i - 1;
1242 break;
1245 /* success */
1246 return 1;
1249 /* --------------------------------------------------------------------------------------------- */
1251 * Get current word under cursor
1253 * @param edit editor object
1254 * @param srch mc_search object
1255 * @param word_start start word position
1257 * @return newly allocated string or NULL if no any words under cursor
1260 static char *
1261 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, long word_start)
1263 gsize len = 0, i;
1264 GString *temp;
1266 if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len))
1267 return NULL;
1269 temp = g_string_sized_new (len);
1271 for (i = 0; i < len; i++)
1273 int chr;
1275 chr = edit_get_byte (edit, word_start + i);
1276 if (!isspace (chr))
1277 g_string_append_c (temp, chr);
1280 return g_string_free (temp, temp->len == 0);
1283 /* --------------------------------------------------------------------------------------------- */
1284 /** collect the possible completions */
1286 static gsize
1287 edit_collect_completions (WEdit * edit, long word_start, gsize word_len,
1288 char *match_expr, struct selection *compl, gsize * num)
1290 gsize len = 0;
1291 gsize max_len = 0;
1292 gsize i;
1293 int skip;
1294 GString *temp;
1295 mc_search_t *srch;
1296 long last_byte, start = -1;
1297 char *current_word;
1299 srch = mc_search_new (match_expr, -1);
1300 if (srch == NULL)
1301 return 0;
1303 if (mc_config_get_bool
1304 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1306 last_byte = edit->last_byte;
1308 else
1310 last_byte = word_start;
1313 srch->search_type = MC_SEARCH_T_REGEX;
1314 srch->is_case_sensitive = TRUE;
1315 srch->search_fn = edit_search_cmd_callback;
1317 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1319 temp = g_string_new ("");
1321 /* collect max MAX_WORD_COMPLETIONS completions */
1322 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1324 g_string_set_size (temp, 0);
1325 start = srch->normal_offset;
1327 /* add matched completion if not yet added */
1328 for (i = 0; i < len; i++)
1330 skip = edit_get_byte (edit, start + i);
1331 if (isspace (skip))
1332 continue;
1334 /* skip current word */
1335 if (start + (long) i == word_start)
1336 break;
1338 g_string_append_c (temp, skip);
1341 if (temp->len == 0)
1342 continue;
1344 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1345 continue;
1347 skip = 0;
1349 for (i = 0; i < *num; i++)
1351 if (strncmp
1352 ((char *) &compl[i].text[word_len],
1353 (char *) &temp->str[word_len], max (len, compl[i].len) - word_len) == 0)
1355 struct selection this = compl[i];
1356 for (++i; i < *num; i++)
1358 compl[i - 1] = compl[i];
1360 compl[*num - 1] = this;
1361 skip = 1;
1362 break; /* skip it, already added */
1365 if (skip != 0)
1366 continue;
1368 if (*num == MAX_WORD_COMPLETIONS)
1370 g_free (compl[0].text);
1371 for (i = 1; i < *num; i++)
1373 compl[i - 1] = compl[i];
1375 (*num)--;
1377 #ifdef HAVE_CHARSET
1379 GString *recoded;
1380 recoded = str_convert_to_display (temp->str);
1382 if (recoded && recoded->len)
1383 g_string_assign (temp, recoded->str);
1385 g_string_free (recoded, TRUE);
1387 #endif
1388 compl[*num].text = g_strdup (temp->str);
1389 compl[*num].len = temp->len;
1390 (*num)++;
1391 start += len;
1393 /* note the maximal length needed for the completion dialog */
1394 if (len > max_len)
1395 max_len = len;
1398 mc_search_free (srch);
1399 g_string_free (temp, TRUE);
1400 g_free (current_word);
1402 return max_len;
1405 /* --------------------------------------------------------------------------------------------- */
1407 static void
1408 edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width,
1409 long *start_pos, long *end_pos, int *col1, int *col2)
1411 long cursor;
1412 int i, col;
1414 cursor = edit->curs1;
1415 col = edit_get_col (edit);
1417 for (i = 0; i < size; i++)
1419 if (data[i] != '\n')
1420 edit_insert (edit, data[i]);
1421 else
1422 { /* fill in and move to next line */
1423 int l;
1424 long p;
1426 if (edit_get_byte (edit, edit->curs1) != '\n')
1428 l = width - (edit_get_col (edit) - col);
1429 while (l > 0)
1431 edit_insert (edit, ' ');
1432 l -= space_width;
1435 for (p = edit->curs1;; p++)
1437 if (p == edit->last_byte)
1439 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1440 edit_insert_ahead (edit, '\n');
1441 p++;
1442 break;
1444 if (edit_get_byte (edit, p) == '\n')
1446 p++;
1447 break;
1450 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1451 l = col - edit_get_col (edit);
1452 while (l >= space_width)
1454 edit_insert (edit, ' ');
1455 l -= space_width;
1460 *col1 = col;
1461 *col2 = col + width;
1462 *start_pos = cursor;
1463 *end_pos = edit->curs1;
1464 edit_cursor_move (edit, cursor - edit->curs1);
1467 /* --------------------------------------------------------------------------------------------- */
1469 static int
1470 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1472 const macros_t *m1 = (const macros_t *) macro1;
1473 const macros_t *m2 = (const macros_t *) macro2;
1475 return m1->hotkey - m2->hotkey;
1478 /* --------------------------------------------------------------------------------------------- */
1480 static void
1481 edit_macro_sort_by_hotkey (void)
1483 if (macros_list != NULL && macros_list->len != 0)
1484 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1487 /* --------------------------------------------------------------------------------------------- */
1489 static gboolean
1490 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1492 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1493 macros_t *result;
1494 macros_t search_macro;
1496 (void) edit;
1498 search_macro.hotkey = hotkey;
1499 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1500 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1502 if (result != NULL && result->macro != NULL)
1504 *indx = (result - array_start);
1505 *macros = result;
1506 return TRUE;
1508 *indx = 0;
1509 return FALSE;
1512 /* --------------------------------------------------------------------------------------------- */
1513 /** returns FALSE on error */
1515 static gboolean
1516 edit_delete_macro (WEdit * edit, int hotkey)
1518 mc_config_t *macros_config = NULL;
1519 const char *section_name = "editor";
1520 gchar *macros_fname;
1521 guint indx;
1522 char *skeyname;
1523 const macros_t *macros = NULL;
1525 /* clear array of actions for current hotkey */
1526 while (edit_get_macro (edit, hotkey, &macros, &indx))
1528 if (macros->macro != NULL)
1529 g_array_free (macros->macro, TRUE);
1530 macros = NULL;
1531 g_array_remove_index (macros_list, indx);
1532 edit_macro_sort_by_hotkey ();
1535 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1536 macros_config = mc_config_init (macros_fname);
1537 g_free (macros_fname);
1539 if (macros_config == NULL)
1540 return FALSE;
1542 skeyname = lookup_key_by_code (hotkey);
1543 while (mc_config_del_key (macros_config, section_name, skeyname))
1545 g_free (skeyname);
1546 mc_config_save_file (macros_config, NULL);
1547 mc_config_deinit (macros_config);
1548 return TRUE;
1552 /* --------------------------------------------------------------------------------------------- */
1553 /*** public functions ****************************************************************************/
1554 /* --------------------------------------------------------------------------------------------- */
1556 void
1557 edit_help_cmd (WEdit * edit)
1559 ev_help_t event_data = { NULL, "[Internal File Editor]" };
1560 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
1562 edit->force |= REDRAW_COMPLETELY;
1565 /* --------------------------------------------------------------------------------------------- */
1567 void
1568 edit_refresh_cmd (WEdit * edit)
1570 #ifdef HAVE_SLANG
1571 int color;
1573 edit_get_syntax_color (edit, -1, &color);
1574 tty_touch_screen ();
1575 mc_refresh ();
1576 #else
1577 (void) edit;
1579 clr_scr ();
1580 repaint_screen ();
1581 #endif /* !HAVE_SLANG */
1582 tty_keypad (TRUE);
1585 /* --------------------------------------------------------------------------------------------- */
1587 void
1588 menu_save_mode_cmd (void)
1590 /* diaog sizes */
1591 const int DLG_X = 38;
1592 const int DLG_Y = 13;
1594 char *str_result;
1596 const char *str[] = {
1597 N_("&Quick save"),
1598 N_("&Safe save"),
1599 N_("&Do backups with following extension:")
1602 QuickWidget widgets[] = {
1603 /* 0 */
1604 QUICK_BUTTON (18, DLG_X, DLG_Y - 3, DLG_Y, N_("&Cancel"), B_CANCEL, NULL),
1605 /* 1 */
1606 QUICK_BUTTON (6, DLG_X, DLG_Y - 3, DLG_Y, N_("&OK"), B_ENTER, NULL),
1607 /* 2 */
1608 QUICK_CHECKBOX (4, DLG_X, 8, DLG_Y, N_("Check &POSIX new line"), &option_check_nl_at_eof),
1609 /* 3 */
1610 QUICK_INPUT (8, DLG_X, 6, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result),
1611 /* 4 */
1612 QUICK_RADIO (4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode),
1613 QUICK_END
1616 QuickDialog dialog = {
1617 DLG_X, DLG_Y, -1, -1, N_("Edit Save Mode"),
1618 "[Edit Save Mode]", widgets, NULL, FALSE
1621 size_t i;
1622 size_t maxlen = 0;
1623 size_t w0, w1, b_len, w3;
1625 #ifdef HAVE_ASSERT_H
1626 assert (option_backup_ext != NULL);
1627 #endif
1629 /* OK/Cancel buttons */
1630 w0 = str_term_width1 (_(widgets[0].u.button.text)) + 3;
1631 w1 = str_term_width1 (_(widgets[1].u.button.text)) + 5; /* default button */
1632 b_len = w0 + w1 + 3;
1634 maxlen = max (b_len, (size_t) str_term_width1 (_(dialog.title)) + 2);
1636 w3 = 0;
1637 for (i = 0; i < 3; i++)
1639 #ifdef ENABLE_NLS
1640 str[i] = _(str[i]);
1641 #endif
1642 w3 = max (w3, (size_t) str_term_width1 (str[i]));
1645 maxlen = max (maxlen, w3 + 4);
1647 dialog.xlen = min ((size_t) COLS, maxlen + 8);
1649 widgets[3].u.input.len = w3;
1650 widgets[1].relative_x = (dialog.xlen - b_len) / 2;
1651 widgets[0].relative_x = widgets[1].relative_x + w0 + 2;
1653 for (i = 0; i < sizeof (widgets) / sizeof (widgets[0]); i++)
1654 widgets[i].x_divisions = dialog.xlen;
1656 if (quick_dialog (&dialog) != B_CANCEL)
1658 g_free (option_backup_ext);
1659 option_backup_ext = str_result;
1663 /* --------------------------------------------------------------------------------------------- */
1665 void
1666 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1668 vfs_path_free (edit->filename_vpath);
1669 edit->filename_vpath = vfs_path_clone (name_vpath);
1671 if (edit->dir_vpath == NULL)
1672 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1675 /* --------------------------------------------------------------------------------------------- */
1676 /* Here we want to warn the users of overwriting an existing file,
1677 but only if they have made a change to the filename */
1678 /* returns 1 on success */
1680 edit_save_as_cmd (WEdit * edit)
1682 /* This heads the 'Save As' dialog box */
1683 vfs_path_t *exp_vpath;
1684 int save_lock = 0;
1685 int different_filename = 0;
1687 if (!edit_check_newline (edit))
1688 return 0;
1690 exp_vpath = edit_get_save_file_as (edit);
1691 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1693 if (exp_vpath != NULL)
1695 if (vfs_path_len (exp_vpath) == 0)
1696 goto ret;
1697 else
1699 int rv;
1701 if (vfs_path_cmp (edit->filename_vpath, exp_vpath) != 0)
1703 int file;
1704 struct stat sb;
1706 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1708 edit_error_dialog (_("Save as"),
1709 get_sys_error (_
1710 ("Cannot save: destination is not a regular file")));
1711 goto ret;
1714 different_filename = 1;
1715 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1717 if (file != -1)
1719 /* the file exists */
1720 mc_close (file);
1721 /* Overwrite the current file or cancel the operation */
1722 if (edit_query_dialog2
1723 (_("Warning"),
1724 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1725 goto ret;
1727 else
1729 edit->stat1.st_mode |= S_IWUSR;
1731 save_lock = lock_file (exp_vpath);
1733 else
1735 /* filenames equal, check if already locked */
1736 if (!edit->locked && !edit->delete_file)
1737 save_lock = lock_file (exp_vpath);
1740 if (different_filename)
1743 * Allow user to write into saved (under another name) file
1744 * even if original file had r/o user permissions.
1746 edit->stat1.st_mode |= S_IWRITE;
1749 rv = edit_save_file (edit, exp_vpath);
1750 switch (rv)
1752 case 1:
1753 /* Succesful, so unlock both files */
1754 if (different_filename)
1756 if (save_lock)
1757 unlock_file (exp_vpath);
1758 if (edit->locked)
1759 edit->locked = unlock_file (edit->filename_vpath);
1761 else
1763 if (edit->locked || save_lock)
1764 edit->locked = unlock_file (edit->filename_vpath);
1767 edit_set_filename (edit, exp_vpath);
1768 if (edit->lb != LB_ASIS)
1769 edit_reload (edit, exp_vpath);
1770 edit->modified = 0;
1771 edit->delete_file = 0;
1772 if (different_filename)
1773 edit_load_syntax (edit, NULL, edit->syntax_type);
1774 vfs_path_free (exp_vpath);
1775 edit->force |= REDRAW_COMPLETELY;
1776 return 1;
1777 default:
1778 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1779 /* fallthrough */
1780 case -1:
1781 /* Failed, so maintain modify (not save) lock */
1782 if (save_lock)
1783 unlock_file (exp_vpath);
1784 break;
1789 ret:
1790 vfs_path_free (exp_vpath);
1791 edit->force |= REDRAW_COMPLETELY;
1792 return 0;
1795 /* {{{ Macro stuff starts here */
1796 /* --------------------------------------------------------------------------------------------- */
1798 void
1799 edit_delete_macro_cmd (WEdit * edit)
1801 int hotkey;
1803 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1805 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1806 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1809 /* --------------------------------------------------------------------------------------------- */
1811 /** returns FALSE on error */
1812 gboolean
1813 edit_execute_macro (WEdit * edit, int hotkey)
1815 gboolean res = FALSE;
1817 if (hotkey != 0)
1819 const macros_t *macros;
1820 guint indx;
1822 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1823 macros->macro != NULL && macros->macro->len != 0)
1825 guint i;
1827 edit->force |= REDRAW_PAGE;
1829 for (i = 0; i < macros->macro->len; i++)
1831 const macro_action_t *m_act;
1833 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1834 edit_execute_cmd (edit, m_act->action, m_act->ch);
1835 res = TRUE;
1840 return res;
1843 /* --------------------------------------------------------------------------------------------- */
1845 /** returns FALSE on error */
1846 gboolean
1847 edit_store_macro_cmd (WEdit * edit)
1849 int i;
1850 int hotkey;
1851 GString *marcros_string;
1852 mc_config_t *macros_config = NULL;
1853 const char *section_name = "editor";
1854 gchar *macros_fname;
1855 GArray *macros; /* current macro */
1856 int tmp_act;
1857 gboolean have_macro = FALSE;
1858 char *skeyname = NULL;
1860 hotkey = editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1861 if (hotkey == ESC_CHAR)
1862 return FALSE;
1864 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1866 /* return FALSE if try assign macro into restricted hotkeys */
1867 if (tmp_act == CK_MacroStartRecord
1868 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1869 return FALSE;
1871 edit_delete_macro (edit, hotkey);
1873 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1874 macros_config = mc_config_init (macros_fname);
1875 g_free (macros_fname);
1877 if (macros_config == NULL)
1878 return FALSE;
1880 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1882 marcros_string = g_string_sized_new (250);
1883 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1885 skeyname = lookup_key_by_code (hotkey);
1887 for (i = 0; i < macro_index; i++)
1889 macro_action_t m_act;
1890 const char *action_name;
1892 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1894 if (action_name == NULL)
1895 break;
1897 m_act.action = record_macro_buf[i].action;
1898 m_act.ch = record_macro_buf[i].ch;
1899 g_array_append_val (macros, m_act);
1900 have_macro = TRUE;
1901 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1902 (int) record_macro_buf[i].ch);
1904 if (have_macro)
1906 macros_t macro;
1907 macro.hotkey = hotkey;
1908 macro.macro = macros;
1909 g_array_append_val (macros_list, macro);
1910 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1912 else
1913 mc_config_del_key (macros_config, section_name, skeyname);
1915 g_free (skeyname);
1916 edit_macro_sort_by_hotkey ();
1918 g_string_free (marcros_string, TRUE);
1919 mc_config_save_file (macros_config, NULL);
1920 mc_config_deinit (macros_config);
1921 return TRUE;
1924 /* --------------------------------------------------------------------------------------------- */
1926 gboolean
1927 edit_repeat_macro_cmd (WEdit * edit)
1929 int i, j;
1930 char *f;
1931 long count_repeat;
1932 char *error = NULL;
1934 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL);
1935 if (f == NULL || *f == '\0')
1937 g_free (f);
1938 return FALSE;
1941 count_repeat = strtol (f, &error, 0);
1943 if (*error != '\0')
1945 g_free (f);
1946 return FALSE;
1949 g_free (f);
1951 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1952 edit->force |= REDRAW_PAGE;
1954 for (j = 0; j < count_repeat; j++)
1955 for (i = 0; i < macro_index; i++)
1956 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1957 edit_update_screen (edit);
1958 return TRUE;
1961 /* --------------------------------------------------------------------------------------------- */
1962 /** return FALSE on error */
1964 gboolean
1965 edit_load_macro_cmd (WEdit * edit)
1967 mc_config_t *macros_config = NULL;
1968 gchar **profile_keys, **keys;
1969 gchar **values, **curr_values;
1970 gsize len, values_len;
1971 const char *section_name = "editor";
1972 gchar *macros_fname;
1973 int hotkey;
1975 (void) edit;
1977 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1978 macros_config = mc_config_init (macros_fname);
1979 g_free (macros_fname);
1981 if (macros_config == NULL)
1982 return FALSE;
1984 profile_keys = keys = mc_config_get_keys (macros_config, section_name, &len);
1985 while (*profile_keys != NULL)
1987 gboolean have_macro;
1988 GArray *macros;
1989 macros_t macro;
1991 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1993 curr_values = values = mc_config_get_string_list (macros_config, section_name,
1994 *profile_keys, &values_len);
1995 hotkey = lookup_key (*profile_keys, NULL);
1996 have_macro = FALSE;
1998 while (*curr_values != NULL && *curr_values[0] != '\0')
2000 char **macro_pair = NULL;
2002 macro_pair = g_strsplit (*curr_values, ":", 2);
2004 if (macro_pair != NULL)
2006 macro_action_t m_act;
2007 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
2008 m_act.action = 0;
2009 else
2011 m_act.action = keybind_lookup_action (macro_pair[0]);
2012 g_free (macro_pair[0]);
2013 macro_pair[0] = NULL;
2015 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
2016 m_act.ch = -1;
2017 else
2019 m_act.ch = strtol (macro_pair[1], NULL, 0);
2020 g_free (macro_pair[1]);
2021 macro_pair[1] = NULL;
2023 if (m_act.action != 0)
2025 /* a shell command */
2026 if ((m_act.action / CK_PipeBlock (0)) == 1)
2028 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
2029 m_act.ch = -1;
2031 g_array_append_val (macros, m_act);
2032 have_macro = TRUE;
2034 g_strfreev (macro_pair);
2035 macro_pair = NULL;
2037 curr_values++;
2039 if (have_macro)
2041 macro.hotkey = hotkey;
2042 macro.macro = macros;
2043 g_array_append_val (macros_list, macro);
2045 profile_keys++;
2046 g_strfreev (values);
2048 g_strfreev (keys);
2049 mc_config_deinit (macros_config);
2050 edit_macro_sort_by_hotkey ();
2051 return TRUE;
2054 /* }}} Macro stuff end here */
2056 /* --------------------------------------------------------------------------------------------- */
2057 /** returns 1 on success */
2060 edit_save_confirm_cmd (WEdit * edit)
2062 gchar *f = NULL;
2064 if (edit->filename_vpath == NULL)
2065 return edit_save_as_cmd (edit);
2067 if (!edit_check_newline (edit))
2068 return 0;
2070 if (edit_confirm_save)
2072 char *filename;
2073 gboolean ok;
2075 filename = vfs_path_to_str (edit->filename_vpath);
2076 f = g_strdup_printf (_("Confirm save file: \"%s\""), filename);
2077 g_free (filename);
2078 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2079 g_free (f);
2080 if (!ok)
2081 return 0;
2083 return edit_save_cmd (edit);
2086 /* --------------------------------------------------------------------------------------------- */
2088 /* returns TRUE on success */
2089 gboolean
2090 edit_new_cmd (WEdit * edit)
2092 if (edit->modified
2093 && edit_query_dialog2 (_("Warning"),
2094 _("Current text was modified without a file save.\n"
2095 "Continue discards these changes"),
2096 _("C&ontinue"), _("&Cancel")) == 1)
2098 edit->force |= REDRAW_COMPLETELY;
2099 return TRUE;
2102 edit->force |= REDRAW_COMPLETELY;
2104 return edit_renew (edit); /* if this gives an error, something has really screwed up */
2107 /* --------------------------------------------------------------------------------------------- */
2109 gboolean
2110 edit_load_cmd (WEdit * edit, edit_current_file_t what)
2112 gboolean ret = TRUE;
2114 if (edit->modified
2115 && edit_query_dialog2 (_("Warning"),
2116 _("Current text was modified without a file save.\n"
2117 "Continue discards these changes"), _("C&ontinue"),
2118 _("&Cancel")) == 1)
2120 edit->force |= REDRAW_COMPLETELY;
2121 return TRUE;
2124 switch (what)
2126 case EDIT_FILE_COMMON:
2128 char *filename, *exp;
2130 filename = vfs_path_to_str (edit->filename_vpath);
2131 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2132 MC_HISTORY_EDIT_LOAD, filename);
2133 g_free (filename);
2135 if (exp != NULL && *exp != '\0')
2137 vfs_path_t *exp_vpath;
2139 exp_vpath = vfs_path_from_str (exp);
2140 ret = edit_load_file_from_filename (edit, exp_vpath);
2141 vfs_path_free (exp_vpath);
2144 g_free (exp);
2146 break;
2148 case EDIT_FILE_SYNTAX:
2149 edit_load_syntax_file (edit);
2150 break;
2152 case EDIT_FILE_MENU:
2153 edit_load_menu_file (edit);
2154 break;
2156 default:
2157 break;
2160 edit->force |= REDRAW_COMPLETELY;
2161 return ret;
2164 /* --------------------------------------------------------------------------------------------- */
2166 if mark2 is -1 then marking is from mark1 to the cursor.
2167 Otherwise its between the markers. This handles this.
2168 Returns 1 if no text is marked.
2172 eval_marks (WEdit * edit, long *start_mark, long *end_mark)
2174 if (edit->mark1 != edit->mark2)
2176 long start_bol, start_eol;
2177 long end_bol, end_eol;
2178 long col1, col2;
2179 long diff1, diff2;
2180 long end_mark_curs;
2182 if (edit->end_mark_curs < 0)
2183 end_mark_curs = edit->curs1;
2184 else
2185 end_mark_curs = edit->end_mark_curs;
2187 if (edit->mark2 >= 0)
2189 *start_mark = min (edit->mark1, edit->mark2);
2190 *end_mark = max (edit->mark1, edit->mark2);
2192 else
2194 *start_mark = min (edit->mark1, end_mark_curs);
2195 *end_mark = max (edit->mark1, end_mark_curs);
2196 edit->column2 = edit->curs_col + edit->over_col;
2199 if (edit->column_highlight
2200 && (((edit->mark1 > end_mark_curs) && (edit->column1 < edit->column2))
2201 || ((edit->mark1 < end_mark_curs) && (edit->column1 > edit->column2))))
2203 start_bol = edit_bol (edit, *start_mark);
2204 start_eol = edit_eol (edit, start_bol - 1) + 1;
2205 end_bol = edit_bol (edit, *end_mark);
2206 end_eol = edit_eol (edit, *end_mark);
2207 col1 = min (edit->column1, edit->column2);
2208 col2 = max (edit->column1, edit->column2);
2210 diff1 =
2211 edit_move_forward3 (edit, start_bol, col2, 0) - edit_move_forward3 (edit, start_bol,
2212 col1, 0);
2213 diff2 =
2214 edit_move_forward3 (edit, end_bol, col2, 0) - edit_move_forward3 (edit, end_bol,
2215 col1, 0);
2217 *start_mark -= diff1;
2218 *end_mark += diff2;
2219 *start_mark = max (*start_mark, start_eol);
2220 *end_mark = min (*end_mark, end_eol);
2222 return 0;
2224 else
2226 *start_mark = *end_mark = 0;
2227 edit->column2 = edit->column1 = 0;
2228 return 1;
2232 /* --------------------------------------------------------------------------------------------- */
2234 void
2235 edit_insert_over (WEdit * edit)
2237 int i;
2239 for (i = 0; i < edit->over_col; i++)
2241 edit_insert (edit, ' ');
2243 edit->over_col = 0;
2246 /* --------------------------------------------------------------------------------------------- */
2249 edit_insert_column_of_text_from_file (WEdit * edit, int file,
2250 long *start_pos, long *end_pos, int *col1, int *col2)
2252 long cursor;
2253 int col;
2254 int blocklen = -1, width = 0;
2255 unsigned char *data;
2257 cursor = edit->curs1;
2258 col = edit_get_col (edit);
2259 data = g_malloc0 (TEMP_BUF_LEN);
2261 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
2263 int i;
2264 for (width = 0; width < blocklen; width++)
2266 if (data[width] == '\n')
2267 break;
2269 for (i = 0; i < blocklen; i++)
2271 if (data[i] == '\n')
2272 { /* fill in and move to next line */
2273 int l;
2274 long p;
2275 if (edit_get_byte (edit, edit->curs1) != '\n')
2277 l = width - (edit_get_col (edit) - col);
2278 while (l > 0)
2280 edit_insert (edit, ' ');
2281 l -= space_width;
2284 for (p = edit->curs1;; p++)
2286 if (p == edit->last_byte)
2288 edit_cursor_move (edit, edit->last_byte - edit->curs1);
2289 edit_insert_ahead (edit, '\n');
2290 p++;
2291 break;
2293 if (edit_get_byte (edit, p) == '\n')
2295 p++;
2296 break;
2299 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
2300 l = col - edit_get_col (edit);
2301 while (l >= space_width)
2303 edit_insert (edit, ' ');
2304 l -= space_width;
2306 continue;
2308 edit_insert (edit, data[i]);
2311 *col1 = col;
2312 *col2 = col + width;
2313 *start_pos = cursor;
2314 *end_pos = edit->curs1;
2315 edit_cursor_move (edit, cursor - edit->curs1);
2316 g_free (data);
2318 return blocklen;
2321 /* --------------------------------------------------------------------------------------------- */
2323 void
2324 edit_block_copy_cmd (WEdit * edit)
2326 long start_mark, end_mark, current = edit->curs1;
2327 long col_delta = 0;
2328 long mark1, mark2;
2329 int c1, c2;
2330 int size;
2331 unsigned char *copy_buf;
2333 edit_update_curs_col (edit);
2334 if (eval_marks (edit, &start_mark, &end_mark))
2335 return;
2337 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2339 /* all that gets pushed are deletes hence little space is used on the stack */
2341 edit_push_markers (edit);
2343 if (edit->column_highlight)
2345 col_delta = abs (edit->column2 - edit->column1);
2346 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2348 else
2350 while (size--)
2351 edit_insert_ahead (edit, copy_buf[size]);
2354 g_free (copy_buf);
2355 edit_scroll_screen_over_cursor (edit);
2357 if (edit->column_highlight)
2358 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2359 else if (start_mark < current && end_mark > current)
2360 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2362 edit->force |= REDRAW_PAGE;
2366 /* --------------------------------------------------------------------------------------------- */
2368 void
2369 edit_block_move_cmd (WEdit * edit)
2371 long current;
2372 unsigned char *copy_buf = NULL;
2373 long start_mark, end_mark;
2374 long line;
2376 if (eval_marks (edit, &start_mark, &end_mark))
2377 return;
2379 line = edit->curs_line;
2380 if (edit->mark2 < 0)
2381 edit_mark_cmd (edit, 0);
2382 edit_push_markers (edit);
2384 if (edit->column_highlight)
2386 long mark1, mark2;
2387 int size;
2388 int b_width = 0;
2389 int c1, c2;
2390 int x, x2;
2392 c1 = min (edit->column1, edit->column2);
2393 c2 = max (edit->column1, edit->column2);
2394 b_width = (c2 - c1);
2396 edit_update_curs_col (edit);
2398 x = edit->curs_col;
2399 x2 = x + edit->over_col;
2401 /* do nothing when cursor inside first line of selected area */
2402 if ((edit_eol (edit, edit->curs1) == edit_eol (edit, start_mark)) && (x2 > c1 && x2 <= c2))
2403 return;
2405 if (edit->curs1 > start_mark && edit->curs1 < edit_eol (edit, end_mark))
2407 if (x > c2)
2408 x -= b_width;
2409 else if (x > c1 && x <= c2)
2410 x = c1;
2412 /* save current selection into buffer */
2413 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2415 /* remove current selection */
2416 edit_block_delete_cmd (edit);
2418 edit->over_col = max (0, edit->over_col - b_width);
2419 /* calculate the cursor pos after delete block */
2420 current = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), x, 0);
2421 edit_cursor_move (edit, current - edit->curs1);
2422 edit_scroll_screen_over_cursor (edit);
2424 /* add TWS if need before block insertion */
2425 if (option_cursor_beyond_eol && edit->over_col > 0)
2426 edit_insert_over (edit);
2428 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2429 edit_set_markers (edit, mark1, mark2, c1, c2);
2431 else
2433 long count;
2435 current = edit->curs1;
2436 copy_buf = g_malloc0 (end_mark - start_mark);
2437 edit_cursor_move (edit, start_mark - edit->curs1);
2438 edit_scroll_screen_over_cursor (edit);
2439 count = start_mark;
2440 while (count < end_mark)
2442 copy_buf[end_mark - count - 1] = edit_delete (edit, 1);
2443 count++;
2445 edit_scroll_screen_over_cursor (edit);
2446 edit_cursor_move (edit,
2447 current - edit->curs1 -
2448 (((current - edit->curs1) > 0) ? end_mark - start_mark : 0));
2449 edit_scroll_screen_over_cursor (edit);
2450 while (count-- > start_mark)
2451 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2452 edit_set_markers (edit, edit->curs1, edit->curs1 + end_mark - start_mark, 0, 0);
2455 edit_scroll_screen_over_cursor (edit);
2456 g_free (copy_buf);
2457 edit->force |= REDRAW_PAGE;
2460 /* --------------------------------------------------------------------------------------------- */
2461 /** returns 1 if canceelled by user */
2464 edit_block_delete_cmd (WEdit * edit)
2466 long start_mark, end_mark;
2467 if (eval_marks (edit, &start_mark, &end_mark))
2469 edit_delete_line (edit);
2470 return 0;
2472 return edit_block_delete (edit);
2475 /* --------------------------------------------------------------------------------------------- */
2476 /** call with edit = 0 before shutdown to close memory leaks */
2478 void
2479 edit_replace_cmd (WEdit * edit, int again)
2481 /* 1 = search string, 2 = replace with */
2482 static char *saved1 = NULL; /* saved default[123] */
2483 static char *saved2 = NULL;
2484 char *input1 = NULL; /* user input from the dialog */
2485 char *input2 = NULL;
2486 GString *input2_str = NULL;
2487 char *disp1 = NULL;
2488 char *disp2 = NULL;
2489 long times_replaced = 0;
2490 gboolean once_found = FALSE;
2492 if (!edit)
2494 g_free (saved1), saved1 = NULL;
2495 g_free (saved2), saved2 = NULL;
2496 return;
2499 edit->force |= REDRAW_COMPLETELY;
2501 if (again && !saved1 && !saved2)
2502 again = 0;
2504 if (again)
2506 input1 = g_strdup (saved1 ? saved1 : "");
2507 input2 = g_strdup (saved2 ? saved2 : "");
2509 else
2511 char *tmp_inp1, *tmp_inp2;
2513 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2514 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2516 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2518 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2520 g_free (disp1);
2521 g_free (disp2);
2523 if (input1 == NULL || *input1 == '\0')
2525 edit->force = REDRAW_COMPLETELY;
2526 goto cleanup;
2529 tmp_inp1 = input1;
2530 tmp_inp2 = input2;
2531 input1 = edit_replace_cmd__conv_to_input (input1);
2532 input2 = edit_replace_cmd__conv_to_input (input2);
2533 g_free (tmp_inp1);
2534 g_free (tmp_inp2);
2536 g_free (saved1), saved1 = g_strdup (input1);
2537 g_free (saved2), saved2 = g_strdup (input2);
2539 mc_search_free (edit->search);
2540 edit->search = NULL;
2543 input2_str = g_string_new (input2);
2545 if (!edit->search)
2547 edit->search = mc_search_new (input1, -1);
2548 if (edit->search == NULL)
2550 edit->search_start = edit->curs1;
2551 goto cleanup;
2553 edit->search->search_type = edit_search_options.type;
2554 edit->search->is_all_charsets = edit_search_options.all_codepages;
2555 edit->search->is_case_sensitive = edit_search_options.case_sens;
2556 edit->search->whole_words = edit_search_options.whole_words;
2557 edit->search->search_fn = edit_search_cmd_callback;
2558 edit->search_line_type = edit_get_search_line_type (edit->search);
2559 edit_search_fix_search_start_if_selection (edit);
2562 if (edit->found_len && edit->search_start == edit->found_start + 1
2563 && edit_search_options.backwards)
2564 edit->search_start--;
2566 if (edit->found_len && edit->search_start == edit->found_start - 1
2567 && !edit_search_options.backwards)
2568 edit->search_start++;
2572 gsize len = 0;
2574 if (!editcmd_find (edit, &len))
2576 if (!(edit->search->error == MC_SEARCH_E_OK ||
2577 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2579 edit_error_dialog (_("Search"), edit->search->error_str);
2581 break;
2583 once_found = TRUE;
2585 edit->search_start = edit->search->normal_offset;
2586 /*returns negative on not found or error in pattern */
2588 if ((edit->search_start >= 0) && (edit->search_start < edit->last_byte))
2590 gsize i;
2591 GString *repl_str;
2593 edit->found_start = edit->search_start;
2594 i = edit->found_len = len;
2596 edit_cursor_move (edit, edit->search_start - edit->curs1);
2597 edit_scroll_screen_over_cursor (edit);
2599 if (edit->replace_mode == 0)
2601 int l;
2602 int prompt;
2604 l = edit->curs_row - edit->widget.lines / 3;
2605 if (l > 0)
2606 edit_scroll_downward (edit, l);
2607 if (l < 0)
2608 edit_scroll_upward (edit, -l);
2610 edit_scroll_screen_over_cursor (edit);
2611 edit->force |= REDRAW_PAGE;
2612 edit_render_keypress (edit);
2614 /*so that undo stops at each query */
2615 edit_push_key_press (edit);
2616 /* and prompt 2/3 down */
2617 disp1 = edit_replace_cmd__conv_to_display (saved1);
2618 disp2 = edit_replace_cmd__conv_to_display (saved2);
2619 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2620 g_free (disp1);
2621 g_free (disp2);
2623 if (prompt == B_REPLACE_ALL)
2624 edit->replace_mode = 1;
2625 else if (prompt == B_SKIP_REPLACE)
2627 if (edit_search_options.backwards)
2628 edit->search_start--;
2629 else
2630 edit->search_start++;
2631 continue; /* loop */
2633 else if (prompt == B_CANCEL)
2635 edit->replace_mode = -1;
2636 break; /* loop */
2640 repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2642 if (edit->search->error != MC_SEARCH_E_OK)
2644 edit_error_dialog (_("Replace"), edit->search->error_str);
2645 g_string_free (repl_str, TRUE);
2646 break;
2649 /* delete then insert new */
2650 for (i = 0; i < len; i++)
2651 edit_delete (edit, 1);
2653 for (i = 0; i < repl_str->len; i++)
2654 edit_insert (edit, repl_str->str[i]);
2656 edit->found_len = repl_str->len;
2657 g_string_free (repl_str, TRUE);
2658 times_replaced++;
2660 /* so that we don't find the same string again */
2661 if (edit_search_options.backwards)
2663 edit->search_start--;
2665 else
2667 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2669 if (edit->search_start >= edit->last_byte)
2670 break;
2673 edit_scroll_screen_over_cursor (edit);
2675 else
2677 /* try and find from right here for next search */
2678 edit->search_start = edit->curs1;
2679 edit_update_curs_col (edit);
2681 edit->force |= REDRAW_PAGE;
2682 edit_render_keypress (edit);
2684 if (times_replaced == 0)
2685 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2686 break;
2689 while (edit->replace_mode >= 0);
2691 edit_scroll_screen_over_cursor (edit);
2692 edit->force |= REDRAW_COMPLETELY;
2693 edit_render_keypress (edit);
2695 if ((edit->replace_mode == 1) && (times_replaced != 0))
2696 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2698 cleanup:
2699 g_free (input1);
2700 g_free (input2);
2701 if (input2_str != NULL)
2702 g_string_free (input2_str, TRUE);
2705 /* --------------------------------------------------------------------------------------------- */
2707 mc_search_cbret_t
2708 edit_search_cmd_callback (const void *user_data, gsize char_offset, int *current_char)
2710 *current_char = edit_get_byte ((WEdit *) user_data, (long) char_offset);
2711 return MC_SEARCH_CB_OK;
2714 /* --------------------------------------------------------------------------------------------- */
2716 void
2717 edit_search_cmd (WEdit * edit, gboolean again)
2720 if (edit == NULL)
2721 return;
2723 if (!again)
2724 edit_search (edit);
2725 else if (edit->last_search_string != NULL)
2726 edit_do_search (edit);
2727 else
2729 /* find last search string in history */
2730 GList *history;
2732 history = history_get (MC_HISTORY_SHARED_SEARCH);
2733 if (history != NULL && history->data != NULL)
2735 edit->last_search_string = (char *) history->data;
2736 history->data = NULL;
2737 history = g_list_first (history);
2738 g_list_foreach (history, (GFunc) g_free, NULL);
2739 g_list_free (history);
2741 edit->search = mc_search_new (edit->last_search_string, -1);
2742 if (edit->search == NULL)
2744 /* if not... then ask for an expression */
2745 g_free (edit->last_search_string);
2746 edit->last_search_string = NULL;
2747 edit_search (edit);
2749 else
2751 edit->search->search_type = edit_search_options.type;
2752 edit->search->is_all_charsets = edit_search_options.all_codepages;
2753 edit->search->is_case_sensitive = edit_search_options.case_sens;
2754 edit->search->whole_words = edit_search_options.whole_words;
2755 edit->search->search_fn = edit_search_cmd_callback;
2756 edit->search_line_type = edit_get_search_line_type (edit->search);
2757 edit_do_search (edit);
2760 else
2762 /* if not... then ask for an expression */
2763 g_free (edit->last_search_string);
2764 edit->last_search_string = NULL;
2765 edit_search (edit);
2771 /* --------------------------------------------------------------------------------------------- */
2773 * Check if it's OK to close the editor. If there are unsaved changes,
2774 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2777 gboolean
2778 edit_ok_to_exit (WEdit * edit)
2780 int act;
2782 if (!edit->modified)
2783 return TRUE;
2785 if (!mc_global.midnight_shutdown)
2787 if (!edit_check_newline (edit))
2788 return FALSE;
2790 query_set_sel (2);
2791 act = edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2792 _("&Yes"), _("&No"), _("&Cancel quit"));
2794 else
2796 act =
2797 edit_query_dialog2 (_("Quit"),
2798 _("Midnight Commander is being shut down.\nSave modified file?"),
2799 _("&Yes"), _("&No"));
2801 /* Esc is No */
2802 if (act == -1)
2803 act = 1;
2806 switch (act)
2808 case 0: /* Yes */
2809 edit_push_markers (edit);
2810 edit_set_markers (edit, 0, 0, 0, 0);
2811 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2812 return mc_global.midnight_shutdown;
2813 break;
2814 case 1: /* No */
2815 break;
2816 case 2: /* Cancel quit */
2817 case -1: /* Esc */
2818 return FALSE;
2821 return TRUE;
2824 /* --------------------------------------------------------------------------------------------- */
2825 /** save block, returns 1 on success */
2828 edit_save_block (WEdit * edit, const char *filename, long start, long finish)
2830 int len, file;
2831 vfs_path_t *vpath;
2833 vpath = vfs_path_from_str (filename);
2834 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2835 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2836 vfs_path_free (vpath);
2837 if (file == -1)
2838 return 0;
2840 if (edit->column_highlight)
2842 int r;
2844 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2845 if (r > 0)
2847 unsigned char *block, *p;
2849 p = block = edit_get_block (edit, start, finish, &len);
2850 while (len)
2852 r = mc_write (file, p, len);
2853 if (r < 0)
2854 break;
2855 p += r;
2856 len -= r;
2858 g_free (block);
2861 else
2863 unsigned char *buf;
2864 int i = start, end;
2866 len = finish - start;
2867 buf = g_malloc0 (TEMP_BUF_LEN);
2868 while (start != finish)
2870 end = min (finish, start + TEMP_BUF_LEN);
2871 for (; i < end; i++)
2872 buf[i - start] = edit_get_byte (edit, i);
2873 len -= mc_write (file, (char *) buf, end - start);
2874 start = end;
2876 g_free (buf);
2878 mc_close (file);
2879 if (len)
2880 return 0;
2881 return 1;
2884 /* --------------------------------------------------------------------------------------------- */
2886 void
2887 edit_paste_from_history (WEdit * edit)
2889 (void) edit;
2890 edit_error_dialog (_("Error"), _("This function is not implemented"));
2893 /* --------------------------------------------------------------------------------------------- */
2896 edit_copy_to_X_buf_cmd (WEdit * edit)
2898 long start_mark, end_mark;
2899 if (eval_marks (edit, &start_mark, &end_mark))
2900 return 0;
2901 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2903 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2904 return 1;
2906 /* try use external clipboard utility */
2907 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2909 return 0;
2912 /* --------------------------------------------------------------------------------------------- */
2915 edit_cut_to_X_buf_cmd (WEdit * edit)
2917 long start_mark, end_mark;
2918 if (eval_marks (edit, &start_mark, &end_mark))
2919 return 0;
2920 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2922 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2923 return 1;
2925 /* try use external clipboard utility */
2926 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2928 edit_block_delete_cmd (edit);
2929 edit_mark_cmd (edit, 1);
2930 return 0;
2933 /* --------------------------------------------------------------------------------------------- */
2935 void
2936 edit_paste_from_X_buf_cmd (WEdit * edit)
2938 vfs_path_t *tmp;
2940 /* try use external clipboard utility */
2941 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
2942 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
2943 edit_insert_file (edit, tmp);
2944 vfs_path_free (tmp);
2948 /* --------------------------------------------------------------------------------------------- */
2950 * Ask user for the line and go to that line.
2951 * Negative numbers mean line from the end (i.e. -1 is the last line).
2954 void
2955 edit_goto_cmd (WEdit * edit)
2957 char *f;
2958 static long line = 0; /* line as typed, saved as default */
2959 long l;
2960 char *error;
2961 char s[32];
2963 g_snprintf (s, sizeof (s), "%ld", line);
2964 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "");
2965 if (!f)
2966 return;
2968 if (!*f)
2970 g_free (f);
2971 return;
2974 l = strtol (f, &error, 0);
2975 if (*error)
2977 g_free (f);
2978 return;
2981 line = l;
2982 if (l < 0)
2983 l = edit->total_lines + l + 2;
2984 edit_move_display (edit, l - edit->widget.lines / 2 - 1);
2985 edit_move_to_line (edit, l - 1);
2986 edit->force |= REDRAW_COMPLETELY;
2987 g_free (f);
2991 /* --------------------------------------------------------------------------------------------- */
2992 /** Return 1 on success */
2995 edit_save_block_cmd (WEdit * edit)
2997 long start_mark, end_mark;
2998 char *exp, *tmp;
3000 if (eval_marks (edit, &start_mark, &end_mark))
3001 return 1;
3003 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3004 exp =
3005 input_expand_dialog (_("Save block"), _("Enter file name:"),
3006 MC_HISTORY_EDIT_SAVE_BLOCK, tmp);
3007 g_free (tmp);
3008 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3009 if (exp)
3011 if (!*exp)
3013 g_free (exp);
3014 return 0;
3016 else
3018 if (edit_save_block (edit, exp, start_mark, end_mark))
3020 g_free (exp);
3021 edit->force |= REDRAW_COMPLETELY;
3022 return 1;
3024 else
3026 g_free (exp);
3027 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3031 edit->force |= REDRAW_COMPLETELY;
3032 return 0;
3036 /* --------------------------------------------------------------------------------------------- */
3038 /** returns TRUE on success */
3039 gboolean
3040 edit_insert_file_cmd (WEdit * edit)
3042 char *tmp;
3043 char *exp;
3044 gboolean ret = FALSE;
3046 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3047 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3048 MC_HISTORY_EDIT_INSERT_FILE, tmp);
3049 g_free (tmp);
3051 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3053 if (exp != NULL && *exp != '\0')
3055 vfs_path_t *exp_vpath;
3057 exp_vpath = vfs_path_from_str (exp);
3058 ret = (edit_insert_file (edit, exp_vpath) >= 0);
3059 vfs_path_free (exp_vpath);
3061 if (!ret)
3062 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3065 g_free (exp);
3067 edit->force |= REDRAW_COMPLETELY;
3068 return ret;
3071 /* --------------------------------------------------------------------------------------------- */
3072 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3075 edit_sort_cmd (WEdit * edit)
3077 static char *old = 0;
3078 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3079 long start_mark, end_mark;
3080 int e;
3082 if (eval_marks (edit, &start_mark, &end_mark))
3084 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3085 return 0;
3088 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3089 edit_save_block (edit, tmp, start_mark, end_mark);
3090 g_free (tmp);
3092 exp = input_dialog (_("Run sort"),
3093 _("Enter sort options (see manpage) separated by whitespace:"),
3094 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "");
3096 if (!exp)
3097 return 1;
3098 g_free (old);
3099 old = exp;
3100 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3101 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3102 tmp =
3103 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3104 " > ", tmp_edit_temp_name, (char *) NULL);
3105 g_free (tmp_edit_temp_name);
3106 g_free (tmp_edit_block_name);
3108 e = system (tmp);
3109 g_free (tmp);
3110 if (e)
3112 if (e == -1 || e == 127)
3114 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3116 else
3118 char q[8];
3119 sprintf (q, "%d ", e);
3120 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3121 edit_error_dialog (_("Sort"), tmp);
3122 g_free (tmp);
3124 return -1;
3127 edit->force |= REDRAW_COMPLETELY;
3129 if (edit_block_delete_cmd (edit))
3130 return 1;
3133 vfs_path_t *tmp_vpath;
3135 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3136 edit_insert_file (edit, tmp_vpath);
3137 vfs_path_free (tmp_vpath);
3139 return 0;
3142 /* --------------------------------------------------------------------------------------------- */
3144 * Ask user for a command, execute it and paste its output back to the
3145 * editor.
3149 edit_ext_cmd (WEdit * edit)
3151 char *exp, *tmp, *tmp_edit_temp_file;
3152 int e;
3154 exp =
3155 input_dialog (_("Paste output of external command"),
3156 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL);
3158 if (!exp)
3159 return 1;
3161 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3162 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3163 g_free (tmp_edit_temp_file);
3164 e = system (tmp);
3165 g_free (tmp);
3166 g_free (exp);
3168 if (e)
3170 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3171 return -1;
3174 edit->force |= REDRAW_COMPLETELY;
3177 vfs_path_t *tmp_vpath;
3179 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3180 edit_insert_file (edit, tmp_vpath);
3181 vfs_path_free (tmp_vpath);
3183 return 0;
3186 /* --------------------------------------------------------------------------------------------- */
3187 /** if block is 1, a block must be highlighted and the shell command
3188 processes it. If block is 0 the shell command is a straight system
3189 command, that just produces some output which is to be inserted */
3191 void
3192 edit_block_process_cmd (WEdit * edit, int macro_number)
3194 char *fname;
3195 char *macros_fname = NULL;
3197 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3198 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3199 user_menu (edit, macros_fname, 0);
3200 g_free (fname);
3201 g_free (macros_fname);
3202 edit->force |= REDRAW_COMPLETELY;
3205 /* --------------------------------------------------------------------------------------------- */
3207 void
3208 edit_mail_dialog (WEdit * edit)
3210 char *tmail_to;
3211 char *tmail_subject;
3212 char *tmail_cc;
3214 static char *mail_cc_last = 0;
3215 static char *mail_subject_last = 0;
3216 static char *mail_to_last = 0;
3218 QuickWidget quick_widgets[] = {
3219 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
3220 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
3221 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input", &tmail_cc),
3222 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT, N_("Copies to")),
3223 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-2",
3224 &tmail_subject),
3225 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT, N_("Subject")),
3226 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-3", &tmail_to),
3227 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT, N_("To")),
3228 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT, N_("mail -s <subject> -c <cc> <to>")),
3229 QUICK_END
3232 QuickDialog Quick_input = {
3233 50, MAIL_DLG_HEIGHT, -1, -1, N_("Mail"),
3234 "[Input Line Keys]", quick_widgets, NULL, FALSE
3237 quick_widgets[2].u.input.text = mail_cc_last ? mail_cc_last : "";
3238 quick_widgets[4].u.input.text = mail_subject_last ? mail_subject_last : "";
3239 quick_widgets[6].u.input.text = mail_to_last ? mail_to_last : "";
3241 if (quick_dialog (&Quick_input) != B_CANCEL)
3243 g_free (mail_cc_last);
3244 g_free (mail_subject_last);
3245 g_free (mail_to_last);
3246 mail_cc_last = tmail_cc;
3247 mail_subject_last = tmail_subject;
3248 mail_to_last = tmail_to;
3249 pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
3254 /*******************/
3255 /* Word Completion */
3256 /*******************/
3258 /* --------------------------------------------------------------------------------------------- */
3260 * Complete current word using regular expression search
3261 * backwards beginning at the current cursor position.
3264 void
3265 edit_complete_word_cmd (WEdit * edit)
3267 gsize i, max_len, word_len = 0, num_compl = 0;
3268 long word_start = 0;
3269 unsigned char *bufpos;
3270 char *match_expr;
3271 struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
3273 /* search start of word to be completed */
3274 if (!edit_find_word_start (edit, &word_start, &word_len))
3275 return;
3277 /* prepare match expression */
3278 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3280 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3281 match_expr =
3282 g_strdup_printf
3283 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3284 (int) word_len, bufpos);
3286 /* collect the possible completions */
3287 /* start search from begin to end of file */
3288 max_len =
3289 edit_collect_completions (edit, word_start, word_len, match_expr,
3290 (struct selection *) &compl, &num_compl);
3292 if (num_compl > 0)
3294 /* insert completed word if there is only one match */
3295 if (num_compl == 1)
3297 for (i = word_len; i < compl[0].len; i++)
3298 edit_insert (edit, *(compl[0].text + i));
3300 /* more than one possible completion => ask the user */
3301 else
3303 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3304 /* !!! pressed again the selection dialog pops up, but that !!! */
3305 /* !!! seems to require a further internal state !!! */
3306 /*tty_beep (); */
3308 /* let the user select the preferred completion */
3309 editcmd_dialog_completion_show (edit, max_len, word_len,
3310 (struct selection *) &compl, num_compl);
3314 g_free (match_expr);
3315 /* release memory before return */
3316 for (i = 0; i < num_compl; i++)
3317 g_free (compl[i].text);
3320 /* --------------------------------------------------------------------------------------------- */
3322 void
3323 edit_select_codepage_cmd (WEdit * edit)
3325 #ifdef HAVE_CHARSET
3326 if (do_select_codepage ())
3327 edit_set_codeset (edit);
3329 edit->force = REDRAW_COMPLETELY;
3330 edit_refresh_cmd (edit);
3331 #else
3332 (void) edit;
3333 #endif
3336 /* --------------------------------------------------------------------------------------------- */
3338 void
3339 edit_insert_literal_cmd (WEdit * edit)
3341 int char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3342 _("Press any key:"), 0);
3343 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3346 /* --------------------------------------------------------------------------------------------- */
3348 void
3349 edit_begin_end_macro_cmd (WEdit * edit)
3351 /* edit is a pointer to the widget */
3352 if (edit != NULL)
3354 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3355 edit_execute_key_command (edit, command, -1);
3359 /* --------------------------------------------------------------------------------------------- */
3361 void
3362 edit_begin_end_repeat_cmd (WEdit * edit)
3364 /* edit is a pointer to the widget */
3365 if (edit != NULL)
3367 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3368 edit_execute_key_command (edit, command, -1);
3372 /* --------------------------------------------------------------------------------------------- */
3374 gboolean
3375 edit_load_forward_cmd (WEdit * edit)
3377 if (edit->modified
3378 && edit_query_dialog2 (_("Warning"),
3379 _("Current text was modified without a file save.\n"
3380 "Continue discards these changes"), _("C&ontinue"),
3381 _("&Cancel")) == 1)
3383 edit->force |= REDRAW_COMPLETELY;
3384 return TRUE;
3387 if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
3388 return FALSE;
3390 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3391 return FALSE;
3393 edit_stack_iterator++;
3394 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3395 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3396 edit_history_moveto[edit_stack_iterator].line);
3398 return FALSE;
3401 /* --------------------------------------------------------------------------------------------- */
3403 gboolean
3404 edit_load_back_cmd (WEdit * edit)
3406 if (edit->modified
3407 && edit_query_dialog2 (_("Warning"),
3408 _("Current text was modified without a file save.\n"
3409 "Continue discards these changes"), _("C&ontinue"),
3410 _("&Cancel")) == 1)
3412 edit->force |= REDRAW_COMPLETELY;
3413 return TRUE;
3416 /* we are in the bottom of the stack, NO WAY! */
3417 if (edit_stack_iterator == 0)
3418 return FALSE;
3420 edit_stack_iterator--;
3421 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3422 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3423 edit_history_moveto[edit_stack_iterator].line);
3425 return FALSE;
3428 /* --------------------------------------------------------------------------------------------- */
3430 void
3431 edit_get_match_keyword_cmd (WEdit * edit)
3433 gsize word_len = 0, max_len = 0;
3434 int num_def = 0;
3435 int i;
3436 long word_start = 0;
3437 unsigned char *bufpos;
3438 char *match_expr;
3439 char *path = NULL;
3440 char *ptr = NULL;
3441 char *tagfile = NULL;
3443 etags_hash_t def_hash[MAX_DEFINITIONS];
3445 for (i = 0; i < MAX_DEFINITIONS; i++)
3447 def_hash[i].filename = NULL;
3450 /* search start of word to be completed */
3451 if (!edit_find_word_start (edit, &word_start, &word_len))
3452 return;
3454 /* prepare match expression */
3455 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3456 match_expr = g_strdup_printf ("%.*s", (int) word_len, bufpos);
3458 ptr = g_get_current_dir ();
3459 path = g_strconcat (ptr, G_DIR_SEPARATOR_S, (char *) NULL);
3460 g_free (ptr);
3462 /* Recursive search file 'TAGS' in parent dirs */
3465 ptr = g_path_get_dirname (path);
3466 g_free (path);
3467 path = ptr;
3468 g_free (tagfile);
3469 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3470 if (exist_file (tagfile))
3471 break;
3473 while (strcmp (path, G_DIR_SEPARATOR_S) != 0);
3475 if (tagfile)
3477 num_def =
3478 etags_set_definition_hash (tagfile, path, match_expr, (etags_hash_t *) & def_hash);
3479 g_free (tagfile);
3481 g_free (path);
3483 max_len = MAX_WIDTH_DEF_DIALOG;
3484 word_len = 0;
3485 if (num_def > 0)
3487 editcmd_dialog_select_definition_show (edit, match_expr, max_len, word_len,
3488 (etags_hash_t *) & def_hash, num_def);
3490 g_free (match_expr);
3493 /* --------------------------------------------------------------------------------------------- */