Ticket #1868: mcedit hang up on replace with regexp.
[midnight-commander.git] / src / editor / editcmd.c
blob01fd22e5dc4b3a160684037db6a691eb953552da
1 /*
2 Editor high level editing commands
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer, 1996, 1997
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /** \file
28 * \brief Source: editor high level editing commands
29 * \author Paul Sheer
30 * \date 1996, 1997
33 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
35 #include <config.h>
37 #ifdef HAVE_ASSERT_H
38 #include <assert.h>
39 #endif
40 #include <ctype.h>
42 #include <stdio.h>
43 #include <stdarg.h>
44 #include <sys/types.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include <errno.h>
48 #include <sys/stat.h>
49 #include <stdlib.h>
50 #include <fcntl.h>
52 #include "lib/global.h"
53 #include "lib/tty/tty.h"
54 #include "lib/tty/key.h" /* XCTRL */
55 #include "lib/mcconfig.h"
56 #include "lib/skin.h"
57 #include "lib/strutil.h" /* utf string functions */
58 #include "lib/lock.h"
59 #include "lib/util.h" /* tilde_expand() */
60 #include "lib/vfs/vfs.h"
61 #include "lib/widget.h"
62 #include "lib/charsets.h"
63 #include "lib/event.h" /* mc_event_raise() */
65 #include "src/history.h"
66 #include "src/setup.h" /* option_tab_spacing */
67 #include "src/main.h" /* mactos_t */
68 #include "src/selcodepage.h"
69 #include "src/keybind-defaults.h"
70 #include "src/util.h" /* check_for_default() */
71 #include "src/filemanager/layout.h" /* mc_refresh() */
73 #include "edit-impl.h"
74 #include "edit-widget.h"
75 #include "editcmd_dialogs.h"
76 #include "etags.h"
78 /*** global variables ****************************************************************************/
80 /* search and replace: */
81 int search_create_bookmark = FALSE;
83 /* queries on a save */
84 int edit_confirm_save = 1;
86 /*** file scope macro definitions ****************************************************************/
88 #define space_width 1
90 #define TEMP_BUF_LEN 1024
92 #define INPUT_INDEX 9
94 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
95 (and the above) routines to work properly - paul */
97 #define is_digit(x) ((x) >= '0' && (x) <= '9')
99 #define MAIL_DLG_HEIGHT 12
101 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
103 /*** file scope type declarations ****************************************************************/
105 /*** file scope variables ************************************************************************/
107 /*** file scope functions ************************************************************************/
108 /* --------------------------------------------------------------------------------------------- */
110 /* If 0 (quick save) then a) create/truncate <filename> file,
111 b) save to <filename>;
112 if 1 (safe save) then a) save to <tempnam>,
113 b) rename <tempnam> to <filename>;
114 if 2 (do backups) then a) save to <tempnam>,
115 b) rename <filename> to <filename.backup_ext>,
116 c) rename <tempnam> to <filename>. */
118 /* returns 0 on error, -1 on abort */
120 static int
121 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
123 char *p;
124 gchar *tmp;
125 long filelen = 0;
126 int this_save_mode, fd = -1;
127 vfs_path_t *real_filename_vpath;
128 vfs_path_t *savename_vpath = NULL;
129 const char *start_filename;
130 const vfs_path_element_t *vpath_element;
132 vpath_element = vfs_path_get_by_index (filename_vpath, 0);
133 if (vpath_element == NULL)
134 return 0;
136 start_filename = vpath_element->path;
137 if (*start_filename == '\0')
138 return 0;
140 if (*start_filename != PATH_SEP && edit->dir_vpath != NULL)
142 real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
144 else
146 real_filename_vpath = vfs_path_clone (filename_vpath);
149 this_save_mode = option_save_mode;
150 if (this_save_mode != EDIT_QUICK_SAVE)
152 if (!vfs_file_is_local (real_filename_vpath)
153 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
156 * The file does not exists yet, so no safe save or
157 * backup are necessary.
159 this_save_mode = EDIT_QUICK_SAVE;
161 if (fd != -1)
162 mc_close (fd);
165 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt)
167 int rv;
168 struct stat sb;
170 rv = mc_stat (real_filename_vpath, &sb);
171 if (rv == 0 && sb.st_nlink > 1)
173 rv = edit_query_dialog3 (_("Warning"),
174 _("File has hard-links. Detach before saving?"),
175 _("&Yes"), _("&No"), _("&Cancel"));
176 switch (rv)
178 case 0:
179 this_save_mode = EDIT_SAFE_SAVE;
180 /* fallthrough */
181 case 1:
182 edit->skip_detach_prompt = 1;
183 break;
184 default:
185 vfs_path_free (real_filename_vpath);
186 return -1;
190 /* Prevent overwriting changes from other editor sessions. */
191 if (rv == 0 && edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
193 /* The default action is "Cancel". */
194 query_set_sel (1);
196 rv = edit_query_dialog2 (_("Warning"),
197 _("The file has been modified in the meantime. Save anyway?"),
198 _("&Yes"), _("&Cancel"));
199 if (rv != 0)
201 vfs_path_free (real_filename_vpath);
202 return -1;
207 if (this_save_mode != EDIT_QUICK_SAVE)
209 char *savedir, *saveprefix;
211 savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
212 if (savedir == NULL)
213 savedir = g_strdup (".");
215 saveprefix = mc_build_filename (savedir, "cooledit", NULL);
216 g_free (savedir);
217 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
218 g_free (saveprefix);
219 if (savename_vpath == NULL)
221 vfs_path_free (real_filename_vpath);
222 return 0;
224 /* FIXME:
225 * Close for now because mc_mkstemps use pure open system call
226 * to create temporary file and it needs to be reopened by
227 * VFS-aware mc_open().
229 close (fd);
231 else
232 savename_vpath = vfs_path_clone (real_filename_vpath);
234 (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
235 (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
237 fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
238 if (fd == -1)
239 goto error_save;
241 /* pipe save */
242 p = edit_get_write_filter (savename_vpath, real_filename_vpath);
243 if (p != NULL)
245 FILE *file;
247 mc_close (fd);
248 file = (FILE *) popen (p, "w");
250 if (file)
252 filelen = edit_write_stream (edit, file);
253 #if 1
254 pclose (file);
255 #else
256 if (pclose (file) != 0)
258 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
259 edit_error_dialog (_("Error"), tmp);
260 g_free (tmp);
261 g_free (p);
262 goto error_save;
264 #endif
266 else
268 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
269 edit_error_dialog (_("Error"), get_sys_error (tmp));
270 g_free (p);
271 g_free (tmp);
272 goto error_save;
274 g_free (p);
276 else if (edit->lb == LB_ASIS)
277 { /* do not change line breaks */
278 long buf;
279 buf = 0;
280 filelen = edit->last_byte;
281 while (buf <= (edit->curs1 >> S_EDIT_BUF_SIZE) - 1)
283 if (mc_write (fd, (char *) edit->buffers1[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
285 mc_close (fd);
286 goto error_save;
288 buf++;
290 if (mc_write
291 (fd, (char *) edit->buffers1[buf],
292 edit->curs1 & M_EDIT_BUF_SIZE) != (edit->curs1 & M_EDIT_BUF_SIZE))
294 filelen = -1;
296 else if (edit->curs2)
298 edit->curs2--;
299 buf = (edit->curs2 >> S_EDIT_BUF_SIZE);
300 if (mc_write
301 (fd,
302 (char *) edit->buffers2[buf] + EDIT_BUF_SIZE -
303 (edit->curs2 & M_EDIT_BUF_SIZE) - 1,
304 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) != 1 + (edit->curs2 & M_EDIT_BUF_SIZE))
306 filelen = -1;
308 else
310 while (--buf >= 0)
312 if (mc_write (fd, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
314 filelen = -1;
315 break;
319 edit->curs2++;
321 if (mc_close (fd))
322 goto error_save;
324 /* Update the file information, especially the mtime. */
325 if (mc_stat (savename_vpath, &edit->stat1) == -1)
326 goto error_save;
328 else
329 { /* change line breaks */
330 FILE *file;
331 const vfs_path_element_t *path_element;
333 mc_close (fd);
335 path_element = vfs_path_get_by_index (savename_vpath, -1);
336 file = (FILE *) fopen (path_element->path, "w");
337 if (file != NULL)
339 filelen = edit_write_stream (edit, file);
340 fclose (file);
342 else
344 char *msg;
346 msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
347 edit_error_dialog (_("Error"), msg);
348 g_free (msg);
349 goto error_save;
353 if (filelen != edit->last_byte)
354 goto error_save;
356 if (this_save_mode == EDIT_DO_BACKUP)
358 vfs_path_t *tmp_vpath;
359 gboolean ok;
361 #ifdef HAVE_ASSERT_H
362 assert (option_backup_ext != NULL);
363 #endif
364 tmp_vpath = vfs_path_append_new (real_filename_vpath, option_backup_ext, (char *) NULL);
365 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
366 vfs_path_free (tmp_vpath);
367 if (!ok)
368 goto error_save;
371 if (this_save_mode != EDIT_QUICK_SAVE)
372 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
373 goto error_save;
375 vfs_path_free (real_filename_vpath);
376 vfs_path_free (savename_vpath);
377 return 1;
378 error_save:
379 /* FIXME: Is this safe ?
380 * if (this_save_mode != EDIT_QUICK_SAVE)
381 * mc_unlink (savename);
383 vfs_path_free (real_filename_vpath);
384 vfs_path_free (savename_vpath);
385 return 0;
388 /* --------------------------------------------------------------------------------------------- */
390 static gboolean
391 edit_check_newline (WEdit * edit)
393 return !(option_check_nl_at_eof && edit->last_byte > 0
394 && edit_get_byte (edit, edit->last_byte - 1) != '\n'
395 && edit_query_dialog2 (_("Warning"),
396 _("The file you are saving is not finished with a newline"),
397 _("C&ontinue"), _("&Cancel")));
400 /* --------------------------------------------------------------------------------------------- */
402 static vfs_path_t *
403 edit_get_save_file_as (WEdit * edit)
405 #define DLG_WIDTH 64
406 #define DLG_HEIGHT 14
408 static LineBreaks cur_lb = LB_ASIS;
410 char *filename = vfs_path_to_str (edit->filename_vpath);
411 char *filename_res;
413 const char *lb_names[LB_NAMES] = {
414 N_("&Do not change"),
415 N_("&Unix format (LF)"),
416 N_("&Windows/DOS format (CR LF)"),
417 N_("&Macintosh format (CR)")
420 QuickWidget quick_widgets[] = {
421 QUICK_BUTTON (6, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
422 QUICK_BUTTON (2, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
423 QUICK_RADIO (5, DLG_WIDTH, DLG_HEIGHT - 8, DLG_HEIGHT, LB_NAMES, lb_names, (int *) &cur_lb),
424 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 9, DLG_HEIGHT, N_("Change line breaks to:")),
425 QUICK_INPUT (3, DLG_WIDTH, DLG_HEIGHT - 11, DLG_HEIGHT, filename, DLG_WIDTH - 6, 0,
426 "save-as", &filename_res),
427 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 12, DLG_HEIGHT, N_("Enter file name:")),
428 QUICK_END
431 QuickDialog Quick_options = {
432 DLG_WIDTH, DLG_HEIGHT, -1, -1,
433 N_("Save As"), "[Save File As]",
434 quick_widgets, NULL, FALSE
437 if (quick_dialog (&Quick_options) != B_CANCEL)
439 char *fname;
440 vfs_path_t *ret_vpath;
442 edit->lb = cur_lb;
443 fname = tilde_expand (filename_res);
444 g_free (filename_res);
445 ret_vpath = vfs_path_from_str (fname);
446 g_free (fname);
447 return ret_vpath;
449 g_free (filename);
451 return NULL;
453 #undef DLG_WIDTH
454 #undef DLG_HEIGHT
457 /* --------------------------------------------------------------------------------------------- */
459 /** returns 1 on success */
461 static int
462 edit_save_cmd (WEdit * edit)
464 int res, save_lock = 0;
466 if (!edit->locked && !edit->delete_file)
467 save_lock = lock_file (edit->filename_vpath);
468 res = edit_save_file (edit, edit->filename_vpath);
470 /* Maintain modify (not save) lock on failure */
471 if ((res > 0 && edit->locked) || save_lock)
472 edit->locked = unlock_file (edit->filename_vpath);
474 /* On failure try 'save as', it does locking on its own */
475 if (!res)
476 return edit_save_as_cmd (edit);
477 edit->force |= REDRAW_COMPLETELY;
478 if (res > 0)
480 edit->delete_file = 0;
481 edit->modified = 0;
484 return 1;
487 /* --------------------------------------------------------------------------------------------- */
488 /** returns 1 on error */
490 static int
491 edit_load_file_from_filename (WEdit * edit, const vfs_path_t * exp_vpath)
493 int prev_locked = edit->locked;
494 vfs_path_t *prev_filename;
495 int ret = 0;
497 prev_filename = vfs_path_clone (edit->filename_vpath);
498 if (!edit_reload (edit, exp_vpath))
499 ret = 1;
500 else if (prev_locked)
501 unlock_file (prev_filename);
503 vfs_path_free (prev_filename);
504 return ret;
507 /* --------------------------------------------------------------------------------------------- */
509 static void
510 edit_load_syntax_file (WEdit * edit)
512 vfs_path_t *extdir_vpath;
513 int dir = 0;
515 if (geteuid () == 0)
517 dir = query_dialog (_("Syntax file edit"),
518 _("Which syntax file you want to edit?"), D_NORMAL, 2,
519 _("&User"), _("&System Wide"));
522 extdir_vpath =
523 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
524 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
526 vfs_path_free (extdir_vpath);
527 extdir_vpath =
528 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
531 if (dir == 0)
533 vfs_path_t *user_syntax_file_vpath;
535 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
536 check_for_default (extdir_vpath, user_syntax_file_vpath);
537 edit_load_file_from_filename (edit, user_syntax_file_vpath);
538 vfs_path_free (user_syntax_file_vpath);
540 else if (dir == 1)
541 edit_load_file_from_filename (edit, extdir_vpath);
543 vfs_path_free (extdir_vpath);
546 /* --------------------------------------------------------------------------------------------- */
548 static void
549 edit_load_menu_file (WEdit * edit)
551 vfs_path_t *buffer_vpath;
552 vfs_path_t *menufile_vpath;
553 int dir = 0;
555 dir = query_dialog (_("Menu edit"),
556 _("Which menu file do you want to edit?"), D_NORMAL,
557 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
559 menufile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
561 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
563 vfs_path_free (menufile_vpath);
564 menufile_vpath = vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
567 switch (dir)
569 case 0:
570 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
571 check_for_default (menufile_vpath, buffer_vpath);
572 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
573 break;
575 case 1:
576 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
577 check_for_default (menufile_vpath, buffer_vpath);
578 break;
580 case 2:
581 buffer_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
582 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
584 vfs_path_free (buffer_vpath);
585 buffer_vpath =
586 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
588 break;
590 default:
591 vfs_path_free (menufile_vpath);
592 return;
595 edit_load_file_from_filename (edit, buffer_vpath);
597 vfs_path_free (buffer_vpath);
598 vfs_path_free (menufile_vpath);
601 /* --------------------------------------------------------------------------------------------- */
603 static void
604 edit_delete_column_of_text (WEdit * edit)
606 long p, q, r, m1, m2;
607 long b, c, d, n;
609 eval_marks (edit, &m1, &m2);
610 n = edit_move_forward (edit, m1, 0, m2) + 1;
611 c = edit_move_forward3 (edit, edit_bol (edit, m1), 0, m1);
612 d = edit_move_forward3 (edit, edit_bol (edit, m2), 0, m2);
613 b = max (min (c, d), min (edit->column1, edit->column2));
614 c = max (c, max (edit->column1, edit->column2));
616 while (n--)
618 r = edit_bol (edit, edit->curs1);
619 p = edit_move_forward3 (edit, r, b, 0);
620 q = edit_move_forward3 (edit, r, c, 0);
621 if (p < m1)
622 p = m1;
623 if (q > m2)
624 q = m2;
625 edit_cursor_move (edit, p - edit->curs1);
626 while (q > p)
628 /* delete line between margins */
629 if (edit_get_byte (edit, edit->curs1) != '\n')
630 edit_delete (edit, 1);
631 q--;
633 if (n)
634 /* move to next line except on the last delete */
635 edit_cursor_move (edit, edit_move_forward (edit, edit->curs1, 1, 0) - edit->curs1);
639 /* --------------------------------------------------------------------------------------------- */
640 /** if success return 0 */
642 static int
643 edit_block_delete (WEdit * edit)
645 long count;
646 long start_mark, end_mark;
647 int curs_pos, line_width;
648 long curs_line, c1, c2;
650 if (eval_marks (edit, &start_mark, &end_mark))
651 return 0;
652 if (edit->column_highlight && edit->mark2 < 0)
653 edit_mark_cmd (edit, 0);
654 if ((end_mark - start_mark) > option_max_undo / 2)
656 /* Warning message with a query to continue or cancel the operation */
657 if (edit_query_dialog2
658 (_("Warning"),
660 ("Block is large, you may not be able to undo this action"),
661 _("C&ontinue"), _("&Cancel")))
663 return 1;
666 c1 = min (edit->column1, edit->column2);
667 c2 = max (edit->column1, edit->column2);
668 edit->column1 = c1;
669 edit->column2 = c2;
671 edit_push_markers (edit);
673 curs_line = edit->curs_line;
675 /* calculate line width and cursor position before cut */
676 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
677 edit_eol (edit, edit->curs1));
678 curs_pos = edit->curs_col + edit->over_col;
680 /* move cursor to start of selection */
681 edit_cursor_move (edit, start_mark - edit->curs1);
682 edit_scroll_screen_over_cursor (edit);
683 count = start_mark;
684 if (start_mark < end_mark)
686 if (edit->column_highlight)
688 if (edit->mark2 < 0)
689 edit_mark_cmd (edit, 0);
690 edit_delete_column_of_text (edit);
691 /* move cursor to the saved position */
692 edit_move_to_line (edit, curs_line);
693 /* calculate line width after cut */
694 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
695 edit_eol (edit, edit->curs1));
696 if (option_cursor_beyond_eol && curs_pos > line_width)
697 edit->over_col = curs_pos - line_width;
699 else
701 while (count < end_mark)
703 edit_delete (edit, 1);
704 count++;
708 edit_set_markers (edit, 0, 0, 0, 0);
709 edit->force |= REDRAW_PAGE;
710 return 0;
713 /* --------------------------------------------------------------------------------------------- */
715 * Get EOL symbol for searching.
717 * @param edit editor object
718 * @return EOL symbol
721 static inline char
722 edit_search_get_current_end_line_char (const WEdit * edit)
724 switch (edit->lb)
726 case LB_MAC:
727 return '\r';
728 default:
729 return '\n';
733 /* --------------------------------------------------------------------------------------------- */
735 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
737 * @param search search object
738 * @return result of checks.
741 static edit_search_line_t
742 edit_get_search_line_type (mc_search_t * search)
744 edit_search_line_t search_line_type = 0;
746 if (search->search_type != MC_SEARCH_T_REGEX)
747 return search_line_type;
749 if (*search->original == '^')
750 search_line_type |= AT_START_LINE;
752 if (search->original[search->original_len - 1] == '$')
753 search_line_type |= AT_END_LINE;
754 return search_line_type;
757 /* --------------------------------------------------------------------------------------------- */
759 * Calculating the start position of next line.
761 * @param edit editor object
762 * @param current_pos current position
763 * @param max_pos max position
764 * @param end_string_symbol end of line symbol
765 * @return start position of next line
768 static off_t
769 edit_calculate_start_of_next_line (WEdit * edit, off_t current_pos, off_t max_pos,
770 char end_string_symbol)
772 off_t i;
774 for (i = current_pos; i < max_pos; i++)
776 current_pos++;
777 if (edit_get_byte (edit, i) == end_string_symbol)
778 break;
781 return current_pos;
784 /* --------------------------------------------------------------------------------------------- */
786 * Calculating the end position of previous line.
788 * @param edit editor object
789 * @param current_pos current position
790 * @param end_string_symbol end of line symbol
791 * @return end position of previous line
794 static off_t
795 edit_calculate_end_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
797 off_t i;
799 for (i = current_pos - 1; i >= 0; i--)
800 if (edit_get_byte (edit, i) == end_string_symbol)
801 break;
803 return i;
806 /* --------------------------------------------------------------------------------------------- */
808 * Calculating the start position of previous line.
810 * @param edit editor object
811 * @param current_pos current position
812 * @param end_string_symbol end of line symbol
813 * @return start position of previous line
816 static inline off_t
817 edit_calculate_start_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
819 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
820 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
822 return (current_pos + 1);
825 /* --------------------------------------------------------------------------------------------- */
827 * Calculating the start position of current 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 current line
835 static inline off_t
836 edit_calculate_start_of_current_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);
840 return (current_pos + 1);
843 /* --------------------------------------------------------------------------------------------- */
845 * Fixing (if needed) search start position if 'only in selection' option present.
847 * @param edit editor object
850 static void
851 edit_search_fix_search_start_if_selection (WEdit * edit)
853 long start_mark = 0;
854 long end_mark = 0;
856 if (!edit_search_options.only_in_selection)
857 return;
859 if (eval_marks (edit, &start_mark, &end_mark) != 0)
860 return;
862 if (edit_search_options.backwards)
864 if (edit->search_start > end_mark || edit->search_start <= start_mark)
865 edit->search_start = end_mark;
867 else
869 if (edit->search_start < start_mark || edit->search_start >= end_mark)
870 edit->search_start = start_mark;
874 /* --------------------------------------------------------------------------------------------- */
876 static gboolean
877 editcmd_find (WEdit * edit, gsize * len)
879 off_t search_start = edit->search_start;
880 off_t search_end;
881 long start_mark = 0;
882 long end_mark = edit->last_byte;
883 int mark_res = 0;
884 char end_string_symbol;
886 end_string_symbol = edit_search_get_current_end_line_char (edit);
888 /* prepare for search */
889 if (edit_search_options.only_in_selection)
891 mark_res = eval_marks (edit, &start_mark, &end_mark);
892 if (mark_res != 0)
894 edit->search->error = MC_SEARCH_E_NOTFOUND;
895 edit->search->error_str = g_strdup (_("Search string not found"));
896 return FALSE;
899 /* fix the start and the end of search block positions */
900 if ((edit->search_line_type & AT_START_LINE) != 0
901 && (start_mark != 0 || edit_get_byte (edit, start_mark - 1) != end_string_symbol))
903 start_mark =
904 edit_calculate_start_of_next_line (edit, start_mark, edit->last_byte,
905 end_string_symbol);
907 if ((edit->search_line_type & AT_END_LINE) != 0
908 && (end_mark - 1 != edit->last_byte
909 || edit_get_byte (edit, end_mark) != end_string_symbol))
911 end_mark = edit_calculate_end_of_previous_line (edit, end_mark, end_string_symbol);
913 if (start_mark >= end_mark)
915 edit->search->error = MC_SEARCH_E_NOTFOUND;
916 edit->search->error_str = g_strdup (_("Search string not found"));
917 return FALSE;
920 else
922 if (edit_search_options.backwards)
923 end_mark = max (1, edit->curs1) - 1;
926 /* search */
927 if (edit_search_options.backwards)
929 /* backward search */
930 search_end = end_mark;
932 if ((edit->search_line_type & AT_START_LINE) != 0)
933 search_start =
934 edit_calculate_start_of_current_line (edit, search_start, end_string_symbol);
936 while ((int) search_start >= start_mark)
938 if (search_end > (off_t) (search_start + edit->search->original_len)
939 && mc_search_is_fixed_search_str (edit->search))
941 search_end = search_start + edit->search->original_len;
943 if (mc_search_run (edit->search, (void *) edit, search_start, search_end, len)
944 && edit->search->normal_offset == search_start)
946 return TRUE;
950 if ((edit->search_line_type & AT_START_LINE) != 0)
951 search_start =
952 edit_calculate_start_of_previous_line (edit, search_start, end_string_symbol);
953 else
954 search_start--;
956 edit->search->error_str = g_strdup (_("Search string not found"));
958 else
960 /* forward search */
961 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
962 search_start =
963 edit_calculate_start_of_next_line (edit, search_start, end_mark, end_string_symbol);
964 return mc_search_run (edit->search, (void *) edit, search_start, end_mark, len);
966 return FALSE;
969 /* --------------------------------------------------------------------------------------------- */
971 static char *
972 edit_replace_cmd__conv_to_display (char *str)
974 #ifdef HAVE_CHARSET
975 GString *tmp;
977 tmp = str_convert_to_display (str);
978 if (tmp != NULL)
980 if (tmp->len != 0)
981 return g_string_free (tmp, FALSE);
982 g_string_free (tmp, TRUE);
984 #endif
985 return g_strdup (str);
988 /* --------------------------------------------------------------------------------------------- */
990 static char *
991 edit_replace_cmd__conv_to_input (char *str)
993 #ifdef HAVE_CHARSET
994 GString *tmp;
996 tmp = str_convert_to_input (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 void
1010 edit_do_search (WEdit * edit)
1012 gsize len = 0;
1014 if (edit->search == NULL)
1015 edit->search_start = edit->curs1;
1017 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1019 if (search_create_bookmark)
1021 int found = 0, books = 0;
1022 long l = 0, l_last = -1;
1023 long q = 0;
1025 search_create_bookmark = FALSE;
1026 book_mark_flush (edit, -1);
1028 while (TRUE)
1030 if (!mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
1031 break;
1032 if (found == 0)
1033 edit->search_start = edit->search->normal_offset;
1034 found++;
1035 l += edit_count_lines (edit, q, edit->search->normal_offset);
1036 if (l != l_last)
1038 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
1039 books++;
1041 l_last = l;
1042 q = edit->search->normal_offset + 1;
1045 if (found == 0)
1046 edit_error_dialog (_("Search"), _("Search string not found"));
1047 else
1048 edit_cursor_move (edit, edit->search_start - edit->curs1);
1050 else
1052 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
1053 && edit_search_options.backwards)
1054 edit->search_start--;
1056 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
1057 && !edit_search_options.backwards)
1058 edit->search_start++;
1060 if (editcmd_find (edit, &len))
1062 edit->found_start = edit->search_start = edit->search->normal_offset;
1063 edit->found_len = len;
1064 edit->over_col = 0;
1065 edit_cursor_move (edit, edit->search_start - edit->curs1);
1066 edit_scroll_screen_over_cursor (edit);
1067 if (edit_search_options.backwards)
1068 edit->search_start--;
1069 else
1070 edit->search_start++;
1072 else
1074 edit->search_start = edit->curs1;
1075 if (edit->search->error_str != NULL)
1076 edit_error_dialog (_("Search"), edit->search->error_str);
1080 edit->force |= REDRAW_COMPLETELY;
1081 edit_scroll_screen_over_cursor (edit);
1084 /* --------------------------------------------------------------------------------------------- */
1086 static void
1087 edit_search (WEdit * edit)
1089 if (editcmd_dialog_search_show (edit))
1091 edit->search_line_type = edit_get_search_line_type (edit->search);
1092 edit_search_fix_search_start_if_selection (edit);
1093 edit_do_search (edit);
1097 /* --------------------------------------------------------------------------------------------- */
1098 /** Return a null terminated length of text. Result must be g_free'd */
1100 static unsigned char *
1101 edit_get_block (WEdit * edit, long start, long finish, int *l)
1103 unsigned char *s, *r;
1104 r = s = g_malloc0 (finish - start + 1);
1105 if (edit->column_highlight)
1107 *l = 0;
1108 /* copy from buffer, excluding chars that are out of the column 'margins' */
1109 while (start < finish)
1111 int c;
1112 long x;
1113 x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
1114 c = edit_get_byte (edit, start);
1115 if ((x >= edit->column1 && x < edit->column2)
1116 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1118 *s++ = c;
1119 (*l)++;
1121 start++;
1124 else
1126 *l = finish - start;
1127 while (start < finish)
1128 *s++ = edit_get_byte (edit, start++);
1130 *s = 0;
1131 return r;
1134 /* --------------------------------------------------------------------------------------------- */
1135 /** copies a block to clipboard file */
1137 static int
1138 edit_save_block_to_clip_file (WEdit * edit, long start, long finish)
1140 int ret;
1141 gchar *tmp;
1142 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1143 ret = edit_save_block (edit, tmp, start, finish);
1144 g_free (tmp);
1145 return ret;
1148 /* --------------------------------------------------------------------------------------------- */
1150 static void
1151 pipe_mail (WEdit * edit, char *to, char *subject, char *cc)
1153 FILE *p = 0;
1154 char *s;
1156 to = name_quote (to, 0);
1157 subject = name_quote (subject, 0);
1158 cc = name_quote (cc, 0);
1159 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1160 g_free (to);
1161 g_free (subject);
1162 g_free (cc);
1164 if (s)
1166 p = popen (s, "w");
1167 g_free (s);
1170 if (p)
1172 long i;
1173 for (i = 0; i < edit->last_byte; i++)
1174 fputc (edit_get_byte (edit, i), p);
1175 pclose (p);
1179 /* --------------------------------------------------------------------------------------------- */
1181 static gboolean
1182 is_break_char (char c)
1184 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
1187 /* --------------------------------------------------------------------------------------------- */
1188 /** find first character of current word */
1190 static int
1191 edit_find_word_start (WEdit * edit, long *word_start, gsize * word_len)
1193 int c, last;
1194 gsize i;
1196 /* return if at begin of file */
1197 if (edit->curs1 <= 0)
1198 return 0;
1200 c = (unsigned char) edit_get_byte (edit, edit->curs1 - 1);
1201 /* return if not at end or in word */
1202 if (is_break_char (c))
1203 return 0;
1205 /* search start of word to be completed */
1206 for (i = 2;; i++)
1208 /* return if at begin of file */
1209 if ((gsize) edit->curs1 < i)
1210 return 0;
1212 last = c;
1213 c = (unsigned char) edit_get_byte (edit, edit->curs1 - i);
1215 if (is_break_char (c))
1217 /* return if word starts with digit */
1218 if (isdigit (last))
1219 return 0;
1221 *word_start = edit->curs1 - (i - 1); /* start found */
1222 *word_len = i - 1;
1223 break;
1226 /* success */
1227 return 1;
1230 /* --------------------------------------------------------------------------------------------- */
1232 * Get current word under cursor
1234 * @param edit editor object
1235 * @param srch mc_search object
1236 * @param word_start start word position
1238 * @return newly allocated string or NULL if no any words under cursor
1241 static char *
1242 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, long word_start)
1244 gsize len = 0, i;
1245 GString *temp;
1247 if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len))
1248 return NULL;
1250 temp = g_string_sized_new (len);
1252 for (i = 0; i < len; i++)
1254 int chr;
1256 chr = edit_get_byte (edit, word_start + i);
1257 if (!isspace (chr))
1258 g_string_append_c (temp, chr);
1261 return g_string_free (temp, temp->len == 0);
1264 /* --------------------------------------------------------------------------------------------- */
1265 /** collect the possible completions */
1267 static gsize
1268 edit_collect_completions (WEdit * edit, long word_start, gsize word_len,
1269 char *match_expr, struct selection *compl, gsize * num)
1271 gsize len = 0;
1272 gsize max_len = 0;
1273 gsize i;
1274 int skip;
1275 GString *temp;
1276 mc_search_t *srch;
1277 long last_byte, start = -1;
1278 char *current_word;
1280 srch = mc_search_new (match_expr, -1);
1281 if (srch == NULL)
1282 return 0;
1284 if (mc_config_get_bool
1285 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1287 last_byte = edit->last_byte;
1289 else
1291 last_byte = word_start;
1294 srch->search_type = MC_SEARCH_T_REGEX;
1295 srch->is_case_sensitive = TRUE;
1296 srch->search_fn = edit_search_cmd_callback;
1298 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1300 temp = g_string_new ("");
1302 /* collect max MAX_WORD_COMPLETIONS completions */
1303 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1305 g_string_set_size (temp, 0);
1306 start = srch->normal_offset;
1308 /* add matched completion if not yet added */
1309 for (i = 0; i < len; i++)
1311 skip = edit_get_byte (edit, start + i);
1312 if (isspace (skip))
1313 continue;
1315 /* skip current word */
1316 if (start + (long) i == word_start)
1317 break;
1319 g_string_append_c (temp, skip);
1322 if (temp->len == 0)
1323 continue;
1325 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1326 continue;
1328 skip = 0;
1330 for (i = 0; i < *num; i++)
1332 if (strncmp
1333 ((char *) &compl[i].text[word_len],
1334 (char *) &temp->str[word_len], max (len, compl[i].len) - word_len) == 0)
1336 struct selection this = compl[i];
1337 for (++i; i < *num; i++)
1339 compl[i - 1] = compl[i];
1341 compl[*num - 1] = this;
1342 skip = 1;
1343 break; /* skip it, already added */
1346 if (skip != 0)
1347 continue;
1349 if (*num == MAX_WORD_COMPLETIONS)
1351 g_free (compl[0].text);
1352 for (i = 1; i < *num; i++)
1354 compl[i - 1] = compl[i];
1356 (*num)--;
1358 #ifdef HAVE_CHARSET
1360 GString *recoded;
1361 recoded = str_convert_to_display (temp->str);
1363 if (recoded && recoded->len)
1364 g_string_assign (temp, recoded->str);
1366 g_string_free (recoded, TRUE);
1368 #endif
1369 compl[*num].text = g_strdup (temp->str);
1370 compl[*num].len = temp->len;
1371 (*num)++;
1372 start += len;
1374 /* note the maximal length needed for the completion dialog */
1375 if (len > max_len)
1376 max_len = len;
1379 mc_search_free (srch);
1380 g_string_free (temp, TRUE);
1381 g_free (current_word);
1383 return max_len;
1386 /* --------------------------------------------------------------------------------------------- */
1388 static void
1389 edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width,
1390 long *start_pos, long *end_pos, int *col1, int *col2)
1392 long cursor;
1393 int i, col;
1395 cursor = edit->curs1;
1396 col = edit_get_col (edit);
1398 for (i = 0; i < size; i++)
1400 if (data[i] != '\n')
1401 edit_insert (edit, data[i]);
1402 else
1403 { /* fill in and move to next line */
1404 int l;
1405 long p;
1407 if (edit_get_byte (edit, edit->curs1) != '\n')
1409 l = width - (edit_get_col (edit) - col);
1410 while (l > 0)
1412 edit_insert (edit, ' ');
1413 l -= space_width;
1416 for (p = edit->curs1;; p++)
1418 if (p == edit->last_byte)
1420 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1421 edit_insert_ahead (edit, '\n');
1422 p++;
1423 break;
1425 if (edit_get_byte (edit, p) == '\n')
1427 p++;
1428 break;
1431 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1432 l = col - edit_get_col (edit);
1433 while (l >= space_width)
1435 edit_insert (edit, ' ');
1436 l -= space_width;
1441 *col1 = col;
1442 *col2 = col + width;
1443 *start_pos = cursor;
1444 *end_pos = edit->curs1;
1445 edit_cursor_move (edit, cursor - edit->curs1);
1448 /* --------------------------------------------------------------------------------------------- */
1450 static int
1451 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1453 const macros_t *m1 = (const macros_t *) macro1;
1454 const macros_t *m2 = (const macros_t *) macro2;
1456 return m1->hotkey - m2->hotkey;
1459 /* --------------------------------------------------------------------------------------------- */
1461 static void
1462 edit_macro_sort_by_hotkey (void)
1464 if (macros_list != NULL && macros_list->len != 0)
1465 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1468 /* --------------------------------------------------------------------------------------------- */
1470 static gboolean
1471 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1473 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1474 macros_t *result;
1475 macros_t search_macro;
1477 (void) edit;
1479 search_macro.hotkey = hotkey;
1480 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1481 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1483 if (result != NULL && result->macro != NULL)
1485 *indx = (result - array_start);
1486 *macros = result;
1487 return TRUE;
1489 *indx = 0;
1490 return FALSE;
1493 /* --------------------------------------------------------------------------------------------- */
1494 /** returns FALSE on error */
1496 static gboolean
1497 edit_delete_macro (WEdit * edit, int hotkey)
1499 mc_config_t *macros_config = NULL;
1500 const char *section_name = "editor";
1501 gchar *macros_fname;
1502 guint indx;
1503 char *skeyname;
1504 const macros_t *macros = NULL;
1506 /* clear array of actions for current hotkey */
1507 while (edit_get_macro (edit, hotkey, &macros, &indx))
1509 if (macros->macro != NULL)
1510 g_array_free (macros->macro, TRUE);
1511 macros = NULL;
1512 g_array_remove_index (macros_list, indx);
1513 edit_macro_sort_by_hotkey ();
1516 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1517 macros_config = mc_config_init (macros_fname);
1518 g_free (macros_fname);
1520 if (macros_config == NULL)
1521 return FALSE;
1523 skeyname = lookup_key_by_code (hotkey);
1524 while (mc_config_del_key (macros_config, section_name, skeyname))
1526 g_free (skeyname);
1527 mc_config_save_file (macros_config, NULL);
1528 mc_config_deinit (macros_config);
1529 return TRUE;
1533 /* --------------------------------------------------------------------------------------------- */
1534 /*** public functions ****************************************************************************/
1535 /* --------------------------------------------------------------------------------------------- */
1537 void
1538 edit_help_cmd (WEdit * edit)
1540 ev_help_t event_data = { NULL, "[Internal File Editor]" };
1541 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
1543 edit->force |= REDRAW_COMPLETELY;
1546 /* --------------------------------------------------------------------------------------------- */
1548 void
1549 edit_refresh_cmd (WEdit * edit)
1551 #ifdef HAVE_SLANG
1552 int color;
1554 edit_get_syntax_color (edit, -1, &color);
1555 tty_touch_screen ();
1556 mc_refresh ();
1557 #else
1558 (void) edit;
1560 clr_scr ();
1561 repaint_screen ();
1562 #endif /* !HAVE_SLANG */
1563 tty_keypad (TRUE);
1566 /* --------------------------------------------------------------------------------------------- */
1568 void
1569 menu_save_mode_cmd (void)
1571 /* diaog sizes */
1572 const int DLG_X = 38;
1573 const int DLG_Y = 13;
1575 char *str_result;
1577 const char *str[] = {
1578 N_("&Quick save"),
1579 N_("&Safe save"),
1580 N_("&Do backups with following extension:")
1583 QuickWidget widgets[] = {
1584 /* 0 */
1585 QUICK_BUTTON (18, DLG_X, DLG_Y - 3, DLG_Y, N_("&Cancel"), B_CANCEL, NULL),
1586 /* 1 */
1587 QUICK_BUTTON (6, DLG_X, DLG_Y - 3, DLG_Y, N_("&OK"), B_ENTER, NULL),
1588 /* 2 */
1589 QUICK_CHECKBOX (4, DLG_X, 8, DLG_Y, N_("Check &POSIX new line"), &option_check_nl_at_eof),
1590 /* 3 */
1591 QUICK_INPUT (8, DLG_X, 6, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result),
1592 /* 4 */
1593 QUICK_RADIO (4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode),
1594 QUICK_END
1597 QuickDialog dialog = {
1598 DLG_X, DLG_Y, -1, -1, N_("Edit Save Mode"),
1599 "[Edit Save Mode]", widgets, NULL, FALSE
1602 size_t i;
1603 size_t maxlen = 0;
1604 size_t w0, w1, b_len, w3;
1606 #ifdef HAVE_ASSERT_H
1607 assert (option_backup_ext != NULL);
1608 #endif
1610 /* OK/Cancel buttons */
1611 w0 = str_term_width1 (_(widgets[0].u.button.text)) + 3;
1612 w1 = str_term_width1 (_(widgets[1].u.button.text)) + 5; /* default button */
1613 b_len = w0 + w1 + 3;
1615 maxlen = max (b_len, (size_t) str_term_width1 (_(dialog.title)) + 2);
1617 w3 = 0;
1618 for (i = 0; i < 3; i++)
1620 #ifdef ENABLE_NLS
1621 str[i] = _(str[i]);
1622 #endif
1623 w3 = max (w3, (size_t) str_term_width1 (str[i]));
1626 maxlen = max (maxlen, w3 + 4);
1628 dialog.xlen = min ((size_t) COLS, maxlen + 8);
1630 widgets[3].u.input.len = w3;
1631 widgets[1].relative_x = (dialog.xlen - b_len) / 2;
1632 widgets[0].relative_x = widgets[1].relative_x + w0 + 2;
1634 for (i = 0; i < sizeof (widgets) / sizeof (widgets[0]); i++)
1635 widgets[i].x_divisions = dialog.xlen;
1637 if (quick_dialog (&dialog) != B_CANCEL)
1639 g_free (option_backup_ext);
1640 option_backup_ext = str_result;
1644 /* --------------------------------------------------------------------------------------------- */
1646 void
1647 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1649 vfs_path_free (edit->filename_vpath);
1650 edit->filename_vpath = vfs_path_clone (name_vpath);
1652 if (edit->dir_vpath == NULL)
1653 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1656 /* --------------------------------------------------------------------------------------------- */
1657 /* Here we want to warn the users of overwriting an existing file,
1658 but only if they have made a change to the filename */
1659 /* returns 1 on success */
1661 edit_save_as_cmd (WEdit * edit)
1663 /* This heads the 'Save As' dialog box */
1664 vfs_path_t *exp_vpath;
1665 int save_lock = 0;
1666 int different_filename = 0;
1668 if (!edit_check_newline (edit))
1669 return 0;
1671 exp_vpath = edit_get_save_file_as (edit);
1672 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1674 if (exp_vpath != NULL)
1676 if (vfs_path_len (exp_vpath) == 0)
1677 goto ret;
1678 else
1680 int rv;
1682 if (vfs_path_cmp (edit->filename_vpath, exp_vpath) != 0)
1684 int file;
1685 struct stat sb;
1687 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1689 edit_error_dialog (_("Save as"),
1690 get_sys_error (_
1691 ("Cannot save: destination is not a regular file")));
1692 goto ret;
1695 different_filename = 1;
1696 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1698 if (file != -1)
1700 /* the file exists */
1701 mc_close (file);
1702 /* Overwrite the current file or cancel the operation */
1703 if (edit_query_dialog2
1704 (_("Warning"),
1705 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1706 goto ret;
1708 else
1710 edit->stat1.st_mode |= S_IWUSR;
1712 save_lock = lock_file (exp_vpath);
1714 else
1716 /* filenames equal, check if already locked */
1717 if (!edit->locked && !edit->delete_file)
1718 save_lock = lock_file (exp_vpath);
1721 if (different_filename)
1724 * Allow user to write into saved (under another name) file
1725 * even if original file had r/o user permissions.
1727 edit->stat1.st_mode |= S_IWRITE;
1730 rv = edit_save_file (edit, exp_vpath);
1731 switch (rv)
1733 case 1:
1734 /* Succesful, so unlock both files */
1735 if (different_filename)
1737 if (save_lock)
1738 unlock_file (exp_vpath);
1739 if (edit->locked)
1740 edit->locked = unlock_file (edit->filename_vpath);
1742 else
1744 if (edit->locked || save_lock)
1745 edit->locked = unlock_file (edit->filename_vpath);
1748 edit_set_filename (edit, exp_vpath);
1749 if (edit->lb != LB_ASIS)
1750 edit_reload (edit, exp_vpath);
1751 edit->modified = 0;
1752 edit->delete_file = 0;
1753 if (different_filename)
1754 edit_load_syntax (edit, NULL, edit->syntax_type);
1755 vfs_path_free (exp_vpath);
1756 edit->force |= REDRAW_COMPLETELY;
1757 return 1;
1758 default:
1759 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1760 /* fallthrough */
1761 case -1:
1762 /* Failed, so maintain modify (not save) lock */
1763 if (save_lock)
1764 unlock_file (exp_vpath);
1765 break;
1770 ret:
1771 vfs_path_free (exp_vpath);
1772 edit->force |= REDRAW_COMPLETELY;
1773 return 0;
1776 /* {{{ Macro stuff starts here */
1777 /* --------------------------------------------------------------------------------------------- */
1779 void
1780 edit_delete_macro_cmd (WEdit * edit)
1782 int hotkey;
1784 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1786 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1787 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1790 /* --------------------------------------------------------------------------------------------- */
1792 /** returns FALSE on error */
1793 gboolean
1794 edit_execute_macro (WEdit * edit, int hotkey)
1796 gboolean res = FALSE;
1798 if (hotkey != 0)
1800 const macros_t *macros;
1801 guint indx;
1803 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1804 macros->macro != NULL && macros->macro->len != 0)
1806 guint i;
1808 edit->force |= REDRAW_PAGE;
1810 for (i = 0; i < macros->macro->len; i++)
1812 const macro_action_t *m_act;
1814 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1815 edit_execute_cmd (edit, m_act->action, m_act->ch);
1816 res = TRUE;
1821 return res;
1824 /* --------------------------------------------------------------------------------------------- */
1826 /** returns FALSE on error */
1827 gboolean
1828 edit_store_macro_cmd (WEdit * edit)
1830 int i;
1831 int hotkey;
1832 GString *marcros_string;
1833 mc_config_t *macros_config = NULL;
1834 const char *section_name = "editor";
1835 gchar *macros_fname;
1836 GArray *macros; /* current macro */
1837 int tmp_act;
1838 gboolean have_macro = FALSE;
1839 char *skeyname = NULL;
1841 hotkey = editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1842 if (hotkey == ESC_CHAR)
1843 return FALSE;
1845 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1847 /* return FALSE if try assign macro into restricted hotkeys */
1848 if (tmp_act == CK_MacroStartRecord
1849 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1850 return FALSE;
1852 edit_delete_macro (edit, hotkey);
1854 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1855 macros_config = mc_config_init (macros_fname);
1856 g_free (macros_fname);
1858 if (macros_config == NULL)
1859 return FALSE;
1861 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1863 marcros_string = g_string_sized_new (250);
1864 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1866 skeyname = lookup_key_by_code (hotkey);
1868 for (i = 0; i < macro_index; i++)
1870 macro_action_t m_act;
1871 const char *action_name;
1873 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1875 if (action_name == NULL)
1876 break;
1878 m_act.action = record_macro_buf[i].action;
1879 m_act.ch = record_macro_buf[i].ch;
1880 g_array_append_val (macros, m_act);
1881 have_macro = TRUE;
1882 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1883 (int) record_macro_buf[i].ch);
1885 if (have_macro)
1887 macros_t macro;
1888 macro.hotkey = hotkey;
1889 macro.macro = macros;
1890 g_array_append_val (macros_list, macro);
1891 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1893 else
1894 mc_config_del_key (macros_config, section_name, skeyname);
1896 g_free (skeyname);
1897 edit_macro_sort_by_hotkey ();
1899 g_string_free (marcros_string, TRUE);
1900 mc_config_save_file (macros_config, NULL);
1901 mc_config_deinit (macros_config);
1902 return TRUE;
1905 /* --------------------------------------------------------------------------------------------- */
1907 gboolean
1908 edit_repeat_macro_cmd (WEdit * edit)
1910 int i, j;
1911 char *f;
1912 long count_repeat;
1913 char *error = NULL;
1915 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL);
1916 if (f == NULL || *f == '\0')
1918 g_free (f);
1919 return FALSE;
1922 count_repeat = strtol (f, &error, 0);
1924 if (*error != '\0')
1926 g_free (f);
1927 return FALSE;
1930 g_free (f);
1932 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1933 edit->force |= REDRAW_PAGE;
1935 for (j = 0; j < count_repeat; j++)
1936 for (i = 0; i < macro_index; i++)
1937 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1938 edit_update_screen (edit);
1939 return TRUE;
1942 /* --------------------------------------------------------------------------------------------- */
1943 /** return FALSE on error */
1945 gboolean
1946 edit_load_macro_cmd (WEdit * edit)
1948 mc_config_t *macros_config = NULL;
1949 gchar **profile_keys, **keys;
1950 gchar **values, **curr_values;
1951 gsize len, values_len;
1952 const char *section_name = "editor";
1953 gchar *macros_fname;
1954 int hotkey;
1956 (void) edit;
1958 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1959 macros_config = mc_config_init (macros_fname);
1960 g_free (macros_fname);
1962 if (macros_config == NULL)
1963 return FALSE;
1965 profile_keys = keys = mc_config_get_keys (macros_config, section_name, &len);
1966 while (*profile_keys != NULL)
1968 gboolean have_macro;
1969 GArray *macros;
1970 macros_t macro;
1972 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1974 curr_values = values = mc_config_get_string_list (macros_config, section_name,
1975 *profile_keys, &values_len);
1976 hotkey = lookup_key (*profile_keys, NULL);
1977 have_macro = FALSE;
1979 while (*curr_values != NULL && *curr_values[0] != '\0')
1981 char **macro_pair = NULL;
1983 macro_pair = g_strsplit (*curr_values, ":", 2);
1985 if (macro_pair != NULL)
1987 macro_action_t m_act;
1988 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
1989 m_act.action = 0;
1990 else
1992 m_act.action = keybind_lookup_action (macro_pair[0]);
1993 g_free (macro_pair[0]);
1994 macro_pair[0] = NULL;
1996 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
1997 m_act.ch = -1;
1998 else
2000 m_act.ch = strtol (macro_pair[1], NULL, 0);
2001 g_free (macro_pair[1]);
2002 macro_pair[1] = NULL;
2004 if (m_act.action != 0)
2006 /* a shell command */
2007 if ((m_act.action / CK_PipeBlock (0)) == 1)
2009 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
2010 m_act.ch = -1;
2012 g_array_append_val (macros, m_act);
2013 have_macro = TRUE;
2015 g_strfreev (macro_pair);
2016 macro_pair = NULL;
2018 curr_values++;
2020 if (have_macro)
2022 macro.hotkey = hotkey;
2023 macro.macro = macros;
2024 g_array_append_val (macros_list, macro);
2026 profile_keys++;
2027 g_strfreev (values);
2029 g_strfreev (keys);
2030 mc_config_deinit (macros_config);
2031 edit_macro_sort_by_hotkey ();
2032 return TRUE;
2035 /* }}} Macro stuff end here */
2037 /* --------------------------------------------------------------------------------------------- */
2038 /** returns 1 on success */
2041 edit_save_confirm_cmd (WEdit * edit)
2043 gchar *f = NULL;
2045 if (edit->filename_vpath == NULL)
2046 return edit_save_as_cmd (edit);
2048 if (!edit_check_newline (edit))
2049 return 0;
2051 if (edit_confirm_save)
2053 char *filename;
2054 gboolean ok;
2056 filename = vfs_path_to_str (edit->filename_vpath);
2057 f = g_strdup_printf (_("Confirm save file: \"%s\""), filename);
2058 g_free (filename);
2059 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2060 g_free (f);
2061 if (!ok)
2062 return 0;
2064 return edit_save_cmd (edit);
2067 /* --------------------------------------------------------------------------------------------- */
2068 /** returns 1 on success */
2071 edit_new_cmd (WEdit * edit)
2073 if (edit->modified)
2075 if (edit_query_dialog2
2076 (_("Warning"),
2078 ("Current text was modified without a file save.\nContinue discards these changes"),
2079 _("C&ontinue"), _("&Cancel")))
2081 edit->force |= REDRAW_COMPLETELY;
2082 return 0;
2085 edit->force |= REDRAW_COMPLETELY;
2087 return edit_renew (edit); /* if this gives an error, something has really screwed up */
2090 /* --------------------------------------------------------------------------------------------- */
2093 edit_load_cmd (WEdit * edit, edit_current_file_t what)
2095 if (edit->modified
2096 && (edit_query_dialog2
2097 (_("Warning"),
2098 _("Current text was modified without a file save.\n"
2099 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")) == 1))
2101 edit->force |= REDRAW_COMPLETELY;
2102 return 0;
2105 switch (what)
2107 case EDIT_FILE_COMMON:
2109 char *filename, *exp;
2111 filename = vfs_path_to_str (edit->filename_vpath);
2112 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2113 MC_HISTORY_EDIT_LOAD, filename);
2114 g_free (filename);
2116 if (exp != NULL)
2118 if (*exp != '\0')
2120 vfs_path_t *exp_vpath;
2122 exp_vpath = vfs_path_from_str (exp);
2123 edit_load_file_from_filename (edit, exp_vpath);
2124 vfs_path_free (exp_vpath);
2126 g_free (exp);
2129 break;
2131 case EDIT_FILE_SYNTAX:
2132 edit_load_syntax_file (edit);
2133 break;
2135 case EDIT_FILE_MENU:
2136 edit_load_menu_file (edit);
2137 break;
2139 default:
2140 break;
2143 edit->force |= REDRAW_COMPLETELY;
2144 return 0;
2147 /* --------------------------------------------------------------------------------------------- */
2149 if mark2 is -1 then marking is from mark1 to the cursor.
2150 Otherwise its between the markers. This handles this.
2151 Returns 1 if no text is marked.
2155 eval_marks (WEdit * edit, long *start_mark, long *end_mark)
2157 if (edit->mark1 != edit->mark2)
2159 long start_bol, start_eol;
2160 long end_bol, end_eol;
2161 long col1, col2;
2162 long diff1, diff2;
2163 long end_mark_curs;
2165 if (edit->end_mark_curs < 0)
2166 end_mark_curs = edit->curs1;
2167 else
2168 end_mark_curs = edit->end_mark_curs;
2170 if (edit->mark2 >= 0)
2172 *start_mark = min (edit->mark1, edit->mark2);
2173 *end_mark = max (edit->mark1, edit->mark2);
2175 else
2177 *start_mark = min (edit->mark1, end_mark_curs);
2178 *end_mark = max (edit->mark1, end_mark_curs);
2179 edit->column2 = edit->curs_col + edit->over_col;
2182 if (edit->column_highlight
2183 && (((edit->mark1 > end_mark_curs) && (edit->column1 < edit->column2))
2184 || ((edit->mark1 < end_mark_curs) && (edit->column1 > edit->column2))))
2186 start_bol = edit_bol (edit, *start_mark);
2187 start_eol = edit_eol (edit, start_bol - 1) + 1;
2188 end_bol = edit_bol (edit, *end_mark);
2189 end_eol = edit_eol (edit, *end_mark);
2190 col1 = min (edit->column1, edit->column2);
2191 col2 = max (edit->column1, edit->column2);
2193 diff1 =
2194 edit_move_forward3 (edit, start_bol, col2, 0) - edit_move_forward3 (edit, start_bol,
2195 col1, 0);
2196 diff2 =
2197 edit_move_forward3 (edit, end_bol, col2, 0) - edit_move_forward3 (edit, end_bol,
2198 col1, 0);
2200 *start_mark -= diff1;
2201 *end_mark += diff2;
2202 *start_mark = max (*start_mark, start_eol);
2203 *end_mark = min (*end_mark, end_eol);
2205 return 0;
2207 else
2209 *start_mark = *end_mark = 0;
2210 edit->column2 = edit->column1 = 0;
2211 return 1;
2215 /* --------------------------------------------------------------------------------------------- */
2217 void
2218 edit_insert_over (WEdit * edit)
2220 int i;
2222 for (i = 0; i < edit->over_col; i++)
2224 edit_insert (edit, ' ');
2226 edit->over_col = 0;
2229 /* --------------------------------------------------------------------------------------------- */
2232 edit_insert_column_of_text_from_file (WEdit * edit, int file,
2233 long *start_pos, long *end_pos, int *col1, int *col2)
2235 long cursor;
2236 int col;
2237 int blocklen = -1, width = 0;
2238 unsigned char *data;
2240 cursor = edit->curs1;
2241 col = edit_get_col (edit);
2242 data = g_malloc0 (TEMP_BUF_LEN);
2244 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
2246 int i;
2247 for (width = 0; width < blocklen; width++)
2249 if (data[width] == '\n')
2250 break;
2252 for (i = 0; i < blocklen; i++)
2254 if (data[i] == '\n')
2255 { /* fill in and move to next line */
2256 int l;
2257 long p;
2258 if (edit_get_byte (edit, edit->curs1) != '\n')
2260 l = width - (edit_get_col (edit) - col);
2261 while (l > 0)
2263 edit_insert (edit, ' ');
2264 l -= space_width;
2267 for (p = edit->curs1;; p++)
2269 if (p == edit->last_byte)
2271 edit_cursor_move (edit, edit->last_byte - edit->curs1);
2272 edit_insert_ahead (edit, '\n');
2273 p++;
2274 break;
2276 if (edit_get_byte (edit, p) == '\n')
2278 p++;
2279 break;
2282 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
2283 l = col - edit_get_col (edit);
2284 while (l >= space_width)
2286 edit_insert (edit, ' ');
2287 l -= space_width;
2289 continue;
2291 edit_insert (edit, data[i]);
2294 *col1 = col;
2295 *col2 = col + width;
2296 *start_pos = cursor;
2297 *end_pos = edit->curs1;
2298 edit_cursor_move (edit, cursor - edit->curs1);
2299 g_free (data);
2301 return blocklen;
2304 /* --------------------------------------------------------------------------------------------- */
2306 void
2307 edit_block_copy_cmd (WEdit * edit)
2309 long start_mark, end_mark, current = edit->curs1;
2310 long col_delta = 0;
2311 long mark1, mark2;
2312 int c1, c2;
2313 int size;
2314 unsigned char *copy_buf;
2316 edit_update_curs_col (edit);
2317 if (eval_marks (edit, &start_mark, &end_mark))
2318 return;
2320 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2322 /* all that gets pushed are deletes hence little space is used on the stack */
2324 edit_push_markers (edit);
2326 if (edit->column_highlight)
2328 col_delta = abs (edit->column2 - edit->column1);
2329 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2331 else
2333 while (size--)
2334 edit_insert_ahead (edit, copy_buf[size]);
2337 g_free (copy_buf);
2338 edit_scroll_screen_over_cursor (edit);
2340 if (edit->column_highlight)
2341 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2342 else if (start_mark < current && end_mark > current)
2343 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2345 edit->force |= REDRAW_PAGE;
2349 /* --------------------------------------------------------------------------------------------- */
2351 void
2352 edit_block_move_cmd (WEdit * edit)
2354 long current;
2355 unsigned char *copy_buf = NULL;
2356 long start_mark, end_mark;
2357 long line;
2359 if (eval_marks (edit, &start_mark, &end_mark))
2360 return;
2362 line = edit->curs_line;
2363 if (edit->mark2 < 0)
2364 edit_mark_cmd (edit, 0);
2365 edit_push_markers (edit);
2367 if (edit->column_highlight)
2369 long mark1, mark2;
2370 int size;
2371 int b_width = 0;
2372 int c1, c2;
2373 int x, x2;
2375 c1 = min (edit->column1, edit->column2);
2376 c2 = max (edit->column1, edit->column2);
2377 b_width = (c2 - c1);
2379 edit_update_curs_col (edit);
2381 x = edit->curs_col;
2382 x2 = x + edit->over_col;
2384 /* do nothing when cursor inside first line of selected area */
2385 if ((edit_eol (edit, edit->curs1) == edit_eol (edit, start_mark)) && (x2 > c1 && x2 <= c2))
2386 return;
2388 if (edit->curs1 > start_mark && edit->curs1 < edit_eol (edit, end_mark))
2390 if (x > c2)
2391 x -= b_width;
2392 else if (x > c1 && x <= c2)
2393 x = c1;
2395 /* save current selection into buffer */
2396 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2398 /* remove current selection */
2399 edit_block_delete_cmd (edit);
2401 edit->over_col = max (0, edit->over_col - b_width);
2402 /* calculate the cursor pos after delete block */
2403 current = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), x, 0);
2404 edit_cursor_move (edit, current - edit->curs1);
2405 edit_scroll_screen_over_cursor (edit);
2407 /* add TWS if need before block insertion */
2408 if (option_cursor_beyond_eol && edit->over_col > 0)
2409 edit_insert_over (edit);
2411 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2412 edit_set_markers (edit, mark1, mark2, c1, c2);
2414 else
2416 long count;
2418 current = edit->curs1;
2419 copy_buf = g_malloc0 (end_mark - start_mark);
2420 edit_cursor_move (edit, start_mark - edit->curs1);
2421 edit_scroll_screen_over_cursor (edit);
2422 count = start_mark;
2423 while (count < end_mark)
2425 copy_buf[end_mark - count - 1] = edit_delete (edit, 1);
2426 count++;
2428 edit_scroll_screen_over_cursor (edit);
2429 edit_cursor_move (edit,
2430 current - edit->curs1 -
2431 (((current - edit->curs1) > 0) ? end_mark - start_mark : 0));
2432 edit_scroll_screen_over_cursor (edit);
2433 while (count-- > start_mark)
2434 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2435 edit_set_markers (edit, edit->curs1, edit->curs1 + end_mark - start_mark, 0, 0);
2438 edit_scroll_screen_over_cursor (edit);
2439 g_free (copy_buf);
2440 edit->force |= REDRAW_PAGE;
2443 /* --------------------------------------------------------------------------------------------- */
2444 /** returns 1 if canceelled by user */
2447 edit_block_delete_cmd (WEdit * edit)
2449 long start_mark, end_mark;
2450 if (eval_marks (edit, &start_mark, &end_mark))
2452 edit_delete_line (edit);
2453 return 0;
2455 return edit_block_delete (edit);
2458 /* --------------------------------------------------------------------------------------------- */
2459 /** call with edit = 0 before shutdown to close memory leaks */
2461 void
2462 edit_replace_cmd (WEdit * edit, int again)
2464 /* 1 = search string, 2 = replace with */
2465 static char *saved1 = NULL; /* saved default[123] */
2466 static char *saved2 = NULL;
2467 char *input1 = NULL; /* user input from the dialog */
2468 char *input2 = NULL;
2469 char *disp1 = NULL;
2470 char *disp2 = NULL;
2471 long times_replaced = 0;
2472 gboolean once_found = FALSE;
2474 if (!edit)
2476 g_free (saved1), saved1 = NULL;
2477 g_free (saved2), saved2 = NULL;
2478 return;
2481 edit->force |= REDRAW_COMPLETELY;
2483 if (again && !saved1 && !saved2)
2484 again = 0;
2486 if (again)
2488 input1 = g_strdup (saved1 ? saved1 : "");
2489 input2 = g_strdup (saved2 ? saved2 : "");
2491 else
2493 char *tmp_inp1, *tmp_inp2;
2494 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2495 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2497 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2499 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2501 g_free (disp1);
2502 g_free (disp2);
2504 if (input1 == NULL || *input1 == '\0')
2506 edit->force = REDRAW_COMPLETELY;
2507 goto cleanup;
2510 tmp_inp1 = input1;
2511 tmp_inp2 = input2;
2512 input1 = edit_replace_cmd__conv_to_input (input1);
2513 input2 = edit_replace_cmd__conv_to_input (input2);
2514 g_free (tmp_inp1);
2515 g_free (tmp_inp2);
2517 g_free (saved1), saved1 = g_strdup (input1);
2518 g_free (saved2), saved2 = g_strdup (input2);
2520 mc_search_free (edit->search);
2521 edit->search = NULL;
2525 if (!edit->search)
2527 edit->search = mc_search_new (input1, -1);
2528 if (edit->search == NULL)
2530 edit->search_start = edit->curs1;
2531 goto cleanup;
2533 edit->search->search_type = edit_search_options.type;
2534 edit->search->is_all_charsets = edit_search_options.all_codepages;
2535 edit->search->is_case_sensitive = edit_search_options.case_sens;
2536 edit->search->whole_words = edit_search_options.whole_words;
2537 edit->search->search_fn = edit_search_cmd_callback;
2538 edit->search_line_type = edit_get_search_line_type (edit->search);
2539 edit_search_fix_search_start_if_selection (edit);
2542 if (edit->found_len && edit->search_start == edit->found_start + 1
2543 && edit_search_options.backwards)
2544 edit->search_start--;
2546 if (edit->found_len && edit->search_start == edit->found_start - 1
2547 && !edit_search_options.backwards)
2548 edit->search_start++;
2552 gsize len = 0;
2554 if (!editcmd_find (edit, &len))
2556 if (!(edit->search->error == MC_SEARCH_E_OK ||
2557 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2559 edit_error_dialog (_("Search"), edit->search->error_str);
2561 break;
2563 once_found = TRUE;
2565 edit->search_start = edit->search->normal_offset;
2566 /*returns negative on not found or error in pattern */
2568 if ((edit->search_start >= 0) && (edit->search_start < edit->last_byte))
2570 gsize i;
2571 GString *tmp_str, *repl_str;
2573 edit->found_start = edit->search_start;
2574 i = edit->found_len = len;
2576 edit_cursor_move (edit, edit->search_start - edit->curs1);
2577 edit_scroll_screen_over_cursor (edit);
2579 if (edit->replace_mode == 0)
2581 int l;
2582 int prompt;
2584 l = edit->curs_row - edit->widget.lines / 3;
2585 if (l > 0)
2586 edit_scroll_downward (edit, l);
2587 if (l < 0)
2588 edit_scroll_upward (edit, -l);
2590 edit_scroll_screen_over_cursor (edit);
2591 edit->force |= REDRAW_PAGE;
2592 edit_render_keypress (edit);
2594 /*so that undo stops at each query */
2595 edit_push_key_press (edit);
2596 /* and prompt 2/3 down */
2597 disp1 = edit_replace_cmd__conv_to_display (saved1);
2598 disp2 = edit_replace_cmd__conv_to_display (saved2);
2599 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2600 g_free (disp1);
2601 g_free (disp2);
2603 if (prompt == B_REPLACE_ALL)
2604 edit->replace_mode = 1;
2605 else if (prompt == B_SKIP_REPLACE)
2607 if (edit_search_options.backwards)
2608 edit->search_start--;
2609 else
2610 edit->search_start++;
2611 continue; /* loop */
2613 else if (prompt == B_CANCEL)
2615 edit->replace_mode = -1;
2616 break; /* loop */
2620 /* don't process string each time */
2621 tmp_str = g_string_new (input2);
2622 repl_str = mc_search_prepare_replace_str (edit->search, tmp_str);
2623 g_string_free (tmp_str, TRUE);
2625 if (edit->search->error != MC_SEARCH_E_OK)
2627 edit_error_dialog (_("Replace"), edit->search->error_str);
2628 g_string_free (repl_str, TRUE);
2629 break;
2632 /* delete then insert new */
2633 for (i = 0; i < len; i++)
2634 edit_delete (edit, 1);
2636 for (i = 0; i < repl_str->len; i++)
2637 edit_insert (edit, repl_str->str[i]);
2639 edit->found_len = repl_str->len;
2640 g_string_free (repl_str, TRUE);
2641 times_replaced++;
2643 /* so that we don't find the same string again */
2644 if (edit_search_options.backwards)
2646 edit->search_start--;
2648 else
2650 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2652 if (edit->search_start >= edit->last_byte)
2653 break;
2656 edit_scroll_screen_over_cursor (edit);
2658 else
2660 /* try and find from right here for next search */
2661 edit->search_start = edit->curs1;
2662 edit_update_curs_col (edit);
2664 edit->force |= REDRAW_PAGE;
2665 edit_render_keypress (edit);
2667 if (times_replaced == 0)
2668 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2669 break;
2672 while (edit->replace_mode >= 0);
2674 edit_scroll_screen_over_cursor (edit);
2675 edit->force |= REDRAW_COMPLETELY;
2676 edit_render_keypress (edit);
2678 if ((edit->replace_mode == 1) && (times_replaced != 0))
2679 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2681 cleanup:
2682 g_free (input1);
2683 g_free (input2);
2686 /* --------------------------------------------------------------------------------------------- */
2689 edit_search_cmd_callback (const void *user_data, gsize char_offset)
2691 return edit_get_byte ((WEdit *) user_data, (long) char_offset);
2694 /* --------------------------------------------------------------------------------------------- */
2696 void
2697 edit_search_cmd (WEdit * edit, gboolean again)
2700 if (edit == NULL)
2701 return;
2703 if (!again)
2704 edit_search (edit);
2705 else if (edit->last_search_string != NULL)
2706 edit_do_search (edit);
2707 else
2709 /* find last search string in history */
2710 GList *history;
2712 history = history_get (MC_HISTORY_SHARED_SEARCH);
2713 if (history != NULL && history->data != NULL)
2715 edit->last_search_string = (char *) history->data;
2716 history->data = NULL;
2717 history = g_list_first (history);
2718 g_list_foreach (history, (GFunc) g_free, NULL);
2719 g_list_free (history);
2721 edit->search = mc_search_new (edit->last_search_string, -1);
2722 if (edit->search == NULL)
2724 /* if not... then ask for an expression */
2725 g_free (edit->last_search_string);
2726 edit->last_search_string = NULL;
2727 edit_search (edit);
2729 else
2731 edit->search->search_type = edit_search_options.type;
2732 edit->search->is_all_charsets = edit_search_options.all_codepages;
2733 edit->search->is_case_sensitive = edit_search_options.case_sens;
2734 edit->search->whole_words = edit_search_options.whole_words;
2735 edit->search->search_fn = edit_search_cmd_callback;
2736 edit->search_line_type = edit_get_search_line_type (edit->search);
2737 edit_do_search (edit);
2740 else
2742 /* if not... then ask for an expression */
2743 g_free (edit->last_search_string);
2744 edit->last_search_string = NULL;
2745 edit_search (edit);
2751 /* --------------------------------------------------------------------------------------------- */
2753 * Check if it's OK to close the editor. If there are unsaved changes,
2754 * ask user. Return 1 if it's OK to exit, 0 to continue editing.
2757 gboolean
2758 edit_ok_to_exit (WEdit * edit)
2760 int act;
2762 if (!edit->modified)
2763 return TRUE;
2765 if (!mc_global.midnight_shutdown)
2767 if (!edit_check_newline (edit))
2768 return FALSE;
2770 query_set_sel (2);
2771 act = edit_query_dialog3 (_("Quit"), _("File was modified. Save with exit?"),
2772 _("&Yes"), _("&No"), _("&Cancel quit"));
2774 else
2776 act =
2777 edit_query_dialog2 (_("Quit"),
2778 _("Midnight Commander is being shut down.\nSave modified file?"),
2779 _("&Yes"), _("&No"));
2781 /* Esc is No */
2782 if (act == -1)
2783 act = 1;
2786 switch (act)
2788 case 0: /* Yes */
2789 edit_push_markers (edit);
2790 edit_set_markers (edit, 0, 0, 0, 0);
2791 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2792 return mc_global.midnight_shutdown;
2793 break;
2794 case 1: /* No */
2795 break;
2796 case 2: /* Cancel quit */
2797 case -1: /* Esc */
2798 return FALSE;
2801 return TRUE;
2804 /* --------------------------------------------------------------------------------------------- */
2805 /** save block, returns 1 on success */
2808 edit_save_block (WEdit * edit, const char *filename, long start, long finish)
2810 int len, file;
2811 vfs_path_t *vpath;
2813 vpath = vfs_path_from_str (filename);
2814 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2815 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2816 vfs_path_free (vpath);
2817 if (file == -1)
2818 return 0;
2820 if (edit->column_highlight)
2822 int r;
2824 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2825 if (r > 0)
2827 unsigned char *block, *p;
2829 p = block = edit_get_block (edit, start, finish, &len);
2830 while (len)
2832 r = mc_write (file, p, len);
2833 if (r < 0)
2834 break;
2835 p += r;
2836 len -= r;
2838 g_free (block);
2841 else
2843 unsigned char *buf;
2844 int i = start, end;
2846 len = finish - start;
2847 buf = g_malloc0 (TEMP_BUF_LEN);
2848 while (start != finish)
2850 end = min (finish, start + TEMP_BUF_LEN);
2851 for (; i < end; i++)
2852 buf[i - start] = edit_get_byte (edit, i);
2853 len -= mc_write (file, (char *) buf, end - start);
2854 start = end;
2856 g_free (buf);
2858 mc_close (file);
2859 if (len)
2860 return 0;
2861 return 1;
2864 /* --------------------------------------------------------------------------------------------- */
2866 void
2867 edit_paste_from_history (WEdit * edit)
2869 (void) edit;
2870 edit_error_dialog (_("Error"), _("This function is not implemented"));
2873 /* --------------------------------------------------------------------------------------------- */
2876 edit_copy_to_X_buf_cmd (WEdit * edit)
2878 long start_mark, end_mark;
2879 if (eval_marks (edit, &start_mark, &end_mark))
2880 return 0;
2881 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2883 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2884 return 1;
2886 /* try use external clipboard utility */
2887 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2889 return 0;
2892 /* --------------------------------------------------------------------------------------------- */
2895 edit_cut_to_X_buf_cmd (WEdit * edit)
2897 long start_mark, end_mark;
2898 if (eval_marks (edit, &start_mark, &end_mark))
2899 return 0;
2900 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2902 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2903 return 1;
2905 /* try use external clipboard utility */
2906 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2908 edit_block_delete_cmd (edit);
2909 edit_mark_cmd (edit, 1);
2910 return 0;
2913 /* --------------------------------------------------------------------------------------------- */
2915 void
2916 edit_paste_from_X_buf_cmd (WEdit * edit)
2918 vfs_path_t *tmp;
2920 /* try use external clipboard utility */
2921 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
2922 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
2923 edit_insert_file (edit, tmp);
2924 vfs_path_free (tmp);
2928 /* --------------------------------------------------------------------------------------------- */
2930 * Ask user for the line and go to that line.
2931 * Negative numbers mean line from the end (i.e. -1 is the last line).
2934 void
2935 edit_goto_cmd (WEdit * edit)
2937 char *f;
2938 static long line = 0; /* line as typed, saved as default */
2939 long l;
2940 char *error;
2941 char s[32];
2943 g_snprintf (s, sizeof (s), "%ld", line);
2944 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "");
2945 if (!f)
2946 return;
2948 if (!*f)
2950 g_free (f);
2951 return;
2954 l = strtol (f, &error, 0);
2955 if (*error)
2957 g_free (f);
2958 return;
2961 line = l;
2962 if (l < 0)
2963 l = edit->total_lines + l + 2;
2964 edit_move_display (edit, l - edit->widget.lines / 2 - 1);
2965 edit_move_to_line (edit, l - 1);
2966 edit->force |= REDRAW_COMPLETELY;
2967 g_free (f);
2971 /* --------------------------------------------------------------------------------------------- */
2972 /** Return 1 on success */
2975 edit_save_block_cmd (WEdit * edit)
2977 long start_mark, end_mark;
2978 char *exp, *tmp;
2980 if (eval_marks (edit, &start_mark, &end_mark))
2981 return 1;
2983 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
2984 exp =
2985 input_expand_dialog (_("Save block"), _("Enter file name:"),
2986 MC_HISTORY_EDIT_SAVE_BLOCK, tmp);
2987 g_free (tmp);
2988 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2989 if (exp)
2991 if (!*exp)
2993 g_free (exp);
2994 return 0;
2996 else
2998 if (edit_save_block (edit, exp, start_mark, end_mark))
3000 g_free (exp);
3001 edit->force |= REDRAW_COMPLETELY;
3002 return 1;
3004 else
3006 g_free (exp);
3007 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3011 edit->force |= REDRAW_COMPLETELY;
3012 return 0;
3016 /* --------------------------------------------------------------------------------------------- */
3017 /** returns TRUE on success */
3019 gboolean
3020 edit_insert_file_cmd (WEdit * edit)
3022 char *tmp;
3023 char *exp;
3024 gboolean ret = FALSE;
3026 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3027 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3028 MC_HISTORY_EDIT_INSERT_FILE, tmp);
3029 g_free (tmp);
3031 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3033 if (exp != NULL && *exp != '\0')
3035 vfs_path_t *exp_vpath;
3037 exp_vpath = vfs_path_from_str (exp);
3038 ret = (edit_insert_file (edit, exp_vpath) >= 0);
3039 vfs_path_free (exp_vpath);
3041 if (!ret)
3042 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3045 g_free (exp);
3047 edit->force |= REDRAW_COMPLETELY;
3048 return ret;
3051 /* --------------------------------------------------------------------------------------------- */
3052 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3055 edit_sort_cmd (WEdit * edit)
3057 static char *old = 0;
3058 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3059 long start_mark, end_mark;
3060 int e;
3062 if (eval_marks (edit, &start_mark, &end_mark))
3064 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3065 return 0;
3068 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3069 edit_save_block (edit, tmp, start_mark, end_mark);
3070 g_free (tmp);
3072 exp = input_dialog (_("Run sort"),
3073 _("Enter sort options (see manpage) separated by whitespace:"),
3074 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "");
3076 if (!exp)
3077 return 1;
3078 g_free (old);
3079 old = exp;
3080 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3081 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3082 tmp =
3083 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3084 " > ", tmp_edit_temp_name, (char *) NULL);
3085 g_free (tmp_edit_temp_name);
3086 g_free (tmp_edit_block_name);
3088 e = system (tmp);
3089 g_free (tmp);
3090 if (e)
3092 if (e == -1 || e == 127)
3094 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3096 else
3098 char q[8];
3099 sprintf (q, "%d ", e);
3100 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3101 edit_error_dialog (_("Sort"), tmp);
3102 g_free (tmp);
3104 return -1;
3107 edit->force |= REDRAW_COMPLETELY;
3109 if (edit_block_delete_cmd (edit))
3110 return 1;
3113 vfs_path_t *tmp_vpath;
3115 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3116 edit_insert_file (edit, tmp_vpath);
3117 vfs_path_free (tmp_vpath);
3119 return 0;
3122 /* --------------------------------------------------------------------------------------------- */
3124 * Ask user for a command, execute it and paste its output back to the
3125 * editor.
3129 edit_ext_cmd (WEdit * edit)
3131 char *exp, *tmp, *tmp_edit_temp_file;
3132 int e;
3134 exp =
3135 input_dialog (_("Paste output of external command"),
3136 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL);
3138 if (!exp)
3139 return 1;
3141 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3142 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3143 g_free (tmp_edit_temp_file);
3144 e = system (tmp);
3145 g_free (tmp);
3146 g_free (exp);
3148 if (e)
3150 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3151 return -1;
3154 edit->force |= REDRAW_COMPLETELY;
3157 vfs_path_t *tmp_vpath;
3159 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3160 edit_insert_file (edit, tmp_vpath);
3161 vfs_path_free (tmp_vpath);
3163 return 0;
3166 /* --------------------------------------------------------------------------------------------- */
3167 /** if block is 1, a block must be highlighted and the shell command
3168 processes it. If block is 0 the shell command is a straight system
3169 command, that just produces some output which is to be inserted */
3171 void
3172 edit_block_process_cmd (WEdit * edit, int macro_number)
3174 char *fname;
3175 char *macros_fname = NULL;
3177 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3178 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3179 user_menu (edit, macros_fname, 0);
3180 g_free (fname);
3181 g_free (macros_fname);
3182 edit->force |= REDRAW_COMPLETELY;
3185 /* --------------------------------------------------------------------------------------------- */
3187 void
3188 edit_mail_dialog (WEdit * edit)
3190 char *tmail_to;
3191 char *tmail_subject;
3192 char *tmail_cc;
3194 static char *mail_cc_last = 0;
3195 static char *mail_subject_last = 0;
3196 static char *mail_to_last = 0;
3198 QuickWidget quick_widgets[] = {
3199 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
3200 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
3201 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input", &tmail_cc),
3202 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT, N_("Copies to")),
3203 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-2",
3204 &tmail_subject),
3205 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT, N_("Subject")),
3206 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-3", &tmail_to),
3207 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT, N_("To")),
3208 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT, N_("mail -s <subject> -c <cc> <to>")),
3209 QUICK_END
3212 QuickDialog Quick_input = {
3213 50, MAIL_DLG_HEIGHT, -1, -1, N_("Mail"),
3214 "[Input Line Keys]", quick_widgets, NULL, FALSE
3217 quick_widgets[2].u.input.text = mail_cc_last ? mail_cc_last : "";
3218 quick_widgets[4].u.input.text = mail_subject_last ? mail_subject_last : "";
3219 quick_widgets[6].u.input.text = mail_to_last ? mail_to_last : "";
3221 if (quick_dialog (&Quick_input) != B_CANCEL)
3223 g_free (mail_cc_last);
3224 g_free (mail_subject_last);
3225 g_free (mail_to_last);
3226 mail_cc_last = tmail_cc;
3227 mail_subject_last = tmail_subject;
3228 mail_to_last = tmail_to;
3229 pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
3234 /*******************/
3235 /* Word Completion */
3236 /*******************/
3238 /* --------------------------------------------------------------------------------------------- */
3240 * Complete current word using regular expression search
3241 * backwards beginning at the current cursor position.
3244 void
3245 edit_complete_word_cmd (WEdit * edit)
3247 gsize i, max_len, word_len = 0, num_compl = 0;
3248 long word_start = 0;
3249 unsigned char *bufpos;
3250 char *match_expr;
3251 struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
3253 /* search start of word to be completed */
3254 if (!edit_find_word_start (edit, &word_start, &word_len))
3255 return;
3257 /* prepare match expression */
3258 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3260 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3261 match_expr =
3262 g_strdup_printf
3263 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3264 (int) word_len, bufpos);
3266 /* collect the possible completions */
3267 /* start search from begin to end of file */
3268 max_len =
3269 edit_collect_completions (edit, word_start, word_len, match_expr,
3270 (struct selection *) &compl, &num_compl);
3272 if (num_compl > 0)
3274 /* insert completed word if there is only one match */
3275 if (num_compl == 1)
3277 for (i = word_len; i < compl[0].len; i++)
3278 edit_insert (edit, *(compl[0].text + i));
3280 /* more than one possible completion => ask the user */
3281 else
3283 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3284 /* !!! pressed again the selection dialog pops up, but that !!! */
3285 /* !!! seems to require a further internal state !!! */
3286 /*tty_beep (); */
3288 /* let the user select the preferred completion */
3289 editcmd_dialog_completion_show (edit, max_len, word_len,
3290 (struct selection *) &compl, num_compl);
3294 g_free (match_expr);
3295 /* release memory before return */
3296 for (i = 0; i < num_compl; i++)
3297 g_free (compl[i].text);
3300 /* --------------------------------------------------------------------------------------------- */
3302 void
3303 edit_select_codepage_cmd (WEdit * edit)
3305 #ifdef HAVE_CHARSET
3306 if (do_select_codepage ())
3307 edit_set_codeset (edit);
3309 edit->force = REDRAW_COMPLETELY;
3310 edit_refresh_cmd (edit);
3311 #else
3312 (void) edit;
3313 #endif
3316 /* --------------------------------------------------------------------------------------------- */
3318 void
3319 edit_insert_literal_cmd (WEdit * edit)
3321 int char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3322 _("Press any key:"), 0);
3323 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3326 /* --------------------------------------------------------------------------------------------- */
3328 void
3329 edit_begin_end_macro_cmd (WEdit * edit)
3331 /* edit is a pointer to the widget */
3332 if (edit != NULL)
3334 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3335 edit_execute_key_command (edit, command, -1);
3339 /* --------------------------------------------------------------------------------------------- */
3341 void
3342 edit_begin_end_repeat_cmd (WEdit * edit)
3344 /* edit is a pointer to the widget */
3345 if (edit != NULL)
3347 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3348 edit_execute_key_command (edit, command, -1);
3352 /* --------------------------------------------------------------------------------------------- */
3355 edit_load_forward_cmd (WEdit * edit)
3357 if (edit->modified)
3359 if (edit_query_dialog2
3360 (_("Warning"),
3361 _("Current text was modified without a file save\n"
3362 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3364 edit->force |= REDRAW_COMPLETELY;
3365 return 0;
3368 if (edit_stack_iterator + 1 < MAX_HISTORY_MOVETO)
3370 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3372 return 1;
3374 edit_stack_iterator++;
3375 if (edit_history_moveto[edit_stack_iterator].filename_vpath)
3377 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3378 edit_history_moveto[edit_stack_iterator].line);
3379 return 0;
3381 else
3383 return 1;
3386 else
3388 return 1;
3392 /* --------------------------------------------------------------------------------------------- */
3395 edit_load_back_cmd (WEdit * edit)
3397 if (edit->modified)
3399 if (edit_query_dialog2
3400 (_("Warning"),
3401 _("Current text was modified without a file save\n"
3402 "Continue discards these changes"), _("C&ontinue"), _("&Cancel")))
3404 edit->force |= REDRAW_COMPLETELY;
3405 return 0;
3408 if (edit_stack_iterator > 0)
3410 edit_stack_iterator--;
3411 if (edit_history_moveto[edit_stack_iterator].filename_vpath)
3413 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3414 edit_history_moveto[edit_stack_iterator].line);
3415 return 0;
3417 else
3419 return 1;
3422 else
3424 return 1;
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 /* --------------------------------------------------------------------------------------------- */